Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/mm/page_isolation.c
0004  */
0005 
0006 #include <linux/mm.h>
0007 #include <linux/page-isolation.h>
0008 #include <linux/pageblock-flags.h>
0009 #include <linux/memory.h>
0010 #include <linux/hugetlb.h>
0011 #include <linux/page_owner.h>
0012 #include <linux/migrate.h>
0013 #include "internal.h"
0014 
0015 #define CREATE_TRACE_POINTS
0016 #include <trace/events/page_isolation.h>
0017 
0018 /*
0019  * This function checks whether the range [start_pfn, end_pfn) includes
0020  * unmovable pages or not. The range must fall into a single pageblock and
0021  * consequently belong to a single zone.
0022  *
0023  * PageLRU check without isolation or lru_lock could race so that
0024  * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
0025  * check without lock_page also may miss some movable non-lru pages at
0026  * race condition. So you can't expect this function should be exact.
0027  *
0028  * Returns a page without holding a reference. If the caller wants to
0029  * dereference that page (e.g., dumping), it has to make sure that it
0030  * cannot get removed (e.g., via memory unplug) concurrently.
0031  *
0032  */
0033 static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn,
0034                 int migratetype, int flags)
0035 {
0036     struct page *page = pfn_to_page(start_pfn);
0037     struct zone *zone = page_zone(page);
0038     unsigned long pfn;
0039 
0040     VM_BUG_ON(ALIGN_DOWN(start_pfn, pageblock_nr_pages) !=
0041           ALIGN_DOWN(end_pfn - 1, pageblock_nr_pages));
0042 
0043     if (is_migrate_cma_page(page)) {
0044         /*
0045          * CMA allocations (alloc_contig_range) really need to mark
0046          * isolate CMA pageblocks even when they are not movable in fact
0047          * so consider them movable here.
0048          */
0049         if (is_migrate_cma(migratetype))
0050             return NULL;
0051 
0052         return page;
0053     }
0054 
0055     for (pfn = start_pfn; pfn < end_pfn; pfn++) {
0056         page = pfn_to_page(pfn);
0057 
0058         /*
0059          * Both, bootmem allocations and memory holes are marked
0060          * PG_reserved and are unmovable. We can even have unmovable
0061          * allocations inside ZONE_MOVABLE, for example when
0062          * specifying "movablecore".
0063          */
0064         if (PageReserved(page))
0065             return page;
0066 
0067         /*
0068          * If the zone is movable and we have ruled out all reserved
0069          * pages then it should be reasonably safe to assume the rest
0070          * is movable.
0071          */
0072         if (zone_idx(zone) == ZONE_MOVABLE)
0073             continue;
0074 
0075         /*
0076          * Hugepages are not in LRU lists, but they're movable.
0077          * THPs are on the LRU, but need to be counted as #small pages.
0078          * We need not scan over tail pages because we don't
0079          * handle each tail page individually in migration.
0080          */
0081         if (PageHuge(page) || PageTransCompound(page)) {
0082             struct page *head = compound_head(page);
0083             unsigned int skip_pages;
0084 
0085             if (PageHuge(page)) {
0086                 if (!hugepage_migration_supported(page_hstate(head)))
0087                     return page;
0088             } else if (!PageLRU(head) && !__PageMovable(head)) {
0089                 return page;
0090             }
0091 
0092             skip_pages = compound_nr(head) - (page - head);
0093             pfn += skip_pages - 1;
0094             continue;
0095         }
0096 
0097         /*
0098          * We can't use page_count without pin a page
0099          * because another CPU can free compound page.
0100          * This check already skips compound tails of THP
0101          * because their page->_refcount is zero at all time.
0102          */
0103         if (!page_ref_count(page)) {
0104             if (PageBuddy(page))
0105                 pfn += (1 << buddy_order(page)) - 1;
0106             continue;
0107         }
0108 
0109         /*
0110          * The HWPoisoned page may be not in buddy system, and
0111          * page_count() is not 0.
0112          */
0113         if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
0114             continue;
0115 
0116         /*
0117          * We treat all PageOffline() pages as movable when offlining
0118          * to give drivers a chance to decrement their reference count
0119          * in MEM_GOING_OFFLINE in order to indicate that these pages
0120          * can be offlined as there are no direct references anymore.
0121          * For actually unmovable PageOffline() where the driver does
0122          * not support this, we will fail later when trying to actually
0123          * move these pages that still have a reference count > 0.
0124          * (false negatives in this function only)
0125          */
0126         if ((flags & MEMORY_OFFLINE) && PageOffline(page))
0127             continue;
0128 
0129         if (__PageMovable(page) || PageLRU(page))
0130             continue;
0131 
0132         /*
0133          * If there are RECLAIMABLE pages, we need to check
0134          * it.  But now, memory offline itself doesn't call
0135          * shrink_node_slabs() and it still to be fixed.
0136          */
0137         return page;
0138     }
0139     return NULL;
0140 }
0141 
0142 /*
0143  * This function set pageblock migratetype to isolate if no unmovable page is
0144  * present in [start_pfn, end_pfn). The pageblock must intersect with
0145  * [start_pfn, end_pfn).
0146  */
0147 static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags,
0148             unsigned long start_pfn, unsigned long end_pfn)
0149 {
0150     struct zone *zone = page_zone(page);
0151     struct page *unmovable;
0152     unsigned long flags;
0153     unsigned long check_unmovable_start, check_unmovable_end;
0154 
0155     spin_lock_irqsave(&zone->lock, flags);
0156 
0157     /*
0158      * We assume the caller intended to SET migrate type to isolate.
0159      * If it is already set, then someone else must have raced and
0160      * set it before us.
0161      */
0162     if (is_migrate_isolate_page(page)) {
0163         spin_unlock_irqrestore(&zone->lock, flags);
0164         return -EBUSY;
0165     }
0166 
0167     /*
0168      * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
0169      * We just check MOVABLE pages.
0170      *
0171      * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
0172      * to avoid redundant checks.
0173      */
0174     check_unmovable_start = max(page_to_pfn(page), start_pfn);
0175     check_unmovable_end = min(ALIGN(page_to_pfn(page) + 1, pageblock_nr_pages),
0176                   end_pfn);
0177 
0178     unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
0179             migratetype, isol_flags);
0180     if (!unmovable) {
0181         unsigned long nr_pages;
0182         int mt = get_pageblock_migratetype(page);
0183 
0184         set_pageblock_migratetype(page, MIGRATE_ISOLATE);
0185         zone->nr_isolate_pageblock++;
0186         nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
0187                                     NULL);
0188 
0189         __mod_zone_freepage_state(zone, -nr_pages, mt);
0190         spin_unlock_irqrestore(&zone->lock, flags);
0191         return 0;
0192     }
0193 
0194     spin_unlock_irqrestore(&zone->lock, flags);
0195     if (isol_flags & REPORT_FAILURE) {
0196         /*
0197          * printk() with zone->lock held will likely trigger a
0198          * lockdep splat, so defer it here.
0199          */
0200         dump_page(unmovable, "unmovable page");
0201     }
0202 
0203     return -EBUSY;
0204 }
0205 
0206 static void unset_migratetype_isolate(struct page *page, int migratetype)
0207 {
0208     struct zone *zone;
0209     unsigned long flags, nr_pages;
0210     bool isolated_page = false;
0211     unsigned int order;
0212     struct page *buddy;
0213 
0214     zone = page_zone(page);
0215     spin_lock_irqsave(&zone->lock, flags);
0216     if (!is_migrate_isolate_page(page))
0217         goto out;
0218 
0219     /*
0220      * Because freepage with more than pageblock_order on isolated
0221      * pageblock is restricted to merge due to freepage counting problem,
0222      * it is possible that there is free buddy page.
0223      * move_freepages_block() doesn't care of merge so we need other
0224      * approach in order to merge them. Isolation and free will make
0225      * these pages to be merged.
0226      */
0227     if (PageBuddy(page)) {
0228         order = buddy_order(page);
0229         if (order >= pageblock_order && order < MAX_ORDER - 1) {
0230             buddy = find_buddy_page_pfn(page, page_to_pfn(page),
0231                             order, NULL);
0232             if (buddy && !is_migrate_isolate_page(buddy)) {
0233                 isolated_page = !!__isolate_free_page(page, order);
0234                 /*
0235                  * Isolating a free page in an isolated pageblock
0236                  * is expected to always work as watermarks don't
0237                  * apply here.
0238                  */
0239                 VM_WARN_ON(!isolated_page);
0240             }
0241         }
0242     }
0243 
0244     /*
0245      * If we isolate freepage with more than pageblock_order, there
0246      * should be no freepage in the range, so we could avoid costly
0247      * pageblock scanning for freepage moving.
0248      *
0249      * We didn't actually touch any of the isolated pages, so place them
0250      * to the tail of the freelist. This is an optimization for memory
0251      * onlining - just onlined memory won't immediately be considered for
0252      * allocation.
0253      */
0254     if (!isolated_page) {
0255         nr_pages = move_freepages_block(zone, page, migratetype, NULL);
0256         __mod_zone_freepage_state(zone, nr_pages, migratetype);
0257     }
0258     set_pageblock_migratetype(page, migratetype);
0259     if (isolated_page)
0260         __putback_isolated_page(page, order, migratetype);
0261     zone->nr_isolate_pageblock--;
0262 out:
0263     spin_unlock_irqrestore(&zone->lock, flags);
0264 }
0265 
0266 static inline struct page *
0267 __first_valid_page(unsigned long pfn, unsigned long nr_pages)
0268 {
0269     int i;
0270 
0271     for (i = 0; i < nr_pages; i++) {
0272         struct page *page;
0273 
0274         page = pfn_to_online_page(pfn + i);
0275         if (!page)
0276             continue;
0277         return page;
0278     }
0279     return NULL;
0280 }
0281 
0282 /**
0283  * isolate_single_pageblock() -- tries to isolate a pageblock that might be
0284  * within a free or in-use page.
0285  * @boundary_pfn:       pageblock-aligned pfn that a page might cross
0286  * @flags:          isolation flags
0287  * @gfp_flags:          GFP flags used for migrating pages
0288  * @isolate_before: isolate the pageblock before the boundary_pfn
0289  * @skip_isolation: the flag to skip the pageblock isolation in second
0290  *          isolate_single_pageblock()
0291  * @migratetype:    migrate type to set in error recovery.
0292  *
0293  * Free and in-use pages can be as big as MAX_ORDER-1 and contain more than one
0294  * pageblock. When not all pageblocks within a page are isolated at the same
0295  * time, free page accounting can go wrong. For example, in the case of
0296  * MAX_ORDER-1 = pageblock_order + 1, a MAX_ORDER-1 page has two pagelbocks.
0297  * [         MAX_ORDER-1         ]
0298  * [  pageblock0  |  pageblock1  ]
0299  * When either pageblock is isolated, if it is a free page, the page is not
0300  * split into separate migratetype lists, which is supposed to; if it is an
0301  * in-use page and freed later, __free_one_page() does not split the free page
0302  * either. The function handles this by splitting the free page or migrating
0303  * the in-use page then splitting the free page.
0304  */
0305 static int isolate_single_pageblock(unsigned long boundary_pfn, int flags,
0306             gfp_t gfp_flags, bool isolate_before, bool skip_isolation,
0307             int migratetype)
0308 {
0309     unsigned long start_pfn;
0310     unsigned long isolate_pageblock;
0311     unsigned long pfn;
0312     struct zone *zone;
0313     int ret;
0314 
0315     VM_BUG_ON(!IS_ALIGNED(boundary_pfn, pageblock_nr_pages));
0316 
0317     if (isolate_before)
0318         isolate_pageblock = boundary_pfn - pageblock_nr_pages;
0319     else
0320         isolate_pageblock = boundary_pfn;
0321 
0322     /*
0323      * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid
0324      * only isolating a subset of pageblocks from a bigger than pageblock
0325      * free or in-use page. Also make sure all to-be-isolated pageblocks
0326      * are within the same zone.
0327      */
0328     zone  = page_zone(pfn_to_page(isolate_pageblock));
0329     start_pfn  = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES),
0330                       zone->zone_start_pfn);
0331 
0332     if (skip_isolation) {
0333         int mt = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
0334 
0335         VM_BUG_ON(!is_migrate_isolate(mt));
0336     } else {
0337         ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype,
0338                 flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages);
0339 
0340         if (ret)
0341             return ret;
0342     }
0343 
0344     /*
0345      * Bail out early when the to-be-isolated pageblock does not form
0346      * a free or in-use page across boundary_pfn:
0347      *
0348      * 1. isolate before boundary_pfn: the page after is not online
0349      * 2. isolate after boundary_pfn: the page before is not online
0350      *
0351      * This also ensures correctness. Without it, when isolate after
0352      * boundary_pfn and [start_pfn, boundary_pfn) are not online,
0353      * __first_valid_page() will return unexpected NULL in the for loop
0354      * below.
0355      */
0356     if (isolate_before) {
0357         if (!pfn_to_online_page(boundary_pfn))
0358             return 0;
0359     } else {
0360         if (!pfn_to_online_page(boundary_pfn - 1))
0361             return 0;
0362     }
0363 
0364     for (pfn = start_pfn; pfn < boundary_pfn;) {
0365         struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
0366 
0367         VM_BUG_ON(!page);
0368         pfn = page_to_pfn(page);
0369         /*
0370          * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
0371          * free pages in [start_pfn, boundary_pfn), its head page will
0372          * always be in the range.
0373          */
0374         if (PageBuddy(page)) {
0375             int order = buddy_order(page);
0376 
0377             if (pfn + (1UL << order) > boundary_pfn) {
0378                 /* free page changed before split, check it again */
0379                 if (split_free_page(page, order, boundary_pfn - pfn))
0380                     continue;
0381             }
0382 
0383             pfn += 1UL << order;
0384             continue;
0385         }
0386         /*
0387          * migrate compound pages then let the free page handling code
0388          * above do the rest. If migration is not possible, just fail.
0389          */
0390         if (PageCompound(page)) {
0391             struct page *head = compound_head(page);
0392             unsigned long head_pfn = page_to_pfn(head);
0393             unsigned long nr_pages = compound_nr(head);
0394 
0395             if (head_pfn + nr_pages <= boundary_pfn) {
0396                 pfn = head_pfn + nr_pages;
0397                 continue;
0398             }
0399 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
0400             /*
0401              * hugetlb, lru compound (THP), and movable compound pages
0402              * can be migrated. Otherwise, fail the isolation.
0403              */
0404             if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
0405                 int order;
0406                 unsigned long outer_pfn;
0407                 int page_mt = get_pageblock_migratetype(page);
0408                 bool isolate_page = !is_migrate_isolate_page(page);
0409                 struct compact_control cc = {
0410                     .nr_migratepages = 0,
0411                     .order = -1,
0412                     .zone = page_zone(pfn_to_page(head_pfn)),
0413                     .mode = MIGRATE_SYNC,
0414                     .ignore_skip_hint = true,
0415                     .no_set_skip_hint = true,
0416                     .gfp_mask = gfp_flags,
0417                     .alloc_contig = true,
0418                 };
0419                 INIT_LIST_HEAD(&cc.migratepages);
0420 
0421                 /*
0422                  * XXX: mark the page as MIGRATE_ISOLATE so that
0423                  * no one else can grab the freed page after migration.
0424                  * Ideally, the page should be freed as two separate
0425                  * pages to be added into separate migratetype free
0426                  * lists.
0427                  */
0428                 if (isolate_page) {
0429                     ret = set_migratetype_isolate(page, page_mt,
0430                         flags, head_pfn, head_pfn + nr_pages);
0431                     if (ret)
0432                         goto failed;
0433                 }
0434 
0435                 ret = __alloc_contig_migrate_range(&cc, head_pfn,
0436                             head_pfn + nr_pages);
0437 
0438                 /*
0439                  * restore the page's migratetype so that it can
0440                  * be split into separate migratetype free lists
0441                  * later.
0442                  */
0443                 if (isolate_page)
0444                     unset_migratetype_isolate(page, page_mt);
0445 
0446                 if (ret)
0447                     goto failed;
0448                 /*
0449                  * reset pfn to the head of the free page, so
0450                  * that the free page handling code above can split
0451                  * the free page to the right migratetype list.
0452                  *
0453                  * head_pfn is not used here as a hugetlb page order
0454                  * can be bigger than MAX_ORDER-1, but after it is
0455                  * freed, the free page order is not. Use pfn within
0456                  * the range to find the head of the free page.
0457                  */
0458                 order = 0;
0459                 outer_pfn = pfn;
0460                 while (!PageBuddy(pfn_to_page(outer_pfn))) {
0461                     /* stop if we cannot find the free page */
0462                     if (++order >= MAX_ORDER)
0463                         goto failed;
0464                     outer_pfn &= ~0UL << order;
0465                 }
0466                 pfn = outer_pfn;
0467                 continue;
0468             } else
0469 #endif
0470                 goto failed;
0471         }
0472 
0473         pfn++;
0474     }
0475     return 0;
0476 failed:
0477     /* restore the original migratetype */
0478     if (!skip_isolation)
0479         unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype);
0480     return -EBUSY;
0481 }
0482 
0483 /**
0484  * start_isolate_page_range() - make page-allocation-type of range of pages to
0485  * be MIGRATE_ISOLATE.
0486  * @start_pfn:      The lower PFN of the range to be isolated.
0487  * @end_pfn:        The upper PFN of the range to be isolated.
0488  * @migratetype:    Migrate type to set in error recovery.
0489  * @flags:      The following flags are allowed (they can be combined in
0490  *          a bit mask)
0491  *          MEMORY_OFFLINE - isolate to offline (!allocate) memory
0492  *                   e.g., skip over PageHWPoison() pages
0493  *                   and PageOffline() pages.
0494  *          REPORT_FAILURE - report details about the failure to
0495  *          isolate the range
0496  * @gfp_flags:      GFP flags used for migrating pages that sit across the
0497  *          range boundaries.
0498  *
0499  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
0500  * the range will never be allocated. Any free pages and pages freed in the
0501  * future will not be allocated again. If specified range includes migrate types
0502  * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
0503  * pages in the range finally, the caller have to free all pages in the range.
0504  * test_page_isolated() can be used for test it.
0505  *
0506  * The function first tries to isolate the pageblocks at the beginning and end
0507  * of the range, since there might be pages across the range boundaries.
0508  * Afterwards, it isolates the rest of the range.
0509  *
0510  * There is no high level synchronization mechanism that prevents two threads
0511  * from trying to isolate overlapping ranges. If this happens, one thread
0512  * will notice pageblocks in the overlapping range already set to isolate.
0513  * This happens in set_migratetype_isolate, and set_migratetype_isolate
0514  * returns an error. We then clean up by restoring the migration type on
0515  * pageblocks we may have modified and return -EBUSY to caller. This
0516  * prevents two threads from simultaneously working on overlapping ranges.
0517  *
0518  * Please note that there is no strong synchronization with the page allocator
0519  * either. Pages might be freed while their page blocks are marked ISOLATED.
0520  * A call to drain_all_pages() after isolation can flush most of them. However
0521  * in some cases pages might still end up on pcp lists and that would allow
0522  * for their allocation even when they are in fact isolated already. Depending
0523  * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
0524  * might be used to flush and disable pcplist before isolation and enable after
0525  * unisolation.
0526  *
0527  * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
0528  */
0529 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
0530                  int migratetype, int flags, gfp_t gfp_flags)
0531 {
0532     unsigned long pfn;
0533     struct page *page;
0534     /* isolation is done at page block granularity */
0535     unsigned long isolate_start = ALIGN_DOWN(start_pfn, pageblock_nr_pages);
0536     unsigned long isolate_end = ALIGN(end_pfn, pageblock_nr_pages);
0537     int ret;
0538     bool skip_isolation = false;
0539 
0540     /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
0541     ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false,
0542             skip_isolation, migratetype);
0543     if (ret)
0544         return ret;
0545 
0546     if (isolate_start == isolate_end - pageblock_nr_pages)
0547         skip_isolation = true;
0548 
0549     /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
0550     ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true,
0551             skip_isolation, migratetype);
0552     if (ret) {
0553         unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype);
0554         return ret;
0555     }
0556 
0557     /* skip isolated pageblocks at the beginning and end */
0558     for (pfn = isolate_start + pageblock_nr_pages;
0559          pfn < isolate_end - pageblock_nr_pages;
0560          pfn += pageblock_nr_pages) {
0561         page = __first_valid_page(pfn, pageblock_nr_pages);
0562         if (page && set_migratetype_isolate(page, migratetype, flags,
0563                     start_pfn, end_pfn)) {
0564             undo_isolate_page_range(isolate_start, pfn, migratetype);
0565             unset_migratetype_isolate(
0566                 pfn_to_page(isolate_end - pageblock_nr_pages),
0567                 migratetype);
0568             return -EBUSY;
0569         }
0570     }
0571     return 0;
0572 }
0573 
0574 /*
0575  * Make isolated pages available again.
0576  */
0577 void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
0578                 int migratetype)
0579 {
0580     unsigned long pfn;
0581     struct page *page;
0582     unsigned long isolate_start = ALIGN_DOWN(start_pfn, pageblock_nr_pages);
0583     unsigned long isolate_end = ALIGN(end_pfn, pageblock_nr_pages);
0584 
0585 
0586     for (pfn = isolate_start;
0587          pfn < isolate_end;
0588          pfn += pageblock_nr_pages) {
0589         page = __first_valid_page(pfn, pageblock_nr_pages);
0590         if (!page || !is_migrate_isolate_page(page))
0591             continue;
0592         unset_migratetype_isolate(page, migratetype);
0593     }
0594 }
0595 /*
0596  * Test all pages in the range is free(means isolated) or not.
0597  * all pages in [start_pfn...end_pfn) must be in the same zone.
0598  * zone->lock must be held before call this.
0599  *
0600  * Returns the last tested pfn.
0601  */
0602 static unsigned long
0603 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
0604                   int flags)
0605 {
0606     struct page *page;
0607 
0608     while (pfn < end_pfn) {
0609         page = pfn_to_page(pfn);
0610         if (PageBuddy(page))
0611             /*
0612              * If the page is on a free list, it has to be on
0613              * the correct MIGRATE_ISOLATE freelist. There is no
0614              * simple way to verify that as VM_BUG_ON(), though.
0615              */
0616             pfn += 1 << buddy_order(page);
0617         else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
0618             /* A HWPoisoned page cannot be also PageBuddy */
0619             pfn++;
0620         else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
0621              !page_count(page))
0622             /*
0623              * The responsible driver agreed to skip PageOffline()
0624              * pages when offlining memory by dropping its
0625              * reference in MEM_GOING_OFFLINE.
0626              */
0627             pfn++;
0628         else
0629             break;
0630     }
0631 
0632     return pfn;
0633 }
0634 
0635 /* Caller should ensure that requested range is in a single zone */
0636 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
0637             int isol_flags)
0638 {
0639     unsigned long pfn, flags;
0640     struct page *page;
0641     struct zone *zone;
0642     int ret;
0643 
0644     /*
0645      * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
0646      * are not aligned to pageblock_nr_pages.
0647      * Then we just check migratetype first.
0648      */
0649     for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
0650         page = __first_valid_page(pfn, pageblock_nr_pages);
0651         if (page && !is_migrate_isolate_page(page))
0652             break;
0653     }
0654     page = __first_valid_page(start_pfn, end_pfn - start_pfn);
0655     if ((pfn < end_pfn) || !page) {
0656         ret = -EBUSY;
0657         goto out;
0658     }
0659 
0660     /* Check all pages are free or marked as ISOLATED */
0661     zone = page_zone(page);
0662     spin_lock_irqsave(&zone->lock, flags);
0663     pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
0664     spin_unlock_irqrestore(&zone->lock, flags);
0665 
0666     ret = pfn < end_pfn ? -EBUSY : 0;
0667 
0668 out:
0669     trace_test_pages_isolated(start_pfn, end_pfn, pfn);
0670 
0671     return ret;
0672 }