mm, page_alloc: un-inline the bad part of free_pages_check
[cascardo/linux.git] / mm / page_alloc.c
index c1069ef..d51543d 100644 (file)
@@ -784,17 +784,42 @@ out:
        zone->free_area[order].nr_free++;
 }
 
-static inline int free_pages_check(struct page *page)
+/*
+ * A bad page could be due to a number of fields. Instead of multiple branches,
+ * try and check multiple fields with one check. The caller must do a detailed
+ * check if necessary.
+ */
+static inline bool page_expected_state(struct page *page,
+                                       unsigned long check_flags)
 {
-       const char *bad_reason = NULL;
-       unsigned long bad_flags = 0;
+       if (unlikely(atomic_read(&page->_mapcount) != -1))
+               return false;
+
+       if (unlikely((unsigned long)page->mapping |
+                       page_ref_count(page) |
+#ifdef CONFIG_MEMCG
+                       (unsigned long)page->mem_cgroup |
+#endif
+                       (page->flags & check_flags)))
+               return false;
+
+       return true;
+}
+
+static void free_pages_check_bad(struct page *page)
+{
+       const char *bad_reason;
+       unsigned long bad_flags;
+
+       bad_reason = NULL;
+       bad_flags = 0;
 
        if (unlikely(atomic_read(&page->_mapcount) != -1))
                bad_reason = "nonzero mapcount";
        if (unlikely(page->mapping != NULL))
                bad_reason = "non-NULL mapping";
        if (unlikely(page_ref_count(page) != 0))
-               bad_reason = "nonzero _count";
+               bad_reason = "nonzero _refcount";
        if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
                bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
                bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
@@ -803,14 +828,20 @@ static inline int free_pages_check(struct page *page)
        if (unlikely(page->mem_cgroup))
                bad_reason = "page still charged to cgroup";
 #endif
-       if (unlikely(bad_reason)) {
-               bad_page(page, bad_reason, bad_flags);
-               return 1;
-       }
-       page_cpupid_reset_last(page);
-       if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
+       bad_page(page, bad_reason, bad_flags);
+}
+
+static inline int free_pages_check(struct page *page)
+{
+       if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE))) {
+               page_cpupid_reset_last(page);
                page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
-       return 0;
+               return 0;
+       }
+
+       /* Something has gone sideways, find it */
+       free_pages_check_bad(page);
+       return 1;
 }
 
 /*
@@ -831,8 +862,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,
        int batch_free = 0;
        int to_free = count;
        unsigned long nr_scanned;
+       bool isolated_pageblocks;
 
        spin_lock(&zone->lock);
+       isolated_pageblocks = has_isolate_pageblock(zone);
        nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
        if (nr_scanned)
                __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
@@ -870,7 +903,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
                        /* MIGRATE_ISOLATE page should not go to pcplists */
                        VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
                        /* Pageblock could have been isolated meanwhile */
-                       if (unlikely(has_isolate_pageblock(zone)))
+                       if (unlikely(isolated_pageblocks))
                                mt = get_pageblock_migratetype(page);
 
                        __free_one_page(page, page_to_pfn(page), zone, 0, mt);
@@ -1024,24 +1057,33 @@ void __meminit reserve_bootmem_region(unsigned long start, unsigned long end)
 
 static bool free_pages_prepare(struct page *page, unsigned int order)
 {
-       bool compound = PageCompound(page);
-       int i, bad = 0;
+       int bad = 0;
 
        VM_BUG_ON_PAGE(PageTail(page), page);
-       VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
 
        trace_mm_page_free(page, order);
        kmemcheck_free_shadow(page, order);
        kasan_free_pages(page, order);
 
-       if (PageAnon(page))
+       /*
+        * Check tail pages before head page information is cleared to
+        * avoid checking PageCompound for order-0 pages.
+        */
+       if (unlikely(order)) {
+               bool compound = PageCompound(page);
+               int i;
+
+               VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
+
+               for (i = 1; i < (1 << order); i++) {
+                       if (compound)
+                               bad += free_tail_pages_check(page, page + i);
+                       bad += free_pages_check(page + i);
+               }
+       }
+       if (PageAnonHead(page))
                page->mapping = NULL;
        bad += free_pages_check(page);
-       for (i = 1; i < (1 << order); i++) {
-               if (compound)
-                       bad += free_tail_pages_check(page, page + i);
-               bad += free_pages_check(page + i);
-       }
        if (bad)
                return false;
 
@@ -1076,8 +1118,7 @@ static void __free_pages_ok(struct page *page, unsigned int order)
        local_irq_restore(flags);
 }
 
-static void __init __free_pages_boot_core(struct page *page,
-                                       unsigned long pfn, unsigned int order)
+static void __init __free_pages_boot_core(struct page *page, unsigned int order)
 {
        unsigned int nr_pages = 1 << order;
        struct page *p = page;
@@ -1154,7 +1195,7 @@ void __init __free_pages_bootmem(struct page *page, unsigned long pfn,
 {
        if (early_page_uninitialised(pfn))
                return;
-       return __free_pages_boot_core(page, pfn, order);
+       return __free_pages_boot_core(page, order);
 }
 
 /*
@@ -1239,12 +1280,12 @@ static void __init deferred_free_range(struct page *page,
        if (nr_pages == MAX_ORDER_NR_PAGES &&
            (pfn & (MAX_ORDER_NR_PAGES-1)) == 0) {
                set_pageblock_migratetype(page, MIGRATE_MOVABLE);
-               __free_pages_boot_core(page, pfn, MAX_ORDER-1);
+               __free_pages_boot_core(page, MAX_ORDER-1);
                return;
        }
 
-       for (i = 0; i < nr_pages; i++, page++, pfn++)
-               __free_pages_boot_core(page, pfn, 0);
+       for (i = 0; i < nr_pages; i++, page++)
+               __free_pages_boot_core(page, 0);
 }
 
 /* Completion tracking for deferred_init_memmap() threads */
@@ -1482,9 +1523,14 @@ static inline void expand(struct zone *zone, struct page *page,
  */
 static inline int check_new_page(struct page *page)
 {
-       const char *bad_reason = NULL;
-       unsigned long bad_flags = 0;
+       const char *bad_reason;
+       unsigned long bad_flags;
 
+       if (page_expected_state(page, PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON))
+               return 0;
+
+       bad_reason = NULL;
+       bad_flags = 0;
        if (unlikely(atomic_read(&page->_mapcount) != -1))
                bad_reason = "nonzero mapcount";
        if (unlikely(page->mapping != NULL))
@@ -1517,7 +1563,7 @@ static inline bool free_pages_prezeroed(bool poisoned)
 }
 
 static int prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
-                                                               int alloc_flags)
+                                                       unsigned int alloc_flags)
 {
        int i;
        bool poisoned = true;
@@ -2157,6 +2203,10 @@ void mark_free_pages(struct zone *zone)
        for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
                if (pfn_valid(pfn)) {
                        page = pfn_to_page(pfn);
+
+                       if (page_zone(page) != zone)
+                               continue;
+
                        if (!swsusp_page_is_forbidden(page))
                                swsusp_unset_page_free(page);
                }
@@ -2342,13 +2392,45 @@ int split_free_page(struct page *page)
        return nr_pages;
 }
 
+/*
+ * Update NUMA hit/miss statistics
+ *
+ * Must be called with interrupts disabled.
+ *
+ * When __GFP_OTHER_NODE is set assume the node of the preferred
+ * zone is the local node. This is useful for daemons who allocate
+ * memory on behalf of other processes.
+ */
+static inline void zone_statistics(struct zone *preferred_zone, struct zone *z,
+                                                               gfp_t flags)
+{
+#ifdef CONFIG_NUMA
+       int local_nid = numa_node_id();
+       enum zone_stat_item local_stat = NUMA_LOCAL;
+
+       if (unlikely(flags & __GFP_OTHER_NODE)) {
+               local_stat = NUMA_OTHER;
+               local_nid = preferred_zone->node;
+       }
+
+       if (z->node == local_nid) {
+               __inc_zone_state(z, NUMA_HIT);
+               __inc_zone_state(z, local_stat);
+       } else {
+               __inc_zone_state(z, NUMA_MISS);
+               __inc_zone_state(preferred_zone, NUMA_FOREIGN);
+       }
+#endif
+}
+
 /*
  * Allocate a page from the given zone. Use pcplists for order-0 allocations.
  */
 static inline
 struct page *buffered_rmqueue(struct zone *preferred_zone,
                        struct zone *zone, unsigned int order,
-                       gfp_t gfp_flags, int alloc_flags, int migratetype)
+                       gfp_t gfp_flags, unsigned int alloc_flags,
+                       int migratetype)
 {
        unsigned long flags;
        struct page *page;
@@ -2374,6 +2456,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
                else
                        page = list_first_entry(list, struct page, lru);
 
+               __dec_zone_state(zone, NR_ALLOC_BATCH);
                list_del(&page->lru);
                pcp->count--;
        } else {
@@ -2395,11 +2478,11 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
                spin_unlock(&zone->lock);
                if (!page)
                        goto failed;
+               __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
                __mod_zone_freepage_state(zone, -(1 << order),
                                          get_pcppage_migratetype(page));
        }
 
-       __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
        if (atomic_long_read(&zone->vm_stat[NR_ALLOC_BATCH]) <= 0 &&
            !test_bit(ZONE_FAIR_DEPLETED, &zone->flags))
                set_bit(ZONE_FAIR_DEPLETED, &zone->flags);
@@ -2501,12 +2584,13 @@ static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  * to check in the allocation paths if no pages are free.
  */
 static bool __zone_watermark_ok(struct zone *z, unsigned int order,
-                       unsigned long mark, int classzone_idx, int alloc_flags,
+                       unsigned long mark, int classzone_idx,
+                       unsigned int alloc_flags,
                        long free_pages)
 {
        long min = mark;
        int o;
-       const int alloc_harder = (alloc_flags & ALLOC_HARDER);
+       const bool alloc_harder = (alloc_flags & ALLOC_HARDER);
 
        /* free_pages may go negative - that's OK */
        free_pages -= (1 << order) - 1;
@@ -2569,12 +2653,38 @@ static bool __zone_watermark_ok(struct zone *z, unsigned int order,
 }
 
 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
-                     int classzone_idx, int alloc_flags)
+                     int classzone_idx, unsigned int alloc_flags)
 {
        return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
                                        zone_page_state(z, NR_FREE_PAGES));
 }
 
+static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
+               unsigned long mark, int classzone_idx, unsigned int alloc_flags)
+{
+       long free_pages = zone_page_state(z, NR_FREE_PAGES);
+       long cma_pages = 0;
+
+#ifdef CONFIG_CMA
+       /* If allocation can't use CMA areas don't use free CMA pages */
+       if (!(alloc_flags & ALLOC_CMA))
+               cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
+#endif
+
+       /*
+        * Fast check for order-0 only. If this fails then the reserves
+        * need to be calculated. There is a corner case where the check
+        * passes but only the high-order atomic reserve are free. If
+        * the caller is !atomic then it'll uselessly search the free
+        * list. That corner case is then slower but it is harmless.
+        */
+       if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
+               return true;
+
+       return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
+                                       free_pages);
+}
+
 bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
                        unsigned long mark, int classzone_idx)
 {
@@ -2630,22 +2740,19 @@ static struct page *
 get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
                                                const struct alloc_context *ac)
 {
-       struct zonelist *zonelist = ac->zonelist;
-       struct zoneref *z;
-       struct page *page = NULL;
+       struct zoneref *z = ac->preferred_zoneref;
        struct zone *zone;
-       int nr_fair_skipped = 0;
-       bool zonelist_rescan;
+       bool fair_skipped = false;
+       bool apply_fair = (alloc_flags & ALLOC_FAIR);
 
 zonelist_scan:
-       zonelist_rescan = false;
-
        /*
         * Scan zonelist, looking for a zone with enough free.
         * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
         */
-       for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
+       for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
                                                                ac->nodemask) {
+               struct page *page;
                unsigned long mark;
 
                if (cpusets_enabled() &&
@@ -2658,13 +2765,16 @@ zonelist_scan:
                 * page was allocated in should have no effect on the
                 * time the page has in memory before being reclaimed.
                 */
-               if (alloc_flags & ALLOC_FAIR) {
-                       if (!zone_local(ac->preferred_zone, zone))
-                               break;
+               if (apply_fair) {
                        if (test_bit(ZONE_FAIR_DEPLETED, &zone->flags)) {
-                               nr_fair_skipped++;
+                               fair_skipped = true;
                                continue;
                        }
+                       if (!zone_local(ac->preferred_zoneref->zone, zone)) {
+                               if (fair_skipped)
+                                       goto reset_fair;
+                               apply_fair = false;
+                       }
                }
                /*
                 * When allocating a page cache page for writing, we
@@ -2696,8 +2806,8 @@ zonelist_scan:
                        continue;
 
                mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
-               if (!zone_watermark_ok(zone, order, mark,
-                                      ac->classzone_idx, alloc_flags)) {
+               if (!zone_watermark_fast(zone, order, mark,
+                                      ac_classzone_idx(ac), alloc_flags)) {
                        int ret;
 
                        /* Checked here to keep the fast path fast */
@@ -2706,7 +2816,7 @@ zonelist_scan:
                                goto try_this_zone;
 
                        if (zone_reclaim_mode == 0 ||
-                           !zone_allows_reclaim(ac->preferred_zone, zone))
+                           !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
                                continue;
 
                        ret = zone_reclaim(zone, gfp_mask, order);
@@ -2720,7 +2830,7 @@ zonelist_scan:
                        default:
                                /* did we reclaim enough */
                                if (zone_watermark_ok(zone, order, mark,
-                                               ac->classzone_idx, alloc_flags))
+                                               ac_classzone_idx(ac), alloc_flags))
                                        goto try_this_zone;
 
                                continue;
@@ -2728,7 +2838,7 @@ zonelist_scan:
                }
 
 try_this_zone:
-               page = buffered_rmqueue(ac->preferred_zone, zone, order,
+               page = buffered_rmqueue(ac->preferred_zoneref->zone, zone, order,
                                gfp_mask, alloc_flags, ac->migratetype);
                if (page) {
                        if (prep_new_page(page, order, gfp_mask, alloc_flags))
@@ -2753,18 +2863,13 @@ try_this_zone:
         * include remote zones now, before entering the slowpath and waking
         * kswapd: prefer spilling to a remote zone over swapping locally.
         */
-       if (alloc_flags & ALLOC_FAIR) {
-               alloc_flags &= ~ALLOC_FAIR;
-               if (nr_fair_skipped) {
-                       zonelist_rescan = true;
-                       reset_alloc_batches(ac->preferred_zone);
-               }
-               if (nr_online_nodes > 1)
-                       zonelist_rescan = true;
-       }
-
-       if (zonelist_rescan)
+       if (fair_skipped) {
+reset_fair:
+               apply_fair = false;
+               fair_skipped = false;
+               reset_alloc_batches(ac->preferred_zoneref->zone);
                goto zonelist_scan;
+       }
 
        return NULL;
 }
@@ -2872,22 +2977,18 @@ __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
                /* The OOM killer does not needlessly kill tasks for lowmem */
                if (ac->high_zoneidx < ZONE_NORMAL)
                        goto out;
-               /* The OOM killer does not compensate for IO-less reclaim */
-               if (!(gfp_mask & __GFP_FS)) {
-                       /*
-                        * XXX: Page reclaim didn't yield anything,
-                        * and the OOM killer can't be invoked, but
-                        * keep looping as per tradition.
-                        *
-                        * But do not keep looping if oom_killer_disable()
-                        * was already called, for the system is trying to
-                        * enter a quiescent state during suspend.
-                        */
-                       *did_some_progress = !oom_killer_disabled;
-                       goto out;
-               }
                if (pm_suspended_storage())
                        goto out;
+               /*
+                * XXX: GFP_NOFS allocations should rather fail than rely on
+                * other request to make a forward progress.
+                * We are in an unfortunate situation where out_of_memory cannot
+                * do much for this context but let's try it to at least get
+                * access to memory reserved if the current task is killed (see
+                * out_of_memory). Once filesystems are ready to handle allocation
+                * failures more gracefully we should just bail out here.
+                */
+
                /* The OOM killer may not free memory on a specific node */
                if (gfp_mask & __GFP_THISNODE)
                        goto out;
@@ -2917,7 +3018,7 @@ out:
 /* Try memory compaction for high-order allocations before reclaim */
 static struct page *
 __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
-               int alloc_flags, const struct alloc_context *ac,
+               unsigned int alloc_flags, const struct alloc_context *ac,
                enum migrate_mode mode, int *contended_compaction,
                bool *deferred_compaction)
 {
@@ -2973,7 +3074,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
 #else
 static inline struct page *
 __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
-               int alloc_flags, const struct alloc_context *ac,
+               unsigned int alloc_flags, const struct alloc_context *ac,
                enum migrate_mode mode, int *contended_compaction,
                bool *deferred_compaction)
 {
@@ -3013,7 +3114,7 @@ __perform_reclaim(gfp_t gfp_mask, unsigned int order,
 /* The really slow allocator path where we enter direct reclaim */
 static inline struct page *
 __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
-               int alloc_flags, const struct alloc_context *ac,
+               unsigned int alloc_flags, const struct alloc_context *ac,
                unsigned long *did_some_progress)
 {
        struct page *page = NULL;
@@ -3049,13 +3150,13 @@ static void wake_all_kswapds(unsigned int order, const struct alloc_context *ac)
 
        for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
                                                ac->high_zoneidx, ac->nodemask)
-               wakeup_kswapd(zone, order, zone_idx(ac->preferred_zone));
+               wakeup_kswapd(zone, order, ac_classzone_idx(ac));
 }
 
-static inline int
+static inline unsigned int
 gfp_to_alloc_flags(gfp_t gfp_mask)
 {
-       int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
+       unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
 
        /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
        BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
@@ -3116,7 +3217,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 {
        bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
        struct page *page = NULL;
-       int alloc_flags;
+       unsigned int alloc_flags;
        unsigned long pages_reclaimed = 0;
        unsigned long did_some_progress;
        enum migrate_mode migration_mode = MIGRATE_ASYNC;
@@ -3153,17 +3254,6 @@ retry:
         */
        alloc_flags = gfp_to_alloc_flags(gfp_mask);
 
-       /*
-        * Find the true preferred zone if the allocation is unconstrained by
-        * cpusets.
-        */
-       if (!(alloc_flags & ALLOC_CPUSET) && !ac->nodemask) {
-               struct zoneref *preferred_zoneref;
-               preferred_zoneref = first_zones_zonelist(ac->zonelist,
-                               ac->high_zoneidx, NULL, &ac->preferred_zone);
-               ac->classzone_idx = zonelist_zone_idx(preferred_zoneref);
-       }
-
        /* This is the last chance, in general, before the goto nopage. */
        page = get_page_from_freelist(gfp_mask, order,
                                alloc_flags & ~ALLOC_NO_WATERMARKS, ac);
@@ -3278,7 +3368,7 @@ retry:
        if ((did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER) ||
            ((gfp_mask & __GFP_REPEAT) && pages_reclaimed < (1 << order))) {
                /* Wait for some write requests to complete then retry */
-               wait_iff_congested(ac->preferred_zone, BLK_RW_ASYNC, HZ/50);
+               wait_iff_congested(ac->preferred_zoneref->zone, BLK_RW_ASYNC, HZ/50);
                goto retry;
        }
 
@@ -3316,17 +3406,24 @@ struct page *
 __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
                        struct zonelist *zonelist, nodemask_t *nodemask)
 {
-       struct zoneref *preferred_zoneref;
-       struct page *page = NULL;
+       struct page *page;
        unsigned int cpuset_mems_cookie;
-       int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET|ALLOC_FAIR;
-       gfp_t alloc_mask; /* The gfp_t that was actually used for allocation */
+       unsigned int alloc_flags = ALLOC_WMARK_LOW|ALLOC_FAIR;
+       gfp_t alloc_mask = gfp_mask; /* The gfp_t that was actually used for allocation */
        struct alloc_context ac = {
                .high_zoneidx = gfp_zone(gfp_mask),
+               .zonelist = zonelist,
                .nodemask = nodemask,
                .migratetype = gfpflags_to_migratetype(gfp_mask),
        };
 
+       if (cpusets_enabled()) {
+               alloc_mask |= __GFP_HARDWALL;
+               alloc_flags |= ALLOC_CPUSET;
+               if (!ac.nodemask)
+                       ac.nodemask = &cpuset_current_mems_allowed;
+       }
+
        gfp_mask &= gfp_allowed_mask;
 
        lockdep_trace_alloc(gfp_mask);
@@ -3350,49 +3447,48 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
 retry_cpuset:
        cpuset_mems_cookie = read_mems_allowed_begin();
 
-       /* We set it here, as __alloc_pages_slowpath might have changed it */
-       ac.zonelist = zonelist;
-
        /* Dirty zone balancing only done in the fast path */
        ac.spread_dirty_pages = (gfp_mask & __GFP_WRITE);
 
        /* The preferred zone is used for statistics later */
-       preferred_zoneref = first_zones_zonelist(ac.zonelist, ac.high_zoneidx,
-                               ac.nodemask ? : &cpuset_current_mems_allowed,
-                               &ac.preferred_zone);
-       if (!ac.preferred_zone)
-               goto out;
-       ac.classzone_idx = zonelist_zone_idx(preferred_zoneref);
+       ac.preferred_zoneref = first_zones_zonelist(ac.zonelist,
+                                       ac.high_zoneidx, ac.nodemask);
+       if (!ac.preferred_zoneref) {
+               page = NULL;
+               goto no_zone;
+       }
 
        /* First allocation attempt */
-       alloc_mask = gfp_mask|__GFP_HARDWALL;
        page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac);
-       if (unlikely(!page)) {
-               /*
-                * Runtime PM, block IO and its error handling path
-                * can deadlock because I/O on the device might not
-                * complete.
-                */
-               alloc_mask = memalloc_noio_flags(gfp_mask);
-               ac.spread_dirty_pages = false;
-
-               page = __alloc_pages_slowpath(alloc_mask, order, &ac);
-       }
+       if (likely(page))
+               goto out;
 
-       if (kmemcheck_enabled && page)
-               kmemcheck_pagealloc_alloc(page, order, gfp_mask);
+       /*
+        * Runtime PM, block IO and its error handling path can deadlock
+        * because I/O on the device might not complete.
+        */
+       alloc_mask = memalloc_noio_flags(gfp_mask);
+       ac.spread_dirty_pages = false;
 
-       trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
+       page = __alloc_pages_slowpath(alloc_mask, order, &ac);
 
-out:
+no_zone:
        /*
         * When updating a task's mems_allowed, it is possible to race with
         * parallel threads in such a way that an allocation can fail while
         * the mask is being updated. If a page allocation is about to fail,
         * check if the cpuset changed during allocation and if so, retry.
         */
-       if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
+       if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) {
+               alloc_mask = gfp_mask;
                goto retry_cpuset;
+       }
+
+out:
+       if (kmemcheck_enabled && page)
+               kmemcheck_pagealloc_alloc(page, order, gfp_mask);
+
+       trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
 
        return page;
 }
@@ -3790,6 +3886,8 @@ void si_meminfo_node(struct sysinfo *val, int nid)
 {
        int zone_type;          /* needs to be signed */
        unsigned long managed_pages = 0;
+       unsigned long managed_highpages = 0;
+       unsigned long free_highpages = 0;
        pg_data_t *pgdat = NODE_DATA(nid);
 
        for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
@@ -3798,12 +3896,19 @@ void si_meminfo_node(struct sysinfo *val, int nid)
        val->sharedram = node_page_state(nid, NR_SHMEM);
        val->freeram = node_page_state(nid, NR_FREE_PAGES);
 #ifdef CONFIG_HIGHMEM
-       val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].managed_pages;
-       val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
-                       NR_FREE_PAGES);
+       for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
+               struct zone *zone = &pgdat->node_zones[zone_type];
+
+               if (is_highmem(zone)) {
+                       managed_highpages += zone->managed_pages;
+                       free_highpages += zone_page_state(zone, NR_FREE_PAGES);
+               }
+       }
+       val->totalhigh = managed_highpages;
+       val->freehigh = free_highpages;
 #else
-       val->totalhigh = 0;
-       val->freehigh = 0;
+       val->totalhigh = managed_highpages;
+       val->freehigh = free_highpages;
 #endif
        val->mem_unit = PAGE_SIZE;
 }
@@ -4390,13 +4495,12 @@ static void build_zonelists(pg_data_t *pgdat)
  */
 int local_memory_node(int node)
 {
-       struct zone *zone;
+       struct zoneref *z;
 
-       (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
+       z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
                                   gfp_zone(GFP_KERNEL),
-                                  NULL,
-                                  &zone);
-       return zone->node;
+                                  NULL);
+       return z->zone->node;
 }
 #endif
 
@@ -6726,23 +6830,23 @@ void *__init alloc_large_system_hash(const char *tablename,
 }
 
 /* Return a pointer to the bitmap storing bits affecting a block of pages */
-static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
+static inline unsigned long *get_pageblock_bitmap(struct page *page,
                                                        unsigned long pfn)
 {
 #ifdef CONFIG_SPARSEMEM
        return __pfn_to_section(pfn)->pageblock_flags;
 #else
-       return zone->pageblock_flags;
+       return page_zone(page)->pageblock_flags;
 #endif /* CONFIG_SPARSEMEM */
 }
 
-static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
+static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
 {
 #ifdef CONFIG_SPARSEMEM
        pfn &= (PAGES_PER_SECTION-1);
        return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
 #else
-       pfn = pfn - round_down(zone->zone_start_pfn, pageblock_nr_pages);
+       pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
        return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
 #endif /* CONFIG_SPARSEMEM */
 }
@@ -6760,14 +6864,12 @@ unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
                                        unsigned long end_bitidx,
                                        unsigned long mask)
 {
-       struct zone *zone;
        unsigned long *bitmap;
        unsigned long bitidx, word_bitidx;
        unsigned long word;
 
-       zone = page_zone(page);
-       bitmap = get_pageblock_bitmap(zone, pfn);
-       bitidx = pfn_to_bitidx(zone, pfn);
+       bitmap = get_pageblock_bitmap(page, pfn);
+       bitidx = pfn_to_bitidx(page, pfn);
        word_bitidx = bitidx / BITS_PER_LONG;
        bitidx &= (BITS_PER_LONG-1);
 
@@ -6789,20 +6891,18 @@ void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
                                        unsigned long end_bitidx,
                                        unsigned long mask)
 {
-       struct zone *zone;
        unsigned long *bitmap;
        unsigned long bitidx, word_bitidx;
        unsigned long old_word, word;
 
        BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
 
-       zone = page_zone(page);
-       bitmap = get_pageblock_bitmap(zone, pfn);
-       bitidx = pfn_to_bitidx(zone, pfn);
+       bitmap = get_pageblock_bitmap(page, pfn);
+       bitidx = pfn_to_bitidx(page, pfn);
        word_bitidx = bitidx / BITS_PER_LONG;
        bitidx &= (BITS_PER_LONG-1);
 
-       VM_BUG_ON_PAGE(!zone_spans_pfn(zone, pfn), page);
+       VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
 
        bitidx += end_bitidx;
        mask <<= (BITS_PER_LONG - bitidx - 1);
@@ -6864,7 +6964,7 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
                 * We can't use page_count without pin a page
                 * because another CPU can free compound page.
                 * This check already skips compound tails of THP
-                * because their page->_count is zero at all time.
+                * because their page->_refcount is zero at all time.
                 */
                if (!page_ref_count(page)) {
                        if (PageBuddy(page))
@@ -7177,7 +7277,8 @@ void zone_pcp_reset(struct zone *zone)
 
 #ifdef CONFIG_MEMORY_HOTREMOVE
 /*
- * All pages in the range must be isolated before calling this.
+ * All pages in the range must be in a single zone and isolated
+ * before calling this.
  */
 void
 __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)