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/hugetlb_cgroup.h> 37 #include <linux/gfp.h> 38 #include <linux/balloon_compaction.h> 39 40 #include <asm/tlbflush.h> 41 42 #define CREATE_TRACE_POINTS 43 #include <trace/events/migrate.h> 44 45 #include "internal.h" 46 47 /* 48 * migrate_prep() needs to be called before we start compiling a list of pages 49 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is 50 * undesirable, use migrate_prep_local() 51 */ 52 int migrate_prep(void) 53 { 54 /* 55 * Clear the LRU lists so pages can be isolated. 56 * Note that pages may be moved off the LRU after we have 57 * drained them. Those pages will fail to migrate like other 58 * pages that may be busy. 59 */ 60 lru_add_drain_all(); 61 62 return 0; 63 } 64 65 /* Do the necessary work of migrate_prep but not if it involves other CPUs */ 66 int migrate_prep_local(void) 67 { 68 lru_add_drain(); 69 70 return 0; 71 } 72 73 /* 74 * Add isolated pages on the list back to the LRU under page lock 75 * to avoid leaking evictable pages back onto unevictable list. 76 */ 77 void putback_lru_pages(struct list_head *l) 78 { 79 struct page *page; 80 struct page *page2; 81 82 list_for_each_entry_safe(page, page2, l, lru) { 83 list_del(&page->lru); 84 dec_zone_page_state(page, NR_ISOLATED_ANON + 85 page_is_file_cache(page)); 86 putback_lru_page(page); 87 } 88 } 89 90 /* 91 * Put previously isolated pages back onto the appropriate lists 92 * from where they were once taken off for compaction/migration. 93 * 94 * This function shall be used instead of putback_lru_pages(), 95 * whenever the isolated pageset has been built by isolate_migratepages_range() 96 */ 97 void putback_movable_pages(struct list_head *l) 98 { 99 struct page *page; 100 struct page *page2; 101 102 list_for_each_entry_safe(page, page2, l, lru) { 103 if (unlikely(PageHuge(page))) { 104 putback_active_hugepage(page); 105 continue; 106 } 107 list_del(&page->lru); 108 dec_zone_page_state(page, NR_ISOLATED_ANON + 109 page_is_file_cache(page)); 110 if (unlikely(isolated_balloon_page(page))) 111 balloon_page_putback(page); 112 else 113 putback_lru_page(page); 114 } 115 } 116 117 /* 118 * Restore a potential migration pte to a working pte entry 119 */ 120 static int remove_migration_pte(struct page *new, struct vm_area_struct *vma, 121 unsigned long addr, void *old) 122 { 123 struct mm_struct *mm = vma->vm_mm; 124 swp_entry_t entry; 125 pmd_t *pmd; 126 pte_t *ptep, pte; 127 spinlock_t *ptl; 128 129 if (unlikely(PageHuge(new))) { 130 ptep = huge_pte_offset(mm, addr); 131 if (!ptep) 132 goto out; 133 ptl = &mm->page_table_lock; 134 } else { 135 pmd = mm_find_pmd(mm, addr); 136 if (!pmd) 137 goto out; 138 if (pmd_trans_huge(*pmd)) 139 goto out; 140 141 ptep = pte_offset_map(pmd, addr); 142 143 /* 144 * Peek to check is_swap_pte() before taking ptlock? No, we 145 * can race mremap's move_ptes(), which skips anon_vma lock. 146 */ 147 148 ptl = pte_lockptr(mm, pmd); 149 } 150 151 spin_lock(ptl); 152 pte = *ptep; 153 if (!is_swap_pte(pte)) 154 goto unlock; 155 156 entry = pte_to_swp_entry(pte); 157 158 if (!is_migration_entry(entry) || 159 migration_entry_to_page(entry) != old) 160 goto unlock; 161 162 get_page(new); 163 pte = pte_mkold(mk_pte(new, vma->vm_page_prot)); 164 if (pte_swp_soft_dirty(*ptep)) 165 pte = pte_mksoft_dirty(pte); 166 if (is_write_migration_entry(entry)) 167 pte = pte_mkwrite(pte); 168 #ifdef CONFIG_HUGETLB_PAGE 169 if (PageHuge(new)) { 170 pte = pte_mkhuge(pte); 171 pte = arch_make_huge_pte(pte, vma, new, 0); 172 } 173 #endif 174 flush_dcache_page(new); 175 set_pte_at(mm, addr, ptep, pte); 176 177 if (PageHuge(new)) { 178 if (PageAnon(new)) 179 hugepage_add_anon_rmap(new, vma, addr); 180 else 181 page_dup_rmap(new); 182 } else if (PageAnon(new)) 183 page_add_anon_rmap(new, vma, addr); 184 else 185 page_add_file_rmap(new); 186 187 /* No need to invalidate - it was non-present before */ 188 update_mmu_cache(vma, addr, ptep); 189 unlock: 190 pte_unmap_unlock(ptep, ptl); 191 out: 192 return SWAP_AGAIN; 193 } 194 195 /* 196 * Get rid of all migration entries and replace them by 197 * references to the indicated page. 198 */ 199 static void remove_migration_ptes(struct page *old, struct page *new) 200 { 201 rmap_walk(new, remove_migration_pte, old); 202 } 203 204 /* 205 * Something used the pte of a page under migration. We need to 206 * get to the page and wait until migration is finished. 207 * When we return from this function the fault will be retried. 208 */ 209 static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, 210 spinlock_t *ptl) 211 { 212 pte_t pte; 213 swp_entry_t entry; 214 struct page *page; 215 216 spin_lock(ptl); 217 pte = *ptep; 218 if (!is_swap_pte(pte)) 219 goto out; 220 221 entry = pte_to_swp_entry(pte); 222 if (!is_migration_entry(entry)) 223 goto out; 224 225 page = migration_entry_to_page(entry); 226 227 /* 228 * Once radix-tree replacement of page migration started, page_count 229 * *must* be zero. And, we don't want to call wait_on_page_locked() 230 * against a page without get_page(). 231 * So, we use get_page_unless_zero(), here. Even failed, page fault 232 * will occur again. 233 */ 234 if (!get_page_unless_zero(page)) 235 goto out; 236 pte_unmap_unlock(ptep, ptl); 237 wait_on_page_locked(page); 238 put_page(page); 239 return; 240 out: 241 pte_unmap_unlock(ptep, ptl); 242 } 243 244 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, 245 unsigned long address) 246 { 247 spinlock_t *ptl = pte_lockptr(mm, pmd); 248 pte_t *ptep = pte_offset_map(pmd, address); 249 __migration_entry_wait(mm, ptep, ptl); 250 } 251 252 void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte) 253 { 254 spinlock_t *ptl = &(mm)->page_table_lock; 255 __migration_entry_wait(mm, pte, ptl); 256 } 257 258 #ifdef CONFIG_BLOCK 259 /* Returns true if all buffers are successfully locked */ 260 static bool buffer_migrate_lock_buffers(struct buffer_head *head, 261 enum migrate_mode mode) 262 { 263 struct buffer_head *bh = head; 264 265 /* Simple case, sync compaction */ 266 if (mode != MIGRATE_ASYNC) { 267 do { 268 get_bh(bh); 269 lock_buffer(bh); 270 bh = bh->b_this_page; 271 272 } while (bh != head); 273 274 return true; 275 } 276 277 /* async case, we cannot block on lock_buffer so use trylock_buffer */ 278 do { 279 get_bh(bh); 280 if (!trylock_buffer(bh)) { 281 /* 282 * We failed to lock the buffer and cannot stall in 283 * async migration. Release the taken locks 284 */ 285 struct buffer_head *failed_bh = bh; 286 put_bh(failed_bh); 287 bh = head; 288 while (bh != failed_bh) { 289 unlock_buffer(bh); 290 put_bh(bh); 291 bh = bh->b_this_page; 292 } 293 return false; 294 } 295 296 bh = bh->b_this_page; 297 } while (bh != head); 298 return true; 299 } 300 #else 301 static inline bool buffer_migrate_lock_buffers(struct buffer_head *head, 302 enum migrate_mode mode) 303 { 304 return true; 305 } 306 #endif /* CONFIG_BLOCK */ 307 308 /* 309 * Replace the page in the mapping. 310 * 311 * The number of remaining references must be: 312 * 1 for anonymous pages without a mapping 313 * 2 for pages with a mapping 314 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. 315 */ 316 int migrate_page_move_mapping(struct address_space *mapping, 317 struct page *newpage, struct page *page, 318 struct buffer_head *head, enum migrate_mode mode) 319 { 320 int expected_count = 0; 321 void **pslot; 322 323 if (!mapping) { 324 /* Anonymous page without mapping */ 325 if (page_count(page) != 1) 326 return -EAGAIN; 327 return MIGRATEPAGE_SUCCESS; 328 } 329 330 spin_lock_irq(&mapping->tree_lock); 331 332 pslot = radix_tree_lookup_slot(&mapping->page_tree, 333 page_index(page)); 334 335 expected_count = 2 + page_has_private(page); 336 if (page_count(page) != expected_count || 337 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { 338 spin_unlock_irq(&mapping->tree_lock); 339 return -EAGAIN; 340 } 341 342 if (!page_freeze_refs(page, expected_count)) { 343 spin_unlock_irq(&mapping->tree_lock); 344 return -EAGAIN; 345 } 346 347 /* 348 * In the async migration case of moving a page with buffers, lock the 349 * buffers using trylock before the mapping is moved. If the mapping 350 * was moved, we later failed to lock the buffers and could not move 351 * the mapping back due to an elevated page count, we would have to 352 * block waiting on other references to be dropped. 353 */ 354 if (mode == MIGRATE_ASYNC && head && 355 !buffer_migrate_lock_buffers(head, mode)) { 356 page_unfreeze_refs(page, expected_count); 357 spin_unlock_irq(&mapping->tree_lock); 358 return -EAGAIN; 359 } 360 361 /* 362 * Now we know that no one else is looking at the page. 363 */ 364 get_page(newpage); /* add cache reference */ 365 if (PageSwapCache(page)) { 366 SetPageSwapCache(newpage); 367 set_page_private(newpage, page_private(page)); 368 } 369 370 radix_tree_replace_slot(pslot, newpage); 371 372 /* 373 * Drop cache reference from old page by unfreezing 374 * to one less reference. 375 * We know this isn't the last reference. 376 */ 377 page_unfreeze_refs(page, expected_count - 1); 378 379 /* 380 * If moved to a different zone then also account 381 * the page for that zone. Other VM counters will be 382 * taken care of when we establish references to the 383 * new page and drop references to the old page. 384 * 385 * Note that anonymous pages are accounted for 386 * via NR_FILE_PAGES and NR_ANON_PAGES if they 387 * are mapped to swap space. 388 */ 389 __dec_zone_page_state(page, NR_FILE_PAGES); 390 __inc_zone_page_state(newpage, NR_FILE_PAGES); 391 if (!PageSwapCache(page) && PageSwapBacked(page)) { 392 __dec_zone_page_state(page, NR_SHMEM); 393 __inc_zone_page_state(newpage, NR_SHMEM); 394 } 395 spin_unlock_irq(&mapping->tree_lock); 396 397 return MIGRATEPAGE_SUCCESS; 398 } 399 400 /* 401 * The expected number of remaining references is the same as that 402 * of migrate_page_move_mapping(). 403 */ 404 int migrate_huge_page_move_mapping(struct address_space *mapping, 405 struct page *newpage, struct page *page) 406 { 407 int expected_count; 408 void **pslot; 409 410 if (!mapping) { 411 if (page_count(page) != 1) 412 return -EAGAIN; 413 return MIGRATEPAGE_SUCCESS; 414 } 415 416 spin_lock_irq(&mapping->tree_lock); 417 418 pslot = radix_tree_lookup_slot(&mapping->page_tree, 419 page_index(page)); 420 421 expected_count = 2 + page_has_private(page); 422 if (page_count(page) != expected_count || 423 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { 424 spin_unlock_irq(&mapping->tree_lock); 425 return -EAGAIN; 426 } 427 428 if (!page_freeze_refs(page, expected_count)) { 429 spin_unlock_irq(&mapping->tree_lock); 430 return -EAGAIN; 431 } 432 433 get_page(newpage); 434 435 radix_tree_replace_slot(pslot, newpage); 436 437 page_unfreeze_refs(page, expected_count - 1); 438 439 spin_unlock_irq(&mapping->tree_lock); 440 return MIGRATEPAGE_SUCCESS; 441 } 442 443 /* 444 * Copy the page to its new location 445 */ 446 void migrate_page_copy(struct page *newpage, struct page *page) 447 { 448 int cpupid; 449 450 if (PageHuge(page) || PageTransHuge(page)) 451 copy_huge_page(newpage, page); 452 else 453 copy_highpage(newpage, page); 454 455 if (PageError(page)) 456 SetPageError(newpage); 457 if (PageReferenced(page)) 458 SetPageReferenced(newpage); 459 if (PageUptodate(page)) 460 SetPageUptodate(newpage); 461 if (TestClearPageActive(page)) { 462 VM_BUG_ON(PageUnevictable(page)); 463 SetPageActive(newpage); 464 } else if (TestClearPageUnevictable(page)) 465 SetPageUnevictable(newpage); 466 if (PageChecked(page)) 467 SetPageChecked(newpage); 468 if (PageMappedToDisk(page)) 469 SetPageMappedToDisk(newpage); 470 471 if (PageDirty(page)) { 472 clear_page_dirty_for_io(page); 473 /* 474 * Want to mark the page and the radix tree as dirty, and 475 * redo the accounting that clear_page_dirty_for_io undid, 476 * but we can't use set_page_dirty because that function 477 * is actually a signal that all of the page has become dirty. 478 * Whereas only part of our page may be dirty. 479 */ 480 if (PageSwapBacked(page)) 481 SetPageDirty(newpage); 482 else 483 __set_page_dirty_nobuffers(newpage); 484 } 485 486 /* 487 * Copy NUMA information to the new page, to prevent over-eager 488 * future migrations of this same page. 489 */ 490 cpupid = page_cpupid_xchg_last(page, -1); 491 page_cpupid_xchg_last(newpage, cpupid); 492 493 mlock_migrate_page(newpage, page); 494 ksm_migrate_page(newpage, page); 495 /* 496 * Please do not reorder this without considering how mm/ksm.c's 497 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). 498 */ 499 ClearPageSwapCache(page); 500 ClearPagePrivate(page); 501 set_page_private(page, 0); 502 503 /* 504 * If any waiters have accumulated on the new page then 505 * wake them up. 506 */ 507 if (PageWriteback(newpage)) 508 end_page_writeback(newpage); 509 } 510 511 /************************************************************ 512 * Migration functions 513 ***********************************************************/ 514 515 /* Always fail migration. Used for mappings that are not movable */ 516 int fail_migrate_page(struct address_space *mapping, 517 struct page *newpage, struct page *page) 518 { 519 return -EIO; 520 } 521 EXPORT_SYMBOL(fail_migrate_page); 522 523 /* 524 * Common logic to directly migrate a single page suitable for 525 * pages that do not use PagePrivate/PagePrivate2. 526 * 527 * Pages are locked upon entry and exit. 528 */ 529 int migrate_page(struct address_space *mapping, 530 struct page *newpage, struct page *page, 531 enum migrate_mode mode) 532 { 533 int rc; 534 535 BUG_ON(PageWriteback(page)); /* Writeback must be complete */ 536 537 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode); 538 539 if (rc != MIGRATEPAGE_SUCCESS) 540 return rc; 541 542 migrate_page_copy(newpage, page); 543 return MIGRATEPAGE_SUCCESS; 544 } 545 EXPORT_SYMBOL(migrate_page); 546 547 #ifdef CONFIG_BLOCK 548 /* 549 * Migration function for pages with buffers. This function can only be used 550 * if the underlying filesystem guarantees that no other references to "page" 551 * exist. 552 */ 553 int buffer_migrate_page(struct address_space *mapping, 554 struct page *newpage, struct page *page, enum migrate_mode mode) 555 { 556 struct buffer_head *bh, *head; 557 int rc; 558 559 if (!page_has_buffers(page)) 560 return migrate_page(mapping, newpage, page, mode); 561 562 head = page_buffers(page); 563 564 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode); 565 566 if (rc != MIGRATEPAGE_SUCCESS) 567 return rc; 568 569 /* 570 * In the async case, migrate_page_move_mapping locked the buffers 571 * with an IRQ-safe spinlock held. In the sync case, the buffers 572 * need to be locked now 573 */ 574 if (mode != MIGRATE_ASYNC) 575 BUG_ON(!buffer_migrate_lock_buffers(head, mode)); 576 577 ClearPagePrivate(page); 578 set_page_private(newpage, page_private(page)); 579 set_page_private(page, 0); 580 put_page(page); 581 get_page(newpage); 582 583 bh = head; 584 do { 585 set_bh_page(bh, newpage, bh_offset(bh)); 586 bh = bh->b_this_page; 587 588 } while (bh != head); 589 590 SetPagePrivate(newpage); 591 592 migrate_page_copy(newpage, page); 593 594 bh = head; 595 do { 596 unlock_buffer(bh); 597 put_bh(bh); 598 bh = bh->b_this_page; 599 600 } while (bh != head); 601 602 return MIGRATEPAGE_SUCCESS; 603 } 604 EXPORT_SYMBOL(buffer_migrate_page); 605 #endif 606 607 /* 608 * Writeback a page to clean the dirty state 609 */ 610 static int writeout(struct address_space *mapping, struct page *page) 611 { 612 struct writeback_control wbc = { 613 .sync_mode = WB_SYNC_NONE, 614 .nr_to_write = 1, 615 .range_start = 0, 616 .range_end = LLONG_MAX, 617 .for_reclaim = 1 618 }; 619 int rc; 620 621 if (!mapping->a_ops->writepage) 622 /* No write method for the address space */ 623 return -EINVAL; 624 625 if (!clear_page_dirty_for_io(page)) 626 /* Someone else already triggered a write */ 627 return -EAGAIN; 628 629 /* 630 * A dirty page may imply that the underlying filesystem has 631 * the page on some queue. So the page must be clean for 632 * migration. Writeout may mean we loose the lock and the 633 * page state is no longer what we checked for earlier. 634 * At this point we know that the migration attempt cannot 635 * be successful. 636 */ 637 remove_migration_ptes(page, page); 638 639 rc = mapping->a_ops->writepage(page, &wbc); 640 641 if (rc != AOP_WRITEPAGE_ACTIVATE) 642 /* unlocked. Relock */ 643 lock_page(page); 644 645 return (rc < 0) ? -EIO : -EAGAIN; 646 } 647 648 /* 649 * Default handling if a filesystem does not provide a migration function. 650 */ 651 static int fallback_migrate_page(struct address_space *mapping, 652 struct page *newpage, struct page *page, enum migrate_mode mode) 653 { 654 if (PageDirty(page)) { 655 /* Only writeback pages in full synchronous migration */ 656 if (mode != MIGRATE_SYNC) 657 return -EBUSY; 658 return writeout(mapping, page); 659 } 660 661 /* 662 * Buffers may be managed in a filesystem specific way. 663 * We must have no buffers or drop them. 664 */ 665 if (page_has_private(page) && 666 !try_to_release_page(page, GFP_KERNEL)) 667 return -EAGAIN; 668 669 return migrate_page(mapping, newpage, page, mode); 670 } 671 672 /* 673 * Move a page to a newly allocated page 674 * The page is locked and all ptes have been successfully removed. 675 * 676 * The new page will have replaced the old page if this function 677 * is successful. 678 * 679 * Return value: 680 * < 0 - error code 681 * MIGRATEPAGE_SUCCESS - success 682 */ 683 static int move_to_new_page(struct page *newpage, struct page *page, 684 int remap_swapcache, enum migrate_mode mode) 685 { 686 struct address_space *mapping; 687 int rc; 688 689 /* 690 * Block others from accessing the page when we get around to 691 * establishing additional references. We are the only one 692 * holding a reference to the new page at this point. 693 */ 694 if (!trylock_page(newpage)) 695 BUG(); 696 697 /* Prepare mapping for the new page.*/ 698 newpage->index = page->index; 699 newpage->mapping = page->mapping; 700 if (PageSwapBacked(page)) 701 SetPageSwapBacked(newpage); 702 703 mapping = page_mapping(page); 704 if (!mapping) 705 rc = migrate_page(mapping, newpage, page, mode); 706 else if (mapping->a_ops->migratepage) 707 /* 708 * Most pages have a mapping and most filesystems provide a 709 * migratepage callback. Anonymous pages are part of swap 710 * space which also has its own migratepage callback. This 711 * is the most common path for page migration. 712 */ 713 rc = mapping->a_ops->migratepage(mapping, 714 newpage, page, mode); 715 else 716 rc = fallback_migrate_page(mapping, newpage, page, mode); 717 718 if (rc != MIGRATEPAGE_SUCCESS) { 719 newpage->mapping = NULL; 720 } else { 721 if (remap_swapcache) 722 remove_migration_ptes(page, newpage); 723 page->mapping = NULL; 724 } 725 726 unlock_page(newpage); 727 728 return rc; 729 } 730 731 static int __unmap_and_move(struct page *page, struct page *newpage, 732 int force, enum migrate_mode mode) 733 { 734 int rc = -EAGAIN; 735 int remap_swapcache = 1; 736 struct mem_cgroup *mem; 737 struct anon_vma *anon_vma = NULL; 738 739 if (!trylock_page(page)) { 740 if (!force || mode == MIGRATE_ASYNC) 741 goto out; 742 743 /* 744 * It's not safe for direct compaction to call lock_page. 745 * For example, during page readahead pages are added locked 746 * to the LRU. Later, when the IO completes the pages are 747 * marked uptodate and unlocked. However, the queueing 748 * could be merging multiple pages for one bio (e.g. 749 * mpage_readpages). If an allocation happens for the 750 * second or third page, the process can end up locking 751 * the same page twice and deadlocking. Rather than 752 * trying to be clever about what pages can be locked, 753 * avoid the use of lock_page for direct compaction 754 * altogether. 755 */ 756 if (current->flags & PF_MEMALLOC) 757 goto out; 758 759 lock_page(page); 760 } 761 762 /* charge against new page */ 763 mem_cgroup_prepare_migration(page, newpage, &mem); 764 765 if (PageWriteback(page)) { 766 /* 767 * Only in the case of a full synchronous migration is it 768 * necessary to wait for PageWriteback. In the async case, 769 * the retry loop is too short and in the sync-light case, 770 * the overhead of stalling is too much 771 */ 772 if (mode != MIGRATE_SYNC) { 773 rc = -EBUSY; 774 goto uncharge; 775 } 776 if (!force) 777 goto uncharge; 778 wait_on_page_writeback(page); 779 } 780 /* 781 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, 782 * we cannot notice that anon_vma is freed while we migrates a page. 783 * This get_anon_vma() delays freeing anon_vma pointer until the end 784 * of migration. File cache pages are no problem because of page_lock() 785 * File Caches may use write_page() or lock_page() in migration, then, 786 * just care Anon page here. 787 */ 788 if (PageAnon(page) && !PageKsm(page)) { 789 /* 790 * Only page_lock_anon_vma_read() understands the subtleties of 791 * getting a hold on an anon_vma from outside one of its mms. 792 */ 793 anon_vma = page_get_anon_vma(page); 794 if (anon_vma) { 795 /* 796 * Anon page 797 */ 798 } else if (PageSwapCache(page)) { 799 /* 800 * We cannot be sure that the anon_vma of an unmapped 801 * swapcache page is safe to use because we don't 802 * know in advance if the VMA that this page belonged 803 * to still exists. If the VMA and others sharing the 804 * data have been freed, then the anon_vma could 805 * already be invalid. 806 * 807 * To avoid this possibility, swapcache pages get 808 * migrated but are not remapped when migration 809 * completes 810 */ 811 remap_swapcache = 0; 812 } else { 813 goto uncharge; 814 } 815 } 816 817 if (unlikely(balloon_page_movable(page))) { 818 /* 819 * A ballooned page does not need any special attention from 820 * physical to virtual reverse mapping procedures. 821 * Skip any attempt to unmap PTEs or to remap swap cache, 822 * in order to avoid burning cycles at rmap level, and perform 823 * the page migration right away (proteced by page lock). 824 */ 825 rc = balloon_page_migrate(newpage, page, mode); 826 goto uncharge; 827 } 828 829 /* 830 * Corner case handling: 831 * 1. When a new swap-cache page is read into, it is added to the LRU 832 * and treated as swapcache but it has no rmap yet. 833 * Calling try_to_unmap() against a page->mapping==NULL page will 834 * trigger a BUG. So handle it here. 835 * 2. An orphaned page (see truncate_complete_page) might have 836 * fs-private metadata. The page can be picked up due to memory 837 * offlining. Everywhere else except page reclaim, the page is 838 * invisible to the vm, so the page can not be migrated. So try to 839 * free the metadata, so the page can be freed. 840 */ 841 if (!page->mapping) { 842 VM_BUG_ON(PageAnon(page)); 843 if (page_has_private(page)) { 844 try_to_free_buffers(page); 845 goto uncharge; 846 } 847 goto skip_unmap; 848 } 849 850 /* Establish migration ptes or remove ptes */ 851 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); 852 853 skip_unmap: 854 if (!page_mapped(page)) 855 rc = move_to_new_page(newpage, page, remap_swapcache, mode); 856 857 if (rc && remap_swapcache) 858 remove_migration_ptes(page, page); 859 860 /* Drop an anon_vma reference if we took one */ 861 if (anon_vma) 862 put_anon_vma(anon_vma); 863 864 uncharge: 865 mem_cgroup_end_migration(mem, page, newpage, 866 (rc == MIGRATEPAGE_SUCCESS || 867 rc == MIGRATEPAGE_BALLOON_SUCCESS)); 868 unlock_page(page); 869 out: 870 return rc; 871 } 872 873 /* 874 * Obtain the lock on page, remove all ptes and migrate the page 875 * to the newly allocated page in newpage. 876 */ 877 static int unmap_and_move(new_page_t get_new_page, unsigned long private, 878 struct page *page, int force, enum migrate_mode mode) 879 { 880 int rc = 0; 881 int *result = NULL; 882 struct page *newpage = get_new_page(page, private, &result); 883 884 if (!newpage) 885 return -ENOMEM; 886 887 if (page_count(page) == 1) { 888 /* page was freed from under us. So we are done. */ 889 goto out; 890 } 891 892 if (unlikely(PageTransHuge(page))) 893 if (unlikely(split_huge_page(page))) 894 goto out; 895 896 rc = __unmap_and_move(page, newpage, force, mode); 897 898 if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) { 899 /* 900 * A ballooned page has been migrated already. 901 * Now, it's the time to wrap-up counters, 902 * handle the page back to Buddy and return. 903 */ 904 dec_zone_page_state(page, NR_ISOLATED_ANON + 905 page_is_file_cache(page)); 906 balloon_page_free(page); 907 return MIGRATEPAGE_SUCCESS; 908 } 909 out: 910 if (rc != -EAGAIN) { 911 /* 912 * A page that has been migrated has all references 913 * removed and will be freed. A page that has not been 914 * migrated will have kepts its references and be 915 * restored. 916 */ 917 list_del(&page->lru); 918 dec_zone_page_state(page, NR_ISOLATED_ANON + 919 page_is_file_cache(page)); 920 putback_lru_page(page); 921 } 922 /* 923 * Move the new page to the LRU. If migration was not successful 924 * then this will free the page. 925 */ 926 putback_lru_page(newpage); 927 if (result) { 928 if (rc) 929 *result = rc; 930 else 931 *result = page_to_nid(newpage); 932 } 933 return rc; 934 } 935 936 /* 937 * Counterpart of unmap_and_move_page() for hugepage migration. 938 * 939 * This function doesn't wait the completion of hugepage I/O 940 * because there is no race between I/O and migration for hugepage. 941 * Note that currently hugepage I/O occurs only in direct I/O 942 * where no lock is held and PG_writeback is irrelevant, 943 * and writeback status of all subpages are counted in the reference 944 * count of the head page (i.e. if all subpages of a 2MB hugepage are 945 * under direct I/O, the reference of the head page is 512 and a bit more.) 946 * This means that when we try to migrate hugepage whose subpages are 947 * doing direct I/O, some references remain after try_to_unmap() and 948 * hugepage migration fails without data corruption. 949 * 950 * There is also no race when direct I/O is issued on the page under migration, 951 * because then pte is replaced with migration swap entry and direct I/O code 952 * will wait in the page fault for migration to complete. 953 */ 954 static int unmap_and_move_huge_page(new_page_t get_new_page, 955 unsigned long private, struct page *hpage, 956 int force, enum migrate_mode mode) 957 { 958 int rc = 0; 959 int *result = NULL; 960 struct page *new_hpage = get_new_page(hpage, private, &result); 961 struct anon_vma *anon_vma = NULL; 962 963 /* 964 * Movability of hugepages depends on architectures and hugepage size. 965 * This check is necessary because some callers of hugepage migration 966 * like soft offline and memory hotremove don't walk through page 967 * tables or check whether the hugepage is pmd-based or not before 968 * kicking migration. 969 */ 970 if (!hugepage_migration_support(page_hstate(hpage))) 971 return -ENOSYS; 972 973 if (!new_hpage) 974 return -ENOMEM; 975 976 rc = -EAGAIN; 977 978 if (!trylock_page(hpage)) { 979 if (!force || mode != MIGRATE_SYNC) 980 goto out; 981 lock_page(hpage); 982 } 983 984 if (PageAnon(hpage)) 985 anon_vma = page_get_anon_vma(hpage); 986 987 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); 988 989 if (!page_mapped(hpage)) 990 rc = move_to_new_page(new_hpage, hpage, 1, mode); 991 992 if (rc) 993 remove_migration_ptes(hpage, hpage); 994 995 if (anon_vma) 996 put_anon_vma(anon_vma); 997 998 if (!rc) 999 hugetlb_cgroup_migrate(hpage, new_hpage); 1000 1001 unlock_page(hpage); 1002 out: 1003 if (rc != -EAGAIN) 1004 putback_active_hugepage(hpage); 1005 put_page(new_hpage); 1006 if (result) { 1007 if (rc) 1008 *result = rc; 1009 else 1010 *result = page_to_nid(new_hpage); 1011 } 1012 return rc; 1013 } 1014 1015 /* 1016 * migrate_pages - migrate the pages specified in a list, to the free pages 1017 * supplied as the target for the page migration 1018 * 1019 * @from: The list of pages to be migrated. 1020 * @get_new_page: The function used to allocate free pages to be used 1021 * as the target of the page migration. 1022 * @private: Private data to be passed on to get_new_page() 1023 * @mode: The migration mode that specifies the constraints for 1024 * page migration, if any. 1025 * @reason: The reason for page migration. 1026 * 1027 * The function returns after 10 attempts or if no pages are movable any more 1028 * because the list has become empty or no retryable pages exist any more. 1029 * The caller should call putback_lru_pages() to return pages to the LRU 1030 * or free list only if ret != 0. 1031 * 1032 * Returns the number of pages that were not migrated, or an error code. 1033 */ 1034 int migrate_pages(struct list_head *from, new_page_t get_new_page, 1035 unsigned long private, enum migrate_mode mode, int reason) 1036 { 1037 int retry = 1; 1038 int nr_failed = 0; 1039 int nr_succeeded = 0; 1040 int pass = 0; 1041 struct page *page; 1042 struct page *page2; 1043 int swapwrite = current->flags & PF_SWAPWRITE; 1044 int rc; 1045 1046 if (!swapwrite) 1047 current->flags |= PF_SWAPWRITE; 1048 1049 for(pass = 0; pass < 10 && retry; pass++) { 1050 retry = 0; 1051 1052 list_for_each_entry_safe(page, page2, from, lru) { 1053 cond_resched(); 1054 1055 if (PageHuge(page)) 1056 rc = unmap_and_move_huge_page(get_new_page, 1057 private, page, pass > 2, mode); 1058 else 1059 rc = unmap_and_move(get_new_page, private, 1060 page, pass > 2, mode); 1061 1062 switch(rc) { 1063 case -ENOMEM: 1064 goto out; 1065 case -EAGAIN: 1066 retry++; 1067 break; 1068 case MIGRATEPAGE_SUCCESS: 1069 nr_succeeded++; 1070 break; 1071 default: 1072 /* Permanent failure */ 1073 nr_failed++; 1074 break; 1075 } 1076 } 1077 } 1078 rc = nr_failed + retry; 1079 out: 1080 if (nr_succeeded) 1081 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); 1082 if (nr_failed) 1083 count_vm_events(PGMIGRATE_FAIL, nr_failed); 1084 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason); 1085 1086 if (!swapwrite) 1087 current->flags &= ~PF_SWAPWRITE; 1088 1089 return rc; 1090 } 1091 1092 #ifdef CONFIG_NUMA 1093 /* 1094 * Move a list of individual pages 1095 */ 1096 struct page_to_node { 1097 unsigned long addr; 1098 struct page *page; 1099 int node; 1100 int status; 1101 }; 1102 1103 static struct page *new_page_node(struct page *p, unsigned long private, 1104 int **result) 1105 { 1106 struct page_to_node *pm = (struct page_to_node *)private; 1107 1108 while (pm->node != MAX_NUMNODES && pm->page != p) 1109 pm++; 1110 1111 if (pm->node == MAX_NUMNODES) 1112 return NULL; 1113 1114 *result = &pm->status; 1115 1116 if (PageHuge(p)) 1117 return alloc_huge_page_node(page_hstate(compound_head(p)), 1118 pm->node); 1119 else 1120 return alloc_pages_exact_node(pm->node, 1121 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0); 1122 } 1123 1124 /* 1125 * Move a set of pages as indicated in the pm array. The addr 1126 * field must be set to the virtual address of the page to be moved 1127 * and the node number must contain a valid target node. 1128 * The pm array ends with node = MAX_NUMNODES. 1129 */ 1130 static int do_move_page_to_node_array(struct mm_struct *mm, 1131 struct page_to_node *pm, 1132 int migrate_all) 1133 { 1134 int err; 1135 struct page_to_node *pp; 1136 LIST_HEAD(pagelist); 1137 1138 down_read(&mm->mmap_sem); 1139 1140 /* 1141 * Build a list of pages to migrate 1142 */ 1143 for (pp = pm; pp->node != MAX_NUMNODES; pp++) { 1144 struct vm_area_struct *vma; 1145 struct page *page; 1146 1147 err = -EFAULT; 1148 vma = find_vma(mm, pp->addr); 1149 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma)) 1150 goto set_status; 1151 1152 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT); 1153 1154 err = PTR_ERR(page); 1155 if (IS_ERR(page)) 1156 goto set_status; 1157 1158 err = -ENOENT; 1159 if (!page) 1160 goto set_status; 1161 1162 /* Use PageReserved to check for zero page */ 1163 if (PageReserved(page)) 1164 goto put_and_set; 1165 1166 pp->page = page; 1167 err = page_to_nid(page); 1168 1169 if (err == pp->node) 1170 /* 1171 * Node already in the right place 1172 */ 1173 goto put_and_set; 1174 1175 err = -EACCES; 1176 if (page_mapcount(page) > 1 && 1177 !migrate_all) 1178 goto put_and_set; 1179 1180 if (PageHuge(page)) { 1181 isolate_huge_page(page, &pagelist); 1182 goto put_and_set; 1183 } 1184 1185 err = isolate_lru_page(page); 1186 if (!err) { 1187 list_add_tail(&page->lru, &pagelist); 1188 inc_zone_page_state(page, NR_ISOLATED_ANON + 1189 page_is_file_cache(page)); 1190 } 1191 put_and_set: 1192 /* 1193 * Either remove the duplicate refcount from 1194 * isolate_lru_page() or drop the page ref if it was 1195 * not isolated. 1196 */ 1197 put_page(page); 1198 set_status: 1199 pp->status = err; 1200 } 1201 1202 err = 0; 1203 if (!list_empty(&pagelist)) { 1204 err = migrate_pages(&pagelist, new_page_node, 1205 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL); 1206 if (err) 1207 putback_movable_pages(&pagelist); 1208 } 1209 1210 up_read(&mm->mmap_sem); 1211 return err; 1212 } 1213 1214 /* 1215 * Migrate an array of page address onto an array of nodes and fill 1216 * the corresponding array of status. 1217 */ 1218 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, 1219 unsigned long nr_pages, 1220 const void __user * __user *pages, 1221 const int __user *nodes, 1222 int __user *status, int flags) 1223 { 1224 struct page_to_node *pm; 1225 unsigned long chunk_nr_pages; 1226 unsigned long chunk_start; 1227 int err; 1228 1229 err = -ENOMEM; 1230 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); 1231 if (!pm) 1232 goto out; 1233 1234 migrate_prep(); 1235 1236 /* 1237 * Store a chunk of page_to_node array in a page, 1238 * but keep the last one as a marker 1239 */ 1240 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; 1241 1242 for (chunk_start = 0; 1243 chunk_start < nr_pages; 1244 chunk_start += chunk_nr_pages) { 1245 int j; 1246 1247 if (chunk_start + chunk_nr_pages > nr_pages) 1248 chunk_nr_pages = nr_pages - chunk_start; 1249 1250 /* fill the chunk pm with addrs and nodes from user-space */ 1251 for (j = 0; j < chunk_nr_pages; j++) { 1252 const void __user *p; 1253 int node; 1254 1255 err = -EFAULT; 1256 if (get_user(p, pages + j + chunk_start)) 1257 goto out_pm; 1258 pm[j].addr = (unsigned long) p; 1259 1260 if (get_user(node, nodes + j + chunk_start)) 1261 goto out_pm; 1262 1263 err = -ENODEV; 1264 if (node < 0 || node >= MAX_NUMNODES) 1265 goto out_pm; 1266 1267 if (!node_state(node, N_MEMORY)) 1268 goto out_pm; 1269 1270 err = -EACCES; 1271 if (!node_isset(node, task_nodes)) 1272 goto out_pm; 1273 1274 pm[j].node = node; 1275 } 1276 1277 /* End marker for this chunk */ 1278 pm[chunk_nr_pages].node = MAX_NUMNODES; 1279 1280 /* Migrate this chunk */ 1281 err = do_move_page_to_node_array(mm, pm, 1282 flags & MPOL_MF_MOVE_ALL); 1283 if (err < 0) 1284 goto out_pm; 1285 1286 /* Return status information */ 1287 for (j = 0; j < chunk_nr_pages; j++) 1288 if (put_user(pm[j].status, status + j + chunk_start)) { 1289 err = -EFAULT; 1290 goto out_pm; 1291 } 1292 } 1293 err = 0; 1294 1295 out_pm: 1296 free_page((unsigned long)pm); 1297 out: 1298 return err; 1299 } 1300 1301 /* 1302 * Determine the nodes of an array of pages and store it in an array of status. 1303 */ 1304 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, 1305 const void __user **pages, int *status) 1306 { 1307 unsigned long i; 1308 1309 down_read(&mm->mmap_sem); 1310 1311 for (i = 0; i < nr_pages; i++) { 1312 unsigned long addr = (unsigned long)(*pages); 1313 struct vm_area_struct *vma; 1314 struct page *page; 1315 int err = -EFAULT; 1316 1317 vma = find_vma(mm, addr); 1318 if (!vma || addr < vma->vm_start) 1319 goto set_status; 1320 1321 page = follow_page(vma, addr, 0); 1322 1323 err = PTR_ERR(page); 1324 if (IS_ERR(page)) 1325 goto set_status; 1326 1327 err = -ENOENT; 1328 /* Use PageReserved to check for zero page */ 1329 if (!page || PageReserved(page)) 1330 goto set_status; 1331 1332 err = page_to_nid(page); 1333 set_status: 1334 *status = err; 1335 1336 pages++; 1337 status++; 1338 } 1339 1340 up_read(&mm->mmap_sem); 1341 } 1342 1343 /* 1344 * Determine the nodes of a user array of pages and store it in 1345 * a user array of status. 1346 */ 1347 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, 1348 const void __user * __user *pages, 1349 int __user *status) 1350 { 1351 #define DO_PAGES_STAT_CHUNK_NR 16 1352 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; 1353 int chunk_status[DO_PAGES_STAT_CHUNK_NR]; 1354 1355 while (nr_pages) { 1356 unsigned long chunk_nr; 1357 1358 chunk_nr = nr_pages; 1359 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) 1360 chunk_nr = DO_PAGES_STAT_CHUNK_NR; 1361 1362 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) 1363 break; 1364 1365 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); 1366 1367 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) 1368 break; 1369 1370 pages += chunk_nr; 1371 status += chunk_nr; 1372 nr_pages -= chunk_nr; 1373 } 1374 return nr_pages ? -EFAULT : 0; 1375 } 1376 1377 /* 1378 * Move a list of pages in the address space of the currently executing 1379 * process. 1380 */ 1381 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, 1382 const void __user * __user *, pages, 1383 const int __user *, nodes, 1384 int __user *, status, int, flags) 1385 { 1386 const struct cred *cred = current_cred(), *tcred; 1387 struct task_struct *task; 1388 struct mm_struct *mm; 1389 int err; 1390 nodemask_t task_nodes; 1391 1392 /* Check flags */ 1393 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) 1394 return -EINVAL; 1395 1396 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) 1397 return -EPERM; 1398 1399 /* Find the mm_struct */ 1400 rcu_read_lock(); 1401 task = pid ? find_task_by_vpid(pid) : current; 1402 if (!task) { 1403 rcu_read_unlock(); 1404 return -ESRCH; 1405 } 1406 get_task_struct(task); 1407 1408 /* 1409 * Check if this process has the right to modify the specified 1410 * process. The right exists if the process has administrative 1411 * capabilities, superuser privileges or the same 1412 * userid as the target process. 1413 */ 1414 tcred = __task_cred(task); 1415 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) && 1416 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) && 1417 !capable(CAP_SYS_NICE)) { 1418 rcu_read_unlock(); 1419 err = -EPERM; 1420 goto out; 1421 } 1422 rcu_read_unlock(); 1423 1424 err = security_task_movememory(task); 1425 if (err) 1426 goto out; 1427 1428 task_nodes = cpuset_mems_allowed(task); 1429 mm = get_task_mm(task); 1430 put_task_struct(task); 1431 1432 if (!mm) 1433 return -EINVAL; 1434 1435 if (nodes) 1436 err = do_pages_move(mm, task_nodes, nr_pages, pages, 1437 nodes, status, flags); 1438 else 1439 err = do_pages_stat(mm, nr_pages, pages, status); 1440 1441 mmput(mm); 1442 return err; 1443 1444 out: 1445 put_task_struct(task); 1446 return err; 1447 } 1448 1449 /* 1450 * Call migration functions in the vma_ops that may prepare 1451 * memory in a vm for migration. migration functions may perform 1452 * the migration for vmas that do not have an underlying page struct. 1453 */ 1454 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to, 1455 const nodemask_t *from, unsigned long flags) 1456 { 1457 struct vm_area_struct *vma; 1458 int err = 0; 1459 1460 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) { 1461 if (vma->vm_ops && vma->vm_ops->migrate) { 1462 err = vma->vm_ops->migrate(vma, to, from, flags); 1463 if (err) 1464 break; 1465 } 1466 } 1467 return err; 1468 } 1469 1470 #ifdef CONFIG_NUMA_BALANCING 1471 /* 1472 * Returns true if this is a safe migration target node for misplaced NUMA 1473 * pages. Currently it only checks the watermarks which crude 1474 */ 1475 static bool migrate_balanced_pgdat(struct pglist_data *pgdat, 1476 unsigned long nr_migrate_pages) 1477 { 1478 int z; 1479 for (z = pgdat->nr_zones - 1; z >= 0; z--) { 1480 struct zone *zone = pgdat->node_zones + z; 1481 1482 if (!populated_zone(zone)) 1483 continue; 1484 1485 if (!zone_reclaimable(zone)) 1486 continue; 1487 1488 /* Avoid waking kswapd by allocating pages_to_migrate pages. */ 1489 if (!zone_watermark_ok(zone, 0, 1490 high_wmark_pages(zone) + 1491 nr_migrate_pages, 1492 0, 0)) 1493 continue; 1494 return true; 1495 } 1496 return false; 1497 } 1498 1499 static struct page *alloc_misplaced_dst_page(struct page *page, 1500 unsigned long data, 1501 int **result) 1502 { 1503 int nid = (int) data; 1504 struct page *newpage; 1505 1506 newpage = alloc_pages_exact_node(nid, 1507 (GFP_HIGHUSER_MOVABLE | GFP_THISNODE | 1508 __GFP_NOMEMALLOC | __GFP_NORETRY | 1509 __GFP_NOWARN) & 1510 ~GFP_IOFS, 0); 1511 if (newpage) 1512 page_cpupid_xchg_last(newpage, page_cpupid_last(page)); 1513 1514 return newpage; 1515 } 1516 1517 /* 1518 * page migration rate limiting control. 1519 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs 1520 * window of time. Default here says do not migrate more than 1280M per second. 1521 * If a node is rate-limited then PTE NUMA updates are also rate-limited. However 1522 * as it is faults that reset the window, pte updates will happen unconditionally 1523 * if there has not been a fault since @pteupdate_interval_millisecs after the 1524 * throttle window closed. 1525 */ 1526 static unsigned int migrate_interval_millisecs __read_mostly = 100; 1527 static unsigned int pteupdate_interval_millisecs __read_mostly = 1000; 1528 static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT); 1529 1530 /* Returns true if NUMA migration is currently rate limited */ 1531 bool migrate_ratelimited(int node) 1532 { 1533 pg_data_t *pgdat = NODE_DATA(node); 1534 1535 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window + 1536 msecs_to_jiffies(pteupdate_interval_millisecs))) 1537 return false; 1538 1539 if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages) 1540 return false; 1541 1542 return true; 1543 } 1544 1545 /* Returns true if the node is migrate rate-limited after the update */ 1546 bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages) 1547 { 1548 bool rate_limited = false; 1549 1550 /* 1551 * Rate-limit the amount of data that is being migrated to a node. 1552 * Optimal placement is no good if the memory bus is saturated and 1553 * all the time is being spent migrating! 1554 */ 1555 spin_lock(&pgdat->numabalancing_migrate_lock); 1556 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) { 1557 pgdat->numabalancing_migrate_nr_pages = 0; 1558 pgdat->numabalancing_migrate_next_window = jiffies + 1559 msecs_to_jiffies(migrate_interval_millisecs); 1560 } 1561 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) 1562 rate_limited = true; 1563 else 1564 pgdat->numabalancing_migrate_nr_pages += nr_pages; 1565 spin_unlock(&pgdat->numabalancing_migrate_lock); 1566 1567 return rate_limited; 1568 } 1569 1570 int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) 1571 { 1572 int page_lru; 1573 1574 VM_BUG_ON(compound_order(page) && !PageTransHuge(page)); 1575 1576 /* Avoid migrating to a node that is nearly full */ 1577 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page))) 1578 return 0; 1579 1580 if (isolate_lru_page(page)) 1581 return 0; 1582 1583 /* 1584 * migrate_misplaced_transhuge_page() skips page migration's usual 1585 * check on page_count(), so we must do it here, now that the page 1586 * has been isolated: a GUP pin, or any other pin, prevents migration. 1587 * The expected page count is 3: 1 for page's mapcount and 1 for the 1588 * caller's pin and 1 for the reference taken by isolate_lru_page(). 1589 */ 1590 if (PageTransHuge(page) && page_count(page) != 3) { 1591 putback_lru_page(page); 1592 return 0; 1593 } 1594 1595 page_lru = page_is_file_cache(page); 1596 mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru, 1597 hpage_nr_pages(page)); 1598 1599 /* 1600 * Isolating the page has taken another reference, so the 1601 * caller's reference can be safely dropped without the page 1602 * disappearing underneath us during migration. 1603 */ 1604 put_page(page); 1605 return 1; 1606 } 1607 1608 /* 1609 * Attempt to migrate a misplaced page to the specified destination 1610 * node. Caller is expected to have an elevated reference count on 1611 * the page that will be dropped by this function before returning. 1612 */ 1613 int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, 1614 int node) 1615 { 1616 pg_data_t *pgdat = NODE_DATA(node); 1617 int isolated; 1618 int nr_remaining; 1619 LIST_HEAD(migratepages); 1620 1621 /* 1622 * Don't migrate file pages that are mapped in multiple processes 1623 * with execute permissions as they are probably shared libraries. 1624 */ 1625 if (page_mapcount(page) != 1 && page_is_file_cache(page) && 1626 (vma->vm_flags & VM_EXEC)) 1627 goto out; 1628 1629 /* 1630 * Rate-limit the amount of data that is being migrated to a node. 1631 * Optimal placement is no good if the memory bus is saturated and 1632 * all the time is being spent migrating! 1633 */ 1634 if (numamigrate_update_ratelimit(pgdat, 1)) 1635 goto out; 1636 1637 isolated = numamigrate_isolate_page(pgdat, page); 1638 if (!isolated) 1639 goto out; 1640 1641 list_add(&page->lru, &migratepages); 1642 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, 1643 node, MIGRATE_ASYNC, MR_NUMA_MISPLACED); 1644 if (nr_remaining) { 1645 putback_lru_pages(&migratepages); 1646 isolated = 0; 1647 } else 1648 count_vm_numa_event(NUMA_PAGE_MIGRATE); 1649 BUG_ON(!list_empty(&migratepages)); 1650 return isolated; 1651 1652 out: 1653 put_page(page); 1654 return 0; 1655 } 1656 #endif /* CONFIG_NUMA_BALANCING */ 1657 1658 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 1659 /* 1660 * Migrates a THP to a given target node. page must be locked and is unlocked 1661 * before returning. 1662 */ 1663 int migrate_misplaced_transhuge_page(struct mm_struct *mm, 1664 struct vm_area_struct *vma, 1665 pmd_t *pmd, pmd_t entry, 1666 unsigned long address, 1667 struct page *page, int node) 1668 { 1669 unsigned long haddr = address & HPAGE_PMD_MASK; 1670 pg_data_t *pgdat = NODE_DATA(node); 1671 int isolated = 0; 1672 struct page *new_page = NULL; 1673 struct mem_cgroup *memcg = NULL; 1674 int page_lru = page_is_file_cache(page); 1675 1676 /* 1677 * Rate-limit the amount of data that is being migrated to a node. 1678 * Optimal placement is no good if the memory bus is saturated and 1679 * all the time is being spent migrating! 1680 */ 1681 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR)) 1682 goto out_dropref; 1683 1684 new_page = alloc_pages_node(node, 1685 (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER); 1686 if (!new_page) 1687 goto out_fail; 1688 1689 page_cpupid_xchg_last(new_page, page_cpupid_last(page)); 1690 1691 isolated = numamigrate_isolate_page(pgdat, page); 1692 if (!isolated) { 1693 put_page(new_page); 1694 goto out_fail; 1695 } 1696 1697 /* Prepare a page as a migration target */ 1698 __set_page_locked(new_page); 1699 SetPageSwapBacked(new_page); 1700 1701 /* anon mapping, we can simply copy page->mapping to the new page: */ 1702 new_page->mapping = page->mapping; 1703 new_page->index = page->index; 1704 migrate_page_copy(new_page, page); 1705 WARN_ON(PageLRU(new_page)); 1706 1707 /* Recheck the target PMD */ 1708 spin_lock(&mm->page_table_lock); 1709 if (unlikely(!pmd_same(*pmd, entry))) { 1710 spin_unlock(&mm->page_table_lock); 1711 1712 /* Reverse changes made by migrate_page_copy() */ 1713 if (TestClearPageActive(new_page)) 1714 SetPageActive(page); 1715 if (TestClearPageUnevictable(new_page)) 1716 SetPageUnevictable(page); 1717 mlock_migrate_page(page, new_page); 1718 1719 unlock_page(new_page); 1720 put_page(new_page); /* Free it */ 1721 1722 /* Retake the callers reference and putback on LRU */ 1723 get_page(page); 1724 putback_lru_page(page); 1725 mod_zone_page_state(page_zone(page), 1726 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); 1727 goto out_fail; 1728 } 1729 1730 /* 1731 * Traditional migration needs to prepare the memcg charge 1732 * transaction early to prevent the old page from being 1733 * uncharged when installing migration entries. Here we can 1734 * save the potential rollback and start the charge transfer 1735 * only when migration is already known to end successfully. 1736 */ 1737 mem_cgroup_prepare_migration(page, new_page, &memcg); 1738 1739 entry = mk_pmd(new_page, vma->vm_page_prot); 1740 entry = pmd_mknonnuma(entry); 1741 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 1742 entry = pmd_mkhuge(entry); 1743 1744 pmdp_clear_flush(vma, haddr, pmd); 1745 set_pmd_at(mm, haddr, pmd, entry); 1746 page_add_new_anon_rmap(new_page, vma, haddr); 1747 update_mmu_cache_pmd(vma, address, &entry); 1748 page_remove_rmap(page); 1749 /* 1750 * Finish the charge transaction under the page table lock to 1751 * prevent split_huge_page() from dividing up the charge 1752 * before it's fully transferred to the new page. 1753 */ 1754 mem_cgroup_end_migration(memcg, page, new_page, true); 1755 spin_unlock(&mm->page_table_lock); 1756 1757 unlock_page(new_page); 1758 unlock_page(page); 1759 put_page(page); /* Drop the rmap reference */ 1760 put_page(page); /* Drop the LRU isolation reference */ 1761 1762 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); 1763 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); 1764 1765 mod_zone_page_state(page_zone(page), 1766 NR_ISOLATED_ANON + page_lru, 1767 -HPAGE_PMD_NR); 1768 return isolated; 1769 1770 out_fail: 1771 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); 1772 out_dropref: 1773 entry = pmd_mknonnuma(entry); 1774 set_pmd_at(mm, haddr, pmd, entry); 1775 update_mmu_cache_pmd(vma, address, &entry); 1776 1777 unlock_page(page); 1778 put_page(page); 1779 return 0; 1780 } 1781 #endif /* CONFIG_NUMA_BALANCING */ 1782 1783 #endif /* CONFIG_NUMA */ 1784