1.\" $NetBSD: percpu.9,v 1.15 2020/02/07 15:11:46 wiz Exp $ 2.\" 3.\" Copyright (c) 2010 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by David Young. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd February 7, 2020 31.Dt PERCPU 9 32.Os 33.Sh NAME 34.Nm percpu , 35.Nm percpu_alloc , 36.Nm percpu_create , 37.Nm percpu_free , 38.Nm percpu_getref , 39.Nm percpu_putref , 40.Nm percpu_foreach , 41.Nm percpu_foreach_xcall 42.Nd per-CPU storage allocator 43.Sh SYNOPSIS 44.In sys/percpu.h 45.Vt typedef void (*percpu_callback_t)(void *, void *, struct cpu_info *); 46.Ft percpu_t * 47.Fn percpu_alloc "size_t size" 48.Ft percpu_t * 49.Fn percpu_create "size_t size" "percpu_callback_t ctor" "percpu_callback_t dtor" "void *arg" 50.Ft void 51.Fn percpu_free "percpu_t *pc" "size_t size" 52.Ft void * 53.Fn percpu_getref "percpu_t *pc" 54.Ft void 55.Fn percpu_putref "percpu_t *pc" 56.Ft void 57.Fn percpu_foreach "percpu_t *pc" "percpu_callback_t cb" "void *arg" 58.Ft void 59.Fn percpu_foreach_xcall "percpu_t *pc" "u_int xcflags" "percpu_callback_t cb" "void *arg" 60.Sh DESCRIPTION 61The machine-independent 62.Nm 63interface provides per-CPU, CPU-local memory reservations to kernel 64subsystems. 65.Fo percpu_alloc 66.Fa size 67.Fc 68reserves on each CPU an independent memory region of 69.Fa size 70bytes that is local to that CPU, returning a handle 71.Po 72.Vt percpu_t 73.Pc 74to those regions. 75A thread may subsequently ask for a pointer, 76.Fa p , 77to the region held by the 78.Vt percpu_t 79on the thread's current CPU. 80Until the thread relinquishes the pointer, or voluntarily sleeps, 81the thread may read or write the region at 82.Fa p 83without causing interprocessor memory synchronization. 84.Sh FUNCTIONS 85.Bl -tag -width compact 86.It Fn percpu_alloc "size" 87Call this in thread context to allocate 88.Fa size 89bytes of local storage on each CPU. 90The storage is initialized with zeroes. 91Treat this as an expensive operation. 92.Fn percpu_alloc 93returns a handle for the per-CPU storage. 94.It Fn percpu_create "size" "ctor" "dtor" "arg" 95Like 96.Fn percpu_alloc , 97but before returning, for each CPU, call 98.Fn "(*ctor)" p arg ci 99in the current thread, where 100.Fa p 101is the pointer to that CPU's storage and 102.Fa ci 103is the 104.Vt "struct cpu_info *" 105for that CPU. 106Further, arrange that 107.Fn percpu_free 108will do the same with 109.Fn "(*dtor)" p arg ci . 110.Pp 111.Fa ctor 112and 113.Fa dtor 114.Em MAY 115sleep, e.g. to allocate memory or to wait for users to drain before 116deallocating memory. 117Do not rely on any particular order of iteration over the CPUs. 118.It Fn percpu_free "pc" "size" 119Call this in thread context to 120return to the system the per-CPU storage held by 121.Fa pc . 122.Fa size 123should match the 124.Fa size 125passed to 126.Fn percpu_alloc 127or 128.Fn percpu_create . 129When 130.Fn percpu_free 131returns, 132.Fa pc 133is undefined. 134Treat this as an expensive operation. 135.It Fn percpu_getref "pc" 136Disable preemption and return a pointer to the storage held by 137.Fa pc 138on the local CPU. 139Use 140.Fn percpu_getref 141in either thread or interrupt context. 142Follow each 143.Fn percpu_getref 144call with a matching call to 145.Fn percpu_putref . 146.Pp 147Caller 148.Em MUST NOT 149sleep after 150.Fn percpu_getref , 151not even on an adaptive lock, before 152.Fn percpu_putref . 153.It Fn percpu_putref "pc" 154Indicate that the thread is finished 155with the pointer returned by the matching 156call to 157.Fn percpu_getref . 158Re-enables preemption. 159.It Fn percpu_foreach "pc" "cb" "arg" 160For each CPU, with 161.Fa ci 162being the corresponding 163.Vt "struct cpu_info *" 164and 165.Fa "p" 166the CPU-local storage held by 167.Fa pc , 168run 169.Fo "(*cb)" 170.Fa "p" 171.Fa "arg" 172.Fa "ci" 173.Fc . 174The call to 175.Fa cb 176runs in the current thread; use 177.Fn percpu_foreach_xcall 178to execute the call to 179.Fa cb 180on the remote CPUs. 181.Pp 182Must be used in thread context. 183.Fa cb 184.Em MUST NOT 185sleep except on adaptive locks, and should be fast. 186Do not rely on any particular order of iteration over the CPUs. 187.It Fn percpu_foreach_xcall "pc" "xcflags" "cb" "arg" 188Like 189.Fn percpu_foreach , 190except the call to 191.Fa cb 192executes on the remote CPUs. 193The 194.Fa xcflags 195argument specifies how the cross calls are invoked; see 196.Xr xc_broadcast 9 . 197The same caveats and restrictions that apply to 198.Fn percpu_foreach 199also apply to 200.Fn percpu_foreach_xcall . 201.El 202.Sh CODE REFERENCES 203The 204.Nm 205interface is implemented within the file 206.Pa sys/kern/subr_percpu.c . 207.\" .Sh EXAMPLES 208.Sh SEE ALSO 209.Xr atomic_ops 3 , 210.Xr kmem 9 , 211.Xr pcq 9 , 212.Xr pool_cache 9 , 213.Xr xcall 9 214.Sh HISTORY 215The 216.Nm 217interface first appeared in 218.Nx 6.0 . 219.Sh AUTHORS 220.An YAMAMOTO Takashi Aq Mt yamt@NetBSD.org 221.\" .Sh CAVEATS 222.\" .Sh BUGS 223.\" .Sh SECURITY CONSIDERATIONS 224