SLUB Allocator¶
Purpose¶
The SLUB allocator is the primary kernel object allocator in modern Linux.
Unlike the Buddy Allocator, which allocates physical memory pages, SLUB allocates fixed-size kernel objects by dividing one or more pages into equal-sized objects.
kmalloc() uses SLUB internally for most small memory allocations.
Why SLUB?¶
Allocating every kernel object directly from the Buddy Allocator would waste memory.
For example:
Most of the allocated page would remain unused.
SLUB improves memory utilization by storing many fixed-size objects inside a single page.
High-Level Architecture¶
kmalloc()
│
▼
+----------------+
| kmem_cache |
+----------------+
│
▼
+----------------+
| slab |
+----------------+
│
alloc_pages()
│
▼
+----------------+
| struct page |
+----------------+
│
page_address()
│
▼
+----------------+
| Object Memory |
+----------------+
Main Components¶
kmem_cache¶
A kmem_cache manages objects of one fixed size.
For example:
Each cache maintains its own linked list of slabs.
slab¶
A slab is one allocation unit managed by a cache.
Each slab contains:
- One page descriptor
- One page of backing memory
- Bitmap
- Object metadata
Objects¶
Each slab divides its backing page into equal-sized objects.
Example:
4096-byte Page
+----+----+----+----+----+
| 64 | 64 | 64 | 64 | ...|
+----+----+----+----+----+
Total Objects = 4096 / 64 = 64
Bitmap¶
One bit represents one object.
means:
SLUB searches the bitmap for a free object during allocation.
Allocation Flow¶
kmem_cache_alloc()
│
▼
Find slab with free objects
│
▼
Bitmap lookup
│
▼
Mark object allocated
│
▼
Return object pointer
If no slab has free objects:
Free Flow¶
The freed object becomes available for future allocations.
Relationship with Buddy Allocator¶
The Buddy Allocator and SLUB serve different purposes.
SLUB requests backing pages from the Buddy Allocator through alloc_pages(), converts the returned page descriptor into usable memory using page_address(), and divides the page into reusable kernel objects.
Key Points¶
- Buddy Allocator manages pages.
- SLUB manages fixed-size objects.
- Each
kmem_cachemanages one object size. - Each slab is backed by one or more pages.
- Bitmap tracks object allocation.
- Objects are reused after being freed.
kmalloc()is implemented on top of the SLUB allocator.