]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - mm/memory_hotplug.c
Merge tag 'stackleak-v4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / mm / memory_hotplug.c
index 38d94b703e9d4932279b5657a2c889dfaaf1b749..61972da38d93cb54d6f1088df186ed20bb0f98bb 100644 (file)
@@ -33,7 +33,6 @@
 #include <linux/stop_machine.h>
 #include <linux/hugetlb.h>
 #include <linux/memblock.h>
-#include <linux/bootmem.h>
 #include <linux/compaction.h>
 
 #include <asm/tlbflush.h>
@@ -687,62 +686,19 @@ static void node_states_check_changes_online(unsigned long nr_pages,
        struct zone *zone, struct memory_notify *arg)
 {
        int nid = zone_to_nid(zone);
-       enum zone_type zone_last = ZONE_NORMAL;
 
-       /*
-        * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
-        * contains nodes which have zones of 0...ZONE_NORMAL,
-        * set zone_last to ZONE_NORMAL.
-        *
-        * If we don't have HIGHMEM nor movable node,
-        * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
-        * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
-        */
-       if (N_MEMORY == N_NORMAL_MEMORY)
-               zone_last = ZONE_MOVABLE;
+       arg->status_change_nid = -1;
+       arg->status_change_nid_normal = -1;
+       arg->status_change_nid_high = -1;
 
-       /*
-        * if the memory to be online is in a zone of 0...zone_last, and
-        * the zones of 0...zone_last don't have memory before online, we will
-        * need to set the node to node_states[N_NORMAL_MEMORY] after
-        * the memory is online.
-        */
-       if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
+       if (!node_state(nid, N_MEMORY))
+               arg->status_change_nid = nid;
+       if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
                arg->status_change_nid_normal = nid;
-       else
-               arg->status_change_nid_normal = -1;
-
 #ifdef CONFIG_HIGHMEM
-       /*
-        * If we have movable node, node_states[N_HIGH_MEMORY]
-        * contains nodes which have zones of 0...ZONE_HIGHMEM,
-        * set zone_last to ZONE_HIGHMEM.
-        *
-        * If we don't have movable node, node_states[N_NORMAL_MEMORY]
-        * contains nodes which have zones of 0...ZONE_MOVABLE,
-        * set zone_last to ZONE_MOVABLE.
-        */
-       zone_last = ZONE_HIGHMEM;
-       if (N_MEMORY == N_HIGH_MEMORY)
-               zone_last = ZONE_MOVABLE;
-
-       if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
+       if (zone_idx(zone) <= N_HIGH_MEMORY && !node_state(nid, N_HIGH_MEMORY))
                arg->status_change_nid_high = nid;
-       else
-               arg->status_change_nid_high = -1;
-#else
-       arg->status_change_nid_high = arg->status_change_nid_normal;
 #endif
-
-       /*
-        * if the node don't have memory befor online, we will need to
-        * set the node to node_states[N_MEMORY] after the memory
-        * is online.
-        */
-       if (!node_state(nid, N_MEMORY))
-               arg->status_change_nid = nid;
-       else
-               arg->status_change_nid = -1;
 }
 
 static void node_states_set_node(int node, struct memory_notify *arg)
@@ -753,7 +709,8 @@ static void node_states_set_node(int node, struct memory_notify *arg)
        if (arg->status_change_nid_high >= 0)
                node_set_state(node, N_HIGH_MEMORY);
 
-       node_set_state(node, N_MEMORY);
+       if (arg->status_change_nid >= 0)
+               node_set_state(node, N_MEMORY);
 }
 
 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
@@ -881,7 +838,6 @@ static struct zone * __meminit move_pfn_range(int online_type, int nid,
        return zone;
 }
 
-/* Must be protected by mem_hotplug_begin() or a device_lock */
 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
 {
        unsigned long flags;
@@ -893,6 +849,8 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
        struct memory_notify arg;
        struct memory_block *mem;
 
+       mem_hotplug_begin();
+
        /*
         * We can't use pfn_to_nid() because nid might be stored in struct page
         * which is not yet initialized. Instead, we find nid from memory block.
@@ -957,6 +915,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
 
        if (onlined_pages)
                memory_notify(MEM_ONLINE, &arg);
+       mem_hotplug_done();
        return 0;
 
 failed_addition:
@@ -964,6 +923,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
                 (unsigned long long) pfn << PAGE_SHIFT,
                 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
        memory_notify(MEM_CANCEL_ONLINE, &arg);
+       mem_hotplug_done();
        return ret;
 }
 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
@@ -1111,7 +1071,12 @@ static int online_memory_block(struct memory_block *mem, void *arg)
        return device_online(&mem->dev);
 }
 
-/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
+/*
+ * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
+ * and online/offline operations (triggered e.g. by sysfs).
+ *
+ * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
+ */
 int __ref add_memory_resource(int nid, struct resource *res, bool online)
 {
        u64 start, size;
@@ -1163,26 +1128,26 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online)
        /* create new memmap entry */
        firmware_map_add_hotplug(start, start + size, "System RAM");
 
+       /* device_online() will take the lock when calling online_pages() */
+       mem_hotplug_done();
+
        /* online pages if requested */
        if (online)
                walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
                                  NULL, online_memory_block);
 
-       goto out;
-
+       return ret;
 error:
        /* rollback pgdat allocation and others */
        if (new_node)
                rollback_node_hotadd(nid);
        memblock_remove(start, size);
-
-out:
        mem_hotplug_done();
        return ret;
 }
-EXPORT_SYMBOL_GPL(add_memory_resource);
 
-int __ref add_memory(int nid, u64 start, u64 size)
+/* requires device_hotplug_lock, see add_memory_resource() */
+int __ref __add_memory(int nid, u64 start, u64 size)
 {
        struct resource *res;
        int ret;
@@ -1196,6 +1161,17 @@ int __ref add_memory(int nid, u64 start, u64 size)
                release_memory_resource(res);
        return ret;
 }
+
+int add_memory(int nid, u64 start, u64 size)
+{
+       int rc;
+
+       lock_device_hotplug();
+       rc = __add_memory(nid, start, size);
+       unlock_device_hotplug();
+
+       return rc;
+}
 EXPORT_SYMBOL_GPL(add_memory);
 
 #ifdef CONFIG_MEMORY_HOTREMOVE
@@ -1505,75 +1481,53 @@ static void node_states_check_changes_offline(unsigned long nr_pages,
 {
        struct pglist_data *pgdat = zone->zone_pgdat;
        unsigned long present_pages = 0;
-       enum zone_type zt, zone_last = ZONE_NORMAL;
+       enum zone_type zt;
 
-       /*
-        * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
-        * contains nodes which have zones of 0...ZONE_NORMAL,
-        * set zone_last to ZONE_NORMAL.
-        *
-        * If we don't have HIGHMEM nor movable node,
-        * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
-        * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
-        */
-       if (N_MEMORY == N_NORMAL_MEMORY)
-               zone_last = ZONE_MOVABLE;
+       arg->status_change_nid = -1;
+       arg->status_change_nid_normal = -1;
+       arg->status_change_nid_high = -1;
 
        /*
-        * check whether node_states[N_NORMAL_MEMORY] will be changed.
-        * If the memory to be offline is in a zone of 0...zone_last,
-        * and it is the last present memory, 0...zone_last will
-        * become empty after offline , thus we can determind we will
-        * need to clear the node from node_states[N_NORMAL_MEMORY].
+        * Check whether node_states[N_NORMAL_MEMORY] will be changed.
+        * If the memory to be offline is within the range
+        * [0..ZONE_NORMAL], and it is the last present memory there,
+        * the zones in that range will become empty after the offlining,
+        * thus we can determine that we need to clear the node from
+        * node_states[N_NORMAL_MEMORY].
         */
-       for (zt = 0; zt <= zone_last; zt++)
+       for (zt = 0; zt <= ZONE_NORMAL; zt++)
                present_pages += pgdat->node_zones[zt].present_pages;
-       if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
+       if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
                arg->status_change_nid_normal = zone_to_nid(zone);
-       else
-               arg->status_change_nid_normal = -1;
 
 #ifdef CONFIG_HIGHMEM
        /*
-        * If we have movable node, node_states[N_HIGH_MEMORY]
-        * contains nodes which have zones of 0...ZONE_HIGHMEM,
-        * set zone_last to ZONE_HIGHMEM.
-        *
-        * If we don't have movable node, node_states[N_NORMAL_MEMORY]
-        * contains nodes which have zones of 0...ZONE_MOVABLE,
-        * set zone_last to ZONE_MOVABLE.
+        * node_states[N_HIGH_MEMORY] contains nodes which
+        * have normal memory or high memory.
+        * Here we add the present_pages belonging to ZONE_HIGHMEM.
+        * If the zone is within the range of [0..ZONE_HIGHMEM), and
+        * we determine that the zones in that range become empty,
+        * we need to clear the node for N_HIGH_MEMORY.
         */
-       zone_last = ZONE_HIGHMEM;
-       if (N_MEMORY == N_HIGH_MEMORY)
-               zone_last = ZONE_MOVABLE;
-
-       for (; zt <= zone_last; zt++)
-               present_pages += pgdat->node_zones[zt].present_pages;
-       if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
+       present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
+       if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
                arg->status_change_nid_high = zone_to_nid(zone);
-       else
-               arg->status_change_nid_high = -1;
-#else
-       arg->status_change_nid_high = arg->status_change_nid_normal;
 #endif
 
        /*
-        * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
+        * We have accounted the pages from [0..ZONE_NORMAL), and
+        * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
+        * as well.
+        * Here we count the possible pages from ZONE_MOVABLE.
+        * If after having accounted all the pages, we see that the nr_pages
+        * to be offlined is over or equal to the accounted pages,
+        * we know that the node will become empty, and so, we can clear
+        * it for N_MEMORY as well.
         */
-       zone_last = ZONE_MOVABLE;
+       present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
 
-       /*
-        * check whether node_states[N_HIGH_MEMORY] will be changed
-        * If we try to offline the last present @nr_pages from the node,
-        * we can determind we will need to clear the node from
-        * node_states[N_HIGH_MEMORY].
-        */
-       for (; zt <= zone_last; zt++)
-               present_pages += pgdat->node_zones[zt].present_pages;
        if (nr_pages >= present_pages)
                arg->status_change_nid = zone_to_nid(zone);
-       else
-               arg->status_change_nid = -1;
 }
 
 static void node_states_clear_node(int node, struct memory_notify *arg)
@@ -1581,12 +1535,10 @@ static void node_states_clear_node(int node, struct memory_notify *arg)
        if (arg->status_change_nid_normal >= 0)
                node_clear_state(node, N_NORMAL_MEMORY);
 
-       if ((N_MEMORY != N_NORMAL_MEMORY) &&
-           (arg->status_change_nid_high >= 0))
+       if (arg->status_change_nid_high >= 0)
                node_clear_state(node, N_HIGH_MEMORY);
 
-       if ((N_MEMORY != N_HIGH_MEMORY) &&
-           (arg->status_change_nid >= 0))
+       if (arg->status_change_nid >= 0)
                node_clear_state(node, N_MEMORY);
 }
 
@@ -1606,10 +1558,16 @@ static int __ref __offline_pages(unsigned long start_pfn,
                return -EINVAL;
        if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
                return -EINVAL;
+
+       mem_hotplug_begin();
+
        /* This makes hotplug much easier...and readable.
           we assume this for now. .*/
-       if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
+       if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
+                                 &valid_end)) {
+               mem_hotplug_done();
                return -EINVAL;
+       }
 
        zone = page_zone(pfn_to_page(valid_start));
        node = zone_to_nid(zone);
@@ -1618,8 +1576,10 @@ static int __ref __offline_pages(unsigned long start_pfn,
        /* set above range as isolated */
        ret = start_isolate_page_range(start_pfn, end_pfn,
                                       MIGRATE_MOVABLE, true);
-       if (ret)
+       if (ret) {
+               mem_hotplug_done();
                return ret;
+       }
 
        arg.start_pfn = start_pfn;
        arg.nr_pages = nr_pages;
@@ -1690,6 +1650,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
        writeback_set_ratelimit();
 
        memory_notify(MEM_OFFLINE, &arg);
+       mem_hotplug_done();
        return 0;
 
 failed_removal:
@@ -1699,10 +1660,10 @@ static int __ref __offline_pages(unsigned long start_pfn,
        memory_notify(MEM_CANCEL_OFFLINE, &arg);
        /* pushback to free area */
        undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
+       mem_hotplug_done();
        return ret;
 }
 
-/* Must be protected by mem_hotplug_begin() or a device_lock */
 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
 {
        return __offline_pages(start_pfn, start_pfn + nr_pages);
@@ -1873,7 +1834,7 @@ EXPORT_SYMBOL(try_offline_node);
  * and online/offline operations before this call, as required by
  * try_offline_node().
  */
-void __ref remove_memory(int nid, u64 start, u64 size)
+void __ref __remove_memory(int nid, u64 start, u64 size)
 {
        int ret;
 
@@ -1902,5 +1863,12 @@ void __ref remove_memory(int nid, u64 start, u64 size)
 
        mem_hotplug_done();
 }
+
+void remove_memory(int nid, u64 start, u64 size)
+{
+       lock_device_hotplug();
+       __remove_memory(nid, start, size);
+       unlock_device_hotplug();
+}
 EXPORT_SYMBOL_GPL(remove_memory);
 #endif /* CONFIG_MEMORY_HOTREMOVE */