kzalloc(size, GFP_KERNEL). Understanding when to reach for other allocators — and which GFP flags to pass — is essential to writing correct, efficient kernel code.
Slab allocator
kmalloc / kzalloc for small objects up to page size. Physically contiguous, cache-aligned.vmalloc
Large, virtually contiguous mappings that do not need to be physically contiguous.
Page allocator
Direct page-level allocation with
alloc_pages / __get_free_pages for power-of-two page counts.Managed allocation
devm_kmalloc and friends free memory automatically when a device is unbound.GFP flags
All allocation APIs accept agfp_t bitmask that controls reclaim behavior, zone selection, and other attributes. The GFP acronym stands for get free pages, the underlying page-allocation function.
Common flag combinations
GFP_KERNEL
GFP_KERNEL
GFP_ATOMIC
GFP_ATOMIC
GFP_NOWAIT
GFP_NOWAIT
GFP_ATOMIC.GFP_DMA / GFP_DMA32
GFP_DMA / GFP_DMA32
ZONE_DMA or ZONE_DMA32 memory zone for hardware with limited addressing. Prefer the dma_alloc_* APIs over these flags for DMA buffers.Reclaim behavior modifiers
| Combination | Reclaim behavior |
|---|---|
GFP_KERNEL & ~__GFP_RECLAIM | No reclaim attempt at all — lightest weight |
GFP_NOWAIT | Wakes kswapd; no direct reclaim |
GFP_ATOMIC | No direct reclaim; uses emergency reserves |
GFP_KERNEL | Background and direct reclaim; standard behavior |
GFP_KERNEL | __GFP_NORETRY | Backs off after one round of reclaim; no OOM killer |
GFP_KERNEL | __GFP_RETRY_MAYFAIL | Retries hard; fails only if no progress possible |
GFP_KERNEL | __GFP_NOFAIL | Loops until success — dangerous for large orders |
Slab allocator
The slab allocator (kmalloc family) handles small to medium allocations up to roughly page size. Objects are physically contiguous and aligned to at least ARCH_KMALLOC_MINALIGN bytes. For power-of-two sizes the alignment is at least the size itself.
kmalloc
size bytes of physically contiguous, kernel-accessible memory. Returns a pointer to the allocation or NULL on failure.
Number of bytes to allocate. Should be smaller than page size; maximum is architecture and configuration dependent (typically 4 MB).
GFP flags controlling reclaim behavior and zone selection. Use
GFP_KERNEL from process context, GFP_ATOMIC from interrupt context.kzalloc
kmalloc but zeroes the allocated memory before returning. Prefer kzalloc over kmalloc to avoid uninitialized-memory bugs.
krealloc
kmalloc-allocated block. If new_size is zero, the block is freed and ZERO_SIZE_PTR is returned. The contents up to min(old_size, new_size) are preserved.
Pointer previously returned by
kmalloc, kzalloc, or krealloc. May be NULL, in which case this behaves like kmalloc.Desired new size in bytes.
GFP flags for any new allocation needed.
kfree
kmalloc, kzalloc, krealloc, or kmem_cache_alloc. Passing NULL is safe and is a no-op.
Array helpers
n elements each of size bytes. These helpers check for multiplication overflow, making them safer than kmalloc(n * size, flags).
vmalloc — virtually contiguous allocations
For large allocations that do not need to be physically contiguous,vmalloc maps a range of virtual address space backed by individual pages from the page allocator.
Number of bytes to allocate. No hard upper limit beyond available virtual address space and physical memory.
vmalloc always implies GFP_KERNEL semantics — it may sleep. Do not call it from atomic context.kvmalloc — best-effort contiguous
kvmalloc first tries kmalloc; if that fails it falls back to vmalloc. The returned memory may or may not be physically contiguous. Use kvfree to release memory from either path.
Page allocator
For allocations measured in pages (always power-of-two counts), use the page allocator directly.Allocate
2^order contiguous pages. Order 0 = one page (typically 4 KiB). Maximum practical order is MAX_ORDER (usually 10 or 11).Slab cache — kmem_cache
For high-volume allocation of identically sized objects, create a dedicated slab cache. This reduces fragmentation and enables per-object constructors.Managed allocations — devm_kmalloc
Device-managed allocations are freed automatically when the device is detached. This eliminates the need to track and free allocations in error paths and.remove callbacks.
The device whose lifetime governs this allocation. The memory is freed when
dev is unbound.Choosing the right allocator
Default choice
Use
kzalloc(size, GFP_KERNEL) for any small object (under ~4 KiB) from process context. The zeroing eliminates a class of initialization bugs.Atomic context
If the allocation occurs in an interrupt handler, softirq, or with a spinlock held, use
GFP_ATOMIC instead of GFP_KERNEL. Always provide a fallback for failure.Large allocations
For allocations larger than a page that do not require physical contiguity, use
vmalloc. If you are unsure about the size at compile time, use kvmalloc.Many identical objects
For a hot allocation path creating many identical structures, create a dedicated slab cache with
kmem_cache_create / KMEM_CACHE. This improves locality and reduces fragmentation.Driver probe/remove
In device drivers, use
devm_kzalloc so that memory is released automatically when the device is detached, simplifying error handling.