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