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