15.5 Page Migration
Page migration is a technology leveraged by Linux to improve program execution efficiency. Through page migration, the system can alter the physical memory location mapped by a virtual address while guaranteeing that the process's virtual address remains unchanged. This serves to expand physically contiguous memory blocks or ensure that the physical memory occupied by a process resides as close as possible to the CPU running that process. Page migration permits the relocation of physical memory mapped by virtual addresses from one node to another, and also allows physical memory to be migrated between different processes. For instance, when the system leverages cpusets for process and resource management, CPU cores are typically bound to specific memory nodes. When the system switches a process from one cpuset to another, it must concurrently migrate the corresponding memory to the newly bound nodes.
After determining that a page is suitable for migration, the foundational operation of page migration is to locate all page table entries (PTEs) mapping that specific physical memory page via forward and reverse mappings, modify the contents of these entries to point to the new physical memory, and concurrently update the page descriptor (struct page) tracking that physical memory page while modifying its mapping-related fields. Of course, page migration is not that simple; the migration pipeline must first establish whether a memory page is eligible for migration, and isolate the targeted page to prevent other CPU cores from releasing or manipulating it while the migration is underway.
Linux supplies functions such as migrate_pages(), sys_move_pages(), handle_pte_fault(), and handle_mm_fault(). migrate_pages() is utilized internally by the kernel for page migration. sys_move_pages() acts as a system call interface enabling users to manually migrate pages from user space. handle_pte_fault() and handle_mm_fault() serve as the handler routines for page table faults and process address space faults, both of which invoke internal migration routines provided by the kernel to relocate memory.
As established previously, Linux maintains several LRU lists to coordinate memory swapping. To process these LRU lists efficiently, each CPU utilizes a local pagevec vector structure to temporarily stage memory pages destined for the LRU lists; these independently batched pages are flushed onto the main LRU lists only during list synchronization loops. Prior to executing a page migration, the system must flush these temporarily staged pages from each CPU's page vector onto the appropriate LRU lists. Below, we outline the general procedural steps of page migration using the kernel_move_pages() function as an execution example.
Before invoking the page migration routine, the targeted pages must first be isolated via isolate_movable_page(), and these pages must satisfy the criteria for movable pages. The page migration process encompasses the following sequential steps:
-
Upon entering the page isolation procedure, the system first flushes all pages temporarily staged inside each CPU's local pagevec structure onto the primary LRU lists.
-
It scans all pages specified during the invocation of the page migration process to determine the specific home node of each page.
-
Via the process identifier (pid), it resolves the process address space descriptor mm_struct and the memory nodes available to the task.
-
It aggregates all migration-targeted pages residing on the identical memory node into a migration page linked list, and isolates these target pages via the function isolate_lru_page().
-
After collecting all target pages belonging to a specific node, the system executes a batched migration of these pages to their new destinations. This concrete migration workload is performed by the function migrate_pages(), whose primary operations encompass:
a) Clearing specific operational flags within the original page descriptor's flags field based on the exact context.
b) If the migration target is a dirty page, flushing its contents out to external storage; depending on the active migration mode, the system might not wait for this writeback cycle to complete.
c) Configuring the flags field of the new page descriptor based on the status of the original page's flags, and copying the original index and mapping field values over to the new page descriptor.
d) Copying the raw data payload from the original memory page over to the new physical memory page.
e) Modifying the contents of all page table entries currently mapping the original page, routing them to point directly to the new physical page.
f) Recording the explicit reason or trigger for the page migration.
g) Removing the original page frame from the primary LRU tracking lists.
h) Appending the newly allocated page frame onto the primary LRU tracking lists.
i) Clearing the entire flags field of the original page descriptor and releasing the original page frame back to the allocator.
Upon successfully migrating all target pages across the respective memory nodes, the migration task concludes. Figure 15-26 provides a simplified execution flowchart of the page migration process. For the sake of visual clarity and structural conciseness, extensive exception handling blocks and divergent logic paths for specialized page categories (such as unmovable pages, cached pages, and huge pages) are omitted from this diagram.
Figure 15-26 Execution Workflow of Page Migration