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