kmalloc(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– 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 queen
  • GFP_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 see kzalloc. 🫧 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