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