Linux 3.12.39
[linux/fpc-iii.git] / mm / migrate.c
blob66ca0c494b90e5c919b0d6076edba6140749c3ce
1 /*
2 * Memory Migration functionality - linux/mm/migration.c
4 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6 * Page migration was first developed in the context of the memory hotplug
7 * project. The main authors of the migration code are:
9 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10 * Hirokazu Takahashi <taka@valinux.co.jp>
11 * Dave Hansen <haveblue@us.ibm.com>
12 * Christoph Lameter
15 #include <linux/migrate.h>
16 #include <linux/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/hugetlb_cgroup.h>
37 #include <linux/gfp.h>
38 #include <linux/balloon_compaction.h>
39 #include <linux/mmu_notifier.h>
41 #include <asm/tlbflush.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/migrate.h>
46 #include "internal.h"
49 * migrate_prep() needs to be called before we start compiling a list of pages
50 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
51 * undesirable, use migrate_prep_local()
53 int migrate_prep(void)
56 * Clear the LRU lists so pages can be isolated.
57 * Note that pages may be moved off the LRU after we have
58 * drained them. Those pages will fail to migrate like other
59 * pages that may be busy.
61 lru_add_drain_all();
63 return 0;
66 /* Do the necessary work of migrate_prep but not if it involves other CPUs */
67 int migrate_prep_local(void)
69 lru_add_drain();
71 return 0;
75 * Add isolated pages on the list back to the LRU under page lock
76 * to avoid leaking evictable pages back onto unevictable list.
78 void putback_lru_pages(struct list_head *l)
80 struct page *page;
81 struct page *page2;
83 list_for_each_entry_safe(page, page2, l, lru) {
84 list_del(&page->lru);
85 dec_zone_page_state(page, NR_ISOLATED_ANON +
86 page_is_file_cache(page));
87 putback_lru_page(page);
92 * Put previously isolated pages back onto the appropriate lists
93 * from where they were once taken off for compaction/migration.
95 * This function shall be used instead of putback_lru_pages(),
96 * whenever the isolated pageset has been built by isolate_migratepages_range()
98 void putback_movable_pages(struct list_head *l)
100 struct page *page;
101 struct page *page2;
103 list_for_each_entry_safe(page, page2, l, lru) {
104 if (unlikely(PageHuge(page))) {
105 putback_active_hugepage(page);
106 continue;
108 list_del(&page->lru);
109 dec_zone_page_state(page, NR_ISOLATED_ANON +
110 page_is_file_cache(page));
111 if (unlikely(isolated_balloon_page(page)))
112 balloon_page_putback(page);
113 else
114 putback_lru_page(page);
119 * Restore a potential migration pte to a working pte entry
121 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
122 unsigned long addr, void *old)
124 struct mm_struct *mm = vma->vm_mm;
125 swp_entry_t entry;
126 pmd_t *pmd;
127 pte_t *ptep, pte;
128 spinlock_t *ptl;
130 if (unlikely(PageHuge(new))) {
131 ptep = huge_pte_offset(mm, addr);
132 if (!ptep)
133 goto out;
134 ptl = &mm->page_table_lock;
135 } else {
136 pmd = mm_find_pmd(mm, addr);
137 if (!pmd)
138 goto out;
140 ptep = pte_offset_map(pmd, addr);
143 * Peek to check is_swap_pte() before taking ptlock? No, we
144 * can race mremap's move_ptes(), which skips anon_vma lock.
147 ptl = pte_lockptr(mm, pmd);
150 spin_lock(ptl);
151 pte = *ptep;
152 if (!is_swap_pte(pte))
153 goto unlock;
155 entry = pte_to_swp_entry(pte);
157 if (!is_migration_entry(entry) ||
158 migration_entry_to_page(entry) != old)
159 goto unlock;
161 get_page(new);
162 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
163 if (pte_swp_soft_dirty(*ptep))
164 pte = pte_mksoft_dirty(pte);
166 /* Recheck VMA as permissions can change since migration started */
167 if (is_write_migration_entry(entry))
168 pte = maybe_mkwrite(pte, vma);
170 #ifdef CONFIG_HUGETLB_PAGE
171 if (PageHuge(new)) {
172 pte = pte_mkhuge(pte);
173 pte = arch_make_huge_pte(pte, vma, new, 0);
175 #endif
176 flush_dcache_page(new);
177 set_pte_at(mm, addr, ptep, pte);
179 if (PageHuge(new)) {
180 if (PageAnon(new))
181 hugepage_add_anon_rmap(new, vma, addr);
182 else
183 page_dup_rmap(new);
184 } else if (PageAnon(new))
185 page_add_anon_rmap(new, vma, addr);
186 else
187 page_add_file_rmap(new);
189 /* No need to invalidate - it was non-present before */
190 update_mmu_cache(vma, addr, ptep);
191 unlock:
192 pte_unmap_unlock(ptep, ptl);
193 out:
194 return SWAP_AGAIN;
198 * Get rid of all migration entries and replace them by
199 * references to the indicated page.
201 static void remove_migration_ptes(struct page *old, struct page *new)
203 rmap_walk(new, remove_migration_pte, old);
207 * Something used the pte of a page under migration. We need to
208 * get to the page and wait until migration is finished.
209 * When we return from this function the fault will be retried.
211 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
212 spinlock_t *ptl)
214 pte_t pte;
215 swp_entry_t entry;
216 struct page *page;
218 spin_lock(ptl);
219 pte = *ptep;
220 if (!is_swap_pte(pte))
221 goto out;
223 entry = pte_to_swp_entry(pte);
224 if (!is_migration_entry(entry))
225 goto out;
227 page = migration_entry_to_page(entry);
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.
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);
246 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
247 unsigned long address)
249 spinlock_t *ptl = pte_lockptr(mm, pmd);
250 pte_t *ptep = pte_offset_map(pmd, address);
251 __migration_entry_wait(mm, ptep, ptl);
254 void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
256 spinlock_t *ptl = &(mm)->page_table_lock;
257 __migration_entry_wait(mm, pte, ptl);
260 #ifdef CONFIG_BLOCK
261 /* Returns true if all buffers are successfully locked */
262 static bool buffer_migrate_lock_buffers(struct buffer_head *head,
263 enum migrate_mode mode)
265 struct buffer_head *bh = head;
267 /* Simple case, sync compaction */
268 if (mode != MIGRATE_ASYNC) {
269 do {
270 get_bh(bh);
271 lock_buffer(bh);
272 bh = bh->b_this_page;
274 } while (bh != head);
276 return true;
279 /* async case, we cannot block on lock_buffer so use trylock_buffer */
280 do {
281 get_bh(bh);
282 if (!trylock_buffer(bh)) {
284 * We failed to lock the buffer and cannot stall in
285 * async migration. Release the taken locks
287 struct buffer_head *failed_bh = bh;
288 put_bh(failed_bh);
289 bh = head;
290 while (bh != failed_bh) {
291 unlock_buffer(bh);
292 put_bh(bh);
293 bh = bh->b_this_page;
295 return false;
298 bh = bh->b_this_page;
299 } while (bh != head);
300 return true;
302 #else
303 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
304 enum migrate_mode mode)
306 return true;
308 #endif /* CONFIG_BLOCK */
311 * Replace the page in the mapping.
313 * The number of remaining references must be:
314 * 1 for anonymous pages without a mapping
315 * 2 for pages with a mapping
316 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
318 int migrate_page_move_mapping(struct address_space *mapping,
319 struct page *newpage, struct page *page,
320 struct buffer_head *head, enum migrate_mode mode,
321 int extra_count)
323 int expected_count = 1 + extra_count;
324 void **pslot;
326 if (!mapping) {
327 /* Anonymous page without mapping */
328 if (page_count(page) != expected_count)
329 return -EAGAIN;
330 return MIGRATEPAGE_SUCCESS;
333 spin_lock_irq(&mapping->tree_lock);
335 pslot = radix_tree_lookup_slot(&mapping->page_tree,
336 page_index(page));
338 expected_count += 1 + page_has_private(page);
339 if (page_count(page) != expected_count ||
340 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
341 spin_unlock_irq(&mapping->tree_lock);
342 return -EAGAIN;
345 if (!page_freeze_refs(page, expected_count)) {
346 spin_unlock_irq(&mapping->tree_lock);
347 return -EAGAIN;
351 * In the async migration case of moving a page with buffers, lock the
352 * buffers using trylock before the mapping is moved. If the mapping
353 * was moved, we later failed to lock the buffers and could not move
354 * the mapping back due to an elevated page count, we would have to
355 * block waiting on other references to be dropped.
357 if (mode == MIGRATE_ASYNC && head &&
358 !buffer_migrate_lock_buffers(head, mode)) {
359 page_unfreeze_refs(page, expected_count);
360 spin_unlock_irq(&mapping->tree_lock);
361 return -EAGAIN;
365 * Now we know that no one else is looking at the page.
367 get_page(newpage); /* add cache reference */
368 if (PageSwapCache(page)) {
369 SetPageSwapCache(newpage);
370 set_page_private(newpage, page_private(page));
373 radix_tree_replace_slot(pslot, newpage);
376 * Drop cache reference from old page by unfreezing
377 * to one less reference.
378 * We know this isn't the last reference.
380 page_unfreeze_refs(page, expected_count - 1);
383 * If moved to a different zone then also account
384 * the page for that zone. Other VM counters will be
385 * taken care of when we establish references to the
386 * new page and drop references to the old page.
388 * Note that anonymous pages are accounted for
389 * via NR_FILE_PAGES and NR_ANON_PAGES if they
390 * are mapped to swap space.
392 __dec_zone_page_state(page, NR_FILE_PAGES);
393 __inc_zone_page_state(newpage, NR_FILE_PAGES);
394 if (!PageSwapCache(page) && PageSwapBacked(page)) {
395 __dec_zone_page_state(page, NR_SHMEM);
396 __inc_zone_page_state(newpage, NR_SHMEM);
398 spin_unlock_irq(&mapping->tree_lock);
400 return MIGRATEPAGE_SUCCESS;
404 * The expected number of remaining references is the same as that
405 * of migrate_page_move_mapping().
407 int migrate_huge_page_move_mapping(struct address_space *mapping,
408 struct page *newpage, struct page *page)
410 int expected_count;
411 void **pslot;
413 if (!mapping) {
414 if (page_count(page) != 1)
415 return -EAGAIN;
416 return MIGRATEPAGE_SUCCESS;
419 spin_lock_irq(&mapping->tree_lock);
421 pslot = radix_tree_lookup_slot(&mapping->page_tree,
422 page_index(page));
424 expected_count = 2 + page_has_private(page);
425 if (page_count(page) != expected_count ||
426 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
427 spin_unlock_irq(&mapping->tree_lock);
428 return -EAGAIN;
431 if (!page_freeze_refs(page, expected_count)) {
432 spin_unlock_irq(&mapping->tree_lock);
433 return -EAGAIN;
436 get_page(newpage);
438 radix_tree_replace_slot(pslot, newpage);
440 page_unfreeze_refs(page, expected_count - 1);
442 spin_unlock_irq(&mapping->tree_lock);
443 return MIGRATEPAGE_SUCCESS;
447 * Copy the page to its new location
449 void migrate_page_copy(struct page *newpage, struct page *page)
451 if (PageHuge(page) || PageTransHuge(page))
452 copy_huge_page(newpage, page);
453 else
454 copy_highpage(newpage, page);
456 if (PageError(page))
457 SetPageError(newpage);
458 if (PageReferenced(page))
459 SetPageReferenced(newpage);
460 if (PageUptodate(page))
461 SetPageUptodate(newpage);
462 if (TestClearPageActive(page)) {
463 VM_BUG_ON(PageUnevictable(page));
464 SetPageActive(newpage);
465 } else if (TestClearPageUnevictable(page))
466 SetPageUnevictable(newpage);
467 if (PageChecked(page))
468 SetPageChecked(newpage);
469 if (PageMappedToDisk(page))
470 SetPageMappedToDisk(newpage);
472 if (PageDirty(page)) {
473 clear_page_dirty_for_io(page);
475 * Want to mark the page and the radix tree as dirty, and
476 * redo the accounting that clear_page_dirty_for_io undid,
477 * but we can't use set_page_dirty because that function
478 * is actually a signal that all of the page has become dirty.
479 * Whereas only part of our page may be dirty.
481 if (PageSwapBacked(page))
482 SetPageDirty(newpage);
483 else
484 __set_page_dirty_nobuffers(newpage);
487 mlock_migrate_page(newpage, page);
488 ksm_migrate_page(newpage, page);
490 * Please do not reorder this without considering how mm/ksm.c's
491 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
493 ClearPageSwapCache(page);
494 ClearPagePrivate(page);
495 set_page_private(page, 0);
498 * If any waiters have accumulated on the new page then
499 * wake them up.
501 if (PageWriteback(newpage))
502 end_page_writeback(newpage);
505 /************************************************************
506 * Migration functions
507 ***********************************************************/
509 /* Always fail migration. Used for mappings that are not movable */
510 int fail_migrate_page(struct address_space *mapping,
511 struct page *newpage, struct page *page)
513 return -EIO;
515 EXPORT_SYMBOL(fail_migrate_page);
518 * Common logic to directly migrate a single page suitable for
519 * pages that do not use PagePrivate/PagePrivate2.
521 * Pages are locked upon entry and exit.
523 int migrate_page(struct address_space *mapping,
524 struct page *newpage, struct page *page,
525 enum migrate_mode mode)
527 int rc;
529 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
531 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
533 if (rc != MIGRATEPAGE_SUCCESS)
534 return rc;
536 migrate_page_copy(newpage, page);
537 return MIGRATEPAGE_SUCCESS;
539 EXPORT_SYMBOL(migrate_page);
541 #ifdef CONFIG_BLOCK
543 * Migration function for pages with buffers. This function can only be used
544 * if the underlying filesystem guarantees that no other references to "page"
545 * exist.
547 int buffer_migrate_page(struct address_space *mapping,
548 struct page *newpage, struct page *page, enum migrate_mode mode)
550 struct buffer_head *bh, *head;
551 int rc;
553 if (!page_has_buffers(page))
554 return migrate_page(mapping, newpage, page, mode);
556 head = page_buffers(page);
558 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
560 if (rc != MIGRATEPAGE_SUCCESS)
561 return rc;
564 * In the async case, migrate_page_move_mapping locked the buffers
565 * with an IRQ-safe spinlock held. In the sync case, the buffers
566 * need to be locked now
568 if (mode != MIGRATE_ASYNC)
569 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
571 ClearPagePrivate(page);
572 set_page_private(newpage, page_private(page));
573 set_page_private(page, 0);
574 put_page(page);
575 get_page(newpage);
577 bh = head;
578 do {
579 set_bh_page(bh, newpage, bh_offset(bh));
580 bh = bh->b_this_page;
582 } while (bh != head);
584 SetPagePrivate(newpage);
586 migrate_page_copy(newpage, page);
588 bh = head;
589 do {
590 unlock_buffer(bh);
591 put_bh(bh);
592 bh = bh->b_this_page;
594 } while (bh != head);
596 return MIGRATEPAGE_SUCCESS;
598 EXPORT_SYMBOL(buffer_migrate_page);
599 #endif
602 * Writeback a page to clean the dirty state
604 static int writeout(struct address_space *mapping, struct page *page)
606 struct writeback_control wbc = {
607 .sync_mode = WB_SYNC_NONE,
608 .nr_to_write = 1,
609 .range_start = 0,
610 .range_end = LLONG_MAX,
611 .for_reclaim = 1
613 int rc;
615 if (!mapping->a_ops->writepage)
616 /* No write method for the address space */
617 return -EINVAL;
619 if (!clear_page_dirty_for_io(page))
620 /* Someone else already triggered a write */
621 return -EAGAIN;
624 * A dirty page may imply that the underlying filesystem has
625 * the page on some queue. So the page must be clean for
626 * migration. Writeout may mean we loose the lock and the
627 * page state is no longer what we checked for earlier.
628 * At this point we know that the migration attempt cannot
629 * be successful.
631 remove_migration_ptes(page, page);
633 rc = mapping->a_ops->writepage(page, &wbc);
635 if (rc != AOP_WRITEPAGE_ACTIVATE)
636 /* unlocked. Relock */
637 lock_page(page);
639 return (rc < 0) ? -EIO : -EAGAIN;
643 * Default handling if a filesystem does not provide a migration function.
645 static int fallback_migrate_page(struct address_space *mapping,
646 struct page *newpage, struct page *page, enum migrate_mode mode)
648 if (PageDirty(page)) {
649 /* Only writeback pages in full synchronous migration */
650 if (mode != MIGRATE_SYNC)
651 return -EBUSY;
652 return writeout(mapping, page);
656 * Buffers may be managed in a filesystem specific way.
657 * We must have no buffers or drop them.
659 if (page_has_private(page) &&
660 !try_to_release_page(page, GFP_KERNEL))
661 return -EAGAIN;
663 return migrate_page(mapping, newpage, page, mode);
667 * Move a page to a newly allocated page
668 * The page is locked and all ptes have been successfully removed.
670 * The new page will have replaced the old page if this function
671 * is successful.
673 * Return value:
674 * < 0 - error code
675 * MIGRATEPAGE_SUCCESS - success
677 static int move_to_new_page(struct page *newpage, struct page *page,
678 int remap_swapcache, enum migrate_mode mode)
680 struct address_space *mapping;
681 int rc;
684 * Block others from accessing the page when we get around to
685 * establishing additional references. We are the only one
686 * holding a reference to the new page at this point.
688 if (!trylock_page(newpage))
689 BUG();
691 /* Prepare mapping for the new page.*/
692 newpage->index = page->index;
693 newpage->mapping = page->mapping;
694 if (PageSwapBacked(page))
695 SetPageSwapBacked(newpage);
697 mapping = page_mapping(page);
698 if (!mapping)
699 rc = migrate_page(mapping, newpage, page, mode);
700 else if (mapping->a_ops->migratepage)
702 * Most pages have a mapping and most filesystems provide a
703 * migratepage callback. Anonymous pages are part of swap
704 * space which also has its own migratepage callback. This
705 * is the most common path for page migration.
707 rc = mapping->a_ops->migratepage(mapping,
708 newpage, page, mode);
709 else
710 rc = fallback_migrate_page(mapping, newpage, page, mode);
712 if (rc != MIGRATEPAGE_SUCCESS) {
713 newpage->mapping = NULL;
714 } else {
715 if (remap_swapcache)
716 remove_migration_ptes(page, newpage);
717 page->mapping = NULL;
720 unlock_page(newpage);
722 return rc;
725 static int __unmap_and_move(struct page *page, struct page *newpage,
726 int force, enum migrate_mode mode)
728 int rc = -EAGAIN;
729 int remap_swapcache = 1;
730 struct mem_cgroup *mem;
731 struct anon_vma *anon_vma = NULL;
733 if (!trylock_page(page)) {
734 if (!force || mode == MIGRATE_ASYNC)
735 goto out;
738 * It's not safe for direct compaction to call lock_page.
739 * For example, during page readahead pages are added locked
740 * to the LRU. Later, when the IO completes the pages are
741 * marked uptodate and unlocked. However, the queueing
742 * could be merging multiple pages for one bio (e.g.
743 * mpage_readpages). If an allocation happens for the
744 * second or third page, the process can end up locking
745 * the same page twice and deadlocking. Rather than
746 * trying to be clever about what pages can be locked,
747 * avoid the use of lock_page for direct compaction
748 * altogether.
750 if (current->flags & PF_MEMALLOC)
751 goto out;
753 lock_page(page);
756 /* charge against new page */
757 mem_cgroup_prepare_migration(page, newpage, &mem);
759 if (PageWriteback(page)) {
761 * Only in the case of a full synchronous migration is it
762 * necessary to wait for PageWriteback. In the async case,
763 * the retry loop is too short and in the sync-light case,
764 * the overhead of stalling is too much
766 if (mode != MIGRATE_SYNC) {
767 rc = -EBUSY;
768 goto uncharge;
770 if (!force)
771 goto uncharge;
772 wait_on_page_writeback(page);
775 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
776 * we cannot notice that anon_vma is freed while we migrates a page.
777 * This get_anon_vma() delays freeing anon_vma pointer until the end
778 * of migration. File cache pages are no problem because of page_lock()
779 * File Caches may use write_page() or lock_page() in migration, then,
780 * just care Anon page here.
782 if (PageAnon(page) && !PageKsm(page)) {
784 * Only page_lock_anon_vma_read() understands the subtleties of
785 * getting a hold on an anon_vma from outside one of its mms.
787 anon_vma = page_get_anon_vma(page);
788 if (anon_vma) {
790 * Anon page
792 } else if (PageSwapCache(page)) {
794 * We cannot be sure that the anon_vma of an unmapped
795 * swapcache page is safe to use because we don't
796 * know in advance if the VMA that this page belonged
797 * to still exists. If the VMA and others sharing the
798 * data have been freed, then the anon_vma could
799 * already be invalid.
801 * To avoid this possibility, swapcache pages get
802 * migrated but are not remapped when migration
803 * completes
805 remap_swapcache = 0;
806 } else {
807 goto uncharge;
811 if (unlikely(balloon_page_movable(page))) {
813 * A ballooned page does not need any special attention from
814 * physical to virtual reverse mapping procedures.
815 * Skip any attempt to unmap PTEs or to remap swap cache,
816 * in order to avoid burning cycles at rmap level, and perform
817 * the page migration right away (proteced by page lock).
819 rc = balloon_page_migrate(newpage, page, mode);
820 goto uncharge;
824 * Corner case handling:
825 * 1. When a new swap-cache page is read into, it is added to the LRU
826 * and treated as swapcache but it has no rmap yet.
827 * Calling try_to_unmap() against a page->mapping==NULL page will
828 * trigger a BUG. So handle it here.
829 * 2. An orphaned page (see truncate_complete_page) might have
830 * fs-private metadata. The page can be picked up due to memory
831 * offlining. Everywhere else except page reclaim, the page is
832 * invisible to the vm, so the page can not be migrated. So try to
833 * free the metadata, so the page can be freed.
835 if (!page->mapping) {
836 VM_BUG_ON(PageAnon(page));
837 if (page_has_private(page)) {
838 try_to_free_buffers(page);
839 goto uncharge;
841 goto skip_unmap;
844 /* Establish migration ptes or remove ptes */
845 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
847 skip_unmap:
848 if (!page_mapped(page))
849 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
851 if (rc && remap_swapcache)
852 remove_migration_ptes(page, page);
854 /* Drop an anon_vma reference if we took one */
855 if (anon_vma)
856 put_anon_vma(anon_vma);
858 uncharge:
859 mem_cgroup_end_migration(mem, page, newpage,
860 (rc == MIGRATEPAGE_SUCCESS ||
861 rc == MIGRATEPAGE_BALLOON_SUCCESS));
862 unlock_page(page);
863 out:
864 return rc;
868 * Obtain the lock on page, remove all ptes and migrate the page
869 * to the newly allocated page in newpage.
871 static int unmap_and_move(new_page_t get_new_page, free_page_t put_new_page,
872 unsigned long private, struct page *page, int force,
873 enum migrate_mode mode)
875 int rc = 0;
876 int *result = NULL;
877 struct page *newpage = get_new_page(page, private, &result);
879 if (!newpage)
880 return -ENOMEM;
882 if (page_count(page) == 1) {
883 /* page was freed from under us. So we are done. */
884 goto out;
887 if (unlikely(PageTransHuge(page)))
888 if (unlikely(split_huge_page(page)))
889 goto out;
891 rc = __unmap_and_move(page, newpage, force, mode);
893 if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
895 * A ballooned page has been migrated already.
896 * Now, it's the time to wrap-up counters,
897 * handle the page back to Buddy and return.
899 dec_zone_page_state(page, NR_ISOLATED_ANON +
900 page_is_file_cache(page));
901 balloon_page_free(page);
902 return MIGRATEPAGE_SUCCESS;
904 out:
905 if (rc != -EAGAIN) {
907 * A page that has been migrated has all references
908 * removed and will be freed. A page that has not been
909 * migrated will have kepts its references and be
910 * restored.
912 list_del(&page->lru);
913 dec_zone_page_state(page, NR_ISOLATED_ANON +
914 page_is_file_cache(page));
915 putback_lru_page(page);
919 * If migration was not successful and there's a freeing callback, use
920 * it. Otherwise, putback_lru_page() will drop the reference grabbed
921 * during isolation.
923 if (rc != MIGRATEPAGE_SUCCESS && put_new_page) {
924 ClearPageSwapBacked(newpage);
925 put_new_page(newpage, private);
926 } else
927 putback_lru_page(newpage);
929 if (result) {
930 if (rc)
931 *result = rc;
932 else
933 *result = page_to_nid(newpage);
935 return rc;
939 * Counterpart of unmap_and_move_page() for hugepage migration.
941 * This function doesn't wait the completion of hugepage I/O
942 * because there is no race between I/O and migration for hugepage.
943 * Note that currently hugepage I/O occurs only in direct I/O
944 * where no lock is held and PG_writeback is irrelevant,
945 * and writeback status of all subpages are counted in the reference
946 * count of the head page (i.e. if all subpages of a 2MB hugepage are
947 * under direct I/O, the reference of the head page is 512 and a bit more.)
948 * This means that when we try to migrate hugepage whose subpages are
949 * doing direct I/O, some references remain after try_to_unmap() and
950 * hugepage migration fails without data corruption.
952 * There is also no race when direct I/O is issued on the page under migration,
953 * because then pte is replaced with migration swap entry and direct I/O code
954 * will wait in the page fault for migration to complete.
956 static int unmap_and_move_huge_page(new_page_t get_new_page,
957 free_page_t put_new_page, unsigned long private,
958 struct page *hpage, int force,
959 enum migrate_mode mode)
961 int rc = 0;
962 int *result = NULL;
963 struct page *new_hpage = get_new_page(hpage, private, &result);
964 struct anon_vma *anon_vma = NULL;
967 * Movability of hugepages depends on architectures and hugepage size.
968 * This check is necessary because some callers of hugepage migration
969 * like soft offline and memory hotremove don't walk through page
970 * tables or check whether the hugepage is pmd-based or not before
971 * kicking migration.
973 if (!hugepage_migration_support(page_hstate(hpage)))
974 return -ENOSYS;
976 if (!new_hpage)
977 return -ENOMEM;
979 rc = -EAGAIN;
981 if (!trylock_page(hpage)) {
982 if (!force || mode != MIGRATE_SYNC)
983 goto out;
984 lock_page(hpage);
987 if (PageAnon(hpage))
988 anon_vma = page_get_anon_vma(hpage);
990 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
992 if (!page_mapped(hpage))
993 rc = move_to_new_page(new_hpage, hpage, 1, mode);
995 if (rc != MIGRATEPAGE_SUCCESS)
996 remove_migration_ptes(hpage, hpage);
998 if (anon_vma)
999 put_anon_vma(anon_vma);
1001 if (rc == MIGRATEPAGE_SUCCESS)
1002 hugetlb_cgroup_migrate(hpage, new_hpage);
1004 unlock_page(hpage);
1005 out:
1006 if (rc != -EAGAIN)
1007 putback_active_hugepage(hpage);
1010 * If migration was not successful and there's a freeing callback, use
1011 * it. Otherwise, put_page() will drop the reference grabbed during
1012 * isolation.
1014 if (rc != MIGRATEPAGE_SUCCESS && put_new_page)
1015 put_new_page(new_hpage, private);
1016 else
1017 put_page(new_hpage);
1019 if (result) {
1020 if (rc)
1021 *result = rc;
1022 else
1023 *result = page_to_nid(new_hpage);
1025 return rc;
1029 * migrate_pages - migrate the pages specified in a list, to the free pages
1030 * supplied as the target for the page migration
1032 * @from: The list of pages to be migrated.
1033 * @get_new_page: The function used to allocate free pages to be used
1034 * as the target of the page migration.
1035 * @put_new_page: The function used to free target pages if migration
1036 * fails, or NULL if no special handling is necessary.
1037 * @private: Private data to be passed on to get_new_page()
1038 * @mode: The migration mode that specifies the constraints for
1039 * page migration, if any.
1040 * @reason: The reason for page migration.
1042 * The function returns after 10 attempts or if no pages are movable any more
1043 * because the list has become empty or no retryable pages exist any more.
1044 * The caller should call putback_lru_pages() to return pages to the LRU
1045 * or free list only if ret != 0.
1047 * Returns the number of pages that were not migrated, or an error code.
1049 int migrate_pages(struct list_head *from, new_page_t get_new_page,
1050 free_page_t put_new_page, unsigned long private,
1051 enum migrate_mode mode, int reason)
1053 int retry = 1;
1054 int nr_failed = 0;
1055 int nr_succeeded = 0;
1056 int pass = 0;
1057 struct page *page;
1058 struct page *page2;
1059 int swapwrite = current->flags & PF_SWAPWRITE;
1060 int rc;
1062 if (!swapwrite)
1063 current->flags |= PF_SWAPWRITE;
1065 for(pass = 0; pass < 10 && retry; pass++) {
1066 retry = 0;
1068 list_for_each_entry_safe(page, page2, from, lru) {
1069 cond_resched();
1071 if (PageHuge(page))
1072 rc = unmap_and_move_huge_page(get_new_page,
1073 put_new_page, private, page,
1074 pass > 2, mode);
1075 else
1076 rc = unmap_and_move(get_new_page, put_new_page,
1077 private, page, pass > 2, mode);
1079 switch(rc) {
1080 case -ENOMEM:
1081 goto out;
1082 case -EAGAIN:
1083 retry++;
1084 break;
1085 case MIGRATEPAGE_SUCCESS:
1086 nr_succeeded++;
1087 break;
1088 default:
1089 /* Permanent failure */
1090 nr_failed++;
1091 break;
1095 rc = nr_failed + retry;
1096 out:
1097 if (nr_succeeded)
1098 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1099 if (nr_failed)
1100 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1101 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1103 if (!swapwrite)
1104 current->flags &= ~PF_SWAPWRITE;
1106 return rc;
1109 #ifdef CONFIG_NUMA
1111 * Move a list of individual pages
1113 struct page_to_node {
1114 unsigned long addr;
1115 struct page *page;
1116 int node;
1117 int status;
1120 static struct page *new_page_node(struct page *p, unsigned long private,
1121 int **result)
1123 struct page_to_node *pm = (struct page_to_node *)private;
1125 while (pm->node != MAX_NUMNODES && pm->page != p)
1126 pm++;
1128 if (pm->node == MAX_NUMNODES)
1129 return NULL;
1131 *result = &pm->status;
1133 if (PageHuge(p))
1134 return alloc_huge_page_node(page_hstate(compound_head(p)),
1135 pm->node);
1136 else
1137 return alloc_pages_exact_node(pm->node,
1138 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
1142 * Move a set of pages as indicated in the pm array. The addr
1143 * field must be set to the virtual address of the page to be moved
1144 * and the node number must contain a valid target node.
1145 * The pm array ends with node = MAX_NUMNODES.
1147 static int do_move_page_to_node_array(struct mm_struct *mm,
1148 struct page_to_node *pm,
1149 int migrate_all)
1151 int err;
1152 struct page_to_node *pp;
1153 LIST_HEAD(pagelist);
1155 down_read(&mm->mmap_sem);
1158 * Build a list of pages to migrate
1160 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1161 struct vm_area_struct *vma;
1162 struct page *page;
1164 err = -EFAULT;
1165 vma = find_vma(mm, pp->addr);
1166 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
1167 goto set_status;
1169 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
1171 err = PTR_ERR(page);
1172 if (IS_ERR(page))
1173 goto set_status;
1175 err = -ENOENT;
1176 if (!page)
1177 goto set_status;
1179 /* Use PageReserved to check for zero page */
1180 if (PageReserved(page))
1181 goto put_and_set;
1183 pp->page = page;
1184 err = page_to_nid(page);
1186 if (err == pp->node)
1188 * Node already in the right place
1190 goto put_and_set;
1192 err = -EACCES;
1193 if (page_mapcount(page) > 1 &&
1194 !migrate_all)
1195 goto put_and_set;
1197 if (PageHuge(page)) {
1198 if (PageHead(page))
1199 isolate_huge_page(page, &pagelist);
1200 goto put_and_set;
1203 err = isolate_lru_page(page);
1204 if (!err) {
1205 list_add_tail(&page->lru, &pagelist);
1206 inc_zone_page_state(page, NR_ISOLATED_ANON +
1207 page_is_file_cache(page));
1209 put_and_set:
1211 * Either remove the duplicate refcount from
1212 * isolate_lru_page() or drop the page ref if it was
1213 * not isolated.
1215 put_page(page);
1216 set_status:
1217 pp->status = err;
1220 err = 0;
1221 if (!list_empty(&pagelist)) {
1222 err = migrate_pages(&pagelist, new_page_node, NULL,
1223 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
1224 if (err)
1225 putback_movable_pages(&pagelist);
1228 up_read(&mm->mmap_sem);
1229 return err;
1233 * Migrate an array of page address onto an array of nodes and fill
1234 * the corresponding array of status.
1236 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
1237 unsigned long nr_pages,
1238 const void __user * __user *pages,
1239 const int __user *nodes,
1240 int __user *status, int flags)
1242 struct page_to_node *pm;
1243 unsigned long chunk_nr_pages;
1244 unsigned long chunk_start;
1245 int err;
1247 err = -ENOMEM;
1248 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1249 if (!pm)
1250 goto out;
1252 migrate_prep();
1255 * Store a chunk of page_to_node array in a page,
1256 * but keep the last one as a marker
1258 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
1260 for (chunk_start = 0;
1261 chunk_start < nr_pages;
1262 chunk_start += chunk_nr_pages) {
1263 int j;
1265 if (chunk_start + chunk_nr_pages > nr_pages)
1266 chunk_nr_pages = nr_pages - chunk_start;
1268 /* fill the chunk pm with addrs and nodes from user-space */
1269 for (j = 0; j < chunk_nr_pages; j++) {
1270 const void __user *p;
1271 int node;
1273 err = -EFAULT;
1274 if (get_user(p, pages + j + chunk_start))
1275 goto out_pm;
1276 pm[j].addr = (unsigned long) p;
1278 if (get_user(node, nodes + j + chunk_start))
1279 goto out_pm;
1281 err = -ENODEV;
1282 if (node < 0 || node >= MAX_NUMNODES)
1283 goto out_pm;
1285 if (!node_state(node, N_MEMORY))
1286 goto out_pm;
1288 err = -EACCES;
1289 if (!node_isset(node, task_nodes))
1290 goto out_pm;
1292 pm[j].node = node;
1295 /* End marker for this chunk */
1296 pm[chunk_nr_pages].node = MAX_NUMNODES;
1298 /* Migrate this chunk */
1299 err = do_move_page_to_node_array(mm, pm,
1300 flags & MPOL_MF_MOVE_ALL);
1301 if (err < 0)
1302 goto out_pm;
1304 /* Return status information */
1305 for (j = 0; j < chunk_nr_pages; j++)
1306 if (put_user(pm[j].status, status + j + chunk_start)) {
1307 err = -EFAULT;
1308 goto out_pm;
1311 err = 0;
1313 out_pm:
1314 free_page((unsigned long)pm);
1315 out:
1316 return err;
1320 * Determine the nodes of an array of pages and store it in an array of status.
1322 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1323 const void __user **pages, int *status)
1325 unsigned long i;
1327 down_read(&mm->mmap_sem);
1329 for (i = 0; i < nr_pages; i++) {
1330 unsigned long addr = (unsigned long)(*pages);
1331 struct vm_area_struct *vma;
1332 struct page *page;
1333 int err = -EFAULT;
1335 vma = find_vma(mm, addr);
1336 if (!vma || addr < vma->vm_start)
1337 goto set_status;
1339 page = follow_page(vma, addr, 0);
1341 err = PTR_ERR(page);
1342 if (IS_ERR(page))
1343 goto set_status;
1345 err = -ENOENT;
1346 /* Use PageReserved to check for zero page */
1347 if (!page || PageReserved(page))
1348 goto set_status;
1350 err = page_to_nid(page);
1351 set_status:
1352 *status = err;
1354 pages++;
1355 status++;
1358 up_read(&mm->mmap_sem);
1362 * Determine the nodes of a user array of pages and store it in
1363 * a user array of status.
1365 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1366 const void __user * __user *pages,
1367 int __user *status)
1369 #define DO_PAGES_STAT_CHUNK_NR 16
1370 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1371 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1373 while (nr_pages) {
1374 unsigned long chunk_nr;
1376 chunk_nr = nr_pages;
1377 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1378 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1380 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1381 break;
1383 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1385 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1386 break;
1388 pages += chunk_nr;
1389 status += chunk_nr;
1390 nr_pages -= chunk_nr;
1392 return nr_pages ? -EFAULT : 0;
1396 * Move a list of pages in the address space of the currently executing
1397 * process.
1399 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1400 const void __user * __user *, pages,
1401 const int __user *, nodes,
1402 int __user *, status, int, flags)
1404 const struct cred *cred = current_cred(), *tcred;
1405 struct task_struct *task;
1406 struct mm_struct *mm;
1407 int err;
1408 nodemask_t task_nodes;
1410 /* Check flags */
1411 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1412 return -EINVAL;
1414 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1415 return -EPERM;
1417 /* Find the mm_struct */
1418 rcu_read_lock();
1419 task = pid ? find_task_by_vpid(pid) : current;
1420 if (!task) {
1421 rcu_read_unlock();
1422 return -ESRCH;
1424 get_task_struct(task);
1427 * Check if this process has the right to modify the specified
1428 * process. The right exists if the process has administrative
1429 * capabilities, superuser privileges or the same
1430 * userid as the target process.
1432 tcred = __task_cred(task);
1433 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1434 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
1435 !capable(CAP_SYS_NICE)) {
1436 rcu_read_unlock();
1437 err = -EPERM;
1438 goto out;
1440 rcu_read_unlock();
1442 err = security_task_movememory(task);
1443 if (err)
1444 goto out;
1446 task_nodes = cpuset_mems_allowed(task);
1447 mm = get_task_mm(task);
1448 put_task_struct(task);
1450 if (!mm)
1451 return -EINVAL;
1453 if (nodes)
1454 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1455 nodes, status, flags);
1456 else
1457 err = do_pages_stat(mm, nr_pages, pages, status);
1459 mmput(mm);
1460 return err;
1462 out:
1463 put_task_struct(task);
1464 return err;
1468 * Call migration functions in the vma_ops that may prepare
1469 * memory in a vm for migration. migration functions may perform
1470 * the migration for vmas that do not have an underlying page struct.
1472 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1473 const nodemask_t *from, unsigned long flags)
1475 struct vm_area_struct *vma;
1476 int err = 0;
1478 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1479 if (vma->vm_ops && vma->vm_ops->migrate) {
1480 err = vma->vm_ops->migrate(vma, to, from, flags);
1481 if (err)
1482 break;
1485 return err;
1488 #ifdef CONFIG_NUMA_BALANCING
1490 * Returns true if this is a safe migration target node for misplaced NUMA
1491 * pages. Currently it only checks the watermarks which crude
1493 static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
1494 unsigned long nr_migrate_pages)
1496 int z;
1497 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1498 struct zone *zone = pgdat->node_zones + z;
1500 if (!populated_zone(zone))
1501 continue;
1503 if (!zone_reclaimable(zone))
1504 continue;
1506 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1507 if (!zone_watermark_ok(zone, 0,
1508 high_wmark_pages(zone) +
1509 nr_migrate_pages,
1510 0, 0))
1511 continue;
1512 return true;
1514 return false;
1517 static struct page *alloc_misplaced_dst_page(struct page *page,
1518 unsigned long data,
1519 int **result)
1521 int nid = (int) data;
1522 struct page *newpage;
1524 newpage = alloc_pages_exact_node(nid,
1525 (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
1526 __GFP_NOMEMALLOC | __GFP_NORETRY |
1527 __GFP_NOWARN) &
1528 ~GFP_IOFS, 0);
1529 if (newpage)
1530 page_nid_xchg_last(newpage, page_nid_last(page));
1532 return newpage;
1536 * page migration rate limiting control.
1537 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1538 * window of time. Default here says do not migrate more than 1280M per second.
1539 * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
1540 * as it is faults that reset the window, pte updates will happen unconditionally
1541 * if there has not been a fault since @pteupdate_interval_millisecs after the
1542 * throttle window closed.
1544 static unsigned int migrate_interval_millisecs __read_mostly = 100;
1545 static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
1546 static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1548 /* Returns true if NUMA migration is currently rate limited */
1549 bool migrate_ratelimited(int node)
1551 pg_data_t *pgdat = NODE_DATA(node);
1553 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
1554 msecs_to_jiffies(pteupdate_interval_millisecs)))
1555 return false;
1557 if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
1558 return false;
1560 return true;
1563 /* Returns true if the node is migrate rate-limited after the update */
1564 bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
1566 bool rate_limited = false;
1569 * Rate-limit the amount of data that is being migrated to a node.
1570 * Optimal placement is no good if the memory bus is saturated and
1571 * all the time is being spent migrating!
1573 spin_lock(&pgdat->numabalancing_migrate_lock);
1574 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
1575 pgdat->numabalancing_migrate_nr_pages = 0;
1576 pgdat->numabalancing_migrate_next_window = jiffies +
1577 msecs_to_jiffies(migrate_interval_millisecs);
1579 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
1580 rate_limited = true;
1581 else
1582 pgdat->numabalancing_migrate_nr_pages += nr_pages;
1583 spin_unlock(&pgdat->numabalancing_migrate_lock);
1585 return rate_limited;
1588 int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
1590 int page_lru;
1592 VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
1594 /* Avoid migrating to a node that is nearly full */
1595 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1596 return 0;
1598 if (isolate_lru_page(page))
1599 return 0;
1602 * migrate_misplaced_transhuge_page() skips page migration's usual
1603 * check on page_count(), so we must do it here, now that the page
1604 * has been isolated: a GUP pin, or any other pin, prevents migration.
1605 * The expected page count is 3: 1 for page's mapcount and 1 for the
1606 * caller's pin and 1 for the reference taken by isolate_lru_page().
1608 if (PageTransHuge(page) && page_count(page) != 3) {
1609 putback_lru_page(page);
1610 return 0;
1613 page_lru = page_is_file_cache(page);
1614 mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
1615 hpage_nr_pages(page));
1618 * Isolating the page has taken another reference, so the
1619 * caller's reference can be safely dropped without the page
1620 * disappearing underneath us during migration.
1622 put_page(page);
1623 return 1;
1626 bool pmd_trans_migrating(pmd_t pmd)
1628 struct page *page = pmd_page(pmd);
1629 return PageLocked(page);
1632 void wait_migrate_huge_page(struct anon_vma *anon_vma, pmd_t *pmd)
1634 struct page *page = pmd_page(*pmd);
1635 wait_on_page_locked(page);
1639 * Attempt to migrate a misplaced page to the specified destination
1640 * node. Caller is expected to have an elevated reference count on
1641 * the page that will be dropped by this function before returning.
1643 int migrate_misplaced_page(struct page *page, int node)
1645 pg_data_t *pgdat = NODE_DATA(node);
1646 int isolated;
1647 int nr_remaining;
1648 LIST_HEAD(migratepages);
1651 * Don't migrate pages that are mapped in multiple processes.
1652 * TODO: Handle false sharing detection instead of this hammer
1654 if (page_mapcount(page) != 1)
1655 goto out;
1658 * Rate-limit the amount of data that is being migrated to a node.
1659 * Optimal placement is no good if the memory bus is saturated and
1660 * all the time is being spent migrating!
1662 if (numamigrate_update_ratelimit(pgdat, 1))
1663 goto out;
1665 isolated = numamigrate_isolate_page(pgdat, page);
1666 if (!isolated)
1667 goto out;
1669 list_add(&page->lru, &migratepages);
1670 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
1671 NULL, node, MIGRATE_ASYNC,
1672 MR_NUMA_MISPLACED);
1673 if (nr_remaining) {
1674 putback_lru_pages(&migratepages);
1675 isolated = 0;
1676 } else
1677 count_vm_numa_event(NUMA_PAGE_MIGRATE);
1678 BUG_ON(!list_empty(&migratepages));
1679 return isolated;
1681 out:
1682 put_page(page);
1683 return 0;
1685 #endif /* CONFIG_NUMA_BALANCING */
1687 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1689 * Migrates a THP to a given target node. page must be locked and is unlocked
1690 * before returning.
1692 int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1693 struct vm_area_struct *vma,
1694 pmd_t *pmd, pmd_t entry,
1695 unsigned long address,
1696 struct page *page, int node)
1698 pg_data_t *pgdat = NODE_DATA(node);
1699 int isolated = 0;
1700 struct page *new_page = NULL;
1701 struct mem_cgroup *memcg = NULL;
1702 int page_lru = page_is_file_cache(page);
1703 unsigned long mmun_start = address & HPAGE_PMD_MASK;
1704 unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
1705 pmd_t orig_entry;
1708 * Don't migrate pages that are mapped in multiple processes.
1709 * TODO: Handle false sharing detection instead of this hammer
1711 if (page_mapcount(page) != 1)
1712 goto out_dropref;
1715 * Rate-limit the amount of data that is being migrated to a node.
1716 * Optimal placement is no good if the memory bus is saturated and
1717 * all the time is being spent migrating!
1719 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
1720 goto out_dropref;
1722 new_page = alloc_pages_node(node,
1723 (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
1724 if (!new_page)
1725 goto out_fail;
1727 page_nid_xchg_last(new_page, page_nid_last(page));
1729 isolated = numamigrate_isolate_page(pgdat, page);
1730 if (!isolated) {
1731 put_page(new_page);
1732 goto out_fail;
1735 if (mm_tlb_flush_pending(mm))
1736 flush_tlb_range(vma, mmun_start, mmun_end);
1738 /* Prepare a page as a migration target */
1739 __set_page_locked(new_page);
1740 SetPageSwapBacked(new_page);
1742 /* anon mapping, we can simply copy page->mapping to the new page: */
1743 new_page->mapping = page->mapping;
1744 new_page->index = page->index;
1745 migrate_page_copy(new_page, page);
1746 WARN_ON(PageLRU(new_page));
1748 /* Recheck the target PMD */
1749 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1750 spin_lock(&mm->page_table_lock);
1751 if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
1752 fail_putback:
1753 spin_unlock(&mm->page_table_lock);
1754 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1756 /* Reverse changes made by migrate_page_copy() */
1757 if (TestClearPageActive(new_page))
1758 SetPageActive(page);
1759 if (TestClearPageUnevictable(new_page))
1760 SetPageUnevictable(page);
1761 mlock_migrate_page(page, new_page);
1763 unlock_page(new_page);
1764 put_page(new_page); /* Free it */
1766 /* Retake the callers reference and putback on LRU */
1767 get_page(page);
1768 putback_lru_page(page);
1769 mod_zone_page_state(page_zone(page),
1770 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
1772 goto out_unlock;
1776 * Traditional migration needs to prepare the memcg charge
1777 * transaction early to prevent the old page from being
1778 * uncharged when installing migration entries. Here we can
1779 * save the potential rollback and start the charge transfer
1780 * only when migration is already known to end successfully.
1782 mem_cgroup_prepare_migration(page, new_page, &memcg);
1784 orig_entry = *pmd;
1785 entry = mk_pmd(new_page, vma->vm_page_prot);
1786 entry = pmd_mkhuge(entry);
1787 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1790 * Clear the old entry under pagetable lock and establish the new PTE.
1791 * Any parallel GUP will either observe the old page blocking on the
1792 * page lock, block on the page table lock or observe the new page.
1793 * The SetPageUptodate on the new page and page_add_new_anon_rmap
1794 * guarantee the copy is visible before the pagetable update.
1796 flush_cache_range(vma, mmun_start, mmun_end);
1797 page_add_new_anon_rmap(new_page, vma, mmun_start);
1798 pmdp_clear_flush(vma, mmun_start, pmd);
1799 set_pmd_at(mm, mmun_start, pmd, entry);
1800 flush_tlb_range(vma, mmun_start, mmun_end);
1801 update_mmu_cache_pmd(vma, address, &entry);
1803 if (page_count(page) != 2) {
1804 set_pmd_at(mm, mmun_start, pmd, orig_entry);
1805 flush_tlb_range(vma, mmun_start, mmun_end);
1806 update_mmu_cache_pmd(vma, address, &entry);
1807 page_remove_rmap(new_page);
1808 goto fail_putback;
1811 page_remove_rmap(page);
1814 * Finish the charge transaction under the page table lock to
1815 * prevent split_huge_page() from dividing up the charge
1816 * before it's fully transferred to the new page.
1818 mem_cgroup_end_migration(memcg, page, new_page, true);
1819 spin_unlock(&mm->page_table_lock);
1820 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1822 unlock_page(new_page);
1823 unlock_page(page);
1824 put_page(page); /* Drop the rmap reference */
1825 put_page(page); /* Drop the LRU isolation reference */
1827 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
1828 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
1830 mod_zone_page_state(page_zone(page),
1831 NR_ISOLATED_ANON + page_lru,
1832 -HPAGE_PMD_NR);
1833 return isolated;
1835 out_fail:
1836 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
1837 out_dropref:
1838 spin_lock(&mm->page_table_lock);
1839 if (pmd_same(*pmd, entry)) {
1840 entry = pmd_mknonnuma(entry);
1841 set_pmd_at(mm, mmun_start, pmd, entry);
1842 update_mmu_cache_pmd(vma, address, &entry);
1844 spin_unlock(&mm->page_table_lock);
1846 out_unlock:
1847 unlock_page(page);
1848 put_page(page);
1849 return 0;
1851 #endif /* CONFIG_NUMA_BALANCING */
1853 #endif /* CONFIG_NUMA */