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