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