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/module.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/gfp.h>
39 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
42 * migrate_prep() needs to be called before we start compiling a list of pages
43 * to be migrated using isolate_lru_page().
45 int migrate_prep(void)
48 * Clear the LRU lists so pages can be isolated.
49 * Note that pages may be moved off the LRU after we have
50 * drained them. Those pages will fail to migrate like other
51 * pages that may be busy.
59 * Add isolated pages on the list back to the LRU under page lock
60 * to avoid leaking evictable pages back onto unevictable list.
62 * returns the number of pages put back.
64 int putback_lru_pages(struct list_head *l)
70 list_for_each_entry_safe(page, page2, l, lru) {
72 dec_zone_page_state(page, NR_ISOLATED_ANON +
73 page_is_file_cache(page));
74 putback_lru_page(page);
81 * Restore a potential migration pte to a working pte entry
83 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
84 unsigned long addr, void *old)
86 struct mm_struct *mm = vma->vm_mm;
94 pgd = pgd_offset(mm, addr);
95 if (!pgd_present(*pgd))
98 pud = pud_offset(pgd, addr);
99 if (!pud_present(*pud))
102 pmd = pmd_offset(pud, addr);
103 if (!pmd_present(*pmd))
106 ptep = pte_offset_map(pmd, addr);
108 if (!is_swap_pte(*ptep)) {
113 ptl = pte_lockptr(mm, pmd);
116 if (!is_swap_pte(pte))
119 entry = pte_to_swp_entry(pte);
121 if (!is_migration_entry(entry) ||
122 migration_entry_to_page(entry) != old)
126 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
127 if (is_write_migration_entry(entry))
128 pte = pte_mkwrite(pte);
129 flush_cache_page(vma, addr, pte_pfn(pte));
130 set_pte_at(mm, addr, ptep, pte);
133 page_add_anon_rmap(new, vma, addr);
135 page_add_file_rmap(new);
137 /* No need to invalidate - it was non-present before */
138 update_mmu_cache(vma, addr, ptep);
140 pte_unmap_unlock(ptep, ptl);
146 * Get rid of all migration entries and replace them by
147 * references to the indicated page.
149 static void remove_migration_ptes(struct page *old, struct page *new)
151 rmap_walk(new, remove_migration_pte, old);
155 * Something used the pte of a page under migration. We need to
156 * get to the page and wait until migration is finished.
157 * When we return from this function the fault will be retried.
159 * This function is called from do_swap_page().
161 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
162 unsigned long address)
169 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
171 if (!is_swap_pte(pte))
174 entry = pte_to_swp_entry(pte);
175 if (!is_migration_entry(entry))
178 page = migration_entry_to_page(entry);
181 * Once radix-tree replacement of page migration started, page_count
182 * *must* be zero. And, we don't want to call wait_on_page_locked()
183 * against a page without get_page().
184 * So, we use get_page_unless_zero(), here. Even failed, page fault
187 if (!get_page_unless_zero(page))
189 pte_unmap_unlock(ptep, ptl);
190 wait_on_page_locked(page);
194 pte_unmap_unlock(ptep, ptl);
198 * Replace the page in the mapping.
200 * The number of remaining references must be:
201 * 1 for anonymous pages without a mapping
202 * 2 for pages with a mapping
203 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
205 static int migrate_page_move_mapping(struct address_space *mapping,
206 struct page *newpage, struct page *page)
212 /* Anonymous page without mapping */
213 if (page_count(page) != 1)
218 spin_lock_irq(&mapping->tree_lock);
220 pslot = radix_tree_lookup_slot(&mapping->page_tree,
223 expected_count = 2 + page_has_private(page);
224 if (page_count(page) != expected_count ||
225 (struct page *)radix_tree_deref_slot(pslot) != page) {
226 spin_unlock_irq(&mapping->tree_lock);
230 if (!page_freeze_refs(page, expected_count)) {
231 spin_unlock_irq(&mapping->tree_lock);
236 * Now we know that no one else is looking at the page.
238 get_page(newpage); /* add cache reference */
239 if (PageSwapCache(page)) {
240 SetPageSwapCache(newpage);
241 set_page_private(newpage, page_private(page));
244 radix_tree_replace_slot(pslot, newpage);
246 page_unfreeze_refs(page, expected_count);
248 * Drop cache reference from old page.
249 * We know this isn't the last reference.
254 * If moved to a different zone then also account
255 * the page for that zone. Other VM counters will be
256 * taken care of when we establish references to the
257 * new page and drop references to the old page.
259 * Note that anonymous pages are accounted for
260 * via NR_FILE_PAGES and NR_ANON_PAGES if they
261 * are mapped to swap space.
263 __dec_zone_page_state(page, NR_FILE_PAGES);
264 __inc_zone_page_state(newpage, NR_FILE_PAGES);
265 if (PageSwapBacked(page)) {
266 __dec_zone_page_state(page, NR_SHMEM);
267 __inc_zone_page_state(newpage, NR_SHMEM);
269 spin_unlock_irq(&mapping->tree_lock);
275 * Copy the page to its new location
277 static void migrate_page_copy(struct page *newpage, struct page *page)
279 copy_highpage(newpage, page);
282 SetPageError(newpage);
283 if (PageReferenced(page))
284 SetPageReferenced(newpage);
285 if (PageUptodate(page))
286 SetPageUptodate(newpage);
287 if (TestClearPageActive(page)) {
288 VM_BUG_ON(PageUnevictable(page));
289 SetPageActive(newpage);
290 } else if (TestClearPageUnevictable(page))
291 SetPageUnevictable(newpage);
292 if (PageChecked(page))
293 SetPageChecked(newpage);
294 if (PageMappedToDisk(page))
295 SetPageMappedToDisk(newpage);
297 if (PageDirty(page)) {
298 clear_page_dirty_for_io(page);
300 * Want to mark the page and the radix tree as dirty, and
301 * redo the accounting that clear_page_dirty_for_io undid,
302 * but we can't use set_page_dirty because that function
303 * is actually a signal that all of the page has become dirty.
304 * Wheras only part of our page may be dirty.
306 __set_page_dirty_nobuffers(newpage);
309 mlock_migrate_page(newpage, page);
310 ksm_migrate_page(newpage, page);
312 ClearPageSwapCache(page);
313 ClearPagePrivate(page);
314 set_page_private(page, 0);
315 page->mapping = NULL;
318 * If any waiters have accumulated on the new page then
321 if (PageWriteback(newpage))
322 end_page_writeback(newpage);
325 /************************************************************
326 * Migration functions
327 ***********************************************************/
329 /* Always fail migration. Used for mappings that are not movable */
330 int fail_migrate_page(struct address_space *mapping,
331 struct page *newpage, struct page *page)
335 EXPORT_SYMBOL(fail_migrate_page);
338 * Common logic to directly migrate a single page suitable for
339 * pages that do not use PagePrivate/PagePrivate2.
341 * Pages are locked upon entry and exit.
343 int migrate_page(struct address_space *mapping,
344 struct page *newpage, struct page *page)
348 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
350 rc = migrate_page_move_mapping(mapping, newpage, page);
355 migrate_page_copy(newpage, page);
358 EXPORT_SYMBOL(migrate_page);
362 * Migration function for pages with buffers. This function can only be used
363 * if the underlying filesystem guarantees that no other references to "page"
366 int buffer_migrate_page(struct address_space *mapping,
367 struct page *newpage, struct page *page)
369 struct buffer_head *bh, *head;
372 if (!page_has_buffers(page))
373 return migrate_page(mapping, newpage, page);
375 head = page_buffers(page);
377 rc = migrate_page_move_mapping(mapping, newpage, page);
386 bh = bh->b_this_page;
388 } while (bh != head);
390 ClearPagePrivate(page);
391 set_page_private(newpage, page_private(page));
392 set_page_private(page, 0);
398 set_bh_page(bh, newpage, bh_offset(bh));
399 bh = bh->b_this_page;
401 } while (bh != head);
403 SetPagePrivate(newpage);
405 migrate_page_copy(newpage, page);
411 bh = bh->b_this_page;
413 } while (bh != head);
417 EXPORT_SYMBOL(buffer_migrate_page);
421 * Writeback a page to clean the dirty state
423 static int writeout(struct address_space *mapping, struct page *page)
425 struct writeback_control wbc = {
426 .sync_mode = WB_SYNC_NONE,
429 .range_end = LLONG_MAX,
435 if (!mapping->a_ops->writepage)
436 /* No write method for the address space */
439 if (!clear_page_dirty_for_io(page))
440 /* Someone else already triggered a write */
444 * A dirty page may imply that the underlying filesystem has
445 * the page on some queue. So the page must be clean for
446 * migration. Writeout may mean we loose the lock and the
447 * page state is no longer what we checked for earlier.
448 * At this point we know that the migration attempt cannot
451 remove_migration_ptes(page, page);
453 rc = mapping->a_ops->writepage(page, &wbc);
455 if (rc != AOP_WRITEPAGE_ACTIVATE)
456 /* unlocked. Relock */
459 return (rc < 0) ? -EIO : -EAGAIN;
463 * Default handling if a filesystem does not provide a migration function.
465 static int fallback_migrate_page(struct address_space *mapping,
466 struct page *newpage, struct page *page)
469 return writeout(mapping, page);
472 * Buffers may be managed in a filesystem specific way.
473 * We must have no buffers or drop them.
475 if (page_has_private(page) &&
476 !try_to_release_page(page, GFP_KERNEL))
479 return migrate_page(mapping, newpage, page);
483 * Move a page to a newly allocated page
484 * The page is locked and all ptes have been successfully removed.
486 * The new page will have replaced the old page if this function
493 static int move_to_new_page(struct page *newpage, struct page *page)
495 struct address_space *mapping;
499 * Block others from accessing the page when we get around to
500 * establishing additional references. We are the only one
501 * holding a reference to the new page at this point.
503 if (!trylock_page(newpage))
506 /* Prepare mapping for the new page.*/
507 newpage->index = page->index;
508 newpage->mapping = page->mapping;
509 if (PageSwapBacked(page))
510 SetPageSwapBacked(newpage);
512 mapping = page_mapping(page);
514 rc = migrate_page(mapping, newpage, page);
515 else if (mapping->a_ops->migratepage)
517 * Most pages have a mapping and most filesystems
518 * should provide a migration function. Anonymous
519 * pages are part of swap space which also has its
520 * own migration function. This is the most common
521 * path for page migration.
523 rc = mapping->a_ops->migratepage(mapping,
526 rc = fallback_migrate_page(mapping, newpage, page);
529 remove_migration_ptes(page, newpage);
531 newpage->mapping = NULL;
533 unlock_page(newpage);
539 * Obtain the lock on page, remove all ptes and migrate the page
540 * to the newly allocated page in newpage.
542 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
543 struct page *page, int force, int offlining)
547 struct page *newpage = get_new_page(page, private, &result);
550 struct mem_cgroup *mem = NULL;
555 if (page_count(page) == 1) {
556 /* page was freed from under us. So we are done. */
560 /* prepare cgroup just returns 0 or -ENOMEM */
563 if (!trylock_page(page)) {
570 * Only memory hotplug's offline_pages() caller has locked out KSM,
571 * and can safely migrate a KSM page. The other cases have skipped
572 * PageKsm along with PageReserved - but it is only now when we have
573 * the page lock that we can be certain it will not go KSM beneath us
574 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
575 * its pagecount raised, but only here do we take the page lock which
578 if (PageKsm(page) && !offlining) {
583 /* charge against new page */
584 charge = mem_cgroup_prepare_migration(page, &mem);
585 if (charge == -ENOMEM) {
591 if (PageWriteback(page)) {
594 wait_on_page_writeback(page);
597 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
598 * we cannot notice that anon_vma is freed while we migrates a page.
599 * This rcu_read_lock() delays freeing anon_vma pointer until the end
600 * of migration. File cache pages are no problem because of page_lock()
601 * File Caches may use write_page() or lock_page() in migration, then,
602 * just care Anon page here.
604 if (PageAnon(page)) {
610 * Corner case handling:
611 * 1. When a new swap-cache page is read into, it is added to the LRU
612 * and treated as swapcache but it has no rmap yet.
613 * Calling try_to_unmap() against a page->mapping==NULL page will
614 * trigger a BUG. So handle it here.
615 * 2. An orphaned page (see truncate_complete_page) might have
616 * fs-private metadata. The page can be picked up due to memory
617 * offlining. Everywhere else except page reclaim, the page is
618 * invisible to the vm, so the page can not be migrated. So try to
619 * free the metadata, so the page can be freed.
621 if (!page->mapping) {
622 if (!PageAnon(page) && page_has_private(page)) {
624 * Go direct to try_to_free_buffers() here because
625 * a) that's what try_to_release_page() would do anyway
626 * b) we may be under rcu_read_lock() here, so we can't
627 * use GFP_KERNEL which is what try_to_release_page()
628 * needs to be effective.
630 try_to_free_buffers(page);
636 /* Establish migration ptes or remove ptes */
637 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
640 if (!page_mapped(page))
641 rc = move_to_new_page(newpage, page);
644 remove_migration_ptes(page, page);
650 mem_cgroup_end_migration(mem, page, newpage);
656 * A page that has been migrated has all references
657 * removed and will be freed. A page that has not been
658 * migrated will have kepts its references and be
661 list_del(&page->lru);
662 dec_zone_page_state(page, NR_ISOLATED_ANON +
663 page_is_file_cache(page));
664 putback_lru_page(page);
670 * Move the new page to the LRU. If migration was not successful
671 * then this will free the page.
673 putback_lru_page(newpage);
679 *result = page_to_nid(newpage);
687 * The function takes one list of pages to migrate and a function
688 * that determines from the page to be migrated and the private data
689 * the target of the move and allocates the page.
691 * The function returns after 10 attempts or if no pages
692 * are movable anymore because to has become empty
693 * or no retryable pages exist anymore. All pages will be
694 * returned to the LRU or freed.
696 * Return: Number of pages not migrated or error code.
698 int migrate_pages(struct list_head *from,
699 new_page_t get_new_page, unsigned long private, int offlining)
706 int swapwrite = current->flags & PF_SWAPWRITE;
710 current->flags |= PF_SWAPWRITE;
712 for(pass = 0; pass < 10 && retry; pass++) {
715 list_for_each_entry_safe(page, page2, from, lru) {
718 rc = unmap_and_move(get_new_page, private,
719 page, pass > 2, offlining);
730 /* Permanent failure */
739 current->flags &= ~PF_SWAPWRITE;
741 putback_lru_pages(from);
746 return nr_failed + retry;
751 * Move a list of individual pages
753 struct page_to_node {
760 static struct page *new_page_node(struct page *p, unsigned long private,
763 struct page_to_node *pm = (struct page_to_node *)private;
765 while (pm->node != MAX_NUMNODES && pm->page != p)
768 if (pm->node == MAX_NUMNODES)
771 *result = &pm->status;
773 return alloc_pages_exact_node(pm->node,
774 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
778 * Move a set of pages as indicated in the pm array. The addr
779 * field must be set to the virtual address of the page to be moved
780 * and the node number must contain a valid target node.
781 * The pm array ends with node = MAX_NUMNODES.
783 static int do_move_page_to_node_array(struct mm_struct *mm,
784 struct page_to_node *pm,
788 struct page_to_node *pp;
791 down_read(&mm->mmap_sem);
794 * Build a list of pages to migrate
796 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
797 struct vm_area_struct *vma;
801 vma = find_vma(mm, pp->addr);
802 if (!vma || !vma_migratable(vma))
805 page = follow_page(vma, pp->addr, FOLL_GET);
815 /* Use PageReserved to check for zero page */
816 if (PageReserved(page) || PageKsm(page))
820 err = page_to_nid(page);
824 * Node already in the right place
829 if (page_mapcount(page) > 1 &&
833 err = isolate_lru_page(page);
835 list_add_tail(&page->lru, &pagelist);
836 inc_zone_page_state(page, NR_ISOLATED_ANON +
837 page_is_file_cache(page));
841 * Either remove the duplicate refcount from
842 * isolate_lru_page() or drop the page ref if it was
851 if (!list_empty(&pagelist))
852 err = migrate_pages(&pagelist, new_page_node,
853 (unsigned long)pm, 0);
855 up_read(&mm->mmap_sem);
860 * Migrate an array of page address onto an array of nodes and fill
861 * the corresponding array of status.
863 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
864 unsigned long nr_pages,
865 const void __user * __user *pages,
866 const int __user *nodes,
867 int __user *status, int flags)
869 struct page_to_node *pm;
870 nodemask_t task_nodes;
871 unsigned long chunk_nr_pages;
872 unsigned long chunk_start;
875 task_nodes = cpuset_mems_allowed(task);
878 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
885 * Store a chunk of page_to_node array in a page,
886 * but keep the last one as a marker
888 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
890 for (chunk_start = 0;
891 chunk_start < nr_pages;
892 chunk_start += chunk_nr_pages) {
895 if (chunk_start + chunk_nr_pages > nr_pages)
896 chunk_nr_pages = nr_pages - chunk_start;
898 /* fill the chunk pm with addrs and nodes from user-space */
899 for (j = 0; j < chunk_nr_pages; j++) {
900 const void __user *p;
904 if (get_user(p, pages + j + chunk_start))
906 pm[j].addr = (unsigned long) p;
908 if (get_user(node, nodes + j + chunk_start))
912 if (node < 0 || node >= MAX_NUMNODES)
915 if (!node_state(node, N_HIGH_MEMORY))
919 if (!node_isset(node, task_nodes))
925 /* End marker for this chunk */
926 pm[chunk_nr_pages].node = MAX_NUMNODES;
928 /* Migrate this chunk */
929 err = do_move_page_to_node_array(mm, pm,
930 flags & MPOL_MF_MOVE_ALL);
934 /* Return status information */
935 for (j = 0; j < chunk_nr_pages; j++)
936 if (put_user(pm[j].status, status + j + chunk_start)) {
944 free_page((unsigned long)pm);
950 * Determine the nodes of an array of pages and store it in an array of status.
952 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
953 const void __user **pages, int *status)
957 down_read(&mm->mmap_sem);
959 for (i = 0; i < nr_pages; i++) {
960 unsigned long addr = (unsigned long)(*pages);
961 struct vm_area_struct *vma;
965 vma = find_vma(mm, addr);
969 page = follow_page(vma, addr, 0);
976 /* Use PageReserved to check for zero page */
977 if (!page || PageReserved(page) || PageKsm(page))
980 err = page_to_nid(page);
988 up_read(&mm->mmap_sem);
992 * Determine the nodes of a user array of pages and store it in
993 * a user array of status.
995 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
996 const void __user * __user *pages,
999 #define DO_PAGES_STAT_CHUNK_NR 16
1000 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1001 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1004 unsigned long chunk_nr;
1006 chunk_nr = nr_pages;
1007 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1008 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1010 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1013 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1015 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1020 nr_pages -= chunk_nr;
1022 return nr_pages ? -EFAULT : 0;
1026 * Move a list of pages in the address space of the currently executing
1029 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1030 const void __user * __user *, pages,
1031 const int __user *, nodes,
1032 int __user *, status, int, flags)
1034 const struct cred *cred = current_cred(), *tcred;
1035 struct task_struct *task;
1036 struct mm_struct *mm;
1040 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1043 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1046 /* Find the mm_struct */
1047 read_lock(&tasklist_lock);
1048 task = pid ? find_task_by_vpid(pid) : current;
1050 read_unlock(&tasklist_lock);
1053 mm = get_task_mm(task);
1054 read_unlock(&tasklist_lock);
1060 * Check if this process has the right to modify the specified
1061 * process. The right exists if the process has administrative
1062 * capabilities, superuser privileges or the same
1063 * userid as the target process.
1066 tcred = __task_cred(task);
1067 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1068 cred->uid != tcred->suid && cred->uid != tcred->uid &&
1069 !capable(CAP_SYS_NICE)) {
1076 err = security_task_movememory(task);
1081 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1084 err = do_pages_stat(mm, nr_pages, pages, status);
1093 * Call migration functions in the vma_ops that may prepare
1094 * memory in a vm for migration. migration functions may perform
1095 * the migration for vmas that do not have an underlying page struct.
1097 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1098 const nodemask_t *from, unsigned long flags)
1100 struct vm_area_struct *vma;
1103 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1104 if (vma->vm_ops && vma->vm_ops->migrate) {
1105 err = vma->vm_ops->migrate(vma, to, from, flags);