Kernel Memory Allocation: vmalloc Zone vs. Module Loading
The distinction between where the main kernel code resides versus loadable kernel modules relates to how Linux manages memory and when different components are loaded.
Main Kernel in vmalloc Zone
The main kernel code typically resides in the vmalloc zone because:
-
Early Loading: The core kernel is loaded very early in the boot process, before the memory management subsystem is fully initialized.
-
Contiguous Physical Mapping: The kernel's core code and data structures are often mapped directly to physical memory with a fixed offset (usually starting at 0xC0000000 on 32-bit systems), using a direct mapping approach rather than vmalloc's non-contiguous mapping.
-
Fixed Location: The kernel needs to be at a predictable virtual address for stability and to simplify certain operations.
Modules Outside vmalloc Zone
Kernel modules are typically loaded using kmalloc or specialized module allocation functions rather than vmalloc because:
-
Dynamic Loading: Modules are loaded after the system is fully initialized, when the regular memory allocator is available.
-
Performance: kmalloc allocates physically contiguous memory which provides better performance for code execution compared to the non-contiguous pages that vmalloc provides.
-
Size Considerations: Modules are generally smaller than the main kernel and can fit within physically contiguous memory blocks.
-
Cache Efficiency: Physically contiguous memory (from kmalloc) has better cache behavior than the potentially scattered pages from vmalloc.
That said, some parts of modules that need large contiguous virtual address spaces (but not physically contiguous memory) might still use vmalloc when necessary.
This separation represents Linux's pragmatic approach to memory management, using different allocation strategies based on when and how different components are loaded and accessed.