Choose pages from the per-cpu list based on migration type
[sfrench/cifs-2.6.git] / mm / page_alloc.c
index 6427653023aabfb501a2c34d3cc18f39b686c532..e3e726bd2858f6b97df2e92e4cf7f3ea9073fbd2 100644 (file)
 #include "internal.h"
 
 /*
- * MCD - HACK: Find somewhere to initialize this EARLY, or make this
- * initializer cleaner
+ * Array of node states.
  */
-nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
-EXPORT_SYMBOL(node_online_map);
-nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
-EXPORT_SYMBOL(node_possible_map);
+nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
+       [N_POSSIBLE] = NODE_MASK_ALL,
+       [N_ONLINE] = { { [0] = 1UL } },
+#ifndef CONFIG_NUMA
+       [N_NORMAL_MEMORY] = { { [0] = 1UL } },
+#ifdef CONFIG_HIGHMEM
+       [N_HIGH_MEMORY] = { { [0] = 1UL } },
+#endif
+       [N_CPU] = { { [0] = 1UL } },
+#endif /* NUMA */
+};
+EXPORT_SYMBOL(node_states);
+
 unsigned long totalram_pages __read_mostly;
 unsigned long totalreserve_pages __read_mostly;
 long nr_swap_pages;
@@ -150,6 +158,22 @@ int nr_node_ids __read_mostly = MAX_NUMNODES;
 EXPORT_SYMBOL(nr_node_ids);
 #endif
 
+static inline int get_pageblock_migratetype(struct page *page)
+{
+       return get_pageblock_flags_group(page, PB_migrate, PB_migrate_end);
+}
+
+static void set_pageblock_migratetype(struct page *page, int migratetype)
+{
+       set_pageblock_flags_group(page, (unsigned long)migratetype,
+                                       PB_migrate, PB_migrate_end);
+}
+
+static inline int gfpflags_to_migratetype(gfp_t gfp_flags)
+{
+       return ((gfp_flags & __GFP_MOVABLE) != 0);
+}
+
 #ifdef CONFIG_DEBUG_VM
 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
 {
@@ -404,6 +428,7 @@ static inline void __free_one_page(struct page *page,
 {
        unsigned long page_idx;
        int order_size = 1 << order;
+       int migratetype = get_pageblock_migratetype(page);
 
        if (unlikely(PageCompound(page)))
                destroy_compound_page(page, order);
@@ -416,7 +441,6 @@ static inline void __free_one_page(struct page *page,
        __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
        while (order < MAX_ORDER-1) {
                unsigned long combined_idx;
-               struct free_area *area;
                struct page *buddy;
 
                buddy = __page_find_buddy(page, page_idx, order);
@@ -424,8 +448,7 @@ static inline void __free_one_page(struct page *page,
                        break;          /* Move the buddy up one level. */
 
                list_del(&buddy->lru);
-               area = zone->free_area + order;
-               area->nr_free--;
+               zone->free_area[order].nr_free--;
                rmv_page_order(buddy);
                combined_idx = __find_combined_index(page_idx, order);
                page = page + (combined_idx - page_idx);
@@ -433,7 +456,8 @@ static inline void __free_one_page(struct page *page,
                order++;
        }
        set_page_order(page, order);
-       list_add(&page->lru, &zone->free_area[order].free_list);
+       list_add(&page->lru,
+               &zone->free_area[order].free_list[migratetype]);
        zone->free_area[order].nr_free++;
 }
 
@@ -567,7 +591,8 @@ void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  * -- wli
  */
 static inline void expand(struct zone *zone, struct page *page,
-       int low, int high, struct free_area *area)
+       int low, int high, struct free_area *area,
+       int migratetype)
 {
        unsigned long size = 1 << high;
 
@@ -576,7 +601,7 @@ static inline void expand(struct zone *zone, struct page *page,
                high--;
                size >>= 1;
                VM_BUG_ON(bad_range(zone, &page[size]));
-               list_add(&page[size].lru, &area->free_list);
+               list_add(&page[size].lru, &area->free_list[migratetype]);
                area->nr_free++;
                set_page_order(&page[size], high);
        }
@@ -628,31 +653,95 @@ static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
        return 0;
 }
 
+/*
+ * This array describes the order lists are fallen back to when
+ * the free lists for the desirable migrate type are depleted
+ */
+static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
+       [MIGRATE_UNMOVABLE] = { MIGRATE_MOVABLE   },
+       [MIGRATE_MOVABLE]   = { MIGRATE_UNMOVABLE },
+};
+
+/* Remove an element from the buddy allocator from the fallback list */
+static struct page *__rmqueue_fallback(struct zone *zone, int order,
+                                               int start_migratetype)
+{
+       struct free_area * area;
+       int current_order;
+       struct page *page;
+       int migratetype, i;
+
+       /* Find the largest possible block of pages in the other list */
+       for (current_order = MAX_ORDER-1; current_order >= order;
+                                               --current_order) {
+               for (i = 0; i < MIGRATE_TYPES - 1; i++) {
+                       migratetype = fallbacks[start_migratetype][i];
+
+                       area = &(zone->free_area[current_order]);
+                       if (list_empty(&area->free_list[migratetype]))
+                               continue;
+
+                       page = list_entry(area->free_list[migratetype].next,
+                                       struct page, lru);
+                       area->nr_free--;
+
+                       /*
+                        * If breaking a large block of pages, place the buddies
+                        * on the preferred allocation list
+                        */
+                       if (unlikely(current_order >= MAX_ORDER / 2))
+                               migratetype = start_migratetype;
+
+                       /* Remove the page from the freelists */
+                       list_del(&page->lru);
+                       rmv_page_order(page);
+                       __mod_zone_page_state(zone, NR_FREE_PAGES,
+                                                       -(1UL << order));
+
+                       if (current_order == MAX_ORDER - 1)
+                               set_pageblock_migratetype(page,
+                                                       start_migratetype);
+
+                       expand(zone, page, order, current_order, area, migratetype);
+                       return page;
+               }
+       }
+
+       return NULL;
+}
+
 /* 
  * Do the hard work of removing an element from the buddy allocator.
  * Call me with the zone->lock already held.
  */
-static struct page *__rmqueue(struct zone *zone, unsigned int order)
+static struct page *__rmqueue(struct zone *zone, unsigned int order,
+                                               int migratetype)
 {
        struct free_area * area;
        unsigned int current_order;
        struct page *page;
 
+       /* Find a page of the appropriate size in the preferred list */
        for (current_order = order; current_order < MAX_ORDER; ++current_order) {
-               area = zone->free_area + current_order;
-               if (list_empty(&area->free_list))
+               area = &(zone->free_area[current_order]);
+               if (list_empty(&area->free_list[migratetype]))
                        continue;
 
-               page = list_entry(area->free_list.next, struct page, lru);
+               page = list_entry(area->free_list[migratetype].next,
+                                                       struct page, lru);
                list_del(&page->lru);
                rmv_page_order(page);
                area->nr_free--;
                __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
-               expand(zone, page, order, current_order, area);
-               return page;
+               expand(zone, page, order, current_order, area, migratetype);
+               goto got_page;
        }
 
-       return NULL;
+       page = __rmqueue_fallback(zone, order, migratetype);
+
+got_page:
+
+       return page;
 }
 
 /* 
@@ -661,16 +750,18 @@ static struct page *__rmqueue(struct zone *zone, unsigned int order)
  * Returns the number of new pages which were placed at *list.
  */
 static int rmqueue_bulk(struct zone *zone, unsigned int order, 
-                       unsigned long count, struct list_head *list)
+                       unsigned long count, struct list_head *list,
+                       int migratetype)
 {
        int i;
        
        spin_lock(&zone->lock);
        for (i = 0; i < count; ++i) {
-               struct page *page = __rmqueue(zone, order);
+               struct page *page = __rmqueue(zone, order, migratetype);
                if (unlikely(page == NULL))
                        break;
-               list_add_tail(&page->lru, list);
+               list_add(&page->lru, list);
+               set_page_private(page, migratetype);
        }
        spin_unlock(&zone->lock);
        return i;
@@ -732,7 +823,7 @@ void mark_free_pages(struct zone *zone)
 {
        unsigned long pfn, max_zone_pfn;
        unsigned long flags;
-       int order;
+       int order, t;
        struct list_head *curr;
 
        if (!zone->spanned_pages)
@@ -749,15 +840,15 @@ void mark_free_pages(struct zone *zone)
                                swsusp_unset_page_free(page);
                }
 
-       for (order = MAX_ORDER - 1; order >= 0; --order)
-               list_for_each(curr, &zone->free_area[order].free_list) {
+       for_each_migratetype_order(order, t) {
+               list_for_each(curr, &zone->free_area[order].free_list[t]) {
                        unsigned long i;
 
                        pfn = page_to_pfn(list_entry(curr, struct page, lru));
                        for (i = 0; i < (1UL << order); i++)
                                swsusp_set_page_free(pfn_to_page(pfn + i));
                }
-
+       }
        spin_unlock_irqrestore(&zone->lock, flags);
 }
 
@@ -797,6 +888,7 @@ static void fastcall free_hot_cold_page(struct page *page, int cold)
        local_irq_save(flags);
        __count_vm_event(PGFREE);
        list_add(&page->lru, &pcp->list);
+       set_page_private(page, get_pageblock_migratetype(page));
        pcp->count++;
        if (pcp->count >= pcp->high) {
                free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
@@ -846,6 +938,7 @@ static struct page *buffered_rmqueue(struct zonelist *zonelist,
        struct page *page;
        int cold = !!(gfp_flags & __GFP_COLD);
        int cpu;
+       int migratetype = gfpflags_to_migratetype(gfp_flags);
 
 again:
        cpu  = get_cpu();
@@ -856,16 +949,34 @@ again:
                local_irq_save(flags);
                if (!pcp->count) {
                        pcp->count = rmqueue_bulk(zone, 0,
-                                               pcp->batch, &pcp->list);
+                                       pcp->batch, &pcp->list, migratetype);
                        if (unlikely(!pcp->count))
                                goto failed;
                }
-               page = list_entry(pcp->list.next, struct page, lru);
-               list_del(&page->lru);
-               pcp->count--;
+               /* Find a page of the appropriate migrate type */
+               list_for_each_entry(page, &pcp->list, lru) {
+                       if (page_private(page) == migratetype) {
+                               list_del(&page->lru);
+                               pcp->count--;
+                               break;
+                       }
+               }
+
+               /*
+                * Check if a page of the appropriate migrate type
+                * was found. If not, allocate more to the pcp list
+                */
+               if (&page->lru == &pcp->list) {
+                       pcp->count += rmqueue_bulk(zone, 0,
+                                       pcp->batch, &pcp->list, migratetype);
+                       page = list_entry(pcp->list.next, struct page, lru);
+                       VM_BUG_ON(page_private(page) != migratetype);
+                       list_del(&page->lru);
+                       pcp->count--;
+               }
        } else {
                spin_lock_irqsave(&zone->lock, flags);
-               page = __rmqueue(zone, order);
+               page = __rmqueue(zone, order, migratetype);
                spin_unlock(&zone->lock);
                if (!page)
                        goto failed;
@@ -1032,7 +1143,7 @@ int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  *
  * If the zonelist cache is present in the passed in zonelist, then
  * returns a pointer to the allowed node mask (either the current
- * tasks mems_allowed, or node_online_map.)
+ * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
  *
  * If the zonelist cache is not available for this zonelist, does
  * nothing and returns NULL.
@@ -1061,7 +1172,7 @@ static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
 
        allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
                                        &cpuset_current_mems_allowed :
-                                       &node_online_map;
+                                       &node_states[N_HIGH_MEMORY];
        return allowednodes;
 }
 
@@ -1183,9 +1294,6 @@ zonelist_scan:
                        !zlc_zone_worth_trying(zonelist, z, allowednodes))
                                continue;
                zone = *z;
-               if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
-                       zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
-                               break;
                if ((alloc_flags & ALLOC_CPUSET) &&
                        !cpuset_zone_allowed_softwall(zone, gfp_mask))
                                goto try_next_zone;
@@ -1254,7 +1362,10 @@ restart:
        z = zonelist->zones;  /* the list of zones suitable for gfp_mask */
 
        if (unlikely(*z == NULL)) {
-               /* Should this ever happen?? */
+               /*
+                * Happens if we have an empty zonelist as a result of
+                * GFP_THISNODE being used on a memoryless node
+                */
                return NULL;
        }
 
@@ -1794,7 +1905,7 @@ static int find_next_best_node(int node, nodemask_t *used_node_mask)
                return node;
        }
 
-       for_each_online_node(n) {
+       for_each_node_state(n, N_HIGH_MEMORY) {
                cpumask_t tmp;
 
                /* Don't want a node to appear more than once */
@@ -1849,6 +1960,22 @@ static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
        }
 }
 
+/*
+ * Build gfp_thisnode zonelists
+ */
+static void build_thisnode_zonelists(pg_data_t *pgdat)
+{
+       enum zone_type i;
+       int j;
+       struct zonelist *zonelist;
+
+       for (i = 0; i < MAX_NR_ZONES; i++) {
+               zonelist = pgdat->node_zonelists + MAX_NR_ZONES + i;
+               j = build_zonelists_node(pgdat, zonelist, 0, i);
+               zonelist->zones[j] = NULL;
+       }
+}
+
 /*
  * Build zonelists ordered by zone and nodes within zones.
  * This results in conserving DMA zone[s] until all Normal memory is
@@ -1915,7 +2042,8 @@ static int default_zonelist_order(void)
         * If there is a node whose DMA/DMA32 memory is very big area on
         * local memory, NODE_ORDER may be suitable.
          */
-       average_size = total_size / (num_online_nodes() + 1);
+       average_size = total_size /
+                               (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
        for_each_online_node(nid) {
                low_kmem_size = 0;
                total_size = 0;
@@ -1953,7 +2081,7 @@ static void build_zonelists(pg_data_t *pgdat)
        int order = current_zonelist_order;
 
        /* initialize zonelists */
-       for (i = 0; i < MAX_NR_ZONES; i++) {
+       for (i = 0; i < MAX_ZONELISTS; i++) {
                zonelist = pgdat->node_zonelists + i;
                zonelist->zones[0] = NULL;
        }
@@ -1998,6 +2126,8 @@ static void build_zonelists(pg_data_t *pgdat)
                /* calculate node order -- i.e., DMA last! */
                build_zonelists_in_zone_order(pgdat, j);
        }
+
+       build_thisnode_zonelists(pgdat);
 }
 
 /* Construct the zonelist performance cache - see further mmzone.h */
@@ -2078,8 +2208,10 @@ static int __build_all_zonelists(void *dummy)
        int nid;
 
        for_each_online_node(nid) {
-               build_zonelists(NODE_DATA(nid));
-               build_zonelist_cache(NODE_DATA(nid));
+               pg_data_t *pgdat = NODE_DATA(nid);
+
+               build_zonelists(pgdat);
+               build_zonelist_cache(pgdat);
        }
        return 0;
 }
@@ -2204,6 +2336,16 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
                init_page_count(page);
                reset_page_mapcount(page);
                SetPageReserved(page);
+
+               /*
+                * Mark the block movable so that blocks are reserved for
+                * movable at startup. This will force kernel allocations
+                * to reserve their blocks rather than leaking throughout
+                * the address space during boot when many long-lived
+                * kernel allocations are made
+                */
+               set_pageblock_migratetype(page, MIGRATE_MOVABLE);
+
                INIT_LIST_HEAD(&page->lru);
 #ifdef WANT_PAGE_VIRTUAL
                /* The shift won't overflow because ZONE_NORMAL is below 4G. */
@@ -2216,9 +2358,9 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
                                struct zone *zone, unsigned long size)
 {
-       int order;
-       for (order = 0; order < MAX_ORDER ; order++) {
-               INIT_LIST_HEAD(&zone->free_area[order].free_list);
+       int order, t;
+       for_each_migratetype_order(order, t) {
+               INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
                zone->free_area[order].nr_free = 0;
        }
 }
@@ -2324,6 +2466,9 @@ static struct per_cpu_pageset boot_pageset[NR_CPUS];
 static int __cpuinit process_zones(int cpu)
 {
        struct zone *zone, *dzone;
+       int node = cpu_to_node(cpu);
+
+       node_set_state(node, N_CPU);    /* this node has a cpu */
 
        for_each_zone(zone) {
 
@@ -2331,7 +2476,7 @@ static int __cpuinit process_zones(int cpu)
                        continue;
 
                zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
-                                        GFP_KERNEL, cpu_to_node(cpu));
+                                        GFP_KERNEL, node);
                if (!zone_pcp(zone, cpu))
                        goto bad;
 
@@ -2345,6 +2490,8 @@ static int __cpuinit process_zones(int cpu)
        return 0;
 bad:
        for_each_zone(dzone) {
+               if (!populated_zone(dzone))
+                       continue;
                if (dzone == zone)
                        break;
                kfree(zone_pcp(dzone, cpu));
@@ -2442,7 +2589,7 @@ int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
                 * To use this new node's memory, further consideration will be
                 * necessary.
                 */
-               zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
+               zone->wait_table = vmalloc(alloc_size);
        }
        if (!zone->wait_table)
                return -ENOMEM;
@@ -2678,10 +2825,8 @@ void __meminit get_pfn_range_for_nid(unsigned int nid,
                *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
        }
 
-       if (*start_pfn == -1UL) {
-               printk(KERN_WARNING "Node %u active with no memory\n", nid);
+       if (*start_pfn == -1UL)
                *start_pfn = 0;
-       }
 
        /* Push the node boundaries out if requested */
        account_node_boundary(nid, start_pfn, end_pfn);
@@ -2899,6 +3044,41 @@ static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
                                                        realtotalpages);
 }
 
+#ifndef CONFIG_SPARSEMEM
+/*
+ * Calculate the size of the zone->blockflags rounded to an unsigned long
+ * Start by making sure zonesize is a multiple of MAX_ORDER-1 by rounding up
+ * Then figure 1 NR_PAGEBLOCK_BITS worth of bits per MAX_ORDER-1, finally
+ * round what is now in bits to nearest long in bits, then return it in
+ * bytes.
+ */
+static unsigned long __init usemap_size(unsigned long zonesize)
+{
+       unsigned long usemapsize;
+
+       usemapsize = roundup(zonesize, MAX_ORDER_NR_PAGES);
+       usemapsize = usemapsize >> (MAX_ORDER-1);
+       usemapsize *= NR_PAGEBLOCK_BITS;
+       usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
+
+       return usemapsize / 8;
+}
+
+static void __init setup_usemap(struct pglist_data *pgdat,
+                               struct zone *zone, unsigned long zonesize)
+{
+       unsigned long usemapsize = usemap_size(zonesize);
+       zone->pageblock_flags = NULL;
+       if (usemapsize) {
+               zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
+               memset(zone->pageblock_flags, 0, usemapsize);
+       }
+}
+#else
+static void inline setup_usemap(struct pglist_data *pgdat,
+                               struct zone *zone, unsigned long zonesize) {}
+#endif /* CONFIG_SPARSEMEM */
+
 /*
  * Set up the zone data structures:
  *   - mark all pages reserved
@@ -2979,6 +3159,7 @@ static void __meminit free_area_init_core(struct pglist_data *pgdat,
                if (!size)
                        continue;
 
+               setup_usemap(pgdat, zone, size);
                ret = init_currently_empty_zone(zone, zone_start_pfn,
                                                size, MEMMAP_EARLY);
                BUG_ON(ret);
@@ -3232,16 +3413,24 @@ unsigned long __init find_max_pfn_with_active_regions(void)
        return max_pfn;
 }
 
+/*
+ * early_calculate_totalpages()
+ * Sum pages in active regions for movable zone.
+ * Populate N_HIGH_MEMORY for calculating usable_nodes.
+ */
 unsigned long __init early_calculate_totalpages(void)
 {
        int i;
        unsigned long totalpages = 0;
 
-       for (i = 0; i < nr_nodemap_entries; i++)
-               totalpages += early_node_map[i].end_pfn -
+       for (i = 0; i < nr_nodemap_entries; i++) {
+               unsigned long pages = early_node_map[i].end_pfn -
                                                early_node_map[i].start_pfn;
-
-       return totalpages;
+               totalpages += pages;
+               if (pages)
+                       node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
+       }
+       return totalpages;
 }
 
 /*
@@ -3255,7 +3444,8 @@ void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
        int i, nid;
        unsigned long usable_startpfn;
        unsigned long kernelcore_node, kernelcore_remaining;
-       int usable_nodes = num_online_nodes();
+       unsigned long totalpages = early_calculate_totalpages();
+       int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
 
        /*
         * If movablecore was specified, calculate what size of
@@ -3266,7 +3456,6 @@ void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
         * what movablecore would have allowed.
         */
        if (required_movablecore) {
-               unsigned long totalpages = early_calculate_totalpages();
                unsigned long corepages;
 
                /*
@@ -3291,7 +3480,7 @@ void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
 restart:
        /* Spread kernelcore memory as evenly as possible throughout nodes */
        kernelcore_node = required_kernelcore / usable_nodes;
-       for_each_online_node(nid) {
+       for_each_node_state(nid, N_HIGH_MEMORY) {
                /*
                 * Recalculate kernelcore_node if the division per node
                 * now exceeds what is necessary to satisfy the requested
@@ -3383,6 +3572,20 @@ restart:
                        roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
 }
 
+/* Any regular memory on that node ? */
+static void check_for_regular_memory(pg_data_t *pgdat)
+{
+#ifdef CONFIG_HIGHMEM
+       enum zone_type zone_type;
+
+       for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
+               struct zone *zone = &pgdat->node_zones[zone_type];
+               if (zone->present_pages)
+                       node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
+       }
+#endif
+}
+
 /**
  * free_area_init_nodes - Initialise all pg_data_t and zone data
  * @max_zone_pfn: an array of max PFNs for each zone
@@ -3457,6 +3660,11 @@ void __init free_area_init_nodes(unsigned long *max_zone_pfn)
                pg_data_t *pgdat = NODE_DATA(nid);
                free_area_init_node(nid, pgdat, NULL,
                                find_min_pfn_for_node(nid), NULL);
+
+               /* Any memory on that node */
+               if (pgdat->node_present_pages)
+                       node_set_state(nid, N_HIGH_MEMORY);
+               check_for_regular_memory(pgdat);
        }
 }
 
@@ -3932,4 +4140,79 @@ EXPORT_SYMBOL(pfn_to_page);
 EXPORT_SYMBOL(page_to_pfn);
 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
 
+/* Return a pointer to the bitmap storing bits affecting a block of pages */
+static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
+                                                       unsigned long pfn)
+{
+#ifdef CONFIG_SPARSEMEM
+       return __pfn_to_section(pfn)->pageblock_flags;
+#else
+       return zone->pageblock_flags;
+#endif /* CONFIG_SPARSEMEM */
+}
+
+static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
+{
+#ifdef CONFIG_SPARSEMEM
+       pfn &= (PAGES_PER_SECTION-1);
+       return (pfn >> (MAX_ORDER-1)) * NR_PAGEBLOCK_BITS;
+#else
+       pfn = pfn - zone->zone_start_pfn;
+       return (pfn >> (MAX_ORDER-1)) * NR_PAGEBLOCK_BITS;
+#endif /* CONFIG_SPARSEMEM */
+}
 
+/**
+ * get_pageblock_flags_group - Return the requested group of flags for the MAX_ORDER_NR_PAGES block of pages
+ * @page: The page within the block of interest
+ * @start_bitidx: The first bit of interest to retrieve
+ * @end_bitidx: The last bit of interest
+ * returns pageblock_bits flags
+ */
+unsigned long get_pageblock_flags_group(struct page *page,
+                                       int start_bitidx, int end_bitidx)
+{
+       struct zone *zone;
+       unsigned long *bitmap;
+       unsigned long pfn, bitidx;
+       unsigned long flags = 0;
+       unsigned long value = 1;
+
+       zone = page_zone(page);
+       pfn = page_to_pfn(page);
+       bitmap = get_pageblock_bitmap(zone, pfn);
+       bitidx = pfn_to_bitidx(zone, pfn);
+
+       for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
+               if (test_bit(bitidx + start_bitidx, bitmap))
+                       flags |= value;
+
+       return flags;
+}
+
+/**
+ * set_pageblock_flags_group - Set the requested group of flags for a MAX_ORDER_NR_PAGES block of pages
+ * @page: The page within the block of interest
+ * @start_bitidx: The first bit of interest
+ * @end_bitidx: The last bit of interest
+ * @flags: The flags to set
+ */
+void set_pageblock_flags_group(struct page *page, unsigned long flags,
+                                       int start_bitidx, int end_bitidx)
+{
+       struct zone *zone;
+       unsigned long *bitmap;
+       unsigned long pfn, bitidx;
+       unsigned long value = 1;
+
+       zone = page_zone(page);
+       pfn = page_to_pfn(page);
+       bitmap = get_pageblock_bitmap(zone, pfn);
+       bitidx = pfn_to_bitidx(zone, pfn);
+
+       for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
+               if (flags & value)
+                       __set_bit(bitidx + start_bitidx, bitmap);
+               else
+                       __clear_bit(bitidx + start_bitidx, bitmap);
+}