Skip to content

Day 91 - Linux SLUB Allocator Internals

Objective

Understand how the Linux SLUB allocator manages fixed-size kernel objects on top of the Buddy Allocator, and how kmalloc() achieves efficient small-object allocation through object caches.


Topics

  • Why SLUB
  • Why Buddy Allocator alone is insufficient
  • SLAB Allocator evolution (SLAB → SLUB)
  • kmem_cache
  • slab
  • Object layout
  • Bitmap-based object management
  • Object allocation
  • Object free
  • Object reuse
  • Buddy Allocator integration
  • alloc_pages() and page_address()
  • Relationship between struct page and slab memory

Implementation

Implemented a simplified Linux-style SLUB allocator.

Core components:

  • struct kmem_cache
  • struct slab
  • Bitmap-based object allocation
  • Fixed-size object management
  • Slab linked list
  • Automatic slab creation
  • Object reuse after free
  • Integration with the Buddy Allocator

The allocator uses one page as the backing storage for each slab. A slab requests one page from the Buddy Allocator through alloc_pages(), converts the returned page descriptor into usable memory using page_address(), and divides the page into fixed-size objects.


Labs

Lab1 - SLUB Cache Initialization

Implemented:

  • kmem_cache_create()
  • slab_create()
  • Bitmap initialization
  • First slab creation
  • Cache and slab dump helpers

Verified:

  • Cache creation
  • Slab initialization
  • Object layout
  • Bitmap initialization

Lab2 - Allocation Policy

Implemented:

  • Object allocation
  • Existing slab reuse
  • Automatic slab creation when full
  • Independent cache instances

Verified:

  • Existing slab reuse
  • New slab creation after the current slab becomes full
  • Independent slab lists for different caches

Lab3 - Object Free and Reuse

Implemented:

  • kmem_cache_free()
  • Bitmap update
  • Free object recycling

Verified:

  • Object free
  • Object reuse
  • Lowest-index free object allocation
  • Bitmap consistency after allocation and free

Lab4 - Buddy Allocator Integration

Integrated the SLUB allocator with the simplified Buddy Allocator.

Implemented:

  • One-page slab allocation using alloc_pages()
  • Page-to-memory translation using page_address()
  • Page release through free_pages()

Verified:

  • SLUB requests backing pages from the Buddy Allocator
  • Object memory resides inside Buddy-managed pages
  • Page release after cache destruction
  • Recursive Buddy merge after slab destruction

Summary

This chapter completed a simplified Linux-style SLUB allocator capable of managing fixed-size kernel objects.

Unlike the previous implementation that allocated slab memory using calloc(), slabs are now backed by physical pages provided by the Buddy Allocator. This demonstrates the relationship between Linux page allocation and object allocation:

kmalloc()
SLUB Allocator
Buddy Allocator
Physical Pages

The implementation demonstrates the complete allocation flow from kmem_cache to slab, then to struct page, and finally to page-backed object memory, closely matching the architecture of the Linux kernel memory allocator.