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