Day89 - Kernel Memory Allocation¶
Learning Objectives¶
Understand how Linux kernel memory allocation works, why kernel memory allocation differs from userspace allocation, how different allocation APIs are selected, what GFP flags represent, and how allocation choices relate to execution context and driver development.
What I Learned¶
Why Kernel Memory Allocation¶
- Why the kernel cannot directly use the userspace allocator.
- Dynamic allocation for driver private data and runtime resources.
- Memory lifetime management inside kernel drivers.
Physical Memory vs Virtual Memory¶
- Difference between physical and virtual memory.
- Kernel virtual address space.
- Why virtual addresses simplify memory management.
Kernel Memory Allocation APIs¶
kmalloc()kzalloc()kcalloc()kfree()vmalloc()
GFP Allocation Flags¶
- Meaning of GFP flags.
GFP_KERNELGFP_ATOMIC- Relationship between GFP flags and execution context.
- Why IRQ Context cannot sleep.
Memory Allocation Guidelines¶
kmalloc()vsvmalloc()kzalloc()vskcalloc()- Choosing the appropriate allocation API.
- Choosing the appropriate GFP flag.
Infrastructure¶
Implemented a simplified Linux-style kernel memory allocator.
Memory Allocator¶
Implemented:
- Fixed-size memory pool
- Block metadata management
- Memory allocation
- Memory reuse
- Zero-initialized allocation
- Array allocation
- Integer overflow detection
- Memory release
- Memory pool dump utility
Allocation Policy¶
Implemented:
- Execution context simulation
GFP_KERNELGFP_ATOMIC- GFP policy validation
Labs¶
Lab1 - Simplified kmalloc()¶
Implemented a simplified kernel memory allocator using a fixed-size memory pool.
Verified:
- Memory allocation
- Memory reuse
- Pool exhaustion handling
Lab2 - Simplified kzalloc()¶
Implemented zero-initialized memory allocation.
Verified:
- Newly allocated memory is zero initialized.
- Reused memory is cleared before reuse.
Lab3 - Simplified kcalloc()¶
Implemented array allocation with overflow protection.
Verified:
- Zero-initialized array allocation.
- Integer overflow detection.
- Allocation failure on overflow.
Lab4 - GFP Allocation Policy¶
Simulated Linux execution context and GFP allocation policy.
Verified:
- Process Context +
GFP_KERNEL - Process Context +
GFP_ATOMIC - IRQ Context +
GFP_KERNEL - IRQ Context +
GFP_ATOMIC
Key Takeaways¶
- Kernel memory allocation APIs are selected based on allocation requirements and execution context.
kmalloc()allocates physically contiguous memory and is the most common allocation API used by kernel drivers.kzalloc()simplifies object initialization by automatically zeroing allocated memory.kcalloc()provides safer array allocation by detecting integer overflow.vmalloc()allocates virtually contiguous memory for large allocations.GFP_KERNELis intended for sleepable context.GFP_ATOMICis required for non-sleepable context such as IRQ handlers or when holding spinlocks.- Allocation policy is determined by execution context, not by allocation size.
Next Steps¶
Continue to Linux kernel memory management internals, including the Buddy Allocator, Slab Allocator, and SLUB allocator, to understand how the kernel manages physical pages and object allocation internally.