1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Memory Migration functionality - linux/mm/migrate.c 4 * 5 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter 6 * 7 * Page migration was first developed in the context of the memory hotplug 8 * project. The main authors of the migration code are: 9 * 10 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> 11 * Hirokazu Takahashi <taka@valinux.co.jp> 12 * Dave Hansen <haveblue@us.ibm.com> 13 * Christoph Lameter 14 */ 15 16 #include <linux/migrate.h> 17 #include <linux/export.h> 18 #include <linux/swap.h> 19 #include <linux/swapops.h> 20 #include <linux/pagemap.h> 21 #include <linux/buffer_head.h> 22 #include <linux/mm_inline.h> 23 #include <linux/nsproxy.h> 24 #include <linux/pagevec.h> 25 #include <linux/ksm.h> 26 #include <linux/rmap.h> 27 #include <linux/topology.h> 28 #include <linux/cpu.h> 29 #include <linux/cpuset.h> 30 #include <linux/writeback.h> 31 #include <linux/mempolicy.h> 32 #include <linux/vmalloc.h> 33 #include <linux/security.h> 34 #include <linux/backing-dev.h> 35 #include <linux/compaction.h> 36 #include <linux/syscalls.h> 37 #include <linux/compat.h> 38 #include <linux/hugetlb.h> 39 #include <linux/hugetlb_cgroup.h> 40 #include <linux/gfp.h> 41 #include <linux/pagewalk.h> 42 #include <linux/pfn_t.h> 43 #include <linux/memremap.h> 44 #include <linux/userfaultfd_k.h> 45 #include <linux/balloon_compaction.h> 46 #include <linux/mmu_notifier.h> 47 #include <linux/page_idle.h> 48 #include <linux/page_owner.h> 49 #include <linux/sched/mm.h> 50 #include <linux/ptrace.h> 51 #include <linux/oom.h> 52 53 #include <asm/tlbflush.h> 54 55 #define CREATE_TRACE_POINTS 56 #include <trace/events/migrate.h> 57 58 #include "internal.h" 59 60 /* 61 * migrate_prep() needs to be called before we start compiling a list of pages 62 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is 63 * undesirable, use migrate_prep_local() 64 */ 65 int migrate_prep(void) 66 { 67 /* 68 * Clear the LRU lists so pages can be isolated. 69 * Note that pages may be moved off the LRU after we have 70 * drained them. Those pages will fail to migrate like other 71 * pages that may be busy. 72 */ 73 lru_add_drain_all(); 74 75 return 0; 76 } 77 78 /* Do the necessary work of migrate_prep but not if it involves other CPUs */ 79 int migrate_prep_local(void) 80 { 81 lru_add_drain(); 82 83 return 0; 84 } 85 86 int isolate_movable_page(struct page *page, isolate_mode_t mode) 87 { 88 struct address_space *mapping; 89 90 /* 91 * Avoid burning cycles with pages that are yet under __free_pages(), 92 * or just got freed under us. 93 * 94 * In case we 'win' a race for a movable page being freed under us and 95 * raise its refcount preventing __free_pages() from doing its job 96 * the put_page() at the end of this block will take care of 97 * release this page, thus avoiding a nasty leakage. 98 */ 99 if (unlikely(!get_page_unless_zero(page))) 100 goto out; 101 102 /* 103 * Check PageMovable before holding a PG_lock because page's owner 104 * assumes anybody doesn't touch PG_lock of newly allocated page 105 * so unconditionally grabbing the lock ruins page's owner side. 106 */ 107 if (unlikely(!__PageMovable(page))) 108 goto out_putpage; 109 /* 110 * As movable pages are not isolated from LRU lists, concurrent 111 * compaction threads can race against page migration functions 112 * as well as race against the releasing a page. 113 * 114 * In order to avoid having an already isolated movable page 115 * being (wrongly) re-isolated while it is under migration, 116 * or to avoid attempting to isolate pages being released, 117 * lets be sure we have the page lock 118 * before proceeding with the movable page isolation steps. 119 */ 120 if (unlikely(!trylock_page(page))) 121 goto out_putpage; 122 123 if (!PageMovable(page) || PageIsolated(page)) 124 goto out_no_isolated; 125 126 mapping = page_mapping(page); 127 VM_BUG_ON_PAGE(!mapping, page); 128 129 if (!mapping->a_ops->isolate_page(page, mode)) 130 goto out_no_isolated; 131 132 /* Driver shouldn't use PG_isolated bit of page->flags */ 133 WARN_ON_ONCE(PageIsolated(page)); 134 __SetPageIsolated(page); 135 unlock_page(page); 136 137 return 0; 138 139 out_no_isolated: 140 unlock_page(page); 141 out_putpage: 142 put_page(page); 143 out: 144 return -EBUSY; 145 } 146 147 /* It should be called on page which is PG_movable */ 148 void putback_movable_page(struct page *page) 149 { 150 struct address_space *mapping; 151 152 VM_BUG_ON_PAGE(!PageLocked(page), page); 153 VM_BUG_ON_PAGE(!PageMovable(page), page); 154 VM_BUG_ON_PAGE(!PageIsolated(page), page); 155 156 mapping = page_mapping(page); 157 mapping->a_ops->putback_page(page); 158 __ClearPageIsolated(page); 159 } 160 161 /* 162 * Put previously isolated pages back onto the appropriate lists 163 * from where they were once taken off for compaction/migration. 164 * 165 * This function shall be used whenever the isolated pageset has been 166 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() 167 * and isolate_huge_page(). 168 */ 169 void putback_movable_pages(struct list_head *l) 170 { 171 struct page *page; 172 struct page *page2; 173 174 list_for_each_entry_safe(page, page2, l, lru) { 175 if (unlikely(PageHuge(page))) { 176 putback_active_hugepage(page); 177 continue; 178 } 179 list_del(&page->lru); 180 /* 181 * We isolated non-lru movable page so here we can use 182 * __PageMovable because LRU page's mapping cannot have 183 * PAGE_MAPPING_MOVABLE. 184 */ 185 if (unlikely(__PageMovable(page))) { 186 VM_BUG_ON_PAGE(!PageIsolated(page), page); 187 lock_page(page); 188 if (PageMovable(page)) 189 putback_movable_page(page); 190 else 191 __ClearPageIsolated(page); 192 unlock_page(page); 193 put_page(page); 194 } else { 195 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + 196 page_is_file_lru(page), -thp_nr_pages(page)); 197 putback_lru_page(page); 198 } 199 } 200 } 201 202 /* 203 * Restore a potential migration pte to a working pte entry 204 */ 205 static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma, 206 unsigned long addr, void *old) 207 { 208 struct page_vma_mapped_walk pvmw = { 209 .page = old, 210 .vma = vma, 211 .address = addr, 212 .flags = PVMW_SYNC | PVMW_MIGRATION, 213 }; 214 struct page *new; 215 pte_t pte; 216 swp_entry_t entry; 217 218 VM_BUG_ON_PAGE(PageTail(page), page); 219 while (page_vma_mapped_walk(&pvmw)) { 220 if (PageKsm(page)) 221 new = page; 222 else 223 new = page - pvmw.page->index + 224 linear_page_index(vma, pvmw.address); 225 226 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 227 /* PMD-mapped THP migration entry */ 228 if (!pvmw.pte) { 229 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page); 230 remove_migration_pmd(&pvmw, new); 231 continue; 232 } 233 #endif 234 235 get_page(new); 236 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); 237 if (pte_swp_soft_dirty(*pvmw.pte)) 238 pte = pte_mksoft_dirty(pte); 239 240 /* 241 * Recheck VMA as permissions can change since migration started 242 */ 243 entry = pte_to_swp_entry(*pvmw.pte); 244 if (is_write_migration_entry(entry)) 245 pte = maybe_mkwrite(pte, vma); 246 else if (pte_swp_uffd_wp(*pvmw.pte)) 247 pte = pte_mkuffd_wp(pte); 248 249 if (unlikely(is_device_private_page(new))) { 250 entry = make_device_private_entry(new, pte_write(pte)); 251 pte = swp_entry_to_pte(entry); 252 if (pte_swp_uffd_wp(*pvmw.pte)) 253 pte = pte_swp_mkuffd_wp(pte); 254 } 255 256 #ifdef CONFIG_HUGETLB_PAGE 257 if (PageHuge(new)) { 258 pte = pte_mkhuge(pte); 259 pte = arch_make_huge_pte(pte, vma, new, 0); 260 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); 261 if (PageAnon(new)) 262 hugepage_add_anon_rmap(new, vma, pvmw.address); 263 else 264 page_dup_rmap(new, true); 265 } else 266 #endif 267 { 268 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); 269 270 if (PageAnon(new)) 271 page_add_anon_rmap(new, vma, pvmw.address, false); 272 else 273 page_add_file_rmap(new, false); 274 } 275 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) 276 mlock_vma_page(new); 277 278 if (PageTransHuge(page) && PageMlocked(page)) 279 clear_page_mlock(page); 280 281 /* No need to invalidate - it was non-present before */ 282 update_mmu_cache(vma, pvmw.address, pvmw.pte); 283 } 284 285 return true; 286 } 287 288 /* 289 * Get rid of all migration entries and replace them by 290 * references to the indicated page. 291 */ 292 void remove_migration_ptes(struct page *old, struct page *new, bool locked) 293 { 294 struct rmap_walk_control rwc = { 295 .rmap_one = remove_migration_pte, 296 .arg = old, 297 }; 298 299 if (locked) 300 rmap_walk_locked(new, &rwc); 301 else 302 rmap_walk(new, &rwc); 303 } 304 305 /* 306 * Something used the pte of a page under migration. We need to 307 * get to the page and wait until migration is finished. 308 * When we return from this function the fault will be retried. 309 */ 310 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, 311 spinlock_t *ptl) 312 { 313 pte_t pte; 314 swp_entry_t entry; 315 struct page *page; 316 317 spin_lock(ptl); 318 pte = *ptep; 319 if (!is_swap_pte(pte)) 320 goto out; 321 322 entry = pte_to_swp_entry(pte); 323 if (!is_migration_entry(entry)) 324 goto out; 325 326 page = migration_entry_to_page(entry); 327 328 /* 329 * Once page cache replacement of page migration started, page_count 330 * is zero; but we must not call put_and_wait_on_page_locked() without 331 * a ref. Use get_page_unless_zero(), and just fault again if it fails. 332 */ 333 if (!get_page_unless_zero(page)) 334 goto out; 335 pte_unmap_unlock(ptep, ptl); 336 put_and_wait_on_page_locked(page); 337 return; 338 out: 339 pte_unmap_unlock(ptep, ptl); 340 } 341 342 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, 343 unsigned long address) 344 { 345 spinlock_t *ptl = pte_lockptr(mm, pmd); 346 pte_t *ptep = pte_offset_map(pmd, address); 347 __migration_entry_wait(mm, ptep, ptl); 348 } 349 350 void migration_entry_wait_huge(struct vm_area_struct *vma, 351 struct mm_struct *mm, pte_t *pte) 352 { 353 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); 354 __migration_entry_wait(mm, pte, ptl); 355 } 356 357 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 358 void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) 359 { 360 spinlock_t *ptl; 361 struct page *page; 362 363 ptl = pmd_lock(mm, pmd); 364 if (!is_pmd_migration_entry(*pmd)) 365 goto unlock; 366 page = migration_entry_to_page(pmd_to_swp_entry(*pmd)); 367 if (!get_page_unless_zero(page)) 368 goto unlock; 369 spin_unlock(ptl); 370 put_and_wait_on_page_locked(page); 371 return; 372 unlock: 373 spin_unlock(ptl); 374 } 375 #endif 376 377 static int expected_page_refs(struct address_space *mapping, struct page *page) 378 { 379 int expected_count = 1; 380 381 /* 382 * Device public or private pages have an extra refcount as they are 383 * ZONE_DEVICE pages. 384 */ 385 expected_count += is_device_private_page(page); 386 if (mapping) 387 expected_count += thp_nr_pages(page) + page_has_private(page); 388 389 return expected_count; 390 } 391 392 /* 393 * Replace the page in the mapping. 394 * 395 * The number of remaining references must be: 396 * 1 for anonymous pages without a mapping 397 * 2 for pages with a mapping 398 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. 399 */ 400 int migrate_page_move_mapping(struct address_space *mapping, 401 struct page *newpage, struct page *page, int extra_count) 402 { 403 XA_STATE(xas, &mapping->i_pages, page_index(page)); 404 struct zone *oldzone, *newzone; 405 int dirty; 406 int expected_count = expected_page_refs(mapping, page) + extra_count; 407 408 if (!mapping) { 409 /* Anonymous page without mapping */ 410 if (page_count(page) != expected_count) 411 return -EAGAIN; 412 413 /* No turning back from here */ 414 newpage->index = page->index; 415 newpage->mapping = page->mapping; 416 if (PageSwapBacked(page)) 417 __SetPageSwapBacked(newpage); 418 419 return MIGRATEPAGE_SUCCESS; 420 } 421 422 oldzone = page_zone(page); 423 newzone = page_zone(newpage); 424 425 xas_lock_irq(&xas); 426 if (page_count(page) != expected_count || xas_load(&xas) != page) { 427 xas_unlock_irq(&xas); 428 return -EAGAIN; 429 } 430 431 if (!page_ref_freeze(page, expected_count)) { 432 xas_unlock_irq(&xas); 433 return -EAGAIN; 434 } 435 436 /* 437 * Now we know that no one else is looking at the page: 438 * no turning back from here. 439 */ 440 newpage->index = page->index; 441 newpage->mapping = page->mapping; 442 page_ref_add(newpage, thp_nr_pages(page)); /* add cache reference */ 443 if (PageSwapBacked(page)) { 444 __SetPageSwapBacked(newpage); 445 if (PageSwapCache(page)) { 446 SetPageSwapCache(newpage); 447 set_page_private(newpage, page_private(page)); 448 } 449 } else { 450 VM_BUG_ON_PAGE(PageSwapCache(page), page); 451 } 452 453 /* Move dirty while page refs frozen and newpage not yet exposed */ 454 dirty = PageDirty(page); 455 if (dirty) { 456 ClearPageDirty(page); 457 SetPageDirty(newpage); 458 } 459 460 xas_store(&xas, newpage); 461 if (PageTransHuge(page)) { 462 int i; 463 464 for (i = 1; i < HPAGE_PMD_NR; i++) { 465 xas_next(&xas); 466 xas_store(&xas, newpage); 467 } 468 } 469 470 /* 471 * Drop cache reference from old page by unfreezing 472 * to one less reference. 473 * We know this isn't the last reference. 474 */ 475 page_ref_unfreeze(page, expected_count - thp_nr_pages(page)); 476 477 xas_unlock(&xas); 478 /* Leave irq disabled to prevent preemption while updating stats */ 479 480 /* 481 * If moved to a different zone then also account 482 * the page for that zone. Other VM counters will be 483 * taken care of when we establish references to the 484 * new page and drop references to the old page. 485 * 486 * Note that anonymous pages are accounted for 487 * via NR_FILE_PAGES and NR_ANON_MAPPED if they 488 * are mapped to swap space. 489 */ 490 if (newzone != oldzone) { 491 struct lruvec *old_lruvec, *new_lruvec; 492 struct mem_cgroup *memcg; 493 494 memcg = page_memcg(page); 495 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat); 496 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat); 497 498 __dec_lruvec_state(old_lruvec, NR_FILE_PAGES); 499 __inc_lruvec_state(new_lruvec, NR_FILE_PAGES); 500 if (PageSwapBacked(page) && !PageSwapCache(page)) { 501 __dec_lruvec_state(old_lruvec, NR_SHMEM); 502 __inc_lruvec_state(new_lruvec, NR_SHMEM); 503 } 504 if (dirty && mapping_cap_account_dirty(mapping)) { 505 __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY); 506 __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING); 507 __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY); 508 __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING); 509 } 510 } 511 local_irq_enable(); 512 513 return MIGRATEPAGE_SUCCESS; 514 } 515 EXPORT_SYMBOL(migrate_page_move_mapping); 516 517 /* 518 * The expected number of remaining references is the same as that 519 * of migrate_page_move_mapping(). 520 */ 521 int migrate_huge_page_move_mapping(struct address_space *mapping, 522 struct page *newpage, struct page *page) 523 { 524 XA_STATE(xas, &mapping->i_pages, page_index(page)); 525 int expected_count; 526 527 xas_lock_irq(&xas); 528 expected_count = 2 + page_has_private(page); 529 if (page_count(page) != expected_count || xas_load(&xas) != page) { 530 xas_unlock_irq(&xas); 531 return -EAGAIN; 532 } 533 534 if (!page_ref_freeze(page, expected_count)) { 535 xas_unlock_irq(&xas); 536 return -EAGAIN; 537 } 538 539 newpage->index = page->index; 540 newpage->mapping = page->mapping; 541 542 get_page(newpage); 543 544 xas_store(&xas, newpage); 545 546 page_ref_unfreeze(page, expected_count - 1); 547 548 xas_unlock_irq(&xas); 549 550 return MIGRATEPAGE_SUCCESS; 551 } 552 553 /* 554 * Gigantic pages are so large that we do not guarantee that page++ pointer 555 * arithmetic will work across the entire page. We need something more 556 * specialized. 557 */ 558 static void __copy_gigantic_page(struct page *dst, struct page *src, 559 int nr_pages) 560 { 561 int i; 562 struct page *dst_base = dst; 563 struct page *src_base = src; 564 565 for (i = 0; i < nr_pages; ) { 566 cond_resched(); 567 copy_highpage(dst, src); 568 569 i++; 570 dst = mem_map_next(dst, dst_base, i); 571 src = mem_map_next(src, src_base, i); 572 } 573 } 574 575 static void copy_huge_page(struct page *dst, struct page *src) 576 { 577 int i; 578 int nr_pages; 579 580 if (PageHuge(src)) { 581 /* hugetlbfs page */ 582 struct hstate *h = page_hstate(src); 583 nr_pages = pages_per_huge_page(h); 584 585 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { 586 __copy_gigantic_page(dst, src, nr_pages); 587 return; 588 } 589 } else { 590 /* thp page */ 591 BUG_ON(!PageTransHuge(src)); 592 nr_pages = thp_nr_pages(src); 593 } 594 595 for (i = 0; i < nr_pages; i++) { 596 cond_resched(); 597 copy_highpage(dst + i, src + i); 598 } 599 } 600 601 /* 602 * Copy the page to its new location 603 */ 604 void migrate_page_states(struct page *newpage, struct page *page) 605 { 606 int cpupid; 607 608 if (PageError(page)) 609 SetPageError(newpage); 610 if (PageReferenced(page)) 611 SetPageReferenced(newpage); 612 if (PageUptodate(page)) 613 SetPageUptodate(newpage); 614 if (TestClearPageActive(page)) { 615 VM_BUG_ON_PAGE(PageUnevictable(page), page); 616 SetPageActive(newpage); 617 } else if (TestClearPageUnevictable(page)) 618 SetPageUnevictable(newpage); 619 if (PageWorkingset(page)) 620 SetPageWorkingset(newpage); 621 if (PageChecked(page)) 622 SetPageChecked(newpage); 623 if (PageMappedToDisk(page)) 624 SetPageMappedToDisk(newpage); 625 626 /* Move dirty on pages not done by migrate_page_move_mapping() */ 627 if (PageDirty(page)) 628 SetPageDirty(newpage); 629 630 if (page_is_young(page)) 631 set_page_young(newpage); 632 if (page_is_idle(page)) 633 set_page_idle(newpage); 634 635 /* 636 * Copy NUMA information to the new page, to prevent over-eager 637 * future migrations of this same page. 638 */ 639 cpupid = page_cpupid_xchg_last(page, -1); 640 page_cpupid_xchg_last(newpage, cpupid); 641 642 ksm_migrate_page(newpage, page); 643 /* 644 * Please do not reorder this without considering how mm/ksm.c's 645 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). 646 */ 647 if (PageSwapCache(page)) 648 ClearPageSwapCache(page); 649 ClearPagePrivate(page); 650 set_page_private(page, 0); 651 652 /* 653 * If any waiters have accumulated on the new page then 654 * wake them up. 655 */ 656 if (PageWriteback(newpage)) 657 end_page_writeback(newpage); 658 659 /* 660 * PG_readahead shares the same bit with PG_reclaim. The above 661 * end_page_writeback() may clear PG_readahead mistakenly, so set the 662 * bit after that. 663 */ 664 if (PageReadahead(page)) 665 SetPageReadahead(newpage); 666 667 copy_page_owner(page, newpage); 668 669 mem_cgroup_migrate(page, newpage); 670 } 671 EXPORT_SYMBOL(migrate_page_states); 672 673 void migrate_page_copy(struct page *newpage, struct page *page) 674 { 675 if (PageHuge(page) || PageTransHuge(page)) 676 copy_huge_page(newpage, page); 677 else 678 copy_highpage(newpage, page); 679 680 migrate_page_states(newpage, page); 681 } 682 EXPORT_SYMBOL(migrate_page_copy); 683 684 /************************************************************ 685 * Migration functions 686 ***********************************************************/ 687 688 /* 689 * Common logic to directly migrate a single LRU page suitable for 690 * pages that do not use PagePrivate/PagePrivate2. 691 * 692 * Pages are locked upon entry and exit. 693 */ 694 int migrate_page(struct address_space *mapping, 695 struct page *newpage, struct page *page, 696 enum migrate_mode mode) 697 { 698 int rc; 699 700 BUG_ON(PageWriteback(page)); /* Writeback must be complete */ 701 702 rc = migrate_page_move_mapping(mapping, newpage, page, 0); 703 704 if (rc != MIGRATEPAGE_SUCCESS) 705 return rc; 706 707 if (mode != MIGRATE_SYNC_NO_COPY) 708 migrate_page_copy(newpage, page); 709 else 710 migrate_page_states(newpage, page); 711 return MIGRATEPAGE_SUCCESS; 712 } 713 EXPORT_SYMBOL(migrate_page); 714 715 #ifdef CONFIG_BLOCK 716 /* Returns true if all buffers are successfully locked */ 717 static bool buffer_migrate_lock_buffers(struct buffer_head *head, 718 enum migrate_mode mode) 719 { 720 struct buffer_head *bh = head; 721 722 /* Simple case, sync compaction */ 723 if (mode != MIGRATE_ASYNC) { 724 do { 725 lock_buffer(bh); 726 bh = bh->b_this_page; 727 728 } while (bh != head); 729 730 return true; 731 } 732 733 /* async case, we cannot block on lock_buffer so use trylock_buffer */ 734 do { 735 if (!trylock_buffer(bh)) { 736 /* 737 * We failed to lock the buffer and cannot stall in 738 * async migration. Release the taken locks 739 */ 740 struct buffer_head *failed_bh = bh; 741 bh = head; 742 while (bh != failed_bh) { 743 unlock_buffer(bh); 744 bh = bh->b_this_page; 745 } 746 return false; 747 } 748 749 bh = bh->b_this_page; 750 } while (bh != head); 751 return true; 752 } 753 754 static int __buffer_migrate_page(struct address_space *mapping, 755 struct page *newpage, struct page *page, enum migrate_mode mode, 756 bool check_refs) 757 { 758 struct buffer_head *bh, *head; 759 int rc; 760 int expected_count; 761 762 if (!page_has_buffers(page)) 763 return migrate_page(mapping, newpage, page, mode); 764 765 /* Check whether page does not have extra refs before we do more work */ 766 expected_count = expected_page_refs(mapping, page); 767 if (page_count(page) != expected_count) 768 return -EAGAIN; 769 770 head = page_buffers(page); 771 if (!buffer_migrate_lock_buffers(head, mode)) 772 return -EAGAIN; 773 774 if (check_refs) { 775 bool busy; 776 bool invalidated = false; 777 778 recheck_buffers: 779 busy = false; 780 spin_lock(&mapping->private_lock); 781 bh = head; 782 do { 783 if (atomic_read(&bh->b_count)) { 784 busy = true; 785 break; 786 } 787 bh = bh->b_this_page; 788 } while (bh != head); 789 if (busy) { 790 if (invalidated) { 791 rc = -EAGAIN; 792 goto unlock_buffers; 793 } 794 spin_unlock(&mapping->private_lock); 795 invalidate_bh_lrus(); 796 invalidated = true; 797 goto recheck_buffers; 798 } 799 } 800 801 rc = migrate_page_move_mapping(mapping, newpage, page, 0); 802 if (rc != MIGRATEPAGE_SUCCESS) 803 goto unlock_buffers; 804 805 attach_page_private(newpage, detach_page_private(page)); 806 807 bh = head; 808 do { 809 set_bh_page(bh, newpage, bh_offset(bh)); 810 bh = bh->b_this_page; 811 812 } while (bh != head); 813 814 if (mode != MIGRATE_SYNC_NO_COPY) 815 migrate_page_copy(newpage, page); 816 else 817 migrate_page_states(newpage, page); 818 819 rc = MIGRATEPAGE_SUCCESS; 820 unlock_buffers: 821 if (check_refs) 822 spin_unlock(&mapping->private_lock); 823 bh = head; 824 do { 825 unlock_buffer(bh); 826 bh = bh->b_this_page; 827 828 } while (bh != head); 829 830 return rc; 831 } 832 833 /* 834 * Migration function for pages with buffers. This function can only be used 835 * if the underlying filesystem guarantees that no other references to "page" 836 * exist. For example attached buffer heads are accessed only under page lock. 837 */ 838 int buffer_migrate_page(struct address_space *mapping, 839 struct page *newpage, struct page *page, enum migrate_mode mode) 840 { 841 return __buffer_migrate_page(mapping, newpage, page, mode, false); 842 } 843 EXPORT_SYMBOL(buffer_migrate_page); 844 845 /* 846 * Same as above except that this variant is more careful and checks that there 847 * are also no buffer head references. This function is the right one for 848 * mappings where buffer heads are directly looked up and referenced (such as 849 * block device mappings). 850 */ 851 int buffer_migrate_page_norefs(struct address_space *mapping, 852 struct page *newpage, struct page *page, enum migrate_mode mode) 853 { 854 return __buffer_migrate_page(mapping, newpage, page, mode, true); 855 } 856 #endif 857 858 /* 859 * Writeback a page to clean the dirty state 860 */ 861 static int writeout(struct address_space *mapping, struct page *page) 862 { 863 struct writeback_control wbc = { 864 .sync_mode = WB_SYNC_NONE, 865 .nr_to_write = 1, 866 .range_start = 0, 867 .range_end = LLONG_MAX, 868 .for_reclaim = 1 869 }; 870 int rc; 871 872 if (!mapping->a_ops->writepage) 873 /* No write method for the address space */ 874 return -EINVAL; 875 876 if (!clear_page_dirty_for_io(page)) 877 /* Someone else already triggered a write */ 878 return -EAGAIN; 879 880 /* 881 * A dirty page may imply that the underlying filesystem has 882 * the page on some queue. So the page must be clean for 883 * migration. Writeout may mean we loose the lock and the 884 * page state is no longer what we checked for earlier. 885 * At this point we know that the migration attempt cannot 886 * be successful. 887 */ 888 remove_migration_ptes(page, page, false); 889 890 rc = mapping->a_ops->writepage(page, &wbc); 891 892 if (rc != AOP_WRITEPAGE_ACTIVATE) 893 /* unlocked. Relock */ 894 lock_page(page); 895 896 return (rc < 0) ? -EIO : -EAGAIN; 897 } 898 899 /* 900 * Default handling if a filesystem does not provide a migration function. 901 */ 902 static int fallback_migrate_page(struct address_space *mapping, 903 struct page *newpage, struct page *page, enum migrate_mode mode) 904 { 905 if (PageDirty(page)) { 906 /* Only writeback pages in full synchronous migration */ 907 switch (mode) { 908 case MIGRATE_SYNC: 909 case MIGRATE_SYNC_NO_COPY: 910 break; 911 default: 912 return -EBUSY; 913 } 914 return writeout(mapping, page); 915 } 916 917 /* 918 * Buffers may be managed in a filesystem specific way. 919 * We must have no buffers or drop them. 920 */ 921 if (page_has_private(page) && 922 !try_to_release_page(page, GFP_KERNEL)) 923 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; 924 925 return migrate_page(mapping, newpage, page, mode); 926 } 927 928 /* 929 * Move a page to a newly allocated page 930 * The page is locked and all ptes have been successfully removed. 931 * 932 * The new page will have replaced the old page if this function 933 * is successful. 934 * 935 * Return value: 936 * < 0 - error code 937 * MIGRATEPAGE_SUCCESS - success 938 */ 939 static int move_to_new_page(struct page *newpage, struct page *page, 940 enum migrate_mode mode) 941 { 942 struct address_space *mapping; 943 int rc = -EAGAIN; 944 bool is_lru = !__PageMovable(page); 945 946 VM_BUG_ON_PAGE(!PageLocked(page), page); 947 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); 948 949 mapping = page_mapping(page); 950 951 if (likely(is_lru)) { 952 if (!mapping) 953 rc = migrate_page(mapping, newpage, page, mode); 954 else if (mapping->a_ops->migratepage) 955 /* 956 * Most pages have a mapping and most filesystems 957 * provide a migratepage callback. Anonymous pages 958 * are part of swap space which also has its own 959 * migratepage callback. This is the most common path 960 * for page migration. 961 */ 962 rc = mapping->a_ops->migratepage(mapping, newpage, 963 page, mode); 964 else 965 rc = fallback_migrate_page(mapping, newpage, 966 page, mode); 967 } else { 968 /* 969 * In case of non-lru page, it could be released after 970 * isolation step. In that case, we shouldn't try migration. 971 */ 972 VM_BUG_ON_PAGE(!PageIsolated(page), page); 973 if (!PageMovable(page)) { 974 rc = MIGRATEPAGE_SUCCESS; 975 __ClearPageIsolated(page); 976 goto out; 977 } 978 979 rc = mapping->a_ops->migratepage(mapping, newpage, 980 page, mode); 981 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && 982 !PageIsolated(page)); 983 } 984 985 /* 986 * When successful, old pagecache page->mapping must be cleared before 987 * page is freed; but stats require that PageAnon be left as PageAnon. 988 */ 989 if (rc == MIGRATEPAGE_SUCCESS) { 990 if (__PageMovable(page)) { 991 VM_BUG_ON_PAGE(!PageIsolated(page), page); 992 993 /* 994 * We clear PG_movable under page_lock so any compactor 995 * cannot try to migrate this page. 996 */ 997 __ClearPageIsolated(page); 998 } 999 1000 /* 1001 * Anonymous and movable page->mapping will be cleared by 1002 * free_pages_prepare so don't reset it here for keeping 1003 * the type to work PageAnon, for example. 1004 */ 1005 if (!PageMappingFlags(page)) 1006 page->mapping = NULL; 1007 1008 if (likely(!is_zone_device_page(newpage))) 1009 flush_dcache_page(newpage); 1010 1011 } 1012 out: 1013 return rc; 1014 } 1015 1016 static int __unmap_and_move(struct page *page, struct page *newpage, 1017 int force, enum migrate_mode mode) 1018 { 1019 int rc = -EAGAIN; 1020 int page_was_mapped = 0; 1021 struct anon_vma *anon_vma = NULL; 1022 bool is_lru = !__PageMovable(page); 1023 1024 if (!trylock_page(page)) { 1025 if (!force || mode == MIGRATE_ASYNC) 1026 goto out; 1027 1028 /* 1029 * It's not safe for direct compaction to call lock_page. 1030 * For example, during page readahead pages are added locked 1031 * to the LRU. Later, when the IO completes the pages are 1032 * marked uptodate and unlocked. However, the queueing 1033 * could be merging multiple pages for one bio (e.g. 1034 * mpage_readahead). If an allocation happens for the 1035 * second or third page, the process can end up locking 1036 * the same page twice and deadlocking. Rather than 1037 * trying to be clever about what pages can be locked, 1038 * avoid the use of lock_page for direct compaction 1039 * altogether. 1040 */ 1041 if (current->flags & PF_MEMALLOC) 1042 goto out; 1043 1044 lock_page(page); 1045 } 1046 1047 if (PageWriteback(page)) { 1048 /* 1049 * Only in the case of a full synchronous migration is it 1050 * necessary to wait for PageWriteback. In the async case, 1051 * the retry loop is too short and in the sync-light case, 1052 * the overhead of stalling is too much 1053 */ 1054 switch (mode) { 1055 case MIGRATE_SYNC: 1056 case MIGRATE_SYNC_NO_COPY: 1057 break; 1058 default: 1059 rc = -EBUSY; 1060 goto out_unlock; 1061 } 1062 if (!force) 1063 goto out_unlock; 1064 wait_on_page_writeback(page); 1065 } 1066 1067 /* 1068 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, 1069 * we cannot notice that anon_vma is freed while we migrates a page. 1070 * This get_anon_vma() delays freeing anon_vma pointer until the end 1071 * of migration. File cache pages are no problem because of page_lock() 1072 * File Caches may use write_page() or lock_page() in migration, then, 1073 * just care Anon page here. 1074 * 1075 * Only page_get_anon_vma() understands the subtleties of 1076 * getting a hold on an anon_vma from outside one of its mms. 1077 * But if we cannot get anon_vma, then we won't need it anyway, 1078 * because that implies that the anon page is no longer mapped 1079 * (and cannot be remapped so long as we hold the page lock). 1080 */ 1081 if (PageAnon(page) && !PageKsm(page)) 1082 anon_vma = page_get_anon_vma(page); 1083 1084 /* 1085 * Block others from accessing the new page when we get around to 1086 * establishing additional references. We are usually the only one 1087 * holding a reference to newpage at this point. We used to have a BUG 1088 * here if trylock_page(newpage) fails, but would like to allow for 1089 * cases where there might be a race with the previous use of newpage. 1090 * This is much like races on refcount of oldpage: just don't BUG(). 1091 */ 1092 if (unlikely(!trylock_page(newpage))) 1093 goto out_unlock; 1094 1095 if (unlikely(!is_lru)) { 1096 rc = move_to_new_page(newpage, page, mode); 1097 goto out_unlock_both; 1098 } 1099 1100 /* 1101 * Corner case handling: 1102 * 1. When a new swap-cache page is read into, it is added to the LRU 1103 * and treated as swapcache but it has no rmap yet. 1104 * Calling try_to_unmap() against a page->mapping==NULL page will 1105 * trigger a BUG. So handle it here. 1106 * 2. An orphaned page (see truncate_complete_page) might have 1107 * fs-private metadata. The page can be picked up due to memory 1108 * offlining. Everywhere else except page reclaim, the page is 1109 * invisible to the vm, so the page can not be migrated. So try to 1110 * free the metadata, so the page can be freed. 1111 */ 1112 if (!page->mapping) { 1113 VM_BUG_ON_PAGE(PageAnon(page), page); 1114 if (page_has_private(page)) { 1115 try_to_free_buffers(page); 1116 goto out_unlock_both; 1117 } 1118 } else if (page_mapped(page)) { 1119 /* Establish migration ptes */ 1120 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, 1121 page); 1122 try_to_unmap(page, 1123 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); 1124 page_was_mapped = 1; 1125 } 1126 1127 if (!page_mapped(page)) 1128 rc = move_to_new_page(newpage, page, mode); 1129 1130 if (page_was_mapped) 1131 remove_migration_ptes(page, 1132 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); 1133 1134 out_unlock_both: 1135 unlock_page(newpage); 1136 out_unlock: 1137 /* Drop an anon_vma reference if we took one */ 1138 if (anon_vma) 1139 put_anon_vma(anon_vma); 1140 unlock_page(page); 1141 out: 1142 /* 1143 * If migration is successful, decrease refcount of the newpage 1144 * which will not free the page because new page owner increased 1145 * refcounter. As well, if it is LRU page, add the page to LRU 1146 * list in here. Use the old state of the isolated source page to 1147 * determine if we migrated a LRU page. newpage was already unlocked 1148 * and possibly modified by its owner - don't rely on the page 1149 * state. 1150 */ 1151 if (rc == MIGRATEPAGE_SUCCESS) { 1152 if (unlikely(!is_lru)) 1153 put_page(newpage); 1154 else 1155 putback_lru_page(newpage); 1156 } 1157 1158 return rc; 1159 } 1160 1161 /* 1162 * Obtain the lock on page, remove all ptes and migrate the page 1163 * to the newly allocated page in newpage. 1164 */ 1165 static int unmap_and_move(new_page_t get_new_page, 1166 free_page_t put_new_page, 1167 unsigned long private, struct page *page, 1168 int force, enum migrate_mode mode, 1169 enum migrate_reason reason) 1170 { 1171 int rc = MIGRATEPAGE_SUCCESS; 1172 struct page *newpage = NULL; 1173 1174 if (!thp_migration_supported() && PageTransHuge(page)) 1175 return -ENOMEM; 1176 1177 if (page_count(page) == 1) { 1178 /* page was freed from under us. So we are done. */ 1179 ClearPageActive(page); 1180 ClearPageUnevictable(page); 1181 if (unlikely(__PageMovable(page))) { 1182 lock_page(page); 1183 if (!PageMovable(page)) 1184 __ClearPageIsolated(page); 1185 unlock_page(page); 1186 } 1187 goto out; 1188 } 1189 1190 newpage = get_new_page(page, private); 1191 if (!newpage) 1192 return -ENOMEM; 1193 1194 rc = __unmap_and_move(page, newpage, force, mode); 1195 if (rc == MIGRATEPAGE_SUCCESS) 1196 set_page_owner_migrate_reason(newpage, reason); 1197 1198 out: 1199 if (rc != -EAGAIN) { 1200 /* 1201 * A page that has been migrated has all references 1202 * removed and will be freed. A page that has not been 1203 * migrated will have kept its references and be restored. 1204 */ 1205 list_del(&page->lru); 1206 1207 /* 1208 * Compaction can migrate also non-LRU pages which are 1209 * not accounted to NR_ISOLATED_*. They can be recognized 1210 * as __PageMovable 1211 */ 1212 if (likely(!__PageMovable(page))) 1213 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + 1214 page_is_file_lru(page), -thp_nr_pages(page)); 1215 } 1216 1217 /* 1218 * If migration is successful, releases reference grabbed during 1219 * isolation. Otherwise, restore the page to right list unless 1220 * we want to retry. 1221 */ 1222 if (rc == MIGRATEPAGE_SUCCESS) { 1223 put_page(page); 1224 if (reason == MR_MEMORY_FAILURE) { 1225 /* 1226 * Set PG_HWPoison on just freed page 1227 * intentionally. Although it's rather weird, 1228 * it's how HWPoison flag works at the moment. 1229 */ 1230 if (set_hwpoison_free_buddy_page(page)) 1231 num_poisoned_pages_inc(); 1232 } 1233 } else { 1234 if (rc != -EAGAIN) { 1235 if (likely(!__PageMovable(page))) { 1236 putback_lru_page(page); 1237 goto put_new; 1238 } 1239 1240 lock_page(page); 1241 if (PageMovable(page)) 1242 putback_movable_page(page); 1243 else 1244 __ClearPageIsolated(page); 1245 unlock_page(page); 1246 put_page(page); 1247 } 1248 put_new: 1249 if (put_new_page) 1250 put_new_page(newpage, private); 1251 else 1252 put_page(newpage); 1253 } 1254 1255 return rc; 1256 } 1257 1258 /* 1259 * Counterpart of unmap_and_move_page() for hugepage migration. 1260 * 1261 * This function doesn't wait the completion of hugepage I/O 1262 * because there is no race between I/O and migration for hugepage. 1263 * Note that currently hugepage I/O occurs only in direct I/O 1264 * where no lock is held and PG_writeback is irrelevant, 1265 * and writeback status of all subpages are counted in the reference 1266 * count of the head page (i.e. if all subpages of a 2MB hugepage are 1267 * under direct I/O, the reference of the head page is 512 and a bit more.) 1268 * This means that when we try to migrate hugepage whose subpages are 1269 * doing direct I/O, some references remain after try_to_unmap() and 1270 * hugepage migration fails without data corruption. 1271 * 1272 * There is also no race when direct I/O is issued on the page under migration, 1273 * because then pte is replaced with migration swap entry and direct I/O code 1274 * will wait in the page fault for migration to complete. 1275 */ 1276 static int unmap_and_move_huge_page(new_page_t get_new_page, 1277 free_page_t put_new_page, unsigned long private, 1278 struct page *hpage, int force, 1279 enum migrate_mode mode, int reason) 1280 { 1281 int rc = -EAGAIN; 1282 int page_was_mapped = 0; 1283 struct page *new_hpage; 1284 struct anon_vma *anon_vma = NULL; 1285 struct address_space *mapping = NULL; 1286 1287 /* 1288 * Migratability of hugepages depends on architectures and their size. 1289 * This check is necessary because some callers of hugepage migration 1290 * like soft offline and memory hotremove don't walk through page 1291 * tables or check whether the hugepage is pmd-based or not before 1292 * kicking migration. 1293 */ 1294 if (!hugepage_migration_supported(page_hstate(hpage))) { 1295 putback_active_hugepage(hpage); 1296 return -ENOSYS; 1297 } 1298 1299 new_hpage = get_new_page(hpage, private); 1300 if (!new_hpage) 1301 return -ENOMEM; 1302 1303 if (!trylock_page(hpage)) { 1304 if (!force) 1305 goto out; 1306 switch (mode) { 1307 case MIGRATE_SYNC: 1308 case MIGRATE_SYNC_NO_COPY: 1309 break; 1310 default: 1311 goto out; 1312 } 1313 lock_page(hpage); 1314 } 1315 1316 /* 1317 * Check for pages which are in the process of being freed. Without 1318 * page_mapping() set, hugetlbfs specific move page routine will not 1319 * be called and we could leak usage counts for subpools. 1320 */ 1321 if (page_private(hpage) && !page_mapping(hpage)) { 1322 rc = -EBUSY; 1323 goto out_unlock; 1324 } 1325 1326 if (PageAnon(hpage)) 1327 anon_vma = page_get_anon_vma(hpage); 1328 1329 if (unlikely(!trylock_page(new_hpage))) 1330 goto put_anon; 1331 1332 if (page_mapped(hpage)) { 1333 /* 1334 * try_to_unmap could potentially call huge_pmd_unshare. 1335 * Because of this, take semaphore in write mode here and 1336 * set TTU_RMAP_LOCKED to let lower levels know we have 1337 * taken the lock. 1338 */ 1339 mapping = hugetlb_page_mapping_lock_write(hpage); 1340 if (unlikely(!mapping)) 1341 goto unlock_put_anon; 1342 1343 try_to_unmap(hpage, 1344 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS| 1345 TTU_RMAP_LOCKED); 1346 page_was_mapped = 1; 1347 /* 1348 * Leave mapping locked until after subsequent call to 1349 * remove_migration_ptes() 1350 */ 1351 } 1352 1353 if (!page_mapped(hpage)) 1354 rc = move_to_new_page(new_hpage, hpage, mode); 1355 1356 if (page_was_mapped) { 1357 remove_migration_ptes(hpage, 1358 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, true); 1359 i_mmap_unlock_write(mapping); 1360 } 1361 1362 unlock_put_anon: 1363 unlock_page(new_hpage); 1364 1365 put_anon: 1366 if (anon_vma) 1367 put_anon_vma(anon_vma); 1368 1369 if (rc == MIGRATEPAGE_SUCCESS) { 1370 move_hugetlb_state(hpage, new_hpage, reason); 1371 put_new_page = NULL; 1372 } 1373 1374 out_unlock: 1375 unlock_page(hpage); 1376 out: 1377 if (rc != -EAGAIN) 1378 putback_active_hugepage(hpage); 1379 1380 /* 1381 * If migration was not successful and there's a freeing callback, use 1382 * it. Otherwise, put_page() will drop the reference grabbed during 1383 * isolation. 1384 */ 1385 if (put_new_page) 1386 put_new_page(new_hpage, private); 1387 else 1388 putback_active_hugepage(new_hpage); 1389 1390 return rc; 1391 } 1392 1393 /* 1394 * migrate_pages - migrate the pages specified in a list, to the free pages 1395 * supplied as the target for the page migration 1396 * 1397 * @from: The list of pages to be migrated. 1398 * @get_new_page: The function used to allocate free pages to be used 1399 * as the target of the page migration. 1400 * @put_new_page: The function used to free target pages if migration 1401 * fails, or NULL if no special handling is necessary. 1402 * @private: Private data to be passed on to get_new_page() 1403 * @mode: The migration mode that specifies the constraints for 1404 * page migration, if any. 1405 * @reason: The reason for page migration. 1406 * 1407 * The function returns after 10 attempts or if no pages are movable any more 1408 * because the list has become empty or no retryable pages exist any more. 1409 * The caller should call putback_movable_pages() to return pages to the LRU 1410 * or free list only if ret != 0. 1411 * 1412 * Returns the number of pages that were not migrated, or an error code. 1413 */ 1414 int migrate_pages(struct list_head *from, new_page_t get_new_page, 1415 free_page_t put_new_page, unsigned long private, 1416 enum migrate_mode mode, int reason) 1417 { 1418 int retry = 1; 1419 int thp_retry = 1; 1420 int nr_failed = 0; 1421 int nr_succeeded = 0; 1422 int nr_thp_succeeded = 0; 1423 int nr_thp_failed = 0; 1424 int nr_thp_split = 0; 1425 int pass = 0; 1426 bool is_thp = false; 1427 struct page *page; 1428 struct page *page2; 1429 int swapwrite = current->flags & PF_SWAPWRITE; 1430 int rc, nr_subpages; 1431 1432 if (!swapwrite) 1433 current->flags |= PF_SWAPWRITE; 1434 1435 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) { 1436 retry = 0; 1437 thp_retry = 0; 1438 1439 list_for_each_entry_safe(page, page2, from, lru) { 1440 retry: 1441 /* 1442 * THP statistics is based on the source huge page. 1443 * Capture required information that might get lost 1444 * during migration. 1445 */ 1446 is_thp = PageTransHuge(page); 1447 nr_subpages = thp_nr_pages(page); 1448 cond_resched(); 1449 1450 if (PageHuge(page)) 1451 rc = unmap_and_move_huge_page(get_new_page, 1452 put_new_page, private, page, 1453 pass > 2, mode, reason); 1454 else 1455 rc = unmap_and_move(get_new_page, put_new_page, 1456 private, page, pass > 2, mode, 1457 reason); 1458 1459 switch(rc) { 1460 case -ENOMEM: 1461 /* 1462 * THP migration might be unsupported or the 1463 * allocation could've failed so we should 1464 * retry on the same page with the THP split 1465 * to base pages. 1466 * 1467 * Head page is retried immediately and tail 1468 * pages are added to the tail of the list so 1469 * we encounter them after the rest of the list 1470 * is processed. 1471 */ 1472 if (PageTransHuge(page) && !PageHuge(page)) { 1473 lock_page(page); 1474 rc = split_huge_page_to_list(page, from); 1475 unlock_page(page); 1476 if (!rc) { 1477 list_safe_reset_next(page, page2, lru); 1478 nr_thp_split++; 1479 goto retry; 1480 } 1481 } 1482 if (is_thp) { 1483 nr_thp_failed++; 1484 nr_failed += nr_subpages; 1485 goto out; 1486 } 1487 nr_failed++; 1488 goto out; 1489 case -EAGAIN: 1490 if (is_thp) { 1491 thp_retry++; 1492 break; 1493 } 1494 retry++; 1495 break; 1496 case MIGRATEPAGE_SUCCESS: 1497 if (is_thp) { 1498 nr_thp_succeeded++; 1499 nr_succeeded += nr_subpages; 1500 break; 1501 } 1502 nr_succeeded++; 1503 break; 1504 default: 1505 /* 1506 * Permanent failure (-EBUSY, -ENOSYS, etc.): 1507 * unlike -EAGAIN case, the failed page is 1508 * removed from migration page list and not 1509 * retried in the next outer loop. 1510 */ 1511 if (is_thp) { 1512 nr_thp_failed++; 1513 nr_failed += nr_subpages; 1514 break; 1515 } 1516 nr_failed++; 1517 break; 1518 } 1519 } 1520 } 1521 nr_failed += retry + thp_retry; 1522 nr_thp_failed += thp_retry; 1523 rc = nr_failed; 1524 out: 1525 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); 1526 count_vm_events(PGMIGRATE_FAIL, nr_failed); 1527 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded); 1528 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed); 1529 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split); 1530 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded, 1531 nr_thp_failed, nr_thp_split, mode, reason); 1532 1533 if (!swapwrite) 1534 current->flags &= ~PF_SWAPWRITE; 1535 1536 return rc; 1537 } 1538 1539 struct page *alloc_migration_target(struct page *page, unsigned long private) 1540 { 1541 struct migration_target_control *mtc; 1542 gfp_t gfp_mask; 1543 unsigned int order = 0; 1544 struct page *new_page = NULL; 1545 int nid; 1546 int zidx; 1547 1548 mtc = (struct migration_target_control *)private; 1549 gfp_mask = mtc->gfp_mask; 1550 nid = mtc->nid; 1551 if (nid == NUMA_NO_NODE) 1552 nid = page_to_nid(page); 1553 1554 if (PageHuge(page)) { 1555 struct hstate *h = page_hstate(compound_head(page)); 1556 1557 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask); 1558 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask); 1559 } 1560 1561 if (PageTransHuge(page)) { 1562 /* 1563 * clear __GFP_RECLAIM to make the migration callback 1564 * consistent with regular THP allocations. 1565 */ 1566 gfp_mask &= ~__GFP_RECLAIM; 1567 gfp_mask |= GFP_TRANSHUGE; 1568 order = HPAGE_PMD_ORDER; 1569 } 1570 zidx = zone_idx(page_zone(page)); 1571 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE) 1572 gfp_mask |= __GFP_HIGHMEM; 1573 1574 new_page = __alloc_pages_nodemask(gfp_mask, order, nid, mtc->nmask); 1575 1576 if (new_page && PageTransHuge(new_page)) 1577 prep_transhuge_page(new_page); 1578 1579 return new_page; 1580 } 1581 1582 #ifdef CONFIG_NUMA 1583 1584 static int store_status(int __user *status, int start, int value, int nr) 1585 { 1586 while (nr-- > 0) { 1587 if (put_user(value, status + start)) 1588 return -EFAULT; 1589 start++; 1590 } 1591 1592 return 0; 1593 } 1594 1595 static int do_move_pages_to_node(struct mm_struct *mm, 1596 struct list_head *pagelist, int node) 1597 { 1598 int err; 1599 struct migration_target_control mtc = { 1600 .nid = node, 1601 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 1602 }; 1603 1604 err = migrate_pages(pagelist, alloc_migration_target, NULL, 1605 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL); 1606 if (err) 1607 putback_movable_pages(pagelist); 1608 return err; 1609 } 1610 1611 /* 1612 * Resolves the given address to a struct page, isolates it from the LRU and 1613 * puts it to the given pagelist. 1614 * Returns: 1615 * errno - if the page cannot be found/isolated 1616 * 0 - when it doesn't have to be migrated because it is already on the 1617 * target node 1618 * 1 - when it has been queued 1619 */ 1620 static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, 1621 int node, struct list_head *pagelist, bool migrate_all) 1622 { 1623 struct vm_area_struct *vma; 1624 struct page *page; 1625 unsigned int follflags; 1626 int err; 1627 1628 mmap_read_lock(mm); 1629 err = -EFAULT; 1630 vma = find_vma(mm, addr); 1631 if (!vma || addr < vma->vm_start || !vma_migratable(vma)) 1632 goto out; 1633 1634 /* FOLL_DUMP to ignore special (like zero) pages */ 1635 follflags = FOLL_GET | FOLL_DUMP; 1636 page = follow_page(vma, addr, follflags); 1637 1638 err = PTR_ERR(page); 1639 if (IS_ERR(page)) 1640 goto out; 1641 1642 err = -ENOENT; 1643 if (!page) 1644 goto out; 1645 1646 err = 0; 1647 if (page_to_nid(page) == node) 1648 goto out_putpage; 1649 1650 err = -EACCES; 1651 if (page_mapcount(page) > 1 && !migrate_all) 1652 goto out_putpage; 1653 1654 if (PageHuge(page)) { 1655 if (PageHead(page)) { 1656 isolate_huge_page(page, pagelist); 1657 err = 1; 1658 } 1659 } else { 1660 struct page *head; 1661 1662 head = compound_head(page); 1663 err = isolate_lru_page(head); 1664 if (err) 1665 goto out_putpage; 1666 1667 err = 1; 1668 list_add_tail(&head->lru, pagelist); 1669 mod_node_page_state(page_pgdat(head), 1670 NR_ISOLATED_ANON + page_is_file_lru(head), 1671 thp_nr_pages(head)); 1672 } 1673 out_putpage: 1674 /* 1675 * Either remove the duplicate refcount from 1676 * isolate_lru_page() or drop the page ref if it was 1677 * not isolated. 1678 */ 1679 put_page(page); 1680 out: 1681 mmap_read_unlock(mm); 1682 return err; 1683 } 1684 1685 static int move_pages_and_store_status(struct mm_struct *mm, int node, 1686 struct list_head *pagelist, int __user *status, 1687 int start, int i, unsigned long nr_pages) 1688 { 1689 int err; 1690 1691 if (list_empty(pagelist)) 1692 return 0; 1693 1694 err = do_move_pages_to_node(mm, pagelist, node); 1695 if (err) { 1696 /* 1697 * Positive err means the number of failed 1698 * pages to migrate. Since we are going to 1699 * abort and return the number of non-migrated 1700 * pages, so need to incude the rest of the 1701 * nr_pages that have not been attempted as 1702 * well. 1703 */ 1704 if (err > 0) 1705 err += nr_pages - i - 1; 1706 return err; 1707 } 1708 return store_status(status, start, node, i - start); 1709 } 1710 1711 /* 1712 * Migrate an array of page address onto an array of nodes and fill 1713 * the corresponding array of status. 1714 */ 1715 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, 1716 unsigned long nr_pages, 1717 const void __user * __user *pages, 1718 const int __user *nodes, 1719 int __user *status, int flags) 1720 { 1721 int current_node = NUMA_NO_NODE; 1722 LIST_HEAD(pagelist); 1723 int start, i; 1724 int err = 0, err1; 1725 1726 migrate_prep(); 1727 1728 for (i = start = 0; i < nr_pages; i++) { 1729 const void __user *p; 1730 unsigned long addr; 1731 int node; 1732 1733 err = -EFAULT; 1734 if (get_user(p, pages + i)) 1735 goto out_flush; 1736 if (get_user(node, nodes + i)) 1737 goto out_flush; 1738 addr = (unsigned long)untagged_addr(p); 1739 1740 err = -ENODEV; 1741 if (node < 0 || node >= MAX_NUMNODES) 1742 goto out_flush; 1743 if (!node_state(node, N_MEMORY)) 1744 goto out_flush; 1745 1746 err = -EACCES; 1747 if (!node_isset(node, task_nodes)) 1748 goto out_flush; 1749 1750 if (current_node == NUMA_NO_NODE) { 1751 current_node = node; 1752 start = i; 1753 } else if (node != current_node) { 1754 err = move_pages_and_store_status(mm, current_node, 1755 &pagelist, status, start, i, nr_pages); 1756 if (err) 1757 goto out; 1758 start = i; 1759 current_node = node; 1760 } 1761 1762 /* 1763 * Errors in the page lookup or isolation are not fatal and we simply 1764 * report them via status 1765 */ 1766 err = add_page_for_migration(mm, addr, current_node, 1767 &pagelist, flags & MPOL_MF_MOVE_ALL); 1768 1769 if (err > 0) { 1770 /* The page is successfully queued for migration */ 1771 continue; 1772 } 1773 1774 /* 1775 * If the page is already on the target node (!err), store the 1776 * node, otherwise, store the err. 1777 */ 1778 err = store_status(status, i, err ? : current_node, 1); 1779 if (err) 1780 goto out_flush; 1781 1782 err = move_pages_and_store_status(mm, current_node, &pagelist, 1783 status, start, i, nr_pages); 1784 if (err) 1785 goto out; 1786 current_node = NUMA_NO_NODE; 1787 } 1788 out_flush: 1789 /* Make sure we do not overwrite the existing error */ 1790 err1 = move_pages_and_store_status(mm, current_node, &pagelist, 1791 status, start, i, nr_pages); 1792 if (err >= 0) 1793 err = err1; 1794 out: 1795 return err; 1796 } 1797 1798 /* 1799 * Determine the nodes of an array of pages and store it in an array of status. 1800 */ 1801 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, 1802 const void __user **pages, int *status) 1803 { 1804 unsigned long i; 1805 1806 mmap_read_lock(mm); 1807 1808 for (i = 0; i < nr_pages; i++) { 1809 unsigned long addr = (unsigned long)(*pages); 1810 struct vm_area_struct *vma; 1811 struct page *page; 1812 int err = -EFAULT; 1813 1814 vma = find_vma(mm, addr); 1815 if (!vma || addr < vma->vm_start) 1816 goto set_status; 1817 1818 /* FOLL_DUMP to ignore special (like zero) pages */ 1819 page = follow_page(vma, addr, FOLL_DUMP); 1820 1821 err = PTR_ERR(page); 1822 if (IS_ERR(page)) 1823 goto set_status; 1824 1825 err = page ? page_to_nid(page) : -ENOENT; 1826 set_status: 1827 *status = err; 1828 1829 pages++; 1830 status++; 1831 } 1832 1833 mmap_read_unlock(mm); 1834 } 1835 1836 /* 1837 * Determine the nodes of a user array of pages and store it in 1838 * a user array of status. 1839 */ 1840 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, 1841 const void __user * __user *pages, 1842 int __user *status) 1843 { 1844 #define DO_PAGES_STAT_CHUNK_NR 16 1845 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; 1846 int chunk_status[DO_PAGES_STAT_CHUNK_NR]; 1847 1848 while (nr_pages) { 1849 unsigned long chunk_nr; 1850 1851 chunk_nr = nr_pages; 1852 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) 1853 chunk_nr = DO_PAGES_STAT_CHUNK_NR; 1854 1855 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) 1856 break; 1857 1858 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); 1859 1860 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) 1861 break; 1862 1863 pages += chunk_nr; 1864 status += chunk_nr; 1865 nr_pages -= chunk_nr; 1866 } 1867 return nr_pages ? -EFAULT : 0; 1868 } 1869 1870 /* 1871 * Move a list of pages in the address space of the currently executing 1872 * process. 1873 */ 1874 static int kernel_move_pages(pid_t pid, unsigned long nr_pages, 1875 const void __user * __user *pages, 1876 const int __user *nodes, 1877 int __user *status, int flags) 1878 { 1879 struct task_struct *task; 1880 struct mm_struct *mm; 1881 int err; 1882 nodemask_t task_nodes; 1883 1884 /* Check flags */ 1885 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) 1886 return -EINVAL; 1887 1888 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) 1889 return -EPERM; 1890 1891 /* Find the mm_struct */ 1892 rcu_read_lock(); 1893 task = pid ? find_task_by_vpid(pid) : current; 1894 if (!task) { 1895 rcu_read_unlock(); 1896 return -ESRCH; 1897 } 1898 get_task_struct(task); 1899 1900 /* 1901 * Check if this process has the right to modify the specified 1902 * process. Use the regular "ptrace_may_access()" checks. 1903 */ 1904 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { 1905 rcu_read_unlock(); 1906 err = -EPERM; 1907 goto out; 1908 } 1909 rcu_read_unlock(); 1910 1911 err = security_task_movememory(task); 1912 if (err) 1913 goto out; 1914 1915 task_nodes = cpuset_mems_allowed(task); 1916 mm = get_task_mm(task); 1917 put_task_struct(task); 1918 1919 if (!mm) 1920 return -EINVAL; 1921 1922 if (nodes) 1923 err = do_pages_move(mm, task_nodes, nr_pages, pages, 1924 nodes, status, flags); 1925 else 1926 err = do_pages_stat(mm, nr_pages, pages, status); 1927 1928 mmput(mm); 1929 return err; 1930 1931 out: 1932 put_task_struct(task); 1933 return err; 1934 } 1935 1936 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, 1937 const void __user * __user *, pages, 1938 const int __user *, nodes, 1939 int __user *, status, int, flags) 1940 { 1941 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); 1942 } 1943 1944 #ifdef CONFIG_COMPAT 1945 COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, 1946 compat_uptr_t __user *, pages32, 1947 const int __user *, nodes, 1948 int __user *, status, 1949 int, flags) 1950 { 1951 const void __user * __user *pages; 1952 int i; 1953 1954 pages = compat_alloc_user_space(nr_pages * sizeof(void *)); 1955 for (i = 0; i < nr_pages; i++) { 1956 compat_uptr_t p; 1957 1958 if (get_user(p, pages32 + i) || 1959 put_user(compat_ptr(p), pages + i)) 1960 return -EFAULT; 1961 } 1962 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); 1963 } 1964 #endif /* CONFIG_COMPAT */ 1965 1966 #ifdef CONFIG_NUMA_BALANCING 1967 /* 1968 * Returns true if this is a safe migration target node for misplaced NUMA 1969 * pages. Currently it only checks the watermarks which crude 1970 */ 1971 static bool migrate_balanced_pgdat(struct pglist_data *pgdat, 1972 unsigned long nr_migrate_pages) 1973 { 1974 int z; 1975 1976 for (z = pgdat->nr_zones - 1; z >= 0; z--) { 1977 struct zone *zone = pgdat->node_zones + z; 1978 1979 if (!populated_zone(zone)) 1980 continue; 1981 1982 /* Avoid waking kswapd by allocating pages_to_migrate pages. */ 1983 if (!zone_watermark_ok(zone, 0, 1984 high_wmark_pages(zone) + 1985 nr_migrate_pages, 1986 ZONE_MOVABLE, 0)) 1987 continue; 1988 return true; 1989 } 1990 return false; 1991 } 1992 1993 static struct page *alloc_misplaced_dst_page(struct page *page, 1994 unsigned long data) 1995 { 1996 int nid = (int) data; 1997 struct page *newpage; 1998 1999 newpage = __alloc_pages_node(nid, 2000 (GFP_HIGHUSER_MOVABLE | 2001 __GFP_THISNODE | __GFP_NOMEMALLOC | 2002 __GFP_NORETRY | __GFP_NOWARN) & 2003 ~__GFP_RECLAIM, 0); 2004 2005 return newpage; 2006 } 2007 2008 static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) 2009 { 2010 int page_lru; 2011 2012 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); 2013 2014 /* Avoid migrating to a node that is nearly full */ 2015 if (!migrate_balanced_pgdat(pgdat, compound_nr(page))) 2016 return 0; 2017 2018 if (isolate_lru_page(page)) 2019 return 0; 2020 2021 /* 2022 * migrate_misplaced_transhuge_page() skips page migration's usual 2023 * check on page_count(), so we must do it here, now that the page 2024 * has been isolated: a GUP pin, or any other pin, prevents migration. 2025 * The expected page count is 3: 1 for page's mapcount and 1 for the 2026 * caller's pin and 1 for the reference taken by isolate_lru_page(). 2027 */ 2028 if (PageTransHuge(page) && page_count(page) != 3) { 2029 putback_lru_page(page); 2030 return 0; 2031 } 2032 2033 page_lru = page_is_file_lru(page); 2034 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, 2035 thp_nr_pages(page)); 2036 2037 /* 2038 * Isolating the page has taken another reference, so the 2039 * caller's reference can be safely dropped without the page 2040 * disappearing underneath us during migration. 2041 */ 2042 put_page(page); 2043 return 1; 2044 } 2045 2046 bool pmd_trans_migrating(pmd_t pmd) 2047 { 2048 struct page *page = pmd_page(pmd); 2049 return PageLocked(page); 2050 } 2051 2052 /* 2053 * Attempt to migrate a misplaced page to the specified destination 2054 * node. Caller is expected to have an elevated reference count on 2055 * the page that will be dropped by this function before returning. 2056 */ 2057 int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, 2058 int node) 2059 { 2060 pg_data_t *pgdat = NODE_DATA(node); 2061 int isolated; 2062 int nr_remaining; 2063 LIST_HEAD(migratepages); 2064 2065 /* 2066 * Don't migrate file pages that are mapped in multiple processes 2067 * with execute permissions as they are probably shared libraries. 2068 */ 2069 if (page_mapcount(page) != 1 && page_is_file_lru(page) && 2070 (vma->vm_flags & VM_EXEC)) 2071 goto out; 2072 2073 /* 2074 * Also do not migrate dirty pages as not all filesystems can move 2075 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles. 2076 */ 2077 if (page_is_file_lru(page) && PageDirty(page)) 2078 goto out; 2079 2080 isolated = numamigrate_isolate_page(pgdat, page); 2081 if (!isolated) 2082 goto out; 2083 2084 list_add(&page->lru, &migratepages); 2085 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, 2086 NULL, node, MIGRATE_ASYNC, 2087 MR_NUMA_MISPLACED); 2088 if (nr_remaining) { 2089 if (!list_empty(&migratepages)) { 2090 list_del(&page->lru); 2091 dec_node_page_state(page, NR_ISOLATED_ANON + 2092 page_is_file_lru(page)); 2093 putback_lru_page(page); 2094 } 2095 isolated = 0; 2096 } else 2097 count_vm_numa_event(NUMA_PAGE_MIGRATE); 2098 BUG_ON(!list_empty(&migratepages)); 2099 return isolated; 2100 2101 out: 2102 put_page(page); 2103 return 0; 2104 } 2105 #endif /* CONFIG_NUMA_BALANCING */ 2106 2107 #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 2108 /* 2109 * Migrates a THP to a given target node. page must be locked and is unlocked 2110 * before returning. 2111 */ 2112 int migrate_misplaced_transhuge_page(struct mm_struct *mm, 2113 struct vm_area_struct *vma, 2114 pmd_t *pmd, pmd_t entry, 2115 unsigned long address, 2116 struct page *page, int node) 2117 { 2118 spinlock_t *ptl; 2119 pg_data_t *pgdat = NODE_DATA(node); 2120 int isolated = 0; 2121 struct page *new_page = NULL; 2122 int page_lru = page_is_file_lru(page); 2123 unsigned long start = address & HPAGE_PMD_MASK; 2124 2125 new_page = alloc_pages_node(node, 2126 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), 2127 HPAGE_PMD_ORDER); 2128 if (!new_page) 2129 goto out_fail; 2130 prep_transhuge_page(new_page); 2131 2132 isolated = numamigrate_isolate_page(pgdat, page); 2133 if (!isolated) { 2134 put_page(new_page); 2135 goto out_fail; 2136 } 2137 2138 /* Prepare a page as a migration target */ 2139 __SetPageLocked(new_page); 2140 if (PageSwapBacked(page)) 2141 __SetPageSwapBacked(new_page); 2142 2143 /* anon mapping, we can simply copy page->mapping to the new page: */ 2144 new_page->mapping = page->mapping; 2145 new_page->index = page->index; 2146 /* flush the cache before copying using the kernel virtual address */ 2147 flush_cache_range(vma, start, start + HPAGE_PMD_SIZE); 2148 migrate_page_copy(new_page, page); 2149 WARN_ON(PageLRU(new_page)); 2150 2151 /* Recheck the target PMD */ 2152 ptl = pmd_lock(mm, pmd); 2153 if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) { 2154 spin_unlock(ptl); 2155 2156 /* Reverse changes made by migrate_page_copy() */ 2157 if (TestClearPageActive(new_page)) 2158 SetPageActive(page); 2159 if (TestClearPageUnevictable(new_page)) 2160 SetPageUnevictable(page); 2161 2162 unlock_page(new_page); 2163 put_page(new_page); /* Free it */ 2164 2165 /* Retake the callers reference and putback on LRU */ 2166 get_page(page); 2167 putback_lru_page(page); 2168 mod_node_page_state(page_pgdat(page), 2169 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); 2170 2171 goto out_unlock; 2172 } 2173 2174 entry = mk_huge_pmd(new_page, vma->vm_page_prot); 2175 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 2176 2177 /* 2178 * Overwrite the old entry under pagetable lock and establish 2179 * the new PTE. Any parallel GUP will either observe the old 2180 * page blocking on the page lock, block on the page table 2181 * lock or observe the new page. The SetPageUptodate on the 2182 * new page and page_add_new_anon_rmap guarantee the copy is 2183 * visible before the pagetable update. 2184 */ 2185 page_add_anon_rmap(new_page, vma, start, true); 2186 /* 2187 * At this point the pmd is numa/protnone (i.e. non present) and the TLB 2188 * has already been flushed globally. So no TLB can be currently 2189 * caching this non present pmd mapping. There's no need to clear the 2190 * pmd before doing set_pmd_at(), nor to flush the TLB after 2191 * set_pmd_at(). Clearing the pmd here would introduce a race 2192 * condition against MADV_DONTNEED, because MADV_DONTNEED only holds the 2193 * mmap_lock for reading. If the pmd is set to NULL at any given time, 2194 * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this 2195 * pmd. 2196 */ 2197 set_pmd_at(mm, start, pmd, entry); 2198 update_mmu_cache_pmd(vma, address, &entry); 2199 2200 page_ref_unfreeze(page, 2); 2201 mlock_migrate_page(new_page, page); 2202 page_remove_rmap(page, true); 2203 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); 2204 2205 spin_unlock(ptl); 2206 2207 /* Take an "isolate" reference and put new page on the LRU. */ 2208 get_page(new_page); 2209 putback_lru_page(new_page); 2210 2211 unlock_page(new_page); 2212 unlock_page(page); 2213 put_page(page); /* Drop the rmap reference */ 2214 put_page(page); /* Drop the LRU isolation reference */ 2215 2216 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); 2217 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); 2218 2219 mod_node_page_state(page_pgdat(page), 2220 NR_ISOLATED_ANON + page_lru, 2221 -HPAGE_PMD_NR); 2222 return isolated; 2223 2224 out_fail: 2225 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); 2226 ptl = pmd_lock(mm, pmd); 2227 if (pmd_same(*pmd, entry)) { 2228 entry = pmd_modify(entry, vma->vm_page_prot); 2229 set_pmd_at(mm, start, pmd, entry); 2230 update_mmu_cache_pmd(vma, address, &entry); 2231 } 2232 spin_unlock(ptl); 2233 2234 out_unlock: 2235 unlock_page(page); 2236 put_page(page); 2237 return 0; 2238 } 2239 #endif /* CONFIG_NUMA_BALANCING */ 2240 2241 #endif /* CONFIG_NUMA */ 2242 2243 #ifdef CONFIG_DEVICE_PRIVATE 2244 static int migrate_vma_collect_hole(unsigned long start, 2245 unsigned long end, 2246 __always_unused int depth, 2247 struct mm_walk *walk) 2248 { 2249 struct migrate_vma *migrate = walk->private; 2250 unsigned long addr; 2251 2252 /* Only allow populating anonymous memory. */ 2253 if (!vma_is_anonymous(walk->vma)) { 2254 for (addr = start; addr < end; addr += PAGE_SIZE) { 2255 migrate->src[migrate->npages] = 0; 2256 migrate->dst[migrate->npages] = 0; 2257 migrate->npages++; 2258 } 2259 return 0; 2260 } 2261 2262 for (addr = start; addr < end; addr += PAGE_SIZE) { 2263 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; 2264 migrate->dst[migrate->npages] = 0; 2265 migrate->npages++; 2266 migrate->cpages++; 2267 } 2268 2269 return 0; 2270 } 2271 2272 static int migrate_vma_collect_skip(unsigned long start, 2273 unsigned long end, 2274 struct mm_walk *walk) 2275 { 2276 struct migrate_vma *migrate = walk->private; 2277 unsigned long addr; 2278 2279 for (addr = start; addr < end; addr += PAGE_SIZE) { 2280 migrate->dst[migrate->npages] = 0; 2281 migrate->src[migrate->npages++] = 0; 2282 } 2283 2284 return 0; 2285 } 2286 2287 static int migrate_vma_collect_pmd(pmd_t *pmdp, 2288 unsigned long start, 2289 unsigned long end, 2290 struct mm_walk *walk) 2291 { 2292 struct migrate_vma *migrate = walk->private; 2293 struct vm_area_struct *vma = walk->vma; 2294 struct mm_struct *mm = vma->vm_mm; 2295 unsigned long addr = start, unmapped = 0; 2296 spinlock_t *ptl; 2297 pte_t *ptep; 2298 2299 again: 2300 if (pmd_none(*pmdp)) 2301 return migrate_vma_collect_hole(start, end, -1, walk); 2302 2303 if (pmd_trans_huge(*pmdp)) { 2304 struct page *page; 2305 2306 ptl = pmd_lock(mm, pmdp); 2307 if (unlikely(!pmd_trans_huge(*pmdp))) { 2308 spin_unlock(ptl); 2309 goto again; 2310 } 2311 2312 page = pmd_page(*pmdp); 2313 if (is_huge_zero_page(page)) { 2314 spin_unlock(ptl); 2315 split_huge_pmd(vma, pmdp, addr); 2316 if (pmd_trans_unstable(pmdp)) 2317 return migrate_vma_collect_skip(start, end, 2318 walk); 2319 } else { 2320 int ret; 2321 2322 get_page(page); 2323 spin_unlock(ptl); 2324 if (unlikely(!trylock_page(page))) 2325 return migrate_vma_collect_skip(start, end, 2326 walk); 2327 ret = split_huge_page(page); 2328 unlock_page(page); 2329 put_page(page); 2330 if (ret) 2331 return migrate_vma_collect_skip(start, end, 2332 walk); 2333 if (pmd_none(*pmdp)) 2334 return migrate_vma_collect_hole(start, end, -1, 2335 walk); 2336 } 2337 } 2338 2339 if (unlikely(pmd_bad(*pmdp))) 2340 return migrate_vma_collect_skip(start, end, walk); 2341 2342 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); 2343 arch_enter_lazy_mmu_mode(); 2344 2345 for (; addr < end; addr += PAGE_SIZE, ptep++) { 2346 unsigned long mpfn = 0, pfn; 2347 struct page *page; 2348 swp_entry_t entry; 2349 pte_t pte; 2350 2351 pte = *ptep; 2352 2353 if (pte_none(pte)) { 2354 if (vma_is_anonymous(vma)) { 2355 mpfn = MIGRATE_PFN_MIGRATE; 2356 migrate->cpages++; 2357 } 2358 goto next; 2359 } 2360 2361 if (!pte_present(pte)) { 2362 /* 2363 * Only care about unaddressable device page special 2364 * page table entry. Other special swap entries are not 2365 * migratable, and we ignore regular swapped page. 2366 */ 2367 entry = pte_to_swp_entry(pte); 2368 if (!is_device_private_entry(entry)) 2369 goto next; 2370 2371 page = device_private_entry_to_page(entry); 2372 if (!(migrate->flags & 2373 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || 2374 page->pgmap->owner != migrate->pgmap_owner) 2375 goto next; 2376 2377 mpfn = migrate_pfn(page_to_pfn(page)) | 2378 MIGRATE_PFN_MIGRATE; 2379 if (is_write_device_private_entry(entry)) 2380 mpfn |= MIGRATE_PFN_WRITE; 2381 } else { 2382 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) 2383 goto next; 2384 pfn = pte_pfn(pte); 2385 if (is_zero_pfn(pfn)) { 2386 mpfn = MIGRATE_PFN_MIGRATE; 2387 migrate->cpages++; 2388 goto next; 2389 } 2390 page = vm_normal_page(migrate->vma, addr, pte); 2391 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; 2392 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; 2393 } 2394 2395 /* FIXME support THP */ 2396 if (!page || !page->mapping || PageTransCompound(page)) { 2397 mpfn = 0; 2398 goto next; 2399 } 2400 2401 /* 2402 * By getting a reference on the page we pin it and that blocks 2403 * any kind of migration. Side effect is that it "freezes" the 2404 * pte. 2405 * 2406 * We drop this reference after isolating the page from the lru 2407 * for non device page (device page are not on the lru and thus 2408 * can't be dropped from it). 2409 */ 2410 get_page(page); 2411 migrate->cpages++; 2412 2413 /* 2414 * Optimize for the common case where page is only mapped once 2415 * in one process. If we can lock the page, then we can safely 2416 * set up a special migration page table entry now. 2417 */ 2418 if (trylock_page(page)) { 2419 pte_t swp_pte; 2420 2421 mpfn |= MIGRATE_PFN_LOCKED; 2422 ptep_get_and_clear(mm, addr, ptep); 2423 2424 /* Setup special migration page table entry */ 2425 entry = make_migration_entry(page, mpfn & 2426 MIGRATE_PFN_WRITE); 2427 swp_pte = swp_entry_to_pte(entry); 2428 if (pte_present(pte)) { 2429 if (pte_soft_dirty(pte)) 2430 swp_pte = pte_swp_mksoft_dirty(swp_pte); 2431 if (pte_uffd_wp(pte)) 2432 swp_pte = pte_swp_mkuffd_wp(swp_pte); 2433 } else { 2434 if (pte_swp_soft_dirty(pte)) 2435 swp_pte = pte_swp_mksoft_dirty(swp_pte); 2436 if (pte_swp_uffd_wp(pte)) 2437 swp_pte = pte_swp_mkuffd_wp(swp_pte); 2438 } 2439 set_pte_at(mm, addr, ptep, swp_pte); 2440 2441 /* 2442 * This is like regular unmap: we remove the rmap and 2443 * drop page refcount. Page won't be freed, as we took 2444 * a reference just above. 2445 */ 2446 page_remove_rmap(page, false); 2447 put_page(page); 2448 2449 if (pte_present(pte)) 2450 unmapped++; 2451 } 2452 2453 next: 2454 migrate->dst[migrate->npages] = 0; 2455 migrate->src[migrate->npages++] = mpfn; 2456 } 2457 arch_leave_lazy_mmu_mode(); 2458 pte_unmap_unlock(ptep - 1, ptl); 2459 2460 /* Only flush the TLB if we actually modified any entries */ 2461 if (unmapped) 2462 flush_tlb_range(walk->vma, start, end); 2463 2464 return 0; 2465 } 2466 2467 static const struct mm_walk_ops migrate_vma_walk_ops = { 2468 .pmd_entry = migrate_vma_collect_pmd, 2469 .pte_hole = migrate_vma_collect_hole, 2470 }; 2471 2472 /* 2473 * migrate_vma_collect() - collect pages over a range of virtual addresses 2474 * @migrate: migrate struct containing all migration information 2475 * 2476 * This will walk the CPU page table. For each virtual address backed by a 2477 * valid page, it updates the src array and takes a reference on the page, in 2478 * order to pin the page until we lock it and unmap it. 2479 */ 2480 static void migrate_vma_collect(struct migrate_vma *migrate) 2481 { 2482 struct mmu_notifier_range range; 2483 2484 /* 2485 * Note that the pgmap_owner is passed to the mmu notifier callback so 2486 * that the registered device driver can skip invalidating device 2487 * private page mappings that won't be migrated. 2488 */ 2489 mmu_notifier_range_init_migrate(&range, 0, migrate->vma, 2490 migrate->vma->vm_mm, migrate->start, migrate->end, 2491 migrate->pgmap_owner); 2492 mmu_notifier_invalidate_range_start(&range); 2493 2494 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end, 2495 &migrate_vma_walk_ops, migrate); 2496 2497 mmu_notifier_invalidate_range_end(&range); 2498 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); 2499 } 2500 2501 /* 2502 * migrate_vma_check_page() - check if page is pinned or not 2503 * @page: struct page to check 2504 * 2505 * Pinned pages cannot be migrated. This is the same test as in 2506 * migrate_page_move_mapping(), except that here we allow migration of a 2507 * ZONE_DEVICE page. 2508 */ 2509 static bool migrate_vma_check_page(struct page *page) 2510 { 2511 /* 2512 * One extra ref because caller holds an extra reference, either from 2513 * isolate_lru_page() for a regular page, or migrate_vma_collect() for 2514 * a device page. 2515 */ 2516 int extra = 1; 2517 2518 /* 2519 * FIXME support THP (transparent huge page), it is bit more complex to 2520 * check them than regular pages, because they can be mapped with a pmd 2521 * or with a pte (split pte mapping). 2522 */ 2523 if (PageCompound(page)) 2524 return false; 2525 2526 /* Page from ZONE_DEVICE have one extra reference */ 2527 if (is_zone_device_page(page)) { 2528 /* 2529 * Private page can never be pin as they have no valid pte and 2530 * GUP will fail for those. Yet if there is a pending migration 2531 * a thread might try to wait on the pte migration entry and 2532 * will bump the page reference count. Sadly there is no way to 2533 * differentiate a regular pin from migration wait. Hence to 2534 * avoid 2 racing thread trying to migrate back to CPU to enter 2535 * infinite loop (one stoping migration because the other is 2536 * waiting on pte migration entry). We always return true here. 2537 * 2538 * FIXME proper solution is to rework migration_entry_wait() so 2539 * it does not need to take a reference on page. 2540 */ 2541 return is_device_private_page(page); 2542 } 2543 2544 /* For file back page */ 2545 if (page_mapping(page)) 2546 extra += 1 + page_has_private(page); 2547 2548 if ((page_count(page) - extra) > page_mapcount(page)) 2549 return false; 2550 2551 return true; 2552 } 2553 2554 /* 2555 * migrate_vma_prepare() - lock pages and isolate them from the lru 2556 * @migrate: migrate struct containing all migration information 2557 * 2558 * This locks pages that have been collected by migrate_vma_collect(). Once each 2559 * page is locked it is isolated from the lru (for non-device pages). Finally, 2560 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be 2561 * migrated by concurrent kernel threads. 2562 */ 2563 static void migrate_vma_prepare(struct migrate_vma *migrate) 2564 { 2565 const unsigned long npages = migrate->npages; 2566 const unsigned long start = migrate->start; 2567 unsigned long addr, i, restore = 0; 2568 bool allow_drain = true; 2569 2570 lru_add_drain(); 2571 2572 for (i = 0; (i < npages) && migrate->cpages; i++) { 2573 struct page *page = migrate_pfn_to_page(migrate->src[i]); 2574 bool remap = true; 2575 2576 if (!page) 2577 continue; 2578 2579 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) { 2580 /* 2581 * Because we are migrating several pages there can be 2582 * a deadlock between 2 concurrent migration where each 2583 * are waiting on each other page lock. 2584 * 2585 * Make migrate_vma() a best effort thing and backoff 2586 * for any page we can not lock right away. 2587 */ 2588 if (!trylock_page(page)) { 2589 migrate->src[i] = 0; 2590 migrate->cpages--; 2591 put_page(page); 2592 continue; 2593 } 2594 remap = false; 2595 migrate->src[i] |= MIGRATE_PFN_LOCKED; 2596 } 2597 2598 /* ZONE_DEVICE pages are not on LRU */ 2599 if (!is_zone_device_page(page)) { 2600 if (!PageLRU(page) && allow_drain) { 2601 /* Drain CPU's pagevec */ 2602 lru_add_drain_all(); 2603 allow_drain = false; 2604 } 2605 2606 if (isolate_lru_page(page)) { 2607 if (remap) { 2608 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 2609 migrate->cpages--; 2610 restore++; 2611 } else { 2612 migrate->src[i] = 0; 2613 unlock_page(page); 2614 migrate->cpages--; 2615 put_page(page); 2616 } 2617 continue; 2618 } 2619 2620 /* Drop the reference we took in collect */ 2621 put_page(page); 2622 } 2623 2624 if (!migrate_vma_check_page(page)) { 2625 if (remap) { 2626 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 2627 migrate->cpages--; 2628 restore++; 2629 2630 if (!is_zone_device_page(page)) { 2631 get_page(page); 2632 putback_lru_page(page); 2633 } 2634 } else { 2635 migrate->src[i] = 0; 2636 unlock_page(page); 2637 migrate->cpages--; 2638 2639 if (!is_zone_device_page(page)) 2640 putback_lru_page(page); 2641 else 2642 put_page(page); 2643 } 2644 } 2645 } 2646 2647 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) { 2648 struct page *page = migrate_pfn_to_page(migrate->src[i]); 2649 2650 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) 2651 continue; 2652 2653 remove_migration_pte(page, migrate->vma, addr, page); 2654 2655 migrate->src[i] = 0; 2656 unlock_page(page); 2657 put_page(page); 2658 restore--; 2659 } 2660 } 2661 2662 /* 2663 * migrate_vma_unmap() - replace page mapping with special migration pte entry 2664 * @migrate: migrate struct containing all migration information 2665 * 2666 * Replace page mapping (CPU page table pte) with a special migration pte entry 2667 * and check again if it has been pinned. Pinned pages are restored because we 2668 * cannot migrate them. 2669 * 2670 * This is the last step before we call the device driver callback to allocate 2671 * destination memory and copy contents of original page over to new page. 2672 */ 2673 static void migrate_vma_unmap(struct migrate_vma *migrate) 2674 { 2675 int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS; 2676 const unsigned long npages = migrate->npages; 2677 const unsigned long start = migrate->start; 2678 unsigned long addr, i, restore = 0; 2679 2680 for (i = 0; i < npages; i++) { 2681 struct page *page = migrate_pfn_to_page(migrate->src[i]); 2682 2683 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE)) 2684 continue; 2685 2686 if (page_mapped(page)) { 2687 try_to_unmap(page, flags); 2688 if (page_mapped(page)) 2689 goto restore; 2690 } 2691 2692 if (migrate_vma_check_page(page)) 2693 continue; 2694 2695 restore: 2696 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 2697 migrate->cpages--; 2698 restore++; 2699 } 2700 2701 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) { 2702 struct page *page = migrate_pfn_to_page(migrate->src[i]); 2703 2704 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) 2705 continue; 2706 2707 remove_migration_ptes(page, page, false); 2708 2709 migrate->src[i] = 0; 2710 unlock_page(page); 2711 restore--; 2712 2713 if (is_zone_device_page(page)) 2714 put_page(page); 2715 else 2716 putback_lru_page(page); 2717 } 2718 } 2719 2720 /** 2721 * migrate_vma_setup() - prepare to migrate a range of memory 2722 * @args: contains the vma, start, and pfns arrays for the migration 2723 * 2724 * Returns: negative errno on failures, 0 when 0 or more pages were migrated 2725 * without an error. 2726 * 2727 * Prepare to migrate a range of memory virtual address range by collecting all 2728 * the pages backing each virtual address in the range, saving them inside the 2729 * src array. Then lock those pages and unmap them. Once the pages are locked 2730 * and unmapped, check whether each page is pinned or not. Pages that aren't 2731 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the 2732 * corresponding src array entry. Then restores any pages that are pinned, by 2733 * remapping and unlocking those pages. 2734 * 2735 * The caller should then allocate destination memory and copy source memory to 2736 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE 2737 * flag set). Once these are allocated and copied, the caller must update each 2738 * corresponding entry in the dst array with the pfn value of the destination 2739 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set 2740 * (destination pages must have their struct pages locked, via lock_page()). 2741 * 2742 * Note that the caller does not have to migrate all the pages that are marked 2743 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from 2744 * device memory to system memory. If the caller cannot migrate a device page 2745 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe 2746 * consequences for the userspace process, so it must be avoided if at all 2747 * possible. 2748 * 2749 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we 2750 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus 2751 * allowing the caller to allocate device memory for those unback virtual 2752 * address. For this the caller simply has to allocate device memory and 2753 * properly set the destination entry like for regular migration. Note that 2754 * this can still fails and thus inside the device driver must check if the 2755 * migration was successful for those entries after calling migrate_vma_pages() 2756 * just like for regular migration. 2757 * 2758 * After that, the callers must call migrate_vma_pages() to go over each entry 2759 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag 2760 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, 2761 * then migrate_vma_pages() to migrate struct page information from the source 2762 * struct page to the destination struct page. If it fails to migrate the 2763 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the 2764 * src array. 2765 * 2766 * At this point all successfully migrated pages have an entry in the src 2767 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst 2768 * array entry with MIGRATE_PFN_VALID flag set. 2769 * 2770 * Once migrate_vma_pages() returns the caller may inspect which pages were 2771 * successfully migrated, and which were not. Successfully migrated pages will 2772 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry. 2773 * 2774 * It is safe to update device page table after migrate_vma_pages() because 2775 * both destination and source page are still locked, and the mmap_lock is held 2776 * in read mode (hence no one can unmap the range being migrated). 2777 * 2778 * Once the caller is done cleaning up things and updating its page table (if it 2779 * chose to do so, this is not an obligation) it finally calls 2780 * migrate_vma_finalize() to update the CPU page table to point to new pages 2781 * for successfully migrated pages or otherwise restore the CPU page table to 2782 * point to the original source pages. 2783 */ 2784 int migrate_vma_setup(struct migrate_vma *args) 2785 { 2786 long nr_pages = (args->end - args->start) >> PAGE_SHIFT; 2787 2788 args->start &= PAGE_MASK; 2789 args->end &= PAGE_MASK; 2790 if (!args->vma || is_vm_hugetlb_page(args->vma) || 2791 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) 2792 return -EINVAL; 2793 if (nr_pages <= 0) 2794 return -EINVAL; 2795 if (args->start < args->vma->vm_start || 2796 args->start >= args->vma->vm_end) 2797 return -EINVAL; 2798 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) 2799 return -EINVAL; 2800 if (!args->src || !args->dst) 2801 return -EINVAL; 2802 2803 memset(args->src, 0, sizeof(*args->src) * nr_pages); 2804 args->cpages = 0; 2805 args->npages = 0; 2806 2807 migrate_vma_collect(args); 2808 2809 if (args->cpages) 2810 migrate_vma_prepare(args); 2811 if (args->cpages) 2812 migrate_vma_unmap(args); 2813 2814 /* 2815 * At this point pages are locked and unmapped, and thus they have 2816 * stable content and can safely be copied to destination memory that 2817 * is allocated by the drivers. 2818 */ 2819 return 0; 2820 2821 } 2822 EXPORT_SYMBOL(migrate_vma_setup); 2823 2824 /* 2825 * This code closely matches the code in: 2826 * __handle_mm_fault() 2827 * handle_pte_fault() 2828 * do_anonymous_page() 2829 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE 2830 * private page. 2831 */ 2832 static void migrate_vma_insert_page(struct migrate_vma *migrate, 2833 unsigned long addr, 2834 struct page *page, 2835 unsigned long *src, 2836 unsigned long *dst) 2837 { 2838 struct vm_area_struct *vma = migrate->vma; 2839 struct mm_struct *mm = vma->vm_mm; 2840 bool flush = false; 2841 spinlock_t *ptl; 2842 pte_t entry; 2843 pgd_t *pgdp; 2844 p4d_t *p4dp; 2845 pud_t *pudp; 2846 pmd_t *pmdp; 2847 pte_t *ptep; 2848 2849 /* Only allow populating anonymous memory */ 2850 if (!vma_is_anonymous(vma)) 2851 goto abort; 2852 2853 pgdp = pgd_offset(mm, addr); 2854 p4dp = p4d_alloc(mm, pgdp, addr); 2855 if (!p4dp) 2856 goto abort; 2857 pudp = pud_alloc(mm, p4dp, addr); 2858 if (!pudp) 2859 goto abort; 2860 pmdp = pmd_alloc(mm, pudp, addr); 2861 if (!pmdp) 2862 goto abort; 2863 2864 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp)) 2865 goto abort; 2866 2867 /* 2868 * Use pte_alloc() instead of pte_alloc_map(). We can't run 2869 * pte_offset_map() on pmds where a huge pmd might be created 2870 * from a different thread. 2871 * 2872 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when 2873 * parallel threads are excluded by other means. 2874 * 2875 * Here we only have mmap_read_lock(mm). 2876 */ 2877 if (pte_alloc(mm, pmdp)) 2878 goto abort; 2879 2880 /* See the comment in pte_alloc_one_map() */ 2881 if (unlikely(pmd_trans_unstable(pmdp))) 2882 goto abort; 2883 2884 if (unlikely(anon_vma_prepare(vma))) 2885 goto abort; 2886 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) 2887 goto abort; 2888 2889 /* 2890 * The memory barrier inside __SetPageUptodate makes sure that 2891 * preceding stores to the page contents become visible before 2892 * the set_pte_at() write. 2893 */ 2894 __SetPageUptodate(page); 2895 2896 if (is_zone_device_page(page)) { 2897 if (is_device_private_page(page)) { 2898 swp_entry_t swp_entry; 2899 2900 swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE); 2901 entry = swp_entry_to_pte(swp_entry); 2902 } 2903 } else { 2904 entry = mk_pte(page, vma->vm_page_prot); 2905 if (vma->vm_flags & VM_WRITE) 2906 entry = pte_mkwrite(pte_mkdirty(entry)); 2907 } 2908 2909 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); 2910 2911 if (check_stable_address_space(mm)) 2912 goto unlock_abort; 2913 2914 if (pte_present(*ptep)) { 2915 unsigned long pfn = pte_pfn(*ptep); 2916 2917 if (!is_zero_pfn(pfn)) 2918 goto unlock_abort; 2919 flush = true; 2920 } else if (!pte_none(*ptep)) 2921 goto unlock_abort; 2922 2923 /* 2924 * Check for userfaultfd but do not deliver the fault. Instead, 2925 * just back off. 2926 */ 2927 if (userfaultfd_missing(vma)) 2928 goto unlock_abort; 2929 2930 inc_mm_counter(mm, MM_ANONPAGES); 2931 page_add_new_anon_rmap(page, vma, addr, false); 2932 if (!is_zone_device_page(page)) 2933 lru_cache_add_inactive_or_unevictable(page, vma); 2934 get_page(page); 2935 2936 if (flush) { 2937 flush_cache_page(vma, addr, pte_pfn(*ptep)); 2938 ptep_clear_flush_notify(vma, addr, ptep); 2939 set_pte_at_notify(mm, addr, ptep, entry); 2940 update_mmu_cache(vma, addr, ptep); 2941 } else { 2942 /* No need to invalidate - it was non-present before */ 2943 set_pte_at(mm, addr, ptep, entry); 2944 update_mmu_cache(vma, addr, ptep); 2945 } 2946 2947 pte_unmap_unlock(ptep, ptl); 2948 *src = MIGRATE_PFN_MIGRATE; 2949 return; 2950 2951 unlock_abort: 2952 pte_unmap_unlock(ptep, ptl); 2953 abort: 2954 *src &= ~MIGRATE_PFN_MIGRATE; 2955 } 2956 2957 /** 2958 * migrate_vma_pages() - migrate meta-data from src page to dst page 2959 * @migrate: migrate struct containing all migration information 2960 * 2961 * This migrates struct page meta-data from source struct page to destination 2962 * struct page. This effectively finishes the migration from source page to the 2963 * destination page. 2964 */ 2965 void migrate_vma_pages(struct migrate_vma *migrate) 2966 { 2967 const unsigned long npages = migrate->npages; 2968 const unsigned long start = migrate->start; 2969 struct mmu_notifier_range range; 2970 unsigned long addr, i; 2971 bool notified = false; 2972 2973 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { 2974 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); 2975 struct page *page = migrate_pfn_to_page(migrate->src[i]); 2976 struct address_space *mapping; 2977 int r; 2978 2979 if (!newpage) { 2980 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 2981 continue; 2982 } 2983 2984 if (!page) { 2985 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) 2986 continue; 2987 if (!notified) { 2988 notified = true; 2989 2990 mmu_notifier_range_init(&range, 2991 MMU_NOTIFY_CLEAR, 0, 2992 NULL, 2993 migrate->vma->vm_mm, 2994 addr, migrate->end); 2995 mmu_notifier_invalidate_range_start(&range); 2996 } 2997 migrate_vma_insert_page(migrate, addr, newpage, 2998 &migrate->src[i], 2999 &migrate->dst[i]); 3000 continue; 3001 } 3002 3003 mapping = page_mapping(page); 3004 3005 if (is_zone_device_page(newpage)) { 3006 if (is_device_private_page(newpage)) { 3007 /* 3008 * For now only support private anonymous when 3009 * migrating to un-addressable device memory. 3010 */ 3011 if (mapping) { 3012 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 3013 continue; 3014 } 3015 } else { 3016 /* 3017 * Other types of ZONE_DEVICE page are not 3018 * supported. 3019 */ 3020 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 3021 continue; 3022 } 3023 } 3024 3025 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); 3026 if (r != MIGRATEPAGE_SUCCESS) 3027 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; 3028 } 3029 3030 /* 3031 * No need to double call mmu_notifier->invalidate_range() callback as 3032 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page() 3033 * did already call it. 3034 */ 3035 if (notified) 3036 mmu_notifier_invalidate_range_only_end(&range); 3037 } 3038 EXPORT_SYMBOL(migrate_vma_pages); 3039 3040 /** 3041 * migrate_vma_finalize() - restore CPU page table entry 3042 * @migrate: migrate struct containing all migration information 3043 * 3044 * This replaces the special migration pte entry with either a mapping to the 3045 * new page if migration was successful for that page, or to the original page 3046 * otherwise. 3047 * 3048 * This also unlocks the pages and puts them back on the lru, or drops the extra 3049 * refcount, for device pages. 3050 */ 3051 void migrate_vma_finalize(struct migrate_vma *migrate) 3052 { 3053 const unsigned long npages = migrate->npages; 3054 unsigned long i; 3055 3056 for (i = 0; i < npages; i++) { 3057 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); 3058 struct page *page = migrate_pfn_to_page(migrate->src[i]); 3059 3060 if (!page) { 3061 if (newpage) { 3062 unlock_page(newpage); 3063 put_page(newpage); 3064 } 3065 continue; 3066 } 3067 3068 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { 3069 if (newpage) { 3070 unlock_page(newpage); 3071 put_page(newpage); 3072 } 3073 newpage = page; 3074 } 3075 3076 remove_migration_ptes(page, newpage, false); 3077 unlock_page(page); 3078 migrate->cpages--; 3079 3080 if (is_zone_device_page(page)) 3081 put_page(page); 3082 else 3083 putback_lru_page(page); 3084 3085 if (newpage != page) { 3086 unlock_page(newpage); 3087 if (is_zone_device_page(newpage)) 3088 put_page(newpage); 3089 else 3090 putback_lru_page(newpage); 3091 } 3092 } 3093 } 3094 EXPORT_SYMBOL(migrate_vma_finalize); 3095 #endif /* CONFIG_DEVICE_PRIVATE */ 3096