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>
45 * migrate_prep() needs to be called before we start compiling a list of pages
46 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
47 * undesirable, use migrate_prep_local()
49 int migrate_prep(void)
52 * Clear the LRU lists so pages can be isolated.
53 * Note that pages may be moved off the LRU after we have
54 * drained them. Those pages will fail to migrate like other
55 * pages that may be busy.
62 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
63 int migrate_prep_local(void)
71 * Add isolated pages on the list back to the LRU under page lock
72 * to avoid leaking evictable pages back onto unevictable list.
74 void putback_lru_pages(struct list_head *l)
79 list_for_each_entry_safe(page, page2, l, lru) {
81 dec_zone_page_state(page, NR_ISOLATED_ANON +
82 page_is_file_cache(page));
83 if (unlikely(balloon_page_movable(page)))
84 balloon_page_putback(page);
86 putback_lru_page(page);
91 * Restore a potential migration pte to a working pte entry
93 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
94 unsigned long addr, void *old)
96 struct mm_struct *mm = vma->vm_mm;
102 if (unlikely(PageHuge(new))) {
103 ptep = huge_pte_offset(mm, addr);
106 ptl = &mm->page_table_lock;
108 pmd = mm_find_pmd(mm, addr);
111 if (pmd_trans_huge(*pmd))
114 ptep = pte_offset_map(pmd, addr);
117 * Peek to check is_swap_pte() before taking ptlock? No, we
118 * can race mremap's move_ptes(), which skips anon_vma lock.
121 ptl = pte_lockptr(mm, pmd);
126 if (!is_swap_pte(pte))
129 entry = pte_to_swp_entry(pte);
131 if (!is_migration_entry(entry) ||
132 migration_entry_to_page(entry) != old)
136 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
137 if (is_write_migration_entry(entry))
138 pte = pte_mkwrite(pte);
139 #ifdef CONFIG_HUGETLB_PAGE
141 pte = pte_mkhuge(pte);
143 flush_cache_page(vma, addr, pte_pfn(pte));
144 set_pte_at(mm, addr, ptep, pte);
148 hugepage_add_anon_rmap(new, vma, addr);
151 } else if (PageAnon(new))
152 page_add_anon_rmap(new, vma, addr);
154 page_add_file_rmap(new);
156 /* No need to invalidate - it was non-present before */
157 update_mmu_cache(vma, addr, ptep);
159 pte_unmap_unlock(ptep, ptl);
165 * Get rid of all migration entries and replace them by
166 * references to the indicated page.
168 static void remove_migration_ptes(struct page *old, struct page *new)
170 rmap_walk(new, remove_migration_pte, old);
174 * Something used the pte of a page under migration. We need to
175 * get to the page and wait until migration is finished.
176 * When we return from this function the fault will be retried.
178 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
179 unsigned long address)
186 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
188 if (!is_swap_pte(pte))
191 entry = pte_to_swp_entry(pte);
192 if (!is_migration_entry(entry))
195 page = migration_entry_to_page(entry);
198 * Once radix-tree replacement of page migration started, page_count
199 * *must* be zero. And, we don't want to call wait_on_page_locked()
200 * against a page without get_page().
201 * So, we use get_page_unless_zero(), here. Even failed, page fault
204 if (!get_page_unless_zero(page))
206 pte_unmap_unlock(ptep, ptl);
207 wait_on_page_locked(page);
211 pte_unmap_unlock(ptep, ptl);
215 /* Returns true if all buffers are successfully locked */
216 static bool buffer_migrate_lock_buffers(struct buffer_head *head,
217 enum migrate_mode mode)
219 struct buffer_head *bh = head;
221 /* Simple case, sync compaction */
222 if (mode != MIGRATE_ASYNC && mode != MIGRATE_FAULT) {
226 bh = bh->b_this_page;
228 } while (bh != head);
233 /* async case, we cannot block on lock_buffer so use trylock_buffer */
236 if (!trylock_buffer(bh)) {
238 * We failed to lock the buffer and cannot stall in
239 * async migration. Release the taken locks
241 struct buffer_head *failed_bh = bh;
244 while (bh != failed_bh) {
247 bh = bh->b_this_page;
252 bh = bh->b_this_page;
253 } while (bh != head);
257 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
258 enum migrate_mode mode)
262 #endif /* CONFIG_BLOCK */
265 * Replace the page in the mapping.
267 * The number of remaining references must be:
268 * 1 for anonymous pages without a mapping
269 * 2 for pages with a mapping
270 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
272 static int migrate_page_move_mapping(struct address_space *mapping,
273 struct page *newpage, struct page *page,
274 struct buffer_head *head, enum migrate_mode mode)
276 int expected_count = 0;
279 if (mode == MIGRATE_FAULT) {
281 * MIGRATE_FAULT has an extra reference on the page and
282 * otherwise acts like ASYNC, no point in delaying the
283 * fault, we'll try again next time.
289 /* Anonymous page without mapping */
291 if (page_count(page) != expected_count)
293 return MIGRATEPAGE_SUCCESS;
296 spin_lock_irq(&mapping->tree_lock);
298 pslot = radix_tree_lookup_slot(&mapping->page_tree,
301 expected_count += 2 + page_has_private(page);
302 if (page_count(page) != expected_count ||
303 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
304 spin_unlock_irq(&mapping->tree_lock);
308 if (!page_freeze_refs(page, expected_count)) {
309 spin_unlock_irq(&mapping->tree_lock);
314 * In the async migration case of moving a page with buffers, lock the
315 * buffers using trylock before the mapping is moved. If the mapping
316 * was moved, we later failed to lock the buffers and could not move
317 * the mapping back due to an elevated page count, we would have to
318 * block waiting on other references to be dropped.
320 if ((mode == MIGRATE_ASYNC || mode == MIGRATE_FAULT) && head &&
321 !buffer_migrate_lock_buffers(head, mode)) {
322 page_unfreeze_refs(page, expected_count);
323 spin_unlock_irq(&mapping->tree_lock);
328 * Now we know that no one else is looking at the page.
330 get_page(newpage); /* add cache reference */
331 if (PageSwapCache(page)) {
332 SetPageSwapCache(newpage);
333 set_page_private(newpage, page_private(page));
336 radix_tree_replace_slot(pslot, newpage);
339 * Drop cache reference from old page by unfreezing
340 * to one less reference.
341 * We know this isn't the last reference.
343 page_unfreeze_refs(page, expected_count - 1);
346 * If moved to a different zone then also account
347 * the page for that zone. Other VM counters will be
348 * taken care of when we establish references to the
349 * new page and drop references to the old page.
351 * Note that anonymous pages are accounted for
352 * via NR_FILE_PAGES and NR_ANON_PAGES if they
353 * are mapped to swap space.
355 __dec_zone_page_state(page, NR_FILE_PAGES);
356 __inc_zone_page_state(newpage, NR_FILE_PAGES);
357 if (!PageSwapCache(page) && PageSwapBacked(page)) {
358 __dec_zone_page_state(page, NR_SHMEM);
359 __inc_zone_page_state(newpage, NR_SHMEM);
361 spin_unlock_irq(&mapping->tree_lock);
363 return MIGRATEPAGE_SUCCESS;
367 * The expected number of remaining references is the same as that
368 * of migrate_page_move_mapping().
370 int migrate_huge_page_move_mapping(struct address_space *mapping,
371 struct page *newpage, struct page *page)
377 if (page_count(page) != 1)
379 return MIGRATEPAGE_SUCCESS;
382 spin_lock_irq(&mapping->tree_lock);
384 pslot = radix_tree_lookup_slot(&mapping->page_tree,
387 expected_count = 2 + page_has_private(page);
388 if (page_count(page) != expected_count ||
389 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
390 spin_unlock_irq(&mapping->tree_lock);
394 if (!page_freeze_refs(page, expected_count)) {
395 spin_unlock_irq(&mapping->tree_lock);
401 radix_tree_replace_slot(pslot, newpage);
403 page_unfreeze_refs(page, expected_count - 1);
405 spin_unlock_irq(&mapping->tree_lock);
406 return MIGRATEPAGE_SUCCESS;
410 * Copy the page to its new location
412 void migrate_page_copy(struct page *newpage, struct page *page)
414 if (PageHuge(page) || PageTransHuge(page))
415 copy_huge_page(newpage, page);
417 copy_highpage(newpage, page);
420 SetPageError(newpage);
421 if (PageReferenced(page))
422 SetPageReferenced(newpage);
423 if (PageUptodate(page))
424 SetPageUptodate(newpage);
425 if (TestClearPageActive(page)) {
426 VM_BUG_ON(PageUnevictable(page));
427 SetPageActive(newpage);
428 } else if (TestClearPageUnevictable(page))
429 SetPageUnevictable(newpage);
430 if (PageChecked(page))
431 SetPageChecked(newpage);
432 if (PageMappedToDisk(page))
433 SetPageMappedToDisk(newpage);
435 if (PageDirty(page)) {
436 clear_page_dirty_for_io(page);
438 * Want to mark the page and the radix tree as dirty, and
439 * redo the accounting that clear_page_dirty_for_io undid,
440 * but we can't use set_page_dirty because that function
441 * is actually a signal that all of the page has become dirty.
442 * Whereas only part of our page may be dirty.
444 if (PageSwapBacked(page))
445 SetPageDirty(newpage);
447 __set_page_dirty_nobuffers(newpage);
450 mlock_migrate_page(newpage, page);
451 ksm_migrate_page(newpage, page);
453 ClearPageSwapCache(page);
454 ClearPagePrivate(page);
455 set_page_private(page, 0);
458 * If any waiters have accumulated on the new page then
461 if (PageWriteback(newpage))
462 end_page_writeback(newpage);
465 /************************************************************
466 * Migration functions
467 ***********************************************************/
469 /* Always fail migration. Used for mappings that are not movable */
470 int fail_migrate_page(struct address_space *mapping,
471 struct page *newpage, struct page *page)
475 EXPORT_SYMBOL(fail_migrate_page);
478 * Common logic to directly migrate a single page suitable for
479 * pages that do not use PagePrivate/PagePrivate2.
481 * Pages are locked upon entry and exit.
483 int migrate_page(struct address_space *mapping,
484 struct page *newpage, struct page *page,
485 enum migrate_mode mode)
489 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
491 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
493 if (rc != MIGRATEPAGE_SUCCESS)
496 migrate_page_copy(newpage, page);
497 return MIGRATEPAGE_SUCCESS;
499 EXPORT_SYMBOL(migrate_page);
503 * Migration function for pages with buffers. This function can only be used
504 * if the underlying filesystem guarantees that no other references to "page"
507 int buffer_migrate_page(struct address_space *mapping,
508 struct page *newpage, struct page *page, enum migrate_mode mode)
510 struct buffer_head *bh, *head;
513 if (!page_has_buffers(page))
514 return migrate_page(mapping, newpage, page, mode);
516 head = page_buffers(page);
518 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
520 if (rc != MIGRATEPAGE_SUCCESS)
524 * In the async case, migrate_page_move_mapping locked the buffers
525 * with an IRQ-safe spinlock held. In the sync case, the buffers
526 * need to be locked now
528 if (mode != MIGRATE_ASYNC && mode != MIGRATE_FAULT)
529 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
531 ClearPagePrivate(page);
532 set_page_private(newpage, page_private(page));
533 set_page_private(page, 0);
539 set_bh_page(bh, newpage, bh_offset(bh));
540 bh = bh->b_this_page;
542 } while (bh != head);
544 SetPagePrivate(newpage);
546 migrate_page_copy(newpage, page);
552 bh = bh->b_this_page;
554 } while (bh != head);
556 return MIGRATEPAGE_SUCCESS;
558 EXPORT_SYMBOL(buffer_migrate_page);
562 * Writeback a page to clean the dirty state
564 static int writeout(struct address_space *mapping, struct page *page)
566 struct writeback_control wbc = {
567 .sync_mode = WB_SYNC_NONE,
570 .range_end = LLONG_MAX,
575 if (!mapping->a_ops->writepage)
576 /* No write method for the address space */
579 if (!clear_page_dirty_for_io(page))
580 /* Someone else already triggered a write */
584 * A dirty page may imply that the underlying filesystem has
585 * the page on some queue. So the page must be clean for
586 * migration. Writeout may mean we loose the lock and the
587 * page state is no longer what we checked for earlier.
588 * At this point we know that the migration attempt cannot
591 remove_migration_ptes(page, page);
593 rc = mapping->a_ops->writepage(page, &wbc);
595 if (rc != AOP_WRITEPAGE_ACTIVATE)
596 /* unlocked. Relock */
599 return (rc < 0) ? -EIO : -EAGAIN;
603 * Default handling if a filesystem does not provide a migration function.
605 static int fallback_migrate_page(struct address_space *mapping,
606 struct page *newpage, struct page *page, enum migrate_mode mode)
608 if (PageDirty(page)) {
609 /* Only writeback pages in full synchronous migration */
610 if (mode != MIGRATE_SYNC)
612 return writeout(mapping, page);
616 * Buffers may be managed in a filesystem specific way.
617 * We must have no buffers or drop them.
619 if (page_has_private(page) &&
620 !try_to_release_page(page, GFP_KERNEL))
623 return migrate_page(mapping, newpage, page, mode);
627 * Move a page to a newly allocated page
628 * The page is locked and all ptes have been successfully removed.
630 * The new page will have replaced the old page if this function
637 static int move_to_new_page(struct page *newpage, struct page *page,
638 int remap_swapcache, enum migrate_mode mode)
640 struct address_space *mapping;
644 * Block others from accessing the page when we get around to
645 * establishing additional references. We are the only one
646 * holding a reference to the new page at this point.
648 if (!trylock_page(newpage))
651 /* Prepare mapping for the new page.*/
652 newpage->index = page->index;
653 newpage->mapping = page->mapping;
654 if (PageSwapBacked(page))
655 SetPageSwapBacked(newpage);
657 mapping = page_mapping(page);
659 rc = migrate_page(mapping, newpage, page, mode);
660 else if (mapping->a_ops->migratepage)
662 * Most pages have a mapping and most filesystems provide a
663 * migratepage callback. Anonymous pages are part of swap
664 * space which also has its own migratepage callback. This
665 * is the most common path for page migration.
667 rc = mapping->a_ops->migratepage(mapping,
668 newpage, page, mode);
670 rc = fallback_migrate_page(mapping, newpage, page, mode);
672 if (rc != MIGRATEPAGE_SUCCESS) {
673 newpage->mapping = NULL;
676 remove_migration_ptes(page, newpage);
677 page->mapping = NULL;
680 unlock_page(newpage);
685 static int __unmap_and_move(struct page *page, struct page *newpage,
686 int force, bool offlining, enum migrate_mode mode)
689 int remap_swapcache = 1;
690 struct mem_cgroup *mem;
691 struct anon_vma *anon_vma = NULL;
693 if (!trylock_page(page)) {
694 if (!force || mode == MIGRATE_ASYNC || mode == MIGRATE_FAULT)
698 * It's not safe for direct compaction to call lock_page.
699 * For example, during page readahead pages are added locked
700 * to the LRU. Later, when the IO completes the pages are
701 * marked uptodate and unlocked. However, the queueing
702 * could be merging multiple pages for one bio (e.g.
703 * mpage_readpages). If an allocation happens for the
704 * second or third page, the process can end up locking
705 * the same page twice and deadlocking. Rather than
706 * trying to be clever about what pages can be locked,
707 * avoid the use of lock_page for direct compaction
710 if (current->flags & PF_MEMALLOC)
717 * Only memory hotplug's offline_pages() caller has locked out KSM,
718 * and can safely migrate a KSM page. The other cases have skipped
719 * PageKsm along with PageReserved - but it is only now when we have
720 * the page lock that we can be certain it will not go KSM beneath us
721 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
722 * its pagecount raised, but only here do we take the page lock which
725 if (PageKsm(page) && !offlining) {
730 /* charge against new page */
731 mem_cgroup_prepare_migration(page, newpage, &mem);
733 if (PageWriteback(page)) {
735 * Only in the case of a full syncronous migration is it
736 * necessary to wait for PageWriteback. In the async case,
737 * the retry loop is too short and in the sync-light case,
738 * the overhead of stalling is too much
740 if (mode != MIGRATE_SYNC) {
746 wait_on_page_writeback(page);
749 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
750 * we cannot notice that anon_vma is freed while we migrates a page.
751 * This get_anon_vma() delays freeing anon_vma pointer until the end
752 * of migration. File cache pages are no problem because of page_lock()
753 * File Caches may use write_page() or lock_page() in migration, then,
754 * just care Anon page here.
756 if (PageAnon(page)) {
758 * Only page_lock_anon_vma() understands the subtleties of
759 * getting a hold on an anon_vma from outside one of its mms.
761 anon_vma = page_get_anon_vma(page);
766 } else if (PageSwapCache(page)) {
768 * We cannot be sure that the anon_vma of an unmapped
769 * swapcache page is safe to use because we don't
770 * know in advance if the VMA that this page belonged
771 * to still exists. If the VMA and others sharing the
772 * data have been freed, then the anon_vma could
773 * already be invalid.
775 * To avoid this possibility, swapcache pages get
776 * migrated but are not remapped when migration
785 if (unlikely(balloon_page_movable(page))) {
787 * A ballooned page does not need any special attention from
788 * physical to virtual reverse mapping procedures.
789 * Skip any attempt to unmap PTEs or to remap swap cache,
790 * in order to avoid burning cycles at rmap level, and perform
791 * the page migration right away (proteced by page lock).
793 rc = balloon_page_migrate(newpage, page, mode);
798 * Corner case handling:
799 * 1. When a new swap-cache page is read into, it is added to the LRU
800 * and treated as swapcache but it has no rmap yet.
801 * Calling try_to_unmap() against a page->mapping==NULL page will
802 * trigger a BUG. So handle it here.
803 * 2. An orphaned page (see truncate_complete_page) might have
804 * fs-private metadata. The page can be picked up due to memory
805 * offlining. Everywhere else except page reclaim, the page is
806 * invisible to the vm, so the page can not be migrated. So try to
807 * free the metadata, so the page can be freed.
809 if (!page->mapping) {
810 VM_BUG_ON(PageAnon(page));
811 if (page_has_private(page)) {
812 try_to_free_buffers(page);
818 /* Establish migration ptes or remove ptes */
819 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
822 if (!page_mapped(page))
823 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
825 if (rc && remap_swapcache)
826 remove_migration_ptes(page, page);
828 /* Drop an anon_vma reference if we took one */
830 put_anon_vma(anon_vma);
833 mem_cgroup_end_migration(mem, page, newpage,
834 (rc == MIGRATEPAGE_SUCCESS ||
835 rc == MIGRATEPAGE_BALLOON_SUCCESS));
843 * Obtain the lock on page, remove all ptes and migrate the page
844 * to the newly allocated page in newpage.
846 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
847 struct page *page, int force, bool offlining,
848 enum migrate_mode mode)
852 struct page *newpage = get_new_page(page, private, &result);
857 if (page_count(page) == 1) {
858 /* page was freed from under us. So we are done. */
862 if (unlikely(PageTransHuge(page)))
863 if (unlikely(split_huge_page(page)))
866 rc = __unmap_and_move(page, newpage, force, offlining, mode);
868 if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
870 * A ballooned page has been migrated already.
871 * Now, it's the time to remove the old page from the isolated
872 * pageset list and handle it back to Buddy, wrap-up counters
875 dec_zone_page_state(page, NR_ISOLATED_ANON +
876 page_is_file_cache(page));
884 * A page that has been migrated has all references
885 * removed and will be freed. A page that has not been
886 * migrated will have kepts its references and be
889 list_del(&page->lru);
890 dec_zone_page_state(page, NR_ISOLATED_ANON +
891 page_is_file_cache(page));
892 putback_lru_page(page);
895 * Move the new page to the LRU. If migration was not successful
896 * then this will free the page.
898 putback_lru_page(newpage);
903 *result = page_to_nid(newpage);
909 * Counterpart of unmap_and_move_page() for hugepage migration.
911 * This function doesn't wait the completion of hugepage I/O
912 * because there is no race between I/O and migration for hugepage.
913 * Note that currently hugepage I/O occurs only in direct I/O
914 * where no lock is held and PG_writeback is irrelevant,
915 * and writeback status of all subpages are counted in the reference
916 * count of the head page (i.e. if all subpages of a 2MB hugepage are
917 * under direct I/O, the reference of the head page is 512 and a bit more.)
918 * This means that when we try to migrate hugepage whose subpages are
919 * doing direct I/O, some references remain after try_to_unmap() and
920 * hugepage migration fails without data corruption.
922 * There is also no race when direct I/O is issued on the page under migration,
923 * because then pte is replaced with migration swap entry and direct I/O code
924 * will wait in the page fault for migration to complete.
926 static int unmap_and_move_huge_page(new_page_t get_new_page,
927 unsigned long private, struct page *hpage,
928 int force, bool offlining,
929 enum migrate_mode mode)
933 struct page *new_hpage = get_new_page(hpage, private, &result);
934 struct anon_vma *anon_vma = NULL;
941 if (!trylock_page(hpage)) {
942 if (!force || mode != MIGRATE_SYNC)
948 anon_vma = page_get_anon_vma(hpage);
950 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
952 if (!page_mapped(hpage))
953 rc = move_to_new_page(new_hpage, hpage, 1, mode);
956 remove_migration_ptes(hpage, hpage);
959 put_anon_vma(anon_vma);
962 hugetlb_cgroup_migrate(hpage, new_hpage);
971 *result = page_to_nid(new_hpage);
979 * The function takes one list of pages to migrate and a function
980 * that determines from the page to be migrated and the private data
981 * the target of the move and allocates the page.
983 * The function returns after 10 attempts or if no pages
984 * are movable anymore because to has become empty
985 * or no retryable pages exist anymore.
986 * Caller should call putback_lru_pages to return pages to the LRU
987 * or free list only if ret != 0.
989 * Return: Number of pages not migrated or error code.
991 int migrate_pages(struct list_head *from,
992 new_page_t get_new_page, unsigned long private, bool offlining,
993 enum migrate_mode mode)
1000 int swapwrite = current->flags & PF_SWAPWRITE;
1004 current->flags |= PF_SWAPWRITE;
1006 for(pass = 0; pass < 10 && retry; pass++) {
1009 list_for_each_entry_safe(page, page2, from, lru) {
1012 rc = unmap_and_move(get_new_page, private,
1013 page, pass > 2, offlining,
1022 case MIGRATEPAGE_SUCCESS:
1025 /* Permanent failure */
1031 rc = nr_failed + retry;
1034 current->flags &= ~PF_SWAPWRITE;
1039 int migrate_huge_page(struct page *hpage, new_page_t get_new_page,
1040 unsigned long private, bool offlining,
1041 enum migrate_mode mode)
1045 for (pass = 0; pass < 10; pass++) {
1046 rc = unmap_and_move_huge_page(get_new_page,
1047 private, hpage, pass > 2, offlining,
1056 case MIGRATEPAGE_SUCCESS:
1069 * Move a list of individual pages
1071 struct page_to_node {
1078 static struct page *new_page_node(struct page *p, unsigned long private,
1081 struct page_to_node *pm = (struct page_to_node *)private;
1083 while (pm->node != MAX_NUMNODES && pm->page != p)
1086 if (pm->node == MAX_NUMNODES)
1089 *result = &pm->status;
1091 return alloc_pages_exact_node(pm->node,
1092 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
1096 * Move a set of pages as indicated in the pm array. The addr
1097 * field must be set to the virtual address of the page to be moved
1098 * and the node number must contain a valid target node.
1099 * The pm array ends with node = MAX_NUMNODES.
1101 static int do_move_page_to_node_array(struct mm_struct *mm,
1102 struct page_to_node *pm,
1106 struct page_to_node *pp;
1107 LIST_HEAD(pagelist);
1109 down_read(&mm->mmap_sem);
1112 * Build a list of pages to migrate
1114 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1115 struct vm_area_struct *vma;
1119 vma = find_vma(mm, pp->addr);
1120 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
1123 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
1125 err = PTR_ERR(page);
1133 /* Use PageReserved to check for zero page */
1134 if (PageReserved(page) || PageKsm(page))
1138 err = page_to_nid(page);
1140 if (err == pp->node)
1142 * Node already in the right place
1147 if (page_mapcount(page) > 1 &&
1151 err = isolate_lru_page(page);
1153 list_add_tail(&page->lru, &pagelist);
1154 inc_zone_page_state(page, NR_ISOLATED_ANON +
1155 page_is_file_cache(page));
1159 * Either remove the duplicate refcount from
1160 * isolate_lru_page() or drop the page ref if it was
1169 if (!list_empty(&pagelist)) {
1170 err = migrate_pages(&pagelist, new_page_node,
1171 (unsigned long)pm, 0, MIGRATE_SYNC);
1173 putback_lru_pages(&pagelist);
1176 up_read(&mm->mmap_sem);
1181 * Migrate an array of page address onto an array of nodes and fill
1182 * the corresponding array of status.
1184 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
1185 unsigned long nr_pages,
1186 const void __user * __user *pages,
1187 const int __user *nodes,
1188 int __user *status, int flags)
1190 struct page_to_node *pm;
1191 unsigned long chunk_nr_pages;
1192 unsigned long chunk_start;
1196 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1203 * Store a chunk of page_to_node array in a page,
1204 * but keep the last one as a marker
1206 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
1208 for (chunk_start = 0;
1209 chunk_start < nr_pages;
1210 chunk_start += chunk_nr_pages) {
1213 if (chunk_start + chunk_nr_pages > nr_pages)
1214 chunk_nr_pages = nr_pages - chunk_start;
1216 /* fill the chunk pm with addrs and nodes from user-space */
1217 for (j = 0; j < chunk_nr_pages; j++) {
1218 const void __user *p;
1222 if (get_user(p, pages + j + chunk_start))
1224 pm[j].addr = (unsigned long) p;
1226 if (get_user(node, nodes + j + chunk_start))
1230 if (node < 0 || node >= MAX_NUMNODES)
1233 if (!node_state(node, N_HIGH_MEMORY))
1237 if (!node_isset(node, task_nodes))
1243 /* End marker for this chunk */
1244 pm[chunk_nr_pages].node = MAX_NUMNODES;
1246 /* Migrate this chunk */
1247 err = do_move_page_to_node_array(mm, pm,
1248 flags & MPOL_MF_MOVE_ALL);
1252 /* Return status information */
1253 for (j = 0; j < chunk_nr_pages; j++)
1254 if (put_user(pm[j].status, status + j + chunk_start)) {
1262 free_page((unsigned long)pm);
1268 * Determine the nodes of an array of pages and store it in an array of status.
1270 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1271 const void __user **pages, int *status)
1275 down_read(&mm->mmap_sem);
1277 for (i = 0; i < nr_pages; i++) {
1278 unsigned long addr = (unsigned long)(*pages);
1279 struct vm_area_struct *vma;
1283 vma = find_vma(mm, addr);
1284 if (!vma || addr < vma->vm_start)
1287 page = follow_page(vma, addr, 0);
1289 err = PTR_ERR(page);
1294 /* Use PageReserved to check for zero page */
1295 if (!page || PageReserved(page) || PageKsm(page))
1298 err = page_to_nid(page);
1306 up_read(&mm->mmap_sem);
1310 * Determine the nodes of a user array of pages and store it in
1311 * a user array of status.
1313 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1314 const void __user * __user *pages,
1317 #define DO_PAGES_STAT_CHUNK_NR 16
1318 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1319 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1322 unsigned long chunk_nr;
1324 chunk_nr = nr_pages;
1325 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1326 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1328 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1331 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1333 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1338 nr_pages -= chunk_nr;
1340 return nr_pages ? -EFAULT : 0;
1344 * Move a list of pages in the address space of the currently executing
1347 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1348 const void __user * __user *, pages,
1349 const int __user *, nodes,
1350 int __user *, status, int, flags)
1352 const struct cred *cred = current_cred(), *tcred;
1353 struct task_struct *task;
1354 struct mm_struct *mm;
1356 nodemask_t task_nodes;
1359 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1362 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1365 /* Find the mm_struct */
1367 task = pid ? find_task_by_vpid(pid) : current;
1372 get_task_struct(task);
1375 * Check if this process has the right to modify the specified
1376 * process. The right exists if the process has administrative
1377 * capabilities, superuser privileges or the same
1378 * userid as the target process.
1380 tcred = __task_cred(task);
1381 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1382 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
1383 !capable(CAP_SYS_NICE)) {
1390 err = security_task_movememory(task);
1394 task_nodes = cpuset_mems_allowed(task);
1395 mm = get_task_mm(task);
1396 put_task_struct(task);
1402 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1403 nodes, status, flags);
1405 err = do_pages_stat(mm, nr_pages, pages, status);
1411 put_task_struct(task);
1416 * Call migration functions in the vma_ops that may prepare
1417 * memory in a vm for migration. migration functions may perform
1418 * the migration for vmas that do not have an underlying page struct.
1420 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1421 const nodemask_t *from, unsigned long flags)
1423 struct vm_area_struct *vma;
1426 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1427 if (vma->vm_ops && vma->vm_ops->migrate) {
1428 err = vma->vm_ops->migrate(vma, to, from, flags);
1437 * Attempt to migrate a misplaced page to the specified destination
1440 int migrate_misplaced_page(struct page *page, int node)
1442 struct address_space *mapping = page_mapping(page);
1443 int page_lru = page_is_file_cache(page);
1444 struct page *newpage;
1446 gfp_t gfp = GFP_HIGHUSER_MOVABLE;
1449 * Don't migrate pages that are mapped in multiple processes.
1451 if (page_mapcount(page) != 1)
1455 * Never wait for allocations just to migrate on fault, but don't dip
1456 * into reserves. And, only accept pages from the specified node. No
1457 * sense migrating to a different "misplaced" page!
1460 gfp = mapping_gfp_mask(mapping);
1462 gfp |= __GFP_NOMEMALLOC | GFP_THISNODE;
1464 newpage = alloc_pages_node(node, gfp, 0);
1470 if (isolate_lru_page(page)) {
1475 inc_zone_page_state(page, NR_ISOLATED_ANON + page_lru);
1476 ret = __unmap_and_move(page, newpage, 0, 0, MIGRATE_FAULT);
1478 * A page that has been migrated has all references removed and will be
1479 * freed. A page that has not been migrated will have kepts its
1480 * references and be restored.
1482 dec_zone_page_state(page, NR_ISOLATED_ANON + page_lru);
1483 putback_lru_page(page);
1486 * Move the new page to the LRU. If migration was not successful
1487 * then this will free the page.
1489 putback_lru_page(newpage);
1494 #endif /* CONFIG_NUMA */