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>
12 * Christoph Lameter <clameter@sgi.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/pagevec.h>
23 #include <linux/rmap.h>
24 #include <linux/topology.h>
25 #include <linux/cpu.h>
26 #include <linux/cpuset.h>
27 #include <linux/writeback.h>
28 #include <linux/mempolicy.h>
29 #include <linux/vmalloc.h>
33 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
36 * Isolate one page from the LRU lists. If successful put it onto
37 * the indicated list with elevated page count.
40 * -EBUSY: page not on LRU list
41 * 0: page removed from LRU list and added to the specified list.
43 int isolate_lru_page(struct page *page, struct list_head *pagelist)
48 struct zone *zone = page_zone(page);
50 spin_lock_irq(&zone->lru_lock);
56 del_page_from_active_list(zone, page);
58 del_page_from_inactive_list(zone, page);
59 list_add_tail(&page->lru, pagelist);
61 spin_unlock_irq(&zone->lru_lock);
67 * migrate_prep() needs to be called before we start compiling a list of pages
68 * to be migrated using isolate_lru_page().
70 int migrate_prep(void)
73 * Clear the LRU lists so pages can be isolated.
74 * Note that pages may be moved off the LRU after we have
75 * drained them. Those pages will fail to migrate like other
76 * pages that may be busy.
83 static inline void move_to_lru(struct page *page)
85 if (PageActive(page)) {
87 * lru_cache_add_active checks that
88 * the PG_active bit is off.
90 ClearPageActive(page);
91 lru_cache_add_active(page);
99 * Add isolated pages on the list back to the LRU.
101 * returns the number of pages put back.
103 int putback_lru_pages(struct list_head *l)
109 list_for_each_entry_safe(page, page2, l, lru) {
110 list_del(&page->lru);
117 static inline int is_swap_pte(pte_t pte)
119 return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
123 * Restore a potential migration pte to a working pte entry
125 static void remove_migration_pte(struct vm_area_struct *vma,
126 struct page *old, struct page *new)
128 struct mm_struct *mm = vma->vm_mm;
135 unsigned long addr = page_address_in_vma(new, vma);
140 pgd = pgd_offset(mm, addr);
141 if (!pgd_present(*pgd))
144 pud = pud_offset(pgd, addr);
145 if (!pud_present(*pud))
148 pmd = pmd_offset(pud, addr);
149 if (!pmd_present(*pmd))
152 ptep = pte_offset_map(pmd, addr);
154 if (!is_swap_pte(*ptep)) {
159 ptl = pte_lockptr(mm, pmd);
162 if (!is_swap_pte(pte))
165 entry = pte_to_swp_entry(pte);
167 if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
171 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
172 if (is_write_migration_entry(entry))
173 pte = pte_mkwrite(pte);
174 set_pte_at(mm, addr, ptep, pte);
177 page_add_anon_rmap(new, vma, addr);
179 page_add_file_rmap(new);
181 /* No need to invalidate - it was non-present before */
182 update_mmu_cache(vma, addr, pte);
183 lazy_mmu_prot_update(pte);
186 pte_unmap_unlock(ptep, ptl);
190 * Note that remove_file_migration_ptes will only work on regular mappings,
191 * Nonlinear mappings do not use migration entries.
193 static void remove_file_migration_ptes(struct page *old, struct page *new)
195 struct vm_area_struct *vma;
196 struct address_space *mapping = page_mapping(new);
197 struct prio_tree_iter iter;
198 pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
203 spin_lock(&mapping->i_mmap_lock);
205 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
206 remove_migration_pte(vma, old, new);
208 spin_unlock(&mapping->i_mmap_lock);
212 * Must hold mmap_sem lock on at least one of the vmas containing
213 * the page so that the anon_vma cannot vanish.
215 static void remove_anon_migration_ptes(struct page *old, struct page *new)
217 struct anon_vma *anon_vma;
218 struct vm_area_struct *vma;
219 unsigned long mapping;
221 mapping = (unsigned long)new->mapping;
223 if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
227 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
229 anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
230 spin_lock(&anon_vma->lock);
232 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
233 remove_migration_pte(vma, old, new);
235 spin_unlock(&anon_vma->lock);
239 * Get rid of all migration entries and replace them by
240 * references to the indicated page.
242 static void remove_migration_ptes(struct page *old, struct page *new)
245 remove_anon_migration_ptes(old, new);
247 remove_file_migration_ptes(old, new);
251 * Something used the pte of a page under migration. We need to
252 * get to the page and wait until migration is finished.
253 * When we return from this function the fault will be retried.
255 * This function is called from do_swap_page().
257 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
258 unsigned long address)
265 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
267 if (!is_swap_pte(pte))
270 entry = pte_to_swp_entry(pte);
271 if (!is_migration_entry(entry))
274 page = migration_entry_to_page(entry);
277 pte_unmap_unlock(ptep, ptl);
278 wait_on_page_locked(page);
282 pte_unmap_unlock(ptep, ptl);
286 * Replace the page in the mapping.
288 * The number of remaining references must be:
289 * 1 for anonymous pages without a mapping
290 * 2 for pages with a mapping
291 * 3 for pages with a mapping and PagePrivate set.
293 static int migrate_page_move_mapping(struct address_space *mapping,
294 struct page *newpage, struct page *page)
296 struct page **radix_pointer;
300 if (page_count(page) != 1)
305 write_lock_irq(&mapping->tree_lock);
307 radix_pointer = (struct page **)radix_tree_lookup_slot(
311 if (page_count(page) != 2 + !!PagePrivate(page) ||
312 *radix_pointer != page) {
313 write_unlock_irq(&mapping->tree_lock);
318 * Now we know that no one else is looking at the page.
322 if (PageSwapCache(page)) {
323 SetPageSwapCache(newpage);
324 set_page_private(newpage, page_private(page));
328 *radix_pointer = newpage;
330 write_unlock_irq(&mapping->tree_lock);
336 * Copy the page to its new location
338 static void migrate_page_copy(struct page *newpage, struct page *page)
340 copy_highpage(newpage, page);
343 SetPageError(newpage);
344 if (PageReferenced(page))
345 SetPageReferenced(newpage);
346 if (PageUptodate(page))
347 SetPageUptodate(newpage);
348 if (PageActive(page))
349 SetPageActive(newpage);
350 if (PageChecked(page))
351 SetPageChecked(newpage);
352 if (PageMappedToDisk(page))
353 SetPageMappedToDisk(newpage);
355 if (PageDirty(page)) {
356 clear_page_dirty_for_io(page);
357 set_page_dirty(newpage);
361 ClearPageSwapCache(page);
363 ClearPageActive(page);
364 ClearPagePrivate(page);
365 set_page_private(page, 0);
366 page->mapping = NULL;
369 * If any waiters have accumulated on the new page then
372 if (PageWriteback(newpage))
373 end_page_writeback(newpage);
376 /************************************************************
377 * Migration functions
378 ***********************************************************/
380 /* Always fail migration. Used for mappings that are not movable */
381 int fail_migrate_page(struct address_space *mapping,
382 struct page *newpage, struct page *page)
386 EXPORT_SYMBOL(fail_migrate_page);
389 * Common logic to directly migrate a single page suitable for
390 * pages that do not use PagePrivate.
392 * Pages are locked upon entry and exit.
394 int migrate_page(struct address_space *mapping,
395 struct page *newpage, struct page *page)
399 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
401 rc = migrate_page_move_mapping(mapping, newpage, page);
406 migrate_page_copy(newpage, page);
409 EXPORT_SYMBOL(migrate_page);
412 * Migration function for pages with buffers. This function can only be used
413 * if the underlying filesystem guarantees that no other references to "page"
416 int buffer_migrate_page(struct address_space *mapping,
417 struct page *newpage, struct page *page)
419 struct buffer_head *bh, *head;
422 if (!page_has_buffers(page))
423 return migrate_page(mapping, newpage, page);
425 head = page_buffers(page);
427 rc = migrate_page_move_mapping(mapping, newpage, page);
436 bh = bh->b_this_page;
438 } while (bh != head);
440 ClearPagePrivate(page);
441 set_page_private(newpage, page_private(page));
442 set_page_private(page, 0);
448 set_bh_page(bh, newpage, bh_offset(bh));
449 bh = bh->b_this_page;
451 } while (bh != head);
453 SetPagePrivate(newpage);
455 migrate_page_copy(newpage, page);
461 bh = bh->b_this_page;
463 } while (bh != head);
467 EXPORT_SYMBOL(buffer_migrate_page);
470 * Writeback a page to clean the dirty state
472 static int writeout(struct address_space *mapping, struct page *page)
474 struct writeback_control wbc = {
475 .sync_mode = WB_SYNC_NONE,
478 .range_end = LLONG_MAX,
484 if (!mapping->a_ops->writepage)
485 /* No write method for the address space */
488 if (!clear_page_dirty_for_io(page))
489 /* Someone else already triggered a write */
493 * A dirty page may imply that the underlying filesystem has
494 * the page on some queue. So the page must be clean for
495 * migration. Writeout may mean we loose the lock and the
496 * page state is no longer what we checked for earlier.
497 * At this point we know that the migration attempt cannot
500 remove_migration_ptes(page, page);
502 rc = mapping->a_ops->writepage(page, &wbc);
504 /* I/O Error writing */
507 if (rc != AOP_WRITEPAGE_ACTIVATE)
508 /* unlocked. Relock */
515 * Default handling if a filesystem does not provide a migration function.
517 static int fallback_migrate_page(struct address_space *mapping,
518 struct page *newpage, struct page *page)
521 return writeout(mapping, page);
524 * Buffers may be managed in a filesystem specific way.
525 * We must have no buffers or drop them.
527 if (page_has_buffers(page) &&
528 !try_to_release_page(page, GFP_KERNEL))
531 return migrate_page(mapping, newpage, page);
535 * Move a page to a newly allocated page
536 * The page is locked and all ptes have been successfully removed.
538 * The new page will have replaced the old page if this function
541 static int move_to_new_page(struct page *newpage, struct page *page)
543 struct address_space *mapping;
547 * Block others from accessing the page when we get around to
548 * establishing additional references. We are the only one
549 * holding a reference to the new page at this point.
551 if (TestSetPageLocked(newpage))
554 /* Prepare mapping for the new page.*/
555 newpage->index = page->index;
556 newpage->mapping = page->mapping;
558 mapping = page_mapping(page);
560 rc = migrate_page(mapping, newpage, page);
561 else if (mapping->a_ops->migratepage)
563 * Most pages have a mapping and most filesystems
564 * should provide a migration function. Anonymous
565 * pages are part of swap space which also has its
566 * own migration function. This is the most common
567 * path for page migration.
569 rc = mapping->a_ops->migratepage(mapping,
572 rc = fallback_migrate_page(mapping, newpage, page);
575 remove_migration_ptes(page, newpage);
577 newpage->mapping = NULL;
579 unlock_page(newpage);
585 * Obtain the lock on page, remove all ptes and migrate the page
586 * to the newly allocated page in newpage.
588 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
589 struct page *page, int force)
593 struct page *newpage = get_new_page(page, private, &result);
598 if (page_count(page) == 1)
599 /* page was freed from under us. So we are done. */
603 if (TestSetPageLocked(page)) {
609 if (PageWriteback(page)) {
612 wait_on_page_writeback(page);
616 * Establish migration ptes or remove ptes
618 if (try_to_unmap(page, 1) != SWAP_FAIL) {
619 if (!page_mapped(page))
620 rc = move_to_new_page(newpage, page);
622 /* A vma has VM_LOCKED set -> permanent failure */
626 remove_migration_ptes(page, page);
632 * A page that has been migrated has all references
633 * removed and will be freed. A page that has not been
634 * migrated will have kepts its references and be
637 list_del(&page->lru);
643 * Move the new page to the LRU. If migration was not successful
644 * then this will free the page.
646 move_to_lru(newpage);
651 *result = page_to_nid(newpage);
659 * The function takes one list of pages to migrate and a function
660 * that determines from the page to be migrated and the private data
661 * the target of the move and allocates the page.
663 * The function returns after 10 attempts or if no pages
664 * are movable anymore because to has become empty
665 * or no retryable pages exist anymore. All pages will be
666 * retruned to the LRU or freed.
668 * Return: Number of pages not migrated or error code.
670 int migrate_pages(struct list_head *from,
671 new_page_t get_new_page, unsigned long private)
678 int swapwrite = current->flags & PF_SWAPWRITE;
682 current->flags |= PF_SWAPWRITE;
684 for(pass = 0; pass < 10 && retry; pass++) {
687 list_for_each_entry_safe(page, page2, from, lru) {
690 rc = unmap_and_move(get_new_page, private,
702 /* Permanent failure */
711 current->flags &= ~PF_SWAPWRITE;
713 putback_lru_pages(from);
718 return nr_failed + retry;
723 * Move a list of individual pages
725 struct page_to_node {
732 static struct page *new_page_node(struct page *p, unsigned long private,
735 struct page_to_node *pm = (struct page_to_node *)private;
737 while (pm->node != MAX_NUMNODES && pm->page != p)
740 if (pm->node == MAX_NUMNODES)
743 *result = &pm->status;
745 return alloc_pages_node(pm->node, GFP_HIGHUSER, 0);
749 * Move a set of pages as indicated in the pm array. The addr
750 * field must be set to the virtual address of the page to be moved
751 * and the node number must contain a valid target node.
753 static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
757 struct page_to_node *pp;
760 down_read(&mm->mmap_sem);
763 * Build a list of pages to migrate
766 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
767 struct vm_area_struct *vma;
771 * A valid page pointer that will not match any of the
772 * pages that will be moved.
774 pp->page = ZERO_PAGE(0);
777 vma = find_vma(mm, pp->addr);
781 page = follow_page(vma, pp->addr, FOLL_GET);
786 if (PageReserved(page)) /* Check for zero page */
790 err = page_to_nid(page);
794 * Node already in the right place
799 if (page_mapcount(page) > 1 &&
803 err = isolate_lru_page(page, &pagelist);
806 * Either remove the duplicate refcount from
807 * isolate_lru_page() or drop the page ref if it was
815 if (!list_empty(&pagelist))
816 err = migrate_pages(&pagelist, new_page_node,
821 up_read(&mm->mmap_sem);
826 * Determine the nodes of a list of pages. The addr in the pm array
827 * must have been set to the virtual address of which we want to determine
830 static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
832 down_read(&mm->mmap_sem);
834 for ( ; pm->node != MAX_NUMNODES; pm++) {
835 struct vm_area_struct *vma;
840 vma = find_vma(mm, pm->addr);
844 page = follow_page(vma, pm->addr, 0);
846 /* Use PageReserved to check for zero page */
847 if (!page || PageReserved(page))
850 err = page_to_nid(page);
855 up_read(&mm->mmap_sem);
860 * Move a list of pages in the address space of the currently executing
863 asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
864 const void __user * __user *pages,
865 const int __user *nodes,
866 int __user *status, int flags)
870 struct task_struct *task;
871 nodemask_t task_nodes;
872 struct mm_struct *mm;
873 struct page_to_node *pm = NULL;
876 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
879 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
882 /* Find the mm_struct */
883 read_lock(&tasklist_lock);
884 task = pid ? find_task_by_pid(pid) : current;
886 read_unlock(&tasklist_lock);
889 mm = get_task_mm(task);
890 read_unlock(&tasklist_lock);
896 * Check if this process has the right to modify the specified
897 * process. The right exists if the process has administrative
898 * capabilities, superuser privileges or the same
899 * userid as the target process.
901 if ((current->euid != task->suid) && (current->euid != task->uid) &&
902 (current->uid != task->suid) && (current->uid != task->uid) &&
903 !capable(CAP_SYS_NICE)) {
908 task_nodes = cpuset_mems_allowed(task);
910 /* Limit nr_pages so that the multiplication may not overflow */
911 if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
916 pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
923 * Get parameters from user space and initialize the pm
924 * array. Return various errors if the user did something wrong.
926 for (i = 0; i < nr_pages; i++) {
930 if (get_user(p, pages + i))
933 pm[i].addr = (unsigned long)p;
937 if (get_user(node, nodes + i))
941 if (!node_online(node))
945 if (!node_isset(node, task_nodes))
952 pm[nr_pages].node = MAX_NUMNODES;
955 err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
957 err = do_pages_stat(mm, pm);
960 /* Return status information */
961 for (i = 0; i < nr_pages; i++)
962 if (put_user(pm[i].status, status + i))