- patches.arch/x86_mce_intel_decode_physical_address.patch:
[linux-flexiantxendom0-3.2.10.git] / 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/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>
36
37 #include "internal.h"
38
39 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
40
41 /*
42  * migrate_prep() needs to be called before we start compiling a list of pages
43  * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
44  * undesirable, use migrate_prep_local()
45  */
46 int migrate_prep(void)
47 {
48         /*
49          * Clear the LRU lists so pages can be isolated.
50          * Note that pages may be moved off the LRU after we have
51          * drained them. Those pages will fail to migrate like other
52          * pages that may be busy.
53          */
54         lru_add_drain_all();
55
56         return 0;
57 }
58
59 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
60 int migrate_prep_local(void)
61 {
62         lru_add_drain();
63
64         return 0;
65 }
66 EXPORT_SYMBOL(migrate_prep);
67
68 /*
69  * Add isolated pages on the list back to the LRU under page lock
70  * to avoid leaking evictable pages back onto unevictable list.
71  */
72 void putback_lru_pages(struct list_head *l)
73 {
74         struct page *page;
75         struct page *page2;
76
77         list_for_each_entry_safe(page, page2, l, lru) {
78                 list_del(&page->lru);
79                 dec_zone_page_state(page, NR_ISOLATED_ANON +
80                                 page_is_file_cache(page));
81                 putback_lru_page(page);
82         }
83 }
84 EXPORT_SYMBOL(putback_lru_pages);
85
86 /*
87  * Restore a potential migration pte to a working pte entry
88  */
89 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
90                                  unsigned long addr, void *old)
91 {
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;
99
100         pgd = pgd_offset(mm, addr);
101         if (!pgd_present(*pgd))
102                 goto out;
103
104         pud = pud_offset(pgd, addr);
105         if (!pud_present(*pud))
106                 goto out;
107
108         pmd = pmd_offset(pud, addr);
109         if (!pmd_present(*pmd))
110                 goto out;
111
112         ptep = pte_offset_map(pmd, addr);
113
114         if (!is_swap_pte(*ptep)) {
115                 pte_unmap(ptep);
116                 goto out;
117         }
118
119         ptl = pte_lockptr(mm, pmd);
120         spin_lock(ptl);
121         pte = *ptep;
122         if (!is_swap_pte(pte))
123                 goto unlock;
124
125         entry = pte_to_swp_entry(pte);
126
127         if (!is_migration_entry(entry) ||
128             migration_entry_to_page(entry) != old)
129                 goto unlock;
130
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);
137
138         if (PageAnon(new))
139                 page_add_anon_rmap(new, vma, addr);
140         else
141                 page_add_file_rmap(new);
142
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;
149 }
150
151 /*
152  * Get rid of all migration entries and replace them by
153  * references to the indicated page.
154  */
155 static void remove_migration_ptes(struct page *old, struct page *new)
156 {
157         rmap_walk(new, remove_migration_pte, old);
158 }
159
160 /*
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.
164  *
165  * This function is called from do_swap_page().
166  */
167 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
168                                 unsigned long address)
169 {
170         pte_t *ptep, pte;
171         spinlock_t *ptl;
172         swp_entry_t entry;
173         struct page *page;
174
175         ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
176         pte = *ptep;
177         if (!is_swap_pte(pte))
178                 goto out;
179
180         entry = pte_to_swp_entry(pte);
181         if (!is_migration_entry(entry))
182                 goto out;
183
184         page = migration_entry_to_page(entry);
185
186         /*
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.
192          */
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);
201 }
202
203 /*
204  * Replace the page in the mapping.
205  *
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.
210  */
211 static int migrate_page_move_mapping(struct address_space *mapping,
212                 struct page *newpage, struct page *page)
213 {
214         int expected_count;
215         void **pslot;
216
217         if (!mapping) {
218                 /* Anonymous page without mapping */
219                 if (page_count(page) != 1)
220                         return -EAGAIN;
221                 return 0;
222         }
223
224         spin_lock_irq(&mapping->tree_lock);
225
226         pslot = radix_tree_lookup_slot(&mapping->page_tree,
227                                         page_index(page));
228
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;
234         }
235
236         if (!page_freeze_refs(page, expected_count)) {
237                 spin_unlock_irq(&mapping->tree_lock);
238                 return -EAGAIN;
239         }
240
241         /*
242          * Now we know that no one else is looking at the page.
243          */
244         get_page(newpage);      /* add cache reference */
245         if (PageSwapCache(page)) {
246                 SetPageSwapCache(newpage);
247                 set_page_private(newpage, page_private(page));
248         }
249
250         radix_tree_replace_slot(pslot, newpage);
251
252         page_unfreeze_refs(page, expected_count);
253         /*
254          * Drop cache reference from old page.
255          * We know this isn't the last reference.
256          */
257         __put_page(page);
258
259         /*
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.
264          *
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.
268          */
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);
274         }
275         spin_unlock_irq(&mapping->tree_lock);
276
277         return 0;
278 }
279
280 /*
281  * Copy the page to its new location
282  */
283 static void migrate_page_copy(struct page *newpage, struct page *page)
284 {
285         copy_highpage(newpage, page);
286
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);
302
303         if (PageDirty(page)) {
304                 clear_page_dirty_for_io(page);
305                 /*
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.
311                  */
312                 __set_page_dirty_nobuffers(newpage);
313         }
314
315         mlock_migrate_page(newpage, page);
316         ksm_migrate_page(newpage, page);
317
318         ClearPageSwapCache(page);
319         ClearPagePrivate(page);
320         set_page_private(page, 0);
321         page->mapping = NULL;
322
323         /*
324          * If any waiters have accumulated on the new page then
325          * wake them up.
326          */
327         if (PageWriteback(newpage))
328                 end_page_writeback(newpage);
329 }
330
331 /************************************************************
332  *                    Migration functions
333  ***********************************************************/
334
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)
338 {
339         return -EIO;
340 }
341 EXPORT_SYMBOL(fail_migrate_page);
342
343 /*
344  * Common logic to directly migrate a single page suitable for
345  * pages that do not use PagePrivate/PagePrivate2.
346  *
347  * Pages are locked upon entry and exit.
348  */
349 int migrate_page(struct address_space *mapping,
350                 struct page *newpage, struct page *page)
351 {
352         int rc;
353
354         BUG_ON(PageWriteback(page));    /* Writeback must be complete */
355
356         rc = migrate_page_move_mapping(mapping, newpage, page);
357
358         if (rc)
359                 return rc;
360
361         migrate_page_copy(newpage, page);
362         return 0;
363 }
364 EXPORT_SYMBOL(migrate_page);
365
366 #ifdef CONFIG_BLOCK
367 /*
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.
371  */
372 int buffer_migrate_page(struct address_space *mapping,
373                 struct page *newpage, struct page *page)
374 {
375         struct buffer_head *bh, *head;
376         int rc;
377
378         if (!page_has_buffers(page))
379                 return migrate_page(mapping, newpage, page);
380
381         head = page_buffers(page);
382
383         rc = migrate_page_move_mapping(mapping, newpage, page);
384
385         if (rc)
386                 return rc;
387
388         bh = head;
389         do {
390                 get_bh(bh);
391                 lock_buffer(bh);
392                 bh = bh->b_this_page;
393
394         } while (bh != head);
395
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);
401
402         bh = head;
403         do {
404                 set_bh_page(bh, newpage, bh_offset(bh));
405                 bh = bh->b_this_page;
406
407         } while (bh != head);
408
409         SetPagePrivate(newpage);
410
411         migrate_page_copy(newpage, page);
412
413         bh = head;
414         do {
415                 unlock_buffer(bh);
416                 put_bh(bh);
417                 bh = bh->b_this_page;
418
419         } while (bh != head);
420
421         return 0;
422 }
423 EXPORT_SYMBOL(buffer_migrate_page);
424 #endif
425
426 /*
427  * Writeback a page to clean the dirty state
428  */
429 static int writeout(struct address_space *mapping, struct page *page)
430 {
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
438         };
439         int rc;
440
441         if (!mapping->a_ops->writepage)
442                 /* No write method for the address space */
443                 return -EINVAL;
444
445         if (!clear_page_dirty_for_io(page))
446                 /* Someone else already triggered a write */
447                 return -EAGAIN;
448
449         /*
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.
456          */
457         remove_migration_ptes(page, page);
458
459         rc = mapping->a_ops->writepage(page, &wbc);
460
461         if (rc != AOP_WRITEPAGE_ACTIVATE)
462                 /* unlocked. Relock */
463                 lock_page(page);
464
465         return (rc < 0) ? -EIO : -EAGAIN;
466 }
467
468 /*
469  * Default handling if a filesystem does not provide a migration function.
470  */
471 static int fallback_migrate_page(struct address_space *mapping,
472         struct page *newpage, struct page *page)
473 {
474         if (PageDirty(page))
475                 return writeout(mapping, page);
476
477         /*
478          * Buffers may be managed in a filesystem specific way.
479          * We must have no buffers or drop them.
480          */
481         if (page_has_private(page) &&
482             !try_to_release_page(page, GFP_KERNEL))
483                 return -EAGAIN;
484
485         return migrate_page(mapping, newpage, page);
486 }
487
488 /*
489  * Move a page to a newly allocated page
490  * The page is locked and all ptes have been successfully removed.
491  *
492  * The new page will have replaced the old page if this function
493  * is successful.
494  *
495  * Return value:
496  *   < 0 - error code
497  *  == 0 - success
498  */
499 static int move_to_new_page(struct page *newpage, struct page *page,
500                                                 int remap_swapcache)
501 {
502         struct address_space *mapping;
503         int rc;
504
505         /*
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.
509          */
510         if (!trylock_page(newpage))
511                 BUG();
512
513         /* Prepare mapping for the new page.*/
514         newpage->index = page->index;
515         newpage->mapping = page->mapping;
516         if (PageSwapBacked(page))
517                 SetPageSwapBacked(newpage);
518
519         mapping = page_mapping(page);
520         if (!mapping)
521                 rc = migrate_page(mapping, newpage, page);
522         else if (mapping->a_ops->migratepage)
523                 /*
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.
529                  */
530                 rc = mapping->a_ops->migratepage(mapping,
531                                                 newpage, page);
532         else
533                 rc = fallback_migrate_page(mapping, newpage, page);
534
535         if (rc) {
536                 newpage->mapping = NULL;
537         } else {
538                 if (remap_swapcache)
539                         remove_migration_ptes(page, newpage);
540         }
541
542         unlock_page(newpage);
543
544         return rc;
545 }
546
547 /*
548  * Obtain the lock on page, remove all ptes and migrate the page
549  * to the newly allocated page in newpage.
550  */
551 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
552                         struct page *page, int force, int offlining)
553 {
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 rcu_locked = 0;
559         int charge = 0;
560         struct mem_cgroup *mem = NULL;
561         struct anon_vma *anon_vma = NULL;
562
563         if (!newpage)
564                 return -ENOMEM;
565
566         if (page_count(page) == 1) {
567                 /* page was freed from under us. So we are done. */
568                 goto move_newpage;
569         }
570
571         /* prepare cgroup just returns 0 or -ENOMEM */
572         rc = -EAGAIN;
573
574         if (!trylock_page(page)) {
575                 if (!force)
576                         goto move_newpage;
577                 lock_page(page);
578         }
579
580         /*
581          * Only memory hotplug's offline_pages() caller has locked out KSM,
582          * and can safely migrate a KSM page.  The other cases have skipped
583          * PageKsm along with PageReserved - but it is only now when we have
584          * the page lock that we can be certain it will not go KSM beneath us
585          * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
586          * its pagecount raised, but only here do we take the page lock which
587          * serializes that).
588          */
589         if (PageKsm(page) && !offlining) {
590                 rc = -EBUSY;
591                 goto unlock;
592         }
593
594         /* charge against new page */
595         charge = mem_cgroup_prepare_migration(page, newpage, &mem);
596         if (charge == -ENOMEM) {
597                 rc = -ENOMEM;
598                 goto unlock;
599         }
600         BUG_ON(charge);
601
602         if (PageWriteback(page)) {
603                 if (!force)
604                         goto uncharge;
605                 wait_on_page_writeback(page);
606         }
607         /*
608          * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
609          * we cannot notice that anon_vma is freed while we migrates a page.
610          * This rcu_read_lock() delays freeing anon_vma pointer until the end
611          * of migration. File cache pages are no problem because of page_lock()
612          * File Caches may use write_page() or lock_page() in migration, then,
613          * just care Anon page here.
614          */
615         if (PageAnon(page)) {
616                 rcu_read_lock();
617                 rcu_locked = 1;
618
619                 /* Determine how to safely use anon_vma */
620                 if (!page_mapped(page)) {
621                         if (!PageSwapCache(page))
622                                 goto rcu_unlock;
623
624                         /*
625                          * We cannot be sure that the anon_vma of an unmapped
626                          * swapcache page is safe to use because we don't
627                          * know in advance if the VMA that this page belonged
628                          * to still exists. If the VMA and others sharing the
629                          * data have been freed, then the anon_vma could
630                          * already be invalid.
631                          *
632                          * To avoid this possibility, swapcache pages get
633                          * migrated but are not remapped when migration
634                          * completes
635                          */
636                         remap_swapcache = 0;
637                 } else {
638                         /*
639                          * Take a reference count on the anon_vma if the
640                          * page is mapped so that it is guaranteed to
641                          * exist when the page is remapped later
642                          */
643                         anon_vma = page_anon_vma(page);
644                         atomic_inc(&anon_vma->external_refcount);
645                 }
646         }
647
648         /*
649          * Corner case handling:
650          * 1. When a new swap-cache page is read into, it is added to the LRU
651          * and treated as swapcache but it has no rmap yet.
652          * Calling try_to_unmap() against a page->mapping==NULL page will
653          * trigger a BUG.  So handle it here.
654          * 2. An orphaned page (see truncate_complete_page) might have
655          * fs-private metadata. The page can be picked up due to memory
656          * offlining.  Everywhere else except page reclaim, the page is
657          * invisible to the vm, so the page can not be migrated.  So try to
658          * free the metadata, so the page can be freed.
659          */
660         if (!page->mapping) {
661                 if (!PageAnon(page) && page_has_private(page)) {
662                         /*
663                          * Go direct to try_to_free_buffers() here because
664                          * a) that's what try_to_release_page() would do anyway
665                          * b) we may be under rcu_read_lock() here, so we can't
666                          *    use GFP_KERNEL which is what try_to_release_page()
667                          *    needs to be effective.
668                          */
669                         try_to_free_buffers(page);
670                         goto rcu_unlock;
671                 }
672                 goto skip_unmap;
673         }
674
675         /* Establish migration ptes or remove ptes */
676         try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
677
678 skip_unmap:
679         if (!page_mapped(page))
680                 rc = move_to_new_page(newpage, page, remap_swapcache);
681
682         if (rc && remap_swapcache)
683                 remove_migration_ptes(page, page);
684 rcu_unlock:
685
686         /* Drop an anon_vma reference if we took one */
687         if (anon_vma && atomic_dec_and_lock(&anon_vma->external_refcount, &anon_vma->lock)) {
688                 int empty = list_empty(&anon_vma->head);
689                 spin_unlock(&anon_vma->lock);
690                 if (empty)
691                         anon_vma_free(anon_vma);
692         }
693
694         if (rcu_locked)
695                 rcu_read_unlock();
696 uncharge:
697         if (!charge)
698                 mem_cgroup_end_migration(mem, page, newpage);
699 unlock:
700         unlock_page(page);
701
702         if (rc != -EAGAIN) {
703                 /*
704                  * A page that has been migrated has all references
705                  * removed and will be freed. A page that has not been
706                  * migrated will have kepts its references and be
707                  * restored.
708                  */
709                 list_del(&page->lru);
710                 if (PageMemError(page)) {
711                         if (rc == 0)
712                                 /*
713                                  * A page with a memory error that has
714                                  * been migrated will not be moved to
715                                  * the LRU.
716                                  */
717                                 goto move_newpage;
718                         else
719                                 /*
720                                  * The page failed to migrate and will not
721                                  * be added to the bad page list.  Clearing
722                                  * the error bit will allow another attempt
723                                  * to migrate if it gets another correctable
724                                  * error.
725                                  */
726                                 ClearPageMemError(page);
727                 }
728
729                 dec_zone_page_state(page, NR_ISOLATED_ANON +
730                                 page_is_file_cache(page));
731                 putback_lru_page(page);
732         }
733
734 move_newpage:
735
736         /*
737          * Move the new page to the LRU. If migration was not successful
738          * then this will free the page.
739          */
740         putback_lru_page(newpage);
741
742         if (result) {
743                 if (rc)
744                         *result = rc;
745                 else
746                         *result = page_to_nid(newpage);
747         }
748         return rc;
749 }
750
751 /*
752  * migrate_pages
753  *
754  * The function takes one list of pages to migrate and a function
755  * that determines from the page to be migrated and the private data
756  * the target of the move and allocates the page.
757  *
758  * The function returns after 10 attempts or if no pages
759  * are movable anymore because to has become empty
760  * or no retryable pages exist anymore. All pages will be
761  * returned to the LRU or freed.
762  *
763  * Return: Number of pages not migrated or error code.
764  */
765 int migrate_pages(struct list_head *from,
766                 new_page_t get_new_page, unsigned long private, int offlining)
767 {
768         int retry = 1;
769         int nr_failed = 0;
770         int pass = 0;
771         struct page *page;
772         struct page *page2;
773         int swapwrite = current->flags & PF_SWAPWRITE;
774         int rc;
775
776         if (!swapwrite)
777                 current->flags |= PF_SWAPWRITE;
778
779         for(pass = 0; pass < 10 && retry; pass++) {
780                 retry = 0;
781
782                 list_for_each_entry_safe(page, page2, from, lru) {
783                         cond_resched();
784
785                         rc = unmap_and_move(get_new_page, private,
786                                                 page, pass > 2, offlining);
787
788                         switch(rc) {
789                         case -ENOMEM:
790                                 goto out;
791                         case -EAGAIN:
792                                 retry++;
793                                 break;
794                         case 0:
795                                 break;
796                         default:
797                                 /* Permanent failure */
798                                 nr_failed++;
799                                 break;
800                         }
801                 }
802         }
803
804         if (rc != 0)
805                 list_for_each_entry_safe(page, page2, from, lru)
806                         if (PageMemError(page))
807                                 /*
808                                  * The page failed to migrate.  Clearing
809                                  * the error bit will allow another attempt
810                                  * to migrate if it gets another correctable
811                                  * error.
812                                  */
813                                 ClearPageMemError(page);
814         rc = 0;
815 out:
816         if (!swapwrite)
817                 current->flags &= ~PF_SWAPWRITE;
818
819         putback_lru_pages(from);
820
821         if (rc)
822                 return rc;
823
824         return nr_failed + retry;
825 }
826 EXPORT_SYMBOL(migrate_pages);
827
828 #ifdef CONFIG_NUMA
829 /*
830  * Move a list of individual pages
831  */
832 struct page_to_node {
833         unsigned long addr;
834         struct page *page;
835         int node;
836         int status;
837 };
838
839 static struct page *new_page_node(struct page *p, unsigned long private,
840                 int **result)
841 {
842         struct page_to_node *pm = (struct page_to_node *)private;
843
844         while (pm->node != MAX_NUMNODES && pm->page != p)
845                 pm++;
846
847         if (pm->node == MAX_NUMNODES)
848                 return NULL;
849
850         *result = &pm->status;
851
852         return alloc_pages_exact_node(pm->node,
853                                 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
854 }
855
856 /*
857  * Move a set of pages as indicated in the pm array. The addr
858  * field must be set to the virtual address of the page to be moved
859  * and the node number must contain a valid target node.
860  * The pm array ends with node = MAX_NUMNODES.
861  */
862 static int do_move_page_to_node_array(struct mm_struct *mm,
863                                       struct page_to_node *pm,
864                                       int migrate_all)
865 {
866         int err;
867         struct page_to_node *pp;
868         LIST_HEAD(pagelist);
869
870         down_read(&mm->mmap_sem);
871
872         /*
873          * Build a list of pages to migrate
874          */
875         for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
876                 struct vm_area_struct *vma;
877                 struct page *page;
878
879                 err = -EFAULT;
880                 vma = find_vma(mm, pp->addr);
881                 if (!vma || !vma_migratable(vma))
882                         goto set_status;
883
884                 page = follow_page(vma, pp->addr, FOLL_GET);
885
886                 err = PTR_ERR(page);
887                 if (IS_ERR(page))
888                         goto set_status;
889
890                 err = -ENOENT;
891                 if (!page)
892                         goto set_status;
893
894                 /* Use PageReserved to check for zero page */
895                 if (PageReserved(page) || PageKsm(page))
896                         goto put_and_set;
897
898                 pp->page = page;
899                 err = page_to_nid(page);
900
901                 if (err == pp->node)
902                         /*
903                          * Node already in the right place
904                          */
905                         goto put_and_set;
906
907                 err = -EACCES;
908                 if (page_mapcount(page) > 1 &&
909                                 !migrate_all)
910                         goto put_and_set;
911
912                 err = isolate_lru_page(page);
913                 if (!err) {
914                         list_add_tail(&page->lru, &pagelist);
915                         inc_zone_page_state(page, NR_ISOLATED_ANON +
916                                             page_is_file_cache(page));
917                 }
918 put_and_set:
919                 /*
920                  * Either remove the duplicate refcount from
921                  * isolate_lru_page() or drop the page ref if it was
922                  * not isolated.
923                  */
924                 put_page(page);
925 set_status:
926                 pp->status = err;
927         }
928
929         err = 0;
930         if (!list_empty(&pagelist))
931                 err = migrate_pages(&pagelist, new_page_node,
932                                 (unsigned long)pm, 0);
933
934         up_read(&mm->mmap_sem);
935         return err;
936 }
937
938 /*
939  * Migrate an array of page address onto an array of nodes and fill
940  * the corresponding array of status.
941  */
942 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
943                          unsigned long nr_pages,
944                          const void __user * __user *pages,
945                          const int __user *nodes,
946                          int __user *status, int flags)
947 {
948         struct page_to_node *pm;
949         nodemask_t task_nodes;
950         unsigned long chunk_nr_pages;
951         unsigned long chunk_start;
952         int err;
953
954         task_nodes = cpuset_mems_allowed(task);
955
956         err = -ENOMEM;
957         pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
958         if (!pm)
959                 goto out;
960
961         migrate_prep();
962
963         /*
964          * Store a chunk of page_to_node array in a page,
965          * but keep the last one as a marker
966          */
967         chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
968
969         for (chunk_start = 0;
970              chunk_start < nr_pages;
971              chunk_start += chunk_nr_pages) {
972                 int j;
973
974                 if (chunk_start + chunk_nr_pages > nr_pages)
975                         chunk_nr_pages = nr_pages - chunk_start;
976
977                 /* fill the chunk pm with addrs and nodes from user-space */
978                 for (j = 0; j < chunk_nr_pages; j++) {
979                         const void __user *p;
980                         int node;
981
982                         err = -EFAULT;
983                         if (get_user(p, pages + j + chunk_start))
984                                 goto out_pm;
985                         pm[j].addr = (unsigned long) p;
986
987                         if (get_user(node, nodes + j + chunk_start))
988                                 goto out_pm;
989
990                         err = -ENODEV;
991                         if (node < 0 || node >= MAX_NUMNODES)
992                                 goto out_pm;
993
994                         if (!node_state(node, N_HIGH_MEMORY))
995                                 goto out_pm;
996
997                         err = -EACCES;
998                         if (!node_isset(node, task_nodes))
999                                 goto out_pm;
1000
1001                         pm[j].node = node;
1002                 }
1003
1004                 /* End marker for this chunk */
1005                 pm[chunk_nr_pages].node = MAX_NUMNODES;
1006
1007                 /* Migrate this chunk */
1008                 err = do_move_page_to_node_array(mm, pm,
1009                                                  flags & MPOL_MF_MOVE_ALL);
1010                 if (err < 0)
1011                         goto out_pm;
1012
1013                 /* Return status information */
1014                 for (j = 0; j < chunk_nr_pages; j++)
1015                         if (put_user(pm[j].status, status + j + chunk_start)) {
1016                                 err = -EFAULT;
1017                                 goto out_pm;
1018                         }
1019         }
1020         err = 0;
1021
1022 out_pm:
1023         free_page((unsigned long)pm);
1024 out:
1025         return err;
1026 }
1027
1028 /*
1029  * Determine the nodes of an array of pages and store it in an array of status.
1030  */
1031 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1032                                 const void __user **pages, int *status)
1033 {
1034         unsigned long i;
1035
1036         down_read(&mm->mmap_sem);
1037
1038         for (i = 0; i < nr_pages; i++) {
1039                 unsigned long addr = (unsigned long)(*pages);
1040                 struct vm_area_struct *vma;
1041                 struct page *page;
1042                 int err = -EFAULT;
1043
1044                 vma = find_vma(mm, addr);
1045                 if (!vma)
1046                         goto set_status;
1047
1048                 page = follow_page(vma, addr, 0);
1049
1050                 err = PTR_ERR(page);
1051                 if (IS_ERR(page))
1052                         goto set_status;
1053
1054                 err = -ENOENT;
1055                 /* Use PageReserved to check for zero page */
1056                 if (!page || PageReserved(page) || PageKsm(page))
1057                         goto set_status;
1058
1059                 err = page_to_nid(page);
1060 set_status:
1061                 *status = err;
1062
1063                 pages++;
1064                 status++;
1065         }
1066
1067         up_read(&mm->mmap_sem);
1068 }
1069
1070 /*
1071  * Determine the nodes of a user array of pages and store it in
1072  * a user array of status.
1073  */
1074 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1075                          const void __user * __user *pages,
1076                          int __user *status)
1077 {
1078 #define DO_PAGES_STAT_CHUNK_NR 16
1079         const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1080         int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1081
1082         while (nr_pages) {
1083                 unsigned long chunk_nr;
1084
1085                 chunk_nr = nr_pages;
1086                 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1087                         chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1088
1089                 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1090                         break;
1091
1092                 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1093
1094                 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1095                         break;
1096
1097                 pages += chunk_nr;
1098                 status += chunk_nr;
1099                 nr_pages -= chunk_nr;
1100         }
1101         return nr_pages ? -EFAULT : 0;
1102 }
1103
1104 /*
1105  * Move a list of pages in the address space of the currently executing
1106  * process.
1107  */
1108 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1109                 const void __user * __user *, pages,
1110                 const int __user *, nodes,
1111                 int __user *, status, int, flags)
1112 {
1113         const struct cred *cred = current_cred(), *tcred;
1114         struct task_struct *task;
1115         struct mm_struct *mm;
1116         int err;
1117
1118         /* Check flags */
1119         if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1120                 return -EINVAL;
1121
1122         if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1123                 return -EPERM;
1124
1125         /* Find the mm_struct */
1126         read_lock(&tasklist_lock);
1127         task = pid ? find_task_by_vpid(pid) : current;
1128         if (!task) {
1129                 read_unlock(&tasklist_lock);
1130                 return -ESRCH;
1131         }
1132         mm = get_task_mm(task);
1133         read_unlock(&tasklist_lock);
1134
1135         if (!mm)
1136                 return -EINVAL;
1137
1138         /*
1139          * Check if this process has the right to modify the specified
1140          * process. The right exists if the process has administrative
1141          * capabilities, superuser privileges or the same
1142          * userid as the target process.
1143          */
1144         rcu_read_lock();
1145         tcred = __task_cred(task);
1146         if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1147             cred->uid  != tcred->suid && cred->uid  != tcred->uid &&
1148             !capable(CAP_SYS_NICE)) {
1149                 rcu_read_unlock();
1150                 err = -EPERM;
1151                 goto out;
1152         }
1153         rcu_read_unlock();
1154
1155         err = security_task_movememory(task);
1156         if (err)
1157                 goto out;
1158
1159         if (nodes) {
1160                 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1161                                     flags);
1162         } else {
1163                 err = do_pages_stat(mm, nr_pages, pages, status);
1164         }
1165
1166 out:
1167         mmput(mm);
1168         return err;
1169 }
1170
1171 /*
1172  * Call migration functions in the vma_ops that may prepare
1173  * memory in a vm for migration. migration functions may perform
1174  * the migration for vmas that do not have an underlying page struct.
1175  */
1176 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1177         const nodemask_t *from, unsigned long flags)
1178 {
1179         struct vm_area_struct *vma;
1180         int err = 0;
1181
1182         for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1183                 if (vma->vm_ops && vma->vm_ops->migrate) {
1184                         err = vma->vm_ops->migrate(vma, to, from, flags);
1185                         if (err)
1186                                 break;
1187                 }
1188         }
1189         return err;
1190 }
1191 #endif