mm: define memblock_virt_alloc_try_nid_raw
[sfrench/cifs-2.6.git] / mm / page_alloc.c
index 77e4d3c5c57b72dcd7e411a03707c26dc85c7c04..805f30dd1c26ef3755b485b5452c589c0e00493d 100644 (file)
@@ -24,7 +24,6 @@
 #include <linux/memblock.h>
 #include <linux/compiler.h>
 #include <linux/kernel.h>
-#include <linux/kmemcheck.h>
 #include <linux/kasan.h>
 #include <linux/module.h>
 #include <linux/suspend.h>
@@ -1013,7 +1012,6 @@ static __always_inline bool free_pages_prepare(struct page *page,
        VM_BUG_ON_PAGE(PageTail(page), page);
 
        trace_mm_page_free(page, order);
-       kmemcheck_free_shadow(page, order);
 
        /*
         * Check tail pages before head page information is cleared to
@@ -1410,14 +1408,17 @@ void clear_zone_contiguous(struct zone *zone)
 }
 
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-static void __init deferred_free_range(struct page *page,
-                                       unsigned long pfn, int nr_pages)
+static void __init deferred_free_range(unsigned long pfn,
+                                      unsigned long nr_pages)
 {
-       int i;
+       struct page *page;
+       unsigned long i;
 
-       if (!page)
+       if (!nr_pages)
                return;
 
+       page = pfn_to_page(pfn);
+
        /* Free a large naturally-aligned chunk if possible */
        if (nr_pages == pageblock_nr_pages &&
            (pfn & (pageblock_nr_pages - 1)) == 0) {
@@ -1443,19 +1444,109 @@ static inline void __init pgdat_init_report_one_done(void)
                complete(&pgdat_init_all_done_comp);
 }
 
+/*
+ * Helper for deferred_init_range, free the given range, reset the counters, and
+ * return number of pages freed.
+ */
+static inline unsigned long __init __def_free(unsigned long *nr_free,
+                                             unsigned long *free_base_pfn,
+                                             struct page **page)
+{
+       unsigned long nr = *nr_free;
+
+       deferred_free_range(*free_base_pfn, nr);
+       *free_base_pfn = 0;
+       *nr_free = 0;
+       *page = NULL;
+
+       return nr;
+}
+
+static unsigned long __init deferred_init_range(int nid, int zid,
+                                               unsigned long start_pfn,
+                                               unsigned long end_pfn)
+{
+       struct mminit_pfnnid_cache nid_init_state = { };
+       unsigned long nr_pgmask = pageblock_nr_pages - 1;
+       unsigned long free_base_pfn = 0;
+       unsigned long nr_pages = 0;
+       unsigned long nr_free = 0;
+       struct page *page = NULL;
+       unsigned long pfn;
+
+       /*
+        * First we check if pfn is valid on architectures where it is possible
+        * to have holes within pageblock_nr_pages. On systems where it is not
+        * possible, this function is optimized out.
+        *
+        * Then, we check if a current large page is valid by only checking the
+        * validity of the head pfn.
+        *
+        * meminit_pfn_in_nid is checked on systems where pfns can interleave
+        * within a node: a pfn is between start and end of a node, but does not
+        * belong to this memory node.
+        *
+        * Finally, we minimize pfn page lookups and scheduler checks by
+        * performing it only once every pageblock_nr_pages.
+        *
+        * We do it in two loops: first we initialize struct page, than free to
+        * buddy allocator, becuse while we are freeing pages we can access
+        * pages that are ahead (computing buddy page in __free_one_page()).
+        */
+       for (pfn = start_pfn; pfn < end_pfn; pfn++) {
+               if (!pfn_valid_within(pfn))
+                       continue;
+               if ((pfn & nr_pgmask) || pfn_valid(pfn)) {
+                       if (meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
+                               if (page && (pfn & nr_pgmask))
+                                       page++;
+                               else
+                                       page = pfn_to_page(pfn);
+                               __init_single_page(page, pfn, zid, nid);
+                               cond_resched();
+                       }
+               }
+       }
+
+       page = NULL;
+       for (pfn = start_pfn; pfn < end_pfn; pfn++) {
+               if (!pfn_valid_within(pfn)) {
+                       nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
+               } else if (!(pfn & nr_pgmask) && !pfn_valid(pfn)) {
+                       nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
+               } else if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
+                       nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
+               } else if (page && (pfn & nr_pgmask)) {
+                       page++;
+                       nr_free++;
+               } else {
+                       nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
+                       page = pfn_to_page(pfn);
+                       free_base_pfn = pfn;
+                       nr_free = 1;
+                       cond_resched();
+               }
+       }
+       /* Free the last block of pages to allocator */
+       nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
+
+       return nr_pages;
+}
+
 /* Initialise remaining memory on a node */
 static int __init deferred_init_memmap(void *data)
 {
        pg_data_t *pgdat = data;
        int nid = pgdat->node_id;
-       struct mminit_pfnnid_cache nid_init_state = { };
        unsigned long start = jiffies;
        unsigned long nr_pages = 0;
-       unsigned long walk_start, walk_end;
-       int i, zid;
+       unsigned long spfn, epfn;
+       phys_addr_t spa, epa;
+       int zid;
        struct zone *zone;
        unsigned long first_init_pfn = pgdat->first_deferred_pfn;
        const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
+       u64 i;
 
        if (first_init_pfn == ULONG_MAX) {
                pgdat_init_report_one_done();
@@ -1477,83 +1568,12 @@ static int __init deferred_init_memmap(void *data)
                if (first_init_pfn < zone_end_pfn(zone))
                        break;
        }
+       first_init_pfn = max(zone->zone_start_pfn, first_init_pfn);
 
-       for_each_mem_pfn_range(i, nid, &walk_start, &walk_end, NULL) {
-               unsigned long pfn, end_pfn;
-               struct page *page = NULL;
-               struct page *free_base_page = NULL;
-               unsigned long free_base_pfn = 0;
-               int nr_to_free = 0;
-
-               end_pfn = min(walk_end, zone_end_pfn(zone));
-               pfn = first_init_pfn;
-               if (pfn < walk_start)
-                       pfn = walk_start;
-               if (pfn < zone->zone_start_pfn)
-                       pfn = zone->zone_start_pfn;
-
-               for (; pfn < end_pfn; pfn++) {
-                       if (!pfn_valid_within(pfn))
-                               goto free_range;
-
-                       /*
-                        * Ensure pfn_valid is checked every
-                        * pageblock_nr_pages for memory holes
-                        */
-                       if ((pfn & (pageblock_nr_pages - 1)) == 0) {
-                               if (!pfn_valid(pfn)) {
-                                       page = NULL;
-                                       goto free_range;
-                               }
-                       }
-
-                       if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
-                               page = NULL;
-                               goto free_range;
-                       }
-
-                       /* Minimise pfn page lookups and scheduler checks */
-                       if (page && (pfn & (pageblock_nr_pages - 1)) != 0) {
-                               page++;
-                       } else {
-                               nr_pages += nr_to_free;
-                               deferred_free_range(free_base_page,
-                                               free_base_pfn, nr_to_free);
-                               free_base_page = NULL;
-                               free_base_pfn = nr_to_free = 0;
-
-                               page = pfn_to_page(pfn);
-                               cond_resched();
-                       }
-
-                       if (page->flags) {
-                               VM_BUG_ON(page_zone(page) != zone);
-                               goto free_range;
-                       }
-
-                       __init_single_page(page, pfn, zid, nid);
-                       if (!free_base_page) {
-                               free_base_page = page;
-                               free_base_pfn = pfn;
-                               nr_to_free = 0;
-                       }
-                       nr_to_free++;
-
-                       /* Where possible, batch up pages for a single free */
-                       continue;
-free_range:
-                       /* Free the current block of pages to allocator */
-                       nr_pages += nr_to_free;
-                       deferred_free_range(free_base_page, free_base_pfn,
-                                                               nr_to_free);
-                       free_base_page = NULL;
-                       free_base_pfn = nr_to_free = 0;
-               }
-               /* Free the last block of pages to allocator */
-               nr_pages += nr_to_free;
-               deferred_free_range(free_base_page, free_base_pfn, nr_to_free);
-
-               first_init_pfn = max(end_pfn, first_init_pfn);
+       for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
+               spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
+               epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
+               nr_pages += deferred_init_range(nid, zid, spfn, epfn);
        }
 
        /* Sanity check that the next zone really is unpopulated */
@@ -2669,15 +2689,6 @@ void split_page(struct page *page, unsigned int order)
        VM_BUG_ON_PAGE(PageCompound(page), page);
        VM_BUG_ON_PAGE(!page_count(page), page);
 
-#ifdef CONFIG_KMEMCHECK
-       /*
-        * Split shadow pages too, because free(page[0]) would
-        * otherwise free the whole shadow.
-        */
-       if (kmemcheck_page_is_tracked(page))
-               split_page(virt_to_page(page[0].shadow), order);
-#endif
-
        for (i = 1; i < (1 << order); i++)
                set_page_refcounted(page + i);
        split_page_owner(page, order);
@@ -4223,9 +4234,6 @@ out:
                page = NULL;
        }
 
-       if (kmemcheck_enabled && page)
-               kmemcheck_pagealloc_alloc(page, order, gfp_mask);
-
        trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
 
        return page;
@@ -7305,18 +7313,17 @@ void *__init alloc_large_system_hash(const char *tablename,
 
        log2qty = ilog2(numentries);
 
-       /*
-        * memblock allocator returns zeroed memory already, so HASH_ZERO is
-        * currently not used when HASH_EARLY is specified.
-        */
        gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC;
        do {
                size = bucketsize << log2qty;
-               if (flags & HASH_EARLY)
-                       table = memblock_virt_alloc_nopanic(size, 0);
-               else if (hashdist)
+               if (flags & HASH_EARLY) {
+                       if (flags & HASH_ZERO)
+                               table = memblock_virt_alloc_nopanic(size, 0);
+                       else
+                               table = memblock_virt_alloc_raw(size, 0);
+               } else if (hashdist) {
                        table = __vmalloc(size, gfp_flags, PAGE_KERNEL);
-               else {
+               else {
                        /*
                         * If bucketsize is not a power-of-two, we may free
                         * some pages at the end of hash table which
@@ -7353,10 +7360,10 @@ void *__init alloc_large_system_hash(const char *tablename,
  * race condition. So you can't expect this function should be exact.
  */
 bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
+                        int migratetype,
                         bool skip_hwpoisoned_pages)
 {
        unsigned long pfn, iter, found;
-       int mt;
 
        /*
         * For avoiding noise data, lru_add_drain_all() should be called
@@ -7364,8 +7371,14 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
         */
        if (zone_idx(zone) == ZONE_MOVABLE)
                return false;
-       mt = get_pageblock_migratetype(page);
-       if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
+
+       /*
+        * CMA allocations (alloc_contig_range) really need to mark isolate
+        * CMA pageblocks even when they are not movable in fact so consider
+        * them movable here.
+        */
+       if (is_migrate_cma(migratetype) &&
+                       is_migrate_cma(get_pageblock_migratetype(page)))
                return false;
 
        pfn = page_to_pfn(page);
@@ -7377,6 +7390,9 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
 
                page = pfn_to_page(check);
 
+               if (PageReserved(page))
+                       return true;
+
                /*
                 * Hugepages are not in LRU lists, but they're movable.
                 * We need not scan over tail pages bacause we don't
@@ -7450,7 +7466,7 @@ bool is_pageblock_removable_nolock(struct page *page)
        if (!zone_spans_pfn(zone, pfn))
                return false;
 
-       return !has_unmovable_pages(zone, page, 0, true);
+       return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, true);
 }
 
 #if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA)