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