How vmalloc Maps Discontinuous Physical Pages to a Continuous Virtual Space in Linux
vmalloc allocates virtual memory by mapping non‑contiguous physical pages into a contiguous virtual address range, using vmap_area and vm_struct structures, and follows a three‑step process of finding a hole, allocating pages, and establishing the mappings, with details on its data structures and region layout.
Introduction to vmalloc
Based on earlier articles about the buddy system and slab allocator, we know that kmalloc provides physically contiguous memory. As fragmentation grows, allocating large contiguous physical blocks becomes difficult. For non‑DMA accesses, physical contiguity is not required, so vmalloc uses the vmap mechanism to map separate physical pages into a contiguous virtual address space, similar to user‑space malloc.
The vmalloc region lies between VMALLOC_START and VMALLOC_END, and its layout can be inspected via /proc/vmallocinfo.
Data Structures
vmap_area describes a contiguous virtual address region and links multiple struct vm_struct instances into a list.
struct vmap_area {
unsigned long va_start; // vmalloc returned start address
unsigned long va_end; // vmalloc returned end address
unsigned long flags;
// linked into vmap_area_root red‑black tree
struct rb_node rb_node; /* address‑sorted rbtree */
// linked into vmap_area_list list
struct list_head list; /* address‑sorted list */
struct llist_node purge_list; /* "lazy purge" list */
// if the VA is in use (present in both tree and list), vm is valid
struct vm_struct *vm;
struct rcu_head rcu_head;
};vm_struct manages the mapping between virtual addresses and the underlying physical pages.
struct vm_struct {
struct vm_struct *next; // next vm_struct in the list
void *addr; // start of the virtual region
unsigned long size; // size of the virtual region
unsigned long flags;
// vmalloc may allocate non‑contiguous physical pages; each page is described by struct page.
// vm_struct holds an array of pointers to those pages.
struct page **pages;
unsigned int nr_pages; // number of pages mapped by vmalloc
phys_addr_t phys_addr; // for IO shared memory, otherwise 0
const void *caller; // address of the function that called vmalloc
};vmalloc Allocation Process
The allocation proceeds in three main steps:
Search the virtual address space from VMALLOC_START to VMALLOC_END for a free region (hole) large enough for the requested size.
Based on the requested size, repeatedly call alloc_page to allocate individual physical pages.
Map each allocated physical page into the contiguous virtual region found in step 1, establishing the final virtual address block.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
