CPUMEM_GET(9)
πΈ CPUMEM_GET(9) πΈ
okay bestie, welcome to the Kernel Developer's Manual β this one's about per CPU memory allocations and it's honestly kind of iconic once you get the vibe. there's a whole fam of functions and macros here (cpumem_get, cpumem_put, cpumem_malloc, cpumem_malloc_ncpus, cpumem_free, CPUMEM_BOOT_MEMORY, CPUMEM_BOOT_INITIALIZER, cpumem_enter, cpumem_leave, cpumem_first, cpumem_next, CPUMEM_FOREACH) and we're gonna go through every single one, no skipping π
β¨ SYNOPSIS β¨
#include <sys/percpu.h>
struct cpumem *
cpumem_get(struct pool *pp);
void
cpumem_put(struct pool *pp, struct cpumem *cm);
struct cpumem *
cpumem_malloc(size_t sz, int type);
struct cpumem *
cpumem_malloc_ncpus(struct cpumem *cm, size_t sz, int type);
void
cpumem_free(struct cpumem *cm, int type, size_t sz);
CPUMEM_BOOT_MEMORY(NAME, size_t sz);
CPUMEM_BOOT_INITIALIZER(NAME);
void *
cpumem_enter(struct cpumem *cm);
void
cpumem_leave(struct cpumem *cm, void *m);
void *
cpumem_first(struct cpumem_iter *ci, struct cpumem *cm);
void *
cpumem_next(struct cpumem_iter *ci, struct cpumem *cm);
CPUMEM_FOREACH(VARNAME, struct cpumem_iter *ci, struct cpumem *cm);
π DESCRIPTION π
so the per CPU memory API is basically a cute little wrapper layer around allocating and accessing per CPU memory. that's the whole job π«Ά
plot twist: on uni-processor systems (like, when the kernel is not built with MULTIPROCESSOR defined) there's an alternate implementation of the API that adds literally zero overhead compared to just reaching straight into a data structure. so you can use the API everywhere and it won't tank performance on uni-processor systems. slay β¨
π Per CPU Memory Allocation and Deallocation π
cpumem_get()allocates memory for each CPU from thepppool. the memory gets zeroed on allocation, so it's giving clean slate energy.cpumem_put()returns each CPU's memory allocation referenced bycmback to thepppool.cpumem_malloc()allocatesszbytes oftypememory for each CPU usingmalloc(9). yep, zeroed on allocation too.cpumem_free()hands each CPU's memory allocation referenced bycmback to the system usingfree(9). β οΈ you must pass the same object size and type that you originally gave tocpumem_malloc()β that meansszandtyperespectively. no swapsies.cpumem_get()andcpumem_malloc()may only be used after all the CPUs in the system have been attached. that's the rule, periodt. but if you need per CPU memory during early boot, you can statically allocate a cpumem pointer and memory for the boot CPU instead.CPUMEM_BOOT_MEMORY()statically allocates memory for use on the boot CPU before the other CPUs in the system have been attached. the allocation is identified byNAME, and gives you the number of bytes specified by theszargument. the memory is initialised to zeros π§CPUMEM_BOOT_INITIALIZER()initialises a cpumem pointer with the memory that was previously allocated usingCPUMEM_BOOT_MEMORY()and identified byNAME. it's the glow-up step, basically.cpumem_malloc_ncpus()allocates additional memory for the CPUs that were attached during boot. the cpumem structurecmmust have been initialised withCPUMEM_BOOT_INITIALIZER()first β non-negotiable. the same number of bytes originally passed toCOUNTERS_BOOT_MEMORYmust be specified bysz. thetypeargument specifies the type of memory that the counters will be allocated as viamalloc(9). the contents of the memory on the boot CPU will be preserved, while the allocations for the additional CPU's will be zeroed on allocation π
π¨ big warning, bestie: per CPU memory that's been allocated withCPUMEM_BOOT_MEMORY()andcpumem_malloc_ncpus()cannot be deallocated withcpumem_free. any attempt to do so will lead to undefined behaviour. that's not a vibe check you want to fail π
π Per CPU Memory Access π
cpumem_enter()provides access to the current CPU's memory allocation referenced bycm.cpumem_leave()indicates the end of access to the current CPU's memory allocation referenced bycm. it's the little "we're done here" π«‘
π¦ Per CPU Memory Iterators π¦
cpumem_first()provides access to the first CPU's memory allocation referenced bycm. the iteratorcimay be used in subsequent calls tocpumem_next().cpumem_next()provides access to the next CPU's memory allocation referenced bycmandci.CPUMEM_FOREACH()is the macro that iterates over each CPU's memory allocation referenced bycmusing the iteratorci, settingVARNAMEto each CPU's allocation in turn. it's the main character of this section tbh π
β CONTEXT β
cpumem_get(),cpumem_put(),cpumem_malloc(),cpumem_free(), andcpumem_malloc_ncpus()may be called during autoconf, or from process context.cpumem_enter(),cpumem_leave(),cpumem_first(),cpumem_next(), andCPUMEM_FOREACH()may be called during autoconf, from process context, or from interrupt context.
π«Ά real talk though: the per CPU memory API does not provide any locking or serialisation of access to each CPU's memory allocation. it is up to you, the caller, to provide appropriate locking or serialisation around calls to these functions so you don't get concurrent access to the relevant data structures. no cap, that's on you π
π₯ RETURN VALUES π₯
cpumem_get(),cpumem_malloc(), andcpumem_malloc_ncpus()return an opaquecpumempointer that references each CPU's memory allocation.cpumem_enter()returns a reference to the current CPU's memory allocation.cpumem_first()returns a reference to the first CPU's memory allocation.cpumem_next()returns a reference to the next CPU's memory allocation according to the iteratorci, orNULLif the iterator has run out of CPUs. that's your cue to stop looping, bestie π
πΈ SEE ALSO πΈ
counters_alloc(9), malloc(9), pool_get(9) β go read these next, iykyk β¨
π§ HISTORY π§
the per CPU memory API first appeared in OpenBSD 6.1. a genuine icon from day one π
π AUTHORS π
the per CPU memory API was written by David Gwynne <dlg@openbsd.org>. king behaviour, honestly π«Ά
and that's the whole page, bestie β this one came from the Debian edition of the manual, dated August 27, 2020, from CPUMEM_GET(9) to the very last line. mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms