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