1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/mm/page_isolation.c 4 */ 5 6 #include <linux/mm.h> 7 #include <linux/page-isolation.h> 8 #include <linux/pageblock-flags.h> 9 #include <linux/memory.h> 10 #include <linux/hugetlb.h> 11 #include <linux/page_owner.h> 12 #include <linux/migrate.h> 13 #include "internal.h" 14 15 #define CREATE_TRACE_POINTS 16 #include <trace/events/page_isolation.h> 17 18 bool page_is_unmovable(struct zone *zone, struct page *page, 19 enum pb_isolate_mode mode, unsigned long *step) 20 { 21 /* 22 * Both, bootmem allocations and memory holes are marked 23 * PG_reserved and are unmovable. We can even have unmovable 24 * allocations inside ZONE_MOVABLE, for example when 25 * specifying "movablecore". 26 */ 27 if (PageReserved(page)) 28 return true; 29 30 /* 31 * If the zone is movable and we have ruled out all reserved 32 * pages then it should be reasonably safe to assume the rest 33 * is movable. 34 */ 35 if (zone_idx(zone) == ZONE_MOVABLE) 36 return false; 37 38 /* 39 * Hugepages are not in LRU lists, but they're movable. 40 * THPs are on the LRU, but need to be counted as #small pages. 41 * We need not scan over tail pages because we don't 42 * handle each tail page individually in migration. 43 */ 44 if (PageCompound(page)) { 45 struct folio *folio = page_folio(page); 46 unsigned long nr_pages, pfn; 47 unsigned int order; 48 49 order = compound_order(&folio->page); 50 if (order > MAX_FOLIO_ORDER) 51 return true; 52 53 if (folio_test_hugetlb(folio)) { 54 struct hstate *h; 55 56 if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION)) 57 return true; 58 59 /* 60 * The huge page may be freed so can not 61 * use folio_hstate() directly. 62 */ 63 h = size_to_hstate(PAGE_SIZE << order); 64 if (!h || !hugepage_migration_supported(h)) 65 return true; 66 } else if (!PageLRU(page)) { 67 return true; 68 } 69 70 nr_pages = 1UL << order; 71 pfn = page_to_pfn(page); 72 *step = (pfn | (nr_pages - 1)) + 1 - pfn; 73 return false; 74 } 75 76 /* 77 * We can't use page_count without pin a page 78 * because another CPU can free compound page. 79 * This check already skips compound tails of THP 80 * because their page->_refcount is zero at all time. 81 */ 82 if (!page_ref_count(page)) { 83 if (PageBuddy(page)) 84 *step = (1 << buddy_order(page)); 85 return false; 86 } 87 88 /* 89 * The HWPoisoned page may be not in buddy system, and 90 * page_count() is not 0. 91 */ 92 if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageHWPoison(page)) 93 return false; 94 95 /* 96 * We treat all PageOffline() pages as movable when offlining 97 * to give drivers a chance to decrement their reference count 98 * in MEM_GOING_OFFLINE in order to indicate that these pages 99 * can be offlined as there are no direct references anymore. 100 * For actually unmovable PageOffline() where the driver does 101 * not support this, we will fail later when trying to actually 102 * move these pages that still have a reference count > 0. 103 * (false negatives in this function only) 104 */ 105 if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageOffline(page)) 106 return false; 107 108 if (PageLRU(page) || page_has_movable_ops(page)) 109 return false; 110 111 /* 112 * If there are RECLAIMABLE pages, we need to check 113 * it. But now, memory offline itself doesn't call 114 * shrink_node_slabs() and it still to be fixed. 115 */ 116 return true; 117 } 118 119 /* 120 * This function checks whether the range [start_pfn, end_pfn) includes 121 * unmovable pages or not. The range must fall into a single pageblock and 122 * consequently belong to a single zone. 123 * 124 * PageLRU check without isolation or lru_lock could race so that 125 * MIGRATE_MOVABLE block might include unmovable pages. Similarly, pages 126 * with movable_ops can only be identified some time after they were 127 * allocated. So you can't expect this function should be exact. 128 * 129 * Returns a page without holding a reference. If the caller wants to 130 * dereference that page (e.g., dumping), it has to make sure that it 131 * cannot get removed (e.g., via memory unplug) concurrently. 132 * 133 */ 134 static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn, 135 enum pb_isolate_mode mode) 136 { 137 struct page *page = pfn_to_page(start_pfn); 138 struct zone *zone = page_zone(page); 139 140 VM_BUG_ON(pageblock_start_pfn(start_pfn) != 141 pageblock_start_pfn(end_pfn - 1)); 142 143 if (is_migrate_cma_page(page)) { 144 /* 145 * CMA allocations (alloc_contig_range) really need to mark 146 * isolate CMA pageblocks even when they are not movable in fact 147 * so consider them movable here. 148 */ 149 if (mode == PB_ISOLATE_MODE_CMA_ALLOC) 150 return NULL; 151 152 return page; 153 } 154 155 while (start_pfn < end_pfn) { 156 unsigned long step = 1; 157 158 page = pfn_to_page(start_pfn); 159 if (page_is_unmovable(zone, page, mode, &step)) 160 return page; 161 162 start_pfn += step; 163 } 164 return NULL; 165 } 166 167 /* 168 * This function set pageblock migratetype to isolate if no unmovable page is 169 * present in [start_pfn, end_pfn). The pageblock must intersect with 170 * [start_pfn, end_pfn). 171 */ 172 static int set_migratetype_isolate(struct page *page, enum pb_isolate_mode mode, 173 unsigned long start_pfn, unsigned long end_pfn) 174 { 175 struct zone *zone = page_zone(page); 176 struct page *unmovable; 177 unsigned long check_unmovable_start, check_unmovable_end; 178 179 if (PageUnaccepted(page)) 180 accept_page(page); 181 182 scoped_guard(spinlock_irqsave, &zone->lock) { 183 /* 184 * We assume the caller intended to SET migrate type to 185 * isolate. If it is already set, then someone else must have 186 * raced and set it before us. 187 */ 188 if (is_migrate_isolate_page(page)) 189 return -EBUSY; 190 191 /* 192 * FIXME: Now, memory hotplug doesn't call shrink_slab() by 193 * itself. We just check MOVABLE pages. 194 * 195 * Pass the intersection of [start_pfn, end_pfn) and the page's 196 * pageblock to avoid redundant checks. 197 */ 198 check_unmovable_start = max(page_to_pfn(page), start_pfn); 199 check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)), 200 end_pfn); 201 202 unmovable = has_unmovable_pages(check_unmovable_start, 203 check_unmovable_end, mode); 204 if (!unmovable) { 205 if (!pageblock_isolate_and_move_free_pages(zone, page)) 206 return -EBUSY; 207 zone->nr_isolate_pageblock++; 208 return 0; 209 } 210 } 211 if (mode == PB_ISOLATE_MODE_MEM_OFFLINE) { 212 /* 213 * printk() with zone->lock held will likely trigger a 214 * lockdep splat, so defer it here. 215 */ 216 dump_page(unmovable, "unmovable page"); 217 } 218 219 return -EBUSY; 220 } 221 222 static void unset_migratetype_isolate(struct page *page) 223 { 224 struct zone *zone; 225 bool isolated_page = false; 226 unsigned int order; 227 struct page *buddy; 228 229 zone = page_zone(page); 230 guard(spinlock_irqsave)(&zone->lock); 231 if (!is_migrate_isolate_page(page)) 232 return; 233 234 /* 235 * Because freepage with more than pageblock_order on isolated 236 * pageblock is restricted to merge due to freepage counting problem, 237 * it is possible that there is free buddy page. 238 * move_freepages_block() doesn't care of merge so we need other 239 * approach in order to merge them. Isolation and free will make 240 * these pages to be merged. 241 */ 242 if (PageBuddy(page)) { 243 order = buddy_order(page); 244 if (order >= pageblock_order && order < MAX_PAGE_ORDER) { 245 buddy = find_buddy_page_pfn(page, page_to_pfn(page), 246 order, NULL); 247 if (buddy && !is_migrate_isolate_page(buddy)) { 248 isolated_page = !!__isolate_free_page(page, order); 249 /* 250 * Isolating a free page in an isolated pageblock 251 * is expected to always work as watermarks don't 252 * apply here. 253 */ 254 VM_WARN_ON(!isolated_page); 255 } 256 } 257 } 258 259 /* 260 * If we isolate freepage with more than pageblock_order, there 261 * should be no freepage in the range, so we could avoid costly 262 * pageblock scanning for freepage moving. 263 * 264 * We didn't actually touch any of the isolated pages, so place them 265 * to the tail of the freelist. This is an optimization for memory 266 * onlining - just onlined memory won't immediately be considered for 267 * allocation. 268 */ 269 if (!isolated_page) { 270 /* 271 * Isolating this block already succeeded, so this 272 * should not fail on zone boundaries. 273 */ 274 WARN_ON_ONCE(!pageblock_unisolate_and_move_free_pages(zone, page)); 275 } else { 276 clear_pageblock_isolate(page); 277 __putback_isolated_page(page, order, get_pageblock_migratetype(page)); 278 } 279 zone->nr_isolate_pageblock--; 280 } 281 282 static inline struct page * 283 __first_valid_page(unsigned long pfn, unsigned long nr_pages) 284 { 285 int i; 286 287 for (i = 0; i < nr_pages; i++) { 288 struct page *page; 289 290 page = pfn_to_online_page(pfn + i); 291 if (!page) 292 continue; 293 return page; 294 } 295 return NULL; 296 } 297 298 /** 299 * isolate_single_pageblock() -- tries to isolate a pageblock that might be 300 * within a free or in-use page. 301 * @boundary_pfn: pageblock-aligned pfn that a page might cross 302 * @mode: isolation mode 303 * @isolate_before: isolate the pageblock before the boundary_pfn 304 * @skip_isolation: the flag to skip the pageblock isolation in second 305 * isolate_single_pageblock() 306 * 307 * Free and in-use pages can be as big as MAX_PAGE_ORDER and contain more than one 308 * pageblock. When not all pageblocks within a page are isolated at the same 309 * time, free page accounting can go wrong. For example, in the case of 310 * MAX_PAGE_ORDER = pageblock_order + 1, a MAX_PAGE_ORDER page has two 311 * pageblocks. 312 * [ MAX_PAGE_ORDER ] 313 * [ pageblock0 | pageblock1 ] 314 * When either pageblock is isolated, if it is a free page, the page is not 315 * split into separate migratetype lists, which is supposed to; if it is an 316 * in-use page and freed later, __free_one_page() does not split the free page 317 * either. The function handles this by splitting the free page or migrating 318 * the in-use page then splitting the free page. 319 */ 320 static int isolate_single_pageblock(unsigned long boundary_pfn, 321 enum pb_isolate_mode mode, bool isolate_before, 322 bool skip_isolation) 323 { 324 unsigned long start_pfn; 325 unsigned long isolate_pageblock; 326 unsigned long pfn; 327 struct zone *zone; 328 int ret; 329 330 VM_BUG_ON(!pageblock_aligned(boundary_pfn)); 331 332 if (isolate_before) 333 isolate_pageblock = boundary_pfn - pageblock_nr_pages; 334 else 335 isolate_pageblock = boundary_pfn; 336 337 /* 338 * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid 339 * only isolating a subset of pageblocks from a bigger than pageblock 340 * free or in-use page. Also make sure all to-be-isolated pageblocks 341 * are within the same zone. 342 */ 343 zone = page_zone(pfn_to_page(isolate_pageblock)); 344 start_pfn = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES), 345 zone->zone_start_pfn); 346 347 if (skip_isolation) { 348 VM_BUG_ON(!get_pageblock_isolate(pfn_to_page(isolate_pageblock))); 349 } else { 350 ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), 351 mode, isolate_pageblock, 352 isolate_pageblock + pageblock_nr_pages); 353 354 if (ret) 355 return ret; 356 } 357 358 /* 359 * Bail out early when the to-be-isolated pageblock does not form 360 * a free or in-use page across boundary_pfn: 361 * 362 * 1. isolate before boundary_pfn: the page after is not online 363 * 2. isolate after boundary_pfn: the page before is not online 364 * 365 * This also ensures correctness. Without it, when isolate after 366 * boundary_pfn and [start_pfn, boundary_pfn) are not online, 367 * __first_valid_page() will return unexpected NULL in the for loop 368 * below. 369 */ 370 if (isolate_before) { 371 if (!pfn_to_online_page(boundary_pfn)) 372 return 0; 373 } else { 374 if (!pfn_to_online_page(boundary_pfn - 1)) 375 return 0; 376 } 377 378 for (pfn = start_pfn; pfn < boundary_pfn;) { 379 struct page *page = __first_valid_page(pfn, boundary_pfn - pfn); 380 381 VM_BUG_ON(!page); 382 pfn = page_to_pfn(page); 383 384 if (PageUnaccepted(page)) { 385 pfn += MAX_ORDER_NR_PAGES; 386 continue; 387 } 388 389 if (PageBuddy(page)) { 390 int order = buddy_order(page); 391 392 /* pageblock_isolate_and_move_free_pages() handled this */ 393 VM_WARN_ON_ONCE(pfn + (1 << order) > boundary_pfn); 394 395 pfn += 1UL << order; 396 continue; 397 } 398 399 /* 400 * If a compound page is straddling our block, attempt 401 * to migrate it out of the way. 402 * 403 * We don't have to worry about this creating a large 404 * free page that straddles into our block: gigantic 405 * pages are freed as order-0 chunks, and LRU pages 406 * (currently) do not exceed pageblock_order. 407 * 408 * The block of interest has already been marked 409 * MIGRATE_ISOLATE above, so when migration is done it 410 * will free its pages onto the correct freelists. 411 */ 412 if (PageCompound(page)) { 413 struct page *head = compound_head(page); 414 unsigned long head_pfn = page_to_pfn(head); 415 unsigned long nr_pages = compound_nr(head); 416 417 if (head_pfn + nr_pages <= boundary_pfn || 418 PageHuge(page)) { 419 pfn = head_pfn + nr_pages; 420 continue; 421 } 422 423 /* 424 * These pages are movable too, but they're 425 * not expected to exceed pageblock_order. 426 * 427 * Let us know when they do, so we can add 428 * proper free and split handling for them. 429 */ 430 VM_WARN_ON_ONCE_PAGE(PageLRU(page), page); 431 VM_WARN_ON_ONCE_PAGE(page_has_movable_ops(page), page); 432 433 goto failed; 434 } 435 436 pfn++; 437 } 438 return 0; 439 failed: 440 /* restore the original migratetype */ 441 if (!skip_isolation) 442 unset_migratetype_isolate(pfn_to_page(isolate_pageblock)); 443 return -EBUSY; 444 } 445 446 /** 447 * start_isolate_page_range() - mark page range MIGRATE_ISOLATE 448 * @start_pfn: The first PFN of the range to be isolated. 449 * @end_pfn: The last PFN of the range to be isolated. 450 * @mode: isolation mode 451 * 452 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in 453 * the range will never be allocated. Any free pages and pages freed in the 454 * future will not be allocated again. If specified range includes migrate types 455 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all 456 * pages in the range finally, the caller have to free all pages in the range. 457 * test_page_isolated() can be used for test it. 458 * 459 * The function first tries to isolate the pageblocks at the beginning and end 460 * of the range, since there might be pages across the range boundaries. 461 * Afterwards, it isolates the rest of the range. 462 * 463 * There is no high level synchronization mechanism that prevents two threads 464 * from trying to isolate overlapping ranges. If this happens, one thread 465 * will notice pageblocks in the overlapping range already set to isolate. 466 * This happens in set_migratetype_isolate, and set_migratetype_isolate 467 * returns an error. We then clean up by restoring the migration type on 468 * pageblocks we may have modified and return -EBUSY to caller. This 469 * prevents two threads from simultaneously working on overlapping ranges. 470 * 471 * Please note that there is no strong synchronization with the page allocator 472 * either. Pages might be freed while their page blocks are marked ISOLATED. 473 * A call to drain_all_pages() after isolation can flush most of them. However 474 * in some cases pages might still end up on pcp lists and that would allow 475 * for their allocation even when they are in fact isolated already. Depending 476 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable() 477 * might be used to flush and disable pcplist before isolation and enable after 478 * unisolation. 479 * 480 * Return: 0 on success and -EBUSY if any part of range cannot be isolated. 481 */ 482 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, 483 enum pb_isolate_mode mode) 484 { 485 unsigned long pfn; 486 struct page *page; 487 /* isolation is done at page block granularity */ 488 unsigned long isolate_start = pageblock_start_pfn(start_pfn); 489 unsigned long isolate_end = pageblock_align(end_pfn); 490 int ret; 491 bool skip_isolation = false; 492 493 /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */ 494 ret = isolate_single_pageblock(isolate_start, mode, false, 495 skip_isolation); 496 if (ret) 497 return ret; 498 499 if (isolate_start == isolate_end - pageblock_nr_pages) 500 skip_isolation = true; 501 502 /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */ 503 ret = isolate_single_pageblock(isolate_end, mode, true, skip_isolation); 504 if (ret) { 505 unset_migratetype_isolate(pfn_to_page(isolate_start)); 506 return ret; 507 } 508 509 /* skip isolated pageblocks at the beginning and end */ 510 for (pfn = isolate_start + pageblock_nr_pages; 511 pfn < isolate_end - pageblock_nr_pages; 512 pfn += pageblock_nr_pages) { 513 page = __first_valid_page(pfn, pageblock_nr_pages); 514 if (page && set_migratetype_isolate(page, mode, start_pfn, 515 end_pfn)) { 516 undo_isolate_page_range(isolate_start, pfn); 517 unset_migratetype_isolate( 518 pfn_to_page(isolate_end - pageblock_nr_pages)); 519 return -EBUSY; 520 } 521 } 522 return 0; 523 } 524 525 /** 526 * undo_isolate_page_range - undo effects of start_isolate_page_range() 527 * @start_pfn: The first PFN of the isolated range 528 * @end_pfn: The last PFN of the isolated range 529 * 530 * This finds and unsets every MIGRATE_ISOLATE page block in the given range 531 */ 532 void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn) 533 { 534 unsigned long pfn; 535 struct page *page; 536 unsigned long isolate_start = pageblock_start_pfn(start_pfn); 537 unsigned long isolate_end = pageblock_align(end_pfn); 538 539 for (pfn = isolate_start; 540 pfn < isolate_end; 541 pfn += pageblock_nr_pages) { 542 page = __first_valid_page(pfn, pageblock_nr_pages); 543 if (!page || !is_migrate_isolate_page(page)) 544 continue; 545 unset_migratetype_isolate(page); 546 } 547 } 548 /* 549 * Test all pages in the range is free(means isolated) or not. 550 * all pages in [start_pfn...end_pfn) must be in the same zone. 551 * zone->lock must be held before call this. 552 * 553 * Returns the last tested pfn. 554 */ 555 static unsigned long 556 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, 557 enum pb_isolate_mode mode) 558 { 559 struct page *page; 560 561 while (pfn < end_pfn) { 562 page = pfn_to_page(pfn); 563 if (PageBuddy(page)) 564 /* 565 * If the page is on a free list, it has to be on 566 * the correct MIGRATE_ISOLATE freelist. There is no 567 * simple way to verify that as VM_BUG_ON(), though. 568 */ 569 pfn += 1 << buddy_order(page); 570 else if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && 571 PageHWPoison(page)) 572 /* A HWPoisoned page cannot be also PageBuddy */ 573 pfn++; 574 else if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && 575 PageOffline(page) && !page_count(page)) 576 /* 577 * The responsible driver agreed to skip PageOffline() 578 * pages when offlining memory by dropping its 579 * reference in MEM_GOING_OFFLINE. 580 */ 581 pfn++; 582 else 583 break; 584 } 585 586 return pfn; 587 } 588 589 /** 590 * test_pages_isolated - check if pageblocks in range are isolated 591 * @start_pfn: The first PFN of the isolated range 592 * @end_pfn: The first PFN *after* the isolated range 593 * @mode: Testing mode 594 * 595 * This tests if all in the specified range are free. 596 * 597 * If %PB_ISOLATE_MODE_MEM_OFFLINE specified in @mode, it will consider 598 * poisoned and offlined pages free as well. 599 * 600 * Caller must ensure the requested range doesn't span zones. 601 * 602 * Returns 0 if true, -EBUSY if one or more pages are in use. 603 */ 604 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn, 605 enum pb_isolate_mode mode) 606 { 607 unsigned long pfn, flags; 608 struct page *page; 609 struct zone *zone; 610 int ret; 611 612 /* 613 * Due to the deferred freeing of hugetlb folios, the hugepage folios may 614 * not immediately release to the buddy system. This can cause PageBuddy() 615 * to fail in __test_page_isolated_in_pageblock(). To ensure that the 616 * hugetlb folios are properly released back to the buddy system, we 617 * invoke the wait_for_freed_hugetlb_folios() function to wait for the 618 * release to complete. 619 */ 620 wait_for_freed_hugetlb_folios(); 621 622 /* 623 * Note: pageblock_nr_pages != MAX_PAGE_ORDER. Then, chunks of free 624 * pages are not aligned to pageblock_nr_pages. 625 * Then we just check migratetype first. 626 */ 627 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 628 page = __first_valid_page(pfn, pageblock_nr_pages); 629 if (page && !is_migrate_isolate_page(page)) 630 break; 631 } 632 page = __first_valid_page(start_pfn, end_pfn - start_pfn); 633 if ((pfn < end_pfn) || !page) { 634 ret = -EBUSY; 635 goto out; 636 } 637 638 /* Check all pages are free or marked as ISOLATED */ 639 zone = page_zone(page); 640 spin_lock_irqsave(&zone->lock, flags); 641 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, mode); 642 spin_unlock_irqrestore(&zone->lock, flags); 643 644 ret = pfn < end_pfn ? -EBUSY : 0; 645 646 out: 647 trace_test_pages_isolated(start_pfn, end_pfn, pfn); 648 649 return ret; 650 } 651