]>
git.ipfire.org Git - thirdparty/linux.git/blob - mm/page_isolation.c
1 // SPDX-License-Identifier: GPL-2.0
3 * linux/mm/page_isolation.c
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>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/page_isolation.h>
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.
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.
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.
33 static struct page
*has_unmovable_pages(unsigned long start_pfn
, unsigned long end_pfn
,
34 int migratetype
, int flags
)
36 struct page
*page
= pfn_to_page(start_pfn
);
37 struct zone
*zone
= page_zone(page
);
40 VM_BUG_ON(pageblock_start_pfn(start_pfn
) !=
41 pageblock_start_pfn(end_pfn
- 1));
43 if (is_migrate_cma_page(page
)) {
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.
49 if (is_migrate_cma(migratetype
))
55 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++) {
56 page
= pfn_to_page(pfn
);
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".
64 if (PageReserved(page
))
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
72 if (zone_idx(zone
) == ZONE_MOVABLE
)
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.
81 if (PageHuge(page
) || PageTransCompound(page
)) {
82 struct folio
*folio
= page_folio(page
);
83 unsigned int skip_pages
;
86 if (!hugepage_migration_supported(folio_hstate(folio
)))
88 } else if (!folio_test_lru(folio
) && !__folio_test_movable(folio
)) {
92 skip_pages
= folio_nr_pages(folio
) - folio_page_idx(folio
, page
);
93 pfn
+= skip_pages
- 1;
98 * We can't use page_count without pin a page
99 * because another CPU can free compound page.
100 * This check already skips compound tails of THP
101 * because their page->_refcount is zero at all time.
103 if (!page_ref_count(page
)) {
105 pfn
+= (1 << buddy_order(page
)) - 1;
110 * The HWPoisoned page may be not in buddy system, and
111 * page_count() is not 0.
113 if ((flags
& MEMORY_OFFLINE
) && PageHWPoison(page
))
117 * We treat all PageOffline() pages as movable when offlining
118 * to give drivers a chance to decrement their reference count
119 * in MEM_GOING_OFFLINE in order to indicate that these pages
120 * can be offlined as there are no direct references anymore.
121 * For actually unmovable PageOffline() where the driver does
122 * not support this, we will fail later when trying to actually
123 * move these pages that still have a reference count > 0.
124 * (false negatives in this function only)
126 if ((flags
& MEMORY_OFFLINE
) && PageOffline(page
))
129 if (__PageMovable(page
) || PageLRU(page
))
133 * If there are RECLAIMABLE pages, we need to check
134 * it. But now, memory offline itself doesn't call
135 * shrink_node_slabs() and it still to be fixed.
143 * This function set pageblock migratetype to isolate if no unmovable page is
144 * present in [start_pfn, end_pfn). The pageblock must intersect with
145 * [start_pfn, end_pfn).
147 static int set_migratetype_isolate(struct page
*page
, int migratetype
, int isol_flags
,
148 unsigned long start_pfn
, unsigned long end_pfn
)
150 struct zone
*zone
= page_zone(page
);
151 struct page
*unmovable
;
153 unsigned long check_unmovable_start
, check_unmovable_end
;
155 spin_lock_irqsave(&zone
->lock
, flags
);
158 * We assume the caller intended to SET migrate type to isolate.
159 * If it is already set, then someone else must have raced and
162 if (is_migrate_isolate_page(page
)) {
163 spin_unlock_irqrestore(&zone
->lock
, flags
);
168 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
169 * We just check MOVABLE pages.
171 * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
172 * to avoid redundant checks.
174 check_unmovable_start
= max(page_to_pfn(page
), start_pfn
);
175 check_unmovable_end
= min(pageblock_end_pfn(page_to_pfn(page
)),
178 unmovable
= has_unmovable_pages(check_unmovable_start
, check_unmovable_end
,
179 migratetype
, isol_flags
);
181 unsigned long nr_pages
;
182 int mt
= get_pageblock_migratetype(page
);
184 set_pageblock_migratetype(page
, MIGRATE_ISOLATE
);
185 zone
->nr_isolate_pageblock
++;
186 nr_pages
= move_freepages_block(zone
, page
, MIGRATE_ISOLATE
,
189 __mod_zone_freepage_state(zone
, -nr_pages
, mt
);
190 spin_unlock_irqrestore(&zone
->lock
, flags
);
194 spin_unlock_irqrestore(&zone
->lock
, flags
);
195 if (isol_flags
& REPORT_FAILURE
) {
197 * printk() with zone->lock held will likely trigger a
198 * lockdep splat, so defer it here.
200 dump_page(unmovable
, "unmovable page");
206 static void unset_migratetype_isolate(struct page
*page
, int migratetype
)
209 unsigned long flags
, nr_pages
;
210 bool isolated_page
= false;
214 zone
= page_zone(page
);
215 spin_lock_irqsave(&zone
->lock
, flags
);
216 if (!is_migrate_isolate_page(page
))
220 * Because freepage with more than pageblock_order on isolated
221 * pageblock is restricted to merge due to freepage counting problem,
222 * it is possible that there is free buddy page.
223 * move_freepages_block() doesn't care of merge so we need other
224 * approach in order to merge them. Isolation and free will make
225 * these pages to be merged.
227 if (PageBuddy(page
)) {
228 order
= buddy_order(page
);
229 if (order
>= pageblock_order
&& order
< MAX_ORDER
) {
230 buddy
= find_buddy_page_pfn(page
, page_to_pfn(page
),
232 if (buddy
&& !is_migrate_isolate_page(buddy
)) {
233 isolated_page
= !!__isolate_free_page(page
, order
);
235 * Isolating a free page in an isolated pageblock
236 * is expected to always work as watermarks don't
239 VM_WARN_ON(!isolated_page
);
245 * If we isolate freepage with more than pageblock_order, there
246 * should be no freepage in the range, so we could avoid costly
247 * pageblock scanning for freepage moving.
249 * We didn't actually touch any of the isolated pages, so place them
250 * to the tail of the freelist. This is an optimization for memory
251 * onlining - just onlined memory won't immediately be considered for
254 if (!isolated_page
) {
255 nr_pages
= move_freepages_block(zone
, page
, migratetype
, NULL
);
256 __mod_zone_freepage_state(zone
, nr_pages
, migratetype
);
258 set_pageblock_migratetype(page
, migratetype
);
260 __putback_isolated_page(page
, order
, migratetype
);
261 zone
->nr_isolate_pageblock
--;
263 spin_unlock_irqrestore(&zone
->lock
, flags
);
266 static inline struct page
*
267 __first_valid_page(unsigned long pfn
, unsigned long nr_pages
)
271 for (i
= 0; i
< nr_pages
; i
++) {
274 page
= pfn_to_online_page(pfn
+ i
);
283 * isolate_single_pageblock() -- tries to isolate a pageblock that might be
284 * within a free or in-use page.
285 * @boundary_pfn: pageblock-aligned pfn that a page might cross
286 * @flags: isolation flags
287 * @gfp_flags: GFP flags used for migrating pages
288 * @isolate_before: isolate the pageblock before the boundary_pfn
289 * @skip_isolation: the flag to skip the pageblock isolation in second
290 * isolate_single_pageblock()
291 * @migratetype: migrate type to set in error recovery.
293 * Free and in-use pages can be as big as MAX_ORDER and contain more than one
294 * pageblock. When not all pageblocks within a page are isolated at the same
295 * time, free page accounting can go wrong. For example, in the case of
296 * MAX_ORDER = pageblock_order + 1, a MAX_ORDER page has two pagelbocks.
298 * [ pageblock0 | pageblock1 ]
299 * When either pageblock is isolated, if it is a free page, the page is not
300 * split into separate migratetype lists, which is supposed to; if it is an
301 * in-use page and freed later, __free_one_page() does not split the free page
302 * either. The function handles this by splitting the free page or migrating
303 * the in-use page then splitting the free page.
305 static int isolate_single_pageblock(unsigned long boundary_pfn
, int flags
,
306 gfp_t gfp_flags
, bool isolate_before
, bool skip_isolation
,
309 unsigned long start_pfn
;
310 unsigned long isolate_pageblock
;
315 VM_BUG_ON(!pageblock_aligned(boundary_pfn
));
318 isolate_pageblock
= boundary_pfn
- pageblock_nr_pages
;
320 isolate_pageblock
= boundary_pfn
;
323 * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid
324 * only isolating a subset of pageblocks from a bigger than pageblock
325 * free or in-use page. Also make sure all to-be-isolated pageblocks
326 * are within the same zone.
328 zone
= page_zone(pfn_to_page(isolate_pageblock
));
329 start_pfn
= max(ALIGN_DOWN(isolate_pageblock
, MAX_ORDER_NR_PAGES
),
330 zone
->zone_start_pfn
);
332 if (skip_isolation
) {
333 int mt __maybe_unused
= get_pageblock_migratetype(pfn_to_page(isolate_pageblock
));
335 VM_BUG_ON(!is_migrate_isolate(mt
));
337 ret
= set_migratetype_isolate(pfn_to_page(isolate_pageblock
), migratetype
,
338 flags
, isolate_pageblock
, isolate_pageblock
+ pageblock_nr_pages
);
345 * Bail out early when the to-be-isolated pageblock does not form
346 * a free or in-use page across boundary_pfn:
348 * 1. isolate before boundary_pfn: the page after is not online
349 * 2. isolate after boundary_pfn: the page before is not online
351 * This also ensures correctness. Without it, when isolate after
352 * boundary_pfn and [start_pfn, boundary_pfn) are not online,
353 * __first_valid_page() will return unexpected NULL in the for loop
356 if (isolate_before
) {
357 if (!pfn_to_online_page(boundary_pfn
))
360 if (!pfn_to_online_page(boundary_pfn
- 1))
364 for (pfn
= start_pfn
; pfn
< boundary_pfn
;) {
365 struct page
*page
= __first_valid_page(pfn
, boundary_pfn
- pfn
);
368 pfn
= page_to_pfn(page
);
370 * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
371 * free pages in [start_pfn, boundary_pfn), its head page will
372 * always be in the range.
374 if (PageBuddy(page
)) {
375 int order
= buddy_order(page
);
377 if (pfn
+ (1UL << order
) > boundary_pfn
) {
378 /* free page changed before split, check it again */
379 if (split_free_page(page
, order
, boundary_pfn
- pfn
))
387 * migrate compound pages then let the free page handling code
388 * above do the rest. If migration is not possible, just fail.
390 if (PageCompound(page
)) {
391 struct page
*head
= compound_head(page
);
392 unsigned long head_pfn
= page_to_pfn(head
);
393 unsigned long nr_pages
= compound_nr(head
);
395 if (head_pfn
+ nr_pages
<= boundary_pfn
) {
396 pfn
= head_pfn
+ nr_pages
;
399 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
401 * hugetlb, lru compound (THP), and movable compound pages
402 * can be migrated. Otherwise, fail the isolation.
404 if (PageHuge(page
) || PageLRU(page
) || __PageMovable(page
)) {
406 unsigned long outer_pfn
;
407 int page_mt
= get_pageblock_migratetype(page
);
408 bool isolate_page
= !is_migrate_isolate_page(page
);
409 struct compact_control cc
= {
410 .nr_migratepages
= 0,
412 .zone
= page_zone(pfn_to_page(head_pfn
)),
413 .mode
= MIGRATE_SYNC
,
414 .ignore_skip_hint
= true,
415 .no_set_skip_hint
= true,
416 .gfp_mask
= gfp_flags
,
417 .alloc_contig
= true,
419 INIT_LIST_HEAD(&cc
.migratepages
);
422 * XXX: mark the page as MIGRATE_ISOLATE so that
423 * no one else can grab the freed page after migration.
424 * Ideally, the page should be freed as two separate
425 * pages to be added into separate migratetype free
429 ret
= set_migratetype_isolate(page
, page_mt
,
430 flags
, head_pfn
, head_pfn
+ nr_pages
);
435 ret
= __alloc_contig_migrate_range(&cc
, head_pfn
,
436 head_pfn
+ nr_pages
);
439 * restore the page's migratetype so that it can
440 * be split into separate migratetype free lists
444 unset_migratetype_isolate(page
, page_mt
);
449 * reset pfn to the head of the free page, so
450 * that the free page handling code above can split
451 * the free page to the right migratetype list.
453 * head_pfn is not used here as a hugetlb page order
454 * can be bigger than MAX_ORDER, but after it is
455 * freed, the free page order is not. Use pfn within
456 * the range to find the head of the free page.
460 while (!PageBuddy(pfn_to_page(outer_pfn
))) {
461 /* stop if we cannot find the free page */
462 if (++order
> MAX_ORDER
)
464 outer_pfn
&= ~0UL << order
;
477 /* restore the original migratetype */
479 unset_migratetype_isolate(pfn_to_page(isolate_pageblock
), migratetype
);
484 * start_isolate_page_range() - mark page range MIGRATE_ISOLATE
485 * @start_pfn: The first PFN of the range to be isolated.
486 * @end_pfn: The last PFN of the range to be isolated.
487 * @migratetype: Migrate type to set in error recovery.
488 * @flags: The following flags are allowed (they can be combined in
490 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
491 * e.g., skip over PageHWPoison() pages
492 * and PageOffline() pages.
493 * REPORT_FAILURE - report details about the failure to
495 * @gfp_flags: GFP flags used for migrating pages that sit across the
498 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
499 * the range will never be allocated. Any free pages and pages freed in the
500 * future will not be allocated again. If specified range includes migrate types
501 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
502 * pages in the range finally, the caller have to free all pages in the range.
503 * test_page_isolated() can be used for test it.
505 * The function first tries to isolate the pageblocks at the beginning and end
506 * of the range, since there might be pages across the range boundaries.
507 * Afterwards, it isolates the rest of the range.
509 * There is no high level synchronization mechanism that prevents two threads
510 * from trying to isolate overlapping ranges. If this happens, one thread
511 * will notice pageblocks in the overlapping range already set to isolate.
512 * This happens in set_migratetype_isolate, and set_migratetype_isolate
513 * returns an error. We then clean up by restoring the migration type on
514 * pageblocks we may have modified and return -EBUSY to caller. This
515 * prevents two threads from simultaneously working on overlapping ranges.
517 * Please note that there is no strong synchronization with the page allocator
518 * either. Pages might be freed while their page blocks are marked ISOLATED.
519 * A call to drain_all_pages() after isolation can flush most of them. However
520 * in some cases pages might still end up on pcp lists and that would allow
521 * for their allocation even when they are in fact isolated already. Depending
522 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
523 * might be used to flush and disable pcplist before isolation and enable after
526 * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
528 int start_isolate_page_range(unsigned long start_pfn
, unsigned long end_pfn
,
529 int migratetype
, int flags
, gfp_t gfp_flags
)
533 /* isolation is done at page block granularity */
534 unsigned long isolate_start
= pageblock_start_pfn(start_pfn
);
535 unsigned long isolate_end
= pageblock_align(end_pfn
);
537 bool skip_isolation
= false;
539 /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
540 ret
= isolate_single_pageblock(isolate_start
, flags
, gfp_flags
, false,
541 skip_isolation
, migratetype
);
545 if (isolate_start
== isolate_end
- pageblock_nr_pages
)
546 skip_isolation
= true;
548 /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
549 ret
= isolate_single_pageblock(isolate_end
, flags
, gfp_flags
, true,
550 skip_isolation
, migratetype
);
552 unset_migratetype_isolate(pfn_to_page(isolate_start
), migratetype
);
556 /* skip isolated pageblocks at the beginning and end */
557 for (pfn
= isolate_start
+ pageblock_nr_pages
;
558 pfn
< isolate_end
- pageblock_nr_pages
;
559 pfn
+= pageblock_nr_pages
) {
560 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
561 if (page
&& set_migratetype_isolate(page
, migratetype
, flags
,
562 start_pfn
, end_pfn
)) {
563 undo_isolate_page_range(isolate_start
, pfn
, migratetype
);
564 unset_migratetype_isolate(
565 pfn_to_page(isolate_end
- pageblock_nr_pages
),
574 * undo_isolate_page_range - undo effects of start_isolate_page_range()
575 * @start_pfn: The first PFN of the isolated range
576 * @end_pfn: The last PFN of the isolated range
577 * @migratetype: New migrate type to set on the range
579 * This finds every MIGRATE_ISOLATE page block in the given range
580 * and switches it to @migratetype.
582 void undo_isolate_page_range(unsigned long start_pfn
, unsigned long end_pfn
,
587 unsigned long isolate_start
= pageblock_start_pfn(start_pfn
);
588 unsigned long isolate_end
= pageblock_align(end_pfn
);
590 for (pfn
= isolate_start
;
592 pfn
+= pageblock_nr_pages
) {
593 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
594 if (!page
|| !is_migrate_isolate_page(page
))
596 unset_migratetype_isolate(page
, migratetype
);
600 * Test all pages in the range is free(means isolated) or not.
601 * all pages in [start_pfn...end_pfn) must be in the same zone.
602 * zone->lock must be held before call this.
604 * Returns the last tested pfn.
607 __test_page_isolated_in_pageblock(unsigned long pfn
, unsigned long end_pfn
,
612 while (pfn
< end_pfn
) {
613 page
= pfn_to_page(pfn
);
616 * If the page is on a free list, it has to be on
617 * the correct MIGRATE_ISOLATE freelist. There is no
618 * simple way to verify that as VM_BUG_ON(), though.
620 pfn
+= 1 << buddy_order(page
);
621 else if ((flags
& MEMORY_OFFLINE
) && PageHWPoison(page
))
622 /* A HWPoisoned page cannot be also PageBuddy */
624 else if ((flags
& MEMORY_OFFLINE
) && PageOffline(page
) &&
627 * The responsible driver agreed to skip PageOffline()
628 * pages when offlining memory by dropping its
629 * reference in MEM_GOING_OFFLINE.
640 * test_pages_isolated - check if pageblocks in range are isolated
641 * @start_pfn: The first PFN of the isolated range
642 * @end_pfn: The first PFN *after* the isolated range
643 * @isol_flags: Testing mode flags
645 * This tests if all in the specified range are free.
647 * If %MEMORY_OFFLINE is specified in @flags, it will consider
648 * poisoned and offlined pages free as well.
650 * Caller must ensure the requested range doesn't span zones.
652 * Returns 0 if true, -EBUSY if one or more pages are in use.
654 int test_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
,
657 unsigned long pfn
, flags
;
663 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
664 * are not aligned to pageblock_nr_pages.
665 * Then we just check migratetype first.
667 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= pageblock_nr_pages
) {
668 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
669 if (page
&& !is_migrate_isolate_page(page
))
672 page
= __first_valid_page(start_pfn
, end_pfn
- start_pfn
);
673 if ((pfn
< end_pfn
) || !page
) {
678 /* Check all pages are free or marked as ISOLATED */
679 zone
= page_zone(page
);
680 spin_lock_irqsave(&zone
->lock
, flags
);
681 pfn
= __test_page_isolated_in_pageblock(start_pfn
, end_pfn
, isol_flags
);
682 spin_unlock_irqrestore(&zone
->lock
, flags
);
684 ret
= pfn
< end_pfn
? -EBUSY
: 0;
687 trace_test_pages_isolated(start_pfn
, end_pfn
, pfn
);