What You Will Learn
This lecture is part of our free Linux kernel programming course and free Linux device drivers course. By the end of this article, you will understand what the Linux kernel slab allocator is, why the kernel needs it in addition to the page allocator, how the modern SLUB allocator organizes memory internally, and how to inspect live slab cache statistics on a running system. This is foundational knowledge for anyone learning kernel memory management, driver development, or preparing for kernel internals interviews.
Prerequisites
Before starting this lecture, you should be comfortable with:
- Basic C programming (pointers, structures)
- General idea of what a Linux kernel module is
- Basic Linux command line usage
- Concept of virtual memory and pages (helpful but not mandatory)
If you are completely new to kernel programming, we recommend starting from the beginning of this free Linux kernel programming course before diving into memory allocators.
Topics Covered In This Lecture
SLUB allocator
kmalloc internals
Object caching
/proc/slabinfo
slabtop
Kernel memory management
Why the Linux Kernel Needs a Slab Allocator
The Linux kernel manages physical memory in fixed-size chunks called pages, typically 4 KB on most systems, using a component called the page allocator (also known as the buddy system allocator). This works well when a driver or subsystem needs a whole page or a small number of pages. The problem arises when the kernel needs something much smaller than a page — say, a 64-byte structure to represent an open file, or a 200-byte structure for a network packet buffer.
If every small allocation request consumed a full page, memory would be wasted on a massive scale. This wastage is called internal fragmentation. The Linux kernel slab allocator sits on top of the page allocator and solves exactly this problem by carving pages into small, conveniently sized, reusable chunks called objects.
The Object Caching Idea Behind the Slab Allocator
The core idea behind the Linux kernel slab allocator is object caching. Certain kernel data structures get allocated and freed extremely often while the system is running — think of network packet buffers, file descriptors, or process-related structures. Instead of repeatedly asking the page allocator for memory, initializing it, and later tearing it down, the slab allocator keeps a ready-made pool of these objects sitting in memory.
When a driver or kernel subsystem needs one of these objects, the slab allocator can usually hand it out almost instantly because the memory is already carved up and, in many cases, already partially initialized. When the object is freed, it goes back into the pool instead of being released to the page allocator, so it is ready for the next request. This design dramatically reduces allocation latency in performance-critical paths such as networking and block I/O.
SLAB, SLOB, and SLUB: A Quick History Lesson
If you are learning kernel memory management from older books, tutorials, or courses, you may come across references to three different slab allocator implementations: SLAB, SLOB, and SLUB. It is important to know that this is now outdated information for any current kernel.
| Allocator | Status in Modern Kernel | Notes |
|---|---|---|
| SLOB | Removed | Was aimed at very low-memory embedded systems; removed from mainline in the 6.4 kernel series. |
| SLAB | Removed | The original general-purpose allocator; deprecated and then removed from mainline starting with the 6.8 kernel series. |
| SLUB | Active — the only allocator | Now the sole slab allocator in the Linux kernel. All the API names such as kmalloc() stay the same for driver authors. |
Practical takeaway: On any current mainline Linux kernel you compile or work with today, there is only one slab allocator implementation under the hood — SLUB. You do not need to choose one at configuration time anymore. The good news for kernel module and device driver authors is that the public API (kmalloc(), kzalloc(), kfree(), and friends) has stayed stable across this transition, so code you write works the same regardless of which historical allocator a particular kernel used internally.
How the SLUB Slab Allocator Organizes Memory
The SLUB slab allocator keeps memory organized per CPU for speed, and falls back to shared per-node lists when needed. Understanding this layout helps when you are debugging memory issues or reading kernel crash dumps.
Per-CPU Active Slab
The slab a CPU is currently allocating from. Lock-free fast path.
Per-CPU Partial List
Backup slabs kept nearby to avoid touching shared node locks.
Per-Node Partial List
Shared across CPUs on the same NUMA node when local slabs run out.
Free objects inside a slab are tracked using a simple singly linked list where the “next free object” pointer is stored inside the free object itself — this avoids maintaining separate bookkeeping arrays and keeps the metadata overhead low, which is one of the key design improvements the SLUB allocator brought over its predecessor.
Inspecting Slab Caches on a Live System
You can observe the Linux kernel slab allocator in action on any running system. This is extremely useful when debugging memory leaks or high memory usage from a driver.
Using /proc/slabinfo
This file exposes per-cache statistics directly from the kernel and requires root privileges to read:
sudo cat /proc/slabinfo | head -n 15
Each row corresponds to one slab cache and shows the cache name, the number of active objects, the total number of objects currently allocated, and the size of each object in bytes.
Using slabtop for a Live View
For a continuously updating, human-friendly view similar to top, use:
sudo slabtop
This sorts caches by memory usage so you can quickly spot which kernel subsystem or driver is consuming the most slab memory at any given moment.
Checking Total Slab Memory Usage
grep -i "^Slab:" /proc/meminfo
This single line tells you the total kilobytes currently held by all slab caches combined, which is a useful first check when investigating unexpected memory pressure.
Real-World Use Cases of the Slab Allocator
The slab allocator is not an academic concept — it is used constantly by core kernel subsystems:
- Networking: Socket buffers used for every packet sent or received are allocated from dedicated slab caches for speed.
- Filesystems: Inode and directory entry (dentry) structures, which represent files and directory lookups, live in slab caches.
- Process management: Structures representing running processes and their memory descriptors are slab-cached for fast context switching and process creation.
- Block I/O: Structures describing pending disk I/O requests are pooled in slab caches to keep storage performance high.
- Device drivers: Any driver that repeatedly allocates fixed-size structures (like USB request blocks) benefits from slab-backed allocation via kmalloc().
Common Mistakes When Learning the Slab Allocator
| Mistake | Why It’s a Problem |
|---|---|
| Assuming SLAB or SLOB still exist as configuration choices | Modern kernels only ship SLUB; tutorials referencing CONFIG_SLAB are outdated. |
| Reading /proc/slabinfo without root | The file requires elevated privileges; without sudo you will get a permission error. |
| Confusing slab memory with page cache | Both show up under buff/cache in free -h, but they serve very different purposes. |
Best Practices
- Always check /proc/slabinfo or slabtop when debugging unexplained kernel memory growth.
- Understand that slab caches shrink automatically under memory pressure — sudden growth in a specific cache usually points to a leak in a driver, not the allocator itself.
- When reading kernel source or crash dumps, remember that all internal implementation details now refer to SLUB, even when the historical name “slab” is still used generically.
Performance and Security Considerations
Performance: Because slab objects are largely pre-allocated and reused, allocation and freeing in the SLUB allocator’s fast path avoids taking locks in the common case, which keeps latency low even under heavy concurrent access from multiple CPUs.
Security: Slab allocator internals are a known target for kernel exploitation techniques such as heap spraying and use-after-free attacks. The kernel includes several slab-level debugging and hardening options (covered in later lectures of this free Linux kernel programming course) that can help detect corruption of slab objects during development and testing.
Key Takeaways
- The Linux kernel slab allocator sits above the page allocator and provides small, reusable memory objects.
- Modern kernels use only the SLUB implementation — SLAB and SLOB have been removed.
- The kmalloc() and kzalloc() APIs remain the primary interface for module and driver authors.
- /proc/slabinfo and slabtop are your main tools for observing slab cache behavior on a live system.
- Core subsystems like networking, filesystems, and process management depend heavily on the slab allocator for performance.
Frequently Asked Questions
Is the slab allocator the same as kmalloc?
kmalloc() is the API function that driver and kernel code calls; the slab allocator (SLUB) is the underlying implementation that actually services that call for typical small allocation sizes.
Do I need to enable SLUB manually when building a kernel?
No. On current mainline kernels, SLUB is the only slab allocator available, so there is no separate allocator to select in kernel configuration.
Why did the kernel remove SLAB and SLOB?
Maintaining three allocators with overlapping functionality created ongoing complexity and duplicated maintenance work. Once SLUB matured enough to cover the remaining use cases, the kernel community removed the older allocators to simplify the codebase.
Can I view slab cache information without root access?
No, both /proc/slabinfo and slabtop require root privileges because they expose internal kernel memory layout information.
What is the difference between slab memory and the page cache?
Slab memory holds small kernel objects like inodes and network buffers, while the page cache holds the contents of files read from or written to disk. Both are reclaimable under memory pressure but serve different purposes.
Is learning the slab allocator necessary for device driver development?
Yes. Almost every non-trivial device driver allocates memory using kmalloc() or kzalloc(), both of which are backed by the slab allocator, so understanding it helps you write more efficient and leak-free drivers.
Conclusion
The Linux kernel slab allocator is one of the most important building blocks of kernel memory management. In this first lecture of our free Linux kernel programming course, you learned why the slab allocator exists, how the object caching idea reduces allocation overhead, why modern kernels rely exclusively on the SLUB implementation, and how to inspect live slab statistics using /proc/slabinfo and slabtop. In the next lecture of this free Linux device drivers course, we move from theory to practice and start using the kmalloc() and kzalloc() APIs with real, working driver code examples.
More lectures on memory management, device drivers, and Bluetooth/BLE are available on EmbeddedPathashala — completely free.
