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