Day91 - Linux SLUB Allocator Internals¶
Overview¶
The SLUB allocator is the primary kernel object allocator used by modern Linux systems.
Unlike the Buddy Allocator, which manages physical memory pages, SLUB manages fixed-size kernel objects. It divides one or more pages into many equal-sized objects, allowing efficient allocation and reuse of frequently used kernel data structures.
In this lab, a simplified Linux-style SLUB allocator is implemented on top of the Buddy Allocator developed in Day90.
Learning Objectives¶
After completing this lab, you will understand:
- Why the Buddy Allocator alone is insufficient for small object allocation
- How SLUB manages fixed-size kernel objects
- The relationship between
kmem_cache,slab, and objects - Bitmap-based object allocation and reuse
- How SLUB integrates with the Buddy Allocator
- The allocation path from
kmalloc()to physical pages
Lab Architecture¶
kmem_cache
│
▼
+-----------------+
| struct slab |
+-----------------+
│ page descriptor │
│ bitmap │
│ object memory │
+-----------------+
│
alloc_pages(0)
│
▼
Buddy Allocator
Lab1 - SLUB Cache Initialization¶
Objective¶
Implement the basic SLUB data structures and initialize a cache.
Implemented Components¶
struct kmem_cachestruct slab- Bitmap initialization
- Slab creation
- Cache dump helper
Verification¶
- Cache creation
- Slab creation
- Object count calculation
- Bitmap initialization
Lab2 - Allocation Policy¶
Objective¶
Implement fixed-size object allocation and verify SLUB allocation behavior.
Implemented Components¶
- Object allocation
- Existing slab reuse
- Automatic slab creation
- Multiple cache support
Verification¶
- Existing slab reuse
- Slab expansion after becoming full
- Independent slab lists for different caches
Lab3 - Object Free and Reuse¶
Objective¶
Implement object free and verify object reuse.
Implemented Components¶
kmem_cache_free()- Bitmap update
- Free object reuse
Verification¶
- Freed object becomes available again
- Lowest-index free object is reused
- Bitmap consistency
Lab4 - Buddy Allocator Integration¶
Objective¶
Replace heap-backed slab memory with Buddy-managed pages.
Implemented Components¶
alloc_pages()page_address()free_pages()- Page-backed slab memory
Verification¶
- One page allocated for each slab
- Object memory located inside Buddy-managed pages
- Page released after cache destruction
- Buddy merge restored the original free block
Key Takeaways¶
- Buddy Allocator manages physical memory pages.
- SLUB manages fixed-size kernel objects.
- One slab typically occupies one or more pages.
- A slab divides page memory into equal-sized objects.
- Bitmap tracks object allocation status.
- Objects are reused after being freed.
- SLUB obtains backing pages from the Buddy Allocator through
alloc_pages(). page_address()converts a page descriptor into usable object memory.kmalloc()is built on top of the SLUB allocator.