memory_hotplug: fix possible incorrect node_states[N_NORMAL_MEMORY]
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / memory_hotplug.c
index e4eeaca..7219560 100644 (file)
@@ -460,6 +460,53 @@ static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
        return 0;
 }
 
+/* check which state of node_states will be changed when online memory */
+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, node_states[N_NORMAL_MEMORY] contains nodes
+        * which have 0...ZONE_NORMAL, set zone_last to ZONE_NORMAL.
+        *
+        * If we don't have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
+        * which have 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
+        */
+       if (N_HIGH_MEMORY == N_NORMAL_MEMORY)
+               zone_last = ZONE_MOVABLE;
+
+       /*
+        * 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))
+               arg->status_change_nid_normal = nid;
+       else
+               arg->status_change_nid_normal = -1;
+
+       /*
+        * if the node don't have memory befor online, we will need to
+        * set the node to node_states[N_HIGH_MEMORY] after the memory
+        * is online.
+        */
+       if (!node_state(nid, N_HIGH_MEMORY))
+               arg->status_change_nid = nid;
+       else
+               arg->status_change_nid = -1;
+}
+
+static void node_states_set_node(int node, struct memory_notify *arg)
+{
+       if (arg->status_change_nid_normal >= 0)
+               node_set_state(node, N_NORMAL_MEMORY);
+
+       node_set_state(node, N_HIGH_MEMORY);
+}
+
 
 int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
 {
@@ -471,13 +518,18 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
        struct memory_notify arg;
 
        lock_memory_hotplug();
+       /*
+        * This doesn't need a lock to do pfn_to_page().
+        * The section can't be removed here because of the
+        * memory_block->state_mutex.
+        */
+       zone = page_zone(pfn_to_page(pfn));
+
        arg.start_pfn = pfn;
        arg.nr_pages = nr_pages;
-       arg.status_change_nid = -1;
+       node_states_check_changes_online(nr_pages, zone, &arg);
 
        nid = page_to_nid(pfn_to_page(pfn));
-       if (node_present_pages(nid) == 0)
-               arg.status_change_nid = nid;
 
        ret = memory_notify(MEM_GOING_ONLINE, &arg);
        ret = notifier_to_errno(ret);
@@ -487,23 +539,21 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
                return ret;
        }
        /*
-        * This doesn't need a lock to do pfn_to_page().
-        * The section can't be removed here because of the
-        * memory_block->state_mutex.
-        */
-       zone = page_zone(pfn_to_page(pfn));
-       /*
         * If this zone is not populated, then it is not in zonelist.
         * This means the page allocator ignores this zone.
         * So, zonelist must be updated after online.
         */
        mutex_lock(&zonelists_mutex);
-       if (!populated_zone(zone))
+       if (!populated_zone(zone)) {
                need_zonelists_rebuild = 1;
+               build_all_zonelists(NULL, zone);
+       }
 
        ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
                online_pages_range);
        if (ret) {
+               if (need_zonelists_rebuild)
+                       zone_pcp_reset(zone);
                mutex_unlock(&zonelists_mutex);
                printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
                       (unsigned long long) pfn << PAGE_SHIFT,
@@ -517,9 +567,9 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
        zone->present_pages += onlined_pages;
        zone->zone_pgdat->node_present_pages += onlined_pages;
        if (onlined_pages) {
-               node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
+               node_states_set_node(zone_to_nid(zone), &arg);
                if (need_zonelists_rebuild)
-                       build_all_zonelists(NULL, zone);
+                       build_all_zonelists(NULL, NULL);
                else
                        zone_pcp_update(zone);
        }
@@ -847,7 +897,7 @@ check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
 {
        int ret;
        long offlined = *(long *)data;
-       ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
+       ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
        offlined = nr_pages;
        if (!ret)
                *(long *)data += offlined;
@@ -867,6 +917,67 @@ check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
        return offlined;
 }
 
+/* check which state of node_states will be changed when offline memory */
+static void node_states_check_changes_offline(unsigned long nr_pages,
+               struct zone *zone, struct memory_notify *arg)
+{
+       struct pglist_data *pgdat = zone->zone_pgdat;
+       unsigned long present_pages = 0;
+       enum zone_type zt, zone_last = ZONE_NORMAL;
+
+       /*
+        * If we have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
+        * which have 0...ZONE_NORMAL, set zone_last to ZONE_NORMAL.
+        *
+        * If we don't have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
+        * which have 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
+        */
+       if (N_HIGH_MEMORY == N_NORMAL_MEMORY)
+               zone_last = ZONE_MOVABLE;
+
+       /*
+        * 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].
+        */
+       for (zt = 0; zt <= zone_last; zt++)
+               present_pages += pgdat->node_zones[zt].present_pages;
+       if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
+               arg->status_change_nid_normal = zone_to_nid(zone);
+       else
+               arg->status_change_nid_normal = -1;
+
+       /*
+        * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
+        */
+       zone_last = ZONE_MOVABLE;
+
+       /*
+        * 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)
+{
+       if (arg->status_change_nid_normal >= 0)
+               node_clear_state(node, N_NORMAL_MEMORY);
+
+       if ((N_HIGH_MEMORY != N_NORMAL_MEMORY) &&
+           (arg->status_change_nid >= 0))
+               node_clear_state(node, N_HIGH_MEMORY);
+}
+
 static int __ref __offline_pages(unsigned long start_pfn,
                  unsigned long end_pfn, unsigned long timeout)
 {
@@ -894,15 +1005,14 @@ static int __ref __offline_pages(unsigned long start_pfn,
        nr_pages = end_pfn - start_pfn;
 
        /* set above range as isolated */
-       ret = start_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
+       ret = start_isolate_page_range(start_pfn, end_pfn,
+                                      MIGRATE_MOVABLE, true);
        if (ret)
                goto out;
 
        arg.start_pfn = start_pfn;
        arg.nr_pages = nr_pages;
-       arg.status_change_nid = -1;
-       if (nr_pages >= node_present_pages(node))
-               arg.status_change_nid = node;
+       node_states_check_changes_offline(nr_pages, zone, &arg);
 
        ret = memory_notify(MEM_GOING_OFFLINE, &arg);
        ret = notifier_to_errno(ret);
@@ -975,10 +1085,9 @@ repeat:
        } else
                zone_pcp_update(zone);
 
-       if (!node_present_pages(node)) {
-               node_clear_state(node, N_HIGH_MEMORY);
+       node_states_clear_node(node, &arg);
+       if (arg.status_change_nid >= 0)
                kswapd_stop(node);
-       }
 
        vm_total_pages = nr_free_pagecache_pages();
        writeback_set_ratelimit();