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