A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Slab Allocator for Kernel Memory

Graduate Depth 97 in the knowledge graph ☐ I know this ☆ Set as goal
360prerequisites beneath it
See this on the map →
Buddy System Memory AllocationBoot Process and Kernel Initialization+1 more→
allocation kernel performance

Core Idea

The slab allocator pre-allocates memory in slabs (contiguous blocks containing multiple objects of the same type) to reduce allocation overhead. Each object type (inode, file descriptor, task structure, etc.) has its own cache of slabs. The allocator caches pre-constructed objects to reduce initialization cost and dramatically improves kernel memory allocation performance.

Explainer

From the buddy system, you know how the kernel can allocate and free memory in power-of-two-sized blocks while keeping fragmentation manageable through splitting and coalescing. But the buddy system has a problem: the kernel constantly allocates and frees small, identically-sized objects — a task_struct here, an inode there, a file descriptor over there — and the buddy system treats every allocation as a generic block request. It wastes memory on internal fragmentation (a 96-byte inode gets a 128-byte block), and it pays initialization costs every time because each freed block is returned to the pool as raw memory. The slab allocator solves both problems by recognizing that the kernel allocates the same types of objects over and over.

The design works in three layers: caches, slabs, and objects. A cache is created for each type of kernel object — there's a cache for inodes, a cache for dentry structures, a cache for task_struct, and so on. Each cache contains one or more slabs, where a slab is a contiguous chunk of memory (typically one or a few pages obtained from the buddy system) that has been pre-divided into slots exactly the right size for that object type. When the kernel needs a new inode, it asks the inode cache, which hands back an already-sized slot from a slab. No searching, no splitting, no size rounding — just grab the next free slot.

The key insight that makes slab allocation fast is object caching. When an object is freed, it isn't destroyed — it's returned to the slab in a pre-initialized state, ready to be handed out again. Many kernel objects require expensive initialization (setting up internal locks, zeroing fields, linking pointers), and this initialization is identical every time. By keeping freed objects constructed, the allocator avoids redoing that setup work on each allocation. Think of it like a restaurant that washes and resets table settings after each customer rather than buying new plates every time. The plates are "freed" but remain ready to use.

Slabs within a cache exist in three states: full (all object slots occupied), partial (some slots free), and empty (all slots free). The allocator satisfies requests from partial slabs first, falling back to empty slabs, and only requesting new pages from the buddy system when no empty slabs remain. This layered approach means the slab allocator and the buddy system work together: the buddy system handles large, coarse-grained page allocations, and the slab allocator handles the fine-grained, type-specific allocations that dominate kernel activity. The result is dramatically reduced fragmentation, faster allocation, and lower initialization overhead — all of which matter in an environment where millions of small objects are created and destroyed every second.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Assembly Language Basics → Memory Organization and Addressing → Memory Hierarchy → Memory Management Fundamentals → Contiguous Memory Allocation and Fragmentation → Buddy System Memory Allocation → Slab Allocator for Kernel Memory

Longest path: 98 steps · 360 total prerequisite topics

Prerequisites (3)

Leads To (0)

No topics depend on this one yet.