PERCPU(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸŽ€ 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 allocate size bytes 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) β€” Like percpu_alloc(), but before any access to the storage on any CPU, arrange to call (*ctor)(p, arg, ci), where p is the pointer to that CPU’s storage and ci is the struct cpu_info * for that CPU. This may happen synchronously in percpu_create() or when a CPU is attached later. Further, arrange that percpu_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 by pc. size should match the size passed to percpu_alloc() or percpu_create(). When percpu_free() returns, pc is undefined. Treat this as an expensive operation πŸ’Έ.
  • percpu_getref(pc) β€” Disable preemption and return a pointer to the storage held by pc on the local CPU. Use percpu_getref() in either thread or interrupt context. Follow each percpu_getref() call with a matching call to percpu_putref().
🚨 Caller MUST NOT sleep after percpu_getref(), not even on an adaptive lock, before percpu_putref(). Like, seriously. 😭
  • percpu_putref(pc) β€” Indicate that the thread is finished with the pointer returned by the matching call to percpu_getref(). Re-enables preemption πŸŽ€.
  • percpu_foreach(pc, cb, arg) β€” For each CPU, with ci being the corresponding struct cpu_info * and p the CPU-local storage held by pc, run (*cb)(p, arg, ci). The call to cb runs in the current thread; use percpu_foreach_xcall() to execute the call to cb on 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) β€” Like percpu_foreach(), except the call to cb executes on the remote CPUs. The xcflags argument specifies how the cross calls are invoked; see xc_broadcast(9). The same caveats and restrictions that apply to percpu_foreach() also apply to percpu_foreach_xcall() ⭐.

πŸ” CODE REFERENCES πŸ”

The percpu interface is implemented within the file sys/kern/subr_percpu.c πŸ‘‘.


🌟 SEE ALSO 🌟


πŸ“œ 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