Back to home page

LXR

 
 

    


0001 /*
0002  * linux/mm/page_isolation.c
0003  */
0004 
0005 #include <linux/mm.h>
0006 #include <linux/page-isolation.h>
0007 #include <linux/pageblock-flags.h>
0008 #include <linux/memory.h>
0009 #include <linux/hugetlb.h>
0010 #include <linux/page_owner.h>
0011 #include "internal.h"
0012 
0013 #define CREATE_TRACE_POINTS
0014 #include <trace/events/page_isolation.h>
0015 
0016 static int set_migratetype_isolate(struct page *page,
0017                 bool skip_hwpoisoned_pages)
0018 {
0019     struct zone *zone;
0020     unsigned long flags, pfn;
0021     struct memory_isolate_notify arg;
0022     int notifier_ret;
0023     int ret = -EBUSY;
0024 
0025     zone = page_zone(page);
0026 
0027     spin_lock_irqsave(&zone->lock, flags);
0028 
0029     pfn = page_to_pfn(page);
0030     arg.start_pfn = pfn;
0031     arg.nr_pages = pageblock_nr_pages;
0032     arg.pages_found = 0;
0033 
0034     /*
0035      * It may be possible to isolate a pageblock even if the
0036      * migratetype is not MIGRATE_MOVABLE. The memory isolation
0037      * notifier chain is used by balloon drivers to return the
0038      * number of pages in a range that are held by the balloon
0039      * driver to shrink memory. If all the pages are accounted for
0040      * by balloons, are free, or on the LRU, isolation can continue.
0041      * Later, for example, when memory hotplug notifier runs, these
0042      * pages reported as "can be isolated" should be isolated(freed)
0043      * by the balloon driver through the memory notifier chain.
0044      */
0045     notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
0046     notifier_ret = notifier_to_errno(notifier_ret);
0047     if (notifier_ret)
0048         goto out;
0049     /*
0050      * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
0051      * We just check MOVABLE pages.
0052      */
0053     if (!has_unmovable_pages(zone, page, arg.pages_found,
0054                  skip_hwpoisoned_pages))
0055         ret = 0;
0056 
0057     /*
0058      * immobile means "not-on-lru" pages. If immobile is larger than
0059      * removable-by-driver pages reported by notifier, we'll fail.
0060      */
0061 
0062 out:
0063     if (!ret) {
0064         unsigned long nr_pages;
0065         int migratetype = get_pageblock_migratetype(page);
0066 
0067         set_pageblock_migratetype(page, MIGRATE_ISOLATE);
0068         zone->nr_isolate_pageblock++;
0069         nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE);
0070 
0071         __mod_zone_freepage_state(zone, -nr_pages, migratetype);
0072     }
0073 
0074     spin_unlock_irqrestore(&zone->lock, flags);
0075     if (!ret)
0076         drain_all_pages(zone);
0077     return ret;
0078 }
0079 
0080 static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
0081 {
0082     struct zone *zone;
0083     unsigned long flags, nr_pages;
0084     bool isolated_page = false;
0085     unsigned int order;
0086     unsigned long page_idx, buddy_idx;
0087     struct page *buddy;
0088 
0089     zone = page_zone(page);
0090     spin_lock_irqsave(&zone->lock, flags);
0091     if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
0092         goto out;
0093 
0094     /*
0095      * Because freepage with more than pageblock_order on isolated
0096      * pageblock is restricted to merge due to freepage counting problem,
0097      * it is possible that there is free buddy page.
0098      * move_freepages_block() doesn't care of merge so we need other
0099      * approach in order to merge them. Isolation and free will make
0100      * these pages to be merged.
0101      */
0102     if (PageBuddy(page)) {
0103         order = page_order(page);
0104         if (order >= pageblock_order) {
0105             page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
0106             buddy_idx = __find_buddy_index(page_idx, order);
0107             buddy = page + (buddy_idx - page_idx);
0108 
0109             if (pfn_valid_within(page_to_pfn(buddy)) &&
0110                 !is_migrate_isolate_page(buddy)) {
0111                 __isolate_free_page(page, order);
0112                 isolated_page = true;
0113             }
0114         }
0115     }
0116 
0117     /*
0118      * If we isolate freepage with more than pageblock_order, there
0119      * should be no freepage in the range, so we could avoid costly
0120      * pageblock scanning for freepage moving.
0121      */
0122     if (!isolated_page) {
0123         nr_pages = move_freepages_block(zone, page, migratetype);
0124         __mod_zone_freepage_state(zone, nr_pages, migratetype);
0125     }
0126     set_pageblock_migratetype(page, migratetype);
0127     zone->nr_isolate_pageblock--;
0128 out:
0129     spin_unlock_irqrestore(&zone->lock, flags);
0130     if (isolated_page) {
0131         post_alloc_hook(page, order, __GFP_MOVABLE);
0132         __free_pages(page, order);
0133     }
0134 }
0135 
0136 static inline struct page *
0137 __first_valid_page(unsigned long pfn, unsigned long nr_pages)
0138 {
0139     int i;
0140     for (i = 0; i < nr_pages; i++)
0141         if (pfn_valid_within(pfn + i))
0142             break;
0143     if (unlikely(i == nr_pages))
0144         return NULL;
0145     return pfn_to_page(pfn + i);
0146 }
0147 
0148 /*
0149  * start_isolate_page_range() -- make page-allocation-type of range of pages
0150  * to be MIGRATE_ISOLATE.
0151  * @start_pfn: The lower PFN of the range to be isolated.
0152  * @end_pfn: The upper PFN of the range to be isolated.
0153  * @migratetype: migrate type to set in error recovery.
0154  *
0155  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
0156  * the range will never be allocated. Any free pages and pages freed in the
0157  * future will not be allocated again.
0158  *
0159  * start_pfn/end_pfn must be aligned to pageblock_order.
0160  * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
0161  */
0162 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
0163                  unsigned migratetype, bool skip_hwpoisoned_pages)
0164 {
0165     unsigned long pfn;
0166     unsigned long undo_pfn;
0167     struct page *page;
0168 
0169     BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
0170     BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
0171 
0172     for (pfn = start_pfn;
0173          pfn < end_pfn;
0174          pfn += pageblock_nr_pages) {
0175         page = __first_valid_page(pfn, pageblock_nr_pages);
0176         if (page &&
0177             set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
0178             undo_pfn = pfn;
0179             goto undo;
0180         }
0181     }
0182     return 0;
0183 undo:
0184     for (pfn = start_pfn;
0185          pfn < undo_pfn;
0186          pfn += pageblock_nr_pages)
0187         unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
0188 
0189     return -EBUSY;
0190 }
0191 
0192 /*
0193  * Make isolated pages available again.
0194  */
0195 int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
0196                 unsigned migratetype)
0197 {
0198     unsigned long pfn;
0199     struct page *page;
0200 
0201     BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
0202     BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
0203 
0204     for (pfn = start_pfn;
0205          pfn < end_pfn;
0206          pfn += pageblock_nr_pages) {
0207         page = __first_valid_page(pfn, pageblock_nr_pages);
0208         if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
0209             continue;
0210         unset_migratetype_isolate(page, migratetype);
0211     }
0212     return 0;
0213 }
0214 /*
0215  * Test all pages in the range is free(means isolated) or not.
0216  * all pages in [start_pfn...end_pfn) must be in the same zone.
0217  * zone->lock must be held before call this.
0218  *
0219  * Returns the last tested pfn.
0220  */
0221 static unsigned long
0222 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
0223                   bool skip_hwpoisoned_pages)
0224 {
0225     struct page *page;
0226 
0227     while (pfn < end_pfn) {
0228         if (!pfn_valid_within(pfn)) {
0229             pfn++;
0230             continue;
0231         }
0232         page = pfn_to_page(pfn);
0233         if (PageBuddy(page))
0234             /*
0235              * If the page is on a free list, it has to be on
0236              * the correct MIGRATE_ISOLATE freelist. There is no
0237              * simple way to verify that as VM_BUG_ON(), though.
0238              */
0239             pfn += 1 << page_order(page);
0240         else if (skip_hwpoisoned_pages && PageHWPoison(page))
0241             /* A HWPoisoned page cannot be also PageBuddy */
0242             pfn++;
0243         else
0244             break;
0245     }
0246 
0247     return pfn;
0248 }
0249 
0250 /* Caller should ensure that requested range is in a single zone */
0251 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
0252             bool skip_hwpoisoned_pages)
0253 {
0254     unsigned long pfn, flags;
0255     struct page *page;
0256     struct zone *zone;
0257 
0258     /*
0259      * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
0260      * are not aligned to pageblock_nr_pages.
0261      * Then we just check migratetype first.
0262      */
0263     for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
0264         page = __first_valid_page(pfn, pageblock_nr_pages);
0265         if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
0266             break;
0267     }
0268     page = __first_valid_page(start_pfn, end_pfn - start_pfn);
0269     if ((pfn < end_pfn) || !page)
0270         return -EBUSY;
0271     /* Check all pages are free or marked as ISOLATED */
0272     zone = page_zone(page);
0273     spin_lock_irqsave(&zone->lock, flags);
0274     pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
0275                         skip_hwpoisoned_pages);
0276     spin_unlock_irqrestore(&zone->lock, flags);
0277 
0278     trace_test_pages_isolated(start_pfn, end_pfn, pfn);
0279 
0280     return pfn < end_pfn ? -EBUSY : 0;
0281 }
0282 
0283 struct page *alloc_migrate_target(struct page *page, unsigned long private,
0284                   int **resultp)
0285 {
0286     gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
0287 
0288     /*
0289      * TODO: allocate a destination hugepage from a nearest neighbor node,
0290      * accordance with memory policy of the user process if possible. For
0291      * now as a simple work-around, we use the next node for destination.
0292      */
0293     if (PageHuge(page))
0294         return alloc_huge_page_node(page_hstate(compound_head(page)),
0295                         next_node_in(page_to_nid(page),
0296                              node_online_map));
0297 
0298     if (PageHighMem(page))
0299         gfp_mask |= __GFP_HIGHMEM;
0300 
0301     return alloc_page(gfp_mask);
0302 }