xref: /openbmc/linux/mm/page_isolation.c (revision affd26b1)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a5d76b54SKAMEZAWA Hiroyuki /*
3a5d76b54SKAMEZAWA Hiroyuki  * linux/mm/page_isolation.c
4a5d76b54SKAMEZAWA Hiroyuki  */
5a5d76b54SKAMEZAWA Hiroyuki 
6a5d76b54SKAMEZAWA Hiroyuki #include <linux/mm.h>
7a5d76b54SKAMEZAWA Hiroyuki #include <linux/page-isolation.h>
8a5d76b54SKAMEZAWA Hiroyuki #include <linux/pageblock-flags.h>
9ee6f509cSMinchan Kim #include <linux/memory.h>
10c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h>
1183358eceSJoonsoo Kim #include <linux/page_owner.h>
128b913238SMichal Hocko #include <linux/migrate.h>
13a5d76b54SKAMEZAWA Hiroyuki #include "internal.h"
14a5d76b54SKAMEZAWA Hiroyuki 
150f0848e5SJoonsoo Kim #define CREATE_TRACE_POINTS
160f0848e5SJoonsoo Kim #include <trace/events/page_isolation.h>
170f0848e5SJoonsoo Kim 
18b48d8a8eSZi Yan /*
19844fbae6SZi Yan  * This function checks whether the range [start_pfn, end_pfn) includes
20844fbae6SZi Yan  * unmovable pages or not. The range must fall into a single pageblock and
21844fbae6SZi Yan  * consequently belong to a single zone.
22b48d8a8eSZi Yan  *
23b48d8a8eSZi Yan  * PageLRU check without isolation or lru_lock could race so that
24b48d8a8eSZi Yan  * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
25b48d8a8eSZi Yan  * check without lock_page also may miss some movable non-lru pages at
26b48d8a8eSZi Yan  * race condition. So you can't expect this function should be exact.
27b48d8a8eSZi Yan  *
28b48d8a8eSZi Yan  * Returns a page without holding a reference. If the caller wants to
29b48d8a8eSZi Yan  * dereference that page (e.g., dumping), it has to make sure that it
30b48d8a8eSZi Yan  * cannot get removed (e.g., via memory unplug) concurrently.
31b48d8a8eSZi Yan  *
32b48d8a8eSZi Yan  */
has_unmovable_pages(unsigned long start_pfn,unsigned long end_pfn,int migratetype,int flags)33844fbae6SZi Yan static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn,
34b48d8a8eSZi Yan 				int migratetype, int flags)
35b48d8a8eSZi Yan {
36844fbae6SZi Yan 	struct page *page = pfn_to_page(start_pfn);
37844fbae6SZi Yan 	struct zone *zone = page_zone(page);
38844fbae6SZi Yan 	unsigned long pfn;
39844fbae6SZi Yan 
404f9bc69aSKefeng Wang 	VM_BUG_ON(pageblock_start_pfn(start_pfn) !=
414f9bc69aSKefeng Wang 		  pageblock_start_pfn(end_pfn - 1));
42b48d8a8eSZi Yan 
43b48d8a8eSZi Yan 	if (is_migrate_cma_page(page)) {
44b48d8a8eSZi Yan 		/*
45b48d8a8eSZi Yan 		 * CMA allocations (alloc_contig_range) really need to mark
46b48d8a8eSZi Yan 		 * isolate CMA pageblocks even when they are not movable in fact
47b48d8a8eSZi Yan 		 * so consider them movable here.
48b48d8a8eSZi Yan 		 */
49b48d8a8eSZi Yan 		if (is_migrate_cma(migratetype))
50b48d8a8eSZi Yan 			return NULL;
51b48d8a8eSZi Yan 
52b48d8a8eSZi Yan 		return page;
53b48d8a8eSZi Yan 	}
54b48d8a8eSZi Yan 
55844fbae6SZi Yan 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
56844fbae6SZi Yan 		page = pfn_to_page(pfn);
57b48d8a8eSZi Yan 
58b48d8a8eSZi Yan 		/*
59b48d8a8eSZi Yan 		 * Both, bootmem allocations and memory holes are marked
60b48d8a8eSZi Yan 		 * PG_reserved and are unmovable. We can even have unmovable
61b48d8a8eSZi Yan 		 * allocations inside ZONE_MOVABLE, for example when
62b48d8a8eSZi Yan 		 * specifying "movablecore".
63b48d8a8eSZi Yan 		 */
64b48d8a8eSZi Yan 		if (PageReserved(page))
65b48d8a8eSZi Yan 			return page;
66b48d8a8eSZi Yan 
67b48d8a8eSZi Yan 		/*
68b48d8a8eSZi Yan 		 * If the zone is movable and we have ruled out all reserved
69b48d8a8eSZi Yan 		 * pages then it should be reasonably safe to assume the rest
70b48d8a8eSZi Yan 		 * is movable.
71b48d8a8eSZi Yan 		 */
72b48d8a8eSZi Yan 		if (zone_idx(zone) == ZONE_MOVABLE)
73b48d8a8eSZi Yan 			continue;
74b48d8a8eSZi Yan 
75b48d8a8eSZi Yan 		/*
76b48d8a8eSZi Yan 		 * Hugepages are not in LRU lists, but they're movable.
77b48d8a8eSZi Yan 		 * THPs are on the LRU, but need to be counted as #small pages.
78b48d8a8eSZi Yan 		 * We need not scan over tail pages because we don't
79b48d8a8eSZi Yan 		 * handle each tail page individually in migration.
80b48d8a8eSZi Yan 		 */
81b48d8a8eSZi Yan 		if (PageHuge(page) || PageTransCompound(page)) {
82*affd26b1SSidhartha Kumar 			struct folio *folio = page_folio(page);
83b48d8a8eSZi Yan 			unsigned int skip_pages;
84b48d8a8eSZi Yan 
85b48d8a8eSZi Yan 			if (PageHuge(page)) {
86*affd26b1SSidhartha Kumar 				if (!hugepage_migration_supported(folio_hstate(folio)))
87b48d8a8eSZi Yan 					return page;
88*affd26b1SSidhartha Kumar 			} else if (!folio_test_lru(folio) && !__folio_test_movable(folio)) {
89b48d8a8eSZi Yan 				return page;
90b48d8a8eSZi Yan 			}
91b48d8a8eSZi Yan 
92*affd26b1SSidhartha Kumar 			skip_pages = folio_nr_pages(folio) - folio_page_idx(folio, page);
93844fbae6SZi Yan 			pfn += skip_pages - 1;
94b48d8a8eSZi Yan 			continue;
95b48d8a8eSZi Yan 		}
96b48d8a8eSZi Yan 
97b48d8a8eSZi Yan 		/*
98b48d8a8eSZi Yan 		 * We can't use page_count without pin a page
99b48d8a8eSZi Yan 		 * because another CPU can free compound page.
100b48d8a8eSZi Yan 		 * This check already skips compound tails of THP
101b48d8a8eSZi Yan 		 * because their page->_refcount is zero at all time.
102b48d8a8eSZi Yan 		 */
103b48d8a8eSZi Yan 		if (!page_ref_count(page)) {
104b48d8a8eSZi Yan 			if (PageBuddy(page))
105844fbae6SZi Yan 				pfn += (1 << buddy_order(page)) - 1;
106b48d8a8eSZi Yan 			continue;
107b48d8a8eSZi Yan 		}
108b48d8a8eSZi Yan 
109b48d8a8eSZi Yan 		/*
110b48d8a8eSZi Yan 		 * The HWPoisoned page may be not in buddy system, and
111b48d8a8eSZi Yan 		 * page_count() is not 0.
112b48d8a8eSZi Yan 		 */
113b48d8a8eSZi Yan 		if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
114b48d8a8eSZi Yan 			continue;
115b48d8a8eSZi Yan 
116b48d8a8eSZi Yan 		/*
117b48d8a8eSZi Yan 		 * We treat all PageOffline() pages as movable when offlining
118b48d8a8eSZi Yan 		 * to give drivers a chance to decrement their reference count
119b48d8a8eSZi Yan 		 * in MEM_GOING_OFFLINE in order to indicate that these pages
120b48d8a8eSZi Yan 		 * can be offlined as there are no direct references anymore.
121b48d8a8eSZi Yan 		 * For actually unmovable PageOffline() where the driver does
122b48d8a8eSZi Yan 		 * not support this, we will fail later when trying to actually
123b48d8a8eSZi Yan 		 * move these pages that still have a reference count > 0.
124b48d8a8eSZi Yan 		 * (false negatives in this function only)
125b48d8a8eSZi Yan 		 */
126b48d8a8eSZi Yan 		if ((flags & MEMORY_OFFLINE) && PageOffline(page))
127b48d8a8eSZi Yan 			continue;
128b48d8a8eSZi Yan 
129b48d8a8eSZi Yan 		if (__PageMovable(page) || PageLRU(page))
130b48d8a8eSZi Yan 			continue;
131b48d8a8eSZi Yan 
132b48d8a8eSZi Yan 		/*
133b48d8a8eSZi Yan 		 * If there are RECLAIMABLE pages, we need to check
134b48d8a8eSZi Yan 		 * it.  But now, memory offline itself doesn't call
135b48d8a8eSZi Yan 		 * shrink_node_slabs() and it still to be fixed.
136b48d8a8eSZi Yan 		 */
137b48d8a8eSZi Yan 		return page;
138b48d8a8eSZi Yan 	}
139b48d8a8eSZi Yan 	return NULL;
140b48d8a8eSZi Yan }
141b48d8a8eSZi Yan 
142844fbae6SZi Yan /*
143844fbae6SZi Yan  * This function set pageblock migratetype to isolate if no unmovable page is
144844fbae6SZi Yan  * present in [start_pfn, end_pfn). The pageblock must intersect with
145844fbae6SZi Yan  * [start_pfn, end_pfn).
146844fbae6SZi Yan  */
set_migratetype_isolate(struct page * page,int migratetype,int isol_flags,unsigned long start_pfn,unsigned long end_pfn)147844fbae6SZi Yan static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags,
148844fbae6SZi Yan 			unsigned long start_pfn, unsigned long end_pfn)
149ee6f509cSMinchan Kim {
1501c31cb49SDavid Hildenbrand 	struct zone *zone = page_zone(page);
1511c31cb49SDavid Hildenbrand 	struct page *unmovable;
1523f9903b9SDavid Hildenbrand 	unsigned long flags;
153844fbae6SZi Yan 	unsigned long check_unmovable_start, check_unmovable_end;
154ee6f509cSMinchan Kim 
155ee6f509cSMinchan Kim 	spin_lock_irqsave(&zone->lock, flags);
156ee6f509cSMinchan Kim 
1572c7452a0SMike Kravetz 	/*
1582c7452a0SMike Kravetz 	 * We assume the caller intended to SET migrate type to isolate.
1592c7452a0SMike Kravetz 	 * If it is already set, then someone else must have raced and
16051030a53SDavid Hildenbrand 	 * set it before us.
1612c7452a0SMike Kravetz 	 */
16251030a53SDavid Hildenbrand 	if (is_migrate_isolate_page(page)) {
16351030a53SDavid Hildenbrand 		spin_unlock_irqrestore(&zone->lock, flags);
16451030a53SDavid Hildenbrand 		return -EBUSY;
16551030a53SDavid Hildenbrand 	}
1662c7452a0SMike Kravetz 
167ee6f509cSMinchan Kim 	/*
168ee6f509cSMinchan Kim 	 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
169ee6f509cSMinchan Kim 	 * We just check MOVABLE pages.
170844fbae6SZi Yan 	 *
171844fbae6SZi Yan 	 * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
172844fbae6SZi Yan 	 * to avoid redundant checks.
173ee6f509cSMinchan Kim 	 */
174844fbae6SZi Yan 	check_unmovable_start = max(page_to_pfn(page), start_pfn);
1754f9bc69aSKefeng Wang 	check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)),
176844fbae6SZi Yan 				  end_pfn);
177844fbae6SZi Yan 
178844fbae6SZi Yan 	unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
179844fbae6SZi Yan 			migratetype, isol_flags);
1804a55c047SQian Cai 	if (!unmovable) {
1812139cbe6SBartlomiej Zolnierkiewicz 		unsigned long nr_pages;
1824da2ce25SMichal Hocko 		int mt = get_pageblock_migratetype(page);
1832139cbe6SBartlomiej Zolnierkiewicz 
184a458431eSBartlomiej Zolnierkiewicz 		set_pageblock_migratetype(page, MIGRATE_ISOLATE);
185ad53f92eSJoonsoo Kim 		zone->nr_isolate_pageblock++;
18602aa0cddSVlastimil Babka 		nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
18702aa0cddSVlastimil Babka 									NULL);
1882139cbe6SBartlomiej Zolnierkiewicz 
1894da2ce25SMichal Hocko 		__mod_zone_freepage_state(zone, -nr_pages, mt);
1901c31cb49SDavid Hildenbrand 		spin_unlock_irqrestore(&zone->lock, flags);
1911c31cb49SDavid Hildenbrand 		return 0;
192ee6f509cSMinchan Kim 	}
193ee6f509cSMinchan Kim 
194ee6f509cSMinchan Kim 	spin_unlock_irqrestore(&zone->lock, flags);
1951c31cb49SDavid Hildenbrand 	if (isol_flags & REPORT_FAILURE) {
1964a55c047SQian Cai 		/*
1973d680bdfSQian Cai 		 * printk() with zone->lock held will likely trigger a
1984a55c047SQian Cai 		 * lockdep splat, so defer it here.
1994a55c047SQian Cai 		 */
2004a55c047SQian Cai 		dump_page(unmovable, "unmovable page");
2013d680bdfSQian Cai 	}
2024a55c047SQian Cai 
2031c31cb49SDavid Hildenbrand 	return -EBUSY;
204ee6f509cSMinchan Kim }
205ee6f509cSMinchan Kim 
unset_migratetype_isolate(struct page * page,int migratetype)206b2c9e2fbSZi Yan static void unset_migratetype_isolate(struct page *page, int migratetype)
207ee6f509cSMinchan Kim {
208ee6f509cSMinchan Kim 	struct zone *zone;
2092139cbe6SBartlomiej Zolnierkiewicz 	unsigned long flags, nr_pages;
210e3a2713cSJoonsoo Kim 	bool isolated_page = false;
2113c605096SJoonsoo Kim 	unsigned int order;
2123c605096SJoonsoo Kim 	struct page *buddy;
2132139cbe6SBartlomiej Zolnierkiewicz 
214ee6f509cSMinchan Kim 	zone = page_zone(page);
215ee6f509cSMinchan Kim 	spin_lock_irqsave(&zone->lock, flags);
216bbf9ce97SXishi Qiu 	if (!is_migrate_isolate_page(page))
217ee6f509cSMinchan Kim 		goto out;
2183c605096SJoonsoo Kim 
2193c605096SJoonsoo Kim 	/*
2203c605096SJoonsoo Kim 	 * Because freepage with more than pageblock_order on isolated
2213c605096SJoonsoo Kim 	 * pageblock is restricted to merge due to freepage counting problem,
2223c605096SJoonsoo Kim 	 * it is possible that there is free buddy page.
2233c605096SJoonsoo Kim 	 * move_freepages_block() doesn't care of merge so we need other
2243c605096SJoonsoo Kim 	 * approach in order to merge them. Isolation and free will make
2253c605096SJoonsoo Kim 	 * these pages to be merged.
2263c605096SJoonsoo Kim 	 */
2273c605096SJoonsoo Kim 	if (PageBuddy(page)) {
228ab130f91SMatthew Wilcox (Oracle) 		order = buddy_order(page);
22923baf831SKirill A. Shutemov 		if (order >= pageblock_order && order < MAX_ORDER) {
2308170ac47SZi Yan 			buddy = find_buddy_page_pfn(page, page_to_pfn(page),
2318170ac47SZi Yan 						    order, NULL);
2328170ac47SZi Yan 			if (buddy && !is_migrate_isolate_page(buddy)) {
233a500cb34SMiaohe Lin 				isolated_page = !!__isolate_free_page(page, order);
234a500cb34SMiaohe Lin 				/*
235a500cb34SMiaohe Lin 				 * Isolating a free page in an isolated pageblock
236a500cb34SMiaohe Lin 				 * is expected to always work as watermarks don't
237a500cb34SMiaohe Lin 				 * apply here.
238a500cb34SMiaohe Lin 				 */
239a500cb34SMiaohe Lin 				VM_WARN_ON(!isolated_page);
2403c605096SJoonsoo Kim 			}
2413c605096SJoonsoo Kim 		}
2423c605096SJoonsoo Kim 	}
2433c605096SJoonsoo Kim 
2443c605096SJoonsoo Kim 	/*
2453c605096SJoonsoo Kim 	 * If we isolate freepage with more than pageblock_order, there
2463c605096SJoonsoo Kim 	 * should be no freepage in the range, so we could avoid costly
2473c605096SJoonsoo Kim 	 * pageblock scanning for freepage moving.
248293ffa5eSDavid Hildenbrand 	 *
249293ffa5eSDavid Hildenbrand 	 * We didn't actually touch any of the isolated pages, so place them
250293ffa5eSDavid Hildenbrand 	 * to the tail of the freelist. This is an optimization for memory
251293ffa5eSDavid Hildenbrand 	 * onlining - just onlined memory won't immediately be considered for
252293ffa5eSDavid Hildenbrand 	 * allocation.
2533c605096SJoonsoo Kim 	 */
254a85468b7SChen Wandun 	if (!isolated_page) {
25502aa0cddSVlastimil Babka 		nr_pages = move_freepages_block(zone, page, migratetype, NULL);
256d1ce749aSBartlomiej Zolnierkiewicz 		__mod_zone_freepage_state(zone, nr_pages, migratetype);
2573c605096SJoonsoo Kim 	}
258a458431eSBartlomiej Zolnierkiewicz 	set_pageblock_migratetype(page, migratetype);
259624f58d8SAlexander Duyck 	if (isolated_page)
260624f58d8SAlexander Duyck 		__putback_isolated_page(page, order, migratetype);
261ad53f92eSJoonsoo Kim 	zone->nr_isolate_pageblock--;
262ee6f509cSMinchan Kim out:
263ee6f509cSMinchan Kim 	spin_unlock_irqrestore(&zone->lock, flags);
26483358eceSJoonsoo Kim }
265ee6f509cSMinchan Kim 
266a5d76b54SKAMEZAWA Hiroyuki static inline struct page *
__first_valid_page(unsigned long pfn,unsigned long nr_pages)267a5d76b54SKAMEZAWA Hiroyuki __first_valid_page(unsigned long pfn, unsigned long nr_pages)
268a5d76b54SKAMEZAWA Hiroyuki {
269a5d76b54SKAMEZAWA Hiroyuki 	int i;
2702ce13640SMichal Hocko 
2712ce13640SMichal Hocko 	for (i = 0; i < nr_pages; i++) {
2722ce13640SMichal Hocko 		struct page *page;
2732ce13640SMichal Hocko 
2742ce13640SMichal Hocko 		page = pfn_to_online_page(pfn + i);
2752ce13640SMichal Hocko 		if (!page)
2762ce13640SMichal Hocko 			continue;
2772ce13640SMichal Hocko 		return page;
2782ce13640SMichal Hocko 	}
279a5d76b54SKAMEZAWA Hiroyuki 	return NULL;
280a5d76b54SKAMEZAWA Hiroyuki }
281a5d76b54SKAMEZAWA Hiroyuki 
2829b7ea46aSQian Cai /**
283b2c9e2fbSZi Yan  * isolate_single_pageblock() -- tries to isolate a pageblock that might be
284b2c9e2fbSZi Yan  * within a free or in-use page.
285b2c9e2fbSZi Yan  * @boundary_pfn:		pageblock-aligned pfn that a page might cross
28688ee1343SZi Yan  * @flags:			isolation flags
287b2c9e2fbSZi Yan  * @gfp_flags:			GFP flags used for migrating pages
288b2c9e2fbSZi Yan  * @isolate_before:	isolate the pageblock before the boundary_pfn
28904299938SYang Li  * @skip_isolation:	the flag to skip the pageblock isolation in second
29004299938SYang Li  *			isolate_single_pageblock()
29180e2b584SZi Yan  * @migratetype:	migrate type to set in error recovery.
292b2c9e2fbSZi Yan  *
29323baf831SKirill A. Shutemov  * Free and in-use pages can be as big as MAX_ORDER and contain more than one
294b2c9e2fbSZi Yan  * pageblock. When not all pageblocks within a page are isolated at the same
295b2c9e2fbSZi Yan  * time, free page accounting can go wrong. For example, in the case of
29623baf831SKirill A. Shutemov  * MAX_ORDER = pageblock_order + 1, a MAX_ORDER page has two pagelbocks.
29723baf831SKirill A. Shutemov  * [         MAX_ORDER           ]
298b2c9e2fbSZi Yan  * [  pageblock0  |  pageblock1  ]
299b2c9e2fbSZi Yan  * When either pageblock is isolated, if it is a free page, the page is not
300b2c9e2fbSZi Yan  * split into separate migratetype lists, which is supposed to; if it is an
301b2c9e2fbSZi Yan  * in-use page and freed later, __free_one_page() does not split the free page
302b2c9e2fbSZi Yan  * either. The function handles this by splitting the free page or migrating
303b2c9e2fbSZi Yan  * the in-use page then splitting the free page.
304b2c9e2fbSZi Yan  */
isolate_single_pageblock(unsigned long boundary_pfn,int flags,gfp_t gfp_flags,bool isolate_before,bool skip_isolation,int migratetype)30588ee1343SZi Yan static int isolate_single_pageblock(unsigned long boundary_pfn, int flags,
30680e2b584SZi Yan 			gfp_t gfp_flags, bool isolate_before, bool skip_isolation,
30780e2b584SZi Yan 			int migratetype)
308b2c9e2fbSZi Yan {
309b2c9e2fbSZi Yan 	unsigned long start_pfn;
310b2c9e2fbSZi Yan 	unsigned long isolate_pageblock;
311b2c9e2fbSZi Yan 	unsigned long pfn;
312b2c9e2fbSZi Yan 	struct zone *zone;
31388ee1343SZi Yan 	int ret;
314b2c9e2fbSZi Yan 
315ee0913c4SKefeng Wang 	VM_BUG_ON(!pageblock_aligned(boundary_pfn));
316b2c9e2fbSZi Yan 
317b2c9e2fbSZi Yan 	if (isolate_before)
318b2c9e2fbSZi Yan 		isolate_pageblock = boundary_pfn - pageblock_nr_pages;
319b2c9e2fbSZi Yan 	else
320b2c9e2fbSZi Yan 		isolate_pageblock = boundary_pfn;
321b2c9e2fbSZi Yan 
322b2c9e2fbSZi Yan 	/*
323b2c9e2fbSZi Yan 	 * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid
324b2c9e2fbSZi Yan 	 * only isolating a subset of pageblocks from a bigger than pageblock
325b2c9e2fbSZi Yan 	 * free or in-use page. Also make sure all to-be-isolated pageblocks
326b2c9e2fbSZi Yan 	 * are within the same zone.
327b2c9e2fbSZi Yan 	 */
328b2c9e2fbSZi Yan 	zone  = page_zone(pfn_to_page(isolate_pageblock));
329b2c9e2fbSZi Yan 	start_pfn  = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES),
330b2c9e2fbSZi Yan 				      zone->zone_start_pfn);
331b2c9e2fbSZi Yan 
33280e2b584SZi Yan 	if (skip_isolation) {
333fba4eaf9SMaria Yu 		int mt __maybe_unused = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
3349b209e55SZi Yan 
33580e2b584SZi Yan 		VM_BUG_ON(!is_migrate_isolate(mt));
33680e2b584SZi Yan 	} else {
33780e2b584SZi Yan 		ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype,
33880e2b584SZi Yan 				flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages);
33988ee1343SZi Yan 
34088ee1343SZi Yan 		if (ret)
34188ee1343SZi Yan 			return ret;
3429b209e55SZi Yan 	}
343b2c9e2fbSZi Yan 
344b2c9e2fbSZi Yan 	/*
345b2c9e2fbSZi Yan 	 * Bail out early when the to-be-isolated pageblock does not form
346b2c9e2fbSZi Yan 	 * a free or in-use page across boundary_pfn:
347b2c9e2fbSZi Yan 	 *
348b2c9e2fbSZi Yan 	 * 1. isolate before boundary_pfn: the page after is not online
349b2c9e2fbSZi Yan 	 * 2. isolate after boundary_pfn: the page before is not online
350b2c9e2fbSZi Yan 	 *
351b2c9e2fbSZi Yan 	 * This also ensures correctness. Without it, when isolate after
352b2c9e2fbSZi Yan 	 * boundary_pfn and [start_pfn, boundary_pfn) are not online,
353b2c9e2fbSZi Yan 	 * __first_valid_page() will return unexpected NULL in the for loop
354b2c9e2fbSZi Yan 	 * below.
355b2c9e2fbSZi Yan 	 */
356b2c9e2fbSZi Yan 	if (isolate_before) {
357b2c9e2fbSZi Yan 		if (!pfn_to_online_page(boundary_pfn))
358b2c9e2fbSZi Yan 			return 0;
359b2c9e2fbSZi Yan 	} else {
360b2c9e2fbSZi Yan 		if (!pfn_to_online_page(boundary_pfn - 1))
361b2c9e2fbSZi Yan 			return 0;
362b2c9e2fbSZi Yan 	}
363b2c9e2fbSZi Yan 
364b2c9e2fbSZi Yan 	for (pfn = start_pfn; pfn < boundary_pfn;) {
365b2c9e2fbSZi Yan 		struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
366b2c9e2fbSZi Yan 
367b2c9e2fbSZi Yan 		VM_BUG_ON(!page);
368b2c9e2fbSZi Yan 		pfn = page_to_pfn(page);
369b2c9e2fbSZi Yan 		/*
370b2c9e2fbSZi Yan 		 * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
371b2c9e2fbSZi Yan 		 * free pages in [start_pfn, boundary_pfn), its head page will
372b2c9e2fbSZi Yan 		 * always be in the range.
373b2c9e2fbSZi Yan 		 */
374b2c9e2fbSZi Yan 		if (PageBuddy(page)) {
375b2c9e2fbSZi Yan 			int order = buddy_order(page);
376b2c9e2fbSZi Yan 
37786d28b07SZi Yan 			if (pfn + (1UL << order) > boundary_pfn) {
37886d28b07SZi Yan 				/* free page changed before split, check it again */
37986d28b07SZi Yan 				if (split_free_page(page, order, boundary_pfn - pfn))
38086d28b07SZi Yan 					continue;
38186d28b07SZi Yan 			}
38286d28b07SZi Yan 
38386d28b07SZi Yan 			pfn += 1UL << order;
384b2c9e2fbSZi Yan 			continue;
385b2c9e2fbSZi Yan 		}
386b2c9e2fbSZi Yan 		/*
387b2c9e2fbSZi Yan 		 * migrate compound pages then let the free page handling code
388b2c9e2fbSZi Yan 		 * above do the rest. If migration is not possible, just fail.
389b2c9e2fbSZi Yan 		 */
390b2c9e2fbSZi Yan 		if (PageCompound(page)) {
391b2c9e2fbSZi Yan 			struct page *head = compound_head(page);
392b2c9e2fbSZi Yan 			unsigned long head_pfn = page_to_pfn(head);
393547be963SZi Yan 			unsigned long nr_pages = compound_nr(head);
394b2c9e2fbSZi Yan 
39588ee1343SZi Yan 			if (head_pfn + nr_pages <= boundary_pfn) {
396b2c9e2fbSZi Yan 				pfn = head_pfn + nr_pages;
397b2c9e2fbSZi Yan 				continue;
398b2c9e2fbSZi Yan 			}
399b2c9e2fbSZi Yan #if defined CONFIG_COMPACTION || defined CONFIG_CMA
400b2c9e2fbSZi Yan 			/*
401b2c9e2fbSZi Yan 			 * hugetlb, lru compound (THP), and movable compound pages
402b2c9e2fbSZi Yan 			 * can be migrated. Otherwise, fail the isolation.
403b2c9e2fbSZi Yan 			 */
404b2c9e2fbSZi Yan 			if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
405b2c9e2fbSZi Yan 				int order;
406b2c9e2fbSZi Yan 				unsigned long outer_pfn;
40788ee1343SZi Yan 				int page_mt = get_pageblock_migratetype(page);
40888ee1343SZi Yan 				bool isolate_page = !is_migrate_isolate_page(page);
409b2c9e2fbSZi Yan 				struct compact_control cc = {
410b2c9e2fbSZi Yan 					.nr_migratepages = 0,
411b2c9e2fbSZi Yan 					.order = -1,
412b2c9e2fbSZi Yan 					.zone = page_zone(pfn_to_page(head_pfn)),
413b2c9e2fbSZi Yan 					.mode = MIGRATE_SYNC,
414b2c9e2fbSZi Yan 					.ignore_skip_hint = true,
415b2c9e2fbSZi Yan 					.no_set_skip_hint = true,
416b2c9e2fbSZi Yan 					.gfp_mask = gfp_flags,
417b2c9e2fbSZi Yan 					.alloc_contig = true,
418b2c9e2fbSZi Yan 				};
419b2c9e2fbSZi Yan 				INIT_LIST_HEAD(&cc.migratepages);
420b2c9e2fbSZi Yan 
42188ee1343SZi Yan 				/*
42288ee1343SZi Yan 				 * XXX: mark the page as MIGRATE_ISOLATE so that
42388ee1343SZi Yan 				 * no one else can grab the freed page after migration.
42488ee1343SZi Yan 				 * Ideally, the page should be freed as two separate
42588ee1343SZi Yan 				 * pages to be added into separate migratetype free
42688ee1343SZi Yan 				 * lists.
42788ee1343SZi Yan 				 */
42888ee1343SZi Yan 				if (isolate_page) {
42988ee1343SZi Yan 					ret = set_migratetype_isolate(page, page_mt,
43088ee1343SZi Yan 						flags, head_pfn, head_pfn + nr_pages);
43188ee1343SZi Yan 					if (ret)
43288ee1343SZi Yan 						goto failed;
43388ee1343SZi Yan 				}
43488ee1343SZi Yan 
435b2c9e2fbSZi Yan 				ret = __alloc_contig_migrate_range(&cc, head_pfn,
436b2c9e2fbSZi Yan 							head_pfn + nr_pages);
437b2c9e2fbSZi Yan 
43888ee1343SZi Yan 				/*
43988ee1343SZi Yan 				 * restore the page's migratetype so that it can
44088ee1343SZi Yan 				 * be split into separate migratetype free lists
44188ee1343SZi Yan 				 * later.
44288ee1343SZi Yan 				 */
44388ee1343SZi Yan 				if (isolate_page)
44488ee1343SZi Yan 					unset_migratetype_isolate(page, page_mt);
44588ee1343SZi Yan 
446b2c9e2fbSZi Yan 				if (ret)
447b2c9e2fbSZi Yan 					goto failed;
448b2c9e2fbSZi Yan 				/*
449b2c9e2fbSZi Yan 				 * reset pfn to the head of the free page, so
450b2c9e2fbSZi Yan 				 * that the free page handling code above can split
451b2c9e2fbSZi Yan 				 * the free page to the right migratetype list.
452b2c9e2fbSZi Yan 				 *
453b2c9e2fbSZi Yan 				 * head_pfn is not used here as a hugetlb page order
45423baf831SKirill A. Shutemov 				 * can be bigger than MAX_ORDER, but after it is
455b2c9e2fbSZi Yan 				 * freed, the free page order is not. Use pfn within
456b2c9e2fbSZi Yan 				 * the range to find the head of the free page.
457b2c9e2fbSZi Yan 				 */
458b2c9e2fbSZi Yan 				order = 0;
459b2c9e2fbSZi Yan 				outer_pfn = pfn;
460b2c9e2fbSZi Yan 				while (!PageBuddy(pfn_to_page(outer_pfn))) {
46188ee1343SZi Yan 					/* stop if we cannot find the free page */
46223baf831SKirill A. Shutemov 					if (++order > MAX_ORDER)
46388ee1343SZi Yan 						goto failed;
464b2c9e2fbSZi Yan 					outer_pfn &= ~0UL << order;
465b2c9e2fbSZi Yan 				}
466b2c9e2fbSZi Yan 				pfn = outer_pfn;
467b2c9e2fbSZi Yan 				continue;
468b2c9e2fbSZi Yan 			} else
469b2c9e2fbSZi Yan #endif
470b2c9e2fbSZi Yan 				goto failed;
471b2c9e2fbSZi Yan 		}
472b2c9e2fbSZi Yan 
473b2c9e2fbSZi Yan 		pfn++;
474b2c9e2fbSZi Yan 	}
475b2c9e2fbSZi Yan 	return 0;
476b2c9e2fbSZi Yan failed:
477b2c9e2fbSZi Yan 	/* restore the original migratetype */
4789b209e55SZi Yan 	if (!skip_isolation)
47980e2b584SZi Yan 		unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype);
480b2c9e2fbSZi Yan 	return -EBUSY;
481b2c9e2fbSZi Yan }
482b2c9e2fbSZi Yan 
483b2c9e2fbSZi Yan /**
484ce5df776SJohannes Weiner  * start_isolate_page_range() - mark page range MIGRATE_ISOLATE
485ce5df776SJohannes Weiner  * @start_pfn:		The first PFN of the range to be isolated.
486ce5df776SJohannes Weiner  * @end_pfn:		The last PFN of the range to be isolated.
4879b7ea46aSQian Cai  * @migratetype:	Migrate type to set in error recovery.
4889b7ea46aSQian Cai  * @flags:		The following flags are allowed (they can be combined in
4899b7ea46aSQian Cai  *			a bit mask)
490756d25beSDavid Hildenbrand  *			MEMORY_OFFLINE - isolate to offline (!allocate) memory
491756d25beSDavid Hildenbrand  *					 e.g., skip over PageHWPoison() pages
492aa218795SDavid Hildenbrand  *					 and PageOffline() pages.
4939b7ea46aSQian Cai  *			REPORT_FAILURE - report details about the failure to
4949b7ea46aSQian Cai  *			isolate the range
495b2c9e2fbSZi Yan  * @gfp_flags:		GFP flags used for migrating pages that sit across the
496b2c9e2fbSZi Yan  *			range boundaries.
497a5d76b54SKAMEZAWA Hiroyuki  *
498a5d76b54SKAMEZAWA Hiroyuki  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
499a5d76b54SKAMEZAWA Hiroyuki  * the range will never be allocated. Any free pages and pages freed in the
5009b7ea46aSQian Cai  * future will not be allocated again. If specified range includes migrate types
5019b7ea46aSQian Cai  * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
5029b7ea46aSQian Cai  * pages in the range finally, the caller have to free all pages in the range.
5039b7ea46aSQian Cai  * test_page_isolated() can be used for test it.
5042c7452a0SMike Kravetz  *
505b2c9e2fbSZi Yan  * The function first tries to isolate the pageblocks at the beginning and end
506b2c9e2fbSZi Yan  * of the range, since there might be pages across the range boundaries.
507b2c9e2fbSZi Yan  * Afterwards, it isolates the rest of the range.
508b2c9e2fbSZi Yan  *
5092c7452a0SMike Kravetz  * There is no high level synchronization mechanism that prevents two threads
5102c7452a0SMike Kravetz  * from trying to isolate overlapping ranges. If this happens, one thread
5112c7452a0SMike Kravetz  * will notice pageblocks in the overlapping range already set to isolate.
5122c7452a0SMike Kravetz  * This happens in set_migratetype_isolate, and set_migratetype_isolate
5132c7452a0SMike Kravetz  * returns an error. We then clean up by restoring the migration type on
5142c7452a0SMike Kravetz  * pageblocks we may have modified and return -EBUSY to caller. This
5152c7452a0SMike Kravetz  * prevents two threads from simultaneously working on overlapping ranges.
5169b7ea46aSQian Cai  *
51796831826SPavel Tatashin  * Please note that there is no strong synchronization with the page allocator
51896831826SPavel Tatashin  * either. Pages might be freed while their page blocks are marked ISOLATED.
5197612921fSVlastimil Babka  * A call to drain_all_pages() after isolation can flush most of them. However
5207612921fSVlastimil Babka  * in some cases pages might still end up on pcp lists and that would allow
52196831826SPavel Tatashin  * for their allocation even when they are in fact isolated already. Depending
522ec6e8c7eSVlastimil Babka  * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
523ec6e8c7eSVlastimil Babka  * might be used to flush and disable pcplist before isolation and enable after
524ec6e8c7eSVlastimil Babka  * unisolation.
52596831826SPavel Tatashin  *
5263fa0c7c7SDavid Hildenbrand  * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
527a5d76b54SKAMEZAWA Hiroyuki  */
start_isolate_page_range(unsigned long start_pfn,unsigned long end_pfn,int migratetype,int flags,gfp_t gfp_flags)5280815f3d8SMichal Nazarewicz int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
529b2c9e2fbSZi Yan 			     int migratetype, int flags, gfp_t gfp_flags)
530a5d76b54SKAMEZAWA Hiroyuki {
531a5d76b54SKAMEZAWA Hiroyuki 	unsigned long pfn;
532a5d76b54SKAMEZAWA Hiroyuki 	struct page *page;
5336e263fffSZi Yan 	/* isolation is done at page block granularity */
5344f9bc69aSKefeng Wang 	unsigned long isolate_start = pageblock_start_pfn(start_pfn);
5355f7fa13fSKefeng Wang 	unsigned long isolate_end = pageblock_align(end_pfn);
536b2c9e2fbSZi Yan 	int ret;
5379b209e55SZi Yan 	bool skip_isolation = false;
538a5d76b54SKAMEZAWA Hiroyuki 
5396e263fffSZi Yan 	/* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
54080e2b584SZi Yan 	ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false,
54180e2b584SZi Yan 			skip_isolation, migratetype);
542b2c9e2fbSZi Yan 	if (ret)
543b2c9e2fbSZi Yan 		return ret;
544b2c9e2fbSZi Yan 
5459b209e55SZi Yan 	if (isolate_start == isolate_end - pageblock_nr_pages)
5469b209e55SZi Yan 		skip_isolation = true;
5479b209e55SZi Yan 
5486e263fffSZi Yan 	/* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
54980e2b584SZi Yan 	ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true,
55080e2b584SZi Yan 			skip_isolation, migratetype);
551b2c9e2fbSZi Yan 	if (ret) {
5526e263fffSZi Yan 		unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype);
553b2c9e2fbSZi Yan 		return ret;
554b2c9e2fbSZi Yan 	}
555b2c9e2fbSZi Yan 
556b2c9e2fbSZi Yan 	/* skip isolated pageblocks at the beginning and end */
5576e263fffSZi Yan 	for (pfn = isolate_start + pageblock_nr_pages;
5586e263fffSZi Yan 	     pfn < isolate_end - pageblock_nr_pages;
559a5d76b54SKAMEZAWA Hiroyuki 	     pfn += pageblock_nr_pages) {
560a5d76b54SKAMEZAWA Hiroyuki 		page = __first_valid_page(pfn, pageblock_nr_pages);
561844fbae6SZi Yan 		if (page && set_migratetype_isolate(page, migratetype, flags,
562844fbae6SZi Yan 					start_pfn, end_pfn)) {
5636e263fffSZi Yan 			undo_isolate_page_range(isolate_start, pfn, migratetype);
564b2c9e2fbSZi Yan 			unset_migratetype_isolate(
5656e263fffSZi Yan 				pfn_to_page(isolate_end - pageblock_nr_pages),
566b2c9e2fbSZi Yan 				migratetype);
567e1d8c966SMiaohe Lin 			return -EBUSY;
568a5d76b54SKAMEZAWA Hiroyuki 		}
5699b7ea46aSQian Cai 	}
5703fa0c7c7SDavid Hildenbrand 	return 0;
571a5d76b54SKAMEZAWA Hiroyuki }
572a5d76b54SKAMEZAWA Hiroyuki 
573ce5df776SJohannes Weiner /**
574ce5df776SJohannes Weiner  * undo_isolate_page_range - undo effects of start_isolate_page_range()
575ce5df776SJohannes Weiner  * @start_pfn:		The first PFN of the isolated range
576ce5df776SJohannes Weiner  * @end_pfn:		The last PFN of the isolated range
577ce5df776SJohannes Weiner  * @migratetype:	New migrate type to set on the range
578ce5df776SJohannes Weiner  *
579ce5df776SJohannes Weiner  * This finds every MIGRATE_ISOLATE page block in the given range
580ce5df776SJohannes Weiner  * and switches it to @migratetype.
581a5d76b54SKAMEZAWA Hiroyuki  */
undo_isolate_page_range(unsigned long start_pfn,unsigned long end_pfn,int migratetype)5821fcf0a56SPingfan Liu void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
583b2c9e2fbSZi Yan 			    int migratetype)
584a5d76b54SKAMEZAWA Hiroyuki {
585a5d76b54SKAMEZAWA Hiroyuki 	unsigned long pfn;
586a5d76b54SKAMEZAWA Hiroyuki 	struct page *page;
5874f9bc69aSKefeng Wang 	unsigned long isolate_start = pageblock_start_pfn(start_pfn);
5885f7fa13fSKefeng Wang 	unsigned long isolate_end = pageblock_align(end_pfn);
5896f8d2b8aSWang Xiaoqiang 
5906e263fffSZi Yan 	for (pfn = isolate_start;
5916e263fffSZi Yan 	     pfn < isolate_end;
592a5d76b54SKAMEZAWA Hiroyuki 	     pfn += pageblock_nr_pages) {
593a5d76b54SKAMEZAWA Hiroyuki 		page = __first_valid_page(pfn, pageblock_nr_pages);
594bbf9ce97SXishi Qiu 		if (!page || !is_migrate_isolate_page(page))
595a5d76b54SKAMEZAWA Hiroyuki 			continue;
5960815f3d8SMichal Nazarewicz 		unset_migratetype_isolate(page, migratetype);
597a5d76b54SKAMEZAWA Hiroyuki 	}
598a5d76b54SKAMEZAWA Hiroyuki }
599a5d76b54SKAMEZAWA Hiroyuki /*
600a5d76b54SKAMEZAWA Hiroyuki  * Test all pages in the range is free(means isolated) or not.
601a5d76b54SKAMEZAWA Hiroyuki  * all pages in [start_pfn...end_pfn) must be in the same zone.
602a5d76b54SKAMEZAWA Hiroyuki  * zone->lock must be held before call this.
603a5d76b54SKAMEZAWA Hiroyuki  *
604ec3b6882SNeil Zhang  * Returns the last tested pfn.
605a5d76b54SKAMEZAWA Hiroyuki  */
606fea85cffSJoonsoo Kim static unsigned long
__test_page_isolated_in_pageblock(unsigned long pfn,unsigned long end_pfn,int flags)607b023f468SWen Congyang __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
608756d25beSDavid Hildenbrand 				  int flags)
609a5d76b54SKAMEZAWA Hiroyuki {
610a5d76b54SKAMEZAWA Hiroyuki 	struct page *page;
611a5d76b54SKAMEZAWA Hiroyuki 
612a5d76b54SKAMEZAWA Hiroyuki 	while (pfn < end_pfn) {
613a5d76b54SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
614aa016d14SVlastimil Babka 		if (PageBuddy(page))
615435b405cSMinchan Kim 			/*
616aa016d14SVlastimil Babka 			 * If the page is on a free list, it has to be on
617aa016d14SVlastimil Babka 			 * the correct MIGRATE_ISOLATE freelist. There is no
618aa016d14SVlastimil Babka 			 * simple way to verify that as VM_BUG_ON(), though.
619435b405cSMinchan Kim 			 */
620ab130f91SMatthew Wilcox (Oracle) 			pfn += 1 << buddy_order(page);
621756d25beSDavid Hildenbrand 		else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
622aa016d14SVlastimil Babka 			/* A HWPoisoned page cannot be also PageBuddy */
623b023f468SWen Congyang 			pfn++;
624aa218795SDavid Hildenbrand 		else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
625aa218795SDavid Hildenbrand 			 !page_count(page))
626aa218795SDavid Hildenbrand 			/*
627aa218795SDavid Hildenbrand 			 * The responsible driver agreed to skip PageOffline()
628aa218795SDavid Hildenbrand 			 * pages when offlining memory by dropping its
629aa218795SDavid Hildenbrand 			 * reference in MEM_GOING_OFFLINE.
630aa218795SDavid Hildenbrand 			 */
631aa218795SDavid Hildenbrand 			pfn++;
632a5d76b54SKAMEZAWA Hiroyuki 		else
633a5d76b54SKAMEZAWA Hiroyuki 			break;
634a5d76b54SKAMEZAWA Hiroyuki 	}
635fea85cffSJoonsoo Kim 
636fea85cffSJoonsoo Kim 	return pfn;
637a5d76b54SKAMEZAWA Hiroyuki }
638a5d76b54SKAMEZAWA Hiroyuki 
639ce5df776SJohannes Weiner /**
640ce5df776SJohannes Weiner  * test_pages_isolated - check if pageblocks in range are isolated
641ce5df776SJohannes Weiner  * @start_pfn:		The first PFN of the isolated range
642ce5df776SJohannes Weiner  * @end_pfn:		The first PFN *after* the isolated range
643ce5df776SJohannes Weiner  * @isol_flags:		Testing mode flags
644ce5df776SJohannes Weiner  *
645ce5df776SJohannes Weiner  * This tests if all in the specified range are free.
646ce5df776SJohannes Weiner  *
647ce5df776SJohannes Weiner  * If %MEMORY_OFFLINE is specified in @flags, it will consider
648ce5df776SJohannes Weiner  * poisoned and offlined pages free as well.
649ce5df776SJohannes Weiner  *
650ce5df776SJohannes Weiner  * Caller must ensure the requested range doesn't span zones.
651ce5df776SJohannes Weiner  *
652ce5df776SJohannes Weiner  * Returns 0 if true, -EBUSY if one or more pages are in use.
653ce5df776SJohannes Weiner  */
test_pages_isolated(unsigned long start_pfn,unsigned long end_pfn,int isol_flags)654b023f468SWen Congyang int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
655756d25beSDavid Hildenbrand 			int isol_flags)
656a5d76b54SKAMEZAWA Hiroyuki {
6576c1b7f68SGerald Schaefer 	unsigned long pfn, flags;
658a5d76b54SKAMEZAWA Hiroyuki 	struct page *page;
6596c1b7f68SGerald Schaefer 	struct zone *zone;
6601d09510bSGeorge G. Davis 	int ret;
661a5d76b54SKAMEZAWA Hiroyuki 
662a5d76b54SKAMEZAWA Hiroyuki 	/*
66385dbe706STang Chen 	 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
66485dbe706STang Chen 	 * are not aligned to pageblock_nr_pages.
66585dbe706STang Chen 	 * Then we just check migratetype first.
666a5d76b54SKAMEZAWA Hiroyuki 	 */
667a5d76b54SKAMEZAWA Hiroyuki 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
668a5d76b54SKAMEZAWA Hiroyuki 		page = __first_valid_page(pfn, pageblock_nr_pages);
669bbf9ce97SXishi Qiu 		if (page && !is_migrate_isolate_page(page))
670a5d76b54SKAMEZAWA Hiroyuki 			break;
671a5d76b54SKAMEZAWA Hiroyuki 	}
672a70dcb96SGerald Schaefer 	page = __first_valid_page(start_pfn, end_pfn - start_pfn);
6731d09510bSGeorge G. Davis 	if ((pfn < end_pfn) || !page) {
6741d09510bSGeorge G. Davis 		ret = -EBUSY;
6751d09510bSGeorge G. Davis 		goto out;
6761d09510bSGeorge G. Davis 	}
6771d09510bSGeorge G. Davis 
67885dbe706STang Chen 	/* Check all pages are free or marked as ISOLATED */
679a70dcb96SGerald Schaefer 	zone = page_zone(page);
6806c1b7f68SGerald Schaefer 	spin_lock_irqsave(&zone->lock, flags);
681756d25beSDavid Hildenbrand 	pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
6826c1b7f68SGerald Schaefer 	spin_unlock_irqrestore(&zone->lock, flags);
683fea85cffSJoonsoo Kim 
6841d09510bSGeorge G. Davis 	ret = pfn < end_pfn ? -EBUSY : 0;
6851d09510bSGeorge G. Davis 
6861d09510bSGeorge G. Davis out:
6870f0848e5SJoonsoo Kim 	trace_test_pages_isolated(start_pfn, end_pfn, pfn);
6880f0848e5SJoonsoo Kim 
6891d09510bSGeorge G. Davis 	return ret;
690a5d76b54SKAMEZAWA Hiroyuki }
691