]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/migrate.c
mm: mempolicy: mbind and migrate_pages support thp migration
[thirdparty/kernel/stable.git] / mm / migrate.c
CommitLineData
b20a3503 1/*
14e0f9bc 2 * Memory Migration functionality - linux/mm/migrate.c
b20a3503
CL
3 *
4 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
5 *
6 * Page migration was first developed in the context of the memory hotplug
7 * project. The main authors of the migration code are:
8 *
9 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10 * Hirokazu Takahashi <taka@valinux.co.jp>
11 * Dave Hansen <haveblue@us.ibm.com>
cde53535 12 * Christoph Lameter
b20a3503
CL
13 */
14
15#include <linux/migrate.h>
b95f1b31 16#include <linux/export.h>
b20a3503 17#include <linux/swap.h>
0697212a 18#include <linux/swapops.h>
b20a3503 19#include <linux/pagemap.h>
e23ca00b 20#include <linux/buffer_head.h>
b20a3503 21#include <linux/mm_inline.h>
b488893a 22#include <linux/nsproxy.h>
b20a3503 23#include <linux/pagevec.h>
e9995ef9 24#include <linux/ksm.h>
b20a3503
CL
25#include <linux/rmap.h>
26#include <linux/topology.h>
27#include <linux/cpu.h>
28#include <linux/cpuset.h>
04e62a29 29#include <linux/writeback.h>
742755a1
CL
30#include <linux/mempolicy.h>
31#include <linux/vmalloc.h>
86c3a764 32#include <linux/security.h>
42cb14b1 33#include <linux/backing-dev.h>
bda807d4 34#include <linux/compaction.h>
4f5ca265 35#include <linux/syscalls.h>
290408d4 36#include <linux/hugetlb.h>
8e6ac7fa 37#include <linux/hugetlb_cgroup.h>
5a0e3ad6 38#include <linux/gfp.h>
bf6bddf1 39#include <linux/balloon_compaction.h>
f714f4f2 40#include <linux/mmu_notifier.h>
33c3fc71 41#include <linux/page_idle.h>
d435edca 42#include <linux/page_owner.h>
6e84f315 43#include <linux/sched/mm.h>
197e7e52 44#include <linux/ptrace.h>
b20a3503 45
0d1836c3
MN
46#include <asm/tlbflush.h>
47
7b2a2d4a
MG
48#define CREATE_TRACE_POINTS
49#include <trace/events/migrate.h>
50
b20a3503
CL
51#include "internal.h"
52
b20a3503 53/*
742755a1 54 * migrate_prep() needs to be called before we start compiling a list of pages
748446bb
MG
55 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
56 * undesirable, use migrate_prep_local()
b20a3503
CL
57 */
58int migrate_prep(void)
59{
b20a3503
CL
60 /*
61 * Clear the LRU lists so pages can be isolated.
62 * Note that pages may be moved off the LRU after we have
63 * drained them. Those pages will fail to migrate like other
64 * pages that may be busy.
65 */
66 lru_add_drain_all();
67
68 return 0;
69}
70
748446bb
MG
71/* Do the necessary work of migrate_prep but not if it involves other CPUs */
72int migrate_prep_local(void)
73{
74 lru_add_drain();
75
76 return 0;
77}
78
9e5bcd61 79int isolate_movable_page(struct page *page, isolate_mode_t mode)
bda807d4
MK
80{
81 struct address_space *mapping;
82
83 /*
84 * Avoid burning cycles with pages that are yet under __free_pages(),
85 * or just got freed under us.
86 *
87 * In case we 'win' a race for a movable page being freed under us and
88 * raise its refcount preventing __free_pages() from doing its job
89 * the put_page() at the end of this block will take care of
90 * release this page, thus avoiding a nasty leakage.
91 */
92 if (unlikely(!get_page_unless_zero(page)))
93 goto out;
94
95 /*
96 * Check PageMovable before holding a PG_lock because page's owner
97 * assumes anybody doesn't touch PG_lock of newly allocated page
98 * so unconditionally grapping the lock ruins page's owner side.
99 */
100 if (unlikely(!__PageMovable(page)))
101 goto out_putpage;
102 /*
103 * As movable pages are not isolated from LRU lists, concurrent
104 * compaction threads can race against page migration functions
105 * as well as race against the releasing a page.
106 *
107 * In order to avoid having an already isolated movable page
108 * being (wrongly) re-isolated while it is under migration,
109 * or to avoid attempting to isolate pages being released,
110 * lets be sure we have the page lock
111 * before proceeding with the movable page isolation steps.
112 */
113 if (unlikely(!trylock_page(page)))
114 goto out_putpage;
115
116 if (!PageMovable(page) || PageIsolated(page))
117 goto out_no_isolated;
118
119 mapping = page_mapping(page);
120 VM_BUG_ON_PAGE(!mapping, page);
121
122 if (!mapping->a_ops->isolate_page(page, mode))
123 goto out_no_isolated;
124
125 /* Driver shouldn't use PG_isolated bit of page->flags */
126 WARN_ON_ONCE(PageIsolated(page));
127 __SetPageIsolated(page);
128 unlock_page(page);
129
9e5bcd61 130 return 0;
bda807d4
MK
131
132out_no_isolated:
133 unlock_page(page);
134out_putpage:
135 put_page(page);
136out:
9e5bcd61 137 return -EBUSY;
bda807d4
MK
138}
139
140/* It should be called on page which is PG_movable */
141void putback_movable_page(struct page *page)
142{
143 struct address_space *mapping;
144
145 VM_BUG_ON_PAGE(!PageLocked(page), page);
146 VM_BUG_ON_PAGE(!PageMovable(page), page);
147 VM_BUG_ON_PAGE(!PageIsolated(page), page);
148
149 mapping = page_mapping(page);
150 mapping->a_ops->putback_page(page);
151 __ClearPageIsolated(page);
152}
153
5733c7d1
RA
154/*
155 * Put previously isolated pages back onto the appropriate lists
156 * from where they were once taken off for compaction/migration.
157 *
59c82b70
JK
158 * This function shall be used whenever the isolated pageset has been
159 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
160 * and isolate_huge_page().
5733c7d1
RA
161 */
162void putback_movable_pages(struct list_head *l)
163{
164 struct page *page;
165 struct page *page2;
166
b20a3503 167 list_for_each_entry_safe(page, page2, l, lru) {
31caf665
NH
168 if (unlikely(PageHuge(page))) {
169 putback_active_hugepage(page);
170 continue;
171 }
e24f0b8f 172 list_del(&page->lru);
bda807d4
MK
173 /*
174 * We isolated non-lru movable page so here we can use
175 * __PageMovable because LRU page's mapping cannot have
176 * PAGE_MAPPING_MOVABLE.
177 */
b1123ea6 178 if (unlikely(__PageMovable(page))) {
bda807d4
MK
179 VM_BUG_ON_PAGE(!PageIsolated(page), page);
180 lock_page(page);
181 if (PageMovable(page))
182 putback_movable_page(page);
183 else
184 __ClearPageIsolated(page);
185 unlock_page(page);
186 put_page(page);
187 } else {
6afcf8ef
ML
188 dec_node_page_state(page, NR_ISOLATED_ANON +
189 page_is_file_cache(page));
fc280fe8 190 putback_lru_page(page);
bda807d4 191 }
b20a3503 192 }
b20a3503
CL
193}
194
0697212a
CL
195/*
196 * Restore a potential migration pte to a working pte entry
197 */
e4b82222 198static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
e9995ef9 199 unsigned long addr, void *old)
0697212a 200{
3fe87967
KS
201 struct page_vma_mapped_walk pvmw = {
202 .page = old,
203 .vma = vma,
204 .address = addr,
205 .flags = PVMW_SYNC | PVMW_MIGRATION,
206 };
207 struct page *new;
208 pte_t pte;
0697212a 209 swp_entry_t entry;
0697212a 210
3fe87967
KS
211 VM_BUG_ON_PAGE(PageTail(page), page);
212 while (page_vma_mapped_walk(&pvmw)) {
4b0ece6f
NH
213 if (PageKsm(page))
214 new = page;
215 else
216 new = page - pvmw.page->index +
217 linear_page_index(vma, pvmw.address);
0697212a 218
616b8371
ZY
219#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
220 /* PMD-mapped THP migration entry */
221 if (!pvmw.pte) {
222 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
223 remove_migration_pmd(&pvmw, new);
224 continue;
225 }
226#endif
227
3fe87967
KS
228 get_page(new);
229 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
230 if (pte_swp_soft_dirty(*pvmw.pte))
231 pte = pte_mksoft_dirty(pte);
0697212a 232
3fe87967
KS
233 /*
234 * Recheck VMA as permissions can change since migration started
235 */
236 entry = pte_to_swp_entry(*pvmw.pte);
237 if (is_write_migration_entry(entry))
238 pte = maybe_mkwrite(pte, vma);
d3cb8bf6 239
383321ab 240 flush_dcache_page(new);
3ef8fd7f 241#ifdef CONFIG_HUGETLB_PAGE
3fe87967
KS
242 if (PageHuge(new)) {
243 pte = pte_mkhuge(pte);
244 pte = arch_make_huge_pte(pte, vma, new, 0);
383321ab 245 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
3fe87967
KS
246 if (PageAnon(new))
247 hugepage_add_anon_rmap(new, vma, pvmw.address);
248 else
249 page_dup_rmap(new, true);
383321ab
AK
250 } else
251#endif
252 {
253 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
04e62a29 254
383321ab
AK
255 if (PageAnon(new))
256 page_add_anon_rmap(new, vma, pvmw.address, false);
257 else
258 page_add_file_rmap(new, false);
259 }
3fe87967
KS
260 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
261 mlock_vma_page(new);
262
263 /* No need to invalidate - it was non-present before */
264 update_mmu_cache(vma, pvmw.address, pvmw.pte);
265 }
51afb12b 266
e4b82222 267 return true;
0697212a
CL
268}
269
04e62a29
CL
270/*
271 * Get rid of all migration entries and replace them by
272 * references to the indicated page.
273 */
e388466d 274void remove_migration_ptes(struct page *old, struct page *new, bool locked)
04e62a29 275{
051ac83a
JK
276 struct rmap_walk_control rwc = {
277 .rmap_one = remove_migration_pte,
278 .arg = old,
279 };
280
e388466d
KS
281 if (locked)
282 rmap_walk_locked(new, &rwc);
283 else
284 rmap_walk(new, &rwc);
04e62a29
CL
285}
286
0697212a
CL
287/*
288 * Something used the pte of a page under migration. We need to
289 * get to the page and wait until migration is finished.
290 * When we return from this function the fault will be retried.
0697212a 291 */
e66f17ff 292void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
30dad309 293 spinlock_t *ptl)
0697212a 294{
30dad309 295 pte_t pte;
0697212a
CL
296 swp_entry_t entry;
297 struct page *page;
298
30dad309 299 spin_lock(ptl);
0697212a
CL
300 pte = *ptep;
301 if (!is_swap_pte(pte))
302 goto out;
303
304 entry = pte_to_swp_entry(pte);
305 if (!is_migration_entry(entry))
306 goto out;
307
308 page = migration_entry_to_page(entry);
309
e286781d
NP
310 /*
311 * Once radix-tree replacement of page migration started, page_count
312 * *must* be zero. And, we don't want to call wait_on_page_locked()
313 * against a page without get_page().
314 * So, we use get_page_unless_zero(), here. Even failed, page fault
315 * will occur again.
316 */
317 if (!get_page_unless_zero(page))
318 goto out;
0697212a
CL
319 pte_unmap_unlock(ptep, ptl);
320 wait_on_page_locked(page);
321 put_page(page);
322 return;
323out:
324 pte_unmap_unlock(ptep, ptl);
325}
326
30dad309
NH
327void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
328 unsigned long address)
329{
330 spinlock_t *ptl = pte_lockptr(mm, pmd);
331 pte_t *ptep = pte_offset_map(pmd, address);
332 __migration_entry_wait(mm, ptep, ptl);
333}
334
cb900f41
KS
335void migration_entry_wait_huge(struct vm_area_struct *vma,
336 struct mm_struct *mm, pte_t *pte)
30dad309 337{
cb900f41 338 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
30dad309
NH
339 __migration_entry_wait(mm, pte, ptl);
340}
341
616b8371
ZY
342#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
343void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
344{
345 spinlock_t *ptl;
346 struct page *page;
347
348 ptl = pmd_lock(mm, pmd);
349 if (!is_pmd_migration_entry(*pmd))
350 goto unlock;
351 page = migration_entry_to_page(pmd_to_swp_entry(*pmd));
352 if (!get_page_unless_zero(page))
353 goto unlock;
354 spin_unlock(ptl);
355 wait_on_page_locked(page);
356 put_page(page);
357 return;
358unlock:
359 spin_unlock(ptl);
360}
361#endif
362
b969c4ab
MG
363#ifdef CONFIG_BLOCK
364/* Returns true if all buffers are successfully locked */
a6bc32b8
MG
365static bool buffer_migrate_lock_buffers(struct buffer_head *head,
366 enum migrate_mode mode)
b969c4ab
MG
367{
368 struct buffer_head *bh = head;
369
370 /* Simple case, sync compaction */
a6bc32b8 371 if (mode != MIGRATE_ASYNC) {
b969c4ab
MG
372 do {
373 get_bh(bh);
374 lock_buffer(bh);
375 bh = bh->b_this_page;
376
377 } while (bh != head);
378
379 return true;
380 }
381
382 /* async case, we cannot block on lock_buffer so use trylock_buffer */
383 do {
384 get_bh(bh);
385 if (!trylock_buffer(bh)) {
386 /*
387 * We failed to lock the buffer and cannot stall in
388 * async migration. Release the taken locks
389 */
390 struct buffer_head *failed_bh = bh;
391 put_bh(failed_bh);
392 bh = head;
393 while (bh != failed_bh) {
394 unlock_buffer(bh);
395 put_bh(bh);
396 bh = bh->b_this_page;
397 }
398 return false;
399 }
400
401 bh = bh->b_this_page;
402 } while (bh != head);
403 return true;
404}
405#else
406static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
a6bc32b8 407 enum migrate_mode mode)
b969c4ab
MG
408{
409 return true;
410}
411#endif /* CONFIG_BLOCK */
412
b20a3503 413/*
c3fcf8a5 414 * Replace the page in the mapping.
5b5c7120
CL
415 *
416 * The number of remaining references must be:
417 * 1 for anonymous pages without a mapping
418 * 2 for pages with a mapping
266cf658 419 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
b20a3503 420 */
36bc08cc 421int migrate_page_move_mapping(struct address_space *mapping,
b969c4ab 422 struct page *newpage, struct page *page,
8e321fef
BL
423 struct buffer_head *head, enum migrate_mode mode,
424 int extra_count)
b20a3503 425{
42cb14b1
HD
426 struct zone *oldzone, *newzone;
427 int dirty;
8e321fef 428 int expected_count = 1 + extra_count;
7cf9c2c7 429 void **pslot;
b20a3503 430
6c5240ae 431 if (!mapping) {
0e8c7d0f 432 /* Anonymous page without mapping */
8e321fef 433 if (page_count(page) != expected_count)
6c5240ae 434 return -EAGAIN;
cf4b769a
HD
435
436 /* No turning back from here */
cf4b769a
HD
437 newpage->index = page->index;
438 newpage->mapping = page->mapping;
439 if (PageSwapBacked(page))
fa9949da 440 __SetPageSwapBacked(newpage);
cf4b769a 441
78bd5209 442 return MIGRATEPAGE_SUCCESS;
6c5240ae
CL
443 }
444
42cb14b1
HD
445 oldzone = page_zone(page);
446 newzone = page_zone(newpage);
447
19fd6231 448 spin_lock_irq(&mapping->tree_lock);
b20a3503 449
7cf9c2c7
NP
450 pslot = radix_tree_lookup_slot(&mapping->page_tree,
451 page_index(page));
b20a3503 452
8e321fef 453 expected_count += 1 + page_has_private(page);
e286781d 454 if (page_count(page) != expected_count ||
29c1f677 455 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
19fd6231 456 spin_unlock_irq(&mapping->tree_lock);
e23ca00b 457 return -EAGAIN;
b20a3503
CL
458 }
459
fe896d18 460 if (!page_ref_freeze(page, expected_count)) {
19fd6231 461 spin_unlock_irq(&mapping->tree_lock);
e286781d
NP
462 return -EAGAIN;
463 }
464
b969c4ab
MG
465 /*
466 * In the async migration case of moving a page with buffers, lock the
467 * buffers using trylock before the mapping is moved. If the mapping
468 * was moved, we later failed to lock the buffers and could not move
469 * the mapping back due to an elevated page count, we would have to
470 * block waiting on other references to be dropped.
471 */
a6bc32b8
MG
472 if (mode == MIGRATE_ASYNC && head &&
473 !buffer_migrate_lock_buffers(head, mode)) {
fe896d18 474 page_ref_unfreeze(page, expected_count);
b969c4ab
MG
475 spin_unlock_irq(&mapping->tree_lock);
476 return -EAGAIN;
477 }
478
b20a3503 479 /*
cf4b769a
HD
480 * Now we know that no one else is looking at the page:
481 * no turning back from here.
b20a3503 482 */
cf4b769a
HD
483 newpage->index = page->index;
484 newpage->mapping = page->mapping;
7cf9c2c7 485 get_page(newpage); /* add cache reference */
6326fec1
NP
486 if (PageSwapBacked(page)) {
487 __SetPageSwapBacked(newpage);
488 if (PageSwapCache(page)) {
489 SetPageSwapCache(newpage);
490 set_page_private(newpage, page_private(page));
491 }
492 } else {
493 VM_BUG_ON_PAGE(PageSwapCache(page), page);
b20a3503
CL
494 }
495
42cb14b1
HD
496 /* Move dirty while page refs frozen and newpage not yet exposed */
497 dirty = PageDirty(page);
498 if (dirty) {
499 ClearPageDirty(page);
500 SetPageDirty(newpage);
501 }
502
6d75f366 503 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
7cf9c2c7
NP
504
505 /*
937a94c9
JG
506 * Drop cache reference from old page by unfreezing
507 * to one less reference.
7cf9c2c7
NP
508 * We know this isn't the last reference.
509 */
fe896d18 510 page_ref_unfreeze(page, expected_count - 1);
7cf9c2c7 511
42cb14b1
HD
512 spin_unlock(&mapping->tree_lock);
513 /* Leave irq disabled to prevent preemption while updating stats */
514
0e8c7d0f
CL
515 /*
516 * If moved to a different zone then also account
517 * the page for that zone. Other VM counters will be
518 * taken care of when we establish references to the
519 * new page and drop references to the old page.
520 *
521 * Note that anonymous pages are accounted for
4b9d0fab 522 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
0e8c7d0f
CL
523 * are mapped to swap space.
524 */
42cb14b1 525 if (newzone != oldzone) {
11fb9989
MG
526 __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES);
527 __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES);
42cb14b1 528 if (PageSwapBacked(page) && !PageSwapCache(page)) {
11fb9989
MG
529 __dec_node_state(oldzone->zone_pgdat, NR_SHMEM);
530 __inc_node_state(newzone->zone_pgdat, NR_SHMEM);
42cb14b1
HD
531 }
532 if (dirty && mapping_cap_account_dirty(mapping)) {
11fb9989 533 __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY);
5a1c84b4 534 __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING);
11fb9989 535 __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY);
5a1c84b4 536 __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING);
42cb14b1 537 }
4b02108a 538 }
42cb14b1 539 local_irq_enable();
b20a3503 540
78bd5209 541 return MIGRATEPAGE_SUCCESS;
b20a3503 542}
1118dce7 543EXPORT_SYMBOL(migrate_page_move_mapping);
b20a3503 544
290408d4
NH
545/*
546 * The expected number of remaining references is the same as that
547 * of migrate_page_move_mapping().
548 */
549int migrate_huge_page_move_mapping(struct address_space *mapping,
550 struct page *newpage, struct page *page)
551{
552 int expected_count;
553 void **pslot;
554
290408d4
NH
555 spin_lock_irq(&mapping->tree_lock);
556
557 pslot = radix_tree_lookup_slot(&mapping->page_tree,
558 page_index(page));
559
560 expected_count = 2 + page_has_private(page);
561 if (page_count(page) != expected_count ||
29c1f677 562 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
290408d4
NH
563 spin_unlock_irq(&mapping->tree_lock);
564 return -EAGAIN;
565 }
566
fe896d18 567 if (!page_ref_freeze(page, expected_count)) {
290408d4
NH
568 spin_unlock_irq(&mapping->tree_lock);
569 return -EAGAIN;
570 }
571
cf4b769a
HD
572 newpage->index = page->index;
573 newpage->mapping = page->mapping;
6a93ca8f 574
290408d4
NH
575 get_page(newpage);
576
6d75f366 577 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
290408d4 578
fe896d18 579 page_ref_unfreeze(page, expected_count - 1);
290408d4
NH
580
581 spin_unlock_irq(&mapping->tree_lock);
6a93ca8f 582
78bd5209 583 return MIGRATEPAGE_SUCCESS;
290408d4
NH
584}
585
30b0a105
DH
586/*
587 * Gigantic pages are so large that we do not guarantee that page++ pointer
588 * arithmetic will work across the entire page. We need something more
589 * specialized.
590 */
591static void __copy_gigantic_page(struct page *dst, struct page *src,
592 int nr_pages)
593{
594 int i;
595 struct page *dst_base = dst;
596 struct page *src_base = src;
597
598 for (i = 0; i < nr_pages; ) {
599 cond_resched();
600 copy_highpage(dst, src);
601
602 i++;
603 dst = mem_map_next(dst, dst_base, i);
604 src = mem_map_next(src, src_base, i);
605 }
606}
607
608static void copy_huge_page(struct page *dst, struct page *src)
609{
610 int i;
611 int nr_pages;
612
613 if (PageHuge(src)) {
614 /* hugetlbfs page */
615 struct hstate *h = page_hstate(src);
616 nr_pages = pages_per_huge_page(h);
617
618 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
619 __copy_gigantic_page(dst, src, nr_pages);
620 return;
621 }
622 } else {
623 /* thp page */
624 BUG_ON(!PageTransHuge(src));
625 nr_pages = hpage_nr_pages(src);
626 }
627
628 for (i = 0; i < nr_pages; i++) {
629 cond_resched();
630 copy_highpage(dst + i, src + i);
631 }
632}
633
b20a3503
CL
634/*
635 * Copy the page to its new location
636 */
290408d4 637void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503 638{
7851a45c
RR
639 int cpupid;
640
b32967ff 641 if (PageHuge(page) || PageTransHuge(page))
290408d4
NH
642 copy_huge_page(newpage, page);
643 else
644 copy_highpage(newpage, page);
b20a3503
CL
645
646 if (PageError(page))
647 SetPageError(newpage);
648 if (PageReferenced(page))
649 SetPageReferenced(newpage);
650 if (PageUptodate(page))
651 SetPageUptodate(newpage);
894bc310 652 if (TestClearPageActive(page)) {
309381fe 653 VM_BUG_ON_PAGE(PageUnevictable(page), page);
b20a3503 654 SetPageActive(newpage);
418b27ef
LS
655 } else if (TestClearPageUnevictable(page))
656 SetPageUnevictable(newpage);
b20a3503
CL
657 if (PageChecked(page))
658 SetPageChecked(newpage);
659 if (PageMappedToDisk(page))
660 SetPageMappedToDisk(newpage);
661
42cb14b1
HD
662 /* Move dirty on pages not done by migrate_page_move_mapping() */
663 if (PageDirty(page))
664 SetPageDirty(newpage);
b20a3503 665
33c3fc71
VD
666 if (page_is_young(page))
667 set_page_young(newpage);
668 if (page_is_idle(page))
669 set_page_idle(newpage);
670
7851a45c
RR
671 /*
672 * Copy NUMA information to the new page, to prevent over-eager
673 * future migrations of this same page.
674 */
675 cpupid = page_cpupid_xchg_last(page, -1);
676 page_cpupid_xchg_last(newpage, cpupid);
677
e9995ef9 678 ksm_migrate_page(newpage, page);
c8d6553b
HD
679 /*
680 * Please do not reorder this without considering how mm/ksm.c's
681 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
682 */
b3b3a99c
NH
683 if (PageSwapCache(page))
684 ClearPageSwapCache(page);
b20a3503
CL
685 ClearPagePrivate(page);
686 set_page_private(page, 0);
b20a3503
CL
687
688 /*
689 * If any waiters have accumulated on the new page then
690 * wake them up.
691 */
692 if (PageWriteback(newpage))
693 end_page_writeback(newpage);
d435edca
VB
694
695 copy_page_owner(page, newpage);
74485cf2
JW
696
697 mem_cgroup_migrate(page, newpage);
b20a3503 698}
1118dce7 699EXPORT_SYMBOL(migrate_page_copy);
b20a3503 700
1d8b85cc
CL
701/************************************************************
702 * Migration functions
703 ***********************************************************/
704
b20a3503 705/*
bda807d4 706 * Common logic to directly migrate a single LRU page suitable for
266cf658 707 * pages that do not use PagePrivate/PagePrivate2.
b20a3503
CL
708 *
709 * Pages are locked upon entry and exit.
710 */
2d1db3b1 711int migrate_page(struct address_space *mapping,
a6bc32b8
MG
712 struct page *newpage, struct page *page,
713 enum migrate_mode mode)
b20a3503
CL
714{
715 int rc;
716
717 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
718
8e321fef 719 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
b20a3503 720
78bd5209 721 if (rc != MIGRATEPAGE_SUCCESS)
b20a3503
CL
722 return rc;
723
724 migrate_page_copy(newpage, page);
78bd5209 725 return MIGRATEPAGE_SUCCESS;
b20a3503
CL
726}
727EXPORT_SYMBOL(migrate_page);
728
9361401e 729#ifdef CONFIG_BLOCK
1d8b85cc
CL
730/*
731 * Migration function for pages with buffers. This function can only be used
732 * if the underlying filesystem guarantees that no other references to "page"
733 * exist.
734 */
2d1db3b1 735int buffer_migrate_page(struct address_space *mapping,
a6bc32b8 736 struct page *newpage, struct page *page, enum migrate_mode mode)
1d8b85cc 737{
1d8b85cc
CL
738 struct buffer_head *bh, *head;
739 int rc;
740
1d8b85cc 741 if (!page_has_buffers(page))
a6bc32b8 742 return migrate_page(mapping, newpage, page, mode);
1d8b85cc
CL
743
744 head = page_buffers(page);
745
8e321fef 746 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
1d8b85cc 747
78bd5209 748 if (rc != MIGRATEPAGE_SUCCESS)
1d8b85cc
CL
749 return rc;
750
b969c4ab
MG
751 /*
752 * In the async case, migrate_page_move_mapping locked the buffers
753 * with an IRQ-safe spinlock held. In the sync case, the buffers
754 * need to be locked now
755 */
a6bc32b8
MG
756 if (mode != MIGRATE_ASYNC)
757 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
1d8b85cc
CL
758
759 ClearPagePrivate(page);
760 set_page_private(newpage, page_private(page));
761 set_page_private(page, 0);
762 put_page(page);
763 get_page(newpage);
764
765 bh = head;
766 do {
767 set_bh_page(bh, newpage, bh_offset(bh));
768 bh = bh->b_this_page;
769
770 } while (bh != head);
771
772 SetPagePrivate(newpage);
773
774 migrate_page_copy(newpage, page);
775
776 bh = head;
777 do {
778 unlock_buffer(bh);
779 put_bh(bh);
780 bh = bh->b_this_page;
781
782 } while (bh != head);
783
78bd5209 784 return MIGRATEPAGE_SUCCESS;
1d8b85cc
CL
785}
786EXPORT_SYMBOL(buffer_migrate_page);
9361401e 787#endif
1d8b85cc 788
04e62a29
CL
789/*
790 * Writeback a page to clean the dirty state
791 */
792static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 793{
04e62a29
CL
794 struct writeback_control wbc = {
795 .sync_mode = WB_SYNC_NONE,
796 .nr_to_write = 1,
797 .range_start = 0,
798 .range_end = LLONG_MAX,
04e62a29
CL
799 .for_reclaim = 1
800 };
801 int rc;
802
803 if (!mapping->a_ops->writepage)
804 /* No write method for the address space */
805 return -EINVAL;
806
807 if (!clear_page_dirty_for_io(page))
808 /* Someone else already triggered a write */
809 return -EAGAIN;
810
8351a6e4 811 /*
04e62a29
CL
812 * A dirty page may imply that the underlying filesystem has
813 * the page on some queue. So the page must be clean for
814 * migration. Writeout may mean we loose the lock and the
815 * page state is no longer what we checked for earlier.
816 * At this point we know that the migration attempt cannot
817 * be successful.
8351a6e4 818 */
e388466d 819 remove_migration_ptes(page, page, false);
8351a6e4 820
04e62a29 821 rc = mapping->a_ops->writepage(page, &wbc);
8351a6e4 822
04e62a29
CL
823 if (rc != AOP_WRITEPAGE_ACTIVATE)
824 /* unlocked. Relock */
825 lock_page(page);
826
bda8550d 827 return (rc < 0) ? -EIO : -EAGAIN;
04e62a29
CL
828}
829
830/*
831 * Default handling if a filesystem does not provide a migration function.
832 */
833static int fallback_migrate_page(struct address_space *mapping,
a6bc32b8 834 struct page *newpage, struct page *page, enum migrate_mode mode)
04e62a29 835{
b969c4ab 836 if (PageDirty(page)) {
a6bc32b8
MG
837 /* Only writeback pages in full synchronous migration */
838 if (mode != MIGRATE_SYNC)
b969c4ab 839 return -EBUSY;
04e62a29 840 return writeout(mapping, page);
b969c4ab 841 }
8351a6e4
CL
842
843 /*
844 * Buffers may be managed in a filesystem specific way.
845 * We must have no buffers or drop them.
846 */
266cf658 847 if (page_has_private(page) &&
8351a6e4
CL
848 !try_to_release_page(page, GFP_KERNEL))
849 return -EAGAIN;
850
a6bc32b8 851 return migrate_page(mapping, newpage, page, mode);
8351a6e4
CL
852}
853
e24f0b8f
CL
854/*
855 * Move a page to a newly allocated page
856 * The page is locked and all ptes have been successfully removed.
857 *
858 * The new page will have replaced the old page if this function
859 * is successful.
894bc310
LS
860 *
861 * Return value:
862 * < 0 - error code
78bd5209 863 * MIGRATEPAGE_SUCCESS - success
e24f0b8f 864 */
3fe2011f 865static int move_to_new_page(struct page *newpage, struct page *page,
5c3f9a67 866 enum migrate_mode mode)
e24f0b8f
CL
867{
868 struct address_space *mapping;
bda807d4
MK
869 int rc = -EAGAIN;
870 bool is_lru = !__PageMovable(page);
e24f0b8f 871
7db7671f
HD
872 VM_BUG_ON_PAGE(!PageLocked(page), page);
873 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
e24f0b8f 874
e24f0b8f 875 mapping = page_mapping(page);
bda807d4
MK
876
877 if (likely(is_lru)) {
878 if (!mapping)
879 rc = migrate_page(mapping, newpage, page, mode);
880 else if (mapping->a_ops->migratepage)
881 /*
882 * Most pages have a mapping and most filesystems
883 * provide a migratepage callback. Anonymous pages
884 * are part of swap space which also has its own
885 * migratepage callback. This is the most common path
886 * for page migration.
887 */
888 rc = mapping->a_ops->migratepage(mapping, newpage,
889 page, mode);
890 else
891 rc = fallback_migrate_page(mapping, newpage,
892 page, mode);
893 } else {
e24f0b8f 894 /*
bda807d4
MK
895 * In case of non-lru page, it could be released after
896 * isolation step. In that case, we shouldn't try migration.
e24f0b8f 897 */
bda807d4
MK
898 VM_BUG_ON_PAGE(!PageIsolated(page), page);
899 if (!PageMovable(page)) {
900 rc = MIGRATEPAGE_SUCCESS;
901 __ClearPageIsolated(page);
902 goto out;
903 }
904
905 rc = mapping->a_ops->migratepage(mapping, newpage,
906 page, mode);
907 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
908 !PageIsolated(page));
909 }
e24f0b8f 910
5c3f9a67
HD
911 /*
912 * When successful, old pagecache page->mapping must be cleared before
913 * page is freed; but stats require that PageAnon be left as PageAnon.
914 */
915 if (rc == MIGRATEPAGE_SUCCESS) {
bda807d4
MK
916 if (__PageMovable(page)) {
917 VM_BUG_ON_PAGE(!PageIsolated(page), page);
918
919 /*
920 * We clear PG_movable under page_lock so any compactor
921 * cannot try to migrate this page.
922 */
923 __ClearPageIsolated(page);
924 }
925
926 /*
927 * Anonymous and movable page->mapping will be cleard by
928 * free_pages_prepare so don't reset it here for keeping
929 * the type to work PageAnon, for example.
930 */
931 if (!PageMappingFlags(page))
5c3f9a67 932 page->mapping = NULL;
3fe2011f 933 }
bda807d4 934out:
e24f0b8f
CL
935 return rc;
936}
937
0dabec93 938static int __unmap_and_move(struct page *page, struct page *newpage,
9c620e2b 939 int force, enum migrate_mode mode)
e24f0b8f 940{
0dabec93 941 int rc = -EAGAIN;
2ebba6b7 942 int page_was_mapped = 0;
3f6c8272 943 struct anon_vma *anon_vma = NULL;
bda807d4 944 bool is_lru = !__PageMovable(page);
95a402c3 945
529ae9aa 946 if (!trylock_page(page)) {
a6bc32b8 947 if (!force || mode == MIGRATE_ASYNC)
0dabec93 948 goto out;
3e7d3449
MG
949
950 /*
951 * It's not safe for direct compaction to call lock_page.
952 * For example, during page readahead pages are added locked
953 * to the LRU. Later, when the IO completes the pages are
954 * marked uptodate and unlocked. However, the queueing
955 * could be merging multiple pages for one bio (e.g.
956 * mpage_readpages). If an allocation happens for the
957 * second or third page, the process can end up locking
958 * the same page twice and deadlocking. Rather than
959 * trying to be clever about what pages can be locked,
960 * avoid the use of lock_page for direct compaction
961 * altogether.
962 */
963 if (current->flags & PF_MEMALLOC)
0dabec93 964 goto out;
3e7d3449 965
e24f0b8f
CL
966 lock_page(page);
967 }
968
969 if (PageWriteback(page)) {
11bc82d6 970 /*
fed5b64a 971 * Only in the case of a full synchronous migration is it
a6bc32b8
MG
972 * necessary to wait for PageWriteback. In the async case,
973 * the retry loop is too short and in the sync-light case,
974 * the overhead of stalling is too much
11bc82d6 975 */
a6bc32b8 976 if (mode != MIGRATE_SYNC) {
11bc82d6 977 rc = -EBUSY;
0a31bc97 978 goto out_unlock;
11bc82d6
AA
979 }
980 if (!force)
0a31bc97 981 goto out_unlock;
e24f0b8f
CL
982 wait_on_page_writeback(page);
983 }
03f15c86 984
e24f0b8f 985 /*
dc386d4d
KH
986 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
987 * we cannot notice that anon_vma is freed while we migrates a page.
1ce82b69 988 * This get_anon_vma() delays freeing anon_vma pointer until the end
dc386d4d 989 * of migration. File cache pages are no problem because of page_lock()
989f89c5
KH
990 * File Caches may use write_page() or lock_page() in migration, then,
991 * just care Anon page here.
03f15c86
HD
992 *
993 * Only page_get_anon_vma() understands the subtleties of
994 * getting a hold on an anon_vma from outside one of its mms.
995 * But if we cannot get anon_vma, then we won't need it anyway,
996 * because that implies that the anon page is no longer mapped
997 * (and cannot be remapped so long as we hold the page lock).
dc386d4d 998 */
03f15c86 999 if (PageAnon(page) && !PageKsm(page))
746b18d4 1000 anon_vma = page_get_anon_vma(page);
62e1c553 1001
7db7671f
HD
1002 /*
1003 * Block others from accessing the new page when we get around to
1004 * establishing additional references. We are usually the only one
1005 * holding a reference to newpage at this point. We used to have a BUG
1006 * here if trylock_page(newpage) fails, but would like to allow for
1007 * cases where there might be a race with the previous use of newpage.
1008 * This is much like races on refcount of oldpage: just don't BUG().
1009 */
1010 if (unlikely(!trylock_page(newpage)))
1011 goto out_unlock;
1012
bda807d4
MK
1013 if (unlikely(!is_lru)) {
1014 rc = move_to_new_page(newpage, page, mode);
1015 goto out_unlock_both;
1016 }
1017
dc386d4d 1018 /*
62e1c553
SL
1019 * Corner case handling:
1020 * 1. When a new swap-cache page is read into, it is added to the LRU
1021 * and treated as swapcache but it has no rmap yet.
1022 * Calling try_to_unmap() against a page->mapping==NULL page will
1023 * trigger a BUG. So handle it here.
1024 * 2. An orphaned page (see truncate_complete_page) might have
1025 * fs-private metadata. The page can be picked up due to memory
1026 * offlining. Everywhere else except page reclaim, the page is
1027 * invisible to the vm, so the page can not be migrated. So try to
1028 * free the metadata, so the page can be freed.
e24f0b8f 1029 */
62e1c553 1030 if (!page->mapping) {
309381fe 1031 VM_BUG_ON_PAGE(PageAnon(page), page);
1ce82b69 1032 if (page_has_private(page)) {
62e1c553 1033 try_to_free_buffers(page);
7db7671f 1034 goto out_unlock_both;
62e1c553 1035 }
7db7671f
HD
1036 } else if (page_mapped(page)) {
1037 /* Establish migration ptes */
03f15c86
HD
1038 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1039 page);
2ebba6b7 1040 try_to_unmap(page,
da1b13cc 1041 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
2ebba6b7
HD
1042 page_was_mapped = 1;
1043 }
dc386d4d 1044
e6a1530d 1045 if (!page_mapped(page))
5c3f9a67 1046 rc = move_to_new_page(newpage, page, mode);
e24f0b8f 1047
5c3f9a67
HD
1048 if (page_was_mapped)
1049 remove_migration_ptes(page,
e388466d 1050 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
3f6c8272 1051
7db7671f
HD
1052out_unlock_both:
1053 unlock_page(newpage);
1054out_unlock:
3f6c8272 1055 /* Drop an anon_vma reference if we took one */
76545066 1056 if (anon_vma)
9e60109f 1057 put_anon_vma(anon_vma);
e24f0b8f 1058 unlock_page(page);
0dabec93 1059out:
c6c919eb
MK
1060 /*
1061 * If migration is successful, decrease refcount of the newpage
1062 * which will not free the page because new page owner increased
1063 * refcounter. As well, if it is LRU page, add the page to LRU
1064 * list in here.
1065 */
1066 if (rc == MIGRATEPAGE_SUCCESS) {
b1123ea6 1067 if (unlikely(__PageMovable(newpage)))
c6c919eb
MK
1068 put_page(newpage);
1069 else
1070 putback_lru_page(newpage);
1071 }
1072
0dabec93
MK
1073 return rc;
1074}
95a402c3 1075
ef2a5153
GU
1076/*
1077 * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
1078 * around it.
1079 */
1080#if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
1081#define ICE_noinline noinline
1082#else
1083#define ICE_noinline
1084#endif
1085
0dabec93
MK
1086/*
1087 * Obtain the lock on page, remove all ptes and migrate the page
1088 * to the newly allocated page in newpage.
1089 */
ef2a5153
GU
1090static ICE_noinline int unmap_and_move(new_page_t get_new_page,
1091 free_page_t put_new_page,
1092 unsigned long private, struct page *page,
add05cec
NH
1093 int force, enum migrate_mode mode,
1094 enum migrate_reason reason)
0dabec93 1095{
2def7424 1096 int rc = MIGRATEPAGE_SUCCESS;
0dabec93 1097 int *result = NULL;
2def7424 1098 struct page *newpage;
0dabec93 1099
2def7424 1100 newpage = get_new_page(page, private, &result);
0dabec93
MK
1101 if (!newpage)
1102 return -ENOMEM;
1103
1104 if (page_count(page) == 1) {
1105 /* page was freed from under us. So we are done. */
c6c919eb
MK
1106 ClearPageActive(page);
1107 ClearPageUnevictable(page);
bda807d4
MK
1108 if (unlikely(__PageMovable(page))) {
1109 lock_page(page);
1110 if (!PageMovable(page))
1111 __ClearPageIsolated(page);
1112 unlock_page(page);
1113 }
c6c919eb
MK
1114 if (put_new_page)
1115 put_new_page(newpage, private);
1116 else
1117 put_page(newpage);
0dabec93
MK
1118 goto out;
1119 }
1120
616b8371 1121 if (unlikely(PageTransHuge(page) && !PageTransHuge(newpage))) {
4d2fa965
KS
1122 lock_page(page);
1123 rc = split_huge_page(page);
1124 unlock_page(page);
1125 if (rc)
0dabec93 1126 goto out;
4d2fa965 1127 }
0dabec93 1128
9c620e2b 1129 rc = __unmap_and_move(page, newpage, force, mode);
c6c919eb 1130 if (rc == MIGRATEPAGE_SUCCESS)
7cd12b4a 1131 set_page_owner_migrate_reason(newpage, reason);
bf6bddf1 1132
0dabec93 1133out:
e24f0b8f 1134 if (rc != -EAGAIN) {
0dabec93
MK
1135 /*
1136 * A page that has been migrated has all references
1137 * removed and will be freed. A page that has not been
1138 * migrated will have kepts its references and be
1139 * restored.
1140 */
1141 list_del(&page->lru);
6afcf8ef
ML
1142
1143 /*
1144 * Compaction can migrate also non-LRU pages which are
1145 * not accounted to NR_ISOLATED_*. They can be recognized
1146 * as __PageMovable
1147 */
1148 if (likely(!__PageMovable(page)))
1149 dec_node_page_state(page, NR_ISOLATED_ANON +
1150 page_is_file_cache(page));
c6c919eb
MK
1151 }
1152
1153 /*
1154 * If migration is successful, releases reference grabbed during
1155 * isolation. Otherwise, restore the page to right list unless
1156 * we want to retry.
1157 */
1158 if (rc == MIGRATEPAGE_SUCCESS) {
1159 put_page(page);
1160 if (reason == MR_MEMORY_FAILURE) {
d7e69488 1161 /*
c6c919eb
MK
1162 * Set PG_HWPoison on just freed page
1163 * intentionally. Although it's rather weird,
1164 * it's how HWPoison flag works at the moment.
d7e69488 1165 */
da1b13cc
WL
1166 if (!test_set_page_hwpoison(page))
1167 num_poisoned_pages_inc();
c6c919eb
MK
1168 }
1169 } else {
bda807d4
MK
1170 if (rc != -EAGAIN) {
1171 if (likely(!__PageMovable(page))) {
1172 putback_lru_page(page);
1173 goto put_new;
1174 }
1175
1176 lock_page(page);
1177 if (PageMovable(page))
1178 putback_movable_page(page);
1179 else
1180 __ClearPageIsolated(page);
1181 unlock_page(page);
1182 put_page(page);
1183 }
1184put_new:
c6c919eb
MK
1185 if (put_new_page)
1186 put_new_page(newpage, private);
1187 else
1188 put_page(newpage);
e24f0b8f 1189 }
68711a74 1190
742755a1
CL
1191 if (result) {
1192 if (rc)
1193 *result = rc;
1194 else
1195 *result = page_to_nid(newpage);
1196 }
e24f0b8f
CL
1197 return rc;
1198}
1199
290408d4
NH
1200/*
1201 * Counterpart of unmap_and_move_page() for hugepage migration.
1202 *
1203 * This function doesn't wait the completion of hugepage I/O
1204 * because there is no race between I/O and migration for hugepage.
1205 * Note that currently hugepage I/O occurs only in direct I/O
1206 * where no lock is held and PG_writeback is irrelevant,
1207 * and writeback status of all subpages are counted in the reference
1208 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1209 * under direct I/O, the reference of the head page is 512 and a bit more.)
1210 * This means that when we try to migrate hugepage whose subpages are
1211 * doing direct I/O, some references remain after try_to_unmap() and
1212 * hugepage migration fails without data corruption.
1213 *
1214 * There is also no race when direct I/O is issued on the page under migration,
1215 * because then pte is replaced with migration swap entry and direct I/O code
1216 * will wait in the page fault for migration to complete.
1217 */
1218static int unmap_and_move_huge_page(new_page_t get_new_page,
68711a74
DR
1219 free_page_t put_new_page, unsigned long private,
1220 struct page *hpage, int force,
7cd12b4a 1221 enum migrate_mode mode, int reason)
290408d4 1222{
2def7424 1223 int rc = -EAGAIN;
290408d4 1224 int *result = NULL;
2ebba6b7 1225 int page_was_mapped = 0;
32665f2b 1226 struct page *new_hpage;
290408d4
NH
1227 struct anon_vma *anon_vma = NULL;
1228
83467efb
NH
1229 /*
1230 * Movability of hugepages depends on architectures and hugepage size.
1231 * This check is necessary because some callers of hugepage migration
1232 * like soft offline and memory hotremove don't walk through page
1233 * tables or check whether the hugepage is pmd-based or not before
1234 * kicking migration.
1235 */
100873d7 1236 if (!hugepage_migration_supported(page_hstate(hpage))) {
32665f2b 1237 putback_active_hugepage(hpage);
83467efb 1238 return -ENOSYS;
32665f2b 1239 }
83467efb 1240
32665f2b 1241 new_hpage = get_new_page(hpage, private, &result);
290408d4
NH
1242 if (!new_hpage)
1243 return -ENOMEM;
1244
290408d4 1245 if (!trylock_page(hpage)) {
a6bc32b8 1246 if (!force || mode != MIGRATE_SYNC)
290408d4
NH
1247 goto out;
1248 lock_page(hpage);
1249 }
1250
746b18d4
PZ
1251 if (PageAnon(hpage))
1252 anon_vma = page_get_anon_vma(hpage);
290408d4 1253
7db7671f
HD
1254 if (unlikely(!trylock_page(new_hpage)))
1255 goto put_anon;
1256
2ebba6b7
HD
1257 if (page_mapped(hpage)) {
1258 try_to_unmap(hpage,
1259 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
1260 page_was_mapped = 1;
1261 }
290408d4
NH
1262
1263 if (!page_mapped(hpage))
5c3f9a67 1264 rc = move_to_new_page(new_hpage, hpage, mode);
290408d4 1265
5c3f9a67
HD
1266 if (page_was_mapped)
1267 remove_migration_ptes(hpage,
e388466d 1268 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
290408d4 1269
7db7671f
HD
1270 unlock_page(new_hpage);
1271
1272put_anon:
fd4a4663 1273 if (anon_vma)
9e60109f 1274 put_anon_vma(anon_vma);
8e6ac7fa 1275
2def7424 1276 if (rc == MIGRATEPAGE_SUCCESS) {
8e6ac7fa 1277 hugetlb_cgroup_migrate(hpage, new_hpage);
2def7424 1278 put_new_page = NULL;
7cd12b4a 1279 set_page_owner_migrate_reason(new_hpage, reason);
2def7424 1280 }
8e6ac7fa 1281
290408d4 1282 unlock_page(hpage);
09761333 1283out:
b8ec1cee
NH
1284 if (rc != -EAGAIN)
1285 putback_active_hugepage(hpage);
c3114a84
AK
1286 if (reason == MR_MEMORY_FAILURE && !test_set_page_hwpoison(hpage))
1287 num_poisoned_pages_inc();
68711a74
DR
1288
1289 /*
1290 * If migration was not successful and there's a freeing callback, use
1291 * it. Otherwise, put_page() will drop the reference grabbed during
1292 * isolation.
1293 */
2def7424 1294 if (put_new_page)
68711a74
DR
1295 put_new_page(new_hpage, private);
1296 else
3aaa76e1 1297 putback_active_hugepage(new_hpage);
68711a74 1298
290408d4
NH
1299 if (result) {
1300 if (rc)
1301 *result = rc;
1302 else
1303 *result = page_to_nid(new_hpage);
1304 }
1305 return rc;
1306}
1307
b20a3503 1308/*
c73e5c9c
SB
1309 * migrate_pages - migrate the pages specified in a list, to the free pages
1310 * supplied as the target for the page migration
b20a3503 1311 *
c73e5c9c
SB
1312 * @from: The list of pages to be migrated.
1313 * @get_new_page: The function used to allocate free pages to be used
1314 * as the target of the page migration.
68711a74
DR
1315 * @put_new_page: The function used to free target pages if migration
1316 * fails, or NULL if no special handling is necessary.
c73e5c9c
SB
1317 * @private: Private data to be passed on to get_new_page()
1318 * @mode: The migration mode that specifies the constraints for
1319 * page migration, if any.
1320 * @reason: The reason for page migration.
b20a3503 1321 *
c73e5c9c
SB
1322 * The function returns after 10 attempts or if no pages are movable any more
1323 * because the list has become empty or no retryable pages exist any more.
14e0f9bc 1324 * The caller should call putback_movable_pages() to return pages to the LRU
28bd6578 1325 * or free list only if ret != 0.
b20a3503 1326 *
c73e5c9c 1327 * Returns the number of pages that were not migrated, or an error code.
b20a3503 1328 */
9c620e2b 1329int migrate_pages(struct list_head *from, new_page_t get_new_page,
68711a74
DR
1330 free_page_t put_new_page, unsigned long private,
1331 enum migrate_mode mode, int reason)
b20a3503 1332{
e24f0b8f 1333 int retry = 1;
b20a3503 1334 int nr_failed = 0;
5647bc29 1335 int nr_succeeded = 0;
b20a3503
CL
1336 int pass = 0;
1337 struct page *page;
1338 struct page *page2;
1339 int swapwrite = current->flags & PF_SWAPWRITE;
1340 int rc;
1341
1342 if (!swapwrite)
1343 current->flags |= PF_SWAPWRITE;
1344
e24f0b8f
CL
1345 for(pass = 0; pass < 10 && retry; pass++) {
1346 retry = 0;
b20a3503 1347
e24f0b8f 1348 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 1349 cond_resched();
2d1db3b1 1350
31caf665
NH
1351 if (PageHuge(page))
1352 rc = unmap_and_move_huge_page(get_new_page,
68711a74 1353 put_new_page, private, page,
7cd12b4a 1354 pass > 2, mode, reason);
31caf665 1355 else
68711a74 1356 rc = unmap_and_move(get_new_page, put_new_page,
add05cec
NH
1357 private, page, pass > 2, mode,
1358 reason);
2d1db3b1 1359
e24f0b8f 1360 switch(rc) {
95a402c3 1361 case -ENOMEM:
dfef2ef4 1362 nr_failed++;
95a402c3 1363 goto out;
e24f0b8f 1364 case -EAGAIN:
2d1db3b1 1365 retry++;
e24f0b8f 1366 break;
78bd5209 1367 case MIGRATEPAGE_SUCCESS:
5647bc29 1368 nr_succeeded++;
e24f0b8f
CL
1369 break;
1370 default:
354a3363
NH
1371 /*
1372 * Permanent failure (-EBUSY, -ENOSYS, etc.):
1373 * unlike -EAGAIN case, the failed page is
1374 * removed from migration page list and not
1375 * retried in the next outer loop.
1376 */
2d1db3b1 1377 nr_failed++;
e24f0b8f 1378 break;
2d1db3b1 1379 }
b20a3503
CL
1380 }
1381 }
f2f81fb2
VB
1382 nr_failed += retry;
1383 rc = nr_failed;
95a402c3 1384out:
5647bc29
MG
1385 if (nr_succeeded)
1386 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1387 if (nr_failed)
1388 count_vm_events(PGMIGRATE_FAIL, nr_failed);
7b2a2d4a
MG
1389 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1390
b20a3503
CL
1391 if (!swapwrite)
1392 current->flags &= ~PF_SWAPWRITE;
1393
78bd5209 1394 return rc;
b20a3503 1395}
95a402c3 1396
742755a1
CL
1397#ifdef CONFIG_NUMA
1398/*
1399 * Move a list of individual pages
1400 */
1401struct page_to_node {
1402 unsigned long addr;
1403 struct page *page;
1404 int node;
1405 int status;
1406};
1407
1408static struct page *new_page_node(struct page *p, unsigned long private,
1409 int **result)
1410{
1411 struct page_to_node *pm = (struct page_to_node *)private;
1412
1413 while (pm->node != MAX_NUMNODES && pm->page != p)
1414 pm++;
1415
1416 if (pm->node == MAX_NUMNODES)
1417 return NULL;
1418
1419 *result = &pm->status;
1420
e632a938
NH
1421 if (PageHuge(p))
1422 return alloc_huge_page_node(page_hstate(compound_head(p)),
1423 pm->node);
1424 else
96db800f 1425 return __alloc_pages_node(pm->node,
e97ca8e5 1426 GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
742755a1
CL
1427}
1428
1429/*
1430 * Move a set of pages as indicated in the pm array. The addr
1431 * field must be set to the virtual address of the page to be moved
1432 * and the node number must contain a valid target node.
5e9a0f02 1433 * The pm array ends with node = MAX_NUMNODES.
742755a1 1434 */
5e9a0f02
BG
1435static int do_move_page_to_node_array(struct mm_struct *mm,
1436 struct page_to_node *pm,
1437 int migrate_all)
742755a1
CL
1438{
1439 int err;
1440 struct page_to_node *pp;
1441 LIST_HEAD(pagelist);
1442
1443 down_read(&mm->mmap_sem);
1444
1445 /*
1446 * Build a list of pages to migrate
1447 */
742755a1
CL
1448 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1449 struct vm_area_struct *vma;
1450 struct page *page;
1451
742755a1
CL
1452 err = -EFAULT;
1453 vma = find_vma(mm, pp->addr);
70384dc6 1454 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
742755a1
CL
1455 goto set_status;
1456
d899844e
KS
1457 /* FOLL_DUMP to ignore special (like zero) pages */
1458 page = follow_page(vma, pp->addr,
1459 FOLL_GET | FOLL_SPLIT | FOLL_DUMP);
89f5b7da
LT
1460
1461 err = PTR_ERR(page);
1462 if (IS_ERR(page))
1463 goto set_status;
1464
742755a1
CL
1465 err = -ENOENT;
1466 if (!page)
1467 goto set_status;
1468
742755a1
CL
1469 pp->page = page;
1470 err = page_to_nid(page);
1471
1472 if (err == pp->node)
1473 /*
1474 * Node already in the right place
1475 */
1476 goto put_and_set;
1477
1478 err = -EACCES;
1479 if (page_mapcount(page) > 1 &&
1480 !migrate_all)
1481 goto put_and_set;
1482
e632a938 1483 if (PageHuge(page)) {
e66f17ff
NH
1484 if (PageHead(page))
1485 isolate_huge_page(page, &pagelist);
e632a938
NH
1486 goto put_and_set;
1487 }
1488
62695a84 1489 err = isolate_lru_page(page);
6d9c285a 1490 if (!err) {
62695a84 1491 list_add_tail(&page->lru, &pagelist);
599d0c95 1492 inc_node_page_state(page, NR_ISOLATED_ANON +
6d9c285a
KM
1493 page_is_file_cache(page));
1494 }
742755a1
CL
1495put_and_set:
1496 /*
1497 * Either remove the duplicate refcount from
1498 * isolate_lru_page() or drop the page ref if it was
1499 * not isolated.
1500 */
1501 put_page(page);
1502set_status:
1503 pp->status = err;
1504 }
1505
e78bbfa8 1506 err = 0;
cf608ac1 1507 if (!list_empty(&pagelist)) {
68711a74 1508 err = migrate_pages(&pagelist, new_page_node, NULL,
9c620e2b 1509 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
cf608ac1 1510 if (err)
e632a938 1511 putback_movable_pages(&pagelist);
cf608ac1 1512 }
742755a1
CL
1513
1514 up_read(&mm->mmap_sem);
1515 return err;
1516}
1517
5e9a0f02
BG
1518/*
1519 * Migrate an array of page address onto an array of nodes and fill
1520 * the corresponding array of status.
1521 */
3268c63e 1522static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
5e9a0f02
BG
1523 unsigned long nr_pages,
1524 const void __user * __user *pages,
1525 const int __user *nodes,
1526 int __user *status, int flags)
1527{
3140a227 1528 struct page_to_node *pm;
3140a227
BG
1529 unsigned long chunk_nr_pages;
1530 unsigned long chunk_start;
1531 int err;
5e9a0f02 1532
3140a227
BG
1533 err = -ENOMEM;
1534 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1535 if (!pm)
5e9a0f02 1536 goto out;
35282a2d
BG
1537
1538 migrate_prep();
1539
5e9a0f02 1540 /*
3140a227
BG
1541 * Store a chunk of page_to_node array in a page,
1542 * but keep the last one as a marker
5e9a0f02 1543 */
3140a227 1544 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
5e9a0f02 1545
3140a227
BG
1546 for (chunk_start = 0;
1547 chunk_start < nr_pages;
1548 chunk_start += chunk_nr_pages) {
1549 int j;
5e9a0f02 1550
3140a227
BG
1551 if (chunk_start + chunk_nr_pages > nr_pages)
1552 chunk_nr_pages = nr_pages - chunk_start;
1553
1554 /* fill the chunk pm with addrs and nodes from user-space */
1555 for (j = 0; j < chunk_nr_pages; j++) {
1556 const void __user *p;
5e9a0f02
BG
1557 int node;
1558
3140a227
BG
1559 err = -EFAULT;
1560 if (get_user(p, pages + j + chunk_start))
1561 goto out_pm;
1562 pm[j].addr = (unsigned long) p;
1563
1564 if (get_user(node, nodes + j + chunk_start))
5e9a0f02
BG
1565 goto out_pm;
1566
1567 err = -ENODEV;
6f5a55f1
LT
1568 if (node < 0 || node >= MAX_NUMNODES)
1569 goto out_pm;
1570
389162c2 1571 if (!node_state(node, N_MEMORY))
5e9a0f02
BG
1572 goto out_pm;
1573
1574 err = -EACCES;
1575 if (!node_isset(node, task_nodes))
1576 goto out_pm;
1577
3140a227
BG
1578 pm[j].node = node;
1579 }
1580
1581 /* End marker for this chunk */
1582 pm[chunk_nr_pages].node = MAX_NUMNODES;
1583
1584 /* Migrate this chunk */
1585 err = do_move_page_to_node_array(mm, pm,
1586 flags & MPOL_MF_MOVE_ALL);
1587 if (err < 0)
1588 goto out_pm;
5e9a0f02 1589
5e9a0f02 1590 /* Return status information */
3140a227
BG
1591 for (j = 0; j < chunk_nr_pages; j++)
1592 if (put_user(pm[j].status, status + j + chunk_start)) {
5e9a0f02 1593 err = -EFAULT;
3140a227
BG
1594 goto out_pm;
1595 }
1596 }
1597 err = 0;
5e9a0f02
BG
1598
1599out_pm:
3140a227 1600 free_page((unsigned long)pm);
5e9a0f02
BG
1601out:
1602 return err;
1603}
1604
742755a1 1605/*
2f007e74 1606 * Determine the nodes of an array of pages and store it in an array of status.
742755a1 1607 */
80bba129
BG
1608static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1609 const void __user **pages, int *status)
742755a1 1610{
2f007e74 1611 unsigned long i;
2f007e74 1612
742755a1
CL
1613 down_read(&mm->mmap_sem);
1614
2f007e74 1615 for (i = 0; i < nr_pages; i++) {
80bba129 1616 unsigned long addr = (unsigned long)(*pages);
742755a1
CL
1617 struct vm_area_struct *vma;
1618 struct page *page;
c095adbc 1619 int err = -EFAULT;
2f007e74
BG
1620
1621 vma = find_vma(mm, addr);
70384dc6 1622 if (!vma || addr < vma->vm_start)
742755a1
CL
1623 goto set_status;
1624
d899844e
KS
1625 /* FOLL_DUMP to ignore special (like zero) pages */
1626 page = follow_page(vma, addr, FOLL_DUMP);
89f5b7da
LT
1627
1628 err = PTR_ERR(page);
1629 if (IS_ERR(page))
1630 goto set_status;
1631
d899844e 1632 err = page ? page_to_nid(page) : -ENOENT;
742755a1 1633set_status:
80bba129
BG
1634 *status = err;
1635
1636 pages++;
1637 status++;
1638 }
1639
1640 up_read(&mm->mmap_sem);
1641}
1642
1643/*
1644 * Determine the nodes of a user array of pages and store it in
1645 * a user array of status.
1646 */
1647static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1648 const void __user * __user *pages,
1649 int __user *status)
1650{
1651#define DO_PAGES_STAT_CHUNK_NR 16
1652 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1653 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
80bba129 1654
87b8d1ad
PA
1655 while (nr_pages) {
1656 unsigned long chunk_nr;
80bba129 1657
87b8d1ad
PA
1658 chunk_nr = nr_pages;
1659 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1660 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1661
1662 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1663 break;
80bba129
BG
1664
1665 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1666
87b8d1ad
PA
1667 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1668 break;
742755a1 1669
87b8d1ad
PA
1670 pages += chunk_nr;
1671 status += chunk_nr;
1672 nr_pages -= chunk_nr;
1673 }
1674 return nr_pages ? -EFAULT : 0;
742755a1
CL
1675}
1676
1677/*
1678 * Move a list of pages in the address space of the currently executing
1679 * process.
1680 */
938bb9f5
HC
1681SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1682 const void __user * __user *, pages,
1683 const int __user *, nodes,
1684 int __user *, status, int, flags)
742755a1 1685{
742755a1 1686 struct task_struct *task;
742755a1 1687 struct mm_struct *mm;
5e9a0f02 1688 int err;
3268c63e 1689 nodemask_t task_nodes;
742755a1
CL
1690
1691 /* Check flags */
1692 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1693 return -EINVAL;
1694
1695 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1696 return -EPERM;
1697
1698 /* Find the mm_struct */
a879bf58 1699 rcu_read_lock();
228ebcbe 1700 task = pid ? find_task_by_vpid(pid) : current;
742755a1 1701 if (!task) {
a879bf58 1702 rcu_read_unlock();
742755a1
CL
1703 return -ESRCH;
1704 }
3268c63e 1705 get_task_struct(task);
742755a1
CL
1706
1707 /*
1708 * Check if this process has the right to modify the specified
197e7e52 1709 * process. Use the regular "ptrace_may_access()" checks.
742755a1 1710 */
197e7e52 1711 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
c69e8d9c 1712 rcu_read_unlock();
742755a1 1713 err = -EPERM;
5e9a0f02 1714 goto out;
742755a1 1715 }
c69e8d9c 1716 rcu_read_unlock();
742755a1 1717
86c3a764
DQ
1718 err = security_task_movememory(task);
1719 if (err)
5e9a0f02 1720 goto out;
86c3a764 1721
3268c63e
CL
1722 task_nodes = cpuset_mems_allowed(task);
1723 mm = get_task_mm(task);
1724 put_task_struct(task);
1725
6e8b09ea
SL
1726 if (!mm)
1727 return -EINVAL;
1728
1729 if (nodes)
1730 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1731 nodes, status, flags);
1732 else
1733 err = do_pages_stat(mm, nr_pages, pages, status);
742755a1 1734
742755a1
CL
1735 mmput(mm);
1736 return err;
3268c63e
CL
1737
1738out:
1739 put_task_struct(task);
1740 return err;
742755a1 1741}
742755a1 1742
7039e1db
PZ
1743#ifdef CONFIG_NUMA_BALANCING
1744/*
1745 * Returns true if this is a safe migration target node for misplaced NUMA
1746 * pages. Currently it only checks the watermarks which crude
1747 */
1748static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
3abef4e6 1749 unsigned long nr_migrate_pages)
7039e1db
PZ
1750{
1751 int z;
599d0c95 1752
7039e1db
PZ
1753 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1754 struct zone *zone = pgdat->node_zones + z;
1755
1756 if (!populated_zone(zone))
1757 continue;
1758
7039e1db
PZ
1759 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1760 if (!zone_watermark_ok(zone, 0,
1761 high_wmark_pages(zone) +
1762 nr_migrate_pages,
1763 0, 0))
1764 continue;
1765 return true;
1766 }
1767 return false;
1768}
1769
1770static struct page *alloc_misplaced_dst_page(struct page *page,
1771 unsigned long data,
1772 int **result)
1773{
1774 int nid = (int) data;
1775 struct page *newpage;
1776
96db800f 1777 newpage = __alloc_pages_node(nid,
e97ca8e5
JW
1778 (GFP_HIGHUSER_MOVABLE |
1779 __GFP_THISNODE | __GFP_NOMEMALLOC |
1780 __GFP_NORETRY | __GFP_NOWARN) &
8479eba7 1781 ~__GFP_RECLAIM, 0);
bac0382c 1782
7039e1db
PZ
1783 return newpage;
1784}
1785
a8f60772
MG
1786/*
1787 * page migration rate limiting control.
1788 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1789 * window of time. Default here says do not migrate more than 1280M per second.
1790 */
1791static unsigned int migrate_interval_millisecs __read_mostly = 100;
1792static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1793
b32967ff 1794/* Returns true if the node is migrate rate-limited after the update */
1c30e017
MG
1795static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
1796 unsigned long nr_pages)
7039e1db 1797{
a8f60772
MG
1798 /*
1799 * Rate-limit the amount of data that is being migrated to a node.
1800 * Optimal placement is no good if the memory bus is saturated and
1801 * all the time is being spent migrating!
1802 */
a8f60772 1803 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
1c5e9c27 1804 spin_lock(&pgdat->numabalancing_migrate_lock);
a8f60772
MG
1805 pgdat->numabalancing_migrate_nr_pages = 0;
1806 pgdat->numabalancing_migrate_next_window = jiffies +
1807 msecs_to_jiffies(migrate_interval_millisecs);
1c5e9c27 1808 spin_unlock(&pgdat->numabalancing_migrate_lock);
a8f60772 1809 }
af1839d7
MG
1810 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
1811 trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
1812 nr_pages);
1c5e9c27 1813 return true;
af1839d7 1814 }
1c5e9c27
MG
1815
1816 /*
1817 * This is an unlocked non-atomic update so errors are possible.
1818 * The consequences are failing to migrate when we potentiall should
1819 * have which is not severe enough to warrant locking. If it is ever
1820 * a problem, it can be converted to a per-cpu counter.
1821 */
1822 pgdat->numabalancing_migrate_nr_pages += nr_pages;
1823 return false;
b32967ff
MG
1824}
1825
1c30e017 1826static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
b32967ff 1827{
340ef390 1828 int page_lru;
a8f60772 1829
309381fe 1830 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
3abef4e6 1831
7039e1db 1832 /* Avoid migrating to a node that is nearly full */
340ef390
HD
1833 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1834 return 0;
7039e1db 1835
340ef390
HD
1836 if (isolate_lru_page(page))
1837 return 0;
7039e1db 1838
340ef390
HD
1839 /*
1840 * migrate_misplaced_transhuge_page() skips page migration's usual
1841 * check on page_count(), so we must do it here, now that the page
1842 * has been isolated: a GUP pin, or any other pin, prevents migration.
1843 * The expected page count is 3: 1 for page's mapcount and 1 for the
1844 * caller's pin and 1 for the reference taken by isolate_lru_page().
1845 */
1846 if (PageTransHuge(page) && page_count(page) != 3) {
1847 putback_lru_page(page);
1848 return 0;
7039e1db
PZ
1849 }
1850
340ef390 1851 page_lru = page_is_file_cache(page);
599d0c95 1852 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
340ef390
HD
1853 hpage_nr_pages(page));
1854
149c33e1 1855 /*
340ef390
HD
1856 * Isolating the page has taken another reference, so the
1857 * caller's reference can be safely dropped without the page
1858 * disappearing underneath us during migration.
149c33e1
MG
1859 */
1860 put_page(page);
340ef390 1861 return 1;
b32967ff
MG
1862}
1863
de466bd6
MG
1864bool pmd_trans_migrating(pmd_t pmd)
1865{
1866 struct page *page = pmd_page(pmd);
1867 return PageLocked(page);
1868}
1869
b32967ff
MG
1870/*
1871 * Attempt to migrate a misplaced page to the specified destination
1872 * node. Caller is expected to have an elevated reference count on
1873 * the page that will be dropped by this function before returning.
1874 */
1bc115d8
MG
1875int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
1876 int node)
b32967ff
MG
1877{
1878 pg_data_t *pgdat = NODE_DATA(node);
340ef390 1879 int isolated;
b32967ff
MG
1880 int nr_remaining;
1881 LIST_HEAD(migratepages);
1882
1883 /*
1bc115d8
MG
1884 * Don't migrate file pages that are mapped in multiple processes
1885 * with execute permissions as they are probably shared libraries.
b32967ff 1886 */
1bc115d8
MG
1887 if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
1888 (vma->vm_flags & VM_EXEC))
b32967ff 1889 goto out;
b32967ff
MG
1890
1891 /*
1892 * Rate-limit the amount of data that is being migrated to a node.
1893 * Optimal placement is no good if the memory bus is saturated and
1894 * all the time is being spent migrating!
1895 */
340ef390 1896 if (numamigrate_update_ratelimit(pgdat, 1))
b32967ff 1897 goto out;
b32967ff
MG
1898
1899 isolated = numamigrate_isolate_page(pgdat, page);
1900 if (!isolated)
1901 goto out;
1902
1903 list_add(&page->lru, &migratepages);
9c620e2b 1904 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
68711a74
DR
1905 NULL, node, MIGRATE_ASYNC,
1906 MR_NUMA_MISPLACED);
b32967ff 1907 if (nr_remaining) {
59c82b70
JK
1908 if (!list_empty(&migratepages)) {
1909 list_del(&page->lru);
599d0c95 1910 dec_node_page_state(page, NR_ISOLATED_ANON +
59c82b70
JK
1911 page_is_file_cache(page));
1912 putback_lru_page(page);
1913 }
b32967ff
MG
1914 isolated = 0;
1915 } else
1916 count_vm_numa_event(NUMA_PAGE_MIGRATE);
7039e1db 1917 BUG_ON(!list_empty(&migratepages));
7039e1db 1918 return isolated;
340ef390
HD
1919
1920out:
1921 put_page(page);
1922 return 0;
7039e1db 1923}
220018d3 1924#endif /* CONFIG_NUMA_BALANCING */
b32967ff 1925
220018d3 1926#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
340ef390
HD
1927/*
1928 * Migrates a THP to a given target node. page must be locked and is unlocked
1929 * before returning.
1930 */
b32967ff
MG
1931int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1932 struct vm_area_struct *vma,
1933 pmd_t *pmd, pmd_t entry,
1934 unsigned long address,
1935 struct page *page, int node)
1936{
c4088ebd 1937 spinlock_t *ptl;
b32967ff
MG
1938 pg_data_t *pgdat = NODE_DATA(node);
1939 int isolated = 0;
1940 struct page *new_page = NULL;
b32967ff 1941 int page_lru = page_is_file_cache(page);
f714f4f2
MG
1942 unsigned long mmun_start = address & HPAGE_PMD_MASK;
1943 unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
b32967ff 1944
b32967ff
MG
1945 /*
1946 * Rate-limit the amount of data that is being migrated to a node.
1947 * Optimal placement is no good if the memory bus is saturated and
1948 * all the time is being spent migrating!
1949 */
d28d4335 1950 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
b32967ff
MG
1951 goto out_dropref;
1952
1953 new_page = alloc_pages_node(node,
25160354 1954 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
e97ca8e5 1955 HPAGE_PMD_ORDER);
340ef390
HD
1956 if (!new_page)
1957 goto out_fail;
9a982250 1958 prep_transhuge_page(new_page);
340ef390 1959
b32967ff 1960 isolated = numamigrate_isolate_page(pgdat, page);
340ef390 1961 if (!isolated) {
b32967ff 1962 put_page(new_page);
340ef390 1963 goto out_fail;
b32967ff 1964 }
b0943d61 1965
b32967ff 1966 /* Prepare a page as a migration target */
48c935ad 1967 __SetPageLocked(new_page);
d44d363f
SL
1968 if (PageSwapBacked(page))
1969 __SetPageSwapBacked(new_page);
b32967ff
MG
1970
1971 /* anon mapping, we can simply copy page->mapping to the new page: */
1972 new_page->mapping = page->mapping;
1973 new_page->index = page->index;
1974 migrate_page_copy(new_page, page);
1975 WARN_ON(PageLRU(new_page));
1976
1977 /* Recheck the target PMD */
f714f4f2 1978 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
c4088ebd 1979 ptl = pmd_lock(mm, pmd);
f4e177d1 1980 if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
c4088ebd 1981 spin_unlock(ptl);
f714f4f2 1982 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
b32967ff
MG
1983
1984 /* Reverse changes made by migrate_page_copy() */
1985 if (TestClearPageActive(new_page))
1986 SetPageActive(page);
1987 if (TestClearPageUnevictable(new_page))
1988 SetPageUnevictable(page);
b32967ff
MG
1989
1990 unlock_page(new_page);
1991 put_page(new_page); /* Free it */
1992
a54a407f
MG
1993 /* Retake the callers reference and putback on LRU */
1994 get_page(page);
b32967ff 1995 putback_lru_page(page);
599d0c95 1996 mod_node_page_state(page_pgdat(page),
a54a407f 1997 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
eb4489f6
MG
1998
1999 goto out_unlock;
b32967ff
MG
2000 }
2001
10102459 2002 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
2b4847e7 2003 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
b32967ff 2004
2b4847e7
MG
2005 /*
2006 * Clear the old entry under pagetable lock and establish the new PTE.
2007 * Any parallel GUP will either observe the old page blocking on the
2008 * page lock, block on the page table lock or observe the new page.
2009 * The SetPageUptodate on the new page and page_add_new_anon_rmap
2010 * guarantee the copy is visible before the pagetable update.
2011 */
f714f4f2 2012 flush_cache_range(vma, mmun_start, mmun_end);
d281ee61 2013 page_add_anon_rmap(new_page, vma, mmun_start, true);
8809aa2d 2014 pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
f714f4f2 2015 set_pmd_at(mm, mmun_start, pmd, entry);
ce4a9cc5 2016 update_mmu_cache_pmd(vma, address, &entry);
2b4847e7 2017
f4e177d1 2018 page_ref_unfreeze(page, 2);
51afb12b 2019 mlock_migrate_page(new_page, page);
d281ee61 2020 page_remove_rmap(page, true);
7cd12b4a 2021 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
2b4847e7 2022
c4088ebd 2023 spin_unlock(ptl);
f714f4f2 2024 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
b32967ff 2025
11de9927
MG
2026 /* Take an "isolate" reference and put new page on the LRU. */
2027 get_page(new_page);
2028 putback_lru_page(new_page);
2029
b32967ff
MG
2030 unlock_page(new_page);
2031 unlock_page(page);
2032 put_page(page); /* Drop the rmap reference */
2033 put_page(page); /* Drop the LRU isolation reference */
2034
2035 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
2036 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
2037
599d0c95 2038 mod_node_page_state(page_pgdat(page),
b32967ff
MG
2039 NR_ISOLATED_ANON + page_lru,
2040 -HPAGE_PMD_NR);
2041 return isolated;
2042
340ef390
HD
2043out_fail:
2044 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
b32967ff 2045out_dropref:
2b4847e7
MG
2046 ptl = pmd_lock(mm, pmd);
2047 if (pmd_same(*pmd, entry)) {
4d942466 2048 entry = pmd_modify(entry, vma->vm_page_prot);
f714f4f2 2049 set_pmd_at(mm, mmun_start, pmd, entry);
2b4847e7
MG
2050 update_mmu_cache_pmd(vma, address, &entry);
2051 }
2052 spin_unlock(ptl);
a54a407f 2053
eb4489f6 2054out_unlock:
340ef390 2055 unlock_page(page);
b32967ff 2056 put_page(page);
b32967ff
MG
2057 return 0;
2058}
7039e1db
PZ
2059#endif /* CONFIG_NUMA_BALANCING */
2060
2061#endif /* CONFIG_NUMA */