squash mm: Export migrate_page_... : also make it non-static
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / migrate.c
CommitLineData
b20a3503
CL
1/*
2 * Memory Migration functionality - linux/mm/migration.c
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>
af110cc4 33#include <linux/backing-dev.h>
8a9f3ccd 34#include <linux/memcontrol.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>
b20a3503 40
0d1836c3
MN
41#include <asm/tlbflush.h>
42
7b2a2d4a
MG
43#define CREATE_TRACE_POINTS
44#include <trace/events/migrate.h>
45
b20a3503
CL
46#include "internal.h"
47
b20a3503 48/*
742755a1 49 * migrate_prep() needs to be called before we start compiling a list of pages
748446bb
MG
50 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
51 * undesirable, use migrate_prep_local()
b20a3503
CL
52 */
53int migrate_prep(void)
54{
b20a3503
CL
55 /*
56 * Clear the LRU lists so pages can be isolated.
57 * Note that pages may be moved off the LRU after we have
58 * drained them. Those pages will fail to migrate like other
59 * pages that may be busy.
60 */
61 lru_add_drain_all();
62
63 return 0;
64}
65
748446bb
MG
66/* Do the necessary work of migrate_prep but not if it involves other CPUs */
67int migrate_prep_local(void)
68{
69 lru_add_drain();
70
71 return 0;
72}
73
b20a3503 74/*
894bc310
LS
75 * Add isolated pages on the list back to the LRU under page lock
76 * to avoid leaking evictable pages back onto unevictable list.
b20a3503 77 */
e13861d8 78void putback_lru_pages(struct list_head *l)
b20a3503
CL
79{
80 struct page *page;
81 struct page *page2;
b20a3503 82
5733c7d1
RA
83 list_for_each_entry_safe(page, page2, l, lru) {
84 list_del(&page->lru);
85 dec_zone_page_state(page, NR_ISOLATED_ANON +
86 page_is_file_cache(page));
87 putback_lru_page(page);
88 }
89}
90
91/*
92 * Put previously isolated pages back onto the appropriate lists
93 * from where they were once taken off for compaction/migration.
94 *
95 * This function shall be used instead of putback_lru_pages(),
96 * whenever the isolated pageset has been built by isolate_migratepages_range()
97 */
98void putback_movable_pages(struct list_head *l)
99{
100 struct page *page;
101 struct page *page2;
102
b20a3503 103 list_for_each_entry_safe(page, page2, l, lru) {
e24f0b8f 104 list_del(&page->lru);
a731286d 105 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 106 page_is_file_cache(page));
009dfd44 107 if (unlikely(isolated_balloon_page(page)))
bf6bddf1
RA
108 balloon_page_putback(page);
109 else
110 putback_lru_page(page);
b20a3503 111 }
b20a3503
CL
112}
113
0697212a
CL
114/*
115 * Restore a potential migration pte to a working pte entry
116 */
e9995ef9
HD
117static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
118 unsigned long addr, void *old)
0697212a
CL
119{
120 struct mm_struct *mm = vma->vm_mm;
121 swp_entry_t entry;
0697212a
CL
122 pmd_t *pmd;
123 pte_t *ptep, pte;
124 spinlock_t *ptl;
125
290408d4
NH
126 if (unlikely(PageHuge(new))) {
127 ptep = huge_pte_offset(mm, addr);
128 if (!ptep)
129 goto out;
130 ptl = &mm->page_table_lock;
131 } else {
6219049a
BL
132 pmd = mm_find_pmd(mm, addr);
133 if (!pmd)
290408d4 134 goto out;
500d65d4
AA
135 if (pmd_trans_huge(*pmd))
136 goto out;
0697212a 137
290408d4 138 ptep = pte_offset_map(pmd, addr);
0697212a 139
486cf46f
HD
140 /*
141 * Peek to check is_swap_pte() before taking ptlock? No, we
142 * can race mremap's move_ptes(), which skips anon_vma lock.
143 */
290408d4
NH
144
145 ptl = pte_lockptr(mm, pmd);
146 }
0697212a 147
0697212a
CL
148 spin_lock(ptl);
149 pte = *ptep;
150 if (!is_swap_pte(pte))
e9995ef9 151 goto unlock;
0697212a
CL
152
153 entry = pte_to_swp_entry(pte);
154
e9995ef9
HD
155 if (!is_migration_entry(entry) ||
156 migration_entry_to_page(entry) != old)
157 goto unlock;
0697212a 158
0697212a
CL
159 get_page(new);
160 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
161 if (is_write_migration_entry(entry))
162 pte = pte_mkwrite(pte);
3ef8fd7f 163#ifdef CONFIG_HUGETLB_PAGE
be7517d6 164 if (PageHuge(new)) {
290408d4 165 pte = pte_mkhuge(pte);
be7517d6
TL
166 pte = arch_make_huge_pte(pte, vma, new, 0);
167 }
3ef8fd7f 168#endif
c2cc499c 169 flush_dcache_page(new);
0697212a 170 set_pte_at(mm, addr, ptep, pte);
04e62a29 171
290408d4
NH
172 if (PageHuge(new)) {
173 if (PageAnon(new))
174 hugepage_add_anon_rmap(new, vma, addr);
175 else
176 page_dup_rmap(new);
177 } else if (PageAnon(new))
04e62a29
CL
178 page_add_anon_rmap(new, vma, addr);
179 else
180 page_add_file_rmap(new);
181
182 /* No need to invalidate - it was non-present before */
4b3073e1 183 update_mmu_cache(vma, addr, ptep);
e9995ef9 184unlock:
0697212a 185 pte_unmap_unlock(ptep, ptl);
e9995ef9
HD
186out:
187 return SWAP_AGAIN;
0697212a
CL
188}
189
04e62a29
CL
190/*
191 * Get rid of all migration entries and replace them by
192 * references to the indicated page.
193 */
194static void remove_migration_ptes(struct page *old, struct page *new)
195{
e9995ef9 196 rmap_walk(new, remove_migration_pte, old);
04e62a29
CL
197}
198
0697212a
CL
199/*
200 * Something used the pte of a page under migration. We need to
201 * get to the page and wait until migration is finished.
202 * When we return from this function the fault will be retried.
0697212a 203 */
30dad309
NH
204static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
205 spinlock_t *ptl)
0697212a 206{
30dad309 207 pte_t pte;
0697212a
CL
208 swp_entry_t entry;
209 struct page *page;
210
30dad309 211 spin_lock(ptl);
0697212a
CL
212 pte = *ptep;
213 if (!is_swap_pte(pte))
214 goto out;
215
216 entry = pte_to_swp_entry(pte);
217 if (!is_migration_entry(entry))
218 goto out;
219
220 page = migration_entry_to_page(entry);
221
e286781d
NP
222 /*
223 * Once radix-tree replacement of page migration started, page_count
224 * *must* be zero. And, we don't want to call wait_on_page_locked()
225 * against a page without get_page().
226 * So, we use get_page_unless_zero(), here. Even failed, page fault
227 * will occur again.
228 */
229 if (!get_page_unless_zero(page))
230 goto out;
0697212a
CL
231 pte_unmap_unlock(ptep, ptl);
232 wait_on_page_locked(page);
233 put_page(page);
234 return;
235out:
236 pte_unmap_unlock(ptep, ptl);
237}
238
30dad309
NH
239void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
240 unsigned long address)
241{
242 spinlock_t *ptl = pte_lockptr(mm, pmd);
243 pte_t *ptep = pte_offset_map(pmd, address);
244 __migration_entry_wait(mm, ptep, ptl);
245}
246
247void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
248{
249 spinlock_t *ptl = &(mm)->page_table_lock;
250 __migration_entry_wait(mm, pte, ptl);
251}
252
b969c4ab
MG
253#ifdef CONFIG_BLOCK
254/* Returns true if all buffers are successfully locked */
a6bc32b8
MG
255static bool buffer_migrate_lock_buffers(struct buffer_head *head,
256 enum migrate_mode mode)
b969c4ab
MG
257{
258 struct buffer_head *bh = head;
259
260 /* Simple case, sync compaction */
a6bc32b8 261 if (mode != MIGRATE_ASYNC) {
b969c4ab
MG
262 do {
263 get_bh(bh);
264 lock_buffer(bh);
265 bh = bh->b_this_page;
266
267 } while (bh != head);
268
269 return true;
270 }
271
272 /* async case, we cannot block on lock_buffer so use trylock_buffer */
273 do {
274 get_bh(bh);
275 if (!trylock_buffer(bh)) {
276 /*
277 * We failed to lock the buffer and cannot stall in
278 * async migration. Release the taken locks
279 */
280 struct buffer_head *failed_bh = bh;
281 put_bh(failed_bh);
282 bh = head;
283 while (bh != failed_bh) {
284 unlock_buffer(bh);
285 put_bh(bh);
286 bh = bh->b_this_page;
287 }
288 return false;
289 }
290
291 bh = bh->b_this_page;
292 } while (bh != head);
293 return true;
294}
295#else
296static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
a6bc32b8 297 enum migrate_mode mode)
b969c4ab
MG
298{
299 return true;
300}
301#endif /* CONFIG_BLOCK */
302
b20a3503 303/*
c3fcf8a5 304 * Replace the page in the mapping.
5b5c7120
CL
305 *
306 * The number of remaining references must be:
307 * 1 for anonymous pages without a mapping
308 * 2 for pages with a mapping
266cf658 309 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
b20a3503 310 */
f7ed93fe 311int migrate_page_move_mapping(struct address_space *mapping,
b969c4ab 312 struct page *newpage, struct page *page,
a6bc32b8 313 struct buffer_head *head, enum migrate_mode mode)
b20a3503 314{
af110cc4
HD
315 struct zone *oldzone, *newzone;
316 int dirty;
7039e1db 317 int expected_count = 0;
7cf9c2c7 318 void **pslot;
b20a3503 319
6c5240ae 320 if (!mapping) {
0e8c7d0f 321 /* Anonymous page without mapping */
6c5240ae
CL
322 if (page_count(page) != 1)
323 return -EAGAIN;
78bd5209 324 return MIGRATEPAGE_SUCCESS;
6c5240ae
CL
325 }
326
af110cc4
HD
327 oldzone = page_zone(page);
328 newzone = page_zone(newpage);
329
19fd6231 330 spin_lock_irq(&mapping->tree_lock);
b20a3503 331
7cf9c2c7
NP
332 pslot = radix_tree_lookup_slot(&mapping->page_tree,
333 page_index(page));
b20a3503 334
edcf4748 335 expected_count = 2 + page_has_private(page);
e286781d 336 if (page_count(page) != expected_count ||
29c1f677 337 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
19fd6231 338 spin_unlock_irq(&mapping->tree_lock);
e23ca00b 339 return -EAGAIN;
b20a3503
CL
340 }
341
e286781d 342 if (!page_freeze_refs(page, expected_count)) {
19fd6231 343 spin_unlock_irq(&mapping->tree_lock);
e286781d
NP
344 return -EAGAIN;
345 }
346
b969c4ab
MG
347 /*
348 * In the async migration case of moving a page with buffers, lock the
349 * buffers using trylock before the mapping is moved. If the mapping
350 * was moved, we later failed to lock the buffers and could not move
351 * the mapping back due to an elevated page count, we would have to
352 * block waiting on other references to be dropped.
353 */
a6bc32b8
MG
354 if (mode == MIGRATE_ASYNC && head &&
355 !buffer_migrate_lock_buffers(head, mode)) {
b969c4ab
MG
356 page_unfreeze_refs(page, expected_count);
357 spin_unlock_irq(&mapping->tree_lock);
358 return -EAGAIN;
359 }
360
b20a3503
CL
361 /*
362 * Now we know that no one else is looking at the page.
b20a3503 363 */
7cf9c2c7 364 get_page(newpage); /* add cache reference */
b20a3503
CL
365 if (PageSwapCache(page)) {
366 SetPageSwapCache(newpage);
367 set_page_private(newpage, page_private(page));
368 }
369
af110cc4
HD
370 /* Move dirty while page refs frozen and newpage not yet exposed */
371 dirty = PageDirty(page);
372 if (dirty) {
373 ClearPageDirty(page);
374 SetPageDirty(newpage);
375 }
376
7cf9c2c7
NP
377 radix_tree_replace_slot(pslot, newpage);
378
379 /*
937a94c9
JG
380 * Drop cache reference from old page by unfreezing
381 * to one less reference.
7cf9c2c7
NP
382 * We know this isn't the last reference.
383 */
937a94c9 384 page_unfreeze_refs(page, expected_count - 1);
7cf9c2c7 385
af110cc4
HD
386 spin_unlock(&mapping->tree_lock);
387 /* Leave irq disabled to prevent preemption while updating stats */
388
0e8c7d0f
CL
389 /*
390 * If moved to a different zone then also account
391 * the page for that zone. Other VM counters will be
392 * taken care of when we establish references to the
393 * new page and drop references to the old page.
394 *
395 * Note that anonymous pages are accounted for
396 * via NR_FILE_PAGES and NR_ANON_PAGES if they
397 * are mapped to swap space.
398 */
af110cc4
HD
399 if (newzone != oldzone) {
400 __dec_zone_state(oldzone, NR_FILE_PAGES);
401 __inc_zone_state(newzone, NR_FILE_PAGES);
402 if (PageSwapBacked(page) && !PageSwapCache(page)) {
403 __dec_zone_state(oldzone, NR_SHMEM);
404 __inc_zone_state(newzone, NR_SHMEM);
405 }
406 if (dirty && mapping_cap_account_dirty(mapping)) {
407 __dec_zone_state(oldzone, NR_FILE_DIRTY);
408 __inc_zone_state(newzone, NR_FILE_DIRTY);
409 }
4b02108a 410 }
af110cc4 411 local_irq_enable();
b20a3503 412
78bd5209 413 return MIGRATEPAGE_SUCCESS;
b20a3503 414}
31f47651 415EXPORT_SYMBOL(migrate_page_move_mapping);
b20a3503 416
290408d4
NH
417/*
418 * The expected number of remaining references is the same as that
419 * of migrate_page_move_mapping().
420 */
421int migrate_huge_page_move_mapping(struct address_space *mapping,
422 struct page *newpage, struct page *page)
423{
424 int expected_count;
425 void **pslot;
426
427 if (!mapping) {
428 if (page_count(page) != 1)
429 return -EAGAIN;
78bd5209 430 return MIGRATEPAGE_SUCCESS;
290408d4
NH
431 }
432
433 spin_lock_irq(&mapping->tree_lock);
434
435 pslot = radix_tree_lookup_slot(&mapping->page_tree,
436 page_index(page));
437
438 expected_count = 2 + page_has_private(page);
439 if (page_count(page) != expected_count ||
29c1f677 440 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
290408d4
NH
441 spin_unlock_irq(&mapping->tree_lock);
442 return -EAGAIN;
443 }
444
445 if (!page_freeze_refs(page, expected_count)) {
446 spin_unlock_irq(&mapping->tree_lock);
447 return -EAGAIN;
448 }
449
450 get_page(newpage);
451
452 radix_tree_replace_slot(pslot, newpage);
453
937a94c9 454 page_unfreeze_refs(page, expected_count - 1);
290408d4
NH
455
456 spin_unlock_irq(&mapping->tree_lock);
78bd5209 457 return MIGRATEPAGE_SUCCESS;
290408d4
NH
458}
459
b20a3503
CL
460/*
461 * Copy the page to its new location
462 */
290408d4 463void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503 464{
b32967ff 465 if (PageHuge(page) || PageTransHuge(page))
290408d4
NH
466 copy_huge_page(newpage, page);
467 else
468 copy_highpage(newpage, page);
b20a3503
CL
469
470 if (PageError(page))
471 SetPageError(newpage);
472 if (PageReferenced(page))
473 SetPageReferenced(newpage);
474 if (PageUptodate(page))
475 SetPageUptodate(newpage);
894bc310
LS
476 if (TestClearPageActive(page)) {
477 VM_BUG_ON(PageUnevictable(page));
b20a3503 478 SetPageActive(newpage);
418b27ef
LS
479 } else if (TestClearPageUnevictable(page))
480 SetPageUnevictable(newpage);
b20a3503
CL
481 if (PageChecked(page))
482 SetPageChecked(newpage);
483 if (PageMappedToDisk(page))
484 SetPageMappedToDisk(newpage);
485
af110cc4
HD
486 /* Move dirty on pages not done by migrate_page_move_mapping() */
487 if (PageDirty(page))
488 SetPageDirty(newpage);
b20a3503 489
b291f000 490 mlock_migrate_page(newpage, page);
e9995ef9 491 ksm_migrate_page(newpage, page);
c8d6553b
HD
492 /*
493 * Please do not reorder this without considering how mm/ksm.c's
494 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
495 */
b20a3503 496 ClearPageSwapCache(page);
b20a3503
CL
497 ClearPagePrivate(page);
498 set_page_private(page, 0);
b20a3503
CL
499
500 /*
501 * If any waiters have accumulated on the new page then
502 * wake them up.
503 */
504 if (PageWriteback(newpage))
505 end_page_writeback(newpage);
506}
31f47651 507EXPORT_SYMBOL(migrate_page_copy);
b20a3503 508
1d8b85cc
CL
509/************************************************************
510 * Migration functions
511 ***********************************************************/
512
513/* Always fail migration. Used for mappings that are not movable */
2d1db3b1
CL
514int fail_migrate_page(struct address_space *mapping,
515 struct page *newpage, struct page *page)
1d8b85cc
CL
516{
517 return -EIO;
518}
519EXPORT_SYMBOL(fail_migrate_page);
520
b20a3503
CL
521/*
522 * Common logic to directly migrate a single page suitable for
266cf658 523 * pages that do not use PagePrivate/PagePrivate2.
b20a3503
CL
524 *
525 * Pages are locked upon entry and exit.
526 */
2d1db3b1 527int migrate_page(struct address_space *mapping,
a6bc32b8
MG
528 struct page *newpage, struct page *page,
529 enum migrate_mode mode)
b20a3503
CL
530{
531 int rc;
532
533 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
534
a6bc32b8 535 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
b20a3503 536
78bd5209 537 if (rc != MIGRATEPAGE_SUCCESS)
b20a3503
CL
538 return rc;
539
540 migrate_page_copy(newpage, page);
78bd5209 541 return MIGRATEPAGE_SUCCESS;
b20a3503
CL
542}
543EXPORT_SYMBOL(migrate_page);
544
9361401e 545#ifdef CONFIG_BLOCK
1d8b85cc
CL
546/*
547 * Migration function for pages with buffers. This function can only be used
548 * if the underlying filesystem guarantees that no other references to "page"
549 * exist.
550 */
2d1db3b1 551int buffer_migrate_page(struct address_space *mapping,
a6bc32b8 552 struct page *newpage, struct page *page, enum migrate_mode mode)
1d8b85cc 553{
1d8b85cc
CL
554 struct buffer_head *bh, *head;
555 int rc;
556
1d8b85cc 557 if (!page_has_buffers(page))
a6bc32b8 558 return migrate_page(mapping, newpage, page, mode);
1d8b85cc
CL
559
560 head = page_buffers(page);
561
a6bc32b8 562 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
1d8b85cc 563
78bd5209 564 if (rc != MIGRATEPAGE_SUCCESS)
1d8b85cc
CL
565 return rc;
566
b969c4ab
MG
567 /*
568 * In the async case, migrate_page_move_mapping locked the buffers
569 * with an IRQ-safe spinlock held. In the sync case, the buffers
570 * need to be locked now
571 */
a6bc32b8
MG
572 if (mode != MIGRATE_ASYNC)
573 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
1d8b85cc
CL
574
575 ClearPagePrivate(page);
576 set_page_private(newpage, page_private(page));
577 set_page_private(page, 0);
578 put_page(page);
579 get_page(newpage);
580
581 bh = head;
582 do {
583 set_bh_page(bh, newpage, bh_offset(bh));
584 bh = bh->b_this_page;
585
586 } while (bh != head);
587
588 SetPagePrivate(newpage);
589
590 migrate_page_copy(newpage, page);
591
592 bh = head;
593 do {
594 unlock_buffer(bh);
595 put_bh(bh);
596 bh = bh->b_this_page;
597
598 } while (bh != head);
599
78bd5209 600 return MIGRATEPAGE_SUCCESS;
1d8b85cc
CL
601}
602EXPORT_SYMBOL(buffer_migrate_page);
9361401e 603#endif
1d8b85cc 604
04e62a29
CL
605/*
606 * Writeback a page to clean the dirty state
607 */
608static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 609{
04e62a29
CL
610 struct writeback_control wbc = {
611 .sync_mode = WB_SYNC_NONE,
612 .nr_to_write = 1,
613 .range_start = 0,
614 .range_end = LLONG_MAX,
04e62a29
CL
615 .for_reclaim = 1
616 };
617 int rc;
618
619 if (!mapping->a_ops->writepage)
620 /* No write method for the address space */
621 return -EINVAL;
622
623 if (!clear_page_dirty_for_io(page))
624 /* Someone else already triggered a write */
625 return -EAGAIN;
626
8351a6e4 627 /*
04e62a29
CL
628 * A dirty page may imply that the underlying filesystem has
629 * the page on some queue. So the page must be clean for
630 * migration. Writeout may mean we loose the lock and the
631 * page state is no longer what we checked for earlier.
632 * At this point we know that the migration attempt cannot
633 * be successful.
8351a6e4 634 */
04e62a29 635 remove_migration_ptes(page, page);
8351a6e4 636
04e62a29 637 rc = mapping->a_ops->writepage(page, &wbc);
8351a6e4 638
04e62a29
CL
639 if (rc != AOP_WRITEPAGE_ACTIVATE)
640 /* unlocked. Relock */
641 lock_page(page);
642
bda8550d 643 return (rc < 0) ? -EIO : -EAGAIN;
04e62a29
CL
644}
645
646/*
647 * Default handling if a filesystem does not provide a migration function.
648 */
649static int fallback_migrate_page(struct address_space *mapping,
a6bc32b8 650 struct page *newpage, struct page *page, enum migrate_mode mode)
04e62a29 651{
b969c4ab 652 if (PageDirty(page)) {
a6bc32b8
MG
653 /* Only writeback pages in full synchronous migration */
654 if (mode != MIGRATE_SYNC)
b969c4ab 655 return -EBUSY;
04e62a29 656 return writeout(mapping, page);
b969c4ab 657 }
8351a6e4
CL
658
659 /*
660 * Buffers may be managed in a filesystem specific way.
661 * We must have no buffers or drop them.
662 */
266cf658 663 if (page_has_private(page) &&
8351a6e4
CL
664 !try_to_release_page(page, GFP_KERNEL))
665 return -EAGAIN;
666
a6bc32b8 667 return migrate_page(mapping, newpage, page, mode);
8351a6e4
CL
668}
669
e24f0b8f
CL
670/*
671 * Move a page to a newly allocated page
672 * The page is locked and all ptes have been successfully removed.
673 *
674 * The new page will have replaced the old page if this function
675 * is successful.
894bc310
LS
676 *
677 * Return value:
678 * < 0 - error code
78bd5209 679 * MIGRATEPAGE_SUCCESS - success
e24f0b8f 680 */
3fe2011f 681static int move_to_new_page(struct page *newpage, struct page *page,
a6bc32b8 682 int remap_swapcache, enum migrate_mode mode)
e24f0b8f
CL
683{
684 struct address_space *mapping;
685 int rc;
686
687 /*
688 * Block others from accessing the page when we get around to
689 * establishing additional references. We are the only one
690 * holding a reference to the new page at this point.
691 */
529ae9aa 692 if (!trylock_page(newpage))
e24f0b8f
CL
693 BUG();
694
695 /* Prepare mapping for the new page.*/
696 newpage->index = page->index;
697 newpage->mapping = page->mapping;
b2e18538
RR
698 if (PageSwapBacked(page))
699 SetPageSwapBacked(newpage);
e24f0b8f
CL
700
701 mapping = page_mapping(page);
702 if (!mapping)
a6bc32b8 703 rc = migrate_page(mapping, newpage, page, mode);
b969c4ab 704 else if (mapping->a_ops->migratepage)
e24f0b8f 705 /*
b969c4ab
MG
706 * Most pages have a mapping and most filesystems provide a
707 * migratepage callback. Anonymous pages are part of swap
708 * space which also has its own migratepage callback. This
709 * is the most common path for page migration.
e24f0b8f 710 */
b969c4ab 711 rc = mapping->a_ops->migratepage(mapping,
a6bc32b8 712 newpage, page, mode);
b969c4ab 713 else
a6bc32b8 714 rc = fallback_migrate_page(mapping, newpage, page, mode);
e24f0b8f 715
78bd5209 716 if (rc != MIGRATEPAGE_SUCCESS) {
e24f0b8f 717 newpage->mapping = NULL;
3fe2011f
MG
718 } else {
719 if (remap_swapcache)
720 remove_migration_ptes(page, newpage);
35512eca 721 page->mapping = NULL;
3fe2011f 722 }
e24f0b8f
CL
723
724 unlock_page(newpage);
725
726 return rc;
727}
728
0dabec93 729static int __unmap_and_move(struct page *page, struct page *newpage,
9c620e2b 730 int force, enum migrate_mode mode)
e24f0b8f 731{
0dabec93 732 int rc = -EAGAIN;
3fe2011f 733 int remap_swapcache = 1;
56039efa 734 struct mem_cgroup *mem;
3f6c8272 735 struct anon_vma *anon_vma = NULL;
95a402c3 736
529ae9aa 737 if (!trylock_page(page)) {
a6bc32b8 738 if (!force || mode == MIGRATE_ASYNC)
0dabec93 739 goto out;
3e7d3449
MG
740
741 /*
742 * It's not safe for direct compaction to call lock_page.
743 * For example, during page readahead pages are added locked
744 * to the LRU. Later, when the IO completes the pages are
745 * marked uptodate and unlocked. However, the queueing
746 * could be merging multiple pages for one bio (e.g.
747 * mpage_readpages). If an allocation happens for the
748 * second or third page, the process can end up locking
749 * the same page twice and deadlocking. Rather than
750 * trying to be clever about what pages can be locked,
751 * avoid the use of lock_page for direct compaction
752 * altogether.
753 */
754 if (current->flags & PF_MEMALLOC)
0dabec93 755 goto out;
3e7d3449 756
e24f0b8f
CL
757 lock_page(page);
758 }
759
01b1ae63 760 /* charge against new page */
0030f535 761 mem_cgroup_prepare_migration(page, newpage, &mem);
01b1ae63 762
e24f0b8f 763 if (PageWriteback(page)) {
11bc82d6 764 /*
fed5b64a 765 * Only in the case of a full synchronous migration is it
a6bc32b8
MG
766 * necessary to wait for PageWriteback. In the async case,
767 * the retry loop is too short and in the sync-light case,
768 * the overhead of stalling is too much
11bc82d6 769 */
a6bc32b8 770 if (mode != MIGRATE_SYNC) {
11bc82d6
AA
771 rc = -EBUSY;
772 goto uncharge;
773 }
774 if (!force)
01b1ae63 775 goto uncharge;
e24f0b8f
CL
776 wait_on_page_writeback(page);
777 }
e24f0b8f 778 /*
dc386d4d
KH
779 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
780 * we cannot notice that anon_vma is freed while we migrates a page.
1ce82b69 781 * This get_anon_vma() delays freeing anon_vma pointer until the end
dc386d4d 782 * of migration. File cache pages are no problem because of page_lock()
989f89c5
KH
783 * File Caches may use write_page() or lock_page() in migration, then,
784 * just care Anon page here.
dc386d4d 785 */
b79bc0a0 786 if (PageAnon(page) && !PageKsm(page)) {
1ce82b69 787 /*
4fc3f1d6 788 * Only page_lock_anon_vma_read() understands the subtleties of
1ce82b69
HD
789 * getting a hold on an anon_vma from outside one of its mms.
790 */
746b18d4 791 anon_vma = page_get_anon_vma(page);
1ce82b69
HD
792 if (anon_vma) {
793 /*
746b18d4 794 * Anon page
1ce82b69 795 */
1ce82b69 796 } else if (PageSwapCache(page)) {
3fe2011f
MG
797 /*
798 * We cannot be sure that the anon_vma of an unmapped
799 * swapcache page is safe to use because we don't
800 * know in advance if the VMA that this page belonged
801 * to still exists. If the VMA and others sharing the
802 * data have been freed, then the anon_vma could
803 * already be invalid.
804 *
805 * To avoid this possibility, swapcache pages get
806 * migrated but are not remapped when migration
807 * completes
808 */
809 remap_swapcache = 0;
810 } else {
1ce82b69 811 goto uncharge;
3fe2011f 812 }
989f89c5 813 }
62e1c553 814
bf6bddf1
RA
815 if (unlikely(balloon_page_movable(page))) {
816 /*
817 * A ballooned page does not need any special attention from
818 * physical to virtual reverse mapping procedures.
819 * Skip any attempt to unmap PTEs or to remap swap cache,
820 * in order to avoid burning cycles at rmap level, and perform
821 * the page migration right away (proteced by page lock).
822 */
823 rc = balloon_page_migrate(newpage, page, mode);
824 goto uncharge;
825 }
826
dc386d4d 827 /*
62e1c553
SL
828 * Corner case handling:
829 * 1. When a new swap-cache page is read into, it is added to the LRU
830 * and treated as swapcache but it has no rmap yet.
831 * Calling try_to_unmap() against a page->mapping==NULL page will
832 * trigger a BUG. So handle it here.
833 * 2. An orphaned page (see truncate_complete_page) might have
834 * fs-private metadata. The page can be picked up due to memory
835 * offlining. Everywhere else except page reclaim, the page is
836 * invisible to the vm, so the page can not be migrated. So try to
837 * free the metadata, so the page can be freed.
e24f0b8f 838 */
62e1c553 839 if (!page->mapping) {
1ce82b69
HD
840 VM_BUG_ON(PageAnon(page));
841 if (page_has_private(page)) {
62e1c553 842 try_to_free_buffers(page);
1ce82b69 843 goto uncharge;
62e1c553 844 }
abfc3488 845 goto skip_unmap;
62e1c553
SL
846 }
847
dc386d4d 848 /* Establish migration ptes or remove ptes */
14fa31b8 849 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
dc386d4d 850
abfc3488 851skip_unmap:
e6a1530d 852 if (!page_mapped(page))
a6bc32b8 853 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
e24f0b8f 854
3fe2011f 855 if (rc && remap_swapcache)
e24f0b8f 856 remove_migration_ptes(page, page);
3f6c8272
MG
857
858 /* Drop an anon_vma reference if we took one */
76545066 859 if (anon_vma)
9e60109f 860 put_anon_vma(anon_vma);
3f6c8272 861
01b1ae63 862uncharge:
bf6bddf1
RA
863 mem_cgroup_end_migration(mem, page, newpage,
864 (rc == MIGRATEPAGE_SUCCESS ||
865 rc == MIGRATEPAGE_BALLOON_SUCCESS));
e24f0b8f 866 unlock_page(page);
0dabec93
MK
867out:
868 return rc;
869}
95a402c3 870
0dabec93
MK
871/*
872 * Obtain the lock on page, remove all ptes and migrate the page
873 * to the newly allocated page in newpage.
874 */
875static int unmap_and_move(new_page_t get_new_page, unsigned long private,
9c620e2b 876 struct page *page, int force, enum migrate_mode mode)
0dabec93
MK
877{
878 int rc = 0;
879 int *result = NULL;
880 struct page *newpage = get_new_page(page, private, &result);
881
882 if (!newpage)
883 return -ENOMEM;
884
885 if (page_count(page) == 1) {
886 /* page was freed from under us. So we are done. */
887 goto out;
888 }
889
890 if (unlikely(PageTransHuge(page)))
891 if (unlikely(split_huge_page(page)))
892 goto out;
893
9c620e2b 894 rc = __unmap_and_move(page, newpage, force, mode);
bf6bddf1
RA
895
896 if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
897 /*
898 * A ballooned page has been migrated already.
899 * Now, it's the time to wrap-up counters,
900 * handle the page back to Buddy and return.
901 */
902 dec_zone_page_state(page, NR_ISOLATED_ANON +
903 page_is_file_cache(page));
904 balloon_page_free(page);
905 return MIGRATEPAGE_SUCCESS;
906 }
0dabec93 907out:
e24f0b8f 908 if (rc != -EAGAIN) {
0dabec93
MK
909 /*
910 * A page that has been migrated has all references
911 * removed and will be freed. A page that has not been
912 * migrated will have kepts its references and be
913 * restored.
914 */
915 list_del(&page->lru);
a731286d 916 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 917 page_is_file_cache(page));
894bc310 918 putback_lru_page(page);
e24f0b8f 919 }
95a402c3
CL
920 /*
921 * Move the new page to the LRU. If migration was not successful
922 * then this will free the page.
923 */
894bc310 924 putback_lru_page(newpage);
742755a1
CL
925 if (result) {
926 if (rc)
927 *result = rc;
928 else
929 *result = page_to_nid(newpage);
930 }
e24f0b8f
CL
931 return rc;
932}
933
290408d4
NH
934/*
935 * Counterpart of unmap_and_move_page() for hugepage migration.
936 *
937 * This function doesn't wait the completion of hugepage I/O
938 * because there is no race between I/O and migration for hugepage.
939 * Note that currently hugepage I/O occurs only in direct I/O
940 * where no lock is held and PG_writeback is irrelevant,
941 * and writeback status of all subpages are counted in the reference
942 * count of the head page (i.e. if all subpages of a 2MB hugepage are
943 * under direct I/O, the reference of the head page is 512 and a bit more.)
944 * This means that when we try to migrate hugepage whose subpages are
945 * doing direct I/O, some references remain after try_to_unmap() and
946 * hugepage migration fails without data corruption.
947 *
948 * There is also no race when direct I/O is issued on the page under migration,
949 * because then pte is replaced with migration swap entry and direct I/O code
950 * will wait in the page fault for migration to complete.
951 */
952static int unmap_and_move_huge_page(new_page_t get_new_page,
953 unsigned long private, struct page *hpage,
9c620e2b 954 int force, enum migrate_mode mode)
290408d4
NH
955{
956 int rc = 0;
957 int *result = NULL;
958 struct page *new_hpage = get_new_page(hpage, private, &result);
290408d4
NH
959 struct anon_vma *anon_vma = NULL;
960
961 if (!new_hpage)
962 return -ENOMEM;
963
964 rc = -EAGAIN;
965
966 if (!trylock_page(hpage)) {
a6bc32b8 967 if (!force || mode != MIGRATE_SYNC)
290408d4
NH
968 goto out;
969 lock_page(hpage);
970 }
971
746b18d4
PZ
972 if (PageAnon(hpage))
973 anon_vma = page_get_anon_vma(hpage);
290408d4
NH
974
975 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
976
977 if (!page_mapped(hpage))
a6bc32b8 978 rc = move_to_new_page(new_hpage, hpage, 1, mode);
290408d4
NH
979
980 if (rc)
981 remove_migration_ptes(hpage, hpage);
982
fd4a4663 983 if (anon_vma)
9e60109f 984 put_anon_vma(anon_vma);
8e6ac7fa
AK
985
986 if (!rc)
987 hugetlb_cgroup_migrate(hpage, new_hpage);
988
290408d4 989 unlock_page(hpage);
09761333 990out:
290408d4 991 put_page(new_hpage);
290408d4
NH
992 if (result) {
993 if (rc)
994 *result = rc;
995 else
996 *result = page_to_nid(new_hpage);
997 }
998 return rc;
999}
1000
b20a3503 1001/*
c73e5c9c
SB
1002 * migrate_pages - migrate the pages specified in a list, to the free pages
1003 * supplied as the target for the page migration
b20a3503 1004 *
c73e5c9c
SB
1005 * @from: The list of pages to be migrated.
1006 * @get_new_page: The function used to allocate free pages to be used
1007 * as the target of the page migration.
1008 * @private: Private data to be passed on to get_new_page()
1009 * @mode: The migration mode that specifies the constraints for
1010 * page migration, if any.
1011 * @reason: The reason for page migration.
b20a3503 1012 *
c73e5c9c
SB
1013 * The function returns after 10 attempts or if no pages are movable any more
1014 * because the list has become empty or no retryable pages exist any more.
1015 * The caller should call putback_lru_pages() to return pages to the LRU
28bd6578 1016 * or free list only if ret != 0.
b20a3503 1017 *
c73e5c9c 1018 * Returns the number of pages that were not migrated, or an error code.
b20a3503 1019 */
9c620e2b
HD
1020int migrate_pages(struct list_head *from, new_page_t get_new_page,
1021 unsigned long private, enum migrate_mode mode, int reason)
b20a3503 1022{
e24f0b8f 1023 int retry = 1;
b20a3503 1024 int nr_failed = 0;
5647bc29 1025 int nr_succeeded = 0;
b20a3503
CL
1026 int pass = 0;
1027 struct page *page;
1028 struct page *page2;
1029 int swapwrite = current->flags & PF_SWAPWRITE;
1030 int rc;
1031
1032 if (!swapwrite)
1033 current->flags |= PF_SWAPWRITE;
1034
e24f0b8f
CL
1035 for(pass = 0; pass < 10 && retry; pass++) {
1036 retry = 0;
b20a3503 1037
e24f0b8f 1038 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 1039 cond_resched();
2d1db3b1 1040
95a402c3 1041 rc = unmap_and_move(get_new_page, private,
9c620e2b 1042 page, pass > 2, mode);
2d1db3b1 1043
e24f0b8f 1044 switch(rc) {
95a402c3
CL
1045 case -ENOMEM:
1046 goto out;
e24f0b8f 1047 case -EAGAIN:
2d1db3b1 1048 retry++;
e24f0b8f 1049 break;
78bd5209 1050 case MIGRATEPAGE_SUCCESS:
5647bc29 1051 nr_succeeded++;
e24f0b8f
CL
1052 break;
1053 default:
2d1db3b1 1054 /* Permanent failure */
2d1db3b1 1055 nr_failed++;
e24f0b8f 1056 break;
2d1db3b1 1057 }
b20a3503
CL
1058 }
1059 }
78bd5209 1060 rc = nr_failed + retry;
95a402c3 1061out:
5647bc29
MG
1062 if (nr_succeeded)
1063 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1064 if (nr_failed)
1065 count_vm_events(PGMIGRATE_FAIL, nr_failed);
7b2a2d4a
MG
1066 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1067
b20a3503
CL
1068 if (!swapwrite)
1069 current->flags &= ~PF_SWAPWRITE;
1070
78bd5209 1071 return rc;
b20a3503 1072}
95a402c3 1073
189ebff2 1074int migrate_huge_page(struct page *hpage, new_page_t get_new_page,
9c620e2b 1075 unsigned long private, enum migrate_mode mode)
290408d4 1076{
189ebff2
AK
1077 int pass, rc;
1078
1079 for (pass = 0; pass < 10; pass++) {
9c620e2b
HD
1080 rc = unmap_and_move_huge_page(get_new_page, private,
1081 hpage, pass > 2, mode);
189ebff2
AK
1082 switch (rc) {
1083 case -ENOMEM:
1084 goto out;
1085 case -EAGAIN:
1086 /* try again */
290408d4 1087 cond_resched();
189ebff2 1088 break;
78bd5209 1089 case MIGRATEPAGE_SUCCESS:
189ebff2
AK
1090 goto out;
1091 default:
1092 rc = -EIO;
1093 goto out;
290408d4
NH
1094 }
1095 }
290408d4 1096out:
189ebff2 1097 return rc;
290408d4
NH
1098}
1099
742755a1
CL
1100#ifdef CONFIG_NUMA
1101/*
1102 * Move a list of individual pages
1103 */
1104struct page_to_node {
1105 unsigned long addr;
1106 struct page *page;
1107 int node;
1108 int status;
1109};
1110
1111static struct page *new_page_node(struct page *p, unsigned long private,
1112 int **result)
1113{
1114 struct page_to_node *pm = (struct page_to_node *)private;
1115
1116 while (pm->node != MAX_NUMNODES && pm->page != p)
1117 pm++;
1118
1119 if (pm->node == MAX_NUMNODES)
1120 return NULL;
1121
1122 *result = &pm->status;
1123
6484eb3e 1124 return alloc_pages_exact_node(pm->node,
769848c0 1125 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
742755a1
CL
1126}
1127
1128/*
1129 * Move a set of pages as indicated in the pm array. The addr
1130 * field must be set to the virtual address of the page to be moved
1131 * and the node number must contain a valid target node.
5e9a0f02 1132 * The pm array ends with node = MAX_NUMNODES.
742755a1 1133 */
5e9a0f02
BG
1134static int do_move_page_to_node_array(struct mm_struct *mm,
1135 struct page_to_node *pm,
1136 int migrate_all)
742755a1
CL
1137{
1138 int err;
1139 struct page_to_node *pp;
1140 LIST_HEAD(pagelist);
1141
1142 down_read(&mm->mmap_sem);
1143
1144 /*
1145 * Build a list of pages to migrate
1146 */
742755a1
CL
1147 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1148 struct vm_area_struct *vma;
1149 struct page *page;
1150
742755a1
CL
1151 err = -EFAULT;
1152 vma = find_vma(mm, pp->addr);
70384dc6 1153 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
742755a1
CL
1154 goto set_status;
1155
500d65d4 1156 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
89f5b7da
LT
1157
1158 err = PTR_ERR(page);
1159 if (IS_ERR(page))
1160 goto set_status;
1161
742755a1
CL
1162 err = -ENOENT;
1163 if (!page)
1164 goto set_status;
1165
62b61f61 1166 /* Use PageReserved to check for zero page */
b79bc0a0 1167 if (PageReserved(page))
742755a1
CL
1168 goto put_and_set;
1169
1170 pp->page = page;
1171 err = page_to_nid(page);
1172
1173 if (err == pp->node)
1174 /*
1175 * Node already in the right place
1176 */
1177 goto put_and_set;
1178
1179 err = -EACCES;
1180 if (page_mapcount(page) > 1 &&
1181 !migrate_all)
1182 goto put_and_set;
1183
62695a84 1184 err = isolate_lru_page(page);
6d9c285a 1185 if (!err) {
62695a84 1186 list_add_tail(&page->lru, &pagelist);
6d9c285a
KM
1187 inc_zone_page_state(page, NR_ISOLATED_ANON +
1188 page_is_file_cache(page));
1189 }
742755a1
CL
1190put_and_set:
1191 /*
1192 * Either remove the duplicate refcount from
1193 * isolate_lru_page() or drop the page ref if it was
1194 * not isolated.
1195 */
1196 put_page(page);
1197set_status:
1198 pp->status = err;
1199 }
1200
e78bbfa8 1201 err = 0;
cf608ac1 1202 if (!list_empty(&pagelist)) {
742755a1 1203 err = migrate_pages(&pagelist, new_page_node,
9c620e2b 1204 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
cf608ac1
MK
1205 if (err)
1206 putback_lru_pages(&pagelist);
1207 }
742755a1
CL
1208
1209 up_read(&mm->mmap_sem);
1210 return err;
1211}
1212
5e9a0f02
BG
1213/*
1214 * Migrate an array of page address onto an array of nodes and fill
1215 * the corresponding array of status.
1216 */
3268c63e 1217static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
5e9a0f02
BG
1218 unsigned long nr_pages,
1219 const void __user * __user *pages,
1220 const int __user *nodes,
1221 int __user *status, int flags)
1222{
3140a227 1223 struct page_to_node *pm;
3140a227
BG
1224 unsigned long chunk_nr_pages;
1225 unsigned long chunk_start;
1226 int err;
5e9a0f02 1227
3140a227
BG
1228 err = -ENOMEM;
1229 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1230 if (!pm)
5e9a0f02 1231 goto out;
35282a2d
BG
1232
1233 migrate_prep();
1234
5e9a0f02 1235 /*
3140a227
BG
1236 * Store a chunk of page_to_node array in a page,
1237 * but keep the last one as a marker
5e9a0f02 1238 */
3140a227 1239 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
5e9a0f02 1240
3140a227
BG
1241 for (chunk_start = 0;
1242 chunk_start < nr_pages;
1243 chunk_start += chunk_nr_pages) {
1244 int j;
5e9a0f02 1245
3140a227
BG
1246 if (chunk_start + chunk_nr_pages > nr_pages)
1247 chunk_nr_pages = nr_pages - chunk_start;
1248
1249 /* fill the chunk pm with addrs and nodes from user-space */
1250 for (j = 0; j < chunk_nr_pages; j++) {
1251 const void __user *p;
5e9a0f02
BG
1252 int node;
1253
3140a227
BG
1254 err = -EFAULT;
1255 if (get_user(p, pages + j + chunk_start))
1256 goto out_pm;
1257 pm[j].addr = (unsigned long) p;
1258
1259 if (get_user(node, nodes + j + chunk_start))
5e9a0f02
BG
1260 goto out_pm;
1261
1262 err = -ENODEV;
6f5a55f1
LT
1263 if (node < 0 || node >= MAX_NUMNODES)
1264 goto out_pm;
1265
389162c2 1266 if (!node_state(node, N_MEMORY))
5e9a0f02
BG
1267 goto out_pm;
1268
1269 err = -EACCES;
1270 if (!node_isset(node, task_nodes))
1271 goto out_pm;
1272
3140a227
BG
1273 pm[j].node = node;
1274 }
1275
1276 /* End marker for this chunk */
1277 pm[chunk_nr_pages].node = MAX_NUMNODES;
1278
1279 /* Migrate this chunk */
1280 err = do_move_page_to_node_array(mm, pm,
1281 flags & MPOL_MF_MOVE_ALL);
1282 if (err < 0)
1283 goto out_pm;
5e9a0f02 1284
5e9a0f02 1285 /* Return status information */
3140a227
BG
1286 for (j = 0; j < chunk_nr_pages; j++)
1287 if (put_user(pm[j].status, status + j + chunk_start)) {
5e9a0f02 1288 err = -EFAULT;
3140a227
BG
1289 goto out_pm;
1290 }
1291 }
1292 err = 0;
5e9a0f02
BG
1293
1294out_pm:
3140a227 1295 free_page((unsigned long)pm);
5e9a0f02
BG
1296out:
1297 return err;
1298}
1299
742755a1 1300/*
2f007e74 1301 * Determine the nodes of an array of pages and store it in an array of status.
742755a1 1302 */
80bba129
BG
1303static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1304 const void __user **pages, int *status)
742755a1 1305{
2f007e74 1306 unsigned long i;
2f007e74 1307
742755a1
CL
1308 down_read(&mm->mmap_sem);
1309
2f007e74 1310 for (i = 0; i < nr_pages; i++) {
80bba129 1311 unsigned long addr = (unsigned long)(*pages);
742755a1
CL
1312 struct vm_area_struct *vma;
1313 struct page *page;
c095adbc 1314 int err = -EFAULT;
2f007e74
BG
1315
1316 vma = find_vma(mm, addr);
70384dc6 1317 if (!vma || addr < vma->vm_start)
742755a1
CL
1318 goto set_status;
1319
2f007e74 1320 page = follow_page(vma, addr, 0);
89f5b7da
LT
1321
1322 err = PTR_ERR(page);
1323 if (IS_ERR(page))
1324 goto set_status;
1325
742755a1
CL
1326 err = -ENOENT;
1327 /* Use PageReserved to check for zero page */
b79bc0a0 1328 if (!page || PageReserved(page))
742755a1
CL
1329 goto set_status;
1330
1331 err = page_to_nid(page);
1332set_status:
80bba129
BG
1333 *status = err;
1334
1335 pages++;
1336 status++;
1337 }
1338
1339 up_read(&mm->mmap_sem);
1340}
1341
1342/*
1343 * Determine the nodes of a user array of pages and store it in
1344 * a user array of status.
1345 */
1346static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1347 const void __user * __user *pages,
1348 int __user *status)
1349{
1350#define DO_PAGES_STAT_CHUNK_NR 16
1351 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1352 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
80bba129 1353
87b8d1ad
PA
1354 while (nr_pages) {
1355 unsigned long chunk_nr;
80bba129 1356
87b8d1ad
PA
1357 chunk_nr = nr_pages;
1358 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1359 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1360
1361 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1362 break;
80bba129
BG
1363
1364 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1365
87b8d1ad
PA
1366 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1367 break;
742755a1 1368
87b8d1ad
PA
1369 pages += chunk_nr;
1370 status += chunk_nr;
1371 nr_pages -= chunk_nr;
1372 }
1373 return nr_pages ? -EFAULT : 0;
742755a1
CL
1374}
1375
1376/*
1377 * Move a list of pages in the address space of the currently executing
1378 * process.
1379 */
938bb9f5
HC
1380SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1381 const void __user * __user *, pages,
1382 const int __user *, nodes,
1383 int __user *, status, int, flags)
742755a1 1384{
c69e8d9c 1385 const struct cred *cred = current_cred(), *tcred;
742755a1 1386 struct task_struct *task;
742755a1 1387 struct mm_struct *mm;
5e9a0f02 1388 int err;
3268c63e 1389 nodemask_t task_nodes;
742755a1
CL
1390
1391 /* Check flags */
1392 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1393 return -EINVAL;
1394
1395 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1396 return -EPERM;
1397
1398 /* Find the mm_struct */
a879bf58 1399 rcu_read_lock();
228ebcbe 1400 task = pid ? find_task_by_vpid(pid) : current;
742755a1 1401 if (!task) {
a879bf58 1402 rcu_read_unlock();
742755a1
CL
1403 return -ESRCH;
1404 }
3268c63e 1405 get_task_struct(task);
742755a1
CL
1406
1407 /*
1408 * Check if this process has the right to modify the specified
1409 * process. The right exists if the process has administrative
1410 * capabilities, superuser privileges or the same
1411 * userid as the target process.
1412 */
c69e8d9c 1413 tcred = __task_cred(task);
b38a86eb
EB
1414 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1415 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
742755a1 1416 !capable(CAP_SYS_NICE)) {
c69e8d9c 1417 rcu_read_unlock();
742755a1 1418 err = -EPERM;
5e9a0f02 1419 goto out;
742755a1 1420 }
c69e8d9c 1421 rcu_read_unlock();
742755a1 1422
86c3a764
DQ
1423 err = security_task_movememory(task);
1424 if (err)
5e9a0f02 1425 goto out;
86c3a764 1426
3268c63e
CL
1427 task_nodes = cpuset_mems_allowed(task);
1428 mm = get_task_mm(task);
1429 put_task_struct(task);
1430
6e8b09ea
SL
1431 if (!mm)
1432 return -EINVAL;
1433
1434 if (nodes)
1435 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1436 nodes, status, flags);
1437 else
1438 err = do_pages_stat(mm, nr_pages, pages, status);
742755a1 1439
742755a1
CL
1440 mmput(mm);
1441 return err;
3268c63e
CL
1442
1443out:
1444 put_task_struct(task);
1445 return err;
742755a1 1446}
742755a1 1447
7b2259b3
CL
1448/*
1449 * Call migration functions in the vma_ops that may prepare
1450 * memory in a vm for migration. migration functions may perform
1451 * the migration for vmas that do not have an underlying page struct.
1452 */
1453int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1454 const nodemask_t *from, unsigned long flags)
1455{
1456 struct vm_area_struct *vma;
1457 int err = 0;
1458
1001c9fb 1459 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
7b2259b3
CL
1460 if (vma->vm_ops && vma->vm_ops->migrate) {
1461 err = vma->vm_ops->migrate(vma, to, from, flags);
1462 if (err)
1463 break;
1464 }
1465 }
1466 return err;
1467}
7039e1db
PZ
1468
1469#ifdef CONFIG_NUMA_BALANCING
1470/*
1471 * Returns true if this is a safe migration target node for misplaced NUMA
1472 * pages. Currently it only checks the watermarks which crude
1473 */
1474static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
3abef4e6 1475 unsigned long nr_migrate_pages)
7039e1db
PZ
1476{
1477 int z;
1478 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1479 struct zone *zone = pgdat->node_zones + z;
1480
1481 if (!populated_zone(zone))
1482 continue;
1483
1484 if (zone->all_unreclaimable)
1485 continue;
1486
1487 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1488 if (!zone_watermark_ok(zone, 0,
1489 high_wmark_pages(zone) +
1490 nr_migrate_pages,
1491 0, 0))
1492 continue;
1493 return true;
1494 }
1495 return false;
1496}
1497
1498static struct page *alloc_misplaced_dst_page(struct page *page,
1499 unsigned long data,
1500 int **result)
1501{
1502 int nid = (int) data;
1503 struct page *newpage;
1504
1505 newpage = alloc_pages_exact_node(nid,
1506 (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
1507 __GFP_NOMEMALLOC | __GFP_NORETRY |
1508 __GFP_NOWARN) &
1509 ~GFP_IOFS, 0);
bac0382c 1510 if (newpage)
22b751c3 1511 page_nid_xchg_last(newpage, page_nid_last(page));
bac0382c 1512
7039e1db
PZ
1513 return newpage;
1514}
1515
a8f60772
MG
1516/*
1517 * page migration rate limiting control.
1518 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1519 * window of time. Default here says do not migrate more than 1280M per second.
e14808b4
MG
1520 * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
1521 * as it is faults that reset the window, pte updates will happen unconditionally
1522 * if there has not been a fault since @pteupdate_interval_millisecs after the
1523 * throttle window closed.
a8f60772
MG
1524 */
1525static unsigned int migrate_interval_millisecs __read_mostly = 100;
e14808b4 1526static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
a8f60772
MG
1527static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1528
e14808b4
MG
1529/* Returns true if NUMA migration is currently rate limited */
1530bool migrate_ratelimited(int node)
1531{
1532 pg_data_t *pgdat = NODE_DATA(node);
1533
1534 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
1535 msecs_to_jiffies(pteupdate_interval_millisecs)))
1536 return false;
1537
1538 if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
1539 return false;
1540
1541 return true;
1542}
1543
b32967ff 1544/* Returns true if the node is migrate rate-limited after the update */
d28d4335 1545bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
7039e1db 1546{
b32967ff 1547 bool rate_limited = false;
7039e1db 1548
a8f60772
MG
1549 /*
1550 * Rate-limit the amount of data that is being migrated to a node.
1551 * Optimal placement is no good if the memory bus is saturated and
1552 * all the time is being spent migrating!
1553 */
1554 spin_lock(&pgdat->numabalancing_migrate_lock);
1555 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
1556 pgdat->numabalancing_migrate_nr_pages = 0;
1557 pgdat->numabalancing_migrate_next_window = jiffies +
1558 msecs_to_jiffies(migrate_interval_millisecs);
1559 }
b32967ff
MG
1560 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
1561 rate_limited = true;
1562 else
d28d4335 1563 pgdat->numabalancing_migrate_nr_pages += nr_pages;
a8f60772 1564 spin_unlock(&pgdat->numabalancing_migrate_lock);
b32967ff
MG
1565
1566 return rate_limited;
1567}
1568
1569int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
1570{
340ef390 1571 int page_lru;
a8f60772 1572
3abef4e6
MG
1573 VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
1574
7039e1db 1575 /* Avoid migrating to a node that is nearly full */
340ef390
HD
1576 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1577 return 0;
7039e1db 1578
340ef390
HD
1579 if (isolate_lru_page(page))
1580 return 0;
7039e1db 1581
340ef390
HD
1582 /*
1583 * migrate_misplaced_transhuge_page() skips page migration's usual
1584 * check on page_count(), so we must do it here, now that the page
1585 * has been isolated: a GUP pin, or any other pin, prevents migration.
1586 * The expected page count is 3: 1 for page's mapcount and 1 for the
1587 * caller's pin and 1 for the reference taken by isolate_lru_page().
1588 */
1589 if (PageTransHuge(page) && page_count(page) != 3) {
1590 putback_lru_page(page);
1591 return 0;
7039e1db
PZ
1592 }
1593
340ef390
HD
1594 page_lru = page_is_file_cache(page);
1595 mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
1596 hpage_nr_pages(page));
1597
149c33e1 1598 /*
340ef390
HD
1599 * Isolating the page has taken another reference, so the
1600 * caller's reference can be safely dropped without the page
1601 * disappearing underneath us during migration.
149c33e1
MG
1602 */
1603 put_page(page);
340ef390 1604 return 1;
b32967ff
MG
1605}
1606
1607/*
1608 * Attempt to migrate a misplaced page to the specified destination
1609 * node. Caller is expected to have an elevated reference count on
1610 * the page that will be dropped by this function before returning.
1611 */
1612int migrate_misplaced_page(struct page *page, int node)
1613{
1614 pg_data_t *pgdat = NODE_DATA(node);
340ef390 1615 int isolated;
b32967ff
MG
1616 int nr_remaining;
1617 LIST_HEAD(migratepages);
1618
1619 /*
1620 * Don't migrate pages that are mapped in multiple processes.
1621 * TODO: Handle false sharing detection instead of this hammer
1622 */
340ef390 1623 if (page_mapcount(page) != 1)
b32967ff 1624 goto out;
b32967ff
MG
1625
1626 /*
1627 * Rate-limit the amount of data that is being migrated to a node.
1628 * Optimal placement is no good if the memory bus is saturated and
1629 * all the time is being spent migrating!
1630 */
340ef390 1631 if (numamigrate_update_ratelimit(pgdat, 1))
b32967ff 1632 goto out;
b32967ff
MG
1633
1634 isolated = numamigrate_isolate_page(pgdat, page);
1635 if (!isolated)
1636 goto out;
1637
1638 list_add(&page->lru, &migratepages);
9c620e2b
HD
1639 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
1640 node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
b32967ff
MG
1641 if (nr_remaining) {
1642 putback_lru_pages(&migratepages);
1643 isolated = 0;
1644 } else
1645 count_vm_numa_event(NUMA_PAGE_MIGRATE);
7039e1db 1646 BUG_ON(!list_empty(&migratepages));
7039e1db 1647 return isolated;
340ef390
HD
1648
1649out:
1650 put_page(page);
1651 return 0;
7039e1db 1652}
220018d3 1653#endif /* CONFIG_NUMA_BALANCING */
b32967ff 1654
220018d3 1655#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
340ef390
HD
1656/*
1657 * Migrates a THP to a given target node. page must be locked and is unlocked
1658 * before returning.
1659 */
b32967ff
MG
1660int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1661 struct vm_area_struct *vma,
1662 pmd_t *pmd, pmd_t entry,
1663 unsigned long address,
1664 struct page *page, int node)
1665{
1666 unsigned long haddr = address & HPAGE_PMD_MASK;
1667 pg_data_t *pgdat = NODE_DATA(node);
1668 int isolated = 0;
1669 struct page *new_page = NULL;
1670 struct mem_cgroup *memcg = NULL;
1671 int page_lru = page_is_file_cache(page);
1672
1673 /*
1674 * Don't migrate pages that are mapped in multiple processes.
1675 * TODO: Handle false sharing detection instead of this hammer
1676 */
1677 if (page_mapcount(page) != 1)
1678 goto out_dropref;
1679
1680 /*
1681 * Rate-limit the amount of data that is being migrated to a node.
1682 * Optimal placement is no good if the memory bus is saturated and
1683 * all the time is being spent migrating!
1684 */
d28d4335 1685 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
b32967ff
MG
1686 goto out_dropref;
1687
1688 new_page = alloc_pages_node(node,
1689 (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
340ef390
HD
1690 if (!new_page)
1691 goto out_fail;
1692
22b751c3 1693 page_nid_xchg_last(new_page, page_nid_last(page));
b32967ff
MG
1694
1695 isolated = numamigrate_isolate_page(pgdat, page);
340ef390 1696 if (!isolated) {
b32967ff 1697 put_page(new_page);
340ef390 1698 goto out_fail;
b32967ff
MG
1699 }
1700
1701 /* Prepare a page as a migration target */
1702 __set_page_locked(new_page);
1703 SetPageSwapBacked(new_page);
1704
1705 /* anon mapping, we can simply copy page->mapping to the new page: */
1706 new_page->mapping = page->mapping;
1707 new_page->index = page->index;
1708 migrate_page_copy(new_page, page);
1709 WARN_ON(PageLRU(new_page));
1710
1711 /* Recheck the target PMD */
1712 spin_lock(&mm->page_table_lock);
1713 if (unlikely(!pmd_same(*pmd, entry))) {
1714 spin_unlock(&mm->page_table_lock);
1715
1716 /* Reverse changes made by migrate_page_copy() */
1717 if (TestClearPageActive(new_page))
1718 SetPageActive(page);
1719 if (TestClearPageUnevictable(new_page))
1720 SetPageUnevictable(page);
1721 mlock_migrate_page(page, new_page);
1722
1723 unlock_page(new_page);
1724 put_page(new_page); /* Free it */
1725
a490bb33
MG
1726 /* Retake the callers reference and putback on LRU */
1727 get_page(page);
b32967ff 1728 putback_lru_page(page);
a490bb33
MG
1729 mod_zone_page_state(page_zone(page),
1730 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
a29ccdd1
MG
1731
1732 goto out_unlock;
b32967ff
MG
1733 }
1734
1735 /*
1736 * Traditional migration needs to prepare the memcg charge
1737 * transaction early to prevent the old page from being
1738 * uncharged when installing migration entries. Here we can
1739 * save the potential rollback and start the charge transfer
1740 * only when migration is already known to end successfully.
1741 */
1742 mem_cgroup_prepare_migration(page, new_page, &memcg);
1743
1744 entry = mk_pmd(new_page, vma->vm_page_prot);
1745 entry = pmd_mknonnuma(entry);
1746 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1747 entry = pmd_mkhuge(entry);
1748
a490bb33 1749 pmdp_clear_flush(vma, haddr, pmd);
b32967ff 1750 set_pmd_at(mm, haddr, pmd, entry);
a490bb33 1751 page_add_new_anon_rmap(new_page, vma, haddr);
ce4a9cc5 1752 update_mmu_cache_pmd(vma, address, &entry);
b32967ff
MG
1753 page_remove_rmap(page);
1754 /*
1755 * Finish the charge transaction under the page table lock to
1756 * prevent split_huge_page() from dividing up the charge
1757 * before it's fully transferred to the new page.
1758 */
1759 mem_cgroup_end_migration(memcg, page, new_page, true);
1760 spin_unlock(&mm->page_table_lock);
1761
1762 unlock_page(new_page);
1763 unlock_page(page);
1764 put_page(page); /* Drop the rmap reference */
1765 put_page(page); /* Drop the LRU isolation reference */
1766
1767 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
1768 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
1769
b32967ff
MG
1770 mod_zone_page_state(page_zone(page),
1771 NR_ISOLATED_ANON + page_lru,
1772 -HPAGE_PMD_NR);
1773 return isolated;
1774
340ef390
HD
1775out_fail:
1776 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
b32967ff 1777out_dropref:
a490bb33
MG
1778 entry = pmd_mknonnuma(entry);
1779 set_pmd_at(mm, haddr, pmd, entry);
1780 update_mmu_cache_pmd(vma, address, &entry);
1781
a29ccdd1 1782out_unlock:
340ef390 1783 unlock_page(page);
b32967ff 1784 put_page(page);
b32967ff
MG
1785 return 0;
1786}
7039e1db
PZ
1787#endif /* CONFIG_NUMA_BALANCING */
1788
1789#endif /* CONFIG_NUMA */