Page migration¶
Page migration allows moving the physical location of pages betweennodes in a NUMA system while the process is running. This means that thevirtual addresses that the process sees do not change. However, thesystem rearranges the physical location of those pages.
Also seeHeterogeneous Memory Management (HMM) for migrating pages to or from deviceprivate memory.
The main intent of page migration is to reduce the latency of memory accessesby moving pages near to the processor where the process accessing that memoryis running.
Page migration allows a process to manually relocate the node on which itspages are located through the MF_MOVE and MF_MOVE_ALL options while settinga new memory policy viambind(). The pages of a process can also be relocatedfrom another process using thesys_migrate_pages() function call. Themigrate_pages() function call takes two sets of nodes and moves pages of aprocess that are located on the from nodes to the destination nodes.Page migration functions are provided by the numactl package by Andi Kleen(a version later than 0.9.3 is required. Get it fromhttps://github.com/numactl/numactl.git). numactl provides libnumawhich provides an interface similar to other NUMA functionality for pagemigration. cat/proc/<pid>/numa_maps allows an easy review of where thepages of a process are located. See also the numa_maps documentation in theproc(5) man page.
Manual migration is useful if for example the scheduler has relocateda process to a processor on a distant node. A batch scheduler or anadministrator may detect the situation and move the pages of the processnearer to the new processor. The kernel itself only providesmanual page migration support. Automatic page migration may be implementedthrough user space processes that move pages. A special function call“move_pages” allows the moving of individual pages within a process.For example, A NUMA profiler may obtain a log showing frequent off-nodeaccesses and may use the result to move pages to more advantageouslocations.
Larger installations usually partition the system using cpusets intosections of nodes. Paul Jackson has equipped cpusets with the ability tomove pages when a task is moved to another cpuset (SeeCPUSETS).Cpusets allow the automation of process locality. If a task is moved toa new cpuset then also all its pages are moved with it so that theperformance of the process does not sink dramatically. Also the pagesof processes in a cpuset are moved if the allowed memory nodes of acpuset are changed.
Page migration allows the preservation of the relative location of pageswithin a group of nodes for all migration techniques which will preserve aparticular memory allocation pattern generated even after migrating aprocess. This is necessary in order to preserve the memory latencies.Processes will run with similar performance after migration.
Page migration occurs in several steps. First a high leveldescription for those trying to usemigrate_pages() from the kernel(for userspace usage see the Andi Kleen’s numactl package mentioned above)and then a low level description of how the low level details work.
In kernel use of migrate_pages()¶
Remove folios from the LRU.
Lists of folios to be migrated are generated by scanning overfolios and moving them into lists. This is done bycalling
folio_isolate_lru().Callingfolio_isolate_lru()increases the references to the folioso that it cannot vanish while the folio migration occurs.It also prevents the swapper or other scans from encounteringthe folio.We need to have a function of type new_folio_t that can bepassed to
migrate_pages(). This function should figure outhow to allocate the correct new folio given the old folio.The
migrate_pages()function is called which attemptsto do the migration. It will call the function to allocatethe new folio for each folio that is considered for moving.
How migrate_pages() works¶
migrate_pages() does several passes over its list of folios. A folio is movedif all references to a folio are removable at the time. The folio hasalready been removed from the LRU viafolio_isolate_lru() and the refcountis increased so that the folio cannot be freed while folio migration occurs.
Steps:
Lock the page to be migrated.
Ensure that writeback is complete.
Lock the new page that we want to move to. It is locked so that accesses tothis (not yet up-to-date) page immediately block while the move is in progress.
All the page table references to the page are converted to migrationentries. This decreases the mapcount of a page. If the resultingmapcount is not zero then we do not migrate the page. All user spaceprocesses that attempt to access the page will now wait on the page lockor wait for the migration page table entry to be removed.
The i_pages lock is taken. This will cause all processes tryingto access the page via the mapping to block on the spinlock.
The refcount of the page is examined and we back out if references remain.Otherwise, we know that we are the only one referencing this page.
The radix tree is checked and if it does not contain the pointer to thispage then we back out because someone else modified the radix tree.
The new page is prepped with some settings from the old page so thataccesses to the new page will discover a page with the correct settings.
The radix tree is changed to point to the new page.
The reference count of the old page is dropped because the address spacereference is gone. A reference to the new page is established becausethe new page is referenced by the address space.
The i_pages lock is dropped. With that lookups in the mappingbecome possible again. Processes will move from spinning on the lockto sleeping on the locked new page.
The page contents are copied to the new page.
The remaining page flags are copied to the new page.
The old page flags are cleared to indicate that the page doesnot provide any information anymore.
Queued up writeback on the new page is triggered.
If migration entries were inserted into the page table, then replace themwith real ptes. Doing so will enable access for user space processes notalready waiting for the page lock.
The page locks are dropped from the old and new page.Processes waiting on the page lock will redo their page faultsand will reach the new page.
The new page is moved to the LRU and can be scanned by the swapper,etc. again.
movable_ops page migration¶
Selected typed, non-folio pages (e.g., pages inflated in a memory balloon,zsmalloc pages) can be migrated using the movable_ops migration framework.
The “structmovable_operations” provide callbacks specific to a page typefor isolating, migrating and un-isolating (putback) these pages.
Once a page is indicated as having movable_ops, that condition must notchange until the page was freed back to the buddy. This includes notchanging/clearing the page type and not changing/clearing thePG_movable_ops page flag.
Arbitrary drivers cannot currently make use of this framework, as itrequires:
a page type
indicating them as possibly having movable_ops in
page_has_movable_ops()based on the page typereturning the movable_ops from
page_movable_ops()based on the pagetypenot reusing the PG_movable_ops and PG_movable_ops_isolated page flagsfor other purposes
For example, balloon drivers can make use of this framework through theballoon-compaction infrastructure residing in the core kernel.
Monitoring Migration¶
The following events (counters) can be used to monitor page migration.
PGMIGRATE_SUCCESS: Normal page migration success. Each count means that apage was migrated. If the page was a non-THP and non-hugetlb page, thenthis counter is increased by one. If the page was a THP or hugetlb, thenthis counter is increased by the number of THP or hugetlb subpages.For example, migration of a single 2MB THP that has 4KB-size base pages(subpages) will cause this counter to increase by 512.
PGMIGRATE_FAIL: Normal page migration failure. Same counting rules as forPGMIGRATE_SUCCESS, above: this will be increased by the number of subpages,if it was a THP or hugetlb.
THP_MIGRATION_SUCCESS: A THP was migrated without being split.
THP_MIGRATION_FAIL: A THP could not be migrated nor it could be split.
THP_MIGRATION_SPLIT: A THP was migrated, but not as such: first, the THP hadto be split. After splitting, a migration retry was used for its sub-pages.
THP_MIGRATION_* events also update the appropriate PGMIGRATE_SUCCESS orPGMIGRATE_FAIL events. For example, a THP migration failure will cause bothTHP_MIGRATION_FAIL and PGMIGRATE_FAIL to increase.
Christoph Lameter, May 8, 2006.Minchan Kim, Mar 28, 2016.
- structmovable_operations¶
Driver page migration
Definition:
struct movable_operations { bool (*isolate_page)(struct page *, isolate_mode_t); int (*migrate_page)(struct page *dst, struct page *src, enum migrate_mode); void (*putback_page)(struct page *);};Members
isolate_pageThe VM calls this function to prepare the page to be moved. The pageis locked and the driver should not unlock it. The driver shouldreturn
trueif the page is movable andfalseif it is notcurrently movable. After this function returns, the VM uses thepage->lru field, so the driver must preserve any information whichis usually stored here.migrate_pageAfter isolation, the VM calls this function with the isolatedsrc page. The driver should copy the contents of thesrc page to thedst page and set up the fields ofdst page.Both pages are locked.If page migration is successful, the driver should return 0.If the driver cannot migrate the page at the moment, it can return-EAGAIN. The VM interprets this as a temporary migration failure andwill retry it later. Any other error value is a permanent migrationfailure and migration will not be retried.The driver shouldn’t touch thesrc->lru field while in the
migrate_page()function. It may write todst->lru.putback_pageIf migration fails on the isolated page, the VM informs the driverthat the page is no longer a candidate for migration by callingthis function. The driver should put the isolated page back intoits own data structure.