2 * Memory Migration functionality - linux/mm/migration.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/memcontrol.h>
34 #include <linux/syscalls.h>
35 #include <linux/hugetlb.h>
36 #include <linux/hugetlb_cgroup.h>
37 #include <linux/gfp.h>
38 #include <linux/balloon_compaction.h>
40 #include <asm/tlbflush.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/migrate.h>
48 * migrate_prep() needs to be called before we start compiling a list of pages
49 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
50 * undesirable, use migrate_prep_local()
52 int migrate_prep(void)
55 * Clear the LRU lists so pages can be isolated.
56 * Note that pages may be moved off the LRU after we have
57 * drained them. Those pages will fail to migrate like other
58 * pages that may be busy.
65 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
66 int migrate_prep_local(void)
74 * Add isolated pages on the list back to the LRU under page lock
75 * to avoid leaking evictable pages back onto unevictable list.
77 void putback_lru_pages(struct list_head
*l
)
82 list_for_each_entry_safe(page
, page2
, l
, lru
) {
84 dec_zone_page_state(page
, NR_ISOLATED_ANON
+
85 page_is_file_cache(page
));
86 putback_lru_page(page
);
91 * Put previously isolated pages back onto the appropriate lists
92 * from where they were once taken off for compaction/migration.
94 * This function shall be used instead of putback_lru_pages(),
95 * whenever the isolated pageset has been built by isolate_migratepages_range()
97 void putback_movable_pages(struct list_head
*l
)
102 list_for_each_entry_safe(page
, page2
, l
, lru
) {
103 if (unlikely(PageHuge(page
))) {
104 putback_active_hugepage(page
);
107 list_del(&page
->lru
);
108 dec_zone_page_state(page
, NR_ISOLATED_ANON
+
109 page_is_file_cache(page
));
110 if (unlikely(isolated_balloon_page(page
)))
111 balloon_page_putback(page
);
113 putback_lru_page(page
);
118 * Restore a potential migration pte to a working pte entry
120 static int remove_migration_pte(struct page
*new, struct vm_area_struct
*vma
,
121 unsigned long addr
, void *old
)
123 struct mm_struct
*mm
= vma
->vm_mm
;
129 if (unlikely(PageHuge(new))) {
130 ptep
= huge_pte_offset(mm
, addr
);
133 ptl
= &mm
->page_table_lock
;
135 pmd
= mm_find_pmd(mm
, addr
);
138 if (pmd_trans_huge(*pmd
))
141 ptep
= pte_offset_map(pmd
, addr
);
144 * Peek to check is_swap_pte() before taking ptlock? No, we
145 * can race mremap's move_ptes(), which skips anon_vma lock.
148 ptl
= pte_lockptr(mm
, pmd
);
153 if (!is_swap_pte(pte
))
156 entry
= pte_to_swp_entry(pte
);
158 if (!is_migration_entry(entry
) ||
159 migration_entry_to_page(entry
) != old
)
163 pte
= pte_mkold(mk_pte(new, vma
->vm_page_prot
));
164 if (pte_swp_soft_dirty(*ptep
))
165 pte
= pte_mksoft_dirty(pte
);
166 if (is_write_migration_entry(entry
))
167 pte
= pte_mkwrite(pte
);
168 #ifdef CONFIG_HUGETLB_PAGE
170 pte
= pte_mkhuge(pte
);
171 pte
= arch_make_huge_pte(pte
, vma
, new, 0);
174 flush_dcache_page(new);
175 set_pte_at(mm
, addr
, ptep
, pte
);
179 hugepage_add_anon_rmap(new, vma
, addr
);
182 } else if (PageAnon(new))
183 page_add_anon_rmap(new, vma
, addr
);
185 page_add_file_rmap(new);
187 /* No need to invalidate - it was non-present before */
188 update_mmu_cache(vma
, addr
, ptep
);
190 pte_unmap_unlock(ptep
, ptl
);
196 * Get rid of all migration entries and replace them by
197 * references to the indicated page.
199 static void remove_migration_ptes(struct page
*old
, struct page
*new)
201 rmap_walk(new, remove_migration_pte
, old
);
205 * Something used the pte of a page under migration. We need to
206 * get to the page and wait until migration is finished.
207 * When we return from this function the fault will be retried.
209 static void __migration_entry_wait(struct mm_struct
*mm
, pte_t
*ptep
,
218 if (!is_swap_pte(pte
))
221 entry
= pte_to_swp_entry(pte
);
222 if (!is_migration_entry(entry
))
225 page
= migration_entry_to_page(entry
);
228 * Once radix-tree replacement of page migration started, page_count
229 * *must* be zero. And, we don't want to call wait_on_page_locked()
230 * against a page without get_page().
231 * So, we use get_page_unless_zero(), here. Even failed, page fault
234 if (!get_page_unless_zero(page
))
236 pte_unmap_unlock(ptep
, ptl
);
237 wait_on_page_locked(page
);
241 pte_unmap_unlock(ptep
, ptl
);
244 void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
245 unsigned long address
)
247 spinlock_t
*ptl
= pte_lockptr(mm
, pmd
);
248 pte_t
*ptep
= pte_offset_map(pmd
, address
);
249 __migration_entry_wait(mm
, ptep
, ptl
);
252 void migration_entry_wait_huge(struct mm_struct
*mm
, pte_t
*pte
)
254 spinlock_t
*ptl
= &(mm
)->page_table_lock
;
255 __migration_entry_wait(mm
, pte
, ptl
);
259 /* Returns true if all buffers are successfully locked */
260 static bool buffer_migrate_lock_buffers(struct buffer_head
*head
,
261 enum migrate_mode mode
)
263 struct buffer_head
*bh
= head
;
265 /* Simple case, sync compaction */
266 if (mode
!= MIGRATE_ASYNC
) {
270 bh
= bh
->b_this_page
;
272 } while (bh
!= head
);
277 /* async case, we cannot block on lock_buffer so use trylock_buffer */
280 if (!trylock_buffer(bh
)) {
282 * We failed to lock the buffer and cannot stall in
283 * async migration. Release the taken locks
285 struct buffer_head
*failed_bh
= bh
;
288 while (bh
!= failed_bh
) {
291 bh
= bh
->b_this_page
;
296 bh
= bh
->b_this_page
;
297 } while (bh
!= head
);
301 static inline bool buffer_migrate_lock_buffers(struct buffer_head
*head
,
302 enum migrate_mode mode
)
306 #endif /* CONFIG_BLOCK */
309 * Replace the page in the mapping.
311 * The number of remaining references must be:
312 * 1 for anonymous pages without a mapping
313 * 2 for pages with a mapping
314 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
316 int migrate_page_move_mapping(struct address_space
*mapping
,
317 struct page
*newpage
, struct page
*page
,
318 struct buffer_head
*head
, enum migrate_mode mode
)
320 int expected_count
= 0;
324 /* Anonymous page without mapping */
325 if (page_count(page
) != 1)
327 return MIGRATEPAGE_SUCCESS
;
330 spin_lock_irq(&mapping
->tree_lock
);
332 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
335 expected_count
= 2 + page_has_private(page
);
336 if (page_count(page
) != expected_count
||
337 radix_tree_deref_slot_protected(pslot
, &mapping
->tree_lock
) != page
) {
338 spin_unlock_irq(&mapping
->tree_lock
);
342 if (!page_freeze_refs(page
, expected_count
)) {
343 spin_unlock_irq(&mapping
->tree_lock
);
348 * In the async migration case of moving a page with buffers, lock the
349 * buffers using trylock before the mapping is moved. If the mapping
350 * was moved, we later failed to lock the buffers and could not move
351 * the mapping back due to an elevated page count, we would have to
352 * block waiting on other references to be dropped.
354 if (mode
== MIGRATE_ASYNC
&& head
&&
355 !buffer_migrate_lock_buffers(head
, mode
)) {
356 page_unfreeze_refs(page
, expected_count
);
357 spin_unlock_irq(&mapping
->tree_lock
);
362 * Now we know that no one else is looking at the page.
364 get_page(newpage
); /* add cache reference */
365 if (PageSwapCache(page
)) {
366 SetPageSwapCache(newpage
);
367 set_page_private(newpage
, page_private(page
));
370 radix_tree_replace_slot(pslot
, newpage
);
373 * Drop cache reference from old page by unfreezing
374 * to one less reference.
375 * We know this isn't the last reference.
377 page_unfreeze_refs(page
, expected_count
- 1);
380 * If moved to a different zone then also account
381 * the page for that zone. Other VM counters will be
382 * taken care of when we establish references to the
383 * new page and drop references to the old page.
385 * Note that anonymous pages are accounted for
386 * via NR_FILE_PAGES and NR_ANON_PAGES if they
387 * are mapped to swap space.
389 __dec_zone_page_state(page
, NR_FILE_PAGES
);
390 __inc_zone_page_state(newpage
, NR_FILE_PAGES
);
391 if (!PageSwapCache(page
) && PageSwapBacked(page
)) {
392 __dec_zone_page_state(page
, NR_SHMEM
);
393 __inc_zone_page_state(newpage
, NR_SHMEM
);
395 spin_unlock_irq(&mapping
->tree_lock
);
397 return MIGRATEPAGE_SUCCESS
;
401 * The expected number of remaining references is the same as that
402 * of migrate_page_move_mapping().
404 int migrate_huge_page_move_mapping(struct address_space
*mapping
,
405 struct page
*newpage
, struct page
*page
)
411 if (page_count(page
) != 1)
413 return MIGRATEPAGE_SUCCESS
;
416 spin_lock_irq(&mapping
->tree_lock
);
418 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
421 expected_count
= 2 + page_has_private(page
);
422 if (page_count(page
) != expected_count
||
423 radix_tree_deref_slot_protected(pslot
, &mapping
->tree_lock
) != page
) {
424 spin_unlock_irq(&mapping
->tree_lock
);
428 if (!page_freeze_refs(page
, expected_count
)) {
429 spin_unlock_irq(&mapping
->tree_lock
);
435 radix_tree_replace_slot(pslot
, newpage
);
437 page_unfreeze_refs(page
, expected_count
- 1);
439 spin_unlock_irq(&mapping
->tree_lock
);
440 return MIGRATEPAGE_SUCCESS
;
444 * Copy the page to its new location
446 void migrate_page_copy(struct page
*newpage
, struct page
*page
)
450 if (PageHuge(page
) || PageTransHuge(page
))
451 copy_huge_page(newpage
, page
);
453 copy_highpage(newpage
, page
);
456 SetPageError(newpage
);
457 if (PageReferenced(page
))
458 SetPageReferenced(newpage
);
459 if (PageUptodate(page
))
460 SetPageUptodate(newpage
);
461 if (TestClearPageActive(page
)) {
462 VM_BUG_ON(PageUnevictable(page
));
463 SetPageActive(newpage
);
464 } else if (TestClearPageUnevictable(page
))
465 SetPageUnevictable(newpage
);
466 if (PageChecked(page
))
467 SetPageChecked(newpage
);
468 if (PageMappedToDisk(page
))
469 SetPageMappedToDisk(newpage
);
471 if (PageDirty(page
)) {
472 clear_page_dirty_for_io(page
);
474 * Want to mark the page and the radix tree as dirty, and
475 * redo the accounting that clear_page_dirty_for_io undid,
476 * but we can't use set_page_dirty because that function
477 * is actually a signal that all of the page has become dirty.
478 * Whereas only part of our page may be dirty.
480 if (PageSwapBacked(page
))
481 SetPageDirty(newpage
);
483 __set_page_dirty_nobuffers(newpage
);
487 * Copy NUMA information to the new page, to prevent over-eager
488 * future migrations of this same page.
490 cpupid
= page_cpupid_xchg_last(page
, -1);
491 page_cpupid_xchg_last(newpage
, cpupid
);
493 mlock_migrate_page(newpage
, page
);
494 ksm_migrate_page(newpage
, page
);
496 * Please do not reorder this without considering how mm/ksm.c's
497 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
499 ClearPageSwapCache(page
);
500 ClearPagePrivate(page
);
501 set_page_private(page
, 0);
504 * If any waiters have accumulated on the new page then
507 if (PageWriteback(newpage
))
508 end_page_writeback(newpage
);
511 /************************************************************
512 * Migration functions
513 ***********************************************************/
515 /* Always fail migration. Used for mappings that are not movable */
516 int fail_migrate_page(struct address_space
*mapping
,
517 struct page
*newpage
, struct page
*page
)
521 EXPORT_SYMBOL(fail_migrate_page
);
524 * Common logic to directly migrate a single page suitable for
525 * pages that do not use PagePrivate/PagePrivate2.
527 * Pages are locked upon entry and exit.
529 int migrate_page(struct address_space
*mapping
,
530 struct page
*newpage
, struct page
*page
,
531 enum migrate_mode mode
)
535 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
537 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, NULL
, mode
);
539 if (rc
!= MIGRATEPAGE_SUCCESS
)
542 migrate_page_copy(newpage
, page
);
543 return MIGRATEPAGE_SUCCESS
;
545 EXPORT_SYMBOL(migrate_page
);
549 * Migration function for pages with buffers. This function can only be used
550 * if the underlying filesystem guarantees that no other references to "page"
553 int buffer_migrate_page(struct address_space
*mapping
,
554 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
556 struct buffer_head
*bh
, *head
;
559 if (!page_has_buffers(page
))
560 return migrate_page(mapping
, newpage
, page
, mode
);
562 head
= page_buffers(page
);
564 rc
= migrate_page_move_mapping(mapping
, newpage
, page
, head
, mode
);
566 if (rc
!= MIGRATEPAGE_SUCCESS
)
570 * In the async case, migrate_page_move_mapping locked the buffers
571 * with an IRQ-safe spinlock held. In the sync case, the buffers
572 * need to be locked now
574 if (mode
!= MIGRATE_ASYNC
)
575 BUG_ON(!buffer_migrate_lock_buffers(head
, mode
));
577 ClearPagePrivate(page
);
578 set_page_private(newpage
, page_private(page
));
579 set_page_private(page
, 0);
585 set_bh_page(bh
, newpage
, bh_offset(bh
));
586 bh
= bh
->b_this_page
;
588 } while (bh
!= head
);
590 SetPagePrivate(newpage
);
592 migrate_page_copy(newpage
, page
);
598 bh
= bh
->b_this_page
;
600 } while (bh
!= head
);
602 return MIGRATEPAGE_SUCCESS
;
604 EXPORT_SYMBOL(buffer_migrate_page
);
608 * Writeback a page to clean the dirty state
610 static int writeout(struct address_space
*mapping
, struct page
*page
)
612 struct writeback_control wbc
= {
613 .sync_mode
= WB_SYNC_NONE
,
616 .range_end
= LLONG_MAX
,
621 if (!mapping
->a_ops
->writepage
)
622 /* No write method for the address space */
625 if (!clear_page_dirty_for_io(page
))
626 /* Someone else already triggered a write */
630 * A dirty page may imply that the underlying filesystem has
631 * the page on some queue. So the page must be clean for
632 * migration. Writeout may mean we loose the lock and the
633 * page state is no longer what we checked for earlier.
634 * At this point we know that the migration attempt cannot
637 remove_migration_ptes(page
, page
);
639 rc
= mapping
->a_ops
->writepage(page
, &wbc
);
641 if (rc
!= AOP_WRITEPAGE_ACTIVATE
)
642 /* unlocked. Relock */
645 return (rc
< 0) ? -EIO
: -EAGAIN
;
649 * Default handling if a filesystem does not provide a migration function.
651 static int fallback_migrate_page(struct address_space
*mapping
,
652 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
654 if (PageDirty(page
)) {
655 /* Only writeback pages in full synchronous migration */
656 if (mode
!= MIGRATE_SYNC
)
658 return writeout(mapping
, page
);
662 * Buffers may be managed in a filesystem specific way.
663 * We must have no buffers or drop them.
665 if (page_has_private(page
) &&
666 !try_to_release_page(page
, GFP_KERNEL
))
669 return migrate_page(mapping
, newpage
, page
, mode
);
673 * Move a page to a newly allocated page
674 * The page is locked and all ptes have been successfully removed.
676 * The new page will have replaced the old page if this function
681 * MIGRATEPAGE_SUCCESS - success
683 static int move_to_new_page(struct page
*newpage
, struct page
*page
,
684 int remap_swapcache
, enum migrate_mode mode
)
686 struct address_space
*mapping
;
690 * Block others from accessing the page when we get around to
691 * establishing additional references. We are the only one
692 * holding a reference to the new page at this point.
694 if (!trylock_page(newpage
))
697 /* Prepare mapping for the new page.*/
698 newpage
->index
= page
->index
;
699 newpage
->mapping
= page
->mapping
;
700 if (PageSwapBacked(page
))
701 SetPageSwapBacked(newpage
);
703 mapping
= page_mapping(page
);
705 rc
= migrate_page(mapping
, newpage
, page
, mode
);
706 else if (mapping
->a_ops
->migratepage
)
708 * Most pages have a mapping and most filesystems provide a
709 * migratepage callback. Anonymous pages are part of swap
710 * space which also has its own migratepage callback. This
711 * is the most common path for page migration.
713 rc
= mapping
->a_ops
->migratepage(mapping
,
714 newpage
, page
, mode
);
716 rc
= fallback_migrate_page(mapping
, newpage
, page
, mode
);
718 if (rc
!= MIGRATEPAGE_SUCCESS
) {
719 newpage
->mapping
= NULL
;
722 remove_migration_ptes(page
, newpage
);
723 page
->mapping
= NULL
;
726 unlock_page(newpage
);
731 static int __unmap_and_move(struct page
*page
, struct page
*newpage
,
732 int force
, enum migrate_mode mode
)
735 int remap_swapcache
= 1;
736 struct mem_cgroup
*mem
;
737 struct anon_vma
*anon_vma
= NULL
;
739 if (!trylock_page(page
)) {
740 if (!force
|| mode
== MIGRATE_ASYNC
)
744 * It's not safe for direct compaction to call lock_page.
745 * For example, during page readahead pages are added locked
746 * to the LRU. Later, when the IO completes the pages are
747 * marked uptodate and unlocked. However, the queueing
748 * could be merging multiple pages for one bio (e.g.
749 * mpage_readpages). If an allocation happens for the
750 * second or third page, the process can end up locking
751 * the same page twice and deadlocking. Rather than
752 * trying to be clever about what pages can be locked,
753 * avoid the use of lock_page for direct compaction
756 if (current
->flags
& PF_MEMALLOC
)
762 /* charge against new page */
763 mem_cgroup_prepare_migration(page
, newpage
, &mem
);
765 if (PageWriteback(page
)) {
767 * Only in the case of a full synchronous migration is it
768 * necessary to wait for PageWriteback. In the async case,
769 * the retry loop is too short and in the sync-light case,
770 * the overhead of stalling is too much
772 if (mode
!= MIGRATE_SYNC
) {
778 wait_on_page_writeback(page
);
781 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
782 * we cannot notice that anon_vma is freed while we migrates a page.
783 * This get_anon_vma() delays freeing anon_vma pointer until the end
784 * of migration. File cache pages are no problem because of page_lock()
785 * File Caches may use write_page() or lock_page() in migration, then,
786 * just care Anon page here.
788 if (PageAnon(page
) && !PageKsm(page
)) {
790 * Only page_lock_anon_vma_read() understands the subtleties of
791 * getting a hold on an anon_vma from outside one of its mms.
793 anon_vma
= page_get_anon_vma(page
);
798 } else if (PageSwapCache(page
)) {
800 * We cannot be sure that the anon_vma of an unmapped
801 * swapcache page is safe to use because we don't
802 * know in advance if the VMA that this page belonged
803 * to still exists. If the VMA and others sharing the
804 * data have been freed, then the anon_vma could
805 * already be invalid.
807 * To avoid this possibility, swapcache pages get
808 * migrated but are not remapped when migration
817 if (unlikely(balloon_page_movable(page
))) {
819 * A ballooned page does not need any special attention from
820 * physical to virtual reverse mapping procedures.
821 * Skip any attempt to unmap PTEs or to remap swap cache,
822 * in order to avoid burning cycles at rmap level, and perform
823 * the page migration right away (proteced by page lock).
825 rc
= balloon_page_migrate(newpage
, page
, mode
);
830 * Corner case handling:
831 * 1. When a new swap-cache page is read into, it is added to the LRU
832 * and treated as swapcache but it has no rmap yet.
833 * Calling try_to_unmap() against a page->mapping==NULL page will
834 * trigger a BUG. So handle it here.
835 * 2. An orphaned page (see truncate_complete_page) might have
836 * fs-private metadata. The page can be picked up due to memory
837 * offlining. Everywhere else except page reclaim, the page is
838 * invisible to the vm, so the page can not be migrated. So try to
839 * free the metadata, so the page can be freed.
841 if (!page
->mapping
) {
842 VM_BUG_ON(PageAnon(page
));
843 if (page_has_private(page
)) {
844 try_to_free_buffers(page
);
850 /* Establish migration ptes or remove ptes */
851 try_to_unmap(page
, TTU_MIGRATION
|TTU_IGNORE_MLOCK
|TTU_IGNORE_ACCESS
);
854 if (!page_mapped(page
))
855 rc
= move_to_new_page(newpage
, page
, remap_swapcache
, mode
);
857 if (rc
&& remap_swapcache
)
858 remove_migration_ptes(page
, page
);
860 /* Drop an anon_vma reference if we took one */
862 put_anon_vma(anon_vma
);
865 mem_cgroup_end_migration(mem
, page
, newpage
,
866 (rc
== MIGRATEPAGE_SUCCESS
||
867 rc
== MIGRATEPAGE_BALLOON_SUCCESS
));
874 * Obtain the lock on page, remove all ptes and migrate the page
875 * to the newly allocated page in newpage.
877 static int unmap_and_move(new_page_t get_new_page
, unsigned long private,
878 struct page
*page
, int force
, enum migrate_mode mode
)
882 struct page
*newpage
= get_new_page(page
, private, &result
);
887 if (page_count(page
) == 1) {
888 /* page was freed from under us. So we are done. */
892 if (unlikely(PageTransHuge(page
)))
893 if (unlikely(split_huge_page(page
)))
896 rc
= __unmap_and_move(page
, newpage
, force
, mode
);
898 if (unlikely(rc
== MIGRATEPAGE_BALLOON_SUCCESS
)) {
900 * A ballooned page has been migrated already.
901 * Now, it's the time to wrap-up counters,
902 * handle the page back to Buddy and return.
904 dec_zone_page_state(page
, NR_ISOLATED_ANON
+
905 page_is_file_cache(page
));
906 balloon_page_free(page
);
907 return MIGRATEPAGE_SUCCESS
;
912 * A page that has been migrated has all references
913 * removed and will be freed. A page that has not been
914 * migrated will have kepts its references and be
917 list_del(&page
->lru
);
918 dec_zone_page_state(page
, NR_ISOLATED_ANON
+
919 page_is_file_cache(page
));
920 putback_lru_page(page
);
923 * Move the new page to the LRU. If migration was not successful
924 * then this will free the page.
926 putback_lru_page(newpage
);
931 *result
= page_to_nid(newpage
);
937 * Counterpart of unmap_and_move_page() for hugepage migration.
939 * This function doesn't wait the completion of hugepage I/O
940 * because there is no race between I/O and migration for hugepage.
941 * Note that currently hugepage I/O occurs only in direct I/O
942 * where no lock is held and PG_writeback is irrelevant,
943 * and writeback status of all subpages are counted in the reference
944 * count of the head page (i.e. if all subpages of a 2MB hugepage are
945 * under direct I/O, the reference of the head page is 512 and a bit more.)
946 * This means that when we try to migrate hugepage whose subpages are
947 * doing direct I/O, some references remain after try_to_unmap() and
948 * hugepage migration fails without data corruption.
950 * There is also no race when direct I/O is issued on the page under migration,
951 * because then pte is replaced with migration swap entry and direct I/O code
952 * will wait in the page fault for migration to complete.
954 static int unmap_and_move_huge_page(new_page_t get_new_page
,
955 unsigned long private, struct page
*hpage
,
956 int force
, enum migrate_mode mode
)
960 struct page
*new_hpage
= get_new_page(hpage
, private, &result
);
961 struct anon_vma
*anon_vma
= NULL
;
964 * Movability of hugepages depends on architectures and hugepage size.
965 * This check is necessary because some callers of hugepage migration
966 * like soft offline and memory hotremove don't walk through page
967 * tables or check whether the hugepage is pmd-based or not before
970 if (!hugepage_migration_support(page_hstate(hpage
)))
978 if (!trylock_page(hpage
)) {
979 if (!force
|| mode
!= MIGRATE_SYNC
)
985 anon_vma
= page_get_anon_vma(hpage
);
987 try_to_unmap(hpage
, TTU_MIGRATION
|TTU_IGNORE_MLOCK
|TTU_IGNORE_ACCESS
);
989 if (!page_mapped(hpage
))
990 rc
= move_to_new_page(new_hpage
, hpage
, 1, mode
);
993 remove_migration_ptes(hpage
, hpage
);
996 put_anon_vma(anon_vma
);
999 hugetlb_cgroup_migrate(hpage
, new_hpage
);
1004 putback_active_hugepage(hpage
);
1005 put_page(new_hpage
);
1010 *result
= page_to_nid(new_hpage
);
1016 * migrate_pages - migrate the pages specified in a list, to the free pages
1017 * supplied as the target for the page migration
1019 * @from: The list of pages to be migrated.
1020 * @get_new_page: The function used to allocate free pages to be used
1021 * as the target of the page migration.
1022 * @private: Private data to be passed on to get_new_page()
1023 * @mode: The migration mode that specifies the constraints for
1024 * page migration, if any.
1025 * @reason: The reason for page migration.
1027 * The function returns after 10 attempts or if no pages are movable any more
1028 * because the list has become empty or no retryable pages exist any more.
1029 * The caller should call putback_lru_pages() to return pages to the LRU
1030 * or free list only if ret != 0.
1032 * Returns the number of pages that were not migrated, or an error code.
1034 int migrate_pages(struct list_head
*from
, new_page_t get_new_page
,
1035 unsigned long private, enum migrate_mode mode
, int reason
)
1039 int nr_succeeded
= 0;
1043 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
1047 current
->flags
|= PF_SWAPWRITE
;
1049 for(pass
= 0; pass
< 10 && retry
; pass
++) {
1052 list_for_each_entry_safe(page
, page2
, from
, lru
) {
1056 rc
= unmap_and_move_huge_page(get_new_page
,
1057 private, page
, pass
> 2, mode
);
1059 rc
= unmap_and_move(get_new_page
, private,
1060 page
, pass
> 2, mode
);
1068 case MIGRATEPAGE_SUCCESS
:
1072 /* Permanent failure */
1078 rc
= nr_failed
+ retry
;
1081 count_vm_events(PGMIGRATE_SUCCESS
, nr_succeeded
);
1083 count_vm_events(PGMIGRATE_FAIL
, nr_failed
);
1084 trace_mm_migrate_pages(nr_succeeded
, nr_failed
, mode
, reason
);
1087 current
->flags
&= ~PF_SWAPWRITE
;
1094 * Move a list of individual pages
1096 struct page_to_node
{
1103 static struct page
*new_page_node(struct page
*p
, unsigned long private,
1106 struct page_to_node
*pm
= (struct page_to_node
*)private;
1108 while (pm
->node
!= MAX_NUMNODES
&& pm
->page
!= p
)
1111 if (pm
->node
== MAX_NUMNODES
)
1114 *result
= &pm
->status
;
1117 return alloc_huge_page_node(page_hstate(compound_head(p
)),
1120 return alloc_pages_exact_node(pm
->node
,
1121 GFP_HIGHUSER_MOVABLE
| GFP_THISNODE
, 0);
1125 * Move a set of pages as indicated in the pm array. The addr
1126 * field must be set to the virtual address of the page to be moved
1127 * and the node number must contain a valid target node.
1128 * The pm array ends with node = MAX_NUMNODES.
1130 static int do_move_page_to_node_array(struct mm_struct
*mm
,
1131 struct page_to_node
*pm
,
1135 struct page_to_node
*pp
;
1136 LIST_HEAD(pagelist
);
1138 down_read(&mm
->mmap_sem
);
1141 * Build a list of pages to migrate
1143 for (pp
= pm
; pp
->node
!= MAX_NUMNODES
; pp
++) {
1144 struct vm_area_struct
*vma
;
1148 vma
= find_vma(mm
, pp
->addr
);
1149 if (!vma
|| pp
->addr
< vma
->vm_start
|| !vma_migratable(vma
))
1152 page
= follow_page(vma
, pp
->addr
, FOLL_GET
|FOLL_SPLIT
);
1154 err
= PTR_ERR(page
);
1162 /* Use PageReserved to check for zero page */
1163 if (PageReserved(page
))
1167 err
= page_to_nid(page
);
1169 if (err
== pp
->node
)
1171 * Node already in the right place
1176 if (page_mapcount(page
) > 1 &&
1180 if (PageHuge(page
)) {
1181 isolate_huge_page(page
, &pagelist
);
1185 err
= isolate_lru_page(page
);
1187 list_add_tail(&page
->lru
, &pagelist
);
1188 inc_zone_page_state(page
, NR_ISOLATED_ANON
+
1189 page_is_file_cache(page
));
1193 * Either remove the duplicate refcount from
1194 * isolate_lru_page() or drop the page ref if it was
1203 if (!list_empty(&pagelist
)) {
1204 err
= migrate_pages(&pagelist
, new_page_node
,
1205 (unsigned long)pm
, MIGRATE_SYNC
, MR_SYSCALL
);
1207 putback_movable_pages(&pagelist
);
1210 up_read(&mm
->mmap_sem
);
1215 * Migrate an array of page address onto an array of nodes and fill
1216 * the corresponding array of status.
1218 static int do_pages_move(struct mm_struct
*mm
, nodemask_t task_nodes
,
1219 unsigned long nr_pages
,
1220 const void __user
* __user
*pages
,
1221 const int __user
*nodes
,
1222 int __user
*status
, int flags
)
1224 struct page_to_node
*pm
;
1225 unsigned long chunk_nr_pages
;
1226 unsigned long chunk_start
;
1230 pm
= (struct page_to_node
*)__get_free_page(GFP_KERNEL
);
1237 * Store a chunk of page_to_node array in a page,
1238 * but keep the last one as a marker
1240 chunk_nr_pages
= (PAGE_SIZE
/ sizeof(struct page_to_node
)) - 1;
1242 for (chunk_start
= 0;
1243 chunk_start
< nr_pages
;
1244 chunk_start
+= chunk_nr_pages
) {
1247 if (chunk_start
+ chunk_nr_pages
> nr_pages
)
1248 chunk_nr_pages
= nr_pages
- chunk_start
;
1250 /* fill the chunk pm with addrs and nodes from user-space */
1251 for (j
= 0; j
< chunk_nr_pages
; j
++) {
1252 const void __user
*p
;
1256 if (get_user(p
, pages
+ j
+ chunk_start
))
1258 pm
[j
].addr
= (unsigned long) p
;
1260 if (get_user(node
, nodes
+ j
+ chunk_start
))
1264 if (node
< 0 || node
>= MAX_NUMNODES
)
1267 if (!node_state(node
, N_MEMORY
))
1271 if (!node_isset(node
, task_nodes
))
1277 /* End marker for this chunk */
1278 pm
[chunk_nr_pages
].node
= MAX_NUMNODES
;
1280 /* Migrate this chunk */
1281 err
= do_move_page_to_node_array(mm
, pm
,
1282 flags
& MPOL_MF_MOVE_ALL
);
1286 /* Return status information */
1287 for (j
= 0; j
< chunk_nr_pages
; j
++)
1288 if (put_user(pm
[j
].status
, status
+ j
+ chunk_start
)) {
1296 free_page((unsigned long)pm
);
1302 * Determine the nodes of an array of pages and store it in an array of status.
1304 static void do_pages_stat_array(struct mm_struct
*mm
, unsigned long nr_pages
,
1305 const void __user
**pages
, int *status
)
1309 down_read(&mm
->mmap_sem
);
1311 for (i
= 0; i
< nr_pages
; i
++) {
1312 unsigned long addr
= (unsigned long)(*pages
);
1313 struct vm_area_struct
*vma
;
1317 vma
= find_vma(mm
, addr
);
1318 if (!vma
|| addr
< vma
->vm_start
)
1321 page
= follow_page(vma
, addr
, 0);
1323 err
= PTR_ERR(page
);
1328 /* Use PageReserved to check for zero page */
1329 if (!page
|| PageReserved(page
))
1332 err
= page_to_nid(page
);
1340 up_read(&mm
->mmap_sem
);
1344 * Determine the nodes of a user array of pages and store it in
1345 * a user array of status.
1347 static int do_pages_stat(struct mm_struct
*mm
, unsigned long nr_pages
,
1348 const void __user
* __user
*pages
,
1351 #define DO_PAGES_STAT_CHUNK_NR 16
1352 const void __user
*chunk_pages
[DO_PAGES_STAT_CHUNK_NR
];
1353 int chunk_status
[DO_PAGES_STAT_CHUNK_NR
];
1356 unsigned long chunk_nr
;
1358 chunk_nr
= nr_pages
;
1359 if (chunk_nr
> DO_PAGES_STAT_CHUNK_NR
)
1360 chunk_nr
= DO_PAGES_STAT_CHUNK_NR
;
1362 if (copy_from_user(chunk_pages
, pages
, chunk_nr
* sizeof(*chunk_pages
)))
1365 do_pages_stat_array(mm
, chunk_nr
, chunk_pages
, chunk_status
);
1367 if (copy_to_user(status
, chunk_status
, chunk_nr
* sizeof(*status
)))
1372 nr_pages
-= chunk_nr
;
1374 return nr_pages
? -EFAULT
: 0;
1378 * Move a list of pages in the address space of the currently executing
1381 SYSCALL_DEFINE6(move_pages
, pid_t
, pid
, unsigned long, nr_pages
,
1382 const void __user
* __user
*, pages
,
1383 const int __user
*, nodes
,
1384 int __user
*, status
, int, flags
)
1386 const struct cred
*cred
= current_cred(), *tcred
;
1387 struct task_struct
*task
;
1388 struct mm_struct
*mm
;
1390 nodemask_t task_nodes
;
1393 if (flags
& ~(MPOL_MF_MOVE
|MPOL_MF_MOVE_ALL
))
1396 if ((flags
& MPOL_MF_MOVE_ALL
) && !capable(CAP_SYS_NICE
))
1399 /* Find the mm_struct */
1401 task
= pid
? find_task_by_vpid(pid
) : current
;
1406 get_task_struct(task
);
1409 * Check if this process has the right to modify the specified
1410 * process. The right exists if the process has administrative
1411 * capabilities, superuser privileges or the same
1412 * userid as the target process.
1414 tcred
= __task_cred(task
);
1415 if (!uid_eq(cred
->euid
, tcred
->suid
) && !uid_eq(cred
->euid
, tcred
->uid
) &&
1416 !uid_eq(cred
->uid
, tcred
->suid
) && !uid_eq(cred
->uid
, tcred
->uid
) &&
1417 !capable(CAP_SYS_NICE
)) {
1424 err
= security_task_movememory(task
);
1428 task_nodes
= cpuset_mems_allowed(task
);
1429 mm
= get_task_mm(task
);
1430 put_task_struct(task
);
1436 err
= do_pages_move(mm
, task_nodes
, nr_pages
, pages
,
1437 nodes
, status
, flags
);
1439 err
= do_pages_stat(mm
, nr_pages
, pages
, status
);
1445 put_task_struct(task
);
1450 * Call migration functions in the vma_ops that may prepare
1451 * memory in a vm for migration. migration functions may perform
1452 * the migration for vmas that do not have an underlying page struct.
1454 int migrate_vmas(struct mm_struct
*mm
, const nodemask_t
*to
,
1455 const nodemask_t
*from
, unsigned long flags
)
1457 struct vm_area_struct
*vma
;
1460 for (vma
= mm
->mmap
; vma
&& !err
; vma
= vma
->vm_next
) {
1461 if (vma
->vm_ops
&& vma
->vm_ops
->migrate
) {
1462 err
= vma
->vm_ops
->migrate(vma
, to
, from
, flags
);
1470 #ifdef CONFIG_NUMA_BALANCING
1472 * Returns true if this is a safe migration target node for misplaced NUMA
1473 * pages. Currently it only checks the watermarks which crude
1475 static bool migrate_balanced_pgdat(struct pglist_data
*pgdat
,
1476 unsigned long nr_migrate_pages
)
1479 for (z
= pgdat
->nr_zones
- 1; z
>= 0; z
--) {
1480 struct zone
*zone
= pgdat
->node_zones
+ z
;
1482 if (!populated_zone(zone
))
1485 if (!zone_reclaimable(zone
))
1488 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1489 if (!zone_watermark_ok(zone
, 0,
1490 high_wmark_pages(zone
) +
1499 static struct page
*alloc_misplaced_dst_page(struct page
*page
,
1503 int nid
= (int) data
;
1504 struct page
*newpage
;
1506 newpage
= alloc_pages_exact_node(nid
,
1507 (GFP_HIGHUSER_MOVABLE
| GFP_THISNODE
|
1508 __GFP_NOMEMALLOC
| __GFP_NORETRY
|
1512 page_cpupid_xchg_last(newpage
, page_cpupid_last(page
));
1518 * page migration rate limiting control.
1519 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1520 * window of time. Default here says do not migrate more than 1280M per second.
1521 * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
1522 * as it is faults that reset the window, pte updates will happen unconditionally
1523 * if there has not been a fault since @pteupdate_interval_millisecs after the
1524 * throttle window closed.
1526 static unsigned int migrate_interval_millisecs __read_mostly
= 100;
1527 static unsigned int pteupdate_interval_millisecs __read_mostly
= 1000;
1528 static unsigned int ratelimit_pages __read_mostly
= 128 << (20 - PAGE_SHIFT
);
1530 /* Returns true if NUMA migration is currently rate limited */
1531 bool migrate_ratelimited(int node
)
1533 pg_data_t
*pgdat
= NODE_DATA(node
);
1535 if (time_after(jiffies
, pgdat
->numabalancing_migrate_next_window
+
1536 msecs_to_jiffies(pteupdate_interval_millisecs
)))
1539 if (pgdat
->numabalancing_migrate_nr_pages
< ratelimit_pages
)
1545 /* Returns true if the node is migrate rate-limited after the update */
1546 bool numamigrate_update_ratelimit(pg_data_t
*pgdat
, unsigned long nr_pages
)
1548 bool rate_limited
= false;
1551 * Rate-limit the amount of data that is being migrated to a node.
1552 * Optimal placement is no good if the memory bus is saturated and
1553 * all the time is being spent migrating!
1555 spin_lock(&pgdat
->numabalancing_migrate_lock
);
1556 if (time_after(jiffies
, pgdat
->numabalancing_migrate_next_window
)) {
1557 pgdat
->numabalancing_migrate_nr_pages
= 0;
1558 pgdat
->numabalancing_migrate_next_window
= jiffies
+
1559 msecs_to_jiffies(migrate_interval_millisecs
);
1561 if (pgdat
->numabalancing_migrate_nr_pages
> ratelimit_pages
)
1562 rate_limited
= true;
1564 pgdat
->numabalancing_migrate_nr_pages
+= nr_pages
;
1565 spin_unlock(&pgdat
->numabalancing_migrate_lock
);
1567 return rate_limited
;
1570 int numamigrate_isolate_page(pg_data_t
*pgdat
, struct page
*page
)
1574 VM_BUG_ON(compound_order(page
) && !PageTransHuge(page
));
1576 /* Avoid migrating to a node that is nearly full */
1577 if (!migrate_balanced_pgdat(pgdat
, 1UL << compound_order(page
)))
1580 if (isolate_lru_page(page
))
1584 * migrate_misplaced_transhuge_page() skips page migration's usual
1585 * check on page_count(), so we must do it here, now that the page
1586 * has been isolated: a GUP pin, or any other pin, prevents migration.
1587 * The expected page count is 3: 1 for page's mapcount and 1 for the
1588 * caller's pin and 1 for the reference taken by isolate_lru_page().
1590 if (PageTransHuge(page
) && page_count(page
) != 3) {
1591 putback_lru_page(page
);
1595 page_lru
= page_is_file_cache(page
);
1596 mod_zone_page_state(page_zone(page
), NR_ISOLATED_ANON
+ page_lru
,
1597 hpage_nr_pages(page
));
1600 * Isolating the page has taken another reference, so the
1601 * caller's reference can be safely dropped without the page
1602 * disappearing underneath us during migration.
1609 * Attempt to migrate a misplaced page to the specified destination
1610 * node. Caller is expected to have an elevated reference count on
1611 * the page that will be dropped by this function before returning.
1613 int migrate_misplaced_page(struct page
*page
, struct vm_area_struct
*vma
,
1616 pg_data_t
*pgdat
= NODE_DATA(node
);
1619 LIST_HEAD(migratepages
);
1622 * Don't migrate file pages that are mapped in multiple processes
1623 * with execute permissions as they are probably shared libraries.
1625 if (page_mapcount(page
) != 1 && page_is_file_cache(page
) &&
1626 (vma
->vm_flags
& VM_EXEC
))
1630 * Rate-limit the amount of data that is being migrated to a node.
1631 * Optimal placement is no good if the memory bus is saturated and
1632 * all the time is being spent migrating!
1634 if (numamigrate_update_ratelimit(pgdat
, 1))
1637 isolated
= numamigrate_isolate_page(pgdat
, page
);
1641 list_add(&page
->lru
, &migratepages
);
1642 nr_remaining
= migrate_pages(&migratepages
, alloc_misplaced_dst_page
,
1643 node
, MIGRATE_ASYNC
, MR_NUMA_MISPLACED
);
1645 putback_lru_pages(&migratepages
);
1648 count_vm_numa_event(NUMA_PAGE_MIGRATE
);
1649 BUG_ON(!list_empty(&migratepages
));
1656 #endif /* CONFIG_NUMA_BALANCING */
1658 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1660 * Migrates a THP to a given target node. page must be locked and is unlocked
1663 int migrate_misplaced_transhuge_page(struct mm_struct
*mm
,
1664 struct vm_area_struct
*vma
,
1665 pmd_t
*pmd
, pmd_t entry
,
1666 unsigned long address
,
1667 struct page
*page
, int node
)
1669 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
1670 pg_data_t
*pgdat
= NODE_DATA(node
);
1672 struct page
*new_page
= NULL
;
1673 struct mem_cgroup
*memcg
= NULL
;
1674 int page_lru
= page_is_file_cache(page
);
1677 * Rate-limit the amount of data that is being migrated to a node.
1678 * Optimal placement is no good if the memory bus is saturated and
1679 * all the time is being spent migrating!
1681 if (numamigrate_update_ratelimit(pgdat
, HPAGE_PMD_NR
))
1684 new_page
= alloc_pages_node(node
,
1685 (GFP_TRANSHUGE
| GFP_THISNODE
) & ~__GFP_WAIT
, HPAGE_PMD_ORDER
);
1689 page_cpupid_xchg_last(new_page
, page_cpupid_last(page
));
1691 isolated
= numamigrate_isolate_page(pgdat
, page
);
1697 /* Prepare a page as a migration target */
1698 __set_page_locked(new_page
);
1699 SetPageSwapBacked(new_page
);
1701 /* anon mapping, we can simply copy page->mapping to the new page: */
1702 new_page
->mapping
= page
->mapping
;
1703 new_page
->index
= page
->index
;
1704 migrate_page_copy(new_page
, page
);
1705 WARN_ON(PageLRU(new_page
));
1707 /* Recheck the target PMD */
1708 spin_lock(&mm
->page_table_lock
);
1709 if (unlikely(!pmd_same(*pmd
, entry
))) {
1710 spin_unlock(&mm
->page_table_lock
);
1712 /* Reverse changes made by migrate_page_copy() */
1713 if (TestClearPageActive(new_page
))
1714 SetPageActive(page
);
1715 if (TestClearPageUnevictable(new_page
))
1716 SetPageUnevictable(page
);
1717 mlock_migrate_page(page
, new_page
);
1719 unlock_page(new_page
);
1720 put_page(new_page
); /* Free it */
1722 /* Retake the callers reference and putback on LRU */
1724 putback_lru_page(page
);
1725 mod_zone_page_state(page_zone(page
),
1726 NR_ISOLATED_ANON
+ page_lru
, -HPAGE_PMD_NR
);
1731 * Traditional migration needs to prepare the memcg charge
1732 * transaction early to prevent the old page from being
1733 * uncharged when installing migration entries. Here we can
1734 * save the potential rollback and start the charge transfer
1735 * only when migration is already known to end successfully.
1737 mem_cgroup_prepare_migration(page
, new_page
, &memcg
);
1739 entry
= mk_pmd(new_page
, vma
->vm_page_prot
);
1740 entry
= pmd_mknonnuma(entry
);
1741 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1742 entry
= pmd_mkhuge(entry
);
1744 pmdp_clear_flush(vma
, haddr
, pmd
);
1745 set_pmd_at(mm
, haddr
, pmd
, entry
);
1746 page_add_new_anon_rmap(new_page
, vma
, haddr
);
1747 update_mmu_cache_pmd(vma
, address
, &entry
);
1748 page_remove_rmap(page
);
1750 * Finish the charge transaction under the page table lock to
1751 * prevent split_huge_page() from dividing up the charge
1752 * before it's fully transferred to the new page.
1754 mem_cgroup_end_migration(memcg
, page
, new_page
, true);
1755 spin_unlock(&mm
->page_table_lock
);
1757 unlock_page(new_page
);
1759 put_page(page
); /* Drop the rmap reference */
1760 put_page(page
); /* Drop the LRU isolation reference */
1762 count_vm_events(PGMIGRATE_SUCCESS
, HPAGE_PMD_NR
);
1763 count_vm_numa_events(NUMA_PAGE_MIGRATE
, HPAGE_PMD_NR
);
1765 mod_zone_page_state(page_zone(page
),
1766 NR_ISOLATED_ANON
+ page_lru
,
1771 count_vm_events(PGMIGRATE_FAIL
, HPAGE_PMD_NR
);
1773 entry
= pmd_mknonnuma(entry
);
1774 set_pmd_at(mm
, haddr
, pmd
, entry
);
1775 update_mmu_cache_pmd(vma
, address
, &entry
);
1781 #endif /* CONFIG_NUMA_BALANCING */
1783 #endif /* CONFIG_NUMA */