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/gfp.h>
38 #include <asm/tlbflush.h>
42 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
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 putback_lru_page(page);
88 * Restore a potential migration pte to a working pte entry
90 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
91 unsigned long addr, void *old)
93 struct mm_struct *mm = vma->vm_mm;
101 if (unlikely(PageHuge(new))) {
102 ptep = huge_pte_offset(mm, addr);
105 ptl = &mm->page_table_lock;
107 pgd = pgd_offset(mm, addr);
108 if (!pgd_present(*pgd))
111 pud = pud_offset(pgd, addr);
112 if (!pud_present(*pud))
115 pmd = pmd_offset(pud, addr);
116 if (pmd_trans_huge(*pmd))
118 if (!pmd_present(*pmd))
121 ptep = pte_offset_map(pmd, addr);
124 * Peek to check is_swap_pte() before taking ptlock? No, we
125 * can race mremap's move_ptes(), which skips anon_vma lock.
128 ptl = pte_lockptr(mm, pmd);
133 if (!is_swap_pte(pte))
136 entry = pte_to_swp_entry(pte);
138 if (!is_migration_entry(entry) ||
139 migration_entry_to_page(entry) != old)
143 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
144 if (is_write_migration_entry(entry))
145 pte = pte_mkwrite(pte);
146 #ifdef CONFIG_HUGETLB_PAGE
148 pte = pte_mkhuge(pte);
150 flush_cache_page(vma, addr, pte_pfn(pte));
151 set_pte_at(mm, addr, ptep, pte);
155 hugepage_add_anon_rmap(new, vma, addr);
158 } else if (PageAnon(new))
159 page_add_anon_rmap(new, vma, addr);
161 page_add_file_rmap(new);
163 /* No need to invalidate - it was non-present before */
164 update_mmu_cache(vma, addr, ptep);
166 pte_unmap_unlock(ptep, ptl);
172 * Get rid of all migration entries and replace them by
173 * references to the indicated page.
175 static void remove_migration_ptes(struct page *old, struct page *new)
177 rmap_walk(new, remove_migration_pte, old);
181 * Something used the pte of a page under migration. We need to
182 * get to the page and wait until migration is finished.
183 * When we return from this function the fault will be retried.
185 * This function is called from do_swap_page().
187 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
188 unsigned long address)
195 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
197 if (!is_swap_pte(pte))
200 entry = pte_to_swp_entry(pte);
201 if (!is_migration_entry(entry))
204 page = migration_entry_to_page(entry);
207 * Once radix-tree replacement of page migration started, page_count
208 * *must* be zero. And, we don't want to call wait_on_page_locked()
209 * against a page without get_page().
210 * So, we use get_page_unless_zero(), here. Even failed, page fault
213 if (!get_page_unless_zero(page))
215 pte_unmap_unlock(ptep, ptl);
216 wait_on_page_locked(page);
220 pte_unmap_unlock(ptep, ptl);
224 /* Returns true if all buffers are successfully locked */
225 static bool buffer_migrate_lock_buffers(struct buffer_head *head, bool sync)
227 struct buffer_head *bh = head;
229 /* Simple case, sync compaction */
234 bh = bh->b_this_page;
236 } while (bh != head);
241 /* async case, we cannot block on lock_buffer so use trylock_buffer */
244 if (!trylock_buffer(bh)) {
246 * We failed to lock the buffer and cannot stall in
247 * async migration. Release the taken locks
249 struct buffer_head *failed_bh = bh;
252 while (bh != failed_bh) {
255 bh = bh->b_this_page;
260 bh = bh->b_this_page;
261 } while (bh != head);
265 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
270 #endif /* CONFIG_BLOCK */
273 * Replace the page in the mapping.
275 * The number of remaining references must be:
276 * 1 for anonymous pages without a mapping
277 * 2 for pages with a mapping
278 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
280 static int migrate_page_move_mapping(struct address_space *mapping,
281 struct page *newpage, struct page *page,
282 struct buffer_head *head, bool sync)
288 /* Anonymous page without mapping */
289 if (page_count(page) != 1)
294 spin_lock_irq(&mapping->tree_lock);
296 pslot = radix_tree_lookup_slot(&mapping->page_tree,
299 expected_count = 2 + page_has_private(page);
300 if (page_count(page) != expected_count ||
301 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
302 spin_unlock_irq(&mapping->tree_lock);
306 if (!page_freeze_refs(page, expected_count)) {
307 spin_unlock_irq(&mapping->tree_lock);
312 * In the async migration case of moving a page with buffers, lock the
313 * buffers using trylock before the mapping is moved. If the mapping
314 * was moved, we later failed to lock the buffers and could not move
315 * the mapping back due to an elevated page count, we would have to
316 * block waiting on other references to be dropped.
318 if (!sync && head && !buffer_migrate_lock_buffers(head, sync)) {
319 page_unfreeze_refs(page, expected_count);
320 spin_unlock_irq(&mapping->tree_lock);
325 * Now we know that no one else is looking at the page.
327 get_page(newpage); /* add cache reference */
328 if (PageSwapCache(page)) {
329 SetPageSwapCache(newpage);
330 set_page_private(newpage, page_private(page));
333 radix_tree_replace_slot(pslot, newpage);
335 page_unfreeze_refs(page, expected_count);
337 * Drop cache reference from old page.
338 * We know this isn't the last reference.
343 * If moved to a different zone then also account
344 * the page for that zone. Other VM counters will be
345 * taken care of when we establish references to the
346 * new page and drop references to the old page.
348 * Note that anonymous pages are accounted for
349 * via NR_FILE_PAGES and NR_ANON_PAGES if they
350 * are mapped to swap space.
352 __dec_zone_page_state(page, NR_FILE_PAGES);
353 __inc_zone_page_state(newpage, NR_FILE_PAGES);
354 if (!PageSwapCache(page) && PageSwapBacked(page)) {
355 __dec_zone_page_state(page, NR_SHMEM);
356 __inc_zone_page_state(newpage, NR_SHMEM);
358 spin_unlock_irq(&mapping->tree_lock);
364 * The expected number of remaining references is the same as that
365 * of migrate_page_move_mapping().
367 int migrate_huge_page_move_mapping(struct address_space *mapping,
368 struct page *newpage, struct page *page)
374 if (page_count(page) != 1)
379 spin_lock_irq(&mapping->tree_lock);
381 pslot = radix_tree_lookup_slot(&mapping->page_tree,
384 expected_count = 2 + page_has_private(page);
385 if (page_count(page) != expected_count ||
386 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
387 spin_unlock_irq(&mapping->tree_lock);
391 if (!page_freeze_refs(page, expected_count)) {
392 spin_unlock_irq(&mapping->tree_lock);
398 radix_tree_replace_slot(pslot, newpage);
400 page_unfreeze_refs(page, expected_count);
404 spin_unlock_irq(&mapping->tree_lock);
409 * Copy the page to its new location
411 void migrate_page_copy(struct page *newpage, struct page *page)
414 copy_huge_page(newpage, page);
416 copy_highpage(newpage, page);
419 SetPageError(newpage);
420 if (PageReferenced(page))
421 SetPageReferenced(newpage);
422 if (PageUptodate(page))
423 SetPageUptodate(newpage);
424 if (TestClearPageActive(page)) {
425 VM_BUG_ON(PageUnevictable(page));
426 SetPageActive(newpage);
427 } else if (TestClearPageUnevictable(page))
428 SetPageUnevictable(newpage);
429 if (PageChecked(page))
430 SetPageChecked(newpage);
431 if (PageMappedToDisk(page))
432 SetPageMappedToDisk(newpage);
434 if (PageDirty(page)) {
435 clear_page_dirty_for_io(page);
437 * Want to mark the page and the radix tree as dirty, and
438 * redo the accounting that clear_page_dirty_for_io undid,
439 * but we can't use set_page_dirty because that function
440 * is actually a signal that all of the page has become dirty.
441 * Whereas only part of our page may be dirty.
443 __set_page_dirty_nobuffers(newpage);
446 mlock_migrate_page(newpage, page);
447 ksm_migrate_page(newpage, page);
449 ClearPageSwapCache(page);
450 ClearPagePrivate(page);
451 set_page_private(page, 0);
452 page->mapping = NULL;
455 * If any waiters have accumulated on the new page then
458 if (PageWriteback(newpage))
459 end_page_writeback(newpage);
462 /************************************************************
463 * Migration functions
464 ***********************************************************/
466 /* Always fail migration. Used for mappings that are not movable */
467 int fail_migrate_page(struct address_space *mapping,
468 struct page *newpage, struct page *page)
472 EXPORT_SYMBOL(fail_migrate_page);
475 * Common logic to directly migrate a single page suitable for
476 * pages that do not use PagePrivate/PagePrivate2.
478 * Pages are locked upon entry and exit.
480 int migrate_page(struct address_space *mapping,
481 struct page *newpage, struct page *page, bool sync)
485 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
487 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, sync);
492 migrate_page_copy(newpage, page);
495 EXPORT_SYMBOL(migrate_page);
499 * Migration function for pages with buffers. This function can only be used
500 * if the underlying filesystem guarantees that no other references to "page"
503 int buffer_migrate_page(struct address_space *mapping,
504 struct page *newpage, struct page *page, bool sync)
506 struct buffer_head *bh, *head;
509 if (!page_has_buffers(page))
510 return migrate_page(mapping, newpage, page, sync);
512 head = page_buffers(page);
514 rc = migrate_page_move_mapping(mapping, newpage, page, head, sync);
520 * In the async case, migrate_page_move_mapping locked the buffers
521 * with an IRQ-safe spinlock held. In the sync case, the buffers
522 * need to be locked now
525 BUG_ON(!buffer_migrate_lock_buffers(head, sync));
527 ClearPagePrivate(page);
528 set_page_private(newpage, page_private(page));
529 set_page_private(page, 0);
535 set_bh_page(bh, newpage, bh_offset(bh));
536 bh = bh->b_this_page;
538 } while (bh != head);
540 SetPagePrivate(newpage);
542 migrate_page_copy(newpage, page);
548 bh = bh->b_this_page;
550 } while (bh != head);
554 EXPORT_SYMBOL(buffer_migrate_page);
558 * Writeback a page to clean the dirty state
560 static int writeout(struct address_space *mapping, struct page *page)
562 struct writeback_control wbc = {
563 .sync_mode = WB_SYNC_NONE,
566 .range_end = LLONG_MAX,
571 if (!mapping->a_ops->writepage)
572 /* No write method for the address space */
575 if (!clear_page_dirty_for_io(page))
576 /* Someone else already triggered a write */
580 * A dirty page may imply that the underlying filesystem has
581 * the page on some queue. So the page must be clean for
582 * migration. Writeout may mean we loose the lock and the
583 * page state is no longer what we checked for earlier.
584 * At this point we know that the migration attempt cannot
587 remove_migration_ptes(page, page);
589 rc = mapping->a_ops->writepage(page, &wbc);
591 if (rc != AOP_WRITEPAGE_ACTIVATE)
592 /* unlocked. Relock */
595 return (rc < 0) ? -EIO : -EAGAIN;
599 * Default handling if a filesystem does not provide a migration function.
601 static int fallback_migrate_page(struct address_space *mapping,
602 struct page *newpage, struct page *page, bool sync)
604 if (PageDirty(page)) {
607 return writeout(mapping, page);
611 * Buffers may be managed in a filesystem specific way.
612 * We must have no buffers or drop them.
614 if (page_has_private(page) &&
615 !try_to_release_page(page, GFP_KERNEL))
618 return migrate_page(mapping, newpage, page, sync);
622 * Move a page to a newly allocated page
623 * The page is locked and all ptes have been successfully removed.
625 * The new page will have replaced the old page if this function
632 static int move_to_new_page(struct page *newpage, struct page *page,
633 int remap_swapcache, bool sync)
635 struct address_space *mapping;
639 * Block others from accessing the page when we get around to
640 * establishing additional references. We are the only one
641 * holding a reference to the new page at this point.
643 if (!trylock_page(newpage))
646 /* Prepare mapping for the new page.*/
647 newpage->index = page->index;
648 newpage->mapping = page->mapping;
649 if (PageSwapBacked(page))
650 SetPageSwapBacked(newpage);
652 mapping = page_mapping(page);
654 rc = migrate_page(mapping, newpage, page, sync);
655 else if (mapping->a_ops->migratepage)
657 * Most pages have a mapping and most filesystems provide a
658 * migratepage callback. Anonymous pages are part of swap
659 * space which also has its own migratepage callback. This
660 * is the most common path for page migration.
662 rc = mapping->a_ops->migratepage(mapping,
663 newpage, page, sync);
665 rc = fallback_migrate_page(mapping, newpage, page, sync);
668 newpage->mapping = NULL;
671 remove_migration_ptes(page, newpage);
674 unlock_page(newpage);
679 static int __unmap_and_move(struct page *page, struct page *newpage,
680 int force, bool offlining, bool sync)
683 int remap_swapcache = 1;
685 struct mem_cgroup *mem;
686 struct anon_vma *anon_vma = NULL;
688 if (!trylock_page(page)) {
693 * It's not safe for direct compaction to call lock_page.
694 * For example, during page readahead pages are added locked
695 * to the LRU. Later, when the IO completes the pages are
696 * marked uptodate and unlocked. However, the queueing
697 * could be merging multiple pages for one bio (e.g.
698 * mpage_readpages). If an allocation happens for the
699 * second or third page, the process can end up locking
700 * the same page twice and deadlocking. Rather than
701 * trying to be clever about what pages can be locked,
702 * avoid the use of lock_page for direct compaction
705 if (current->flags & PF_MEMALLOC)
712 * Only memory hotplug's offline_pages() caller has locked out KSM,
713 * and can safely migrate a KSM page. The other cases have skipped
714 * PageKsm along with PageReserved - but it is only now when we have
715 * the page lock that we can be certain it will not go KSM beneath us
716 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
717 * its pagecount raised, but only here do we take the page lock which
720 if (PageKsm(page) && !offlining) {
725 /* charge against new page */
726 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
727 if (charge == -ENOMEM) {
733 if (PageWriteback(page)) {
735 * For !sync, there is no point retrying as the retry loop
736 * is expected to be too short for PageWriteback to be cleared
744 wait_on_page_writeback(page);
747 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
748 * we cannot notice that anon_vma is freed while we migrates a page.
749 * This get_anon_vma() delays freeing anon_vma pointer until the end
750 * of migration. File cache pages are no problem because of page_lock()
751 * File Caches may use write_page() or lock_page() in migration, then,
752 * just care Anon page here.
754 if (PageAnon(page)) {
756 * Only page_lock_anon_vma() understands the subtleties of
757 * getting a hold on an anon_vma from outside one of its mms.
759 anon_vma = page_get_anon_vma(page);
764 } else if (PageSwapCache(page)) {
766 * We cannot be sure that the anon_vma of an unmapped
767 * swapcache page is safe to use because we don't
768 * know in advance if the VMA that this page belonged
769 * to still exists. If the VMA and others sharing the
770 * data have been freed, then the anon_vma could
771 * already be invalid.
773 * To avoid this possibility, swapcache pages get
774 * migrated but are not remapped when migration
784 * Corner case handling:
785 * 1. When a new swap-cache page is read into, it is added to the LRU
786 * and treated as swapcache but it has no rmap yet.
787 * Calling try_to_unmap() against a page->mapping==NULL page will
788 * trigger a BUG. So handle it here.
789 * 2. An orphaned page (see truncate_complete_page) might have
790 * fs-private metadata. The page can be picked up due to memory
791 * offlining. Everywhere else except page reclaim, the page is
792 * invisible to the vm, so the page can not be migrated. So try to
793 * free the metadata, so the page can be freed.
795 if (!page->mapping) {
796 VM_BUG_ON(PageAnon(page));
797 if (page_has_private(page)) {
798 try_to_free_buffers(page);
804 /* Establish migration ptes or remove ptes */
805 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
808 if (!page_mapped(page))
809 rc = move_to_new_page(newpage, page, remap_swapcache, sync);
811 if (rc && remap_swapcache)
812 remove_migration_ptes(page, page);
814 /* Drop an anon_vma reference if we took one */
816 put_anon_vma(anon_vma);
820 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
828 * Obtain the lock on page, remove all ptes and migrate the page
829 * to the newly allocated page in newpage.
831 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
832 struct page *page, int force, bool offlining, bool sync)
836 struct page *newpage = get_new_page(page, private, &result);
841 if (page_count(page) == 1) {
842 /* page was freed from under us. So we are done. */
846 if (unlikely(PageTransHuge(page)))
847 if (unlikely(split_huge_page(page)))
850 rc = __unmap_and_move(page, newpage, force, offlining, sync);
854 * A page that has been migrated has all references
855 * removed and will be freed. A page that has not been
856 * migrated will have kepts its references and be
859 list_del(&page->lru);
860 dec_zone_page_state(page, NR_ISOLATED_ANON +
861 page_is_file_cache(page));
862 putback_lru_page(page);
865 * Move the new page to the LRU. If migration was not successful
866 * then this will free the page.
868 putback_lru_page(newpage);
873 *result = page_to_nid(newpage);
879 * Counterpart of unmap_and_move_page() for hugepage migration.
881 * This function doesn't wait the completion of hugepage I/O
882 * because there is no race between I/O and migration for hugepage.
883 * Note that currently hugepage I/O occurs only in direct I/O
884 * where no lock is held and PG_writeback is irrelevant,
885 * and writeback status of all subpages are counted in the reference
886 * count of the head page (i.e. if all subpages of a 2MB hugepage are
887 * under direct I/O, the reference of the head page is 512 and a bit more.)
888 * This means that when we try to migrate hugepage whose subpages are
889 * doing direct I/O, some references remain after try_to_unmap() and
890 * hugepage migration fails without data corruption.
892 * There is also no race when direct I/O is issued on the page under migration,
893 * because then pte is replaced with migration swap entry and direct I/O code
894 * will wait in the page fault for migration to complete.
896 static int unmap_and_move_huge_page(new_page_t get_new_page,
897 unsigned long private, struct page *hpage,
898 int force, bool offlining, bool sync)
902 struct page *new_hpage = get_new_page(hpage, private, &result);
903 struct anon_vma *anon_vma = NULL;
910 if (!trylock_page(hpage)) {
917 anon_vma = page_get_anon_vma(hpage);
919 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
921 if (!page_mapped(hpage))
922 rc = move_to_new_page(new_hpage, hpage, 1, sync);
925 remove_migration_ptes(hpage, hpage);
928 put_anon_vma(anon_vma);
933 list_del(&hpage->lru);
943 *result = page_to_nid(new_hpage);
951 * The function takes one list of pages to migrate and a function
952 * that determines from the page to be migrated and the private data
953 * the target of the move and allocates the page.
955 * The function returns after 10 attempts or if no pages
956 * are movable anymore because to has become empty
957 * or no retryable pages exist anymore.
958 * Caller should call putback_lru_pages to return pages to the LRU
959 * or free list only if ret != 0.
961 * Return: Number of pages not migrated or error code.
963 int migrate_pages(struct list_head *from,
964 new_page_t get_new_page, unsigned long private, bool offlining,
972 int swapwrite = current->flags & PF_SWAPWRITE;
976 current->flags |= PF_SWAPWRITE;
978 for(pass = 0; pass < 10 && retry; pass++) {
981 list_for_each_entry_safe(page, page2, from, lru) {
984 rc = unmap_and_move(get_new_page, private,
985 page, pass > 2, offlining,
997 /* Permanent failure */
1006 current->flags &= ~PF_SWAPWRITE;
1011 return nr_failed + retry;
1014 int migrate_huge_pages(struct list_head *from,
1015 new_page_t get_new_page, unsigned long private, bool offlining,
1025 for (pass = 0; pass < 10 && retry; pass++) {
1028 list_for_each_entry_safe(page, page2, from, lru) {
1031 rc = unmap_and_move_huge_page(get_new_page,
1032 private, page, pass > 2, offlining,
1044 /* Permanent failure */
1055 return nr_failed + retry;
1060 * Move a list of individual pages
1062 struct page_to_node {
1069 static struct page *new_page_node(struct page *p, unsigned long private,
1072 struct page_to_node *pm = (struct page_to_node *)private;
1074 while (pm->node != MAX_NUMNODES && pm->page != p)
1077 if (pm->node == MAX_NUMNODES)
1080 *result = &pm->status;
1082 return alloc_pages_exact_node(pm->node,
1083 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
1087 * Move a set of pages as indicated in the pm array. The addr
1088 * field must be set to the virtual address of the page to be moved
1089 * and the node number must contain a valid target node.
1090 * The pm array ends with node = MAX_NUMNODES.
1092 static int do_move_page_to_node_array(struct mm_struct *mm,
1093 struct page_to_node *pm,
1097 struct page_to_node *pp;
1098 LIST_HEAD(pagelist);
1100 down_read(&mm->mmap_sem);
1103 * Build a list of pages to migrate
1105 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1106 struct vm_area_struct *vma;
1110 vma = find_vma(mm, pp->addr);
1111 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
1114 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
1116 err = PTR_ERR(page);
1124 /* Use PageReserved to check for zero page */
1125 if (PageReserved(page) || PageKsm(page))
1129 err = page_to_nid(page);
1131 if (err == pp->node)
1133 * Node already in the right place
1138 if (page_mapcount(page) > 1 &&
1142 err = isolate_lru_page(page);
1144 list_add_tail(&page->lru, &pagelist);
1145 inc_zone_page_state(page, NR_ISOLATED_ANON +
1146 page_is_file_cache(page));
1150 * Either remove the duplicate refcount from
1151 * isolate_lru_page() or drop the page ref if it was
1160 if (!list_empty(&pagelist)) {
1161 err = migrate_pages(&pagelist, new_page_node,
1162 (unsigned long)pm, 0, true);
1164 putback_lru_pages(&pagelist);
1167 up_read(&mm->mmap_sem);
1172 * Migrate an array of page address onto an array of nodes and fill
1173 * the corresponding array of status.
1175 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
1176 unsigned long nr_pages,
1177 const void __user * __user *pages,
1178 const int __user *nodes,
1179 int __user *status, int flags)
1181 struct page_to_node *pm;
1182 nodemask_t task_nodes;
1183 unsigned long chunk_nr_pages;
1184 unsigned long chunk_start;
1187 task_nodes = cpuset_mems_allowed(task);
1190 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1197 * Store a chunk of page_to_node array in a page,
1198 * but keep the last one as a marker
1200 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
1202 for (chunk_start = 0;
1203 chunk_start < nr_pages;
1204 chunk_start += chunk_nr_pages) {
1207 if (chunk_start + chunk_nr_pages > nr_pages)
1208 chunk_nr_pages = nr_pages - chunk_start;
1210 /* fill the chunk pm with addrs and nodes from user-space */
1211 for (j = 0; j < chunk_nr_pages; j++) {
1212 const void __user *p;
1216 if (get_user(p, pages + j + chunk_start))
1218 pm[j].addr = (unsigned long) p;
1220 if (get_user(node, nodes + j + chunk_start))
1224 if (node < 0 || node >= MAX_NUMNODES)
1227 if (!node_state(node, N_HIGH_MEMORY))
1231 if (!node_isset(node, task_nodes))
1237 /* End marker for this chunk */
1238 pm[chunk_nr_pages].node = MAX_NUMNODES;
1240 /* Migrate this chunk */
1241 err = do_move_page_to_node_array(mm, pm,
1242 flags & MPOL_MF_MOVE_ALL);
1246 /* Return status information */
1247 for (j = 0; j < chunk_nr_pages; j++)
1248 if (put_user(pm[j].status, status + j + chunk_start)) {
1256 free_page((unsigned long)pm);
1262 * Determine the nodes of an array of pages and store it in an array of status.
1264 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1265 const void __user **pages, int *status)
1269 down_read(&mm->mmap_sem);
1271 for (i = 0; i < nr_pages; i++) {
1272 unsigned long addr = (unsigned long)(*pages);
1273 struct vm_area_struct *vma;
1277 vma = find_vma(mm, addr);
1278 if (!vma || addr < vma->vm_start)
1281 page = follow_page(vma, addr, 0);
1283 err = PTR_ERR(page);
1288 /* Use PageReserved to check for zero page */
1289 if (!page || PageReserved(page) || PageKsm(page))
1292 err = page_to_nid(page);
1300 up_read(&mm->mmap_sem);
1304 * Determine the nodes of a user array of pages and store it in
1305 * a user array of status.
1307 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1308 const void __user * __user *pages,
1311 #define DO_PAGES_STAT_CHUNK_NR 16
1312 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1313 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1316 unsigned long chunk_nr;
1318 chunk_nr = nr_pages;
1319 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1320 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1322 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1325 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1327 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1332 nr_pages -= chunk_nr;
1334 return nr_pages ? -EFAULT : 0;
1338 * Move a list of pages in the address space of the currently executing
1341 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1342 const void __user * __user *, pages,
1343 const int __user *, nodes,
1344 int __user *, status, int, flags)
1346 const struct cred *cred = current_cred(), *tcred;
1347 struct task_struct *task;
1348 struct mm_struct *mm;
1352 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1355 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1358 /* Find the mm_struct */
1360 task = pid ? find_task_by_vpid(pid) : current;
1365 mm = get_task_mm(task);
1372 * Check if this process has the right to modify the specified
1373 * process. The right exists if the process has administrative
1374 * capabilities, superuser privileges or the same
1375 * userid as the target process.
1378 tcred = __task_cred(task);
1379 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1380 cred->uid != tcred->suid && cred->uid != tcred->uid &&
1381 !capable(CAP_SYS_NICE)) {
1388 err = security_task_movememory(task);
1393 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1396 err = do_pages_stat(mm, nr_pages, pages, status);
1405 * Call migration functions in the vma_ops that may prepare
1406 * memory in a vm for migration. migration functions may perform
1407 * the migration for vmas that do not have an underlying page struct.
1409 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1410 const nodemask_t *from, unsigned long flags)
1412 struct vm_area_struct *vma;
1415 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1416 if (vma->vm_ops && vma->vm_ops->migrate) {
1417 err = vma->vm_ops->migrate(vma, to, from, flags);