xref: /linux/mm/page_isolation.c (revision beace86e61e465dba204a268ab3f3377153a4973)
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. Similarly, pages
25  * with movable_ops can only be identified some time after they were
26  * allocated. 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  */
has_unmovable_pages(unsigned long start_pfn,unsigned long end_pfn,enum pb_isolate_mode mode)33 static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn,
34 				enum pb_isolate_mode mode)
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 (mode == PB_ISOLATE_MODE_CMA_ALLOC)
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)) {
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 ((mode == PB_ISOLATE_MODE_MEM_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 ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageOffline(page))
134 			continue;
135 
136 		if (PageLRU(page) || page_has_movable_ops(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  */
set_migratetype_isolate(struct page * page,enum pb_isolate_mode mode,unsigned long start_pfn,unsigned long end_pfn)154 static int set_migratetype_isolate(struct page *page, enum pb_isolate_mode mode,
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 			mode);
190 	if (!unmovable) {
191 		if (!pageblock_isolate_and_move_free_pages(zone, page)) {
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 (mode == PB_ISOLATE_MODE_MEM_OFFLINE) {
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 
unset_migratetype_isolate(struct page * page)212 static void unset_migratetype_isolate(struct page *page)
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(!pageblock_unisolate_and_move_free_pages(zone, page));
266 	} else {
267 		clear_pageblock_isolate(page);
268 		__putback_isolated_page(page, order, get_pageblock_migratetype(page));
269 	}
270 	zone->nr_isolate_pageblock--;
271 out:
272 	spin_unlock_irqrestore(&zone->lock, flags);
273 }
274 
275 static inline struct page *
__first_valid_page(unsigned long pfn,unsigned long nr_pages)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  * @mode:			isolation mode
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  *
300  * Free and in-use pages can be as big as MAX_PAGE_ORDER and contain more than one
301  * pageblock. When not all pageblocks within a page are isolated at the same
302  * time, free page accounting can go wrong. For example, in the case of
303  * MAX_PAGE_ORDER = pageblock_order + 1, a MAX_PAGE_ORDER page has two
304  * pagelbocks.
305  * [      MAX_PAGE_ORDER         ]
306  * [  pageblock0  |  pageblock1  ]
307  * When either pageblock is isolated, if it is a free page, the page is not
308  * split into separate migratetype lists, which is supposed to; if it is an
309  * in-use page and freed later, __free_one_page() does not split the free page
310  * either. The function handles this by splitting the free page or migrating
311  * the in-use page then splitting the free page.
312  */
isolate_single_pageblock(unsigned long boundary_pfn,enum pb_isolate_mode mode,bool isolate_before,bool skip_isolation)313 static int isolate_single_pageblock(unsigned long boundary_pfn,
314 			enum pb_isolate_mode mode, bool isolate_before,
315 			bool skip_isolation)
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 		VM_BUG_ON(!get_pageblock_isolate(pfn_to_page(isolate_pageblock)));
342 	} else {
343 		ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock),
344 				mode, isolate_pageblock,
345 				isolate_pageblock + pageblock_nr_pages);
346 
347 		if (ret)
348 			return ret;
349 	}
350 
351 	/*
352 	 * Bail out early when the to-be-isolated pageblock does not form
353 	 * a free or in-use page across boundary_pfn:
354 	 *
355 	 * 1. isolate before boundary_pfn: the page after is not online
356 	 * 2. isolate after boundary_pfn: the page before is not online
357 	 *
358 	 * This also ensures correctness. Without it, when isolate after
359 	 * boundary_pfn and [start_pfn, boundary_pfn) are not online,
360 	 * __first_valid_page() will return unexpected NULL in the for loop
361 	 * below.
362 	 */
363 	if (isolate_before) {
364 		if (!pfn_to_online_page(boundary_pfn))
365 			return 0;
366 	} else {
367 		if (!pfn_to_online_page(boundary_pfn - 1))
368 			return 0;
369 	}
370 
371 	for (pfn = start_pfn; pfn < boundary_pfn;) {
372 		struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
373 
374 		VM_BUG_ON(!page);
375 		pfn = page_to_pfn(page);
376 
377 		if (PageUnaccepted(page)) {
378 			pfn += MAX_ORDER_NR_PAGES;
379 			continue;
380 		}
381 
382 		if (PageBuddy(page)) {
383 			int order = buddy_order(page);
384 
385 			/* pageblock_isolate_and_move_free_pages() handled this */
386 			VM_WARN_ON_ONCE(pfn + (1 << order) > boundary_pfn);
387 
388 			pfn += 1UL << order;
389 			continue;
390 		}
391 
392 		/*
393 		 * If a compound page is straddling our block, attempt
394 		 * to migrate it out of the way.
395 		 *
396 		 * We don't have to worry about this creating a large
397 		 * free page that straddles into our block: gigantic
398 		 * pages are freed as order-0 chunks, and LRU pages
399 		 * (currently) do not exceed pageblock_order.
400 		 *
401 		 * The block of interest has already been marked
402 		 * MIGRATE_ISOLATE above, so when migration is done it
403 		 * will free its pages onto the correct freelists.
404 		 */
405 		if (PageCompound(page)) {
406 			struct page *head = compound_head(page);
407 			unsigned long head_pfn = page_to_pfn(head);
408 			unsigned long nr_pages = compound_nr(head);
409 
410 			if (head_pfn + nr_pages <= boundary_pfn ||
411 			    PageHuge(page)) {
412 				pfn = head_pfn + nr_pages;
413 				continue;
414 			}
415 
416 			/*
417 			 * These pages are movable too, but they're
418 			 * not expected to exceed pageblock_order.
419 			 *
420 			 * Let us know when they do, so we can add
421 			 * proper free and split handling for them.
422 			 */
423 			VM_WARN_ON_ONCE_PAGE(PageLRU(page), page);
424 			VM_WARN_ON_ONCE_PAGE(page_has_movable_ops(page), page);
425 
426 			goto failed;
427 		}
428 
429 		pfn++;
430 	}
431 	return 0;
432 failed:
433 	/* restore the original migratetype */
434 	if (!skip_isolation)
435 		unset_migratetype_isolate(pfn_to_page(isolate_pageblock));
436 	return -EBUSY;
437 }
438 
439 /**
440  * start_isolate_page_range() - mark page range MIGRATE_ISOLATE
441  * @start_pfn:		The first PFN of the range to be isolated.
442  * @end_pfn:		The last PFN of the range to be isolated.
443  * @mode:		isolation mode
444  *
445  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
446  * the range will never be allocated. Any free pages and pages freed in the
447  * future will not be allocated again. If specified range includes migrate types
448  * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
449  * pages in the range finally, the caller have to free all pages in the range.
450  * test_page_isolated() can be used for test it.
451  *
452  * The function first tries to isolate the pageblocks at the beginning and end
453  * of the range, since there might be pages across the range boundaries.
454  * Afterwards, it isolates the rest of the range.
455  *
456  * There is no high level synchronization mechanism that prevents two threads
457  * from trying to isolate overlapping ranges. If this happens, one thread
458  * will notice pageblocks in the overlapping range already set to isolate.
459  * This happens in set_migratetype_isolate, and set_migratetype_isolate
460  * returns an error. We then clean up by restoring the migration type on
461  * pageblocks we may have modified and return -EBUSY to caller. This
462  * prevents two threads from simultaneously working on overlapping ranges.
463  *
464  * Please note that there is no strong synchronization with the page allocator
465  * either. Pages might be freed while their page blocks are marked ISOLATED.
466  * A call to drain_all_pages() after isolation can flush most of them. However
467  * in some cases pages might still end up on pcp lists and that would allow
468  * for their allocation even when they are in fact isolated already. Depending
469  * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
470  * might be used to flush and disable pcplist before isolation and enable after
471  * unisolation.
472  *
473  * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
474  */
start_isolate_page_range(unsigned long start_pfn,unsigned long end_pfn,enum pb_isolate_mode mode)475 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
476 			     enum pb_isolate_mode mode)
477 {
478 	unsigned long pfn;
479 	struct page *page;
480 	/* isolation is done at page block granularity */
481 	unsigned long isolate_start = pageblock_start_pfn(start_pfn);
482 	unsigned long isolate_end = pageblock_align(end_pfn);
483 	int ret;
484 	bool skip_isolation = false;
485 
486 	/* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
487 	ret = isolate_single_pageblock(isolate_start, mode, false,
488 			skip_isolation);
489 	if (ret)
490 		return ret;
491 
492 	if (isolate_start == isolate_end - pageblock_nr_pages)
493 		skip_isolation = true;
494 
495 	/* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
496 	ret = isolate_single_pageblock(isolate_end, mode, true, skip_isolation);
497 	if (ret) {
498 		unset_migratetype_isolate(pfn_to_page(isolate_start));
499 		return ret;
500 	}
501 
502 	/* skip isolated pageblocks at the beginning and end */
503 	for (pfn = isolate_start + pageblock_nr_pages;
504 	     pfn < isolate_end - pageblock_nr_pages;
505 	     pfn += pageblock_nr_pages) {
506 		page = __first_valid_page(pfn, pageblock_nr_pages);
507 		if (page && set_migratetype_isolate(page, mode, start_pfn,
508 					end_pfn)) {
509 			undo_isolate_page_range(isolate_start, pfn);
510 			unset_migratetype_isolate(
511 				pfn_to_page(isolate_end - pageblock_nr_pages));
512 			return -EBUSY;
513 		}
514 	}
515 	return 0;
516 }
517 
518 /**
519  * undo_isolate_page_range - undo effects of start_isolate_page_range()
520  * @start_pfn:		The first PFN of the isolated range
521  * @end_pfn:		The last PFN of the isolated range
522  *
523  * This finds and unsets every MIGRATE_ISOLATE page block in the given range
524  */
undo_isolate_page_range(unsigned long start_pfn,unsigned long end_pfn)525 void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn)
526 {
527 	unsigned long pfn;
528 	struct page *page;
529 	unsigned long isolate_start = pageblock_start_pfn(start_pfn);
530 	unsigned long isolate_end = pageblock_align(end_pfn);
531 
532 	for (pfn = isolate_start;
533 	     pfn < isolate_end;
534 	     pfn += pageblock_nr_pages) {
535 		page = __first_valid_page(pfn, pageblock_nr_pages);
536 		if (!page || !is_migrate_isolate_page(page))
537 			continue;
538 		unset_migratetype_isolate(page);
539 	}
540 }
541 /*
542  * Test all pages in the range is free(means isolated) or not.
543  * all pages in [start_pfn...end_pfn) must be in the same zone.
544  * zone->lock must be held before call this.
545  *
546  * Returns the last tested pfn.
547  */
548 static unsigned long
__test_page_isolated_in_pageblock(unsigned long pfn,unsigned long end_pfn,enum pb_isolate_mode mode)549 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
550 				  enum pb_isolate_mode mode)
551 {
552 	struct page *page;
553 
554 	while (pfn < end_pfn) {
555 		page = pfn_to_page(pfn);
556 		if (PageBuddy(page))
557 			/*
558 			 * If the page is on a free list, it has to be on
559 			 * the correct MIGRATE_ISOLATE freelist. There is no
560 			 * simple way to verify that as VM_BUG_ON(), though.
561 			 */
562 			pfn += 1 << buddy_order(page);
563 		else if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) &&
564 			 PageHWPoison(page))
565 			/* A HWPoisoned page cannot be also PageBuddy */
566 			pfn++;
567 		else if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) &&
568 			 PageOffline(page) && !page_count(page))
569 			/*
570 			 * The responsible driver agreed to skip PageOffline()
571 			 * pages when offlining memory by dropping its
572 			 * reference in MEM_GOING_OFFLINE.
573 			 */
574 			pfn++;
575 		else
576 			break;
577 	}
578 
579 	return pfn;
580 }
581 
582 /**
583  * test_pages_isolated - check if pageblocks in range are isolated
584  * @start_pfn:		The first PFN of the isolated range
585  * @end_pfn:		The first PFN *after* the isolated range
586  * @mode:		Testing mode
587  *
588  * This tests if all in the specified range are free.
589  *
590  * If %PB_ISOLATE_MODE_MEM_OFFLINE specified in @mode, it will consider
591  * poisoned and offlined pages free as well.
592  *
593  * Caller must ensure the requested range doesn't span zones.
594  *
595  * Returns 0 if true, -EBUSY if one or more pages are in use.
596  */
test_pages_isolated(unsigned long start_pfn,unsigned long end_pfn,enum pb_isolate_mode mode)597 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
598 			enum pb_isolate_mode mode)
599 {
600 	unsigned long pfn, flags;
601 	struct page *page;
602 	struct zone *zone;
603 	int ret;
604 
605 	/*
606 	 * Due to the deferred freeing of hugetlb folios, the hugepage folios may
607 	 * not immediately release to the buddy system. This can cause PageBuddy()
608 	 * to fail in __test_page_isolated_in_pageblock(). To ensure that the
609 	 * hugetlb folios are properly released back to the buddy system, we
610 	 * invoke the wait_for_freed_hugetlb_folios() function to wait for the
611 	 * release to complete.
612 	 */
613 	wait_for_freed_hugetlb_folios();
614 
615 	/*
616 	 * Note: pageblock_nr_pages != MAX_PAGE_ORDER. Then, chunks of free
617 	 * pages are not aligned to pageblock_nr_pages.
618 	 * Then we just check migratetype first.
619 	 */
620 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
621 		page = __first_valid_page(pfn, pageblock_nr_pages);
622 		if (page && !is_migrate_isolate_page(page))
623 			break;
624 	}
625 	page = __first_valid_page(start_pfn, end_pfn - start_pfn);
626 	if ((pfn < end_pfn) || !page) {
627 		ret = -EBUSY;
628 		goto out;
629 	}
630 
631 	/* Check all pages are free or marked as ISOLATED */
632 	zone = page_zone(page);
633 	spin_lock_irqsave(&zone->lock, flags);
634 	pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, mode);
635 	spin_unlock_irqrestore(&zone->lock, flags);
636 
637 	ret = pfn < end_pfn ? -EBUSY : 0;
638 
639 out:
640 	trace_test_pages_isolated(start_pfn, end_pfn, pfn);
641 
642 	return ret;
643 }
644