]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/page_isolation.c
mm: make alloc_contig_range work at pageblock granularity
[thirdparty/linux.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
40 VM_BUG_ON(ALIGN_DOWN(start_pfn, pageblock_nr_pages) !=
41 ALIGN_DOWN(end_pfn - 1, pageblock_nr_pages));
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)) {
82 struct page *head = compound_head(page);
83 unsigned int skip_pages;
84
85 if (PageHuge(page)) {
86 if (!hugepage_migration_supported(page_hstate(head)))
87 return page;
88 } else if (!PageLRU(head) && !__PageMovable(head)) {
89 return page;
90 }
91
92 skip_pages = compound_nr(head) - (page - head);
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
ZY
174 check_unmovable_start = max(page_to_pfn(page), start_pfn);
175 check_unmovable_end = min(ALIGN(page_to_pfn(page) + 1, pageblock_nr_pages),
176 end_pfn);
177
178 unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
179 migratetype, isol_flags);
4a55c047 180 if (!unmovable) {
2139cbe6 181 unsigned long nr_pages;
4da2ce25 182 int mt = get_pageblock_migratetype(page);
2139cbe6 183
a458431e 184 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
ad53f92e 185 zone->nr_isolate_pageblock++;
02aa0cdd
VB
186 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
187 NULL);
2139cbe6 188
4da2ce25 189 __mod_zone_freepage_state(zone, -nr_pages, mt);
1c31cb49 190 spin_unlock_irqrestore(&zone->lock, flags);
1c31cb49 191 return 0;
ee6f509c
MK
192 }
193
194 spin_unlock_irqrestore(&zone->lock, flags);
1c31cb49 195 if (isol_flags & REPORT_FAILURE) {
48381d7e
DH
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");
3d680bdf 201 }
4a55c047 202
1c31cb49 203 return -EBUSY;
ee6f509c
MK
204}
205
b2c9e2fb 206static void unset_migratetype_isolate(struct page *page, int migratetype)
ee6f509c
MK
207{
208 struct zone *zone;
2139cbe6 209 unsigned long flags, nr_pages;
e3a2713c 210 bool isolated_page = false;
3c605096 211 unsigned int order;
3c605096 212 struct page *buddy;
2139cbe6 213
ee6f509c
MK
214 zone = page_zone(page);
215 spin_lock_irqsave(&zone->lock, flags);
bbf9ce97 216 if (!is_migrate_isolate_page(page))
ee6f509c 217 goto out;
3c605096
JK
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)) {
ab130f91 228 order = buddy_order(page);
2484be0f 229 if (order >= pageblock_order && order < MAX_ORDER - 1) {
8170ac47
ZY
230 buddy = find_buddy_page_pfn(page, page_to_pfn(page),
231 order, NULL);
232 if (buddy && !is_migrate_isolate_page(buddy)) {
a500cb34
ML
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);
3c605096
JK
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.
293ffa5e
DH
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.
3c605096 253 */
a85468b7 254 if (!isolated_page) {
02aa0cdd 255 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
3c605096
JK
256 __mod_zone_freepage_state(zone, nr_pages, migratetype);
257 }
a458431e 258 set_pageblock_migratetype(page, migratetype);
624f58d8
AD
259 if (isolated_page)
260 __putback_isolated_page(page, order, migratetype);
ad53f92e 261 zone->nr_isolate_pageblock--;
ee6f509c
MK
262out:
263 spin_unlock_irqrestore(&zone->lock, flags);
264}
265
a5d76b54
KH
266static inline struct page *
267__first_valid_page(unsigned long pfn, unsigned long nr_pages)
268{
269 int i;
2ce13640
MH
270
271 for (i = 0; i < nr_pages; i++) {
272 struct page *page;
273
2ce13640
MH
274 page = pfn_to_online_page(pfn + i);
275 if (!page)
276 continue;
277 return page;
278 }
279 return NULL;
a5d76b54
KH
280}
281
b2c9e2fb
ZY
282/**
283 * isolate_single_pageblock() -- tries to isolate a pageblock that might be
284 * within a free or in-use page.
285 * @boundary_pfn: pageblock-aligned pfn that a page might cross
286 * @gfp_flags: GFP flags used for migrating pages
287 * @isolate_before: isolate the pageblock before the boundary_pfn
288 *
289 * Free and in-use pages can be as big as MAX_ORDER-1 and contain more than one
290 * pageblock. When not all pageblocks within a page are isolated at the same
291 * time, free page accounting can go wrong. For example, in the case of
292 * MAX_ORDER-1 = pageblock_order + 1, a MAX_ORDER-1 page has two pagelbocks.
293 * [ MAX_ORDER-1 ]
294 * [ pageblock0 | pageblock1 ]
295 * When either pageblock is isolated, if it is a free page, the page is not
296 * split into separate migratetype lists, which is supposed to; if it is an
297 * in-use page and freed later, __free_one_page() does not split the free page
298 * either. The function handles this by splitting the free page or migrating
299 * the in-use page then splitting the free page.
300 */
301static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
302 bool isolate_before)
303{
304 unsigned char saved_mt;
305 unsigned long start_pfn;
306 unsigned long isolate_pageblock;
307 unsigned long pfn;
308 struct zone *zone;
309
310 VM_BUG_ON(!IS_ALIGNED(boundary_pfn, pageblock_nr_pages));
311
312 if (isolate_before)
313 isolate_pageblock = boundary_pfn - pageblock_nr_pages;
314 else
315 isolate_pageblock = boundary_pfn;
316
317 /*
318 * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid
319 * only isolating a subset of pageblocks from a bigger than pageblock
320 * free or in-use page. Also make sure all to-be-isolated pageblocks
321 * are within the same zone.
322 */
323 zone = page_zone(pfn_to_page(isolate_pageblock));
324 start_pfn = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES),
325 zone->zone_start_pfn);
326
327 saved_mt = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
328 set_pageblock_migratetype(pfn_to_page(isolate_pageblock), MIGRATE_ISOLATE);
329
330 /*
331 * Bail out early when the to-be-isolated pageblock does not form
332 * a free or in-use page across boundary_pfn:
333 *
334 * 1. isolate before boundary_pfn: the page after is not online
335 * 2. isolate after boundary_pfn: the page before is not online
336 *
337 * This also ensures correctness. Without it, when isolate after
338 * boundary_pfn and [start_pfn, boundary_pfn) are not online,
339 * __first_valid_page() will return unexpected NULL in the for loop
340 * below.
341 */
342 if (isolate_before) {
343 if (!pfn_to_online_page(boundary_pfn))
344 return 0;
345 } else {
346 if (!pfn_to_online_page(boundary_pfn - 1))
347 return 0;
348 }
349
350 for (pfn = start_pfn; pfn < boundary_pfn;) {
351 struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
352
353 VM_BUG_ON(!page);
354 pfn = page_to_pfn(page);
355 /*
356 * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
357 * free pages in [start_pfn, boundary_pfn), its head page will
358 * always be in the range.
359 */
360 if (PageBuddy(page)) {
361 int order = buddy_order(page);
362
363 if (pfn + (1UL << order) > boundary_pfn)
364 split_free_page(page, order, boundary_pfn - pfn);
365 pfn += (1UL << order);
366 continue;
367 }
368 /*
369 * migrate compound pages then let the free page handling code
370 * above do the rest. If migration is not possible, just fail.
371 */
372 if (PageCompound(page)) {
373 unsigned long nr_pages = compound_nr(page);
374 struct page *head = compound_head(page);
375 unsigned long head_pfn = page_to_pfn(head);
376
377 if (head_pfn + nr_pages < boundary_pfn) {
378 pfn = head_pfn + nr_pages;
379 continue;
380 }
381#if defined CONFIG_COMPACTION || defined CONFIG_CMA
382 /*
383 * hugetlb, lru compound (THP), and movable compound pages
384 * can be migrated. Otherwise, fail the isolation.
385 */
386 if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
387 int order;
388 unsigned long outer_pfn;
389 int ret;
390 struct compact_control cc = {
391 .nr_migratepages = 0,
392 .order = -1,
393 .zone = page_zone(pfn_to_page(head_pfn)),
394 .mode = MIGRATE_SYNC,
395 .ignore_skip_hint = true,
396 .no_set_skip_hint = true,
397 .gfp_mask = gfp_flags,
398 .alloc_contig = true,
399 };
400 INIT_LIST_HEAD(&cc.migratepages);
401
402 ret = __alloc_contig_migrate_range(&cc, head_pfn,
403 head_pfn + nr_pages);
404
405 if (ret)
406 goto failed;
407 /*
408 * reset pfn to the head of the free page, so
409 * that the free page handling code above can split
410 * the free page to the right migratetype list.
411 *
412 * head_pfn is not used here as a hugetlb page order
413 * can be bigger than MAX_ORDER-1, but after it is
414 * freed, the free page order is not. Use pfn within
415 * the range to find the head of the free page.
416 */
417 order = 0;
418 outer_pfn = pfn;
419 while (!PageBuddy(pfn_to_page(outer_pfn))) {
420 if (++order >= MAX_ORDER) {
421 outer_pfn = pfn;
422 break;
423 }
424 outer_pfn &= ~0UL << order;
425 }
426 pfn = outer_pfn;
427 continue;
428 } else
429#endif
430 goto failed;
431 }
432
433 pfn++;
434 }
435 return 0;
436failed:
437 /* restore the original migratetype */
438 set_pageblock_migratetype(pfn_to_page(isolate_pageblock), saved_mt);
439 return -EBUSY;
440}
441
9b7ea46a
QC
442/**
443 * start_isolate_page_range() - make page-allocation-type of range of pages to
444 * be MIGRATE_ISOLATE.
445 * @start_pfn: The lower PFN of the range to be isolated.
446 * @end_pfn: The upper PFN of the range to be isolated.
447 * start_pfn/end_pfn must be aligned to pageblock_order.
448 * @migratetype: Migrate type to set in error recovery.
449 * @flags: The following flags are allowed (they can be combined in
450 * a bit mask)
756d25be
DH
451 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
452 * e.g., skip over PageHWPoison() pages
aa218795 453 * and PageOffline() pages.
9b7ea46a
QC
454 * REPORT_FAILURE - report details about the failure to
455 * isolate the range
b2c9e2fb
ZY
456 * @gfp_flags: GFP flags used for migrating pages that sit across the
457 * range boundaries.
a5d76b54
KH
458 *
459 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
460 * the range will never be allocated. Any free pages and pages freed in the
9b7ea46a
QC
461 * future will not be allocated again. If specified range includes migrate types
462 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
463 * pages in the range finally, the caller have to free all pages in the range.
464 * test_page_isolated() can be used for test it.
2c7452a0 465 *
b2c9e2fb
ZY
466 * The function first tries to isolate the pageblocks at the beginning and end
467 * of the range, since there might be pages across the range boundaries.
468 * Afterwards, it isolates the rest of the range.
469 *
2c7452a0 470 * There is no high level synchronization mechanism that prevents two threads
9b7ea46a 471 * from trying to isolate overlapping ranges. If this happens, one thread
2c7452a0
MK
472 * will notice pageblocks in the overlapping range already set to isolate.
473 * This happens in set_migratetype_isolate, and set_migratetype_isolate
9b7ea46a
QC
474 * returns an error. We then clean up by restoring the migration type on
475 * pageblocks we may have modified and return -EBUSY to caller. This
2c7452a0 476 * prevents two threads from simultaneously working on overlapping ranges.
9b7ea46a 477 *
96831826
PT
478 * Please note that there is no strong synchronization with the page allocator
479 * either. Pages might be freed while their page blocks are marked ISOLATED.
7612921f
VB
480 * A call to drain_all_pages() after isolation can flush most of them. However
481 * in some cases pages might still end up on pcp lists and that would allow
96831826 482 * for their allocation even when they are in fact isolated already. Depending
ec6e8c7e
VB
483 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
484 * might be used to flush and disable pcplist before isolation and enable after
485 * unisolation.
96831826 486 *
3fa0c7c7 487 * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
a5d76b54 488 */
0815f3d8 489int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
b2c9e2fb 490 int migratetype, int flags, gfp_t gfp_flags)
a5d76b54
KH
491{
492 unsigned long pfn;
a5d76b54 493 struct page *page;
b2c9e2fb 494 int ret;
a5d76b54 495
fec174d6
NH
496 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
497 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
a5d76b54 498
b2c9e2fb
ZY
499 /* isolate [start_pfn, start_pfn + pageblock_nr_pages) pageblock */
500 ret = isolate_single_pageblock(start_pfn, gfp_flags, false);
501 if (ret)
502 return ret;
503
504 /* isolate [end_pfn - pageblock_nr_pages, end_pfn) pageblock */
505 ret = isolate_single_pageblock(end_pfn, gfp_flags, true);
506 if (ret) {
507 unset_migratetype_isolate(pfn_to_page(start_pfn), migratetype);
508 return ret;
509 }
510
511 /* skip isolated pageblocks at the beginning and end */
512 for (pfn = start_pfn + pageblock_nr_pages;
513 pfn < end_pfn - pageblock_nr_pages;
a5d76b54
KH
514 pfn += pageblock_nr_pages) {
515 page = __first_valid_page(pfn, pageblock_nr_pages);
844fbae6
ZY
516 if (page && set_migratetype_isolate(page, migratetype, flags,
517 start_pfn, end_pfn)) {
e1d8c966 518 undo_isolate_page_range(start_pfn, pfn, migratetype);
b2c9e2fb
ZY
519 unset_migratetype_isolate(
520 pfn_to_page(end_pfn - pageblock_nr_pages),
521 migratetype);
e1d8c966 522 return -EBUSY;
a5d76b54
KH
523 }
524 }
3fa0c7c7 525 return 0;
a5d76b54
KH
526}
527
528/*
529 * Make isolated pages available again.
530 */
1fcf0a56 531void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
b2c9e2fb 532 int migratetype)
a5d76b54
KH
533{
534 unsigned long pfn;
535 struct page *page;
6f8d2b8a
WX
536
537 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
538 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
539
a5d76b54
KH
540 for (pfn = start_pfn;
541 pfn < end_pfn;
542 pfn += pageblock_nr_pages) {
543 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 544 if (!page || !is_migrate_isolate_page(page))
a5d76b54 545 continue;
0815f3d8 546 unset_migratetype_isolate(page, migratetype);
a5d76b54 547 }
a5d76b54
KH
548}
549/*
550 * Test all pages in the range is free(means isolated) or not.
551 * all pages in [start_pfn...end_pfn) must be in the same zone.
552 * zone->lock must be held before call this.
553 *
ec3b6882 554 * Returns the last tested pfn.
a5d76b54 555 */
fea85cff 556static unsigned long
b023f468 557__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
756d25be 558 int flags)
a5d76b54
KH
559{
560 struct page *page;
561
562 while (pfn < end_pfn) {
a5d76b54 563 page = pfn_to_page(pfn);
aa016d14 564 if (PageBuddy(page))
435b405c 565 /*
aa016d14
VB
566 * If the page is on a free list, it has to be on
567 * the correct MIGRATE_ISOLATE freelist. There is no
568 * simple way to verify that as VM_BUG_ON(), though.
435b405c 569 */
ab130f91 570 pfn += 1 << buddy_order(page);
756d25be 571 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
aa016d14 572 /* A HWPoisoned page cannot be also PageBuddy */
b023f468 573 pfn++;
aa218795
DH
574 else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
575 !page_count(page))
576 /*
577 * The responsible driver agreed to skip PageOffline()
578 * pages when offlining memory by dropping its
579 * reference in MEM_GOING_OFFLINE.
580 */
581 pfn++;
a5d76b54
KH
582 else
583 break;
584 }
fea85cff
JK
585
586 return pfn;
a5d76b54
KH
587}
588
b9eb6319 589/* Caller should ensure that requested range is in a single zone */
b023f468 590int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
756d25be 591 int isol_flags)
a5d76b54 592{
6c1b7f68 593 unsigned long pfn, flags;
a5d76b54 594 struct page *page;
6c1b7f68 595 struct zone *zone;
1d09510b 596 int ret;
a5d76b54 597
a5d76b54 598 /*
85dbe706
TC
599 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
600 * are not aligned to pageblock_nr_pages.
601 * Then we just check migratetype first.
a5d76b54
KH
602 */
603 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
604 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 605 if (page && !is_migrate_isolate_page(page))
a5d76b54
KH
606 break;
607 }
a70dcb96 608 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
1d09510b
GD
609 if ((pfn < end_pfn) || !page) {
610 ret = -EBUSY;
611 goto out;
612 }
613
85dbe706 614 /* Check all pages are free or marked as ISOLATED */
a70dcb96 615 zone = page_zone(page);
6c1b7f68 616 spin_lock_irqsave(&zone->lock, flags);
756d25be 617 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
6c1b7f68 618 spin_unlock_irqrestore(&zone->lock, flags);
fea85cff 619
1d09510b
GD
620 ret = pfn < end_pfn ? -EBUSY : 0;
621
622out:
0f0848e5
JK
623 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
624
1d09510b 625 return ret;
a5d76b54 626}