]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - mm/migrate.c
mm, migrate: remove reason argument from new_page_t
[mirror_ubuntu-hirsute-kernel.git] / mm / migrate.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Memory Migration functionality - linux/mm/migrate.c
4 *
5 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6 *
7 * Page migration was first developed in the context of the memory hotplug
8 * project. The main authors of the migration code are:
9 *
10 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
11 * Hirokazu Takahashi <taka@valinux.co.jp>
12 * Dave Hansen <haveblue@us.ibm.com>
13 * Christoph Lameter
14 */
15
16 #include <linux/migrate.h>
17 #include <linux/export.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
20 #include <linux/pagemap.h>
21 #include <linux/buffer_head.h>
22 #include <linux/mm_inline.h>
23 #include <linux/nsproxy.h>
24 #include <linux/pagevec.h>
25 #include <linux/ksm.h>
26 #include <linux/rmap.h>
27 #include <linux/topology.h>
28 #include <linux/cpu.h>
29 #include <linux/cpuset.h>
30 #include <linux/writeback.h>
31 #include <linux/mempolicy.h>
32 #include <linux/vmalloc.h>
33 #include <linux/security.h>
34 #include <linux/backing-dev.h>
35 #include <linux/compaction.h>
36 #include <linux/syscalls.h>
37 #include <linux/compat.h>
38 #include <linux/hugetlb.h>
39 #include <linux/hugetlb_cgroup.h>
40 #include <linux/gfp.h>
41 #include <linux/pfn_t.h>
42 #include <linux/memremap.h>
43 #include <linux/userfaultfd_k.h>
44 #include <linux/balloon_compaction.h>
45 #include <linux/mmu_notifier.h>
46 #include <linux/page_idle.h>
47 #include <linux/page_owner.h>
48 #include <linux/sched/mm.h>
49 #include <linux/ptrace.h>
50
51 #include <asm/tlbflush.h>
52
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/migrate.h>
55
56 #include "internal.h"
57
58 /*
59 * migrate_prep() needs to be called before we start compiling a list of pages
60 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
61 * undesirable, use migrate_prep_local()
62 */
63 int migrate_prep(void)
64 {
65 /*
66 * Clear the LRU lists so pages can be isolated.
67 * Note that pages may be moved off the LRU after we have
68 * drained them. Those pages will fail to migrate like other
69 * pages that may be busy.
70 */
71 lru_add_drain_all();
72
73 return 0;
74 }
75
76 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
77 int migrate_prep_local(void)
78 {
79 lru_add_drain();
80
81 return 0;
82 }
83
84 int isolate_movable_page(struct page *page, isolate_mode_t mode)
85 {
86 struct address_space *mapping;
87
88 /*
89 * Avoid burning cycles with pages that are yet under __free_pages(),
90 * or just got freed under us.
91 *
92 * In case we 'win' a race for a movable page being freed under us and
93 * raise its refcount preventing __free_pages() from doing its job
94 * the put_page() at the end of this block will take care of
95 * release this page, thus avoiding a nasty leakage.
96 */
97 if (unlikely(!get_page_unless_zero(page)))
98 goto out;
99
100 /*
101 * Check PageMovable before holding a PG_lock because page's owner
102 * assumes anybody doesn't touch PG_lock of newly allocated page
103 * so unconditionally grapping the lock ruins page's owner side.
104 */
105 if (unlikely(!__PageMovable(page)))
106 goto out_putpage;
107 /*
108 * As movable pages are not isolated from LRU lists, concurrent
109 * compaction threads can race against page migration functions
110 * as well as race against the releasing a page.
111 *
112 * In order to avoid having an already isolated movable page
113 * being (wrongly) re-isolated while it is under migration,
114 * or to avoid attempting to isolate pages being released,
115 * lets be sure we have the page lock
116 * before proceeding with the movable page isolation steps.
117 */
118 if (unlikely(!trylock_page(page)))
119 goto out_putpage;
120
121 if (!PageMovable(page) || PageIsolated(page))
122 goto out_no_isolated;
123
124 mapping = page_mapping(page);
125 VM_BUG_ON_PAGE(!mapping, page);
126
127 if (!mapping->a_ops->isolate_page(page, mode))
128 goto out_no_isolated;
129
130 /* Driver shouldn't use PG_isolated bit of page->flags */
131 WARN_ON_ONCE(PageIsolated(page));
132 __SetPageIsolated(page);
133 unlock_page(page);
134
135 return 0;
136
137 out_no_isolated:
138 unlock_page(page);
139 out_putpage:
140 put_page(page);
141 out:
142 return -EBUSY;
143 }
144
145 /* It should be called on page which is PG_movable */
146 void putback_movable_page(struct page *page)
147 {
148 struct address_space *mapping;
149
150 VM_BUG_ON_PAGE(!PageLocked(page), page);
151 VM_BUG_ON_PAGE(!PageMovable(page), page);
152 VM_BUG_ON_PAGE(!PageIsolated(page), page);
153
154 mapping = page_mapping(page);
155 mapping->a_ops->putback_page(page);
156 __ClearPageIsolated(page);
157 }
158
159 /*
160 * Put previously isolated pages back onto the appropriate lists
161 * from where they were once taken off for compaction/migration.
162 *
163 * This function shall be used whenever the isolated pageset has been
164 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
165 * and isolate_huge_page().
166 */
167 void putback_movable_pages(struct list_head *l)
168 {
169 struct page *page;
170 struct page *page2;
171
172 list_for_each_entry_safe(page, page2, l, lru) {
173 if (unlikely(PageHuge(page))) {
174 putback_active_hugepage(page);
175 continue;
176 }
177 list_del(&page->lru);
178 /*
179 * We isolated non-lru movable page so here we can use
180 * __PageMovable because LRU page's mapping cannot have
181 * PAGE_MAPPING_MOVABLE.
182 */
183 if (unlikely(__PageMovable(page))) {
184 VM_BUG_ON_PAGE(!PageIsolated(page), page);
185 lock_page(page);
186 if (PageMovable(page))
187 putback_movable_page(page);
188 else
189 __ClearPageIsolated(page);
190 unlock_page(page);
191 put_page(page);
192 } else {
193 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
194 page_is_file_cache(page), -hpage_nr_pages(page));
195 putback_lru_page(page);
196 }
197 }
198 }
199
200 /*
201 * Restore a potential migration pte to a working pte entry
202 */
203 static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
204 unsigned long addr, void *old)
205 {
206 struct page_vma_mapped_walk pvmw = {
207 .page = old,
208 .vma = vma,
209 .address = addr,
210 .flags = PVMW_SYNC | PVMW_MIGRATION,
211 };
212 struct page *new;
213 pte_t pte;
214 swp_entry_t entry;
215
216 VM_BUG_ON_PAGE(PageTail(page), page);
217 while (page_vma_mapped_walk(&pvmw)) {
218 if (PageKsm(page))
219 new = page;
220 else
221 new = page - pvmw.page->index +
222 linear_page_index(vma, pvmw.address);
223
224 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
225 /* PMD-mapped THP migration entry */
226 if (!pvmw.pte) {
227 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
228 remove_migration_pmd(&pvmw, new);
229 continue;
230 }
231 #endif
232
233 get_page(new);
234 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
235 if (pte_swp_soft_dirty(*pvmw.pte))
236 pte = pte_mksoft_dirty(pte);
237
238 /*
239 * Recheck VMA as permissions can change since migration started
240 */
241 entry = pte_to_swp_entry(*pvmw.pte);
242 if (is_write_migration_entry(entry))
243 pte = maybe_mkwrite(pte, vma);
244
245 if (unlikely(is_zone_device_page(new))) {
246 if (is_device_private_page(new)) {
247 entry = make_device_private_entry(new, pte_write(pte));
248 pte = swp_entry_to_pte(entry);
249 } else if (is_device_public_page(new)) {
250 pte = pte_mkdevmap(pte);
251 flush_dcache_page(new);
252 }
253 } else
254 flush_dcache_page(new);
255
256 #ifdef CONFIG_HUGETLB_PAGE
257 if (PageHuge(new)) {
258 pte = pte_mkhuge(pte);
259 pte = arch_make_huge_pte(pte, vma, new, 0);
260 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
261 if (PageAnon(new))
262 hugepage_add_anon_rmap(new, vma, pvmw.address);
263 else
264 page_dup_rmap(new, true);
265 } else
266 #endif
267 {
268 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
269
270 if (PageAnon(new))
271 page_add_anon_rmap(new, vma, pvmw.address, false);
272 else
273 page_add_file_rmap(new, false);
274 }
275 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
276 mlock_vma_page(new);
277
278 /* No need to invalidate - it was non-present before */
279 update_mmu_cache(vma, pvmw.address, pvmw.pte);
280 }
281
282 return true;
283 }
284
285 /*
286 * Get rid of all migration entries and replace them by
287 * references to the indicated page.
288 */
289 void remove_migration_ptes(struct page *old, struct page *new, bool locked)
290 {
291 struct rmap_walk_control rwc = {
292 .rmap_one = remove_migration_pte,
293 .arg = old,
294 };
295
296 if (locked)
297 rmap_walk_locked(new, &rwc);
298 else
299 rmap_walk(new, &rwc);
300 }
301
302 /*
303 * Something used the pte of a page under migration. We need to
304 * get to the page and wait until migration is finished.
305 * When we return from this function the fault will be retried.
306 */
307 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
308 spinlock_t *ptl)
309 {
310 pte_t pte;
311 swp_entry_t entry;
312 struct page *page;
313
314 spin_lock(ptl);
315 pte = *ptep;
316 if (!is_swap_pte(pte))
317 goto out;
318
319 entry = pte_to_swp_entry(pte);
320 if (!is_migration_entry(entry))
321 goto out;
322
323 page = migration_entry_to_page(entry);
324
325 /*
326 * Once radix-tree replacement of page migration started, page_count
327 * *must* be zero. And, we don't want to call wait_on_page_locked()
328 * against a page without get_page().
329 * So, we use get_page_unless_zero(), here. Even failed, page fault
330 * will occur again.
331 */
332 if (!get_page_unless_zero(page))
333 goto out;
334 pte_unmap_unlock(ptep, ptl);
335 wait_on_page_locked(page);
336 put_page(page);
337 return;
338 out:
339 pte_unmap_unlock(ptep, ptl);
340 }
341
342 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
343 unsigned long address)
344 {
345 spinlock_t *ptl = pte_lockptr(mm, pmd);
346 pte_t *ptep = pte_offset_map(pmd, address);
347 __migration_entry_wait(mm, ptep, ptl);
348 }
349
350 void migration_entry_wait_huge(struct vm_area_struct *vma,
351 struct mm_struct *mm, pte_t *pte)
352 {
353 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
354 __migration_entry_wait(mm, pte, ptl);
355 }
356
357 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
358 void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
359 {
360 spinlock_t *ptl;
361 struct page *page;
362
363 ptl = pmd_lock(mm, pmd);
364 if (!is_pmd_migration_entry(*pmd))
365 goto unlock;
366 page = migration_entry_to_page(pmd_to_swp_entry(*pmd));
367 if (!get_page_unless_zero(page))
368 goto unlock;
369 spin_unlock(ptl);
370 wait_on_page_locked(page);
371 put_page(page);
372 return;
373 unlock:
374 spin_unlock(ptl);
375 }
376 #endif
377
378 #ifdef CONFIG_BLOCK
379 /* Returns true if all buffers are successfully locked */
380 static bool buffer_migrate_lock_buffers(struct buffer_head *head,
381 enum migrate_mode mode)
382 {
383 struct buffer_head *bh = head;
384
385 /* Simple case, sync compaction */
386 if (mode != MIGRATE_ASYNC) {
387 do {
388 get_bh(bh);
389 lock_buffer(bh);
390 bh = bh->b_this_page;
391
392 } while (bh != head);
393
394 return true;
395 }
396
397 /* async case, we cannot block on lock_buffer so use trylock_buffer */
398 do {
399 get_bh(bh);
400 if (!trylock_buffer(bh)) {
401 /*
402 * We failed to lock the buffer and cannot stall in
403 * async migration. Release the taken locks
404 */
405 struct buffer_head *failed_bh = bh;
406 put_bh(failed_bh);
407 bh = head;
408 while (bh != failed_bh) {
409 unlock_buffer(bh);
410 put_bh(bh);
411 bh = bh->b_this_page;
412 }
413 return false;
414 }
415
416 bh = bh->b_this_page;
417 } while (bh != head);
418 return true;
419 }
420 #else
421 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
422 enum migrate_mode mode)
423 {
424 return true;
425 }
426 #endif /* CONFIG_BLOCK */
427
428 /*
429 * Replace the page in the mapping.
430 *
431 * The number of remaining references must be:
432 * 1 for anonymous pages without a mapping
433 * 2 for pages with a mapping
434 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
435 */
436 int migrate_page_move_mapping(struct address_space *mapping,
437 struct page *newpage, struct page *page,
438 struct buffer_head *head, enum migrate_mode mode,
439 int extra_count)
440 {
441 struct zone *oldzone, *newzone;
442 int dirty;
443 int expected_count = 1 + extra_count;
444 void **pslot;
445
446 /*
447 * Device public or private pages have an extra refcount as they are
448 * ZONE_DEVICE pages.
449 */
450 expected_count += is_device_private_page(page);
451 expected_count += is_device_public_page(page);
452
453 if (!mapping) {
454 /* Anonymous page without mapping */
455 if (page_count(page) != expected_count)
456 return -EAGAIN;
457
458 /* No turning back from here */
459 newpage->index = page->index;
460 newpage->mapping = page->mapping;
461 if (PageSwapBacked(page))
462 __SetPageSwapBacked(newpage);
463
464 return MIGRATEPAGE_SUCCESS;
465 }
466
467 oldzone = page_zone(page);
468 newzone = page_zone(newpage);
469
470 spin_lock_irq(&mapping->tree_lock);
471
472 pslot = radix_tree_lookup_slot(&mapping->page_tree,
473 page_index(page));
474
475 expected_count += 1 + page_has_private(page);
476 if (page_count(page) != expected_count ||
477 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
478 spin_unlock_irq(&mapping->tree_lock);
479 return -EAGAIN;
480 }
481
482 if (!page_ref_freeze(page, expected_count)) {
483 spin_unlock_irq(&mapping->tree_lock);
484 return -EAGAIN;
485 }
486
487 /*
488 * In the async migration case of moving a page with buffers, lock the
489 * buffers using trylock before the mapping is moved. If the mapping
490 * was moved, we later failed to lock the buffers and could not move
491 * the mapping back due to an elevated page count, we would have to
492 * block waiting on other references to be dropped.
493 */
494 if (mode == MIGRATE_ASYNC && head &&
495 !buffer_migrate_lock_buffers(head, mode)) {
496 page_ref_unfreeze(page, expected_count);
497 spin_unlock_irq(&mapping->tree_lock);
498 return -EAGAIN;
499 }
500
501 /*
502 * Now we know that no one else is looking at the page:
503 * no turning back from here.
504 */
505 newpage->index = page->index;
506 newpage->mapping = page->mapping;
507 get_page(newpage); /* add cache reference */
508 if (PageSwapBacked(page)) {
509 __SetPageSwapBacked(newpage);
510 if (PageSwapCache(page)) {
511 SetPageSwapCache(newpage);
512 set_page_private(newpage, page_private(page));
513 }
514 } else {
515 VM_BUG_ON_PAGE(PageSwapCache(page), page);
516 }
517
518 /* Move dirty while page refs frozen and newpage not yet exposed */
519 dirty = PageDirty(page);
520 if (dirty) {
521 ClearPageDirty(page);
522 SetPageDirty(newpage);
523 }
524
525 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
526
527 /*
528 * Drop cache reference from old page by unfreezing
529 * to one less reference.
530 * We know this isn't the last reference.
531 */
532 page_ref_unfreeze(page, expected_count - 1);
533
534 spin_unlock(&mapping->tree_lock);
535 /* Leave irq disabled to prevent preemption while updating stats */
536
537 /*
538 * If moved to a different zone then also account
539 * the page for that zone. Other VM counters will be
540 * taken care of when we establish references to the
541 * new page and drop references to the old page.
542 *
543 * Note that anonymous pages are accounted for
544 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
545 * are mapped to swap space.
546 */
547 if (newzone != oldzone) {
548 __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES);
549 __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES);
550 if (PageSwapBacked(page) && !PageSwapCache(page)) {
551 __dec_node_state(oldzone->zone_pgdat, NR_SHMEM);
552 __inc_node_state(newzone->zone_pgdat, NR_SHMEM);
553 }
554 if (dirty && mapping_cap_account_dirty(mapping)) {
555 __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY);
556 __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING);
557 __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY);
558 __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING);
559 }
560 }
561 local_irq_enable();
562
563 return MIGRATEPAGE_SUCCESS;
564 }
565 EXPORT_SYMBOL(migrate_page_move_mapping);
566
567 /*
568 * The expected number of remaining references is the same as that
569 * of migrate_page_move_mapping().
570 */
571 int migrate_huge_page_move_mapping(struct address_space *mapping,
572 struct page *newpage, struct page *page)
573 {
574 int expected_count;
575 void **pslot;
576
577 spin_lock_irq(&mapping->tree_lock);
578
579 pslot = radix_tree_lookup_slot(&mapping->page_tree,
580 page_index(page));
581
582 expected_count = 2 + page_has_private(page);
583 if (page_count(page) != expected_count ||
584 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
585 spin_unlock_irq(&mapping->tree_lock);
586 return -EAGAIN;
587 }
588
589 if (!page_ref_freeze(page, expected_count)) {
590 spin_unlock_irq(&mapping->tree_lock);
591 return -EAGAIN;
592 }
593
594 newpage->index = page->index;
595 newpage->mapping = page->mapping;
596
597 get_page(newpage);
598
599 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
600
601 page_ref_unfreeze(page, expected_count - 1);
602
603 spin_unlock_irq(&mapping->tree_lock);
604
605 return MIGRATEPAGE_SUCCESS;
606 }
607
608 /*
609 * Gigantic pages are so large that we do not guarantee that page++ pointer
610 * arithmetic will work across the entire page. We need something more
611 * specialized.
612 */
613 static void __copy_gigantic_page(struct page *dst, struct page *src,
614 int nr_pages)
615 {
616 int i;
617 struct page *dst_base = dst;
618 struct page *src_base = src;
619
620 for (i = 0; i < nr_pages; ) {
621 cond_resched();
622 copy_highpage(dst, src);
623
624 i++;
625 dst = mem_map_next(dst, dst_base, i);
626 src = mem_map_next(src, src_base, i);
627 }
628 }
629
630 static void copy_huge_page(struct page *dst, struct page *src)
631 {
632 int i;
633 int nr_pages;
634
635 if (PageHuge(src)) {
636 /* hugetlbfs page */
637 struct hstate *h = page_hstate(src);
638 nr_pages = pages_per_huge_page(h);
639
640 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
641 __copy_gigantic_page(dst, src, nr_pages);
642 return;
643 }
644 } else {
645 /* thp page */
646 BUG_ON(!PageTransHuge(src));
647 nr_pages = hpage_nr_pages(src);
648 }
649
650 for (i = 0; i < nr_pages; i++) {
651 cond_resched();
652 copy_highpage(dst + i, src + i);
653 }
654 }
655
656 /*
657 * Copy the page to its new location
658 */
659 void migrate_page_states(struct page *newpage, struct page *page)
660 {
661 int cpupid;
662
663 if (PageError(page))
664 SetPageError(newpage);
665 if (PageReferenced(page))
666 SetPageReferenced(newpage);
667 if (PageUptodate(page))
668 SetPageUptodate(newpage);
669 if (TestClearPageActive(page)) {
670 VM_BUG_ON_PAGE(PageUnevictable(page), page);
671 SetPageActive(newpage);
672 } else if (TestClearPageUnevictable(page))
673 SetPageUnevictable(newpage);
674 if (PageChecked(page))
675 SetPageChecked(newpage);
676 if (PageMappedToDisk(page))
677 SetPageMappedToDisk(newpage);
678
679 /* Move dirty on pages not done by migrate_page_move_mapping() */
680 if (PageDirty(page))
681 SetPageDirty(newpage);
682
683 if (page_is_young(page))
684 set_page_young(newpage);
685 if (page_is_idle(page))
686 set_page_idle(newpage);
687
688 /*
689 * Copy NUMA information to the new page, to prevent over-eager
690 * future migrations of this same page.
691 */
692 cpupid = page_cpupid_xchg_last(page, -1);
693 page_cpupid_xchg_last(newpage, cpupid);
694
695 ksm_migrate_page(newpage, page);
696 /*
697 * Please do not reorder this without considering how mm/ksm.c's
698 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
699 */
700 if (PageSwapCache(page))
701 ClearPageSwapCache(page);
702 ClearPagePrivate(page);
703 set_page_private(page, 0);
704
705 /*
706 * If any waiters have accumulated on the new page then
707 * wake them up.
708 */
709 if (PageWriteback(newpage))
710 end_page_writeback(newpage);
711
712 copy_page_owner(page, newpage);
713
714 mem_cgroup_migrate(page, newpage);
715 }
716 EXPORT_SYMBOL(migrate_page_states);
717
718 void migrate_page_copy(struct page *newpage, struct page *page)
719 {
720 if (PageHuge(page) || PageTransHuge(page))
721 copy_huge_page(newpage, page);
722 else
723 copy_highpage(newpage, page);
724
725 migrate_page_states(newpage, page);
726 }
727 EXPORT_SYMBOL(migrate_page_copy);
728
729 /************************************************************
730 * Migration functions
731 ***********************************************************/
732
733 /*
734 * Common logic to directly migrate a single LRU page suitable for
735 * pages that do not use PagePrivate/PagePrivate2.
736 *
737 * Pages are locked upon entry and exit.
738 */
739 int migrate_page(struct address_space *mapping,
740 struct page *newpage, struct page *page,
741 enum migrate_mode mode)
742 {
743 int rc;
744
745 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
746
747 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
748
749 if (rc != MIGRATEPAGE_SUCCESS)
750 return rc;
751
752 if (mode != MIGRATE_SYNC_NO_COPY)
753 migrate_page_copy(newpage, page);
754 else
755 migrate_page_states(newpage, page);
756 return MIGRATEPAGE_SUCCESS;
757 }
758 EXPORT_SYMBOL(migrate_page);
759
760 #ifdef CONFIG_BLOCK
761 /*
762 * Migration function for pages with buffers. This function can only be used
763 * if the underlying filesystem guarantees that no other references to "page"
764 * exist.
765 */
766 int buffer_migrate_page(struct address_space *mapping,
767 struct page *newpage, struct page *page, enum migrate_mode mode)
768 {
769 struct buffer_head *bh, *head;
770 int rc;
771
772 if (!page_has_buffers(page))
773 return migrate_page(mapping, newpage, page, mode);
774
775 head = page_buffers(page);
776
777 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
778
779 if (rc != MIGRATEPAGE_SUCCESS)
780 return rc;
781
782 /*
783 * In the async case, migrate_page_move_mapping locked the buffers
784 * with an IRQ-safe spinlock held. In the sync case, the buffers
785 * need to be locked now
786 */
787 if (mode != MIGRATE_ASYNC)
788 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
789
790 ClearPagePrivate(page);
791 set_page_private(newpage, page_private(page));
792 set_page_private(page, 0);
793 put_page(page);
794 get_page(newpage);
795
796 bh = head;
797 do {
798 set_bh_page(bh, newpage, bh_offset(bh));
799 bh = bh->b_this_page;
800
801 } while (bh != head);
802
803 SetPagePrivate(newpage);
804
805 if (mode != MIGRATE_SYNC_NO_COPY)
806 migrate_page_copy(newpage, page);
807 else
808 migrate_page_states(newpage, page);
809
810 bh = head;
811 do {
812 unlock_buffer(bh);
813 put_bh(bh);
814 bh = bh->b_this_page;
815
816 } while (bh != head);
817
818 return MIGRATEPAGE_SUCCESS;
819 }
820 EXPORT_SYMBOL(buffer_migrate_page);
821 #endif
822
823 /*
824 * Writeback a page to clean the dirty state
825 */
826 static int writeout(struct address_space *mapping, struct page *page)
827 {
828 struct writeback_control wbc = {
829 .sync_mode = WB_SYNC_NONE,
830 .nr_to_write = 1,
831 .range_start = 0,
832 .range_end = LLONG_MAX,
833 .for_reclaim = 1
834 };
835 int rc;
836
837 if (!mapping->a_ops->writepage)
838 /* No write method for the address space */
839 return -EINVAL;
840
841 if (!clear_page_dirty_for_io(page))
842 /* Someone else already triggered a write */
843 return -EAGAIN;
844
845 /*
846 * A dirty page may imply that the underlying filesystem has
847 * the page on some queue. So the page must be clean for
848 * migration. Writeout may mean we loose the lock and the
849 * page state is no longer what we checked for earlier.
850 * At this point we know that the migration attempt cannot
851 * be successful.
852 */
853 remove_migration_ptes(page, page, false);
854
855 rc = mapping->a_ops->writepage(page, &wbc);
856
857 if (rc != AOP_WRITEPAGE_ACTIVATE)
858 /* unlocked. Relock */
859 lock_page(page);
860
861 return (rc < 0) ? -EIO : -EAGAIN;
862 }
863
864 /*
865 * Default handling if a filesystem does not provide a migration function.
866 */
867 static int fallback_migrate_page(struct address_space *mapping,
868 struct page *newpage, struct page *page, enum migrate_mode mode)
869 {
870 if (PageDirty(page)) {
871 /* Only writeback pages in full synchronous migration */
872 switch (mode) {
873 case MIGRATE_SYNC:
874 case MIGRATE_SYNC_NO_COPY:
875 break;
876 default:
877 return -EBUSY;
878 }
879 return writeout(mapping, page);
880 }
881
882 /*
883 * Buffers may be managed in a filesystem specific way.
884 * We must have no buffers or drop them.
885 */
886 if (page_has_private(page) &&
887 !try_to_release_page(page, GFP_KERNEL))
888 return -EAGAIN;
889
890 return migrate_page(mapping, newpage, page, mode);
891 }
892
893 /*
894 * Move a page to a newly allocated page
895 * The page is locked and all ptes have been successfully removed.
896 *
897 * The new page will have replaced the old page if this function
898 * is successful.
899 *
900 * Return value:
901 * < 0 - error code
902 * MIGRATEPAGE_SUCCESS - success
903 */
904 static int move_to_new_page(struct page *newpage, struct page *page,
905 enum migrate_mode mode)
906 {
907 struct address_space *mapping;
908 int rc = -EAGAIN;
909 bool is_lru = !__PageMovable(page);
910
911 VM_BUG_ON_PAGE(!PageLocked(page), page);
912 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
913
914 mapping = page_mapping(page);
915
916 if (likely(is_lru)) {
917 if (!mapping)
918 rc = migrate_page(mapping, newpage, page, mode);
919 else if (mapping->a_ops->migratepage)
920 /*
921 * Most pages have a mapping and most filesystems
922 * provide a migratepage callback. Anonymous pages
923 * are part of swap space which also has its own
924 * migratepage callback. This is the most common path
925 * for page migration.
926 */
927 rc = mapping->a_ops->migratepage(mapping, newpage,
928 page, mode);
929 else
930 rc = fallback_migrate_page(mapping, newpage,
931 page, mode);
932 } else {
933 /*
934 * In case of non-lru page, it could be released after
935 * isolation step. In that case, we shouldn't try migration.
936 */
937 VM_BUG_ON_PAGE(!PageIsolated(page), page);
938 if (!PageMovable(page)) {
939 rc = MIGRATEPAGE_SUCCESS;
940 __ClearPageIsolated(page);
941 goto out;
942 }
943
944 rc = mapping->a_ops->migratepage(mapping, newpage,
945 page, mode);
946 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
947 !PageIsolated(page));
948 }
949
950 /*
951 * When successful, old pagecache page->mapping must be cleared before
952 * page is freed; but stats require that PageAnon be left as PageAnon.
953 */
954 if (rc == MIGRATEPAGE_SUCCESS) {
955 if (__PageMovable(page)) {
956 VM_BUG_ON_PAGE(!PageIsolated(page), page);
957
958 /*
959 * We clear PG_movable under page_lock so any compactor
960 * cannot try to migrate this page.
961 */
962 __ClearPageIsolated(page);
963 }
964
965 /*
966 * Anonymous and movable page->mapping will be cleard by
967 * free_pages_prepare so don't reset it here for keeping
968 * the type to work PageAnon, for example.
969 */
970 if (!PageMappingFlags(page))
971 page->mapping = NULL;
972 }
973 out:
974 return rc;
975 }
976
977 static int __unmap_and_move(struct page *page, struct page *newpage,
978 int force, enum migrate_mode mode)
979 {
980 int rc = -EAGAIN;
981 int page_was_mapped = 0;
982 struct anon_vma *anon_vma = NULL;
983 bool is_lru = !__PageMovable(page);
984
985 if (!trylock_page(page)) {
986 if (!force || mode == MIGRATE_ASYNC)
987 goto out;
988
989 /*
990 * It's not safe for direct compaction to call lock_page.
991 * For example, during page readahead pages are added locked
992 * to the LRU. Later, when the IO completes the pages are
993 * marked uptodate and unlocked. However, the queueing
994 * could be merging multiple pages for one bio (e.g.
995 * mpage_readpages). If an allocation happens for the
996 * second or third page, the process can end up locking
997 * the same page twice and deadlocking. Rather than
998 * trying to be clever about what pages can be locked,
999 * avoid the use of lock_page for direct compaction
1000 * altogether.
1001 */
1002 if (current->flags & PF_MEMALLOC)
1003 goto out;
1004
1005 lock_page(page);
1006 }
1007
1008 if (PageWriteback(page)) {
1009 /*
1010 * Only in the case of a full synchronous migration is it
1011 * necessary to wait for PageWriteback. In the async case,
1012 * the retry loop is too short and in the sync-light case,
1013 * the overhead of stalling is too much
1014 */
1015 switch (mode) {
1016 case MIGRATE_SYNC:
1017 case MIGRATE_SYNC_NO_COPY:
1018 break;
1019 default:
1020 rc = -EBUSY;
1021 goto out_unlock;
1022 }
1023 if (!force)
1024 goto out_unlock;
1025 wait_on_page_writeback(page);
1026 }
1027
1028 /*
1029 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
1030 * we cannot notice that anon_vma is freed while we migrates a page.
1031 * This get_anon_vma() delays freeing anon_vma pointer until the end
1032 * of migration. File cache pages are no problem because of page_lock()
1033 * File Caches may use write_page() or lock_page() in migration, then,
1034 * just care Anon page here.
1035 *
1036 * Only page_get_anon_vma() understands the subtleties of
1037 * getting a hold on an anon_vma from outside one of its mms.
1038 * But if we cannot get anon_vma, then we won't need it anyway,
1039 * because that implies that the anon page is no longer mapped
1040 * (and cannot be remapped so long as we hold the page lock).
1041 */
1042 if (PageAnon(page) && !PageKsm(page))
1043 anon_vma = page_get_anon_vma(page);
1044
1045 /*
1046 * Block others from accessing the new page when we get around to
1047 * establishing additional references. We are usually the only one
1048 * holding a reference to newpage at this point. We used to have a BUG
1049 * here if trylock_page(newpage) fails, but would like to allow for
1050 * cases where there might be a race with the previous use of newpage.
1051 * This is much like races on refcount of oldpage: just don't BUG().
1052 */
1053 if (unlikely(!trylock_page(newpage)))
1054 goto out_unlock;
1055
1056 if (unlikely(!is_lru)) {
1057 rc = move_to_new_page(newpage, page, mode);
1058 goto out_unlock_both;
1059 }
1060
1061 /*
1062 * Corner case handling:
1063 * 1. When a new swap-cache page is read into, it is added to the LRU
1064 * and treated as swapcache but it has no rmap yet.
1065 * Calling try_to_unmap() against a page->mapping==NULL page will
1066 * trigger a BUG. So handle it here.
1067 * 2. An orphaned page (see truncate_complete_page) might have
1068 * fs-private metadata. The page can be picked up due to memory
1069 * offlining. Everywhere else except page reclaim, the page is
1070 * invisible to the vm, so the page can not be migrated. So try to
1071 * free the metadata, so the page can be freed.
1072 */
1073 if (!page->mapping) {
1074 VM_BUG_ON_PAGE(PageAnon(page), page);
1075 if (page_has_private(page)) {
1076 try_to_free_buffers(page);
1077 goto out_unlock_both;
1078 }
1079 } else if (page_mapped(page)) {
1080 /* Establish migration ptes */
1081 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1082 page);
1083 try_to_unmap(page,
1084 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
1085 page_was_mapped = 1;
1086 }
1087
1088 if (!page_mapped(page))
1089 rc = move_to_new_page(newpage, page, mode);
1090
1091 if (page_was_mapped)
1092 remove_migration_ptes(page,
1093 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
1094
1095 out_unlock_both:
1096 unlock_page(newpage);
1097 out_unlock:
1098 /* Drop an anon_vma reference if we took one */
1099 if (anon_vma)
1100 put_anon_vma(anon_vma);
1101 unlock_page(page);
1102 out:
1103 /*
1104 * If migration is successful, decrease refcount of the newpage
1105 * which will not free the page because new page owner increased
1106 * refcounter. As well, if it is LRU page, add the page to LRU
1107 * list in here.
1108 */
1109 if (rc == MIGRATEPAGE_SUCCESS) {
1110 if (unlikely(__PageMovable(newpage)))
1111 put_page(newpage);
1112 else
1113 putback_lru_page(newpage);
1114 }
1115
1116 return rc;
1117 }
1118
1119 /*
1120 * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
1121 * around it.
1122 */
1123 #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
1124 #define ICE_noinline noinline
1125 #else
1126 #define ICE_noinline
1127 #endif
1128
1129 /*
1130 * Obtain the lock on page, remove all ptes and migrate the page
1131 * to the newly allocated page in newpage.
1132 */
1133 static ICE_noinline int unmap_and_move(new_page_t get_new_page,
1134 free_page_t put_new_page,
1135 unsigned long private, struct page *page,
1136 int force, enum migrate_mode mode,
1137 enum migrate_reason reason)
1138 {
1139 int rc = MIGRATEPAGE_SUCCESS;
1140 struct page *newpage;
1141
1142 newpage = get_new_page(page, private);
1143 if (!newpage)
1144 return -ENOMEM;
1145
1146 if (page_count(page) == 1) {
1147 /* page was freed from under us. So we are done. */
1148 ClearPageActive(page);
1149 ClearPageUnevictable(page);
1150 if (unlikely(__PageMovable(page))) {
1151 lock_page(page);
1152 if (!PageMovable(page))
1153 __ClearPageIsolated(page);
1154 unlock_page(page);
1155 }
1156 if (put_new_page)
1157 put_new_page(newpage, private);
1158 else
1159 put_page(newpage);
1160 goto out;
1161 }
1162
1163 if (unlikely(PageTransHuge(page) && !PageTransHuge(newpage))) {
1164 lock_page(page);
1165 rc = split_huge_page(page);
1166 unlock_page(page);
1167 if (rc)
1168 goto out;
1169 }
1170
1171 rc = __unmap_and_move(page, newpage, force, mode);
1172 if (rc == MIGRATEPAGE_SUCCESS)
1173 set_page_owner_migrate_reason(newpage, reason);
1174
1175 out:
1176 if (rc != -EAGAIN) {
1177 /*
1178 * A page that has been migrated has all references
1179 * removed and will be freed. A page that has not been
1180 * migrated will have kepts its references and be
1181 * restored.
1182 */
1183 list_del(&page->lru);
1184
1185 /*
1186 * Compaction can migrate also non-LRU pages which are
1187 * not accounted to NR_ISOLATED_*. They can be recognized
1188 * as __PageMovable
1189 */
1190 if (likely(!__PageMovable(page)))
1191 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1192 page_is_file_cache(page), -hpage_nr_pages(page));
1193 }
1194
1195 /*
1196 * If migration is successful, releases reference grabbed during
1197 * isolation. Otherwise, restore the page to right list unless
1198 * we want to retry.
1199 */
1200 if (rc == MIGRATEPAGE_SUCCESS) {
1201 put_page(page);
1202 if (reason == MR_MEMORY_FAILURE) {
1203 /*
1204 * Set PG_HWPoison on just freed page
1205 * intentionally. Although it's rather weird,
1206 * it's how HWPoison flag works at the moment.
1207 */
1208 if (!test_set_page_hwpoison(page))
1209 num_poisoned_pages_inc();
1210 }
1211 } else {
1212 if (rc != -EAGAIN) {
1213 if (likely(!__PageMovable(page))) {
1214 putback_lru_page(page);
1215 goto put_new;
1216 }
1217
1218 lock_page(page);
1219 if (PageMovable(page))
1220 putback_movable_page(page);
1221 else
1222 __ClearPageIsolated(page);
1223 unlock_page(page);
1224 put_page(page);
1225 }
1226 put_new:
1227 if (put_new_page)
1228 put_new_page(newpage, private);
1229 else
1230 put_page(newpage);
1231 }
1232
1233 return rc;
1234 }
1235
1236 /*
1237 * Counterpart of unmap_and_move_page() for hugepage migration.
1238 *
1239 * This function doesn't wait the completion of hugepage I/O
1240 * because there is no race between I/O and migration for hugepage.
1241 * Note that currently hugepage I/O occurs only in direct I/O
1242 * where no lock is held and PG_writeback is irrelevant,
1243 * and writeback status of all subpages are counted in the reference
1244 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1245 * under direct I/O, the reference of the head page is 512 and a bit more.)
1246 * This means that when we try to migrate hugepage whose subpages are
1247 * doing direct I/O, some references remain after try_to_unmap() and
1248 * hugepage migration fails without data corruption.
1249 *
1250 * There is also no race when direct I/O is issued on the page under migration,
1251 * because then pte is replaced with migration swap entry and direct I/O code
1252 * will wait in the page fault for migration to complete.
1253 */
1254 static int unmap_and_move_huge_page(new_page_t get_new_page,
1255 free_page_t put_new_page, unsigned long private,
1256 struct page *hpage, int force,
1257 enum migrate_mode mode, int reason)
1258 {
1259 int rc = -EAGAIN;
1260 int page_was_mapped = 0;
1261 struct page *new_hpage;
1262 struct anon_vma *anon_vma = NULL;
1263
1264 /*
1265 * Movability of hugepages depends on architectures and hugepage size.
1266 * This check is necessary because some callers of hugepage migration
1267 * like soft offline and memory hotremove don't walk through page
1268 * tables or check whether the hugepage is pmd-based or not before
1269 * kicking migration.
1270 */
1271 if (!hugepage_migration_supported(page_hstate(hpage))) {
1272 putback_active_hugepage(hpage);
1273 return -ENOSYS;
1274 }
1275
1276 new_hpage = get_new_page(hpage, private);
1277 if (!new_hpage)
1278 return -ENOMEM;
1279
1280 if (!trylock_page(hpage)) {
1281 if (!force)
1282 goto out;
1283 switch (mode) {
1284 case MIGRATE_SYNC:
1285 case MIGRATE_SYNC_NO_COPY:
1286 break;
1287 default:
1288 goto out;
1289 }
1290 lock_page(hpage);
1291 }
1292
1293 if (PageAnon(hpage))
1294 anon_vma = page_get_anon_vma(hpage);
1295
1296 if (unlikely(!trylock_page(new_hpage)))
1297 goto put_anon;
1298
1299 if (page_mapped(hpage)) {
1300 try_to_unmap(hpage,
1301 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
1302 page_was_mapped = 1;
1303 }
1304
1305 if (!page_mapped(hpage))
1306 rc = move_to_new_page(new_hpage, hpage, mode);
1307
1308 if (page_was_mapped)
1309 remove_migration_ptes(hpage,
1310 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
1311
1312 unlock_page(new_hpage);
1313
1314 put_anon:
1315 if (anon_vma)
1316 put_anon_vma(anon_vma);
1317
1318 if (rc == MIGRATEPAGE_SUCCESS) {
1319 move_hugetlb_state(hpage, new_hpage, reason);
1320 put_new_page = NULL;
1321 }
1322
1323 unlock_page(hpage);
1324 out:
1325 if (rc != -EAGAIN)
1326 putback_active_hugepage(hpage);
1327 if (reason == MR_MEMORY_FAILURE && !test_set_page_hwpoison(hpage))
1328 num_poisoned_pages_inc();
1329
1330 /*
1331 * If migration was not successful and there's a freeing callback, use
1332 * it. Otherwise, put_page() will drop the reference grabbed during
1333 * isolation.
1334 */
1335 if (put_new_page)
1336 put_new_page(new_hpage, private);
1337 else
1338 putback_active_hugepage(new_hpage);
1339
1340 return rc;
1341 }
1342
1343 /*
1344 * migrate_pages - migrate the pages specified in a list, to the free pages
1345 * supplied as the target for the page migration
1346 *
1347 * @from: The list of pages to be migrated.
1348 * @get_new_page: The function used to allocate free pages to be used
1349 * as the target of the page migration.
1350 * @put_new_page: The function used to free target pages if migration
1351 * fails, or NULL if no special handling is necessary.
1352 * @private: Private data to be passed on to get_new_page()
1353 * @mode: The migration mode that specifies the constraints for
1354 * page migration, if any.
1355 * @reason: The reason for page migration.
1356 *
1357 * The function returns after 10 attempts or if no pages are movable any more
1358 * because the list has become empty or no retryable pages exist any more.
1359 * The caller should call putback_movable_pages() to return pages to the LRU
1360 * or free list only if ret != 0.
1361 *
1362 * Returns the number of pages that were not migrated, or an error code.
1363 */
1364 int migrate_pages(struct list_head *from, new_page_t get_new_page,
1365 free_page_t put_new_page, unsigned long private,
1366 enum migrate_mode mode, int reason)
1367 {
1368 int retry = 1;
1369 int nr_failed = 0;
1370 int nr_succeeded = 0;
1371 int pass = 0;
1372 struct page *page;
1373 struct page *page2;
1374 int swapwrite = current->flags & PF_SWAPWRITE;
1375 int rc;
1376
1377 if (!swapwrite)
1378 current->flags |= PF_SWAPWRITE;
1379
1380 for(pass = 0; pass < 10 && retry; pass++) {
1381 retry = 0;
1382
1383 list_for_each_entry_safe(page, page2, from, lru) {
1384 cond_resched();
1385
1386 if (PageHuge(page))
1387 rc = unmap_and_move_huge_page(get_new_page,
1388 put_new_page, private, page,
1389 pass > 2, mode, reason);
1390 else
1391 rc = unmap_and_move(get_new_page, put_new_page,
1392 private, page, pass > 2, mode,
1393 reason);
1394
1395 switch(rc) {
1396 case -ENOMEM:
1397 nr_failed++;
1398 goto out;
1399 case -EAGAIN:
1400 retry++;
1401 break;
1402 case MIGRATEPAGE_SUCCESS:
1403 nr_succeeded++;
1404 break;
1405 default:
1406 /*
1407 * Permanent failure (-EBUSY, -ENOSYS, etc.):
1408 * unlike -EAGAIN case, the failed page is
1409 * removed from migration page list and not
1410 * retried in the next outer loop.
1411 */
1412 nr_failed++;
1413 break;
1414 }
1415 }
1416 }
1417 nr_failed += retry;
1418 rc = nr_failed;
1419 out:
1420 if (nr_succeeded)
1421 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1422 if (nr_failed)
1423 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1424 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1425
1426 if (!swapwrite)
1427 current->flags &= ~PF_SWAPWRITE;
1428
1429 return rc;
1430 }
1431
1432 #ifdef CONFIG_NUMA
1433
1434 static int store_status(int __user *status, int start, int value, int nr)
1435 {
1436 while (nr-- > 0) {
1437 if (put_user(value, status + start))
1438 return -EFAULT;
1439 start++;
1440 }
1441
1442 return 0;
1443 }
1444
1445 static int do_move_pages_to_node(struct mm_struct *mm,
1446 struct list_head *pagelist, int node)
1447 {
1448 int err;
1449
1450 if (list_empty(pagelist))
1451 return 0;
1452
1453 err = migrate_pages(pagelist, alloc_new_node_page, NULL, node,
1454 MIGRATE_SYNC, MR_SYSCALL);
1455 if (err)
1456 putback_movable_pages(pagelist);
1457 return err;
1458 }
1459
1460 /*
1461 * Resolves the given address to a struct page, isolates it from the LRU and
1462 * puts it to the given pagelist.
1463 * Returns -errno if the page cannot be found/isolated or 0 when it has been
1464 * queued or the page doesn't need to be migrated because it is already on
1465 * the target node
1466 */
1467 static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1468 int node, struct list_head *pagelist, bool migrate_all)
1469 {
1470 struct vm_area_struct *vma;
1471 struct page *page;
1472 unsigned int follflags;
1473 int err;
1474
1475 down_read(&mm->mmap_sem);
1476 err = -EFAULT;
1477 vma = find_vma(mm, addr);
1478 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1479 goto out;
1480
1481 /* FOLL_DUMP to ignore special (like zero) pages */
1482 follflags = FOLL_GET | FOLL_DUMP;
1483 if (!thp_migration_supported())
1484 follflags |= FOLL_SPLIT;
1485 page = follow_page(vma, addr, follflags);
1486
1487 err = PTR_ERR(page);
1488 if (IS_ERR(page))
1489 goto out;
1490
1491 err = -ENOENT;
1492 if (!page)
1493 goto out;
1494
1495 err = 0;
1496 if (page_to_nid(page) == node)
1497 goto out_putpage;
1498
1499 err = -EACCES;
1500 if (page_mapcount(page) > 1 && !migrate_all)
1501 goto out_putpage;
1502
1503 if (PageHuge(page)) {
1504 if (PageHead(page)) {
1505 isolate_huge_page(page, pagelist);
1506 err = 0;
1507 }
1508 } else {
1509 struct page *head;
1510
1511 head = compound_head(page);
1512 err = isolate_lru_page(head);
1513 if (err)
1514 goto out_putpage;
1515
1516 err = 0;
1517 list_add_tail(&head->lru, pagelist);
1518 mod_node_page_state(page_pgdat(head),
1519 NR_ISOLATED_ANON + page_is_file_cache(head),
1520 hpage_nr_pages(head));
1521 }
1522 out_putpage:
1523 /*
1524 * Either remove the duplicate refcount from
1525 * isolate_lru_page() or drop the page ref if it was
1526 * not isolated.
1527 */
1528 put_page(page);
1529 out:
1530 up_read(&mm->mmap_sem);
1531 return err;
1532 }
1533
1534 /*
1535 * Migrate an array of page address onto an array of nodes and fill
1536 * the corresponding array of status.
1537 */
1538 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
1539 unsigned long nr_pages,
1540 const void __user * __user *pages,
1541 const int __user *nodes,
1542 int __user *status, int flags)
1543 {
1544 int current_node = NUMA_NO_NODE;
1545 LIST_HEAD(pagelist);
1546 int start, i;
1547 int err = 0, err1;
1548
1549 migrate_prep();
1550
1551 for (i = start = 0; i < nr_pages; i++) {
1552 const void __user *p;
1553 unsigned long addr;
1554 int node;
1555
1556 err = -EFAULT;
1557 if (get_user(p, pages + i))
1558 goto out_flush;
1559 if (get_user(node, nodes + i))
1560 goto out_flush;
1561 addr = (unsigned long)p;
1562
1563 err = -ENODEV;
1564 if (node < 0 || node >= MAX_NUMNODES)
1565 goto out_flush;
1566 if (!node_state(node, N_MEMORY))
1567 goto out_flush;
1568
1569 err = -EACCES;
1570 if (!node_isset(node, task_nodes))
1571 goto out_flush;
1572
1573 if (current_node == NUMA_NO_NODE) {
1574 current_node = node;
1575 start = i;
1576 } else if (node != current_node) {
1577 err = do_move_pages_to_node(mm, &pagelist, current_node);
1578 if (err)
1579 goto out;
1580 err = store_status(status, start, current_node, i - start);
1581 if (err)
1582 goto out;
1583 start = i;
1584 current_node = node;
1585 }
1586
1587 /*
1588 * Errors in the page lookup or isolation are not fatal and we simply
1589 * report them via status
1590 */
1591 err = add_page_for_migration(mm, addr, current_node,
1592 &pagelist, flags & MPOL_MF_MOVE_ALL);
1593 if (!err)
1594 continue;
1595
1596 err = store_status(status, i, err, 1);
1597 if (err)
1598 goto out_flush;
1599
1600 err = do_move_pages_to_node(mm, &pagelist, current_node);
1601 if (err)
1602 goto out;
1603 if (i > start) {
1604 err = store_status(status, start, current_node, i - start);
1605 if (err)
1606 goto out;
1607 }
1608 current_node = NUMA_NO_NODE;
1609 }
1610 out_flush:
1611 /* Make sure we do not overwrite the existing error */
1612 err1 = do_move_pages_to_node(mm, &pagelist, current_node);
1613 if (!err1)
1614 err1 = store_status(status, start, current_node, i - start);
1615 if (!err)
1616 err = err1;
1617 out:
1618 return err;
1619 }
1620
1621 /*
1622 * Determine the nodes of an array of pages and store it in an array of status.
1623 */
1624 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1625 const void __user **pages, int *status)
1626 {
1627 unsigned long i;
1628
1629 down_read(&mm->mmap_sem);
1630
1631 for (i = 0; i < nr_pages; i++) {
1632 unsigned long addr = (unsigned long)(*pages);
1633 struct vm_area_struct *vma;
1634 struct page *page;
1635 int err = -EFAULT;
1636
1637 vma = find_vma(mm, addr);
1638 if (!vma || addr < vma->vm_start)
1639 goto set_status;
1640
1641 /* FOLL_DUMP to ignore special (like zero) pages */
1642 page = follow_page(vma, addr, FOLL_DUMP);
1643
1644 err = PTR_ERR(page);
1645 if (IS_ERR(page))
1646 goto set_status;
1647
1648 err = page ? page_to_nid(page) : -ENOENT;
1649 set_status:
1650 *status = err;
1651
1652 pages++;
1653 status++;
1654 }
1655
1656 up_read(&mm->mmap_sem);
1657 }
1658
1659 /*
1660 * Determine the nodes of a user array of pages and store it in
1661 * a user array of status.
1662 */
1663 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1664 const void __user * __user *pages,
1665 int __user *status)
1666 {
1667 #define DO_PAGES_STAT_CHUNK_NR 16
1668 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1669 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1670
1671 while (nr_pages) {
1672 unsigned long chunk_nr;
1673
1674 chunk_nr = nr_pages;
1675 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1676 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1677
1678 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1679 break;
1680
1681 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1682
1683 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1684 break;
1685
1686 pages += chunk_nr;
1687 status += chunk_nr;
1688 nr_pages -= chunk_nr;
1689 }
1690 return nr_pages ? -EFAULT : 0;
1691 }
1692
1693 /*
1694 * Move a list of pages in the address space of the currently executing
1695 * process.
1696 */
1697 static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
1698 const void __user * __user *pages,
1699 const int __user *nodes,
1700 int __user *status, int flags)
1701 {
1702 struct task_struct *task;
1703 struct mm_struct *mm;
1704 int err;
1705 nodemask_t task_nodes;
1706
1707 /* Check flags */
1708 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1709 return -EINVAL;
1710
1711 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1712 return -EPERM;
1713
1714 /* Find the mm_struct */
1715 rcu_read_lock();
1716 task = pid ? find_task_by_vpid(pid) : current;
1717 if (!task) {
1718 rcu_read_unlock();
1719 return -ESRCH;
1720 }
1721 get_task_struct(task);
1722
1723 /*
1724 * Check if this process has the right to modify the specified
1725 * process. Use the regular "ptrace_may_access()" checks.
1726 */
1727 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
1728 rcu_read_unlock();
1729 err = -EPERM;
1730 goto out;
1731 }
1732 rcu_read_unlock();
1733
1734 err = security_task_movememory(task);
1735 if (err)
1736 goto out;
1737
1738 task_nodes = cpuset_mems_allowed(task);
1739 mm = get_task_mm(task);
1740 put_task_struct(task);
1741
1742 if (!mm)
1743 return -EINVAL;
1744
1745 if (nodes)
1746 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1747 nodes, status, flags);
1748 else
1749 err = do_pages_stat(mm, nr_pages, pages, status);
1750
1751 mmput(mm);
1752 return err;
1753
1754 out:
1755 put_task_struct(task);
1756 return err;
1757 }
1758
1759 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1760 const void __user * __user *, pages,
1761 const int __user *, nodes,
1762 int __user *, status, int, flags)
1763 {
1764 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
1765 }
1766
1767 #ifdef CONFIG_COMPAT
1768 COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1769 compat_uptr_t __user *, pages32,
1770 const int __user *, nodes,
1771 int __user *, status,
1772 int, flags)
1773 {
1774 const void __user * __user *pages;
1775 int i;
1776
1777 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1778 for (i = 0; i < nr_pages; i++) {
1779 compat_uptr_t p;
1780
1781 if (get_user(p, pages32 + i) ||
1782 put_user(compat_ptr(p), pages + i))
1783 return -EFAULT;
1784 }
1785 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
1786 }
1787 #endif /* CONFIG_COMPAT */
1788
1789 #ifdef CONFIG_NUMA_BALANCING
1790 /*
1791 * Returns true if this is a safe migration target node for misplaced NUMA
1792 * pages. Currently it only checks the watermarks which crude
1793 */
1794 static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
1795 unsigned long nr_migrate_pages)
1796 {
1797 int z;
1798
1799 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1800 struct zone *zone = pgdat->node_zones + z;
1801
1802 if (!populated_zone(zone))
1803 continue;
1804
1805 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1806 if (!zone_watermark_ok(zone, 0,
1807 high_wmark_pages(zone) +
1808 nr_migrate_pages,
1809 0, 0))
1810 continue;
1811 return true;
1812 }
1813 return false;
1814 }
1815
1816 static struct page *alloc_misplaced_dst_page(struct page *page,
1817 unsigned long data)
1818 {
1819 int nid = (int) data;
1820 struct page *newpage;
1821
1822 newpage = __alloc_pages_node(nid,
1823 (GFP_HIGHUSER_MOVABLE |
1824 __GFP_THISNODE | __GFP_NOMEMALLOC |
1825 __GFP_NORETRY | __GFP_NOWARN) &
1826 ~__GFP_RECLAIM, 0);
1827
1828 return newpage;
1829 }
1830
1831 /*
1832 * page migration rate limiting control.
1833 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1834 * window of time. Default here says do not migrate more than 1280M per second.
1835 */
1836 static unsigned int migrate_interval_millisecs __read_mostly = 100;
1837 static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1838
1839 /* Returns true if the node is migrate rate-limited after the update */
1840 static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
1841 unsigned long nr_pages)
1842 {
1843 /*
1844 * Rate-limit the amount of data that is being migrated to a node.
1845 * Optimal placement is no good if the memory bus is saturated and
1846 * all the time is being spent migrating!
1847 */
1848 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
1849 spin_lock(&pgdat->numabalancing_migrate_lock);
1850 pgdat->numabalancing_migrate_nr_pages = 0;
1851 pgdat->numabalancing_migrate_next_window = jiffies +
1852 msecs_to_jiffies(migrate_interval_millisecs);
1853 spin_unlock(&pgdat->numabalancing_migrate_lock);
1854 }
1855 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
1856 trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
1857 nr_pages);
1858 return true;
1859 }
1860
1861 /*
1862 * This is an unlocked non-atomic update so errors are possible.
1863 * The consequences are failing to migrate when we potentiall should
1864 * have which is not severe enough to warrant locking. If it is ever
1865 * a problem, it can be converted to a per-cpu counter.
1866 */
1867 pgdat->numabalancing_migrate_nr_pages += nr_pages;
1868 return false;
1869 }
1870
1871 static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
1872 {
1873 int page_lru;
1874
1875 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
1876
1877 /* Avoid migrating to a node that is nearly full */
1878 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1879 return 0;
1880
1881 if (isolate_lru_page(page))
1882 return 0;
1883
1884 /*
1885 * migrate_misplaced_transhuge_page() skips page migration's usual
1886 * check on page_count(), so we must do it here, now that the page
1887 * has been isolated: a GUP pin, or any other pin, prevents migration.
1888 * The expected page count is 3: 1 for page's mapcount and 1 for the
1889 * caller's pin and 1 for the reference taken by isolate_lru_page().
1890 */
1891 if (PageTransHuge(page) && page_count(page) != 3) {
1892 putback_lru_page(page);
1893 return 0;
1894 }
1895
1896 page_lru = page_is_file_cache(page);
1897 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
1898 hpage_nr_pages(page));
1899
1900 /*
1901 * Isolating the page has taken another reference, so the
1902 * caller's reference can be safely dropped without the page
1903 * disappearing underneath us during migration.
1904 */
1905 put_page(page);
1906 return 1;
1907 }
1908
1909 bool pmd_trans_migrating(pmd_t pmd)
1910 {
1911 struct page *page = pmd_page(pmd);
1912 return PageLocked(page);
1913 }
1914
1915 /*
1916 * Attempt to migrate a misplaced page to the specified destination
1917 * node. Caller is expected to have an elevated reference count on
1918 * the page that will be dropped by this function before returning.
1919 */
1920 int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
1921 int node)
1922 {
1923 pg_data_t *pgdat = NODE_DATA(node);
1924 int isolated;
1925 int nr_remaining;
1926 LIST_HEAD(migratepages);
1927
1928 /*
1929 * Don't migrate file pages that are mapped in multiple processes
1930 * with execute permissions as they are probably shared libraries.
1931 */
1932 if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
1933 (vma->vm_flags & VM_EXEC))
1934 goto out;
1935
1936 /*
1937 * Also do not migrate dirty pages as not all filesystems can move
1938 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
1939 */
1940 if (page_is_file_cache(page) && PageDirty(page))
1941 goto out;
1942
1943 /*
1944 * Rate-limit the amount of data that is being migrated to a node.
1945 * Optimal placement is no good if the memory bus is saturated and
1946 * all the time is being spent migrating!
1947 */
1948 if (numamigrate_update_ratelimit(pgdat, 1))
1949 goto out;
1950
1951 isolated = numamigrate_isolate_page(pgdat, page);
1952 if (!isolated)
1953 goto out;
1954
1955 list_add(&page->lru, &migratepages);
1956 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
1957 NULL, node, MIGRATE_ASYNC,
1958 MR_NUMA_MISPLACED);
1959 if (nr_remaining) {
1960 if (!list_empty(&migratepages)) {
1961 list_del(&page->lru);
1962 dec_node_page_state(page, NR_ISOLATED_ANON +
1963 page_is_file_cache(page));
1964 putback_lru_page(page);
1965 }
1966 isolated = 0;
1967 } else
1968 count_vm_numa_event(NUMA_PAGE_MIGRATE);
1969 BUG_ON(!list_empty(&migratepages));
1970 return isolated;
1971
1972 out:
1973 put_page(page);
1974 return 0;
1975 }
1976 #endif /* CONFIG_NUMA_BALANCING */
1977
1978 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1979 /*
1980 * Migrates a THP to a given target node. page must be locked and is unlocked
1981 * before returning.
1982 */
1983 int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1984 struct vm_area_struct *vma,
1985 pmd_t *pmd, pmd_t entry,
1986 unsigned long address,
1987 struct page *page, int node)
1988 {
1989 spinlock_t *ptl;
1990 pg_data_t *pgdat = NODE_DATA(node);
1991 int isolated = 0;
1992 struct page *new_page = NULL;
1993 int page_lru = page_is_file_cache(page);
1994 unsigned long mmun_start = address & HPAGE_PMD_MASK;
1995 unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
1996
1997 /*
1998 * Rate-limit the amount of data that is being migrated to a node.
1999 * Optimal placement is no good if the memory bus is saturated and
2000 * all the time is being spent migrating!
2001 */
2002 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
2003 goto out_dropref;
2004
2005 new_page = alloc_pages_node(node,
2006 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2007 HPAGE_PMD_ORDER);
2008 if (!new_page)
2009 goto out_fail;
2010 prep_transhuge_page(new_page);
2011
2012 isolated = numamigrate_isolate_page(pgdat, page);
2013 if (!isolated) {
2014 put_page(new_page);
2015 goto out_fail;
2016 }
2017
2018 /* Prepare a page as a migration target */
2019 __SetPageLocked(new_page);
2020 if (PageSwapBacked(page))
2021 __SetPageSwapBacked(new_page);
2022
2023 /* anon mapping, we can simply copy page->mapping to the new page: */
2024 new_page->mapping = page->mapping;
2025 new_page->index = page->index;
2026 migrate_page_copy(new_page, page);
2027 WARN_ON(PageLRU(new_page));
2028
2029 /* Recheck the target PMD */
2030 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2031 ptl = pmd_lock(mm, pmd);
2032 if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
2033 spin_unlock(ptl);
2034 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2035
2036 /* Reverse changes made by migrate_page_copy() */
2037 if (TestClearPageActive(new_page))
2038 SetPageActive(page);
2039 if (TestClearPageUnevictable(new_page))
2040 SetPageUnevictable(page);
2041
2042 unlock_page(new_page);
2043 put_page(new_page); /* Free it */
2044
2045 /* Retake the callers reference and putback on LRU */
2046 get_page(page);
2047 putback_lru_page(page);
2048 mod_node_page_state(page_pgdat(page),
2049 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
2050
2051 goto out_unlock;
2052 }
2053
2054 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
2055 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
2056
2057 /*
2058 * Clear the old entry under pagetable lock and establish the new PTE.
2059 * Any parallel GUP will either observe the old page blocking on the
2060 * page lock, block on the page table lock or observe the new page.
2061 * The SetPageUptodate on the new page and page_add_new_anon_rmap
2062 * guarantee the copy is visible before the pagetable update.
2063 */
2064 flush_cache_range(vma, mmun_start, mmun_end);
2065 page_add_anon_rmap(new_page, vma, mmun_start, true);
2066 pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
2067 set_pmd_at(mm, mmun_start, pmd, entry);
2068 update_mmu_cache_pmd(vma, address, &entry);
2069
2070 page_ref_unfreeze(page, 2);
2071 mlock_migrate_page(new_page, page);
2072 page_remove_rmap(page, true);
2073 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
2074
2075 spin_unlock(ptl);
2076 /*
2077 * No need to double call mmu_notifier->invalidate_range() callback as
2078 * the above pmdp_huge_clear_flush_notify() did already call it.
2079 */
2080 mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end);
2081
2082 /* Take an "isolate" reference and put new page on the LRU. */
2083 get_page(new_page);
2084 putback_lru_page(new_page);
2085
2086 unlock_page(new_page);
2087 unlock_page(page);
2088 put_page(page); /* Drop the rmap reference */
2089 put_page(page); /* Drop the LRU isolation reference */
2090
2091 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
2092 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
2093
2094 mod_node_page_state(page_pgdat(page),
2095 NR_ISOLATED_ANON + page_lru,
2096 -HPAGE_PMD_NR);
2097 return isolated;
2098
2099 out_fail:
2100 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
2101 out_dropref:
2102 ptl = pmd_lock(mm, pmd);
2103 if (pmd_same(*pmd, entry)) {
2104 entry = pmd_modify(entry, vma->vm_page_prot);
2105 set_pmd_at(mm, mmun_start, pmd, entry);
2106 update_mmu_cache_pmd(vma, address, &entry);
2107 }
2108 spin_unlock(ptl);
2109
2110 out_unlock:
2111 unlock_page(page);
2112 put_page(page);
2113 return 0;
2114 }
2115 #endif /* CONFIG_NUMA_BALANCING */
2116
2117 #endif /* CONFIG_NUMA */
2118
2119 #if defined(CONFIG_MIGRATE_VMA_HELPER)
2120 struct migrate_vma {
2121 struct vm_area_struct *vma;
2122 unsigned long *dst;
2123 unsigned long *src;
2124 unsigned long cpages;
2125 unsigned long npages;
2126 unsigned long start;
2127 unsigned long end;
2128 };
2129
2130 static int migrate_vma_collect_hole(unsigned long start,
2131 unsigned long end,
2132 struct mm_walk *walk)
2133 {
2134 struct migrate_vma *migrate = walk->private;
2135 unsigned long addr;
2136
2137 for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
2138 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2139 migrate->dst[migrate->npages] = 0;
2140 migrate->npages++;
2141 migrate->cpages++;
2142 }
2143
2144 return 0;
2145 }
2146
2147 static int migrate_vma_collect_skip(unsigned long start,
2148 unsigned long end,
2149 struct mm_walk *walk)
2150 {
2151 struct migrate_vma *migrate = walk->private;
2152 unsigned long addr;
2153
2154 for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
2155 migrate->dst[migrate->npages] = 0;
2156 migrate->src[migrate->npages++] = 0;
2157 }
2158
2159 return 0;
2160 }
2161
2162 static int migrate_vma_collect_pmd(pmd_t *pmdp,
2163 unsigned long start,
2164 unsigned long end,
2165 struct mm_walk *walk)
2166 {
2167 struct migrate_vma *migrate = walk->private;
2168 struct vm_area_struct *vma = walk->vma;
2169 struct mm_struct *mm = vma->vm_mm;
2170 unsigned long addr = start, unmapped = 0;
2171 spinlock_t *ptl;
2172 pte_t *ptep;
2173
2174 again:
2175 if (pmd_none(*pmdp))
2176 return migrate_vma_collect_hole(start, end, walk);
2177
2178 if (pmd_trans_huge(*pmdp)) {
2179 struct page *page;
2180
2181 ptl = pmd_lock(mm, pmdp);
2182 if (unlikely(!pmd_trans_huge(*pmdp))) {
2183 spin_unlock(ptl);
2184 goto again;
2185 }
2186
2187 page = pmd_page(*pmdp);
2188 if (is_huge_zero_page(page)) {
2189 spin_unlock(ptl);
2190 split_huge_pmd(vma, pmdp, addr);
2191 if (pmd_trans_unstable(pmdp))
2192 return migrate_vma_collect_skip(start, end,
2193 walk);
2194 } else {
2195 int ret;
2196
2197 get_page(page);
2198 spin_unlock(ptl);
2199 if (unlikely(!trylock_page(page)))
2200 return migrate_vma_collect_skip(start, end,
2201 walk);
2202 ret = split_huge_page(page);
2203 unlock_page(page);
2204 put_page(page);
2205 if (ret)
2206 return migrate_vma_collect_skip(start, end,
2207 walk);
2208 if (pmd_none(*pmdp))
2209 return migrate_vma_collect_hole(start, end,
2210 walk);
2211 }
2212 }
2213
2214 if (unlikely(pmd_bad(*pmdp)))
2215 return migrate_vma_collect_skip(start, end, walk);
2216
2217 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2218 arch_enter_lazy_mmu_mode();
2219
2220 for (; addr < end; addr += PAGE_SIZE, ptep++) {
2221 unsigned long mpfn, pfn;
2222 struct page *page;
2223 swp_entry_t entry;
2224 pte_t pte;
2225
2226 pte = *ptep;
2227 pfn = pte_pfn(pte);
2228
2229 if (pte_none(pte)) {
2230 mpfn = MIGRATE_PFN_MIGRATE;
2231 migrate->cpages++;
2232 pfn = 0;
2233 goto next;
2234 }
2235
2236 if (!pte_present(pte)) {
2237 mpfn = pfn = 0;
2238
2239 /*
2240 * Only care about unaddressable device page special
2241 * page table entry. Other special swap entries are not
2242 * migratable, and we ignore regular swapped page.
2243 */
2244 entry = pte_to_swp_entry(pte);
2245 if (!is_device_private_entry(entry))
2246 goto next;
2247
2248 page = device_private_entry_to_page(entry);
2249 mpfn = migrate_pfn(page_to_pfn(page))|
2250 MIGRATE_PFN_DEVICE | MIGRATE_PFN_MIGRATE;
2251 if (is_write_device_private_entry(entry))
2252 mpfn |= MIGRATE_PFN_WRITE;
2253 } else {
2254 if (is_zero_pfn(pfn)) {
2255 mpfn = MIGRATE_PFN_MIGRATE;
2256 migrate->cpages++;
2257 pfn = 0;
2258 goto next;
2259 }
2260 page = _vm_normal_page(migrate->vma, addr, pte, true);
2261 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2262 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2263 }
2264
2265 /* FIXME support THP */
2266 if (!page || !page->mapping || PageTransCompound(page)) {
2267 mpfn = pfn = 0;
2268 goto next;
2269 }
2270 pfn = page_to_pfn(page);
2271
2272 /*
2273 * By getting a reference on the page we pin it and that blocks
2274 * any kind of migration. Side effect is that it "freezes" the
2275 * pte.
2276 *
2277 * We drop this reference after isolating the page from the lru
2278 * for non device page (device page are not on the lru and thus
2279 * can't be dropped from it).
2280 */
2281 get_page(page);
2282 migrate->cpages++;
2283
2284 /*
2285 * Optimize for the common case where page is only mapped once
2286 * in one process. If we can lock the page, then we can safely
2287 * set up a special migration page table entry now.
2288 */
2289 if (trylock_page(page)) {
2290 pte_t swp_pte;
2291
2292 mpfn |= MIGRATE_PFN_LOCKED;
2293 ptep_get_and_clear(mm, addr, ptep);
2294
2295 /* Setup special migration page table entry */
2296 entry = make_migration_entry(page, mpfn &
2297 MIGRATE_PFN_WRITE);
2298 swp_pte = swp_entry_to_pte(entry);
2299 if (pte_soft_dirty(pte))
2300 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2301 set_pte_at(mm, addr, ptep, swp_pte);
2302
2303 /*
2304 * This is like regular unmap: we remove the rmap and
2305 * drop page refcount. Page won't be freed, as we took
2306 * a reference just above.
2307 */
2308 page_remove_rmap(page, false);
2309 put_page(page);
2310
2311 if (pte_present(pte))
2312 unmapped++;
2313 }
2314
2315 next:
2316 migrate->dst[migrate->npages] = 0;
2317 migrate->src[migrate->npages++] = mpfn;
2318 }
2319 arch_leave_lazy_mmu_mode();
2320 pte_unmap_unlock(ptep - 1, ptl);
2321
2322 /* Only flush the TLB if we actually modified any entries */
2323 if (unmapped)
2324 flush_tlb_range(walk->vma, start, end);
2325
2326 return 0;
2327 }
2328
2329 /*
2330 * migrate_vma_collect() - collect pages over a range of virtual addresses
2331 * @migrate: migrate struct containing all migration information
2332 *
2333 * This will walk the CPU page table. For each virtual address backed by a
2334 * valid page, it updates the src array and takes a reference on the page, in
2335 * order to pin the page until we lock it and unmap it.
2336 */
2337 static void migrate_vma_collect(struct migrate_vma *migrate)
2338 {
2339 struct mm_walk mm_walk;
2340
2341 mm_walk.pmd_entry = migrate_vma_collect_pmd;
2342 mm_walk.pte_entry = NULL;
2343 mm_walk.pte_hole = migrate_vma_collect_hole;
2344 mm_walk.hugetlb_entry = NULL;
2345 mm_walk.test_walk = NULL;
2346 mm_walk.vma = migrate->vma;
2347 mm_walk.mm = migrate->vma->vm_mm;
2348 mm_walk.private = migrate;
2349
2350 mmu_notifier_invalidate_range_start(mm_walk.mm,
2351 migrate->start,
2352 migrate->end);
2353 walk_page_range(migrate->start, migrate->end, &mm_walk);
2354 mmu_notifier_invalidate_range_end(mm_walk.mm,
2355 migrate->start,
2356 migrate->end);
2357
2358 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2359 }
2360
2361 /*
2362 * migrate_vma_check_page() - check if page is pinned or not
2363 * @page: struct page to check
2364 *
2365 * Pinned pages cannot be migrated. This is the same test as in
2366 * migrate_page_move_mapping(), except that here we allow migration of a
2367 * ZONE_DEVICE page.
2368 */
2369 static bool migrate_vma_check_page(struct page *page)
2370 {
2371 /*
2372 * One extra ref because caller holds an extra reference, either from
2373 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2374 * a device page.
2375 */
2376 int extra = 1;
2377
2378 /*
2379 * FIXME support THP (transparent huge page), it is bit more complex to
2380 * check them than regular pages, because they can be mapped with a pmd
2381 * or with a pte (split pte mapping).
2382 */
2383 if (PageCompound(page))
2384 return false;
2385
2386 /* Page from ZONE_DEVICE have one extra reference */
2387 if (is_zone_device_page(page)) {
2388 /*
2389 * Private page can never be pin as they have no valid pte and
2390 * GUP will fail for those. Yet if there is a pending migration
2391 * a thread might try to wait on the pte migration entry and
2392 * will bump the page reference count. Sadly there is no way to
2393 * differentiate a regular pin from migration wait. Hence to
2394 * avoid 2 racing thread trying to migrate back to CPU to enter
2395 * infinite loop (one stoping migration because the other is
2396 * waiting on pte migration entry). We always return true here.
2397 *
2398 * FIXME proper solution is to rework migration_entry_wait() so
2399 * it does not need to take a reference on page.
2400 */
2401 if (is_device_private_page(page))
2402 return true;
2403
2404 /*
2405 * Only allow device public page to be migrated and account for
2406 * the extra reference count imply by ZONE_DEVICE pages.
2407 */
2408 if (!is_device_public_page(page))
2409 return false;
2410 extra++;
2411 }
2412
2413 /* For file back page */
2414 if (page_mapping(page))
2415 extra += 1 + page_has_private(page);
2416
2417 if ((page_count(page) - extra) > page_mapcount(page))
2418 return false;
2419
2420 return true;
2421 }
2422
2423 /*
2424 * migrate_vma_prepare() - lock pages and isolate them from the lru
2425 * @migrate: migrate struct containing all migration information
2426 *
2427 * This locks pages that have been collected by migrate_vma_collect(). Once each
2428 * page is locked it is isolated from the lru (for non-device pages). Finally,
2429 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2430 * migrated by concurrent kernel threads.
2431 */
2432 static void migrate_vma_prepare(struct migrate_vma *migrate)
2433 {
2434 const unsigned long npages = migrate->npages;
2435 const unsigned long start = migrate->start;
2436 unsigned long addr, i, restore = 0;
2437 bool allow_drain = true;
2438
2439 lru_add_drain();
2440
2441 for (i = 0; (i < npages) && migrate->cpages; i++) {
2442 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2443 bool remap = true;
2444
2445 if (!page)
2446 continue;
2447
2448 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2449 /*
2450 * Because we are migrating several pages there can be
2451 * a deadlock between 2 concurrent migration where each
2452 * are waiting on each other page lock.
2453 *
2454 * Make migrate_vma() a best effort thing and backoff
2455 * for any page we can not lock right away.
2456 */
2457 if (!trylock_page(page)) {
2458 migrate->src[i] = 0;
2459 migrate->cpages--;
2460 put_page(page);
2461 continue;
2462 }
2463 remap = false;
2464 migrate->src[i] |= MIGRATE_PFN_LOCKED;
2465 }
2466
2467 /* ZONE_DEVICE pages are not on LRU */
2468 if (!is_zone_device_page(page)) {
2469 if (!PageLRU(page) && allow_drain) {
2470 /* Drain CPU's pagevec */
2471 lru_add_drain_all();
2472 allow_drain = false;
2473 }
2474
2475 if (isolate_lru_page(page)) {
2476 if (remap) {
2477 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2478 migrate->cpages--;
2479 restore++;
2480 } else {
2481 migrate->src[i] = 0;
2482 unlock_page(page);
2483 migrate->cpages--;
2484 put_page(page);
2485 }
2486 continue;
2487 }
2488
2489 /* Drop the reference we took in collect */
2490 put_page(page);
2491 }
2492
2493 if (!migrate_vma_check_page(page)) {
2494 if (remap) {
2495 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2496 migrate->cpages--;
2497 restore++;
2498
2499 if (!is_zone_device_page(page)) {
2500 get_page(page);
2501 putback_lru_page(page);
2502 }
2503 } else {
2504 migrate->src[i] = 0;
2505 unlock_page(page);
2506 migrate->cpages--;
2507
2508 if (!is_zone_device_page(page))
2509 putback_lru_page(page);
2510 else
2511 put_page(page);
2512 }
2513 }
2514 }
2515
2516 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2517 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2518
2519 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2520 continue;
2521
2522 remove_migration_pte(page, migrate->vma, addr, page);
2523
2524 migrate->src[i] = 0;
2525 unlock_page(page);
2526 put_page(page);
2527 restore--;
2528 }
2529 }
2530
2531 /*
2532 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2533 * @migrate: migrate struct containing all migration information
2534 *
2535 * Replace page mapping (CPU page table pte) with a special migration pte entry
2536 * and check again if it has been pinned. Pinned pages are restored because we
2537 * cannot migrate them.
2538 *
2539 * This is the last step before we call the device driver callback to allocate
2540 * destination memory and copy contents of original page over to new page.
2541 */
2542 static void migrate_vma_unmap(struct migrate_vma *migrate)
2543 {
2544 int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
2545 const unsigned long npages = migrate->npages;
2546 const unsigned long start = migrate->start;
2547 unsigned long addr, i, restore = 0;
2548
2549 for (i = 0; i < npages; i++) {
2550 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2551
2552 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2553 continue;
2554
2555 if (page_mapped(page)) {
2556 try_to_unmap(page, flags);
2557 if (page_mapped(page))
2558 goto restore;
2559 }
2560
2561 if (migrate_vma_check_page(page))
2562 continue;
2563
2564 restore:
2565 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2566 migrate->cpages--;
2567 restore++;
2568 }
2569
2570 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2571 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2572
2573 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2574 continue;
2575
2576 remove_migration_ptes(page, page, false);
2577
2578 migrate->src[i] = 0;
2579 unlock_page(page);
2580 restore--;
2581
2582 if (is_zone_device_page(page))
2583 put_page(page);
2584 else
2585 putback_lru_page(page);
2586 }
2587 }
2588
2589 static void migrate_vma_insert_page(struct migrate_vma *migrate,
2590 unsigned long addr,
2591 struct page *page,
2592 unsigned long *src,
2593 unsigned long *dst)
2594 {
2595 struct vm_area_struct *vma = migrate->vma;
2596 struct mm_struct *mm = vma->vm_mm;
2597 struct mem_cgroup *memcg;
2598 bool flush = false;
2599 spinlock_t *ptl;
2600 pte_t entry;
2601 pgd_t *pgdp;
2602 p4d_t *p4dp;
2603 pud_t *pudp;
2604 pmd_t *pmdp;
2605 pte_t *ptep;
2606
2607 /* Only allow populating anonymous memory */
2608 if (!vma_is_anonymous(vma))
2609 goto abort;
2610
2611 pgdp = pgd_offset(mm, addr);
2612 p4dp = p4d_alloc(mm, pgdp, addr);
2613 if (!p4dp)
2614 goto abort;
2615 pudp = pud_alloc(mm, p4dp, addr);
2616 if (!pudp)
2617 goto abort;
2618 pmdp = pmd_alloc(mm, pudp, addr);
2619 if (!pmdp)
2620 goto abort;
2621
2622 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2623 goto abort;
2624
2625 /*
2626 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2627 * pte_offset_map() on pmds where a huge pmd might be created
2628 * from a different thread.
2629 *
2630 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
2631 * parallel threads are excluded by other means.
2632 *
2633 * Here we only have down_read(mmap_sem).
2634 */
2635 if (pte_alloc(mm, pmdp, addr))
2636 goto abort;
2637
2638 /* See the comment in pte_alloc_one_map() */
2639 if (unlikely(pmd_trans_unstable(pmdp)))
2640 goto abort;
2641
2642 if (unlikely(anon_vma_prepare(vma)))
2643 goto abort;
2644 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
2645 goto abort;
2646
2647 /*
2648 * The memory barrier inside __SetPageUptodate makes sure that
2649 * preceding stores to the page contents become visible before
2650 * the set_pte_at() write.
2651 */
2652 __SetPageUptodate(page);
2653
2654 if (is_zone_device_page(page)) {
2655 if (is_device_private_page(page)) {
2656 swp_entry_t swp_entry;
2657
2658 swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE);
2659 entry = swp_entry_to_pte(swp_entry);
2660 } else if (is_device_public_page(page)) {
2661 entry = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
2662 if (vma->vm_flags & VM_WRITE)
2663 entry = pte_mkwrite(pte_mkdirty(entry));
2664 entry = pte_mkdevmap(entry);
2665 }
2666 } else {
2667 entry = mk_pte(page, vma->vm_page_prot);
2668 if (vma->vm_flags & VM_WRITE)
2669 entry = pte_mkwrite(pte_mkdirty(entry));
2670 }
2671
2672 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2673
2674 if (pte_present(*ptep)) {
2675 unsigned long pfn = pte_pfn(*ptep);
2676
2677 if (!is_zero_pfn(pfn)) {
2678 pte_unmap_unlock(ptep, ptl);
2679 mem_cgroup_cancel_charge(page, memcg, false);
2680 goto abort;
2681 }
2682 flush = true;
2683 } else if (!pte_none(*ptep)) {
2684 pte_unmap_unlock(ptep, ptl);
2685 mem_cgroup_cancel_charge(page, memcg, false);
2686 goto abort;
2687 }
2688
2689 /*
2690 * Check for usefaultfd but do not deliver the fault. Instead,
2691 * just back off.
2692 */
2693 if (userfaultfd_missing(vma)) {
2694 pte_unmap_unlock(ptep, ptl);
2695 mem_cgroup_cancel_charge(page, memcg, false);
2696 goto abort;
2697 }
2698
2699 inc_mm_counter(mm, MM_ANONPAGES);
2700 page_add_new_anon_rmap(page, vma, addr, false);
2701 mem_cgroup_commit_charge(page, memcg, false, false);
2702 if (!is_zone_device_page(page))
2703 lru_cache_add_active_or_unevictable(page, vma);
2704 get_page(page);
2705
2706 if (flush) {
2707 flush_cache_page(vma, addr, pte_pfn(*ptep));
2708 ptep_clear_flush_notify(vma, addr, ptep);
2709 set_pte_at_notify(mm, addr, ptep, entry);
2710 update_mmu_cache(vma, addr, ptep);
2711 } else {
2712 /* No need to invalidate - it was non-present before */
2713 set_pte_at(mm, addr, ptep, entry);
2714 update_mmu_cache(vma, addr, ptep);
2715 }
2716
2717 pte_unmap_unlock(ptep, ptl);
2718 *src = MIGRATE_PFN_MIGRATE;
2719 return;
2720
2721 abort:
2722 *src &= ~MIGRATE_PFN_MIGRATE;
2723 }
2724
2725 /*
2726 * migrate_vma_pages() - migrate meta-data from src page to dst page
2727 * @migrate: migrate struct containing all migration information
2728 *
2729 * This migrates struct page meta-data from source struct page to destination
2730 * struct page. This effectively finishes the migration from source page to the
2731 * destination page.
2732 */
2733 static void migrate_vma_pages(struct migrate_vma *migrate)
2734 {
2735 const unsigned long npages = migrate->npages;
2736 const unsigned long start = migrate->start;
2737 struct vm_area_struct *vma = migrate->vma;
2738 struct mm_struct *mm = vma->vm_mm;
2739 unsigned long addr, i, mmu_start;
2740 bool notified = false;
2741
2742 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
2743 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2744 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2745 struct address_space *mapping;
2746 int r;
2747
2748 if (!newpage) {
2749 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2750 continue;
2751 }
2752
2753 if (!page) {
2754 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) {
2755 continue;
2756 }
2757 if (!notified) {
2758 mmu_start = addr;
2759 notified = true;
2760 mmu_notifier_invalidate_range_start(mm,
2761 mmu_start,
2762 migrate->end);
2763 }
2764 migrate_vma_insert_page(migrate, addr, newpage,
2765 &migrate->src[i],
2766 &migrate->dst[i]);
2767 continue;
2768 }
2769
2770 mapping = page_mapping(page);
2771
2772 if (is_zone_device_page(newpage)) {
2773 if (is_device_private_page(newpage)) {
2774 /*
2775 * For now only support private anonymous when
2776 * migrating to un-addressable device memory.
2777 */
2778 if (mapping) {
2779 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2780 continue;
2781 }
2782 } else if (!is_device_public_page(newpage)) {
2783 /*
2784 * Other types of ZONE_DEVICE page are not
2785 * supported.
2786 */
2787 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2788 continue;
2789 }
2790 }
2791
2792 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
2793 if (r != MIGRATEPAGE_SUCCESS)
2794 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2795 }
2796
2797 /*
2798 * No need to double call mmu_notifier->invalidate_range() callback as
2799 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
2800 * did already call it.
2801 */
2802 if (notified)
2803 mmu_notifier_invalidate_range_only_end(mm, mmu_start,
2804 migrate->end);
2805 }
2806
2807 /*
2808 * migrate_vma_finalize() - restore CPU page table entry
2809 * @migrate: migrate struct containing all migration information
2810 *
2811 * This replaces the special migration pte entry with either a mapping to the
2812 * new page if migration was successful for that page, or to the original page
2813 * otherwise.
2814 *
2815 * This also unlocks the pages and puts them back on the lru, or drops the extra
2816 * refcount, for device pages.
2817 */
2818 static void migrate_vma_finalize(struct migrate_vma *migrate)
2819 {
2820 const unsigned long npages = migrate->npages;
2821 unsigned long i;
2822
2823 for (i = 0; i < npages; i++) {
2824 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2825 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2826
2827 if (!page) {
2828 if (newpage) {
2829 unlock_page(newpage);
2830 put_page(newpage);
2831 }
2832 continue;
2833 }
2834
2835 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
2836 if (newpage) {
2837 unlock_page(newpage);
2838 put_page(newpage);
2839 }
2840 newpage = page;
2841 }
2842
2843 remove_migration_ptes(page, newpage, false);
2844 unlock_page(page);
2845 migrate->cpages--;
2846
2847 if (is_zone_device_page(page))
2848 put_page(page);
2849 else
2850 putback_lru_page(page);
2851
2852 if (newpage != page) {
2853 unlock_page(newpage);
2854 if (is_zone_device_page(newpage))
2855 put_page(newpage);
2856 else
2857 putback_lru_page(newpage);
2858 }
2859 }
2860 }
2861
2862 /*
2863 * migrate_vma() - migrate a range of memory inside vma
2864 *
2865 * @ops: migration callback for allocating destination memory and copying
2866 * @vma: virtual memory area containing the range to be migrated
2867 * @start: start address of the range to migrate (inclusive)
2868 * @end: end address of the range to migrate (exclusive)
2869 * @src: array of hmm_pfn_t containing source pfns
2870 * @dst: array of hmm_pfn_t containing destination pfns
2871 * @private: pointer passed back to each of the callback
2872 * Returns: 0 on success, error code otherwise
2873 *
2874 * This function tries to migrate a range of memory virtual address range, using
2875 * callbacks to allocate and copy memory from source to destination. First it
2876 * collects all the pages backing each virtual address in the range, saving this
2877 * inside the src array. Then it locks those pages and unmaps them. Once the pages
2878 * are locked and unmapped, it checks whether each page is pinned or not. Pages
2879 * that aren't pinned have the MIGRATE_PFN_MIGRATE flag set (by this function)
2880 * in the corresponding src array entry. It then restores any pages that are
2881 * pinned, by remapping and unlocking those pages.
2882 *
2883 * At this point it calls the alloc_and_copy() callback. For documentation on
2884 * what is expected from that callback, see struct migrate_vma_ops comments in
2885 * include/linux/migrate.h
2886 *
2887 * After the alloc_and_copy() callback, this function goes over each entry in
2888 * the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2889 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2890 * then the function tries to migrate struct page information from the source
2891 * struct page to the destination struct page. If it fails to migrate the struct
2892 * page information, then it clears the MIGRATE_PFN_MIGRATE flag in the src
2893 * array.
2894 *
2895 * At this point all successfully migrated pages have an entry in the src
2896 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2897 * array entry with MIGRATE_PFN_VALID flag set.
2898 *
2899 * It then calls the finalize_and_map() callback. See comments for "struct
2900 * migrate_vma_ops", in include/linux/migrate.h for details about
2901 * finalize_and_map() behavior.
2902 *
2903 * After the finalize_and_map() callback, for successfully migrated pages, this
2904 * function updates the CPU page table to point to new pages, otherwise it
2905 * restores the CPU page table to point to the original source pages.
2906 *
2907 * Function returns 0 after the above steps, even if no pages were migrated
2908 * (The function only returns an error if any of the arguments are invalid.)
2909 *
2910 * Both src and dst array must be big enough for (end - start) >> PAGE_SHIFT
2911 * unsigned long entries.
2912 */
2913 int migrate_vma(const struct migrate_vma_ops *ops,
2914 struct vm_area_struct *vma,
2915 unsigned long start,
2916 unsigned long end,
2917 unsigned long *src,
2918 unsigned long *dst,
2919 void *private)
2920 {
2921 struct migrate_vma migrate;
2922
2923 /* Sanity check the arguments */
2924 start &= PAGE_MASK;
2925 end &= PAGE_MASK;
2926 if (!vma || is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL))
2927 return -EINVAL;
2928 if (start < vma->vm_start || start >= vma->vm_end)
2929 return -EINVAL;
2930 if (end <= vma->vm_start || end > vma->vm_end)
2931 return -EINVAL;
2932 if (!ops || !src || !dst || start >= end)
2933 return -EINVAL;
2934
2935 memset(src, 0, sizeof(*src) * ((end - start) >> PAGE_SHIFT));
2936 migrate.src = src;
2937 migrate.dst = dst;
2938 migrate.start = start;
2939 migrate.npages = 0;
2940 migrate.cpages = 0;
2941 migrate.end = end;
2942 migrate.vma = vma;
2943
2944 /* Collect, and try to unmap source pages */
2945 migrate_vma_collect(&migrate);
2946 if (!migrate.cpages)
2947 return 0;
2948
2949 /* Lock and isolate page */
2950 migrate_vma_prepare(&migrate);
2951 if (!migrate.cpages)
2952 return 0;
2953
2954 /* Unmap pages */
2955 migrate_vma_unmap(&migrate);
2956 if (!migrate.cpages)
2957 return 0;
2958
2959 /*
2960 * At this point pages are locked and unmapped, and thus they have
2961 * stable content and can safely be copied to destination memory that
2962 * is allocated by the callback.
2963 *
2964 * Note that migration can fail in migrate_vma_struct_page() for each
2965 * individual page.
2966 */
2967 ops->alloc_and_copy(vma, src, dst, start, end, private);
2968
2969 /* This does the real migration of struct page */
2970 migrate_vma_pages(&migrate);
2971
2972 ops->finalize_and_map(vma, src, dst, start, end, private);
2973
2974 /* Unlock and remap pages */
2975 migrate_vma_finalize(&migrate);
2976
2977 return 0;
2978 }
2979 EXPORT_SYMBOL(migrate_vma);
2980 #endif /* defined(MIGRATE_VMA_HELPER) */