Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/memory_hotplug.c |
| 3 | * |
| 4 | * Copyright (C) |
| 5 | */ |
| 6 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 7 | #include <linux/stddef.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/swap.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/pagemap.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 14 | #include <linux/pagevec.h> |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 15 | #include <linux/writeback.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/sysctl.h> |
| 18 | #include <linux/cpu.h> |
| 19 | #include <linux/memory.h> |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 20 | #include <linux/memremap.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 21 | #include <linux/memory_hotplug.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/vmalloc.h> |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 24 | #include <linux/ioport.h> |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/migrate.h> |
| 27 | #include <linux/page-isolation.h> |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 28 | #include <linux/pfn.h> |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 29 | #include <linux/suspend.h> |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 30 | #include <linux/mm_inline.h> |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 31 | #include <linux/firmware-map.h> |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 32 | #include <linux/stop_machine.h> |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 33 | #include <linux/hugetlb.h> |
Tang Chen | c532092 | 2013-11-12 15:08:10 -0800 | [diff] [blame] | 34 | #include <linux/memblock.h> |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 35 | #include <linux/bootmem.h> |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 36 | #include <linux/compaction.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/tlbflush.h> |
| 39 | |
Adrian Bunk | 1e5ad9a | 2008-04-28 20:40:08 +0300 | [diff] [blame] | 40 | #include "internal.h" |
| 41 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 42 | /* |
| 43 | * online_page_callback contains pointer to current page onlining function. |
| 44 | * Initially it is generic_online_page(). If it is required it could be |
| 45 | * changed by calling set_online_page_callback() for callback registration |
| 46 | * and restore_online_page_callback() for generic callback restore. |
| 47 | */ |
| 48 | |
| 49 | static void generic_online_page(struct page *page); |
| 50 | |
| 51 | static online_page_callback_t online_page_callback = generic_online_page; |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 52 | static DEFINE_MUTEX(online_page_callback_lock); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 53 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 54 | /* The same as the cpu_hotplug lock, but for memory hotplug. */ |
| 55 | static struct { |
| 56 | struct task_struct *active_writer; |
| 57 | struct mutex lock; /* Synchronizes accesses to refcount, */ |
| 58 | /* |
| 59 | * Also blocks the new readers during |
| 60 | * an ongoing mem hotplug operation. |
| 61 | */ |
| 62 | int refcount; |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 63 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 64 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 65 | struct lockdep_map dep_map; |
| 66 | #endif |
| 67 | } mem_hotplug = { |
| 68 | .active_writer = NULL, |
| 69 | .lock = __MUTEX_INITIALIZER(mem_hotplug.lock), |
| 70 | .refcount = 0, |
| 71 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 72 | .dep_map = {.name = "mem_hotplug.lock" }, |
| 73 | #endif |
| 74 | }; |
| 75 | |
| 76 | /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */ |
| 77 | #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map) |
| 78 | #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map) |
| 79 | #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map) |
| 80 | |
Vitaly Kuznetsov | 8604d9e | 2016-05-19 17:13:03 -0700 | [diff] [blame^] | 81 | #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 82 | bool memhp_auto_online; |
Vitaly Kuznetsov | 8604d9e | 2016-05-19 17:13:03 -0700 | [diff] [blame^] | 83 | #else |
| 84 | bool memhp_auto_online = true; |
| 85 | #endif |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(memhp_auto_online); |
| 87 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 88 | void get_online_mems(void) |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 89 | { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 90 | might_sleep(); |
| 91 | if (mem_hotplug.active_writer == current) |
| 92 | return; |
| 93 | memhp_lock_acquire_read(); |
| 94 | mutex_lock(&mem_hotplug.lock); |
| 95 | mem_hotplug.refcount++; |
| 96 | mutex_unlock(&mem_hotplug.lock); |
| 97 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 100 | void put_online_mems(void) |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 101 | { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 102 | if (mem_hotplug.active_writer == current) |
| 103 | return; |
| 104 | mutex_lock(&mem_hotplug.lock); |
| 105 | |
| 106 | if (WARN_ON(!mem_hotplug.refcount)) |
| 107 | mem_hotplug.refcount++; /* try to fix things up */ |
| 108 | |
| 109 | if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer)) |
| 110 | wake_up_process(mem_hotplug.active_writer); |
| 111 | mutex_unlock(&mem_hotplug.lock); |
| 112 | memhp_lock_release(); |
| 113 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 114 | } |
| 115 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 116 | void mem_hotplug_begin(void) |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 117 | { |
| 118 | mem_hotplug.active_writer = current; |
| 119 | |
| 120 | memhp_lock_acquire(); |
| 121 | for (;;) { |
| 122 | mutex_lock(&mem_hotplug.lock); |
| 123 | if (likely(!mem_hotplug.refcount)) |
| 124 | break; |
| 125 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 126 | mutex_unlock(&mem_hotplug.lock); |
| 127 | schedule(); |
| 128 | } |
| 129 | } |
| 130 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 131 | void mem_hotplug_done(void) |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 132 | { |
| 133 | mem_hotplug.active_writer = NULL; |
| 134 | mutex_unlock(&mem_hotplug.lock); |
| 135 | memhp_lock_release(); |
| 136 | } |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 137 | |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 138 | /* add this memory to iomem resource */ |
| 139 | static struct resource *register_memory_resource(u64 start, u64 size) |
| 140 | { |
| 141 | struct resource *res; |
| 142 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
Vitaly Kuznetsov | 6f754ba | 2016-01-14 15:21:55 -0800 | [diff] [blame] | 143 | if (!res) |
| 144 | return ERR_PTR(-ENOMEM); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 145 | |
| 146 | res->name = "System RAM"; |
| 147 | res->start = start; |
| 148 | res->end = start + size - 1; |
Toshi Kani | 782b866 | 2016-01-26 21:57:24 +0100 | [diff] [blame] | 149 | res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 150 | if (request_resource(&iomem_resource, res) < 0) { |
Toshi Kani | 4996eed | 2013-07-03 15:02:39 -0700 | [diff] [blame] | 151 | pr_debug("System RAM resource %pR cannot be added\n", res); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 152 | kfree(res); |
Vitaly Kuznetsov | 6f754ba | 2016-01-14 15:21:55 -0800 | [diff] [blame] | 153 | return ERR_PTR(-EEXIST); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 154 | } |
| 155 | return res; |
| 156 | } |
| 157 | |
| 158 | static void release_memory_resource(struct resource *res) |
| 159 | { |
| 160 | if (!res) |
| 161 | return; |
| 162 | release_resource(res); |
| 163 | kfree(res); |
| 164 | return; |
| 165 | } |
| 166 | |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 167 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 168 | void get_page_bootmem(unsigned long info, struct page *page, |
| 169 | unsigned long type) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 170 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 171 | page->lru.next = (struct list_head *) type; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 172 | SetPagePrivate(page); |
| 173 | set_page_private(page, info); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 174 | page_ref_inc(page); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jiang Liu | 170a5a7 | 2013-07-03 15:03:17 -0700 | [diff] [blame] | 177 | void put_page_bootmem(struct page *page) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 178 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 179 | unsigned long type; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 180 | |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 181 | type = (unsigned long) page->lru.next; |
| 182 | BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || |
| 183 | type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 184 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 185 | if (page_ref_dec_return(page) == 1) { |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 186 | ClearPagePrivate(page); |
| 187 | set_page_private(page, 0); |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 188 | INIT_LIST_HEAD(&page->lru); |
Jiang Liu | 170a5a7 | 2013-07-03 15:03:17 -0700 | [diff] [blame] | 189 | free_reserved_page(page); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 190 | } |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 193 | #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE |
| 194 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
Adrian Bunk | d92bc31 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 195 | static void register_page_bootmem_info_section(unsigned long start_pfn) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 196 | { |
| 197 | unsigned long *usemap, mapsize, section_nr, i; |
| 198 | struct mem_section *ms; |
| 199 | struct page *page, *memmap; |
| 200 | |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 201 | section_nr = pfn_to_section_nr(start_pfn); |
| 202 | ms = __nr_to_section(section_nr); |
| 203 | |
| 204 | /* Get section's memmap address */ |
| 205 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); |
| 206 | |
| 207 | /* |
| 208 | * Get page for the memmap's phys address |
| 209 | * XXX: need more consideration for sparse_vmemmap... |
| 210 | */ |
| 211 | page = virt_to_page(memmap); |
| 212 | mapsize = sizeof(struct page) * PAGES_PER_SECTION; |
| 213 | mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; |
| 214 | |
| 215 | /* remember memmap's page */ |
| 216 | for (i = 0; i < mapsize; i++, page++) |
| 217 | get_page_bootmem(section_nr, page, SECTION_INFO); |
| 218 | |
| 219 | usemap = __nr_to_section(section_nr)->pageblock_flags; |
| 220 | page = virt_to_page(usemap); |
| 221 | |
| 222 | mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; |
| 223 | |
| 224 | for (i = 0; i < mapsize; i++, page++) |
Yasunori Goto | af370fb | 2008-07-23 21:28:17 -0700 | [diff] [blame] | 225 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 226 | |
| 227 | } |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 228 | #else /* CONFIG_SPARSEMEM_VMEMMAP */ |
| 229 | static void register_page_bootmem_info_section(unsigned long start_pfn) |
| 230 | { |
| 231 | unsigned long *usemap, mapsize, section_nr, i; |
| 232 | struct mem_section *ms; |
| 233 | struct page *page, *memmap; |
| 234 | |
| 235 | if (!pfn_valid(start_pfn)) |
| 236 | return; |
| 237 | |
| 238 | section_nr = pfn_to_section_nr(start_pfn); |
| 239 | ms = __nr_to_section(section_nr); |
| 240 | |
| 241 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); |
| 242 | |
| 243 | register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); |
| 244 | |
| 245 | usemap = __nr_to_section(section_nr)->pageblock_flags; |
| 246 | page = virt_to_page(usemap); |
| 247 | |
| 248 | mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; |
| 249 | |
| 250 | for (i = 0; i < mapsize; i++, page++) |
| 251 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); |
| 252 | } |
| 253 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 254 | |
| 255 | void register_page_bootmem_info_node(struct pglist_data *pgdat) |
| 256 | { |
| 257 | unsigned long i, pfn, end_pfn, nr_pages; |
| 258 | int node = pgdat->node_id; |
| 259 | struct page *page; |
| 260 | struct zone *zone; |
| 261 | |
| 262 | nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; |
| 263 | page = virt_to_page(pgdat); |
| 264 | |
| 265 | for (i = 0; i < nr_pages; i++, page++) |
| 266 | get_page_bootmem(node, page, NODE_INFO); |
| 267 | |
| 268 | zone = &pgdat->node_zones[0]; |
| 269 | for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) { |
Xishi Qiu | 139c2d7 | 2013-09-11 14:21:46 -0700 | [diff] [blame] | 270 | if (zone_is_initialized(zone)) { |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 271 | nr_pages = zone->wait_table_hash_nr_entries |
| 272 | * sizeof(wait_queue_head_t); |
| 273 | nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT; |
| 274 | page = virt_to_page(zone->wait_table); |
| 275 | |
| 276 | for (i = 0; i < nr_pages; i++, page++) |
| 277 | get_page_bootmem(node, page, NODE_INFO); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | pfn = pgdat->node_start_pfn; |
Cody P Schafer | c1f1949 | 2013-02-22 16:35:32 -0800 | [diff] [blame] | 282 | end_pfn = pgdat_end_pfn(pgdat); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 283 | |
Tang Chen | 7e9f5eb | 2013-07-08 16:00:23 -0700 | [diff] [blame] | 284 | /* register section info */ |
qiuxishi | f14851a | 2012-09-17 14:09:24 -0700 | [diff] [blame] | 285 | for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 286 | /* |
| 287 | * Some platforms can assign the same pfn to multiple nodes - on |
| 288 | * node0 as well as nodeN. To avoid registering a pfn against |
| 289 | * multiple nodes we check that this pfn does not already |
Tang Chen | 7e9f5eb | 2013-07-08 16:00:23 -0700 | [diff] [blame] | 290 | * reside in some other nodes. |
qiuxishi | f14851a | 2012-09-17 14:09:24 -0700 | [diff] [blame] | 291 | */ |
| 292 | if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node)) |
| 293 | register_page_bootmem_info_section(pfn); |
| 294 | } |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 295 | } |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 296 | #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 297 | |
Fabian Frederick | f276540 | 2014-08-06 16:04:57 -0700 | [diff] [blame] | 298 | static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn, |
| 299 | unsigned long end_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 300 | { |
| 301 | unsigned long old_zone_end_pfn; |
| 302 | |
| 303 | zone_span_writelock(zone); |
| 304 | |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 305 | old_zone_end_pfn = zone_end_pfn(zone); |
Xishi Qiu | 8080fc0 | 2013-09-11 14:21:45 -0700 | [diff] [blame] | 306 | if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 307 | zone->zone_start_pfn = start_pfn; |
| 308 | |
| 309 | zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - |
| 310 | zone->zone_start_pfn; |
| 311 | |
| 312 | zone_span_writeunlock(zone); |
| 313 | } |
| 314 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 315 | static void resize_zone(struct zone *zone, unsigned long start_pfn, |
| 316 | unsigned long end_pfn) |
| 317 | { |
| 318 | zone_span_writelock(zone); |
| 319 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 320 | if (end_pfn - start_pfn) { |
| 321 | zone->zone_start_pfn = start_pfn; |
| 322 | zone->spanned_pages = end_pfn - start_pfn; |
| 323 | } else { |
| 324 | /* |
| 325 | * make it consist as free_area_init_core(), |
| 326 | * if spanned_pages = 0, then keep start_pfn = 0 |
| 327 | */ |
| 328 | zone->zone_start_pfn = 0; |
| 329 | zone->spanned_pages = 0; |
| 330 | } |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 331 | |
| 332 | zone_span_writeunlock(zone); |
| 333 | } |
| 334 | |
| 335 | static void fix_zone_id(struct zone *zone, unsigned long start_pfn, |
| 336 | unsigned long end_pfn) |
| 337 | { |
| 338 | enum zone_type zid = zone_idx(zone); |
| 339 | int nid = zone->zone_pgdat->node_id; |
| 340 | unsigned long pfn; |
| 341 | |
| 342 | for (pfn = start_pfn; pfn < end_pfn; pfn++) |
| 343 | set_page_links(pfn_to_page(pfn), zid, nid, pfn); |
| 344 | } |
| 345 | |
Cody P Schafer | f6bbb78 | 2013-02-22 16:35:30 -0800 | [diff] [blame] | 346 | /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or |
Santosh Shilimkar | 9e43aa2 | 2014-01-21 15:50:43 -0800 | [diff] [blame] | 347 | * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */ |
Cody P Schafer | f6bbb78 | 2013-02-22 16:35:30 -0800 | [diff] [blame] | 348 | static int __ref ensure_zone_is_initialized(struct zone *zone, |
| 349 | unsigned long start_pfn, unsigned long num_pages) |
| 350 | { |
| 351 | if (!zone_is_initialized(zone)) |
Yaowei Bai | b171e40 | 2015-11-05 18:47:06 -0800 | [diff] [blame] | 352 | return init_currently_empty_zone(zone, start_pfn, num_pages); |
| 353 | |
Cody P Schafer | f6bbb78 | 2013-02-22 16:35:30 -0800 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 357 | static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2, |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 358 | unsigned long start_pfn, unsigned long end_pfn) |
| 359 | { |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 360 | int ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 361 | unsigned long flags; |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 362 | unsigned long z1_start_pfn; |
| 363 | |
Cody P Schafer | 64dd1b2 | 2013-02-22 16:35:31 -0800 | [diff] [blame] | 364 | ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn); |
| 365 | if (ret) |
| 366 | return ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 367 | |
| 368 | pgdat_resize_lock(z1->zone_pgdat, &flags); |
| 369 | |
| 370 | /* can't move pfns which are higher than @z2 */ |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 371 | if (end_pfn > zone_end_pfn(z2)) |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 372 | goto out_fail; |
Jiang Liu | 834405c | 2013-07-03 15:03:04 -0700 | [diff] [blame] | 373 | /* the move out part must be at the left most of @z2 */ |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 374 | if (start_pfn > z2->zone_start_pfn) |
| 375 | goto out_fail; |
| 376 | /* must included/overlap */ |
| 377 | if (end_pfn <= z2->zone_start_pfn) |
| 378 | goto out_fail; |
| 379 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 380 | /* use start_pfn for z1's start_pfn if z1 is empty */ |
Xishi Qiu | 8080fc0 | 2013-09-11 14:21:45 -0700 | [diff] [blame] | 381 | if (!zone_is_empty(z1)) |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 382 | z1_start_pfn = z1->zone_start_pfn; |
| 383 | else |
| 384 | z1_start_pfn = start_pfn; |
| 385 | |
| 386 | resize_zone(z1, z1_start_pfn, end_pfn); |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 387 | resize_zone(z2, end_pfn, zone_end_pfn(z2)); |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 388 | |
| 389 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 390 | |
| 391 | fix_zone_id(z1, start_pfn, end_pfn); |
| 392 | |
| 393 | return 0; |
| 394 | out_fail: |
| 395 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 396 | return -1; |
| 397 | } |
| 398 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 399 | static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2, |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 400 | unsigned long start_pfn, unsigned long end_pfn) |
| 401 | { |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 402 | int ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 403 | unsigned long flags; |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 404 | unsigned long z2_end_pfn; |
| 405 | |
Cody P Schafer | 64dd1b2 | 2013-02-22 16:35:31 -0800 | [diff] [blame] | 406 | ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn); |
| 407 | if (ret) |
| 408 | return ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 409 | |
| 410 | pgdat_resize_lock(z1->zone_pgdat, &flags); |
| 411 | |
| 412 | /* can't move pfns which are lower than @z1 */ |
| 413 | if (z1->zone_start_pfn > start_pfn) |
| 414 | goto out_fail; |
| 415 | /* the move out part mast at the right most of @z1 */ |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 416 | if (zone_end_pfn(z1) > end_pfn) |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 417 | goto out_fail; |
| 418 | /* must included/overlap */ |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 419 | if (start_pfn >= zone_end_pfn(z1)) |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 420 | goto out_fail; |
| 421 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 422 | /* use end_pfn for z2's end_pfn if z2 is empty */ |
Xishi Qiu | 8080fc0 | 2013-09-11 14:21:45 -0700 | [diff] [blame] | 423 | if (!zone_is_empty(z2)) |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 424 | z2_end_pfn = zone_end_pfn(z2); |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 425 | else |
| 426 | z2_end_pfn = end_pfn; |
| 427 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 428 | resize_zone(z1, z1->zone_start_pfn, start_pfn); |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 429 | resize_zone(z2, start_pfn, z2_end_pfn); |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 430 | |
| 431 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 432 | |
| 433 | fix_zone_id(z2, start_pfn, end_pfn); |
| 434 | |
| 435 | return 0; |
| 436 | out_fail: |
| 437 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 438 | return -1; |
| 439 | } |
| 440 | |
Fabian Frederick | f276540 | 2014-08-06 16:04:57 -0700 | [diff] [blame] | 441 | static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn, |
| 442 | unsigned long end_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 443 | { |
Xishi Qiu | 83285c7 | 2013-11-12 15:07:19 -0800 | [diff] [blame] | 444 | unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat); |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 445 | |
Tang Chen | 712cd38 | 2012-12-11 16:01:07 -0800 | [diff] [blame] | 446 | if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 447 | pgdat->node_start_pfn = start_pfn; |
| 448 | |
| 449 | pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - |
| 450 | pgdat->node_start_pfn; |
| 451 | } |
| 452 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 453 | static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 454 | { |
| 455 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 456 | int nr_pages = PAGES_PER_SECTION; |
| 457 | int nid = pgdat->node_id; |
| 458 | int zone_type; |
Mel Gorman | e298ff7 | 2015-08-06 15:46:51 -0700 | [diff] [blame] | 459 | unsigned long flags, pfn; |
Cody P Schafer | 64dd1b2 | 2013-02-22 16:35:31 -0800 | [diff] [blame] | 460 | int ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 461 | |
| 462 | zone_type = zone - pgdat->node_zones; |
Cody P Schafer | 64dd1b2 | 2013-02-22 16:35:31 -0800 | [diff] [blame] | 463 | ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages); |
| 464 | if (ret) |
| 465 | return ret; |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 466 | |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 467 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 468 | grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages); |
| 469 | grow_pgdat_span(zone->zone_pgdat, phys_start_pfn, |
| 470 | phys_start_pfn + nr_pages); |
| 471 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 472 | memmap_init_zone(nr_pages, nid, zone_type, |
| 473 | phys_start_pfn, MEMMAP_HOTPLUG); |
Mel Gorman | e298ff7 | 2015-08-06 15:46:51 -0700 | [diff] [blame] | 474 | |
| 475 | /* online_page_range is called later and expects pages reserved */ |
| 476 | for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) { |
| 477 | if (!pfn_valid(pfn)) |
| 478 | continue; |
| 479 | |
| 480 | SetPageReserved(pfn_to_page(pfn)); |
| 481 | } |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 482 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 485 | static int __meminit __add_section(int nid, struct zone *zone, |
| 486 | unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 487 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 488 | int ret; |
| 489 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 490 | if (pfn_valid(phys_start_pfn)) |
| 491 | return -EEXIST; |
| 492 | |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 493 | ret = sparse_add_one_section(zone, phys_start_pfn); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 494 | |
| 495 | if (ret < 0) |
| 496 | return ret; |
| 497 | |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 498 | ret = __add_zone(zone, phys_start_pfn); |
| 499 | |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 503 | return register_new_memory(nid, __pfn_to_section(phys_start_pfn)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 504 | } |
| 505 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Reasonably generic function for adding memory. It is |
| 508 | * expected that archs that support memory hotplug will |
| 509 | * call this function after deciding the zone to which to |
| 510 | * add the new pages. |
| 511 | */ |
| 512 | int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn, |
| 513 | unsigned long nr_pages) |
| 514 | { |
| 515 | unsigned long i; |
| 516 | int err = 0; |
| 517 | int start_sec, end_sec; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 518 | struct vmem_altmap *altmap; |
| 519 | |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 520 | clear_zone_contiguous(zone); |
| 521 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 522 | /* during initialize mem_map, align hot-added range to section */ |
| 523 | start_sec = pfn_to_section_nr(phys_start_pfn); |
| 524 | end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); |
| 525 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 526 | altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn)); |
| 527 | if (altmap) { |
| 528 | /* |
| 529 | * Validate altmap is within bounds of the total request |
| 530 | */ |
| 531 | if (altmap->base_pfn != phys_start_pfn |
| 532 | || vmem_altmap_offset(altmap) > nr_pages) { |
| 533 | pr_warn_once("memory add fail, invalid altmap\n"); |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 534 | err = -EINVAL; |
| 535 | goto out; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 536 | } |
| 537 | altmap->alloc = 0; |
| 538 | } |
| 539 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 540 | for (i = start_sec; i <= end_sec; i++) { |
Sheng Yong | 19c07d5 | 2015-04-14 15:44:54 -0700 | [diff] [blame] | 541 | err = __add_section(nid, zone, section_nr_to_pfn(i)); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * EEXIST is finally dealt with by ioresource collision |
| 545 | * check. see add_memory() => register_memory_resource() |
| 546 | * Warning will be printed if there is collision. |
| 547 | */ |
| 548 | if (err && (err != -EEXIST)) |
| 549 | break; |
| 550 | err = 0; |
| 551 | } |
Zhu Guihua | c435a39 | 2015-06-24 16:58:42 -0700 | [diff] [blame] | 552 | vmemmap_populate_print_last(); |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 553 | out: |
| 554 | set_zone_contiguous(zone); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 555 | return err; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(__add_pages); |
| 558 | |
| 559 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 560 | /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ |
| 561 | static int find_smallest_section_pfn(int nid, struct zone *zone, |
| 562 | unsigned long start_pfn, |
| 563 | unsigned long end_pfn) |
| 564 | { |
| 565 | struct mem_section *ms; |
| 566 | |
| 567 | for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) { |
| 568 | ms = __pfn_to_section(start_pfn); |
| 569 | |
| 570 | if (unlikely(!valid_section(ms))) |
| 571 | continue; |
| 572 | |
| 573 | if (unlikely(pfn_to_nid(start_pfn) != nid)) |
| 574 | continue; |
| 575 | |
| 576 | if (zone && zone != page_zone(pfn_to_page(start_pfn))) |
| 577 | continue; |
| 578 | |
| 579 | return start_pfn; |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ |
| 586 | static int find_biggest_section_pfn(int nid, struct zone *zone, |
| 587 | unsigned long start_pfn, |
| 588 | unsigned long end_pfn) |
| 589 | { |
| 590 | struct mem_section *ms; |
| 591 | unsigned long pfn; |
| 592 | |
| 593 | /* pfn is the end pfn of a memory section. */ |
| 594 | pfn = end_pfn - 1; |
| 595 | for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) { |
| 596 | ms = __pfn_to_section(pfn); |
| 597 | |
| 598 | if (unlikely(!valid_section(ms))) |
| 599 | continue; |
| 600 | |
| 601 | if (unlikely(pfn_to_nid(pfn) != nid)) |
| 602 | continue; |
| 603 | |
| 604 | if (zone && zone != page_zone(pfn_to_page(pfn))) |
| 605 | continue; |
| 606 | |
| 607 | return pfn; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, |
| 614 | unsigned long end_pfn) |
| 615 | { |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 616 | unsigned long zone_start_pfn = zone->zone_start_pfn; |
| 617 | unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ |
| 618 | unsigned long zone_end_pfn = z; |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 619 | unsigned long pfn; |
| 620 | struct mem_section *ms; |
| 621 | int nid = zone_to_nid(zone); |
| 622 | |
| 623 | zone_span_writelock(zone); |
| 624 | if (zone_start_pfn == start_pfn) { |
| 625 | /* |
| 626 | * If the section is smallest section in the zone, it need |
| 627 | * shrink zone->zone_start_pfn and zone->zone_spanned_pages. |
| 628 | * In this case, we find second smallest valid mem_section |
| 629 | * for shrinking zone. |
| 630 | */ |
| 631 | pfn = find_smallest_section_pfn(nid, zone, end_pfn, |
| 632 | zone_end_pfn); |
| 633 | if (pfn) { |
| 634 | zone->zone_start_pfn = pfn; |
| 635 | zone->spanned_pages = zone_end_pfn - pfn; |
| 636 | } |
| 637 | } else if (zone_end_pfn == end_pfn) { |
| 638 | /* |
| 639 | * If the section is biggest section in the zone, it need |
| 640 | * shrink zone->spanned_pages. |
| 641 | * In this case, we find second biggest valid mem_section for |
| 642 | * shrinking zone. |
| 643 | */ |
| 644 | pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, |
| 645 | start_pfn); |
| 646 | if (pfn) |
| 647 | zone->spanned_pages = pfn - zone_start_pfn + 1; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * The section is not biggest or smallest mem_section in the zone, it |
| 652 | * only creates a hole in the zone. So in this case, we need not |
| 653 | * change the zone. But perhaps, the zone has only hole data. Thus |
| 654 | * it check the zone has only hole or not. |
| 655 | */ |
| 656 | pfn = zone_start_pfn; |
| 657 | for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) { |
| 658 | ms = __pfn_to_section(pfn); |
| 659 | |
| 660 | if (unlikely(!valid_section(ms))) |
| 661 | continue; |
| 662 | |
| 663 | if (page_zone(pfn_to_page(pfn)) != zone) |
| 664 | continue; |
| 665 | |
| 666 | /* If the section is current section, it continues the loop */ |
| 667 | if (start_pfn == pfn) |
| 668 | continue; |
| 669 | |
| 670 | /* If we find valid section, we have nothing to do */ |
| 671 | zone_span_writeunlock(zone); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | /* The zone has no valid section */ |
| 676 | zone->zone_start_pfn = 0; |
| 677 | zone->spanned_pages = 0; |
| 678 | zone_span_writeunlock(zone); |
| 679 | } |
| 680 | |
| 681 | static void shrink_pgdat_span(struct pglist_data *pgdat, |
| 682 | unsigned long start_pfn, unsigned long end_pfn) |
| 683 | { |
Xishi Qiu | 83285c7 | 2013-11-12 15:07:19 -0800 | [diff] [blame] | 684 | unsigned long pgdat_start_pfn = pgdat->node_start_pfn; |
| 685 | unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */ |
| 686 | unsigned long pgdat_end_pfn = p; |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 687 | unsigned long pfn; |
| 688 | struct mem_section *ms; |
| 689 | int nid = pgdat->node_id; |
| 690 | |
| 691 | if (pgdat_start_pfn == start_pfn) { |
| 692 | /* |
| 693 | * If the section is smallest section in the pgdat, it need |
| 694 | * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages. |
| 695 | * In this case, we find second smallest valid mem_section |
| 696 | * for shrinking zone. |
| 697 | */ |
| 698 | pfn = find_smallest_section_pfn(nid, NULL, end_pfn, |
| 699 | pgdat_end_pfn); |
| 700 | if (pfn) { |
| 701 | pgdat->node_start_pfn = pfn; |
| 702 | pgdat->node_spanned_pages = pgdat_end_pfn - pfn; |
| 703 | } |
| 704 | } else if (pgdat_end_pfn == end_pfn) { |
| 705 | /* |
| 706 | * If the section is biggest section in the pgdat, it need |
| 707 | * shrink pgdat->node_spanned_pages. |
| 708 | * In this case, we find second biggest valid mem_section for |
| 709 | * shrinking zone. |
| 710 | */ |
| 711 | pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn, |
| 712 | start_pfn); |
| 713 | if (pfn) |
| 714 | pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * If the section is not biggest or smallest mem_section in the pgdat, |
| 719 | * it only creates a hole in the pgdat. So in this case, we need not |
| 720 | * change the pgdat. |
| 721 | * But perhaps, the pgdat has only hole data. Thus it check the pgdat |
| 722 | * has only hole or not. |
| 723 | */ |
| 724 | pfn = pgdat_start_pfn; |
| 725 | for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) { |
| 726 | ms = __pfn_to_section(pfn); |
| 727 | |
| 728 | if (unlikely(!valid_section(ms))) |
| 729 | continue; |
| 730 | |
| 731 | if (pfn_to_nid(pfn) != nid) |
| 732 | continue; |
| 733 | |
| 734 | /* If the section is current section, it continues the loop */ |
| 735 | if (start_pfn == pfn) |
| 736 | continue; |
| 737 | |
| 738 | /* If we find valid section, we have nothing to do */ |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | /* The pgdat has no valid section */ |
| 743 | pgdat->node_start_pfn = 0; |
| 744 | pgdat->node_spanned_pages = 0; |
| 745 | } |
| 746 | |
| 747 | static void __remove_zone(struct zone *zone, unsigned long start_pfn) |
| 748 | { |
| 749 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 750 | int nr_pages = PAGES_PER_SECTION; |
| 751 | int zone_type; |
| 752 | unsigned long flags; |
| 753 | |
| 754 | zone_type = zone - pgdat->node_zones; |
| 755 | |
| 756 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 757 | shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); |
| 758 | shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages); |
| 759 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
| 760 | } |
| 761 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 762 | static int __remove_section(struct zone *zone, struct mem_section *ms, |
| 763 | unsigned long map_offset) |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 764 | { |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 765 | unsigned long start_pfn; |
| 766 | int scn_nr; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 767 | int ret = -EINVAL; |
| 768 | |
| 769 | if (!valid_section(ms)) |
| 770 | return ret; |
| 771 | |
| 772 | ret = unregister_memory_section(ms); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 776 | scn_nr = __section_nr(ms); |
| 777 | start_pfn = section_nr_to_pfn(scn_nr); |
| 778 | __remove_zone(zone, start_pfn); |
| 779 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 780 | sparse_remove_one_section(zone, ms, map_offset); |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 784 | /** |
| 785 | * __remove_pages() - remove sections of pages from a zone |
| 786 | * @zone: zone from which pages need to be removed |
| 787 | * @phys_start_pfn: starting pageframe (must be aligned to start of a section) |
| 788 | * @nr_pages: number of pages to remove (must be multiple of section size) |
| 789 | * |
| 790 | * Generic helper function to remove section mappings and sysfs entries |
| 791 | * for the section of the memory we are removing. Caller needs to make |
| 792 | * sure that pages are marked reserved and zones are adjust properly by |
| 793 | * calling offline_pages(). |
| 794 | */ |
| 795 | int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 796 | unsigned long nr_pages) |
| 797 | { |
Toshi Kani | fe74ebb | 2013-04-29 15:08:20 -0700 | [diff] [blame] | 798 | unsigned long i; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 799 | unsigned long map_offset = 0; |
| 800 | int sections_to_remove, ret = 0; |
| 801 | |
| 802 | /* In the ZONE_DEVICE case device driver owns the memory region */ |
| 803 | if (is_dev_zone(zone)) { |
| 804 | struct page *page = pfn_to_page(phys_start_pfn); |
| 805 | struct vmem_altmap *altmap; |
| 806 | |
| 807 | altmap = to_vmem_altmap((unsigned long) page); |
| 808 | if (altmap) |
| 809 | map_offset = vmem_altmap_offset(altmap); |
| 810 | } else { |
| 811 | resource_size_t start, size; |
| 812 | |
| 813 | start = phys_start_pfn << PAGE_SHIFT; |
| 814 | size = nr_pages * PAGE_SIZE; |
| 815 | |
| 816 | ret = release_mem_region_adjustable(&iomem_resource, start, |
| 817 | size); |
| 818 | if (ret) { |
| 819 | resource_size_t endres = start + size - 1; |
| 820 | |
| 821 | pr_warn("Unable to release resource <%pa-%pa> (%d)\n", |
| 822 | &start, &endres, ret); |
| 823 | } |
| 824 | } |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 825 | |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 826 | clear_zone_contiguous(zone); |
| 827 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 828 | /* |
| 829 | * We can only remove entire sections |
| 830 | */ |
| 831 | BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); |
| 832 | BUG_ON(nr_pages % PAGES_PER_SECTION); |
| 833 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 834 | sections_to_remove = nr_pages / PAGES_PER_SECTION; |
| 835 | for (i = 0; i < sections_to_remove; i++) { |
| 836 | unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 837 | |
| 838 | ret = __remove_section(zone, __pfn_to_section(pfn), map_offset); |
| 839 | map_offset = 0; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 840 | if (ret) |
| 841 | break; |
| 842 | } |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 843 | |
| 844 | set_zone_contiguous(zone); |
| 845 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | EXPORT_SYMBOL_GPL(__remove_pages); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 849 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 850 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 851 | int set_online_page_callback(online_page_callback_t callback) |
| 852 | { |
| 853 | int rc = -EINVAL; |
| 854 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 855 | get_online_mems(); |
| 856 | mutex_lock(&online_page_callback_lock); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 857 | |
| 858 | if (online_page_callback == generic_online_page) { |
| 859 | online_page_callback = callback; |
| 860 | rc = 0; |
| 861 | } |
| 862 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 863 | mutex_unlock(&online_page_callback_lock); |
| 864 | put_online_mems(); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 865 | |
| 866 | return rc; |
| 867 | } |
| 868 | EXPORT_SYMBOL_GPL(set_online_page_callback); |
| 869 | |
| 870 | int restore_online_page_callback(online_page_callback_t callback) |
| 871 | { |
| 872 | int rc = -EINVAL; |
| 873 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 874 | get_online_mems(); |
| 875 | mutex_lock(&online_page_callback_lock); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 876 | |
| 877 | if (online_page_callback == callback) { |
| 878 | online_page_callback = generic_online_page; |
| 879 | rc = 0; |
| 880 | } |
| 881 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 882 | mutex_unlock(&online_page_callback_lock); |
| 883 | put_online_mems(); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 884 | |
| 885 | return rc; |
| 886 | } |
| 887 | EXPORT_SYMBOL_GPL(restore_online_page_callback); |
| 888 | |
| 889 | void __online_page_set_limits(struct page *page) |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 890 | { |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 891 | } |
| 892 | EXPORT_SYMBOL_GPL(__online_page_set_limits); |
| 893 | |
| 894 | void __online_page_increment_counters(struct page *page) |
| 895 | { |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 896 | adjust_managed_page_count(page, 1); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 897 | } |
| 898 | EXPORT_SYMBOL_GPL(__online_page_increment_counters); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 899 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 900 | void __online_page_free(struct page *page) |
| 901 | { |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 902 | __free_reserved_page(page); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 903 | } |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 904 | EXPORT_SYMBOL_GPL(__online_page_free); |
| 905 | |
| 906 | static void generic_online_page(struct page *page) |
| 907 | { |
| 908 | __online_page_set_limits(page); |
| 909 | __online_page_increment_counters(page); |
| 910 | __online_page_free(page); |
| 911 | } |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 912 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 913 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
| 914 | void *arg) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 915 | { |
| 916 | unsigned long i; |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 917 | unsigned long onlined_pages = *(unsigned long *)arg; |
| 918 | struct page *page; |
| 919 | if (PageReserved(pfn_to_page(start_pfn))) |
| 920 | for (i = 0; i < nr_pages; i++) { |
| 921 | page = pfn_to_page(start_pfn + i); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 922 | (*online_page_callback)(page); |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 923 | onlined_pages++; |
| 924 | } |
| 925 | *(unsigned long *)arg = onlined_pages; |
| 926 | return 0; |
| 927 | } |
| 928 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 929 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 930 | /* |
| 931 | * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have |
| 932 | * normal memory. |
| 933 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 934 | static bool can_online_high_movable(struct zone *zone) |
| 935 | { |
| 936 | return true; |
| 937 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 938 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 939 | /* ensure every online node has NORMAL memory */ |
| 940 | static bool can_online_high_movable(struct zone *zone) |
| 941 | { |
| 942 | return node_state(zone_to_nid(zone), N_NORMAL_MEMORY); |
| 943 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 944 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 945 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 946 | /* check which state of node_states will be changed when online memory */ |
| 947 | static void node_states_check_changes_online(unsigned long nr_pages, |
| 948 | struct zone *zone, struct memory_notify *arg) |
| 949 | { |
| 950 | int nid = zone_to_nid(zone); |
| 951 | enum zone_type zone_last = ZONE_NORMAL; |
| 952 | |
| 953 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 954 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 955 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 956 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 957 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 958 | * If we don't have HIGHMEM nor movable node, |
| 959 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 960 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 961 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 962 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 963 | zone_last = ZONE_MOVABLE; |
| 964 | |
| 965 | /* |
| 966 | * if the memory to be online is in a zone of 0...zone_last, and |
| 967 | * the zones of 0...zone_last don't have memory before online, we will |
| 968 | * need to set the node to node_states[N_NORMAL_MEMORY] after |
| 969 | * the memory is online. |
| 970 | */ |
| 971 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY)) |
| 972 | arg->status_change_nid_normal = nid; |
| 973 | else |
| 974 | arg->status_change_nid_normal = -1; |
| 975 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 976 | #ifdef CONFIG_HIGHMEM |
| 977 | /* |
| 978 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 979 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 980 | * set zone_last to ZONE_HIGHMEM. |
| 981 | * |
| 982 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 983 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 984 | * set zone_last to ZONE_MOVABLE. |
| 985 | */ |
| 986 | zone_last = ZONE_HIGHMEM; |
| 987 | if (N_MEMORY == N_HIGH_MEMORY) |
| 988 | zone_last = ZONE_MOVABLE; |
| 989 | |
| 990 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY)) |
| 991 | arg->status_change_nid_high = nid; |
| 992 | else |
| 993 | arg->status_change_nid_high = -1; |
| 994 | #else |
| 995 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 996 | #endif |
| 997 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 998 | /* |
| 999 | * if the node don't have memory befor online, we will need to |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1000 | * set the node to node_states[N_MEMORY] after the memory |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1001 | * is online. |
| 1002 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1003 | if (!node_state(nid, N_MEMORY)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1004 | arg->status_change_nid = nid; |
| 1005 | else |
| 1006 | arg->status_change_nid = -1; |
| 1007 | } |
| 1008 | |
| 1009 | static void node_states_set_node(int node, struct memory_notify *arg) |
| 1010 | { |
| 1011 | if (arg->status_change_nid_normal >= 0) |
| 1012 | node_set_state(node, N_NORMAL_MEMORY); |
| 1013 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1014 | if (arg->status_change_nid_high >= 0) |
| 1015 | node_set_state(node, N_HIGH_MEMORY); |
| 1016 | |
| 1017 | node_set_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 1020 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1021 | /* Must be protected by mem_hotplug_begin() */ |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 1022 | int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 1023 | { |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 1024 | unsigned long flags; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1025 | unsigned long onlined_pages = 0; |
| 1026 | struct zone *zone; |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1027 | int need_zonelists_rebuild = 0; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1028 | int nid; |
| 1029 | int ret; |
| 1030 | struct memory_notify arg; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1031 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1032 | /* |
| 1033 | * This doesn't need a lock to do pfn_to_page(). |
| 1034 | * The section can't be removed here because of the |
| 1035 | * memory_block->state_mutex. |
| 1036 | */ |
| 1037 | zone = page_zone(pfn_to_page(pfn)); |
| 1038 | |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 1039 | if ((zone_idx(zone) > ZONE_NORMAL || |
| 1040 | online_type == MMOP_ONLINE_MOVABLE) && |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1041 | !can_online_high_movable(zone)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1042 | return -EINVAL; |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1043 | |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 1044 | if (online_type == MMOP_ONLINE_KERNEL && |
| 1045 | zone_idx(zone) == ZONE_MOVABLE) { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1046 | if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1047 | return -EINVAL; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 1048 | } |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 1049 | if (online_type == MMOP_ONLINE_MOVABLE && |
| 1050 | zone_idx(zone) == ZONE_MOVABLE - 1) { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1051 | if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1052 | return -EINVAL; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* Previous code may changed the zone of the pfn range */ |
| 1056 | zone = page_zone(pfn_to_page(pfn)); |
| 1057 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1058 | arg.start_pfn = pfn; |
| 1059 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1060 | node_states_check_changes_online(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1061 | |
Vlastimil Babka | e888ca3 | 2016-03-17 14:18:12 -0700 | [diff] [blame] | 1062 | nid = zone_to_nid(zone); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
| 1065 | ret = notifier_to_errno(ret); |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1066 | if (ret) |
| 1067 | goto failed_addition; |
| 1068 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1069 | /* |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1070 | * If this zone is not populated, then it is not in zonelist. |
| 1071 | * This means the page allocator ignores this zone. |
| 1072 | * So, zonelist must be updated after online. |
| 1073 | */ |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1074 | mutex_lock(&zonelists_mutex); |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1075 | if (!populated_zone(zone)) { |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1076 | need_zonelists_rebuild = 1; |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1077 | build_all_zonelists(NULL, zone); |
| 1078 | } |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1079 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1080 | ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 1081 | online_pages_range); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 1082 | if (ret) { |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1083 | if (need_zonelists_rebuild) |
| 1084 | zone_pcp_reset(zone); |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1085 | mutex_unlock(&zonelists_mutex); |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1086 | goto failed_addition; |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1089 | zone->present_pages += onlined_pages; |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 1090 | |
| 1091 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
Yasunori Goto | f2937be | 2006-03-09 17:33:51 -0800 | [diff] [blame] | 1092 | zone->zone_pgdat->node_present_pages += onlined_pages; |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 1093 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
| 1094 | |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1095 | if (onlined_pages) { |
Vlastimil Babka | e888ca3 | 2016-03-17 14:18:12 -0700 | [diff] [blame] | 1096 | node_states_set_node(nid, &arg); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1097 | if (need_zonelists_rebuild) |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1098 | build_all_zonelists(NULL, NULL); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1099 | else |
| 1100 | zone_pcp_update(zone); |
| 1101 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1102 | |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1103 | mutex_unlock(&zonelists_mutex); |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1104 | |
| 1105 | init_per_zone_wmark_min(); |
| 1106 | |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1107 | if (onlined_pages) { |
Vlastimil Babka | e888ca3 | 2016-03-17 14:18:12 -0700 | [diff] [blame] | 1108 | kswapd_run(nid); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1109 | kcompactd_run(nid); |
| 1110 | } |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 1111 | |
Haicheng Li | 1f52250 | 2010-05-24 14:32:51 -0700 | [diff] [blame] | 1112 | vm_total_pages = nr_free_pagecache_pages(); |
Kent Liu | 2f7f24e | 2008-07-23 21:28:18 -0700 | [diff] [blame] | 1113 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 1114 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (onlined_pages) |
| 1117 | memory_notify(MEM_ONLINE, &arg); |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1118 | return 0; |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1119 | |
| 1120 | failed_addition: |
| 1121 | pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", |
| 1122 | (unsigned long long) pfn << PAGE_SHIFT, |
| 1123 | (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); |
| 1124 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
| 1125 | return ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1126 | } |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 1127 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1128 | |
Tang Chen | 0bd8542 | 2014-11-13 15:19:41 -0800 | [diff] [blame] | 1129 | static void reset_node_present_pages(pg_data_t *pgdat) |
| 1130 | { |
| 1131 | struct zone *z; |
| 1132 | |
| 1133 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
| 1134 | z->present_pages = 0; |
| 1135 | |
| 1136 | pgdat->node_present_pages = 0; |
| 1137 | } |
| 1138 | |
Hidetoshi Seto | e131933 | 2009-11-17 14:06:18 -0800 | [diff] [blame] | 1139 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 1140 | static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1141 | { |
| 1142 | struct pglist_data *pgdat; |
| 1143 | unsigned long zones_size[MAX_NR_ZONES] = {0}; |
| 1144 | unsigned long zholes_size[MAX_NR_ZONES] = {0}; |
Fabian Frederick | c8e861a | 2014-06-04 16:07:51 -0700 | [diff] [blame] | 1145 | unsigned long start_pfn = PFN_DOWN(start); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1146 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1147 | pgdat = NODE_DATA(nid); |
| 1148 | if (!pgdat) { |
| 1149 | pgdat = arch_alloc_nodedata(nid); |
| 1150 | if (!pgdat) |
| 1151 | return NULL; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1152 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1153 | arch_refresh_nodedata(nid, pgdat); |
Gu Zheng | b0dc3a3 | 2015-03-25 15:55:20 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | /* Reset the nr_zones and classzone_idx to 0 before reuse */ |
| 1156 | pgdat->nr_zones = 0; |
| 1157 | pgdat->classzone_idx = 0; |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1158 | } |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1159 | |
| 1160 | /* we can use NODE_DATA(nid) from here */ |
| 1161 | |
| 1162 | /* init node's zones as empty zones, we don't have any present pages.*/ |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 1163 | free_area_init_node(nid, zones_size, start_pfn, zholes_size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1164 | |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * The node we allocated has no zone fallback lists. For avoiding |
| 1167 | * to access not-initialized zonelist, build here. |
| 1168 | */ |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 1169 | mutex_lock(&zonelists_mutex); |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 1170 | build_all_zonelists(pgdat, NULL); |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 1171 | mutex_unlock(&zonelists_mutex); |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 1172 | |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 1173 | /* |
| 1174 | * zone->managed_pages is set to an approximate value in |
| 1175 | * free_area_init_core(), which will cause |
| 1176 | * /sys/device/system/node/nodeX/meminfo has wrong data. |
| 1177 | * So reset it to 0 before any memory is onlined. |
| 1178 | */ |
| 1179 | reset_node_managed_pages(pgdat); |
| 1180 | |
Tang Chen | 0bd8542 | 2014-11-13 15:19:41 -0800 | [diff] [blame] | 1181 | /* |
| 1182 | * When memory is hot-added, all the memory is in offline state. So |
| 1183 | * clear all zones' present_pages because they will be updated in |
| 1184 | * online_pages() and offline_pages(). |
| 1185 | */ |
| 1186 | reset_node_present_pages(pgdat); |
| 1187 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1188 | return pgdat; |
| 1189 | } |
| 1190 | |
| 1191 | static void rollback_node_hotadd(int nid, pg_data_t *pgdat) |
| 1192 | { |
| 1193 | arch_refresh_nodedata(nid, NULL); |
| 1194 | arch_free_nodedata(pgdat); |
| 1195 | return; |
| 1196 | } |
| 1197 | |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 1198 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1199 | /** |
| 1200 | * try_online_node - online a node if offlined |
| 1201 | * |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1202 | * called by cpu_up() to online a node without onlined memory. |
| 1203 | */ |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1204 | int try_online_node(int nid) |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1205 | { |
| 1206 | pg_data_t *pgdat; |
| 1207 | int ret; |
| 1208 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1209 | if (node_online(nid)) |
| 1210 | return 0; |
| 1211 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1212 | mem_hotplug_begin(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1213 | pgdat = hotadd_new_pgdat(nid, 0); |
David Rientjes | 7553e8f | 2011-06-22 18:13:01 -0700 | [diff] [blame] | 1214 | if (!pgdat) { |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1215 | pr_err("Cannot online node %d due to NULL pgdat\n", nid); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1216 | ret = -ENOMEM; |
| 1217 | goto out; |
| 1218 | } |
| 1219 | node_set_online(nid); |
| 1220 | ret = register_one_node(nid); |
| 1221 | BUG_ON(ret); |
| 1222 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1223 | if (pgdat->node_zonelists->_zonerefs->zone == NULL) { |
| 1224 | mutex_lock(&zonelists_mutex); |
| 1225 | build_all_zonelists(NULL, NULL); |
| 1226 | mutex_unlock(&zonelists_mutex); |
| 1227 | } |
| 1228 | |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1229 | out: |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1230 | mem_hotplug_done(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1231 | return ret; |
| 1232 | } |
| 1233 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1234 | static int check_hotplug_memory_range(u64 start, u64 size) |
| 1235 | { |
Fabian Frederick | c8e861a | 2014-06-04 16:07:51 -0700 | [diff] [blame] | 1236 | u64 start_pfn = PFN_DOWN(start); |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1237 | u64 nr_pages = size >> PAGE_SHIFT; |
| 1238 | |
| 1239 | /* Memory range must be aligned with section */ |
| 1240 | if ((start_pfn & ~PAGE_SECTION_MASK) || |
| 1241 | (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) { |
| 1242 | pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n", |
| 1243 | (unsigned long long)start, |
| 1244 | (unsigned long long)size); |
| 1245 | return -EINVAL; |
| 1246 | } |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1251 | /* |
| 1252 | * If movable zone has already been setup, newly added memory should be check. |
| 1253 | * If its address is higher than movable zone, it should be added as movable. |
| 1254 | * Without this check, movable zone may overlap with other zone. |
| 1255 | */ |
| 1256 | static int should_add_memory_movable(int nid, u64 start, u64 size) |
| 1257 | { |
| 1258 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 1259 | pg_data_t *pgdat = NODE_DATA(nid); |
| 1260 | struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE; |
| 1261 | |
| 1262 | if (zone_is_empty(movable_zone)) |
| 1263 | return 0; |
| 1264 | |
| 1265 | if (movable_zone->zone_start_pfn <= start_pfn) |
| 1266 | return 1; |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1271 | int zone_for_memory(int nid, u64 start, u64 size, int zone_default, |
| 1272 | bool for_device) |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1273 | { |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1274 | #ifdef CONFIG_ZONE_DEVICE |
| 1275 | if (for_device) |
| 1276 | return ZONE_DEVICE; |
| 1277 | #endif |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1278 | if (should_add_memory_movable(nid, start, size)) |
| 1279 | return ZONE_MOVABLE; |
| 1280 | |
| 1281 | return zone_default; |
| 1282 | } |
| 1283 | |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 1284 | static int online_memory_block(struct memory_block *mem, void *arg) |
| 1285 | { |
| 1286 | return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
| 1287 | } |
| 1288 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 1289 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 1290 | int __ref add_memory_resource(int nid, struct resource *res, bool online) |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1291 | { |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1292 | u64 start, size; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1293 | pg_data_t *pgdat = NULL; |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1294 | bool new_pgdat; |
| 1295 | bool new_node; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1296 | int ret; |
| 1297 | |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1298 | start = res->start; |
| 1299 | size = resource_size(res); |
| 1300 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1301 | ret = check_hotplug_memory_range(start, size); |
| 1302 | if (ret) |
| 1303 | return ret; |
| 1304 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1305 | { /* Stupid hack to suppress address-never-null warning */ |
| 1306 | void *p = NODE_DATA(nid); |
| 1307 | new_pgdat = !p; |
| 1308 | } |
Nathan Zimmer | ac13c46 | 2014-01-23 15:53:26 -0800 | [diff] [blame] | 1309 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1310 | mem_hotplug_begin(); |
Nathan Zimmer | ac13c46 | 2014-01-23 15:53:26 -0800 | [diff] [blame] | 1311 | |
Tang Chen | 7f36e3e | 2015-09-04 15:42:32 -0700 | [diff] [blame] | 1312 | /* |
| 1313 | * Add new range to memblock so that when hotadd_new_pgdat() is called |
| 1314 | * to allocate new pgdat, get_pfn_range_for_nid() will be able to find |
| 1315 | * this new range and calculate total pages correctly. The range will |
| 1316 | * be removed at hot-remove time. |
| 1317 | */ |
| 1318 | memblock_add_node(start, size, nid); |
| 1319 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1320 | new_node = !node_online(nid); |
| 1321 | if (new_node) { |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1322 | pgdat = hotadd_new_pgdat(nid, start); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1323 | ret = -ENOMEM; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1324 | if (!pgdat) |
Wen Congyang | 41b9e2d | 2012-07-11 14:02:31 -0700 | [diff] [blame] | 1325 | goto error; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1328 | /* call arch's memory hotadd */ |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1329 | ret = arch_add_memory(nid, start, size, false); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1330 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1331 | if (ret < 0) |
| 1332 | goto error; |
| 1333 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 1334 | /* we online node here. we can't roll back from here. */ |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1335 | node_set_online(nid); |
| 1336 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1337 | if (new_node) { |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 1338 | ret = register_one_node(nid); |
| 1339 | /* |
| 1340 | * If sysfs file of new node can't create, cpu on the node |
| 1341 | * can't be hot-added. There is no rollback way now. |
| 1342 | * So, check by BUG_ON() to catch it reluctantly.. |
| 1343 | */ |
| 1344 | BUG_ON(ret); |
| 1345 | } |
| 1346 | |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 1347 | /* create new memmap entry */ |
| 1348 | firmware_map_add_hotplug(start, start + size, "System RAM"); |
| 1349 | |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 1350 | /* online pages if requested */ |
| 1351 | if (online) |
| 1352 | walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), |
| 1353 | NULL, online_memory_block); |
| 1354 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1355 | goto out; |
| 1356 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1357 | error: |
| 1358 | /* rollback pgdat allocation and others */ |
| 1359 | if (new_pgdat) |
| 1360 | rollback_node_hotadd(nid, pgdat); |
Tang Chen | 7f36e3e | 2015-09-04 15:42:32 -0700 | [diff] [blame] | 1361 | memblock_remove(start, size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1362 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1363 | out: |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1364 | mem_hotplug_done(); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1367 | EXPORT_SYMBOL_GPL(add_memory_resource); |
| 1368 | |
| 1369 | int __ref add_memory(int nid, u64 start, u64 size) |
| 1370 | { |
| 1371 | struct resource *res; |
| 1372 | int ret; |
| 1373 | |
| 1374 | res = register_memory_resource(start, size); |
Vitaly Kuznetsov | 6f754ba | 2016-01-14 15:21:55 -0800 | [diff] [blame] | 1375 | if (IS_ERR(res)) |
| 1376 | return PTR_ERR(res); |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1377 | |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 1378 | ret = add_memory_resource(nid, res, memhp_auto_online); |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1379 | if (ret < 0) |
| 1380 | release_memory_resource(res); |
| 1381 | return ret; |
| 1382 | } |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1383 | EXPORT_SYMBOL_GPL(add_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1384 | |
| 1385 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 1386 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1387 | * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy |
| 1388 | * set and the size of the free page is given by page_order(). Using this, |
| 1389 | * the function determines if the pageblock contains only free pages. |
| 1390 | * Due to buddy contraints, a free page at least the size of a pageblock will |
| 1391 | * be located at the start of the pageblock |
| 1392 | */ |
| 1393 | static inline int pageblock_free(struct page *page) |
| 1394 | { |
| 1395 | return PageBuddy(page) && page_order(page) >= pageblock_order; |
| 1396 | } |
| 1397 | |
| 1398 | /* Return the start of the next active pageblock after a given page */ |
| 1399 | static struct page *next_active_pageblock(struct page *page) |
| 1400 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1401 | /* Ensure the starting page is pageblock-aligned */ |
| 1402 | BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); |
| 1403 | |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1404 | /* If the entire pageblock is free, move to the end of free page */ |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 1405 | if (pageblock_free(page)) { |
| 1406 | int order; |
| 1407 | /* be careful. we don't have locks, page_order can be changed.*/ |
| 1408 | order = page_order(page); |
| 1409 | if ((order < MAX_ORDER) && (order >= pageblock_order)) |
| 1410 | return page + (1 << order); |
| 1411 | } |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1412 | |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 1413 | return page + pageblock_nr_pages; |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* Checks if this range of memory is likely to be hot-removable. */ |
Yaowei Bai | c98940f | 2016-05-19 17:11:26 -0700 | [diff] [blame] | 1417 | bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1418 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1419 | struct page *page = pfn_to_page(start_pfn); |
| 1420 | struct page *end_page = page + nr_pages; |
| 1421 | |
| 1422 | /* Check the starting page of each pageblock within the range */ |
| 1423 | for (; page < end_page; page = next_active_pageblock(page)) { |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 1424 | if (!is_pageblock_removable_nolock(page)) |
Yaowei Bai | c98940f | 2016-05-19 17:11:26 -0700 | [diff] [blame] | 1425 | return false; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 1426 | cond_resched(); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /* All pageblocks in the memory block are likely to be hot-removable */ |
Yaowei Bai | c98940f | 2016-05-19 17:11:26 -0700 | [diff] [blame] | 1430 | return true; |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1434 | * Confirm all pages in a range [start, end) is belongs to the same zone. |
| 1435 | */ |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 1436 | int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1437 | { |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame] | 1438 | unsigned long pfn, sec_end_pfn; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1439 | struct zone *zone = NULL; |
| 1440 | struct page *page; |
| 1441 | int i; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame] | 1442 | for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1443 | pfn < end_pfn; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame] | 1444 | pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) { |
| 1445 | /* Make sure the memory section is present first */ |
| 1446 | if (!present_section_nr(pfn_to_section_nr(pfn))) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1447 | continue; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame] | 1448 | for (; pfn < sec_end_pfn && pfn < end_pfn; |
| 1449 | pfn += MAX_ORDER_NR_PAGES) { |
| 1450 | i = 0; |
| 1451 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ |
| 1452 | while ((i < MAX_ORDER_NR_PAGES) && |
| 1453 | !pfn_valid_within(pfn + i)) |
| 1454 | i++; |
| 1455 | if (i == MAX_ORDER_NR_PAGES) |
| 1456 | continue; |
| 1457 | page = pfn_to_page(pfn + i); |
| 1458 | if (zone && page_zone(page) != zone) |
| 1459 | return 0; |
| 1460 | zone = page_zone(page); |
| 1461 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1462 | } |
| 1463 | return 1; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1467 | * Scan pfn range [start,end) to find movable/migratable pages (LRU pages |
| 1468 | * and hugepages). We scan pfn because it's much easier than scanning over |
| 1469 | * linked list. This function returns the pfn of the first found movable |
| 1470 | * page if it's found, otherwise 0. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1471 | */ |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1472 | static unsigned long scan_movable_pages(unsigned long start, unsigned long end) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1473 | { |
| 1474 | unsigned long pfn; |
| 1475 | struct page *page; |
| 1476 | for (pfn = start; pfn < end; pfn++) { |
| 1477 | if (pfn_valid(pfn)) { |
| 1478 | page = pfn_to_page(pfn); |
| 1479 | if (PageLRU(page)) |
| 1480 | return pfn; |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1481 | if (PageHuge(page)) { |
Naoya Horiguchi | 7e1f049 | 2015-04-15 16:14:41 -0700 | [diff] [blame] | 1482 | if (page_huge_active(page)) |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1483 | return pfn; |
| 1484 | else |
| 1485 | pfn = round_up(pfn + 1, |
| 1486 | 1 << compound_order(page)) - 1; |
| 1487 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1493 | #define NR_OFFLINE_AT_ONCE_PAGES (256) |
| 1494 | static int |
| 1495 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) |
| 1496 | { |
| 1497 | unsigned long pfn; |
| 1498 | struct page *page; |
| 1499 | int move_pages = NR_OFFLINE_AT_ONCE_PAGES; |
| 1500 | int not_managed = 0; |
| 1501 | int ret = 0; |
| 1502 | LIST_HEAD(source); |
| 1503 | |
| 1504 | for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { |
| 1505 | if (!pfn_valid(pfn)) |
| 1506 | continue; |
| 1507 | page = pfn_to_page(pfn); |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1508 | |
| 1509 | if (PageHuge(page)) { |
| 1510 | struct page *head = compound_head(page); |
| 1511 | pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1; |
| 1512 | if (compound_order(head) > PFN_SECTION_SHIFT) { |
| 1513 | ret = -EBUSY; |
| 1514 | break; |
| 1515 | } |
| 1516 | if (isolate_huge_page(page, &source)) |
| 1517 | move_pages -= 1 << compound_order(head); |
| 1518 | continue; |
| 1519 | } |
| 1520 | |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1521 | if (!get_page_unless_zero(page)) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1522 | continue; |
| 1523 | /* |
| 1524 | * We can skip free pages. And we can only deal with pages on |
| 1525 | * LRU. |
| 1526 | */ |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1527 | ret = isolate_lru_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1528 | if (!ret) { /* Success */ |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1529 | put_page(page); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1530 | list_add_tail(&page->lru, &source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1531 | move_pages--; |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1532 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 1533 | page_is_file_cache(page)); |
| 1534 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1535 | } else { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1536 | #ifdef CONFIG_DEBUG_VM |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1537 | pr_alert("removing pfn %lx from LRU failed\n", pfn); |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1538 | dump_page(page, "failed to remove from LRU"); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1539 | #endif |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1540 | put_page(page); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1541 | /* Because we don't have big zone->lock. we should |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1542 | check this again here. */ |
| 1543 | if (page_count(page)) { |
| 1544 | not_managed++; |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1545 | ret = -EBUSY; |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1546 | break; |
| 1547 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1548 | } |
| 1549 | } |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1550 | if (!list_empty(&source)) { |
| 1551 | if (not_managed) { |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1552 | putback_movable_pages(&source); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1553 | goto out; |
| 1554 | } |
Minchan Kim | 74c08f9 | 2012-10-08 16:32:54 -0700 | [diff] [blame] | 1555 | |
| 1556 | /* |
| 1557 | * alloc_migrate_target should be improooooved!! |
| 1558 | * migrate_pages returns # of failed pages. |
| 1559 | */ |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1560 | ret = migrate_pages(&source, alloc_migrate_target, NULL, 0, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1561 | MIGRATE_SYNC, MR_MEMORY_HOTPLUG); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1562 | if (ret) |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1563 | putback_movable_pages(&source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1564 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1565 | out: |
| 1566 | return ret; |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * remove from free_area[] and mark all as Reserved. |
| 1571 | */ |
| 1572 | static int |
| 1573 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, |
| 1574 | void *data) |
| 1575 | { |
| 1576 | __offline_isolated_pages(start, start + nr_pages); |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | static void |
| 1581 | offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 1582 | { |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1583 | walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1584 | offline_isolated_pages_cb); |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * Check all pages in range, recoreded as memory resource, are isolated. |
| 1589 | */ |
| 1590 | static int |
| 1591 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, |
| 1592 | void *data) |
| 1593 | { |
| 1594 | int ret; |
| 1595 | long offlined = *(long *)data; |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1596 | ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1597 | offlined = nr_pages; |
| 1598 | if (!ret) |
| 1599 | *(long *)data += offlined; |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
| 1603 | static long |
| 1604 | check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) |
| 1605 | { |
| 1606 | long offlined = 0; |
| 1607 | int ret; |
| 1608 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1609 | ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1610 | check_pages_isolated_cb); |
| 1611 | if (ret < 0) |
| 1612 | offlined = (long)ret; |
| 1613 | return offlined; |
| 1614 | } |
| 1615 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1616 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1617 | /* |
| 1618 | * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have |
| 1619 | * normal memory. |
| 1620 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1621 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1622 | { |
| 1623 | return true; |
| 1624 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1625 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1626 | /* ensure the node has NORMAL memory if it is still online */ |
| 1627 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1628 | { |
| 1629 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1630 | unsigned long present_pages = 0; |
| 1631 | enum zone_type zt; |
| 1632 | |
| 1633 | for (zt = 0; zt <= ZONE_NORMAL; zt++) |
| 1634 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1635 | |
| 1636 | if (present_pages > nr_pages) |
| 1637 | return true; |
| 1638 | |
| 1639 | present_pages = 0; |
| 1640 | for (; zt <= ZONE_MOVABLE; zt++) |
| 1641 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1642 | |
| 1643 | /* |
| 1644 | * we can't offline the last normal memory until all |
| 1645 | * higher memory is offlined. |
| 1646 | */ |
| 1647 | return present_pages == 0; |
| 1648 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1649 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1650 | |
Tang Chen | c532092 | 2013-11-12 15:08:10 -0800 | [diff] [blame] | 1651 | static int __init cmdline_parse_movable_node(char *p) |
| 1652 | { |
| 1653 | #ifdef CONFIG_MOVABLE_NODE |
| 1654 | /* |
| 1655 | * Memory used by the kernel cannot be hot-removed because Linux |
| 1656 | * cannot migrate the kernel pages. When memory hotplug is |
| 1657 | * enabled, we should prevent memblock from allocating memory |
| 1658 | * for the kernel. |
| 1659 | * |
| 1660 | * ACPI SRAT records all hotpluggable memory ranges. But before |
| 1661 | * SRAT is parsed, we don't know about it. |
| 1662 | * |
| 1663 | * The kernel image is loaded into memory at very early time. We |
| 1664 | * cannot prevent this anyway. So on NUMA system, we set any |
| 1665 | * node the kernel resides in as un-hotpluggable. |
| 1666 | * |
| 1667 | * Since on modern servers, one node could have double-digit |
| 1668 | * gigabytes memory, we can assume the memory around the kernel |
| 1669 | * image is also un-hotpluggable. So before SRAT is parsed, just |
| 1670 | * allocate memory near the kernel image to try the best to keep |
| 1671 | * the kernel away from hotpluggable memory. |
| 1672 | */ |
| 1673 | memblock_set_bottom_up(true); |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 1674 | movable_node_enabled = true; |
Tang Chen | c532092 | 2013-11-12 15:08:10 -0800 | [diff] [blame] | 1675 | #else |
| 1676 | pr_warn("movable_node option not supported\n"); |
| 1677 | #endif |
| 1678 | return 0; |
| 1679 | } |
| 1680 | early_param("movable_node", cmdline_parse_movable_node); |
| 1681 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1682 | /* check which state of node_states will be changed when offline memory */ |
| 1683 | static void node_states_check_changes_offline(unsigned long nr_pages, |
| 1684 | struct zone *zone, struct memory_notify *arg) |
| 1685 | { |
| 1686 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1687 | unsigned long present_pages = 0; |
| 1688 | enum zone_type zt, zone_last = ZONE_NORMAL; |
| 1689 | |
| 1690 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1691 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 1692 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 1693 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1694 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1695 | * If we don't have HIGHMEM nor movable node, |
| 1696 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 1697 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1698 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1699 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1700 | zone_last = ZONE_MOVABLE; |
| 1701 | |
| 1702 | /* |
| 1703 | * check whether node_states[N_NORMAL_MEMORY] will be changed. |
| 1704 | * If the memory to be offline is in a zone of 0...zone_last, |
| 1705 | * and it is the last present memory, 0...zone_last will |
| 1706 | * become empty after offline , thus we can determind we will |
| 1707 | * need to clear the node from node_states[N_NORMAL_MEMORY]. |
| 1708 | */ |
| 1709 | for (zt = 0; zt <= zone_last; zt++) |
| 1710 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1711 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1712 | arg->status_change_nid_normal = zone_to_nid(zone); |
| 1713 | else |
| 1714 | arg->status_change_nid_normal = -1; |
| 1715 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1716 | #ifdef CONFIG_HIGHMEM |
| 1717 | /* |
| 1718 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 1719 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 1720 | * set zone_last to ZONE_HIGHMEM. |
| 1721 | * |
| 1722 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 1723 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 1724 | * set zone_last to ZONE_MOVABLE. |
| 1725 | */ |
| 1726 | zone_last = ZONE_HIGHMEM; |
| 1727 | if (N_MEMORY == N_HIGH_MEMORY) |
| 1728 | zone_last = ZONE_MOVABLE; |
| 1729 | |
| 1730 | for (; zt <= zone_last; zt++) |
| 1731 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1732 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1733 | arg->status_change_nid_high = zone_to_nid(zone); |
| 1734 | else |
| 1735 | arg->status_change_nid_high = -1; |
| 1736 | #else |
| 1737 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 1738 | #endif |
| 1739 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1740 | /* |
| 1741 | * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE |
| 1742 | */ |
| 1743 | zone_last = ZONE_MOVABLE; |
| 1744 | |
| 1745 | /* |
| 1746 | * check whether node_states[N_HIGH_MEMORY] will be changed |
| 1747 | * If we try to offline the last present @nr_pages from the node, |
| 1748 | * we can determind we will need to clear the node from |
| 1749 | * node_states[N_HIGH_MEMORY]. |
| 1750 | */ |
| 1751 | for (; zt <= zone_last; zt++) |
| 1752 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1753 | if (nr_pages >= present_pages) |
| 1754 | arg->status_change_nid = zone_to_nid(zone); |
| 1755 | else |
| 1756 | arg->status_change_nid = -1; |
| 1757 | } |
| 1758 | |
| 1759 | static void node_states_clear_node(int node, struct memory_notify *arg) |
| 1760 | { |
| 1761 | if (arg->status_change_nid_normal >= 0) |
| 1762 | node_clear_state(node, N_NORMAL_MEMORY); |
| 1763 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1764 | if ((N_MEMORY != N_NORMAL_MEMORY) && |
| 1765 | (arg->status_change_nid_high >= 0)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1766 | node_clear_state(node, N_HIGH_MEMORY); |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1767 | |
| 1768 | if ((N_MEMORY != N_HIGH_MEMORY) && |
| 1769 | (arg->status_change_nid >= 0)) |
| 1770 | node_clear_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1773 | static int __ref __offline_pages(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1774 | unsigned long end_pfn, unsigned long timeout) |
| 1775 | { |
| 1776 | unsigned long pfn, nr_pages, expire; |
| 1777 | long offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1778 | int ret, drain, retry_max, node; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1779 | unsigned long flags; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1780 | struct zone *zone; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1781 | struct memory_notify arg; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1782 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1783 | /* at least, alignment against pageblock is necessary */ |
| 1784 | if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) |
| 1785 | return -EINVAL; |
| 1786 | if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) |
| 1787 | return -EINVAL; |
| 1788 | /* This makes hotplug much easier...and readable. |
| 1789 | we assume this for now. .*/ |
| 1790 | if (!test_pages_in_a_zone(start_pfn, end_pfn)) |
| 1791 | return -EINVAL; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1792 | |
| 1793 | zone = page_zone(pfn_to_page(start_pfn)); |
| 1794 | node = zone_to_nid(zone); |
| 1795 | nr_pages = end_pfn - start_pfn; |
| 1796 | |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1797 | if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1798 | return -EINVAL; |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1799 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1800 | /* set above range as isolated */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1801 | ret = start_isolate_page_range(start_pfn, end_pfn, |
| 1802 | MIGRATE_MOVABLE, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1803 | if (ret) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1804 | return ret; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1805 | |
| 1806 | arg.start_pfn = start_pfn; |
| 1807 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1808 | node_states_check_changes_offline(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1809 | |
| 1810 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); |
| 1811 | ret = notifier_to_errno(ret); |
| 1812 | if (ret) |
| 1813 | goto failed_removal; |
| 1814 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1815 | pfn = start_pfn; |
| 1816 | expire = jiffies + timeout; |
| 1817 | drain = 0; |
| 1818 | retry_max = 5; |
| 1819 | repeat: |
| 1820 | /* start memory hot removal */ |
| 1821 | ret = -EAGAIN; |
| 1822 | if (time_after(jiffies, expire)) |
| 1823 | goto failed_removal; |
| 1824 | ret = -EINTR; |
| 1825 | if (signal_pending(current)) |
| 1826 | goto failed_removal; |
| 1827 | ret = 0; |
| 1828 | if (drain) { |
| 1829 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1830 | cond_resched(); |
Vlastimil Babka | c055432 | 2014-12-10 15:43:10 -0800 | [diff] [blame] | 1831 | drain_all_pages(zone); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1834 | pfn = scan_movable_pages(start_pfn, end_pfn); |
| 1835 | if (pfn) { /* We have movable pages */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1836 | ret = do_migrate_range(pfn, end_pfn); |
| 1837 | if (!ret) { |
| 1838 | drain = 1; |
| 1839 | goto repeat; |
| 1840 | } else { |
| 1841 | if (ret < 0) |
| 1842 | if (--retry_max == 0) |
| 1843 | goto failed_removal; |
| 1844 | yield(); |
| 1845 | drain = 1; |
| 1846 | goto repeat; |
| 1847 | } |
| 1848 | } |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1849 | /* drain all zone's lru pagevec, this is asynchronous... */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1850 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1851 | yield(); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1852 | /* drain pcp pages, this is synchronous. */ |
Vlastimil Babka | c055432 | 2014-12-10 15:43:10 -0800 | [diff] [blame] | 1853 | drain_all_pages(zone); |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1854 | /* |
| 1855 | * dissolve free hugepages in the memory block before doing offlining |
| 1856 | * actually in order to make hugetlbfs's object counting consistent. |
| 1857 | */ |
| 1858 | dissolve_free_huge_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1859 | /* check again */ |
| 1860 | offlined_pages = check_pages_isolated(start_pfn, end_pfn); |
| 1861 | if (offlined_pages < 0) { |
| 1862 | ret = -EBUSY; |
| 1863 | goto failed_removal; |
| 1864 | } |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1865 | pr_info("Offlined Pages %ld\n", offlined_pages); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1866 | /* Ok, all of our target is isolated. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1867 | We cannot do rollback at this point. */ |
| 1868 | offline_isolated_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | dbc0e4c | 2007-11-14 16:59:12 -0800 | [diff] [blame] | 1869 | /* reset pagetype flags and makes migrate type to be MOVABLE */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1870 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1871 | /* removal success */ |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 1872 | adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1873 | zone->present_pages -= offlined_pages; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1874 | |
| 1875 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1876 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1877 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1878 | |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1879 | init_per_zone_wmark_min(); |
| 1880 | |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1881 | if (!populated_zone(zone)) { |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1882 | zone_pcp_reset(zone); |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1883 | mutex_lock(&zonelists_mutex); |
| 1884 | build_all_zonelists(NULL, NULL); |
| 1885 | mutex_unlock(&zonelists_mutex); |
| 1886 | } else |
| 1887 | zone_pcp_update(zone); |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1888 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1889 | node_states_clear_node(node, &arg); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1890 | if (arg.status_change_nid >= 0) { |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 1891 | kswapd_stop(node); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1892 | kcompactd_stop(node); |
| 1893 | } |
Minchan Kim | bce7394 | 2009-06-16 15:32:50 -0700 | [diff] [blame] | 1894 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1895 | vm_total_pages = nr_free_pagecache_pages(); |
| 1896 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | memory_notify(MEM_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1899 | return 0; |
| 1900 | |
| 1901 | failed_removal: |
Chen Yucong | e33e33b | 2016-03-17 14:19:35 -0700 | [diff] [blame] | 1902 | pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n", |
| 1903 | (unsigned long long) start_pfn << PAGE_SHIFT, |
| 1904 | ((unsigned long long) end_pfn << PAGE_SHIFT) - 1); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1905 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1906 | /* pushback to free area */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1907 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1908 | return ret; |
| 1909 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1910 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1911 | /* Must be protected by mem_hotplug_begin() */ |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1912 | int offline_pages(unsigned long start_pfn, unsigned long nr_pages) |
| 1913 | { |
| 1914 | return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ); |
| 1915 | } |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1916 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1917 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1918 | /** |
| 1919 | * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn) |
| 1920 | * @start_pfn: start pfn of the memory range |
Toshi Kani | e05c4bb | 2013-04-29 15:06:16 -0700 | [diff] [blame] | 1921 | * @end_pfn: end pfn of the memory range |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1922 | * @arg: argument passed to func |
| 1923 | * @func: callback for each memory section walked |
| 1924 | * |
| 1925 | * This function walks through all present mem sections in range |
| 1926 | * [start_pfn, end_pfn) and call func on each mem section. |
| 1927 | * |
| 1928 | * Returns the return value of func. |
| 1929 | */ |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1930 | int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1931 | void *arg, int (*func)(struct memory_block *, void *)) |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1932 | { |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1933 | struct memory_block *mem = NULL; |
| 1934 | struct mem_section *section; |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1935 | unsigned long pfn, section_nr; |
| 1936 | int ret; |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1937 | |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1938 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 1939 | section_nr = pfn_to_section_nr(pfn); |
| 1940 | if (!present_section_nr(section_nr)) |
| 1941 | continue; |
| 1942 | |
| 1943 | section = __nr_to_section(section_nr); |
| 1944 | /* same memblock? */ |
| 1945 | if (mem) |
| 1946 | if ((section_nr >= mem->start_section_nr) && |
| 1947 | (section_nr <= mem->end_section_nr)) |
| 1948 | continue; |
| 1949 | |
| 1950 | mem = find_memory_block_hinted(section, mem); |
| 1951 | if (!mem) |
| 1952 | continue; |
| 1953 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1954 | ret = func(mem, arg); |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1955 | if (ret) { |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1956 | kobject_put(&mem->dev.kobj); |
| 1957 | return ret; |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | if (mem) |
| 1962 | kobject_put(&mem->dev.kobj); |
| 1963 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
| 1966 | |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1967 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Xishi Qiu | d6de9d5 | 2013-11-12 15:07:20 -0800 | [diff] [blame] | 1968 | static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1969 | { |
| 1970 | int ret = !is_memblock_offlined(mem); |
| 1971 | |
Randy Dunlap | 349daa0 | 2013-04-29 15:08:49 -0700 | [diff] [blame] | 1972 | if (unlikely(ret)) { |
| 1973 | phys_addr_t beginpa, endpa; |
| 1974 | |
| 1975 | beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); |
| 1976 | endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1; |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 1977 | pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", |
Randy Dunlap | 349daa0 | 2013-04-29 15:08:49 -0700 | [diff] [blame] | 1978 | &beginpa, &endpa); |
| 1979 | } |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1980 | |
| 1981 | return ret; |
| 1982 | } |
| 1983 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1984 | static int check_cpu_on_node(pg_data_t *pgdat) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1985 | { |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1986 | int cpu; |
| 1987 | |
| 1988 | for_each_present_cpu(cpu) { |
| 1989 | if (cpu_to_node(cpu) == pgdat->node_id) |
| 1990 | /* |
| 1991 | * the cpu on this node isn't removed, and we can't |
| 1992 | * offline this node. |
| 1993 | */ |
| 1994 | return -EBUSY; |
| 1995 | } |
| 1996 | |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2000 | static void unmap_cpu_on_node(pg_data_t *pgdat) |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2001 | { |
| 2002 | #ifdef CONFIG_ACPI_NUMA |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2003 | int cpu; |
| 2004 | |
| 2005 | for_each_possible_cpu(cpu) |
| 2006 | if (cpu_to_node(cpu) == pgdat->node_id) |
| 2007 | numa_clear_node(cpu); |
| 2008 | #endif |
| 2009 | } |
| 2010 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2011 | static int check_and_unmap_cpu_on_node(pg_data_t *pgdat) |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2012 | { |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2013 | int ret; |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2014 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2015 | ret = check_cpu_on_node(pgdat); |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2016 | if (ret) |
| 2017 | return ret; |
| 2018 | |
| 2019 | /* |
| 2020 | * the node will be offlined when we come here, so we can clear |
| 2021 | * the cpu_to_node() now. |
| 2022 | */ |
| 2023 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2024 | unmap_cpu_on_node(pgdat); |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 2025 | return 0; |
| 2026 | } |
| 2027 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2028 | /** |
| 2029 | * try_offline_node |
| 2030 | * |
| 2031 | * Offline a node if all memory sections and cpus of the node are removed. |
| 2032 | * |
| 2033 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug |
| 2034 | * and online/offline operations before this call. |
| 2035 | */ |
Wen Congyang | 90b30cd | 2013-02-22 16:33:27 -0800 | [diff] [blame] | 2036 | void try_offline_node(int nid) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2037 | { |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2038 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2039 | unsigned long start_pfn = pgdat->node_start_pfn; |
| 2040 | unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2041 | unsigned long pfn; |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2042 | int i; |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2043 | |
| 2044 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 2045 | unsigned long section_nr = pfn_to_section_nr(pfn); |
| 2046 | |
| 2047 | if (!present_section_nr(section_nr)) |
| 2048 | continue; |
| 2049 | |
| 2050 | if (pfn_to_nid(pfn) != nid) |
| 2051 | continue; |
| 2052 | |
| 2053 | /* |
| 2054 | * some memory sections of this node are not removed, and we |
| 2055 | * can't offline node now. |
| 2056 | */ |
| 2057 | return; |
| 2058 | } |
| 2059 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2060 | if (check_and_unmap_cpu_on_node(pgdat)) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2061 | return; |
| 2062 | |
| 2063 | /* |
| 2064 | * all memory/cpu of this node are removed, we can offline this |
| 2065 | * node now. |
| 2066 | */ |
| 2067 | node_set_offline(nid); |
| 2068 | unregister_one_node(nid); |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2069 | |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2070 | /* free waittable in each zone */ |
| 2071 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 2072 | struct zone *zone = pgdat->node_zones + i; |
| 2073 | |
Jianguo Wu | ca4b3f3 | 2013-03-22 15:04:50 -0700 | [diff] [blame] | 2074 | /* |
| 2075 | * wait_table may be allocated from boot memory, |
| 2076 | * here only free if it's allocated by vmalloc. |
| 2077 | */ |
Gu Zheng | 85bd839 | 2015-06-10 11:14:43 -0700 | [diff] [blame] | 2078 | if (is_vmalloc_addr(zone->wait_table)) { |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2079 | vfree(zone->wait_table); |
Gu Zheng | 85bd839 | 2015-06-10 11:14:43 -0700 | [diff] [blame] | 2080 | zone->wait_table = NULL; |
| 2081 | } |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2082 | } |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2083 | } |
Wen Congyang | 90b30cd | 2013-02-22 16:33:27 -0800 | [diff] [blame] | 2084 | EXPORT_SYMBOL(try_offline_node); |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2085 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2086 | /** |
| 2087 | * remove_memory |
| 2088 | * |
| 2089 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug |
| 2090 | * and online/offline operations before this call, as required by |
| 2091 | * try_offline_node(). |
| 2092 | */ |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2093 | void __ref remove_memory(int nid, u64 start, u64 size) |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 2094 | { |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2095 | int ret; |
Wen Congyang | 993c1aa | 2013-02-22 16:32:50 -0800 | [diff] [blame] | 2096 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 2097 | BUG_ON(check_hotplug_memory_range(start, size)); |
| 2098 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2099 | mem_hotplug_begin(); |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2100 | |
| 2101 | /* |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2102 | * All memory blocks must be offlined before removing memory. Check |
| 2103 | * whether all memory blocks in question are offline and trigger a BUG() |
| 2104 | * if this is not the case. |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2105 | */ |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2106 | ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, |
Xishi Qiu | d6de9d5 | 2013-11-12 15:07:20 -0800 | [diff] [blame] | 2107 | check_memblock_offlined_cb); |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2108 | if (ret) |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2109 | BUG(); |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2110 | |
Yasuaki Ishimatsu | 46c66c4 | 2013-02-22 16:32:56 -0800 | [diff] [blame] | 2111 | /* remove memmap entry */ |
| 2112 | firmware_map_remove(start, start + size, "System RAM"); |
Xishi Qiu | f9126ab | 2015-08-14 15:35:16 -0700 | [diff] [blame] | 2113 | memblock_free(start, size); |
| 2114 | memblock_remove(start, size); |
Yasuaki Ishimatsu | 46c66c4 | 2013-02-22 16:32:56 -0800 | [diff] [blame] | 2115 | |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 2116 | arch_remove_memory(start, size); |
| 2117 | |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2118 | try_offline_node(nid); |
| 2119 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2120 | mem_hotplug_done(); |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 2121 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 2122 | EXPORT_SYMBOL_GPL(remove_memory); |
Rafael J. Wysocki | aba6efc | 2013-06-01 22:24:07 +0200 | [diff] [blame] | 2123 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |