Merge tag 'drm-misc-next-fixes-2018-06-15' of git://anongit.freedesktop.org/drm/drm...
[sfrench/cifs-2.6.git] / Documentation / vm / page_migration.rst
1 .. _page_migration:
2
3 ==============
4 Page migration
5 ==============
6
7 Page migration allows the moving of the physical location of pages between
8 nodes in a numa system while the process is running. This means that the
9 virtual addresses that the process sees do not change. However, the
10 system rearranges the physical location of those pages.
11
12 The main intend of page migration is to reduce the latency of memory access
13 by moving pages near to the processor where the process accessing that memory
14 is running.
15
16 Page migration allows a process to manually relocate the node on which its
17 pages are located through the MF_MOVE and MF_MOVE_ALL options while setting
18 a new memory policy via mbind(). The pages of process can also be relocated
19 from another process using the sys_migrate_pages() function call. The
20 migrate_pages function call takes two sets of nodes and moves pages of a
21 process that are located on the from nodes to the destination nodes.
22 Page migration functions are provided by the numactl package by Andi Kleen
23 (a version later than 0.9.3 is required. Get it from
24 ftp://oss.sgi.com/www/projects/libnuma/download/). numactl provides libnuma
25 which provides an interface similar to other numa functionality for page
26 migration.  cat ``/proc/<pid>/numa_maps`` allows an easy review of where the
27 pages of a process are located. See also the numa_maps documentation in the
28 proc(5) man page.
29
30 Manual migration is useful if for example the scheduler has relocated
31 a process to a processor on a distant node. A batch scheduler or an
32 administrator may detect the situation and move the pages of the process
33 nearer to the new processor. The kernel itself does only provide
34 manual page migration support. Automatic page migration may be implemented
35 through user space processes that move pages. A special function call
36 "move_pages" allows the moving of individual pages within a process.
37 A NUMA profiler may f.e. obtain a log showing frequent off node
38 accesses and may use the result to move pages to more advantageous
39 locations.
40
41 Larger installations usually partition the system using cpusets into
42 sections of nodes. Paul Jackson has equipped cpusets with the ability to
43 move pages when a task is moved to another cpuset (See
44 Documentation/cgroup-v1/cpusets.txt).
45 Cpusets allows the automation of process locality. If a task is moved to
46 a new cpuset then also all its pages are moved with it so that the
47 performance of the process does not sink dramatically. Also the pages
48 of processes in a cpuset are moved if the allowed memory nodes of a
49 cpuset are changed.
50
51 Page migration allows the preservation of the relative location of pages
52 within a group of nodes for all migration techniques which will preserve a
53 particular memory allocation pattern generated even after migrating a
54 process. This is necessary in order to preserve the memory latencies.
55 Processes will run with similar performance after migration.
56
57 Page migration occurs in several steps. First a high level
58 description for those trying to use migrate_pages() from the kernel
59 (for userspace usage see the Andi Kleen's numactl package mentioned above)
60 and then a low level description of how the low level details work.
61
62 In kernel use of migrate_pages()
63 ================================
64
65 1. Remove pages from the LRU.
66
67    Lists of pages to be migrated are generated by scanning over
68    pages and moving them into lists. This is done by
69    calling isolate_lru_page().
70    Calling isolate_lru_page increases the references to the page
71    so that it cannot vanish while the page migration occurs.
72    It also prevents the swapper or other scans to encounter
73    the page.
74
75 2. We need to have a function of type new_page_t that can be
76    passed to migrate_pages(). This function should figure out
77    how to allocate the correct new page given the old page.
78
79 3. The migrate_pages() function is called which attempts
80    to do the migration. It will call the function to allocate
81    the new page for each page that is considered for
82    moving.
83
84 How migrate_pages() works
85 =========================
86
87 migrate_pages() does several passes over its list of pages. A page is moved
88 if all references to a page are removable at the time. The page has
89 already been removed from the LRU via isolate_lru_page() and the refcount
90 is increased so that the page cannot be freed while page migration occurs.
91
92 Steps:
93
94 1. Lock the page to be migrated
95
96 2. Ensure that writeback is complete.
97
98 3. Lock the new page that we want to move to. It is locked so that accesses to
99    this (not yet uptodate) page immediately lock while the move is in progress.
100
101 4. All the page table references to the page are converted to migration
102    entries. This decreases the mapcount of a page. If the resulting
103    mapcount is not zero then we do not migrate the page. All user space
104    processes that attempt to access the page will now wait on the page lock.
105
106 5. The i_pages lock is taken. This will cause all processes trying
107    to access the page via the mapping to block on the spinlock.
108
109 6. The refcount of the page is examined and we back out if references remain
110    otherwise we know that we are the only one referencing this page.
111
112 7. The radix tree is checked and if it does not contain the pointer to this
113    page then we back out because someone else modified the radix tree.
114
115 8. The new page is prepped with some settings from the old page so that
116    accesses to the new page will discover a page with the correct settings.
117
118 9. The radix tree is changed to point to the new page.
119
120 10. The reference count of the old page is dropped because the address space
121     reference is gone. A reference to the new page is established because
122     the new page is referenced by the address space.
123
124 11. The i_pages lock is dropped. With that lookups in the mapping
125     become possible again. Processes will move from spinning on the lock
126     to sleeping on the locked new page.
127
128 12. The page contents are copied to the new page.
129
130 13. The remaining page flags are copied to the new page.
131
132 14. The old page flags are cleared to indicate that the page does
133     not provide any information anymore.
134
135 15. Queued up writeback on the new page is triggered.
136
137 16. If migration entries were page then replace them with real ptes. Doing
138     so will enable access for user space processes not already waiting for
139     the page lock.
140
141 19. The page locks are dropped from the old and new page.
142     Processes waiting on the page lock will redo their page faults
143     and will reach the new page.
144
145 20. The new page is moved to the LRU and can be scanned by the swapper
146     etc again.
147
148 Non-LRU page migration
149 ======================
150
151 Although original migration aimed for reducing the latency of memory access
152 for NUMA, compaction who want to create high-order page is also main customer.
153
154 Current problem of the implementation is that it is designed to migrate only
155 *LRU* pages. However, there are potential non-lru pages which can be migrated
156 in drivers, for example, zsmalloc, virtio-balloon pages.
157
158 For virtio-balloon pages, some parts of migration code path have been hooked
159 up and added virtio-balloon specific functions to intercept migration logics.
160 It's too specific to a driver so other drivers who want to make their pages
161 movable would have to add own specific hooks in migration path.
162
163 To overclome the problem, VM supports non-LRU page migration which provides
164 generic functions for non-LRU movable pages without driver specific hooks
165 migration path.
166
167 If a driver want to make own pages movable, it should define three functions
168 which are function pointers of struct address_space_operations.
169
170 1. ``bool (*isolate_page) (struct page *page, isolate_mode_t mode);``
171
172    What VM expects on isolate_page function of driver is to return *true*
173    if driver isolates page successfully. On returing true, VM marks the page
174    as PG_isolated so concurrent isolation in several CPUs skip the page
175    for isolation. If a driver cannot isolate the page, it should return *false*.
176
177    Once page is successfully isolated, VM uses page.lru fields so driver
178    shouldn't expect to preserve values in that fields.
179
180 2. ``int (*migratepage) (struct address_space *mapping,``
181 |       ``struct page *newpage, struct page *oldpage, enum migrate_mode);``
182
183    After isolation, VM calls migratepage of driver with isolated page.
184    The function of migratepage is to move content of the old page to new page
185    and set up fields of struct page newpage. Keep in mind that you should
186    indicate to the VM the oldpage is no longer movable via __ClearPageMovable()
187    under page_lock if you migrated the oldpage successfully and returns
188    MIGRATEPAGE_SUCCESS. If driver cannot migrate the page at the moment, driver
189    can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time
190    because VM interprets -EAGAIN as "temporal migration failure". On returning
191    any error except -EAGAIN, VM will give up the page migration without retrying
192    in this time.
193
194    Driver shouldn't touch page.lru field VM using in the functions.
195
196 3. ``void (*putback_page)(struct page *);``
197
198    If migration fails on isolated page, VM should return the isolated page
199    to the driver so VM calls driver's putback_page with migration failed page.
200    In this function, driver should put the isolated page back to the own data
201    structure.
202
203 4. non-lru movable page flags
204
205    There are two page flags for supporting non-lru movable page.
206
207    * PG_movable
208
209      Driver should use the below function to make page movable under page_lock::
210
211         void __SetPageMovable(struct page *page, struct address_space *mapping)
212
213      It needs argument of address_space for registering migration
214      family functions which will be called by VM. Exactly speaking,
215      PG_movable is not a real flag of struct page. Rather than, VM
216      reuses page->mapping's lower bits to represent it.
217
218 ::
219         #define PAGE_MAPPING_MOVABLE 0x2
220         page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;
221
222      so driver shouldn't access page->mapping directly. Instead, driver should
223      use page_mapping which mask off the low two bits of page->mapping under
224      page lock so it can get right struct address_space.
225
226      For testing of non-lru movable page, VM supports __PageMovable function.
227      However, it doesn't guarantee to identify non-lru movable page because
228      page->mapping field is unified with other variables in struct page.
229      As well, if driver releases the page after isolation by VM, page->mapping
230      doesn't have stable value although it has PAGE_MAPPING_MOVABLE
231      (Look at __ClearPageMovable). But __PageMovable is cheap to catch whether
232      page is LRU or non-lru movable once the page has been isolated. Because
233      LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also
234      good for just peeking to test non-lru movable pages before more expensive
235      checking with lock_page in pfn scanning to select victim.
236
237      For guaranteeing non-lru movable page, VM provides PageMovable function.
238      Unlike __PageMovable, PageMovable functions validates page->mapping and
239      mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden
240      destroying of page->mapping.
241
242      Driver using __SetPageMovable should clear the flag via __ClearMovablePage
243      under page_lock before the releasing the page.
244
245    * PG_isolated
246
247      To prevent concurrent isolation among several CPUs, VM marks isolated page
248      as PG_isolated under lock_page. So if a CPU encounters PG_isolated non-lru
249      movable page, it can skip it. Driver doesn't need to manipulate the flag
250      because VM will set/clear it automatically. Keep in mind that if driver
251      sees PG_isolated page, it means the page have been isolated by VM so it
252      shouldn't touch page.lru field.
253      PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag
254      for own purpose.
255
256 Christoph Lameter, May 8, 2006.
257 Minchan Kim, Mar 28, 2016.