mm: page_alloc: set migratetype inside move_freepages()
[linux-2.6-block.git] / mm / page_isolation.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a5d76b54
KH
2/*
3 * linux/mm/page_isolation.c
4 */
5
a5d76b54
KH
6#include <linux/mm.h>
7#include <linux/page-isolation.h>
8#include <linux/pageblock-flags.h>
ee6f509c 9#include <linux/memory.h>
c8721bbb 10#include <linux/hugetlb.h>
83358ece 11#include <linux/page_owner.h>
8b913238 12#include <linux/migrate.h>
a5d76b54
KH
13#include "internal.h"
14
0f0848e5
JK
15#define CREATE_TRACE_POINTS
16#include <trace/events/page_isolation.h>
17
b48d8a8e 18/*
844fbae6
ZY
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.
b48d8a8e
ZY
22 *
23 * PageLRU check without isolation or lru_lock could race so that
24 * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
25 * check without lock_page also may miss some movable non-lru pages at
26 * race condition. So you can't expect this function should be exact.
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 */
844fbae6
ZY
33static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn,
34 int migratetype, int flags)
b48d8a8e 35{
844fbae6
ZY
36 struct page *page = pfn_to_page(start_pfn);
37 struct zone *zone = page_zone(page);
38 unsigned long pfn;
39
4f9bc69a
KW
40 VM_BUG_ON(pageblock_start_pfn(start_pfn) !=
41 pageblock_start_pfn(end_pfn - 1));
b48d8a8e
ZY
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 (is_migrate_cma(migratetype))
50 return NULL;
51
52 return page;
53 }
54
844fbae6
ZY
55 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
56 page = pfn_to_page(pfn);
b48d8a8e
ZY
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)) {
affd26b1 82 struct folio *folio = page_folio(page);
b48d8a8e
ZY
83 unsigned int skip_pages;
84
85 if (PageHuge(page)) {
affd26b1 86 if (!hugepage_migration_supported(folio_hstate(folio)))
b48d8a8e 87 return page;
affd26b1 88 } else if (!folio_test_lru(folio) && !__folio_test_movable(folio)) {
b48d8a8e
ZY
89 return page;
90 }
91
affd26b1 92 skip_pages = folio_nr_pages(folio) - folio_page_idx(folio, page);
844fbae6 93 pfn += skip_pages - 1;
b48d8a8e
ZY
94 continue;
95 }
96
97 /*
98 * We can't use page_count without pin a page
99 * because another CPU can free compound page.
100 * This check already skips compound tails of THP
101 * because their page->_refcount is zero at all time.
102 */
103 if (!page_ref_count(page)) {
104 if (PageBuddy(page))
844fbae6 105 pfn += (1 << buddy_order(page)) - 1;
b48d8a8e
ZY
106 continue;
107 }
108
109 /*
110 * The HWPoisoned page may be not in buddy system, and
111 * page_count() is not 0.
112 */
113 if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
114 continue;
115
116 /*
117 * We treat all PageOffline() pages as movable when offlining
118 * to give drivers a chance to decrement their reference count
119 * in MEM_GOING_OFFLINE in order to indicate that these pages
120 * can be offlined as there are no direct references anymore.
121 * For actually unmovable PageOffline() where the driver does
122 * not support this, we will fail later when trying to actually
123 * move these pages that still have a reference count > 0.
124 * (false negatives in this function only)
125 */
126 if ((flags & MEMORY_OFFLINE) && PageOffline(page))
127 continue;
128
129 if (__PageMovable(page) || PageLRU(page))
130 continue;
131
132 /*
133 * If there are RECLAIMABLE pages, we need to check
134 * it. But now, memory offline itself doesn't call
135 * shrink_node_slabs() and it still to be fixed.
136 */
137 return page;
138 }
139 return NULL;
140}
141
844fbae6
ZY
142/*
143 * This function set pageblock migratetype to isolate if no unmovable page is
144 * present in [start_pfn, end_pfn). The pageblock must intersect with
145 * [start_pfn, end_pfn).
146 */
147static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags,
148 unsigned long start_pfn, unsigned long end_pfn)
ee6f509c 149{
1c31cb49
DH
150 struct zone *zone = page_zone(page);
151 struct page *unmovable;
3f9903b9 152 unsigned long flags;
844fbae6 153 unsigned long check_unmovable_start, check_unmovable_end;
ee6f509c
MK
154
155 spin_lock_irqsave(&zone->lock, flags);
156
2c7452a0
MK
157 /*
158 * We assume the caller intended to SET migrate type to isolate.
159 * If it is already set, then someone else must have raced and
51030a53 160 * set it before us.
2c7452a0 161 */
51030a53
DH
162 if (is_migrate_isolate_page(page)) {
163 spin_unlock_irqrestore(&zone->lock, flags);
164 return -EBUSY;
165 }
2c7452a0 166
ee6f509c
MK
167 /*
168 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
169 * We just check MOVABLE pages.
844fbae6
ZY
170 *
171 * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
172 * to avoid redundant checks.
ee6f509c 173 */
844fbae6 174 check_unmovable_start = max(page_to_pfn(page), start_pfn);
4f9bc69a 175 check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)),
844fbae6
ZY
176 end_pfn);
177
178 unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
179 migratetype, isol_flags);
4a55c047 180 if (!unmovable) {
c0cd6f55 181 int nr_pages;
4da2ce25 182 int mt = get_pageblock_migratetype(page);
2139cbe6 183
c0cd6f55
JW
184 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE);
185 /* Block spans zone boundaries? */
186 if (nr_pages == -1) {
187 spin_unlock_irqrestore(&zone->lock, flags);
188 return -EBUSY;
189 }
190 __mod_zone_freepage_state(zone, -nr_pages, mt);
ad53f92e 191 zone->nr_isolate_pageblock++;
1c31cb49 192 spin_unlock_irqrestore(&zone->lock, flags);
1c31cb49 193 return 0;
ee6f509c
MK
194 }
195
196 spin_unlock_irqrestore(&zone->lock, flags);
1c31cb49 197 if (isol_flags & REPORT_FAILURE) {
48381d7e
DH
198 /*
199 * printk() with zone->lock held will likely trigger a
200 * lockdep splat, so defer it here.
201 */
202 dump_page(unmovable, "unmovable page");
3d680bdf 203 }
4a55c047 204
1c31cb49 205 return -EBUSY;
ee6f509c
MK
206}
207
b2c9e2fb 208static void unset_migratetype_isolate(struct page *page, int migratetype)
ee6f509c
MK
209{
210 struct zone *zone;
c0cd6f55 211 unsigned long flags;
e3a2713c 212 bool isolated_page = false;
3c605096 213 unsigned int order;
3c605096 214 struct page *buddy;
2139cbe6 215
ee6f509c
MK
216 zone = page_zone(page);
217 spin_lock_irqsave(&zone->lock, flags);
bbf9ce97 218 if (!is_migrate_isolate_page(page))
ee6f509c 219 goto out;
3c605096
JK
220
221 /*
222 * Because freepage with more than pageblock_order on isolated
223 * pageblock is restricted to merge due to freepage counting problem,
224 * it is possible that there is free buddy page.
225 * move_freepages_block() doesn't care of merge so we need other
226 * approach in order to merge them. Isolation and free will make
227 * these pages to be merged.
228 */
229 if (PageBuddy(page)) {
ab130f91 230 order = buddy_order(page);
5e0a760b 231 if (order >= pageblock_order && order < MAX_PAGE_ORDER) {
8170ac47
ZY
232 buddy = find_buddy_page_pfn(page, page_to_pfn(page),
233 order, NULL);
234 if (buddy && !is_migrate_isolate_page(buddy)) {
a500cb34
ML
235 isolated_page = !!__isolate_free_page(page, order);
236 /*
237 * Isolating a free page in an isolated pageblock
238 * is expected to always work as watermarks don't
239 * apply here.
240 */
241 VM_WARN_ON(!isolated_page);
3c605096
JK
242 }
243 }
244 }
245
246 /*
247 * If we isolate freepage with more than pageblock_order, there
248 * should be no freepage in the range, so we could avoid costly
249 * pageblock scanning for freepage moving.
293ffa5e
DH
250 *
251 * We didn't actually touch any of the isolated pages, so place them
252 * to the tail of the freelist. This is an optimization for memory
253 * onlining - just onlined memory won't immediately be considered for
254 * allocation.
3c605096 255 */
a85468b7 256 if (!isolated_page) {
c0cd6f55
JW
257 int nr_pages = move_freepages_block(zone, page, migratetype);
258 /*
259 * Isolating this block already succeeded, so this
260 * should not fail on zone boundaries.
261 */
262 WARN_ON_ONCE(nr_pages == -1);
3c605096 263 __mod_zone_freepage_state(zone, nr_pages, migratetype);
f37c0f68
ZY
264 } else {
265 set_pageblock_migratetype(page, migratetype);
624f58d8 266 __putback_isolated_page(page, order, migratetype);
f37c0f68 267 }
ad53f92e 268 zone->nr_isolate_pageblock--;
ee6f509c
MK
269out:
270 spin_unlock_irqrestore(&zone->lock, flags);
271}
272
a5d76b54
KH
273static inline struct page *
274__first_valid_page(unsigned long pfn, unsigned long nr_pages)
275{
276 int i;
2ce13640
MH
277
278 for (i = 0; i < nr_pages; i++) {
279 struct page *page;
280
2ce13640
MH
281 page = pfn_to_online_page(pfn + i);
282 if (!page)
283 continue;
284 return page;
285 }
286 return NULL;
a5d76b54
KH
287}
288
b2c9e2fb
ZY
289/**
290 * isolate_single_pageblock() -- tries to isolate a pageblock that might be
291 * within a free or in-use page.
292 * @boundary_pfn: pageblock-aligned pfn that a page might cross
88ee1343 293 * @flags: isolation flags
b2c9e2fb
ZY
294 * @gfp_flags: GFP flags used for migrating pages
295 * @isolate_before: isolate the pageblock before the boundary_pfn
04299938
YL
296 * @skip_isolation: the flag to skip the pageblock isolation in second
297 * isolate_single_pageblock()
80e2b584 298 * @migratetype: migrate type to set in error recovery.
b2c9e2fb 299 *
5e0a760b 300 * Free and in-use pages can be as big as MAX_PAGE_ORDER and contain more than one
b2c9e2fb
ZY
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
5e0a760b
KS
303 * MAX_PAGE_ORDER = pageblock_order + 1, a MAX_PAGE_ORDER page has two
304 * pagelbocks.
305 * [ MAX_PAGE_ORDER ]
b2c9e2fb
ZY
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 */
88ee1343 313static int isolate_single_pageblock(unsigned long boundary_pfn, int flags,
80e2b584
ZY
314 gfp_t gfp_flags, bool isolate_before, bool skip_isolation,
315 int migratetype)
b2c9e2fb 316{
b2c9e2fb
ZY
317 unsigned long start_pfn;
318 unsigned long isolate_pageblock;
319 unsigned long pfn;
320 struct zone *zone;
88ee1343 321 int ret;
b2c9e2fb 322
ee0913c4 323 VM_BUG_ON(!pageblock_aligned(boundary_pfn));
b2c9e2fb
ZY
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
80e2b584 340 if (skip_isolation) {
fba4eaf9 341 int mt __maybe_unused = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
88ee1343 342
80e2b584
ZY
343 VM_BUG_ON(!is_migrate_isolate(mt));
344 } else {
345 ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype,
346 flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages);
9b209e55
ZY
347
348 if (ret)
349 return ret;
350 }
b2c9e2fb
ZY
351
352 /*
353 * Bail out early when the to-be-isolated pageblock does not form
354 * a free or in-use page across boundary_pfn:
355 *
356 * 1. isolate before boundary_pfn: the page after is not online
357 * 2. isolate after boundary_pfn: the page before is not online
358 *
359 * This also ensures correctness. Without it, when isolate after
360 * boundary_pfn and [start_pfn, boundary_pfn) are not online,
361 * __first_valid_page() will return unexpected NULL in the for loop
362 * below.
363 */
364 if (isolate_before) {
365 if (!pfn_to_online_page(boundary_pfn))
366 return 0;
367 } else {
368 if (!pfn_to_online_page(boundary_pfn - 1))
369 return 0;
370 }
371
372 for (pfn = start_pfn; pfn < boundary_pfn;) {
373 struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
374
375 VM_BUG_ON(!page);
376 pfn = page_to_pfn(page);
377 /*
378 * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
379 * free pages in [start_pfn, boundary_pfn), its head page will
380 * always be in the range.
381 */
382 if (PageBuddy(page)) {
383 int order = buddy_order(page);
384
86d28b07
ZY
385 if (pfn + (1UL << order) > boundary_pfn) {
386 /* free page changed before split, check it again */
387 if (split_free_page(page, order, boundary_pfn - pfn))
388 continue;
389 }
390
391 pfn += 1UL << order;
b2c9e2fb
ZY
392 continue;
393 }
394 /*
395 * migrate compound pages then let the free page handling code
396 * above do the rest. If migration is not possible, just fail.
397 */
398 if (PageCompound(page)) {
b2c9e2fb
ZY
399 struct page *head = compound_head(page);
400 unsigned long head_pfn = page_to_pfn(head);
547be963 401 unsigned long nr_pages = compound_nr(head);
b2c9e2fb 402
88ee1343 403 if (head_pfn + nr_pages <= boundary_pfn) {
b2c9e2fb
ZY
404 pfn = head_pfn + nr_pages;
405 continue;
406 }
407#if defined CONFIG_COMPACTION || defined CONFIG_CMA
408 /*
409 * hugetlb, lru compound (THP), and movable compound pages
410 * can be migrated. Otherwise, fail the isolation.
411 */
412 if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
413 int order;
414 unsigned long outer_pfn;
88ee1343
ZY
415 int page_mt = get_pageblock_migratetype(page);
416 bool isolate_page = !is_migrate_isolate_page(page);
b2c9e2fb
ZY
417 struct compact_control cc = {
418 .nr_migratepages = 0,
419 .order = -1,
420 .zone = page_zone(pfn_to_page(head_pfn)),
421 .mode = MIGRATE_SYNC,
422 .ignore_skip_hint = true,
423 .no_set_skip_hint = true,
424 .gfp_mask = gfp_flags,
425 .alloc_contig = true,
426 };
427 INIT_LIST_HEAD(&cc.migratepages);
428
88ee1343
ZY
429 /*
430 * XXX: mark the page as MIGRATE_ISOLATE so that
431 * no one else can grab the freed page after migration.
432 * Ideally, the page should be freed as two separate
433 * pages to be added into separate migratetype free
434 * lists.
435 */
436 if (isolate_page) {
437 ret = set_migratetype_isolate(page, page_mt,
438 flags, head_pfn, head_pfn + nr_pages);
439 if (ret)
440 goto failed;
441 }
442
b2c9e2fb 443 ret = __alloc_contig_migrate_range(&cc, head_pfn,
c8b36003 444 head_pfn + nr_pages, page_mt);
b2c9e2fb 445
88ee1343
ZY
446 /*
447 * restore the page's migratetype so that it can
448 * be split into separate migratetype free lists
449 * later.
450 */
451 if (isolate_page)
452 unset_migratetype_isolate(page, page_mt);
453
b2c9e2fb
ZY
454 if (ret)
455 goto failed;
456 /*
457 * reset pfn to the head of the free page, so
458 * that the free page handling code above can split
459 * the free page to the right migratetype list.
460 *
461 * head_pfn is not used here as a hugetlb page order
5e0a760b 462 * can be bigger than MAX_PAGE_ORDER, but after it is
b2c9e2fb
ZY
463 * freed, the free page order is not. Use pfn within
464 * the range to find the head of the free page.
465 */
466 order = 0;
467 outer_pfn = pfn;
468 while (!PageBuddy(pfn_to_page(outer_pfn))) {
88ee1343 469 /* stop if we cannot find the free page */
5e0a760b 470 if (++order > MAX_PAGE_ORDER)
88ee1343 471 goto failed;
b2c9e2fb
ZY
472 outer_pfn &= ~0UL << order;
473 }
474 pfn = outer_pfn;
475 continue;
476 } else
477#endif
478 goto failed;
479 }
480
481 pfn++;
482 }
483 return 0;
484failed:
485 /* restore the original migratetype */
9b209e55 486 if (!skip_isolation)
80e2b584 487 unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype);
b2c9e2fb
ZY
488 return -EBUSY;
489}
490
9b7ea46a 491/**
ce5df776
JW
492 * start_isolate_page_range() - mark page range MIGRATE_ISOLATE
493 * @start_pfn: The first PFN of the range to be isolated.
494 * @end_pfn: The last PFN of the range to be isolated.
9b7ea46a
QC
495 * @migratetype: Migrate type to set in error recovery.
496 * @flags: The following flags are allowed (they can be combined in
497 * a bit mask)
756d25be
DH
498 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
499 * e.g., skip over PageHWPoison() pages
aa218795 500 * and PageOffline() pages.
9b7ea46a
QC
501 * REPORT_FAILURE - report details about the failure to
502 * isolate the range
b2c9e2fb
ZY
503 * @gfp_flags: GFP flags used for migrating pages that sit across the
504 * range boundaries.
a5d76b54
KH
505 *
506 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
507 * the range will never be allocated. Any free pages and pages freed in the
9b7ea46a
QC
508 * future will not be allocated again. If specified range includes migrate types
509 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
510 * pages in the range finally, the caller have to free all pages in the range.
511 * test_page_isolated() can be used for test it.
2c7452a0 512 *
b2c9e2fb
ZY
513 * The function first tries to isolate the pageblocks at the beginning and end
514 * of the range, since there might be pages across the range boundaries.
515 * Afterwards, it isolates the rest of the range.
516 *
2c7452a0 517 * There is no high level synchronization mechanism that prevents two threads
9b7ea46a 518 * from trying to isolate overlapping ranges. If this happens, one thread
2c7452a0
MK
519 * will notice pageblocks in the overlapping range already set to isolate.
520 * This happens in set_migratetype_isolate, and set_migratetype_isolate
9b7ea46a
QC
521 * returns an error. We then clean up by restoring the migration type on
522 * pageblocks we may have modified and return -EBUSY to caller. This
2c7452a0 523 * prevents two threads from simultaneously working on overlapping ranges.
9b7ea46a 524 *
96831826
PT
525 * Please note that there is no strong synchronization with the page allocator
526 * either. Pages might be freed while their page blocks are marked ISOLATED.
7612921f
VB
527 * A call to drain_all_pages() after isolation can flush most of them. However
528 * in some cases pages might still end up on pcp lists and that would allow
96831826 529 * for their allocation even when they are in fact isolated already. Depending
ec6e8c7e
VB
530 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
531 * might be used to flush and disable pcplist before isolation and enable after
532 * unisolation.
96831826 533 *
3fa0c7c7 534 * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
a5d76b54 535 */
0815f3d8 536int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
b2c9e2fb 537 int migratetype, int flags, gfp_t gfp_flags)
a5d76b54
KH
538{
539 unsigned long pfn;
a5d76b54 540 struct page *page;
6e263fff 541 /* isolation is done at page block granularity */
4f9bc69a 542 unsigned long isolate_start = pageblock_start_pfn(start_pfn);
5f7fa13f 543 unsigned long isolate_end = pageblock_align(end_pfn);
b2c9e2fb 544 int ret;
9b209e55 545 bool skip_isolation = false;
a5d76b54 546
6e263fff 547 /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
80e2b584
ZY
548 ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false,
549 skip_isolation, migratetype);
b2c9e2fb
ZY
550 if (ret)
551 return ret;
552
9b209e55
ZY
553 if (isolate_start == isolate_end - pageblock_nr_pages)
554 skip_isolation = true;
555
6e263fff 556 /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
80e2b584
ZY
557 ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true,
558 skip_isolation, migratetype);
b2c9e2fb 559 if (ret) {
6e263fff 560 unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype);
b2c9e2fb
ZY
561 return ret;
562 }
563
564 /* skip isolated pageblocks at the beginning and end */
6e263fff
ZY
565 for (pfn = isolate_start + pageblock_nr_pages;
566 pfn < isolate_end - pageblock_nr_pages;
a5d76b54
KH
567 pfn += pageblock_nr_pages) {
568 page = __first_valid_page(pfn, pageblock_nr_pages);
844fbae6
ZY
569 if (page && set_migratetype_isolate(page, migratetype, flags,
570 start_pfn, end_pfn)) {
6e263fff 571 undo_isolate_page_range(isolate_start, pfn, migratetype);
b2c9e2fb 572 unset_migratetype_isolate(
6e263fff 573 pfn_to_page(isolate_end - pageblock_nr_pages),
b2c9e2fb 574 migratetype);
e1d8c966 575 return -EBUSY;
a5d76b54
KH
576 }
577 }
3fa0c7c7 578 return 0;
a5d76b54
KH
579}
580
ce5df776
JW
581/**
582 * undo_isolate_page_range - undo effects of start_isolate_page_range()
583 * @start_pfn: The first PFN of the isolated range
584 * @end_pfn: The last PFN of the isolated range
585 * @migratetype: New migrate type to set on the range
586 *
587 * This finds every MIGRATE_ISOLATE page block in the given range
588 * and switches it to @migratetype.
a5d76b54 589 */
1fcf0a56 590void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
b2c9e2fb 591 int migratetype)
a5d76b54
KH
592{
593 unsigned long pfn;
594 struct page *page;
4f9bc69a 595 unsigned long isolate_start = pageblock_start_pfn(start_pfn);
5f7fa13f 596 unsigned long isolate_end = pageblock_align(end_pfn);
6f8d2b8a 597
6e263fff
ZY
598 for (pfn = isolate_start;
599 pfn < isolate_end;
a5d76b54
KH
600 pfn += pageblock_nr_pages) {
601 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 602 if (!page || !is_migrate_isolate_page(page))
a5d76b54 603 continue;
0815f3d8 604 unset_migratetype_isolate(page, migratetype);
a5d76b54 605 }
a5d76b54
KH
606}
607/*
608 * Test all pages in the range is free(means isolated) or not.
609 * all pages in [start_pfn...end_pfn) must be in the same zone.
610 * zone->lock must be held before call this.
611 *
ec3b6882 612 * Returns the last tested pfn.
a5d76b54 613 */
fea85cff 614static unsigned long
b023f468 615__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
756d25be 616 int flags)
a5d76b54
KH
617{
618 struct page *page;
619
620 while (pfn < end_pfn) {
a5d76b54 621 page = pfn_to_page(pfn);
aa016d14 622 if (PageBuddy(page))
435b405c 623 /*
aa016d14
VB
624 * If the page is on a free list, it has to be on
625 * the correct MIGRATE_ISOLATE freelist. There is no
626 * simple way to verify that as VM_BUG_ON(), though.
435b405c 627 */
ab130f91 628 pfn += 1 << buddy_order(page);
756d25be 629 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
aa016d14 630 /* A HWPoisoned page cannot be also PageBuddy */
b023f468 631 pfn++;
aa218795
DH
632 else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
633 !page_count(page))
634 /*
635 * The responsible driver agreed to skip PageOffline()
636 * pages when offlining memory by dropping its
637 * reference in MEM_GOING_OFFLINE.
638 */
639 pfn++;
a5d76b54
KH
640 else
641 break;
642 }
fea85cff
JK
643
644 return pfn;
a5d76b54
KH
645}
646
ce5df776
JW
647/**
648 * test_pages_isolated - check if pageblocks in range are isolated
649 * @start_pfn: The first PFN of the isolated range
650 * @end_pfn: The first PFN *after* the isolated range
651 * @isol_flags: Testing mode flags
652 *
653 * This tests if all in the specified range are free.
654 *
655 * If %MEMORY_OFFLINE is specified in @flags, it will consider
656 * poisoned and offlined pages free as well.
657 *
658 * Caller must ensure the requested range doesn't span zones.
659 *
660 * Returns 0 if true, -EBUSY if one or more pages are in use.
661 */
b023f468 662int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
756d25be 663 int isol_flags)
a5d76b54 664{
6c1b7f68 665 unsigned long pfn, flags;
a5d76b54 666 struct page *page;
6c1b7f68 667 struct zone *zone;
1d09510b 668 int ret;
a5d76b54 669
a5d76b54 670 /*
5e0a760b
KS
671 * Note: pageblock_nr_pages != MAX_PAGE_ORDER. Then, chunks of free
672 * pages are not aligned to pageblock_nr_pages.
85dbe706 673 * Then we just check migratetype first.
a5d76b54
KH
674 */
675 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
676 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 677 if (page && !is_migrate_isolate_page(page))
a5d76b54
KH
678 break;
679 }
a70dcb96 680 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
1d09510b
GD
681 if ((pfn < end_pfn) || !page) {
682 ret = -EBUSY;
683 goto out;
684 }
685
85dbe706 686 /* Check all pages are free or marked as ISOLATED */
a70dcb96 687 zone = page_zone(page);
6c1b7f68 688 spin_lock_irqsave(&zone->lock, flags);
756d25be 689 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
6c1b7f68 690 spin_unlock_irqrestore(&zone->lock, flags);
fea85cff 691
1d09510b
GD
692 ret = pfn < end_pfn ? -EBUSY : 0;
693
694out:
0f0848e5
JK
695 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
696
1d09510b 697 return ret;
a5d76b54 698}