Skip to content

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:

Need: 64 Bytes

Buddy Allocator

↓

Allocate 1 Page

↓

4096 Bytes

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:

kmalloc-32

↓

32-byte objects
kmalloc-64

↓

64-byte objects

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.

11100000

means:

Object0  Used
Object1  Used
Object2  Used
Object3  Free
...

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:

Create new slab
        │
        ▼
alloc_pages(0)
        │
        ▼
page_address()
        │
        ▼
Initialize bitmap

Free Flow

kmem_cache_free()
        │
        ▼
Locate slab
        │
        ▼
Clear bitmap bit
        │
        ▼
Increase free_objects

The freed object becomes available for future allocations.


Relationship with Buddy Allocator

The Buddy Allocator and SLUB serve different purposes.

Buddy Allocator

↓

Allocates physical pages

↓

SLUB

↓

Allocates fixed-size kernel objects

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_cache manages 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.