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/leafops.h> 20 #include <linux/pagemap.h> 21 #include <linux/buffer_head.h> 22 #include <linux/mm_inline.h> 23 #include <linux/ksm.h> 24 #include <linux/rmap.h> 25 #include <linux/topology.h> 26 #include <linux/cpu.h> 27 #include <linux/cpuset.h> 28 #include <linux/writeback.h> 29 #include <linux/mempolicy.h> 30 #include <linux/vmalloc.h> 31 #include <linux/security.h> 32 #include <linux/backing-dev.h> 33 #include <linux/compaction.h> 34 #include <linux/syscalls.h> 35 #include <linux/compat.h> 36 #include <linux/hugetlb.h> 37 #include <linux/gfp.h> 38 #include <linux/page_idle.h> 39 #include <linux/page_owner.h> 40 #include <linux/sched/mm.h> 41 #include <linux/ptrace.h> 42 #include <linux/memory.h> 43 #include <linux/sched/sysctl.h> 44 #include <linux/memory-tiers.h> 45 #include <linux/pagewalk.h> 46 47 #include <asm/tlbflush.h> 48 49 #include <trace/events/migrate.h> 50 51 #include "internal.h" 52 #include "swap.h" 53 54 static const struct movable_operations *offline_movable_ops; 55 static const struct movable_operations *zsmalloc_movable_ops; 56 57 int set_movable_ops(const struct movable_operations *ops, enum pagetype type) 58 { 59 /* 60 * We only allow for selected types and don't handle concurrent 61 * registration attempts yet. 62 */ 63 switch (type) { 64 case PGTY_offline: 65 if (offline_movable_ops && ops) 66 return -EBUSY; 67 offline_movable_ops = ops; 68 break; 69 case PGTY_zsmalloc: 70 if (zsmalloc_movable_ops && ops) 71 return -EBUSY; 72 zsmalloc_movable_ops = ops; 73 break; 74 default: 75 return -EINVAL; 76 } 77 return 0; 78 } 79 EXPORT_SYMBOL_GPL(set_movable_ops); 80 81 static const struct movable_operations *page_movable_ops(struct page *page) 82 { 83 VM_WARN_ON_ONCE_PAGE(!page_has_movable_ops(page), page); 84 85 /* 86 * If we enable page migration for a page of a certain type by marking 87 * it as movable, the page type must be sticky until the page gets freed 88 * back to the buddy. 89 */ 90 if (PageOffline(page)) 91 /* Only balloon page migration sets PageOffline pages movable. */ 92 return offline_movable_ops; 93 if (PageZsmalloc(page)) 94 return zsmalloc_movable_ops; 95 96 return NULL; 97 } 98 99 /** 100 * isolate_movable_ops_page - isolate a movable_ops page for migration 101 * @page: The page. 102 * @mode: The isolation mode. 103 * 104 * Try to isolate a movable_ops page for migration. Will fail if the page is 105 * not a movable_ops page, if the page is already isolated for migration 106 * or if the page was just was released by its owner. 107 * 108 * Once isolated, the page cannot get freed until it is either putback 109 * or migrated. 110 * 111 * Returns true if isolation succeeded, otherwise false. 112 */ 113 bool isolate_movable_ops_page(struct page *page, isolate_mode_t mode) 114 { 115 /* 116 * TODO: these pages will not be folios in the future. All 117 * folio dependencies will have to be removed. 118 */ 119 struct folio *folio = folio_get_nontail_page(page); 120 const struct movable_operations *mops; 121 122 /* 123 * Avoid burning cycles with pages that are yet under __free_pages(), 124 * or just got freed under us. 125 * 126 * In case we 'win' a race for a movable page being freed under us and 127 * raise its refcount preventing __free_pages() from doing its job 128 * the put_page() at the end of this block will take care of 129 * release this page, thus avoiding a nasty leakage. 130 */ 131 if (!folio) 132 goto out; 133 134 /* 135 * Check for movable_ops pages before taking the page lock because 136 * we use non-atomic bitops on newly allocated page flags so 137 * unconditionally grabbing the lock ruins page's owner side. 138 * 139 * Note that once a page has movable_ops, it will stay that way 140 * until the page was freed. 141 */ 142 if (unlikely(!page_has_movable_ops(page))) 143 goto out_putfolio; 144 145 /* 146 * As movable pages are not isolated from LRU lists, concurrent 147 * compaction threads can race against page migration functions 148 * as well as race against the releasing a page. 149 * 150 * In order to avoid having an already isolated movable page 151 * being (wrongly) re-isolated while it is under migration, 152 * or to avoid attempting to isolate pages being released, 153 * lets be sure we have the page lock 154 * before proceeding with the movable page isolation steps. 155 */ 156 if (unlikely(!folio_trylock(folio))) 157 goto out_putfolio; 158 159 VM_WARN_ON_ONCE_PAGE(!page_has_movable_ops(page), page); 160 if (PageMovableOpsIsolated(page)) 161 goto out_no_isolated; 162 163 mops = page_movable_ops(page); 164 if (WARN_ON_ONCE(!mops)) 165 goto out_no_isolated; 166 167 if (!mops->isolate_page(page, mode)) 168 goto out_no_isolated; 169 170 /* Driver shouldn't use the isolated flag */ 171 VM_WARN_ON_ONCE_PAGE(PageMovableOpsIsolated(page), page); 172 SetPageMovableOpsIsolated(page); 173 folio_unlock(folio); 174 175 return true; 176 177 out_no_isolated: 178 folio_unlock(folio); 179 out_putfolio: 180 folio_put(folio); 181 out: 182 return false; 183 } 184 185 /** 186 * putback_movable_ops_page - putback an isolated movable_ops page 187 * @page: The isolated page. 188 * 189 * Putback an isolated movable_ops page. 190 * 191 * After the page was putback, it might get freed instantly. 192 */ 193 static void putback_movable_ops_page(struct page *page) 194 { 195 /* 196 * TODO: these pages will not be folios in the future. All 197 * folio dependencies will have to be removed. 198 */ 199 struct folio *folio = page_folio(page); 200 201 VM_WARN_ON_ONCE_PAGE(!page_has_movable_ops(page), page); 202 VM_WARN_ON_ONCE_PAGE(!PageMovableOpsIsolated(page), page); 203 folio_lock(folio); 204 page_movable_ops(page)->putback_page(page); 205 ClearPageMovableOpsIsolated(page); 206 folio_unlock(folio); 207 folio_put(folio); 208 } 209 210 /** 211 * migrate_movable_ops_page - migrate an isolated movable_ops page 212 * @dst: The destination page. 213 * @src: The source page. 214 * @mode: The migration mode. 215 * 216 * Migrate an isolated movable_ops page. 217 * 218 * If the src page was already released by its owner, the src page is 219 * un-isolated (putback) and migration succeeds; the migration core will be the 220 * owner of both pages. 221 * 222 * If the src page was not released by its owner and the migration was 223 * successful, the owner of the src page and the dst page are swapped and 224 * the src page is un-isolated. 225 * 226 * If migration fails, the ownership stays unmodified and the src page 227 * remains isolated: migration may be retried later or the page can be putback. 228 * 229 * TODO: migration core will treat both pages as folios and lock them before 230 * this call to unlock them after this call. Further, the folio refcounts on 231 * src and dst are also released by migration core. These pages will not be 232 * folios in the future, so that must be reworked. 233 * 234 * Returns 0 on success, otherwise a negative error code. 235 */ 236 static int migrate_movable_ops_page(struct page *dst, struct page *src, 237 enum migrate_mode mode) 238 { 239 int rc; 240 241 VM_WARN_ON_ONCE_PAGE(!page_has_movable_ops(src), src); 242 VM_WARN_ON_ONCE_PAGE(!PageMovableOpsIsolated(src), src); 243 rc = page_movable_ops(src)->migrate_page(dst, src, mode); 244 if (!rc) 245 ClearPageMovableOpsIsolated(src); 246 return rc; 247 } 248 249 /* 250 * Put previously isolated pages back onto the appropriate lists 251 * from where they were once taken off for compaction/migration. 252 * 253 * This function shall be used whenever the isolated pageset has been 254 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() 255 * and folio_isolate_hugetlb(). 256 */ 257 void putback_movable_pages(struct list_head *l) 258 { 259 struct folio *folio; 260 struct folio *folio2; 261 262 list_for_each_entry_safe(folio, folio2, l, lru) { 263 if (unlikely(folio_test_hugetlb(folio))) { 264 folio_putback_hugetlb(folio); 265 continue; 266 } 267 list_del(&folio->lru); 268 if (unlikely(page_has_movable_ops(&folio->page))) { 269 putback_movable_ops_page(&folio->page); 270 } else { 271 node_stat_mod_folio(folio, NR_ISOLATED_ANON + 272 folio_is_file_lru(folio), -folio_nr_pages(folio)); 273 folio_putback_lru(folio); 274 } 275 } 276 } 277 278 /* Must be called with an elevated refcount on the non-hugetlb folio */ 279 bool isolate_folio_to_list(struct folio *folio, struct list_head *list) 280 { 281 if (folio_test_hugetlb(folio)) 282 return folio_isolate_hugetlb(folio, list); 283 284 if (page_has_movable_ops(&folio->page)) { 285 if (!isolate_movable_ops_page(&folio->page, 286 ISOLATE_UNEVICTABLE)) 287 return false; 288 } else { 289 if (!folio_isolate_lru(folio)) 290 return false; 291 node_stat_add_folio(folio, NR_ISOLATED_ANON + 292 folio_is_file_lru(folio)); 293 } 294 list_add(&folio->lru, list); 295 return true; 296 } 297 298 static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw, 299 struct folio *folio, pte_t old_pte, unsigned long idx) 300 { 301 struct page *page = folio_page(folio, idx); 302 pte_t newpte; 303 304 if (PageCompound(page) || PageHWPoison(page)) 305 return false; 306 307 VM_BUG_ON_PAGE(!PageAnon(page), page); 308 VM_BUG_ON_PAGE(!PageLocked(page), page); 309 VM_BUG_ON_PAGE(pte_present(old_pte), page); 310 VM_WARN_ON_ONCE_FOLIO(folio_is_device_private(folio), folio); 311 312 if (folio_test_mlocked(folio) || (pvmw->vma->vm_flags & VM_LOCKED) || 313 mm_forbids_zeropage(pvmw->vma->vm_mm)) 314 return false; 315 316 /* 317 * The pmd entry mapping the old thp was flushed and the pte mapping 318 * this subpage has been non present. If the subpage is only zero-filled 319 * then map it to the shared zeropage. 320 */ 321 if (!pages_identical(page, ZERO_PAGE(0))) 322 return false; 323 324 newpte = pte_mkspecial(pfn_pte(zero_pfn(pvmw->address), 325 pvmw->vma->vm_page_prot)); 326 327 if (pte_swp_soft_dirty(old_pte)) 328 newpte = pte_mksoft_dirty(newpte); 329 if (pte_swp_uffd_wp(old_pte)) 330 newpte = pte_mkuffd_wp(newpte); 331 332 set_pte_at(pvmw->vma->vm_mm, pvmw->address, pvmw->pte, newpte); 333 334 dec_mm_counter(pvmw->vma->vm_mm, mm_counter(folio)); 335 return true; 336 } 337 338 struct rmap_walk_arg { 339 struct folio *folio; 340 bool map_unused_to_zeropage; 341 }; 342 343 /* 344 * Restore a potential migration pte to a working pte entry 345 */ 346 static bool remove_migration_pte(struct folio *folio, 347 struct vm_area_struct *vma, unsigned long addr, void *arg) 348 { 349 struct rmap_walk_arg *rmap_walk_arg = arg; 350 DEFINE_FOLIO_VMA_WALK(pvmw, rmap_walk_arg->folio, vma, addr, PVMW_SYNC | PVMW_MIGRATION); 351 352 while (page_vma_mapped_walk(&pvmw)) { 353 rmap_t rmap_flags = RMAP_NONE; 354 pte_t old_pte; 355 pte_t pte; 356 softleaf_t entry; 357 struct page *new; 358 unsigned long idx = 0; 359 360 /* pgoff is invalid for ksm pages, but they are never large */ 361 if (folio_test_large(folio) && !folio_test_hugetlb(folio)) 362 idx = linear_page_index(vma, pvmw.address) - pvmw.pgoff; 363 new = folio_page(folio, idx); 364 365 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 366 /* PMD-mapped THP migration entry */ 367 if (!pvmw.pte) { 368 VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) || 369 !folio_test_pmd_mappable(folio), folio); 370 remove_migration_pmd(&pvmw, new); 371 continue; 372 } 373 #endif 374 old_pte = ptep_get(pvmw.pte); 375 if (rmap_walk_arg->map_unused_to_zeropage && 376 try_to_map_unused_to_zeropage(&pvmw, folio, old_pte, idx)) 377 continue; 378 379 folio_get(folio); 380 pte = mk_pte(new, READ_ONCE(vma->vm_page_prot)); 381 382 entry = softleaf_from_pte(old_pte); 383 if (!softleaf_is_migration_young(entry)) 384 pte = pte_mkold(pte); 385 if (folio_test_dirty(folio) && softleaf_is_migration_dirty(entry)) 386 pte = pte_mkdirty(pte); 387 if (pte_swp_soft_dirty(old_pte)) 388 pte = pte_mksoft_dirty(pte); 389 else 390 pte = pte_clear_soft_dirty(pte); 391 392 if (softleaf_is_migration_write(entry)) 393 pte = pte_mkwrite(pte, vma); 394 else if (pte_swp_uffd_wp(old_pte)) 395 pte = pte_mkuffd_wp(pte); 396 397 if (folio_test_anon(folio) && !softleaf_is_migration_read(entry)) 398 rmap_flags |= RMAP_EXCLUSIVE; 399 400 if (unlikely(is_device_private_page(new))) { 401 if (pte_write(pte)) 402 entry = make_writable_device_private_entry( 403 page_to_pfn(new)); 404 else 405 entry = make_readable_device_private_entry( 406 page_to_pfn(new)); 407 pte = softleaf_to_pte(entry); 408 if (pte_swp_soft_dirty(old_pte)) 409 pte = pte_swp_mksoft_dirty(pte); 410 if (pte_swp_uffd_wp(old_pte)) 411 pte = pte_swp_mkuffd_wp(pte); 412 } 413 414 #ifdef CONFIG_HUGETLB_PAGE 415 if (folio_test_hugetlb(folio)) { 416 struct hstate *h = hstate_vma(vma); 417 unsigned int shift = huge_page_shift(h); 418 unsigned long psize = huge_page_size(h); 419 420 pte = arch_make_huge_pte(pte, shift, vma->vm_flags); 421 if (folio_test_anon(folio)) 422 hugetlb_add_anon_rmap(folio, vma, pvmw.address, 423 rmap_flags); 424 else 425 hugetlb_add_file_rmap(folio); 426 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte, 427 psize); 428 } else 429 #endif 430 { 431 if (folio_test_anon(folio)) 432 folio_add_anon_rmap_pte(folio, new, vma, 433 pvmw.address, rmap_flags); 434 else 435 folio_add_file_rmap_pte(folio, new, vma); 436 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); 437 } 438 if (READ_ONCE(vma->vm_flags) & VM_LOCKED) 439 mlock_drain_local(); 440 441 trace_remove_migration_pte(pvmw.address, pte_val(pte), 442 compound_order(new)); 443 444 /* No need to invalidate - it was non-present before */ 445 update_mmu_cache(vma, pvmw.address, pvmw.pte); 446 } 447 448 return true; 449 } 450 451 /* 452 * Get rid of all migration entries and replace them by 453 * references to the indicated page. 454 */ 455 void remove_migration_ptes(struct folio *src, struct folio *dst, 456 enum ttu_flags flags) 457 { 458 struct rmap_walk_arg rmap_walk_arg = { 459 .folio = src, 460 .map_unused_to_zeropage = flags & TTU_USE_SHARED_ZEROPAGE, 461 }; 462 463 struct rmap_walk_control rwc = { 464 .rmap_one = remove_migration_pte, 465 .arg = &rmap_walk_arg, 466 }; 467 468 VM_BUG_ON_FOLIO((flags & TTU_USE_SHARED_ZEROPAGE) && (src != dst), src); 469 470 if (flags & TTU_RMAP_LOCKED) 471 rmap_walk_locked(dst, &rwc); 472 else 473 rmap_walk(dst, &rwc); 474 } 475 476 /* 477 * Something used the pte of a page under migration. We need to 478 * get to the page and wait until migration is finished. 479 * When we return from this function the fault will be retried. 480 */ 481 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, 482 unsigned long address) 483 { 484 spinlock_t *ptl; 485 pte_t *ptep; 486 pte_t pte; 487 softleaf_t entry; 488 489 ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 490 if (!ptep) 491 return; 492 493 pte = ptep_get(ptep); 494 pte_unmap(ptep); 495 496 if (pte_none(pte) || pte_present(pte)) 497 goto out; 498 499 entry = softleaf_from_pte(pte); 500 if (!softleaf_is_migration(entry)) 501 goto out; 502 503 softleaf_entry_wait_on_locked(entry, ptl); 504 return; 505 out: 506 spin_unlock(ptl); 507 } 508 509 #ifdef CONFIG_HUGETLB_PAGE 510 /* 511 * The vma read lock must be held upon entry. Holding that lock prevents either 512 * the pte or the ptl from being freed. 513 * 514 * This function will release the vma lock before returning. 515 */ 516 void migration_entry_wait_huge(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) 517 { 518 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, ptep); 519 softleaf_t entry; 520 pte_t pte; 521 522 hugetlb_vma_assert_locked(vma); 523 spin_lock(ptl); 524 pte = huge_ptep_get(vma->vm_mm, addr, ptep); 525 526 if (huge_pte_none(pte)) 527 goto fail; 528 529 entry = softleaf_from_pte(pte); 530 if (softleaf_is_migration(entry)) { 531 /* 532 * If migration entry existed, safe to release vma lock 533 * here because the pgtable page won't be freed without the 534 * pgtable lock released. See comment right above pgtable 535 * lock release in softleaf_entry_wait_on_locked(). 536 */ 537 hugetlb_vma_unlock_read(vma); 538 softleaf_entry_wait_on_locked(entry, ptl); 539 return; 540 } 541 542 fail: 543 spin_unlock(ptl); 544 hugetlb_vma_unlock_read(vma); 545 } 546 #endif 547 548 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 549 void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) 550 { 551 spinlock_t *ptl; 552 553 ptl = pmd_lock(mm, pmd); 554 if (!pmd_is_migration_entry(*pmd)) 555 goto unlock; 556 softleaf_entry_wait_on_locked(softleaf_from_pmd(*pmd), ptl); 557 return; 558 unlock: 559 spin_unlock(ptl); 560 } 561 #endif 562 563 /* 564 * Replace the folio in the mapping. 565 * 566 * The number of remaining references must be: 567 * 1 for anonymous folios without a mapping 568 * 2 for folios with a mapping 569 * 3 for folios with a mapping and the private flag set. 570 */ 571 static int __folio_migrate_mapping(struct address_space *mapping, 572 struct folio *newfolio, struct folio *folio, int expected_count) 573 { 574 XA_STATE(xas, &mapping->i_pages, folio->index); 575 struct swap_cluster_info *ci = NULL; 576 struct zone *oldzone, *newzone; 577 int dirty; 578 long nr = folio_nr_pages(folio); 579 580 if (!mapping) { 581 /* Take off deferred split queue while frozen and memcg set */ 582 if (folio_test_large(folio) && 583 folio_test_large_rmappable(folio)) { 584 if (!folio_ref_freeze(folio, expected_count)) 585 return -EAGAIN; 586 folio_unqueue_deferred_split(folio); 587 folio_ref_unfreeze(folio, expected_count); 588 } 589 590 /* No turning back from here */ 591 newfolio->index = folio->index; 592 newfolio->mapping = folio->mapping; 593 if (folio_test_anon(folio) && folio_test_large(folio) && 594 !folio_test_hugetlb(folio)) 595 mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON, 1); 596 if (folio_test_swapbacked(folio)) 597 __folio_set_swapbacked(newfolio); 598 599 return 0; 600 } 601 602 oldzone = folio_zone(folio); 603 newzone = folio_zone(newfolio); 604 605 if (folio_test_swapcache(folio)) 606 ci = swap_cluster_get_and_lock_irq(folio); 607 else 608 xas_lock_irq(&xas); 609 610 if (!folio_ref_freeze(folio, expected_count)) { 611 if (ci) 612 swap_cluster_unlock_irq(ci); 613 else 614 xas_unlock_irq(&xas); 615 return -EAGAIN; 616 } 617 618 /* Take off deferred split queue while frozen and memcg set */ 619 folio_unqueue_deferred_split(folio); 620 621 /* 622 * Now we know that no one else is looking at the folio: 623 * no turning back from here. 624 */ 625 newfolio->index = folio->index; 626 newfolio->mapping = folio->mapping; 627 if (folio_test_anon(folio) && folio_test_large(folio)) 628 mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON, 1); 629 folio_ref_add(newfolio, nr); /* add cache reference */ 630 if (folio_test_swapbacked(folio)) 631 __folio_set_swapbacked(newfolio); 632 if (folio_test_swapcache(folio)) { 633 folio_set_swapcache(newfolio); 634 newfolio->private = folio_get_private(folio); 635 } 636 637 /* Move dirty while folio refs frozen and newfolio not yet exposed */ 638 dirty = folio_test_dirty(folio); 639 if (dirty) { 640 folio_clear_dirty(folio); 641 folio_set_dirty(newfolio); 642 } 643 644 if (folio_test_swapcache(folio)) 645 __swap_cache_replace_folio(ci, folio, newfolio); 646 else 647 xas_store(&xas, newfolio); 648 649 /* 650 * Drop cache reference from old folio by unfreezing 651 * to one less reference. 652 * We know this isn't the last reference. 653 */ 654 folio_ref_unfreeze(folio, expected_count - nr); 655 656 /* Leave irq disabled to prevent preemption while updating stats */ 657 if (ci) 658 swap_cluster_unlock(ci); 659 else 660 xas_unlock(&xas); 661 662 /* 663 * If moved to a different zone then also account 664 * the folio for that zone. Other VM counters will be 665 * taken care of when we establish references to the 666 * new folio and drop references to the old folio. 667 * 668 * Note that anonymous folios are accounted for 669 * via NR_FILE_PAGES and NR_ANON_MAPPED if they 670 * are mapped to swap space. 671 */ 672 if (newzone != oldzone) { 673 struct lruvec *old_lruvec, *new_lruvec; 674 struct mem_cgroup *memcg; 675 676 rcu_read_lock(); 677 memcg = folio_memcg(folio); 678 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat); 679 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat); 680 681 mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr); 682 mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr); 683 if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) { 684 mod_lruvec_state(old_lruvec, NR_SHMEM, -nr); 685 mod_lruvec_state(new_lruvec, NR_SHMEM, nr); 686 687 if (folio_test_pmd_mappable(folio)) { 688 mod_lruvec_state(old_lruvec, NR_SHMEM_THPS, -nr); 689 mod_lruvec_state(new_lruvec, NR_SHMEM_THPS, nr); 690 } 691 } 692 #ifdef CONFIG_SWAP 693 if (folio_test_swapcache(folio)) { 694 mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr); 695 mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr); 696 } 697 #endif 698 if (dirty && mapping_can_writeback(mapping)) { 699 mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr); 700 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr); 701 mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr); 702 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr); 703 } 704 rcu_read_unlock(); 705 } 706 local_irq_enable(); 707 708 return 0; 709 } 710 711 int folio_migrate_mapping(struct address_space *mapping, 712 struct folio *newfolio, struct folio *folio, int extra_count) 713 { 714 int expected_count = folio_expected_ref_count(folio) + extra_count + 1; 715 716 if (folio_ref_count(folio) != expected_count) 717 return -EAGAIN; 718 719 return __folio_migrate_mapping(mapping, newfolio, folio, expected_count); 720 } 721 EXPORT_SYMBOL(folio_migrate_mapping); 722 723 /* 724 * The expected number of remaining references is the same as that 725 * of folio_migrate_mapping(). 726 */ 727 int migrate_huge_page_move_mapping(struct address_space *mapping, 728 struct folio *dst, struct folio *src) 729 { 730 XA_STATE(xas, &mapping->i_pages, src->index); 731 int rc, expected_count = folio_expected_ref_count(src) + 1; 732 733 if (folio_ref_count(src) != expected_count) 734 return -EAGAIN; 735 736 rc = folio_mc_copy(dst, src); 737 if (unlikely(rc)) 738 return rc; 739 740 xas_lock_irq(&xas); 741 if (!folio_ref_freeze(src, expected_count)) { 742 xas_unlock_irq(&xas); 743 return -EAGAIN; 744 } 745 746 dst->index = src->index; 747 dst->mapping = src->mapping; 748 749 folio_ref_add(dst, folio_nr_pages(dst)); 750 751 xas_store(&xas, dst); 752 753 folio_ref_unfreeze(src, expected_count - folio_nr_pages(src)); 754 755 xas_unlock_irq(&xas); 756 757 return 0; 758 } 759 760 /* 761 * Copy the flags and some other ancillary information 762 */ 763 void folio_migrate_flags(struct folio *newfolio, struct folio *folio) 764 { 765 int cpupid; 766 767 if (folio_test_referenced(folio)) 768 folio_set_referenced(newfolio); 769 if (folio_test_uptodate(folio)) 770 folio_mark_uptodate(newfolio); 771 if (folio_test_clear_active(folio)) { 772 VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio); 773 folio_set_active(newfolio); 774 } else if (folio_test_clear_unevictable(folio)) 775 folio_set_unevictable(newfolio); 776 if (folio_test_workingset(folio)) 777 folio_set_workingset(newfolio); 778 if (folio_test_checked(folio)) 779 folio_set_checked(newfolio); 780 /* 781 * PG_anon_exclusive (-> PG_mappedtodisk) is always migrated via 782 * migration entries. We can still have PG_anon_exclusive set on an 783 * effectively unmapped and unreferenced first sub-pages of an 784 * anonymous THP: we can simply copy it here via PG_mappedtodisk. 785 */ 786 if (folio_test_mappedtodisk(folio)) 787 folio_set_mappedtodisk(newfolio); 788 789 /* Move dirty on pages not done by folio_migrate_mapping() */ 790 if (folio_test_dirty(folio)) 791 folio_set_dirty(newfolio); 792 793 if (folio_test_young(folio)) 794 folio_set_young(newfolio); 795 if (folio_test_idle(folio)) 796 folio_set_idle(newfolio); 797 798 folio_migrate_refs(newfolio, folio); 799 /* 800 * Copy NUMA information to the new page, to prevent over-eager 801 * future migrations of this same page. 802 */ 803 cpupid = folio_xchg_last_cpupid(folio, -1); 804 /* 805 * For memory tiering mode, when migrate between slow and fast 806 * memory node, reset cpupid, because that is used to record 807 * page access time in slow memory node. 808 */ 809 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) { 810 bool f_toptier = node_is_toptier(folio_nid(folio)); 811 bool t_toptier = node_is_toptier(folio_nid(newfolio)); 812 813 if (f_toptier != t_toptier) 814 cpupid = -1; 815 } 816 folio_xchg_last_cpupid(newfolio, cpupid); 817 818 folio_migrate_ksm(newfolio, folio); 819 /* 820 * Please do not reorder this without considering how mm/ksm.c's 821 * ksm_get_folio() depends upon ksm_migrate_page() and the 822 * swapcache flag. 823 */ 824 if (folio_test_swapcache(folio)) 825 folio_clear_swapcache(folio); 826 folio_clear_private(folio); 827 828 /* page->private contains hugetlb specific flags */ 829 if (!folio_test_hugetlb(folio)) 830 folio->private = NULL; 831 832 /* 833 * If any waiters have accumulated on the new page then 834 * wake them up. 835 */ 836 if (folio_test_writeback(newfolio)) 837 folio_end_writeback(newfolio); 838 839 /* 840 * PG_readahead shares the same bit with PG_reclaim. The above 841 * end_page_writeback() may clear PG_readahead mistakenly, so set the 842 * bit after that. 843 */ 844 if (folio_test_readahead(folio)) 845 folio_set_readahead(newfolio); 846 847 folio_copy_owner(newfolio, folio); 848 pgalloc_tag_swap(newfolio, folio); 849 850 mem_cgroup_migrate(folio, newfolio); 851 } 852 EXPORT_SYMBOL(folio_migrate_flags); 853 854 /************************************************************ 855 * Migration functions 856 ***********************************************************/ 857 858 static int __migrate_folio(struct address_space *mapping, struct folio *dst, 859 struct folio *src, void *src_private, 860 enum migrate_mode mode) 861 { 862 int rc, expected_count = folio_expected_ref_count(src) + 1; 863 864 /* Check whether src does not have extra refs before we do more work */ 865 if (folio_ref_count(src) != expected_count) 866 return -EAGAIN; 867 868 rc = folio_mc_copy(dst, src); 869 if (unlikely(rc)) 870 return rc; 871 872 rc = __folio_migrate_mapping(mapping, dst, src, expected_count); 873 if (rc) 874 return rc; 875 876 if (src_private) 877 folio_attach_private(dst, folio_detach_private(src)); 878 879 folio_migrate_flags(dst, src); 880 return 0; 881 } 882 883 /** 884 * migrate_folio() - Simple folio migration. 885 * @mapping: The address_space containing the folio. 886 * @dst: The folio to migrate the data to. 887 * @src: The folio containing the current data. 888 * @mode: How to migrate the page. 889 * 890 * Common logic to directly migrate a single LRU folio suitable for 891 * folios that do not have private data. 892 * 893 * Folios are locked upon entry and exit. 894 */ 895 int migrate_folio(struct address_space *mapping, struct folio *dst, 896 struct folio *src, enum migrate_mode mode) 897 { 898 BUG_ON(folio_test_writeback(src)); /* Writeback must be complete */ 899 return __migrate_folio(mapping, dst, src, NULL, mode); 900 } 901 EXPORT_SYMBOL(migrate_folio); 902 903 #ifdef CONFIG_BUFFER_HEAD 904 /* Returns true if all buffers are successfully locked */ 905 static bool buffer_migrate_lock_buffers(struct buffer_head *head, 906 enum migrate_mode mode) 907 { 908 struct buffer_head *bh = head; 909 struct buffer_head *failed_bh; 910 911 do { 912 if (!trylock_buffer(bh)) { 913 if (mode == MIGRATE_ASYNC) 914 goto unlock; 915 if (mode == MIGRATE_SYNC_LIGHT && !buffer_uptodate(bh)) 916 goto unlock; 917 lock_buffer(bh); 918 } 919 920 bh = bh->b_this_page; 921 } while (bh != head); 922 923 return true; 924 925 unlock: 926 /* We failed to lock the buffer and cannot stall. */ 927 failed_bh = bh; 928 bh = head; 929 while (bh != failed_bh) { 930 unlock_buffer(bh); 931 bh = bh->b_this_page; 932 } 933 934 return false; 935 } 936 937 static int __buffer_migrate_folio(struct address_space *mapping, 938 struct folio *dst, struct folio *src, enum migrate_mode mode, 939 bool check_refs) 940 { 941 struct buffer_head *bh, *head; 942 int rc; 943 int expected_count; 944 945 head = folio_buffers(src); 946 if (!head) 947 return migrate_folio(mapping, dst, src, mode); 948 949 /* Check whether page does not have extra refs before we do more work */ 950 expected_count = folio_expected_ref_count(src) + 1; 951 if (folio_ref_count(src) != expected_count) 952 return -EAGAIN; 953 954 if (!buffer_migrate_lock_buffers(head, mode)) 955 return -EAGAIN; 956 957 if (check_refs) { 958 bool busy, migrating; 959 bool invalidated = false; 960 961 migrating = test_and_set_bit_lock(BH_Migrate, &head->b_state); 962 VM_WARN_ON_ONCE(migrating); 963 recheck_buffers: 964 busy = false; 965 spin_lock(&mapping->i_private_lock); 966 bh = head; 967 do { 968 if (atomic_read(&bh->b_count)) { 969 busy = true; 970 break; 971 } 972 bh = bh->b_this_page; 973 } while (bh != head); 974 spin_unlock(&mapping->i_private_lock); 975 if (busy) { 976 if (invalidated) { 977 rc = -EAGAIN; 978 goto unlock_buffers; 979 } 980 invalidate_bh_lrus(); 981 invalidated = true; 982 goto recheck_buffers; 983 } 984 } 985 986 rc = filemap_migrate_folio(mapping, dst, src, mode); 987 if (rc) 988 goto unlock_buffers; 989 990 bh = head; 991 do { 992 folio_set_bh(bh, dst, bh_offset(bh)); 993 bh = bh->b_this_page; 994 } while (bh != head); 995 996 unlock_buffers: 997 if (check_refs) 998 clear_bit_unlock(BH_Migrate, &head->b_state); 999 bh = head; 1000 do { 1001 unlock_buffer(bh); 1002 bh = bh->b_this_page; 1003 } while (bh != head); 1004 1005 return rc; 1006 } 1007 1008 /** 1009 * buffer_migrate_folio() - Migration function for folios with buffers. 1010 * @mapping: The address space containing @src. 1011 * @dst: The folio to migrate to. 1012 * @src: The folio to migrate from. 1013 * @mode: How to migrate the folio. 1014 * 1015 * This function can only be used if the underlying filesystem guarantees 1016 * that no other references to @src exist. For example attached buffer 1017 * heads are accessed only under the folio lock. If your filesystem cannot 1018 * provide this guarantee, buffer_migrate_folio_norefs() may be more 1019 * appropriate. 1020 * 1021 * Return: 0 on success or a negative errno on failure. 1022 */ 1023 int buffer_migrate_folio(struct address_space *mapping, 1024 struct folio *dst, struct folio *src, enum migrate_mode mode) 1025 { 1026 return __buffer_migrate_folio(mapping, dst, src, mode, false); 1027 } 1028 EXPORT_SYMBOL(buffer_migrate_folio); 1029 1030 /** 1031 * buffer_migrate_folio_norefs() - Migration function for folios with buffers. 1032 * @mapping: The address space containing @src. 1033 * @dst: The folio to migrate to. 1034 * @src: The folio to migrate from. 1035 * @mode: How to migrate the folio. 1036 * 1037 * Like buffer_migrate_folio() except that this variant is more careful 1038 * and checks that there are also no buffer head references. This function 1039 * is the right one for mappings where buffer heads are directly looked 1040 * up and referenced (such as block device mappings). 1041 * 1042 * Return: 0 on success or a negative errno on failure. 1043 */ 1044 int buffer_migrate_folio_norefs(struct address_space *mapping, 1045 struct folio *dst, struct folio *src, enum migrate_mode mode) 1046 { 1047 return __buffer_migrate_folio(mapping, dst, src, mode, true); 1048 } 1049 EXPORT_SYMBOL_GPL(buffer_migrate_folio_norefs); 1050 #endif /* CONFIG_BUFFER_HEAD */ 1051 1052 int filemap_migrate_folio(struct address_space *mapping, 1053 struct folio *dst, struct folio *src, enum migrate_mode mode) 1054 { 1055 return __migrate_folio(mapping, dst, src, folio_get_private(src), mode); 1056 } 1057 EXPORT_SYMBOL_GPL(filemap_migrate_folio); 1058 1059 /* 1060 * Default handling if a filesystem does not provide a migration function. 1061 */ 1062 static int fallback_migrate_folio(struct address_space *mapping, 1063 struct folio *dst, struct folio *src, enum migrate_mode mode) 1064 { 1065 WARN_ONCE(mapping->a_ops->writepages, 1066 "%ps does not implement migrate_folio\n", 1067 mapping->a_ops); 1068 if (folio_test_dirty(src)) 1069 return -EBUSY; 1070 1071 /* 1072 * Filesystem may have private data at folio->private that we 1073 * can't migrate automatically. 1074 */ 1075 if (!filemap_release_folio(src, GFP_KERNEL)) 1076 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; 1077 1078 return migrate_folio(mapping, dst, src, mode); 1079 } 1080 1081 /* 1082 * Move a src folio to a newly allocated dst folio. 1083 * 1084 * The src and dst folios are locked and the src folios was unmapped from 1085 * the page tables. 1086 * 1087 * On success, the src folio was replaced by the dst folio. 1088 * 1089 * Return value: 1090 * < 0 - error code 1091 * 0 - success 1092 */ 1093 static int move_to_new_folio(struct folio *dst, struct folio *src, 1094 enum migrate_mode mode) 1095 { 1096 struct address_space *mapping = folio_mapping(src); 1097 int rc = -EAGAIN; 1098 1099 VM_BUG_ON_FOLIO(!folio_test_locked(src), src); 1100 VM_BUG_ON_FOLIO(!folio_test_locked(dst), dst); 1101 1102 if (!mapping) 1103 rc = migrate_folio(mapping, dst, src, mode); 1104 else if (mapping_inaccessible(mapping)) 1105 rc = -EOPNOTSUPP; 1106 else if (mapping->a_ops->migrate_folio) 1107 /* 1108 * Most folios have a mapping and most filesystems 1109 * provide a migrate_folio callback. Anonymous folios 1110 * are part of swap space which also has its own 1111 * migrate_folio callback. This is the most common path 1112 * for page migration. 1113 */ 1114 rc = mapping->a_ops->migrate_folio(mapping, dst, src, 1115 mode); 1116 else 1117 rc = fallback_migrate_folio(mapping, dst, src, mode); 1118 1119 if (!rc) { 1120 /* 1121 * For pagecache folios, src->mapping must be cleared before src 1122 * is freed. Anonymous folios must stay anonymous until freed. 1123 */ 1124 if (!folio_test_anon(src)) 1125 src->mapping = NULL; 1126 1127 if (likely(!folio_is_zone_device(dst))) 1128 flush_dcache_folio(dst); 1129 } 1130 return rc; 1131 } 1132 1133 /* 1134 * To record some information during migration, we use unused private 1135 * field of struct folio of the newly allocated destination folio. 1136 * This is safe because nobody is using it except us. 1137 */ 1138 enum { 1139 FOLIO_WAS_MAPPED = BIT(0), 1140 FOLIO_WAS_MLOCKED = BIT(1), 1141 FOLIO_OLD_STATES = FOLIO_WAS_MAPPED | FOLIO_WAS_MLOCKED, 1142 }; 1143 1144 static void __migrate_folio_record(struct folio *dst, 1145 int old_folio_state, struct anon_vma *anon_vma) 1146 { 1147 dst->private = (void *)anon_vma + old_folio_state; 1148 } 1149 1150 static void __migrate_folio_extract(struct folio *dst, 1151 int *old_folio_state, struct anon_vma **anon_vmap) 1152 { 1153 unsigned long private = (unsigned long)dst->private; 1154 1155 *anon_vmap = (struct anon_vma *)(private & ~FOLIO_OLD_STATES); 1156 *old_folio_state = private & FOLIO_OLD_STATES; 1157 dst->private = NULL; 1158 } 1159 1160 /* Restore the source folio to the original state upon failure */ 1161 static void migrate_folio_undo_src(struct folio *src, 1162 int page_was_mapped, 1163 struct anon_vma *anon_vma, 1164 bool locked, 1165 struct list_head *ret) 1166 { 1167 if (page_was_mapped) 1168 remove_migration_ptes(src, src, 0); 1169 /* Drop an anon_vma reference if we took one */ 1170 if (anon_vma) 1171 put_anon_vma(anon_vma); 1172 if (locked) 1173 folio_unlock(src); 1174 if (ret) 1175 list_move_tail(&src->lru, ret); 1176 } 1177 1178 /* Restore the destination folio to the original state upon failure */ 1179 static void migrate_folio_undo_dst(struct folio *dst, bool locked, 1180 free_folio_t put_new_folio, unsigned long private) 1181 { 1182 if (locked) 1183 folio_unlock(dst); 1184 if (put_new_folio) 1185 put_new_folio(dst, private); 1186 else 1187 folio_put(dst); 1188 } 1189 1190 /* Cleanup src folio upon migration success */ 1191 static void migrate_folio_done(struct folio *src, 1192 enum migrate_reason reason) 1193 { 1194 if (likely(!page_has_movable_ops(&src->page)) && reason != MR_DEMOTION) 1195 mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON + 1196 folio_is_file_lru(src), -folio_nr_pages(src)); 1197 1198 if (reason != MR_MEMORY_FAILURE) 1199 /* We release the page in page_handle_poison. */ 1200 folio_put(src); 1201 } 1202 1203 /* Obtain the lock on page, remove all ptes. */ 1204 static int migrate_folio_unmap(new_folio_t get_new_folio, 1205 free_folio_t put_new_folio, unsigned long private, 1206 struct folio *src, struct folio **dstp, enum migrate_mode mode, 1207 struct list_head *ret) 1208 { 1209 struct folio *dst; 1210 int rc = -EAGAIN; 1211 int old_folio_state = 0; 1212 struct anon_vma *anon_vma = NULL; 1213 bool locked = false; 1214 bool dst_locked = false; 1215 1216 dst = get_new_folio(src, private); 1217 if (!dst) 1218 return -ENOMEM; 1219 *dstp = dst; 1220 1221 dst->private = NULL; 1222 1223 if (!folio_trylock(src)) { 1224 if (mode == MIGRATE_ASYNC) 1225 goto out; 1226 1227 /* 1228 * It's not safe for direct compaction to call lock_page. 1229 * For example, during page readahead pages are added locked 1230 * to the LRU. Later, when the IO completes the pages are 1231 * marked uptodate and unlocked. However, the queueing 1232 * could be merging multiple pages for one bio (e.g. 1233 * mpage_readahead). If an allocation happens for the 1234 * second or third page, the process can end up locking 1235 * the same page twice and deadlocking. Rather than 1236 * trying to be clever about what pages can be locked, 1237 * avoid the use of lock_page for direct compaction 1238 * altogether. 1239 */ 1240 if (current->flags & PF_MEMALLOC) 1241 goto out; 1242 1243 /* 1244 * In "light" mode, we can wait for transient locks (eg 1245 * inserting a page into the page table), but it's not 1246 * worth waiting for I/O. 1247 */ 1248 if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(src)) 1249 goto out; 1250 1251 folio_lock(src); 1252 } 1253 locked = true; 1254 if (folio_test_mlocked(src)) 1255 old_folio_state |= FOLIO_WAS_MLOCKED; 1256 1257 if (folio_test_writeback(src)) { 1258 /* 1259 * Only in the case of a full synchronous migration is it 1260 * necessary to wait for writeback. In the async case, 1261 * the retry loop is too short and in the sync-light case, 1262 * the overhead of stalling is too much 1263 */ 1264 switch (mode) { 1265 case MIGRATE_SYNC: 1266 break; 1267 default: 1268 rc = -EBUSY; 1269 goto out; 1270 } 1271 folio_wait_writeback(src); 1272 } 1273 1274 /* 1275 * By try_to_migrate(), src->mapcount goes down to 0 here. In this case, 1276 * we cannot notice that anon_vma is freed while we migrate a page. 1277 * This get_anon_vma() delays freeing anon_vma pointer until the end 1278 * of migration. File cache pages are no problem because of page_lock() 1279 * File Caches may use write_page() or lock_page() in migration, then, 1280 * just care Anon page here. 1281 * 1282 * Only folio_get_anon_vma() understands the subtleties of 1283 * getting a hold on an anon_vma from outside one of its mms. 1284 * But if we cannot get anon_vma, then we won't need it anyway, 1285 * because that implies that the anon page is no longer mapped 1286 * (and cannot be remapped so long as we hold the page lock). 1287 */ 1288 if (folio_test_anon(src) && !folio_test_ksm(src)) 1289 anon_vma = folio_get_anon_vma(src); 1290 1291 /* 1292 * Block others from accessing the new page when we get around to 1293 * establishing additional references. We are usually the only one 1294 * holding a reference to dst at this point. We used to have a BUG 1295 * here if folio_trylock(dst) fails, but would like to allow for 1296 * cases where there might be a race with the previous use of dst. 1297 * This is much like races on refcount of oldpage: just don't BUG(). 1298 */ 1299 if (unlikely(!folio_trylock(dst))) 1300 goto out; 1301 dst_locked = true; 1302 1303 if (unlikely(page_has_movable_ops(&src->page))) { 1304 __migrate_folio_record(dst, old_folio_state, anon_vma); 1305 return 0; 1306 } 1307 1308 /* 1309 * Corner case handling: 1310 * 1. When a new swap-cache page is read into, it is added to the LRU 1311 * and treated as swapcache but it has no rmap yet. 1312 * Calling try_to_unmap() against a src->mapping==NULL page will 1313 * trigger a BUG. So handle it here. 1314 * 2. An orphaned page (see truncate_cleanup_page) might have 1315 * fs-private metadata. The page can be picked up due to memory 1316 * offlining. Everywhere else except page reclaim, the page is 1317 * invisible to the vm, so the page can not be migrated. So try to 1318 * free the metadata, so the page can be freed. 1319 */ 1320 if (!src->mapping) { 1321 if (folio_test_private(src)) { 1322 try_to_free_buffers(src); 1323 goto out; 1324 } 1325 } else if (folio_mapped(src)) { 1326 /* Establish migration ptes */ 1327 VM_BUG_ON_FOLIO(folio_test_anon(src) && 1328 !folio_test_ksm(src) && !anon_vma, src); 1329 try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0); 1330 old_folio_state |= FOLIO_WAS_MAPPED; 1331 } 1332 1333 if (!folio_mapped(src)) { 1334 __migrate_folio_record(dst, old_folio_state, anon_vma); 1335 return 0; 1336 } 1337 1338 out: 1339 /* 1340 * A folio that has not been unmapped will be restored to 1341 * right list unless we want to retry. 1342 */ 1343 if (rc == -EAGAIN) 1344 ret = NULL; 1345 1346 migrate_folio_undo_src(src, old_folio_state & FOLIO_WAS_MAPPED, 1347 anon_vma, locked, ret); 1348 migrate_folio_undo_dst(dst, dst_locked, put_new_folio, private); 1349 1350 return rc; 1351 } 1352 1353 /* Migrate the folio to the newly allocated folio in dst. */ 1354 static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, 1355 struct folio *src, struct folio *dst, 1356 enum migrate_mode mode, enum migrate_reason reason, 1357 struct list_head *ret) 1358 { 1359 int rc; 1360 int old_folio_state = 0; 1361 struct anon_vma *anon_vma = NULL; 1362 bool src_deferred_split = false; 1363 bool src_partially_mapped = false; 1364 struct list_head *prev; 1365 1366 __migrate_folio_extract(dst, &old_folio_state, &anon_vma); 1367 prev = dst->lru.prev; 1368 list_del(&dst->lru); 1369 1370 if (unlikely(page_has_movable_ops(&src->page))) { 1371 rc = migrate_movable_ops_page(&dst->page, &src->page, mode); 1372 if (rc) 1373 goto out; 1374 goto out_unlock_both; 1375 } 1376 1377 if (folio_order(src) > 1 && 1378 !data_race(list_empty(&src->_deferred_list))) { 1379 src_deferred_split = true; 1380 src_partially_mapped = folio_test_partially_mapped(src); 1381 } 1382 1383 rc = move_to_new_folio(dst, src, mode); 1384 if (rc) 1385 goto out; 1386 1387 /* 1388 * Requeue the destination folio on the deferred split queue if 1389 * the source was on the queue. The source is unqueued in 1390 * __folio_migrate_mapping(), so we recorded the state from 1391 * before move_to_new_folio(). 1392 */ 1393 if (src_deferred_split) 1394 deferred_split_folio(dst, src_partially_mapped); 1395 1396 /* 1397 * When successful, push dst to LRU immediately: so that if it 1398 * turns out to be an mlocked page, remove_migration_ptes() will 1399 * automatically build up the correct dst->mlock_count for it. 1400 * 1401 * We would like to do something similar for the old page, when 1402 * unsuccessful, and other cases when a page has been temporarily 1403 * isolated from the unevictable LRU: but this case is the easiest. 1404 */ 1405 folio_add_lru(dst); 1406 if (old_folio_state & FOLIO_WAS_MLOCKED) 1407 lru_add_drain(); 1408 1409 if (old_folio_state & FOLIO_WAS_MAPPED) 1410 remove_migration_ptes(src, dst, 0); 1411 1412 out_unlock_both: 1413 folio_unlock(dst); 1414 folio_set_owner_migrate_reason(dst, reason); 1415 /* 1416 * If migration is successful, decrease refcount of dst, 1417 * which will not free the page because new page owner increased 1418 * refcounter. 1419 */ 1420 folio_put(dst); 1421 1422 /* 1423 * A folio that has been migrated has all references removed 1424 * and will be freed. 1425 */ 1426 list_del(&src->lru); 1427 /* Drop an anon_vma reference if we took one */ 1428 if (anon_vma) 1429 put_anon_vma(anon_vma); 1430 folio_unlock(src); 1431 migrate_folio_done(src, reason); 1432 1433 return rc; 1434 out: 1435 /* 1436 * A folio that has not been migrated will be restored to 1437 * right list unless we want to retry. 1438 */ 1439 if (rc == -EAGAIN) { 1440 list_add(&dst->lru, prev); 1441 __migrate_folio_record(dst, old_folio_state, anon_vma); 1442 return rc; 1443 } 1444 1445 migrate_folio_undo_src(src, old_folio_state & FOLIO_WAS_MAPPED, 1446 anon_vma, true, ret); 1447 migrate_folio_undo_dst(dst, true, put_new_folio, private); 1448 1449 return rc; 1450 } 1451 1452 /* 1453 * Counterpart of unmap_and_move_page() for hugepage migration. 1454 * 1455 * This function doesn't wait the completion of hugepage I/O 1456 * because there is no race between I/O and migration for hugepage. 1457 * Note that currently hugepage I/O occurs only in direct I/O 1458 * where no lock is held and PG_writeback is irrelevant, 1459 * and writeback status of all subpages are counted in the reference 1460 * count of the head page (i.e. if all subpages of a 2MB hugepage are 1461 * under direct I/O, the reference of the head page is 512 and a bit more.) 1462 * This means that when we try to migrate hugepage whose subpages are 1463 * doing direct I/O, some references remain after try_to_unmap() and 1464 * hugepage migration fails without data corruption. 1465 * 1466 * There is also no race when direct I/O is issued on the page under migration, 1467 * because then pte is replaced with migration swap entry and direct I/O code 1468 * will wait in the page fault for migration to complete. 1469 */ 1470 static int unmap_and_move_huge_page(new_folio_t get_new_folio, 1471 free_folio_t put_new_folio, unsigned long private, 1472 struct folio *src, int force, enum migrate_mode mode, 1473 int reason, struct list_head *ret) 1474 { 1475 struct folio *dst; 1476 int rc = -EAGAIN; 1477 int page_was_mapped = 0; 1478 struct anon_vma *anon_vma = NULL; 1479 struct address_space *mapping = NULL; 1480 enum ttu_flags ttu = 0; 1481 1482 if (folio_ref_count(src) == 1) { 1483 /* page was freed from under us. So we are done. */ 1484 folio_putback_hugetlb(src); 1485 return 0; 1486 } 1487 1488 dst = get_new_folio(src, private); 1489 if (!dst) 1490 return -ENOMEM; 1491 1492 if (!folio_trylock(src)) { 1493 if (!force) 1494 goto out; 1495 switch (mode) { 1496 case MIGRATE_SYNC: 1497 break; 1498 default: 1499 goto out; 1500 } 1501 folio_lock(src); 1502 } 1503 1504 /* 1505 * Check for pages which are in the process of being freed. Without 1506 * folio_mapping() set, hugetlbfs specific move page routine will not 1507 * be called and we could leak usage counts for subpools. 1508 */ 1509 if (hugetlb_folio_subpool(src) && !folio_mapping(src)) { 1510 rc = -EBUSY; 1511 goto out_unlock; 1512 } 1513 1514 if (folio_test_anon(src)) 1515 anon_vma = folio_get_anon_vma(src); 1516 1517 if (unlikely(!folio_trylock(dst))) 1518 goto put_anon; 1519 1520 if (folio_mapped(src)) { 1521 if (!folio_test_anon(src)) { 1522 /* 1523 * In shared mappings, try_to_unmap could potentially 1524 * call huge_pmd_unshare. Because of this, take 1525 * semaphore in write mode here and set TTU_RMAP_LOCKED 1526 * to let lower levels know we have taken the lock. 1527 */ 1528 mapping = hugetlb_folio_mapping_lock_write(src); 1529 if (unlikely(!mapping)) 1530 goto unlock_put_anon; 1531 1532 ttu = TTU_RMAP_LOCKED; 1533 } 1534 1535 try_to_migrate(src, ttu); 1536 page_was_mapped = 1; 1537 } 1538 1539 if (!folio_mapped(src)) 1540 rc = move_to_new_folio(dst, src, mode); 1541 1542 if (page_was_mapped) 1543 remove_migration_ptes(src, !rc ? dst : src, ttu); 1544 1545 if (ttu & TTU_RMAP_LOCKED) 1546 i_mmap_unlock_write(mapping); 1547 1548 unlock_put_anon: 1549 folio_unlock(dst); 1550 1551 put_anon: 1552 if (anon_vma) 1553 put_anon_vma(anon_vma); 1554 1555 if (!rc) { 1556 move_hugetlb_state(src, dst, reason); 1557 put_new_folio = NULL; 1558 } 1559 1560 out_unlock: 1561 folio_unlock(src); 1562 out: 1563 if (!rc) 1564 folio_putback_hugetlb(src); 1565 else if (rc != -EAGAIN) 1566 list_move_tail(&src->lru, ret); 1567 1568 /* 1569 * If migration was not successful and there's a freeing callback, 1570 * return the folio to that special allocator. Otherwise, simply drop 1571 * our additional reference. 1572 */ 1573 if (put_new_folio) 1574 put_new_folio(dst, private); 1575 else 1576 folio_put(dst); 1577 1578 return rc; 1579 } 1580 1581 static inline int try_split_folio(struct folio *folio, struct list_head *split_folios, 1582 enum migrate_mode mode) 1583 { 1584 int rc; 1585 1586 if (mode == MIGRATE_ASYNC) { 1587 if (!folio_trylock(folio)) 1588 return -EAGAIN; 1589 } else { 1590 folio_lock(folio); 1591 } 1592 rc = split_folio_to_list(folio, split_folios); 1593 folio_unlock(folio); 1594 if (!rc) 1595 list_move_tail(&folio->lru, split_folios); 1596 1597 return rc; 1598 } 1599 1600 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1601 #define NR_MAX_BATCHED_MIGRATION HPAGE_PMD_NR 1602 #else 1603 #define NR_MAX_BATCHED_MIGRATION 512 1604 #endif 1605 #define NR_MAX_MIGRATE_PAGES_RETRY 10 1606 #define NR_MAX_MIGRATE_ASYNC_RETRY 3 1607 #define NR_MAX_MIGRATE_SYNC_RETRY \ 1608 (NR_MAX_MIGRATE_PAGES_RETRY - NR_MAX_MIGRATE_ASYNC_RETRY) 1609 1610 struct migrate_pages_stats { 1611 int nr_succeeded; /* Normal and large folios migrated successfully, in 1612 units of base pages */ 1613 int nr_failed_pages; /* Normal and large folios failed to be migrated, in 1614 units of base pages. Untried folios aren't counted */ 1615 int nr_thp_succeeded; /* THP migrated successfully */ 1616 int nr_thp_failed; /* THP failed to be migrated */ 1617 int nr_thp_split; /* THP split before migrating */ 1618 int nr_split; /* Large folio (include THP) split before migrating */ 1619 }; 1620 1621 /* 1622 * Returns the number of hugetlb folios that were not migrated, or an error code 1623 * after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable 1624 * any more because the list has become empty or no retryable hugetlb folios 1625 * exist any more. It is caller's responsibility to call putback_movable_pages() 1626 * only if ret != 0. 1627 */ 1628 static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio, 1629 free_folio_t put_new_folio, unsigned long private, 1630 enum migrate_mode mode, int reason, 1631 struct migrate_pages_stats *stats, 1632 struct list_head *ret_folios) 1633 { 1634 int retry = 1; 1635 int nr_failed = 0; 1636 int nr_retry_pages = 0; 1637 int pass = 0; 1638 struct folio *folio, *folio2; 1639 int rc, nr_pages; 1640 1641 for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { 1642 retry = 0; 1643 nr_retry_pages = 0; 1644 1645 list_for_each_entry_safe(folio, folio2, from, lru) { 1646 if (!folio_test_hugetlb(folio)) 1647 continue; 1648 1649 nr_pages = folio_nr_pages(folio); 1650 1651 cond_resched(); 1652 1653 /* 1654 * Migratability of hugepages depends on architectures and 1655 * their size. This check is necessary because some callers 1656 * of hugepage migration like soft offline and memory 1657 * hotremove don't walk through page tables or check whether 1658 * the hugepage is pmd-based or not before kicking migration. 1659 */ 1660 if (!hugepage_migration_supported(folio_hstate(folio))) { 1661 nr_failed++; 1662 stats->nr_failed_pages += nr_pages; 1663 list_move_tail(&folio->lru, ret_folios); 1664 continue; 1665 } 1666 1667 rc = unmap_and_move_huge_page(get_new_folio, 1668 put_new_folio, private, 1669 folio, pass > 2, mode, 1670 reason, ret_folios); 1671 /* 1672 * The rules are: 1673 * 0: hugetlb folio will be put back 1674 * -EAGAIN: stay on the from list 1675 * -ENOMEM: stay on the from list 1676 * Other errno: put on ret_folios list 1677 */ 1678 switch(rc) { 1679 case -ENOMEM: 1680 /* 1681 * When memory is low, don't bother to try to migrate 1682 * other folios, just exit. 1683 */ 1684 stats->nr_failed_pages += nr_pages + nr_retry_pages; 1685 return -ENOMEM; 1686 case -EAGAIN: 1687 retry++; 1688 nr_retry_pages += nr_pages; 1689 break; 1690 case 0: 1691 stats->nr_succeeded += nr_pages; 1692 break; 1693 default: 1694 /* 1695 * Permanent failure (-EBUSY, etc.): 1696 * unlike -EAGAIN case, the failed folio is 1697 * removed from migration folio list and not 1698 * retried in the next outer loop. 1699 */ 1700 nr_failed++; 1701 stats->nr_failed_pages += nr_pages; 1702 break; 1703 } 1704 } 1705 } 1706 /* 1707 * nr_failed is number of hugetlb folios failed to be migrated. After 1708 * NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb 1709 * folios as failed. 1710 */ 1711 nr_failed += retry; 1712 stats->nr_failed_pages += nr_retry_pages; 1713 1714 return nr_failed; 1715 } 1716 1717 static void migrate_folios_move(struct list_head *src_folios, 1718 struct list_head *dst_folios, 1719 free_folio_t put_new_folio, unsigned long private, 1720 enum migrate_mode mode, int reason, 1721 struct list_head *ret_folios, 1722 struct migrate_pages_stats *stats, 1723 int *retry, int *thp_retry, int *nr_failed, 1724 int *nr_retry_pages) 1725 { 1726 struct folio *folio, *folio2, *dst, *dst2; 1727 bool is_thp; 1728 int nr_pages; 1729 int rc; 1730 1731 dst = list_first_entry(dst_folios, struct folio, lru); 1732 dst2 = list_next_entry(dst, lru); 1733 list_for_each_entry_safe(folio, folio2, src_folios, lru) { 1734 is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio); 1735 nr_pages = folio_nr_pages(folio); 1736 1737 cond_resched(); 1738 1739 rc = migrate_folio_move(put_new_folio, private, 1740 folio, dst, mode, 1741 reason, ret_folios); 1742 /* 1743 * The rules are: 1744 * 0: folio will be freed 1745 * -EAGAIN: stay on the unmap_folios list 1746 * Other errno: put on ret_folios list 1747 */ 1748 switch (rc) { 1749 case -EAGAIN: 1750 *retry += 1; 1751 *thp_retry += is_thp; 1752 *nr_retry_pages += nr_pages; 1753 break; 1754 case 0: 1755 stats->nr_succeeded += nr_pages; 1756 stats->nr_thp_succeeded += is_thp; 1757 break; 1758 default: 1759 *nr_failed += 1; 1760 stats->nr_thp_failed += is_thp; 1761 stats->nr_failed_pages += nr_pages; 1762 break; 1763 } 1764 dst = dst2; 1765 dst2 = list_next_entry(dst, lru); 1766 } 1767 } 1768 1769 static void migrate_folios_undo(struct list_head *src_folios, 1770 struct list_head *dst_folios, 1771 free_folio_t put_new_folio, unsigned long private, 1772 struct list_head *ret_folios) 1773 { 1774 struct folio *folio, *folio2, *dst, *dst2; 1775 1776 dst = list_first_entry(dst_folios, struct folio, lru); 1777 dst2 = list_next_entry(dst, lru); 1778 list_for_each_entry_safe(folio, folio2, src_folios, lru) { 1779 int old_folio_state = 0; 1780 struct anon_vma *anon_vma = NULL; 1781 1782 __migrate_folio_extract(dst, &old_folio_state, &anon_vma); 1783 migrate_folio_undo_src(folio, old_folio_state & FOLIO_WAS_MAPPED, 1784 anon_vma, true, ret_folios); 1785 list_del(&dst->lru); 1786 migrate_folio_undo_dst(dst, true, put_new_folio, private); 1787 dst = dst2; 1788 dst2 = list_next_entry(dst, lru); 1789 } 1790 } 1791 1792 /* 1793 * migrate_pages_batch() first unmaps folios in the from list as many as 1794 * possible, then move the unmapped folios. 1795 * 1796 * We only batch migration if mode == MIGRATE_ASYNC to avoid to wait a 1797 * lock or bit when we have locked more than one folio. Which may cause 1798 * deadlock (e.g., for loop device). So, if mode != MIGRATE_ASYNC, the 1799 * length of the from list must be <= 1. 1800 */ 1801 static int migrate_pages_batch(struct list_head *from, 1802 new_folio_t get_new_folio, free_folio_t put_new_folio, 1803 unsigned long private, enum migrate_mode mode, int reason, 1804 struct list_head *ret_folios, struct list_head *split_folios, 1805 struct migrate_pages_stats *stats, int nr_pass) 1806 { 1807 int retry = 1; 1808 int thp_retry = 1; 1809 int nr_failed = 0; 1810 int nr_retry_pages = 0; 1811 int pass = 0; 1812 bool is_thp = false; 1813 bool is_large = false; 1814 struct folio *folio, *folio2, *dst = NULL; 1815 int rc, rc_saved = 0, nr_pages; 1816 LIST_HEAD(unmap_folios); 1817 LIST_HEAD(dst_folios); 1818 bool nosplit = (reason == MR_NUMA_MISPLACED); 1819 1820 VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC && 1821 !list_empty(from) && !list_is_singular(from)); 1822 1823 for (pass = 0; pass < nr_pass && retry; pass++) { 1824 retry = 0; 1825 thp_retry = 0; 1826 nr_retry_pages = 0; 1827 1828 list_for_each_entry_safe(folio, folio2, from, lru) { 1829 is_large = folio_test_large(folio); 1830 is_thp = folio_test_pmd_mappable(folio); 1831 nr_pages = folio_nr_pages(folio); 1832 1833 cond_resched(); 1834 1835 /* 1836 * The rare folio on the deferred split list should 1837 * be split now. It should not count as a failure: 1838 * but increment nr_failed because, without doing so, 1839 * migrate_pages() may report success with (split but 1840 * unmigrated) pages still on its fromlist; whereas it 1841 * always reports success when its fromlist is empty. 1842 * stats->nr_thp_failed should be increased too, 1843 * otherwise stats inconsistency will happen when 1844 * migrate_pages_batch is called via migrate_pages() 1845 * with MIGRATE_SYNC and MIGRATE_ASYNC. 1846 * 1847 * Only check it without removing it from the list. 1848 * Since the folio can be on deferred_split_scan() 1849 * local list and removing it can cause the local list 1850 * corruption. Folio split process below can handle it 1851 * with the help of folio_ref_freeze(). 1852 * 1853 * nr_pages > 2 is needed to avoid checking order-1 1854 * page cache folios. They exist, in contrast to 1855 * non-existent order-1 anonymous folios, and do not 1856 * use _deferred_list. 1857 */ 1858 if (nr_pages > 2 && 1859 !list_empty(&folio->_deferred_list) && 1860 folio_test_partially_mapped(folio)) { 1861 if (!try_split_folio(folio, split_folios, mode)) { 1862 nr_failed++; 1863 stats->nr_thp_failed += is_thp; 1864 stats->nr_thp_split += is_thp; 1865 stats->nr_split++; 1866 continue; 1867 } 1868 } 1869 1870 /* 1871 * Large folio migration might be unsupported or 1872 * the allocation might be failed so we should retry 1873 * on the same folio with the large folio split 1874 * to normal folios. 1875 * 1876 * Split folios are put in split_folios, and 1877 * we will migrate them after the rest of the 1878 * list is processed. 1879 */ 1880 if (!thp_migration_supported() && is_thp) { 1881 nr_failed++; 1882 stats->nr_thp_failed++; 1883 if (!try_split_folio(folio, split_folios, mode)) { 1884 stats->nr_thp_split++; 1885 stats->nr_split++; 1886 continue; 1887 } 1888 stats->nr_failed_pages += nr_pages; 1889 list_move_tail(&folio->lru, ret_folios); 1890 continue; 1891 } 1892 1893 /* 1894 * If we are holding the last folio reference, the folio 1895 * was freed from under us, so just drop our reference. 1896 */ 1897 if (likely(!page_has_movable_ops(&folio->page)) && 1898 folio_ref_count(folio) == 1) { 1899 folio_clear_active(folio); 1900 folio_clear_unevictable(folio); 1901 list_del(&folio->lru); 1902 migrate_folio_done(folio, reason); 1903 stats->nr_succeeded += nr_pages; 1904 stats->nr_thp_succeeded += is_thp; 1905 continue; 1906 } 1907 1908 rc = migrate_folio_unmap(get_new_folio, put_new_folio, 1909 private, folio, &dst, mode, ret_folios); 1910 /* 1911 * The rules are: 1912 * 0: folio will be put on unmap_folios list, 1913 * dst folio put on dst_folios list 1914 * -EAGAIN: stay on the from list 1915 * -ENOMEM: stay on the from list 1916 * Other errno: put on ret_folios list 1917 */ 1918 switch(rc) { 1919 case -ENOMEM: 1920 /* 1921 * When memory is low, don't bother to try to migrate 1922 * other folios, move unmapped folios, then exit. 1923 */ 1924 nr_failed++; 1925 stats->nr_thp_failed += is_thp; 1926 /* Large folio NUMA faulting doesn't split to retry. */ 1927 if (is_large && !nosplit) { 1928 int ret = try_split_folio(folio, split_folios, mode); 1929 1930 if (!ret) { 1931 stats->nr_thp_split += is_thp; 1932 stats->nr_split++; 1933 break; 1934 } else if (reason == MR_LONGTERM_PIN && 1935 ret == -EAGAIN) { 1936 /* 1937 * Try again to split large folio to 1938 * mitigate the failure of longterm pinning. 1939 */ 1940 retry++; 1941 thp_retry += is_thp; 1942 nr_retry_pages += nr_pages; 1943 /* Undo duplicated failure counting. */ 1944 nr_failed--; 1945 stats->nr_thp_failed -= is_thp; 1946 break; 1947 } 1948 } 1949 1950 stats->nr_failed_pages += nr_pages + nr_retry_pages; 1951 /* nr_failed isn't updated for not used */ 1952 stats->nr_thp_failed += thp_retry; 1953 rc_saved = rc; 1954 if (list_empty(&unmap_folios)) 1955 goto out; 1956 else 1957 goto move; 1958 case -EAGAIN: 1959 retry++; 1960 thp_retry += is_thp; 1961 nr_retry_pages += nr_pages; 1962 break; 1963 case 0: 1964 list_move_tail(&folio->lru, &unmap_folios); 1965 list_add_tail(&dst->lru, &dst_folios); 1966 break; 1967 default: 1968 /* 1969 * Permanent failure (-EBUSY, etc.): 1970 * unlike -EAGAIN case, the failed folio is 1971 * removed from migration folio list and not 1972 * retried in the next outer loop. 1973 */ 1974 nr_failed++; 1975 stats->nr_thp_failed += is_thp; 1976 stats->nr_failed_pages += nr_pages; 1977 break; 1978 } 1979 } 1980 } 1981 nr_failed += retry; 1982 stats->nr_thp_failed += thp_retry; 1983 stats->nr_failed_pages += nr_retry_pages; 1984 move: 1985 /* Flush TLBs for all unmapped folios */ 1986 try_to_unmap_flush(); 1987 1988 retry = 1; 1989 for (pass = 0; pass < nr_pass && retry; pass++) { 1990 retry = 0; 1991 thp_retry = 0; 1992 nr_retry_pages = 0; 1993 1994 /* Move the unmapped folios */ 1995 migrate_folios_move(&unmap_folios, &dst_folios, 1996 put_new_folio, private, mode, reason, 1997 ret_folios, stats, &retry, &thp_retry, 1998 &nr_failed, &nr_retry_pages); 1999 } 2000 nr_failed += retry; 2001 stats->nr_thp_failed += thp_retry; 2002 stats->nr_failed_pages += nr_retry_pages; 2003 2004 rc = rc_saved ? : nr_failed; 2005 out: 2006 /* Cleanup remaining folios */ 2007 migrate_folios_undo(&unmap_folios, &dst_folios, 2008 put_new_folio, private, ret_folios); 2009 2010 return rc; 2011 } 2012 2013 static int migrate_pages_sync(struct list_head *from, new_folio_t get_new_folio, 2014 free_folio_t put_new_folio, unsigned long private, 2015 enum migrate_mode mode, int reason, 2016 struct list_head *ret_folios, struct list_head *split_folios, 2017 struct migrate_pages_stats *stats) 2018 { 2019 int rc, nr_failed = 0; 2020 LIST_HEAD(folios); 2021 struct migrate_pages_stats astats; 2022 2023 memset(&astats, 0, sizeof(astats)); 2024 /* Try to migrate in batch with MIGRATE_ASYNC mode firstly */ 2025 rc = migrate_pages_batch(from, get_new_folio, put_new_folio, private, MIGRATE_ASYNC, 2026 reason, &folios, split_folios, &astats, 2027 NR_MAX_MIGRATE_ASYNC_RETRY); 2028 stats->nr_succeeded += astats.nr_succeeded; 2029 stats->nr_thp_succeeded += astats.nr_thp_succeeded; 2030 stats->nr_thp_split += astats.nr_thp_split; 2031 stats->nr_split += astats.nr_split; 2032 if (rc < 0) { 2033 stats->nr_failed_pages += astats.nr_failed_pages; 2034 stats->nr_thp_failed += astats.nr_thp_failed; 2035 list_splice_tail(&folios, ret_folios); 2036 return rc; 2037 } 2038 stats->nr_thp_failed += astats.nr_thp_split; 2039 /* 2040 * Do not count rc, as pages will be retried below. 2041 * Count nr_split only, since it includes nr_thp_split. 2042 */ 2043 nr_failed += astats.nr_split; 2044 /* 2045 * Fall back to migrate all failed folios one by one synchronously. All 2046 * failed folios except split THPs will be retried, so their failure 2047 * isn't counted 2048 */ 2049 list_splice_tail_init(&folios, from); 2050 while (!list_empty(from)) { 2051 list_move(from->next, &folios); 2052 rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio, 2053 private, mode, reason, ret_folios, 2054 split_folios, stats, NR_MAX_MIGRATE_SYNC_RETRY); 2055 list_splice_tail_init(&folios, ret_folios); 2056 if (rc < 0) 2057 return rc; 2058 nr_failed += rc; 2059 } 2060 2061 return nr_failed; 2062 } 2063 2064 /* 2065 * migrate_pages - migrate the folios specified in a list, to the free folios 2066 * supplied as the target for the page migration 2067 * 2068 * @from: The list of folios to be migrated. 2069 * @get_new_folio: The function used to allocate free folios to be used 2070 * as the target of the folio migration. 2071 * @put_new_folio: The function used to free target folios if migration 2072 * fails, or NULL if no special handling is necessary. 2073 * @private: Private data to be passed on to get_new_folio() 2074 * @mode: The migration mode that specifies the constraints for 2075 * folio migration, if any. 2076 * @reason: The reason for folio migration. 2077 * @ret_succeeded: Set to the number of folios migrated successfully if 2078 * the caller passes a non-NULL pointer. 2079 * 2080 * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios 2081 * are movable any more because the list has become empty or no retryable folios 2082 * exist any more. It is caller's responsibility to call putback_movable_pages() 2083 * only if ret != 0. 2084 * 2085 * Returns the number of {normal folio, large folio, hugetlb} that were not 2086 * migrated, or an error code. The number of large folio splits will be 2087 * considered as the number of non-migrated large folio, no matter how many 2088 * split folios of the large folio are migrated successfully. 2089 */ 2090 int migrate_pages(struct list_head *from, new_folio_t get_new_folio, 2091 free_folio_t put_new_folio, unsigned long private, 2092 enum migrate_mode mode, int reason, unsigned int *ret_succeeded) 2093 { 2094 int rc, rc_gather; 2095 int nr_pages; 2096 struct folio *folio, *folio2; 2097 LIST_HEAD(folios); 2098 LIST_HEAD(ret_folios); 2099 LIST_HEAD(split_folios); 2100 struct migrate_pages_stats stats; 2101 2102 trace_mm_migrate_pages_start(mode, reason); 2103 2104 memset(&stats, 0, sizeof(stats)); 2105 2106 rc_gather = migrate_hugetlbs(from, get_new_folio, put_new_folio, private, 2107 mode, reason, &stats, &ret_folios); 2108 if (rc_gather < 0) 2109 goto out; 2110 2111 again: 2112 nr_pages = 0; 2113 list_for_each_entry_safe(folio, folio2, from, lru) { 2114 /* Retried hugetlb folios will be kept in list */ 2115 if (folio_test_hugetlb(folio)) { 2116 list_move_tail(&folio->lru, &ret_folios); 2117 continue; 2118 } 2119 2120 nr_pages += folio_nr_pages(folio); 2121 if (nr_pages >= NR_MAX_BATCHED_MIGRATION) 2122 break; 2123 } 2124 if (nr_pages >= NR_MAX_BATCHED_MIGRATION) 2125 list_cut_before(&folios, from, &folio2->lru); 2126 else 2127 list_splice_init(from, &folios); 2128 if (mode == MIGRATE_ASYNC) 2129 rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio, 2130 private, mode, reason, &ret_folios, 2131 &split_folios, &stats, 2132 NR_MAX_MIGRATE_PAGES_RETRY); 2133 else 2134 rc = migrate_pages_sync(&folios, get_new_folio, put_new_folio, 2135 private, mode, reason, &ret_folios, 2136 &split_folios, &stats); 2137 list_splice_tail_init(&folios, &ret_folios); 2138 if (rc < 0) { 2139 rc_gather = rc; 2140 list_splice_tail(&split_folios, &ret_folios); 2141 goto out; 2142 } 2143 if (!list_empty(&split_folios)) { 2144 /* 2145 * Failure isn't counted since all split folios of a large folio 2146 * is counted as 1 failure already. And, we only try to migrate 2147 * with minimal effort, force MIGRATE_ASYNC mode and retry once. 2148 */ 2149 migrate_pages_batch(&split_folios, get_new_folio, 2150 put_new_folio, private, MIGRATE_ASYNC, reason, 2151 &ret_folios, NULL, &stats, 1); 2152 list_splice_tail_init(&split_folios, &ret_folios); 2153 } 2154 rc_gather += rc; 2155 if (!list_empty(from)) 2156 goto again; 2157 out: 2158 /* 2159 * Put the permanent failure folio back to migration list, they 2160 * will be put back to the right list by the caller. 2161 */ 2162 list_splice(&ret_folios, from); 2163 2164 /* 2165 * Return 0 in case all split folios of fail-to-migrate large folios 2166 * are migrated successfully. 2167 */ 2168 if (list_empty(from)) 2169 rc_gather = 0; 2170 2171 count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded); 2172 count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages); 2173 count_vm_events(THP_MIGRATION_SUCCESS, stats.nr_thp_succeeded); 2174 count_vm_events(THP_MIGRATION_FAIL, stats.nr_thp_failed); 2175 count_vm_events(THP_MIGRATION_SPLIT, stats.nr_thp_split); 2176 trace_mm_migrate_pages(stats.nr_succeeded, stats.nr_failed_pages, 2177 stats.nr_thp_succeeded, stats.nr_thp_failed, 2178 stats.nr_thp_split, stats.nr_split, mode, 2179 reason); 2180 2181 if (ret_succeeded) 2182 *ret_succeeded = stats.nr_succeeded; 2183 2184 return rc_gather; 2185 } 2186 2187 struct folio *alloc_migration_target(struct folio *src, unsigned long private) 2188 { 2189 struct migration_target_control *mtc; 2190 gfp_t gfp_mask; 2191 unsigned int order = 0; 2192 int nid; 2193 enum zone_type zidx; 2194 2195 mtc = (struct migration_target_control *)private; 2196 gfp_mask = mtc->gfp_mask; 2197 nid = mtc->nid; 2198 if (nid == NUMA_NO_NODE) 2199 nid = folio_nid(src); 2200 2201 if (folio_test_hugetlb(src)) { 2202 struct hstate *h = folio_hstate(src); 2203 2204 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask); 2205 return alloc_hugetlb_folio_nodemask(h, nid, 2206 mtc->nmask, gfp_mask, 2207 htlb_allow_alloc_fallback(mtc->reason)); 2208 } 2209 2210 if (folio_test_large(src)) { 2211 /* 2212 * clear __GFP_RECLAIM to make the migration callback 2213 * consistent with regular THP allocations. 2214 */ 2215 gfp_mask &= ~__GFP_RECLAIM; 2216 gfp_mask |= GFP_TRANSHUGE; 2217 order = folio_order(src); 2218 } 2219 zidx = folio_zonenum(src); 2220 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE) 2221 gfp_mask |= __GFP_HIGHMEM; 2222 2223 return __folio_alloc(gfp_mask, order, nid, mtc->nmask); 2224 } 2225 2226 #ifdef CONFIG_NUMA_MIGRATION 2227 static int store_status(int __user *status, int start, int value, int nr) 2228 { 2229 while (nr-- > 0) { 2230 if (put_user(value, status + start)) 2231 return -EFAULT; 2232 start++; 2233 } 2234 2235 return 0; 2236 } 2237 2238 static int do_move_pages_to_node(struct list_head *pagelist, int node) 2239 { 2240 int err; 2241 struct migration_target_control mtc = { 2242 .nid = node, 2243 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 2244 .reason = MR_SYSCALL, 2245 }; 2246 2247 err = migrate_pages(pagelist, alloc_migration_target, NULL, 2248 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); 2249 if (err) 2250 putback_movable_pages(pagelist); 2251 return err; 2252 } 2253 2254 static int __add_folio_for_migration(struct folio *folio, int node, 2255 struct list_head *pagelist, bool migrate_all) 2256 { 2257 if (is_zero_folio(folio) || is_huge_zero_folio(folio)) 2258 return -EFAULT; 2259 2260 if (folio_is_zone_device(folio)) 2261 return -ENOENT; 2262 2263 if (folio_nid(folio) == node) 2264 return 0; 2265 2266 if (folio_maybe_mapped_shared(folio) && !migrate_all) 2267 return -EACCES; 2268 2269 if (folio_test_hugetlb(folio)) { 2270 if (folio_isolate_hugetlb(folio, pagelist)) 2271 return 1; 2272 } else if (folio_isolate_lru(folio)) { 2273 list_add_tail(&folio->lru, pagelist); 2274 node_stat_mod_folio(folio, 2275 NR_ISOLATED_ANON + folio_is_file_lru(folio), 2276 folio_nr_pages(folio)); 2277 return 1; 2278 } 2279 return -EBUSY; 2280 } 2281 2282 /* 2283 * Resolves the given address to a struct folio, isolates it from the LRU and 2284 * puts it to the given pagelist. 2285 * Returns: 2286 * errno - if the folio cannot be found/isolated 2287 * 0 - when it doesn't have to be migrated because it is already on the 2288 * target node 2289 * 1 - when it has been queued 2290 */ 2291 static int add_folio_for_migration(struct mm_struct *mm, const void __user *p, 2292 int node, struct list_head *pagelist, bool migrate_all) 2293 { 2294 struct vm_area_struct *vma; 2295 struct folio_walk fw; 2296 struct folio *folio; 2297 unsigned long addr; 2298 int err = -EFAULT; 2299 2300 mmap_read_lock(mm); 2301 addr = (unsigned long)untagged_addr_remote(mm, p); 2302 2303 vma = vma_lookup(mm, addr); 2304 if (vma && vma_migratable(vma)) { 2305 folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE); 2306 if (folio) { 2307 err = __add_folio_for_migration(folio, node, pagelist, 2308 migrate_all); 2309 folio_walk_end(&fw, vma); 2310 } else { 2311 err = -ENOENT; 2312 } 2313 } 2314 mmap_read_unlock(mm); 2315 return err; 2316 } 2317 2318 static int move_pages_and_store_status(int node, 2319 struct list_head *pagelist, int __user *status, 2320 int start, int i, unsigned long nr_pages) 2321 { 2322 int err; 2323 2324 if (list_empty(pagelist)) 2325 return 0; 2326 2327 err = do_move_pages_to_node(pagelist, node); 2328 if (err) { 2329 /* 2330 * Positive err means the number of failed 2331 * pages to migrate. Since we are going to 2332 * abort and return the number of non-migrated 2333 * pages, so need to include the rest of the 2334 * nr_pages that have not been attempted as 2335 * well. 2336 */ 2337 if (err > 0) 2338 err += nr_pages - i; 2339 return err; 2340 } 2341 return store_status(status, start, node, i - start); 2342 } 2343 2344 /* 2345 * Migrate an array of page address onto an array of nodes and fill 2346 * the corresponding array of status. 2347 */ 2348 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, 2349 unsigned long nr_pages, 2350 const void __user * __user *pages, 2351 const int __user *nodes, 2352 int __user *status, int flags) 2353 { 2354 compat_uptr_t __user *compat_pages = (void __user *)pages; 2355 int current_node = NUMA_NO_NODE; 2356 LIST_HEAD(pagelist); 2357 int start, i; 2358 int err = 0, err1; 2359 2360 lru_cache_disable(); 2361 2362 for (i = start = 0; i < nr_pages; i++) { 2363 const void __user *p; 2364 int node; 2365 2366 err = -EFAULT; 2367 if (in_compat_syscall()) { 2368 compat_uptr_t cp; 2369 2370 if (get_user(cp, compat_pages + i)) 2371 goto out_flush; 2372 2373 p = compat_ptr(cp); 2374 } else { 2375 if (get_user(p, pages + i)) 2376 goto out_flush; 2377 } 2378 if (get_user(node, nodes + i)) 2379 goto out_flush; 2380 2381 err = -ENODEV; 2382 if (node < 0 || node >= MAX_NUMNODES) 2383 goto out_flush; 2384 if (!node_state(node, N_MEMORY)) 2385 goto out_flush; 2386 2387 err = -EACCES; 2388 if (!node_isset(node, task_nodes)) 2389 goto out_flush; 2390 2391 if (current_node == NUMA_NO_NODE) { 2392 current_node = node; 2393 start = i; 2394 } else if (node != current_node) { 2395 err = move_pages_and_store_status(current_node, 2396 &pagelist, status, start, i, nr_pages); 2397 if (err) 2398 goto out; 2399 start = i; 2400 current_node = node; 2401 } 2402 2403 /* 2404 * Errors in the page lookup or isolation are not fatal and we simply 2405 * report them via status 2406 */ 2407 err = add_folio_for_migration(mm, p, current_node, &pagelist, 2408 flags & MPOL_MF_MOVE_ALL); 2409 2410 if (err > 0) { 2411 /* The page is successfully queued for migration */ 2412 continue; 2413 } 2414 2415 /* 2416 * If the page is already on the target node (!err), store the 2417 * node, otherwise, store the err. 2418 */ 2419 err = store_status(status, i, err ? : current_node, 1); 2420 if (err) 2421 goto out_flush; 2422 2423 err = move_pages_and_store_status(current_node, &pagelist, 2424 status, start, i, nr_pages); 2425 if (err) { 2426 /* We have accounted for page i */ 2427 if (err > 0) 2428 err--; 2429 goto out; 2430 } 2431 current_node = NUMA_NO_NODE; 2432 } 2433 out_flush: 2434 /* Make sure we do not overwrite the existing error */ 2435 err1 = move_pages_and_store_status(current_node, &pagelist, 2436 status, start, i, nr_pages); 2437 if (err >= 0) 2438 err = err1; 2439 out: 2440 lru_cache_enable(); 2441 return err; 2442 } 2443 2444 /* 2445 * Determine the nodes of an array of pages and store it in an array of status. 2446 */ 2447 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, 2448 const void __user **pages, int *status) 2449 { 2450 unsigned long i; 2451 2452 mmap_read_lock(mm); 2453 2454 for (i = 0; i < nr_pages; i++) { 2455 unsigned long addr = (unsigned long)(*pages); 2456 struct vm_area_struct *vma; 2457 struct folio_walk fw; 2458 struct folio *folio; 2459 int err = -EFAULT; 2460 2461 vma = vma_lookup(mm, addr); 2462 if (!vma) 2463 goto set_status; 2464 2465 folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE); 2466 if (folio) { 2467 if (is_zero_folio(folio) || is_huge_zero_folio(folio)) 2468 err = -EFAULT; 2469 else if (folio_is_zone_device(folio)) 2470 err = -ENOENT; 2471 else 2472 err = folio_nid(folio); 2473 folio_walk_end(&fw, vma); 2474 } else { 2475 err = -ENOENT; 2476 } 2477 set_status: 2478 *status = err; 2479 2480 pages++; 2481 status++; 2482 } 2483 2484 mmap_read_unlock(mm); 2485 } 2486 2487 static int get_compat_pages_array(const void __user *chunk_pages[], 2488 const void __user * __user *pages, 2489 unsigned long chunk_offset, 2490 unsigned long chunk_nr) 2491 { 2492 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages; 2493 compat_uptr_t p; 2494 int i; 2495 2496 for (i = 0; i < chunk_nr; i++) { 2497 if (get_user(p, pages32 + chunk_offset + i)) 2498 return -EFAULT; 2499 chunk_pages[i] = compat_ptr(p); 2500 } 2501 2502 return 0; 2503 } 2504 2505 /* 2506 * Determine the nodes of a user array of pages and store it in 2507 * a user array of status. 2508 */ 2509 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, 2510 const void __user * __user *pages, 2511 int __user *status) 2512 { 2513 #define DO_PAGES_STAT_CHUNK_NR 16UL 2514 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; 2515 int chunk_status[DO_PAGES_STAT_CHUNK_NR]; 2516 unsigned long chunk_offset = 0; 2517 2518 while (nr_pages) { 2519 unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR); 2520 2521 if (in_compat_syscall()) { 2522 if (get_compat_pages_array(chunk_pages, pages, 2523 chunk_offset, chunk_nr)) 2524 break; 2525 } else { 2526 if (copy_from_user(chunk_pages, pages + chunk_offset, 2527 chunk_nr * sizeof(*chunk_pages))) 2528 break; 2529 } 2530 2531 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); 2532 2533 if (copy_to_user(status + chunk_offset, chunk_status, 2534 chunk_nr * sizeof(*status))) 2535 break; 2536 2537 chunk_offset += chunk_nr; 2538 nr_pages -= chunk_nr; 2539 } 2540 return nr_pages ? -EFAULT : 0; 2541 } 2542 2543 static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) 2544 { 2545 struct task_struct *task; 2546 struct mm_struct *mm; 2547 2548 /* 2549 * There is no need to check if current process has the right to modify 2550 * the specified process when they are same. 2551 */ 2552 if (!pid) { 2553 mmget(current->mm); 2554 *mem_nodes = cpuset_mems_allowed(current); 2555 return current->mm; 2556 } 2557 2558 task = find_get_task_by_vpid(pid); 2559 if (!task) 2560 return ERR_PTR(-ESRCH); 2561 2562 if (down_read_killable(&task->signal->exec_update_lock)) { 2563 mm = ERR_PTR(-EINTR); 2564 goto out; 2565 } 2566 /* 2567 * Check if this process has the right to modify the specified 2568 * process. Use the regular "ptrace_may_access()" checks. 2569 */ 2570 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { 2571 mm = ERR_PTR(-EPERM); 2572 goto unlock; 2573 } 2574 2575 mm = ERR_PTR(security_task_movememory(task)); 2576 if (IS_ERR(mm)) 2577 goto unlock; 2578 *mem_nodes = cpuset_mems_allowed(task); 2579 mm = get_task_mm(task); 2580 unlock: 2581 up_read(&task->signal->exec_update_lock); 2582 out: 2583 put_task_struct(task); 2584 if (!mm) 2585 mm = ERR_PTR(-EINVAL); 2586 return mm; 2587 } 2588 2589 /* 2590 * Move a list of pages in the address space of the currently executing 2591 * process. 2592 */ 2593 static int kernel_move_pages(pid_t pid, unsigned long nr_pages, 2594 const void __user * __user *pages, 2595 const int __user *nodes, 2596 int __user *status, int flags) 2597 { 2598 struct mm_struct *mm; 2599 int err; 2600 nodemask_t task_nodes; 2601 2602 /* Check flags */ 2603 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) 2604 return -EINVAL; 2605 2606 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) 2607 return -EPERM; 2608 2609 mm = find_mm_struct(pid, &task_nodes); 2610 if (IS_ERR(mm)) 2611 return PTR_ERR(mm); 2612 2613 if (nodes) 2614 err = do_pages_move(mm, task_nodes, nr_pages, pages, 2615 nodes, status, flags); 2616 else 2617 err = do_pages_stat(mm, nr_pages, pages, status); 2618 2619 mmput(mm); 2620 return err; 2621 } 2622 2623 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, 2624 const void __user * __user *, pages, 2625 const int __user *, nodes, 2626 int __user *, status, int, flags) 2627 { 2628 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); 2629 } 2630 #endif /* CONFIG_NUMA_MIGRATION */ 2631 2632 #ifdef CONFIG_NUMA_BALANCING 2633 /* 2634 * Returns true if this is a safe migration target node for misplaced NUMA 2635 * pages. Currently it only checks the watermarks which is crude. 2636 */ 2637 static bool migrate_balanced_pgdat(struct pglist_data *pgdat, 2638 unsigned long nr_migrate_pages) 2639 { 2640 int z; 2641 2642 for (z = pgdat->nr_zones - 1; z >= 0; z--) { 2643 struct zone *zone = pgdat->node_zones + z; 2644 2645 if (!managed_zone(zone)) 2646 continue; 2647 2648 /* Avoid waking kswapd by allocating pages_to_migrate pages. */ 2649 if (!zone_watermark_ok(zone, 0, 2650 high_wmark_pages(zone) + 2651 nr_migrate_pages, 2652 ZONE_MOVABLE, ALLOC_CMA)) 2653 continue; 2654 return true; 2655 } 2656 return false; 2657 } 2658 2659 static struct folio *alloc_misplaced_dst_folio(struct folio *src, 2660 unsigned long data) 2661 { 2662 int nid = (int) data; 2663 int order = folio_order(src); 2664 gfp_t gfp = __GFP_THISNODE; 2665 2666 if (order > 0) 2667 gfp |= GFP_TRANSHUGE_LIGHT; 2668 else { 2669 gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY | 2670 __GFP_NOWARN; 2671 gfp &= ~__GFP_RECLAIM; 2672 } 2673 return __folio_alloc_node(gfp, order, nid); 2674 } 2675 2676 /* 2677 * Prepare for calling migrate_misplaced_folio() by isolating the folio if 2678 * permitted. Must be called with the PTL still held. 2679 */ 2680 int migrate_misplaced_folio_prepare(struct folio *folio, 2681 struct vm_area_struct *vma, int node) 2682 { 2683 int nr_pages = folio_nr_pages(folio); 2684 pg_data_t *pgdat = NODE_DATA(node); 2685 2686 if (folio_is_file_lru(folio)) { 2687 /* 2688 * Do not migrate file folios that are mapped in multiple 2689 * processes with execute permissions as they are probably 2690 * shared libraries. 2691 * 2692 * See folio_maybe_mapped_shared() on possible imprecision 2693 * when we cannot easily detect if a folio is shared. 2694 */ 2695 if ((vma->vm_flags & VM_EXEC) && folio_maybe_mapped_shared(folio)) 2696 return -EACCES; 2697 2698 /* 2699 * Do not migrate dirty folios as not all filesystems can move 2700 * dirty folios in MIGRATE_ASYNC mode which is a waste of 2701 * cycles. 2702 */ 2703 if (folio_test_dirty(folio)) 2704 return -EAGAIN; 2705 } 2706 2707 /* Avoid migrating to a node that is nearly full */ 2708 if (!migrate_balanced_pgdat(pgdat, nr_pages)) { 2709 int z; 2710 2711 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)) 2712 return -EAGAIN; 2713 for (z = pgdat->nr_zones - 1; z >= 0; z--) { 2714 if (managed_zone(pgdat->node_zones + z)) 2715 break; 2716 } 2717 2718 /* 2719 * If there are no managed zones, it should not proceed 2720 * further. 2721 */ 2722 if (z < 0) 2723 return -EAGAIN; 2724 2725 wakeup_kswapd(pgdat->node_zones + z, 0, 2726 folio_order(folio), ZONE_MOVABLE); 2727 return -EAGAIN; 2728 } 2729 2730 if (!folio_isolate_lru(folio)) 2731 return -EAGAIN; 2732 2733 node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), 2734 nr_pages); 2735 return 0; 2736 } 2737 2738 /* 2739 * Attempt to migrate a misplaced folio to the specified destination 2740 * node. Caller is expected to have isolated the folio by calling 2741 * migrate_misplaced_folio_prepare(), which will result in an 2742 * elevated reference count on the folio. This function will un-isolate the 2743 * folio, dereferencing the folio before returning. 2744 */ 2745 int migrate_misplaced_folio(struct folio *folio, int node) 2746 { 2747 pg_data_t *pgdat = NODE_DATA(node); 2748 int nr_remaining; 2749 unsigned int nr_succeeded; 2750 LIST_HEAD(migratepages); 2751 struct mem_cgroup *memcg = get_mem_cgroup_from_folio(folio); 2752 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 2753 2754 list_add(&folio->lru, &migratepages); 2755 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_folio, 2756 NULL, node, MIGRATE_ASYNC, 2757 MR_NUMA_MISPLACED, &nr_succeeded); 2758 if (nr_remaining && !list_empty(&migratepages)) 2759 putback_movable_pages(&migratepages); 2760 if (nr_succeeded) { 2761 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded); 2762 count_memcg_events(memcg, NUMA_PAGE_MIGRATE, nr_succeeded); 2763 if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) 2764 && !node_is_toptier(folio_nid(folio)) 2765 && node_is_toptier(node)) 2766 mod_lruvec_state(lruvec, PGPROMOTE_SUCCESS, nr_succeeded); 2767 } 2768 mem_cgroup_put(memcg); 2769 BUG_ON(!list_empty(&migratepages)); 2770 return nr_remaining ? -EAGAIN : 0; 2771 } 2772 #endif /* CONFIG_NUMA_BALANCING */ 2773