In the kernel, Kmalloc is able to allocate a maximum contiguous memory of 2 ( MAX_ORDER
-1) page (see alloc_pages function, "if (Unlikely (order >= Max_order)) return N ULL; "), the page size is generally 4K bytes, the MAX_ORDER
default is defined as 11, so if you do not modify the kernel, Kmalloc can allocate the maximum contiguous memory is generally 4M bytes.
There are three ways to get more than 4M of large memory in the kernel:
1. Modify MAX_ORDER
, recompile the kernel
2. The kernel starts the selection pass " mem=
" parameter, such as "mem=80m", reserves some memory, and then maps the reserved memory to the module through Request_mem_region and Ioremap_nocache. The kernel boot parameters need to be modified, No need to recompile the kernel. However, this method does not support the x86 architecture, only the arm, PowerPC and other non-x86 architectures are supported.
3. Calling the Alloc_boot_mem function to pre-allocate large chunks of memory before mem_init the function in Start_kernel requires recompiling the kernel.
Without recompiling the kernel, the kernel in the x86 schema can only get up to 4M of contiguous memory, or use Vmalloc to get more than 4M of non-contiguous memory. Moreover, whether it is Kmalloc or vmalloc, the larger the allocated memory, the greater the likelihood of failure; The sooner the system starts allocating memory (the more free memory, the more regular the division), the greater the likelihood of success.
How to allocate more than 4M large memory in the Linux kernel