drivers/base/memory: clarify adding and removing of memory blocks
[linux-block.git] / mm / migrate.c
CommitLineData
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>
884a6e5d 52#include <linux/memory.h>
ac16ec83 53#include <linux/random.h>
c574bbe9 54#include <linux/sched/sysctl.h>
b20a3503 55
0d1836c3
MN
56#include <asm/tlbflush.h>
57
7b2a2d4a
MG
58#define CREATE_TRACE_POINTS
59#include <trace/events/migrate.h>
60
b20a3503
CL
61#include "internal.h"
62
9e5bcd61 63int isolate_movable_page(struct page *page, isolate_mode_t mode)
bda807d4
MK
64{
65 struct address_space *mapping;
66
67 /*
68 * Avoid burning cycles with pages that are yet under __free_pages(),
69 * or just got freed under us.
70 *
71 * In case we 'win' a race for a movable page being freed under us and
72 * raise its refcount preventing __free_pages() from doing its job
73 * the put_page() at the end of this block will take care of
74 * release this page, thus avoiding a nasty leakage.
75 */
76 if (unlikely(!get_page_unless_zero(page)))
77 goto out;
78
79 /*
80 * Check PageMovable before holding a PG_lock because page's owner
81 * assumes anybody doesn't touch PG_lock of newly allocated page
8bb4e7a2 82 * so unconditionally grabbing the lock ruins page's owner side.
bda807d4
MK
83 */
84 if (unlikely(!__PageMovable(page)))
85 goto out_putpage;
86 /*
87 * As movable pages are not isolated from LRU lists, concurrent
88 * compaction threads can race against page migration functions
89 * as well as race against the releasing a page.
90 *
91 * In order to avoid having an already isolated movable page
92 * being (wrongly) re-isolated while it is under migration,
93 * or to avoid attempting to isolate pages being released,
94 * lets be sure we have the page lock
95 * before proceeding with the movable page isolation steps.
96 */
97 if (unlikely(!trylock_page(page)))
98 goto out_putpage;
99
100 if (!PageMovable(page) || PageIsolated(page))
101 goto out_no_isolated;
102
103 mapping = page_mapping(page);
104 VM_BUG_ON_PAGE(!mapping, page);
105
106 if (!mapping->a_ops->isolate_page(page, mode))
107 goto out_no_isolated;
108
109 /* Driver shouldn't use PG_isolated bit of page->flags */
110 WARN_ON_ONCE(PageIsolated(page));
356ea386 111 SetPageIsolated(page);
bda807d4
MK
112 unlock_page(page);
113
9e5bcd61 114 return 0;
bda807d4
MK
115
116out_no_isolated:
117 unlock_page(page);
118out_putpage:
119 put_page(page);
120out:
9e5bcd61 121 return -EBUSY;
bda807d4
MK
122}
123
606a6f71 124static void putback_movable_page(struct page *page)
bda807d4
MK
125{
126 struct address_space *mapping;
127
bda807d4
MK
128 mapping = page_mapping(page);
129 mapping->a_ops->putback_page(page);
356ea386 130 ClearPageIsolated(page);
bda807d4
MK
131}
132
5733c7d1
RA
133/*
134 * Put previously isolated pages back onto the appropriate lists
135 * from where they were once taken off for compaction/migration.
136 *
59c82b70
JK
137 * This function shall be used whenever the isolated pageset has been
138 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
139 * and isolate_huge_page().
5733c7d1
RA
140 */
141void putback_movable_pages(struct list_head *l)
142{
143 struct page *page;
144 struct page *page2;
145
b20a3503 146 list_for_each_entry_safe(page, page2, l, lru) {
31caf665
NH
147 if (unlikely(PageHuge(page))) {
148 putback_active_hugepage(page);
149 continue;
150 }
e24f0b8f 151 list_del(&page->lru);
bda807d4
MK
152 /*
153 * We isolated non-lru movable page so here we can use
154 * __PageMovable because LRU page's mapping cannot have
155 * PAGE_MAPPING_MOVABLE.
156 */
b1123ea6 157 if (unlikely(__PageMovable(page))) {
bda807d4
MK
158 VM_BUG_ON_PAGE(!PageIsolated(page), page);
159 lock_page(page);
160 if (PageMovable(page))
161 putback_movable_page(page);
162 else
356ea386 163 ClearPageIsolated(page);
bda807d4
MK
164 unlock_page(page);
165 put_page(page);
166 } else {
e8db67eb 167 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
6c357848 168 page_is_file_lru(page), -thp_nr_pages(page));
fc280fe8 169 putback_lru_page(page);
bda807d4 170 }
b20a3503 171 }
b20a3503
CL
172}
173
0697212a
CL
174/*
175 * Restore a potential migration pte to a working pte entry
176 */
e4b82222 177static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
e9995ef9 178 unsigned long addr, void *old)
0697212a 179{
3fe87967
KS
180 struct page_vma_mapped_walk pvmw = {
181 .page = old,
182 .vma = vma,
183 .address = addr,
184 .flags = PVMW_SYNC | PVMW_MIGRATION,
185 };
186 struct page *new;
187 pte_t pte;
0697212a 188 swp_entry_t entry;
0697212a 189
3fe87967
KS
190 VM_BUG_ON_PAGE(PageTail(page), page);
191 while (page_vma_mapped_walk(&pvmw)) {
4b0ece6f
NH
192 if (PageKsm(page))
193 new = page;
194 else
195 new = page - pvmw.page->index +
196 linear_page_index(vma, pvmw.address);
0697212a 197
616b8371
ZY
198#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
199 /* PMD-mapped THP migration entry */
200 if (!pvmw.pte) {
201 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
202 remove_migration_pmd(&pvmw, new);
203 continue;
204 }
205#endif
206
3fe87967
KS
207 get_page(new);
208 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
209 if (pte_swp_soft_dirty(*pvmw.pte))
210 pte = pte_mksoft_dirty(pte);
0697212a 211
3fe87967
KS
212 /*
213 * Recheck VMA as permissions can change since migration started
214 */
215 entry = pte_to_swp_entry(*pvmw.pte);
4dd845b5 216 if (is_writable_migration_entry(entry))
3fe87967 217 pte = maybe_mkwrite(pte, vma);
f45ec5ff
PX
218 else if (pte_swp_uffd_wp(*pvmw.pte))
219 pte = pte_mkuffd_wp(pte);
d3cb8bf6 220
6128763f 221 if (unlikely(is_device_private_page(new))) {
4dd845b5
AP
222 if (pte_write(pte))
223 entry = make_writable_device_private_entry(
224 page_to_pfn(new));
225 else
226 entry = make_readable_device_private_entry(
227 page_to_pfn(new));
6128763f 228 pte = swp_entry_to_pte(entry);
3d321bf8
RC
229 if (pte_swp_soft_dirty(*pvmw.pte))
230 pte = pte_swp_mksoft_dirty(pte);
6128763f
RC
231 if (pte_swp_uffd_wp(*pvmw.pte))
232 pte = pte_swp_mkuffd_wp(pte);
d2b2c6dd 233 }
a5430dda 234
3ef8fd7f 235#ifdef CONFIG_HUGETLB_PAGE
3fe87967 236 if (PageHuge(new)) {
79c1c594
CL
237 unsigned int shift = huge_page_shift(hstate_vma(vma));
238
3fe87967 239 pte = pte_mkhuge(pte);
79c1c594 240 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
3fe87967
KS
241 if (PageAnon(new))
242 hugepage_add_anon_rmap(new, vma, pvmw.address);
243 else
244 page_dup_rmap(new, true);
1eba86c0 245 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
383321ab
AK
246 } else
247#endif
248 {
383321ab
AK
249 if (PageAnon(new))
250 page_add_anon_rmap(new, vma, pvmw.address, false);
251 else
252 page_add_file_rmap(new, false);
1eba86c0 253 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
383321ab 254 }
3fe87967
KS
255 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
256 mlock_vma_page(new);
257
e125fe40
KS
258 if (PageTransHuge(page) && PageMlocked(page))
259 clear_page_mlock(page);
260
3fe87967
KS
261 /* No need to invalidate - it was non-present before */
262 update_mmu_cache(vma, pvmw.address, pvmw.pte);
263 }
51afb12b 264
e4b82222 265 return true;
0697212a
CL
266}
267
04e62a29
CL
268/*
269 * Get rid of all migration entries and replace them by
270 * references to the indicated page.
271 */
e388466d 272void remove_migration_ptes(struct page *old, struct page *new, bool locked)
04e62a29 273{
051ac83a
JK
274 struct rmap_walk_control rwc = {
275 .rmap_one = remove_migration_pte,
276 .arg = old,
277 };
278
e388466d
KS
279 if (locked)
280 rmap_walk_locked(new, &rwc);
281 else
282 rmap_walk(new, &rwc);
04e62a29
CL
283}
284
0697212a
CL
285/*
286 * Something used the pte of a page under migration. We need to
287 * get to the page and wait until migration is finished.
288 * When we return from this function the fault will be retried.
0697212a 289 */
e66f17ff 290void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
30dad309 291 spinlock_t *ptl)
0697212a 292{
30dad309 293 pte_t pte;
0697212a 294 swp_entry_t entry;
0697212a 295
30dad309 296 spin_lock(ptl);
0697212a
CL
297 pte = *ptep;
298 if (!is_swap_pte(pte))
299 goto out;
300
301 entry = pte_to_swp_entry(pte);
302 if (!is_migration_entry(entry))
303 goto out;
304
ffa65753 305 migration_entry_wait_on_locked(entry, ptep, ptl);
0697212a
CL
306 return;
307out:
308 pte_unmap_unlock(ptep, ptl);
309}
310
30dad309
NH
311void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
312 unsigned long address)
313{
314 spinlock_t *ptl = pte_lockptr(mm, pmd);
315 pte_t *ptep = pte_offset_map(pmd, address);
316 __migration_entry_wait(mm, ptep, ptl);
317}
318
cb900f41
KS
319void migration_entry_wait_huge(struct vm_area_struct *vma,
320 struct mm_struct *mm, pte_t *pte)
30dad309 321{
cb900f41 322 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
30dad309
NH
323 __migration_entry_wait(mm, pte, ptl);
324}
325
616b8371
ZY
326#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
327void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
328{
329 spinlock_t *ptl;
616b8371
ZY
330
331 ptl = pmd_lock(mm, pmd);
332 if (!is_pmd_migration_entry(*pmd))
333 goto unlock;
ffa65753 334 migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
616b8371
ZY
335 return;
336unlock:
337 spin_unlock(ptl);
338}
339#endif
340
f900482d 341static int expected_page_refs(struct address_space *mapping, struct page *page)
0b3901b3
JK
342{
343 int expected_count = 1;
344
345 /*
f1f4f3ab 346 * Device private pages have an extra refcount as they are
0b3901b3
JK
347 * ZONE_DEVICE pages.
348 */
349 expected_count += is_device_private_page(page);
f900482d 350 if (mapping)
3417013e 351 expected_count += compound_nr(page) + page_has_private(page);
0b3901b3
JK
352
353 return expected_count;
354}
355
b20a3503 356/*
c3fcf8a5 357 * Replace the page in the mapping.
5b5c7120
CL
358 *
359 * The number of remaining references must be:
360 * 1 for anonymous pages without a mapping
361 * 2 for pages with a mapping
266cf658 362 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
b20a3503 363 */
3417013e
MWO
364int folio_migrate_mapping(struct address_space *mapping,
365 struct folio *newfolio, struct folio *folio, int extra_count)
b20a3503 366{
3417013e 367 XA_STATE(xas, &mapping->i_pages, folio_index(folio));
42cb14b1
HD
368 struct zone *oldzone, *newzone;
369 int dirty;
3417013e
MWO
370 int expected_count = expected_page_refs(mapping, &folio->page) + extra_count;
371 long nr = folio_nr_pages(folio);
8763cb45 372
6c5240ae 373 if (!mapping) {
0e8c7d0f 374 /* Anonymous page without mapping */
3417013e 375 if (folio_ref_count(folio) != expected_count)
6c5240ae 376 return -EAGAIN;
cf4b769a
HD
377
378 /* No turning back from here */
3417013e
MWO
379 newfolio->index = folio->index;
380 newfolio->mapping = folio->mapping;
381 if (folio_test_swapbacked(folio))
382 __folio_set_swapbacked(newfolio);
cf4b769a 383
78bd5209 384 return MIGRATEPAGE_SUCCESS;
6c5240ae
CL
385 }
386
3417013e
MWO
387 oldzone = folio_zone(folio);
388 newzone = folio_zone(newfolio);
42cb14b1 389
89eb946a 390 xas_lock_irq(&xas);
3417013e 391 if (!folio_ref_freeze(folio, expected_count)) {
89eb946a 392 xas_unlock_irq(&xas);
e286781d
NP
393 return -EAGAIN;
394 }
395
b20a3503 396 /*
3417013e 397 * Now we know that no one else is looking at the folio:
cf4b769a 398 * no turning back from here.
b20a3503 399 */
3417013e
MWO
400 newfolio->index = folio->index;
401 newfolio->mapping = folio->mapping;
402 folio_ref_add(newfolio, nr); /* add cache reference */
403 if (folio_test_swapbacked(folio)) {
404 __folio_set_swapbacked(newfolio);
405 if (folio_test_swapcache(folio)) {
406 folio_set_swapcache(newfolio);
407 newfolio->private = folio_get_private(folio);
6326fec1
NP
408 }
409 } else {
3417013e 410 VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
b20a3503
CL
411 }
412
42cb14b1 413 /* Move dirty while page refs frozen and newpage not yet exposed */
3417013e 414 dirty = folio_test_dirty(folio);
42cb14b1 415 if (dirty) {
3417013e
MWO
416 folio_clear_dirty(folio);
417 folio_set_dirty(newfolio);
42cb14b1
HD
418 }
419
3417013e 420 xas_store(&xas, newfolio);
7cf9c2c7
NP
421
422 /*
937a94c9
JG
423 * Drop cache reference from old page by unfreezing
424 * to one less reference.
7cf9c2c7
NP
425 * We know this isn't the last reference.
426 */
3417013e 427 folio_ref_unfreeze(folio, expected_count - nr);
7cf9c2c7 428
89eb946a 429 xas_unlock(&xas);
42cb14b1
HD
430 /* Leave irq disabled to prevent preemption while updating stats */
431
0e8c7d0f
CL
432 /*
433 * If moved to a different zone then also account
434 * the page for that zone. Other VM counters will be
435 * taken care of when we establish references to the
436 * new page and drop references to the old page.
437 *
438 * Note that anonymous pages are accounted for
4b9d0fab 439 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
0e8c7d0f
CL
440 * are mapped to swap space.
441 */
42cb14b1 442 if (newzone != oldzone) {
0d1c2072
JW
443 struct lruvec *old_lruvec, *new_lruvec;
444 struct mem_cgroup *memcg;
445
3417013e 446 memcg = folio_memcg(folio);
0d1c2072
JW
447 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
448 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
449
5c447d27
SB
450 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
451 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
3417013e 452 if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) {
5c447d27
SB
453 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
454 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
42cb14b1 455 }
b6038942 456#ifdef CONFIG_SWAP
3417013e 457 if (folio_test_swapcache(folio)) {
b6038942
SB
458 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
459 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
460 }
461#endif
f56753ac 462 if (dirty && mapping_can_writeback(mapping)) {
5c447d27
SB
463 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
464 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
465 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
466 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
42cb14b1 467 }
4b02108a 468 }
42cb14b1 469 local_irq_enable();
b20a3503 470
78bd5209 471 return MIGRATEPAGE_SUCCESS;
b20a3503 472}
3417013e 473EXPORT_SYMBOL(folio_migrate_mapping);
b20a3503 474
290408d4
NH
475/*
476 * The expected number of remaining references is the same as that
3417013e 477 * of folio_migrate_mapping().
290408d4
NH
478 */
479int migrate_huge_page_move_mapping(struct address_space *mapping,
480 struct page *newpage, struct page *page)
481{
89eb946a 482 XA_STATE(xas, &mapping->i_pages, page_index(page));
290408d4 483 int expected_count;
290408d4 484
89eb946a 485 xas_lock_irq(&xas);
290408d4 486 expected_count = 2 + page_has_private(page);
89eb946a
MW
487 if (page_count(page) != expected_count || xas_load(&xas) != page) {
488 xas_unlock_irq(&xas);
290408d4
NH
489 return -EAGAIN;
490 }
491
fe896d18 492 if (!page_ref_freeze(page, expected_count)) {
89eb946a 493 xas_unlock_irq(&xas);
290408d4
NH
494 return -EAGAIN;
495 }
496
cf4b769a
HD
497 newpage->index = page->index;
498 newpage->mapping = page->mapping;
6a93ca8f 499
290408d4
NH
500 get_page(newpage);
501
89eb946a 502 xas_store(&xas, newpage);
290408d4 503
fe896d18 504 page_ref_unfreeze(page, expected_count - 1);
290408d4 505
89eb946a 506 xas_unlock_irq(&xas);
6a93ca8f 507
78bd5209 508 return MIGRATEPAGE_SUCCESS;
290408d4
NH
509}
510
b20a3503 511/*
19138349 512 * Copy the flags and some other ancillary information
b20a3503 513 */
19138349 514void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
b20a3503 515{
7851a45c
RR
516 int cpupid;
517
19138349
MWO
518 if (folio_test_error(folio))
519 folio_set_error(newfolio);
520 if (folio_test_referenced(folio))
521 folio_set_referenced(newfolio);
522 if (folio_test_uptodate(folio))
523 folio_mark_uptodate(newfolio);
524 if (folio_test_clear_active(folio)) {
525 VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio);
526 folio_set_active(newfolio);
527 } else if (folio_test_clear_unevictable(folio))
528 folio_set_unevictable(newfolio);
529 if (folio_test_workingset(folio))
530 folio_set_workingset(newfolio);
531 if (folio_test_checked(folio))
532 folio_set_checked(newfolio);
533 if (folio_test_mappedtodisk(folio))
534 folio_set_mappedtodisk(newfolio);
b20a3503 535
3417013e 536 /* Move dirty on pages not done by folio_migrate_mapping() */
19138349
MWO
537 if (folio_test_dirty(folio))
538 folio_set_dirty(newfolio);
b20a3503 539
19138349
MWO
540 if (folio_test_young(folio))
541 folio_set_young(newfolio);
542 if (folio_test_idle(folio))
543 folio_set_idle(newfolio);
33c3fc71 544
7851a45c
RR
545 /*
546 * Copy NUMA information to the new page, to prevent over-eager
547 * future migrations of this same page.
548 */
19138349
MWO
549 cpupid = page_cpupid_xchg_last(&folio->page, -1);
550 page_cpupid_xchg_last(&newfolio->page, cpupid);
7851a45c 551
19138349 552 folio_migrate_ksm(newfolio, folio);
c8d6553b
HD
553 /*
554 * Please do not reorder this without considering how mm/ksm.c's
555 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
556 */
19138349
MWO
557 if (folio_test_swapcache(folio))
558 folio_clear_swapcache(folio);
559 folio_clear_private(folio);
ad2fa371
MS
560
561 /* page->private contains hugetlb specific flags */
19138349
MWO
562 if (!folio_test_hugetlb(folio))
563 folio->private = NULL;
b20a3503
CL
564
565 /*
566 * If any waiters have accumulated on the new page then
567 * wake them up.
568 */
19138349
MWO
569 if (folio_test_writeback(newfolio))
570 folio_end_writeback(newfolio);
d435edca 571
6aeff241
YS
572 /*
573 * PG_readahead shares the same bit with PG_reclaim. The above
574 * end_page_writeback() may clear PG_readahead mistakenly, so set the
575 * bit after that.
576 */
19138349
MWO
577 if (folio_test_readahead(folio))
578 folio_set_readahead(newfolio);
6aeff241 579
19138349 580 folio_copy_owner(newfolio, folio);
74485cf2 581
19138349 582 if (!folio_test_hugetlb(folio))
d21bba2b 583 mem_cgroup_migrate(folio, newfolio);
b20a3503 584}
19138349 585EXPORT_SYMBOL(folio_migrate_flags);
2916ecc0 586
715cbfd6 587void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
2916ecc0 588{
715cbfd6
MWO
589 folio_copy(newfolio, folio);
590 folio_migrate_flags(newfolio, folio);
2916ecc0 591}
715cbfd6 592EXPORT_SYMBOL(folio_migrate_copy);
b20a3503 593
1d8b85cc
CL
594/************************************************************
595 * Migration functions
596 ***********************************************************/
597
b20a3503 598/*
bda807d4 599 * Common logic to directly migrate a single LRU page suitable for
266cf658 600 * pages that do not use PagePrivate/PagePrivate2.
b20a3503
CL
601 *
602 * Pages are locked upon entry and exit.
603 */
2d1db3b1 604int migrate_page(struct address_space *mapping,
a6bc32b8
MG
605 struct page *newpage, struct page *page,
606 enum migrate_mode mode)
b20a3503 607{
3417013e
MWO
608 struct folio *newfolio = page_folio(newpage);
609 struct folio *folio = page_folio(page);
b20a3503
CL
610 int rc;
611
3417013e 612 BUG_ON(folio_test_writeback(folio)); /* Writeback must be complete */
b20a3503 613
3417013e 614 rc = folio_migrate_mapping(mapping, newfolio, folio, 0);
b20a3503 615
78bd5209 616 if (rc != MIGRATEPAGE_SUCCESS)
b20a3503
CL
617 return rc;
618
2916ecc0 619 if (mode != MIGRATE_SYNC_NO_COPY)
715cbfd6 620 folio_migrate_copy(newfolio, folio);
2916ecc0 621 else
19138349 622 folio_migrate_flags(newfolio, folio);
78bd5209 623 return MIGRATEPAGE_SUCCESS;
b20a3503
CL
624}
625EXPORT_SYMBOL(migrate_page);
626
9361401e 627#ifdef CONFIG_BLOCK
84ade7c1
JK
628/* Returns true if all buffers are successfully locked */
629static bool buffer_migrate_lock_buffers(struct buffer_head *head,
630 enum migrate_mode mode)
631{
632 struct buffer_head *bh = head;
633
634 /* Simple case, sync compaction */
635 if (mode != MIGRATE_ASYNC) {
636 do {
84ade7c1
JK
637 lock_buffer(bh);
638 bh = bh->b_this_page;
639
640 } while (bh != head);
641
642 return true;
643 }
644
645 /* async case, we cannot block on lock_buffer so use trylock_buffer */
646 do {
84ade7c1
JK
647 if (!trylock_buffer(bh)) {
648 /*
649 * We failed to lock the buffer and cannot stall in
650 * async migration. Release the taken locks
651 */
652 struct buffer_head *failed_bh = bh;
84ade7c1
JK
653 bh = head;
654 while (bh != failed_bh) {
655 unlock_buffer(bh);
84ade7c1
JK
656 bh = bh->b_this_page;
657 }
658 return false;
659 }
660
661 bh = bh->b_this_page;
662 } while (bh != head);
663 return true;
664}
665
89cb0888
JK
666static int __buffer_migrate_page(struct address_space *mapping,
667 struct page *newpage, struct page *page, enum migrate_mode mode,
668 bool check_refs)
1d8b85cc 669{
1d8b85cc
CL
670 struct buffer_head *bh, *head;
671 int rc;
cc4f11e6 672 int expected_count;
1d8b85cc 673
1d8b85cc 674 if (!page_has_buffers(page))
a6bc32b8 675 return migrate_page(mapping, newpage, page, mode);
1d8b85cc 676
cc4f11e6 677 /* Check whether page does not have extra refs before we do more work */
f900482d 678 expected_count = expected_page_refs(mapping, page);
cc4f11e6
JK
679 if (page_count(page) != expected_count)
680 return -EAGAIN;
1d8b85cc 681
cc4f11e6
JK
682 head = page_buffers(page);
683 if (!buffer_migrate_lock_buffers(head, mode))
684 return -EAGAIN;
1d8b85cc 685
89cb0888
JK
686 if (check_refs) {
687 bool busy;
688 bool invalidated = false;
689
690recheck_buffers:
691 busy = false;
692 spin_lock(&mapping->private_lock);
693 bh = head;
694 do {
695 if (atomic_read(&bh->b_count)) {
696 busy = true;
697 break;
698 }
699 bh = bh->b_this_page;
700 } while (bh != head);
89cb0888
JK
701 if (busy) {
702 if (invalidated) {
703 rc = -EAGAIN;
704 goto unlock_buffers;
705 }
ebdf4de5 706 spin_unlock(&mapping->private_lock);
89cb0888
JK
707 invalidate_bh_lrus();
708 invalidated = true;
709 goto recheck_buffers;
710 }
711 }
712
37109694 713 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
78bd5209 714 if (rc != MIGRATEPAGE_SUCCESS)
cc4f11e6 715 goto unlock_buffers;
1d8b85cc 716
cd0f3715 717 attach_page_private(newpage, detach_page_private(page));
1d8b85cc
CL
718
719 bh = head;
720 do {
721 set_bh_page(bh, newpage, bh_offset(bh));
722 bh = bh->b_this_page;
723
724 } while (bh != head);
725
2916ecc0
JG
726 if (mode != MIGRATE_SYNC_NO_COPY)
727 migrate_page_copy(newpage, page);
728 else
729 migrate_page_states(newpage, page);
1d8b85cc 730
cc4f11e6
JK
731 rc = MIGRATEPAGE_SUCCESS;
732unlock_buffers:
ebdf4de5
JK
733 if (check_refs)
734 spin_unlock(&mapping->private_lock);
1d8b85cc
CL
735 bh = head;
736 do {
737 unlock_buffer(bh);
1d8b85cc
CL
738 bh = bh->b_this_page;
739
740 } while (bh != head);
741
cc4f11e6 742 return rc;
1d8b85cc 743}
89cb0888
JK
744
745/*
746 * Migration function for pages with buffers. This function can only be used
747 * if the underlying filesystem guarantees that no other references to "page"
748 * exist. For example attached buffer heads are accessed only under page lock.
749 */
750int buffer_migrate_page(struct address_space *mapping,
751 struct page *newpage, struct page *page, enum migrate_mode mode)
752{
753 return __buffer_migrate_page(mapping, newpage, page, mode, false);
754}
1d8b85cc 755EXPORT_SYMBOL(buffer_migrate_page);
89cb0888
JK
756
757/*
758 * Same as above except that this variant is more careful and checks that there
759 * are also no buffer head references. This function is the right one for
760 * mappings where buffer heads are directly looked up and referenced (such as
761 * block device mappings).
762 */
763int buffer_migrate_page_norefs(struct address_space *mapping,
764 struct page *newpage, struct page *page, enum migrate_mode mode)
765{
766 return __buffer_migrate_page(mapping, newpage, page, mode, true);
767}
9361401e 768#endif
1d8b85cc 769
04e62a29
CL
770/*
771 * Writeback a page to clean the dirty state
772 */
773static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 774{
04e62a29
CL
775 struct writeback_control wbc = {
776 .sync_mode = WB_SYNC_NONE,
777 .nr_to_write = 1,
778 .range_start = 0,
779 .range_end = LLONG_MAX,
04e62a29
CL
780 .for_reclaim = 1
781 };
782 int rc;
783
784 if (!mapping->a_ops->writepage)
785 /* No write method for the address space */
786 return -EINVAL;
787
788 if (!clear_page_dirty_for_io(page))
789 /* Someone else already triggered a write */
790 return -EAGAIN;
791
8351a6e4 792 /*
04e62a29
CL
793 * A dirty page may imply that the underlying filesystem has
794 * the page on some queue. So the page must be clean for
795 * migration. Writeout may mean we loose the lock and the
796 * page state is no longer what we checked for earlier.
797 * At this point we know that the migration attempt cannot
798 * be successful.
8351a6e4 799 */
e388466d 800 remove_migration_ptes(page, page, false);
8351a6e4 801
04e62a29 802 rc = mapping->a_ops->writepage(page, &wbc);
8351a6e4 803
04e62a29
CL
804 if (rc != AOP_WRITEPAGE_ACTIVATE)
805 /* unlocked. Relock */
806 lock_page(page);
807
bda8550d 808 return (rc < 0) ? -EIO : -EAGAIN;
04e62a29
CL
809}
810
811/*
812 * Default handling if a filesystem does not provide a migration function.
813 */
814static int fallback_migrate_page(struct address_space *mapping,
a6bc32b8 815 struct page *newpage, struct page *page, enum migrate_mode mode)
04e62a29 816{
b969c4ab 817 if (PageDirty(page)) {
a6bc32b8 818 /* Only writeback pages in full synchronous migration */
2916ecc0
JG
819 switch (mode) {
820 case MIGRATE_SYNC:
821 case MIGRATE_SYNC_NO_COPY:
822 break;
823 default:
b969c4ab 824 return -EBUSY;
2916ecc0 825 }
04e62a29 826 return writeout(mapping, page);
b969c4ab 827 }
8351a6e4
CL
828
829 /*
830 * Buffers may be managed in a filesystem specific way.
831 * We must have no buffers or drop them.
832 */
266cf658 833 if (page_has_private(page) &&
8351a6e4 834 !try_to_release_page(page, GFP_KERNEL))
806031bb 835 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
8351a6e4 836
a6bc32b8 837 return migrate_page(mapping, newpage, page, mode);
8351a6e4
CL
838}
839
e24f0b8f
CL
840/*
841 * Move a page to a newly allocated page
842 * The page is locked and all ptes have been successfully removed.
843 *
844 * The new page will have replaced the old page if this function
845 * is successful.
894bc310
LS
846 *
847 * Return value:
848 * < 0 - error code
78bd5209 849 * MIGRATEPAGE_SUCCESS - success
e24f0b8f 850 */
3fe2011f 851static int move_to_new_page(struct page *newpage, struct page *page,
5c3f9a67 852 enum migrate_mode mode)
e24f0b8f
CL
853{
854 struct address_space *mapping;
bda807d4
MK
855 int rc = -EAGAIN;
856 bool is_lru = !__PageMovable(page);
e24f0b8f 857
7db7671f
HD
858 VM_BUG_ON_PAGE(!PageLocked(page), page);
859 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
e24f0b8f 860
e24f0b8f 861 mapping = page_mapping(page);
bda807d4
MK
862
863 if (likely(is_lru)) {
864 if (!mapping)
865 rc = migrate_page(mapping, newpage, page, mode);
866 else if (mapping->a_ops->migratepage)
867 /*
868 * Most pages have a mapping and most filesystems
869 * provide a migratepage callback. Anonymous pages
870 * are part of swap space which also has its own
871 * migratepage callback. This is the most common path
872 * for page migration.
873 */
874 rc = mapping->a_ops->migratepage(mapping, newpage,
875 page, mode);
876 else
877 rc = fallback_migrate_page(mapping, newpage,
878 page, mode);
879 } else {
e24f0b8f 880 /*
bda807d4
MK
881 * In case of non-lru page, it could be released after
882 * isolation step. In that case, we shouldn't try migration.
e24f0b8f 883 */
bda807d4
MK
884 VM_BUG_ON_PAGE(!PageIsolated(page), page);
885 if (!PageMovable(page)) {
886 rc = MIGRATEPAGE_SUCCESS;
356ea386 887 ClearPageIsolated(page);
bda807d4
MK
888 goto out;
889 }
890
891 rc = mapping->a_ops->migratepage(mapping, newpage,
892 page, mode);
893 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
894 !PageIsolated(page));
895 }
e24f0b8f 896
5c3f9a67
HD
897 /*
898 * When successful, old pagecache page->mapping must be cleared before
899 * page is freed; but stats require that PageAnon be left as PageAnon.
900 */
901 if (rc == MIGRATEPAGE_SUCCESS) {
bda807d4
MK
902 if (__PageMovable(page)) {
903 VM_BUG_ON_PAGE(!PageIsolated(page), page);
904
905 /*
906 * We clear PG_movable under page_lock so any compactor
907 * cannot try to migrate this page.
908 */
356ea386 909 ClearPageIsolated(page);
bda807d4
MK
910 }
911
912 /*
c23a0c99 913 * Anonymous and movable page->mapping will be cleared by
bda807d4
MK
914 * free_pages_prepare so don't reset it here for keeping
915 * the type to work PageAnon, for example.
916 */
917 if (!PageMappingFlags(page))
5c3f9a67 918 page->mapping = NULL;
d2b2c6dd 919
3150be8f
MS
920 if (likely(!is_zone_device_page(newpage)))
921 flush_dcache_folio(page_folio(newpage));
3fe2011f 922 }
bda807d4 923out:
e24f0b8f
CL
924 return rc;
925}
926
0dabec93 927static int __unmap_and_move(struct page *page, struct page *newpage,
9c620e2b 928 int force, enum migrate_mode mode)
e24f0b8f 929{
0dabec93 930 int rc = -EAGAIN;
213ecb31 931 bool page_was_mapped = false;
3f6c8272 932 struct anon_vma *anon_vma = NULL;
bda807d4 933 bool is_lru = !__PageMovable(page);
95a402c3 934
529ae9aa 935 if (!trylock_page(page)) {
a6bc32b8 936 if (!force || mode == MIGRATE_ASYNC)
0dabec93 937 goto out;
3e7d3449
MG
938
939 /*
940 * It's not safe for direct compaction to call lock_page.
941 * For example, during page readahead pages are added locked
942 * to the LRU. Later, when the IO completes the pages are
943 * marked uptodate and unlocked. However, the queueing
944 * could be merging multiple pages for one bio (e.g.
d4388340 945 * mpage_readahead). If an allocation happens for the
3e7d3449
MG
946 * second or third page, the process can end up locking
947 * the same page twice and deadlocking. Rather than
948 * trying to be clever about what pages can be locked,
949 * avoid the use of lock_page for direct compaction
950 * altogether.
951 */
952 if (current->flags & PF_MEMALLOC)
0dabec93 953 goto out;
3e7d3449 954
e24f0b8f
CL
955 lock_page(page);
956 }
957
958 if (PageWriteback(page)) {
11bc82d6 959 /*
fed5b64a 960 * Only in the case of a full synchronous migration is it
a6bc32b8
MG
961 * necessary to wait for PageWriteback. In the async case,
962 * the retry loop is too short and in the sync-light case,
963 * the overhead of stalling is too much
11bc82d6 964 */
2916ecc0
JG
965 switch (mode) {
966 case MIGRATE_SYNC:
967 case MIGRATE_SYNC_NO_COPY:
968 break;
969 default:
11bc82d6 970 rc = -EBUSY;
0a31bc97 971 goto out_unlock;
11bc82d6
AA
972 }
973 if (!force)
0a31bc97 974 goto out_unlock;
e24f0b8f
CL
975 wait_on_page_writeback(page);
976 }
03f15c86 977
e24f0b8f 978 /*
68a9843f 979 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
dc386d4d 980 * we cannot notice that anon_vma is freed while we migrates a page.
1ce82b69 981 * This get_anon_vma() delays freeing anon_vma pointer until the end
dc386d4d 982 * of migration. File cache pages are no problem because of page_lock()
989f89c5
KH
983 * File Caches may use write_page() or lock_page() in migration, then,
984 * just care Anon page here.
03f15c86
HD
985 *
986 * Only page_get_anon_vma() understands the subtleties of
987 * getting a hold on an anon_vma from outside one of its mms.
988 * But if we cannot get anon_vma, then we won't need it anyway,
989 * because that implies that the anon page is no longer mapped
990 * (and cannot be remapped so long as we hold the page lock).
dc386d4d 991 */
03f15c86 992 if (PageAnon(page) && !PageKsm(page))
746b18d4 993 anon_vma = page_get_anon_vma(page);
62e1c553 994
7db7671f
HD
995 /*
996 * Block others from accessing the new page when we get around to
997 * establishing additional references. We are usually the only one
998 * holding a reference to newpage at this point. We used to have a BUG
999 * here if trylock_page(newpage) fails, but would like to allow for
1000 * cases where there might be a race with the previous use of newpage.
1001 * This is much like races on refcount of oldpage: just don't BUG().
1002 */
1003 if (unlikely(!trylock_page(newpage)))
1004 goto out_unlock;
1005
bda807d4
MK
1006 if (unlikely(!is_lru)) {
1007 rc = move_to_new_page(newpage, page, mode);
1008 goto out_unlock_both;
1009 }
1010
dc386d4d 1011 /*
62e1c553
SL
1012 * Corner case handling:
1013 * 1. When a new swap-cache page is read into, it is added to the LRU
1014 * and treated as swapcache but it has no rmap yet.
1015 * Calling try_to_unmap() against a page->mapping==NULL page will
1016 * trigger a BUG. So handle it here.
d12b8951 1017 * 2. An orphaned page (see truncate_cleanup_page) might have
62e1c553
SL
1018 * fs-private metadata. The page can be picked up due to memory
1019 * offlining. Everywhere else except page reclaim, the page is
1020 * invisible to the vm, so the page can not be migrated. So try to
1021 * free the metadata, so the page can be freed.
e24f0b8f 1022 */
62e1c553 1023 if (!page->mapping) {
309381fe 1024 VM_BUG_ON_PAGE(PageAnon(page), page);
1ce82b69 1025 if (page_has_private(page)) {
62e1c553 1026 try_to_free_buffers(page);
7db7671f 1027 goto out_unlock_both;
62e1c553 1028 }
7db7671f
HD
1029 } else if (page_mapped(page)) {
1030 /* Establish migration ptes */
03f15c86
HD
1031 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1032 page);
a98a2f0c 1033 try_to_migrate(page, 0);
213ecb31 1034 page_was_mapped = true;
2ebba6b7 1035 }
dc386d4d 1036
e6a1530d 1037 if (!page_mapped(page))
5c3f9a67 1038 rc = move_to_new_page(newpage, page, mode);
e24f0b8f 1039
5c3f9a67
HD
1040 if (page_was_mapped)
1041 remove_migration_ptes(page,
e388466d 1042 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
3f6c8272 1043
7db7671f
HD
1044out_unlock_both:
1045 unlock_page(newpage);
1046out_unlock:
3f6c8272 1047 /* Drop an anon_vma reference if we took one */
76545066 1048 if (anon_vma)
9e60109f 1049 put_anon_vma(anon_vma);
e24f0b8f 1050 unlock_page(page);
0dabec93 1051out:
c6c919eb
MK
1052 /*
1053 * If migration is successful, decrease refcount of the newpage
1054 * which will not free the page because new page owner increased
1055 * refcounter. As well, if it is LRU page, add the page to LRU
e0a352fa
DH
1056 * list in here. Use the old state of the isolated source page to
1057 * determine if we migrated a LRU page. newpage was already unlocked
1058 * and possibly modified by its owner - don't rely on the page
1059 * state.
c6c919eb
MK
1060 */
1061 if (rc == MIGRATEPAGE_SUCCESS) {
e0a352fa 1062 if (unlikely(!is_lru))
c6c919eb
MK
1063 put_page(newpage);
1064 else
1065 putback_lru_page(newpage);
1066 }
1067
0dabec93
MK
1068 return rc;
1069}
95a402c3 1070
0dabec93
MK
1071/*
1072 * Obtain the lock on page, remove all ptes and migrate the page
1073 * to the newly allocated page in newpage.
1074 */
6ec4476a 1075static int unmap_and_move(new_page_t get_new_page,
ef2a5153
GU
1076 free_page_t put_new_page,
1077 unsigned long private, struct page *page,
add05cec 1078 int force, enum migrate_mode mode,
dd4ae78a
YS
1079 enum migrate_reason reason,
1080 struct list_head *ret)
0dabec93 1081{
2def7424 1082 int rc = MIGRATEPAGE_SUCCESS;
74d4a579 1083 struct page *newpage = NULL;
0dabec93 1084
94723aaf 1085 if (!thp_migration_supported() && PageTransHuge(page))
d532e2e5 1086 return -ENOSYS;
94723aaf 1087
0dabec93
MK
1088 if (page_count(page) == 1) {
1089 /* page was freed from under us. So we are done. */
c6c919eb
MK
1090 ClearPageActive(page);
1091 ClearPageUnevictable(page);
bda807d4
MK
1092 if (unlikely(__PageMovable(page))) {
1093 lock_page(page);
1094 if (!PageMovable(page))
356ea386 1095 ClearPageIsolated(page);
bda807d4
MK
1096 unlock_page(page);
1097 }
0dabec93
MK
1098 goto out;
1099 }
1100
74d4a579
YS
1101 newpage = get_new_page(page, private);
1102 if (!newpage)
1103 return -ENOMEM;
1104
9c620e2b 1105 rc = __unmap_and_move(page, newpage, force, mode);
c6c919eb 1106 if (rc == MIGRATEPAGE_SUCCESS)
7cd12b4a 1107 set_page_owner_migrate_reason(newpage, reason);
bf6bddf1 1108
0dabec93 1109out:
e24f0b8f 1110 if (rc != -EAGAIN) {
0dabec93
MK
1111 /*
1112 * A page that has been migrated has all references
1113 * removed and will be freed. A page that has not been
c23a0c99 1114 * migrated will have kept its references and be restored.
0dabec93
MK
1115 */
1116 list_del(&page->lru);
dd4ae78a 1117 }
6afcf8ef 1118
dd4ae78a
YS
1119 /*
1120 * If migration is successful, releases reference grabbed during
1121 * isolation. Otherwise, restore the page to right list unless
1122 * we want to retry.
1123 */
1124 if (rc == MIGRATEPAGE_SUCCESS) {
6afcf8ef
ML
1125 /*
1126 * Compaction can migrate also non-LRU pages which are
1127 * not accounted to NR_ISOLATED_*. They can be recognized
1128 * as __PageMovable
1129 */
1130 if (likely(!__PageMovable(page)))
e8db67eb 1131 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
6c357848 1132 page_is_file_lru(page), -thp_nr_pages(page));
c6c919eb 1133
79f5f8fa 1134 if (reason != MR_MEMORY_FAILURE)
d7e69488 1135 /*
79f5f8fa 1136 * We release the page in page_handle_poison.
d7e69488 1137 */
79f5f8fa 1138 put_page(page);
c6c919eb 1139 } else {
dd4ae78a
YS
1140 if (rc != -EAGAIN)
1141 list_add_tail(&page->lru, ret);
bda807d4 1142
c6c919eb
MK
1143 if (put_new_page)
1144 put_new_page(newpage, private);
1145 else
1146 put_page(newpage);
e24f0b8f 1147 }
68711a74 1148
e24f0b8f
CL
1149 return rc;
1150}
1151
290408d4
NH
1152/*
1153 * Counterpart of unmap_and_move_page() for hugepage migration.
1154 *
1155 * This function doesn't wait the completion of hugepage I/O
1156 * because there is no race between I/O and migration for hugepage.
1157 * Note that currently hugepage I/O occurs only in direct I/O
1158 * where no lock is held and PG_writeback is irrelevant,
1159 * and writeback status of all subpages are counted in the reference
1160 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1161 * under direct I/O, the reference of the head page is 512 and a bit more.)
1162 * This means that when we try to migrate hugepage whose subpages are
1163 * doing direct I/O, some references remain after try_to_unmap() and
1164 * hugepage migration fails without data corruption.
1165 *
1166 * There is also no race when direct I/O is issued on the page under migration,
1167 * because then pte is replaced with migration swap entry and direct I/O code
1168 * will wait in the page fault for migration to complete.
1169 */
1170static int unmap_and_move_huge_page(new_page_t get_new_page,
68711a74
DR
1171 free_page_t put_new_page, unsigned long private,
1172 struct page *hpage, int force,
dd4ae78a
YS
1173 enum migrate_mode mode, int reason,
1174 struct list_head *ret)
290408d4 1175{
2def7424 1176 int rc = -EAGAIN;
2ebba6b7 1177 int page_was_mapped = 0;
32665f2b 1178 struct page *new_hpage;
290408d4 1179 struct anon_vma *anon_vma = NULL;
c0d0381a 1180 struct address_space *mapping = NULL;
290408d4 1181
83467efb 1182 /*
7ed2c31d 1183 * Migratability of hugepages depends on architectures and their size.
83467efb
NH
1184 * This check is necessary because some callers of hugepage migration
1185 * like soft offline and memory hotremove don't walk through page
1186 * tables or check whether the hugepage is pmd-based or not before
1187 * kicking migration.
1188 */
100873d7 1189 if (!hugepage_migration_supported(page_hstate(hpage))) {
dd4ae78a 1190 list_move_tail(&hpage->lru, ret);
83467efb 1191 return -ENOSYS;
32665f2b 1192 }
83467efb 1193
71a64f61
MS
1194 if (page_count(hpage) == 1) {
1195 /* page was freed from under us. So we are done. */
1196 putback_active_hugepage(hpage);
1197 return MIGRATEPAGE_SUCCESS;
1198 }
1199
666feb21 1200 new_hpage = get_new_page(hpage, private);
290408d4
NH
1201 if (!new_hpage)
1202 return -ENOMEM;
1203
290408d4 1204 if (!trylock_page(hpage)) {
2916ecc0 1205 if (!force)
290408d4 1206 goto out;
2916ecc0
JG
1207 switch (mode) {
1208 case MIGRATE_SYNC:
1209 case MIGRATE_SYNC_NO_COPY:
1210 break;
1211 default:
1212 goto out;
1213 }
290408d4
NH
1214 lock_page(hpage);
1215 }
1216
cb6acd01
MK
1217 /*
1218 * Check for pages which are in the process of being freed. Without
1219 * page_mapping() set, hugetlbfs specific move page routine will not
1220 * be called and we could leak usage counts for subpools.
1221 */
6acfb5ba 1222 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
cb6acd01
MK
1223 rc = -EBUSY;
1224 goto out_unlock;
1225 }
1226
746b18d4
PZ
1227 if (PageAnon(hpage))
1228 anon_vma = page_get_anon_vma(hpage);
290408d4 1229
7db7671f
HD
1230 if (unlikely(!trylock_page(new_hpage)))
1231 goto put_anon;
1232
2ebba6b7 1233 if (page_mapped(hpage)) {
336bf30e 1234 bool mapping_locked = false;
a98a2f0c 1235 enum ttu_flags ttu = 0;
336bf30e
MK
1236
1237 if (!PageAnon(hpage)) {
1238 /*
1239 * In shared mappings, try_to_unmap could potentially
1240 * call huge_pmd_unshare. Because of this, take
1241 * semaphore in write mode here and set TTU_RMAP_LOCKED
1242 * to let lower levels know we have taken the lock.
1243 */
1244 mapping = hugetlb_page_mapping_lock_write(hpage);
1245 if (unlikely(!mapping))
1246 goto unlock_put_anon;
1247
1248 mapping_locked = true;
1249 ttu |= TTU_RMAP_LOCKED;
1250 }
c0d0381a 1251
a98a2f0c 1252 try_to_migrate(hpage, ttu);
2ebba6b7 1253 page_was_mapped = 1;
336bf30e
MK
1254
1255 if (mapping_locked)
1256 i_mmap_unlock_write(mapping);
2ebba6b7 1257 }
290408d4
NH
1258
1259 if (!page_mapped(hpage))
5c3f9a67 1260 rc = move_to_new_page(new_hpage, hpage, mode);
290408d4 1261
336bf30e 1262 if (page_was_mapped)
5c3f9a67 1263 remove_migration_ptes(hpage,
336bf30e 1264 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
290408d4 1265
c0d0381a 1266unlock_put_anon:
7db7671f
HD
1267 unlock_page(new_hpage);
1268
1269put_anon:
fd4a4663 1270 if (anon_vma)
9e60109f 1271 put_anon_vma(anon_vma);
8e6ac7fa 1272
2def7424 1273 if (rc == MIGRATEPAGE_SUCCESS) {
ab5ac90a 1274 move_hugetlb_state(hpage, new_hpage, reason);
2def7424
HD
1275 put_new_page = NULL;
1276 }
8e6ac7fa 1277
cb6acd01 1278out_unlock:
290408d4 1279 unlock_page(hpage);
09761333 1280out:
dd4ae78a 1281 if (rc == MIGRATEPAGE_SUCCESS)
b8ec1cee 1282 putback_active_hugepage(hpage);
a04840c6 1283 else if (rc != -EAGAIN)
dd4ae78a 1284 list_move_tail(&hpage->lru, ret);
68711a74
DR
1285
1286 /*
1287 * If migration was not successful and there's a freeing callback, use
1288 * it. Otherwise, put_page() will drop the reference grabbed during
1289 * isolation.
1290 */
2def7424 1291 if (put_new_page)
68711a74
DR
1292 put_new_page(new_hpage, private);
1293 else
3aaa76e1 1294 putback_active_hugepage(new_hpage);
68711a74 1295
290408d4
NH
1296 return rc;
1297}
1298
d532e2e5
YS
1299static inline int try_split_thp(struct page *page, struct page **page2,
1300 struct list_head *from)
1301{
1302 int rc = 0;
1303
1304 lock_page(page);
1305 rc = split_huge_page_to_list(page, from);
1306 unlock_page(page);
1307 if (!rc)
1308 list_safe_reset_next(page, *page2, lru);
1309
1310 return rc;
1311}
1312
b20a3503 1313/*
c73e5c9c
SB
1314 * migrate_pages - migrate the pages specified in a list, to the free pages
1315 * supplied as the target for the page migration
b20a3503 1316 *
c73e5c9c
SB
1317 * @from: The list of pages to be migrated.
1318 * @get_new_page: The function used to allocate free pages to be used
1319 * as the target of the page migration.
68711a74
DR
1320 * @put_new_page: The function used to free target pages if migration
1321 * fails, or NULL if no special handling is necessary.
c73e5c9c
SB
1322 * @private: Private data to be passed on to get_new_page()
1323 * @mode: The migration mode that specifies the constraints for
1324 * page migration, if any.
1325 * @reason: The reason for page migration.
b5bade97 1326 * @ret_succeeded: Set to the number of normal pages migrated successfully if
5ac95884 1327 * the caller passes a non-NULL pointer.
b20a3503 1328 *
c73e5c9c
SB
1329 * The function returns after 10 attempts or if no pages are movable any more
1330 * because the list has become empty or no retryable pages exist any more.
dd4ae78a
YS
1331 * It is caller's responsibility to call putback_movable_pages() to return pages
1332 * to the LRU or free list only if ret != 0.
b20a3503 1333 *
5d39a7eb
BW
1334 * Returns the number of {normal page, THP, hugetlb} that were not migrated, or
1335 * an error code. The number of THP splits will be considered as the number of
1336 * non-migrated THP, no matter how many subpages of the THP are migrated successfully.
b20a3503 1337 */
9c620e2b 1338int migrate_pages(struct list_head *from, new_page_t get_new_page,
68711a74 1339 free_page_t put_new_page, unsigned long private,
5ac95884 1340 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
b20a3503 1341{
e24f0b8f 1342 int retry = 1;
1a5bae25 1343 int thp_retry = 1;
b20a3503 1344 int nr_failed = 0;
b5bade97 1345 int nr_failed_pages = 0;
5647bc29 1346 int nr_succeeded = 0;
1a5bae25
AK
1347 int nr_thp_succeeded = 0;
1348 int nr_thp_failed = 0;
1349 int nr_thp_split = 0;
b20a3503 1350 int pass = 0;
1a5bae25 1351 bool is_thp = false;
b20a3503
CL
1352 struct page *page;
1353 struct page *page2;
1a5bae25 1354 int rc, nr_subpages;
dd4ae78a 1355 LIST_HEAD(ret_pages);
b5bade97 1356 LIST_HEAD(thp_split_pages);
b0b515bf 1357 bool nosplit = (reason == MR_NUMA_MISPLACED);
b5bade97 1358 bool no_subpage_counting = false;
b20a3503 1359
7bc1aec5
LM
1360 trace_mm_migrate_pages_start(mode, reason);
1361
b5bade97 1362thp_subpage_migration:
1a5bae25 1363 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
e24f0b8f 1364 retry = 0;
1a5bae25 1365 thp_retry = 0;
b20a3503 1366
e24f0b8f 1367 list_for_each_entry_safe(page, page2, from, lru) {
94723aaf 1368retry:
1a5bae25
AK
1369 /*
1370 * THP statistics is based on the source huge page.
1371 * Capture required information that might get lost
1372 * during migration.
1373 */
6c5c7b9f 1374 is_thp = PageTransHuge(page) && !PageHuge(page);
5d39a7eb 1375 nr_subpages = compound_nr(page);
e24f0b8f 1376 cond_resched();
2d1db3b1 1377
31caf665
NH
1378 if (PageHuge(page))
1379 rc = unmap_and_move_huge_page(get_new_page,
68711a74 1380 put_new_page, private, page,
dd4ae78a
YS
1381 pass > 2, mode, reason,
1382 &ret_pages);
31caf665 1383 else
68711a74 1384 rc = unmap_and_move(get_new_page, put_new_page,
add05cec 1385 private, page, pass > 2, mode,
dd4ae78a
YS
1386 reason, &ret_pages);
1387 /*
1388 * The rules are:
1389 * Success: non hugetlb page will be freed, hugetlb
1390 * page will be put back
1391 * -EAGAIN: stay on the from list
1392 * -ENOMEM: stay on the from list
1393 * Other errno: put on ret_pages list then splice to
1394 * from list
1395 */
e24f0b8f 1396 switch(rc) {
d532e2e5
YS
1397 /*
1398 * THP migration might be unsupported or the
1399 * allocation could've failed so we should
1400 * retry on the same page with the THP split
1401 * to base pages.
1402 *
1403 * Head page is retried immediately and tail
1404 * pages are added to the tail of the list so
1405 * we encounter them after the rest of the list
1406 * is processed.
1407 */
1408 case -ENOSYS:
1409 /* THP migration is unsupported */
1410 if (is_thp) {
b5bade97
BW
1411 nr_thp_failed++;
1412 if (!try_split_thp(page, &page2, &thp_split_pages)) {
d532e2e5
YS
1413 nr_thp_split++;
1414 goto retry;
1415 }
1416
b5bade97 1417 nr_failed_pages += nr_subpages;
d532e2e5
YS
1418 break;
1419 }
1420
1421 /* Hugetlb migration is unsupported */
b5bade97
BW
1422 if (!no_subpage_counting)
1423 nr_failed++;
5d39a7eb 1424 nr_failed_pages += nr_subpages;
d532e2e5 1425 break;
95a402c3 1426 case -ENOMEM:
94723aaf 1427 /*
d532e2e5
YS
1428 * When memory is low, don't bother to try to migrate
1429 * other pages, just exit.
b0b515bf 1430 * THP NUMA faulting doesn't split THP to retry.
94723aaf 1431 */
b0b515bf 1432 if (is_thp && !nosplit) {
b5bade97
BW
1433 nr_thp_failed++;
1434 if (!try_split_thp(page, &page2, &thp_split_pages)) {
1a5bae25 1435 nr_thp_split++;
94723aaf
MH
1436 goto retry;
1437 }
6c5c7b9f 1438
b5bade97 1439 nr_failed_pages += nr_subpages;
1a5bae25
AK
1440 goto out;
1441 }
b5bade97
BW
1442
1443 if (!no_subpage_counting)
1444 nr_failed++;
5d39a7eb 1445 nr_failed_pages += nr_subpages;
95a402c3 1446 goto out;
e24f0b8f 1447 case -EAGAIN:
1a5bae25
AK
1448 if (is_thp) {
1449 thp_retry++;
1450 break;
1451 }
2d1db3b1 1452 retry++;
e24f0b8f 1453 break;
78bd5209 1454 case MIGRATEPAGE_SUCCESS:
5d39a7eb 1455 nr_succeeded += nr_subpages;
1a5bae25
AK
1456 if (is_thp) {
1457 nr_thp_succeeded++;
1a5bae25
AK
1458 break;
1459 }
e24f0b8f
CL
1460 break;
1461 default:
354a3363 1462 /*
d532e2e5 1463 * Permanent failure (-EBUSY, etc.):
354a3363
NH
1464 * unlike -EAGAIN case, the failed page is
1465 * removed from migration page list and not
1466 * retried in the next outer loop.
1467 */
1a5bae25
AK
1468 if (is_thp) {
1469 nr_thp_failed++;
b5bade97 1470 nr_failed_pages += nr_subpages;
1a5bae25
AK
1471 break;
1472 }
b5bade97
BW
1473
1474 if (!no_subpage_counting)
1475 nr_failed++;
5d39a7eb 1476 nr_failed_pages += nr_subpages;
e24f0b8f 1477 break;
2d1db3b1 1478 }
b20a3503
CL
1479 }
1480 }
b5bade97 1481 nr_failed += retry;
1a5bae25 1482 nr_thp_failed += thp_retry;
b5bade97
BW
1483 /*
1484 * Try to migrate subpages of fail-to-migrate THPs, no nr_failed
1485 * counting in this round, since all subpages of a THP is counted
1486 * as 1 failure in the first round.
1487 */
1488 if (!list_empty(&thp_split_pages)) {
1489 /*
1490 * Move non-migrated pages (after 10 retries) to ret_pages
1491 * to avoid migrating them again.
1492 */
1493 list_splice_init(from, &ret_pages);
1494 list_splice_init(&thp_split_pages, from);
1495 no_subpage_counting = true;
1496 retry = 1;
1497 goto thp_subpage_migration;
1498 }
1499
1500 rc = nr_failed + nr_thp_failed;
95a402c3 1501out:
dd4ae78a
YS
1502 /*
1503 * Put the permanent failure page back to migration list, they
1504 * will be put back to the right list by the caller.
1505 */
1506 list_splice(&ret_pages, from);
1507
1a5bae25 1508 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
b5bade97 1509 count_vm_events(PGMIGRATE_FAIL, nr_failed_pages);
1a5bae25
AK
1510 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1511 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1512 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
b5bade97 1513 trace_mm_migrate_pages(nr_succeeded, nr_failed_pages, nr_thp_succeeded,
1a5bae25 1514 nr_thp_failed, nr_thp_split, mode, reason);
7b2a2d4a 1515
5ac95884
YS
1516 if (ret_succeeded)
1517 *ret_succeeded = nr_succeeded;
1518
78bd5209 1519 return rc;
b20a3503 1520}
95a402c3 1521
19fc7bed 1522struct page *alloc_migration_target(struct page *page, unsigned long private)
b4b38223 1523{
19fc7bed
JK
1524 struct migration_target_control *mtc;
1525 gfp_t gfp_mask;
b4b38223
JK
1526 unsigned int order = 0;
1527 struct page *new_page = NULL;
19fc7bed
JK
1528 int nid;
1529 int zidx;
1530
1531 mtc = (struct migration_target_control *)private;
1532 gfp_mask = mtc->gfp_mask;
1533 nid = mtc->nid;
1534 if (nid == NUMA_NO_NODE)
1535 nid = page_to_nid(page);
b4b38223 1536
d92bbc27
JK
1537 if (PageHuge(page)) {
1538 struct hstate *h = page_hstate(compound_head(page));
1539
19fc7bed
JK
1540 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1541 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
d92bbc27 1542 }
b4b38223
JK
1543
1544 if (PageTransHuge(page)) {
9933a0c8
JK
1545 /*
1546 * clear __GFP_RECLAIM to make the migration callback
1547 * consistent with regular THP allocations.
1548 */
1549 gfp_mask &= ~__GFP_RECLAIM;
b4b38223
JK
1550 gfp_mask |= GFP_TRANSHUGE;
1551 order = HPAGE_PMD_ORDER;
1552 }
19fc7bed
JK
1553 zidx = zone_idx(page_zone(page));
1554 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
b4b38223
JK
1555 gfp_mask |= __GFP_HIGHMEM;
1556
84172f4b 1557 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
b4b38223
JK
1558
1559 if (new_page && PageTransHuge(new_page))
1560 prep_transhuge_page(new_page);
1561
1562 return new_page;
1563}
1564
742755a1 1565#ifdef CONFIG_NUMA
742755a1 1566
a49bd4d7 1567static int store_status(int __user *status, int start, int value, int nr)
742755a1 1568{
a49bd4d7
MH
1569 while (nr-- > 0) {
1570 if (put_user(value, status + start))
1571 return -EFAULT;
1572 start++;
1573 }
1574
1575 return 0;
1576}
1577
1578static int do_move_pages_to_node(struct mm_struct *mm,
1579 struct list_head *pagelist, int node)
1580{
1581 int err;
a0976311
JK
1582 struct migration_target_control mtc = {
1583 .nid = node,
1584 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1585 };
a49bd4d7 1586
a0976311 1587 err = migrate_pages(pagelist, alloc_migration_target, NULL,
5ac95884 1588 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
a49bd4d7
MH
1589 if (err)
1590 putback_movable_pages(pagelist);
1591 return err;
742755a1
CL
1592}
1593
1594/*
a49bd4d7
MH
1595 * Resolves the given address to a struct page, isolates it from the LRU and
1596 * puts it to the given pagelist.
e0153fc2
YS
1597 * Returns:
1598 * errno - if the page cannot be found/isolated
1599 * 0 - when it doesn't have to be migrated because it is already on the
1600 * target node
1601 * 1 - when it has been queued
742755a1 1602 */
a49bd4d7
MH
1603static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1604 int node, struct list_head *pagelist, bool migrate_all)
742755a1 1605{
a49bd4d7
MH
1606 struct vm_area_struct *vma;
1607 struct page *page;
742755a1 1608 int err;
742755a1 1609
d8ed45c5 1610 mmap_read_lock(mm);
a49bd4d7
MH
1611 err = -EFAULT;
1612 vma = find_vma(mm, addr);
1613 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1614 goto out;
742755a1 1615
a49bd4d7 1616 /* FOLL_DUMP to ignore special (like zero) pages */
87d2762e 1617 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
89f5b7da 1618
a49bd4d7
MH
1619 err = PTR_ERR(page);
1620 if (IS_ERR(page))
1621 goto out;
89f5b7da 1622
a49bd4d7
MH
1623 err = -ENOENT;
1624 if (!page)
1625 goto out;
742755a1 1626
a49bd4d7
MH
1627 err = 0;
1628 if (page_to_nid(page) == node)
1629 goto out_putpage;
742755a1 1630
a49bd4d7
MH
1631 err = -EACCES;
1632 if (page_mapcount(page) > 1 && !migrate_all)
1633 goto out_putpage;
742755a1 1634
a49bd4d7
MH
1635 if (PageHuge(page)) {
1636 if (PageHead(page)) {
1637 isolate_huge_page(page, pagelist);
e0153fc2 1638 err = 1;
e632a938 1639 }
a49bd4d7
MH
1640 } else {
1641 struct page *head;
e632a938 1642
e8db67eb
NH
1643 head = compound_head(page);
1644 err = isolate_lru_page(head);
cf608ac1 1645 if (err)
a49bd4d7 1646 goto out_putpage;
742755a1 1647
e0153fc2 1648 err = 1;
a49bd4d7
MH
1649 list_add_tail(&head->lru, pagelist);
1650 mod_node_page_state(page_pgdat(head),
9de4f22a 1651 NR_ISOLATED_ANON + page_is_file_lru(head),
6c357848 1652 thp_nr_pages(head));
a49bd4d7
MH
1653 }
1654out_putpage:
1655 /*
1656 * Either remove the duplicate refcount from
1657 * isolate_lru_page() or drop the page ref if it was
1658 * not isolated.
1659 */
1660 put_page(page);
1661out:
d8ed45c5 1662 mmap_read_unlock(mm);
742755a1
CL
1663 return err;
1664}
1665
7ca8783a
WY
1666static int move_pages_and_store_status(struct mm_struct *mm, int node,
1667 struct list_head *pagelist, int __user *status,
1668 int start, int i, unsigned long nr_pages)
1669{
1670 int err;
1671
5d7ae891
WY
1672 if (list_empty(pagelist))
1673 return 0;
1674
7ca8783a
WY
1675 err = do_move_pages_to_node(mm, pagelist, node);
1676 if (err) {
1677 /*
1678 * Positive err means the number of failed
1679 * pages to migrate. Since we are going to
1680 * abort and return the number of non-migrated
ab9dd4f8 1681 * pages, so need to include the rest of the
7ca8783a
WY
1682 * nr_pages that have not been attempted as
1683 * well.
1684 */
1685 if (err > 0)
1686 err += nr_pages - i - 1;
1687 return err;
1688 }
1689 return store_status(status, start, node, i - start);
1690}
1691
5e9a0f02
BG
1692/*
1693 * Migrate an array of page address onto an array of nodes and fill
1694 * the corresponding array of status.
1695 */
3268c63e 1696static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
5e9a0f02
BG
1697 unsigned long nr_pages,
1698 const void __user * __user *pages,
1699 const int __user *nodes,
1700 int __user *status, int flags)
1701{
a49bd4d7
MH
1702 int current_node = NUMA_NO_NODE;
1703 LIST_HEAD(pagelist);
1704 int start, i;
1705 int err = 0, err1;
35282a2d 1706
361a2a22 1707 lru_cache_disable();
35282a2d 1708
a49bd4d7
MH
1709 for (i = start = 0; i < nr_pages; i++) {
1710 const void __user *p;
1711 unsigned long addr;
1712 int node;
3140a227 1713
a49bd4d7
MH
1714 err = -EFAULT;
1715 if (get_user(p, pages + i))
1716 goto out_flush;
1717 if (get_user(node, nodes + i))
1718 goto out_flush;
057d3389 1719 addr = (unsigned long)untagged_addr(p);
a49bd4d7
MH
1720
1721 err = -ENODEV;
1722 if (node < 0 || node >= MAX_NUMNODES)
1723 goto out_flush;
1724 if (!node_state(node, N_MEMORY))
1725 goto out_flush;
5e9a0f02 1726
a49bd4d7
MH
1727 err = -EACCES;
1728 if (!node_isset(node, task_nodes))
1729 goto out_flush;
1730
1731 if (current_node == NUMA_NO_NODE) {
1732 current_node = node;
1733 start = i;
1734 } else if (node != current_node) {
7ca8783a
WY
1735 err = move_pages_and_store_status(mm, current_node,
1736 &pagelist, status, start, i, nr_pages);
a49bd4d7
MH
1737 if (err)
1738 goto out;
1739 start = i;
1740 current_node = node;
3140a227
BG
1741 }
1742
a49bd4d7
MH
1743 /*
1744 * Errors in the page lookup or isolation are not fatal and we simply
1745 * report them via status
1746 */
1747 err = add_page_for_migration(mm, addr, current_node,
1748 &pagelist, flags & MPOL_MF_MOVE_ALL);
e0153fc2 1749
d08221a0 1750 if (err > 0) {
e0153fc2
YS
1751 /* The page is successfully queued for migration */
1752 continue;
1753 }
3140a227 1754
65462462
JH
1755 /*
1756 * The move_pages() man page does not have an -EEXIST choice, so
1757 * use -EFAULT instead.
1758 */
1759 if (err == -EEXIST)
1760 err = -EFAULT;
1761
d08221a0
WY
1762 /*
1763 * If the page is already on the target node (!err), store the
1764 * node, otherwise, store the err.
1765 */
1766 err = store_status(status, i, err ? : current_node, 1);
a49bd4d7
MH
1767 if (err)
1768 goto out_flush;
5e9a0f02 1769
7ca8783a
WY
1770 err = move_pages_and_store_status(mm, current_node, &pagelist,
1771 status, start, i, nr_pages);
4afdacec
WY
1772 if (err)
1773 goto out;
a49bd4d7 1774 current_node = NUMA_NO_NODE;
3140a227 1775 }
a49bd4d7
MH
1776out_flush:
1777 /* Make sure we do not overwrite the existing error */
7ca8783a
WY
1778 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1779 status, start, i, nr_pages);
dfe9aa23 1780 if (err >= 0)
a49bd4d7 1781 err = err1;
5e9a0f02 1782out:
361a2a22 1783 lru_cache_enable();
5e9a0f02
BG
1784 return err;
1785}
1786
742755a1 1787/*
2f007e74 1788 * Determine the nodes of an array of pages and store it in an array of status.
742755a1 1789 */
80bba129
BG
1790static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1791 const void __user **pages, int *status)
742755a1 1792{
2f007e74 1793 unsigned long i;
2f007e74 1794
d8ed45c5 1795 mmap_read_lock(mm);
742755a1 1796
2f007e74 1797 for (i = 0; i < nr_pages; i++) {
80bba129 1798 unsigned long addr = (unsigned long)(*pages);
742755a1
CL
1799 struct vm_area_struct *vma;
1800 struct page *page;
c095adbc 1801 int err = -EFAULT;
2f007e74 1802
059b8b48
LH
1803 vma = vma_lookup(mm, addr);
1804 if (!vma)
742755a1
CL
1805 goto set_status;
1806
d899844e
KS
1807 /* FOLL_DUMP to ignore special (like zero) pages */
1808 page = follow_page(vma, addr, FOLL_DUMP);
89f5b7da
LT
1809
1810 err = PTR_ERR(page);
1811 if (IS_ERR(page))
1812 goto set_status;
1813
d899844e 1814 err = page ? page_to_nid(page) : -ENOENT;
742755a1 1815set_status:
80bba129
BG
1816 *status = err;
1817
1818 pages++;
1819 status++;
1820 }
1821
d8ed45c5 1822 mmap_read_unlock(mm);
80bba129
BG
1823}
1824
5b1b561b
AB
1825static int get_compat_pages_array(const void __user *chunk_pages[],
1826 const void __user * __user *pages,
1827 unsigned long chunk_nr)
1828{
1829 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1830 compat_uptr_t p;
1831 int i;
1832
1833 for (i = 0; i < chunk_nr; i++) {
1834 if (get_user(p, pages32 + i))
1835 return -EFAULT;
1836 chunk_pages[i] = compat_ptr(p);
1837 }
1838
1839 return 0;
1840}
1841
80bba129
BG
1842/*
1843 * Determine the nodes of a user array of pages and store it in
1844 * a user array of status.
1845 */
1846static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1847 const void __user * __user *pages,
1848 int __user *status)
1849{
1850#define DO_PAGES_STAT_CHUNK_NR 16
1851 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1852 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
80bba129 1853
87b8d1ad
PA
1854 while (nr_pages) {
1855 unsigned long chunk_nr;
80bba129 1856
87b8d1ad
PA
1857 chunk_nr = nr_pages;
1858 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1859 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1860
5b1b561b
AB
1861 if (in_compat_syscall()) {
1862 if (get_compat_pages_array(chunk_pages, pages,
1863 chunk_nr))
1864 break;
1865 } else {
1866 if (copy_from_user(chunk_pages, pages,
1867 chunk_nr * sizeof(*chunk_pages)))
1868 break;
1869 }
80bba129
BG
1870
1871 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1872
87b8d1ad
PA
1873 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1874 break;
742755a1 1875
87b8d1ad
PA
1876 pages += chunk_nr;
1877 status += chunk_nr;
1878 nr_pages -= chunk_nr;
1879 }
1880 return nr_pages ? -EFAULT : 0;
742755a1
CL
1881}
1882
4dc200ce 1883static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
742755a1 1884{
742755a1 1885 struct task_struct *task;
742755a1 1886 struct mm_struct *mm;
742755a1 1887
4dc200ce
ML
1888 /*
1889 * There is no need to check if current process has the right to modify
1890 * the specified process when they are same.
1891 */
1892 if (!pid) {
1893 mmget(current->mm);
1894 *mem_nodes = cpuset_mems_allowed(current);
1895 return current->mm;
1896 }
742755a1
CL
1897
1898 /* Find the mm_struct */
a879bf58 1899 rcu_read_lock();
4dc200ce 1900 task = find_task_by_vpid(pid);
742755a1 1901 if (!task) {
a879bf58 1902 rcu_read_unlock();
4dc200ce 1903 return ERR_PTR(-ESRCH);
742755a1 1904 }
3268c63e 1905 get_task_struct(task);
742755a1
CL
1906
1907 /*
1908 * Check if this process has the right to modify the specified
197e7e52 1909 * process. Use the regular "ptrace_may_access()" checks.
742755a1 1910 */
197e7e52 1911 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
c69e8d9c 1912 rcu_read_unlock();
4dc200ce 1913 mm = ERR_PTR(-EPERM);
5e9a0f02 1914 goto out;
742755a1 1915 }
c69e8d9c 1916 rcu_read_unlock();
742755a1 1917
4dc200ce
ML
1918 mm = ERR_PTR(security_task_movememory(task));
1919 if (IS_ERR(mm))
5e9a0f02 1920 goto out;
4dc200ce 1921 *mem_nodes = cpuset_mems_allowed(task);
3268c63e 1922 mm = get_task_mm(task);
4dc200ce 1923out:
3268c63e 1924 put_task_struct(task);
6e8b09ea 1925 if (!mm)
4dc200ce
ML
1926 mm = ERR_PTR(-EINVAL);
1927 return mm;
1928}
1929
1930/*
1931 * Move a list of pages in the address space of the currently executing
1932 * process.
1933 */
1934static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
1935 const void __user * __user *pages,
1936 const int __user *nodes,
1937 int __user *status, int flags)
1938{
1939 struct mm_struct *mm;
1940 int err;
1941 nodemask_t task_nodes;
1942
1943 /* Check flags */
1944 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
6e8b09ea
SL
1945 return -EINVAL;
1946
4dc200ce
ML
1947 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1948 return -EPERM;
1949
1950 mm = find_mm_struct(pid, &task_nodes);
1951 if (IS_ERR(mm))
1952 return PTR_ERR(mm);
1953
6e8b09ea
SL
1954 if (nodes)
1955 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1956 nodes, status, flags);
1957 else
1958 err = do_pages_stat(mm, nr_pages, pages, status);
742755a1 1959
742755a1
CL
1960 mmput(mm);
1961 return err;
1962}
742755a1 1963
7addf443
DB
1964SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1965 const void __user * __user *, pages,
1966 const int __user *, nodes,
1967 int __user *, status, int, flags)
1968{
1969 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
1970}
1971
7039e1db
PZ
1972#ifdef CONFIG_NUMA_BALANCING
1973/*
1974 * Returns true if this is a safe migration target node for misplaced NUMA
1975 * pages. Currently it only checks the watermarks which crude
1976 */
1977static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
3abef4e6 1978 unsigned long nr_migrate_pages)
7039e1db
PZ
1979{
1980 int z;
599d0c95 1981
7039e1db
PZ
1982 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1983 struct zone *zone = pgdat->node_zones + z;
1984
1985 if (!populated_zone(zone))
1986 continue;
1987
7039e1db
PZ
1988 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1989 if (!zone_watermark_ok(zone, 0,
1990 high_wmark_pages(zone) +
1991 nr_migrate_pages,
bfe9d006 1992 ZONE_MOVABLE, 0))
7039e1db
PZ
1993 continue;
1994 return true;
1995 }
1996 return false;
1997}
1998
1999static struct page *alloc_misplaced_dst_page(struct page *page,
666feb21 2000 unsigned long data)
7039e1db
PZ
2001{
2002 int nid = (int) data;
2003 struct page *newpage;
2004
96db800f 2005 newpage = __alloc_pages_node(nid,
e97ca8e5
JW
2006 (GFP_HIGHUSER_MOVABLE |
2007 __GFP_THISNODE | __GFP_NOMEMALLOC |
2008 __GFP_NORETRY | __GFP_NOWARN) &
8479eba7 2009 ~__GFP_RECLAIM, 0);
bac0382c 2010
7039e1db
PZ
2011 return newpage;
2012}
2013
c5b5a3dd
YS
2014static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2015 unsigned long data)
2016{
2017 int nid = (int) data;
2018 struct page *newpage;
2019
2020 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2021 HPAGE_PMD_ORDER);
2022 if (!newpage)
2023 goto out;
2024
2025 prep_transhuge_page(newpage);
2026
2027out:
2028 return newpage;
2029}
2030
1c30e017 2031static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
b32967ff 2032{
340ef390 2033 int page_lru;
2b9b624f 2034 int nr_pages = thp_nr_pages(page);
c574bbe9 2035 int order = compound_order(page);
a8f60772 2036
c574bbe9 2037 VM_BUG_ON_PAGE(order && !PageTransHuge(page), page);
3abef4e6 2038
662aeea7
YS
2039 /* Do not migrate THP mapped by multiple processes */
2040 if (PageTransHuge(page) && total_mapcount(page) > 1)
2041 return 0;
2042
7039e1db 2043 /* Avoid migrating to a node that is nearly full */
c574bbe9
HY
2044 if (!migrate_balanced_pgdat(pgdat, nr_pages)) {
2045 int z;
2046
2047 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING))
2048 return 0;
2049 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2050 if (populated_zone(pgdat->node_zones + z))
2051 break;
2052 }
2053 wakeup_kswapd(pgdat->node_zones + z, 0, order, ZONE_MOVABLE);
340ef390 2054 return 0;
c574bbe9 2055 }
7039e1db 2056
340ef390
HD
2057 if (isolate_lru_page(page))
2058 return 0;
7039e1db 2059
9de4f22a 2060 page_lru = page_is_file_lru(page);
599d0c95 2061 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
2b9b624f 2062 nr_pages);
340ef390 2063
149c33e1 2064 /*
340ef390
HD
2065 * Isolating the page has taken another reference, so the
2066 * caller's reference can be safely dropped without the page
2067 * disappearing underneath us during migration.
149c33e1
MG
2068 */
2069 put_page(page);
340ef390 2070 return 1;
b32967ff
MG
2071}
2072
2073/*
2074 * Attempt to migrate a misplaced page to the specified destination
2075 * node. Caller is expected to have an elevated reference count on
2076 * the page that will be dropped by this function before returning.
2077 */
1bc115d8
MG
2078int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2079 int node)
b32967ff
MG
2080{
2081 pg_data_t *pgdat = NODE_DATA(node);
340ef390 2082 int isolated;
b32967ff 2083 int nr_remaining;
e39bb6be 2084 unsigned int nr_succeeded;
b32967ff 2085 LIST_HEAD(migratepages);
c5b5a3dd
YS
2086 new_page_t *new;
2087 bool compound;
b5916c02 2088 int nr_pages = thp_nr_pages(page);
c5b5a3dd
YS
2089
2090 /*
2091 * PTE mapped THP or HugeTLB page can't reach here so the page could
2092 * be either base page or THP. And it must be head page if it is
2093 * THP.
2094 */
2095 compound = PageTransHuge(page);
2096
2097 if (compound)
2098 new = alloc_misplaced_dst_page_thp;
2099 else
2100 new = alloc_misplaced_dst_page;
b32967ff
MG
2101
2102 /*
1bc115d8
MG
2103 * Don't migrate file pages that are mapped in multiple processes
2104 * with execute permissions as they are probably shared libraries.
b32967ff 2105 */
7ee820ee
ML
2106 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2107 (vma->vm_flags & VM_EXEC))
b32967ff 2108 goto out;
b32967ff 2109
09a913a7
MG
2110 /*
2111 * Also do not migrate dirty pages as not all filesystems can move
2112 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2113 */
9de4f22a 2114 if (page_is_file_lru(page) && PageDirty(page))
09a913a7
MG
2115 goto out;
2116
b32967ff
MG
2117 isolated = numamigrate_isolate_page(pgdat, page);
2118 if (!isolated)
2119 goto out;
2120
2121 list_add(&page->lru, &migratepages);
c5b5a3dd 2122 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
e39bb6be
HY
2123 MIGRATE_ASYNC, MR_NUMA_MISPLACED,
2124 &nr_succeeded);
b32967ff 2125 if (nr_remaining) {
59c82b70
JK
2126 if (!list_empty(&migratepages)) {
2127 list_del(&page->lru);
c5fc5c3a
YS
2128 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2129 page_is_file_lru(page), -nr_pages);
59c82b70
JK
2130 putback_lru_page(page);
2131 }
b32967ff 2132 isolated = 0;
e39bb6be
HY
2133 }
2134 if (nr_succeeded) {
2135 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
2136 if (!node_is_toptier(page_to_nid(page)) && node_is_toptier(node))
2137 mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
2138 nr_succeeded);
2139 }
7039e1db 2140 BUG_ON(!list_empty(&migratepages));
7039e1db 2141 return isolated;
340ef390
HD
2142
2143out:
2144 put_page(page);
2145 return 0;
7039e1db 2146}
220018d3 2147#endif /* CONFIG_NUMA_BALANCING */
7039e1db 2148#endif /* CONFIG_NUMA */
8763cb45 2149
9b2ed9cb 2150#ifdef CONFIG_DEVICE_PRIVATE
843e1be1 2151static int migrate_vma_collect_skip(unsigned long start,
8763cb45
JG
2152 unsigned long end,
2153 struct mm_walk *walk)
2154{
2155 struct migrate_vma *migrate = walk->private;
2156 unsigned long addr;
2157
872ea707 2158 for (addr = start; addr < end; addr += PAGE_SIZE) {
8315ada7 2159 migrate->dst[migrate->npages] = 0;
843e1be1 2160 migrate->src[migrate->npages++] = 0;
8315ada7
JG
2161 }
2162
2163 return 0;
2164}
2165
843e1be1 2166static int migrate_vma_collect_hole(unsigned long start,
8315ada7 2167 unsigned long end,
843e1be1 2168 __always_unused int depth,
8315ada7
JG
2169 struct mm_walk *walk)
2170{
2171 struct migrate_vma *migrate = walk->private;
2172 unsigned long addr;
2173
843e1be1
ML
2174 /* Only allow populating anonymous memory. */
2175 if (!vma_is_anonymous(walk->vma))
2176 return migrate_vma_collect_skip(start, end, walk);
2177
872ea707 2178 for (addr = start; addr < end; addr += PAGE_SIZE) {
843e1be1 2179 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
8763cb45 2180 migrate->dst[migrate->npages] = 0;
843e1be1
ML
2181 migrate->npages++;
2182 migrate->cpages++;
8763cb45
JG
2183 }
2184
2185 return 0;
2186}
2187
2188static int migrate_vma_collect_pmd(pmd_t *pmdp,
2189 unsigned long start,
2190 unsigned long end,
2191 struct mm_walk *walk)
2192{
2193 struct migrate_vma *migrate = walk->private;
2194 struct vm_area_struct *vma = walk->vma;
2195 struct mm_struct *mm = vma->vm_mm;
8c3328f1 2196 unsigned long addr = start, unmapped = 0;
8763cb45
JG
2197 spinlock_t *ptl;
2198 pte_t *ptep;
2199
2200again:
2201 if (pmd_none(*pmdp))
b7a16c7a 2202 return migrate_vma_collect_hole(start, end, -1, walk);
8763cb45
JG
2203
2204 if (pmd_trans_huge(*pmdp)) {
2205 struct page *page;
2206
2207 ptl = pmd_lock(mm, pmdp);
2208 if (unlikely(!pmd_trans_huge(*pmdp))) {
2209 spin_unlock(ptl);
2210 goto again;
2211 }
2212
2213 page = pmd_page(*pmdp);
2214 if (is_huge_zero_page(page)) {
2215 spin_unlock(ptl);
2216 split_huge_pmd(vma, pmdp, addr);
2217 if (pmd_trans_unstable(pmdp))
8315ada7 2218 return migrate_vma_collect_skip(start, end,
8763cb45
JG
2219 walk);
2220 } else {
2221 int ret;
2222
2223 get_page(page);
2224 spin_unlock(ptl);
2225 if (unlikely(!trylock_page(page)))
8315ada7 2226 return migrate_vma_collect_skip(start, end,
8763cb45
JG
2227 walk);
2228 ret = split_huge_page(page);
2229 unlock_page(page);
2230 put_page(page);
8315ada7
JG
2231 if (ret)
2232 return migrate_vma_collect_skip(start, end,
2233 walk);
2234 if (pmd_none(*pmdp))
b7a16c7a 2235 return migrate_vma_collect_hole(start, end, -1,
8763cb45
JG
2236 walk);
2237 }
2238 }
2239
2240 if (unlikely(pmd_bad(*pmdp)))
8315ada7 2241 return migrate_vma_collect_skip(start, end, walk);
8763cb45
JG
2242
2243 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
8c3328f1
JG
2244 arch_enter_lazy_mmu_mode();
2245
8763cb45 2246 for (; addr < end; addr += PAGE_SIZE, ptep++) {
800bb1c8 2247 unsigned long mpfn = 0, pfn;
8763cb45 2248 struct page *page;
8c3328f1 2249 swp_entry_t entry;
8763cb45
JG
2250 pte_t pte;
2251
2252 pte = *ptep;
8763cb45 2253
a5430dda 2254 if (pte_none(pte)) {
0744f280
RC
2255 if (vma_is_anonymous(vma)) {
2256 mpfn = MIGRATE_PFN_MIGRATE;
2257 migrate->cpages++;
2258 }
8763cb45
JG
2259 goto next;
2260 }
2261
a5430dda 2262 if (!pte_present(pte)) {
a5430dda
JG
2263 /*
2264 * Only care about unaddressable device page special
2265 * page table entry. Other special swap entries are not
2266 * migratable, and we ignore regular swapped page.
2267 */
2268 entry = pte_to_swp_entry(pte);
2269 if (!is_device_private_entry(entry))
2270 goto next;
2271
af5cdaf8 2272 page = pfn_swap_entry_to_page(entry);
5143192c
RC
2273 if (!(migrate->flags &
2274 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2275 page->pgmap->owner != migrate->pgmap_owner)
800bb1c8
CH
2276 goto next;
2277
06d462be
CH
2278 mpfn = migrate_pfn(page_to_pfn(page)) |
2279 MIGRATE_PFN_MIGRATE;
4dd845b5 2280 if (is_writable_device_private_entry(entry))
a5430dda
JG
2281 mpfn |= MIGRATE_PFN_WRITE;
2282 } else {
5143192c 2283 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
800bb1c8 2284 goto next;
276f756d 2285 pfn = pte_pfn(pte);
8315ada7
JG
2286 if (is_zero_pfn(pfn)) {
2287 mpfn = MIGRATE_PFN_MIGRATE;
2288 migrate->cpages++;
8315ada7
JG
2289 goto next;
2290 }
25b2995a 2291 page = vm_normal_page(migrate->vma, addr, pte);
a5430dda
JG
2292 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2293 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2294 }
2295
8763cb45 2296 /* FIXME support THP */
8763cb45 2297 if (!page || !page->mapping || PageTransCompound(page)) {
276f756d 2298 mpfn = 0;
8763cb45
JG
2299 goto next;
2300 }
2301
2302 /*
2303 * By getting a reference on the page we pin it and that blocks
2304 * any kind of migration. Side effect is that it "freezes" the
2305 * pte.
2306 *
2307 * We drop this reference after isolating the page from the lru
2308 * for non device page (device page are not on the lru and thus
2309 * can't be dropped from it).
2310 */
2311 get_page(page);
8763cb45 2312
8c3328f1
JG
2313 /*
2314 * Optimize for the common case where page is only mapped once
2315 * in one process. If we can lock the page, then we can safely
2316 * set up a special migration page table entry now.
2317 */
2318 if (trylock_page(page)) {
2319 pte_t swp_pte;
2320
ab09243a 2321 migrate->cpages++;
8c3328f1
JG
2322 ptep_get_and_clear(mm, addr, ptep);
2323
2324 /* Setup special migration page table entry */
4dd845b5
AP
2325 if (mpfn & MIGRATE_PFN_WRITE)
2326 entry = make_writable_migration_entry(
2327 page_to_pfn(page));
2328 else
2329 entry = make_readable_migration_entry(
2330 page_to_pfn(page));
8c3328f1 2331 swp_pte = swp_entry_to_pte(entry);
ad7df764
AP
2332 if (pte_present(pte)) {
2333 if (pte_soft_dirty(pte))
2334 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2335 if (pte_uffd_wp(pte))
2336 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2337 } else {
2338 if (pte_swp_soft_dirty(pte))
2339 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2340 if (pte_swp_uffd_wp(pte))
2341 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2342 }
8c3328f1
JG
2343 set_pte_at(mm, addr, ptep, swp_pte);
2344
2345 /*
2346 * This is like regular unmap: we remove the rmap and
2347 * drop page refcount. Page won't be freed, as we took
2348 * a reference just above.
2349 */
2350 page_remove_rmap(page, false);
2351 put_page(page);
a5430dda
JG
2352
2353 if (pte_present(pte))
2354 unmapped++;
ab09243a
AP
2355 } else {
2356 put_page(page);
2357 mpfn = 0;
8c3328f1
JG
2358 }
2359
8763cb45 2360next:
a5430dda 2361 migrate->dst[migrate->npages] = 0;
8763cb45
JG
2362 migrate->src[migrate->npages++] = mpfn;
2363 }
8c3328f1 2364 arch_leave_lazy_mmu_mode();
8763cb45
JG
2365 pte_unmap_unlock(ptep - 1, ptl);
2366
8c3328f1
JG
2367 /* Only flush the TLB if we actually modified any entries */
2368 if (unmapped)
2369 flush_tlb_range(walk->vma, start, end);
2370
8763cb45
JG
2371 return 0;
2372}
2373
7b86ac33
CH
2374static const struct mm_walk_ops migrate_vma_walk_ops = {
2375 .pmd_entry = migrate_vma_collect_pmd,
2376 .pte_hole = migrate_vma_collect_hole,
2377};
2378
8763cb45
JG
2379/*
2380 * migrate_vma_collect() - collect pages over a range of virtual addresses
2381 * @migrate: migrate struct containing all migration information
2382 *
2383 * This will walk the CPU page table. For each virtual address backed by a
2384 * valid page, it updates the src array and takes a reference on the page, in
2385 * order to pin the page until we lock it and unmap it.
2386 */
2387static void migrate_vma_collect(struct migrate_vma *migrate)
2388{
ac46d4f3 2389 struct mmu_notifier_range range;
8763cb45 2390
998427b3
RC
2391 /*
2392 * Note that the pgmap_owner is passed to the mmu notifier callback so
2393 * that the registered device driver can skip invalidating device
2394 * private page mappings that won't be migrated.
2395 */
6b49bf6d
AP
2396 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2397 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
c1a06df6 2398 migrate->pgmap_owner);
ac46d4f3 2399 mmu_notifier_invalidate_range_start(&range);
8763cb45 2400
7b86ac33
CH
2401 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2402 &migrate_vma_walk_ops, migrate);
2403
2404 mmu_notifier_invalidate_range_end(&range);
8763cb45
JG
2405 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2406}
2407
2408/*
2409 * migrate_vma_check_page() - check if page is pinned or not
2410 * @page: struct page to check
2411 *
2412 * Pinned pages cannot be migrated. This is the same test as in
3417013e 2413 * folio_migrate_mapping(), except that here we allow migration of a
8763cb45
JG
2414 * ZONE_DEVICE page.
2415 */
2416static bool migrate_vma_check_page(struct page *page)
2417{
2418 /*
2419 * One extra ref because caller holds an extra reference, either from
2420 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2421 * a device page.
2422 */
2423 int extra = 1;
2424
2425 /*
2426 * FIXME support THP (transparent huge page), it is bit more complex to
2427 * check them than regular pages, because they can be mapped with a pmd
2428 * or with a pte (split pte mapping).
2429 */
2430 if (PageCompound(page))
2431 return false;
2432
a5430dda 2433 /* Page from ZONE_DEVICE have one extra reference */
ffa65753
AP
2434 if (is_zone_device_page(page))
2435 extra++;
a5430dda 2436
df6ad698
JG
2437 /* For file back page */
2438 if (page_mapping(page))
2439 extra += 1 + page_has_private(page);
2440
8763cb45
JG
2441 if ((page_count(page) - extra) > page_mapcount(page))
2442 return false;
2443
2444 return true;
2445}
2446
2447/*
ab09243a 2448 * migrate_vma_unmap() - replace page mapping with special migration pte entry
8763cb45
JG
2449 * @migrate: migrate struct containing all migration information
2450 *
ab09243a
AP
2451 * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
2452 * special migration pte entry and check if it has been pinned. Pinned pages are
2453 * restored because we cannot migrate them.
2454 *
2455 * This is the last step before we call the device driver callback to allocate
2456 * destination memory and copy contents of original page over to new page.
8763cb45 2457 */
ab09243a 2458static void migrate_vma_unmap(struct migrate_vma *migrate)
8763cb45
JG
2459{
2460 const unsigned long npages = migrate->npages;
f1e8db04 2461 unsigned long i, restore = 0;
8763cb45 2462 bool allow_drain = true;
8763cb45
JG
2463
2464 lru_add_drain();
2465
ab09243a 2466 for (i = 0; i < npages; i++) {
8763cb45
JG
2467 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2468
2469 if (!page)
2470 continue;
2471
a5430dda
JG
2472 /* ZONE_DEVICE pages are not on LRU */
2473 if (!is_zone_device_page(page)) {
2474 if (!PageLRU(page) && allow_drain) {
2475 /* Drain CPU's pagevec */
2476 lru_add_drain_all();
2477 allow_drain = false;
2478 }
8763cb45 2479
a5430dda 2480 if (isolate_lru_page(page)) {
ab09243a
AP
2481 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2482 migrate->cpages--;
2483 restore++;
a5430dda 2484 continue;
8c3328f1 2485 }
a5430dda
JG
2486
2487 /* Drop the reference we took in collect */
2488 put_page(page);
8763cb45
JG
2489 }
2490
ab09243a
AP
2491 if (page_mapped(page))
2492 try_to_migrate(page, 0);
8c3328f1 2493
ab09243a
AP
2494 if (page_mapped(page) || !migrate_vma_check_page(page)) {
2495 if (!is_zone_device_page(page)) {
2496 get_page(page);
2497 putback_lru_page(page);
8c3328f1 2498 }
8c3328f1 2499
ab09243a
AP
2500 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2501 migrate->cpages--;
2502 restore++;
8c3328f1 2503 continue;
8763cb45
JG
2504 }
2505 }
2506
f1e8db04 2507 for (i = 0; i < npages && restore; i++) {
8763cb45
JG
2508 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2509
2510 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2511 continue;
2512
2513 remove_migration_ptes(page, page, false);
2514
2515 migrate->src[i] = 0;
2516 unlock_page(page);
ab09243a 2517 put_page(page);
8763cb45 2518 restore--;
8763cb45
JG
2519 }
2520}
2521
a7d1f22b
CH
2522/**
2523 * migrate_vma_setup() - prepare to migrate a range of memory
eaf444de 2524 * @args: contains the vma, start, and pfns arrays for the migration
a7d1f22b
CH
2525 *
2526 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2527 * without an error.
2528 *
2529 * Prepare to migrate a range of memory virtual address range by collecting all
2530 * the pages backing each virtual address in the range, saving them inside the
2531 * src array. Then lock those pages and unmap them. Once the pages are locked
2532 * and unmapped, check whether each page is pinned or not. Pages that aren't
2533 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2534 * corresponding src array entry. Then restores any pages that are pinned, by
2535 * remapping and unlocking those pages.
2536 *
2537 * The caller should then allocate destination memory and copy source memory to
2538 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2539 * flag set). Once these are allocated and copied, the caller must update each
2540 * corresponding entry in the dst array with the pfn value of the destination
ab09243a
AP
2541 * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
2542 * lock_page().
a7d1f22b
CH
2543 *
2544 * Note that the caller does not have to migrate all the pages that are marked
2545 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2546 * device memory to system memory. If the caller cannot migrate a device page
2547 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2548 * consequences for the userspace process, so it must be avoided if at all
2549 * possible.
2550 *
2551 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2552 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
f0953a1b
IM
2553 * allowing the caller to allocate device memory for those unbacked virtual
2554 * addresses. For this the caller simply has to allocate device memory and
a7d1f22b 2555 * properly set the destination entry like for regular migration. Note that
f0953a1b
IM
2556 * this can still fail, and thus inside the device driver you must check if the
2557 * migration was successful for those entries after calling migrate_vma_pages(),
a7d1f22b
CH
2558 * just like for regular migration.
2559 *
2560 * After that, the callers must call migrate_vma_pages() to go over each entry
2561 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2562 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2563 * then migrate_vma_pages() to migrate struct page information from the source
2564 * struct page to the destination struct page. If it fails to migrate the
2565 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2566 * src array.
2567 *
2568 * At this point all successfully migrated pages have an entry in the src
2569 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2570 * array entry with MIGRATE_PFN_VALID flag set.
2571 *
2572 * Once migrate_vma_pages() returns the caller may inspect which pages were
2573 * successfully migrated, and which were not. Successfully migrated pages will
2574 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2575 *
2576 * It is safe to update device page table after migrate_vma_pages() because
c1e8d7c6 2577 * both destination and source page are still locked, and the mmap_lock is held
a7d1f22b
CH
2578 * in read mode (hence no one can unmap the range being migrated).
2579 *
2580 * Once the caller is done cleaning up things and updating its page table (if it
2581 * chose to do so, this is not an obligation) it finally calls
2582 * migrate_vma_finalize() to update the CPU page table to point to new pages
2583 * for successfully migrated pages or otherwise restore the CPU page table to
2584 * point to the original source pages.
2585 */
2586int migrate_vma_setup(struct migrate_vma *args)
2587{
2588 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2589
2590 args->start &= PAGE_MASK;
2591 args->end &= PAGE_MASK;
2592 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2593 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2594 return -EINVAL;
2595 if (nr_pages <= 0)
2596 return -EINVAL;
2597 if (args->start < args->vma->vm_start ||
2598 args->start >= args->vma->vm_end)
2599 return -EINVAL;
2600 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2601 return -EINVAL;
2602 if (!args->src || !args->dst)
2603 return -EINVAL;
2604
2605 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2606 args->cpages = 0;
2607 args->npages = 0;
2608
2609 migrate_vma_collect(args);
2610
a7d1f22b
CH
2611 if (args->cpages)
2612 migrate_vma_unmap(args);
2613
2614 /*
2615 * At this point pages are locked and unmapped, and thus they have
2616 * stable content and can safely be copied to destination memory that
2617 * is allocated by the drivers.
2618 */
2619 return 0;
2620
2621}
2622EXPORT_SYMBOL(migrate_vma_setup);
2623
34290e2c
RC
2624/*
2625 * This code closely matches the code in:
2626 * __handle_mm_fault()
2627 * handle_pte_fault()
2628 * do_anonymous_page()
2629 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2630 * private page.
2631 */
8315ada7
JG
2632static void migrate_vma_insert_page(struct migrate_vma *migrate,
2633 unsigned long addr,
2634 struct page *page,
d85c6db4 2635 unsigned long *src)
8315ada7
JG
2636{
2637 struct vm_area_struct *vma = migrate->vma;
2638 struct mm_struct *mm = vma->vm_mm;
8315ada7
JG
2639 bool flush = false;
2640 spinlock_t *ptl;
2641 pte_t entry;
2642 pgd_t *pgdp;
2643 p4d_t *p4dp;
2644 pud_t *pudp;
2645 pmd_t *pmdp;
2646 pte_t *ptep;
2647
2648 /* Only allow populating anonymous memory */
2649 if (!vma_is_anonymous(vma))
2650 goto abort;
2651
2652 pgdp = pgd_offset(mm, addr);
2653 p4dp = p4d_alloc(mm, pgdp, addr);
2654 if (!p4dp)
2655 goto abort;
2656 pudp = pud_alloc(mm, p4dp, addr);
2657 if (!pudp)
2658 goto abort;
2659 pmdp = pmd_alloc(mm, pudp, addr);
2660 if (!pmdp)
2661 goto abort;
2662
2663 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2664 goto abort;
2665
2666 /*
2667 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2668 * pte_offset_map() on pmds where a huge pmd might be created
2669 * from a different thread.
2670 *
3e4e28c5 2671 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
8315ada7
JG
2672 * parallel threads are excluded by other means.
2673 *
3e4e28c5 2674 * Here we only have mmap_read_lock(mm).
8315ada7 2675 */
4cf58924 2676 if (pte_alloc(mm, pmdp))
8315ada7
JG
2677 goto abort;
2678
2679 /* See the comment in pte_alloc_one_map() */
2680 if (unlikely(pmd_trans_unstable(pmdp)))
2681 goto abort;
2682
2683 if (unlikely(anon_vma_prepare(vma)))
2684 goto abort;
8f425e4e 2685 if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL))
8315ada7
JG
2686 goto abort;
2687
2688 /*
2689 * The memory barrier inside __SetPageUptodate makes sure that
2690 * preceding stores to the page contents become visible before
2691 * the set_pte_at() write.
2692 */
2693 __SetPageUptodate(page);
2694
df6ad698
JG
2695 if (is_zone_device_page(page)) {
2696 if (is_device_private_page(page)) {
2697 swp_entry_t swp_entry;
2698
4dd845b5
AP
2699 if (vma->vm_flags & VM_WRITE)
2700 swp_entry = make_writable_device_private_entry(
2701 page_to_pfn(page));
2702 else
2703 swp_entry = make_readable_device_private_entry(
2704 page_to_pfn(page));
df6ad698 2705 entry = swp_entry_to_pte(swp_entry);
34f5e9b9
ML
2706 } else {
2707 /*
2708 * For now we only support migrating to un-addressable
2709 * device memory.
2710 */
2711 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2712 goto abort;
df6ad698 2713 }
8315ada7
JG
2714 } else {
2715 entry = mk_pte(page, vma->vm_page_prot);
2716 if (vma->vm_flags & VM_WRITE)
2717 entry = pte_mkwrite(pte_mkdirty(entry));
2718 }
2719
2720 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2721
34290e2c
RC
2722 if (check_stable_address_space(mm))
2723 goto unlock_abort;
2724
8315ada7
JG
2725 if (pte_present(*ptep)) {
2726 unsigned long pfn = pte_pfn(*ptep);
2727
c23a0c99
RC
2728 if (!is_zero_pfn(pfn))
2729 goto unlock_abort;
8315ada7 2730 flush = true;
c23a0c99
RC
2731 } else if (!pte_none(*ptep))
2732 goto unlock_abort;
8315ada7
JG
2733
2734 /*
c23a0c99 2735 * Check for userfaultfd but do not deliver the fault. Instead,
8315ada7
JG
2736 * just back off.
2737 */
c23a0c99
RC
2738 if (userfaultfd_missing(vma))
2739 goto unlock_abort;
8315ada7
JG
2740
2741 inc_mm_counter(mm, MM_ANONPAGES);
be5d0a74 2742 page_add_new_anon_rmap(page, vma, addr, false);
8315ada7 2743 if (!is_zone_device_page(page))
b518154e 2744 lru_cache_add_inactive_or_unevictable(page, vma);
8315ada7
JG
2745 get_page(page);
2746
2747 if (flush) {
2748 flush_cache_page(vma, addr, pte_pfn(*ptep));
2749 ptep_clear_flush_notify(vma, addr, ptep);
2750 set_pte_at_notify(mm, addr, ptep, entry);
2751 update_mmu_cache(vma, addr, ptep);
2752 } else {
2753 /* No need to invalidate - it was non-present before */
2754 set_pte_at(mm, addr, ptep, entry);
2755 update_mmu_cache(vma, addr, ptep);
2756 }
2757
2758 pte_unmap_unlock(ptep, ptl);
2759 *src = MIGRATE_PFN_MIGRATE;
2760 return;
2761
c23a0c99
RC
2762unlock_abort:
2763 pte_unmap_unlock(ptep, ptl);
8315ada7
JG
2764abort:
2765 *src &= ~MIGRATE_PFN_MIGRATE;
2766}
2767
a7d1f22b 2768/**
8763cb45
JG
2769 * migrate_vma_pages() - migrate meta-data from src page to dst page
2770 * @migrate: migrate struct containing all migration information
2771 *
2772 * This migrates struct page meta-data from source struct page to destination
2773 * struct page. This effectively finishes the migration from source page to the
2774 * destination page.
2775 */
a7d1f22b 2776void migrate_vma_pages(struct migrate_vma *migrate)
8763cb45
JG
2777{
2778 const unsigned long npages = migrate->npages;
2779 const unsigned long start = migrate->start;
ac46d4f3
JG
2780 struct mmu_notifier_range range;
2781 unsigned long addr, i;
8315ada7 2782 bool notified = false;
8763cb45
JG
2783
2784 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
2785 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2786 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2787 struct address_space *mapping;
2788 int r;
2789
8315ada7
JG
2790 if (!newpage) {
2791 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
8763cb45 2792 continue;
8315ada7
JG
2793 }
2794
2795 if (!page) {
c23a0c99 2796 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
8315ada7 2797 continue;
8315ada7 2798 if (!notified) {
8315ada7 2799 notified = true;
ac46d4f3 2800
6b49bf6d
AP
2801 mmu_notifier_range_init_owner(&range,
2802 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
2803 migrate->vma->vm_mm, addr, migrate->end,
5e5dda81 2804 migrate->pgmap_owner);
ac46d4f3 2805 mmu_notifier_invalidate_range_start(&range);
8315ada7
JG
2806 }
2807 migrate_vma_insert_page(migrate, addr, newpage,
d85c6db4 2808 &migrate->src[i]);
8763cb45 2809 continue;
8315ada7 2810 }
8763cb45
JG
2811
2812 mapping = page_mapping(page);
2813
a5430dda
JG
2814 if (is_zone_device_page(newpage)) {
2815 if (is_device_private_page(newpage)) {
2816 /*
2817 * For now only support private anonymous when
2818 * migrating to un-addressable device memory.
2819 */
2820 if (mapping) {
2821 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2822 continue;
2823 }
25b2995a 2824 } else {
a5430dda
JG
2825 /*
2826 * Other types of ZONE_DEVICE page are not
2827 * supported.
2828 */
2829 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2830 continue;
2831 }
2832 }
2833
8763cb45
JG
2834 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
2835 if (r != MIGRATEPAGE_SUCCESS)
2836 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2837 }
8315ada7 2838
4645b9fe
JG
2839 /*
2840 * No need to double call mmu_notifier->invalidate_range() callback as
2841 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
2842 * did already call it.
2843 */
8315ada7 2844 if (notified)
ac46d4f3 2845 mmu_notifier_invalidate_range_only_end(&range);
8763cb45 2846}
a7d1f22b 2847EXPORT_SYMBOL(migrate_vma_pages);
8763cb45 2848
a7d1f22b 2849/**
8763cb45
JG
2850 * migrate_vma_finalize() - restore CPU page table entry
2851 * @migrate: migrate struct containing all migration information
2852 *
2853 * This replaces the special migration pte entry with either a mapping to the
2854 * new page if migration was successful for that page, or to the original page
2855 * otherwise.
2856 *
2857 * This also unlocks the pages and puts them back on the lru, or drops the extra
2858 * refcount, for device pages.
2859 */
a7d1f22b 2860void migrate_vma_finalize(struct migrate_vma *migrate)
8763cb45
JG
2861{
2862 const unsigned long npages = migrate->npages;
2863 unsigned long i;
2864
2865 for (i = 0; i < npages; i++) {
2866 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2867 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2868
8315ada7
JG
2869 if (!page) {
2870 if (newpage) {
2871 unlock_page(newpage);
2872 put_page(newpage);
2873 }
8763cb45 2874 continue;
8315ada7
JG
2875 }
2876
8763cb45
JG
2877 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
2878 if (newpage) {
2879 unlock_page(newpage);
2880 put_page(newpage);
2881 }
2882 newpage = page;
2883 }
2884
2885 remove_migration_ptes(page, newpage, false);
2886 unlock_page(page);
8763cb45 2887
a5430dda
JG
2888 if (is_zone_device_page(page))
2889 put_page(page);
2890 else
2891 putback_lru_page(page);
8763cb45
JG
2892
2893 if (newpage != page) {
2894 unlock_page(newpage);
a5430dda
JG
2895 if (is_zone_device_page(newpage))
2896 put_page(newpage);
2897 else
2898 putback_lru_page(newpage);
8763cb45
JG
2899 }
2900 }
2901}
a7d1f22b 2902EXPORT_SYMBOL(migrate_vma_finalize);
9b2ed9cb 2903#endif /* CONFIG_DEVICE_PRIVATE */
79c28a41 2904
dcee9bf5
HY
2905/*
2906 * node_demotion[] example:
2907 *
2908 * Consider a system with two sockets. Each socket has
2909 * three classes of memory attached: fast, medium and slow.
2910 * Each memory class is placed in its own NUMA node. The
2911 * CPUs are placed in the node with the "fast" memory. The
2912 * 6 NUMA nodes (0-5) might be split among the sockets like
2913 * this:
2914 *
2915 * Socket A: 0, 1, 2
2916 * Socket B: 3, 4, 5
2917 *
2918 * When Node 0 fills up, its memory should be migrated to
2919 * Node 1. When Node 1 fills up, it should be migrated to
2920 * Node 2. The migration path start on the nodes with the
2921 * processors (since allocations default to this node) and
2922 * fast memory, progress through medium and end with the
2923 * slow memory:
2924 *
2925 * 0 -> 1 -> 2 -> stop
2926 * 3 -> 4 -> 5 -> stop
2927 *
2928 * This is represented in the node_demotion[] like this:
2929 *
2930 * { nr=1, nodes[0]=1 }, // Node 0 migrates to 1
2931 * { nr=1, nodes[0]=2 }, // Node 1 migrates to 2
2932 * { nr=0, nodes[0]=-1 }, // Node 2 does not migrate
2933 * { nr=1, nodes[0]=4 }, // Node 3 migrates to 4
2934 * { nr=1, nodes[0]=5 }, // Node 4 migrates to 5
2935 * { nr=0, nodes[0]=-1 }, // Node 5 does not migrate
2936 *
2937 * Moreover some systems may have multiple slow memory nodes.
2938 * Suppose a system has one socket with 3 memory nodes, node 0
2939 * is fast memory type, and node 1/2 both are slow memory
2940 * type, and the distance between fast memory node and slow
2941 * memory node is same. So the migration path should be:
2942 *
2943 * 0 -> 1/2 -> stop
2944 *
2945 * This is represented in the node_demotion[] like this:
2946 * { nr=2, {nodes[0]=1, nodes[1]=2} }, // Node 0 migrates to node 1 and node 2
2947 * { nr=0, nodes[0]=-1, }, // Node 1 dose not migrate
2948 * { nr=0, nodes[0]=-1, }, // Node 2 does not migrate
2949 */
2950
2951/*
2952 * Writes to this array occur without locking. Cycles are
2953 * not allowed: Node X demotes to Y which demotes to X...
2954 *
2955 * If multiple reads are performed, a single rcu_read_lock()
2956 * must be held over all reads to ensure that no cycles are
2957 * observed.
2958 */
2959#define DEFAULT_DEMOTION_TARGET_NODES 15
2960
2961#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
2962#define DEMOTION_TARGET_NODES (MAX_NUMNODES - 1)
2963#else
2964#define DEMOTION_TARGET_NODES DEFAULT_DEMOTION_TARGET_NODES
2965#endif
2966
2967struct demotion_nodes {
2968 unsigned short nr;
2969 short nodes[DEMOTION_TARGET_NODES];
2970};
2971
2972static struct demotion_nodes *node_demotion __read_mostly;
2973
2974/**
2975 * next_demotion_node() - Get the next node in the demotion path
2976 * @node: The starting node to lookup the next node
2977 *
2978 * Return: node id for next memory node in the demotion path hierarchy
2979 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
2980 * @node online or guarantee that it *continues* to be the next demotion
2981 * target.
2982 */
2983int next_demotion_node(int node)
2984{
2985 struct demotion_nodes *nd;
2986 unsigned short target_nr, index;
2987 int target;
2988
2989 if (!node_demotion)
2990 return NUMA_NO_NODE;
2991
2992 nd = &node_demotion[node];
2993
2994 /*
2995 * node_demotion[] is updated without excluding this
2996 * function from running. RCU doesn't provide any
2997 * compiler barriers, so the READ_ONCE() is required
2998 * to avoid compiler reordering or read merging.
2999 *
3000 * Make sure to use RCU over entire code blocks if
3001 * node_demotion[] reads need to be consistent.
3002 */
3003 rcu_read_lock();
3004 target_nr = READ_ONCE(nd->nr);
3005
3006 switch (target_nr) {
3007 case 0:
3008 target = NUMA_NO_NODE;
3009 goto out;
3010 case 1:
3011 index = 0;
3012 break;
3013 default:
3014 /*
3015 * If there are multiple target nodes, just select one
3016 * target node randomly.
3017 *
3018 * In addition, we can also use round-robin to select
3019 * target node, but we should introduce another variable
3020 * for node_demotion[] to record last selected target node,
3021 * that may cause cache ping-pong due to the changing of
3022 * last target node. Or introducing per-cpu data to avoid
3023 * caching issue, which seems more complicated. So selecting
3024 * target node randomly seems better until now.
3025 */
3026 index = get_random_int() % target_nr;
3027 break;
3028 }
3029
3030 target = READ_ONCE(nd->nodes[index]);
3031
3032out:
3033 rcu_read_unlock();
3034 return target;
3035}
3036
76af6a05 3037#if defined(CONFIG_HOTPLUG_CPU)
79c28a41
DH
3038/* Disable reclaim-based migration. */
3039static void __disable_all_migrate_targets(void)
3040{
ac16ec83 3041 int node, i;
79c28a41 3042
ac16ec83
BW
3043 if (!node_demotion)
3044 return;
79c28a41 3045
ac16ec83
BW
3046 for_each_online_node(node) {
3047 node_demotion[node].nr = 0;
3048 for (i = 0; i < DEMOTION_TARGET_NODES; i++)
3049 node_demotion[node].nodes[i] = NUMA_NO_NODE;
3050 }
79c28a41
DH
3051}
3052
3053static void disable_all_migrate_targets(void)
3054{
3055 __disable_all_migrate_targets();
3056
3057 /*
3058 * Ensure that the "disable" is visible across the system.
3059 * Readers will see either a combination of before+disable
3060 * state or disable+after. They will never see before and
3061 * after state together.
3062 *
3063 * The before+after state together might have cycles and
3064 * could cause readers to do things like loop until this
3065 * function finishes. This ensures they can only see a
3066 * single "bad" read and would, for instance, only loop
3067 * once.
3068 */
3069 synchronize_rcu();
3070}
3071
3072/*
3073 * Find an automatic demotion target for 'node'.
3074 * Failing here is OK. It might just indicate
3075 * being at the end of a chain.
3076 */
ac16ec83
BW
3077static int establish_migrate_target(int node, nodemask_t *used,
3078 int best_distance)
79c28a41 3079{
ac16ec83
BW
3080 int migration_target, index, val;
3081 struct demotion_nodes *nd;
79c28a41 3082
ac16ec83 3083 if (!node_demotion)
79c28a41
DH
3084 return NUMA_NO_NODE;
3085
ac16ec83
BW
3086 nd = &node_demotion[node];
3087
79c28a41
DH
3088 migration_target = find_next_best_node(node, used);
3089 if (migration_target == NUMA_NO_NODE)
3090 return NUMA_NO_NODE;
3091
ac16ec83
BW
3092 /*
3093 * If the node has been set a migration target node before,
3094 * which means it's the best distance between them. Still
3095 * check if this node can be demoted to other target nodes
3096 * if they have a same best distance.
3097 */
3098 if (best_distance != -1) {
3099 val = node_distance(node, migration_target);
3100 if (val > best_distance)
fc89213a 3101 goto out_clear;
ac16ec83
BW
3102 }
3103
3104 index = nd->nr;
3105 if (WARN_ONCE(index >= DEMOTION_TARGET_NODES,
3106 "Exceeds maximum demotion target nodes\n"))
fc89213a 3107 goto out_clear;
ac16ec83
BW
3108
3109 nd->nodes[index] = migration_target;
3110 nd->nr++;
79c28a41
DH
3111
3112 return migration_target;
fc89213a
HY
3113out_clear:
3114 node_clear(migration_target, *used);
3115 return NUMA_NO_NODE;
79c28a41
DH
3116}
3117
3118/*
3119 * When memory fills up on a node, memory contents can be
3120 * automatically migrated to another node instead of
3121 * discarded at reclaim.
3122 *
3123 * Establish a "migration path" which will start at nodes
3124 * with CPUs and will follow the priorities used to build the
3125 * page allocator zonelists.
3126 *
3127 * The difference here is that cycles must be avoided. If
3128 * node0 migrates to node1, then neither node1, nor anything
ac16ec83
BW
3129 * node1 migrates to can migrate to node0. Also one node can
3130 * be migrated to multiple nodes if the target nodes all have
3131 * a same best-distance against the source node.
79c28a41
DH
3132 *
3133 * This function can run simultaneously with readers of
3134 * node_demotion[]. However, it can not run simultaneously
3135 * with itself. Exclusion is provided by memory hotplug events
3136 * being single-threaded.
3137 */
3138static void __set_migration_target_nodes(void)
3139{
3140 nodemask_t next_pass = NODE_MASK_NONE;
3141 nodemask_t this_pass = NODE_MASK_NONE;
3142 nodemask_t used_targets = NODE_MASK_NONE;
ac16ec83 3143 int node, best_distance;
79c28a41
DH
3144
3145 /*
3146 * Avoid any oddities like cycles that could occur
3147 * from changes in the topology. This will leave
3148 * a momentary gap when migration is disabled.
3149 */
3150 disable_all_migrate_targets();
3151
3152 /*
3153 * Allocations go close to CPUs, first. Assume that
3154 * the migration path starts at the nodes with CPUs.
3155 */
3156 next_pass = node_states[N_CPU];
3157again:
3158 this_pass = next_pass;
3159 next_pass = NODE_MASK_NONE;
3160 /*
3161 * To avoid cycles in the migration "graph", ensure
3162 * that migration sources are not future targets by
3163 * setting them in 'used_targets'. Do this only
3164 * once per pass so that multiple source nodes can
3165 * share a target node.
3166 *
3167 * 'used_targets' will become unavailable in future
3168 * passes. This limits some opportunities for
3169 * multiple source nodes to share a destination.
3170 */
3171 nodes_or(used_targets, used_targets, this_pass);
79c28a41 3172
ac16ec83
BW
3173 for_each_node_mask(node, this_pass) {
3174 best_distance = -1;
79c28a41
DH
3175
3176 /*
ac16ec83
BW
3177 * Try to set up the migration path for the node, and the target
3178 * migration nodes can be multiple, so doing a loop to find all
3179 * the target nodes if they all have a best node distance.
79c28a41 3180 */
ac16ec83
BW
3181 do {
3182 int target_node =
3183 establish_migrate_target(node, &used_targets,
3184 best_distance);
3185
3186 if (target_node == NUMA_NO_NODE)
3187 break;
3188
3189 if (best_distance == -1)
3190 best_distance = node_distance(node, target_node);
3191
3192 /*
3193 * Visit targets from this pass in the next pass.
3194 * Eventually, every node will have been part of
3195 * a pass, and will become set in 'used_targets'.
3196 */
3197 node_set(target_node, next_pass);
3198 } while (1);
79c28a41
DH
3199 }
3200 /*
3201 * 'next_pass' contains nodes which became migration
3202 * targets in this pass. Make additional passes until
3203 * no more migrations targets are available.
3204 */
3205 if (!nodes_empty(next_pass))
3206 goto again;
3207}
3208
3209/*
3210 * For callers that do not hold get_online_mems() already.
3211 */
79c28a41
DH
3212static void set_migration_target_nodes(void)
3213{
3214 get_online_mems();
3215 __set_migration_target_nodes();
3216 put_online_mems();
3217}
884a6e5d 3218
884a6e5d
DH
3219/*
3220 * This leaves migrate-on-reclaim transiently disabled between
3221 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3222 * whether reclaim-based migration is enabled or not, which
3223 * ensures that the user can turn reclaim-based migration at
3224 * any time without needing to recalculate migration targets.
3225 *
3226 * These callbacks already hold get_online_mems(). That is why
3227 * __set_migration_target_nodes() can be used as opposed to
3228 * set_migration_target_nodes().
3229 */
3230static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
295be91f 3231 unsigned long action, void *_arg)
884a6e5d 3232{
295be91f
DH
3233 struct memory_notify *arg = _arg;
3234
3235 /*
3236 * Only update the node migration order when a node is
3237 * changing status, like online->offline. This avoids
3238 * the overhead of synchronize_rcu() in most cases.
3239 */
3240 if (arg->status_change_nid < 0)
3241 return notifier_from_errno(0);
3242
884a6e5d
DH
3243 switch (action) {
3244 case MEM_GOING_OFFLINE:
3245 /*
3246 * Make sure there are not transient states where
3247 * an offline node is a migration target. This
3248 * will leave migration disabled until the offline
3249 * completes and the MEM_OFFLINE case below runs.
3250 */
3251 disable_all_migrate_targets();
3252 break;
3253 case MEM_OFFLINE:
3254 case MEM_ONLINE:
3255 /*
3256 * Recalculate the target nodes once the node
3257 * reaches its final state (online or offline).
3258 */
3259 __set_migration_target_nodes();
3260 break;
3261 case MEM_CANCEL_OFFLINE:
3262 /*
3263 * MEM_GOING_OFFLINE disabled all the migration
3264 * targets. Reenable them.
3265 */
3266 __set_migration_target_nodes();
3267 break;
3268 case MEM_GOING_ONLINE:
3269 case MEM_CANCEL_ONLINE:
3270 break;
3271 }
3272
3273 return notifier_from_errno(0);
3274}
3275
76af6a05
DH
3276/*
3277 * React to hotplug events that might affect the migration targets
3278 * like events that online or offline NUMA nodes.
3279 *
3280 * The ordering is also currently dependent on which nodes have
3281 * CPUs. That means we need CPU on/offline notification too.
3282 */
3283static int migration_online_cpu(unsigned int cpu)
3284{
3285 set_migration_target_nodes();
3286 return 0;
3287}
3288
3289static int migration_offline_cpu(unsigned int cpu)
3290{
3291 set_migration_target_nodes();
3292 return 0;
3293}
3294
884a6e5d
DH
3295static int __init migrate_on_reclaim_init(void)
3296{
3297 int ret;
3298
ac16ec83
BW
3299 node_demotion = kmalloc_array(nr_node_ids,
3300 sizeof(struct demotion_nodes),
3301 GFP_KERNEL);
3302 WARN_ON(!node_demotion);
3303
a6a0251c
HY
3304 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3305 NULL, migration_offline_cpu);
884a6e5d
DH
3306 /*
3307 * In the unlikely case that this fails, the automatic
3308 * migration targets may become suboptimal for nodes
3309 * where N_CPU changes. With such a small impact in a
3310 * rare case, do not bother trying to do anything special.
3311 */
3312 WARN_ON(ret < 0);
a6a0251c
HY
3313 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3314 migration_online_cpu, NULL);
3315 WARN_ON(ret < 0);
884a6e5d
DH
3316
3317 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3318 return 0;
3319}
3320late_initcall(migrate_on_reclaim_init);
76af6a05 3321#endif /* CONFIG_HOTPLUG_CPU */
20f9ba4f
YS
3322
3323bool numa_demotion_enabled = false;
3324
3325#ifdef CONFIG_SYSFS
3326static ssize_t numa_demotion_enabled_show(struct kobject *kobj,
3327 struct kobj_attribute *attr, char *buf)
3328{
3329 return sysfs_emit(buf, "%s\n",
3330 numa_demotion_enabled ? "true" : "false");
3331}
3332
3333static ssize_t numa_demotion_enabled_store(struct kobject *kobj,
3334 struct kobj_attribute *attr,
3335 const char *buf, size_t count)
3336{
3337 if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
3338 numa_demotion_enabled = true;
3339 else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
3340 numa_demotion_enabled = false;
3341 else
3342 return -EINVAL;
3343
3344 return count;
3345}
3346
3347static struct kobj_attribute numa_demotion_enabled_attr =
3348 __ATTR(demotion_enabled, 0644, numa_demotion_enabled_show,
3349 numa_demotion_enabled_store);
3350
3351static struct attribute *numa_attrs[] = {
3352 &numa_demotion_enabled_attr.attr,
3353 NULL,
3354};
3355
3356static const struct attribute_group numa_attr_group = {
3357 .attrs = numa_attrs,
3358};
3359
3360static int __init numa_init_sysfs(void)
3361{
3362 int err;
3363 struct kobject *numa_kobj;
3364
3365 numa_kobj = kobject_create_and_add("numa", mm_kobj);
3366 if (!numa_kobj) {
3367 pr_err("failed to create numa kobject\n");
3368 return -ENOMEM;
3369 }
3370 err = sysfs_create_group(numa_kobj, &numa_attr_group);
3371 if (err) {
3372 pr_err("failed to register numa group\n");
3373 goto delete_obj;
3374 }
3375 return 0;
3376
3377delete_obj:
3378 kobject_put(numa_kobj);
3379 return err;
3380}
3381subsys_initcall(numa_init_sysfs);
3382#endif