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>
38 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
41 * migrate_prep() needs to be called before we start compiling a list of pages
42 * to be migrated using isolate_lru_page().
44 int migrate_prep(void)
47 * Clear the LRU lists so pages can be isolated.
48 * Note that pages may be moved off the LRU after we have
49 * drained them. Those pages will fail to migrate like other
50 * pages that may be busy.
58 * Add isolated pages on the list back to the LRU under page lock
59 * to avoid leaking evictable pages back onto unevictable list.
61 * returns the number of pages put back.
63 int putback_lru_pages(struct list_head *l)
69 list_for_each_entry_safe(page, page2, l, lru) {
71 dec_zone_page_state(page, NR_ISOLATED_ANON +
72 page_is_file_cache(page));
73 putback_lru_page(page);
80 * Restore a potential migration pte to a working pte entry
82 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
83 unsigned long addr, void *old)
85 struct mm_struct *mm = vma->vm_mm;
93 pgd = pgd_offset(mm, addr);
94 if (!pgd_present(*pgd))
97 pud = pud_offset(pgd, addr);
98 if (!pud_present(*pud))
101 pmd = pmd_offset(pud, addr);
102 if (!pmd_present(*pmd))
105 ptep = pte_offset_map(pmd, addr);
107 if (!is_swap_pte(*ptep)) {
112 ptl = pte_lockptr(mm, pmd);
115 if (!is_swap_pte(pte))
118 entry = pte_to_swp_entry(pte);
120 if (!is_migration_entry(entry) ||
121 migration_entry_to_page(entry) != old)
125 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
126 if (is_write_migration_entry(entry))
127 pte = pte_mkwrite(pte);
128 flush_cache_page(vma, addr, pte_pfn(pte));
129 set_pte_at(mm, addr, ptep, pte);
132 page_add_anon_rmap(new, vma, addr);
134 page_add_file_rmap(new);
136 /* No need to invalidate - it was non-present before */
137 update_mmu_cache(vma, addr, ptep);
139 pte_unmap_unlock(ptep, ptl);
145 * Get rid of all migration entries and replace them by
146 * references to the indicated page.
148 static void remove_migration_ptes(struct page *old, struct page *new)
150 rmap_walk(new, remove_migration_pte, old);
154 * Something used the pte of a page under migration. We need to
155 * get to the page and wait until migration is finished.
156 * When we return from this function the fault will be retried.
158 * This function is called from do_swap_page().
160 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
161 unsigned long address)
168 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
170 if (!is_swap_pte(pte))
173 entry = pte_to_swp_entry(pte);
174 if (!is_migration_entry(entry))
177 page = migration_entry_to_page(entry);
180 * Once radix-tree replacement of page migration started, page_count
181 * *must* be zero. And, we don't want to call wait_on_page_locked()
182 * against a page without get_page().
183 * So, we use get_page_unless_zero(), here. Even failed, page fault
186 if (!get_page_unless_zero(page))
188 pte_unmap_unlock(ptep, ptl);
189 wait_on_page_locked(page);
193 pte_unmap_unlock(ptep, ptl);
197 * Replace the page in the mapping.
199 * The number of remaining references must be:
200 * 1 for anonymous pages without a mapping
201 * 2 for pages with a mapping
202 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
204 static int migrate_page_move_mapping(struct address_space *mapping,
205 struct page *newpage, struct page *page)
211 /* Anonymous page without mapping */
212 if (page_count(page) != 1)
217 spin_lock_irq(&mapping->tree_lock);
219 pslot = radix_tree_lookup_slot(&mapping->page_tree,
222 expected_count = 2 + page_has_private(page);
223 if (page_count(page) != expected_count ||
224 (struct page *)radix_tree_deref_slot(pslot) != page) {
225 spin_unlock_irq(&mapping->tree_lock);
229 if (!page_freeze_refs(page, expected_count)) {
230 spin_unlock_irq(&mapping->tree_lock);
235 * Now we know that no one else is looking at the page.
237 get_page(newpage); /* add cache reference */
238 if (PageSwapCache(page)) {
239 SetPageSwapCache(newpage);
240 set_page_private(newpage, page_private(page));
243 radix_tree_replace_slot(pslot, newpage);
245 page_unfreeze_refs(page, expected_count);
247 * Drop cache reference from old page.
248 * We know this isn't the last reference.
253 * If moved to a different zone then also account
254 * the page for that zone. Other VM counters will be
255 * taken care of when we establish references to the
256 * new page and drop references to the old page.
258 * Note that anonymous pages are accounted for
259 * via NR_FILE_PAGES and NR_ANON_PAGES if they
260 * are mapped to swap space.
262 __dec_zone_page_state(page, NR_FILE_PAGES);
263 __inc_zone_page_state(newpage, NR_FILE_PAGES);
264 if (PageSwapBacked(page)) {
265 __dec_zone_page_state(page, NR_SHMEM);
266 __inc_zone_page_state(newpage, NR_SHMEM);
268 spin_unlock_irq(&mapping->tree_lock);
274 * Copy the page to its new location
276 static void migrate_page_copy(struct page *newpage, struct page *page)
278 copy_highpage(newpage, page);
281 SetPageError(newpage);
282 if (PageReferenced(page))
283 SetPageReferenced(newpage);
284 if (PageUptodate(page))
285 SetPageUptodate(newpage);
286 if (TestClearPageActive(page)) {
287 VM_BUG_ON(PageUnevictable(page));
288 SetPageActive(newpage);
289 } else if (TestClearPageUnevictable(page))
290 SetPageUnevictable(newpage);
291 if (PageChecked(page))
292 SetPageChecked(newpage);
293 if (PageMappedToDisk(page))
294 SetPageMappedToDisk(newpage);
296 if (PageDirty(page)) {
297 clear_page_dirty_for_io(page);
299 * Want to mark the page and the radix tree as dirty, and
300 * redo the accounting that clear_page_dirty_for_io undid,
301 * but we can't use set_page_dirty because that function
302 * is actually a signal that all of the page has become dirty.
303 * Wheras only part of our page may be dirty.
305 __set_page_dirty_nobuffers(newpage);
308 mlock_migrate_page(newpage, page);
309 ksm_migrate_page(newpage, page);
311 ClearPageSwapCache(page);
312 ClearPagePrivate(page);
313 set_page_private(page, 0);
314 page->mapping = NULL;
317 * If any waiters have accumulated on the new page then
320 if (PageWriteback(newpage))
321 end_page_writeback(newpage);
324 /************************************************************
325 * Migration functions
326 ***********************************************************/
328 /* Always fail migration. Used for mappings that are not movable */
329 int fail_migrate_page(struct address_space *mapping,
330 struct page *newpage, struct page *page)
334 EXPORT_SYMBOL(fail_migrate_page);
337 * Common logic to directly migrate a single page suitable for
338 * pages that do not use PagePrivate/PagePrivate2.
340 * Pages are locked upon entry and exit.
342 int migrate_page(struct address_space *mapping,
343 struct page *newpage, struct page *page)
347 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
349 rc = migrate_page_move_mapping(mapping, newpage, page);
354 migrate_page_copy(newpage, page);
357 EXPORT_SYMBOL(migrate_page);
361 * Migration function for pages with buffers. This function can only be used
362 * if the underlying filesystem guarantees that no other references to "page"
365 int buffer_migrate_page(struct address_space *mapping,
366 struct page *newpage, struct page *page)
368 struct buffer_head *bh, *head;
371 if (!page_has_buffers(page))
372 return migrate_page(mapping, newpage, page);
374 head = page_buffers(page);
376 rc = migrate_page_move_mapping(mapping, newpage, page);
385 bh = bh->b_this_page;
387 } while (bh != head);
389 ClearPagePrivate(page);
390 set_page_private(newpage, page_private(page));
391 set_page_private(page, 0);
397 set_bh_page(bh, newpage, bh_offset(bh));
398 bh = bh->b_this_page;
400 } while (bh != head);
402 SetPagePrivate(newpage);
404 migrate_page_copy(newpage, page);
410 bh = bh->b_this_page;
412 } while (bh != head);
416 EXPORT_SYMBOL(buffer_migrate_page);
420 * Writeback a page to clean the dirty state
422 static int writeout(struct address_space *mapping, struct page *page)
424 struct writeback_control wbc = {
425 .sync_mode = WB_SYNC_NONE,
428 .range_end = LLONG_MAX,
434 if (!mapping->a_ops->writepage)
435 /* No write method for the address space */
438 if (!clear_page_dirty_for_io(page))
439 /* Someone else already triggered a write */
443 * A dirty page may imply that the underlying filesystem has
444 * the page on some queue. So the page must be clean for
445 * migration. Writeout may mean we loose the lock and the
446 * page state is no longer what we checked for earlier.
447 * At this point we know that the migration attempt cannot
450 remove_migration_ptes(page, page);
452 rc = mapping->a_ops->writepage(page, &wbc);
454 if (rc != AOP_WRITEPAGE_ACTIVATE)
455 /* unlocked. Relock */
458 return (rc < 0) ? -EIO : -EAGAIN;
462 * Default handling if a filesystem does not provide a migration function.
464 static int fallback_migrate_page(struct address_space *mapping,
465 struct page *newpage, struct page *page)
468 return writeout(mapping, page);
471 * Buffers may be managed in a filesystem specific way.
472 * We must have no buffers or drop them.
474 if (page_has_private(page) &&
475 !try_to_release_page(page, GFP_KERNEL))
478 return migrate_page(mapping, newpage, page);
482 * Move a page to a newly allocated page
483 * The page is locked and all ptes have been successfully removed.
485 * The new page will have replaced the old page if this function
492 static int move_to_new_page(struct page *newpage, struct page *page)
494 struct address_space *mapping;
498 * Block others from accessing the page when we get around to
499 * establishing additional references. We are the only one
500 * holding a reference to the new page at this point.
502 if (!trylock_page(newpage))
505 /* Prepare mapping for the new page.*/
506 newpage->index = page->index;
507 newpage->mapping = page->mapping;
508 if (PageSwapBacked(page))
509 SetPageSwapBacked(newpage);
511 mapping = page_mapping(page);
513 rc = migrate_page(mapping, newpage, page);
514 else if (mapping->a_ops->migratepage)
516 * Most pages have a mapping and most filesystems
517 * should provide a migration function. Anonymous
518 * pages are part of swap space which also has its
519 * own migration function. This is the most common
520 * path for page migration.
522 rc = mapping->a_ops->migratepage(mapping,
525 rc = fallback_migrate_page(mapping, newpage, page);
528 remove_migration_ptes(page, newpage);
530 newpage->mapping = NULL;
532 unlock_page(newpage);
538 * Obtain the lock on page, remove all ptes and migrate the page
539 * to the newly allocated page in newpage.
541 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
542 struct page *page, int force, int offlining)
546 struct page *newpage = get_new_page(page, private, &result);
549 struct mem_cgroup *mem = NULL;
554 if (page_count(page) == 1) {
555 /* page was freed from under us. So we are done. */
559 /* prepare cgroup just returns 0 or -ENOMEM */
562 if (!trylock_page(page)) {
569 * Only memory hotplug's offline_pages() caller has locked out KSM,
570 * and can safely migrate a KSM page. The other cases have skipped
571 * PageKsm along with PageReserved - but it is only now when we have
572 * the page lock that we can be certain it will not go KSM beneath us
573 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
574 * its pagecount raised, but only here do we take the page lock which
577 if (PageKsm(page) && !offlining) {
582 /* charge against new page */
583 charge = mem_cgroup_prepare_migration(page, &mem);
584 if (charge == -ENOMEM) {
590 if (PageWriteback(page)) {
593 wait_on_page_writeback(page);
596 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
597 * we cannot notice that anon_vma is freed while we migrates a page.
598 * This rcu_read_lock() delays freeing anon_vma pointer until the end
599 * of migration. File cache pages are no problem because of page_lock()
600 * File Caches may use write_page() or lock_page() in migration, then,
601 * just care Anon page here.
603 if (PageAnon(page)) {
609 * Corner case handling:
610 * 1. When a new swap-cache page is read into, it is added to the LRU
611 * and treated as swapcache but it has no rmap yet.
612 * Calling try_to_unmap() against a page->mapping==NULL page will
613 * trigger a BUG. So handle it here.
614 * 2. An orphaned page (see truncate_complete_page) might have
615 * fs-private metadata. The page can be picked up due to memory
616 * offlining. Everywhere else except page reclaim, the page is
617 * invisible to the vm, so the page can not be migrated. So try to
618 * free the metadata, so the page can be freed.
620 if (!page->mapping) {
621 if (!PageAnon(page) && page_has_private(page)) {
623 * Go direct to try_to_free_buffers() here because
624 * a) that's what try_to_release_page() would do anyway
625 * b) we may be under rcu_read_lock() here, so we can't
626 * use GFP_KERNEL which is what try_to_release_page()
627 * needs to be effective.
629 try_to_free_buffers(page);
635 /* Establish migration ptes or remove ptes */
636 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
639 if (!page_mapped(page))
640 rc = move_to_new_page(newpage, page);
643 remove_migration_ptes(page, page);
649 mem_cgroup_end_migration(mem, page, newpage);
655 * A page that has been migrated has all references
656 * removed and will be freed. A page that has not been
657 * migrated will have kepts its references and be
660 list_del(&page->lru);
661 dec_zone_page_state(page, NR_ISOLATED_ANON +
662 page_is_file_cache(page));
663 putback_lru_page(page);
669 * Move the new page to the LRU. If migration was not successful
670 * then this will free the page.
672 putback_lru_page(newpage);
678 *result = page_to_nid(newpage);
686 * The function takes one list of pages to migrate and a function
687 * that determines from the page to be migrated and the private data
688 * the target of the move and allocates the page.
690 * The function returns after 10 attempts or if no pages
691 * are movable anymore because to has become empty
692 * or no retryable pages exist anymore. All pages will be
693 * returned to the LRU or freed.
695 * Return: Number of pages not migrated or error code.
697 int migrate_pages(struct list_head *from,
698 new_page_t get_new_page, unsigned long private, int offlining)
705 int swapwrite = current->flags & PF_SWAPWRITE;
709 current->flags |= PF_SWAPWRITE;
711 for(pass = 0; pass < 10 && retry; pass++) {
714 list_for_each_entry_safe(page, page2, from, lru) {
717 rc = unmap_and_move(get_new_page, private,
718 page, pass > 2, offlining);
729 /* Permanent failure */
738 current->flags &= ~PF_SWAPWRITE;
740 putback_lru_pages(from);
745 return nr_failed + retry;
750 * Move a list of individual pages
752 struct page_to_node {
759 static struct page *new_page_node(struct page *p, unsigned long private,
762 struct page_to_node *pm = (struct page_to_node *)private;
764 while (pm->node != MAX_NUMNODES && pm->page != p)
767 if (pm->node == MAX_NUMNODES)
770 *result = &pm->status;
772 return alloc_pages_exact_node(pm->node,
773 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
777 * Move a set of pages as indicated in the pm array. The addr
778 * field must be set to the virtual address of the page to be moved
779 * and the node number must contain a valid target node.
780 * The pm array ends with node = MAX_NUMNODES.
782 static int do_move_page_to_node_array(struct mm_struct *mm,
783 struct page_to_node *pm,
787 struct page_to_node *pp;
790 down_read(&mm->mmap_sem);
793 * Build a list of pages to migrate
795 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
796 struct vm_area_struct *vma;
800 vma = find_vma(mm, pp->addr);
801 if (!vma || !vma_migratable(vma))
804 page = follow_page(vma, pp->addr, FOLL_GET);
814 /* Use PageReserved to check for zero page */
815 if (PageReserved(page) || PageKsm(page))
819 err = page_to_nid(page);
823 * Node already in the right place
828 if (page_mapcount(page) > 1 &&
832 err = isolate_lru_page(page);
834 list_add_tail(&page->lru, &pagelist);
835 inc_zone_page_state(page, NR_ISOLATED_ANON +
836 page_is_file_cache(page));
840 * Either remove the duplicate refcount from
841 * isolate_lru_page() or drop the page ref if it was
850 if (!list_empty(&pagelist))
851 err = migrate_pages(&pagelist, new_page_node,
852 (unsigned long)pm, 0);
854 up_read(&mm->mmap_sem);
859 * Migrate an array of page address onto an array of nodes and fill
860 * the corresponding array of status.
862 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
863 unsigned long nr_pages,
864 const void __user * __user *pages,
865 const int __user *nodes,
866 int __user *status, int flags)
868 struct page_to_node *pm;
869 nodemask_t task_nodes;
870 unsigned long chunk_nr_pages;
871 unsigned long chunk_start;
874 task_nodes = cpuset_mems_allowed(task);
877 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
884 * Store a chunk of page_to_node array in a page,
885 * but keep the last one as a marker
887 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
889 for (chunk_start = 0;
890 chunk_start < nr_pages;
891 chunk_start += chunk_nr_pages) {
894 if (chunk_start + chunk_nr_pages > nr_pages)
895 chunk_nr_pages = nr_pages - chunk_start;
897 /* fill the chunk pm with addrs and nodes from user-space */
898 for (j = 0; j < chunk_nr_pages; j++) {
899 const void __user *p;
903 if (get_user(p, pages + j + chunk_start))
905 pm[j].addr = (unsigned long) p;
907 if (get_user(node, nodes + j + chunk_start))
911 if (node < 0 || node >= MAX_NUMNODES)
914 if (!node_state(node, N_HIGH_MEMORY))
918 if (!node_isset(node, task_nodes))
924 /* End marker for this chunk */
925 pm[chunk_nr_pages].node = MAX_NUMNODES;
927 /* Migrate this chunk */
928 err = do_move_page_to_node_array(mm, pm,
929 flags & MPOL_MF_MOVE_ALL);
933 /* Return status information */
934 for (j = 0; j < chunk_nr_pages; j++)
935 if (put_user(pm[j].status, status + j + chunk_start)) {
943 free_page((unsigned long)pm);
949 * Determine the nodes of an array of pages and store it in an array of status.
951 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
952 const void __user **pages, int *status)
956 down_read(&mm->mmap_sem);
958 for (i = 0; i < nr_pages; i++) {
959 unsigned long addr = (unsigned long)(*pages);
960 struct vm_area_struct *vma;
964 vma = find_vma(mm, addr);
968 page = follow_page(vma, addr, 0);
975 /* Use PageReserved to check for zero page */
976 if (!page || PageReserved(page) || PageKsm(page))
979 err = page_to_nid(page);
987 up_read(&mm->mmap_sem);
991 * Determine the nodes of a user array of pages and store it in
992 * a user array of status.
994 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
995 const void __user * __user *pages,
998 #define DO_PAGES_STAT_CHUNK_NR 16
999 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1000 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1003 unsigned long chunk_nr;
1005 chunk_nr = nr_pages;
1006 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1007 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1009 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1012 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1014 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1019 nr_pages -= chunk_nr;
1021 return nr_pages ? -EFAULT : 0;
1025 * Move a list of pages in the address space of the currently executing
1028 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1029 const void __user * __user *, pages,
1030 const int __user *, nodes,
1031 int __user *, status, int, flags)
1033 const struct cred *cred = current_cred(), *tcred;
1034 struct task_struct *task;
1035 struct mm_struct *mm;
1039 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1042 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1045 /* Find the mm_struct */
1046 read_lock(&tasklist_lock);
1047 task = pid ? find_task_by_vpid(pid) : current;
1049 read_unlock(&tasklist_lock);
1052 mm = get_task_mm(task);
1053 read_unlock(&tasklist_lock);
1059 * Check if this process has the right to modify the specified
1060 * process. The right exists if the process has administrative
1061 * capabilities, superuser privileges or the same
1062 * userid as the target process.
1065 tcred = __task_cred(task);
1066 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1067 cred->uid != tcred->suid && cred->uid != tcred->uid &&
1068 !capable(CAP_SYS_NICE)) {
1075 err = security_task_movememory(task);
1080 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1083 err = do_pages_stat(mm, nr_pages, pages, status);
1092 * Call migration functions in the vma_ops that may prepare
1093 * memory in a vm for migration. migration functions may perform
1094 * the migration for vmas that do not have an underlying page struct.
1096 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1097 const nodemask_t *from, unsigned long flags)
1099 struct vm_area_struct *vma;
1102 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1103 if (vma->vm_ops && vma->vm_ops->migrate) {
1104 err = vma->vm_ops->migrate(vma, to, from, flags);