These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / mm / memblock.c
index 9318b56..d300f13 100644 (file)
@@ -54,10 +54,16 @@ int memblock_debug __initdata_memblock;
 #ifdef CONFIG_MOVABLE_NODE
 bool movable_node_enabled __initdata_memblock = false;
 #endif
+static bool system_has_some_mirror __initdata_memblock = false;
 static int memblock_can_resize __initdata_memblock;
 static int memblock_memory_in_slab __initdata_memblock = 0;
 static int memblock_reserved_in_slab __initdata_memblock = 0;
 
+ulong __init_memblock choose_memblock_flags(void)
+{
+       return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
+}
+
 /* inline so we don't get a warning when pr_debug is compiled out */
 static __init_memblock const char *
 memblock_type_name(struct memblock_type *type)
@@ -85,7 +91,7 @@ static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, p
        return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
 }
 
-static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
+bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
                                        phys_addr_t base, phys_addr_t size)
 {
        unsigned long i;
@@ -97,7 +103,7 @@ static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
                        break;
        }
 
-       return (i < type->cnt) ? i : -1;
+       return i < type->cnt;
 }
 
 /*
@@ -107,6 +113,7 @@ static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  * @size: size of free area to find
  * @align: alignment of free area to find
  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
+ * @flags: pick from blocks based on memory attributes
  *
  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  *
@@ -115,12 +122,13 @@ static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  */
 static phys_addr_t __init_memblock
 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
-                               phys_addr_t size, phys_addr_t align, int nid)
+                               phys_addr_t size, phys_addr_t align, int nid,
+                               ulong flags)
 {
        phys_addr_t this_start, this_end, cand;
        u64 i;
 
-       for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
+       for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
                this_start = clamp(this_start, start, end);
                this_end = clamp(this_end, start, end);
 
@@ -139,6 +147,7 @@ __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  * @size: size of free area to find
  * @align: alignment of free area to find
  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
+ * @flags: pick from blocks based on memory attributes
  *
  * Utility called from memblock_find_in_range_node(), find free area top-down.
  *
@@ -147,12 +156,14 @@ __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  */
 static phys_addr_t __init_memblock
 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
-                              phys_addr_t size, phys_addr_t align, int nid)
+                              phys_addr_t size, phys_addr_t align, int nid,
+                              ulong flags)
 {
        phys_addr_t this_start, this_end, cand;
        u64 i;
 
-       for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
+       for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
+                                       NULL) {
                this_start = clamp(this_start, start, end);
                this_end = clamp(this_end, start, end);
 
@@ -174,6 +185,7 @@ __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  * @start: start of candidate range
  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
+ * @flags: pick from blocks based on memory attributes
  *
  * Find @size free area aligned to @align in the specified range and node.
  *
@@ -190,7 +202,7 @@ __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  */
 phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
                                        phys_addr_t align, phys_addr_t start,
-                                       phys_addr_t end, int nid)
+                                       phys_addr_t end, int nid, ulong flags)
 {
        phys_addr_t kernel_end, ret;
 
@@ -215,7 +227,7 @@ phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
                /* ok, try bottom-up allocation first */
                ret = __memblock_find_range_bottom_up(bottom_up_start, end,
-                                                     size, align, nid);
+                                                     size, align, nid, flags);
                if (ret)
                        return ret;
 
@@ -233,7 +245,8 @@ phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
                             "memory hotunplug may be affected\n");
        }
 
-       return __memblock_find_range_top_down(start, end, size, align, nid);
+       return __memblock_find_range_top_down(start, end, size, align, nid,
+                                             flags);
 }
 
 /**
@@ -252,8 +265,21 @@ phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
                                        phys_addr_t end, phys_addr_t size,
                                        phys_addr_t align)
 {
-       return memblock_find_in_range_node(size, align, start, end,
-                                           NUMA_NO_NODE);
+       phys_addr_t ret;
+       ulong flags = choose_memblock_flags();
+
+again:
+       ret = memblock_find_in_range_node(size, align, start, end,
+                                           NUMA_NO_NODE, flags);
+
+       if (!ret && (flags & MEMBLOCK_MIRROR)) {
+               pr_warn("Could not allocate %pap bytes of mirrored memory\n",
+                       &size);
+               flags &= ~MEMBLOCK_MIRROR;
+               goto again;
+       }
+
+       return ret;
 }
 
 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
@@ -540,6 +566,10 @@ repeat:
                 * area, insert that portion.
                 */
                if (rbase > base) {
+#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
+                       WARN_ON(nid != memblock_get_region_node(rgn));
+#endif
+                       WARN_ON(flags != rgn->flags);
                        nr_new++;
                        if (insert)
                                memblock_insert_region(type, i++, base,
@@ -585,14 +615,14 @@ static int __init_memblock memblock_add_region(phys_addr_t base,
                                                int nid,
                                                unsigned long flags)
 {
-       struct memblock_type *_rgn = &memblock.memory;
+       struct memblock_type *type = &memblock.memory;
 
        memblock_dbg("memblock_add: [%#016llx-%#016llx] flags %#02lx %pF\n",
                     (unsigned long long)base,
                     (unsigned long long)base + size - 1,
                     flags, (void *)_RET_IP_);
 
-       return memblock_add_range(_rgn, base, size, nid, flags);
+       return memblock_add_range(type, base, size, nid, flags);
 }
 
 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
@@ -676,7 +706,7 @@ static int __init_memblock memblock_isolate_range(struct memblock_type *type,
        return 0;
 }
 
-int __init_memblock memblock_remove_range(struct memblock_type *type,
+static int __init_memblock memblock_remove_range(struct memblock_type *type,
                                          phys_addr_t base, phys_addr_t size)
 {
        int start_rgn, end_rgn;
@@ -732,7 +762,7 @@ int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  *
  * This function isolates region [@base, @base + @size), and sets/clears flag
  *
- * Return 0 on succees, -errno on failure.
+ * Return 0 on success, -errno on failure.
  */
 static int __init_memblock memblock_setclr_flag(phys_addr_t base,
                                phys_addr_t size, int set, int flag)
@@ -759,7 +789,7 @@ static int __init_memblock memblock_setclr_flag(phys_addr_t base,
  * @base: the base phys addr of the region
  * @size: the size of the region
  *
- * Return 0 on succees, -errno on failure.
+ * Return 0 on success, -errno on failure.
  */
 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
 {
@@ -771,17 +801,65 @@ int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
  * @base: the base phys addr of the region
  * @size: the size of the region
  *
- * Return 0 on succees, -errno on failure.
+ * Return 0 on success, -errno on failure.
  */
 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
 {
        return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
 }
 
+/**
+ * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
+ * @base: the base phys addr of the region
+ * @size: the size of the region
+ *
+ * Return 0 on success, -errno on failure.
+ */
+int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
+{
+       system_has_some_mirror = true;
+
+       return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
+}
+
+
+/**
+ * __next_reserved_mem_region - next function for for_each_reserved_region()
+ * @idx: pointer to u64 loop variable
+ * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
+ * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
+ *
+ * Iterate over all reserved memory regions.
+ */
+void __init_memblock __next_reserved_mem_region(u64 *idx,
+                                          phys_addr_t *out_start,
+                                          phys_addr_t *out_end)
+{
+       struct memblock_type *type = &memblock.reserved;
+
+       if (*idx >= 0 && *idx < type->cnt) {
+               struct memblock_region *r = &type->regions[*idx];
+               phys_addr_t base = r->base;
+               phys_addr_t size = r->size;
+
+               if (out_start)
+                       *out_start = base;
+               if (out_end)
+                       *out_end = base + size - 1;
+
+               *idx += 1;
+               return;
+       }
+
+       /* signal end of iteration */
+       *idx = ULLONG_MAX;
+}
+
 /**
  * __next__mem_range - next function for for_each_free_mem_range() etc.
  * @idx: pointer to u64 loop variable
  * @nid: node selector, %NUMA_NO_NODE for all nodes
+ * @flags: pick from blocks based on memory attributes
  * @type_a: pointer to memblock_type from where the range is taken
  * @type_b: pointer to memblock_type which excludes memory from being taken
  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
@@ -803,7 +881,7 @@ int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
  * As both region arrays are sorted, the function advances the two indices
  * in lockstep and returns each intersection.
  */
-void __init_memblock __next_mem_range(u64 *idx, int nid,
+void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags,
                                      struct memblock_type *type_a,
                                      struct memblock_type *type_b,
                                      phys_addr_t *out_start,
@@ -831,6 +909,10 @@ void __init_memblock __next_mem_range(u64 *idx, int nid,
                if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
                        continue;
 
+               /* if we want mirror memory skip non-mirror memory regions */
+               if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
+                       continue;
+
                if (!type_b) {
                        if (out_start)
                                *out_start = m_start;
@@ -894,7 +976,8 @@ void __init_memblock __next_mem_range(u64 *idx, int nid,
  * in type_b.
  *
  * @idx: pointer to u64 loop variable
- * @nid: nid: node selector, %NUMA_NO_NODE for all nodes
+ * @nid: node selector, %NUMA_NO_NODE for all nodes
+ * @flags: pick from blocks based on memory attributes
  * @type_a: pointer to memblock_type from where the range is taken
  * @type_b: pointer to memblock_type which excludes memory from being taken
  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
@@ -903,7 +986,7 @@ void __init_memblock __next_mem_range(u64 *idx, int nid,
  *
  * Reverse of __next_mem_range().
  */
-void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
+void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags,
                                          struct memblock_type *type_a,
                                          struct memblock_type *type_b,
                                          phys_addr_t *out_start,
@@ -935,6 +1018,10 @@ void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
                if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
                        continue;
 
+               /* if we want mirror memory skip non-mirror memory regions */
+               if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
+                       continue;
+
                if (!type_b) {
                        if (out_start)
                                *out_start = m_start;
@@ -1050,14 +1137,15 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
                                        phys_addr_t align, phys_addr_t start,
-                                       phys_addr_t end, int nid)
+                                       phys_addr_t end, int nid, ulong flags)
 {
        phys_addr_t found;
 
        if (!align)
                align = SMP_CACHE_BYTES;
 
-       found = memblock_find_in_range_node(size, align, start, end, nid);
+       found = memblock_find_in_range_node(size, align, start, end, nid,
+                                           flags);
        if (found && !memblock_reserve(found, size)) {
                /*
                 * The min_count is set to 0 so that memblock allocations are
@@ -1070,26 +1158,40 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 }
 
 phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
-                                       phys_addr_t start, phys_addr_t end)
+                                       phys_addr_t start, phys_addr_t end,
+                                       ulong flags)
 {
-       return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
+       return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
+                                       flags);
 }
 
 static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
                                        phys_addr_t align, phys_addr_t max_addr,
-                                       int nid)
+                                       int nid, ulong flags)
 {
-       return memblock_alloc_range_nid(size, align, 0, max_addr, nid);
+       return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
 }
 
 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
 {
-       return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+       ulong flags = choose_memblock_flags();
+       phys_addr_t ret;
+
+again:
+       ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
+                                     nid, flags);
+
+       if (!ret && (flags & MEMBLOCK_MIRROR)) {
+               flags &= ~MEMBLOCK_MIRROR;
+               goto again;
+       }
+       return ret;
 }
 
 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
 {
-       return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE);
+       return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
+                                      MEMBLOCK_NONE);
 }
 
 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
@@ -1153,6 +1255,7 @@ static void * __init memblock_virt_alloc_internal(
 {
        phys_addr_t alloc;
        void *ptr;
+       ulong flags = choose_memblock_flags();
 
        if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
                nid = NUMA_NO_NODE;
@@ -1173,13 +1276,14 @@ static void * __init memblock_virt_alloc_internal(
 
 again:
        alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
-                                           nid);
+                                           nid, flags);
        if (alloc)
                goto done;
 
        if (nid != NUMA_NO_NODE) {
                alloc = memblock_find_in_range_node(size, align, min_addr,
-                                                   max_addr,  NUMA_NO_NODE);
+                                                   max_addr, NUMA_NO_NODE,
+                                                   flags);
                if (alloc)
                        goto done;
        }
@@ -1187,10 +1291,16 @@ again:
        if (min_addr) {
                min_addr = 0;
                goto again;
-       } else {
-               goto error;
        }
 
+       if (flags & MEMBLOCK_MIRROR) {
+               flags &= ~MEMBLOCK_MIRROR;
+               pr_warn("Could not allocate %pap bytes of mirrored memory\n",
+                       &size);
+               goto again;
+       }
+
+       return NULL;
 done:
        memblock_reserve(alloc, size);
        ptr = phys_to_virt(alloc);
@@ -1205,9 +1315,6 @@ done:
        kmemleak_alloc(ptr, size, 0, 0);
 
        return ptr;
-
-error:
-       return NULL;
 }
 
 /**
@@ -1316,7 +1423,7 @@ void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
        end = PFN_DOWN(base + size);
 
        for (; cursor < end; cursor++) {
-               __free_pages_bootmem(pfn_to_page(cursor), 0);
+               __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
                totalram_pages++;
        }
 }
@@ -1459,12 +1566,12 @@ int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size
  * Check if the region [@base, @base+@size) intersects a reserved memory block.
  *
  * RETURNS:
- * 0 if false, non-zero if true
+ * True if they intersect, false if not.
  */
-int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
+bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
 {
        memblock_cap_size(base, &size);
-       return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
+       return memblock_overlaps_region(&memblock.reserved, base, size);
 }
 
 void __init_memblock memblock_trim_memory(phys_addr_t align)