Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[platform/kernel/linux-rpi.git] / mm / memory_hotplug.c
index 7deb49f..9eea6e8 100644 (file)
@@ -982,8 +982,6 @@ static void reset_node_present_pages(pg_data_t *pgdat)
 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
 {
        struct pglist_data *pgdat;
-       unsigned long zones_size[MAX_NR_ZONES] = {0};
-       unsigned long zholes_size[MAX_NR_ZONES] = {0};
        unsigned long start_pfn = PFN_DOWN(start);
 
        pgdat = NODE_DATA(nid);
@@ -1006,8 +1004,11 @@ static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
 
        /* we can use NODE_DATA(nid) from here */
 
+       pgdat->node_id = nid;
+       pgdat->node_start_pfn = start_pfn;
+
        /* init node's zones as empty zones, we don't have any present pages.*/
-       free_area_init_node(nid, zones_size, start_pfn, zholes_size);
+       free_area_init_core_hotplug(nid);
        pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
 
        /*
@@ -1017,25 +1018,20 @@ static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
        build_all_zonelists(pgdat);
 
        /*
-        * zone->managed_pages is set to an approximate value in
-        * free_area_init_core(), which will cause
-        * /sys/device/system/node/nodeX/meminfo has wrong data.
-        * So reset it to 0 before any memory is onlined.
-        */
-       reset_node_managed_pages(pgdat);
-
-       /*
         * When memory is hot-added, all the memory is in offline state. So
         * clear all zones' present_pages because they will be updated in
         * online_pages() and offline_pages().
         */
+       reset_node_managed_pages(pgdat);
        reset_node_present_pages(pgdat);
 
        return pgdat;
 }
 
-static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
+static void rollback_node_hotadd(int nid)
 {
+       pg_data_t *pgdat = NODE_DATA(nid);
+
        arch_refresh_nodedata(nid, NULL);
        free_percpu(pgdat->per_cpu_nodestats);
        arch_free_nodedata(pgdat);
@@ -1046,28 +1042,48 @@ static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
 /**
  * try_online_node - online a node if offlined
  * @nid: the node ID
- *
+ * @start: start addr of the node
+ * @set_node_online: Whether we want to online the node
  * called by cpu_up() to online a node without onlined memory.
+ *
+ * Returns:
+ * 1 -> a new node has been allocated
+ * 0 -> the node is already online
+ * -ENOMEM -> the node could not be allocated
  */
-int try_online_node(int nid)
+static int __try_online_node(int nid, u64 start, bool set_node_online)
 {
-       pg_data_t       *pgdat;
-       int     ret;
+       pg_data_t *pgdat;
+       int ret = 1;
 
        if (node_online(nid))
                return 0;
 
-       mem_hotplug_begin();
-       pgdat = hotadd_new_pgdat(nid, 0);
+       pgdat = hotadd_new_pgdat(nid, start);
        if (!pgdat) {
                pr_err("Cannot online node %d due to NULL pgdat\n", nid);
                ret = -ENOMEM;
                goto out;
        }
-       node_set_online(nid);
-       ret = register_one_node(nid);
-       BUG_ON(ret);
+
+       if (set_node_online) {
+               node_set_online(nid);
+               ret = register_one_node(nid);
+               BUG_ON(ret);
+       }
 out:
+       return ret;
+}
+
+/*
+ * Users of this function always want to online/register the node
+ */
+int try_online_node(int nid)
+{
+       int ret;
+
+       mem_hotplug_begin();
+       ret =  __try_online_node(nid, 0, true);
        mem_hotplug_done();
        return ret;
 }
@@ -1099,9 +1115,7 @@ static int online_memory_block(struct memory_block *mem, void *arg)
 int __ref add_memory_resource(int nid, struct resource *res, bool online)
 {
        u64 start, size;
-       pg_data_t *pgdat = NULL;
-       bool new_pgdat;
-       bool new_node;
+       bool new_node = false;
        int ret;
 
        start = res->start;
@@ -1111,11 +1125,6 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online)
        if (ret)
                return ret;
 
-       {       /* Stupid hack to suppress address-never-null warning */
-               void *p = NODE_DATA(nid);
-               new_pgdat = !p;
-       }
-
        mem_hotplug_begin();
 
        /*
@@ -1126,48 +1135,31 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online)
         */
        memblock_add_node(start, size, nid);
 
-       new_node = !node_online(nid);
-       if (new_node) {
-               pgdat = hotadd_new_pgdat(nid, start);
-               ret = -ENOMEM;
-               if (!pgdat)
-                       goto error;
-       }
+       ret = __try_online_node(nid, start, false);
+       if (ret < 0)
+               goto error;
+       new_node = ret;
 
        /* call arch's memory hotadd */
        ret = arch_add_memory(nid, start, size, NULL, true);
-
        if (ret < 0)
                goto error;
 
-       /* we online node here. we can't roll back from here. */
-       node_set_online(nid);
-
        if (new_node) {
-               unsigned long start_pfn = start >> PAGE_SHIFT;
-               unsigned long nr_pages = size >> PAGE_SHIFT;
-
-               ret = __register_one_node(nid);
-               if (ret)
-                       goto register_fail;
-
-               /*
-                * link memory sections under this node. This is already
-                * done when creatig memory section in register_new_memory
-                * but that depends to have the node registered so offline
-                * nodes have to go through register_node.
-                * TODO clean up this mess.
-                */
-               ret = link_mem_sections(nid, start_pfn, nr_pages, false);
-register_fail:
-               /*
-                * If sysfs file of new node can't create, cpu on the node
+               /* If sysfs file of new node can't be created, cpu on the node
                 * can't be hot-added. There is no rollback way now.
                 * So, check by BUG_ON() to catch it reluctantly..
+                * We online node here. We can't roll back from here.
                 */
+               node_set_online(nid);
+               ret = __register_one_node(nid);
                BUG_ON(ret);
        }
 
+       /* link memory sections under this node.*/
+       ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
+       BUG_ON(ret);
+
        /* create new memmap entry */
        firmware_map_add_hotplug(start, start + size, "System RAM");
 
@@ -1180,8 +1172,8 @@ register_fail:
 
 error:
        /* rollback pgdat allocation and others */
-       if (new_pgdat && pgdat)
-               rollback_node_hotadd(nid, pgdat);
+       if (new_node)
+               rollback_node_hotadd(nid);
        memblock_remove(start, size);
 
 out: