]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - mm/migrate.c
mm: compaction: determine if dirty pages can be migrated without blocking within...
[mirror_ubuntu-artful-kernel.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/export.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/hugetlb.h>
36 #include <linux/gfp.h>
37
38 #include <asm/tlbflush.h>
39
40 #include "internal.h"
41
42 /*
43 * migrate_prep() needs to be called before we start compiling a list of pages
44 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
45 * undesirable, use migrate_prep_local()
46 */
47 int migrate_prep(void)
48 {
49 /*
50 * Clear the LRU lists so pages can be isolated.
51 * Note that pages may be moved off the LRU after we have
52 * drained them. Those pages will fail to migrate like other
53 * pages that may be busy.
54 */
55 lru_add_drain_all();
56
57 return 0;
58 }
59
60 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
61 int migrate_prep_local(void)
62 {
63 lru_add_drain();
64
65 return 0;
66 }
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
85 /*
86 * Restore a potential migration pte to a working pte entry
87 */
88 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
89 unsigned long addr, void *old)
90 {
91 struct mm_struct *mm = vma->vm_mm;
92 swp_entry_t entry;
93 pgd_t *pgd;
94 pud_t *pud;
95 pmd_t *pmd;
96 pte_t *ptep, pte;
97 spinlock_t *ptl;
98
99 if (unlikely(PageHuge(new))) {
100 ptep = huge_pte_offset(mm, addr);
101 if (!ptep)
102 goto out;
103 ptl = &mm->page_table_lock;
104 } else {
105 pgd = pgd_offset(mm, addr);
106 if (!pgd_present(*pgd))
107 goto out;
108
109 pud = pud_offset(pgd, addr);
110 if (!pud_present(*pud))
111 goto out;
112
113 pmd = pmd_offset(pud, addr);
114 if (pmd_trans_huge(*pmd))
115 goto out;
116 if (!pmd_present(*pmd))
117 goto out;
118
119 ptep = pte_offset_map(pmd, addr);
120
121 /*
122 * Peek to check is_swap_pte() before taking ptlock? No, we
123 * can race mremap's move_ptes(), which skips anon_vma lock.
124 */
125
126 ptl = pte_lockptr(mm, pmd);
127 }
128
129 spin_lock(ptl);
130 pte = *ptep;
131 if (!is_swap_pte(pte))
132 goto unlock;
133
134 entry = pte_to_swp_entry(pte);
135
136 if (!is_migration_entry(entry) ||
137 migration_entry_to_page(entry) != old)
138 goto unlock;
139
140 get_page(new);
141 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
142 if (is_write_migration_entry(entry))
143 pte = pte_mkwrite(pte);
144 #ifdef CONFIG_HUGETLB_PAGE
145 if (PageHuge(new))
146 pte = pte_mkhuge(pte);
147 #endif
148 flush_cache_page(vma, addr, pte_pfn(pte));
149 set_pte_at(mm, addr, ptep, pte);
150
151 if (PageHuge(new)) {
152 if (PageAnon(new))
153 hugepage_add_anon_rmap(new, vma, addr);
154 else
155 page_dup_rmap(new);
156 } else if (PageAnon(new))
157 page_add_anon_rmap(new, vma, addr);
158 else
159 page_add_file_rmap(new);
160
161 /* No need to invalidate - it was non-present before */
162 update_mmu_cache(vma, addr, ptep);
163 unlock:
164 pte_unmap_unlock(ptep, ptl);
165 out:
166 return SWAP_AGAIN;
167 }
168
169 /*
170 * Get rid of all migration entries and replace them by
171 * references to the indicated page.
172 */
173 static void remove_migration_ptes(struct page *old, struct page *new)
174 {
175 rmap_walk(new, remove_migration_pte, old);
176 }
177
178 /*
179 * Something used the pte of a page under migration. We need to
180 * get to the page and wait until migration is finished.
181 * When we return from this function the fault will be retried.
182 */
183 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
184 unsigned long address)
185 {
186 pte_t *ptep, pte;
187 spinlock_t *ptl;
188 swp_entry_t entry;
189 struct page *page;
190
191 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
192 pte = *ptep;
193 if (!is_swap_pte(pte))
194 goto out;
195
196 entry = pte_to_swp_entry(pte);
197 if (!is_migration_entry(entry))
198 goto out;
199
200 page = migration_entry_to_page(entry);
201
202 /*
203 * Once radix-tree replacement of page migration started, page_count
204 * *must* be zero. And, we don't want to call wait_on_page_locked()
205 * against a page without get_page().
206 * So, we use get_page_unless_zero(), here. Even failed, page fault
207 * will occur again.
208 */
209 if (!get_page_unless_zero(page))
210 goto out;
211 pte_unmap_unlock(ptep, ptl);
212 wait_on_page_locked(page);
213 put_page(page);
214 return;
215 out:
216 pte_unmap_unlock(ptep, ptl);
217 }
218
219 #ifdef CONFIG_BLOCK
220 /* Returns true if all buffers are successfully locked */
221 static bool buffer_migrate_lock_buffers(struct buffer_head *head, bool sync)
222 {
223 struct buffer_head *bh = head;
224
225 /* Simple case, sync compaction */
226 if (sync) {
227 do {
228 get_bh(bh);
229 lock_buffer(bh);
230 bh = bh->b_this_page;
231
232 } while (bh != head);
233
234 return true;
235 }
236
237 /* async case, we cannot block on lock_buffer so use trylock_buffer */
238 do {
239 get_bh(bh);
240 if (!trylock_buffer(bh)) {
241 /*
242 * We failed to lock the buffer and cannot stall in
243 * async migration. Release the taken locks
244 */
245 struct buffer_head *failed_bh = bh;
246 put_bh(failed_bh);
247 bh = head;
248 while (bh != failed_bh) {
249 unlock_buffer(bh);
250 put_bh(bh);
251 bh = bh->b_this_page;
252 }
253 return false;
254 }
255
256 bh = bh->b_this_page;
257 } while (bh != head);
258 return true;
259 }
260 #else
261 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
262 bool sync)
263 {
264 return true;
265 }
266 #endif /* CONFIG_BLOCK */
267
268 /*
269 * Replace the page in the mapping.
270 *
271 * The number of remaining references must be:
272 * 1 for anonymous pages without a mapping
273 * 2 for pages with a mapping
274 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
275 */
276 static int migrate_page_move_mapping(struct address_space *mapping,
277 struct page *newpage, struct page *page,
278 struct buffer_head *head, bool sync)
279 {
280 int expected_count;
281 void **pslot;
282
283 if (!mapping) {
284 /* Anonymous page without mapping */
285 if (page_count(page) != 1)
286 return -EAGAIN;
287 return 0;
288 }
289
290 spin_lock_irq(&mapping->tree_lock);
291
292 pslot = radix_tree_lookup_slot(&mapping->page_tree,
293 page_index(page));
294
295 expected_count = 2 + page_has_private(page);
296 if (page_count(page) != expected_count ||
297 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
298 spin_unlock_irq(&mapping->tree_lock);
299 return -EAGAIN;
300 }
301
302 if (!page_freeze_refs(page, expected_count)) {
303 spin_unlock_irq(&mapping->tree_lock);
304 return -EAGAIN;
305 }
306
307 /*
308 * In the async migration case of moving a page with buffers, lock the
309 * buffers using trylock before the mapping is moved. If the mapping
310 * was moved, we later failed to lock the buffers and could not move
311 * the mapping back due to an elevated page count, we would have to
312 * block waiting on other references to be dropped.
313 */
314 if (!sync && head && !buffer_migrate_lock_buffers(head, sync)) {
315 page_unfreeze_refs(page, expected_count);
316 spin_unlock_irq(&mapping->tree_lock);
317 return -EAGAIN;
318 }
319
320 /*
321 * Now we know that no one else is looking at the page.
322 */
323 get_page(newpage); /* add cache reference */
324 if (PageSwapCache(page)) {
325 SetPageSwapCache(newpage);
326 set_page_private(newpage, page_private(page));
327 }
328
329 radix_tree_replace_slot(pslot, newpage);
330
331 /*
332 * Drop cache reference from old page by unfreezing
333 * to one less reference.
334 * We know this isn't the last reference.
335 */
336 page_unfreeze_refs(page, expected_count - 1);
337
338 /*
339 * If moved to a different zone then also account
340 * the page for that zone. Other VM counters will be
341 * taken care of when we establish references to the
342 * new page and drop references to the old page.
343 *
344 * Note that anonymous pages are accounted for
345 * via NR_FILE_PAGES and NR_ANON_PAGES if they
346 * are mapped to swap space.
347 */
348 __dec_zone_page_state(page, NR_FILE_PAGES);
349 __inc_zone_page_state(newpage, NR_FILE_PAGES);
350 if (!PageSwapCache(page) && PageSwapBacked(page)) {
351 __dec_zone_page_state(page, NR_SHMEM);
352 __inc_zone_page_state(newpage, NR_SHMEM);
353 }
354 spin_unlock_irq(&mapping->tree_lock);
355
356 return 0;
357 }
358
359 /*
360 * The expected number of remaining references is the same as that
361 * of migrate_page_move_mapping().
362 */
363 int migrate_huge_page_move_mapping(struct address_space *mapping,
364 struct page *newpage, struct page *page)
365 {
366 int expected_count;
367 void **pslot;
368
369 if (!mapping) {
370 if (page_count(page) != 1)
371 return -EAGAIN;
372 return 0;
373 }
374
375 spin_lock_irq(&mapping->tree_lock);
376
377 pslot = radix_tree_lookup_slot(&mapping->page_tree,
378 page_index(page));
379
380 expected_count = 2 + page_has_private(page);
381 if (page_count(page) != expected_count ||
382 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
383 spin_unlock_irq(&mapping->tree_lock);
384 return -EAGAIN;
385 }
386
387 if (!page_freeze_refs(page, expected_count)) {
388 spin_unlock_irq(&mapping->tree_lock);
389 return -EAGAIN;
390 }
391
392 get_page(newpage);
393
394 radix_tree_replace_slot(pslot, newpage);
395
396 page_unfreeze_refs(page, expected_count - 1);
397
398 spin_unlock_irq(&mapping->tree_lock);
399 return 0;
400 }
401
402 /*
403 * Copy the page to its new location
404 */
405 void migrate_page_copy(struct page *newpage, struct page *page)
406 {
407 if (PageHuge(page))
408 copy_huge_page(newpage, page);
409 else
410 copy_highpage(newpage, page);
411
412 if (PageError(page))
413 SetPageError(newpage);
414 if (PageReferenced(page))
415 SetPageReferenced(newpage);
416 if (PageUptodate(page))
417 SetPageUptodate(newpage);
418 if (TestClearPageActive(page)) {
419 VM_BUG_ON(PageUnevictable(page));
420 SetPageActive(newpage);
421 } else if (TestClearPageUnevictable(page))
422 SetPageUnevictable(newpage);
423 if (PageChecked(page))
424 SetPageChecked(newpage);
425 if (PageMappedToDisk(page))
426 SetPageMappedToDisk(newpage);
427
428 if (PageDirty(page)) {
429 clear_page_dirty_for_io(page);
430 /*
431 * Want to mark the page and the radix tree as dirty, and
432 * redo the accounting that clear_page_dirty_for_io undid,
433 * but we can't use set_page_dirty because that function
434 * is actually a signal that all of the page has become dirty.
435 * Whereas only part of our page may be dirty.
436 */
437 __set_page_dirty_nobuffers(newpage);
438 }
439
440 mlock_migrate_page(newpage, page);
441 ksm_migrate_page(newpage, page);
442
443 ClearPageSwapCache(page);
444 ClearPagePrivate(page);
445 set_page_private(page, 0);
446 page->mapping = NULL;
447
448 /*
449 * If any waiters have accumulated on the new page then
450 * wake them up.
451 */
452 if (PageWriteback(newpage))
453 end_page_writeback(newpage);
454 }
455
456 /************************************************************
457 * Migration functions
458 ***********************************************************/
459
460 /* Always fail migration. Used for mappings that are not movable */
461 int fail_migrate_page(struct address_space *mapping,
462 struct page *newpage, struct page *page)
463 {
464 return -EIO;
465 }
466 EXPORT_SYMBOL(fail_migrate_page);
467
468 /*
469 * Common logic to directly migrate a single page suitable for
470 * pages that do not use PagePrivate/PagePrivate2.
471 *
472 * Pages are locked upon entry and exit.
473 */
474 int migrate_page(struct address_space *mapping,
475 struct page *newpage, struct page *page, bool sync)
476 {
477 int rc;
478
479 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
480
481 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, sync);
482
483 if (rc)
484 return rc;
485
486 migrate_page_copy(newpage, page);
487 return 0;
488 }
489 EXPORT_SYMBOL(migrate_page);
490
491 #ifdef CONFIG_BLOCK
492 /*
493 * Migration function for pages with buffers. This function can only be used
494 * if the underlying filesystem guarantees that no other references to "page"
495 * exist.
496 */
497 int buffer_migrate_page(struct address_space *mapping,
498 struct page *newpage, struct page *page, bool sync)
499 {
500 struct buffer_head *bh, *head;
501 int rc;
502
503 if (!page_has_buffers(page))
504 return migrate_page(mapping, newpage, page, sync);
505
506 head = page_buffers(page);
507
508 rc = migrate_page_move_mapping(mapping, newpage, page, head, sync);
509
510 if (rc)
511 return rc;
512
513 /*
514 * In the async case, migrate_page_move_mapping locked the buffers
515 * with an IRQ-safe spinlock held. In the sync case, the buffers
516 * need to be locked now
517 */
518 if (sync)
519 BUG_ON(!buffer_migrate_lock_buffers(head, sync));
520
521 ClearPagePrivate(page);
522 set_page_private(newpage, page_private(page));
523 set_page_private(page, 0);
524 put_page(page);
525 get_page(newpage);
526
527 bh = head;
528 do {
529 set_bh_page(bh, newpage, bh_offset(bh));
530 bh = bh->b_this_page;
531
532 } while (bh != head);
533
534 SetPagePrivate(newpage);
535
536 migrate_page_copy(newpage, page);
537
538 bh = head;
539 do {
540 unlock_buffer(bh);
541 put_bh(bh);
542 bh = bh->b_this_page;
543
544 } while (bh != head);
545
546 return 0;
547 }
548 EXPORT_SYMBOL(buffer_migrate_page);
549 #endif
550
551 /*
552 * Writeback a page to clean the dirty state
553 */
554 static int writeout(struct address_space *mapping, struct page *page)
555 {
556 struct writeback_control wbc = {
557 .sync_mode = WB_SYNC_NONE,
558 .nr_to_write = 1,
559 .range_start = 0,
560 .range_end = LLONG_MAX,
561 .for_reclaim = 1
562 };
563 int rc;
564
565 if (!mapping->a_ops->writepage)
566 /* No write method for the address space */
567 return -EINVAL;
568
569 if (!clear_page_dirty_for_io(page))
570 /* Someone else already triggered a write */
571 return -EAGAIN;
572
573 /*
574 * A dirty page may imply that the underlying filesystem has
575 * the page on some queue. So the page must be clean for
576 * migration. Writeout may mean we loose the lock and the
577 * page state is no longer what we checked for earlier.
578 * At this point we know that the migration attempt cannot
579 * be successful.
580 */
581 remove_migration_ptes(page, page);
582
583 rc = mapping->a_ops->writepage(page, &wbc);
584
585 if (rc != AOP_WRITEPAGE_ACTIVATE)
586 /* unlocked. Relock */
587 lock_page(page);
588
589 return (rc < 0) ? -EIO : -EAGAIN;
590 }
591
592 /*
593 * Default handling if a filesystem does not provide a migration function.
594 */
595 static int fallback_migrate_page(struct address_space *mapping,
596 struct page *newpage, struct page *page, bool sync)
597 {
598 if (PageDirty(page)) {
599 if (!sync)
600 return -EBUSY;
601 return writeout(mapping, page);
602 }
603
604 /*
605 * Buffers may be managed in a filesystem specific way.
606 * We must have no buffers or drop them.
607 */
608 if (page_has_private(page) &&
609 !try_to_release_page(page, GFP_KERNEL))
610 return -EAGAIN;
611
612 return migrate_page(mapping, newpage, page, sync);
613 }
614
615 /*
616 * Move a page to a newly allocated page
617 * The page is locked and all ptes have been successfully removed.
618 *
619 * The new page will have replaced the old page if this function
620 * is successful.
621 *
622 * Return value:
623 * < 0 - error code
624 * == 0 - success
625 */
626 static int move_to_new_page(struct page *newpage, struct page *page,
627 int remap_swapcache, bool sync)
628 {
629 struct address_space *mapping;
630 int rc;
631
632 /*
633 * Block others from accessing the page when we get around to
634 * establishing additional references. We are the only one
635 * holding a reference to the new page at this point.
636 */
637 if (!trylock_page(newpage))
638 BUG();
639
640 /* Prepare mapping for the new page.*/
641 newpage->index = page->index;
642 newpage->mapping = page->mapping;
643 if (PageSwapBacked(page))
644 SetPageSwapBacked(newpage);
645
646 mapping = page_mapping(page);
647 if (!mapping)
648 rc = migrate_page(mapping, newpage, page, sync);
649 else if (mapping->a_ops->migratepage)
650 /*
651 * Most pages have a mapping and most filesystems provide a
652 * migratepage callback. Anonymous pages are part of swap
653 * space which also has its own migratepage callback. This
654 * is the most common path for page migration.
655 */
656 rc = mapping->a_ops->migratepage(mapping,
657 newpage, page, sync);
658 else
659 rc = fallback_migrate_page(mapping, newpage, page, sync);
660
661 if (rc) {
662 newpage->mapping = NULL;
663 } else {
664 if (remap_swapcache)
665 remove_migration_ptes(page, newpage);
666 }
667
668 unlock_page(newpage);
669
670 return rc;
671 }
672
673 static int __unmap_and_move(struct page *page, struct page *newpage,
674 int force, bool offlining, bool sync)
675 {
676 int rc = -EAGAIN;
677 int remap_swapcache = 1;
678 int charge = 0;
679 struct mem_cgroup *mem;
680 struct anon_vma *anon_vma = NULL;
681
682 if (!trylock_page(page)) {
683 if (!force || !sync)
684 goto out;
685
686 /*
687 * It's not safe for direct compaction to call lock_page.
688 * For example, during page readahead pages are added locked
689 * to the LRU. Later, when the IO completes the pages are
690 * marked uptodate and unlocked. However, the queueing
691 * could be merging multiple pages for one bio (e.g.
692 * mpage_readpages). If an allocation happens for the
693 * second or third page, the process can end up locking
694 * the same page twice and deadlocking. Rather than
695 * trying to be clever about what pages can be locked,
696 * avoid the use of lock_page for direct compaction
697 * altogether.
698 */
699 if (current->flags & PF_MEMALLOC)
700 goto out;
701
702 lock_page(page);
703 }
704
705 /*
706 * Only memory hotplug's offline_pages() caller has locked out KSM,
707 * and can safely migrate a KSM page. The other cases have skipped
708 * PageKsm along with PageReserved - but it is only now when we have
709 * the page lock that we can be certain it will not go KSM beneath us
710 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
711 * its pagecount raised, but only here do we take the page lock which
712 * serializes that).
713 */
714 if (PageKsm(page) && !offlining) {
715 rc = -EBUSY;
716 goto unlock;
717 }
718
719 /* charge against new page */
720 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
721 if (charge == -ENOMEM) {
722 rc = -ENOMEM;
723 goto unlock;
724 }
725 BUG_ON(charge);
726
727 if (PageWriteback(page)) {
728 /*
729 * For !sync, there is no point retrying as the retry loop
730 * is expected to be too short for PageWriteback to be cleared
731 */
732 if (!sync) {
733 rc = -EBUSY;
734 goto uncharge;
735 }
736 if (!force)
737 goto uncharge;
738 wait_on_page_writeback(page);
739 }
740 /*
741 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
742 * we cannot notice that anon_vma is freed while we migrates a page.
743 * This get_anon_vma() delays freeing anon_vma pointer until the end
744 * of migration. File cache pages are no problem because of page_lock()
745 * File Caches may use write_page() or lock_page() in migration, then,
746 * just care Anon page here.
747 */
748 if (PageAnon(page)) {
749 /*
750 * Only page_lock_anon_vma() understands the subtleties of
751 * getting a hold on an anon_vma from outside one of its mms.
752 */
753 anon_vma = page_get_anon_vma(page);
754 if (anon_vma) {
755 /*
756 * Anon page
757 */
758 } else if (PageSwapCache(page)) {
759 /*
760 * We cannot be sure that the anon_vma of an unmapped
761 * swapcache page is safe to use because we don't
762 * know in advance if the VMA that this page belonged
763 * to still exists. If the VMA and others sharing the
764 * data have been freed, then the anon_vma could
765 * already be invalid.
766 *
767 * To avoid this possibility, swapcache pages get
768 * migrated but are not remapped when migration
769 * completes
770 */
771 remap_swapcache = 0;
772 } else {
773 goto uncharge;
774 }
775 }
776
777 /*
778 * Corner case handling:
779 * 1. When a new swap-cache page is read into, it is added to the LRU
780 * and treated as swapcache but it has no rmap yet.
781 * Calling try_to_unmap() against a page->mapping==NULL page will
782 * trigger a BUG. So handle it here.
783 * 2. An orphaned page (see truncate_complete_page) might have
784 * fs-private metadata. The page can be picked up due to memory
785 * offlining. Everywhere else except page reclaim, the page is
786 * invisible to the vm, so the page can not be migrated. So try to
787 * free the metadata, so the page can be freed.
788 */
789 if (!page->mapping) {
790 VM_BUG_ON(PageAnon(page));
791 if (page_has_private(page)) {
792 try_to_free_buffers(page);
793 goto uncharge;
794 }
795 goto skip_unmap;
796 }
797
798 /* Establish migration ptes or remove ptes */
799 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
800
801 skip_unmap:
802 if (!page_mapped(page))
803 rc = move_to_new_page(newpage, page, remap_swapcache, sync);
804
805 if (rc && remap_swapcache)
806 remove_migration_ptes(page, page);
807
808 /* Drop an anon_vma reference if we took one */
809 if (anon_vma)
810 put_anon_vma(anon_vma);
811
812 uncharge:
813 if (!charge)
814 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
815 unlock:
816 unlock_page(page);
817 out:
818 return rc;
819 }
820
821 /*
822 * Obtain the lock on page, remove all ptes and migrate the page
823 * to the newly allocated page in newpage.
824 */
825 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
826 struct page *page, int force, bool offlining, bool sync)
827 {
828 int rc = 0;
829 int *result = NULL;
830 struct page *newpage = get_new_page(page, private, &result);
831
832 if (!newpage)
833 return -ENOMEM;
834
835 mem_cgroup_reset_owner(newpage);
836
837 if (page_count(page) == 1) {
838 /* page was freed from under us. So we are done. */
839 goto out;
840 }
841
842 if (unlikely(PageTransHuge(page)))
843 if (unlikely(split_huge_page(page)))
844 goto out;
845
846 rc = __unmap_and_move(page, newpage, force, offlining, sync);
847 out:
848 if (rc != -EAGAIN) {
849 /*
850 * A page that has been migrated has all references
851 * removed and will be freed. A page that has not been
852 * migrated will have kepts its references and be
853 * restored.
854 */
855 list_del(&page->lru);
856 dec_zone_page_state(page, NR_ISOLATED_ANON +
857 page_is_file_cache(page));
858 putback_lru_page(page);
859 }
860 /*
861 * Move the new page to the LRU. If migration was not successful
862 * then this will free the page.
863 */
864 putback_lru_page(newpage);
865 if (result) {
866 if (rc)
867 *result = rc;
868 else
869 *result = page_to_nid(newpage);
870 }
871 return rc;
872 }
873
874 /*
875 * Counterpart of unmap_and_move_page() for hugepage migration.
876 *
877 * This function doesn't wait the completion of hugepage I/O
878 * because there is no race between I/O and migration for hugepage.
879 * Note that currently hugepage I/O occurs only in direct I/O
880 * where no lock is held and PG_writeback is irrelevant,
881 * and writeback status of all subpages are counted in the reference
882 * count of the head page (i.e. if all subpages of a 2MB hugepage are
883 * under direct I/O, the reference of the head page is 512 and a bit more.)
884 * This means that when we try to migrate hugepage whose subpages are
885 * doing direct I/O, some references remain after try_to_unmap() and
886 * hugepage migration fails without data corruption.
887 *
888 * There is also no race when direct I/O is issued on the page under migration,
889 * because then pte is replaced with migration swap entry and direct I/O code
890 * will wait in the page fault for migration to complete.
891 */
892 static int unmap_and_move_huge_page(new_page_t get_new_page,
893 unsigned long private, struct page *hpage,
894 int force, bool offlining, bool sync)
895 {
896 int rc = 0;
897 int *result = NULL;
898 struct page *new_hpage = get_new_page(hpage, private, &result);
899 struct anon_vma *anon_vma = NULL;
900
901 if (!new_hpage)
902 return -ENOMEM;
903
904 rc = -EAGAIN;
905
906 if (!trylock_page(hpage)) {
907 if (!force || !sync)
908 goto out;
909 lock_page(hpage);
910 }
911
912 if (PageAnon(hpage))
913 anon_vma = page_get_anon_vma(hpage);
914
915 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
916
917 if (!page_mapped(hpage))
918 rc = move_to_new_page(new_hpage, hpage, 1, sync);
919
920 if (rc)
921 remove_migration_ptes(hpage, hpage);
922
923 if (anon_vma)
924 put_anon_vma(anon_vma);
925 unlock_page(hpage);
926
927 out:
928 if (rc != -EAGAIN) {
929 list_del(&hpage->lru);
930 put_page(hpage);
931 }
932
933 put_page(new_hpage);
934
935 if (result) {
936 if (rc)
937 *result = rc;
938 else
939 *result = page_to_nid(new_hpage);
940 }
941 return rc;
942 }
943
944 /*
945 * migrate_pages
946 *
947 * The function takes one list of pages to migrate and a function
948 * that determines from the page to be migrated and the private data
949 * the target of the move and allocates the page.
950 *
951 * The function returns after 10 attempts or if no pages
952 * are movable anymore because to has become empty
953 * or no retryable pages exist anymore.
954 * Caller should call putback_lru_pages to return pages to the LRU
955 * or free list only if ret != 0.
956 *
957 * Return: Number of pages not migrated or error code.
958 */
959 int migrate_pages(struct list_head *from,
960 new_page_t get_new_page, unsigned long private, bool offlining,
961 bool sync)
962 {
963 int retry = 1;
964 int nr_failed = 0;
965 int pass = 0;
966 struct page *page;
967 struct page *page2;
968 int swapwrite = current->flags & PF_SWAPWRITE;
969 int rc;
970
971 if (!swapwrite)
972 current->flags |= PF_SWAPWRITE;
973
974 for(pass = 0; pass < 10 && retry; pass++) {
975 retry = 0;
976
977 list_for_each_entry_safe(page, page2, from, lru) {
978 cond_resched();
979
980 rc = unmap_and_move(get_new_page, private,
981 page, pass > 2, offlining,
982 sync);
983
984 switch(rc) {
985 case -ENOMEM:
986 goto out;
987 case -EAGAIN:
988 retry++;
989 break;
990 case 0:
991 break;
992 default:
993 /* Permanent failure */
994 nr_failed++;
995 break;
996 }
997 }
998 }
999 rc = 0;
1000 out:
1001 if (!swapwrite)
1002 current->flags &= ~PF_SWAPWRITE;
1003
1004 if (rc)
1005 return rc;
1006
1007 return nr_failed + retry;
1008 }
1009
1010 int migrate_huge_pages(struct list_head *from,
1011 new_page_t get_new_page, unsigned long private, bool offlining,
1012 bool sync)
1013 {
1014 int retry = 1;
1015 int nr_failed = 0;
1016 int pass = 0;
1017 struct page *page;
1018 struct page *page2;
1019 int rc;
1020
1021 for (pass = 0; pass < 10 && retry; pass++) {
1022 retry = 0;
1023
1024 list_for_each_entry_safe(page, page2, from, lru) {
1025 cond_resched();
1026
1027 rc = unmap_and_move_huge_page(get_new_page,
1028 private, page, pass > 2, offlining,
1029 sync);
1030
1031 switch(rc) {
1032 case -ENOMEM:
1033 goto out;
1034 case -EAGAIN:
1035 retry++;
1036 break;
1037 case 0:
1038 break;
1039 default:
1040 /* Permanent failure */
1041 nr_failed++;
1042 break;
1043 }
1044 }
1045 }
1046 rc = 0;
1047 out:
1048 if (rc)
1049 return rc;
1050
1051 return nr_failed + retry;
1052 }
1053
1054 #ifdef CONFIG_NUMA
1055 /*
1056 * Move a list of individual pages
1057 */
1058 struct page_to_node {
1059 unsigned long addr;
1060 struct page *page;
1061 int node;
1062 int status;
1063 };
1064
1065 static struct page *new_page_node(struct page *p, unsigned long private,
1066 int **result)
1067 {
1068 struct page_to_node *pm = (struct page_to_node *)private;
1069
1070 while (pm->node != MAX_NUMNODES && pm->page != p)
1071 pm++;
1072
1073 if (pm->node == MAX_NUMNODES)
1074 return NULL;
1075
1076 *result = &pm->status;
1077
1078 return alloc_pages_exact_node(pm->node,
1079 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
1080 }
1081
1082 /*
1083 * Move a set of pages as indicated in the pm array. The addr
1084 * field must be set to the virtual address of the page to be moved
1085 * and the node number must contain a valid target node.
1086 * The pm array ends with node = MAX_NUMNODES.
1087 */
1088 static int do_move_page_to_node_array(struct mm_struct *mm,
1089 struct page_to_node *pm,
1090 int migrate_all)
1091 {
1092 int err;
1093 struct page_to_node *pp;
1094 LIST_HEAD(pagelist);
1095
1096 down_read(&mm->mmap_sem);
1097
1098 /*
1099 * Build a list of pages to migrate
1100 */
1101 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1102 struct vm_area_struct *vma;
1103 struct page *page;
1104
1105 err = -EFAULT;
1106 vma = find_vma(mm, pp->addr);
1107 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
1108 goto set_status;
1109
1110 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
1111
1112 err = PTR_ERR(page);
1113 if (IS_ERR(page))
1114 goto set_status;
1115
1116 err = -ENOENT;
1117 if (!page)
1118 goto set_status;
1119
1120 /* Use PageReserved to check for zero page */
1121 if (PageReserved(page) || PageKsm(page))
1122 goto put_and_set;
1123
1124 pp->page = page;
1125 err = page_to_nid(page);
1126
1127 if (err == pp->node)
1128 /*
1129 * Node already in the right place
1130 */
1131 goto put_and_set;
1132
1133 err = -EACCES;
1134 if (page_mapcount(page) > 1 &&
1135 !migrate_all)
1136 goto put_and_set;
1137
1138 err = isolate_lru_page(page);
1139 if (!err) {
1140 list_add_tail(&page->lru, &pagelist);
1141 inc_zone_page_state(page, NR_ISOLATED_ANON +
1142 page_is_file_cache(page));
1143 }
1144 put_and_set:
1145 /*
1146 * Either remove the duplicate refcount from
1147 * isolate_lru_page() or drop the page ref if it was
1148 * not isolated.
1149 */
1150 put_page(page);
1151 set_status:
1152 pp->status = err;
1153 }
1154
1155 err = 0;
1156 if (!list_empty(&pagelist)) {
1157 err = migrate_pages(&pagelist, new_page_node,
1158 (unsigned long)pm, 0, true);
1159 if (err)
1160 putback_lru_pages(&pagelist);
1161 }
1162
1163 up_read(&mm->mmap_sem);
1164 return err;
1165 }
1166
1167 /*
1168 * Migrate an array of page address onto an array of nodes and fill
1169 * the corresponding array of status.
1170 */
1171 static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
1172 unsigned long nr_pages,
1173 const void __user * __user *pages,
1174 const int __user *nodes,
1175 int __user *status, int flags)
1176 {
1177 struct page_to_node *pm;
1178 nodemask_t task_nodes;
1179 unsigned long chunk_nr_pages;
1180 unsigned long chunk_start;
1181 int err;
1182
1183 task_nodes = cpuset_mems_allowed(task);
1184
1185 err = -ENOMEM;
1186 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1187 if (!pm)
1188 goto out;
1189
1190 migrate_prep();
1191
1192 /*
1193 * Store a chunk of page_to_node array in a page,
1194 * but keep the last one as a marker
1195 */
1196 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
1197
1198 for (chunk_start = 0;
1199 chunk_start < nr_pages;
1200 chunk_start += chunk_nr_pages) {
1201 int j;
1202
1203 if (chunk_start + chunk_nr_pages > nr_pages)
1204 chunk_nr_pages = nr_pages - chunk_start;
1205
1206 /* fill the chunk pm with addrs and nodes from user-space */
1207 for (j = 0; j < chunk_nr_pages; j++) {
1208 const void __user *p;
1209 int node;
1210
1211 err = -EFAULT;
1212 if (get_user(p, pages + j + chunk_start))
1213 goto out_pm;
1214 pm[j].addr = (unsigned long) p;
1215
1216 if (get_user(node, nodes + j + chunk_start))
1217 goto out_pm;
1218
1219 err = -ENODEV;
1220 if (node < 0 || node >= MAX_NUMNODES)
1221 goto out_pm;
1222
1223 if (!node_state(node, N_HIGH_MEMORY))
1224 goto out_pm;
1225
1226 err = -EACCES;
1227 if (!node_isset(node, task_nodes))
1228 goto out_pm;
1229
1230 pm[j].node = node;
1231 }
1232
1233 /* End marker for this chunk */
1234 pm[chunk_nr_pages].node = MAX_NUMNODES;
1235
1236 /* Migrate this chunk */
1237 err = do_move_page_to_node_array(mm, pm,
1238 flags & MPOL_MF_MOVE_ALL);
1239 if (err < 0)
1240 goto out_pm;
1241
1242 /* Return status information */
1243 for (j = 0; j < chunk_nr_pages; j++)
1244 if (put_user(pm[j].status, status + j + chunk_start)) {
1245 err = -EFAULT;
1246 goto out_pm;
1247 }
1248 }
1249 err = 0;
1250
1251 out_pm:
1252 free_page((unsigned long)pm);
1253 out:
1254 return err;
1255 }
1256
1257 /*
1258 * Determine the nodes of an array of pages and store it in an array of status.
1259 */
1260 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1261 const void __user **pages, int *status)
1262 {
1263 unsigned long i;
1264
1265 down_read(&mm->mmap_sem);
1266
1267 for (i = 0; i < nr_pages; i++) {
1268 unsigned long addr = (unsigned long)(*pages);
1269 struct vm_area_struct *vma;
1270 struct page *page;
1271 int err = -EFAULT;
1272
1273 vma = find_vma(mm, addr);
1274 if (!vma || addr < vma->vm_start)
1275 goto set_status;
1276
1277 page = follow_page(vma, addr, 0);
1278
1279 err = PTR_ERR(page);
1280 if (IS_ERR(page))
1281 goto set_status;
1282
1283 err = -ENOENT;
1284 /* Use PageReserved to check for zero page */
1285 if (!page || PageReserved(page) || PageKsm(page))
1286 goto set_status;
1287
1288 err = page_to_nid(page);
1289 set_status:
1290 *status = err;
1291
1292 pages++;
1293 status++;
1294 }
1295
1296 up_read(&mm->mmap_sem);
1297 }
1298
1299 /*
1300 * Determine the nodes of a user array of pages and store it in
1301 * a user array of status.
1302 */
1303 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1304 const void __user * __user *pages,
1305 int __user *status)
1306 {
1307 #define DO_PAGES_STAT_CHUNK_NR 16
1308 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1309 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1310
1311 while (nr_pages) {
1312 unsigned long chunk_nr;
1313
1314 chunk_nr = nr_pages;
1315 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1316 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1317
1318 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1319 break;
1320
1321 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1322
1323 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1324 break;
1325
1326 pages += chunk_nr;
1327 status += chunk_nr;
1328 nr_pages -= chunk_nr;
1329 }
1330 return nr_pages ? -EFAULT : 0;
1331 }
1332
1333 /*
1334 * Move a list of pages in the address space of the currently executing
1335 * process.
1336 */
1337 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1338 const void __user * __user *, pages,
1339 const int __user *, nodes,
1340 int __user *, status, int, flags)
1341 {
1342 const struct cred *cred = current_cred(), *tcred;
1343 struct task_struct *task;
1344 struct mm_struct *mm;
1345 int err;
1346
1347 /* Check flags */
1348 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1349 return -EINVAL;
1350
1351 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1352 return -EPERM;
1353
1354 /* Find the mm_struct */
1355 rcu_read_lock();
1356 task = pid ? find_task_by_vpid(pid) : current;
1357 if (!task) {
1358 rcu_read_unlock();
1359 return -ESRCH;
1360 }
1361 mm = get_task_mm(task);
1362 rcu_read_unlock();
1363
1364 if (!mm)
1365 return -EINVAL;
1366
1367 /*
1368 * Check if this process has the right to modify the specified
1369 * process. The right exists if the process has administrative
1370 * capabilities, superuser privileges or the same
1371 * userid as the target process.
1372 */
1373 rcu_read_lock();
1374 tcred = __task_cred(task);
1375 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1376 cred->uid != tcred->suid && cred->uid != tcred->uid &&
1377 !capable(CAP_SYS_NICE)) {
1378 rcu_read_unlock();
1379 err = -EPERM;
1380 goto out;
1381 }
1382 rcu_read_unlock();
1383
1384 err = security_task_movememory(task);
1385 if (err)
1386 goto out;
1387
1388 if (nodes) {
1389 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1390 flags);
1391 } else {
1392 err = do_pages_stat(mm, nr_pages, pages, status);
1393 }
1394
1395 out:
1396 mmput(mm);
1397 return err;
1398 }
1399
1400 /*
1401 * Call migration functions in the vma_ops that may prepare
1402 * memory in a vm for migration. migration functions may perform
1403 * the migration for vmas that do not have an underlying page struct.
1404 */
1405 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1406 const nodemask_t *from, unsigned long flags)
1407 {
1408 struct vm_area_struct *vma;
1409 int err = 0;
1410
1411 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1412 if (vma->vm_ops && vma->vm_ops->migrate) {
1413 err = vma->vm_ops->migrate(vma, to, from, flags);
1414 if (err)
1415 break;
1416 }
1417 }
1418 return err;
1419 }
1420 #endif