Noncontiguous Memory Area Allocation

From The Linux Memory Wiki

Jump to: navigation, search

We have learned how to allocate contiguous physical memory area previously. However, sometime when contiguous physical memory is not required, it may be helpful to allocate memory that appears to be contiguous in the kernel linear address space but non-contiguous in the real physical memory.

This is done by the vmalloc() function which allocate only contiguous linear address to the kernel. We knew that initially the linear address mappping in the kernel address space is the same as the physical address. vmallo() changes the original page table entries in the kernel address space to match the linear address space to non-contiguous physical address. This is done similarly to the linear address allocation of process address space. As we will learn in the next chapter, processes only obtain contiguous linear address that is also mapped through page tables.


One difference between process memory allocation is that, kernel developers cannot simply call vmalloc(). For instance, if the memory is used to communicate with other hardware such as DMA, the memory must be physically contiguous, because hardware outside of the processor do not refer to the page tables.

Other than that, changing the kernel page table entries is an expensive operation. Moreover, the TLB must be flushed if a page table is modified. Unless really neccessary, kmalloc() is usually much faster than vmalloc().


When we talk about changing page table entries to fit a contiguous linear address, one question comes in mind: What happen then to the original physical address that mapped to the linear address? Obviously we cannot use that linear address since it is already in use. Hence we know that vmalloc cannot simply allocate memory from any linear address. The linear address vmalloc() can use is limited to a range specified in the constant VMALLOC_START and VMALLOC_END, hence the maximum amount of memory vmalloc() can allocate is also limited. The typical reserved memory for vmalloc() is 128MB on x86 architecture.


Normally vmalloc() is only used when we want to allocate very large memory area. Typical example is a kernel module is loaded into the kernel memory through vmalloc(). Another usage of vmalloc() is to store the information of swap mapping.


Prev: Slab Allocator
Next: High Memory Mapping
Advertisement