mm: migrate: support non-lru movable page migration
[deliverable/linux.git] / mm / memory_hotplug.c
index aa34431c3f31769b05a43820aca0a75c6975e8a3..82d0b98d27f878015153dd4d360ad7825bab9c48 100644 (file)
@@ -78,9 +78,24 @@ static struct {
 #define memhp_lock_acquire()      lock_map_acquire(&mem_hotplug.dep_map)
 #define memhp_lock_release()      lock_map_release(&mem_hotplug.dep_map)
 
+#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
 bool memhp_auto_online;
+#else
+bool memhp_auto_online = true;
+#endif
 EXPORT_SYMBOL_GPL(memhp_auto_online);
 
+static int __init setup_memhp_default_state(char *str)
+{
+       if (!strcmp(str, "online"))
+               memhp_auto_online = true;
+       else if (!strcmp(str, "offline"))
+               memhp_auto_online = false;
+
+       return 1;
+}
+__setup("memhp_default_state=", setup_memhp_default_state);
+
 void get_online_mems(void)
 {
        might_sleep();
@@ -248,7 +263,7 @@ static void register_page_bootmem_info_section(unsigned long start_pfn)
 }
 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
 
-void register_page_bootmem_info_node(struct pglist_data *pgdat)
+void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
 {
        unsigned long i, pfn, end_pfn, nr_pages;
        int node = pgdat->node_id;
@@ -285,7 +300,7 @@ void register_page_bootmem_info_node(struct pglist_data *pgdat)
                 * multiple nodes we check that this pfn does not already
                 * reside in some other nodes.
                 */
-               if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
+               if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
                        register_page_bootmem_info_section(pfn);
        }
 }
@@ -434,6 +449,25 @@ out_fail:
        return -1;
 }
 
+static struct zone * __meminit move_pfn_range(int zone_shift,
+               unsigned long start_pfn, unsigned long end_pfn)
+{
+       struct zone *zone = page_zone(pfn_to_page(start_pfn));
+       int ret = 0;
+
+       if (zone_shift < 0)
+               ret = move_pfn_range_left(zone + zone_shift, zone,
+                                         start_pfn, end_pfn);
+       else if (zone_shift)
+               ret = move_pfn_range_right(zone, zone + zone_shift,
+                                          start_pfn, end_pfn);
+
+       if (ret)
+               return NULL;
+
+       return zone + zone_shift;
+}
+
 static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
                                      unsigned long end_pfn)
 {
@@ -1013,6 +1047,37 @@ static void node_states_set_node(int node, struct memory_notify *arg)
        node_set_state(node, N_MEMORY);
 }
 
+int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
+                  enum zone_type target)
+{
+       struct zone *zone = page_zone(pfn_to_page(pfn));
+       enum zone_type idx = zone_idx(zone);
+       int i;
+
+       if (idx < target) {
+               /* pages must be at end of current zone */
+               if (pfn + nr_pages != zone_end_pfn(zone))
+                       return 0;
+
+               /* no zones in use between current zone and target */
+               for (i = idx + 1; i < target; i++)
+                       if (zone_is_initialized(zone - idx + i))
+                               return 0;
+       }
+
+       if (target < idx) {
+               /* pages must be at beginning of current zone */
+               if (pfn != zone->zone_start_pfn)
+                       return 0;
+
+               /* no zones in use between current zone and target */
+               for (i = target + 1; i < idx; i++)
+                       if (zone_is_initialized(zone - idx + i))
+                               return 0;
+       }
+
+       return target - idx;
+}
 
 /* Must be protected by mem_hotplug_begin() */
 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
@@ -1024,6 +1089,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
        int nid;
        int ret;
        struct memory_notify arg;
+       int zone_shift = 0;
 
        /*
         * This doesn't need a lock to do pfn_to_page().
@@ -1037,19 +1103,14 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
            !can_online_high_movable(zone))
                return -EINVAL;
 
-       if (online_type == MMOP_ONLINE_KERNEL &&
-           zone_idx(zone) == ZONE_MOVABLE) {
-               if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
-                       return -EINVAL;
-       }
-       if (online_type == MMOP_ONLINE_MOVABLE &&
-           zone_idx(zone) == ZONE_MOVABLE - 1) {
-               if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
-                       return -EINVAL;
-       }
+       if (online_type == MMOP_ONLINE_KERNEL)
+               zone_shift = zone_can_shift(pfn, nr_pages, ZONE_NORMAL);
+       else if (online_type == MMOP_ONLINE_MOVABLE)
+               zone_shift = zone_can_shift(pfn, nr_pages, ZONE_MOVABLE);
 
-       /* Previous code may changed the zone of the pfn range */
-       zone = page_zone(pfn_to_page(pfn));
+       zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
+       if (!zone)
+               return -EINVAL;
 
        arg.start_pfn = pfn;
        arg.nr_pages = nr_pages;
@@ -1410,7 +1471,7 @@ static struct page *next_active_pageblock(struct page *page)
 }
 
 /* Checks if this range of memory is likely to be hot-removable. */
-int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
+bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
 {
        struct page *page = pfn_to_page(start_pfn);
        struct page *end_page = page + nr_pages;
@@ -1418,12 +1479,12 @@ int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
        /* Check the starting page of each pageblock within the range */
        for (; page < end_page; page = next_active_pageblock(page)) {
                if (!is_pageblock_removable_nolock(page))
-                       return 0;
+                       return false;
                cond_resched();
        }
 
        /* All pageblocks in the memory block are likely to be hot-removable */
-       return 1;
+       return true;
 }
 
 /*
This page took 0.026755 seconds and 5 git commands to generate.