Looking at kmalloc() and the SLUB Memory Allocator (2019)
4 days ago
- #Memory Management
- #SLUB Allocator
- #Linux Kernel
- The article discusses the Linux kernel's memory allocation process, specifically focusing on the `kmalloc()` function and the SLUB allocator.
- Virtual memory in Linux uses paging to map virtual addresses to physical addresses, managed by page tables and cached by the Translation Lookaside Buffer (TLB) for performance.
- Three main memory allocators in Linux are mentioned: SLOB (Simple List Of Blocks), SLAB, and SLUB, with SLUB being the default since kernel version 2.6.23.
- SLUB improves upon SLAB by reducing metadata overhead and simplifying slab management, making it more scalable for large systems.
- The `kmalloc()` function is analyzed in detail, showing how it handles small memory allocations (e.g., 128 bytes) by leveraging slab caches.
- The `slab_alloc_node()` function is examined, highlighting its role in allocating objects from slabs, handling CPU preemption, and ensuring atomic operations with cmpxchg.
- Memory allocation flags like `GFP_KERNEL` and `__GFP_ZERO` are discussed, explaining their impact on sleeping behavior and zeroing out allocated memory.
- The article concludes by emphasizing the complexity and efficiency of the SLUB allocator, and the author's newfound understanding of kernel memory management.