vmalloc and Friends
The next memory allocation function that we’ll show you is
vmalloc, which allocates a contiguous memory
region in the virtual address space. Although
the pages are not necessarily consecutive in physical memory (each
page is retrieved with a separate call to
__get_free_page), the kernel sees them as
a contiguous range of addresses. vmalloc returns
0 (the NULL address) if an error occurs, otherwise,
it returns a pointer to a linear memory area of size at least
size.
The prototypes of the function and its relatives (ioremap, which is not strictly an allocation function, will be discussed shortly) are as follows:
#include <linux/vmalloc.h> void * vmalloc(unsigned long size); void vfree(void * addr); void *ioremap(unsigned long offset, unsigned long size); void iounmap(void * addr);
It’s worth stressing that memory addresses returned by kmalloc and get_free_pages are also virtual addresses. Their actual value is still massaged by the MMU (memory management unit, usually part of the CPU) before it is used to address physical memory.[30] vmalloc is not different in how it uses the hardware, but rather in how the kernel performs the allocation task.
The (virtual) address range used by kmalloc and
get_free_pages features a one-to-one mapping to
physical memory, possibly shifted by a constant
PAGE_OFFSET value; the functions don’t need to
modify the page tables for that address range. The address range used
by vmalloc and ioremap, on the other hand, is ...