Understanding Linux Memory Management: Bootmem, Memblock, Buddy Allocator, and Slab Allocator
This article provides a comprehensive overview of Linux memory management, detailing the roles and mechanisms of the early boot memory allocator, the memblock subsystem, the buddy allocator for physical pages, and the slab allocator for small objects, including their data structures, algorithms, and practical usage scenarios.
Linux memory management relies on a hierarchy of allocators that work together from system boot to runtime. The early boot memory allocator (bootmem) initializes memory regions, tracks reserved and usable pages, and provides simple bitmap allocation before the more advanced subsystems become available.
The memblock subsystem replaces bootmem for early boot allocations, offering flexible region handling, node-aware structures, and functions to add, remove, and merge memory regions. It maintains struct memblock_type and struct memblock_region data structures to describe memory blocks and supports operations such as memblock_add_range and memblock_remove_range .
For general physical page allocation, the buddy allocator organizes memory into power‑of‑two sized blocks called orders. It maintains free lists per order in each struct zone , uses the concept of page block partners to split and merge blocks, and applies watermarks (high, low, min) to decide when to borrow pages from other zones. Allocation proceeds by searching the appropriate order list, splitting larger blocks if necessary, while freeing merges buddies back together.
To efficiently allocate small objects and reduce internal fragmentation, the slab allocator (specifically the SLUB implementation) creates caches ( struct kmem_cache ) for each object type. Each cache has per‑CPU slabs ( struct kmem_cache_cpu ) for fast allocation, node‑wide partial lists ( struct kmem_cache_node ), and uses struct page to store objects. Allocation first checks the per‑CPU freelist, then the node partial list, and finally obtains new pages from the buddy allocator when needed.
These three allocators cooperate: bootmem and memblock set up the initial memory layout; the buddy allocator supplies pages to the slab allocator; and the slab allocator provides fast object allocation for kernel structures. This layered approach balances simplicity, speed, and low fragmentation, enabling Linux to handle diverse memory demands from large contiguous allocations to frequent small object allocations.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.