mm: CONFIG_MMU for PG_mlocked
[safe/jmp/linux-2.6] / mm / migrate.c
1 /*
2  * Memory Migration functionality - linux/mm/migration.c
3  *
4  * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
5  *
6  * Page migration was first developed in the context of the memory hotplug
7  * project. The main authors of the migration code are:
8  *
9  * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10  * Hirokazu Takahashi <taka@valinux.co.jp>
11  * Dave Hansen <haveblue@us.ibm.com>
12  * Christoph Lameter
13  */
14
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/rmap.h>
25 #include <linux/topology.h>
26 #include <linux/cpu.h>
27 #include <linux/cpuset.h>
28 #include <linux/writeback.h>
29 #include <linux/mempolicy.h>
30 #include <linux/vmalloc.h>
31 #include <linux/security.h>
32 #include <linux/memcontrol.h>
33 #include <linux/syscalls.h>
34
35 #include "internal.h"
36
37 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
38
39 /*
40  * migrate_prep() needs to be called before we start compiling a list of pages
41  * to be migrated using isolate_lru_page().
42  */
43 int migrate_prep(void)
44 {
45         /*
46          * Clear the LRU lists so pages can be isolated.
47          * Note that pages may be moved off the LRU after we have
48          * drained them. Those pages will fail to migrate like other
49          * pages that may be busy.
50          */
51         lru_add_drain_all();
52
53         return 0;
54 }
55
56 /*
57  * Add isolated pages on the list back to the LRU under page lock
58  * to avoid leaking evictable pages back onto unevictable list.
59  *
60  * returns the number of pages put back.
61  */
62 int putback_lru_pages(struct list_head *l)
63 {
64         struct page *page;
65         struct page *page2;
66         int count = 0;
67
68         list_for_each_entry_safe(page, page2, l, lru) {
69                 list_del(&page->lru);
70                 dec_zone_page_state(page, NR_ISOLATED_ANON +
71                                 page_is_file_cache(page));
72                 putback_lru_page(page);
73                 count++;
74         }
75         return count;
76 }
77
78 /*
79  * Restore a potential migration pte to a working pte entry
80  */
81 static void remove_migration_pte(struct vm_area_struct *vma,
82                 struct page *old, struct page *new)
83 {
84         struct mm_struct *mm = vma->vm_mm;
85         swp_entry_t entry;
86         pgd_t *pgd;
87         pud_t *pud;
88         pmd_t *pmd;
89         pte_t *ptep, pte;
90         spinlock_t *ptl;
91         unsigned long addr = page_address_in_vma(new, vma);
92
93         if (addr == -EFAULT)
94                 return;
95
96         pgd = pgd_offset(mm, addr);
97         if (!pgd_present(*pgd))
98                 return;
99
100         pud = pud_offset(pgd, addr);
101         if (!pud_present(*pud))
102                 return;
103
104         pmd = pmd_offset(pud, addr);
105         if (!pmd_present(*pmd))
106                 return;
107
108         ptep = pte_offset_map(pmd, addr);
109
110         if (!is_swap_pte(*ptep)) {
111                 pte_unmap(ptep);
112                 return;
113         }
114
115         ptl = pte_lockptr(mm, pmd);
116         spin_lock(ptl);
117         pte = *ptep;
118         if (!is_swap_pte(pte))
119                 goto out;
120
121         entry = pte_to_swp_entry(pte);
122
123         if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
124                 goto out;
125
126         get_page(new);
127         pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
128         if (is_write_migration_entry(entry))
129                 pte = pte_mkwrite(pte);
130         flush_cache_page(vma, addr, pte_pfn(pte));
131         set_pte_at(mm, addr, ptep, pte);
132
133         if (PageAnon(new))
134                 page_add_anon_rmap(new, vma, addr);
135         else
136                 page_add_file_rmap(new);
137
138         /* No need to invalidate - it was non-present before */
139         update_mmu_cache(vma, addr, pte);
140
141 out:
142         pte_unmap_unlock(ptep, ptl);
143 }
144
145 /*
146  * Note that remove_file_migration_ptes will only work on regular mappings,
147  * Nonlinear mappings do not use migration entries.
148  */
149 static void remove_file_migration_ptes(struct page *old, struct page *new)
150 {
151         struct vm_area_struct *vma;
152         struct address_space *mapping = new->mapping;
153         struct prio_tree_iter iter;
154         pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
155
156         if (!mapping)
157                 return;
158
159         spin_lock(&mapping->i_mmap_lock);
160
161         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
162                 remove_migration_pte(vma, old, new);
163
164         spin_unlock(&mapping->i_mmap_lock);
165 }
166
167 /*
168  * Must hold mmap_sem lock on at least one of the vmas containing
169  * the page so that the anon_vma cannot vanish.
170  */
171 static void remove_anon_migration_ptes(struct page *old, struct page *new)
172 {
173         struct anon_vma *anon_vma;
174         struct vm_area_struct *vma;
175
176         /*
177          * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
178          */
179         anon_vma = page_anon_vma(new);
180         if (!anon_vma)
181                 return;
182
183         spin_lock(&anon_vma->lock);
184
185         list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
186                 remove_migration_pte(vma, old, new);
187
188         spin_unlock(&anon_vma->lock);
189 }
190
191 /*
192  * Get rid of all migration entries and replace them by
193  * references to the indicated page.
194  */
195 static void remove_migration_ptes(struct page *old, struct page *new)
196 {
197         if (PageAnon(new))
198                 remove_anon_migration_ptes(old, new);
199         else
200                 remove_file_migration_ptes(old, new);
201 }
202
203 /*
204  * Something used the pte of a page under migration. We need to
205  * get to the page and wait until migration is finished.
206  * When we return from this function the fault will be retried.
207  *
208  * This function is called from do_swap_page().
209  */
210 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
211                                 unsigned long address)
212 {
213         pte_t *ptep, pte;
214         spinlock_t *ptl;
215         swp_entry_t entry;
216         struct page *page;
217
218         ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
219         pte = *ptep;
220         if (!is_swap_pte(pte))
221                 goto out;
222
223         entry = pte_to_swp_entry(pte);
224         if (!is_migration_entry(entry))
225                 goto out;
226
227         page = migration_entry_to_page(entry);
228
229         /*
230          * Once radix-tree replacement of page migration started, page_count
231          * *must* be zero. And, we don't want to call wait_on_page_locked()
232          * against a page without get_page().
233          * So, we use get_page_unless_zero(), here. Even failed, page fault
234          * will occur again.
235          */
236         if (!get_page_unless_zero(page))
237                 goto out;
238         pte_unmap_unlock(ptep, ptl);
239         wait_on_page_locked(page);
240         put_page(page);
241         return;
242 out:
243         pte_unmap_unlock(ptep, ptl);
244 }
245
246 /*
247  * Replace the page in the mapping.
248  *
249  * The number of remaining references must be:
250  * 1 for anonymous pages without a mapping
251  * 2 for pages with a mapping
252  * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
253  */
254 static int migrate_page_move_mapping(struct address_space *mapping,
255                 struct page *newpage, struct page *page)
256 {
257         int expected_count;
258         void **pslot;
259
260         if (!mapping) {
261                 /* Anonymous page without mapping */
262                 if (page_count(page) != 1)
263                         return -EAGAIN;
264                 return 0;
265         }
266
267         spin_lock_irq(&mapping->tree_lock);
268
269         pslot = radix_tree_lookup_slot(&mapping->page_tree,
270                                         page_index(page));
271
272         expected_count = 2 + page_has_private(page);
273         if (page_count(page) != expected_count ||
274                         (struct page *)radix_tree_deref_slot(pslot) != page) {
275                 spin_unlock_irq(&mapping->tree_lock);
276                 return -EAGAIN;
277         }
278
279         if (!page_freeze_refs(page, expected_count)) {
280                 spin_unlock_irq(&mapping->tree_lock);
281                 return -EAGAIN;
282         }
283
284         /*
285          * Now we know that no one else is looking at the page.
286          */
287         get_page(newpage);      /* add cache reference */
288         if (PageSwapCache(page)) {
289                 SetPageSwapCache(newpage);
290                 set_page_private(newpage, page_private(page));
291         }
292
293         radix_tree_replace_slot(pslot, newpage);
294
295         page_unfreeze_refs(page, expected_count);
296         /*
297          * Drop cache reference from old page.
298          * We know this isn't the last reference.
299          */
300         __put_page(page);
301
302         /*
303          * If moved to a different zone then also account
304          * the page for that zone. Other VM counters will be
305          * taken care of when we establish references to the
306          * new page and drop references to the old page.
307          *
308          * Note that anonymous pages are accounted for
309          * via NR_FILE_PAGES and NR_ANON_PAGES if they
310          * are mapped to swap space.
311          */
312         __dec_zone_page_state(page, NR_FILE_PAGES);
313         __inc_zone_page_state(newpage, NR_FILE_PAGES);
314         if (PageSwapBacked(page)) {
315                 __dec_zone_page_state(page, NR_SHMEM);
316                 __inc_zone_page_state(newpage, NR_SHMEM);
317         }
318         spin_unlock_irq(&mapping->tree_lock);
319
320         return 0;
321 }
322
323 /*
324  * Copy the page to its new location
325  */
326 static void migrate_page_copy(struct page *newpage, struct page *page)
327 {
328         int anon;
329
330         copy_highpage(newpage, page);
331
332         if (PageError(page))
333                 SetPageError(newpage);
334         if (PageReferenced(page))
335                 SetPageReferenced(newpage);
336         if (PageUptodate(page))
337                 SetPageUptodate(newpage);
338         if (TestClearPageActive(page)) {
339                 VM_BUG_ON(PageUnevictable(page));
340                 SetPageActive(newpage);
341         } else
342                 unevictable_migrate_page(newpage, page);
343         if (PageChecked(page))
344                 SetPageChecked(newpage);
345         if (PageMappedToDisk(page))
346                 SetPageMappedToDisk(newpage);
347
348         if (PageDirty(page)) {
349                 clear_page_dirty_for_io(page);
350                 /*
351                  * Want to mark the page and the radix tree as dirty, and
352                  * redo the accounting that clear_page_dirty_for_io undid,
353                  * but we can't use set_page_dirty because that function
354                  * is actually a signal that all of the page has become dirty.
355                  * Wheras only part of our page may be dirty.
356                  */
357                 __set_page_dirty_nobuffers(newpage);
358         }
359
360         mlock_migrate_page(newpage, page);
361
362         ClearPageSwapCache(page);
363         ClearPagePrivate(page);
364         set_page_private(page, 0);
365         /* page->mapping contains a flag for PageAnon() */
366         anon = PageAnon(page);
367         page->mapping = NULL;
368
369         /*
370          * If any waiters have accumulated on the new page then
371          * wake them up.
372          */
373         if (PageWriteback(newpage))
374                 end_page_writeback(newpage);
375 }
376
377 /************************************************************
378  *                    Migration functions
379  ***********************************************************/
380
381 /* Always fail migration. Used for mappings that are not movable */
382 int fail_migrate_page(struct address_space *mapping,
383                         struct page *newpage, struct page *page)
384 {
385         return -EIO;
386 }
387 EXPORT_SYMBOL(fail_migrate_page);
388
389 /*
390  * Common logic to directly migrate a single page suitable for
391  * pages that do not use PagePrivate/PagePrivate2.
392  *
393  * Pages are locked upon entry and exit.
394  */
395 int migrate_page(struct address_space *mapping,
396                 struct page *newpage, struct page *page)
397 {
398         int rc;
399
400         BUG_ON(PageWriteback(page));    /* Writeback must be complete */
401
402         rc = migrate_page_move_mapping(mapping, newpage, page);
403
404         if (rc)
405                 return rc;
406
407         migrate_page_copy(newpage, page);
408         return 0;
409 }
410 EXPORT_SYMBOL(migrate_page);
411
412 #ifdef CONFIG_BLOCK
413 /*
414  * Migration function for pages with buffers. This function can only be used
415  * if the underlying filesystem guarantees that no other references to "page"
416  * exist.
417  */
418 int buffer_migrate_page(struct address_space *mapping,
419                 struct page *newpage, struct page *page)
420 {
421         struct buffer_head *bh, *head;
422         int rc;
423
424         if (!page_has_buffers(page))
425                 return migrate_page(mapping, newpage, page);
426
427         head = page_buffers(page);
428
429         rc = migrate_page_move_mapping(mapping, newpage, page);
430
431         if (rc)
432                 return rc;
433
434         bh = head;
435         do {
436                 get_bh(bh);
437                 lock_buffer(bh);
438                 bh = bh->b_this_page;
439
440         } while (bh != head);
441
442         ClearPagePrivate(page);
443         set_page_private(newpage, page_private(page));
444         set_page_private(page, 0);
445         put_page(page);
446         get_page(newpage);
447
448         bh = head;
449         do {
450                 set_bh_page(bh, newpage, bh_offset(bh));
451                 bh = bh->b_this_page;
452
453         } while (bh != head);
454
455         SetPagePrivate(newpage);
456
457         migrate_page_copy(newpage, page);
458
459         bh = head;
460         do {
461                 unlock_buffer(bh);
462                 put_bh(bh);
463                 bh = bh->b_this_page;
464
465         } while (bh != head);
466
467         return 0;
468 }
469 EXPORT_SYMBOL(buffer_migrate_page);
470 #endif
471
472 /*
473  * Writeback a page to clean the dirty state
474  */
475 static int writeout(struct address_space *mapping, struct page *page)
476 {
477         struct writeback_control wbc = {
478                 .sync_mode = WB_SYNC_NONE,
479                 .nr_to_write = 1,
480                 .range_start = 0,
481                 .range_end = LLONG_MAX,
482                 .nonblocking = 1,
483                 .for_reclaim = 1
484         };
485         int rc;
486
487         if (!mapping->a_ops->writepage)
488                 /* No write method for the address space */
489                 return -EINVAL;
490
491         if (!clear_page_dirty_for_io(page))
492                 /* Someone else already triggered a write */
493                 return -EAGAIN;
494
495         /*
496          * A dirty page may imply that the underlying filesystem has
497          * the page on some queue. So the page must be clean for
498          * migration. Writeout may mean we loose the lock and the
499          * page state is no longer what we checked for earlier.
500          * At this point we know that the migration attempt cannot
501          * be successful.
502          */
503         remove_migration_ptes(page, page);
504
505         rc = mapping->a_ops->writepage(page, &wbc);
506
507         if (rc != AOP_WRITEPAGE_ACTIVATE)
508                 /* unlocked. Relock */
509                 lock_page(page);
510
511         return (rc < 0) ? -EIO : -EAGAIN;
512 }
513
514 /*
515  * Default handling if a filesystem does not provide a migration function.
516  */
517 static int fallback_migrate_page(struct address_space *mapping,
518         struct page *newpage, struct page *page)
519 {
520         if (PageDirty(page))
521                 return writeout(mapping, page);
522
523         /*
524          * Buffers may be managed in a filesystem specific way.
525          * We must have no buffers or drop them.
526          */
527         if (page_has_private(page) &&
528             !try_to_release_page(page, GFP_KERNEL))
529                 return -EAGAIN;
530
531         return migrate_page(mapping, newpage, page);
532 }
533
534 /*
535  * Move a page to a newly allocated page
536  * The page is locked and all ptes have been successfully removed.
537  *
538  * The new page will have replaced the old page if this function
539  * is successful.
540  *
541  * Return value:
542  *   < 0 - error code
543  *  == 0 - success
544  */
545 static int move_to_new_page(struct page *newpage, struct page *page)
546 {
547         struct address_space *mapping;
548         int rc;
549
550         /*
551          * Block others from accessing the page when we get around to
552          * establishing additional references. We are the only one
553          * holding a reference to the new page at this point.
554          */
555         if (!trylock_page(newpage))
556                 BUG();
557
558         /* Prepare mapping for the new page.*/
559         newpage->index = page->index;
560         newpage->mapping = page->mapping;
561         if (PageSwapBacked(page))
562                 SetPageSwapBacked(newpage);
563
564         mapping = page_mapping(page);
565         if (!mapping)
566                 rc = migrate_page(mapping, newpage, page);
567         else if (mapping->a_ops->migratepage)
568                 /*
569                  * Most pages have a mapping and most filesystems
570                  * should provide a migration function. Anonymous
571                  * pages are part of swap space which also has its
572                  * own migration function. This is the most common
573                  * path for page migration.
574                  */
575                 rc = mapping->a_ops->migratepage(mapping,
576                                                 newpage, page);
577         else
578                 rc = fallback_migrate_page(mapping, newpage, page);
579
580         if (!rc) {
581                 remove_migration_ptes(page, newpage);
582         } else
583                 newpage->mapping = NULL;
584
585         unlock_page(newpage);
586
587         return rc;
588 }
589
590 /*
591  * Obtain the lock on page, remove all ptes and migrate the page
592  * to the newly allocated page in newpage.
593  */
594 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
595                         struct page *page, int force)
596 {
597         int rc = 0;
598         int *result = NULL;
599         struct page *newpage = get_new_page(page, private, &result);
600         int rcu_locked = 0;
601         int charge = 0;
602         struct mem_cgroup *mem = NULL;
603
604         if (!newpage)
605                 return -ENOMEM;
606
607         if (page_count(page) == 1) {
608                 /* page was freed from under us. So we are done. */
609                 goto move_newpage;
610         }
611
612         /* prepare cgroup just returns 0 or -ENOMEM */
613         rc = -EAGAIN;
614
615         if (!trylock_page(page)) {
616                 if (!force)
617                         goto move_newpage;
618                 lock_page(page);
619         }
620
621         /* charge against new page */
622         charge = mem_cgroup_prepare_migration(page, &mem);
623         if (charge == -ENOMEM) {
624                 rc = -ENOMEM;
625                 goto unlock;
626         }
627         BUG_ON(charge);
628
629         if (PageWriteback(page)) {
630                 if (!force)
631                         goto uncharge;
632                 wait_on_page_writeback(page);
633         }
634         /*
635          * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
636          * we cannot notice that anon_vma is freed while we migrates a page.
637          * This rcu_read_lock() delays freeing anon_vma pointer until the end
638          * of migration. File cache pages are no problem because of page_lock()
639          * File Caches may use write_page() or lock_page() in migration, then,
640          * just care Anon page here.
641          */
642         if (PageAnon(page)) {
643                 rcu_read_lock();
644                 rcu_locked = 1;
645         }
646
647         /*
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.
658          */
659         if (!page->mapping) {
660                 if (!PageAnon(page) && page_has_private(page)) {
661                         /*
662                          * Go direct to try_to_free_buffers() here because
663                          * a) that's what try_to_release_page() would do anyway
664                          * b) we may be under rcu_read_lock() here, so we can't
665                          *    use GFP_KERNEL which is what try_to_release_page()
666                          *    needs to be effective.
667                          */
668                         try_to_free_buffers(page);
669                         goto rcu_unlock;
670                 }
671                 goto skip_unmap;
672         }
673
674         /* Establish migration ptes or remove ptes */
675         try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
676
677 skip_unmap:
678         if (!page_mapped(page))
679                 rc = move_to_new_page(newpage, page);
680
681         if (rc)
682                 remove_migration_ptes(page, page);
683 rcu_unlock:
684         if (rcu_locked)
685                 rcu_read_unlock();
686 uncharge:
687         if (!charge)
688                 mem_cgroup_end_migration(mem, page, newpage);
689 unlock:
690         unlock_page(page);
691
692         if (rc != -EAGAIN) {
693                 /*
694                  * A page that has been migrated has all references
695                  * removed and will be freed. A page that has not been
696                  * migrated will have kepts its references and be
697                  * restored.
698                  */
699                 list_del(&page->lru);
700                 dec_zone_page_state(page, NR_ISOLATED_ANON +
701                                 page_is_file_cache(page));
702                 putback_lru_page(page);
703         }
704
705 move_newpage:
706
707         /*
708          * Move the new page to the LRU. If migration was not successful
709          * then this will free the page.
710          */
711         putback_lru_page(newpage);
712
713         if (result) {
714                 if (rc)
715                         *result = rc;
716                 else
717                         *result = page_to_nid(newpage);
718         }
719         return rc;
720 }
721
722 /*
723  * migrate_pages
724  *
725  * The function takes one list of pages to migrate and a function
726  * that determines from the page to be migrated and the private data
727  * the target of the move and allocates the page.
728  *
729  * The function returns after 10 attempts or if no pages
730  * are movable anymore because to has become empty
731  * or no retryable pages exist anymore. All pages will be
732  * returned to the LRU or freed.
733  *
734  * Return: Number of pages not migrated or error code.
735  */
736 int migrate_pages(struct list_head *from,
737                 new_page_t get_new_page, unsigned long private)
738 {
739         int retry = 1;
740         int nr_failed = 0;
741         int pass = 0;
742         struct page *page;
743         struct page *page2;
744         int swapwrite = current->flags & PF_SWAPWRITE;
745         int rc;
746
747         if (!swapwrite)
748                 current->flags |= PF_SWAPWRITE;
749
750         for(pass = 0; pass < 10 && retry; pass++) {
751                 retry = 0;
752
753                 list_for_each_entry_safe(page, page2, from, lru) {
754                         cond_resched();
755
756                         rc = unmap_and_move(get_new_page, private,
757                                                 page, pass > 2);
758
759                         switch(rc) {
760                         case -ENOMEM:
761                                 goto out;
762                         case -EAGAIN:
763                                 retry++;
764                                 break;
765                         case 0:
766                                 break;
767                         default:
768                                 /* Permanent failure */
769                                 nr_failed++;
770                                 break;
771                         }
772                 }
773         }
774         rc = 0;
775 out:
776         if (!swapwrite)
777                 current->flags &= ~PF_SWAPWRITE;
778
779         putback_lru_pages(from);
780
781         if (rc)
782                 return rc;
783
784         return nr_failed + retry;
785 }
786
787 #ifdef CONFIG_NUMA
788 /*
789  * Move a list of individual pages
790  */
791 struct page_to_node {
792         unsigned long addr;
793         struct page *page;
794         int node;
795         int status;
796 };
797
798 static struct page *new_page_node(struct page *p, unsigned long private,
799                 int **result)
800 {
801         struct page_to_node *pm = (struct page_to_node *)private;
802
803         while (pm->node != MAX_NUMNODES && pm->page != p)
804                 pm++;
805
806         if (pm->node == MAX_NUMNODES)
807                 return NULL;
808
809         *result = &pm->status;
810
811         return alloc_pages_exact_node(pm->node,
812                                 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
813 }
814
815 /*
816  * Move a set of pages as indicated in the pm array. The addr
817  * field must be set to the virtual address of the page to be moved
818  * and the node number must contain a valid target node.
819  * The pm array ends with node = MAX_NUMNODES.
820  */
821 static int do_move_page_to_node_array(struct mm_struct *mm,
822                                       struct page_to_node *pm,
823                                       int migrate_all)
824 {
825         int err;
826         struct page_to_node *pp;
827         LIST_HEAD(pagelist);
828
829         down_read(&mm->mmap_sem);
830
831         /*
832          * Build a list of pages to migrate
833          */
834         for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
835                 struct vm_area_struct *vma;
836                 struct page *page;
837
838                 err = -EFAULT;
839                 vma = find_vma(mm, pp->addr);
840                 if (!vma || !vma_migratable(vma))
841                         goto set_status;
842
843                 page = follow_page(vma, pp->addr, FOLL_GET);
844
845                 err = PTR_ERR(page);
846                 if (IS_ERR(page))
847                         goto set_status;
848
849                 err = -ENOENT;
850                 if (!page)
851                         goto set_status;
852
853                 if (PageReserved(page))         /* Check for zero page */
854                         goto put_and_set;
855
856                 pp->page = page;
857                 err = page_to_nid(page);
858
859                 if (err == pp->node)
860                         /*
861                          * Node already in the right place
862                          */
863                         goto put_and_set;
864
865                 err = -EACCES;
866                 if (page_mapcount(page) > 1 &&
867                                 !migrate_all)
868                         goto put_and_set;
869
870                 err = isolate_lru_page(page);
871                 if (!err) {
872                         list_add_tail(&page->lru, &pagelist);
873                         inc_zone_page_state(page, NR_ISOLATED_ANON +
874                                             page_is_file_cache(page));
875                 }
876 put_and_set:
877                 /*
878                  * Either remove the duplicate refcount from
879                  * isolate_lru_page() or drop the page ref if it was
880                  * not isolated.
881                  */
882                 put_page(page);
883 set_status:
884                 pp->status = err;
885         }
886
887         err = 0;
888         if (!list_empty(&pagelist))
889                 err = migrate_pages(&pagelist, new_page_node,
890                                 (unsigned long)pm);
891
892         up_read(&mm->mmap_sem);
893         return err;
894 }
895
896 /*
897  * Migrate an array of page address onto an array of nodes and fill
898  * the corresponding array of status.
899  */
900 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
901                          unsigned long nr_pages,
902                          const void __user * __user *pages,
903                          const int __user *nodes,
904                          int __user *status, int flags)
905 {
906         struct page_to_node *pm;
907         nodemask_t task_nodes;
908         unsigned long chunk_nr_pages;
909         unsigned long chunk_start;
910         int err;
911
912         task_nodes = cpuset_mems_allowed(task);
913
914         err = -ENOMEM;
915         pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
916         if (!pm)
917                 goto out;
918
919         migrate_prep();
920
921         /*
922          * Store a chunk of page_to_node array in a page,
923          * but keep the last one as a marker
924          */
925         chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
926
927         for (chunk_start = 0;
928              chunk_start < nr_pages;
929              chunk_start += chunk_nr_pages) {
930                 int j;
931
932                 if (chunk_start + chunk_nr_pages > nr_pages)
933                         chunk_nr_pages = nr_pages - chunk_start;
934
935                 /* fill the chunk pm with addrs and nodes from user-space */
936                 for (j = 0; j < chunk_nr_pages; j++) {
937                         const void __user *p;
938                         int node;
939
940                         err = -EFAULT;
941                         if (get_user(p, pages + j + chunk_start))
942                                 goto out_pm;
943                         pm[j].addr = (unsigned long) p;
944
945                         if (get_user(node, nodes + j + chunk_start))
946                                 goto out_pm;
947
948                         err = -ENODEV;
949                         if (!node_state(node, N_HIGH_MEMORY))
950                                 goto out_pm;
951
952                         err = -EACCES;
953                         if (!node_isset(node, task_nodes))
954                                 goto out_pm;
955
956                         pm[j].node = node;
957                 }
958
959                 /* End marker for this chunk */
960                 pm[chunk_nr_pages].node = MAX_NUMNODES;
961
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;
967
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;
973                         }
974         }
975         err = 0;
976
977 out_pm:
978         free_page((unsigned long)pm);
979 out:
980         return err;
981 }
982
983 /*
984  * Determine the nodes of an array of pages and store it in an array of status.
985  */
986 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
987                                 const void __user **pages, int *status)
988 {
989         unsigned long i;
990
991         down_read(&mm->mmap_sem);
992
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;
998
999                 vma = find_vma(mm, addr);
1000                 if (!vma)
1001                         goto set_status;
1002
1003                 page = follow_page(vma, addr, 0);
1004
1005                 err = PTR_ERR(page);
1006                 if (IS_ERR(page))
1007                         goto set_status;
1008
1009                 err = -ENOENT;
1010                 /* Use PageReserved to check for zero page */
1011                 if (!page || PageReserved(page))
1012                         goto set_status;
1013
1014                 err = page_to_nid(page);
1015 set_status:
1016                 *status = err;
1017
1018                 pages++;
1019                 status++;
1020         }
1021
1022         up_read(&mm->mmap_sem);
1023 }
1024
1025 /*
1026  * Determine the nodes of a user array of pages and store it in
1027  * a user array of status.
1028  */
1029 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1030                          const void __user * __user *pages,
1031                          int __user *status)
1032 {
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];
1036         unsigned long i, chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1037         int err;
1038
1039         for (i = 0; i < nr_pages; i += chunk_nr) {
1040                 if (chunk_nr > nr_pages - i)
1041                         chunk_nr = nr_pages - i;
1042
1043                 err = copy_from_user(chunk_pages, &pages[i],
1044                                      chunk_nr * sizeof(*chunk_pages));
1045                 if (err) {
1046                         err = -EFAULT;
1047                         goto out;
1048                 }
1049
1050                 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1051
1052                 err = copy_to_user(&status[i], chunk_status,
1053                                    chunk_nr * sizeof(*chunk_status));
1054                 if (err) {
1055                         err = -EFAULT;
1056                         goto out;
1057                 }
1058         }
1059         err = 0;
1060
1061 out:
1062         return err;
1063 }
1064
1065 /*
1066  * Move a list of pages in the address space of the currently executing
1067  * process.
1068  */
1069 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1070                 const void __user * __user *, pages,
1071                 const int __user *, nodes,
1072                 int __user *, status, int, flags)
1073 {
1074         const struct cred *cred = current_cred(), *tcred;
1075         struct task_struct *task;
1076         struct mm_struct *mm;
1077         int err;
1078
1079         /* Check flags */
1080         if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1081                 return -EINVAL;
1082
1083         if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1084                 return -EPERM;
1085
1086         /* Find the mm_struct */
1087         read_lock(&tasklist_lock);
1088         task = pid ? find_task_by_vpid(pid) : current;
1089         if (!task) {
1090                 read_unlock(&tasklist_lock);
1091                 return -ESRCH;
1092         }
1093         mm = get_task_mm(task);
1094         read_unlock(&tasklist_lock);
1095
1096         if (!mm)
1097                 return -EINVAL;
1098
1099         /*
1100          * Check if this process has the right to modify the specified
1101          * process. The right exists if the process has administrative
1102          * capabilities, superuser privileges or the same
1103          * userid as the target process.
1104          */
1105         rcu_read_lock();
1106         tcred = __task_cred(task);
1107         if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1108             cred->uid  != tcred->suid && cred->uid  != tcred->uid &&
1109             !capable(CAP_SYS_NICE)) {
1110                 rcu_read_unlock();
1111                 err = -EPERM;
1112                 goto out;
1113         }
1114         rcu_read_unlock();
1115
1116         err = security_task_movememory(task);
1117         if (err)
1118                 goto out;
1119
1120         if (nodes) {
1121                 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1122                                     flags);
1123         } else {
1124                 err = do_pages_stat(mm, nr_pages, pages, status);
1125         }
1126
1127 out:
1128         mmput(mm);
1129         return err;
1130 }
1131
1132 /*
1133  * Call migration functions in the vma_ops that may prepare
1134  * memory in a vm for migration. migration functions may perform
1135  * the migration for vmas that do not have an underlying page struct.
1136  */
1137 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1138         const nodemask_t *from, unsigned long flags)
1139 {
1140         struct vm_area_struct *vma;
1141         int err = 0;
1142
1143         for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1144                 if (vma->vm_ops && vma->vm_ops->migrate) {
1145                         err = vma->vm_ops->migrate(vma, to, from, flags);
1146                         if (err)
1147                                 break;
1148                 }
1149         }
1150         return err;
1151 }
1152 #endif