1 // SPDX-License-Identifier: GPL-2.0
3 * Memory Migration functionality - linux/mm/migrate.c
5 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
7 * Page migration was first developed in the context of the memory hotplug
8 * project. The main authors of the migration code are:
10 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
11 * Hirokazu Takahashi <taka@valinux.co.jp>
12 * Dave Hansen <haveblue@us.ibm.com>
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/pagewalk.h>
42 #include <linux/pfn_t.h>
43 #include <linux/memremap.h>
44 #include <linux/userfaultfd_k.h>
45 #include <linux/balloon_compaction.h>
46 #include <linux/mmu_notifier.h>
47 #include <linux/page_idle.h>
48 #include <linux/page_owner.h>
49 #include <linux/sched/mm.h>
50 #include <linux/ptrace.h>
51 #include <linux/oom.h>
53 #include <asm/tlbflush.h>
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/migrate.h>
60 int isolate_movable_page(struct page
*page
, isolate_mode_t mode
)
62 struct address_space
*mapping
;
65 * Avoid burning cycles with pages that are yet under __free_pages(),
66 * or just got freed under us.
68 * In case we 'win' a race for a movable page being freed under us and
69 * raise its refcount preventing __free_pages() from doing its job
70 * the put_page() at the end of this block will take care of
71 * release this page, thus avoiding a nasty leakage.
73 if (unlikely(!get_page_unless_zero(page
)))
77 * Check PageMovable before holding a PG_lock because page's owner
78 * assumes anybody doesn't touch PG_lock of newly allocated page
79 * so unconditionally grabbing the lock ruins page's owner side.
81 if (unlikely(!__PageMovable(page
)))
84 * As movable pages are not isolated from LRU lists, concurrent
85 * compaction threads can race against page migration functions
86 * as well as race against the releasing a page.
88 * In order to avoid having an already isolated movable page
89 * being (wrongly) re-isolated while it is under migration,
90 * or to avoid attempting to isolate pages being released,
91 * lets be sure we have the page lock
92 * before proceeding with the movable page isolation steps.
94 if (unlikely(!trylock_page(page
)))
97 if (!PageMovable(page
) || PageIsolated(page
))
100 mapping
= page_mapping(page
);
101 VM_BUG_ON_PAGE(!mapping
, page
);
103 if (!mapping
->a_ops
->isolate_page(page
, mode
))
104 goto out_no_isolated
;
106 /* Driver shouldn't use PG_isolated bit of page->flags */
107 WARN_ON_ONCE(PageIsolated(page
));
108 __SetPageIsolated(page
);
121 static void putback_movable_page(struct page
*page
)
123 struct address_space
*mapping
;
125 mapping
= page_mapping(page
);
126 mapping
->a_ops
->putback_page(page
);
127 __ClearPageIsolated(page
);
131 * Put previously isolated pages back onto the appropriate lists
132 * from where they were once taken off for compaction/migration.
134 * This function shall be used whenever the isolated pageset has been
135 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
136 * and isolate_huge_page().
138 void putback_movable_pages(struct list_head
*l
)
143 list_for_each_entry_safe(page
, page2
, l
, lru
) {
144 if (unlikely(PageHuge(page
))) {
145 putback_active_hugepage(page
);
148 list_del(&page
->lru
);
150 * We isolated non-lru movable page so here we can use
151 * __PageMovable because LRU page's mapping cannot have
152 * PAGE_MAPPING_MOVABLE.
154 if (unlikely(__PageMovable(page
))) {
155 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
157 if (PageMovable(page
))
158 putback_movable_page(page
);
160 __ClearPageIsolated(page
);
164 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+
165 page_is_file_lru(page
), -thp_nr_pages(page
));
166 putback_lru_page(page
);
172 * Restore a potential migration pte to a working pte entry
174 static bool remove_migration_pte(struct page
*page
, struct vm_area_struct
*vma
,
175 unsigned long addr
, void *old
)
177 struct page_vma_mapped_walk pvmw
= {
181 .flags
= PVMW_SYNC
| PVMW_MIGRATION
,
187 VM_BUG_ON_PAGE(PageTail(page
), page
);
188 while (page_vma_mapped_walk(&pvmw
)) {
192 new = page
- pvmw
.page
->index
+
193 linear_page_index(vma
, pvmw
.address
);
195 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
196 /* PMD-mapped THP migration entry */
198 VM_BUG_ON_PAGE(PageHuge(page
) || !PageTransCompound(page
), page
);
199 remove_migration_pmd(&pvmw
, new);
205 pte
= pte_mkold(mk_pte(new, READ_ONCE(vma
->vm_page_prot
)));
206 if (pte_swp_soft_dirty(*pvmw
.pte
))
207 pte
= pte_mksoft_dirty(pte
);
210 * Recheck VMA as permissions can change since migration started
212 entry
= pte_to_swp_entry(*pvmw
.pte
);
213 if (is_writable_migration_entry(entry
))
214 pte
= maybe_mkwrite(pte
, vma
);
215 else if (pte_swp_uffd_wp(*pvmw
.pte
))
216 pte
= pte_mkuffd_wp(pte
);
218 if (unlikely(is_device_private_page(new))) {
220 entry
= make_writable_device_private_entry(
223 entry
= make_readable_device_private_entry(
225 pte
= swp_entry_to_pte(entry
);
226 if (pte_swp_soft_dirty(*pvmw
.pte
))
227 pte
= pte_swp_mksoft_dirty(pte
);
228 if (pte_swp_uffd_wp(*pvmw
.pte
))
229 pte
= pte_swp_mkuffd_wp(pte
);
232 #ifdef CONFIG_HUGETLB_PAGE
234 unsigned int shift
= huge_page_shift(hstate_vma(vma
));
236 pte
= pte_mkhuge(pte
);
237 pte
= arch_make_huge_pte(pte
, shift
, vma
->vm_flags
);
238 set_huge_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
);
240 hugepage_add_anon_rmap(new, vma
, pvmw
.address
);
242 page_dup_rmap(new, true);
246 set_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
);
249 page_add_anon_rmap(new, vma
, pvmw
.address
, false);
251 page_add_file_rmap(new, false);
253 if (vma
->vm_flags
& VM_LOCKED
&& !PageTransCompound(new))
256 if (PageTransHuge(page
) && PageMlocked(page
))
257 clear_page_mlock(page
);
259 /* No need to invalidate - it was non-present before */
260 update_mmu_cache(vma
, pvmw
.address
, pvmw
.pte
);
267 * Get rid of all migration entries and replace them by
268 * references to the indicated page.
270 void remove_migration_ptes(struct page
*old
, struct page
*new, bool locked
)
272 struct rmap_walk_control rwc
= {
273 .rmap_one
= remove_migration_pte
,
278 rmap_walk_locked(new, &rwc
);
280 rmap_walk(new, &rwc
);
284 * Something used the pte of a page under migration. We need to
285 * get to the page and wait until migration is finished.
286 * When we return from this function the fault will be retried.
288 void __migration_entry_wait(struct mm_struct
*mm
, pte_t
*ptep
,
297 if (!is_swap_pte(pte
))
300 entry
= pte_to_swp_entry(pte
);
301 if (!is_migration_entry(entry
))
304 page
= pfn_swap_entry_to_page(entry
);
305 page
= compound_head(page
);
308 * Once page cache replacement of page migration started, page_count
309 * is zero; but we must not call put_and_wait_on_page_locked() without
310 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
312 if (!get_page_unless_zero(page
))
314 pte_unmap_unlock(ptep
, ptl
);
315 put_and_wait_on_page_locked(page
, TASK_UNINTERRUPTIBLE
);
318 pte_unmap_unlock(ptep
, ptl
);
321 void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
322 unsigned long address
)
324 spinlock_t
*ptl
= pte_lockptr(mm
, pmd
);
325 pte_t
*ptep
= pte_offset_map(pmd
, address
);
326 __migration_entry_wait(mm
, ptep
, ptl
);
329 void migration_entry_wait_huge(struct vm_area_struct
*vma
,
330 struct mm_struct
*mm
, pte_t
*pte
)
332 spinlock_t
*ptl
= huge_pte_lockptr(hstate_vma(vma
), mm
, pte
);
333 __migration_entry_wait(mm
, pte
, ptl
);
336 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
337 void pmd_migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
)
342 ptl
= pmd_lock(mm
, pmd
);
343 if (!is_pmd_migration_entry(*pmd
))
345 page
= pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd
));
346 if (!get_page_unless_zero(page
))
349 put_and_wait_on_page_locked(page
, TASK_UNINTERRUPTIBLE
);
356 static int expected_page_refs(struct address_space
*mapping
, struct page
*page
)
358 int expected_count
= 1;
361 * Device private pages have an extra refcount as they are
364 expected_count
+= is_device_private_page(page
);
366 expected_count
+= thp_nr_pages(page
) + page_has_private(page
);
368 return expected_count
;
372 * Replace the page in the mapping.
374 * The number of remaining references must be:
375 * 1 for anonymous pages without a mapping
376 * 2 for pages with a mapping
377 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
379 int migrate_page_move_mapping(struct address_space
*mapping
,
380 struct page
*newpage
, struct page
*page
, int extra_count
)
382 XA_STATE(xas
, &mapping
->i_pages
, page_index(page
));
383 struct zone
*oldzone
, *newzone
;
385 int expected_count
= expected_page_refs(mapping
, page
) + extra_count
;
386 int nr
= thp_nr_pages(page
);
389 /* Anonymous page without mapping */
390 if (page_count(page
) != expected_count
)
393 /* No turning back from here */
394 newpage
->index
= page
->index
;
395 newpage
->mapping
= page
->mapping
;
396 if (PageSwapBacked(page
))
397 __SetPageSwapBacked(newpage
);
399 return MIGRATEPAGE_SUCCESS
;
402 oldzone
= page_zone(page
);
403 newzone
= page_zone(newpage
);
406 if (page_count(page
) != expected_count
|| xas_load(&xas
) != page
) {
407 xas_unlock_irq(&xas
);
411 if (!page_ref_freeze(page
, expected_count
)) {
412 xas_unlock_irq(&xas
);
417 * Now we know that no one else is looking at the page:
418 * no turning back from here.
420 newpage
->index
= page
->index
;
421 newpage
->mapping
= page
->mapping
;
422 page_ref_add(newpage
, nr
); /* add cache reference */
423 if (PageSwapBacked(page
)) {
424 __SetPageSwapBacked(newpage
);
425 if (PageSwapCache(page
)) {
426 SetPageSwapCache(newpage
);
427 set_page_private(newpage
, page_private(page
));
430 VM_BUG_ON_PAGE(PageSwapCache(page
), page
);
433 /* Move dirty while page refs frozen and newpage not yet exposed */
434 dirty
= PageDirty(page
);
436 ClearPageDirty(page
);
437 SetPageDirty(newpage
);
440 xas_store(&xas
, newpage
);
441 if (PageTransHuge(page
)) {
444 for (i
= 1; i
< nr
; i
++) {
446 xas_store(&xas
, newpage
);
451 * Drop cache reference from old page by unfreezing
452 * to one less reference.
453 * We know this isn't the last reference.
455 page_ref_unfreeze(page
, expected_count
- nr
);
458 /* Leave irq disabled to prevent preemption while updating stats */
461 * If moved to a different zone then also account
462 * the page for that zone. Other VM counters will be
463 * taken care of when we establish references to the
464 * new page and drop references to the old page.
466 * Note that anonymous pages are accounted for
467 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
468 * are mapped to swap space.
470 if (newzone
!= oldzone
) {
471 struct lruvec
*old_lruvec
, *new_lruvec
;
472 struct mem_cgroup
*memcg
;
474 memcg
= page_memcg(page
);
475 old_lruvec
= mem_cgroup_lruvec(memcg
, oldzone
->zone_pgdat
);
476 new_lruvec
= mem_cgroup_lruvec(memcg
, newzone
->zone_pgdat
);
478 __mod_lruvec_state(old_lruvec
, NR_FILE_PAGES
, -nr
);
479 __mod_lruvec_state(new_lruvec
, NR_FILE_PAGES
, nr
);
480 if (PageSwapBacked(page
) && !PageSwapCache(page
)) {
481 __mod_lruvec_state(old_lruvec
, NR_SHMEM
, -nr
);
482 __mod_lruvec_state(new_lruvec
, NR_SHMEM
, nr
);
485 if (PageSwapCache(page
)) {
486 __mod_lruvec_state(old_lruvec
, NR_SWAPCACHE
, -nr
);
487 __mod_lruvec_state(new_lruvec
, NR_SWAPCACHE
, nr
);
490 if (dirty
&& mapping_can_writeback(mapping
)) {
491 __mod_lruvec_state(old_lruvec
, NR_FILE_DIRTY
, -nr
);
492 __mod_zone_page_state(oldzone
, NR_ZONE_WRITE_PENDING
, -nr
);
493 __mod_lruvec_state(new_lruvec
, NR_FILE_DIRTY
, nr
);
494 __mod_zone_page_state(newzone
, NR_ZONE_WRITE_PENDING
, nr
);
499 return MIGRATEPAGE_SUCCESS
;
501 EXPORT_SYMBOL(migrate_page_move_mapping
);
504 * The expected number of remaining references is the same as that
505 * of migrate_page_move_mapping().
507 int migrate_huge_page_move_mapping(struct address_space
*mapping
,
508 struct page
*newpage
, struct page
*page
)
510 XA_STATE(xas
, &mapping
->i_pages
, page_index(page
));
514 expected_count
= 2 + page_has_private(page
);
515 if (page_count(page
) != expected_count
|| xas_load(&xas
) != page
) {
516 xas_unlock_irq(&xas
);
520 if (!page_ref_freeze(page
, expected_count
)) {
521 xas_unlock_irq(&xas
);
525 newpage
->index
= page
->index
;
526 newpage
->mapping
= page
->mapping
;
530 xas_store(&xas
, newpage
);
532 page_ref_unfreeze(page
, expected_count
- 1);
534 xas_unlock_irq(&xas
);
536 return MIGRATEPAGE_SUCCESS
;
540 * Gigantic pages are so large that we do not guarantee that page++ pointer
541 * arithmetic will work across the entire page. We need something more
544 static void __copy_gigantic_page(struct page
*dst
, struct page
*src
,
548 struct page
*dst_base
= dst
;
549 struct page
*src_base
= src
;
551 for (i
= 0; i
< nr_pages
; ) {
553 copy_highpage(dst
, src
);
556 dst
= mem_map_next(dst
, dst_base
, i
);
557 src
= mem_map_next(src
, src_base
, i
);
561 void copy_huge_page(struct page
*dst
, struct page
*src
)
568 struct hstate
*h
= page_hstate(src
);
569 nr_pages
= pages_per_huge_page(h
);
571 if (unlikely(nr_pages
> MAX_ORDER_NR_PAGES
)) {
572 __copy_gigantic_page(dst
, src
, nr_pages
);
577 BUG_ON(!PageTransHuge(src
));
578 nr_pages
= thp_nr_pages(src
);
581 for (i
= 0; i
< nr_pages
; i
++) {
583 copy_highpage(dst
+ i
, src
+ i
);
588 * Copy the page to its new location
590 void migrate_page_states(struct page
*newpage
, struct page
*page
)
595 SetPageError(newpage
);
596 if (PageReferenced(page
))
597 SetPageReferenced(newpage
);
598 if (PageUptodate(page
))
599 SetPageUptodate(newpage
);
600 if (TestClearPageActive(page
)) {
601 VM_BUG_ON_PAGE(PageUnevictable(page
), page
);
602 SetPageActive(newpage
);
603 } else if (TestClearPageUnevictable(page
))
604 SetPageUnevictable(newpage
);
605 if (PageWorkingset(page
))
606 SetPageWorkingset(newpage
);
607 if (PageChecked(page
))
608 SetPageChecked(newpage
);
609 if (PageMappedToDisk(page
))
610 SetPageMappedToDisk(newpage
);
612 /* Move dirty on pages not done by migrate_page_move_mapping() */
614 SetPageDirty(newpage
);
616 if (page_is_young(page
))
617 set_page_young(newpage
);
618 if (page_is_idle(page
))
619 set_page_idle(newpage
);
622 * Copy NUMA information to the new page, to prevent over-eager
623 * future migrations of this same page.
625 cpupid
= page_cpupid_xchg_last(page
, -1);
626 page_cpupid_xchg_last(newpage
, cpupid
);
628 ksm_migrate_page(newpage
, page
);
630 * Please do not reorder this without considering how mm/ksm.c's
631 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
633 if (PageSwapCache(page
))
634 ClearPageSwapCache(page
);
635 ClearPagePrivate(page
);
637 /* page->private contains hugetlb specific flags */
639 set_page_private(page
, 0);
642 * If any waiters have accumulated on the new page then
645 if (PageWriteback(newpage
))
646 end_page_writeback(newpage
);
649 * PG_readahead shares the same bit with PG_reclaim. The above
650 * end_page_writeback() may clear PG_readahead mistakenly, so set the
653 if (PageReadahead(page
))
654 SetPageReadahead(newpage
);
656 copy_page_owner(page
, newpage
);
659 mem_cgroup_migrate(page
, newpage
);
661 EXPORT_SYMBOL(migrate_page_states
);
663 void migrate_page_copy(struct page
*newpage
, struct page
*page
)
665 if (PageHuge(page
) || PageTransHuge(page
))
666 copy_huge_page(newpage
, page
);
668 copy_highpage(newpage
, page
);
670 migrate_page_states(newpage
, page
);
672 EXPORT_SYMBOL(migrate_page_copy
);
674 /************************************************************
675 * Migration functions
676 ***********************************************************/
679 * Common logic to directly migrate a single LRU page suitable for
680 * pages that do not use PagePrivate/PagePrivate2.
682 * Pages are locked upon entry and exit.
684 int migrate_page(struct address_space
*mapping
,
685 struct page
*newpage
, struct page
*page
,
686 enum migrate_mode mode
)
690 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
692 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, 0);
694 if (rc
!= MIGRATEPAGE_SUCCESS
)
697 if (mode
!= MIGRATE_SYNC_NO_COPY
)
698 migrate_page_copy(newpage
, page
);
700 migrate_page_states(newpage
, page
);
701 return MIGRATEPAGE_SUCCESS
;
703 EXPORT_SYMBOL(migrate_page
);
706 /* Returns true if all buffers are successfully locked */
707 static bool buffer_migrate_lock_buffers(struct buffer_head
*head
,
708 enum migrate_mode mode
)
710 struct buffer_head
*bh
= head
;
712 /* Simple case, sync compaction */
713 if (mode
!= MIGRATE_ASYNC
) {
716 bh
= bh
->b_this_page
;
718 } while (bh
!= head
);
723 /* async case, we cannot block on lock_buffer so use trylock_buffer */
725 if (!trylock_buffer(bh
)) {
727 * We failed to lock the buffer and cannot stall in
728 * async migration. Release the taken locks
730 struct buffer_head
*failed_bh
= bh
;
732 while (bh
!= failed_bh
) {
734 bh
= bh
->b_this_page
;
739 bh
= bh
->b_this_page
;
740 } while (bh
!= head
);
744 static int __buffer_migrate_page(struct address_space
*mapping
,
745 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
,
748 struct buffer_head
*bh
, *head
;
752 if (!page_has_buffers(page
))
753 return migrate_page(mapping
, newpage
, page
, mode
);
755 /* Check whether page does not have extra refs before we do more work */
756 expected_count
= expected_page_refs(mapping
, page
);
757 if (page_count(page
) != expected_count
)
760 head
= page_buffers(page
);
761 if (!buffer_migrate_lock_buffers(head
, mode
))
766 bool invalidated
= false;
770 spin_lock(&mapping
->private_lock
);
773 if (atomic_read(&bh
->b_count
)) {
777 bh
= bh
->b_this_page
;
778 } while (bh
!= head
);
784 spin_unlock(&mapping
->private_lock
);
785 invalidate_bh_lrus();
787 goto recheck_buffers
;
791 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, 0);
792 if (rc
!= MIGRATEPAGE_SUCCESS
)
795 attach_page_private(newpage
, detach_page_private(page
));
799 set_bh_page(bh
, newpage
, bh_offset(bh
));
800 bh
= bh
->b_this_page
;
802 } while (bh
!= head
);
804 if (mode
!= MIGRATE_SYNC_NO_COPY
)
805 migrate_page_copy(newpage
, page
);
807 migrate_page_states(newpage
, page
);
809 rc
= MIGRATEPAGE_SUCCESS
;
812 spin_unlock(&mapping
->private_lock
);
816 bh
= bh
->b_this_page
;
818 } while (bh
!= head
);
824 * Migration function for pages with buffers. This function can only be used
825 * if the underlying filesystem guarantees that no other references to "page"
826 * exist. For example attached buffer heads are accessed only under page lock.
828 int buffer_migrate_page(struct address_space
*mapping
,
829 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
831 return __buffer_migrate_page(mapping
, newpage
, page
, mode
, false);
833 EXPORT_SYMBOL(buffer_migrate_page
);
836 * Same as above except that this variant is more careful and checks that there
837 * are also no buffer head references. This function is the right one for
838 * mappings where buffer heads are directly looked up and referenced (such as
839 * block device mappings).
841 int buffer_migrate_page_norefs(struct address_space
*mapping
,
842 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
844 return __buffer_migrate_page(mapping
, newpage
, page
, mode
, true);
849 * Writeback a page to clean the dirty state
851 static int writeout(struct address_space
*mapping
, struct page
*page
)
853 struct writeback_control wbc
= {
854 .sync_mode
= WB_SYNC_NONE
,
857 .range_end
= LLONG_MAX
,
862 if (!mapping
->a_ops
->writepage
)
863 /* No write method for the address space */
866 if (!clear_page_dirty_for_io(page
))
867 /* Someone else already triggered a write */
871 * A dirty page may imply that the underlying filesystem has
872 * the page on some queue. So the page must be clean for
873 * migration. Writeout may mean we loose the lock and the
874 * page state is no longer what we checked for earlier.
875 * At this point we know that the migration attempt cannot
878 remove_migration_ptes(page
, page
, false);
880 rc
= mapping
->a_ops
->writepage(page
, &wbc
);
882 if (rc
!= AOP_WRITEPAGE_ACTIVATE
)
883 /* unlocked. Relock */
886 return (rc
< 0) ? -EIO
: -EAGAIN
;
890 * Default handling if a filesystem does not provide a migration function.
892 static int fallback_migrate_page(struct address_space
*mapping
,
893 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
895 if (PageDirty(page
)) {
896 /* Only writeback pages in full synchronous migration */
899 case MIGRATE_SYNC_NO_COPY
:
904 return writeout(mapping
, page
);
908 * Buffers may be managed in a filesystem specific way.
909 * We must have no buffers or drop them.
911 if (page_has_private(page
) &&
912 !try_to_release_page(page
, GFP_KERNEL
))
913 return mode
== MIGRATE_SYNC
? -EAGAIN
: -EBUSY
;
915 return migrate_page(mapping
, newpage
, page
, mode
);
919 * Move a page to a newly allocated page
920 * The page is locked and all ptes have been successfully removed.
922 * The new page will have replaced the old page if this function
927 * MIGRATEPAGE_SUCCESS - success
929 static int move_to_new_page(struct page
*newpage
, struct page
*page
,
930 enum migrate_mode mode
)
932 struct address_space
*mapping
;
934 bool is_lru
= !__PageMovable(page
);
936 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
937 VM_BUG_ON_PAGE(!PageLocked(newpage
), newpage
);
939 mapping
= page_mapping(page
);
941 if (likely(is_lru
)) {
943 rc
= migrate_page(mapping
, newpage
, page
, mode
);
944 else if (mapping
->a_ops
->migratepage
)
946 * Most pages have a mapping and most filesystems
947 * provide a migratepage callback. Anonymous pages
948 * are part of swap space which also has its own
949 * migratepage callback. This is the most common path
950 * for page migration.
952 rc
= mapping
->a_ops
->migratepage(mapping
, newpage
,
955 rc
= fallback_migrate_page(mapping
, newpage
,
959 * In case of non-lru page, it could be released after
960 * isolation step. In that case, we shouldn't try migration.
962 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
963 if (!PageMovable(page
)) {
964 rc
= MIGRATEPAGE_SUCCESS
;
965 __ClearPageIsolated(page
);
969 rc
= mapping
->a_ops
->migratepage(mapping
, newpage
,
971 WARN_ON_ONCE(rc
== MIGRATEPAGE_SUCCESS
&&
972 !PageIsolated(page
));
976 * When successful, old pagecache page->mapping must be cleared before
977 * page is freed; but stats require that PageAnon be left as PageAnon.
979 if (rc
== MIGRATEPAGE_SUCCESS
) {
980 if (__PageMovable(page
)) {
981 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
984 * We clear PG_movable under page_lock so any compactor
985 * cannot try to migrate this page.
987 __ClearPageIsolated(page
);
991 * Anonymous and movable page->mapping will be cleared by
992 * free_pages_prepare so don't reset it here for keeping
993 * the type to work PageAnon, for example.
995 if (!PageMappingFlags(page
))
996 page
->mapping
= NULL
;
998 if (likely(!is_zone_device_page(newpage
)))
999 flush_dcache_page(newpage
);
1006 static int __unmap_and_move(struct page
*page
, struct page
*newpage
,
1007 int force
, enum migrate_mode mode
)
1010 int page_was_mapped
= 0;
1011 struct anon_vma
*anon_vma
= NULL
;
1012 bool is_lru
= !__PageMovable(page
);
1014 if (!trylock_page(page
)) {
1015 if (!force
|| mode
== MIGRATE_ASYNC
)
1019 * It's not safe for direct compaction to call lock_page.
1020 * For example, during page readahead pages are added locked
1021 * to the LRU. Later, when the IO completes the pages are
1022 * marked uptodate and unlocked. However, the queueing
1023 * could be merging multiple pages for one bio (e.g.
1024 * mpage_readahead). If an allocation happens for the
1025 * second or third page, the process can end up locking
1026 * the same page twice and deadlocking. Rather than
1027 * trying to be clever about what pages can be locked,
1028 * avoid the use of lock_page for direct compaction
1031 if (current
->flags
& PF_MEMALLOC
)
1037 if (PageWriteback(page
)) {
1039 * Only in the case of a full synchronous migration is it
1040 * necessary to wait for PageWriteback. In the async case,
1041 * the retry loop is too short and in the sync-light case,
1042 * the overhead of stalling is too much
1046 case MIGRATE_SYNC_NO_COPY
:
1054 wait_on_page_writeback(page
);
1058 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
1059 * we cannot notice that anon_vma is freed while we migrates a page.
1060 * This get_anon_vma() delays freeing anon_vma pointer until the end
1061 * of migration. File cache pages are no problem because of page_lock()
1062 * File Caches may use write_page() or lock_page() in migration, then,
1063 * just care Anon page here.
1065 * Only page_get_anon_vma() understands the subtleties of
1066 * getting a hold on an anon_vma from outside one of its mms.
1067 * But if we cannot get anon_vma, then we won't need it anyway,
1068 * because that implies that the anon page is no longer mapped
1069 * (and cannot be remapped so long as we hold the page lock).
1071 if (PageAnon(page
) && !PageKsm(page
))
1072 anon_vma
= page_get_anon_vma(page
);
1075 * Block others from accessing the new page when we get around to
1076 * establishing additional references. We are usually the only one
1077 * holding a reference to newpage at this point. We used to have a BUG
1078 * here if trylock_page(newpage) fails, but would like to allow for
1079 * cases where there might be a race with the previous use of newpage.
1080 * This is much like races on refcount of oldpage: just don't BUG().
1082 if (unlikely(!trylock_page(newpage
)))
1085 if (unlikely(!is_lru
)) {
1086 rc
= move_to_new_page(newpage
, page
, mode
);
1087 goto out_unlock_both
;
1091 * Corner case handling:
1092 * 1. When a new swap-cache page is read into, it is added to the LRU
1093 * and treated as swapcache but it has no rmap yet.
1094 * Calling try_to_unmap() against a page->mapping==NULL page will
1095 * trigger a BUG. So handle it here.
1096 * 2. An orphaned page (see truncate_cleanup_page) might have
1097 * fs-private metadata. The page can be picked up due to memory
1098 * offlining. Everywhere else except page reclaim, the page is
1099 * invisible to the vm, so the page can not be migrated. So try to
1100 * free the metadata, so the page can be freed.
1102 if (!page
->mapping
) {
1103 VM_BUG_ON_PAGE(PageAnon(page
), page
);
1104 if (page_has_private(page
)) {
1105 try_to_free_buffers(page
);
1106 goto out_unlock_both
;
1108 } else if (page_mapped(page
)) {
1109 /* Establish migration ptes */
1110 VM_BUG_ON_PAGE(PageAnon(page
) && !PageKsm(page
) && !anon_vma
,
1112 try_to_migrate(page
, 0);
1113 page_was_mapped
= 1;
1116 if (!page_mapped(page
))
1117 rc
= move_to_new_page(newpage
, page
, mode
);
1119 if (page_was_mapped
)
1120 remove_migration_ptes(page
,
1121 rc
== MIGRATEPAGE_SUCCESS
? newpage
: page
, false);
1124 unlock_page(newpage
);
1126 /* Drop an anon_vma reference if we took one */
1128 put_anon_vma(anon_vma
);
1132 * If migration is successful, decrease refcount of the newpage
1133 * which will not free the page because new page owner increased
1134 * refcounter. As well, if it is LRU page, add the page to LRU
1135 * list in here. Use the old state of the isolated source page to
1136 * determine if we migrated a LRU page. newpage was already unlocked
1137 * and possibly modified by its owner - don't rely on the page
1140 if (rc
== MIGRATEPAGE_SUCCESS
) {
1141 if (unlikely(!is_lru
))
1144 putback_lru_page(newpage
);
1151 * Obtain the lock on page, remove all ptes and migrate the page
1152 * to the newly allocated page in newpage.
1154 static int unmap_and_move(new_page_t get_new_page
,
1155 free_page_t put_new_page
,
1156 unsigned long private, struct page
*page
,
1157 int force
, enum migrate_mode mode
,
1158 enum migrate_reason reason
,
1159 struct list_head
*ret
)
1161 int rc
= MIGRATEPAGE_SUCCESS
;
1162 struct page
*newpage
= NULL
;
1164 if (!thp_migration_supported() && PageTransHuge(page
))
1167 if (page_count(page
) == 1) {
1168 /* page was freed from under us. So we are done. */
1169 ClearPageActive(page
);
1170 ClearPageUnevictable(page
);
1171 if (unlikely(__PageMovable(page
))) {
1173 if (!PageMovable(page
))
1174 __ClearPageIsolated(page
);
1180 newpage
= get_new_page(page
, private);
1184 rc
= __unmap_and_move(page
, newpage
, force
, mode
);
1185 if (rc
== MIGRATEPAGE_SUCCESS
)
1186 set_page_owner_migrate_reason(newpage
, reason
);
1189 if (rc
!= -EAGAIN
) {
1191 * A page that has been migrated has all references
1192 * removed and will be freed. A page that has not been
1193 * migrated will have kept its references and be restored.
1195 list_del(&page
->lru
);
1199 * If migration is successful, releases reference grabbed during
1200 * isolation. Otherwise, restore the page to right list unless
1203 if (rc
== MIGRATEPAGE_SUCCESS
) {
1205 * Compaction can migrate also non-LRU pages which are
1206 * not accounted to NR_ISOLATED_*. They can be recognized
1209 if (likely(!__PageMovable(page
)))
1210 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+
1211 page_is_file_lru(page
), -thp_nr_pages(page
));
1213 if (reason
!= MR_MEMORY_FAILURE
)
1215 * We release the page in page_handle_poison.
1220 list_add_tail(&page
->lru
, ret
);
1223 put_new_page(newpage
, private);
1232 * Counterpart of unmap_and_move_page() for hugepage migration.
1234 * This function doesn't wait the completion of hugepage I/O
1235 * because there is no race between I/O and migration for hugepage.
1236 * Note that currently hugepage I/O occurs only in direct I/O
1237 * where no lock is held and PG_writeback is irrelevant,
1238 * and writeback status of all subpages are counted in the reference
1239 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1240 * under direct I/O, the reference of the head page is 512 and a bit more.)
1241 * This means that when we try to migrate hugepage whose subpages are
1242 * doing direct I/O, some references remain after try_to_unmap() and
1243 * hugepage migration fails without data corruption.
1245 * There is also no race when direct I/O is issued on the page under migration,
1246 * because then pte is replaced with migration swap entry and direct I/O code
1247 * will wait in the page fault for migration to complete.
1249 static int unmap_and_move_huge_page(new_page_t get_new_page
,
1250 free_page_t put_new_page
, unsigned long private,
1251 struct page
*hpage
, int force
,
1252 enum migrate_mode mode
, int reason
,
1253 struct list_head
*ret
)
1256 int page_was_mapped
= 0;
1257 struct page
*new_hpage
;
1258 struct anon_vma
*anon_vma
= NULL
;
1259 struct address_space
*mapping
= NULL
;
1262 * Migratability of hugepages depends on architectures and their size.
1263 * This check is necessary because some callers of hugepage migration
1264 * like soft offline and memory hotremove don't walk through page
1265 * tables or check whether the hugepage is pmd-based or not before
1266 * kicking migration.
1268 if (!hugepage_migration_supported(page_hstate(hpage
))) {
1269 list_move_tail(&hpage
->lru
, ret
);
1273 if (page_count(hpage
) == 1) {
1274 /* page was freed from under us. So we are done. */
1275 putback_active_hugepage(hpage
);
1276 return MIGRATEPAGE_SUCCESS
;
1279 new_hpage
= get_new_page(hpage
, private);
1283 if (!trylock_page(hpage
)) {
1288 case MIGRATE_SYNC_NO_COPY
:
1297 * Check for pages which are in the process of being freed. Without
1298 * page_mapping() set, hugetlbfs specific move page routine will not
1299 * be called and we could leak usage counts for subpools.
1301 if (hugetlb_page_subpool(hpage
) && !page_mapping(hpage
)) {
1306 if (PageAnon(hpage
))
1307 anon_vma
= page_get_anon_vma(hpage
);
1309 if (unlikely(!trylock_page(new_hpage
)))
1312 if (page_mapped(hpage
)) {
1313 bool mapping_locked
= false;
1314 enum ttu_flags ttu
= 0;
1316 if (!PageAnon(hpage
)) {
1318 * In shared mappings, try_to_unmap could potentially
1319 * call huge_pmd_unshare. Because of this, take
1320 * semaphore in write mode here and set TTU_RMAP_LOCKED
1321 * to let lower levels know we have taken the lock.
1323 mapping
= hugetlb_page_mapping_lock_write(hpage
);
1324 if (unlikely(!mapping
))
1325 goto unlock_put_anon
;
1327 mapping_locked
= true;
1328 ttu
|= TTU_RMAP_LOCKED
;
1331 try_to_migrate(hpage
, ttu
);
1332 page_was_mapped
= 1;
1335 i_mmap_unlock_write(mapping
);
1338 if (!page_mapped(hpage
))
1339 rc
= move_to_new_page(new_hpage
, hpage
, mode
);
1341 if (page_was_mapped
)
1342 remove_migration_ptes(hpage
,
1343 rc
== MIGRATEPAGE_SUCCESS
? new_hpage
: hpage
, false);
1346 unlock_page(new_hpage
);
1350 put_anon_vma(anon_vma
);
1352 if (rc
== MIGRATEPAGE_SUCCESS
) {
1353 move_hugetlb_state(hpage
, new_hpage
, reason
);
1354 put_new_page
= NULL
;
1360 if (rc
== MIGRATEPAGE_SUCCESS
)
1361 putback_active_hugepage(hpage
);
1362 else if (rc
!= -EAGAIN
)
1363 list_move_tail(&hpage
->lru
, ret
);
1366 * If migration was not successful and there's a freeing callback, use
1367 * it. Otherwise, put_page() will drop the reference grabbed during
1371 put_new_page(new_hpage
, private);
1373 putback_active_hugepage(new_hpage
);
1378 static inline int try_split_thp(struct page
*page
, struct page
**page2
,
1379 struct list_head
*from
)
1384 rc
= split_huge_page_to_list(page
, from
);
1387 list_safe_reset_next(page
, *page2
, lru
);
1393 * migrate_pages - migrate the pages specified in a list, to the free pages
1394 * supplied as the target for the page migration
1396 * @from: The list of pages to be migrated.
1397 * @get_new_page: The function used to allocate free pages to be used
1398 * as the target of the page migration.
1399 * @put_new_page: The function used to free target pages if migration
1400 * fails, or NULL if no special handling is necessary.
1401 * @private: Private data to be passed on to get_new_page()
1402 * @mode: The migration mode that specifies the constraints for
1403 * page migration, if any.
1404 * @reason: The reason for page migration.
1406 * The function returns after 10 attempts or if no pages are movable any more
1407 * because the list has become empty or no retryable pages exist any more.
1408 * It is caller's responsibility to call putback_movable_pages() to return pages
1409 * to the LRU or free list only if ret != 0.
1411 * Returns the number of pages that were not migrated, or an error code.
1413 int migrate_pages(struct list_head
*from
, new_page_t get_new_page
,
1414 free_page_t put_new_page
, unsigned long private,
1415 enum migrate_mode mode
, int reason
)
1420 int nr_succeeded
= 0;
1421 int nr_thp_succeeded
= 0;
1422 int nr_thp_failed
= 0;
1423 int nr_thp_split
= 0;
1425 bool is_thp
= false;
1428 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
1429 int rc
, nr_subpages
;
1430 LIST_HEAD(ret_pages
);
1431 bool nosplit
= (reason
== MR_NUMA_MISPLACED
);
1433 trace_mm_migrate_pages_start(mode
, reason
);
1436 current
->flags
|= PF_SWAPWRITE
;
1438 for (pass
= 0; pass
< 10 && (retry
|| thp_retry
); pass
++) {
1442 list_for_each_entry_safe(page
, page2
, from
, lru
) {
1445 * THP statistics is based on the source huge page.
1446 * Capture required information that might get lost
1449 is_thp
= PageTransHuge(page
) && !PageHuge(page
);
1450 nr_subpages
= thp_nr_pages(page
);
1454 rc
= unmap_and_move_huge_page(get_new_page
,
1455 put_new_page
, private, page
,
1456 pass
> 2, mode
, reason
,
1459 rc
= unmap_and_move(get_new_page
, put_new_page
,
1460 private, page
, pass
> 2, mode
,
1461 reason
, &ret_pages
);
1464 * Success: non hugetlb page will be freed, hugetlb
1465 * page will be put back
1466 * -EAGAIN: stay on the from list
1467 * -ENOMEM: stay on the from list
1468 * Other errno: put on ret_pages list then splice to
1473 * THP migration might be unsupported or the
1474 * allocation could've failed so we should
1475 * retry on the same page with the THP split
1478 * Head page is retried immediately and tail
1479 * pages are added to the tail of the list so
1480 * we encounter them after the rest of the list
1484 /* THP migration is unsupported */
1486 if (!try_split_thp(page
, &page2
, from
)) {
1492 nr_failed
+= nr_subpages
;
1496 /* Hugetlb migration is unsupported */
1501 * When memory is low, don't bother to try to migrate
1502 * other pages, just exit.
1503 * THP NUMA faulting doesn't split THP to retry.
1505 if (is_thp
&& !nosplit
) {
1506 if (!try_split_thp(page
, &page2
, from
)) {
1512 nr_failed
+= nr_subpages
;
1524 case MIGRATEPAGE_SUCCESS
:
1527 nr_succeeded
+= nr_subpages
;
1534 * Permanent failure (-EBUSY, etc.):
1535 * unlike -EAGAIN case, the failed page is
1536 * removed from migration page list and not
1537 * retried in the next outer loop.
1541 nr_failed
+= nr_subpages
;
1549 nr_failed
+= retry
+ thp_retry
;
1550 nr_thp_failed
+= thp_retry
;
1554 * Put the permanent failure page back to migration list, they
1555 * will be put back to the right list by the caller.
1557 list_splice(&ret_pages
, from
);
1559 count_vm_events(PGMIGRATE_SUCCESS
, nr_succeeded
);
1560 count_vm_events(PGMIGRATE_FAIL
, nr_failed
);
1561 count_vm_events(THP_MIGRATION_SUCCESS
, nr_thp_succeeded
);
1562 count_vm_events(THP_MIGRATION_FAIL
, nr_thp_failed
);
1563 count_vm_events(THP_MIGRATION_SPLIT
, nr_thp_split
);
1564 trace_mm_migrate_pages(nr_succeeded
, nr_failed
, nr_thp_succeeded
,
1565 nr_thp_failed
, nr_thp_split
, mode
, reason
);
1568 current
->flags
&= ~PF_SWAPWRITE
;
1573 struct page
*alloc_migration_target(struct page
*page
, unsigned long private)
1575 struct migration_target_control
*mtc
;
1577 unsigned int order
= 0;
1578 struct page
*new_page
= NULL
;
1582 mtc
= (struct migration_target_control
*)private;
1583 gfp_mask
= mtc
->gfp_mask
;
1585 if (nid
== NUMA_NO_NODE
)
1586 nid
= page_to_nid(page
);
1588 if (PageHuge(page
)) {
1589 struct hstate
*h
= page_hstate(compound_head(page
));
1591 gfp_mask
= htlb_modify_alloc_mask(h
, gfp_mask
);
1592 return alloc_huge_page_nodemask(h
, nid
, mtc
->nmask
, gfp_mask
);
1595 if (PageTransHuge(page
)) {
1597 * clear __GFP_RECLAIM to make the migration callback
1598 * consistent with regular THP allocations.
1600 gfp_mask
&= ~__GFP_RECLAIM
;
1601 gfp_mask
|= GFP_TRANSHUGE
;
1602 order
= HPAGE_PMD_ORDER
;
1604 zidx
= zone_idx(page_zone(page
));
1605 if (is_highmem_idx(zidx
) || zidx
== ZONE_MOVABLE
)
1606 gfp_mask
|= __GFP_HIGHMEM
;
1608 new_page
= __alloc_pages(gfp_mask
, order
, nid
, mtc
->nmask
);
1610 if (new_page
&& PageTransHuge(new_page
))
1611 prep_transhuge_page(new_page
);
1618 static int store_status(int __user
*status
, int start
, int value
, int nr
)
1621 if (put_user(value
, status
+ start
))
1629 static int do_move_pages_to_node(struct mm_struct
*mm
,
1630 struct list_head
*pagelist
, int node
)
1633 struct migration_target_control mtc
= {
1635 .gfp_mask
= GFP_HIGHUSER_MOVABLE
| __GFP_THISNODE
,
1638 err
= migrate_pages(pagelist
, alloc_migration_target
, NULL
,
1639 (unsigned long)&mtc
, MIGRATE_SYNC
, MR_SYSCALL
);
1641 putback_movable_pages(pagelist
);
1646 * Resolves the given address to a struct page, isolates it from the LRU and
1647 * puts it to the given pagelist.
1649 * errno - if the page cannot be found/isolated
1650 * 0 - when it doesn't have to be migrated because it is already on the
1652 * 1 - when it has been queued
1654 static int add_page_for_migration(struct mm_struct
*mm
, unsigned long addr
,
1655 int node
, struct list_head
*pagelist
, bool migrate_all
)
1657 struct vm_area_struct
*vma
;
1659 unsigned int follflags
;
1664 vma
= find_vma(mm
, addr
);
1665 if (!vma
|| addr
< vma
->vm_start
|| !vma_migratable(vma
))
1668 /* FOLL_DUMP to ignore special (like zero) pages */
1669 follflags
= FOLL_GET
| FOLL_DUMP
;
1670 page
= follow_page(vma
, addr
, follflags
);
1672 err
= PTR_ERR(page
);
1681 if (page_to_nid(page
) == node
)
1685 if (page_mapcount(page
) > 1 && !migrate_all
)
1688 if (PageHuge(page
)) {
1689 if (PageHead(page
)) {
1690 isolate_huge_page(page
, pagelist
);
1696 head
= compound_head(page
);
1697 err
= isolate_lru_page(head
);
1702 list_add_tail(&head
->lru
, pagelist
);
1703 mod_node_page_state(page_pgdat(head
),
1704 NR_ISOLATED_ANON
+ page_is_file_lru(head
),
1705 thp_nr_pages(head
));
1709 * Either remove the duplicate refcount from
1710 * isolate_lru_page() or drop the page ref if it was
1715 mmap_read_unlock(mm
);
1719 static int move_pages_and_store_status(struct mm_struct
*mm
, int node
,
1720 struct list_head
*pagelist
, int __user
*status
,
1721 int start
, int i
, unsigned long nr_pages
)
1725 if (list_empty(pagelist
))
1728 err
= do_move_pages_to_node(mm
, pagelist
, node
);
1731 * Positive err means the number of failed
1732 * pages to migrate. Since we are going to
1733 * abort and return the number of non-migrated
1734 * pages, so need to include the rest of the
1735 * nr_pages that have not been attempted as
1739 err
+= nr_pages
- i
- 1;
1742 return store_status(status
, start
, node
, i
- start
);
1746 * Migrate an array of page address onto an array of nodes and fill
1747 * the corresponding array of status.
1749 static int do_pages_move(struct mm_struct
*mm
, nodemask_t task_nodes
,
1750 unsigned long nr_pages
,
1751 const void __user
* __user
*pages
,
1752 const int __user
*nodes
,
1753 int __user
*status
, int flags
)
1755 int current_node
= NUMA_NO_NODE
;
1756 LIST_HEAD(pagelist
);
1760 lru_cache_disable();
1762 for (i
= start
= 0; i
< nr_pages
; i
++) {
1763 const void __user
*p
;
1768 if (get_user(p
, pages
+ i
))
1770 if (get_user(node
, nodes
+ i
))
1772 addr
= (unsigned long)untagged_addr(p
);
1775 if (node
< 0 || node
>= MAX_NUMNODES
)
1777 if (!node_state(node
, N_MEMORY
))
1781 if (!node_isset(node
, task_nodes
))
1784 if (current_node
== NUMA_NO_NODE
) {
1785 current_node
= node
;
1787 } else if (node
!= current_node
) {
1788 err
= move_pages_and_store_status(mm
, current_node
,
1789 &pagelist
, status
, start
, i
, nr_pages
);
1793 current_node
= node
;
1797 * Errors in the page lookup or isolation are not fatal and we simply
1798 * report them via status
1800 err
= add_page_for_migration(mm
, addr
, current_node
,
1801 &pagelist
, flags
& MPOL_MF_MOVE_ALL
);
1804 /* The page is successfully queued for migration */
1809 * If the page is already on the target node (!err), store the
1810 * node, otherwise, store the err.
1812 err
= store_status(status
, i
, err
? : current_node
, 1);
1816 err
= move_pages_and_store_status(mm
, current_node
, &pagelist
,
1817 status
, start
, i
, nr_pages
);
1820 current_node
= NUMA_NO_NODE
;
1823 /* Make sure we do not overwrite the existing error */
1824 err1
= move_pages_and_store_status(mm
, current_node
, &pagelist
,
1825 status
, start
, i
, nr_pages
);
1834 * Determine the nodes of an array of pages and store it in an array of status.
1836 static void do_pages_stat_array(struct mm_struct
*mm
, unsigned long nr_pages
,
1837 const void __user
**pages
, int *status
)
1843 for (i
= 0; i
< nr_pages
; i
++) {
1844 unsigned long addr
= (unsigned long)(*pages
);
1845 struct vm_area_struct
*vma
;
1849 vma
= vma_lookup(mm
, addr
);
1853 /* FOLL_DUMP to ignore special (like zero) pages */
1854 page
= follow_page(vma
, addr
, FOLL_DUMP
);
1856 err
= PTR_ERR(page
);
1860 err
= page
? page_to_nid(page
) : -ENOENT
;
1868 mmap_read_unlock(mm
);
1872 * Determine the nodes of a user array of pages and store it in
1873 * a user array of status.
1875 static int do_pages_stat(struct mm_struct
*mm
, unsigned long nr_pages
,
1876 const void __user
* __user
*pages
,
1879 #define DO_PAGES_STAT_CHUNK_NR 16
1880 const void __user
*chunk_pages
[DO_PAGES_STAT_CHUNK_NR
];
1881 int chunk_status
[DO_PAGES_STAT_CHUNK_NR
];
1884 unsigned long chunk_nr
;
1886 chunk_nr
= nr_pages
;
1887 if (chunk_nr
> DO_PAGES_STAT_CHUNK_NR
)
1888 chunk_nr
= DO_PAGES_STAT_CHUNK_NR
;
1890 if (copy_from_user(chunk_pages
, pages
, chunk_nr
* sizeof(*chunk_pages
)))
1893 do_pages_stat_array(mm
, chunk_nr
, chunk_pages
, chunk_status
);
1895 if (copy_to_user(status
, chunk_status
, chunk_nr
* sizeof(*status
)))
1900 nr_pages
-= chunk_nr
;
1902 return nr_pages
? -EFAULT
: 0;
1905 static struct mm_struct
*find_mm_struct(pid_t pid
, nodemask_t
*mem_nodes
)
1907 struct task_struct
*task
;
1908 struct mm_struct
*mm
;
1911 * There is no need to check if current process has the right to modify
1912 * the specified process when they are same.
1916 *mem_nodes
= cpuset_mems_allowed(current
);
1920 /* Find the mm_struct */
1922 task
= find_task_by_vpid(pid
);
1925 return ERR_PTR(-ESRCH
);
1927 get_task_struct(task
);
1930 * Check if this process has the right to modify the specified
1931 * process. Use the regular "ptrace_may_access()" checks.
1933 if (!ptrace_may_access(task
, PTRACE_MODE_READ_REALCREDS
)) {
1935 mm
= ERR_PTR(-EPERM
);
1940 mm
= ERR_PTR(security_task_movememory(task
));
1943 *mem_nodes
= cpuset_mems_allowed(task
);
1944 mm
= get_task_mm(task
);
1946 put_task_struct(task
);
1948 mm
= ERR_PTR(-EINVAL
);
1953 * Move a list of pages in the address space of the currently executing
1956 static int kernel_move_pages(pid_t pid
, unsigned long nr_pages
,
1957 const void __user
* __user
*pages
,
1958 const int __user
*nodes
,
1959 int __user
*status
, int flags
)
1961 struct mm_struct
*mm
;
1963 nodemask_t task_nodes
;
1966 if (flags
& ~(MPOL_MF_MOVE
|MPOL_MF_MOVE_ALL
))
1969 if ((flags
& MPOL_MF_MOVE_ALL
) && !capable(CAP_SYS_NICE
))
1972 mm
= find_mm_struct(pid
, &task_nodes
);
1977 err
= do_pages_move(mm
, task_nodes
, nr_pages
, pages
,
1978 nodes
, status
, flags
);
1980 err
= do_pages_stat(mm
, nr_pages
, pages
, status
);
1986 SYSCALL_DEFINE6(move_pages
, pid_t
, pid
, unsigned long, nr_pages
,
1987 const void __user
* __user
*, pages
,
1988 const int __user
*, nodes
,
1989 int __user
*, status
, int, flags
)
1991 return kernel_move_pages(pid
, nr_pages
, pages
, nodes
, status
, flags
);
1994 #ifdef CONFIG_COMPAT
1995 COMPAT_SYSCALL_DEFINE6(move_pages
, pid_t
, pid
, compat_ulong_t
, nr_pages
,
1996 compat_uptr_t __user
*, pages32
,
1997 const int __user
*, nodes
,
1998 int __user
*, status
,
2001 const void __user
* __user
*pages
;
2004 pages
= compat_alloc_user_space(nr_pages
* sizeof(void *));
2005 for (i
= 0; i
< nr_pages
; i
++) {
2008 if (get_user(p
, pages32
+ i
) ||
2009 put_user(compat_ptr(p
), pages
+ i
))
2012 return kernel_move_pages(pid
, nr_pages
, pages
, nodes
, status
, flags
);
2014 #endif /* CONFIG_COMPAT */
2016 #ifdef CONFIG_NUMA_BALANCING
2018 * Returns true if this is a safe migration target node for misplaced NUMA
2019 * pages. Currently it only checks the watermarks which crude
2021 static bool migrate_balanced_pgdat(struct pglist_data
*pgdat
,
2022 unsigned long nr_migrate_pages
)
2026 for (z
= pgdat
->nr_zones
- 1; z
>= 0; z
--) {
2027 struct zone
*zone
= pgdat
->node_zones
+ z
;
2029 if (!populated_zone(zone
))
2032 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2033 if (!zone_watermark_ok(zone
, 0,
2034 high_wmark_pages(zone
) +
2043 static struct page
*alloc_misplaced_dst_page(struct page
*page
,
2046 int nid
= (int) data
;
2047 struct page
*newpage
;
2049 newpage
= __alloc_pages_node(nid
,
2050 (GFP_HIGHUSER_MOVABLE
|
2051 __GFP_THISNODE
| __GFP_NOMEMALLOC
|
2052 __GFP_NORETRY
| __GFP_NOWARN
) &
2058 static struct page
*alloc_misplaced_dst_page_thp(struct page
*page
,
2061 int nid
= (int) data
;
2062 struct page
*newpage
;
2064 newpage
= alloc_pages_node(nid
, (GFP_TRANSHUGE_LIGHT
| __GFP_THISNODE
),
2069 prep_transhuge_page(newpage
);
2075 static int numamigrate_isolate_page(pg_data_t
*pgdat
, struct page
*page
)
2079 VM_BUG_ON_PAGE(compound_order(page
) && !PageTransHuge(page
), page
);
2081 /* Do not migrate THP mapped by multiple processes */
2082 if (PageTransHuge(page
) && total_mapcount(page
) > 1)
2085 /* Avoid migrating to a node that is nearly full */
2086 if (!migrate_balanced_pgdat(pgdat
, compound_nr(page
)))
2089 if (isolate_lru_page(page
))
2092 page_lru
= page_is_file_lru(page
);
2093 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+ page_lru
,
2094 thp_nr_pages(page
));
2097 * Isolating the page has taken another reference, so the
2098 * caller's reference can be safely dropped without the page
2099 * disappearing underneath us during migration.
2106 * Attempt to migrate a misplaced page to the specified destination
2107 * node. Caller is expected to have an elevated reference count on
2108 * the page that will be dropped by this function before returning.
2110 int migrate_misplaced_page(struct page
*page
, struct vm_area_struct
*vma
,
2113 pg_data_t
*pgdat
= NODE_DATA(node
);
2116 LIST_HEAD(migratepages
);
2119 unsigned int nr_pages
= thp_nr_pages(page
);
2122 * PTE mapped THP or HugeTLB page can't reach here so the page could
2123 * be either base page or THP. And it must be head page if it is
2126 compound
= PageTransHuge(page
);
2129 new = alloc_misplaced_dst_page_thp
;
2131 new = alloc_misplaced_dst_page
;
2134 * Don't migrate file pages that are mapped in multiple processes
2135 * with execute permissions as they are probably shared libraries.
2137 if (page_mapcount(page
) != 1 && page_is_file_lru(page
) &&
2138 (vma
->vm_flags
& VM_EXEC
))
2142 * Also do not migrate dirty pages as not all filesystems can move
2143 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2145 if (page_is_file_lru(page
) && PageDirty(page
))
2148 isolated
= numamigrate_isolate_page(pgdat
, page
);
2152 list_add(&page
->lru
, &migratepages
);
2153 nr_remaining
= migrate_pages(&migratepages
, *new, NULL
, node
,
2154 MIGRATE_ASYNC
, MR_NUMA_MISPLACED
);
2156 if (!list_empty(&migratepages
)) {
2157 list_del(&page
->lru
);
2158 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+
2159 page_is_file_lru(page
), -nr_pages
);
2160 putback_lru_page(page
);
2164 count_vm_numa_events(NUMA_PAGE_MIGRATE
, nr_pages
);
2165 BUG_ON(!list_empty(&migratepages
));
2172 #endif /* CONFIG_NUMA_BALANCING */
2173 #endif /* CONFIG_NUMA */
2175 #ifdef CONFIG_DEVICE_PRIVATE
2176 static int migrate_vma_collect_skip(unsigned long start
,
2178 struct mm_walk
*walk
)
2180 struct migrate_vma
*migrate
= walk
->private;
2183 for (addr
= start
; addr
< end
; addr
+= PAGE_SIZE
) {
2184 migrate
->dst
[migrate
->npages
] = 0;
2185 migrate
->src
[migrate
->npages
++] = 0;
2191 static int migrate_vma_collect_hole(unsigned long start
,
2193 __always_unused
int depth
,
2194 struct mm_walk
*walk
)
2196 struct migrate_vma
*migrate
= walk
->private;
2199 /* Only allow populating anonymous memory. */
2200 if (!vma_is_anonymous(walk
->vma
))
2201 return migrate_vma_collect_skip(start
, end
, walk
);
2203 for (addr
= start
; addr
< end
; addr
+= PAGE_SIZE
) {
2204 migrate
->src
[migrate
->npages
] = MIGRATE_PFN_MIGRATE
;
2205 migrate
->dst
[migrate
->npages
] = 0;
2213 static int migrate_vma_collect_pmd(pmd_t
*pmdp
,
2214 unsigned long start
,
2216 struct mm_walk
*walk
)
2218 struct migrate_vma
*migrate
= walk
->private;
2219 struct vm_area_struct
*vma
= walk
->vma
;
2220 struct mm_struct
*mm
= vma
->vm_mm
;
2221 unsigned long addr
= start
, unmapped
= 0;
2226 if (pmd_none(*pmdp
))
2227 return migrate_vma_collect_hole(start
, end
, -1, walk
);
2229 if (pmd_trans_huge(*pmdp
)) {
2232 ptl
= pmd_lock(mm
, pmdp
);
2233 if (unlikely(!pmd_trans_huge(*pmdp
))) {
2238 page
= pmd_page(*pmdp
);
2239 if (is_huge_zero_page(page
)) {
2241 split_huge_pmd(vma
, pmdp
, addr
);
2242 if (pmd_trans_unstable(pmdp
))
2243 return migrate_vma_collect_skip(start
, end
,
2250 if (unlikely(!trylock_page(page
)))
2251 return migrate_vma_collect_skip(start
, end
,
2253 ret
= split_huge_page(page
);
2257 return migrate_vma_collect_skip(start
, end
,
2259 if (pmd_none(*pmdp
))
2260 return migrate_vma_collect_hole(start
, end
, -1,
2265 if (unlikely(pmd_bad(*pmdp
)))
2266 return migrate_vma_collect_skip(start
, end
, walk
);
2268 ptep
= pte_offset_map_lock(mm
, pmdp
, addr
, &ptl
);
2269 arch_enter_lazy_mmu_mode();
2271 for (; addr
< end
; addr
+= PAGE_SIZE
, ptep
++) {
2272 unsigned long mpfn
= 0, pfn
;
2279 if (pte_none(pte
)) {
2280 if (vma_is_anonymous(vma
)) {
2281 mpfn
= MIGRATE_PFN_MIGRATE
;
2287 if (!pte_present(pte
)) {
2289 * Only care about unaddressable device page special
2290 * page table entry. Other special swap entries are not
2291 * migratable, and we ignore regular swapped page.
2293 entry
= pte_to_swp_entry(pte
);
2294 if (!is_device_private_entry(entry
))
2297 page
= pfn_swap_entry_to_page(entry
);
2298 if (!(migrate
->flags
&
2299 MIGRATE_VMA_SELECT_DEVICE_PRIVATE
) ||
2300 page
->pgmap
->owner
!= migrate
->pgmap_owner
)
2303 mpfn
= migrate_pfn(page_to_pfn(page
)) |
2304 MIGRATE_PFN_MIGRATE
;
2305 if (is_writable_device_private_entry(entry
))
2306 mpfn
|= MIGRATE_PFN_WRITE
;
2308 if (!(migrate
->flags
& MIGRATE_VMA_SELECT_SYSTEM
))
2311 if (is_zero_pfn(pfn
)) {
2312 mpfn
= MIGRATE_PFN_MIGRATE
;
2316 page
= vm_normal_page(migrate
->vma
, addr
, pte
);
2317 mpfn
= migrate_pfn(pfn
) | MIGRATE_PFN_MIGRATE
;
2318 mpfn
|= pte_write(pte
) ? MIGRATE_PFN_WRITE
: 0;
2321 /* FIXME support THP */
2322 if (!page
|| !page
->mapping
|| PageTransCompound(page
)) {
2328 * By getting a reference on the page we pin it and that blocks
2329 * any kind of migration. Side effect is that it "freezes" the
2332 * We drop this reference after isolating the page from the lru
2333 * for non device page (device page are not on the lru and thus
2334 * can't be dropped from it).
2340 * Optimize for the common case where page is only mapped once
2341 * in one process. If we can lock the page, then we can safely
2342 * set up a special migration page table entry now.
2344 if (trylock_page(page
)) {
2347 mpfn
|= MIGRATE_PFN_LOCKED
;
2348 ptep_get_and_clear(mm
, addr
, ptep
);
2350 /* Setup special migration page table entry */
2351 if (mpfn
& MIGRATE_PFN_WRITE
)
2352 entry
= make_writable_migration_entry(
2355 entry
= make_readable_migration_entry(
2357 swp_pte
= swp_entry_to_pte(entry
);
2358 if (pte_present(pte
)) {
2359 if (pte_soft_dirty(pte
))
2360 swp_pte
= pte_swp_mksoft_dirty(swp_pte
);
2361 if (pte_uffd_wp(pte
))
2362 swp_pte
= pte_swp_mkuffd_wp(swp_pte
);
2364 if (pte_swp_soft_dirty(pte
))
2365 swp_pte
= pte_swp_mksoft_dirty(swp_pte
);
2366 if (pte_swp_uffd_wp(pte
))
2367 swp_pte
= pte_swp_mkuffd_wp(swp_pte
);
2369 set_pte_at(mm
, addr
, ptep
, swp_pte
);
2372 * This is like regular unmap: we remove the rmap and
2373 * drop page refcount. Page won't be freed, as we took
2374 * a reference just above.
2376 page_remove_rmap(page
, false);
2379 if (pte_present(pte
))
2384 migrate
->dst
[migrate
->npages
] = 0;
2385 migrate
->src
[migrate
->npages
++] = mpfn
;
2387 arch_leave_lazy_mmu_mode();
2388 pte_unmap_unlock(ptep
- 1, ptl
);
2390 /* Only flush the TLB if we actually modified any entries */
2392 flush_tlb_range(walk
->vma
, start
, end
);
2397 static const struct mm_walk_ops migrate_vma_walk_ops
= {
2398 .pmd_entry
= migrate_vma_collect_pmd
,
2399 .pte_hole
= migrate_vma_collect_hole
,
2403 * migrate_vma_collect() - collect pages over a range of virtual addresses
2404 * @migrate: migrate struct containing all migration information
2406 * This will walk the CPU page table. For each virtual address backed by a
2407 * valid page, it updates the src array and takes a reference on the page, in
2408 * order to pin the page until we lock it and unmap it.
2410 static void migrate_vma_collect(struct migrate_vma
*migrate
)
2412 struct mmu_notifier_range range
;
2415 * Note that the pgmap_owner is passed to the mmu notifier callback so
2416 * that the registered device driver can skip invalidating device
2417 * private page mappings that won't be migrated.
2419 mmu_notifier_range_init_owner(&range
, MMU_NOTIFY_MIGRATE
, 0,
2420 migrate
->vma
, migrate
->vma
->vm_mm
, migrate
->start
, migrate
->end
,
2421 migrate
->pgmap_owner
);
2422 mmu_notifier_invalidate_range_start(&range
);
2424 walk_page_range(migrate
->vma
->vm_mm
, migrate
->start
, migrate
->end
,
2425 &migrate_vma_walk_ops
, migrate
);
2427 mmu_notifier_invalidate_range_end(&range
);
2428 migrate
->end
= migrate
->start
+ (migrate
->npages
<< PAGE_SHIFT
);
2432 * migrate_vma_check_page() - check if page is pinned or not
2433 * @page: struct page to check
2435 * Pinned pages cannot be migrated. This is the same test as in
2436 * migrate_page_move_mapping(), except that here we allow migration of a
2439 static bool migrate_vma_check_page(struct page
*page
)
2442 * One extra ref because caller holds an extra reference, either from
2443 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2449 * FIXME support THP (transparent huge page), it is bit more complex to
2450 * check them than regular pages, because they can be mapped with a pmd
2451 * or with a pte (split pte mapping).
2453 if (PageCompound(page
))
2456 /* Page from ZONE_DEVICE have one extra reference */
2457 if (is_zone_device_page(page
)) {
2459 * Private page can never be pin as they have no valid pte and
2460 * GUP will fail for those. Yet if there is a pending migration
2461 * a thread might try to wait on the pte migration entry and
2462 * will bump the page reference count. Sadly there is no way to
2463 * differentiate a regular pin from migration wait. Hence to
2464 * avoid 2 racing thread trying to migrate back to CPU to enter
2465 * infinite loop (one stopping migration because the other is
2466 * waiting on pte migration entry). We always return true here.
2468 * FIXME proper solution is to rework migration_entry_wait() so
2469 * it does not need to take a reference on page.
2471 return is_device_private_page(page
);
2474 /* For file back page */
2475 if (page_mapping(page
))
2476 extra
+= 1 + page_has_private(page
);
2478 if ((page_count(page
) - extra
) > page_mapcount(page
))
2485 * migrate_vma_prepare() - lock pages and isolate them from the lru
2486 * @migrate: migrate struct containing all migration information
2488 * This locks pages that have been collected by migrate_vma_collect(). Once each
2489 * page is locked it is isolated from the lru (for non-device pages). Finally,
2490 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2491 * migrated by concurrent kernel threads.
2493 static void migrate_vma_prepare(struct migrate_vma
*migrate
)
2495 const unsigned long npages
= migrate
->npages
;
2496 const unsigned long start
= migrate
->start
;
2497 unsigned long addr
, i
, restore
= 0;
2498 bool allow_drain
= true;
2502 for (i
= 0; (i
< npages
) && migrate
->cpages
; i
++) {
2503 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2509 if (!(migrate
->src
[i
] & MIGRATE_PFN_LOCKED
)) {
2511 * Because we are migrating several pages there can be
2512 * a deadlock between 2 concurrent migration where each
2513 * are waiting on each other page lock.
2515 * Make migrate_vma() a best effort thing and backoff
2516 * for any page we can not lock right away.
2518 if (!trylock_page(page
)) {
2519 migrate
->src
[i
] = 0;
2525 migrate
->src
[i
] |= MIGRATE_PFN_LOCKED
;
2528 /* ZONE_DEVICE pages are not on LRU */
2529 if (!is_zone_device_page(page
)) {
2530 if (!PageLRU(page
) && allow_drain
) {
2531 /* Drain CPU's pagevec */
2532 lru_add_drain_all();
2533 allow_drain
= false;
2536 if (isolate_lru_page(page
)) {
2538 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2542 migrate
->src
[i
] = 0;
2550 /* Drop the reference we took in collect */
2554 if (!migrate_vma_check_page(page
)) {
2556 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2560 if (!is_zone_device_page(page
)) {
2562 putback_lru_page(page
);
2565 migrate
->src
[i
] = 0;
2569 if (!is_zone_device_page(page
))
2570 putback_lru_page(page
);
2577 for (i
= 0, addr
= start
; i
< npages
&& restore
; i
++, addr
+= PAGE_SIZE
) {
2578 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2580 if (!page
|| (migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2583 remove_migration_pte(page
, migrate
->vma
, addr
, page
);
2585 migrate
->src
[i
] = 0;
2593 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2594 * @migrate: migrate struct containing all migration information
2596 * Replace page mapping (CPU page table pte) with a special migration pte entry
2597 * and check again if it has been pinned. Pinned pages are restored because we
2598 * cannot migrate them.
2600 * This is the last step before we call the device driver callback to allocate
2601 * destination memory and copy contents of original page over to new page.
2603 static void migrate_vma_unmap(struct migrate_vma
*migrate
)
2605 const unsigned long npages
= migrate
->npages
;
2606 const unsigned long start
= migrate
->start
;
2607 unsigned long addr
, i
, restore
= 0;
2609 for (i
= 0; i
< npages
; i
++) {
2610 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2612 if (!page
|| !(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2615 if (page_mapped(page
)) {
2616 try_to_migrate(page
, 0);
2617 if (page_mapped(page
))
2621 if (migrate_vma_check_page(page
))
2625 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2630 for (addr
= start
, i
= 0; i
< npages
&& restore
; addr
+= PAGE_SIZE
, i
++) {
2631 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2633 if (!page
|| (migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2636 remove_migration_ptes(page
, page
, false);
2638 migrate
->src
[i
] = 0;
2642 if (is_zone_device_page(page
))
2645 putback_lru_page(page
);
2650 * migrate_vma_setup() - prepare to migrate a range of memory
2651 * @args: contains the vma, start, and pfns arrays for the migration
2653 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2656 * Prepare to migrate a range of memory virtual address range by collecting all
2657 * the pages backing each virtual address in the range, saving them inside the
2658 * src array. Then lock those pages and unmap them. Once the pages are locked
2659 * and unmapped, check whether each page is pinned or not. Pages that aren't
2660 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2661 * corresponding src array entry. Then restores any pages that are pinned, by
2662 * remapping and unlocking those pages.
2664 * The caller should then allocate destination memory and copy source memory to
2665 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2666 * flag set). Once these are allocated and copied, the caller must update each
2667 * corresponding entry in the dst array with the pfn value of the destination
2668 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2669 * (destination pages must have their struct pages locked, via lock_page()).
2671 * Note that the caller does not have to migrate all the pages that are marked
2672 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2673 * device memory to system memory. If the caller cannot migrate a device page
2674 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2675 * consequences for the userspace process, so it must be avoided if at all
2678 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2679 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
2680 * allowing the caller to allocate device memory for those unbacked virtual
2681 * addresses. For this the caller simply has to allocate device memory and
2682 * properly set the destination entry like for regular migration. Note that
2683 * this can still fail, and thus inside the device driver you must check if the
2684 * migration was successful for those entries after calling migrate_vma_pages(),
2685 * just like for regular migration.
2687 * After that, the callers must call migrate_vma_pages() to go over each entry
2688 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2689 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2690 * then migrate_vma_pages() to migrate struct page information from the source
2691 * struct page to the destination struct page. If it fails to migrate the
2692 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2695 * At this point all successfully migrated pages have an entry in the src
2696 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2697 * array entry with MIGRATE_PFN_VALID flag set.
2699 * Once migrate_vma_pages() returns the caller may inspect which pages were
2700 * successfully migrated, and which were not. Successfully migrated pages will
2701 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2703 * It is safe to update device page table after migrate_vma_pages() because
2704 * both destination and source page are still locked, and the mmap_lock is held
2705 * in read mode (hence no one can unmap the range being migrated).
2707 * Once the caller is done cleaning up things and updating its page table (if it
2708 * chose to do so, this is not an obligation) it finally calls
2709 * migrate_vma_finalize() to update the CPU page table to point to new pages
2710 * for successfully migrated pages or otherwise restore the CPU page table to
2711 * point to the original source pages.
2713 int migrate_vma_setup(struct migrate_vma
*args
)
2715 long nr_pages
= (args
->end
- args
->start
) >> PAGE_SHIFT
;
2717 args
->start
&= PAGE_MASK
;
2718 args
->end
&= PAGE_MASK
;
2719 if (!args
->vma
|| is_vm_hugetlb_page(args
->vma
) ||
2720 (args
->vma
->vm_flags
& VM_SPECIAL
) || vma_is_dax(args
->vma
))
2724 if (args
->start
< args
->vma
->vm_start
||
2725 args
->start
>= args
->vma
->vm_end
)
2727 if (args
->end
<= args
->vma
->vm_start
|| args
->end
> args
->vma
->vm_end
)
2729 if (!args
->src
|| !args
->dst
)
2732 memset(args
->src
, 0, sizeof(*args
->src
) * nr_pages
);
2736 migrate_vma_collect(args
);
2739 migrate_vma_prepare(args
);
2741 migrate_vma_unmap(args
);
2744 * At this point pages are locked and unmapped, and thus they have
2745 * stable content and can safely be copied to destination memory that
2746 * is allocated by the drivers.
2751 EXPORT_SYMBOL(migrate_vma_setup
);
2754 * This code closely matches the code in:
2755 * __handle_mm_fault()
2756 * handle_pte_fault()
2757 * do_anonymous_page()
2758 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2761 static void migrate_vma_insert_page(struct migrate_vma
*migrate
,
2766 struct vm_area_struct
*vma
= migrate
->vma
;
2767 struct mm_struct
*mm
= vma
->vm_mm
;
2777 /* Only allow populating anonymous memory */
2778 if (!vma_is_anonymous(vma
))
2781 pgdp
= pgd_offset(mm
, addr
);
2782 p4dp
= p4d_alloc(mm
, pgdp
, addr
);
2785 pudp
= pud_alloc(mm
, p4dp
, addr
);
2788 pmdp
= pmd_alloc(mm
, pudp
, addr
);
2792 if (pmd_trans_huge(*pmdp
) || pmd_devmap(*pmdp
))
2796 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2797 * pte_offset_map() on pmds where a huge pmd might be created
2798 * from a different thread.
2800 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
2801 * parallel threads are excluded by other means.
2803 * Here we only have mmap_read_lock(mm).
2805 if (pte_alloc(mm
, pmdp
))
2808 /* See the comment in pte_alloc_one_map() */
2809 if (unlikely(pmd_trans_unstable(pmdp
)))
2812 if (unlikely(anon_vma_prepare(vma
)))
2814 if (mem_cgroup_charge(page
, vma
->vm_mm
, GFP_KERNEL
))
2818 * The memory barrier inside __SetPageUptodate makes sure that
2819 * preceding stores to the page contents become visible before
2820 * the set_pte_at() write.
2822 __SetPageUptodate(page
);
2824 if (is_zone_device_page(page
)) {
2825 if (is_device_private_page(page
)) {
2826 swp_entry_t swp_entry
;
2828 if (vma
->vm_flags
& VM_WRITE
)
2829 swp_entry
= make_writable_device_private_entry(
2832 swp_entry
= make_readable_device_private_entry(
2834 entry
= swp_entry_to_pte(swp_entry
);
2837 * For now we only support migrating to un-addressable
2840 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2844 entry
= mk_pte(page
, vma
->vm_page_prot
);
2845 if (vma
->vm_flags
& VM_WRITE
)
2846 entry
= pte_mkwrite(pte_mkdirty(entry
));
2849 ptep
= pte_offset_map_lock(mm
, pmdp
, addr
, &ptl
);
2851 if (check_stable_address_space(mm
))
2854 if (pte_present(*ptep
)) {
2855 unsigned long pfn
= pte_pfn(*ptep
);
2857 if (!is_zero_pfn(pfn
))
2860 } else if (!pte_none(*ptep
))
2864 * Check for userfaultfd but do not deliver the fault. Instead,
2867 if (userfaultfd_missing(vma
))
2870 inc_mm_counter(mm
, MM_ANONPAGES
);
2871 page_add_new_anon_rmap(page
, vma
, addr
, false);
2872 if (!is_zone_device_page(page
))
2873 lru_cache_add_inactive_or_unevictable(page
, vma
);
2877 flush_cache_page(vma
, addr
, pte_pfn(*ptep
));
2878 ptep_clear_flush_notify(vma
, addr
, ptep
);
2879 set_pte_at_notify(mm
, addr
, ptep
, entry
);
2880 update_mmu_cache(vma
, addr
, ptep
);
2882 /* No need to invalidate - it was non-present before */
2883 set_pte_at(mm
, addr
, ptep
, entry
);
2884 update_mmu_cache(vma
, addr
, ptep
);
2887 pte_unmap_unlock(ptep
, ptl
);
2888 *src
= MIGRATE_PFN_MIGRATE
;
2892 pte_unmap_unlock(ptep
, ptl
);
2894 *src
&= ~MIGRATE_PFN_MIGRATE
;
2898 * migrate_vma_pages() - migrate meta-data from src page to dst page
2899 * @migrate: migrate struct containing all migration information
2901 * This migrates struct page meta-data from source struct page to destination
2902 * struct page. This effectively finishes the migration from source page to the
2905 void migrate_vma_pages(struct migrate_vma
*migrate
)
2907 const unsigned long npages
= migrate
->npages
;
2908 const unsigned long start
= migrate
->start
;
2909 struct mmu_notifier_range range
;
2910 unsigned long addr
, i
;
2911 bool notified
= false;
2913 for (i
= 0, addr
= start
; i
< npages
; addr
+= PAGE_SIZE
, i
++) {
2914 struct page
*newpage
= migrate_pfn_to_page(migrate
->dst
[i
]);
2915 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2916 struct address_space
*mapping
;
2920 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2925 if (!(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2930 mmu_notifier_range_init_owner(&range
,
2931 MMU_NOTIFY_MIGRATE
, 0, migrate
->vma
,
2932 migrate
->vma
->vm_mm
, addr
, migrate
->end
,
2933 migrate
->pgmap_owner
);
2934 mmu_notifier_invalidate_range_start(&range
);
2936 migrate_vma_insert_page(migrate
, addr
, newpage
,
2941 mapping
= page_mapping(page
);
2943 if (is_zone_device_page(newpage
)) {
2944 if (is_device_private_page(newpage
)) {
2946 * For now only support private anonymous when
2947 * migrating to un-addressable device memory.
2950 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2955 * Other types of ZONE_DEVICE page are not
2958 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2963 r
= migrate_page(mapping
, newpage
, page
, MIGRATE_SYNC_NO_COPY
);
2964 if (r
!= MIGRATEPAGE_SUCCESS
)
2965 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2969 * No need to double call mmu_notifier->invalidate_range() callback as
2970 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
2971 * did already call it.
2974 mmu_notifier_invalidate_range_only_end(&range
);
2976 EXPORT_SYMBOL(migrate_vma_pages
);
2979 * migrate_vma_finalize() - restore CPU page table entry
2980 * @migrate: migrate struct containing all migration information
2982 * This replaces the special migration pte entry with either a mapping to the
2983 * new page if migration was successful for that page, or to the original page
2986 * This also unlocks the pages and puts them back on the lru, or drops the extra
2987 * refcount, for device pages.
2989 void migrate_vma_finalize(struct migrate_vma
*migrate
)
2991 const unsigned long npages
= migrate
->npages
;
2994 for (i
= 0; i
< npages
; i
++) {
2995 struct page
*newpage
= migrate_pfn_to_page(migrate
->dst
[i
]);
2996 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
3000 unlock_page(newpage
);
3006 if (!(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
) || !newpage
) {
3008 unlock_page(newpage
);
3014 remove_migration_ptes(page
, newpage
, false);
3017 if (is_zone_device_page(page
))
3020 putback_lru_page(page
);
3022 if (newpage
!= page
) {
3023 unlock_page(newpage
);
3024 if (is_zone_device_page(newpage
))
3027 putback_lru_page(newpage
);
3031 EXPORT_SYMBOL(migrate_vma_finalize
);
3032 #endif /* CONFIG_DEVICE_PRIVATE */