kmalloc(9)
π kmalloc(9) β the Kernel API Manual tea ββ¨
okay so bestie, you're in the Kernel API Manual and today's main character is kmalloc, which is literally the girl who hands out memory in the kernel. sit down, grab a strawberry drink π, we're doing this.
πΈ NAME πΈ
kmalloc β allocate kernel memory π«Ά
β¨ SYNOPSIS β¨
void * kmalloc (size_t size , gfp_t flags );
π ARGUMENTS π
sizeβ how many bytes of memory are required. like, how much room does your girl need πflagsβ describe the allocation context. the vibe check for how the allocation should behave β¨
π¦ DESCRIPTION π¦
kmalloc is the normal method of allocating memory for objects smaller than page size in the kernel. no cap, this is the standard one, the go-to, the icon π
The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN bytes. and listenβfor a size of power of two bytes, the alignment is also guaranteed to be at least to the size. For other sizes, the alignment is guaranteed to be at least the largest power-of-two divisor of size. math ate, honestly π₯
The flags argument may be one of the GFP flags defined at include/linux/gfp_types.h and described at :ref:Documentation/core-api/mm-api.rst <mm-api-gfp-flags>``
The recommended usage of the flags is described at :ref:Documentation/core-api/memory-allocation.rst <memory_allocation>`` β go read it, i'm serious, it's giving required reading π
Below is a brief outline of the most useful GFP flags:
GFP_KERNELβ Allocate normal kernel ram. May sleep. π΄ the everyday queenGFP_NOWAITβ Allocation will not sleep. β‘GFP_ATOMICβ Allocation will not sleep. May use emergency pools. π¨
Also it is possible to set different flags by OR'ing in one or more of the following additional flags:
__GFP_ZEROβ Zero the allocated memory before returning. Also seekzalloc. π«§ clean slate energy__GFP_HIGHβ This allocation has high priority and may use emergency pools. π main character priority fr__GFP_NOFAILβ Indicate that this allocation is in no way allowed to fail (think twice before using). β οΈ bestie. bestie. think twice. i'm begging__GFP_NORETRYβ If memory is not immediately available, then give up at once. π she's not fighting for it__GFP_NOWARNβ If allocation fails, don't issue any warnings. π€«__GFP_RETRY_MAYFAILβ Try really hard to succeed the allocation but fail eventually. π she tries her best, and then, well
π SEE ALSO π
Kernel file ./include/linux/slab.h SLAB_HWCACHE_ALIGN(9), SLAB_TYPESAFE_BY_RCU(9), SLAB_ACCOUNT(9), SLAB_RECLAIM_ACCOUNT(9), struct kmem_cache_args(9), kmem_cache_create_usercopy(9), kmem_cache_create(9), krealloc_node_align(9), kmem_cache_alloc(9), kmem_cache_charge(9), kmalloc_nolock(9), __alloc_objs(9), __alloc_flex(9), kmalloc_obj(9), kmalloc_objs(9), kmalloc_flex(9), kmalloc_array(9), krealloc_array(9), kcalloc(9), kzalloc(9), kvmalloc_node(9), kvrealloc_node_align(9), kmalloc_size_roundup(9) π
the whole friend group, honestly. the way they all showed up to the function π
π§ friendly reminder: this page lives under ./include/linux and is dated October 2026 β so like, yes, it's from the future, and yes, it still slays. periodt. π
okay that's the whole page, go allocate something responsibly bestie πβ¨ bye!! π¦
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms