PERCPU(9)
π PERCPU(9) Β· NAME π
okay so this is PERCPU(9) from the Kernel Developerβs Manual, and bestie itβs literally the per-CPU storage allocator page π
. It covers percpu, percpu_alloc, percpu_create, percpu_free, percpu_getref, percpu_putref, percpu_foreach, and percpu_foreach_xcall β the whole per-CPU storage allocator squad π«Ά.
β¨ SYNOPSIS β¨
#include <sys/percpu.h>
typedef void (*percpu_callback_t)(void *, void *, struct cpu_info *);
percpu_t *
percpu_alloc(size_t size);
percpu_t *
percpu_create(size_t size, percpu_callback_t ctor,
percpu_callback_t dtor, void *arg);
void
percpu_free(percpu_t *pc, size_t size);
void *
percpu_getref(percpu_t *pc);
void
percpu_putref(percpu_t *pc);
void
percpu_foreach(percpu_t *pc, percpu_callback_t cb, void *arg);
void
percpu_foreach_xcall(percpu_t *pc, u_int xcflags, percpu_callback_t cb,
void *arg);
πΈ DESCRIPTION πΈ
The machine-independent percpu interface gives kernel subsystems per-CPU, CPU-local memory reservations β¨. percpu_alloc(size) reserves on each CPU an independent memory region of size bytes that is local to that CPU, and returns a handle (percpu_t) to those regions π.
A thread may then ask for a pointer, p, to the region held by the percpu_t on the threadβs current CPU. Until the thread relinquishes the pointer, or voluntarily sleeps, the thread may read or write the region at p without causing interprocessor memory synchronization π₯.
π¦ FUNCTIONS π¦
percpu_alloc(size)β Call this in thread context to allocatesizebytes of local storage on each CPU. The storage is initialized with zeroes. Treat this as an expensive operation, ngl.percpu_alloc()returns a handle for the per-CPU storage π .percpu_create(size, ctor, dtor, arg)β Likepercpu_alloc(), but before any access to the storage on any CPU, arrange to call(*ctor)(p, arg, ci), wherepis the pointer to that CPUβs storage andciis thestruct cpu_info *for that CPU. This may happen synchronously inpercpu_create()or when a CPU is attached later. Further, arrange thatpercpu_free()will do the same with(*dtor)(p, arg, ci).
ctor and dtor MAY sleep, e.g. to allocate memory or to wait for users to drain before deallocating memory. Do not rely on any particular order of iteration over the CPUs β¨.
percpu_free(pc, size)β Call this in thread context to return to the system the per-CPU storage held bypc.sizeshould match the size passed topercpu_alloc()orpercpu_create(). Whenpercpu_free()returns,pcis undefined. Treat this as an expensive operation πΈ.percpu_getref(pc)β Disable preemption and return a pointer to the storage held bypcon the local CPU. Usepercpu_getref()in either thread or interrupt context. Follow eachpercpu_getref()call with a matching call topercpu_putref().
π¨ Caller MUST NOT sleep afterpercpu_getref(), not even on an adaptive lock, beforepercpu_putref(). Like, seriously. π
percpu_putref(pc)β Indicate that the thread is finished with the pointer returned by the matching call topercpu_getref(). Re-enables preemption π.percpu_foreach(pc, cb, arg)β For each CPU, withcibeing the correspondingstruct cpu_info *andpthe CPU-local storage held bypc, run(*cb)(p, arg, ci). The call tocbruns in the current thread; usepercpu_foreach_xcall()to execute the call tocbon the remote CPUs.
Must be used in thread context. cb MUST NOT sleep except on adaptive locks, and should be fast. Do not rely on any particular order of iteration over the CPUs π.
percpu_foreach_xcall(pc, xcflags, cb, arg)β Likepercpu_foreach(), except the call tocbexecutes on the remote CPUs. Thexcflagsargument specifies how the cross calls are invoked; seexc_broadcast(9). The same caveats and restrictions that apply topercpu_foreach()also apply topercpu_foreach_xcall()β.
π CODE REFERENCES π
The percpu interface is implemented within the file sys/kern/subr_percpu.c π.
π SEE ALSO π
atomic_ops(3)kmem(9)pcq(9)pool_cache(9)xcall(9)
π HISTORY π
The percpu interface first appeared in NetBSD 6.0 π°οΈ.
π AUTHORS π
YAMAMOTO Takashi <yamt@NetBSD.org> π«Ά
π This page came from the Debian build, dated February 7, 2020, and its manual name is PERCPU(9) β slay, periodt. π
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2010 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms