#include <trace/events/cma.h>
 
 #include "cma.h"
-#include "internal.h"
 
 struct cma cma_areas[MAX_CMA_AREAS];
 unsigned cma_area_count;
        if (!cma->bitmap)
                return -ENOMEM;
 
+       WARN_ON_ONCE(!pfn_valid(pfn));
+       zone = page_zone(pfn_to_page(pfn));
+
        do {
                unsigned j;
 
                base_pfn = pfn;
-               if (!pfn_valid(base_pfn))
-                       goto err;
-
-               zone = page_zone(pfn_to_page(base_pfn));
                for (j = pageblock_nr_pages; j; --j, pfn++) {
-                       if (!pfn_valid(pfn))
-                               goto err;
-
+                       WARN_ON_ONCE(!pfn_valid(pfn));
                        /*
-                        * In init_cma_reserved_pageblock(), present_pages
-                        * is adjusted with assumption that all pages in
-                        * the pageblock come from a single zone.
+                        * alloc_contig_range requires the pfn range
+                        * specified to be in the same zone. Make this
+                        * simple by forcing the entire CMA resv range
+                        * to be in the same zone.
                         */
                        if (page_zone(pfn_to_page(pfn)) != zone)
-                               goto err;
+                               goto not_in_zone;
                }
                init_cma_reserved_pageblock(pfn_to_page(base_pfn));
        } while (--i);
 
        return 0;
 
-err:
+not_in_zone:
        pr_err("CMA area %s could not be activated\n", cma->name);
        kfree(cma->bitmap);
        cma->count = 0;
 static int __init cma_init_reserved_areas(void)
 {
        int i;
-       struct zone *zone;
-       pg_data_t *pgdat;
-
-       if (!cma_area_count)
-               return 0;
-
-       for_each_online_pgdat(pgdat) {
-               unsigned long start_pfn = UINT_MAX, end_pfn = 0;
-
-               zone = &pgdat->node_zones[ZONE_MOVABLE];
-
-               /*
-                * In this case, we cannot adjust the zone range
-                * since it is now maximum node span and we don't
-                * know original zone range.
-                */
-               if (populated_zone(zone))
-                       continue;
-
-               for (i = 0; i < cma_area_count; i++) {
-                       if (pfn_to_nid(cma_areas[i].base_pfn) !=
-                               pgdat->node_id)
-                               continue;
-
-                       start_pfn = min(start_pfn, cma_areas[i].base_pfn);
-                       end_pfn = max(end_pfn, cma_areas[i].base_pfn +
-                                               cma_areas[i].count);
-               }
-
-               if (!end_pfn)
-                       continue;
-
-               zone->zone_start_pfn = start_pfn;
-               zone->spanned_pages = end_pfn - start_pfn;
-       }
 
        for (i = 0; i < cma_area_count; i++) {
                int ret = cma_activate_area(&cma_areas[i]);
                        return ret;
        }
 
-       /*
-        * Reserved pages for ZONE_MOVABLE are now activated and
-        * this would change ZONE_MOVABLE's managed page counter and
-        * the other zones' present counter. We need to re-calculate
-        * various zone information that depends on this initialization.
-        */
-       build_all_zonelists(NULL);
-       for_each_populated_zone(zone) {
-               if (zone_idx(zone) == ZONE_MOVABLE) {
-                       zone_pcp_reset(zone);
-                       setup_zone_pageset(zone);
-               } else
-                       zone_pcp_update(zone);
-
-               set_zone_contiguous(zone);
-       }
-
-       /*
-        * We need to re-init per zone wmark by calling
-        * init_per_zone_wmark_min() but doesn't call here because it is
-        * registered on core_initcall and it will be called later than us.
-        */
-
        return 0;
 }
-pure_initcall(cma_init_reserved_areas);
+core_initcall(cma_init_reserved_areas);
 
 /**
  * cma_init_reserved_mem() - create custom contiguous area from reserved memory
 
 }
 
 #ifdef CONFIG_CMA
-static void __init adjust_present_page_count(struct page *page, long count)
-{
-       struct zone *zone = page_zone(page);
-
-       /* We don't need to hold a lock since it is boot-up process */
-       zone->present_pages += count;
-}
-
 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
 void __init init_cma_reserved_pageblock(struct page *page)
 {
        unsigned i = pageblock_nr_pages;
-       unsigned long pfn = page_to_pfn(page);
        struct page *p = page;
-       int nid = page_to_nid(page);
-
-       /*
-        * ZONE_MOVABLE will steal present pages from other zones by
-        * changing page links so page_zone() is changed. Before that,
-        * we need to adjust previous zone's page count first.
-        */
-       adjust_present_page_count(page, -pageblock_nr_pages);
 
        do {
                __ClearPageReserved(p);
                set_page_count(p, 0);
-
-               /* Steal pages from other zones */
-               set_page_links(p, ZONE_MOVABLE, nid, pfn);
-       } while (++p, ++pfn, --i);
-
-       adjust_present_page_count(page, pageblock_nr_pages);
+       } while (++p, --i);
 
        set_pageblock_migratetype(page, MIGRATE_CMA);
 
                 * exists.
                 */
                watermark = min_wmark_pages(zone) + (1UL << order);
-               if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
+               if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
                        return 0;
 
                __mod_zone_freepage_state(zone, -(1UL << order), mt);
        }
 
 
+#ifdef CONFIG_CMA
+       /* If allocation can't use CMA areas don't use free CMA pages */
+       if (!(alloc_flags & ALLOC_CMA))
+               free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
+#endif
+
        /*
         * Check watermarks for an order-0 allocation request. If these
         * are not met, then a high-order request also cannot go ahead
                }
 
 #ifdef CONFIG_CMA
-               if (!list_empty(&area->free_list[MIGRATE_CMA]))
+               if ((alloc_flags & ALLOC_CMA) &&
+                   !list_empty(&area->free_list[MIGRATE_CMA])) {
                        return true;
+               }
 #endif
                if (alloc_harder &&
                        !list_empty(&area->free_list[MIGRATE_HIGHATOMIC]))
                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
         * 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 > mark + z->lowmem_reserve[classzone_idx])
+       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,
        } else if (unlikely(rt_task(current)) && !in_interrupt())
                alloc_flags |= ALLOC_HARDER;
 
+#ifdef CONFIG_CMA
+       if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
+               alloc_flags |= ALLOC_CMA;
+#endif
        return alloc_flags;
 }
 
        if (should_fail_alloc_page(gfp_mask, order))
                return false;
 
+       if (IS_ENABLED(CONFIG_CMA) && ac->migratetype == MIGRATE_MOVABLE)
+               *alloc_flags |= ALLOC_CMA;
+
        return true;
 }
 
 {
        enum zone_type j;
        int nid = pgdat->node_id;
-       unsigned long node_end_pfn = 0;
 
        pgdat_resize_init(pgdat);
 #ifdef CONFIG_NUMA_BALANCING
                struct zone *zone = pgdat->node_zones + j;
                unsigned long size, realsize, freesize, memmap_pages;
                unsigned long zone_start_pfn = zone->zone_start_pfn;
-               unsigned long movable_size = 0;
 
                size = zone->spanned_pages;
                realsize = freesize = zone->present_pages;
-               if (zone_end_pfn(zone) > node_end_pfn)
-                       node_end_pfn = zone_end_pfn(zone);
-
 
                /*
                 * Adjust freesize so that it accounts for how much memory
                zone_seqlock_init(zone);
                zone_pcp_init(zone);
 
-               /*
-                * The size of the CMA area is unknown now so we need to
-                * prepare the memory for the usemap at maximum.
-                */
-               if (IS_ENABLED(CONFIG_CMA) && j == ZONE_MOVABLE &&
-                       pgdat->node_spanned_pages) {
-                       movable_size = node_end_pfn - pgdat->node_start_pfn;
-               }
-
-               if (!size && !movable_size)
+               if (!size)
                        continue;
 
                set_pageblock_order();
-               if (movable_size) {
-                       zone->zone_start_pfn = pgdat->node_start_pfn;
-                       zone->spanned_pages = movable_size;
-                       setup_usemap(pgdat, zone,
-                               pgdat->node_start_pfn, movable_size);
-                       init_currently_empty_zone(zone,
-                               pgdat->node_start_pfn, movable_size);
-               } else {
-                       setup_usemap(pgdat, zone, zone_start_pfn, size);
-                       init_currently_empty_zone(zone, zone_start_pfn, size);
-               }
+               setup_usemap(pgdat, zone, zone_start_pfn, size);
+               init_currently_empty_zone(zone, zone_start_pfn, size);
                memmap_init(size, nid, j, zone_start_pfn);
        }
 }
 }
 #endif
 
-#if defined CONFIG_MEMORY_HOTPLUG || defined CONFIG_CMA
+#ifdef CONFIG_MEMORY_HOTPLUG
 /*
  * The zone indicated has a new number of managed_pages; batch sizes and percpu
  * page high values need to be recalulated.