← Interview Prep

Linux Kernel — Memory Management

Allocating physical memory: the buddy allocator (orders, zones, compaction), slab/SLUB object caches, kmalloc vs vmalloc (contiguity & DMA), GFP flags (GFP_KERNEL vs GFP_ATOMIC), and pressure handling — watermarks, kswapd vs direct reclaim, overcommit, and the OOM killer.

How the kernel allocates physical memory to itself and to processes — the allocators, the flags that decide how you can allocate, and what happens under pressure. Original, interview-focused notes; builds on Memory Addressing.

Two layers: the buddy allocator hands out physical page frames in power-of-two blocks; the slab/slub allocator carves those pages into small kernel objects. Which one you hit depends on whether you call alloc_pages, kmalloc, or vmalloc.

The buddy allocator

Slab / SLUB — kernel object caches

Most kernel allocations are tiny and frequent (dentries, inodes, task_structs). Handing out whole pages would waste memory, so the slab allocator (SLUB is the modern default) keeps per-type caches of pre-carved objects on top of buddy pages — fast alloc/free, low fragmentation, cache-friendly. Inspect with slabtop / /proc/slabinfo. (This is SReclaimable/SUnreclaim in /proc/meminfo.)

kmalloc vs vmalloc

kmallocvmalloc
Backed bySlab → buddyNon-contiguous pages mapped contiguously in kernel VA
Physically contiguous?YesNo (virtually contiguous only)
SizeSmallLarge
ForMost allocations; DMA needs contiguousBig buffers where phys-contiguity doesn't matter

DMA-capable hardware usually needs physically contiguous memory → kmalloc / dma_alloc_coherent, not vmalloc.

GFP flags — the how of allocation

Picking the right flag is a real interview signal: "allocating in an interrupt handler? — GFP_ATOMIC, and handle failure."

Pressure: watermarks, kswapd, OOM

Likely interview questions

Kernel Internals series. Related: Memory Addressing · Page Frame Reclaiming.