2 * Memory Migration functionality - linux/mm/migrate.c
4 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6 * Page migration was first developed in the context of the memory hotplug
7 * project. The main authors of the migration code are:
9 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10 * Hirokazu Takahashi <taka@valinux.co.jp>
11 * Dave Hansen <haveblue@us.ibm.com>
15 #include <linux/migrate.h>
16 #include <linux/export.h>
17 #include <linux/swap.h>
18 #include <linux/swapops.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mm_inline.h>
22 #include <linux/nsproxy.h>
23 #include <linux/pagevec.h>
24 #include <linux/ksm.h>
25 #include <linux/rmap.h>
26 #include <linux/topology.h>
27 #include <linux/cpu.h>
28 #include <linux/cpuset.h>
29 #include <linux/writeback.h>
30 #include <linux/mempolicy.h>
31 #include <linux/vmalloc.h>
32 #include <linux/security.h>
33 #include <linux/backing-dev.h>
34 #include <linux/compaction.h>
35 #include <linux/syscalls.h>
36 #include <linux/hugetlb.h>
37 #include <linux/hugetlb_cgroup.h>
38 #include <linux/gfp.h>
39 #include <linux/memremap.h>
40 #include <linux/balloon_compaction.h>
41 #include <linux/mmu_notifier.h>
42 #include <linux/page_idle.h>
43 #include <linux/page_owner.h>
44 #include <linux/sched/mm.h>
45 #include <linux/ptrace.h>
47 #include <asm/tlbflush.h>
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/migrate.h>
55 * migrate_prep() needs to be called before we start compiling a list of pages
56 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
57 * undesirable, use migrate_prep_local()
59 int migrate_prep(void)
62 * Clear the LRU lists so pages can be isolated.
63 * Note that pages may be moved off the LRU after we have
64 * drained them. Those pages will fail to migrate like other
65 * pages that may be busy.
72 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
73 int migrate_prep_local(void)
80 int isolate_movable_page(struct page
*page
, isolate_mode_t mode
)
82 struct address_space
*mapping
;
85 * Avoid burning cycles with pages that are yet under __free_pages(),
86 * or just got freed under us.
88 * In case we 'win' a race for a movable page being freed under us and
89 * raise its refcount preventing __free_pages() from doing its job
90 * the put_page() at the end of this block will take care of
91 * release this page, thus avoiding a nasty leakage.
93 if (unlikely(!get_page_unless_zero(page
)))
97 * Check PageMovable before holding a PG_lock because page's owner
98 * assumes anybody doesn't touch PG_lock of newly allocated page
99 * so unconditionally grapping the lock ruins page's owner side.
101 if (unlikely(!__PageMovable(page
)))
104 * As movable pages are not isolated from LRU lists, concurrent
105 * compaction threads can race against page migration functions
106 * as well as race against the releasing a page.
108 * In order to avoid having an already isolated movable page
109 * being (wrongly) re-isolated while it is under migration,
110 * or to avoid attempting to isolate pages being released,
111 * lets be sure we have the page lock
112 * before proceeding with the movable page isolation steps.
114 if (unlikely(!trylock_page(page
)))
117 if (!PageMovable(page
) || PageIsolated(page
))
118 goto out_no_isolated
;
120 mapping
= page_mapping(page
);
121 VM_BUG_ON_PAGE(!mapping
, page
);
123 if (!mapping
->a_ops
->isolate_page(page
, mode
))
124 goto out_no_isolated
;
126 /* Driver shouldn't use PG_isolated bit of page->flags */
127 WARN_ON_ONCE(PageIsolated(page
));
128 __SetPageIsolated(page
);
141 /* It should be called on page which is PG_movable */
142 void putback_movable_page(struct page
*page
)
144 struct address_space
*mapping
;
146 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
147 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
148 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
150 mapping
= page_mapping(page
);
151 mapping
->a_ops
->putback_page(page
);
152 __ClearPageIsolated(page
);
156 * Put previously isolated pages back onto the appropriate lists
157 * from where they were once taken off for compaction/migration.
159 * This function shall be used whenever the isolated pageset has been
160 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
161 * and isolate_huge_page().
163 void putback_movable_pages(struct list_head
*l
)
168 list_for_each_entry_safe(page
, page2
, l
, lru
) {
169 if (unlikely(PageHuge(page
))) {
170 putback_active_hugepage(page
);
173 list_del(&page
->lru
);
175 * We isolated non-lru movable page so here we can use
176 * __PageMovable because LRU page's mapping cannot have
177 * PAGE_MAPPING_MOVABLE.
179 if (unlikely(__PageMovable(page
))) {
180 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
182 if (PageMovable(page
))
183 putback_movable_page(page
);
185 __ClearPageIsolated(page
);
189 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+
190 page_is_file_cache(page
), -hpage_nr_pages(page
));
191 putback_lru_page(page
);
197 * Restore a potential migration pte to a working pte entry
199 static bool remove_migration_pte(struct page
*page
, struct vm_area_struct
*vma
,
200 unsigned long addr
, void *old
)
202 struct page_vma_mapped_walk pvmw
= {
206 .flags
= PVMW_SYNC
| PVMW_MIGRATION
,
212 VM_BUG_ON_PAGE(PageTail(page
), page
);
213 while (page_vma_mapped_walk(&pvmw
)) {
217 new = page
- pvmw
.page
->index
+
218 linear_page_index(vma
, pvmw
.address
);
220 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
221 /* PMD-mapped THP migration entry */
223 VM_BUG_ON_PAGE(PageHuge(page
) || !PageTransCompound(page
), page
);
224 remove_migration_pmd(&pvmw
, new);
230 pte
= pte_mkold(mk_pte(new, READ_ONCE(vma
->vm_page_prot
)));
231 if (pte_swp_soft_dirty(*pvmw
.pte
))
232 pte
= pte_mksoft_dirty(pte
);
235 * Recheck VMA as permissions can change since migration started
237 entry
= pte_to_swp_entry(*pvmw
.pte
);
238 if (is_write_migration_entry(entry
))
239 pte
= maybe_mkwrite(pte
, vma
);
241 if (unlikely(is_zone_device_page(new)) &&
242 is_device_private_page(new)) {
243 entry
= make_device_private_entry(new, pte_write(pte
));
244 pte
= swp_entry_to_pte(entry
);
246 flush_dcache_page(new);
248 #ifdef CONFIG_HUGETLB_PAGE
250 pte
= pte_mkhuge(pte
);
251 pte
= arch_make_huge_pte(pte
, vma
, new, 0);
252 set_huge_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
);
254 hugepage_add_anon_rmap(new, vma
, pvmw
.address
);
256 page_dup_rmap(new, true);
260 set_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
);
263 page_add_anon_rmap(new, vma
, pvmw
.address
, false);
265 page_add_file_rmap(new, false);
267 if (vma
->vm_flags
& VM_LOCKED
&& !PageTransCompound(new))
270 /* No need to invalidate - it was non-present before */
271 update_mmu_cache(vma
, pvmw
.address
, pvmw
.pte
);
278 * Get rid of all migration entries and replace them by
279 * references to the indicated page.
281 void remove_migration_ptes(struct page
*old
, struct page
*new, bool locked
)
283 struct rmap_walk_control rwc
= {
284 .rmap_one
= remove_migration_pte
,
289 rmap_walk_locked(new, &rwc
);
291 rmap_walk(new, &rwc
);
295 * Something used the pte of a page under migration. We need to
296 * get to the page and wait until migration is finished.
297 * When we return from this function the fault will be retried.
299 void __migration_entry_wait(struct mm_struct
*mm
, pte_t
*ptep
,
308 if (!is_swap_pte(pte
))
311 entry
= pte_to_swp_entry(pte
);
312 if (!is_migration_entry(entry
))
315 page
= migration_entry_to_page(entry
);
318 * Once radix-tree replacement of page migration started, page_count
319 * *must* be zero. And, we don't want to call wait_on_page_locked()
320 * against a page without get_page().
321 * So, we use get_page_unless_zero(), here. Even failed, page fault
324 if (!get_page_unless_zero(page
))
326 pte_unmap_unlock(ptep
, ptl
);
327 wait_on_page_locked(page
);
331 pte_unmap_unlock(ptep
, ptl
);
334 void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
335 unsigned long address
)
337 spinlock_t
*ptl
= pte_lockptr(mm
, pmd
);
338 pte_t
*ptep
= pte_offset_map(pmd
, address
);
339 __migration_entry_wait(mm
, ptep
, ptl
);
342 void migration_entry_wait_huge(struct vm_area_struct
*vma
,
343 struct mm_struct
*mm
, pte_t
*pte
)
345 spinlock_t
*ptl
= huge_pte_lockptr(hstate_vma(vma
), mm
, pte
);
346 __migration_entry_wait(mm
, pte
, ptl
);
349 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
350 void pmd_migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
)
355 ptl
= pmd_lock(mm
, pmd
);
356 if (!is_pmd_migration_entry(*pmd
))
358 page
= migration_entry_to_page(pmd_to_swp_entry(*pmd
));
359 if (!get_page_unless_zero(page
))
362 wait_on_page_locked(page
);
371 /* Returns true if all buffers are successfully locked */
372 static bool buffer_migrate_lock_buffers(struct buffer_head
*head
,
373 enum migrate_mode mode
)
375 struct buffer_head
*bh
= head
;
377 /* Simple case, sync compaction */
378 if (mode
!= MIGRATE_ASYNC
) {
382 bh
= bh
->b_this_page
;
384 } while (bh
!= head
);
389 /* async case, we cannot block on lock_buffer so use trylock_buffer */
392 if (!trylock_buffer(bh
)) {
394 * We failed to lock the buffer and cannot stall in
395 * async migration. Release the taken locks
397 struct buffer_head
*failed_bh
= bh
;
400 while (bh
!= failed_bh
) {
403 bh
= bh
->b_this_page
;
408 bh
= bh
->b_this_page
;
409 } while (bh
!= head
);
413 static inline bool buffer_migrate_lock_buffers(struct buffer_head
*head
,
414 enum migrate_mode mode
)
418 #endif /* CONFIG_BLOCK */
421 * Replace the page in the mapping.
423 * The number of remaining references must be:
424 * 1 for anonymous pages without a mapping
425 * 2 for pages with a mapping
426 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
428 int migrate_page_move_mapping(struct address_space
*mapping
,
429 struct page
*newpage
, struct page
*page
,
430 struct buffer_head
*head
, enum migrate_mode mode
,
433 struct zone
*oldzone
, *newzone
;
435 int expected_count
= 1 + extra_count
;
439 * ZONE_DEVICE pages have 1 refcount always held by their device
441 * Note that DAX memory will never reach that point as it does not have
442 * the MEMORY_DEVICE_ALLOW_MIGRATE flag set (see memory_hotplug.h).
444 expected_count
+= is_zone_device_page(page
);
447 /* Anonymous page without mapping */
448 if (page_count(page
) != expected_count
)
451 /* No turning back from here */
452 newpage
->index
= page
->index
;
453 newpage
->mapping
= page
->mapping
;
454 if (PageSwapBacked(page
))
455 __SetPageSwapBacked(newpage
);
457 return MIGRATEPAGE_SUCCESS
;
460 oldzone
= page_zone(page
);
461 newzone
= page_zone(newpage
);
463 spin_lock_irq(&mapping
->tree_lock
);
465 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
468 expected_count
+= 1 + page_has_private(page
);
469 if (page_count(page
) != expected_count
||
470 radix_tree_deref_slot_protected(pslot
, &mapping
->tree_lock
) != page
) {
471 spin_unlock_irq(&mapping
->tree_lock
);
475 if (!page_ref_freeze(page
, expected_count
)) {
476 spin_unlock_irq(&mapping
->tree_lock
);
481 * In the async migration case of moving a page with buffers, lock the
482 * buffers using trylock before the mapping is moved. If the mapping
483 * was moved, we later failed to lock the buffers and could not move
484 * the mapping back due to an elevated page count, we would have to
485 * block waiting on other references to be dropped.
487 if (mode
== MIGRATE_ASYNC
&& head
&&
488 !buffer_migrate_lock_buffers(head
, mode
)) {
489 page_ref_unfreeze(page
, expected_count
);
490 spin_unlock_irq(&mapping
->tree_lock
);
495 * Now we know that no one else is looking at the page:
496 * no turning back from here.
498 newpage
->index
= page
->index
;
499 newpage
->mapping
= page
->mapping
;
500 get_page(newpage
); /* add cache reference */
501 if (PageSwapBacked(page
)) {
502 __SetPageSwapBacked(newpage
);
503 if (PageSwapCache(page
)) {
504 SetPageSwapCache(newpage
);
505 set_page_private(newpage
, page_private(page
));
508 VM_BUG_ON_PAGE(PageSwapCache(page
), page
);
511 /* Move dirty while page refs frozen and newpage not yet exposed */
512 dirty
= PageDirty(page
);
514 ClearPageDirty(page
);
515 SetPageDirty(newpage
);
518 radix_tree_replace_slot(&mapping
->page_tree
, pslot
, newpage
);
521 * Drop cache reference from old page by unfreezing
522 * to one less reference.
523 * We know this isn't the last reference.
525 page_ref_unfreeze(page
, expected_count
- 1);
527 spin_unlock(&mapping
->tree_lock
);
528 /* Leave irq disabled to prevent preemption while updating stats */
531 * If moved to a different zone then also account
532 * the page for that zone. Other VM counters will be
533 * taken care of when we establish references to the
534 * new page and drop references to the old page.
536 * Note that anonymous pages are accounted for
537 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
538 * are mapped to swap space.
540 if (newzone
!= oldzone
) {
541 __dec_node_state(oldzone
->zone_pgdat
, NR_FILE_PAGES
);
542 __inc_node_state(newzone
->zone_pgdat
, NR_FILE_PAGES
);
543 if (PageSwapBacked(page
) && !PageSwapCache(page
)) {
544 __dec_node_state(oldzone
->zone_pgdat
, NR_SHMEM
);
545 __inc_node_state(newzone
->zone_pgdat
, NR_SHMEM
);
547 if (dirty
&& mapping_cap_account_dirty(mapping
)) {
548 __dec_node_state(oldzone
->zone_pgdat
, NR_FILE_DIRTY
);
549 __dec_zone_state(oldzone
, NR_ZONE_WRITE_PENDING
);
550 __inc_node_state(newzone
->zone_pgdat
, NR_FILE_DIRTY
);
551 __inc_zone_state(newzone
, NR_ZONE_WRITE_PENDING
);
556 return MIGRATEPAGE_SUCCESS
;
558 EXPORT_SYMBOL(migrate_page_move_mapping
);
561 * The expected number of remaining references is the same as that
562 * of migrate_page_move_mapping().
564 int migrate_huge_page_move_mapping(struct address_space
*mapping
,
565 struct page
*newpage
, struct page
*page
)
570 spin_lock_irq(&mapping
->tree_lock
);
572 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
575 expected_count
= 2 + page_has_private(page
);
576 if (page_count(page
) != expected_count
||
577 radix_tree_deref_slot_protected(pslot
, &mapping
->tree_lock
) != page
) {
578 spin_unlock_irq(&mapping
->tree_lock
);
582 if (!page_ref_freeze(page
, expected_count
)) {
583 spin_unlock_irq(&mapping
->tree_lock
);
587 newpage
->index
= page
->index
;
588 newpage
->mapping
= page
->mapping
;
592 radix_tree_replace_slot(&mapping
->page_tree
, pslot
, newpage
);
594 page_ref_unfreeze(page
, expected_count
- 1);
596 spin_unlock_irq(&mapping
->tree_lock
);
598 return MIGRATEPAGE_SUCCESS
;
602 * Gigantic pages are so large that we do not guarantee that page++ pointer
603 * arithmetic will work across the entire page. We need something more
606 static void __copy_gigantic_page(struct page
*dst
, struct page
*src
,
610 struct page
*dst_base
= dst
;
611 struct page
*src_base
= src
;
613 for (i
= 0; i
< nr_pages
; ) {
615 copy_highpage(dst
, src
);
618 dst
= mem_map_next(dst
, dst_base
, i
);
619 src
= mem_map_next(src
, src_base
, i
);
623 static void copy_huge_page(struct page
*dst
, struct page
*src
)
630 struct hstate
*h
= page_hstate(src
);
631 nr_pages
= pages_per_huge_page(h
);
633 if (unlikely(nr_pages
> MAX_ORDER_NR_PAGES
)) {
634 __copy_gigantic_page(dst
, src
, nr_pages
);
639 BUG_ON(!PageTransHuge(src
));
640 nr_pages
= hpage_nr_pages(src
);
643 for (i
= 0; i
< nr_pages
; i
++) {
645 copy_highpage(dst
+ i
, src
+ i
);
650 * Copy the page to its new location
652 void migrate_page_states(struct page
*newpage
, struct page
*page
)
657 SetPageError(newpage
);
658 if (PageReferenced(page
))
659 SetPageReferenced(newpage
);
660 if (PageUptodate(page
))
661 SetPageUptodate(newpage
);
662 if (TestClearPageActive(page
)) {
663 VM_BUG_ON_PAGE(PageUnevictable(page
), page
);
664 SetPageActive(newpage
);
665 } else if (TestClearPageUnevictable(page
))
666 SetPageUnevictable(newpage
);
667 if (PageChecked(page
))
668 SetPageChecked(newpage
);
669 if (PageMappedToDisk(page
))
670 SetPageMappedToDisk(newpage
);
672 /* Move dirty on pages not done by migrate_page_move_mapping() */
674 SetPageDirty(newpage
);
676 if (page_is_young(page
))
677 set_page_young(newpage
);
678 if (page_is_idle(page
))
679 set_page_idle(newpage
);
682 * Copy NUMA information to the new page, to prevent over-eager
683 * future migrations of this same page.
685 cpupid
= page_cpupid_xchg_last(page
, -1);
686 page_cpupid_xchg_last(newpage
, cpupid
);
688 ksm_migrate_page(newpage
, page
);
690 * Please do not reorder this without considering how mm/ksm.c's
691 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
693 if (PageSwapCache(page
))
694 ClearPageSwapCache(page
);
695 ClearPagePrivate(page
);
696 set_page_private(page
, 0);
699 * If any waiters have accumulated on the new page then
702 if (PageWriteback(newpage
))
703 end_page_writeback(newpage
);
705 copy_page_owner(page
, newpage
);
707 mem_cgroup_migrate(page
, newpage
);
709 EXPORT_SYMBOL(migrate_page_states
);
711 void migrate_page_copy(struct page
*newpage
, struct page
*page
)
713 if (PageHuge(page
) || PageTransHuge(page
))
714 copy_huge_page(newpage
, page
);
716 copy_highpage(newpage
, page
);
718 migrate_page_states(newpage
, page
);
720 EXPORT_SYMBOL(migrate_page_copy
);
722 /************************************************************
723 * Migration functions
724 ***********************************************************/
727 * Common logic to directly migrate a single LRU page suitable for
728 * pages that do not use PagePrivate/PagePrivate2.
730 * Pages are locked upon entry and exit.
732 int migrate_page(struct address_space
*mapping
,
733 struct page
*newpage
, struct page
*page
,
734 enum migrate_mode mode
)
738 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
740 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, NULL
, mode
, 0);
742 if (rc
!= MIGRATEPAGE_SUCCESS
)
745 if (mode
!= MIGRATE_SYNC_NO_COPY
)
746 migrate_page_copy(newpage
, page
);
748 migrate_page_states(newpage
, page
);
749 return MIGRATEPAGE_SUCCESS
;
751 EXPORT_SYMBOL(migrate_page
);
755 * Migration function for pages with buffers. This function can only be used
756 * if the underlying filesystem guarantees that no other references to "page"
759 int buffer_migrate_page(struct address_space
*mapping
,
760 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
762 struct buffer_head
*bh
, *head
;
765 if (!page_has_buffers(page
))
766 return migrate_page(mapping
, newpage
, page
, mode
);
768 head
= page_buffers(page
);
770 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, head
, mode
, 0);
772 if (rc
!= MIGRATEPAGE_SUCCESS
)
776 * In the async case, migrate_page_move_mapping locked the buffers
777 * with an IRQ-safe spinlock held. In the sync case, the buffers
778 * need to be locked now
780 if (mode
!= MIGRATE_ASYNC
)
781 BUG_ON(!buffer_migrate_lock_buffers(head
, mode
));
783 ClearPagePrivate(page
);
784 set_page_private(newpage
, page_private(page
));
785 set_page_private(page
, 0);
791 set_bh_page(bh
, newpage
, bh_offset(bh
));
792 bh
= bh
->b_this_page
;
794 } while (bh
!= head
);
796 SetPagePrivate(newpage
);
798 if (mode
!= MIGRATE_SYNC_NO_COPY
)
799 migrate_page_copy(newpage
, page
);
801 migrate_page_states(newpage
, page
);
807 bh
= bh
->b_this_page
;
809 } while (bh
!= head
);
811 return MIGRATEPAGE_SUCCESS
;
813 EXPORT_SYMBOL(buffer_migrate_page
);
817 * Writeback a page to clean the dirty state
819 static int writeout(struct address_space
*mapping
, struct page
*page
)
821 struct writeback_control wbc
= {
822 .sync_mode
= WB_SYNC_NONE
,
825 .range_end
= LLONG_MAX
,
830 if (!mapping
->a_ops
->writepage
)
831 /* No write method for the address space */
834 if (!clear_page_dirty_for_io(page
))
835 /* Someone else already triggered a write */
839 * A dirty page may imply that the underlying filesystem has
840 * the page on some queue. So the page must be clean for
841 * migration. Writeout may mean we loose the lock and the
842 * page state is no longer what we checked for earlier.
843 * At this point we know that the migration attempt cannot
846 remove_migration_ptes(page
, page
, false);
848 rc
= mapping
->a_ops
->writepage(page
, &wbc
);
850 if (rc
!= AOP_WRITEPAGE_ACTIVATE
)
851 /* unlocked. Relock */
854 return (rc
< 0) ? -EIO
: -EAGAIN
;
858 * Default handling if a filesystem does not provide a migration function.
860 static int fallback_migrate_page(struct address_space
*mapping
,
861 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
863 if (PageDirty(page
)) {
864 /* Only writeback pages in full synchronous migration */
867 case MIGRATE_SYNC_NO_COPY
:
872 return writeout(mapping
, page
);
876 * Buffers may be managed in a filesystem specific way.
877 * We must have no buffers or drop them.
879 if (page_has_private(page
) &&
880 !try_to_release_page(page
, GFP_KERNEL
))
883 return migrate_page(mapping
, newpage
, page
, mode
);
887 * Move a page to a newly allocated page
888 * The page is locked and all ptes have been successfully removed.
890 * The new page will have replaced the old page if this function
895 * MIGRATEPAGE_SUCCESS - success
897 static int move_to_new_page(struct page
*newpage
, struct page
*page
,
898 enum migrate_mode mode
)
900 struct address_space
*mapping
;
902 bool is_lru
= !__PageMovable(page
);
904 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
905 VM_BUG_ON_PAGE(!PageLocked(newpage
), newpage
);
907 mapping
= page_mapping(page
);
909 if (likely(is_lru
)) {
911 rc
= migrate_page(mapping
, newpage
, page
, mode
);
912 else if (mapping
->a_ops
->migratepage
)
914 * Most pages have a mapping and most filesystems
915 * provide a migratepage callback. Anonymous pages
916 * are part of swap space which also has its own
917 * migratepage callback. This is the most common path
918 * for page migration.
920 rc
= mapping
->a_ops
->migratepage(mapping
, newpage
,
923 rc
= fallback_migrate_page(mapping
, newpage
,
927 * In case of non-lru page, it could be released after
928 * isolation step. In that case, we shouldn't try migration.
930 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
931 if (!PageMovable(page
)) {
932 rc
= MIGRATEPAGE_SUCCESS
;
933 __ClearPageIsolated(page
);
937 rc
= mapping
->a_ops
->migratepage(mapping
, newpage
,
939 WARN_ON_ONCE(rc
== MIGRATEPAGE_SUCCESS
&&
940 !PageIsolated(page
));
944 * When successful, old pagecache page->mapping must be cleared before
945 * page is freed; but stats require that PageAnon be left as PageAnon.
947 if (rc
== MIGRATEPAGE_SUCCESS
) {
948 if (__PageMovable(page
)) {
949 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
952 * We clear PG_movable under page_lock so any compactor
953 * cannot try to migrate this page.
955 __ClearPageIsolated(page
);
959 * Anonymous and movable page->mapping will be cleard by
960 * free_pages_prepare so don't reset it here for keeping
961 * the type to work PageAnon, for example.
963 if (!PageMappingFlags(page
))
964 page
->mapping
= NULL
;
970 static int __unmap_and_move(struct page
*page
, struct page
*newpage
,
971 int force
, enum migrate_mode mode
)
974 int page_was_mapped
= 0;
975 struct anon_vma
*anon_vma
= NULL
;
976 bool is_lru
= !__PageMovable(page
);
978 if (!trylock_page(page
)) {
979 if (!force
|| mode
== MIGRATE_ASYNC
)
983 * It's not safe for direct compaction to call lock_page.
984 * For example, during page readahead pages are added locked
985 * to the LRU. Later, when the IO completes the pages are
986 * marked uptodate and unlocked. However, the queueing
987 * could be merging multiple pages for one bio (e.g.
988 * mpage_readpages). If an allocation happens for the
989 * second or third page, the process can end up locking
990 * the same page twice and deadlocking. Rather than
991 * trying to be clever about what pages can be locked,
992 * avoid the use of lock_page for direct compaction
995 if (current
->flags
& PF_MEMALLOC
)
1001 if (PageWriteback(page
)) {
1003 * Only in the case of a full synchronous migration is it
1004 * necessary to wait for PageWriteback. In the async case,
1005 * the retry loop is too short and in the sync-light case,
1006 * the overhead of stalling is too much
1010 case MIGRATE_SYNC_NO_COPY
:
1018 wait_on_page_writeback(page
);
1022 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
1023 * we cannot notice that anon_vma is freed while we migrates a page.
1024 * This get_anon_vma() delays freeing anon_vma pointer until the end
1025 * of migration. File cache pages are no problem because of page_lock()
1026 * File Caches may use write_page() or lock_page() in migration, then,
1027 * just care Anon page here.
1029 * Only page_get_anon_vma() understands the subtleties of
1030 * getting a hold on an anon_vma from outside one of its mms.
1031 * But if we cannot get anon_vma, then we won't need it anyway,
1032 * because that implies that the anon page is no longer mapped
1033 * (and cannot be remapped so long as we hold the page lock).
1035 if (PageAnon(page
) && !PageKsm(page
))
1036 anon_vma
= page_get_anon_vma(page
);
1039 * Block others from accessing the new page when we get around to
1040 * establishing additional references. We are usually the only one
1041 * holding a reference to newpage at this point. We used to have a BUG
1042 * here if trylock_page(newpage) fails, but would like to allow for
1043 * cases where there might be a race with the previous use of newpage.
1044 * This is much like races on refcount of oldpage: just don't BUG().
1046 if (unlikely(!trylock_page(newpage
)))
1049 if (unlikely(!is_lru
)) {
1050 rc
= move_to_new_page(newpage
, page
, mode
);
1051 goto out_unlock_both
;
1055 * Corner case handling:
1056 * 1. When a new swap-cache page is read into, it is added to the LRU
1057 * and treated as swapcache but it has no rmap yet.
1058 * Calling try_to_unmap() against a page->mapping==NULL page will
1059 * trigger a BUG. So handle it here.
1060 * 2. An orphaned page (see truncate_complete_page) might have
1061 * fs-private metadata. The page can be picked up due to memory
1062 * offlining. Everywhere else except page reclaim, the page is
1063 * invisible to the vm, so the page can not be migrated. So try to
1064 * free the metadata, so the page can be freed.
1066 if (!page
->mapping
) {
1067 VM_BUG_ON_PAGE(PageAnon(page
), page
);
1068 if (page_has_private(page
)) {
1069 try_to_free_buffers(page
);
1070 goto out_unlock_both
;
1072 } else if (page_mapped(page
)) {
1073 /* Establish migration ptes */
1074 VM_BUG_ON_PAGE(PageAnon(page
) && !PageKsm(page
) && !anon_vma
,
1077 TTU_MIGRATION
|TTU_IGNORE_MLOCK
|TTU_IGNORE_ACCESS
);
1078 page_was_mapped
= 1;
1081 if (!page_mapped(page
))
1082 rc
= move_to_new_page(newpage
, page
, mode
);
1084 if (page_was_mapped
)
1085 remove_migration_ptes(page
,
1086 rc
== MIGRATEPAGE_SUCCESS
? newpage
: page
, false);
1089 unlock_page(newpage
);
1091 /* Drop an anon_vma reference if we took one */
1093 put_anon_vma(anon_vma
);
1097 * If migration is successful, decrease refcount of the newpage
1098 * which will not free the page because new page owner increased
1099 * refcounter. As well, if it is LRU page, add the page to LRU
1102 if (rc
== MIGRATEPAGE_SUCCESS
) {
1103 if (unlikely(__PageMovable(newpage
)))
1106 putback_lru_page(newpage
);
1113 * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
1116 #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
1117 #define ICE_noinline noinline
1119 #define ICE_noinline
1123 * Obtain the lock on page, remove all ptes and migrate the page
1124 * to the newly allocated page in newpage.
1126 static ICE_noinline
int unmap_and_move(new_page_t get_new_page
,
1127 free_page_t put_new_page
,
1128 unsigned long private, struct page
*page
,
1129 int force
, enum migrate_mode mode
,
1130 enum migrate_reason reason
)
1132 int rc
= MIGRATEPAGE_SUCCESS
;
1134 struct page
*newpage
;
1136 newpage
= get_new_page(page
, private, &result
);
1140 if (page_count(page
) == 1) {
1141 /* page was freed from under us. So we are done. */
1142 ClearPageActive(page
);
1143 ClearPageUnevictable(page
);
1144 if (unlikely(__PageMovable(page
))) {
1146 if (!PageMovable(page
))
1147 __ClearPageIsolated(page
);
1151 put_new_page(newpage
, private);
1157 if (unlikely(PageTransHuge(page
) && !PageTransHuge(newpage
))) {
1159 rc
= split_huge_page(page
);
1165 rc
= __unmap_and_move(page
, newpage
, force
, mode
);
1166 if (rc
== MIGRATEPAGE_SUCCESS
)
1167 set_page_owner_migrate_reason(newpage
, reason
);
1170 if (rc
!= -EAGAIN
) {
1172 * A page that has been migrated has all references
1173 * removed and will be freed. A page that has not been
1174 * migrated will have kepts its references and be
1177 list_del(&page
->lru
);
1180 * Compaction can migrate also non-LRU pages which are
1181 * not accounted to NR_ISOLATED_*. They can be recognized
1184 if (likely(!__PageMovable(page
)))
1185 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+
1186 page_is_file_cache(page
), -hpage_nr_pages(page
));
1190 * If migration is successful, releases reference grabbed during
1191 * isolation. Otherwise, restore the page to right list unless
1194 if (rc
== MIGRATEPAGE_SUCCESS
) {
1196 if (reason
== MR_MEMORY_FAILURE
) {
1198 * Set PG_HWPoison on just freed page
1199 * intentionally. Although it's rather weird,
1200 * it's how HWPoison flag works at the moment.
1202 if (!test_set_page_hwpoison(page
))
1203 num_poisoned_pages_inc();
1206 if (rc
!= -EAGAIN
) {
1207 if (likely(!__PageMovable(page
))) {
1208 putback_lru_page(page
);
1213 if (PageMovable(page
))
1214 putback_movable_page(page
);
1216 __ClearPageIsolated(page
);
1222 put_new_page(newpage
, private);
1231 *result
= page_to_nid(newpage
);
1237 * Counterpart of unmap_and_move_page() for hugepage migration.
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.
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.
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
)
1261 int page_was_mapped
= 0;
1262 struct page
*new_hpage
;
1263 struct anon_vma
*anon_vma
= NULL
;
1266 * Movability of hugepages depends on architectures and hugepage size.
1267 * This check is necessary because some callers of hugepage migration
1268 * like soft offline and memory hotremove don't walk through page
1269 * tables or check whether the hugepage is pmd-based or not before
1270 * kicking migration.
1272 if (!hugepage_migration_supported(page_hstate(hpage
))) {
1273 putback_active_hugepage(hpage
);
1277 new_hpage
= get_new_page(hpage
, private, &result
);
1281 if (!trylock_page(hpage
)) {
1286 case MIGRATE_SYNC_NO_COPY
:
1294 if (PageAnon(hpage
))
1295 anon_vma
= page_get_anon_vma(hpage
);
1297 if (unlikely(!trylock_page(new_hpage
)))
1300 if (page_mapped(hpage
)) {
1302 TTU_MIGRATION
|TTU_IGNORE_MLOCK
|TTU_IGNORE_ACCESS
);
1303 page_was_mapped
= 1;
1306 if (!page_mapped(hpage
))
1307 rc
= move_to_new_page(new_hpage
, hpage
, mode
);
1309 if (page_was_mapped
)
1310 remove_migration_ptes(hpage
,
1311 rc
== MIGRATEPAGE_SUCCESS
? new_hpage
: hpage
, false);
1313 unlock_page(new_hpage
);
1317 put_anon_vma(anon_vma
);
1319 if (rc
== MIGRATEPAGE_SUCCESS
) {
1320 hugetlb_cgroup_migrate(hpage
, new_hpage
);
1321 put_new_page
= NULL
;
1322 set_page_owner_migrate_reason(new_hpage
, reason
);
1328 putback_active_hugepage(hpage
);
1329 if (reason
== MR_MEMORY_FAILURE
&& !test_set_page_hwpoison(hpage
))
1330 num_poisoned_pages_inc();
1333 * If migration was not successful and there's a freeing callback, use
1334 * it. Otherwise, put_page() will drop the reference grabbed during
1338 put_new_page(new_hpage
, private);
1340 putback_active_hugepage(new_hpage
);
1346 *result
= page_to_nid(new_hpage
);
1352 * migrate_pages - migrate the pages specified in a list, to the free pages
1353 * supplied as the target for the page migration
1355 * @from: The list of pages to be migrated.
1356 * @get_new_page: The function used to allocate free pages to be used
1357 * as the target of the page migration.
1358 * @put_new_page: The function used to free target pages if migration
1359 * fails, or NULL if no special handling is necessary.
1360 * @private: Private data to be passed on to get_new_page()
1361 * @mode: The migration mode that specifies the constraints for
1362 * page migration, if any.
1363 * @reason: The reason for page migration.
1365 * The function returns after 10 attempts or if no pages are movable any more
1366 * because the list has become empty or no retryable pages exist any more.
1367 * The caller should call putback_movable_pages() to return pages to the LRU
1368 * or free list only if ret != 0.
1370 * Returns the number of pages that were not migrated, or an error code.
1372 int migrate_pages(struct list_head
*from
, new_page_t get_new_page
,
1373 free_page_t put_new_page
, unsigned long private,
1374 enum migrate_mode mode
, int reason
)
1378 int nr_succeeded
= 0;
1382 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
1386 current
->flags
|= PF_SWAPWRITE
;
1388 for(pass
= 0; pass
< 10 && retry
; pass
++) {
1391 list_for_each_entry_safe(page
, page2
, from
, lru
) {
1395 rc
= unmap_and_move_huge_page(get_new_page
,
1396 put_new_page
, private, page
,
1397 pass
> 2, mode
, reason
);
1399 rc
= unmap_and_move(get_new_page
, put_new_page
,
1400 private, page
, pass
> 2, mode
,
1410 case MIGRATEPAGE_SUCCESS
:
1415 * Permanent failure (-EBUSY, -ENOSYS, etc.):
1416 * unlike -EAGAIN case, the failed page is
1417 * removed from migration page list and not
1418 * retried in the next outer loop.
1429 count_vm_events(PGMIGRATE_SUCCESS
, nr_succeeded
);
1431 count_vm_events(PGMIGRATE_FAIL
, nr_failed
);
1432 trace_mm_migrate_pages(nr_succeeded
, nr_failed
, mode
, reason
);
1435 current
->flags
&= ~PF_SWAPWRITE
;
1442 * Move a list of individual pages
1444 struct page_to_node
{
1451 static struct page
*new_page_node(struct page
*p
, unsigned long private,
1454 struct page_to_node
*pm
= (struct page_to_node
*)private;
1456 while (pm
->node
!= MAX_NUMNODES
&& pm
->page
!= p
)
1459 if (pm
->node
== MAX_NUMNODES
)
1462 *result
= &pm
->status
;
1465 return alloc_huge_page_node(page_hstate(compound_head(p
)),
1467 else if (thp_migration_supported() && PageTransHuge(p
)) {
1470 thp
= alloc_pages_node(pm
->node
,
1471 (GFP_TRANSHUGE
| __GFP_THISNODE
) & ~__GFP_RECLAIM
,
1475 prep_transhuge_page(thp
);
1478 return __alloc_pages_node(pm
->node
,
1479 GFP_HIGHUSER_MOVABLE
| __GFP_THISNODE
, 0);
1483 * Move a set of pages as indicated in the pm array. The addr
1484 * field must be set to the virtual address of the page to be moved
1485 * and the node number must contain a valid target node.
1486 * The pm array ends with node = MAX_NUMNODES.
1488 static int do_move_page_to_node_array(struct mm_struct
*mm
,
1489 struct page_to_node
*pm
,
1493 struct page_to_node
*pp
;
1494 LIST_HEAD(pagelist
);
1496 down_read(&mm
->mmap_sem
);
1499 * Build a list of pages to migrate
1501 for (pp
= pm
; pp
->node
!= MAX_NUMNODES
; pp
++) {
1502 struct vm_area_struct
*vma
;
1505 unsigned int follflags
;
1508 vma
= find_vma(mm
, pp
->addr
);
1509 if (!vma
|| pp
->addr
< vma
->vm_start
|| !vma_migratable(vma
))
1512 /* FOLL_DUMP to ignore special (like zero) pages */
1513 follflags
= FOLL_GET
| FOLL_DUMP
;
1514 if (!thp_migration_supported())
1515 follflags
|= FOLL_SPLIT
;
1516 page
= follow_page(vma
, pp
->addr
, follflags
);
1518 err
= PTR_ERR(page
);
1526 err
= page_to_nid(page
);
1528 if (err
== pp
->node
)
1530 * Node already in the right place
1535 if (page_mapcount(page
) > 1 &&
1539 if (PageHuge(page
)) {
1540 if (PageHead(page
)) {
1541 isolate_huge_page(page
, &pagelist
);
1548 pp
->page
= compound_head(page
);
1549 head
= compound_head(page
);
1550 err
= isolate_lru_page(head
);
1552 list_add_tail(&head
->lru
, &pagelist
);
1553 mod_node_page_state(page_pgdat(head
),
1554 NR_ISOLATED_ANON
+ page_is_file_cache(head
),
1555 hpage_nr_pages(head
));
1559 * Either remove the duplicate refcount from
1560 * isolate_lru_page() or drop the page ref if it was
1569 if (!list_empty(&pagelist
)) {
1570 err
= migrate_pages(&pagelist
, new_page_node
, NULL
,
1571 (unsigned long)pm
, MIGRATE_SYNC
, MR_SYSCALL
);
1573 putback_movable_pages(&pagelist
);
1576 up_read(&mm
->mmap_sem
);
1581 * Migrate an array of page address onto an array of nodes and fill
1582 * the corresponding array of status.
1584 static int do_pages_move(struct mm_struct
*mm
, nodemask_t task_nodes
,
1585 unsigned long nr_pages
,
1586 const void __user
* __user
*pages
,
1587 const int __user
*nodes
,
1588 int __user
*status
, int flags
)
1590 struct page_to_node
*pm
;
1591 unsigned long chunk_nr_pages
;
1592 unsigned long chunk_start
;
1596 pm
= (struct page_to_node
*)__get_free_page(GFP_KERNEL
);
1603 * Store a chunk of page_to_node array in a page,
1604 * but keep the last one as a marker
1606 chunk_nr_pages
= (PAGE_SIZE
/ sizeof(struct page_to_node
)) - 1;
1608 for (chunk_start
= 0;
1609 chunk_start
< nr_pages
;
1610 chunk_start
+= chunk_nr_pages
) {
1613 if (chunk_start
+ chunk_nr_pages
> nr_pages
)
1614 chunk_nr_pages
= nr_pages
- chunk_start
;
1616 /* fill the chunk pm with addrs and nodes from user-space */
1617 for (j
= 0; j
< chunk_nr_pages
; j
++) {
1618 const void __user
*p
;
1622 if (get_user(p
, pages
+ j
+ chunk_start
))
1624 pm
[j
].addr
= (unsigned long) p
;
1626 if (get_user(node
, nodes
+ j
+ chunk_start
))
1630 if (node
< 0 || node
>= MAX_NUMNODES
)
1633 if (!node_state(node
, N_MEMORY
))
1637 if (!node_isset(node
, task_nodes
))
1643 /* End marker for this chunk */
1644 pm
[chunk_nr_pages
].node
= MAX_NUMNODES
;
1646 /* Migrate this chunk */
1647 err
= do_move_page_to_node_array(mm
, pm
,
1648 flags
& MPOL_MF_MOVE_ALL
);
1652 /* Return status information */
1653 for (j
= 0; j
< chunk_nr_pages
; j
++)
1654 if (put_user(pm
[j
].status
, status
+ j
+ chunk_start
)) {
1662 free_page((unsigned long)pm
);
1668 * Determine the nodes of an array of pages and store it in an array of status.
1670 static void do_pages_stat_array(struct mm_struct
*mm
, unsigned long nr_pages
,
1671 const void __user
**pages
, int *status
)
1675 down_read(&mm
->mmap_sem
);
1677 for (i
= 0; i
< nr_pages
; i
++) {
1678 unsigned long addr
= (unsigned long)(*pages
);
1679 struct vm_area_struct
*vma
;
1683 vma
= find_vma(mm
, addr
);
1684 if (!vma
|| addr
< vma
->vm_start
)
1687 /* FOLL_DUMP to ignore special (like zero) pages */
1688 page
= follow_page(vma
, addr
, FOLL_DUMP
);
1690 err
= PTR_ERR(page
);
1694 err
= page
? page_to_nid(page
) : -ENOENT
;
1702 up_read(&mm
->mmap_sem
);
1706 * Determine the nodes of a user array of pages and store it in
1707 * a user array of status.
1709 static int do_pages_stat(struct mm_struct
*mm
, unsigned long nr_pages
,
1710 const void __user
* __user
*pages
,
1713 #define DO_PAGES_STAT_CHUNK_NR 16
1714 const void __user
*chunk_pages
[DO_PAGES_STAT_CHUNK_NR
];
1715 int chunk_status
[DO_PAGES_STAT_CHUNK_NR
];
1718 unsigned long chunk_nr
;
1720 chunk_nr
= nr_pages
;
1721 if (chunk_nr
> DO_PAGES_STAT_CHUNK_NR
)
1722 chunk_nr
= DO_PAGES_STAT_CHUNK_NR
;
1724 if (copy_from_user(chunk_pages
, pages
, chunk_nr
* sizeof(*chunk_pages
)))
1727 do_pages_stat_array(mm
, chunk_nr
, chunk_pages
, chunk_status
);
1729 if (copy_to_user(status
, chunk_status
, chunk_nr
* sizeof(*status
)))
1734 nr_pages
-= chunk_nr
;
1736 return nr_pages
? -EFAULT
: 0;
1740 * Move a list of pages in the address space of the currently executing
1743 SYSCALL_DEFINE6(move_pages
, pid_t
, pid
, unsigned long, nr_pages
,
1744 const void __user
* __user
*, pages
,
1745 const int __user
*, nodes
,
1746 int __user
*, status
, int, flags
)
1748 struct task_struct
*task
;
1749 struct mm_struct
*mm
;
1751 nodemask_t task_nodes
;
1754 if (flags
& ~(MPOL_MF_MOVE
|MPOL_MF_MOVE_ALL
))
1757 if ((flags
& MPOL_MF_MOVE_ALL
) && !capable(CAP_SYS_NICE
))
1760 /* Find the mm_struct */
1762 task
= pid
? find_task_by_vpid(pid
) : current
;
1767 get_task_struct(task
);
1770 * Check if this process has the right to modify the specified
1771 * process. Use the regular "ptrace_may_access()" checks.
1773 if (!ptrace_may_access(task
, PTRACE_MODE_READ_REALCREDS
)) {
1780 err
= security_task_movememory(task
);
1784 task_nodes
= cpuset_mems_allowed(task
);
1785 mm
= get_task_mm(task
);
1786 put_task_struct(task
);
1792 err
= do_pages_move(mm
, task_nodes
, nr_pages
, pages
,
1793 nodes
, status
, flags
);
1795 err
= do_pages_stat(mm
, nr_pages
, pages
, status
);
1801 put_task_struct(task
);
1805 #ifdef CONFIG_NUMA_BALANCING
1807 * Returns true if this is a safe migration target node for misplaced NUMA
1808 * pages. Currently it only checks the watermarks which crude
1810 static bool migrate_balanced_pgdat(struct pglist_data
*pgdat
,
1811 unsigned long nr_migrate_pages
)
1815 for (z
= pgdat
->nr_zones
- 1; z
>= 0; z
--) {
1816 struct zone
*zone
= pgdat
->node_zones
+ z
;
1818 if (!populated_zone(zone
))
1821 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1822 if (!zone_watermark_ok(zone
, 0,
1823 high_wmark_pages(zone
) +
1832 static struct page
*alloc_misplaced_dst_page(struct page
*page
,
1836 int nid
= (int) data
;
1837 struct page
*newpage
;
1839 newpage
= __alloc_pages_node(nid
,
1840 (GFP_HIGHUSER_MOVABLE
|
1841 __GFP_THISNODE
| __GFP_NOMEMALLOC
|
1842 __GFP_NORETRY
| __GFP_NOWARN
) &
1849 * page migration rate limiting control.
1850 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1851 * window of time. Default here says do not migrate more than 1280M per second.
1853 static unsigned int migrate_interval_millisecs __read_mostly
= 100;
1854 static unsigned int ratelimit_pages __read_mostly
= 128 << (20 - PAGE_SHIFT
);
1856 /* Returns true if the node is migrate rate-limited after the update */
1857 static bool numamigrate_update_ratelimit(pg_data_t
*pgdat
,
1858 unsigned long nr_pages
)
1861 * Rate-limit the amount of data that is being migrated to a node.
1862 * Optimal placement is no good if the memory bus is saturated and
1863 * all the time is being spent migrating!
1865 if (time_after(jiffies
, pgdat
->numabalancing_migrate_next_window
)) {
1866 spin_lock(&pgdat
->numabalancing_migrate_lock
);
1867 pgdat
->numabalancing_migrate_nr_pages
= 0;
1868 pgdat
->numabalancing_migrate_next_window
= jiffies
+
1869 msecs_to_jiffies(migrate_interval_millisecs
);
1870 spin_unlock(&pgdat
->numabalancing_migrate_lock
);
1872 if (pgdat
->numabalancing_migrate_nr_pages
> ratelimit_pages
) {
1873 trace_mm_numa_migrate_ratelimit(current
, pgdat
->node_id
,
1879 * This is an unlocked non-atomic update so errors are possible.
1880 * The consequences are failing to migrate when we potentiall should
1881 * have which is not severe enough to warrant locking. If it is ever
1882 * a problem, it can be converted to a per-cpu counter.
1884 pgdat
->numabalancing_migrate_nr_pages
+= nr_pages
;
1888 static int numamigrate_isolate_page(pg_data_t
*pgdat
, struct page
*page
)
1892 VM_BUG_ON_PAGE(compound_order(page
) && !PageTransHuge(page
), page
);
1894 /* Avoid migrating to a node that is nearly full */
1895 if (!migrate_balanced_pgdat(pgdat
, 1UL << compound_order(page
)))
1898 if (isolate_lru_page(page
))
1902 * migrate_misplaced_transhuge_page() skips page migration's usual
1903 * check on page_count(), so we must do it here, now that the page
1904 * has been isolated: a GUP pin, or any other pin, prevents migration.
1905 * The expected page count is 3: 1 for page's mapcount and 1 for the
1906 * caller's pin and 1 for the reference taken by isolate_lru_page().
1908 if (PageTransHuge(page
) && page_count(page
) != 3) {
1909 putback_lru_page(page
);
1913 page_lru
= page_is_file_cache(page
);
1914 mod_node_page_state(page_pgdat(page
), NR_ISOLATED_ANON
+ page_lru
,
1915 hpage_nr_pages(page
));
1918 * Isolating the page has taken another reference, so the
1919 * caller's reference can be safely dropped without the page
1920 * disappearing underneath us during migration.
1926 bool pmd_trans_migrating(pmd_t pmd
)
1928 struct page
*page
= pmd_page(pmd
);
1929 return PageLocked(page
);
1933 * Attempt to migrate a misplaced page to the specified destination
1934 * node. Caller is expected to have an elevated reference count on
1935 * the page that will be dropped by this function before returning.
1937 int migrate_misplaced_page(struct page
*page
, struct vm_area_struct
*vma
,
1940 pg_data_t
*pgdat
= NODE_DATA(node
);
1943 LIST_HEAD(migratepages
);
1946 * Don't migrate file pages that are mapped in multiple processes
1947 * with execute permissions as they are probably shared libraries.
1949 if (page_mapcount(page
) != 1 && page_is_file_cache(page
) &&
1950 (vma
->vm_flags
& VM_EXEC
))
1954 * Rate-limit the amount of data that is being migrated to a node.
1955 * Optimal placement is no good if the memory bus is saturated and
1956 * all the time is being spent migrating!
1958 if (numamigrate_update_ratelimit(pgdat
, 1))
1961 isolated
= numamigrate_isolate_page(pgdat
, page
);
1965 list_add(&page
->lru
, &migratepages
);
1966 nr_remaining
= migrate_pages(&migratepages
, alloc_misplaced_dst_page
,
1967 NULL
, node
, MIGRATE_ASYNC
,
1970 if (!list_empty(&migratepages
)) {
1971 list_del(&page
->lru
);
1972 dec_node_page_state(page
, NR_ISOLATED_ANON
+
1973 page_is_file_cache(page
));
1974 putback_lru_page(page
);
1978 count_vm_numa_event(NUMA_PAGE_MIGRATE
);
1979 BUG_ON(!list_empty(&migratepages
));
1986 #endif /* CONFIG_NUMA_BALANCING */
1988 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1990 * Migrates a THP to a given target node. page must be locked and is unlocked
1993 int migrate_misplaced_transhuge_page(struct mm_struct
*mm
,
1994 struct vm_area_struct
*vma
,
1995 pmd_t
*pmd
, pmd_t entry
,
1996 unsigned long address
,
1997 struct page
*page
, int node
)
2000 pg_data_t
*pgdat
= NODE_DATA(node
);
2002 struct page
*new_page
= NULL
;
2003 int page_lru
= page_is_file_cache(page
);
2004 unsigned long mmun_start
= address
& HPAGE_PMD_MASK
;
2005 unsigned long mmun_end
= mmun_start
+ HPAGE_PMD_SIZE
;
2008 * Rate-limit the amount of data that is being migrated to a node.
2009 * Optimal placement is no good if the memory bus is saturated and
2010 * all the time is being spent migrating!
2012 if (numamigrate_update_ratelimit(pgdat
, HPAGE_PMD_NR
))
2015 new_page
= alloc_pages_node(node
,
2016 (GFP_TRANSHUGE_LIGHT
| __GFP_THISNODE
),
2020 prep_transhuge_page(new_page
);
2022 isolated
= numamigrate_isolate_page(pgdat
, page
);
2028 /* Prepare a page as a migration target */
2029 __SetPageLocked(new_page
);
2030 if (PageSwapBacked(page
))
2031 __SetPageSwapBacked(new_page
);
2033 /* anon mapping, we can simply copy page->mapping to the new page: */
2034 new_page
->mapping
= page
->mapping
;
2035 new_page
->index
= page
->index
;
2036 migrate_page_copy(new_page
, page
);
2037 WARN_ON(PageLRU(new_page
));
2039 /* Recheck the target PMD */
2040 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2041 ptl
= pmd_lock(mm
, pmd
);
2042 if (unlikely(!pmd_same(*pmd
, entry
) || !page_ref_freeze(page
, 2))) {
2044 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2046 /* Reverse changes made by migrate_page_copy() */
2047 if (TestClearPageActive(new_page
))
2048 SetPageActive(page
);
2049 if (TestClearPageUnevictable(new_page
))
2050 SetPageUnevictable(page
);
2052 unlock_page(new_page
);
2053 put_page(new_page
); /* Free it */
2055 /* Retake the callers reference and putback on LRU */
2057 putback_lru_page(page
);
2058 mod_node_page_state(page_pgdat(page
),
2059 NR_ISOLATED_ANON
+ page_lru
, -HPAGE_PMD_NR
);
2064 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2065 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
2068 * Clear the old entry under pagetable lock and establish the new PTE.
2069 * Any parallel GUP will either observe the old page blocking on the
2070 * page lock, block on the page table lock or observe the new page.
2071 * The SetPageUptodate on the new page and page_add_new_anon_rmap
2072 * guarantee the copy is visible before the pagetable update.
2074 flush_cache_range(vma
, mmun_start
, mmun_end
);
2075 page_add_anon_rmap(new_page
, vma
, mmun_start
, true);
2076 pmdp_huge_clear_flush_notify(vma
, mmun_start
, pmd
);
2077 set_pmd_at(mm
, mmun_start
, pmd
, entry
);
2078 update_mmu_cache_pmd(vma
, address
, &entry
);
2080 page_ref_unfreeze(page
, 2);
2081 mlock_migrate_page(new_page
, page
);
2082 page_remove_rmap(page
, true);
2083 set_page_owner_migrate_reason(new_page
, MR_NUMA_MISPLACED
);
2086 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2088 /* Take an "isolate" reference and put new page on the LRU. */
2090 putback_lru_page(new_page
);
2092 unlock_page(new_page
);
2094 put_page(page
); /* Drop the rmap reference */
2095 put_page(page
); /* Drop the LRU isolation reference */
2097 count_vm_events(PGMIGRATE_SUCCESS
, HPAGE_PMD_NR
);
2098 count_vm_numa_events(NUMA_PAGE_MIGRATE
, HPAGE_PMD_NR
);
2100 mod_node_page_state(page_pgdat(page
),
2101 NR_ISOLATED_ANON
+ page_lru
,
2106 count_vm_events(PGMIGRATE_FAIL
, HPAGE_PMD_NR
);
2108 ptl
= pmd_lock(mm
, pmd
);
2109 if (pmd_same(*pmd
, entry
)) {
2110 entry
= pmd_modify(entry
, vma
->vm_page_prot
);
2111 set_pmd_at(mm
, mmun_start
, pmd
, entry
);
2112 update_mmu_cache_pmd(vma
, address
, &entry
);
2121 #endif /* CONFIG_NUMA_BALANCING */
2123 #endif /* CONFIG_NUMA */
2126 struct migrate_vma
{
2127 struct vm_area_struct
*vma
;
2130 unsigned long cpages
;
2131 unsigned long npages
;
2132 unsigned long start
;
2136 static int migrate_vma_collect_hole(unsigned long start
,
2138 struct mm_walk
*walk
)
2140 struct migrate_vma
*migrate
= walk
->private;
2143 for (addr
= start
& PAGE_MASK
; addr
< end
; addr
+= PAGE_SIZE
) {
2144 migrate
->dst
[migrate
->npages
] = 0;
2145 migrate
->src
[migrate
->npages
++] = 0;
2151 static int migrate_vma_collect_pmd(pmd_t
*pmdp
,
2152 unsigned long start
,
2154 struct mm_walk
*walk
)
2156 struct migrate_vma
*migrate
= walk
->private;
2157 struct vm_area_struct
*vma
= walk
->vma
;
2158 struct mm_struct
*mm
= vma
->vm_mm
;
2159 unsigned long addr
= start
, unmapped
= 0;
2164 if (pmd_none(*pmdp
))
2165 return migrate_vma_collect_hole(start
, end
, walk
);
2167 if (pmd_trans_huge(*pmdp
)) {
2170 ptl
= pmd_lock(mm
, pmdp
);
2171 if (unlikely(!pmd_trans_huge(*pmdp
))) {
2176 page
= pmd_page(*pmdp
);
2177 if (is_huge_zero_page(page
)) {
2179 split_huge_pmd(vma
, pmdp
, addr
);
2180 if (pmd_trans_unstable(pmdp
))
2181 return migrate_vma_collect_hole(start
, end
,
2188 if (unlikely(!trylock_page(page
)))
2189 return migrate_vma_collect_hole(start
, end
,
2191 ret
= split_huge_page(page
);
2194 if (ret
|| pmd_none(*pmdp
))
2195 return migrate_vma_collect_hole(start
, end
,
2200 if (unlikely(pmd_bad(*pmdp
)))
2201 return migrate_vma_collect_hole(start
, end
, walk
);
2203 ptep
= pte_offset_map_lock(mm
, pmdp
, addr
, &ptl
);
2204 arch_enter_lazy_mmu_mode();
2206 for (; addr
< end
; addr
+= PAGE_SIZE
, ptep
++) {
2207 unsigned long mpfn
, pfn
;
2215 if (pte_none(pte
)) {
2220 if (!pte_present(pte
)) {
2224 * Only care about unaddressable device page special
2225 * page table entry. Other special swap entries are not
2226 * migratable, and we ignore regular swapped page.
2228 entry
= pte_to_swp_entry(pte
);
2229 if (!is_device_private_entry(entry
))
2232 page
= device_private_entry_to_page(entry
);
2233 mpfn
= migrate_pfn(page_to_pfn(page
))|
2234 MIGRATE_PFN_DEVICE
| MIGRATE_PFN_MIGRATE
;
2235 if (is_write_device_private_entry(entry
))
2236 mpfn
|= MIGRATE_PFN_WRITE
;
2238 page
= vm_normal_page(migrate
->vma
, addr
, pte
);
2239 mpfn
= migrate_pfn(pfn
) | MIGRATE_PFN_MIGRATE
;
2240 mpfn
|= pte_write(pte
) ? MIGRATE_PFN_WRITE
: 0;
2243 /* FIXME support THP */
2244 if (!page
|| !page
->mapping
|| PageTransCompound(page
)) {
2248 pfn
= page_to_pfn(page
);
2251 * By getting a reference on the page we pin it and that blocks
2252 * any kind of migration. Side effect is that it "freezes" the
2255 * We drop this reference after isolating the page from the lru
2256 * for non device page (device page are not on the lru and thus
2257 * can't be dropped from it).
2263 * Optimize for the common case where page is only mapped once
2264 * in one process. If we can lock the page, then we can safely
2265 * set up a special migration page table entry now.
2267 if (trylock_page(page
)) {
2270 mpfn
|= MIGRATE_PFN_LOCKED
;
2271 ptep_get_and_clear(mm
, addr
, ptep
);
2273 /* Setup special migration page table entry */
2274 entry
= make_migration_entry(page
, pte_write(pte
));
2275 swp_pte
= swp_entry_to_pte(entry
);
2276 if (pte_soft_dirty(pte
))
2277 swp_pte
= pte_swp_mksoft_dirty(swp_pte
);
2278 set_pte_at(mm
, addr
, ptep
, swp_pte
);
2281 * This is like regular unmap: we remove the rmap and
2282 * drop page refcount. Page won't be freed, as we took
2283 * a reference just above.
2285 page_remove_rmap(page
, false);
2288 if (pte_present(pte
))
2293 migrate
->dst
[migrate
->npages
] = 0;
2294 migrate
->src
[migrate
->npages
++] = mpfn
;
2296 arch_leave_lazy_mmu_mode();
2297 pte_unmap_unlock(ptep
- 1, ptl
);
2299 /* Only flush the TLB if we actually modified any entries */
2301 flush_tlb_range(walk
->vma
, start
, end
);
2307 * migrate_vma_collect() - collect pages over a range of virtual addresses
2308 * @migrate: migrate struct containing all migration information
2310 * This will walk the CPU page table. For each virtual address backed by a
2311 * valid page, it updates the src array and takes a reference on the page, in
2312 * order to pin the page until we lock it and unmap it.
2314 static void migrate_vma_collect(struct migrate_vma
*migrate
)
2316 struct mm_walk mm_walk
;
2318 mm_walk
.pmd_entry
= migrate_vma_collect_pmd
;
2319 mm_walk
.pte_entry
= NULL
;
2320 mm_walk
.pte_hole
= migrate_vma_collect_hole
;
2321 mm_walk
.hugetlb_entry
= NULL
;
2322 mm_walk
.test_walk
= NULL
;
2323 mm_walk
.vma
= migrate
->vma
;
2324 mm_walk
.mm
= migrate
->vma
->vm_mm
;
2325 mm_walk
.private = migrate
;
2327 mmu_notifier_invalidate_range_start(mm_walk
.mm
,
2330 walk_page_range(migrate
->start
, migrate
->end
, &mm_walk
);
2331 mmu_notifier_invalidate_range_end(mm_walk
.mm
,
2335 migrate
->end
= migrate
->start
+ (migrate
->npages
<< PAGE_SHIFT
);
2339 * migrate_vma_check_page() - check if page is pinned or not
2340 * @page: struct page to check
2342 * Pinned pages cannot be migrated. This is the same test as in
2343 * migrate_page_move_mapping(), except that here we allow migration of a
2346 static bool migrate_vma_check_page(struct page
*page
)
2349 * One extra ref because caller holds an extra reference, either from
2350 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2356 * FIXME support THP (transparent huge page), it is bit more complex to
2357 * check them than regular pages, because they can be mapped with a pmd
2358 * or with a pte (split pte mapping).
2360 if (PageCompound(page
))
2363 /* Page from ZONE_DEVICE have one extra reference */
2364 if (is_zone_device_page(page
)) {
2366 * Private page can never be pin as they have no valid pte and
2367 * GUP will fail for those. Yet if there is a pending migration
2368 * a thread might try to wait on the pte migration entry and
2369 * will bump the page reference count. Sadly there is no way to
2370 * differentiate a regular pin from migration wait. Hence to
2371 * avoid 2 racing thread trying to migrate back to CPU to enter
2372 * infinite loop (one stoping migration because the other is
2373 * waiting on pte migration entry). We always return true here.
2375 * FIXME proper solution is to rework migration_entry_wait() so
2376 * it does not need to take a reference on page.
2378 if (is_device_private_page(page
))
2381 /* Other ZONE_DEVICE memory type are not supported */
2385 if ((page_count(page
) - extra
) > page_mapcount(page
))
2392 * migrate_vma_prepare() - lock pages and isolate them from the lru
2393 * @migrate: migrate struct containing all migration information
2395 * This locks pages that have been collected by migrate_vma_collect(). Once each
2396 * page is locked it is isolated from the lru (for non-device pages). Finally,
2397 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2398 * migrated by concurrent kernel threads.
2400 static void migrate_vma_prepare(struct migrate_vma
*migrate
)
2402 const unsigned long npages
= migrate
->npages
;
2403 const unsigned long start
= migrate
->start
;
2404 unsigned long addr
, i
, restore
= 0;
2405 bool allow_drain
= true;
2409 for (i
= 0; (i
< npages
) && migrate
->cpages
; i
++) {
2410 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2416 if (!(migrate
->src
[i
] & MIGRATE_PFN_LOCKED
)) {
2418 * Because we are migrating several pages there can be
2419 * a deadlock between 2 concurrent migration where each
2420 * are waiting on each other page lock.
2422 * Make migrate_vma() a best effort thing and backoff
2423 * for any page we can not lock right away.
2425 if (!trylock_page(page
)) {
2426 migrate
->src
[i
] = 0;
2432 migrate
->src
[i
] |= MIGRATE_PFN_LOCKED
;
2435 /* ZONE_DEVICE pages are not on LRU */
2436 if (!is_zone_device_page(page
)) {
2437 if (!PageLRU(page
) && allow_drain
) {
2438 /* Drain CPU's pagevec */
2439 lru_add_drain_all();
2440 allow_drain
= false;
2443 if (isolate_lru_page(page
)) {
2445 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2449 migrate
->src
[i
] = 0;
2457 /* Drop the reference we took in collect */
2461 if (!migrate_vma_check_page(page
)) {
2463 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2467 if (!is_zone_device_page(page
)) {
2469 putback_lru_page(page
);
2472 migrate
->src
[i
] = 0;
2476 if (!is_zone_device_page(page
))
2477 putback_lru_page(page
);
2484 for (i
= 0, addr
= start
; i
< npages
&& restore
; i
++, addr
+= PAGE_SIZE
) {
2485 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2487 if (!page
|| (migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2490 remove_migration_pte(page
, migrate
->vma
, addr
, page
);
2492 migrate
->src
[i
] = 0;
2500 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2501 * @migrate: migrate struct containing all migration information
2503 * Replace page mapping (CPU page table pte) with a special migration pte entry
2504 * and check again if it has been pinned. Pinned pages are restored because we
2505 * cannot migrate them.
2507 * This is the last step before we call the device driver callback to allocate
2508 * destination memory and copy contents of original page over to new page.
2510 static void migrate_vma_unmap(struct migrate_vma
*migrate
)
2512 int flags
= TTU_MIGRATION
| TTU_IGNORE_MLOCK
| TTU_IGNORE_ACCESS
;
2513 const unsigned long npages
= migrate
->npages
;
2514 const unsigned long start
= migrate
->start
;
2515 unsigned long addr
, i
, restore
= 0;
2517 for (i
= 0; i
< npages
; i
++) {
2518 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2520 if (!page
|| !(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2523 if (page_mapped(page
)) {
2524 try_to_unmap(page
, flags
);
2525 if (page_mapped(page
))
2529 if (migrate_vma_check_page(page
))
2533 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2538 for (addr
= start
, i
= 0; i
< npages
&& restore
; addr
+= PAGE_SIZE
, i
++) {
2539 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2541 if (!page
|| (migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2544 remove_migration_ptes(page
, page
, false);
2546 migrate
->src
[i
] = 0;
2550 if (is_zone_device_page(page
))
2553 putback_lru_page(page
);
2558 * migrate_vma_pages() - migrate meta-data from src page to dst page
2559 * @migrate: migrate struct containing all migration information
2561 * This migrates struct page meta-data from source struct page to destination
2562 * struct page. This effectively finishes the migration from source page to the
2565 static void migrate_vma_pages(struct migrate_vma
*migrate
)
2567 const unsigned long npages
= migrate
->npages
;
2568 const unsigned long start
= migrate
->start
;
2569 unsigned long addr
, i
;
2571 for (i
= 0, addr
= start
; i
< npages
; addr
+= PAGE_SIZE
, i
++) {
2572 struct page
*newpage
= migrate_pfn_to_page(migrate
->dst
[i
]);
2573 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2574 struct address_space
*mapping
;
2577 if (!page
|| !newpage
)
2579 if (!(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
))
2582 mapping
= page_mapping(page
);
2584 if (is_zone_device_page(newpage
)) {
2585 if (is_device_private_page(newpage
)) {
2587 * For now only support private anonymous when
2588 * migrating to un-addressable device memory.
2591 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2596 * Other types of ZONE_DEVICE page are not
2599 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2604 r
= migrate_page(mapping
, newpage
, page
, MIGRATE_SYNC_NO_COPY
);
2605 if (r
!= MIGRATEPAGE_SUCCESS
)
2606 migrate
->src
[i
] &= ~MIGRATE_PFN_MIGRATE
;
2611 * migrate_vma_finalize() - restore CPU page table entry
2612 * @migrate: migrate struct containing all migration information
2614 * This replaces the special migration pte entry with either a mapping to the
2615 * new page if migration was successful for that page, or to the original page
2618 * This also unlocks the pages and puts them back on the lru, or drops the extra
2619 * refcount, for device pages.
2621 static void migrate_vma_finalize(struct migrate_vma
*migrate
)
2623 const unsigned long npages
= migrate
->npages
;
2626 for (i
= 0; i
< npages
; i
++) {
2627 struct page
*newpage
= migrate_pfn_to_page(migrate
->dst
[i
]);
2628 struct page
*page
= migrate_pfn_to_page(migrate
->src
[i
]);
2632 if (!(migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
) || !newpage
) {
2634 unlock_page(newpage
);
2640 remove_migration_ptes(page
, newpage
, false);
2644 if (is_zone_device_page(page
))
2647 putback_lru_page(page
);
2649 if (newpage
!= page
) {
2650 unlock_page(newpage
);
2651 if (is_zone_device_page(newpage
))
2654 putback_lru_page(newpage
);
2660 * migrate_vma() - migrate a range of memory inside vma
2662 * @ops: migration callback for allocating destination memory and copying
2663 * @vma: virtual memory area containing the range to be migrated
2664 * @start: start address of the range to migrate (inclusive)
2665 * @end: end address of the range to migrate (exclusive)
2666 * @src: array of hmm_pfn_t containing source pfns
2667 * @dst: array of hmm_pfn_t containing destination pfns
2668 * @private: pointer passed back to each of the callback
2669 * Returns: 0 on success, error code otherwise
2671 * This function tries to migrate a range of memory virtual address range, using
2672 * callbacks to allocate and copy memory from source to destination. First it
2673 * collects all the pages backing each virtual address in the range, saving this
2674 * inside the src array. Then it locks those pages and unmaps them. Once the pages
2675 * are locked and unmapped, it checks whether each page is pinned or not. Pages
2676 * that aren't pinned have the MIGRATE_PFN_MIGRATE flag set (by this function)
2677 * in the corresponding src array entry. It then restores any pages that are
2678 * pinned, by remapping and unlocking those pages.
2680 * At this point it calls the alloc_and_copy() callback. For documentation on
2681 * what is expected from that callback, see struct migrate_vma_ops comments in
2682 * include/linux/migrate.h
2684 * After the alloc_and_copy() callback, this function goes over each entry in
2685 * the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2686 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2687 * then the function tries to migrate struct page information from the source
2688 * struct page to the destination struct page. If it fails to migrate the struct
2689 * page information, then it clears the MIGRATE_PFN_MIGRATE flag in the src
2692 * At this point all successfully migrated pages have an entry in the src
2693 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2694 * array entry with MIGRATE_PFN_VALID flag set.
2696 * It then calls the finalize_and_map() callback. See comments for "struct
2697 * migrate_vma_ops", in include/linux/migrate.h for details about
2698 * finalize_and_map() behavior.
2700 * After the finalize_and_map() callback, for successfully migrated pages, this
2701 * function updates the CPU page table to point to new pages, otherwise it
2702 * restores the CPU page table to point to the original source pages.
2704 * Function returns 0 after the above steps, even if no pages were migrated
2705 * (The function only returns an error if any of the arguments are invalid.)
2707 * Both src and dst array must be big enough for (end - start) >> PAGE_SHIFT
2708 * unsigned long entries.
2710 int migrate_vma(const struct migrate_vma_ops
*ops
,
2711 struct vm_area_struct
*vma
,
2712 unsigned long start
,
2718 struct migrate_vma migrate
;
2720 /* Sanity check the arguments */
2723 if (!vma
|| is_vm_hugetlb_page(vma
) || (vma
->vm_flags
& VM_SPECIAL
))
2725 if (start
< vma
->vm_start
|| start
>= vma
->vm_end
)
2727 if (end
<= vma
->vm_start
|| end
> vma
->vm_end
)
2729 if (!ops
|| !src
|| !dst
|| start
>= end
)
2732 memset(src
, 0, sizeof(*src
) * ((end
- start
) >> PAGE_SHIFT
));
2735 migrate
.start
= start
;
2741 /* Collect, and try to unmap source pages */
2742 migrate_vma_collect(&migrate
);
2743 if (!migrate
.cpages
)
2746 /* Lock and isolate page */
2747 migrate_vma_prepare(&migrate
);
2748 if (!migrate
.cpages
)
2752 migrate_vma_unmap(&migrate
);
2753 if (!migrate
.cpages
)
2757 * At this point pages are locked and unmapped, and thus they have
2758 * stable content and can safely be copied to destination memory that
2759 * is allocated by the callback.
2761 * Note that migration can fail in migrate_vma_struct_page() for each
2764 ops
->alloc_and_copy(vma
, src
, dst
, start
, end
, private);
2766 /* This does the real migration of struct page */
2767 migrate_vma_pages(&migrate
);
2769 ops
->finalize_and_map(vma
, src
, dst
, start
, end
, private);
2771 /* Unlock and remap pages */
2772 migrate_vma_finalize(&migrate
);
2776 EXPORT_SYMBOL(migrate_vma
);