Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
b20a3503 | 2 | /* |
14e0f9bc | 3 | * Memory Migration functionality - linux/mm/migrate.c |
b20a3503 CL |
4 | * |
5 | * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter | |
6 | * | |
7 | * Page migration was first developed in the context of the memory hotplug | |
8 | * project. The main authors of the migration code are: | |
9 | * | |
10 | * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> | |
11 | * Hirokazu Takahashi <taka@valinux.co.jp> | |
12 | * Dave Hansen <haveblue@us.ibm.com> | |
cde53535 | 13 | * Christoph Lameter |
b20a3503 CL |
14 | */ |
15 | ||
16 | #include <linux/migrate.h> | |
b95f1b31 | 17 | #include <linux/export.h> |
b20a3503 | 18 | #include <linux/swap.h> |
0697212a | 19 | #include <linux/swapops.h> |
b20a3503 | 20 | #include <linux/pagemap.h> |
e23ca00b | 21 | #include <linux/buffer_head.h> |
b20a3503 | 22 | #include <linux/mm_inline.h> |
b488893a | 23 | #include <linux/nsproxy.h> |
b20a3503 | 24 | #include <linux/pagevec.h> |
e9995ef9 | 25 | #include <linux/ksm.h> |
b20a3503 CL |
26 | #include <linux/rmap.h> |
27 | #include <linux/topology.h> | |
28 | #include <linux/cpu.h> | |
29 | #include <linux/cpuset.h> | |
04e62a29 | 30 | #include <linux/writeback.h> |
742755a1 CL |
31 | #include <linux/mempolicy.h> |
32 | #include <linux/vmalloc.h> | |
86c3a764 | 33 | #include <linux/security.h> |
42cb14b1 | 34 | #include <linux/backing-dev.h> |
bda807d4 | 35 | #include <linux/compaction.h> |
4f5ca265 | 36 | #include <linux/syscalls.h> |
7addf443 | 37 | #include <linux/compat.h> |
290408d4 | 38 | #include <linux/hugetlb.h> |
8e6ac7fa | 39 | #include <linux/hugetlb_cgroup.h> |
5a0e3ad6 | 40 | #include <linux/gfp.h> |
a520110e | 41 | #include <linux/pagewalk.h> |
df6ad698 | 42 | #include <linux/pfn_t.h> |
a5430dda | 43 | #include <linux/memremap.h> |
8315ada7 | 44 | #include <linux/userfaultfd_k.h> |
bf6bddf1 | 45 | #include <linux/balloon_compaction.h> |
f714f4f2 | 46 | #include <linux/mmu_notifier.h> |
33c3fc71 | 47 | #include <linux/page_idle.h> |
d435edca | 48 | #include <linux/page_owner.h> |
6e84f315 | 49 | #include <linux/sched/mm.h> |
197e7e52 | 50 | #include <linux/ptrace.h> |
34290e2c | 51 | #include <linux/oom.h> |
b20a3503 | 52 | |
0d1836c3 MN |
53 | #include <asm/tlbflush.h> |
54 | ||
7b2a2d4a MG |
55 | #define CREATE_TRACE_POINTS |
56 | #include <trace/events/migrate.h> | |
57 | ||
b20a3503 CL |
58 | #include "internal.h" |
59 | ||
b20a3503 | 60 | /* |
742755a1 | 61 | * migrate_prep() needs to be called before we start compiling a list of pages |
748446bb MG |
62 | * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is |
63 | * undesirable, use migrate_prep_local() | |
b20a3503 CL |
64 | */ |
65 | int migrate_prep(void) | |
66 | { | |
b20a3503 CL |
67 | /* |
68 | * Clear the LRU lists so pages can be isolated. | |
69 | * Note that pages may be moved off the LRU after we have | |
70 | * drained them. Those pages will fail to migrate like other | |
71 | * pages that may be busy. | |
72 | */ | |
73 | lru_add_drain_all(); | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
748446bb MG |
78 | /* Do the necessary work of migrate_prep but not if it involves other CPUs */ |
79 | int migrate_prep_local(void) | |
80 | { | |
81 | lru_add_drain(); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
9e5bcd61 | 86 | int isolate_movable_page(struct page *page, isolate_mode_t mode) |
bda807d4 MK |
87 | { |
88 | struct address_space *mapping; | |
89 | ||
90 | /* | |
91 | * Avoid burning cycles with pages that are yet under __free_pages(), | |
92 | * or just got freed under us. | |
93 | * | |
94 | * In case we 'win' a race for a movable page being freed under us and | |
95 | * raise its refcount preventing __free_pages() from doing its job | |
96 | * the put_page() at the end of this block will take care of | |
97 | * release this page, thus avoiding a nasty leakage. | |
98 | */ | |
99 | if (unlikely(!get_page_unless_zero(page))) | |
100 | goto out; | |
101 | ||
102 | /* | |
103 | * Check PageMovable before holding a PG_lock because page's owner | |
104 | * assumes anybody doesn't touch PG_lock of newly allocated page | |
8bb4e7a2 | 105 | * so unconditionally grabbing the lock ruins page's owner side. |
bda807d4 MK |
106 | */ |
107 | if (unlikely(!__PageMovable(page))) | |
108 | goto out_putpage; | |
109 | /* | |
110 | * As movable pages are not isolated from LRU lists, concurrent | |
111 | * compaction threads can race against page migration functions | |
112 | * as well as race against the releasing a page. | |
113 | * | |
114 | * In order to avoid having an already isolated movable page | |
115 | * being (wrongly) re-isolated while it is under migration, | |
116 | * or to avoid attempting to isolate pages being released, | |
117 | * lets be sure we have the page lock | |
118 | * before proceeding with the movable page isolation steps. | |
119 | */ | |
120 | if (unlikely(!trylock_page(page))) | |
121 | goto out_putpage; | |
122 | ||
123 | if (!PageMovable(page) || PageIsolated(page)) | |
124 | goto out_no_isolated; | |
125 | ||
126 | mapping = page_mapping(page); | |
127 | VM_BUG_ON_PAGE(!mapping, page); | |
128 | ||
129 | if (!mapping->a_ops->isolate_page(page, mode)) | |
130 | goto out_no_isolated; | |
131 | ||
132 | /* Driver shouldn't use PG_isolated bit of page->flags */ | |
133 | WARN_ON_ONCE(PageIsolated(page)); | |
134 | __SetPageIsolated(page); | |
135 | unlock_page(page); | |
136 | ||
9e5bcd61 | 137 | return 0; |
bda807d4 MK |
138 | |
139 | out_no_isolated: | |
140 | unlock_page(page); | |
141 | out_putpage: | |
142 | put_page(page); | |
143 | out: | |
9e5bcd61 | 144 | return -EBUSY; |
bda807d4 MK |
145 | } |
146 | ||
147 | /* It should be called on page which is PG_movable */ | |
148 | void putback_movable_page(struct page *page) | |
149 | { | |
150 | struct address_space *mapping; | |
151 | ||
152 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
153 | VM_BUG_ON_PAGE(!PageMovable(page), page); | |
154 | VM_BUG_ON_PAGE(!PageIsolated(page), page); | |
155 | ||
156 | mapping = page_mapping(page); | |
157 | mapping->a_ops->putback_page(page); | |
158 | __ClearPageIsolated(page); | |
159 | } | |
160 | ||
5733c7d1 RA |
161 | /* |
162 | * Put previously isolated pages back onto the appropriate lists | |
163 | * from where they were once taken off for compaction/migration. | |
164 | * | |
59c82b70 JK |
165 | * This function shall be used whenever the isolated pageset has been |
166 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() | |
167 | * and isolate_huge_page(). | |
5733c7d1 RA |
168 | */ |
169 | void putback_movable_pages(struct list_head *l) | |
170 | { | |
171 | struct page *page; | |
172 | struct page *page2; | |
173 | ||
b20a3503 | 174 | list_for_each_entry_safe(page, page2, l, lru) { |
31caf665 NH |
175 | if (unlikely(PageHuge(page))) { |
176 | putback_active_hugepage(page); | |
177 | continue; | |
178 | } | |
e24f0b8f | 179 | list_del(&page->lru); |
bda807d4 MK |
180 | /* |
181 | * We isolated non-lru movable page so here we can use | |
182 | * __PageMovable because LRU page's mapping cannot have | |
183 | * PAGE_MAPPING_MOVABLE. | |
184 | */ | |
b1123ea6 | 185 | if (unlikely(__PageMovable(page))) { |
bda807d4 MK |
186 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
187 | lock_page(page); | |
188 | if (PageMovable(page)) | |
189 | putback_movable_page(page); | |
190 | else | |
191 | __ClearPageIsolated(page); | |
192 | unlock_page(page); | |
193 | put_page(page); | |
194 | } else { | |
e8db67eb | 195 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
9de4f22a | 196 | page_is_file_lru(page), -hpage_nr_pages(page)); |
fc280fe8 | 197 | putback_lru_page(page); |
bda807d4 | 198 | } |
b20a3503 | 199 | } |
b20a3503 CL |
200 | } |
201 | ||
0697212a CL |
202 | /* |
203 | * Restore a potential migration pte to a working pte entry | |
204 | */ | |
e4b82222 | 205 | static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma, |
e9995ef9 | 206 | unsigned long addr, void *old) |
0697212a | 207 | { |
3fe87967 KS |
208 | struct page_vma_mapped_walk pvmw = { |
209 | .page = old, | |
210 | .vma = vma, | |
211 | .address = addr, | |
212 | .flags = PVMW_SYNC | PVMW_MIGRATION, | |
213 | }; | |
214 | struct page *new; | |
215 | pte_t pte; | |
0697212a | 216 | swp_entry_t entry; |
0697212a | 217 | |
3fe87967 KS |
218 | VM_BUG_ON_PAGE(PageTail(page), page); |
219 | while (page_vma_mapped_walk(&pvmw)) { | |
4b0ece6f NH |
220 | if (PageKsm(page)) |
221 | new = page; | |
222 | else | |
223 | new = page - pvmw.page->index + | |
224 | linear_page_index(vma, pvmw.address); | |
0697212a | 225 | |
616b8371 ZY |
226 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
227 | /* PMD-mapped THP migration entry */ | |
228 | if (!pvmw.pte) { | |
229 | VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page); | |
230 | remove_migration_pmd(&pvmw, new); | |
231 | continue; | |
232 | } | |
233 | #endif | |
234 | ||
3fe87967 KS |
235 | get_page(new); |
236 | pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); | |
237 | if (pte_swp_soft_dirty(*pvmw.pte)) | |
238 | pte = pte_mksoft_dirty(pte); | |
0697212a | 239 | |
3fe87967 KS |
240 | /* |
241 | * Recheck VMA as permissions can change since migration started | |
242 | */ | |
243 | entry = pte_to_swp_entry(*pvmw.pte); | |
244 | if (is_write_migration_entry(entry)) | |
245 | pte = maybe_mkwrite(pte, vma); | |
f45ec5ff PX |
246 | else if (pte_swp_uffd_wp(*pvmw.pte)) |
247 | pte = pte_mkuffd_wp(pte); | |
d3cb8bf6 | 248 | |
df6ad698 JG |
249 | if (unlikely(is_zone_device_page(new))) { |
250 | if (is_device_private_page(new)) { | |
251 | entry = make_device_private_entry(new, pte_write(pte)); | |
252 | pte = swp_entry_to_pte(entry); | |
f45ec5ff PX |
253 | if (pte_swp_uffd_wp(*pvmw.pte)) |
254 | pte = pte_mkuffd_wp(pte); | |
df6ad698 | 255 | } |
d2b2c6dd | 256 | } |
a5430dda | 257 | |
3ef8fd7f | 258 | #ifdef CONFIG_HUGETLB_PAGE |
3fe87967 KS |
259 | if (PageHuge(new)) { |
260 | pte = pte_mkhuge(pte); | |
261 | pte = arch_make_huge_pte(pte, vma, new, 0); | |
383321ab | 262 | set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
3fe87967 KS |
263 | if (PageAnon(new)) |
264 | hugepage_add_anon_rmap(new, vma, pvmw.address); | |
265 | else | |
266 | page_dup_rmap(new, true); | |
383321ab AK |
267 | } else |
268 | #endif | |
269 | { | |
270 | set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); | |
04e62a29 | 271 | |
383321ab AK |
272 | if (PageAnon(new)) |
273 | page_add_anon_rmap(new, vma, pvmw.address, false); | |
274 | else | |
275 | page_add_file_rmap(new, false); | |
276 | } | |
3fe87967 KS |
277 | if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) |
278 | mlock_vma_page(new); | |
279 | ||
e125fe40 KS |
280 | if (PageTransHuge(page) && PageMlocked(page)) |
281 | clear_page_mlock(page); | |
282 | ||
3fe87967 KS |
283 | /* No need to invalidate - it was non-present before */ |
284 | update_mmu_cache(vma, pvmw.address, pvmw.pte); | |
285 | } | |
51afb12b | 286 | |
e4b82222 | 287 | return true; |
0697212a CL |
288 | } |
289 | ||
04e62a29 CL |
290 | /* |
291 | * Get rid of all migration entries and replace them by | |
292 | * references to the indicated page. | |
293 | */ | |
e388466d | 294 | void remove_migration_ptes(struct page *old, struct page *new, bool locked) |
04e62a29 | 295 | { |
051ac83a JK |
296 | struct rmap_walk_control rwc = { |
297 | .rmap_one = remove_migration_pte, | |
298 | .arg = old, | |
299 | }; | |
300 | ||
e388466d KS |
301 | if (locked) |
302 | rmap_walk_locked(new, &rwc); | |
303 | else | |
304 | rmap_walk(new, &rwc); | |
04e62a29 CL |
305 | } |
306 | ||
0697212a CL |
307 | /* |
308 | * Something used the pte of a page under migration. We need to | |
309 | * get to the page and wait until migration is finished. | |
310 | * When we return from this function the fault will be retried. | |
0697212a | 311 | */ |
e66f17ff | 312 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
30dad309 | 313 | spinlock_t *ptl) |
0697212a | 314 | { |
30dad309 | 315 | pte_t pte; |
0697212a CL |
316 | swp_entry_t entry; |
317 | struct page *page; | |
318 | ||
30dad309 | 319 | spin_lock(ptl); |
0697212a CL |
320 | pte = *ptep; |
321 | if (!is_swap_pte(pte)) | |
322 | goto out; | |
323 | ||
324 | entry = pte_to_swp_entry(pte); | |
325 | if (!is_migration_entry(entry)) | |
326 | goto out; | |
327 | ||
328 | page = migration_entry_to_page(entry); | |
329 | ||
e286781d | 330 | /* |
89eb946a | 331 | * Once page cache replacement of page migration started, page_count |
9a1ea439 HD |
332 | * is zero; but we must not call put_and_wait_on_page_locked() without |
333 | * a ref. Use get_page_unless_zero(), and just fault again if it fails. | |
e286781d NP |
334 | */ |
335 | if (!get_page_unless_zero(page)) | |
336 | goto out; | |
0697212a | 337 | pte_unmap_unlock(ptep, ptl); |
9a1ea439 | 338 | put_and_wait_on_page_locked(page); |
0697212a CL |
339 | return; |
340 | out: | |
341 | pte_unmap_unlock(ptep, ptl); | |
342 | } | |
343 | ||
30dad309 NH |
344 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
345 | unsigned long address) | |
346 | { | |
347 | spinlock_t *ptl = pte_lockptr(mm, pmd); | |
348 | pte_t *ptep = pte_offset_map(pmd, address); | |
349 | __migration_entry_wait(mm, ptep, ptl); | |
350 | } | |
351 | ||
cb900f41 KS |
352 | void migration_entry_wait_huge(struct vm_area_struct *vma, |
353 | struct mm_struct *mm, pte_t *pte) | |
30dad309 | 354 | { |
cb900f41 | 355 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); |
30dad309 NH |
356 | __migration_entry_wait(mm, pte, ptl); |
357 | } | |
358 | ||
616b8371 ZY |
359 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
360 | void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) | |
361 | { | |
362 | spinlock_t *ptl; | |
363 | struct page *page; | |
364 | ||
365 | ptl = pmd_lock(mm, pmd); | |
366 | if (!is_pmd_migration_entry(*pmd)) | |
367 | goto unlock; | |
368 | page = migration_entry_to_page(pmd_to_swp_entry(*pmd)); | |
369 | if (!get_page_unless_zero(page)) | |
370 | goto unlock; | |
371 | spin_unlock(ptl); | |
9a1ea439 | 372 | put_and_wait_on_page_locked(page); |
616b8371 ZY |
373 | return; |
374 | unlock: | |
375 | spin_unlock(ptl); | |
376 | } | |
377 | #endif | |
378 | ||
f900482d | 379 | static int expected_page_refs(struct address_space *mapping, struct page *page) |
0b3901b3 JK |
380 | { |
381 | int expected_count = 1; | |
382 | ||
383 | /* | |
384 | * Device public or private pages have an extra refcount as they are | |
385 | * ZONE_DEVICE pages. | |
386 | */ | |
387 | expected_count += is_device_private_page(page); | |
f900482d | 388 | if (mapping) |
0b3901b3 JK |
389 | expected_count += hpage_nr_pages(page) + page_has_private(page); |
390 | ||
391 | return expected_count; | |
392 | } | |
393 | ||
b20a3503 | 394 | /* |
c3fcf8a5 | 395 | * Replace the page in the mapping. |
5b5c7120 CL |
396 | * |
397 | * The number of remaining references must be: | |
398 | * 1 for anonymous pages without a mapping | |
399 | * 2 for pages with a mapping | |
266cf658 | 400 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
b20a3503 | 401 | */ |
36bc08cc | 402 | int migrate_page_move_mapping(struct address_space *mapping, |
37109694 | 403 | struct page *newpage, struct page *page, int extra_count) |
b20a3503 | 404 | { |
89eb946a | 405 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
42cb14b1 HD |
406 | struct zone *oldzone, *newzone; |
407 | int dirty; | |
f900482d | 408 | int expected_count = expected_page_refs(mapping, page) + extra_count; |
8763cb45 | 409 | |
6c5240ae | 410 | if (!mapping) { |
0e8c7d0f | 411 | /* Anonymous page without mapping */ |
8e321fef | 412 | if (page_count(page) != expected_count) |
6c5240ae | 413 | return -EAGAIN; |
cf4b769a HD |
414 | |
415 | /* No turning back from here */ | |
cf4b769a HD |
416 | newpage->index = page->index; |
417 | newpage->mapping = page->mapping; | |
418 | if (PageSwapBacked(page)) | |
fa9949da | 419 | __SetPageSwapBacked(newpage); |
cf4b769a | 420 | |
78bd5209 | 421 | return MIGRATEPAGE_SUCCESS; |
6c5240ae CL |
422 | } |
423 | ||
42cb14b1 HD |
424 | oldzone = page_zone(page); |
425 | newzone = page_zone(newpage); | |
426 | ||
89eb946a | 427 | xas_lock_irq(&xas); |
89eb946a MW |
428 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
429 | xas_unlock_irq(&xas); | |
e23ca00b | 430 | return -EAGAIN; |
b20a3503 CL |
431 | } |
432 | ||
fe896d18 | 433 | if (!page_ref_freeze(page, expected_count)) { |
89eb946a | 434 | xas_unlock_irq(&xas); |
e286781d NP |
435 | return -EAGAIN; |
436 | } | |
437 | ||
b20a3503 | 438 | /* |
cf4b769a HD |
439 | * Now we know that no one else is looking at the page: |
440 | * no turning back from here. | |
b20a3503 | 441 | */ |
cf4b769a HD |
442 | newpage->index = page->index; |
443 | newpage->mapping = page->mapping; | |
e71769ae | 444 | page_ref_add(newpage, hpage_nr_pages(page)); /* add cache reference */ |
6326fec1 NP |
445 | if (PageSwapBacked(page)) { |
446 | __SetPageSwapBacked(newpage); | |
447 | if (PageSwapCache(page)) { | |
448 | SetPageSwapCache(newpage); | |
449 | set_page_private(newpage, page_private(page)); | |
450 | } | |
451 | } else { | |
452 | VM_BUG_ON_PAGE(PageSwapCache(page), page); | |
b20a3503 CL |
453 | } |
454 | ||
42cb14b1 HD |
455 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
456 | dirty = PageDirty(page); | |
457 | if (dirty) { | |
458 | ClearPageDirty(page); | |
459 | SetPageDirty(newpage); | |
460 | } | |
461 | ||
89eb946a | 462 | xas_store(&xas, newpage); |
e71769ae NH |
463 | if (PageTransHuge(page)) { |
464 | int i; | |
e71769ae | 465 | |
013567be | 466 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
89eb946a | 467 | xas_next(&xas); |
4101196b | 468 | xas_store(&xas, newpage); |
e71769ae | 469 | } |
e71769ae | 470 | } |
7cf9c2c7 NP |
471 | |
472 | /* | |
937a94c9 JG |
473 | * Drop cache reference from old page by unfreezing |
474 | * to one less reference. | |
7cf9c2c7 NP |
475 | * We know this isn't the last reference. |
476 | */ | |
e71769ae | 477 | page_ref_unfreeze(page, expected_count - hpage_nr_pages(page)); |
7cf9c2c7 | 478 | |
89eb946a | 479 | xas_unlock(&xas); |
42cb14b1 HD |
480 | /* Leave irq disabled to prevent preemption while updating stats */ |
481 | ||
0e8c7d0f CL |
482 | /* |
483 | * If moved to a different zone then also account | |
484 | * the page for that zone. Other VM counters will be | |
485 | * taken care of when we establish references to the | |
486 | * new page and drop references to the old page. | |
487 | * | |
488 | * Note that anonymous pages are accounted for | |
4b9d0fab | 489 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
0e8c7d0f CL |
490 | * are mapped to swap space. |
491 | */ | |
42cb14b1 | 492 | if (newzone != oldzone) { |
11fb9989 MG |
493 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES); |
494 | __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES); | |
42cb14b1 | 495 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
11fb9989 MG |
496 | __dec_node_state(oldzone->zone_pgdat, NR_SHMEM); |
497 | __inc_node_state(newzone->zone_pgdat, NR_SHMEM); | |
42cb14b1 HD |
498 | } |
499 | if (dirty && mapping_cap_account_dirty(mapping)) { | |
11fb9989 | 500 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY); |
5a1c84b4 | 501 | __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING); |
11fb9989 | 502 | __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY); |
5a1c84b4 | 503 | __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING); |
42cb14b1 | 504 | } |
4b02108a | 505 | } |
42cb14b1 | 506 | local_irq_enable(); |
b20a3503 | 507 | |
78bd5209 | 508 | return MIGRATEPAGE_SUCCESS; |
b20a3503 | 509 | } |
1118dce7 | 510 | EXPORT_SYMBOL(migrate_page_move_mapping); |
b20a3503 | 511 | |
290408d4 NH |
512 | /* |
513 | * The expected number of remaining references is the same as that | |
514 | * of migrate_page_move_mapping(). | |
515 | */ | |
516 | int migrate_huge_page_move_mapping(struct address_space *mapping, | |
517 | struct page *newpage, struct page *page) | |
518 | { | |
89eb946a | 519 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
290408d4 | 520 | int expected_count; |
290408d4 | 521 | |
89eb946a | 522 | xas_lock_irq(&xas); |
290408d4 | 523 | expected_count = 2 + page_has_private(page); |
89eb946a MW |
524 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
525 | xas_unlock_irq(&xas); | |
290408d4 NH |
526 | return -EAGAIN; |
527 | } | |
528 | ||
fe896d18 | 529 | if (!page_ref_freeze(page, expected_count)) { |
89eb946a | 530 | xas_unlock_irq(&xas); |
290408d4 NH |
531 | return -EAGAIN; |
532 | } | |
533 | ||
cf4b769a HD |
534 | newpage->index = page->index; |
535 | newpage->mapping = page->mapping; | |
6a93ca8f | 536 | |
290408d4 NH |
537 | get_page(newpage); |
538 | ||
89eb946a | 539 | xas_store(&xas, newpage); |
290408d4 | 540 | |
fe896d18 | 541 | page_ref_unfreeze(page, expected_count - 1); |
290408d4 | 542 | |
89eb946a | 543 | xas_unlock_irq(&xas); |
6a93ca8f | 544 | |
78bd5209 | 545 | return MIGRATEPAGE_SUCCESS; |
290408d4 NH |
546 | } |
547 | ||
30b0a105 DH |
548 | /* |
549 | * Gigantic pages are so large that we do not guarantee that page++ pointer | |
550 | * arithmetic will work across the entire page. We need something more | |
551 | * specialized. | |
552 | */ | |
553 | static void __copy_gigantic_page(struct page *dst, struct page *src, | |
554 | int nr_pages) | |
555 | { | |
556 | int i; | |
557 | struct page *dst_base = dst; | |
558 | struct page *src_base = src; | |
559 | ||
560 | for (i = 0; i < nr_pages; ) { | |
561 | cond_resched(); | |
562 | copy_highpage(dst, src); | |
563 | ||
564 | i++; | |
565 | dst = mem_map_next(dst, dst_base, i); | |
566 | src = mem_map_next(src, src_base, i); | |
567 | } | |
568 | } | |
569 | ||
570 | static void copy_huge_page(struct page *dst, struct page *src) | |
571 | { | |
572 | int i; | |
573 | int nr_pages; | |
574 | ||
575 | if (PageHuge(src)) { | |
576 | /* hugetlbfs page */ | |
577 | struct hstate *h = page_hstate(src); | |
578 | nr_pages = pages_per_huge_page(h); | |
579 | ||
580 | if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { | |
581 | __copy_gigantic_page(dst, src, nr_pages); | |
582 | return; | |
583 | } | |
584 | } else { | |
585 | /* thp page */ | |
586 | BUG_ON(!PageTransHuge(src)); | |
587 | nr_pages = hpage_nr_pages(src); | |
588 | } | |
589 | ||
590 | for (i = 0; i < nr_pages; i++) { | |
591 | cond_resched(); | |
592 | copy_highpage(dst + i, src + i); | |
593 | } | |
594 | } | |
595 | ||
b20a3503 CL |
596 | /* |
597 | * Copy the page to its new location | |
598 | */ | |
2916ecc0 | 599 | void migrate_page_states(struct page *newpage, struct page *page) |
b20a3503 | 600 | { |
7851a45c RR |
601 | int cpupid; |
602 | ||
b20a3503 CL |
603 | if (PageError(page)) |
604 | SetPageError(newpage); | |
605 | if (PageReferenced(page)) | |
606 | SetPageReferenced(newpage); | |
607 | if (PageUptodate(page)) | |
608 | SetPageUptodate(newpage); | |
894bc310 | 609 | if (TestClearPageActive(page)) { |
309381fe | 610 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
b20a3503 | 611 | SetPageActive(newpage); |
418b27ef LS |
612 | } else if (TestClearPageUnevictable(page)) |
613 | SetPageUnevictable(newpage); | |
1899ad18 JW |
614 | if (PageWorkingset(page)) |
615 | SetPageWorkingset(newpage); | |
b20a3503 CL |
616 | if (PageChecked(page)) |
617 | SetPageChecked(newpage); | |
618 | if (PageMappedToDisk(page)) | |
619 | SetPageMappedToDisk(newpage); | |
620 | ||
42cb14b1 HD |
621 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
622 | if (PageDirty(page)) | |
623 | SetPageDirty(newpage); | |
b20a3503 | 624 | |
33c3fc71 VD |
625 | if (page_is_young(page)) |
626 | set_page_young(newpage); | |
627 | if (page_is_idle(page)) | |
628 | set_page_idle(newpage); | |
629 | ||
7851a45c RR |
630 | /* |
631 | * Copy NUMA information to the new page, to prevent over-eager | |
632 | * future migrations of this same page. | |
633 | */ | |
634 | cpupid = page_cpupid_xchg_last(page, -1); | |
635 | page_cpupid_xchg_last(newpage, cpupid); | |
636 | ||
e9995ef9 | 637 | ksm_migrate_page(newpage, page); |
c8d6553b HD |
638 | /* |
639 | * Please do not reorder this without considering how mm/ksm.c's | |
640 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). | |
641 | */ | |
b3b3a99c NH |
642 | if (PageSwapCache(page)) |
643 | ClearPageSwapCache(page); | |
b20a3503 CL |
644 | ClearPagePrivate(page); |
645 | set_page_private(page, 0); | |
b20a3503 CL |
646 | |
647 | /* | |
648 | * If any waiters have accumulated on the new page then | |
649 | * wake them up. | |
650 | */ | |
651 | if (PageWriteback(newpage)) | |
652 | end_page_writeback(newpage); | |
d435edca | 653 | |
6aeff241 YS |
654 | /* |
655 | * PG_readahead shares the same bit with PG_reclaim. The above | |
656 | * end_page_writeback() may clear PG_readahead mistakenly, so set the | |
657 | * bit after that. | |
658 | */ | |
659 | if (PageReadahead(page)) | |
660 | SetPageReadahead(newpage); | |
661 | ||
d435edca | 662 | copy_page_owner(page, newpage); |
74485cf2 JW |
663 | |
664 | mem_cgroup_migrate(page, newpage); | |
b20a3503 | 665 | } |
2916ecc0 JG |
666 | EXPORT_SYMBOL(migrate_page_states); |
667 | ||
668 | void migrate_page_copy(struct page *newpage, struct page *page) | |
669 | { | |
670 | if (PageHuge(page) || PageTransHuge(page)) | |
671 | copy_huge_page(newpage, page); | |
672 | else | |
673 | copy_highpage(newpage, page); | |
674 | ||
675 | migrate_page_states(newpage, page); | |
676 | } | |
1118dce7 | 677 | EXPORT_SYMBOL(migrate_page_copy); |
b20a3503 | 678 | |
1d8b85cc CL |
679 | /************************************************************ |
680 | * Migration functions | |
681 | ***********************************************************/ | |
682 | ||
b20a3503 | 683 | /* |
bda807d4 | 684 | * Common logic to directly migrate a single LRU page suitable for |
266cf658 | 685 | * pages that do not use PagePrivate/PagePrivate2. |
b20a3503 CL |
686 | * |
687 | * Pages are locked upon entry and exit. | |
688 | */ | |
2d1db3b1 | 689 | int migrate_page(struct address_space *mapping, |
a6bc32b8 MG |
690 | struct page *newpage, struct page *page, |
691 | enum migrate_mode mode) | |
b20a3503 CL |
692 | { |
693 | int rc; | |
694 | ||
695 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ | |
696 | ||
37109694 | 697 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
b20a3503 | 698 | |
78bd5209 | 699 | if (rc != MIGRATEPAGE_SUCCESS) |
b20a3503 CL |
700 | return rc; |
701 | ||
2916ecc0 JG |
702 | if (mode != MIGRATE_SYNC_NO_COPY) |
703 | migrate_page_copy(newpage, page); | |
704 | else | |
705 | migrate_page_states(newpage, page); | |
78bd5209 | 706 | return MIGRATEPAGE_SUCCESS; |
b20a3503 CL |
707 | } |
708 | EXPORT_SYMBOL(migrate_page); | |
709 | ||
9361401e | 710 | #ifdef CONFIG_BLOCK |
84ade7c1 JK |
711 | /* Returns true if all buffers are successfully locked */ |
712 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, | |
713 | enum migrate_mode mode) | |
714 | { | |
715 | struct buffer_head *bh = head; | |
716 | ||
717 | /* Simple case, sync compaction */ | |
718 | if (mode != MIGRATE_ASYNC) { | |
719 | do { | |
84ade7c1 JK |
720 | lock_buffer(bh); |
721 | bh = bh->b_this_page; | |
722 | ||
723 | } while (bh != head); | |
724 | ||
725 | return true; | |
726 | } | |
727 | ||
728 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ | |
729 | do { | |
84ade7c1 JK |
730 | if (!trylock_buffer(bh)) { |
731 | /* | |
732 | * We failed to lock the buffer and cannot stall in | |
733 | * async migration. Release the taken locks | |
734 | */ | |
735 | struct buffer_head *failed_bh = bh; | |
84ade7c1 JK |
736 | bh = head; |
737 | while (bh != failed_bh) { | |
738 | unlock_buffer(bh); | |
84ade7c1 JK |
739 | bh = bh->b_this_page; |
740 | } | |
741 | return false; | |
742 | } | |
743 | ||
744 | bh = bh->b_this_page; | |
745 | } while (bh != head); | |
746 | return true; | |
747 | } | |
748 | ||
89cb0888 JK |
749 | static int __buffer_migrate_page(struct address_space *mapping, |
750 | struct page *newpage, struct page *page, enum migrate_mode mode, | |
751 | bool check_refs) | |
1d8b85cc | 752 | { |
1d8b85cc CL |
753 | struct buffer_head *bh, *head; |
754 | int rc; | |
cc4f11e6 | 755 | int expected_count; |
1d8b85cc | 756 | |
1d8b85cc | 757 | if (!page_has_buffers(page)) |
a6bc32b8 | 758 | return migrate_page(mapping, newpage, page, mode); |
1d8b85cc | 759 | |
cc4f11e6 | 760 | /* Check whether page does not have extra refs before we do more work */ |
f900482d | 761 | expected_count = expected_page_refs(mapping, page); |
cc4f11e6 JK |
762 | if (page_count(page) != expected_count) |
763 | return -EAGAIN; | |
1d8b85cc | 764 | |
cc4f11e6 JK |
765 | head = page_buffers(page); |
766 | if (!buffer_migrate_lock_buffers(head, mode)) | |
767 | return -EAGAIN; | |
1d8b85cc | 768 | |
89cb0888 JK |
769 | if (check_refs) { |
770 | bool busy; | |
771 | bool invalidated = false; | |
772 | ||
773 | recheck_buffers: | |
774 | busy = false; | |
775 | spin_lock(&mapping->private_lock); | |
776 | bh = head; | |
777 | do { | |
778 | if (atomic_read(&bh->b_count)) { | |
779 | busy = true; | |
780 | break; | |
781 | } | |
782 | bh = bh->b_this_page; | |
783 | } while (bh != head); | |
89cb0888 JK |
784 | if (busy) { |
785 | if (invalidated) { | |
786 | rc = -EAGAIN; | |
787 | goto unlock_buffers; | |
788 | } | |
ebdf4de5 | 789 | spin_unlock(&mapping->private_lock); |
89cb0888 JK |
790 | invalidate_bh_lrus(); |
791 | invalidated = true; | |
792 | goto recheck_buffers; | |
793 | } | |
794 | } | |
795 | ||
37109694 | 796 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
78bd5209 | 797 | if (rc != MIGRATEPAGE_SUCCESS) |
cc4f11e6 | 798 | goto unlock_buffers; |
1d8b85cc CL |
799 | |
800 | ClearPagePrivate(page); | |
801 | set_page_private(newpage, page_private(page)); | |
802 | set_page_private(page, 0); | |
803 | put_page(page); | |
804 | get_page(newpage); | |
805 | ||
806 | bh = head; | |
807 | do { | |
808 | set_bh_page(bh, newpage, bh_offset(bh)); | |
809 | bh = bh->b_this_page; | |
810 | ||
811 | } while (bh != head); | |
812 | ||
813 | SetPagePrivate(newpage); | |
814 | ||
2916ecc0 JG |
815 | if (mode != MIGRATE_SYNC_NO_COPY) |
816 | migrate_page_copy(newpage, page); | |
817 | else | |
818 | migrate_page_states(newpage, page); | |
1d8b85cc | 819 | |
cc4f11e6 JK |
820 | rc = MIGRATEPAGE_SUCCESS; |
821 | unlock_buffers: | |
ebdf4de5 JK |
822 | if (check_refs) |
823 | spin_unlock(&mapping->private_lock); | |
1d8b85cc CL |
824 | bh = head; |
825 | do { | |
826 | unlock_buffer(bh); | |
1d8b85cc CL |
827 | bh = bh->b_this_page; |
828 | ||
829 | } while (bh != head); | |
830 | ||
cc4f11e6 | 831 | return rc; |
1d8b85cc | 832 | } |
89cb0888 JK |
833 | |
834 | /* | |
835 | * Migration function for pages with buffers. This function can only be used | |
836 | * if the underlying filesystem guarantees that no other references to "page" | |
837 | * exist. For example attached buffer heads are accessed only under page lock. | |
838 | */ | |
839 | int buffer_migrate_page(struct address_space *mapping, | |
840 | struct page *newpage, struct page *page, enum migrate_mode mode) | |
841 | { | |
842 | return __buffer_migrate_page(mapping, newpage, page, mode, false); | |
843 | } | |
1d8b85cc | 844 | EXPORT_SYMBOL(buffer_migrate_page); |
89cb0888 JK |
845 | |
846 | /* | |
847 | * Same as above except that this variant is more careful and checks that there | |
848 | * are also no buffer head references. This function is the right one for | |
849 | * mappings where buffer heads are directly looked up and referenced (such as | |
850 | * block device mappings). | |
851 | */ | |
852 | int buffer_migrate_page_norefs(struct address_space *mapping, | |
853 | struct page *newpage, struct page *page, enum migrate_mode mode) | |
854 | { | |
855 | return __buffer_migrate_page(mapping, newpage, page, mode, true); | |
856 | } | |
9361401e | 857 | #endif |
1d8b85cc | 858 | |
04e62a29 CL |
859 | /* |
860 | * Writeback a page to clean the dirty state | |
861 | */ | |
862 | static int writeout(struct address_space *mapping, struct page *page) | |
8351a6e4 | 863 | { |
04e62a29 CL |
864 | struct writeback_control wbc = { |
865 | .sync_mode = WB_SYNC_NONE, | |
866 | .nr_to_write = 1, | |
867 | .range_start = 0, | |
868 | .range_end = LLONG_MAX, | |
04e62a29 CL |
869 | .for_reclaim = 1 |
870 | }; | |
871 | int rc; | |
872 | ||
873 | if (!mapping->a_ops->writepage) | |
874 | /* No write method for the address space */ | |
875 | return -EINVAL; | |
876 | ||
877 | if (!clear_page_dirty_for_io(page)) | |
878 | /* Someone else already triggered a write */ | |
879 | return -EAGAIN; | |
880 | ||
8351a6e4 | 881 | /* |
04e62a29 CL |
882 | * A dirty page may imply that the underlying filesystem has |
883 | * the page on some queue. So the page must be clean for | |
884 | * migration. Writeout may mean we loose the lock and the | |
885 | * page state is no longer what we checked for earlier. | |
886 | * At this point we know that the migration attempt cannot | |
887 | * be successful. | |
8351a6e4 | 888 | */ |
e388466d | 889 | remove_migration_ptes(page, page, false); |
8351a6e4 | 890 | |
04e62a29 | 891 | rc = mapping->a_ops->writepage(page, &wbc); |
8351a6e4 | 892 | |
04e62a29 CL |
893 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
894 | /* unlocked. Relock */ | |
895 | lock_page(page); | |
896 | ||
bda8550d | 897 | return (rc < 0) ? -EIO : -EAGAIN; |
04e62a29 CL |
898 | } |
899 | ||
900 | /* | |
901 | * Default handling if a filesystem does not provide a migration function. | |
902 | */ | |
903 | static int fallback_migrate_page(struct address_space *mapping, | |
a6bc32b8 | 904 | struct page *newpage, struct page *page, enum migrate_mode mode) |
04e62a29 | 905 | { |
b969c4ab | 906 | if (PageDirty(page)) { |
a6bc32b8 | 907 | /* Only writeback pages in full synchronous migration */ |
2916ecc0 JG |
908 | switch (mode) { |
909 | case MIGRATE_SYNC: | |
910 | case MIGRATE_SYNC_NO_COPY: | |
911 | break; | |
912 | default: | |
b969c4ab | 913 | return -EBUSY; |
2916ecc0 | 914 | } |
04e62a29 | 915 | return writeout(mapping, page); |
b969c4ab | 916 | } |
8351a6e4 CL |
917 | |
918 | /* | |
919 | * Buffers may be managed in a filesystem specific way. | |
920 | * We must have no buffers or drop them. | |
921 | */ | |
266cf658 | 922 | if (page_has_private(page) && |
8351a6e4 | 923 | !try_to_release_page(page, GFP_KERNEL)) |
806031bb | 924 | return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; |
8351a6e4 | 925 | |
a6bc32b8 | 926 | return migrate_page(mapping, newpage, page, mode); |
8351a6e4 CL |
927 | } |
928 | ||
e24f0b8f CL |
929 | /* |
930 | * Move a page to a newly allocated page | |
931 | * The page is locked and all ptes have been successfully removed. | |
932 | * | |
933 | * The new page will have replaced the old page if this function | |
934 | * is successful. | |
894bc310 LS |
935 | * |
936 | * Return value: | |
937 | * < 0 - error code | |
78bd5209 | 938 | * MIGRATEPAGE_SUCCESS - success |
e24f0b8f | 939 | */ |
3fe2011f | 940 | static int move_to_new_page(struct page *newpage, struct page *page, |
5c3f9a67 | 941 | enum migrate_mode mode) |
e24f0b8f CL |
942 | { |
943 | struct address_space *mapping; | |
bda807d4 MK |
944 | int rc = -EAGAIN; |
945 | bool is_lru = !__PageMovable(page); | |
e24f0b8f | 946 | |
7db7671f HD |
947 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
948 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); | |
e24f0b8f | 949 | |
e24f0b8f | 950 | mapping = page_mapping(page); |
bda807d4 MK |
951 | |
952 | if (likely(is_lru)) { | |
953 | if (!mapping) | |
954 | rc = migrate_page(mapping, newpage, page, mode); | |
955 | else if (mapping->a_ops->migratepage) | |
956 | /* | |
957 | * Most pages have a mapping and most filesystems | |
958 | * provide a migratepage callback. Anonymous pages | |
959 | * are part of swap space which also has its own | |
960 | * migratepage callback. This is the most common path | |
961 | * for page migration. | |
962 | */ | |
963 | rc = mapping->a_ops->migratepage(mapping, newpage, | |
964 | page, mode); | |
965 | else | |
966 | rc = fallback_migrate_page(mapping, newpage, | |
967 | page, mode); | |
968 | } else { | |
e24f0b8f | 969 | /* |
bda807d4 MK |
970 | * In case of non-lru page, it could be released after |
971 | * isolation step. In that case, we shouldn't try migration. | |
e24f0b8f | 972 | */ |
bda807d4 MK |
973 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
974 | if (!PageMovable(page)) { | |
975 | rc = MIGRATEPAGE_SUCCESS; | |
976 | __ClearPageIsolated(page); | |
977 | goto out; | |
978 | } | |
979 | ||
980 | rc = mapping->a_ops->migratepage(mapping, newpage, | |
981 | page, mode); | |
982 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && | |
983 | !PageIsolated(page)); | |
984 | } | |
e24f0b8f | 985 | |
5c3f9a67 HD |
986 | /* |
987 | * When successful, old pagecache page->mapping must be cleared before | |
988 | * page is freed; but stats require that PageAnon be left as PageAnon. | |
989 | */ | |
990 | if (rc == MIGRATEPAGE_SUCCESS) { | |
bda807d4 MK |
991 | if (__PageMovable(page)) { |
992 | VM_BUG_ON_PAGE(!PageIsolated(page), page); | |
993 | ||
994 | /* | |
995 | * We clear PG_movable under page_lock so any compactor | |
996 | * cannot try to migrate this page. | |
997 | */ | |
998 | __ClearPageIsolated(page); | |
999 | } | |
1000 | ||
1001 | /* | |
c23a0c99 | 1002 | * Anonymous and movable page->mapping will be cleared by |
bda807d4 MK |
1003 | * free_pages_prepare so don't reset it here for keeping |
1004 | * the type to work PageAnon, for example. | |
1005 | */ | |
1006 | if (!PageMappingFlags(page)) | |
5c3f9a67 | 1007 | page->mapping = NULL; |
d2b2c6dd | 1008 | |
25b2995a | 1009 | if (likely(!is_zone_device_page(newpage))) |
d2b2c6dd LP |
1010 | flush_dcache_page(newpage); |
1011 | ||
3fe2011f | 1012 | } |
bda807d4 | 1013 | out: |
e24f0b8f CL |
1014 | return rc; |
1015 | } | |
1016 | ||
0dabec93 | 1017 | static int __unmap_and_move(struct page *page, struct page *newpage, |
9c620e2b | 1018 | int force, enum migrate_mode mode) |
e24f0b8f | 1019 | { |
0dabec93 | 1020 | int rc = -EAGAIN; |
2ebba6b7 | 1021 | int page_was_mapped = 0; |
3f6c8272 | 1022 | struct anon_vma *anon_vma = NULL; |
bda807d4 | 1023 | bool is_lru = !__PageMovable(page); |
95a402c3 | 1024 | |
529ae9aa | 1025 | if (!trylock_page(page)) { |
a6bc32b8 | 1026 | if (!force || mode == MIGRATE_ASYNC) |
0dabec93 | 1027 | goto out; |
3e7d3449 MG |
1028 | |
1029 | /* | |
1030 | * It's not safe for direct compaction to call lock_page. | |
1031 | * For example, during page readahead pages are added locked | |
1032 | * to the LRU. Later, when the IO completes the pages are | |
1033 | * marked uptodate and unlocked. However, the queueing | |
1034 | * could be merging multiple pages for one bio (e.g. | |
1035 | * mpage_readpages). If an allocation happens for the | |
1036 | * second or third page, the process can end up locking | |
1037 | * the same page twice and deadlocking. Rather than | |
1038 | * trying to be clever about what pages can be locked, | |
1039 | * avoid the use of lock_page for direct compaction | |
1040 | * altogether. | |
1041 | */ | |
1042 | if (current->flags & PF_MEMALLOC) | |
0dabec93 | 1043 | goto out; |
3e7d3449 | 1044 | |
e24f0b8f CL |
1045 | lock_page(page); |
1046 | } | |
1047 | ||
1048 | if (PageWriteback(page)) { | |
11bc82d6 | 1049 | /* |
fed5b64a | 1050 | * Only in the case of a full synchronous migration is it |
a6bc32b8 MG |
1051 | * necessary to wait for PageWriteback. In the async case, |
1052 | * the retry loop is too short and in the sync-light case, | |
1053 | * the overhead of stalling is too much | |
11bc82d6 | 1054 | */ |
2916ecc0 JG |
1055 | switch (mode) { |
1056 | case MIGRATE_SYNC: | |
1057 | case MIGRATE_SYNC_NO_COPY: | |
1058 | break; | |
1059 | default: | |
11bc82d6 | 1060 | rc = -EBUSY; |
0a31bc97 | 1061 | goto out_unlock; |
11bc82d6 AA |
1062 | } |
1063 | if (!force) | |
0a31bc97 | 1064 | goto out_unlock; |
e24f0b8f CL |
1065 | wait_on_page_writeback(page); |
1066 | } | |
03f15c86 | 1067 | |
e24f0b8f | 1068 | /* |
dc386d4d KH |
1069 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
1070 | * we cannot notice that anon_vma is freed while we migrates a page. | |
1ce82b69 | 1071 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
dc386d4d | 1072 | * of migration. File cache pages are no problem because of page_lock() |
989f89c5 KH |
1073 | * File Caches may use write_page() or lock_page() in migration, then, |
1074 | * just care Anon page here. | |
03f15c86 HD |
1075 | * |
1076 | * Only page_get_anon_vma() understands the subtleties of | |
1077 | * getting a hold on an anon_vma from outside one of its mms. | |
1078 | * But if we cannot get anon_vma, then we won't need it anyway, | |
1079 | * because that implies that the anon page is no longer mapped | |
1080 | * (and cannot be remapped so long as we hold the page lock). | |
dc386d4d | 1081 | */ |
03f15c86 | 1082 | if (PageAnon(page) && !PageKsm(page)) |
746b18d4 | 1083 | anon_vma = page_get_anon_vma(page); |
62e1c553 | 1084 | |
7db7671f HD |
1085 | /* |
1086 | * Block others from accessing the new page when we get around to | |
1087 | * establishing additional references. We are usually the only one | |
1088 | * holding a reference to newpage at this point. We used to have a BUG | |
1089 | * here if trylock_page(newpage) fails, but would like to allow for | |
1090 | * cases where there might be a race with the previous use of newpage. | |
1091 | * This is much like races on refcount of oldpage: just don't BUG(). | |
1092 | */ | |
1093 | if (unlikely(!trylock_page(newpage))) | |
1094 | goto out_unlock; | |
1095 | ||
bda807d4 MK |
1096 | if (unlikely(!is_lru)) { |
1097 | rc = move_to_new_page(newpage, page, mode); | |
1098 | goto out_unlock_both; | |
1099 | } | |
1100 | ||
dc386d4d | 1101 | /* |
62e1c553 SL |
1102 | * Corner case handling: |
1103 | * 1. When a new swap-cache page is read into, it is added to the LRU | |
1104 | * and treated as swapcache but it has no rmap yet. | |
1105 | * Calling try_to_unmap() against a page->mapping==NULL page will | |
1106 | * trigger a BUG. So handle it here. | |
1107 | * 2. An orphaned page (see truncate_complete_page) might have | |
1108 | * fs-private metadata. The page can be picked up due to memory | |
1109 | * offlining. Everywhere else except page reclaim, the page is | |
1110 | * invisible to the vm, so the page can not be migrated. So try to | |
1111 | * free the metadata, so the page can be freed. | |
e24f0b8f | 1112 | */ |
62e1c553 | 1113 | if (!page->mapping) { |
309381fe | 1114 | VM_BUG_ON_PAGE(PageAnon(page), page); |
1ce82b69 | 1115 | if (page_has_private(page)) { |
62e1c553 | 1116 | try_to_free_buffers(page); |
7db7671f | 1117 | goto out_unlock_both; |
62e1c553 | 1118 | } |
7db7671f HD |
1119 | } else if (page_mapped(page)) { |
1120 | /* Establish migration ptes */ | |
03f15c86 HD |
1121 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
1122 | page); | |
2ebba6b7 | 1123 | try_to_unmap(page, |
da1b13cc | 1124 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
2ebba6b7 HD |
1125 | page_was_mapped = 1; |
1126 | } | |
dc386d4d | 1127 | |
e6a1530d | 1128 | if (!page_mapped(page)) |
5c3f9a67 | 1129 | rc = move_to_new_page(newpage, page, mode); |
e24f0b8f | 1130 | |
5c3f9a67 HD |
1131 | if (page_was_mapped) |
1132 | remove_migration_ptes(page, | |
e388466d | 1133 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
3f6c8272 | 1134 | |
7db7671f HD |
1135 | out_unlock_both: |
1136 | unlock_page(newpage); | |
1137 | out_unlock: | |
3f6c8272 | 1138 | /* Drop an anon_vma reference if we took one */ |
76545066 | 1139 | if (anon_vma) |
9e60109f | 1140 | put_anon_vma(anon_vma); |
e24f0b8f | 1141 | unlock_page(page); |
0dabec93 | 1142 | out: |
c6c919eb MK |
1143 | /* |
1144 | * If migration is successful, decrease refcount of the newpage | |
1145 | * which will not free the page because new page owner increased | |
1146 | * refcounter. As well, if it is LRU page, add the page to LRU | |
e0a352fa DH |
1147 | * list in here. Use the old state of the isolated source page to |
1148 | * determine if we migrated a LRU page. newpage was already unlocked | |
1149 | * and possibly modified by its owner - don't rely on the page | |
1150 | * state. | |
c6c919eb MK |
1151 | */ |
1152 | if (rc == MIGRATEPAGE_SUCCESS) { | |
e0a352fa | 1153 | if (unlikely(!is_lru)) |
c6c919eb MK |
1154 | put_page(newpage); |
1155 | else | |
1156 | putback_lru_page(newpage); | |
1157 | } | |
1158 | ||
0dabec93 MK |
1159 | return rc; |
1160 | } | |
95a402c3 | 1161 | |
ef2a5153 GU |
1162 | /* |
1163 | * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work | |
1164 | * around it. | |
1165 | */ | |
815f0ddb ND |
1166 | #if defined(CONFIG_ARM) && \ |
1167 | defined(GCC_VERSION) && GCC_VERSION < 40900 && GCC_VERSION >= 40700 | |
ef2a5153 GU |
1168 | #define ICE_noinline noinline |
1169 | #else | |
1170 | #define ICE_noinline | |
1171 | #endif | |
1172 | ||
0dabec93 MK |
1173 | /* |
1174 | * Obtain the lock on page, remove all ptes and migrate the page | |
1175 | * to the newly allocated page in newpage. | |
1176 | */ | |
ef2a5153 GU |
1177 | static ICE_noinline int unmap_and_move(new_page_t get_new_page, |
1178 | free_page_t put_new_page, | |
1179 | unsigned long private, struct page *page, | |
add05cec NH |
1180 | int force, enum migrate_mode mode, |
1181 | enum migrate_reason reason) | |
0dabec93 | 1182 | { |
2def7424 | 1183 | int rc = MIGRATEPAGE_SUCCESS; |
74d4a579 | 1184 | struct page *newpage = NULL; |
0dabec93 | 1185 | |
94723aaf MH |
1186 | if (!thp_migration_supported() && PageTransHuge(page)) |
1187 | return -ENOMEM; | |
1188 | ||
0dabec93 MK |
1189 | if (page_count(page) == 1) { |
1190 | /* page was freed from under us. So we are done. */ | |
c6c919eb MK |
1191 | ClearPageActive(page); |
1192 | ClearPageUnevictable(page); | |
bda807d4 MK |
1193 | if (unlikely(__PageMovable(page))) { |
1194 | lock_page(page); | |
1195 | if (!PageMovable(page)) | |
1196 | __ClearPageIsolated(page); | |
1197 | unlock_page(page); | |
1198 | } | |
0dabec93 MK |
1199 | goto out; |
1200 | } | |
1201 | ||
74d4a579 YS |
1202 | newpage = get_new_page(page, private); |
1203 | if (!newpage) | |
1204 | return -ENOMEM; | |
1205 | ||
9c620e2b | 1206 | rc = __unmap_and_move(page, newpage, force, mode); |
c6c919eb | 1207 | if (rc == MIGRATEPAGE_SUCCESS) |
7cd12b4a | 1208 | set_page_owner_migrate_reason(newpage, reason); |
bf6bddf1 | 1209 | |
0dabec93 | 1210 | out: |
e24f0b8f | 1211 | if (rc != -EAGAIN) { |
0dabec93 MK |
1212 | /* |
1213 | * A page that has been migrated has all references | |
1214 | * removed and will be freed. A page that has not been | |
c23a0c99 | 1215 | * migrated will have kept its references and be restored. |
0dabec93 MK |
1216 | */ |
1217 | list_del(&page->lru); | |
6afcf8ef ML |
1218 | |
1219 | /* | |
1220 | * Compaction can migrate also non-LRU pages which are | |
1221 | * not accounted to NR_ISOLATED_*. They can be recognized | |
1222 | * as __PageMovable | |
1223 | */ | |
1224 | if (likely(!__PageMovable(page))) | |
e8db67eb | 1225 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
9de4f22a | 1226 | page_is_file_lru(page), -hpage_nr_pages(page)); |
c6c919eb MK |
1227 | } |
1228 | ||
1229 | /* | |
1230 | * If migration is successful, releases reference grabbed during | |
1231 | * isolation. Otherwise, restore the page to right list unless | |
1232 | * we want to retry. | |
1233 | */ | |
1234 | if (rc == MIGRATEPAGE_SUCCESS) { | |
1235 | put_page(page); | |
1236 | if (reason == MR_MEMORY_FAILURE) { | |
d7e69488 | 1237 | /* |
c6c919eb MK |
1238 | * Set PG_HWPoison on just freed page |
1239 | * intentionally. Although it's rather weird, | |
1240 | * it's how HWPoison flag works at the moment. | |
d7e69488 | 1241 | */ |
d4ae9916 | 1242 | if (set_hwpoison_free_buddy_page(page)) |
da1b13cc | 1243 | num_poisoned_pages_inc(); |
c6c919eb MK |
1244 | } |
1245 | } else { | |
bda807d4 MK |
1246 | if (rc != -EAGAIN) { |
1247 | if (likely(!__PageMovable(page))) { | |
1248 | putback_lru_page(page); | |
1249 | goto put_new; | |
1250 | } | |
1251 | ||
1252 | lock_page(page); | |
1253 | if (PageMovable(page)) | |
1254 | putback_movable_page(page); | |
1255 | else | |
1256 | __ClearPageIsolated(page); | |
1257 | unlock_page(page); | |
1258 | put_page(page); | |
1259 | } | |
1260 | put_new: | |
c6c919eb MK |
1261 | if (put_new_page) |
1262 | put_new_page(newpage, private); | |
1263 | else | |
1264 | put_page(newpage); | |
e24f0b8f | 1265 | } |
68711a74 | 1266 | |
e24f0b8f CL |
1267 | return rc; |
1268 | } | |
1269 | ||
290408d4 NH |
1270 | /* |
1271 | * Counterpart of unmap_and_move_page() for hugepage migration. | |
1272 | * | |
1273 | * This function doesn't wait the completion of hugepage I/O | |
1274 | * because there is no race between I/O and migration for hugepage. | |
1275 | * Note that currently hugepage I/O occurs only in direct I/O | |
1276 | * where no lock is held and PG_writeback is irrelevant, | |
1277 | * and writeback status of all subpages are counted in the reference | |
1278 | * count of the head page (i.e. if all subpages of a 2MB hugepage are | |
1279 | * under direct I/O, the reference of the head page is 512 and a bit more.) | |
1280 | * This means that when we try to migrate hugepage whose subpages are | |
1281 | * doing direct I/O, some references remain after try_to_unmap() and | |
1282 | * hugepage migration fails without data corruption. | |
1283 | * | |
1284 | * There is also no race when direct I/O is issued on the page under migration, | |
1285 | * because then pte is replaced with migration swap entry and direct I/O code | |
1286 | * will wait in the page fault for migration to complete. | |
1287 | */ | |
1288 | static int unmap_and_move_huge_page(new_page_t get_new_page, | |
68711a74 DR |
1289 | free_page_t put_new_page, unsigned long private, |
1290 | struct page *hpage, int force, | |
7cd12b4a | 1291 | enum migrate_mode mode, int reason) |
290408d4 | 1292 | { |
2def7424 | 1293 | int rc = -EAGAIN; |
2ebba6b7 | 1294 | int page_was_mapped = 0; |
32665f2b | 1295 | struct page *new_hpage; |
290408d4 | 1296 | struct anon_vma *anon_vma = NULL; |
c0d0381a | 1297 | struct address_space *mapping = NULL; |
290408d4 | 1298 | |
83467efb | 1299 | /* |
7ed2c31d | 1300 | * Migratability of hugepages depends on architectures and their size. |
83467efb NH |
1301 | * This check is necessary because some callers of hugepage migration |
1302 | * like soft offline and memory hotremove don't walk through page | |
1303 | * tables or check whether the hugepage is pmd-based or not before | |
1304 | * kicking migration. | |
1305 | */ | |
100873d7 | 1306 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
32665f2b | 1307 | putback_active_hugepage(hpage); |
83467efb | 1308 | return -ENOSYS; |
32665f2b | 1309 | } |
83467efb | 1310 | |
666feb21 | 1311 | new_hpage = get_new_page(hpage, private); |
290408d4 NH |
1312 | if (!new_hpage) |
1313 | return -ENOMEM; | |
1314 | ||
290408d4 | 1315 | if (!trylock_page(hpage)) { |
2916ecc0 | 1316 | if (!force) |
290408d4 | 1317 | goto out; |
2916ecc0 JG |
1318 | switch (mode) { |
1319 | case MIGRATE_SYNC: | |
1320 | case MIGRATE_SYNC_NO_COPY: | |
1321 | break; | |
1322 | default: | |
1323 | goto out; | |
1324 | } | |
290408d4 NH |
1325 | lock_page(hpage); |
1326 | } | |
1327 | ||
cb6acd01 MK |
1328 | /* |
1329 | * Check for pages which are in the process of being freed. Without | |
1330 | * page_mapping() set, hugetlbfs specific move page routine will not | |
1331 | * be called and we could leak usage counts for subpools. | |
1332 | */ | |
1333 | if (page_private(hpage) && !page_mapping(hpage)) { | |
1334 | rc = -EBUSY; | |
1335 | goto out_unlock; | |
1336 | } | |
1337 | ||
746b18d4 PZ |
1338 | if (PageAnon(hpage)) |
1339 | anon_vma = page_get_anon_vma(hpage); | |
290408d4 | 1340 | |
7db7671f HD |
1341 | if (unlikely(!trylock_page(new_hpage))) |
1342 | goto put_anon; | |
1343 | ||
2ebba6b7 | 1344 | if (page_mapped(hpage)) { |
c0d0381a MK |
1345 | /* |
1346 | * try_to_unmap could potentially call huge_pmd_unshare. | |
1347 | * Because of this, take semaphore in write mode here and | |
1348 | * set TTU_RMAP_LOCKED to let lower levels know we have | |
1349 | * taken the lock. | |
1350 | */ | |
1351 | mapping = hugetlb_page_mapping_lock_write(hpage); | |
1352 | if (unlikely(!mapping)) | |
1353 | goto unlock_put_anon; | |
1354 | ||
2ebba6b7 | 1355 | try_to_unmap(hpage, |
c0d0381a MK |
1356 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS| |
1357 | TTU_RMAP_LOCKED); | |
2ebba6b7 | 1358 | page_was_mapped = 1; |
c0d0381a MK |
1359 | /* |
1360 | * Leave mapping locked until after subsequent call to | |
1361 | * remove_migration_ptes() | |
1362 | */ | |
2ebba6b7 | 1363 | } |
290408d4 NH |
1364 | |
1365 | if (!page_mapped(hpage)) | |
5c3f9a67 | 1366 | rc = move_to_new_page(new_hpage, hpage, mode); |
290408d4 | 1367 | |
c0d0381a | 1368 | if (page_was_mapped) { |
5c3f9a67 | 1369 | remove_migration_ptes(hpage, |
c0d0381a MK |
1370 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, true); |
1371 | i_mmap_unlock_write(mapping); | |
1372 | } | |
290408d4 | 1373 | |
c0d0381a | 1374 | unlock_put_anon: |
7db7671f HD |
1375 | unlock_page(new_hpage); |
1376 | ||
1377 | put_anon: | |
fd4a4663 | 1378 | if (anon_vma) |
9e60109f | 1379 | put_anon_vma(anon_vma); |
8e6ac7fa | 1380 | |
2def7424 | 1381 | if (rc == MIGRATEPAGE_SUCCESS) { |
ab5ac90a | 1382 | move_hugetlb_state(hpage, new_hpage, reason); |
2def7424 HD |
1383 | put_new_page = NULL; |
1384 | } | |
8e6ac7fa | 1385 | |
cb6acd01 | 1386 | out_unlock: |
290408d4 | 1387 | unlock_page(hpage); |
09761333 | 1388 | out: |
b8ec1cee NH |
1389 | if (rc != -EAGAIN) |
1390 | putback_active_hugepage(hpage); | |
68711a74 DR |
1391 | |
1392 | /* | |
1393 | * If migration was not successful and there's a freeing callback, use | |
1394 | * it. Otherwise, put_page() will drop the reference grabbed during | |
1395 | * isolation. | |
1396 | */ | |
2def7424 | 1397 | if (put_new_page) |
68711a74 DR |
1398 | put_new_page(new_hpage, private); |
1399 | else | |
3aaa76e1 | 1400 | putback_active_hugepage(new_hpage); |
68711a74 | 1401 | |
290408d4 NH |
1402 | return rc; |
1403 | } | |
1404 | ||
b20a3503 | 1405 | /* |
c73e5c9c SB |
1406 | * migrate_pages - migrate the pages specified in a list, to the free pages |
1407 | * supplied as the target for the page migration | |
b20a3503 | 1408 | * |
c73e5c9c SB |
1409 | * @from: The list of pages to be migrated. |
1410 | * @get_new_page: The function used to allocate free pages to be used | |
1411 | * as the target of the page migration. | |
68711a74 DR |
1412 | * @put_new_page: The function used to free target pages if migration |
1413 | * fails, or NULL if no special handling is necessary. | |
c73e5c9c SB |
1414 | * @private: Private data to be passed on to get_new_page() |
1415 | * @mode: The migration mode that specifies the constraints for | |
1416 | * page migration, if any. | |
1417 | * @reason: The reason for page migration. | |
b20a3503 | 1418 | * |
c73e5c9c SB |
1419 | * The function returns after 10 attempts or if no pages are movable any more |
1420 | * because the list has become empty or no retryable pages exist any more. | |
14e0f9bc | 1421 | * The caller should call putback_movable_pages() to return pages to the LRU |
28bd6578 | 1422 | * or free list only if ret != 0. |
b20a3503 | 1423 | * |
c73e5c9c | 1424 | * Returns the number of pages that were not migrated, or an error code. |
b20a3503 | 1425 | */ |
9c620e2b | 1426 | int migrate_pages(struct list_head *from, new_page_t get_new_page, |
68711a74 DR |
1427 | free_page_t put_new_page, unsigned long private, |
1428 | enum migrate_mode mode, int reason) | |
b20a3503 | 1429 | { |
e24f0b8f | 1430 | int retry = 1; |
b20a3503 | 1431 | int nr_failed = 0; |
5647bc29 | 1432 | int nr_succeeded = 0; |
b20a3503 CL |
1433 | int pass = 0; |
1434 | struct page *page; | |
1435 | struct page *page2; | |
1436 | int swapwrite = current->flags & PF_SWAPWRITE; | |
1437 | int rc; | |
1438 | ||
1439 | if (!swapwrite) | |
1440 | current->flags |= PF_SWAPWRITE; | |
1441 | ||
e24f0b8f CL |
1442 | for(pass = 0; pass < 10 && retry; pass++) { |
1443 | retry = 0; | |
b20a3503 | 1444 | |
e24f0b8f | 1445 | list_for_each_entry_safe(page, page2, from, lru) { |
94723aaf | 1446 | retry: |
e24f0b8f | 1447 | cond_resched(); |
2d1db3b1 | 1448 | |
31caf665 NH |
1449 | if (PageHuge(page)) |
1450 | rc = unmap_and_move_huge_page(get_new_page, | |
68711a74 | 1451 | put_new_page, private, page, |
7cd12b4a | 1452 | pass > 2, mode, reason); |
31caf665 | 1453 | else |
68711a74 | 1454 | rc = unmap_and_move(get_new_page, put_new_page, |
add05cec NH |
1455 | private, page, pass > 2, mode, |
1456 | reason); | |
2d1db3b1 | 1457 | |
e24f0b8f | 1458 | switch(rc) { |
95a402c3 | 1459 | case -ENOMEM: |
94723aaf MH |
1460 | /* |
1461 | * THP migration might be unsupported or the | |
1462 | * allocation could've failed so we should | |
1463 | * retry on the same page with the THP split | |
1464 | * to base pages. | |
1465 | * | |
1466 | * Head page is retried immediately and tail | |
1467 | * pages are added to the tail of the list so | |
1468 | * we encounter them after the rest of the list | |
1469 | * is processed. | |
1470 | */ | |
e6112fc3 | 1471 | if (PageTransHuge(page) && !PageHuge(page)) { |
94723aaf MH |
1472 | lock_page(page); |
1473 | rc = split_huge_page_to_list(page, from); | |
1474 | unlock_page(page); | |
1475 | if (!rc) { | |
1476 | list_safe_reset_next(page, page2, lru); | |
1477 | goto retry; | |
1478 | } | |
1479 | } | |
dfef2ef4 | 1480 | nr_failed++; |
95a402c3 | 1481 | goto out; |
e24f0b8f | 1482 | case -EAGAIN: |
2d1db3b1 | 1483 | retry++; |
e24f0b8f | 1484 | break; |
78bd5209 | 1485 | case MIGRATEPAGE_SUCCESS: |
5647bc29 | 1486 | nr_succeeded++; |
e24f0b8f CL |
1487 | break; |
1488 | default: | |
354a3363 NH |
1489 | /* |
1490 | * Permanent failure (-EBUSY, -ENOSYS, etc.): | |
1491 | * unlike -EAGAIN case, the failed page is | |
1492 | * removed from migration page list and not | |
1493 | * retried in the next outer loop. | |
1494 | */ | |
2d1db3b1 | 1495 | nr_failed++; |
e24f0b8f | 1496 | break; |
2d1db3b1 | 1497 | } |
b20a3503 CL |
1498 | } |
1499 | } | |
f2f81fb2 VB |
1500 | nr_failed += retry; |
1501 | rc = nr_failed; | |
95a402c3 | 1502 | out: |
5647bc29 MG |
1503 | if (nr_succeeded) |
1504 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); | |
1505 | if (nr_failed) | |
1506 | count_vm_events(PGMIGRATE_FAIL, nr_failed); | |
7b2a2d4a MG |
1507 | trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason); |
1508 | ||
b20a3503 CL |
1509 | if (!swapwrite) |
1510 | current->flags &= ~PF_SWAPWRITE; | |
1511 | ||
78bd5209 | 1512 | return rc; |
b20a3503 | 1513 | } |
95a402c3 | 1514 | |
742755a1 | 1515 | #ifdef CONFIG_NUMA |
742755a1 | 1516 | |
a49bd4d7 | 1517 | static int store_status(int __user *status, int start, int value, int nr) |
742755a1 | 1518 | { |
a49bd4d7 MH |
1519 | while (nr-- > 0) { |
1520 | if (put_user(value, status + start)) | |
1521 | return -EFAULT; | |
1522 | start++; | |
1523 | } | |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
1528 | static int do_move_pages_to_node(struct mm_struct *mm, | |
1529 | struct list_head *pagelist, int node) | |
1530 | { | |
1531 | int err; | |
1532 | ||
a49bd4d7 MH |
1533 | err = migrate_pages(pagelist, alloc_new_node_page, NULL, node, |
1534 | MIGRATE_SYNC, MR_SYSCALL); | |
1535 | if (err) | |
1536 | putback_movable_pages(pagelist); | |
1537 | return err; | |
742755a1 CL |
1538 | } |
1539 | ||
1540 | /* | |
a49bd4d7 MH |
1541 | * Resolves the given address to a struct page, isolates it from the LRU and |
1542 | * puts it to the given pagelist. | |
e0153fc2 YS |
1543 | * Returns: |
1544 | * errno - if the page cannot be found/isolated | |
1545 | * 0 - when it doesn't have to be migrated because it is already on the | |
1546 | * target node | |
1547 | * 1 - when it has been queued | |
742755a1 | 1548 | */ |
a49bd4d7 MH |
1549 | static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, |
1550 | int node, struct list_head *pagelist, bool migrate_all) | |
742755a1 | 1551 | { |
a49bd4d7 MH |
1552 | struct vm_area_struct *vma; |
1553 | struct page *page; | |
1554 | unsigned int follflags; | |
742755a1 | 1555 | int err; |
742755a1 CL |
1556 | |
1557 | down_read(&mm->mmap_sem); | |
a49bd4d7 MH |
1558 | err = -EFAULT; |
1559 | vma = find_vma(mm, addr); | |
1560 | if (!vma || addr < vma->vm_start || !vma_migratable(vma)) | |
1561 | goto out; | |
742755a1 | 1562 | |
a49bd4d7 MH |
1563 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1564 | follflags = FOLL_GET | FOLL_DUMP; | |
a49bd4d7 | 1565 | page = follow_page(vma, addr, follflags); |
89f5b7da | 1566 | |
a49bd4d7 MH |
1567 | err = PTR_ERR(page); |
1568 | if (IS_ERR(page)) | |
1569 | goto out; | |
89f5b7da | 1570 | |
a49bd4d7 MH |
1571 | err = -ENOENT; |
1572 | if (!page) | |
1573 | goto out; | |
742755a1 | 1574 | |
a49bd4d7 MH |
1575 | err = 0; |
1576 | if (page_to_nid(page) == node) | |
1577 | goto out_putpage; | |
742755a1 | 1578 | |
a49bd4d7 MH |
1579 | err = -EACCES; |
1580 | if (page_mapcount(page) > 1 && !migrate_all) | |
1581 | goto out_putpage; | |
742755a1 | 1582 | |
a49bd4d7 MH |
1583 | if (PageHuge(page)) { |
1584 | if (PageHead(page)) { | |
1585 | isolate_huge_page(page, pagelist); | |
e0153fc2 | 1586 | err = 1; |
e632a938 | 1587 | } |
a49bd4d7 MH |
1588 | } else { |
1589 | struct page *head; | |
e632a938 | 1590 | |
e8db67eb NH |
1591 | head = compound_head(page); |
1592 | err = isolate_lru_page(head); | |
cf608ac1 | 1593 | if (err) |
a49bd4d7 | 1594 | goto out_putpage; |
742755a1 | 1595 | |
e0153fc2 | 1596 | err = 1; |
a49bd4d7 MH |
1597 | list_add_tail(&head->lru, pagelist); |
1598 | mod_node_page_state(page_pgdat(head), | |
9de4f22a | 1599 | NR_ISOLATED_ANON + page_is_file_lru(head), |
a49bd4d7 MH |
1600 | hpage_nr_pages(head)); |
1601 | } | |
1602 | out_putpage: | |
1603 | /* | |
1604 | * Either remove the duplicate refcount from | |
1605 | * isolate_lru_page() or drop the page ref if it was | |
1606 | * not isolated. | |
1607 | */ | |
1608 | put_page(page); | |
1609 | out: | |
742755a1 CL |
1610 | up_read(&mm->mmap_sem); |
1611 | return err; | |
1612 | } | |
1613 | ||
7ca8783a WY |
1614 | static int move_pages_and_store_status(struct mm_struct *mm, int node, |
1615 | struct list_head *pagelist, int __user *status, | |
1616 | int start, int i, unsigned long nr_pages) | |
1617 | { | |
1618 | int err; | |
1619 | ||
5d7ae891 WY |
1620 | if (list_empty(pagelist)) |
1621 | return 0; | |
1622 | ||
7ca8783a WY |
1623 | err = do_move_pages_to_node(mm, pagelist, node); |
1624 | if (err) { | |
1625 | /* | |
1626 | * Positive err means the number of failed | |
1627 | * pages to migrate. Since we are going to | |
1628 | * abort and return the number of non-migrated | |
1629 | * pages, so need to incude the rest of the | |
1630 | * nr_pages that have not been attempted as | |
1631 | * well. | |
1632 | */ | |
1633 | if (err > 0) | |
1634 | err += nr_pages - i - 1; | |
1635 | return err; | |
1636 | } | |
1637 | return store_status(status, start, node, i - start); | |
1638 | } | |
1639 | ||
5e9a0f02 BG |
1640 | /* |
1641 | * Migrate an array of page address onto an array of nodes and fill | |
1642 | * the corresponding array of status. | |
1643 | */ | |
3268c63e | 1644 | static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, |
5e9a0f02 BG |
1645 | unsigned long nr_pages, |
1646 | const void __user * __user *pages, | |
1647 | const int __user *nodes, | |
1648 | int __user *status, int flags) | |
1649 | { | |
a49bd4d7 MH |
1650 | int current_node = NUMA_NO_NODE; |
1651 | LIST_HEAD(pagelist); | |
1652 | int start, i; | |
1653 | int err = 0, err1; | |
35282a2d BG |
1654 | |
1655 | migrate_prep(); | |
1656 | ||
a49bd4d7 MH |
1657 | for (i = start = 0; i < nr_pages; i++) { |
1658 | const void __user *p; | |
1659 | unsigned long addr; | |
1660 | int node; | |
3140a227 | 1661 | |
a49bd4d7 MH |
1662 | err = -EFAULT; |
1663 | if (get_user(p, pages + i)) | |
1664 | goto out_flush; | |
1665 | if (get_user(node, nodes + i)) | |
1666 | goto out_flush; | |
057d3389 | 1667 | addr = (unsigned long)untagged_addr(p); |
a49bd4d7 MH |
1668 | |
1669 | err = -ENODEV; | |
1670 | if (node < 0 || node >= MAX_NUMNODES) | |
1671 | goto out_flush; | |
1672 | if (!node_state(node, N_MEMORY)) | |
1673 | goto out_flush; | |
5e9a0f02 | 1674 | |
a49bd4d7 MH |
1675 | err = -EACCES; |
1676 | if (!node_isset(node, task_nodes)) | |
1677 | goto out_flush; | |
1678 | ||
1679 | if (current_node == NUMA_NO_NODE) { | |
1680 | current_node = node; | |
1681 | start = i; | |
1682 | } else if (node != current_node) { | |
7ca8783a WY |
1683 | err = move_pages_and_store_status(mm, current_node, |
1684 | &pagelist, status, start, i, nr_pages); | |
a49bd4d7 MH |
1685 | if (err) |
1686 | goto out; | |
1687 | start = i; | |
1688 | current_node = node; | |
3140a227 BG |
1689 | } |
1690 | ||
a49bd4d7 MH |
1691 | /* |
1692 | * Errors in the page lookup or isolation are not fatal and we simply | |
1693 | * report them via status | |
1694 | */ | |
1695 | err = add_page_for_migration(mm, addr, current_node, | |
1696 | &pagelist, flags & MPOL_MF_MOVE_ALL); | |
e0153fc2 | 1697 | |
d08221a0 | 1698 | if (err > 0) { |
e0153fc2 YS |
1699 | /* The page is successfully queued for migration */ |
1700 | continue; | |
1701 | } | |
3140a227 | 1702 | |
d08221a0 WY |
1703 | /* |
1704 | * If the page is already on the target node (!err), store the | |
1705 | * node, otherwise, store the err. | |
1706 | */ | |
1707 | err = store_status(status, i, err ? : current_node, 1); | |
a49bd4d7 MH |
1708 | if (err) |
1709 | goto out_flush; | |
5e9a0f02 | 1710 | |
7ca8783a WY |
1711 | err = move_pages_and_store_status(mm, current_node, &pagelist, |
1712 | status, start, i, nr_pages); | |
4afdacec WY |
1713 | if (err) |
1714 | goto out; | |
a49bd4d7 | 1715 | current_node = NUMA_NO_NODE; |
3140a227 | 1716 | } |
a49bd4d7 MH |
1717 | out_flush: |
1718 | /* Make sure we do not overwrite the existing error */ | |
7ca8783a WY |
1719 | err1 = move_pages_and_store_status(mm, current_node, &pagelist, |
1720 | status, start, i, nr_pages); | |
dfe9aa23 | 1721 | if (err >= 0) |
a49bd4d7 | 1722 | err = err1; |
5e9a0f02 BG |
1723 | out: |
1724 | return err; | |
1725 | } | |
1726 | ||
742755a1 | 1727 | /* |
2f007e74 | 1728 | * Determine the nodes of an array of pages and store it in an array of status. |
742755a1 | 1729 | */ |
80bba129 BG |
1730 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
1731 | const void __user **pages, int *status) | |
742755a1 | 1732 | { |
2f007e74 | 1733 | unsigned long i; |
2f007e74 | 1734 | |
742755a1 CL |
1735 | down_read(&mm->mmap_sem); |
1736 | ||
2f007e74 | 1737 | for (i = 0; i < nr_pages; i++) { |
80bba129 | 1738 | unsigned long addr = (unsigned long)(*pages); |
742755a1 CL |
1739 | struct vm_area_struct *vma; |
1740 | struct page *page; | |
c095adbc | 1741 | int err = -EFAULT; |
2f007e74 BG |
1742 | |
1743 | vma = find_vma(mm, addr); | |
70384dc6 | 1744 | if (!vma || addr < vma->vm_start) |
742755a1 CL |
1745 | goto set_status; |
1746 | ||
d899844e KS |
1747 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1748 | page = follow_page(vma, addr, FOLL_DUMP); | |
89f5b7da LT |
1749 | |
1750 | err = PTR_ERR(page); | |
1751 | if (IS_ERR(page)) | |
1752 | goto set_status; | |
1753 | ||
d899844e | 1754 | err = page ? page_to_nid(page) : -ENOENT; |
742755a1 | 1755 | set_status: |
80bba129 BG |
1756 | *status = err; |
1757 | ||
1758 | pages++; | |
1759 | status++; | |
1760 | } | |
1761 | ||
1762 | up_read(&mm->mmap_sem); | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | * Determine the nodes of a user array of pages and store it in | |
1767 | * a user array of status. | |
1768 | */ | |
1769 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, | |
1770 | const void __user * __user *pages, | |
1771 | int __user *status) | |
1772 | { | |
1773 | #define DO_PAGES_STAT_CHUNK_NR 16 | |
1774 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; | |
1775 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; | |
80bba129 | 1776 | |
87b8d1ad PA |
1777 | while (nr_pages) { |
1778 | unsigned long chunk_nr; | |
80bba129 | 1779 | |
87b8d1ad PA |
1780 | chunk_nr = nr_pages; |
1781 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) | |
1782 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; | |
1783 | ||
1784 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) | |
1785 | break; | |
80bba129 BG |
1786 | |
1787 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); | |
1788 | ||
87b8d1ad PA |
1789 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
1790 | break; | |
742755a1 | 1791 | |
87b8d1ad PA |
1792 | pages += chunk_nr; |
1793 | status += chunk_nr; | |
1794 | nr_pages -= chunk_nr; | |
1795 | } | |
1796 | return nr_pages ? -EFAULT : 0; | |
742755a1 CL |
1797 | } |
1798 | ||
1799 | /* | |
1800 | * Move a list of pages in the address space of the currently executing | |
1801 | * process. | |
1802 | */ | |
7addf443 DB |
1803 | static int kernel_move_pages(pid_t pid, unsigned long nr_pages, |
1804 | const void __user * __user *pages, | |
1805 | const int __user *nodes, | |
1806 | int __user *status, int flags) | |
742755a1 | 1807 | { |
742755a1 | 1808 | struct task_struct *task; |
742755a1 | 1809 | struct mm_struct *mm; |
5e9a0f02 | 1810 | int err; |
3268c63e | 1811 | nodemask_t task_nodes; |
742755a1 CL |
1812 | |
1813 | /* Check flags */ | |
1814 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) | |
1815 | return -EINVAL; | |
1816 | ||
1817 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) | |
1818 | return -EPERM; | |
1819 | ||
1820 | /* Find the mm_struct */ | |
a879bf58 | 1821 | rcu_read_lock(); |
228ebcbe | 1822 | task = pid ? find_task_by_vpid(pid) : current; |
742755a1 | 1823 | if (!task) { |
a879bf58 | 1824 | rcu_read_unlock(); |
742755a1 CL |
1825 | return -ESRCH; |
1826 | } | |
3268c63e | 1827 | get_task_struct(task); |
742755a1 CL |
1828 | |
1829 | /* | |
1830 | * Check if this process has the right to modify the specified | |
197e7e52 | 1831 | * process. Use the regular "ptrace_may_access()" checks. |
742755a1 | 1832 | */ |
197e7e52 | 1833 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
c69e8d9c | 1834 | rcu_read_unlock(); |
742755a1 | 1835 | err = -EPERM; |
5e9a0f02 | 1836 | goto out; |
742755a1 | 1837 | } |
c69e8d9c | 1838 | rcu_read_unlock(); |
742755a1 | 1839 | |
86c3a764 DQ |
1840 | err = security_task_movememory(task); |
1841 | if (err) | |
5e9a0f02 | 1842 | goto out; |
86c3a764 | 1843 | |
3268c63e CL |
1844 | task_nodes = cpuset_mems_allowed(task); |
1845 | mm = get_task_mm(task); | |
1846 | put_task_struct(task); | |
1847 | ||
6e8b09ea SL |
1848 | if (!mm) |
1849 | return -EINVAL; | |
1850 | ||
1851 | if (nodes) | |
1852 | err = do_pages_move(mm, task_nodes, nr_pages, pages, | |
1853 | nodes, status, flags); | |
1854 | else | |
1855 | err = do_pages_stat(mm, nr_pages, pages, status); | |
742755a1 | 1856 | |
742755a1 CL |
1857 | mmput(mm); |
1858 | return err; | |
3268c63e CL |
1859 | |
1860 | out: | |
1861 | put_task_struct(task); | |
1862 | return err; | |
742755a1 | 1863 | } |
742755a1 | 1864 | |
7addf443 DB |
1865 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
1866 | const void __user * __user *, pages, | |
1867 | const int __user *, nodes, | |
1868 | int __user *, status, int, flags) | |
1869 | { | |
1870 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
1871 | } | |
1872 | ||
1873 | #ifdef CONFIG_COMPAT | |
1874 | COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, | |
1875 | compat_uptr_t __user *, pages32, | |
1876 | const int __user *, nodes, | |
1877 | int __user *, status, | |
1878 | int, flags) | |
1879 | { | |
1880 | const void __user * __user *pages; | |
1881 | int i; | |
1882 | ||
1883 | pages = compat_alloc_user_space(nr_pages * sizeof(void *)); | |
1884 | for (i = 0; i < nr_pages; i++) { | |
1885 | compat_uptr_t p; | |
1886 | ||
1887 | if (get_user(p, pages32 + i) || | |
1888 | put_user(compat_ptr(p), pages + i)) | |
1889 | return -EFAULT; | |
1890 | } | |
1891 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
1892 | } | |
1893 | #endif /* CONFIG_COMPAT */ | |
1894 | ||
7039e1db PZ |
1895 | #ifdef CONFIG_NUMA_BALANCING |
1896 | /* | |
1897 | * Returns true if this is a safe migration target node for misplaced NUMA | |
1898 | * pages. Currently it only checks the watermarks which crude | |
1899 | */ | |
1900 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, | |
3abef4e6 | 1901 | unsigned long nr_migrate_pages) |
7039e1db PZ |
1902 | { |
1903 | int z; | |
599d0c95 | 1904 | |
7039e1db PZ |
1905 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
1906 | struct zone *zone = pgdat->node_zones + z; | |
1907 | ||
1908 | if (!populated_zone(zone)) | |
1909 | continue; | |
1910 | ||
7039e1db PZ |
1911 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
1912 | if (!zone_watermark_ok(zone, 0, | |
1913 | high_wmark_pages(zone) + | |
1914 | nr_migrate_pages, | |
bfe9d006 | 1915 | ZONE_MOVABLE, 0)) |
7039e1db PZ |
1916 | continue; |
1917 | return true; | |
1918 | } | |
1919 | return false; | |
1920 | } | |
1921 | ||
1922 | static struct page *alloc_misplaced_dst_page(struct page *page, | |
666feb21 | 1923 | unsigned long data) |
7039e1db PZ |
1924 | { |
1925 | int nid = (int) data; | |
1926 | struct page *newpage; | |
1927 | ||
96db800f | 1928 | newpage = __alloc_pages_node(nid, |
e97ca8e5 JW |
1929 | (GFP_HIGHUSER_MOVABLE | |
1930 | __GFP_THISNODE | __GFP_NOMEMALLOC | | |
1931 | __GFP_NORETRY | __GFP_NOWARN) & | |
8479eba7 | 1932 | ~__GFP_RECLAIM, 0); |
bac0382c | 1933 | |
7039e1db PZ |
1934 | return newpage; |
1935 | } | |
1936 | ||
1c30e017 | 1937 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
b32967ff | 1938 | { |
340ef390 | 1939 | int page_lru; |
a8f60772 | 1940 | |
309381fe | 1941 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
3abef4e6 | 1942 | |
7039e1db | 1943 | /* Avoid migrating to a node that is nearly full */ |
d8c6546b | 1944 | if (!migrate_balanced_pgdat(pgdat, compound_nr(page))) |
340ef390 | 1945 | return 0; |
7039e1db | 1946 | |
340ef390 HD |
1947 | if (isolate_lru_page(page)) |
1948 | return 0; | |
7039e1db | 1949 | |
340ef390 HD |
1950 | /* |
1951 | * migrate_misplaced_transhuge_page() skips page migration's usual | |
1952 | * check on page_count(), so we must do it here, now that the page | |
1953 | * has been isolated: a GUP pin, or any other pin, prevents migration. | |
1954 | * The expected page count is 3: 1 for page's mapcount and 1 for the | |
1955 | * caller's pin and 1 for the reference taken by isolate_lru_page(). | |
1956 | */ | |
1957 | if (PageTransHuge(page) && page_count(page) != 3) { | |
1958 | putback_lru_page(page); | |
1959 | return 0; | |
7039e1db PZ |
1960 | } |
1961 | ||
9de4f22a | 1962 | page_lru = page_is_file_lru(page); |
599d0c95 | 1963 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
340ef390 HD |
1964 | hpage_nr_pages(page)); |
1965 | ||
149c33e1 | 1966 | /* |
340ef390 HD |
1967 | * Isolating the page has taken another reference, so the |
1968 | * caller's reference can be safely dropped without the page | |
1969 | * disappearing underneath us during migration. | |
149c33e1 MG |
1970 | */ |
1971 | put_page(page); | |
340ef390 | 1972 | return 1; |
b32967ff MG |
1973 | } |
1974 | ||
de466bd6 MG |
1975 | bool pmd_trans_migrating(pmd_t pmd) |
1976 | { | |
1977 | struct page *page = pmd_page(pmd); | |
1978 | return PageLocked(page); | |
1979 | } | |
1980 | ||
b32967ff MG |
1981 | /* |
1982 | * Attempt to migrate a misplaced page to the specified destination | |
1983 | * node. Caller is expected to have an elevated reference count on | |
1984 | * the page that will be dropped by this function before returning. | |
1985 | */ | |
1bc115d8 MG |
1986 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
1987 | int node) | |
b32967ff MG |
1988 | { |
1989 | pg_data_t *pgdat = NODE_DATA(node); | |
340ef390 | 1990 | int isolated; |
b32967ff MG |
1991 | int nr_remaining; |
1992 | LIST_HEAD(migratepages); | |
1993 | ||
1994 | /* | |
1bc115d8 MG |
1995 | * Don't migrate file pages that are mapped in multiple processes |
1996 | * with execute permissions as they are probably shared libraries. | |
b32967ff | 1997 | */ |
9de4f22a | 1998 | if (page_mapcount(page) != 1 && page_is_file_lru(page) && |
1bc115d8 | 1999 | (vma->vm_flags & VM_EXEC)) |
b32967ff | 2000 | goto out; |
b32967ff | 2001 | |
09a913a7 MG |
2002 | /* |
2003 | * Also do not migrate dirty pages as not all filesystems can move | |
2004 | * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles. | |
2005 | */ | |
9de4f22a | 2006 | if (page_is_file_lru(page) && PageDirty(page)) |
09a913a7 MG |
2007 | goto out; |
2008 | ||
b32967ff MG |
2009 | isolated = numamigrate_isolate_page(pgdat, page); |
2010 | if (!isolated) | |
2011 | goto out; | |
2012 | ||
2013 | list_add(&page->lru, &migratepages); | |
9c620e2b | 2014 | nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, |
68711a74 DR |
2015 | NULL, node, MIGRATE_ASYNC, |
2016 | MR_NUMA_MISPLACED); | |
b32967ff | 2017 | if (nr_remaining) { |
59c82b70 JK |
2018 | if (!list_empty(&migratepages)) { |
2019 | list_del(&page->lru); | |
599d0c95 | 2020 | dec_node_page_state(page, NR_ISOLATED_ANON + |
9de4f22a | 2021 | page_is_file_lru(page)); |
59c82b70 JK |
2022 | putback_lru_page(page); |
2023 | } | |
b32967ff MG |
2024 | isolated = 0; |
2025 | } else | |
2026 | count_vm_numa_event(NUMA_PAGE_MIGRATE); | |
7039e1db | 2027 | BUG_ON(!list_empty(&migratepages)); |
7039e1db | 2028 | return isolated; |
340ef390 HD |
2029 | |
2030 | out: | |
2031 | put_page(page); | |
2032 | return 0; | |
7039e1db | 2033 | } |
220018d3 | 2034 | #endif /* CONFIG_NUMA_BALANCING */ |
b32967ff | 2035 | |
220018d3 | 2036 | #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
340ef390 HD |
2037 | /* |
2038 | * Migrates a THP to a given target node. page must be locked and is unlocked | |
2039 | * before returning. | |
2040 | */ | |
b32967ff MG |
2041 | int migrate_misplaced_transhuge_page(struct mm_struct *mm, |
2042 | struct vm_area_struct *vma, | |
2043 | pmd_t *pmd, pmd_t entry, | |
2044 | unsigned long address, | |
2045 | struct page *page, int node) | |
2046 | { | |
c4088ebd | 2047 | spinlock_t *ptl; |
b32967ff MG |
2048 | pg_data_t *pgdat = NODE_DATA(node); |
2049 | int isolated = 0; | |
2050 | struct page *new_page = NULL; | |
9de4f22a | 2051 | int page_lru = page_is_file_lru(page); |
7066f0f9 | 2052 | unsigned long start = address & HPAGE_PMD_MASK; |
b32967ff | 2053 | |
b32967ff | 2054 | new_page = alloc_pages_node(node, |
25160354 | 2055 | (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), |
e97ca8e5 | 2056 | HPAGE_PMD_ORDER); |
340ef390 HD |
2057 | if (!new_page) |
2058 | goto out_fail; | |
9a982250 | 2059 | prep_transhuge_page(new_page); |
340ef390 | 2060 | |
b32967ff | 2061 | isolated = numamigrate_isolate_page(pgdat, page); |
340ef390 | 2062 | if (!isolated) { |
b32967ff | 2063 | put_page(new_page); |
340ef390 | 2064 | goto out_fail; |
b32967ff | 2065 | } |
b0943d61 | 2066 | |
b32967ff | 2067 | /* Prepare a page as a migration target */ |
48c935ad | 2068 | __SetPageLocked(new_page); |
d44d363f SL |
2069 | if (PageSwapBacked(page)) |
2070 | __SetPageSwapBacked(new_page); | |
b32967ff MG |
2071 | |
2072 | /* anon mapping, we can simply copy page->mapping to the new page: */ | |
2073 | new_page->mapping = page->mapping; | |
2074 | new_page->index = page->index; | |
7eef5f97 AA |
2075 | /* flush the cache before copying using the kernel virtual address */ |
2076 | flush_cache_range(vma, start, start + HPAGE_PMD_SIZE); | |
b32967ff MG |
2077 | migrate_page_copy(new_page, page); |
2078 | WARN_ON(PageLRU(new_page)); | |
2079 | ||
2080 | /* Recheck the target PMD */ | |
c4088ebd | 2081 | ptl = pmd_lock(mm, pmd); |
f4e177d1 | 2082 | if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) { |
c4088ebd | 2083 | spin_unlock(ptl); |
b32967ff MG |
2084 | |
2085 | /* Reverse changes made by migrate_page_copy() */ | |
2086 | if (TestClearPageActive(new_page)) | |
2087 | SetPageActive(page); | |
2088 | if (TestClearPageUnevictable(new_page)) | |
2089 | SetPageUnevictable(page); | |
b32967ff MG |
2090 | |
2091 | unlock_page(new_page); | |
2092 | put_page(new_page); /* Free it */ | |
2093 | ||
a54a407f MG |
2094 | /* Retake the callers reference and putback on LRU */ |
2095 | get_page(page); | |
b32967ff | 2096 | putback_lru_page(page); |
599d0c95 | 2097 | mod_node_page_state(page_pgdat(page), |
a54a407f | 2098 | NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); |
eb4489f6 MG |
2099 | |
2100 | goto out_unlock; | |
b32967ff MG |
2101 | } |
2102 | ||
10102459 | 2103 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
f55e1014 | 2104 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
b32967ff | 2105 | |
2b4847e7 | 2106 | /* |
d7c33934 AA |
2107 | * Overwrite the old entry under pagetable lock and establish |
2108 | * the new PTE. Any parallel GUP will either observe the old | |
2109 | * page blocking on the page lock, block on the page table | |
2110 | * lock or observe the new page. The SetPageUptodate on the | |
2111 | * new page and page_add_new_anon_rmap guarantee the copy is | |
2112 | * visible before the pagetable update. | |
2b4847e7 | 2113 | */ |
7066f0f9 | 2114 | page_add_anon_rmap(new_page, vma, start, true); |
d7c33934 AA |
2115 | /* |
2116 | * At this point the pmd is numa/protnone (i.e. non present) and the TLB | |
2117 | * has already been flushed globally. So no TLB can be currently | |
2118 | * caching this non present pmd mapping. There's no need to clear the | |
2119 | * pmd before doing set_pmd_at(), nor to flush the TLB after | |
2120 | * set_pmd_at(). Clearing the pmd here would introduce a race | |
2121 | * condition against MADV_DONTNEED, because MADV_DONTNEED only holds the | |
2122 | * mmap_sem for reading. If the pmd is set to NULL at any given time, | |
2123 | * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this | |
2124 | * pmd. | |
2125 | */ | |
7066f0f9 | 2126 | set_pmd_at(mm, start, pmd, entry); |
ce4a9cc5 | 2127 | update_mmu_cache_pmd(vma, address, &entry); |
2b4847e7 | 2128 | |
f4e177d1 | 2129 | page_ref_unfreeze(page, 2); |
51afb12b | 2130 | mlock_migrate_page(new_page, page); |
d281ee61 | 2131 | page_remove_rmap(page, true); |
7cd12b4a | 2132 | set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); |
2b4847e7 | 2133 | |
c4088ebd | 2134 | spin_unlock(ptl); |
b32967ff | 2135 | |
11de9927 MG |
2136 | /* Take an "isolate" reference and put new page on the LRU. */ |
2137 | get_page(new_page); | |
2138 | putback_lru_page(new_page); | |
2139 | ||
b32967ff MG |
2140 | unlock_page(new_page); |
2141 | unlock_page(page); | |
2142 | put_page(page); /* Drop the rmap reference */ | |
2143 | put_page(page); /* Drop the LRU isolation reference */ | |
2144 | ||
2145 | count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); | |
2146 | count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); | |
2147 | ||
599d0c95 | 2148 | mod_node_page_state(page_pgdat(page), |
b32967ff MG |
2149 | NR_ISOLATED_ANON + page_lru, |
2150 | -HPAGE_PMD_NR); | |
2151 | return isolated; | |
2152 | ||
340ef390 HD |
2153 | out_fail: |
2154 | count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); | |
2b4847e7 MG |
2155 | ptl = pmd_lock(mm, pmd); |
2156 | if (pmd_same(*pmd, entry)) { | |
4d942466 | 2157 | entry = pmd_modify(entry, vma->vm_page_prot); |
7066f0f9 | 2158 | set_pmd_at(mm, start, pmd, entry); |
2b4847e7 MG |
2159 | update_mmu_cache_pmd(vma, address, &entry); |
2160 | } | |
2161 | spin_unlock(ptl); | |
a54a407f | 2162 | |
eb4489f6 | 2163 | out_unlock: |
340ef390 | 2164 | unlock_page(page); |
b32967ff | 2165 | put_page(page); |
b32967ff MG |
2166 | return 0; |
2167 | } | |
7039e1db PZ |
2168 | #endif /* CONFIG_NUMA_BALANCING */ |
2169 | ||
2170 | #endif /* CONFIG_NUMA */ | |
8763cb45 | 2171 | |
9b2ed9cb | 2172 | #ifdef CONFIG_DEVICE_PRIVATE |
8763cb45 JG |
2173 | static int migrate_vma_collect_hole(unsigned long start, |
2174 | unsigned long end, | |
b7a16c7a | 2175 | __always_unused int depth, |
8763cb45 JG |
2176 | struct mm_walk *walk) |
2177 | { | |
2178 | struct migrate_vma *migrate = walk->private; | |
2179 | unsigned long addr; | |
2180 | ||
872ea707 | 2181 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
e20d103b | 2182 | migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; |
8315ada7 | 2183 | migrate->dst[migrate->npages] = 0; |
e20d103b | 2184 | migrate->npages++; |
8315ada7 JG |
2185 | migrate->cpages++; |
2186 | } | |
2187 | ||
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | static int migrate_vma_collect_skip(unsigned long start, | |
2192 | unsigned long end, | |
2193 | struct mm_walk *walk) | |
2194 | { | |
2195 | struct migrate_vma *migrate = walk->private; | |
2196 | unsigned long addr; | |
2197 | ||
872ea707 | 2198 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
8763cb45 JG |
2199 | migrate->dst[migrate->npages] = 0; |
2200 | migrate->src[migrate->npages++] = 0; | |
2201 | } | |
2202 | ||
2203 | return 0; | |
2204 | } | |
2205 | ||
2206 | static int migrate_vma_collect_pmd(pmd_t *pmdp, | |
2207 | unsigned long start, | |
2208 | unsigned long end, | |
2209 | struct mm_walk *walk) | |
2210 | { | |
2211 | struct migrate_vma *migrate = walk->private; | |
2212 | struct vm_area_struct *vma = walk->vma; | |
2213 | struct mm_struct *mm = vma->vm_mm; | |
8c3328f1 | 2214 | unsigned long addr = start, unmapped = 0; |
8763cb45 JG |
2215 | spinlock_t *ptl; |
2216 | pte_t *ptep; | |
2217 | ||
2218 | again: | |
2219 | if (pmd_none(*pmdp)) | |
b7a16c7a | 2220 | return migrate_vma_collect_hole(start, end, -1, walk); |
8763cb45 JG |
2221 | |
2222 | if (pmd_trans_huge(*pmdp)) { | |
2223 | struct page *page; | |
2224 | ||
2225 | ptl = pmd_lock(mm, pmdp); | |
2226 | if (unlikely(!pmd_trans_huge(*pmdp))) { | |
2227 | spin_unlock(ptl); | |
2228 | goto again; | |
2229 | } | |
2230 | ||
2231 | page = pmd_page(*pmdp); | |
2232 | if (is_huge_zero_page(page)) { | |
2233 | spin_unlock(ptl); | |
2234 | split_huge_pmd(vma, pmdp, addr); | |
2235 | if (pmd_trans_unstable(pmdp)) | |
8315ada7 | 2236 | return migrate_vma_collect_skip(start, end, |
8763cb45 JG |
2237 | walk); |
2238 | } else { | |
2239 | int ret; | |
2240 | ||
2241 | get_page(page); | |
2242 | spin_unlock(ptl); | |
2243 | if (unlikely(!trylock_page(page))) | |
8315ada7 | 2244 | return migrate_vma_collect_skip(start, end, |
8763cb45 JG |
2245 | walk); |
2246 | ret = split_huge_page(page); | |
2247 | unlock_page(page); | |
2248 | put_page(page); | |
8315ada7 JG |
2249 | if (ret) |
2250 | return migrate_vma_collect_skip(start, end, | |
2251 | walk); | |
2252 | if (pmd_none(*pmdp)) | |
b7a16c7a | 2253 | return migrate_vma_collect_hole(start, end, -1, |
8763cb45 JG |
2254 | walk); |
2255 | } | |
2256 | } | |
2257 | ||
2258 | if (unlikely(pmd_bad(*pmdp))) | |
8315ada7 | 2259 | return migrate_vma_collect_skip(start, end, walk); |
8763cb45 JG |
2260 | |
2261 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
8c3328f1 JG |
2262 | arch_enter_lazy_mmu_mode(); |
2263 | ||
8763cb45 | 2264 | for (; addr < end; addr += PAGE_SIZE, ptep++) { |
800bb1c8 | 2265 | unsigned long mpfn = 0, pfn; |
8763cb45 | 2266 | struct page *page; |
8c3328f1 | 2267 | swp_entry_t entry; |
8763cb45 JG |
2268 | pte_t pte; |
2269 | ||
2270 | pte = *ptep; | |
8763cb45 | 2271 | |
a5430dda | 2272 | if (pte_none(pte)) { |
8315ada7 JG |
2273 | mpfn = MIGRATE_PFN_MIGRATE; |
2274 | migrate->cpages++; | |
8763cb45 JG |
2275 | goto next; |
2276 | } | |
2277 | ||
a5430dda | 2278 | if (!pte_present(pte)) { |
a5430dda JG |
2279 | /* |
2280 | * Only care about unaddressable device page special | |
2281 | * page table entry. Other special swap entries are not | |
2282 | * migratable, and we ignore regular swapped page. | |
2283 | */ | |
2284 | entry = pte_to_swp_entry(pte); | |
2285 | if (!is_device_private_entry(entry)) | |
2286 | goto next; | |
2287 | ||
2288 | page = device_private_entry_to_page(entry); | |
800bb1c8 CH |
2289 | if (page->pgmap->owner != migrate->src_owner) |
2290 | goto next; | |
2291 | ||
06d462be CH |
2292 | mpfn = migrate_pfn(page_to_pfn(page)) | |
2293 | MIGRATE_PFN_MIGRATE; | |
a5430dda JG |
2294 | if (is_write_device_private_entry(entry)) |
2295 | mpfn |= MIGRATE_PFN_WRITE; | |
2296 | } else { | |
800bb1c8 CH |
2297 | if (migrate->src_owner) |
2298 | goto next; | |
276f756d | 2299 | pfn = pte_pfn(pte); |
8315ada7 JG |
2300 | if (is_zero_pfn(pfn)) { |
2301 | mpfn = MIGRATE_PFN_MIGRATE; | |
2302 | migrate->cpages++; | |
8315ada7 JG |
2303 | goto next; |
2304 | } | |
25b2995a | 2305 | page = vm_normal_page(migrate->vma, addr, pte); |
a5430dda JG |
2306 | mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; |
2307 | mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; | |
2308 | } | |
2309 | ||
8763cb45 | 2310 | /* FIXME support THP */ |
8763cb45 | 2311 | if (!page || !page->mapping || PageTransCompound(page)) { |
276f756d | 2312 | mpfn = 0; |
8763cb45 JG |
2313 | goto next; |
2314 | } | |
2315 | ||
2316 | /* | |
2317 | * By getting a reference on the page we pin it and that blocks | |
2318 | * any kind of migration. Side effect is that it "freezes" the | |
2319 | * pte. | |
2320 | * | |
2321 | * We drop this reference after isolating the page from the lru | |
2322 | * for non device page (device page are not on the lru and thus | |
2323 | * can't be dropped from it). | |
2324 | */ | |
2325 | get_page(page); | |
2326 | migrate->cpages++; | |
8763cb45 | 2327 | |
8c3328f1 JG |
2328 | /* |
2329 | * Optimize for the common case where page is only mapped once | |
2330 | * in one process. If we can lock the page, then we can safely | |
2331 | * set up a special migration page table entry now. | |
2332 | */ | |
2333 | if (trylock_page(page)) { | |
2334 | pte_t swp_pte; | |
2335 | ||
2336 | mpfn |= MIGRATE_PFN_LOCKED; | |
2337 | ptep_get_and_clear(mm, addr, ptep); | |
2338 | ||
2339 | /* Setup special migration page table entry */ | |
07707125 RC |
2340 | entry = make_migration_entry(page, mpfn & |
2341 | MIGRATE_PFN_WRITE); | |
8c3328f1 JG |
2342 | swp_pte = swp_entry_to_pte(entry); |
2343 | if (pte_soft_dirty(pte)) | |
2344 | swp_pte = pte_swp_mksoft_dirty(swp_pte); | |
f45ec5ff PX |
2345 | if (pte_uffd_wp(pte)) |
2346 | swp_pte = pte_swp_mkuffd_wp(swp_pte); | |
8c3328f1 JG |
2347 | set_pte_at(mm, addr, ptep, swp_pte); |
2348 | ||
2349 | /* | |
2350 | * This is like regular unmap: we remove the rmap and | |
2351 | * drop page refcount. Page won't be freed, as we took | |
2352 | * a reference just above. | |
2353 | */ | |
2354 | page_remove_rmap(page, false); | |
2355 | put_page(page); | |
a5430dda JG |
2356 | |
2357 | if (pte_present(pte)) | |
2358 | unmapped++; | |
8c3328f1 JG |
2359 | } |
2360 | ||
8763cb45 | 2361 | next: |
a5430dda | 2362 | migrate->dst[migrate->npages] = 0; |
8763cb45 JG |
2363 | migrate->src[migrate->npages++] = mpfn; |
2364 | } | |
8c3328f1 | 2365 | arch_leave_lazy_mmu_mode(); |
8763cb45 JG |
2366 | pte_unmap_unlock(ptep - 1, ptl); |
2367 | ||
8c3328f1 JG |
2368 | /* Only flush the TLB if we actually modified any entries */ |
2369 | if (unmapped) | |
2370 | flush_tlb_range(walk->vma, start, end); | |
2371 | ||
8763cb45 JG |
2372 | return 0; |
2373 | } | |
2374 | ||
7b86ac33 CH |
2375 | static const struct mm_walk_ops migrate_vma_walk_ops = { |
2376 | .pmd_entry = migrate_vma_collect_pmd, | |
2377 | .pte_hole = migrate_vma_collect_hole, | |
2378 | }; | |
2379 | ||
8763cb45 JG |
2380 | /* |
2381 | * migrate_vma_collect() - collect pages over a range of virtual addresses | |
2382 | * @migrate: migrate struct containing all migration information | |
2383 | * | |
2384 | * This will walk the CPU page table. For each virtual address backed by a | |
2385 | * valid page, it updates the src array and takes a reference on the page, in | |
2386 | * order to pin the page until we lock it and unmap it. | |
2387 | */ | |
2388 | static void migrate_vma_collect(struct migrate_vma *migrate) | |
2389 | { | |
ac46d4f3 | 2390 | struct mmu_notifier_range range; |
8763cb45 | 2391 | |
7b86ac33 CH |
2392 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, |
2393 | migrate->vma->vm_mm, migrate->start, migrate->end); | |
ac46d4f3 | 2394 | mmu_notifier_invalidate_range_start(&range); |
8763cb45 | 2395 | |
7b86ac33 CH |
2396 | walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end, |
2397 | &migrate_vma_walk_ops, migrate); | |
2398 | ||
2399 | mmu_notifier_invalidate_range_end(&range); | |
8763cb45 JG |
2400 | migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); |
2401 | } | |
2402 | ||
2403 | /* | |
2404 | * migrate_vma_check_page() - check if page is pinned or not | |
2405 | * @page: struct page to check | |
2406 | * | |
2407 | * Pinned pages cannot be migrated. This is the same test as in | |
2408 | * migrate_page_move_mapping(), except that here we allow migration of a | |
2409 | * ZONE_DEVICE page. | |
2410 | */ | |
2411 | static bool migrate_vma_check_page(struct page *page) | |
2412 | { | |
2413 | /* | |
2414 | * One extra ref because caller holds an extra reference, either from | |
2415 | * isolate_lru_page() for a regular page, or migrate_vma_collect() for | |
2416 | * a device page. | |
2417 | */ | |
2418 | int extra = 1; | |
2419 | ||
2420 | /* | |
2421 | * FIXME support THP (transparent huge page), it is bit more complex to | |
2422 | * check them than regular pages, because they can be mapped with a pmd | |
2423 | * or with a pte (split pte mapping). | |
2424 | */ | |
2425 | if (PageCompound(page)) | |
2426 | return false; | |
2427 | ||
a5430dda JG |
2428 | /* Page from ZONE_DEVICE have one extra reference */ |
2429 | if (is_zone_device_page(page)) { | |
2430 | /* | |
2431 | * Private page can never be pin as they have no valid pte and | |
2432 | * GUP will fail for those. Yet if there is a pending migration | |
2433 | * a thread might try to wait on the pte migration entry and | |
2434 | * will bump the page reference count. Sadly there is no way to | |
2435 | * differentiate a regular pin from migration wait. Hence to | |
2436 | * avoid 2 racing thread trying to migrate back to CPU to enter | |
2437 | * infinite loop (one stoping migration because the other is | |
2438 | * waiting on pte migration entry). We always return true here. | |
2439 | * | |
2440 | * FIXME proper solution is to rework migration_entry_wait() so | |
2441 | * it does not need to take a reference on page. | |
2442 | */ | |
25b2995a | 2443 | return is_device_private_page(page); |
a5430dda JG |
2444 | } |
2445 | ||
df6ad698 JG |
2446 | /* For file back page */ |
2447 | if (page_mapping(page)) | |
2448 | extra += 1 + page_has_private(page); | |
2449 | ||
8763cb45 JG |
2450 | if ((page_count(page) - extra) > page_mapcount(page)) |
2451 | return false; | |
2452 | ||
2453 | return true; | |
2454 | } | |
2455 | ||
2456 | /* | |
2457 | * migrate_vma_prepare() - lock pages and isolate them from the lru | |
2458 | * @migrate: migrate struct containing all migration information | |
2459 | * | |
2460 | * This locks pages that have been collected by migrate_vma_collect(). Once each | |
2461 | * page is locked it is isolated from the lru (for non-device pages). Finally, | |
2462 | * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be | |
2463 | * migrated by concurrent kernel threads. | |
2464 | */ | |
2465 | static void migrate_vma_prepare(struct migrate_vma *migrate) | |
2466 | { | |
2467 | const unsigned long npages = migrate->npages; | |
8c3328f1 JG |
2468 | const unsigned long start = migrate->start; |
2469 | unsigned long addr, i, restore = 0; | |
8763cb45 | 2470 | bool allow_drain = true; |
8763cb45 JG |
2471 | |
2472 | lru_add_drain(); | |
2473 | ||
2474 | for (i = 0; (i < npages) && migrate->cpages; i++) { | |
2475 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
8c3328f1 | 2476 | bool remap = true; |
8763cb45 JG |
2477 | |
2478 | if (!page) | |
2479 | continue; | |
2480 | ||
8c3328f1 JG |
2481 | if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) { |
2482 | /* | |
2483 | * Because we are migrating several pages there can be | |
2484 | * a deadlock between 2 concurrent migration where each | |
2485 | * are waiting on each other page lock. | |
2486 | * | |
2487 | * Make migrate_vma() a best effort thing and backoff | |
2488 | * for any page we can not lock right away. | |
2489 | */ | |
2490 | if (!trylock_page(page)) { | |
2491 | migrate->src[i] = 0; | |
2492 | migrate->cpages--; | |
2493 | put_page(page); | |
2494 | continue; | |
2495 | } | |
2496 | remap = false; | |
2497 | migrate->src[i] |= MIGRATE_PFN_LOCKED; | |
8763cb45 | 2498 | } |
8763cb45 | 2499 | |
a5430dda JG |
2500 | /* ZONE_DEVICE pages are not on LRU */ |
2501 | if (!is_zone_device_page(page)) { | |
2502 | if (!PageLRU(page) && allow_drain) { | |
2503 | /* Drain CPU's pagevec */ | |
2504 | lru_add_drain_all(); | |
2505 | allow_drain = false; | |
2506 | } | |
8763cb45 | 2507 | |
a5430dda JG |
2508 | if (isolate_lru_page(page)) { |
2509 | if (remap) { | |
2510 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2511 | migrate->cpages--; | |
2512 | restore++; | |
2513 | } else { | |
2514 | migrate->src[i] = 0; | |
2515 | unlock_page(page); | |
2516 | migrate->cpages--; | |
2517 | put_page(page); | |
2518 | } | |
2519 | continue; | |
8c3328f1 | 2520 | } |
a5430dda JG |
2521 | |
2522 | /* Drop the reference we took in collect */ | |
2523 | put_page(page); | |
8763cb45 JG |
2524 | } |
2525 | ||
2526 | if (!migrate_vma_check_page(page)) { | |
8c3328f1 JG |
2527 | if (remap) { |
2528 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2529 | migrate->cpages--; | |
2530 | restore++; | |
8763cb45 | 2531 | |
a5430dda JG |
2532 | if (!is_zone_device_page(page)) { |
2533 | get_page(page); | |
2534 | putback_lru_page(page); | |
2535 | } | |
8c3328f1 JG |
2536 | } else { |
2537 | migrate->src[i] = 0; | |
2538 | unlock_page(page); | |
2539 | migrate->cpages--; | |
2540 | ||
a5430dda JG |
2541 | if (!is_zone_device_page(page)) |
2542 | putback_lru_page(page); | |
2543 | else | |
2544 | put_page(page); | |
8c3328f1 | 2545 | } |
8763cb45 JG |
2546 | } |
2547 | } | |
8c3328f1 JG |
2548 | |
2549 | for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) { | |
2550 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2551 | ||
2552 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2553 | continue; | |
2554 | ||
2555 | remove_migration_pte(page, migrate->vma, addr, page); | |
2556 | ||
2557 | migrate->src[i] = 0; | |
2558 | unlock_page(page); | |
2559 | put_page(page); | |
2560 | restore--; | |
2561 | } | |
8763cb45 JG |
2562 | } |
2563 | ||
2564 | /* | |
2565 | * migrate_vma_unmap() - replace page mapping with special migration pte entry | |
2566 | * @migrate: migrate struct containing all migration information | |
2567 | * | |
2568 | * Replace page mapping (CPU page table pte) with a special migration pte entry | |
2569 | * and check again if it has been pinned. Pinned pages are restored because we | |
2570 | * cannot migrate them. | |
2571 | * | |
2572 | * This is the last step before we call the device driver callback to allocate | |
2573 | * destination memory and copy contents of original page over to new page. | |
2574 | */ | |
2575 | static void migrate_vma_unmap(struct migrate_vma *migrate) | |
2576 | { | |
2577 | int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS; | |
2578 | const unsigned long npages = migrate->npages; | |
2579 | const unsigned long start = migrate->start; | |
2580 | unsigned long addr, i, restore = 0; | |
2581 | ||
2582 | for (i = 0; i < npages; i++) { | |
2583 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2584 | ||
2585 | if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2586 | continue; | |
2587 | ||
8c3328f1 JG |
2588 | if (page_mapped(page)) { |
2589 | try_to_unmap(page, flags); | |
2590 | if (page_mapped(page)) | |
2591 | goto restore; | |
8763cb45 | 2592 | } |
8c3328f1 JG |
2593 | |
2594 | if (migrate_vma_check_page(page)) | |
2595 | continue; | |
2596 | ||
2597 | restore: | |
2598 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2599 | migrate->cpages--; | |
2600 | restore++; | |
8763cb45 JG |
2601 | } |
2602 | ||
2603 | for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) { | |
2604 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2605 | ||
2606 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2607 | continue; | |
2608 | ||
2609 | remove_migration_ptes(page, page, false); | |
2610 | ||
2611 | migrate->src[i] = 0; | |
2612 | unlock_page(page); | |
2613 | restore--; | |
2614 | ||
a5430dda JG |
2615 | if (is_zone_device_page(page)) |
2616 | put_page(page); | |
2617 | else | |
2618 | putback_lru_page(page); | |
8763cb45 JG |
2619 | } |
2620 | } | |
2621 | ||
a7d1f22b CH |
2622 | /** |
2623 | * migrate_vma_setup() - prepare to migrate a range of memory | |
2624 | * @args: contains the vma, start, and and pfns arrays for the migration | |
2625 | * | |
2626 | * Returns: negative errno on failures, 0 when 0 or more pages were migrated | |
2627 | * without an error. | |
2628 | * | |
2629 | * Prepare to migrate a range of memory virtual address range by collecting all | |
2630 | * the pages backing each virtual address in the range, saving them inside the | |
2631 | * src array. Then lock those pages and unmap them. Once the pages are locked | |
2632 | * and unmapped, check whether each page is pinned or not. Pages that aren't | |
2633 | * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the | |
2634 | * corresponding src array entry. Then restores any pages that are pinned, by | |
2635 | * remapping and unlocking those pages. | |
2636 | * | |
2637 | * The caller should then allocate destination memory and copy source memory to | |
2638 | * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE | |
2639 | * flag set). Once these are allocated and copied, the caller must update each | |
2640 | * corresponding entry in the dst array with the pfn value of the destination | |
2641 | * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set | |
2642 | * (destination pages must have their struct pages locked, via lock_page()). | |
2643 | * | |
2644 | * Note that the caller does not have to migrate all the pages that are marked | |
2645 | * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from | |
2646 | * device memory to system memory. If the caller cannot migrate a device page | |
2647 | * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe | |
2648 | * consequences for the userspace process, so it must be avoided if at all | |
2649 | * possible. | |
2650 | * | |
2651 | * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we | |
2652 | * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus | |
2653 | * allowing the caller to allocate device memory for those unback virtual | |
2654 | * address. For this the caller simply has to allocate device memory and | |
2655 | * properly set the destination entry like for regular migration. Note that | |
2656 | * this can still fails and thus inside the device driver must check if the | |
2657 | * migration was successful for those entries after calling migrate_vma_pages() | |
2658 | * just like for regular migration. | |
2659 | * | |
2660 | * After that, the callers must call migrate_vma_pages() to go over each entry | |
2661 | * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag | |
2662 | * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, | |
2663 | * then migrate_vma_pages() to migrate struct page information from the source | |
2664 | * struct page to the destination struct page. If it fails to migrate the | |
2665 | * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the | |
2666 | * src array. | |
2667 | * | |
2668 | * At this point all successfully migrated pages have an entry in the src | |
2669 | * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst | |
2670 | * array entry with MIGRATE_PFN_VALID flag set. | |
2671 | * | |
2672 | * Once migrate_vma_pages() returns the caller may inspect which pages were | |
2673 | * successfully migrated, and which were not. Successfully migrated pages will | |
2674 | * have the MIGRATE_PFN_MIGRATE flag set for their src array entry. | |
2675 | * | |
2676 | * It is safe to update device page table after migrate_vma_pages() because | |
2677 | * both destination and source page are still locked, and the mmap_sem is held | |
2678 | * in read mode (hence no one can unmap the range being migrated). | |
2679 | * | |
2680 | * Once the caller is done cleaning up things and updating its page table (if it | |
2681 | * chose to do so, this is not an obligation) it finally calls | |
2682 | * migrate_vma_finalize() to update the CPU page table to point to new pages | |
2683 | * for successfully migrated pages or otherwise restore the CPU page table to | |
2684 | * point to the original source pages. | |
2685 | */ | |
2686 | int migrate_vma_setup(struct migrate_vma *args) | |
2687 | { | |
2688 | long nr_pages = (args->end - args->start) >> PAGE_SHIFT; | |
2689 | ||
2690 | args->start &= PAGE_MASK; | |
2691 | args->end &= PAGE_MASK; | |
2692 | if (!args->vma || is_vm_hugetlb_page(args->vma) || | |
2693 | (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) | |
2694 | return -EINVAL; | |
2695 | if (nr_pages <= 0) | |
2696 | return -EINVAL; | |
2697 | if (args->start < args->vma->vm_start || | |
2698 | args->start >= args->vma->vm_end) | |
2699 | return -EINVAL; | |
2700 | if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) | |
2701 | return -EINVAL; | |
2702 | if (!args->src || !args->dst) | |
2703 | return -EINVAL; | |
2704 | ||
2705 | memset(args->src, 0, sizeof(*args->src) * nr_pages); | |
2706 | args->cpages = 0; | |
2707 | args->npages = 0; | |
2708 | ||
2709 | migrate_vma_collect(args); | |
2710 | ||
2711 | if (args->cpages) | |
2712 | migrate_vma_prepare(args); | |
2713 | if (args->cpages) | |
2714 | migrate_vma_unmap(args); | |
2715 | ||
2716 | /* | |
2717 | * At this point pages are locked and unmapped, and thus they have | |
2718 | * stable content and can safely be copied to destination memory that | |
2719 | * is allocated by the drivers. | |
2720 | */ | |
2721 | return 0; | |
2722 | ||
2723 | } | |
2724 | EXPORT_SYMBOL(migrate_vma_setup); | |
2725 | ||
34290e2c RC |
2726 | /* |
2727 | * This code closely matches the code in: | |
2728 | * __handle_mm_fault() | |
2729 | * handle_pte_fault() | |
2730 | * do_anonymous_page() | |
2731 | * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE | |
2732 | * private page. | |
2733 | */ | |
8315ada7 JG |
2734 | static void migrate_vma_insert_page(struct migrate_vma *migrate, |
2735 | unsigned long addr, | |
2736 | struct page *page, | |
2737 | unsigned long *src, | |
2738 | unsigned long *dst) | |
2739 | { | |
2740 | struct vm_area_struct *vma = migrate->vma; | |
2741 | struct mm_struct *mm = vma->vm_mm; | |
2742 | struct mem_cgroup *memcg; | |
2743 | bool flush = false; | |
2744 | spinlock_t *ptl; | |
2745 | pte_t entry; | |
2746 | pgd_t *pgdp; | |
2747 | p4d_t *p4dp; | |
2748 | pud_t *pudp; | |
2749 | pmd_t *pmdp; | |
2750 | pte_t *ptep; | |
2751 | ||
2752 | /* Only allow populating anonymous memory */ | |
2753 | if (!vma_is_anonymous(vma)) | |
2754 | goto abort; | |
2755 | ||
2756 | pgdp = pgd_offset(mm, addr); | |
2757 | p4dp = p4d_alloc(mm, pgdp, addr); | |
2758 | if (!p4dp) | |
2759 | goto abort; | |
2760 | pudp = pud_alloc(mm, p4dp, addr); | |
2761 | if (!pudp) | |
2762 | goto abort; | |
2763 | pmdp = pmd_alloc(mm, pudp, addr); | |
2764 | if (!pmdp) | |
2765 | goto abort; | |
2766 | ||
2767 | if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp)) | |
2768 | goto abort; | |
2769 | ||
2770 | /* | |
2771 | * Use pte_alloc() instead of pte_alloc_map(). We can't run | |
2772 | * pte_offset_map() on pmds where a huge pmd might be created | |
2773 | * from a different thread. | |
2774 | * | |
2775 | * pte_alloc_map() is safe to use under down_write(mmap_sem) or when | |
2776 | * parallel threads are excluded by other means. | |
2777 | * | |
2778 | * Here we only have down_read(mmap_sem). | |
2779 | */ | |
4cf58924 | 2780 | if (pte_alloc(mm, pmdp)) |
8315ada7 JG |
2781 | goto abort; |
2782 | ||
2783 | /* See the comment in pte_alloc_one_map() */ | |
2784 | if (unlikely(pmd_trans_unstable(pmdp))) | |
2785 | goto abort; | |
2786 | ||
2787 | if (unlikely(anon_vma_prepare(vma))) | |
2788 | goto abort; | |
2789 | if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false)) | |
2790 | goto abort; | |
2791 | ||
2792 | /* | |
2793 | * The memory barrier inside __SetPageUptodate makes sure that | |
2794 | * preceding stores to the page contents become visible before | |
2795 | * the set_pte_at() write. | |
2796 | */ | |
2797 | __SetPageUptodate(page); | |
2798 | ||
df6ad698 JG |
2799 | if (is_zone_device_page(page)) { |
2800 | if (is_device_private_page(page)) { | |
2801 | swp_entry_t swp_entry; | |
2802 | ||
2803 | swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE); | |
2804 | entry = swp_entry_to_pte(swp_entry); | |
df6ad698 | 2805 | } |
8315ada7 JG |
2806 | } else { |
2807 | entry = mk_pte(page, vma->vm_page_prot); | |
2808 | if (vma->vm_flags & VM_WRITE) | |
2809 | entry = pte_mkwrite(pte_mkdirty(entry)); | |
2810 | } | |
2811 | ||
2812 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
2813 | ||
34290e2c RC |
2814 | if (check_stable_address_space(mm)) |
2815 | goto unlock_abort; | |
2816 | ||
8315ada7 JG |
2817 | if (pte_present(*ptep)) { |
2818 | unsigned long pfn = pte_pfn(*ptep); | |
2819 | ||
c23a0c99 RC |
2820 | if (!is_zero_pfn(pfn)) |
2821 | goto unlock_abort; | |
8315ada7 | 2822 | flush = true; |
c23a0c99 RC |
2823 | } else if (!pte_none(*ptep)) |
2824 | goto unlock_abort; | |
8315ada7 JG |
2825 | |
2826 | /* | |
c23a0c99 | 2827 | * Check for userfaultfd but do not deliver the fault. Instead, |
8315ada7 JG |
2828 | * just back off. |
2829 | */ | |
c23a0c99 RC |
2830 | if (userfaultfd_missing(vma)) |
2831 | goto unlock_abort; | |
8315ada7 JG |
2832 | |
2833 | inc_mm_counter(mm, MM_ANONPAGES); | |
2834 | page_add_new_anon_rmap(page, vma, addr, false); | |
2835 | mem_cgroup_commit_charge(page, memcg, false, false); | |
2836 | if (!is_zone_device_page(page)) | |
2837 | lru_cache_add_active_or_unevictable(page, vma); | |
2838 | get_page(page); | |
2839 | ||
2840 | if (flush) { | |
2841 | flush_cache_page(vma, addr, pte_pfn(*ptep)); | |
2842 | ptep_clear_flush_notify(vma, addr, ptep); | |
2843 | set_pte_at_notify(mm, addr, ptep, entry); | |
2844 | update_mmu_cache(vma, addr, ptep); | |
2845 | } else { | |
2846 | /* No need to invalidate - it was non-present before */ | |
2847 | set_pte_at(mm, addr, ptep, entry); | |
2848 | update_mmu_cache(vma, addr, ptep); | |
2849 | } | |
2850 | ||
2851 | pte_unmap_unlock(ptep, ptl); | |
2852 | *src = MIGRATE_PFN_MIGRATE; | |
2853 | return; | |
2854 | ||
c23a0c99 RC |
2855 | unlock_abort: |
2856 | pte_unmap_unlock(ptep, ptl); | |
2857 | mem_cgroup_cancel_charge(page, memcg, false); | |
8315ada7 JG |
2858 | abort: |
2859 | *src &= ~MIGRATE_PFN_MIGRATE; | |
2860 | } | |
2861 | ||
a7d1f22b | 2862 | /** |
8763cb45 JG |
2863 | * migrate_vma_pages() - migrate meta-data from src page to dst page |
2864 | * @migrate: migrate struct containing all migration information | |
2865 | * | |
2866 | * This migrates struct page meta-data from source struct page to destination | |
2867 | * struct page. This effectively finishes the migration from source page to the | |
2868 | * destination page. | |
2869 | */ | |
a7d1f22b | 2870 | void migrate_vma_pages(struct migrate_vma *migrate) |
8763cb45 JG |
2871 | { |
2872 | const unsigned long npages = migrate->npages; | |
2873 | const unsigned long start = migrate->start; | |
ac46d4f3 JG |
2874 | struct mmu_notifier_range range; |
2875 | unsigned long addr, i; | |
8315ada7 | 2876 | bool notified = false; |
8763cb45 JG |
2877 | |
2878 | for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { | |
2879 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
2880 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2881 | struct address_space *mapping; | |
2882 | int r; | |
2883 | ||
8315ada7 JG |
2884 | if (!newpage) { |
2885 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
8763cb45 | 2886 | continue; |
8315ada7 JG |
2887 | } |
2888 | ||
2889 | if (!page) { | |
c23a0c99 | 2890 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
8315ada7 | 2891 | continue; |
8315ada7 | 2892 | if (!notified) { |
8315ada7 | 2893 | notified = true; |
ac46d4f3 JG |
2894 | |
2895 | mmu_notifier_range_init(&range, | |
7269f999 | 2896 | MMU_NOTIFY_CLEAR, 0, |
6f4f13e8 | 2897 | NULL, |
ac46d4f3 JG |
2898 | migrate->vma->vm_mm, |
2899 | addr, migrate->end); | |
2900 | mmu_notifier_invalidate_range_start(&range); | |
8315ada7 JG |
2901 | } |
2902 | migrate_vma_insert_page(migrate, addr, newpage, | |
2903 | &migrate->src[i], | |
2904 | &migrate->dst[i]); | |
8763cb45 | 2905 | continue; |
8315ada7 | 2906 | } |
8763cb45 JG |
2907 | |
2908 | mapping = page_mapping(page); | |
2909 | ||
a5430dda JG |
2910 | if (is_zone_device_page(newpage)) { |
2911 | if (is_device_private_page(newpage)) { | |
2912 | /* | |
2913 | * For now only support private anonymous when | |
2914 | * migrating to un-addressable device memory. | |
2915 | */ | |
2916 | if (mapping) { | |
2917 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2918 | continue; | |
2919 | } | |
25b2995a | 2920 | } else { |
a5430dda JG |
2921 | /* |
2922 | * Other types of ZONE_DEVICE page are not | |
2923 | * supported. | |
2924 | */ | |
2925 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2926 | continue; | |
2927 | } | |
2928 | } | |
2929 | ||
8763cb45 JG |
2930 | r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); |
2931 | if (r != MIGRATEPAGE_SUCCESS) | |
2932 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2933 | } | |
8315ada7 | 2934 | |
4645b9fe JG |
2935 | /* |
2936 | * No need to double call mmu_notifier->invalidate_range() callback as | |
2937 | * the above ptep_clear_flush_notify() inside migrate_vma_insert_page() | |
2938 | * did already call it. | |
2939 | */ | |
8315ada7 | 2940 | if (notified) |
ac46d4f3 | 2941 | mmu_notifier_invalidate_range_only_end(&range); |
8763cb45 | 2942 | } |
a7d1f22b | 2943 | EXPORT_SYMBOL(migrate_vma_pages); |
8763cb45 | 2944 | |
a7d1f22b | 2945 | /** |
8763cb45 JG |
2946 | * migrate_vma_finalize() - restore CPU page table entry |
2947 | * @migrate: migrate struct containing all migration information | |
2948 | * | |
2949 | * This replaces the special migration pte entry with either a mapping to the | |
2950 | * new page if migration was successful for that page, or to the original page | |
2951 | * otherwise. | |
2952 | * | |
2953 | * This also unlocks the pages and puts them back on the lru, or drops the extra | |
2954 | * refcount, for device pages. | |
2955 | */ | |
a7d1f22b | 2956 | void migrate_vma_finalize(struct migrate_vma *migrate) |
8763cb45 JG |
2957 | { |
2958 | const unsigned long npages = migrate->npages; | |
2959 | unsigned long i; | |
2960 | ||
2961 | for (i = 0; i < npages; i++) { | |
2962 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
2963 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2964 | ||
8315ada7 JG |
2965 | if (!page) { |
2966 | if (newpage) { | |
2967 | unlock_page(newpage); | |
2968 | put_page(newpage); | |
2969 | } | |
8763cb45 | 2970 | continue; |
8315ada7 JG |
2971 | } |
2972 | ||
8763cb45 JG |
2973 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { |
2974 | if (newpage) { | |
2975 | unlock_page(newpage); | |
2976 | put_page(newpage); | |
2977 | } | |
2978 | newpage = page; | |
2979 | } | |
2980 | ||
2981 | remove_migration_ptes(page, newpage, false); | |
2982 | unlock_page(page); | |
2983 | migrate->cpages--; | |
2984 | ||
a5430dda JG |
2985 | if (is_zone_device_page(page)) |
2986 | put_page(page); | |
2987 | else | |
2988 | putback_lru_page(page); | |
8763cb45 JG |
2989 | |
2990 | if (newpage != page) { | |
2991 | unlock_page(newpage); | |
a5430dda JG |
2992 | if (is_zone_device_page(newpage)) |
2993 | put_page(newpage); | |
2994 | else | |
2995 | putback_lru_page(newpage); | |
8763cb45 JG |
2996 | } |
2997 | } | |
2998 | } | |
a7d1f22b | 2999 | EXPORT_SYMBOL(migrate_vma_finalize); |
9b2ed9cb | 3000 | #endif /* CONFIG_DEVICE_PRIVATE */ |