GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / mm / migrate.c
blobe7a9c8076d2aa02920b1a72cd593bd330e86b02c
1 /*
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
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>
37 #include "internal.h"
39 #include <asm/tlbflush.h>
41 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
44 * migrate_prep() needs to be called before we start compiling a list of pages
45 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
46 * undesirable, use migrate_prep_local()
48 int migrate_prep(void)
51 * Clear the LRU lists so pages can be isolated.
52 * Note that pages may be moved off the LRU after we have
53 * drained them. Those pages will fail to migrate like other
54 * pages that may be busy.
56 lru_add_drain_all();
58 return 0;
61 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
62 int migrate_prep_local(void)
64 lru_add_drain();
66 return 0;
70 * Add isolated pages on the list back to the LRU under page lock
71 * to avoid leaking evictable pages back onto unevictable list.
73 void putback_lru_pages(struct list_head *l)
75 struct page *page;
76 struct page *page2;
78 list_for_each_entry_safe(page, page2, l, lru) {
79 list_del(&page->lru);
80 dec_zone_page_state(page, NR_ISOLATED_ANON +
81 page_is_file_cache(page));
82 putback_lru_page(page);
87 * Restore a potential migration pte to a working pte entry
89 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
90 unsigned long addr, void *old)
92 struct mm_struct *mm = vma->vm_mm;
93 swp_entry_t entry;
94 pgd_t *pgd;
95 pud_t *pud;
96 pmd_t *pmd;
97 pte_t *ptep, pte;
98 spinlock_t *ptl;
100 pgd = pgd_offset(mm, addr);
101 if (!pgd_present(*pgd))
102 goto out;
104 pud = pud_offset(pgd, addr);
105 if (!pud_present(*pud))
106 goto out;
108 pmd = pmd_offset(pud, addr);
109 if (!pmd_present(*pmd))
110 goto out;
112 ptep = pte_offset_map(pmd, addr);
114 if (!is_swap_pte(*ptep)) {
115 pte_unmap(ptep);
116 goto out;
119 ptl = pte_lockptr(mm, pmd);
120 spin_lock(ptl);
121 pte = *ptep;
122 if (!is_swap_pte(pte))
123 goto unlock;
125 entry = pte_to_swp_entry(pte);
127 if (!is_migration_entry(entry) ||
128 migration_entry_to_page(entry) != old)
129 goto unlock;
131 get_page(new);
132 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
133 if (is_write_migration_entry(entry))
134 pte = pte_mkwrite(pte);
135 flush_cache_page(vma, addr, pte_pfn(pte));
136 set_pte_at(mm, addr, ptep, pte);
138 if (PageAnon(new))
139 page_add_anon_rmap(new, vma, addr);
140 else
141 page_add_file_rmap(new);
143 /* No need to invalidate - it was non-present before */
144 update_mmu_cache(vma, addr, ptep);
145 unlock:
146 pte_unmap_unlock(ptep, ptl);
147 out:
148 return SWAP_AGAIN;
152 * Get rid of all migration entries and replace them by
153 * references to the indicated page.
155 static void remove_migration_ptes(struct page *old, struct page *new)
157 rmap_walk(new, remove_migration_pte, old);
161 * Something used the pte of a page under migration. We need to
162 * get to the page and wait until migration is finished.
163 * When we return from this function the fault will be retried.
165 * This function is called from do_swap_page().
167 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
168 unsigned long address)
170 pte_t *ptep, pte;
171 spinlock_t *ptl;
172 swp_entry_t entry;
173 struct page *page;
175 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
176 pte = *ptep;
177 if (!is_swap_pte(pte))
178 goto out;
180 entry = pte_to_swp_entry(pte);
181 if (!is_migration_entry(entry))
182 goto out;
184 page = migration_entry_to_page(entry);
187 * Once radix-tree replacement of page migration started, page_count
188 * *must* be zero. And, we don't want to call wait_on_page_locked()
189 * against a page without get_page().
190 * So, we use get_page_unless_zero(), here. Even failed, page fault
191 * will occur again.
193 if (!get_page_unless_zero(page))
194 goto out;
195 pte_unmap_unlock(ptep, ptl);
196 wait_on_page_locked(page);
197 put_page(page);
198 return;
199 out:
200 pte_unmap_unlock(ptep, ptl);
204 * Replace the page in the mapping.
206 * The number of remaining references must be:
207 * 1 for anonymous pages without a mapping
208 * 2 for pages with a mapping
209 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
211 static int migrate_page_move_mapping(struct address_space *mapping,
212 struct page *newpage, struct page *page)
214 int expected_count;
215 void **pslot;
217 if (!mapping) {
218 /* Anonymous page without mapping */
219 if (page_count(page) != 1)
220 return -EAGAIN;
221 return 0;
224 spin_lock_irq(&mapping->tree_lock);
226 pslot = radix_tree_lookup_slot(&mapping->page_tree,
227 page_index(page));
229 expected_count = 2 + page_has_private(page);
230 if (page_count(page) != expected_count ||
231 (struct page *)radix_tree_deref_slot(pslot) != page) {
232 spin_unlock_irq(&mapping->tree_lock);
233 return -EAGAIN;
236 if (!page_freeze_refs(page, expected_count)) {
237 spin_unlock_irq(&mapping->tree_lock);
238 return -EAGAIN;
242 * Now we know that no one else is looking at the page.
244 get_page(newpage); /* add cache reference */
245 if (PageSwapCache(page)) {
246 SetPageSwapCache(newpage);
247 set_page_private(newpage, page_private(page));
250 radix_tree_replace_slot(pslot, newpage);
252 page_unfreeze_refs(page, expected_count);
254 * Drop cache reference from old page.
255 * We know this isn't the last reference.
257 __put_page(page);
260 * If moved to a different zone then also account
261 * the page for that zone. Other VM counters will be
262 * taken care of when we establish references to the
263 * new page and drop references to the old page.
265 * Note that anonymous pages are accounted for
266 * via NR_FILE_PAGES and NR_ANON_PAGES if they
267 * are mapped to swap space.
269 __dec_zone_page_state(page, NR_FILE_PAGES);
270 __inc_zone_page_state(newpage, NR_FILE_PAGES);
271 if (PageSwapBacked(page)) {
272 __dec_zone_page_state(page, NR_SHMEM);
273 __inc_zone_page_state(newpage, NR_SHMEM);
275 spin_unlock_irq(&mapping->tree_lock);
277 return 0;
281 * Copy the page to its new location
283 static void migrate_page_copy(struct page *newpage, struct page *page)
285 copy_highpage(newpage, page);
287 if (PageError(page))
288 SetPageError(newpage);
289 if (PageReferenced(page))
290 SetPageReferenced(newpage);
291 if (PageUptodate(page))
292 SetPageUptodate(newpage);
293 if (TestClearPageActive(page)) {
294 VM_BUG_ON(PageUnevictable(page));
295 SetPageActive(newpage);
296 } else if (TestClearPageUnevictable(page))
297 SetPageUnevictable(newpage);
298 if (PageChecked(page))
299 SetPageChecked(newpage);
300 if (PageMappedToDisk(page))
301 SetPageMappedToDisk(newpage);
303 if (PageDirty(page)) {
304 clear_page_dirty_for_io(page);
306 * Want to mark the page and the radix tree as dirty, and
307 * redo the accounting that clear_page_dirty_for_io undid,
308 * but we can't use set_page_dirty because that function
309 * is actually a signal that all of the page has become dirty.
310 * Wheras only part of our page may be dirty.
312 __set_page_dirty_nobuffers(newpage);
315 mlock_migrate_page(newpage, page);
316 ksm_migrate_page(newpage, page);
318 ClearPageSwapCache(page);
319 ClearPagePrivate(page);
320 set_page_private(page, 0);
321 page->mapping = NULL;
324 * If any waiters have accumulated on the new page then
325 * wake them up.
327 if (PageWriteback(newpage))
328 end_page_writeback(newpage);
331 /************************************************************
332 * Migration functions
333 ***********************************************************/
335 /* Always fail migration. Used for mappings that are not movable */
336 int fail_migrate_page(struct address_space *mapping,
337 struct page *newpage, struct page *page)
339 return -EIO;
341 EXPORT_SYMBOL(fail_migrate_page);
344 * Common logic to directly migrate a single page suitable for
345 * pages that do not use PagePrivate/PagePrivate2.
347 * Pages are locked upon entry and exit.
349 int migrate_page(struct address_space *mapping,
350 struct page *newpage, struct page *page)
352 int rc;
354 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
356 rc = migrate_page_move_mapping(mapping, newpage, page);
358 if (rc)
359 return rc;
361 migrate_page_copy(newpage, page);
362 return 0;
364 EXPORT_SYMBOL(migrate_page);
366 #ifdef CONFIG_BLOCK
368 * Migration function for pages with buffers. This function can only be used
369 * if the underlying filesystem guarantees that no other references to "page"
370 * exist.
372 int buffer_migrate_page(struct address_space *mapping,
373 struct page *newpage, struct page *page)
375 struct buffer_head *bh, *head;
376 int rc;
378 if (!page_has_buffers(page))
379 return migrate_page(mapping, newpage, page);
381 head = page_buffers(page);
383 rc = migrate_page_move_mapping(mapping, newpage, page);
385 if (rc)
386 return rc;
388 bh = head;
389 do {
390 get_bh(bh);
391 lock_buffer(bh);
392 bh = bh->b_this_page;
394 } while (bh != head);
396 ClearPagePrivate(page);
397 set_page_private(newpage, page_private(page));
398 set_page_private(page, 0);
399 put_page(page);
400 get_page(newpage);
402 bh = head;
403 do {
404 set_bh_page(bh, newpage, bh_offset(bh));
405 bh = bh->b_this_page;
407 } while (bh != head);
409 SetPagePrivate(newpage);
411 migrate_page_copy(newpage, page);
413 bh = head;
414 do {
415 unlock_buffer(bh);
416 put_bh(bh);
417 bh = bh->b_this_page;
419 } while (bh != head);
421 return 0;
423 EXPORT_SYMBOL(buffer_migrate_page);
424 #endif
427 * Writeback a page to clean the dirty state
429 static int writeout(struct address_space *mapping, struct page *page)
431 struct writeback_control wbc = {
432 .sync_mode = WB_SYNC_NONE,
433 .nr_to_write = 1,
434 .range_start = 0,
435 .range_end = LLONG_MAX,
436 .nonblocking = 1,
437 .for_reclaim = 1
439 int rc;
441 if (!mapping->a_ops->writepage)
442 /* No write method for the address space */
443 return -EINVAL;
445 if (!clear_page_dirty_for_io(page))
446 /* Someone else already triggered a write */
447 return -EAGAIN;
450 * A dirty page may imply that the underlying filesystem has
451 * the page on some queue. So the page must be clean for
452 * migration. Writeout may mean we loose the lock and the
453 * page state is no longer what we checked for earlier.
454 * At this point we know that the migration attempt cannot
455 * be successful.
457 remove_migration_ptes(page, page);
459 rc = mapping->a_ops->writepage(page, &wbc);
461 if (rc != AOP_WRITEPAGE_ACTIVATE)
462 /* unlocked. Relock */
463 lock_page(page);
465 return (rc < 0) ? -EIO : -EAGAIN;
469 * Default handling if a filesystem does not provide a migration function.
471 static int fallback_migrate_page(struct address_space *mapping,
472 struct page *newpage, struct page *page)
474 if (PageDirty(page))
475 return writeout(mapping, page);
478 * Buffers may be managed in a filesystem specific way.
479 * We must have no buffers or drop them.
481 if (page_has_private(page) &&
482 !try_to_release_page(page, GFP_KERNEL))
483 return -EAGAIN;
485 return migrate_page(mapping, newpage, page);
489 * Move a page to a newly allocated page
490 * The page is locked and all ptes have been successfully removed.
492 * The new page will have replaced the old page if this function
493 * is successful.
495 * Return value:
496 * < 0 - error code
497 * == 0 - success
499 static int move_to_new_page(struct page *newpage, struct page *page,
500 int remap_swapcache)
502 struct address_space *mapping;
503 int rc;
506 * Block others from accessing the page when we get around to
507 * establishing additional references. We are the only one
508 * holding a reference to the new page at this point.
510 if (!trylock_page(newpage))
511 BUG();
513 /* Prepare mapping for the new page.*/
514 newpage->index = page->index;
515 newpage->mapping = page->mapping;
516 if (PageSwapBacked(page))
517 SetPageSwapBacked(newpage);
519 mapping = page_mapping(page);
520 if (!mapping)
521 rc = migrate_page(mapping, newpage, page);
522 else if (mapping->a_ops->migratepage)
524 * Most pages have a mapping and most filesystems
525 * should provide a migration function. Anonymous
526 * pages are part of swap space which also has its
527 * own migration function. This is the most common
528 * path for page migration.
530 rc = mapping->a_ops->migratepage(mapping,
531 newpage, page);
532 else
533 rc = fallback_migrate_page(mapping, newpage, page);
535 if (rc) {
536 newpage->mapping = NULL;
537 } else {
538 if (remap_swapcache)
539 remove_migration_ptes(page, newpage);
542 unlock_page(newpage);
544 return rc;
548 * Obtain the lock on page, remove all ptes and migrate the page
549 * to the newly allocated page in newpage.
551 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
552 struct page *page, int force, int offlining)
554 int rc = 0;
555 int *result = NULL;
556 struct page *newpage = get_new_page(page, private, &result);
557 int remap_swapcache = 1;
558 int charge = 0;
559 struct mem_cgroup *mem = NULL;
560 struct anon_vma *anon_vma = NULL;
562 if (!newpage)
563 return -ENOMEM;
565 if (page_count(page) == 1) {
566 /* page was freed from under us. So we are done. */
567 goto move_newpage;
570 /* prepare cgroup just returns 0 or -ENOMEM */
571 rc = -EAGAIN;
573 if (!trylock_page(page)) {
574 if (!force)
575 goto move_newpage;
576 lock_page(page);
580 * Only memory hotplug's offline_pages() caller has locked out KSM,
581 * and can safely migrate a KSM page. The other cases have skipped
582 * PageKsm along with PageReserved - but it is only now when we have
583 * the page lock that we can be certain it will not go KSM beneath us
584 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
585 * its pagecount raised, but only here do we take the page lock which
586 * serializes that).
588 if (PageKsm(page) && !offlining) {
589 rc = -EBUSY;
590 goto unlock;
593 /* charge against new page */
594 charge = mem_cgroup_prepare_migration(page, newpage, &mem);
595 if (charge == -ENOMEM) {
596 rc = -ENOMEM;
597 goto unlock;
599 BUG_ON(charge);
601 if (PageWriteback(page)) {
602 if (!force)
603 goto uncharge;
604 wait_on_page_writeback(page);
607 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
608 * we cannot notice that anon_vma is freed while we migrates a page.
609 * This get_anon_vma() delays freeing anon_vma pointer until the end
610 * of migration. File cache pages are no problem because of page_lock()
611 * File Caches may use write_page() or lock_page() in migration, then,
612 * just care Anon page here.
614 if (PageAnon(page)) {
616 * Only page_lock_anon_vma() understands the subtleties of
617 * getting a hold on an anon_vma from outside one of its mms.
619 anon_vma = page_lock_anon_vma(page);
620 if (anon_vma) {
622 * Take a reference count on the anon_vma if the
623 * page is mapped so that it is guaranteed to
624 * exist when the page is remapped later
626 get_anon_vma(anon_vma);
627 page_unlock_anon_vma(anon_vma);
628 } else if (PageSwapCache(page)) {
630 * We cannot be sure that the anon_vma of an unmapped
631 * swapcache page is safe to use because we don't
632 * know in advance if the VMA that this page belonged
633 * to still exists. If the VMA and others sharing the
634 * data have been freed, then the anon_vma could
635 * already be invalid.
637 * To avoid this possibility, swapcache pages get
638 * migrated but are not remapped when migration
639 * completes
641 remap_swapcache = 0;
642 } else {
643 goto uncharge;
648 * Corner case handling:
649 * 1. When a new swap-cache page is read into, it is added to the LRU
650 * and treated as swapcache but it has no rmap yet.
651 * Calling try_to_unmap() against a page->mapping==NULL page will
652 * trigger a BUG. So handle it here.
653 * 2. An orphaned page (see truncate_complete_page) might have
654 * fs-private metadata. The page can be picked up due to memory
655 * offlining. Everywhere else except page reclaim, the page is
656 * invisible to the vm, so the page can not be migrated. So try to
657 * free the metadata, so the page can be freed.
659 if (!page->mapping) {
660 VM_BUG_ON(PageAnon(page));
661 if (page_has_private(page)) {
662 try_to_free_buffers(page);
663 goto uncharge;
665 goto skip_unmap;
668 /* Establish migration ptes or remove ptes */
669 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
671 skip_unmap:
672 if (!page_mapped(page))
673 rc = move_to_new_page(newpage, page, remap_swapcache);
675 if (rc && remap_swapcache)
676 remove_migration_ptes(page, page);
678 /* Drop an anon_vma reference if we took one */
679 if (anon_vma)
680 drop_anon_vma(anon_vma);
682 uncharge:
683 if (!charge)
684 mem_cgroup_end_migration(mem, page, newpage);
685 unlock:
686 unlock_page(page);
688 if (rc != -EAGAIN) {
690 * A page that has been migrated has all references
691 * removed and will be freed. A page that has not been
692 * migrated will have kepts its references and be
693 * restored.
695 list_del(&page->lru);
696 dec_zone_page_state(page, NR_ISOLATED_ANON +
697 page_is_file_cache(page));
698 putback_lru_page(page);
701 move_newpage:
704 * Move the new page to the LRU. If migration was not successful
705 * then this will free the page.
707 putback_lru_page(newpage);
709 if (result) {
710 if (rc)
711 *result = rc;
712 else
713 *result = page_to_nid(newpage);
715 return rc;
719 * migrate_pages
721 * The function takes one list of pages to migrate and a function
722 * that determines from the page to be migrated and the private data
723 * the target of the move and allocates the page.
725 * The function returns after 10 attempts or if no pages
726 * are movable anymore because to has become empty
727 * or no retryable pages exist anymore. All pages will be
728 * returned to the LRU or freed.
730 * Return: Number of pages not migrated or error code.
732 int migrate_pages(struct list_head *from,
733 new_page_t get_new_page, unsigned long private, int offlining)
735 int retry = 1;
736 int nr_failed = 0;
737 int pass = 0;
738 struct page *page;
739 struct page *page2;
740 int swapwrite = current->flags & PF_SWAPWRITE;
741 int rc;
743 if (!swapwrite)
744 current->flags |= PF_SWAPWRITE;
746 for(pass = 0; pass < 10 && retry; pass++) {
747 retry = 0;
749 list_for_each_entry_safe(page, page2, from, lru) {
750 cond_resched();
752 rc = unmap_and_move(get_new_page, private,
753 page, pass > 2, offlining);
755 switch(rc) {
756 case -ENOMEM:
757 goto out;
758 case -EAGAIN:
759 retry++;
760 break;
761 case 0:
762 break;
763 default:
764 /* Permanent failure */
765 nr_failed++;
766 break;
770 rc = 0;
771 out:
772 if (!swapwrite)
773 current->flags &= ~PF_SWAPWRITE;
775 putback_lru_pages(from);
777 if (rc)
778 return rc;
780 return nr_failed + retry;
783 #ifdef CONFIG_NUMA
785 * Move a list of individual pages
787 struct page_to_node {
788 unsigned long addr;
789 struct page *page;
790 int node;
791 int status;
794 static struct page *new_page_node(struct page *p, unsigned long private,
795 int **result)
797 struct page_to_node *pm = (struct page_to_node *)private;
799 while (pm->node != MAX_NUMNODES && pm->page != p)
800 pm++;
802 if (pm->node == MAX_NUMNODES)
803 return NULL;
805 *result = &pm->status;
807 return alloc_pages_exact_node(pm->node,
808 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
812 * Move a set of pages as indicated in the pm array. The addr
813 * field must be set to the virtual address of the page to be moved
814 * and the node number must contain a valid target node.
815 * The pm array ends with node = MAX_NUMNODES.
817 static int do_move_page_to_node_array(struct mm_struct *mm,
818 struct page_to_node *pm,
819 int migrate_all)
821 int err;
822 struct page_to_node *pp;
823 LIST_HEAD(pagelist);
825 down_read(&mm->mmap_sem);
828 * Build a list of pages to migrate
830 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
831 struct vm_area_struct *vma;
832 struct page *page;
834 err = -EFAULT;
835 vma = find_vma(mm, pp->addr);
836 if (!vma || !vma_migratable(vma))
837 goto set_status;
839 page = follow_page(vma, pp->addr, FOLL_GET);
841 err = PTR_ERR(page);
842 if (IS_ERR(page))
843 goto set_status;
845 err = -ENOENT;
846 if (!page)
847 goto set_status;
849 /* Use PageReserved to check for zero page */
850 if (PageReserved(page) || PageKsm(page))
851 goto put_and_set;
853 pp->page = page;
854 err = page_to_nid(page);
856 if (err == pp->node)
858 * Node already in the right place
860 goto put_and_set;
862 err = -EACCES;
863 if (page_mapcount(page) > 1 &&
864 !migrate_all)
865 goto put_and_set;
867 err = isolate_lru_page(page);
868 if (!err) {
869 list_add_tail(&page->lru, &pagelist);
870 inc_zone_page_state(page, NR_ISOLATED_ANON +
871 page_is_file_cache(page));
873 put_and_set:
875 * Either remove the duplicate refcount from
876 * isolate_lru_page() or drop the page ref if it was
877 * not isolated.
879 put_page(page);
880 set_status:
881 pp->status = err;
884 err = 0;
885 if (!list_empty(&pagelist))
886 err = migrate_pages(&pagelist, new_page_node,
887 (unsigned long)pm, 0);
889 up_read(&mm->mmap_sem);
890 return err;
894 * Migrate an array of page address onto an array of nodes and fill
895 * the corresponding array of status.
897 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
898 unsigned long nr_pages,
899 const void __user * __user *pages,
900 const int __user *nodes,
901 int __user *status, int flags)
903 struct page_to_node *pm;
904 nodemask_t task_nodes;
905 unsigned long chunk_nr_pages;
906 unsigned long chunk_start;
907 int err;
909 task_nodes = cpuset_mems_allowed(task);
911 err = -ENOMEM;
912 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
913 if (!pm)
914 goto out;
916 migrate_prep();
919 * Store a chunk of page_to_node array in a page,
920 * but keep the last one as a marker
922 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
924 for (chunk_start = 0;
925 chunk_start < nr_pages;
926 chunk_start += chunk_nr_pages) {
927 int j;
929 if (chunk_start + chunk_nr_pages > nr_pages)
930 chunk_nr_pages = nr_pages - chunk_start;
932 /* fill the chunk pm with addrs and nodes from user-space */
933 for (j = 0; j < chunk_nr_pages; j++) {
934 const void __user *p;
935 int node;
937 err = -EFAULT;
938 if (get_user(p, pages + j + chunk_start))
939 goto out_pm;
940 pm[j].addr = (unsigned long) p;
942 if (get_user(node, nodes + j + chunk_start))
943 goto out_pm;
945 err = -ENODEV;
946 if (node < 0 || node >= MAX_NUMNODES)
947 goto out_pm;
949 if (!node_state(node, N_HIGH_MEMORY))
950 goto out_pm;
952 err = -EACCES;
953 if (!node_isset(node, task_nodes))
954 goto out_pm;
956 pm[j].node = node;
959 /* End marker for this chunk */
960 pm[chunk_nr_pages].node = MAX_NUMNODES;
962 /* Migrate this chunk */
963 err = do_move_page_to_node_array(mm, pm,
964 flags & MPOL_MF_MOVE_ALL);
965 if (err < 0)
966 goto out_pm;
968 /* Return status information */
969 for (j = 0; j < chunk_nr_pages; j++)
970 if (put_user(pm[j].status, status + j + chunk_start)) {
971 err = -EFAULT;
972 goto out_pm;
975 err = 0;
977 out_pm:
978 free_page((unsigned long)pm);
979 out:
980 return err;
984 * Determine the nodes of an array of pages and store it in an array of status.
986 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
987 const void __user **pages, int *status)
989 unsigned long i;
991 down_read(&mm->mmap_sem);
993 for (i = 0; i < nr_pages; i++) {
994 unsigned long addr = (unsigned long)(*pages);
995 struct vm_area_struct *vma;
996 struct page *page;
997 int err = -EFAULT;
999 vma = find_vma(mm, addr);
1000 if (!vma)
1001 goto set_status;
1003 page = follow_page(vma, addr, 0);
1005 err = PTR_ERR(page);
1006 if (IS_ERR(page))
1007 goto set_status;
1009 err = -ENOENT;
1010 /* Use PageReserved to check for zero page */
1011 if (!page || PageReserved(page) || PageKsm(page))
1012 goto set_status;
1014 err = page_to_nid(page);
1015 set_status:
1016 *status = err;
1018 pages++;
1019 status++;
1022 up_read(&mm->mmap_sem);
1026 * Determine the nodes of a user array of pages and store it in
1027 * a user array of status.
1029 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1030 const void __user * __user *pages,
1031 int __user *status)
1033 #define DO_PAGES_STAT_CHUNK_NR 16
1034 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1035 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1037 while (nr_pages) {
1038 unsigned long chunk_nr;
1040 chunk_nr = nr_pages;
1041 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1042 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1044 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1045 break;
1047 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1049 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1050 break;
1052 pages += chunk_nr;
1053 status += chunk_nr;
1054 nr_pages -= chunk_nr;
1056 return nr_pages ? -EFAULT : 0;
1060 * Move a list of pages in the address space of the currently executing
1061 * process.
1063 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1064 const void __user * __user *, pages,
1065 const int __user *, nodes,
1066 int __user *, status, int, flags)
1068 const struct cred *cred = current_cred(), *tcred;
1069 struct task_struct *task;
1070 struct mm_struct *mm;
1071 int err;
1073 /* Check flags */
1074 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1075 return -EINVAL;
1077 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1078 return -EPERM;
1080 /* Find the mm_struct */
1081 read_lock(&tasklist_lock);
1082 task = pid ? find_task_by_vpid(pid) : current;
1083 if (!task) {
1084 read_unlock(&tasklist_lock);
1085 return -ESRCH;
1087 mm = get_task_mm(task);
1088 read_unlock(&tasklist_lock);
1090 if (!mm)
1091 return -EINVAL;
1094 * Check if this process has the right to modify the specified
1095 * process. The right exists if the process has administrative
1096 * capabilities, superuser privileges or the same
1097 * userid as the target process.
1099 rcu_read_lock();
1100 tcred = __task_cred(task);
1101 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1102 cred->uid != tcred->suid && cred->uid != tcred->uid &&
1103 !capable(CAP_SYS_NICE)) {
1104 rcu_read_unlock();
1105 err = -EPERM;
1106 goto out;
1108 rcu_read_unlock();
1110 err = security_task_movememory(task);
1111 if (err)
1112 goto out;
1114 if (nodes) {
1115 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1116 flags);
1117 } else {
1118 err = do_pages_stat(mm, nr_pages, pages, status);
1121 out:
1122 mmput(mm);
1123 return err;
1127 * Call migration functions in the vma_ops that may prepare
1128 * memory in a vm for migration. migration functions may perform
1129 * the migration for vmas that do not have an underlying page struct.
1131 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1132 const nodemask_t *from, unsigned long flags)
1134 struct vm_area_struct *vma;
1135 int err = 0;
1137 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1138 if (vma->vm_ops && vma->vm_ops->migrate) {
1139 err = vma->vm_ops->migrate(vma, to, from, flags);
1140 if (err)
1141 break;
1144 return err;
1146 #endif