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 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 107 | void mem_hotplug_begin(void) |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 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 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 122 | void mem_hotplug_done(void) |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 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)) |
Yaowei Bai | b171e40 | 2015-11-05 18:47:06 -0800 | [diff] [blame] | 342 | return init_currently_empty_zone(zone, start_pfn, num_pages); |
| 343 | |
Cody P Schafer | f6bbb78 | 2013-02-22 16:35:30 -0800 | [diff] [blame] | 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; |
Mel Gorman | e298ff7 | 2015-08-06 15:46:51 -0700 | [diff] [blame] | 449 | unsigned long flags, pfn; |
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); |
Mel Gorman | e298ff7 | 2015-08-06 15:46:51 -0700 | [diff] [blame] | 464 | |
| 465 | /* online_page_range is called later and expects pages reserved */ |
| 466 | for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) { |
| 467 | if (!pfn_valid(pfn)) |
| 468 | continue; |
| 469 | |
| 470 | SetPageReserved(pfn_to_page(pfn)); |
| 471 | } |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 472 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 475 | static int __meminit __add_section(int nid, struct zone *zone, |
| 476 | unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 477 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 478 | int ret; |
| 479 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 480 | if (pfn_valid(phys_start_pfn)) |
| 481 | return -EEXIST; |
| 482 | |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 483 | ret = sparse_add_one_section(zone, phys_start_pfn); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 484 | |
| 485 | if (ret < 0) |
| 486 | return ret; |
| 487 | |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 488 | ret = __add_zone(zone, phys_start_pfn); |
| 489 | |
| 490 | if (ret < 0) |
| 491 | return ret; |
| 492 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 493 | return register_new_memory(nid, __pfn_to_section(phys_start_pfn)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 494 | } |
| 495 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 496 | /* |
| 497 | * Reasonably generic function for adding memory. It is |
| 498 | * expected that archs that support memory hotplug will |
| 499 | * call this function after deciding the zone to which to |
| 500 | * add the new pages. |
| 501 | */ |
| 502 | int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn, |
| 503 | unsigned long nr_pages) |
| 504 | { |
| 505 | unsigned long i; |
| 506 | int err = 0; |
| 507 | int start_sec, end_sec; |
| 508 | /* during initialize mem_map, align hot-added range to section */ |
| 509 | start_sec = pfn_to_section_nr(phys_start_pfn); |
| 510 | end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); |
| 511 | |
| 512 | for (i = start_sec; i <= end_sec; i++) { |
Sheng Yong | 19c07d5 | 2015-04-14 15:44:54 -0700 | [diff] [blame] | 513 | err = __add_section(nid, zone, section_nr_to_pfn(i)); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * EEXIST is finally dealt with by ioresource collision |
| 517 | * check. see add_memory() => register_memory_resource() |
| 518 | * Warning will be printed if there is collision. |
| 519 | */ |
| 520 | if (err && (err != -EEXIST)) |
| 521 | break; |
| 522 | err = 0; |
| 523 | } |
Zhu Guihua | c435a39 | 2015-06-24 16:58:42 -0700 | [diff] [blame] | 524 | vmemmap_populate_print_last(); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 525 | |
| 526 | return err; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(__add_pages); |
| 529 | |
| 530 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 531 | /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ |
| 532 | static int find_smallest_section_pfn(int nid, struct zone *zone, |
| 533 | unsigned long start_pfn, |
| 534 | unsigned long end_pfn) |
| 535 | { |
| 536 | struct mem_section *ms; |
| 537 | |
| 538 | for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) { |
| 539 | ms = __pfn_to_section(start_pfn); |
| 540 | |
| 541 | if (unlikely(!valid_section(ms))) |
| 542 | continue; |
| 543 | |
| 544 | if (unlikely(pfn_to_nid(start_pfn) != nid)) |
| 545 | continue; |
| 546 | |
| 547 | if (zone && zone != page_zone(pfn_to_page(start_pfn))) |
| 548 | continue; |
| 549 | |
| 550 | return start_pfn; |
| 551 | } |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ |
| 557 | static int find_biggest_section_pfn(int nid, struct zone *zone, |
| 558 | unsigned long start_pfn, |
| 559 | unsigned long end_pfn) |
| 560 | { |
| 561 | struct mem_section *ms; |
| 562 | unsigned long pfn; |
| 563 | |
| 564 | /* pfn is the end pfn of a memory section. */ |
| 565 | pfn = end_pfn - 1; |
| 566 | for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) { |
| 567 | ms = __pfn_to_section(pfn); |
| 568 | |
| 569 | if (unlikely(!valid_section(ms))) |
| 570 | continue; |
| 571 | |
| 572 | if (unlikely(pfn_to_nid(pfn) != nid)) |
| 573 | continue; |
| 574 | |
| 575 | if (zone && zone != page_zone(pfn_to_page(pfn))) |
| 576 | continue; |
| 577 | |
| 578 | return pfn; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, |
| 585 | unsigned long end_pfn) |
| 586 | { |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 587 | unsigned long zone_start_pfn = zone->zone_start_pfn; |
| 588 | unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ |
| 589 | unsigned long zone_end_pfn = z; |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 590 | unsigned long pfn; |
| 591 | struct mem_section *ms; |
| 592 | int nid = zone_to_nid(zone); |
| 593 | |
| 594 | zone_span_writelock(zone); |
| 595 | if (zone_start_pfn == start_pfn) { |
| 596 | /* |
| 597 | * If the section is smallest section in the zone, it need |
| 598 | * shrink zone->zone_start_pfn and zone->zone_spanned_pages. |
| 599 | * In this case, we find second smallest valid mem_section |
| 600 | * for shrinking zone. |
| 601 | */ |
| 602 | pfn = find_smallest_section_pfn(nid, zone, end_pfn, |
| 603 | zone_end_pfn); |
| 604 | if (pfn) { |
| 605 | zone->zone_start_pfn = pfn; |
| 606 | zone->spanned_pages = zone_end_pfn - pfn; |
| 607 | } |
| 608 | } else if (zone_end_pfn == end_pfn) { |
| 609 | /* |
| 610 | * If the section is biggest section in the zone, it need |
| 611 | * shrink zone->spanned_pages. |
| 612 | * In this case, we find second biggest valid mem_section for |
| 613 | * shrinking zone. |
| 614 | */ |
| 615 | pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, |
| 616 | start_pfn); |
| 617 | if (pfn) |
| 618 | zone->spanned_pages = pfn - zone_start_pfn + 1; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * The section is not biggest or smallest mem_section in the zone, it |
| 623 | * only creates a hole in the zone. So in this case, we need not |
| 624 | * change the zone. But perhaps, the zone has only hole data. Thus |
| 625 | * it check the zone has only hole or not. |
| 626 | */ |
| 627 | pfn = zone_start_pfn; |
| 628 | for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) { |
| 629 | ms = __pfn_to_section(pfn); |
| 630 | |
| 631 | if (unlikely(!valid_section(ms))) |
| 632 | continue; |
| 633 | |
| 634 | if (page_zone(pfn_to_page(pfn)) != zone) |
| 635 | continue; |
| 636 | |
| 637 | /* If the section is current section, it continues the loop */ |
| 638 | if (start_pfn == pfn) |
| 639 | continue; |
| 640 | |
| 641 | /* If we find valid section, we have nothing to do */ |
| 642 | zone_span_writeunlock(zone); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | /* The zone has no valid section */ |
| 647 | zone->zone_start_pfn = 0; |
| 648 | zone->spanned_pages = 0; |
| 649 | zone_span_writeunlock(zone); |
| 650 | } |
| 651 | |
| 652 | static void shrink_pgdat_span(struct pglist_data *pgdat, |
| 653 | unsigned long start_pfn, unsigned long end_pfn) |
| 654 | { |
Xishi Qiu | 83285c7 | 2013-11-12 15:07:19 -0800 | [diff] [blame] | 655 | unsigned long pgdat_start_pfn = pgdat->node_start_pfn; |
| 656 | unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */ |
| 657 | unsigned long pgdat_end_pfn = p; |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 658 | unsigned long pfn; |
| 659 | struct mem_section *ms; |
| 660 | int nid = pgdat->node_id; |
| 661 | |
| 662 | if (pgdat_start_pfn == start_pfn) { |
| 663 | /* |
| 664 | * If the section is smallest section in the pgdat, it need |
| 665 | * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages. |
| 666 | * In this case, we find second smallest valid mem_section |
| 667 | * for shrinking zone. |
| 668 | */ |
| 669 | pfn = find_smallest_section_pfn(nid, NULL, end_pfn, |
| 670 | pgdat_end_pfn); |
| 671 | if (pfn) { |
| 672 | pgdat->node_start_pfn = pfn; |
| 673 | pgdat->node_spanned_pages = pgdat_end_pfn - pfn; |
| 674 | } |
| 675 | } else if (pgdat_end_pfn == end_pfn) { |
| 676 | /* |
| 677 | * If the section is biggest section in the pgdat, it need |
| 678 | * shrink pgdat->node_spanned_pages. |
| 679 | * In this case, we find second biggest valid mem_section for |
| 680 | * shrinking zone. |
| 681 | */ |
| 682 | pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn, |
| 683 | start_pfn); |
| 684 | if (pfn) |
| 685 | pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * If the section is not biggest or smallest mem_section in the pgdat, |
| 690 | * it only creates a hole in the pgdat. So in this case, we need not |
| 691 | * change the pgdat. |
| 692 | * But perhaps, the pgdat has only hole data. Thus it check the pgdat |
| 693 | * has only hole or not. |
| 694 | */ |
| 695 | pfn = pgdat_start_pfn; |
| 696 | for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) { |
| 697 | ms = __pfn_to_section(pfn); |
| 698 | |
| 699 | if (unlikely(!valid_section(ms))) |
| 700 | continue; |
| 701 | |
| 702 | if (pfn_to_nid(pfn) != nid) |
| 703 | continue; |
| 704 | |
| 705 | /* If the section is current section, it continues the loop */ |
| 706 | if (start_pfn == pfn) |
| 707 | continue; |
| 708 | |
| 709 | /* If we find valid section, we have nothing to do */ |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | /* The pgdat has no valid section */ |
| 714 | pgdat->node_start_pfn = 0; |
| 715 | pgdat->node_spanned_pages = 0; |
| 716 | } |
| 717 | |
| 718 | static void __remove_zone(struct zone *zone, unsigned long start_pfn) |
| 719 | { |
| 720 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 721 | int nr_pages = PAGES_PER_SECTION; |
| 722 | int zone_type; |
| 723 | unsigned long flags; |
| 724 | |
| 725 | zone_type = zone - pgdat->node_zones; |
| 726 | |
| 727 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 728 | shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); |
| 729 | shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages); |
| 730 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
| 731 | } |
| 732 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 733 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 734 | { |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 735 | unsigned long start_pfn; |
| 736 | int scn_nr; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 737 | int ret = -EINVAL; |
| 738 | |
| 739 | if (!valid_section(ms)) |
| 740 | return ret; |
| 741 | |
| 742 | ret = unregister_memory_section(ms); |
| 743 | if (ret) |
| 744 | return ret; |
| 745 | |
Yasuaki Ishimatsu | 815121d | 2013-02-22 16:33:12 -0800 | [diff] [blame] | 746 | scn_nr = __section_nr(ms); |
| 747 | start_pfn = section_nr_to_pfn(scn_nr); |
| 748 | __remove_zone(zone, start_pfn); |
| 749 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 750 | sparse_remove_one_section(zone, ms); |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 754 | /** |
| 755 | * __remove_pages() - remove sections of pages from a zone |
| 756 | * @zone: zone from which pages need to be removed |
| 757 | * @phys_start_pfn: starting pageframe (must be aligned to start of a section) |
| 758 | * @nr_pages: number of pages to remove (must be multiple of section size) |
| 759 | * |
| 760 | * Generic helper function to remove section mappings and sysfs entries |
| 761 | * for the section of the memory we are removing. Caller needs to make |
| 762 | * sure that pages are marked reserved and zones are adjust properly by |
| 763 | * calling offline_pages(). |
| 764 | */ |
| 765 | int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 766 | unsigned long nr_pages) |
| 767 | { |
Toshi Kani | fe74ebb | 2013-04-29 15:08:20 -0700 | [diff] [blame] | 768 | unsigned long i; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 769 | int sections_to_remove; |
Toshi Kani | fe74ebb | 2013-04-29 15:08:20 -0700 | [diff] [blame] | 770 | resource_size_t start, size; |
| 771 | int ret = 0; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * We can only remove entire sections |
| 775 | */ |
| 776 | BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); |
| 777 | BUG_ON(nr_pages % PAGES_PER_SECTION); |
| 778 | |
Toshi Kani | fe74ebb | 2013-04-29 15:08:20 -0700 | [diff] [blame] | 779 | start = phys_start_pfn << PAGE_SHIFT; |
| 780 | size = nr_pages * PAGE_SIZE; |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 781 | |
| 782 | /* in the ZONE_DEVICE case device driver owns the memory region */ |
| 783 | if (!is_dev_zone(zone)) |
| 784 | ret = release_mem_region_adjustable(&iomem_resource, start, size); |
Randy Dunlap | 348f9f0 | 2013-05-24 15:55:30 -0700 | [diff] [blame] | 785 | if (ret) { |
| 786 | resource_size_t endres = start + size - 1; |
| 787 | |
| 788 | pr_warn("Unable to release resource <%pa-%pa> (%d)\n", |
| 789 | &start, &endres, ret); |
| 790 | } |
Yasuaki Ishimatsu | d760afd | 2012-10-08 16:34:14 -0700 | [diff] [blame] | 791 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 792 | sections_to_remove = nr_pages / PAGES_PER_SECTION; |
| 793 | for (i = 0; i < sections_to_remove; i++) { |
| 794 | unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; |
| 795 | ret = __remove_section(zone, __pfn_to_section(pfn)); |
| 796 | if (ret) |
| 797 | break; |
| 798 | } |
| 799 | return ret; |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(__remove_pages); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 802 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 803 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 804 | int set_online_page_callback(online_page_callback_t callback) |
| 805 | { |
| 806 | int rc = -EINVAL; |
| 807 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 808 | get_online_mems(); |
| 809 | mutex_lock(&online_page_callback_lock); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 810 | |
| 811 | if (online_page_callback == generic_online_page) { |
| 812 | online_page_callback = callback; |
| 813 | rc = 0; |
| 814 | } |
| 815 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 816 | mutex_unlock(&online_page_callback_lock); |
| 817 | put_online_mems(); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 818 | |
| 819 | return rc; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(set_online_page_callback); |
| 822 | |
| 823 | int restore_online_page_callback(online_page_callback_t callback) |
| 824 | { |
| 825 | int rc = -EINVAL; |
| 826 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 827 | get_online_mems(); |
| 828 | mutex_lock(&online_page_callback_lock); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 829 | |
| 830 | if (online_page_callback == callback) { |
| 831 | online_page_callback = generic_online_page; |
| 832 | rc = 0; |
| 833 | } |
| 834 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 835 | mutex_unlock(&online_page_callback_lock); |
| 836 | put_online_mems(); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 837 | |
| 838 | return rc; |
| 839 | } |
| 840 | EXPORT_SYMBOL_GPL(restore_online_page_callback); |
| 841 | |
| 842 | void __online_page_set_limits(struct page *page) |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 843 | { |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 844 | } |
| 845 | EXPORT_SYMBOL_GPL(__online_page_set_limits); |
| 846 | |
| 847 | void __online_page_increment_counters(struct page *page) |
| 848 | { |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 849 | adjust_managed_page_count(page, 1); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 850 | } |
| 851 | EXPORT_SYMBOL_GPL(__online_page_increment_counters); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 852 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 853 | void __online_page_free(struct page *page) |
| 854 | { |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 855 | __free_reserved_page(page); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 856 | } |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 857 | EXPORT_SYMBOL_GPL(__online_page_free); |
| 858 | |
| 859 | static void generic_online_page(struct page *page) |
| 860 | { |
| 861 | __online_page_set_limits(page); |
| 862 | __online_page_increment_counters(page); |
| 863 | __online_page_free(page); |
| 864 | } |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 865 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 866 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
| 867 | void *arg) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 868 | { |
| 869 | unsigned long i; |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 870 | unsigned long onlined_pages = *(unsigned long *)arg; |
| 871 | struct page *page; |
| 872 | if (PageReserved(pfn_to_page(start_pfn))) |
| 873 | for (i = 0; i < nr_pages; i++) { |
| 874 | page = pfn_to_page(start_pfn + i); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 875 | (*online_page_callback)(page); |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 876 | onlined_pages++; |
| 877 | } |
| 878 | *(unsigned long *)arg = onlined_pages; |
| 879 | return 0; |
| 880 | } |
| 881 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 882 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 883 | /* |
| 884 | * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have |
| 885 | * normal memory. |
| 886 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 887 | static bool can_online_high_movable(struct zone *zone) |
| 888 | { |
| 889 | return true; |
| 890 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 891 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 892 | /* ensure every online node has NORMAL memory */ |
| 893 | static bool can_online_high_movable(struct zone *zone) |
| 894 | { |
| 895 | return node_state(zone_to_nid(zone), N_NORMAL_MEMORY); |
| 896 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 897 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 898 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 899 | /* check which state of node_states will be changed when online memory */ |
| 900 | static void node_states_check_changes_online(unsigned long nr_pages, |
| 901 | struct zone *zone, struct memory_notify *arg) |
| 902 | { |
| 903 | int nid = zone_to_nid(zone); |
| 904 | enum zone_type zone_last = ZONE_NORMAL; |
| 905 | |
| 906 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 907 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 908 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 909 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 910 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 911 | * If we don't have HIGHMEM nor movable node, |
| 912 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 913 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 914 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 915 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 916 | zone_last = ZONE_MOVABLE; |
| 917 | |
| 918 | /* |
| 919 | * if the memory to be online is in a zone of 0...zone_last, and |
| 920 | * the zones of 0...zone_last don't have memory before online, we will |
| 921 | * need to set the node to node_states[N_NORMAL_MEMORY] after |
| 922 | * the memory is online. |
| 923 | */ |
| 924 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY)) |
| 925 | arg->status_change_nid_normal = nid; |
| 926 | else |
| 927 | arg->status_change_nid_normal = -1; |
| 928 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 929 | #ifdef CONFIG_HIGHMEM |
| 930 | /* |
| 931 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 932 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 933 | * set zone_last to ZONE_HIGHMEM. |
| 934 | * |
| 935 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 936 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 937 | * set zone_last to ZONE_MOVABLE. |
| 938 | */ |
| 939 | zone_last = ZONE_HIGHMEM; |
| 940 | if (N_MEMORY == N_HIGH_MEMORY) |
| 941 | zone_last = ZONE_MOVABLE; |
| 942 | |
| 943 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY)) |
| 944 | arg->status_change_nid_high = nid; |
| 945 | else |
| 946 | arg->status_change_nid_high = -1; |
| 947 | #else |
| 948 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 949 | #endif |
| 950 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 951 | /* |
| 952 | * 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] | 953 | * set the node to node_states[N_MEMORY] after the memory |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 954 | * is online. |
| 955 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 956 | if (!node_state(nid, N_MEMORY)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 957 | arg->status_change_nid = nid; |
| 958 | else |
| 959 | arg->status_change_nid = -1; |
| 960 | } |
| 961 | |
| 962 | static void node_states_set_node(int node, struct memory_notify *arg) |
| 963 | { |
| 964 | if (arg->status_change_nid_normal >= 0) |
| 965 | node_set_state(node, N_NORMAL_MEMORY); |
| 966 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 967 | if (arg->status_change_nid_high >= 0) |
| 968 | node_set_state(node, N_HIGH_MEMORY); |
| 969 | |
| 970 | node_set_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 971 | } |
| 972 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 973 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 974 | /* Must be protected by mem_hotplug_begin() */ |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 975 | 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] | 976 | { |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 977 | unsigned long flags; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 978 | unsigned long onlined_pages = 0; |
| 979 | struct zone *zone; |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 980 | int need_zonelists_rebuild = 0; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 981 | int nid; |
| 982 | int ret; |
| 983 | struct memory_notify arg; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 984 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 985 | /* |
| 986 | * This doesn't need a lock to do pfn_to_page(). |
| 987 | * The section can't be removed here because of the |
| 988 | * memory_block->state_mutex. |
| 989 | */ |
| 990 | zone = page_zone(pfn_to_page(pfn)); |
| 991 | |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 992 | if ((zone_idx(zone) > ZONE_NORMAL || |
| 993 | online_type == MMOP_ONLINE_MOVABLE) && |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 994 | !can_online_high_movable(zone)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 995 | return -EINVAL; |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 996 | |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 997 | if (online_type == MMOP_ONLINE_KERNEL && |
| 998 | zone_idx(zone) == ZONE_MOVABLE) { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 999 | if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1000 | return -EINVAL; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 1001 | } |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 1002 | if (online_type == MMOP_ONLINE_MOVABLE && |
| 1003 | zone_idx(zone) == ZONE_MOVABLE - 1) { |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1004 | if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1005 | return -EINVAL; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /* Previous code may changed the zone of the pfn range */ |
| 1009 | zone = page_zone(pfn_to_page(pfn)); |
| 1010 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1011 | arg.start_pfn = pfn; |
| 1012 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1013 | node_states_check_changes_online(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1014 | |
Xishi Qiu | 9c2606b | 2013-11-12 15:07:21 -0800 | [diff] [blame] | 1015 | nid = pfn_to_nid(pfn); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
| 1018 | ret = notifier_to_errno(ret); |
| 1019 | if (ret) { |
| 1020 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1021 | return ret; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1022 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1023 | /* |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1024 | * If this zone is not populated, then it is not in zonelist. |
| 1025 | * This means the page allocator ignores this zone. |
| 1026 | * So, zonelist must be updated after online. |
| 1027 | */ |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1028 | mutex_lock(&zonelists_mutex); |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1029 | if (!populated_zone(zone)) { |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1030 | need_zonelists_rebuild = 1; |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1031 | build_all_zonelists(NULL, zone); |
| 1032 | } |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 1033 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1034 | ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 1035 | online_pages_range); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 1036 | if (ret) { |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1037 | if (need_zonelists_rebuild) |
| 1038 | zone_pcp_reset(zone); |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1039 | mutex_unlock(&zonelists_mutex); |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 1040 | printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n", |
| 1041 | (unsigned long long) pfn << PAGE_SHIFT, |
| 1042 | (((unsigned long long) pfn + nr_pages) |
| 1043 | << PAGE_SHIFT) - 1); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 1044 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1045 | return ret; |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1048 | zone->present_pages += onlined_pages; |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 1049 | |
| 1050 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
Yasunori Goto | f2937be | 2006-03-09 17:33:51 -0800 | [diff] [blame] | 1051 | zone->zone_pgdat->node_present_pages += onlined_pages; |
Cody P Schafer | aa47228 | 2013-07-03 15:02:10 -0700 | [diff] [blame] | 1052 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
| 1053 | |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1054 | if (onlined_pages) { |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1055 | node_states_set_node(zone_to_nid(zone), &arg); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1056 | if (need_zonelists_rebuild) |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 1057 | build_all_zonelists(NULL, NULL); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1058 | else |
| 1059 | zone_pcp_update(zone); |
| 1060 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1061 | |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 1062 | mutex_unlock(&zonelists_mutex); |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1063 | |
| 1064 | init_per_zone_wmark_min(); |
| 1065 | |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 1066 | if (onlined_pages) |
Christoph Lameter | 7ea1530 | 2007-10-16 01:25:29 -0700 | [diff] [blame] | 1067 | kswapd_run(zone_to_nid(zone)); |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 1068 | |
Haicheng Li | 1f52250 | 2010-05-24 14:32:51 -0700 | [diff] [blame] | 1069 | vm_total_pages = nr_free_pagecache_pages(); |
Kent Liu | 2f7f24e | 2008-07-23 21:28:18 -0700 | [diff] [blame] | 1070 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 1071 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (onlined_pages) |
| 1074 | memory_notify(MEM_ONLINE, &arg); |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1075 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1076 | } |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 1077 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1078 | |
Tang Chen | 0bd8542 | 2014-11-13 15:19:41 -0800 | [diff] [blame] | 1079 | static void reset_node_present_pages(pg_data_t *pgdat) |
| 1080 | { |
| 1081 | struct zone *z; |
| 1082 | |
| 1083 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
| 1084 | z->present_pages = 0; |
| 1085 | |
| 1086 | pgdat->node_present_pages = 0; |
| 1087 | } |
| 1088 | |
Hidetoshi Seto | e131933 | 2009-11-17 14:06:18 -0800 | [diff] [blame] | 1089 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 1090 | static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1091 | { |
| 1092 | struct pglist_data *pgdat; |
| 1093 | unsigned long zones_size[MAX_NR_ZONES] = {0}; |
| 1094 | unsigned long zholes_size[MAX_NR_ZONES] = {0}; |
Fabian Frederick | c8e861a | 2014-06-04 16:07:51 -0700 | [diff] [blame] | 1095 | unsigned long start_pfn = PFN_DOWN(start); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1096 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1097 | pgdat = NODE_DATA(nid); |
| 1098 | if (!pgdat) { |
| 1099 | pgdat = arch_alloc_nodedata(nid); |
| 1100 | if (!pgdat) |
| 1101 | return NULL; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1102 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1103 | arch_refresh_nodedata(nid, pgdat); |
Gu Zheng | b0dc3a3 | 2015-03-25 15:55:20 -0700 | [diff] [blame] | 1104 | } else { |
| 1105 | /* Reset the nr_zones and classzone_idx to 0 before reuse */ |
| 1106 | pgdat->nr_zones = 0; |
| 1107 | pgdat->classzone_idx = 0; |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1108 | } |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1109 | |
| 1110 | /* we can use NODE_DATA(nid) from here */ |
| 1111 | |
| 1112 | /* 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] | 1113 | free_area_init_node(nid, zones_size, start_pfn, zholes_size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1114 | |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 1115 | /* |
| 1116 | * The node we allocated has no zone fallback lists. For avoiding |
| 1117 | * to access not-initialized zonelist, build here. |
| 1118 | */ |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 1119 | mutex_lock(&zonelists_mutex); |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 1120 | build_all_zonelists(pgdat, NULL); |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 1121 | mutex_unlock(&zonelists_mutex); |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 1122 | |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 1123 | /* |
| 1124 | * zone->managed_pages is set to an approximate value in |
| 1125 | * free_area_init_core(), which will cause |
| 1126 | * /sys/device/system/node/nodeX/meminfo has wrong data. |
| 1127 | * So reset it to 0 before any memory is onlined. |
| 1128 | */ |
| 1129 | reset_node_managed_pages(pgdat); |
| 1130 | |
Tang Chen | 0bd8542 | 2014-11-13 15:19:41 -0800 | [diff] [blame] | 1131 | /* |
| 1132 | * When memory is hot-added, all the memory is in offline state. So |
| 1133 | * clear all zones' present_pages because they will be updated in |
| 1134 | * online_pages() and offline_pages(). |
| 1135 | */ |
| 1136 | reset_node_present_pages(pgdat); |
| 1137 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1138 | return pgdat; |
| 1139 | } |
| 1140 | |
| 1141 | static void rollback_node_hotadd(int nid, pg_data_t *pgdat) |
| 1142 | { |
| 1143 | arch_refresh_nodedata(nid, NULL); |
| 1144 | arch_free_nodedata(pgdat); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 1148 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1149 | /** |
| 1150 | * try_online_node - online a node if offlined |
| 1151 | * |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1152 | * called by cpu_up() to online a node without onlined memory. |
| 1153 | */ |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1154 | int try_online_node(int nid) |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1155 | { |
| 1156 | pg_data_t *pgdat; |
| 1157 | int ret; |
| 1158 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1159 | if (node_online(nid)) |
| 1160 | return 0; |
| 1161 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1162 | mem_hotplug_begin(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1163 | pgdat = hotadd_new_pgdat(nid, 0); |
David Rientjes | 7553e8f | 2011-06-22 18:13:01 -0700 | [diff] [blame] | 1164 | if (!pgdat) { |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1165 | pr_err("Cannot online node %d due to NULL pgdat\n", nid); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1166 | ret = -ENOMEM; |
| 1167 | goto out; |
| 1168 | } |
| 1169 | node_set_online(nid); |
| 1170 | ret = register_one_node(nid); |
| 1171 | BUG_ON(ret); |
| 1172 | |
Toshi Kani | 01b0f19 | 2013-11-12 15:07:25 -0800 | [diff] [blame] | 1173 | if (pgdat->node_zonelists->_zonerefs->zone == NULL) { |
| 1174 | mutex_lock(&zonelists_mutex); |
| 1175 | build_all_zonelists(NULL, NULL); |
| 1176 | mutex_unlock(&zonelists_mutex); |
| 1177 | } |
| 1178 | |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1179 | out: |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1180 | mem_hotplug_done(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 1181 | return ret; |
| 1182 | } |
| 1183 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1184 | static int check_hotplug_memory_range(u64 start, u64 size) |
| 1185 | { |
Fabian Frederick | c8e861a | 2014-06-04 16:07:51 -0700 | [diff] [blame] | 1186 | u64 start_pfn = PFN_DOWN(start); |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1187 | u64 nr_pages = size >> PAGE_SHIFT; |
| 1188 | |
| 1189 | /* Memory range must be aligned with section */ |
| 1190 | if ((start_pfn & ~PAGE_SECTION_MASK) || |
| 1191 | (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) { |
| 1192 | pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n", |
| 1193 | (unsigned long long)start, |
| 1194 | (unsigned long long)size); |
| 1195 | return -EINVAL; |
| 1196 | } |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1201 | /* |
| 1202 | * If movable zone has already been setup, newly added memory should be check. |
| 1203 | * If its address is higher than movable zone, it should be added as movable. |
| 1204 | * Without this check, movable zone may overlap with other zone. |
| 1205 | */ |
| 1206 | static int should_add_memory_movable(int nid, u64 start, u64 size) |
| 1207 | { |
| 1208 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 1209 | pg_data_t *pgdat = NODE_DATA(nid); |
| 1210 | struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE; |
| 1211 | |
| 1212 | if (zone_is_empty(movable_zone)) |
| 1213 | return 0; |
| 1214 | |
| 1215 | if (movable_zone->zone_start_pfn <= start_pfn) |
| 1216 | return 1; |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1221 | int zone_for_memory(int nid, u64 start, u64 size, int zone_default, |
| 1222 | bool for_device) |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1223 | { |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1224 | #ifdef CONFIG_ZONE_DEVICE |
| 1225 | if (for_device) |
| 1226 | return ZONE_DEVICE; |
| 1227 | #endif |
Wang Nan | 6326440 | 2014-08-06 16:07:36 -0700 | [diff] [blame] | 1228 | if (should_add_memory_movable(nid, start, size)) |
| 1229 | return ZONE_MOVABLE; |
| 1230 | |
| 1231 | return zone_default; |
| 1232 | } |
| 1233 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 1234 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1235 | int __ref add_memory_resource(int nid, struct resource *res) |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1236 | { |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1237 | u64 start, size; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1238 | pg_data_t *pgdat = NULL; |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1239 | bool new_pgdat; |
| 1240 | bool new_node; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1241 | int ret; |
| 1242 | |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1243 | start = res->start; |
| 1244 | size = resource_size(res); |
| 1245 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 1246 | ret = check_hotplug_memory_range(start, size); |
| 1247 | if (ret) |
| 1248 | return ret; |
| 1249 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1250 | { /* Stupid hack to suppress address-never-null warning */ |
| 1251 | void *p = NODE_DATA(nid); |
| 1252 | new_pgdat = !p; |
| 1253 | } |
Nathan Zimmer | ac13c46 | 2014-01-23 15:53:26 -0800 | [diff] [blame] | 1254 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1255 | mem_hotplug_begin(); |
Nathan Zimmer | ac13c46 | 2014-01-23 15:53:26 -0800 | [diff] [blame] | 1256 | |
Tang Chen | 7f36e3e | 2015-09-04 15:42:32 -0700 | [diff] [blame] | 1257 | /* |
| 1258 | * Add new range to memblock so that when hotadd_new_pgdat() is called |
| 1259 | * to allocate new pgdat, get_pfn_range_for_nid() will be able to find |
| 1260 | * this new range and calculate total pages correctly. The range will |
| 1261 | * be removed at hot-remove time. |
| 1262 | */ |
| 1263 | memblock_add_node(start, size, nid); |
| 1264 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1265 | new_node = !node_online(nid); |
| 1266 | if (new_node) { |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1267 | pgdat = hotadd_new_pgdat(nid, start); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1268 | ret = -ENOMEM; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1269 | if (!pgdat) |
Wen Congyang | 41b9e2d | 2012-07-11 14:02:31 -0700 | [diff] [blame] | 1270 | goto error; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1273 | /* call arch's memory hotadd */ |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 1274 | ret = arch_add_memory(nid, start, size, false); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1275 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1276 | if (ret < 0) |
| 1277 | goto error; |
| 1278 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 1279 | /* we online node here. we can't roll back from here. */ |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1280 | node_set_online(nid); |
| 1281 | |
Tang Chen | a1e565a | 2013-02-22 16:33:18 -0800 | [diff] [blame] | 1282 | if (new_node) { |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 1283 | ret = register_one_node(nid); |
| 1284 | /* |
| 1285 | * If sysfs file of new node can't create, cpu on the node |
| 1286 | * can't be hot-added. There is no rollback way now. |
| 1287 | * So, check by BUG_ON() to catch it reluctantly.. |
| 1288 | */ |
| 1289 | BUG_ON(ret); |
| 1290 | } |
| 1291 | |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 1292 | /* create new memmap entry */ |
| 1293 | firmware_map_add_hotplug(start, start + size, "System RAM"); |
| 1294 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1295 | goto out; |
| 1296 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1297 | error: |
| 1298 | /* rollback pgdat allocation and others */ |
| 1299 | if (new_pgdat) |
| 1300 | rollback_node_hotadd(nid, pgdat); |
Tang Chen | 7f36e3e | 2015-09-04 15:42:32 -0700 | [diff] [blame] | 1301 | memblock_remove(start, size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 1302 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1303 | out: |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 1304 | mem_hotplug_done(); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1305 | return ret; |
| 1306 | } |
David Vrabel | 62cedb9 | 2015-06-25 16:35:49 +0100 | [diff] [blame] | 1307 | EXPORT_SYMBOL_GPL(add_memory_resource); |
| 1308 | |
| 1309 | int __ref add_memory(int nid, u64 start, u64 size) |
| 1310 | { |
| 1311 | struct resource *res; |
| 1312 | int ret; |
| 1313 | |
| 1314 | res = register_memory_resource(start, size); |
| 1315 | if (!res) |
| 1316 | return -EEXIST; |
| 1317 | |
| 1318 | ret = add_memory_resource(nid, res); |
| 1319 | if (ret < 0) |
| 1320 | release_memory_resource(res); |
| 1321 | return ret; |
| 1322 | } |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 1323 | EXPORT_SYMBOL_GPL(add_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1324 | |
| 1325 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 1326 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1327 | * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy |
| 1328 | * set and the size of the free page is given by page_order(). Using this, |
| 1329 | * the function determines if the pageblock contains only free pages. |
| 1330 | * Due to buddy contraints, a free page at least the size of a pageblock will |
| 1331 | * be located at the start of the pageblock |
| 1332 | */ |
| 1333 | static inline int pageblock_free(struct page *page) |
| 1334 | { |
| 1335 | return PageBuddy(page) && page_order(page) >= pageblock_order; |
| 1336 | } |
| 1337 | |
| 1338 | /* Return the start of the next active pageblock after a given page */ |
| 1339 | static struct page *next_active_pageblock(struct page *page) |
| 1340 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1341 | /* Ensure the starting page is pageblock-aligned */ |
| 1342 | BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); |
| 1343 | |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1344 | /* 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] | 1345 | if (pageblock_free(page)) { |
| 1346 | int order; |
| 1347 | /* be careful. we don't have locks, page_order can be changed.*/ |
| 1348 | order = page_order(page); |
| 1349 | if ((order < MAX_ORDER) && (order >= pageblock_order)) |
| 1350 | return page + (1 << order); |
| 1351 | } |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1352 | |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 1353 | return page + pageblock_nr_pages; |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /* Checks if this range of memory is likely to be hot-removable. */ |
| 1357 | int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) |
| 1358 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1359 | struct page *page = pfn_to_page(start_pfn); |
| 1360 | struct page *end_page = page + nr_pages; |
| 1361 | |
| 1362 | /* Check the starting page of each pageblock within the range */ |
| 1363 | for (; page < end_page; page = next_active_pageblock(page)) { |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 1364 | if (!is_pageblock_removable_nolock(page)) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1365 | return 0; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 1366 | cond_resched(); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* All pageblocks in the memory block are likely to be hot-removable */ |
| 1370 | return 1; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1374 | * Confirm all pages in a range [start, end) is belongs to the same zone. |
| 1375 | */ |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 1376 | 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] | 1377 | { |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame^] | 1378 | unsigned long pfn, sec_end_pfn; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1379 | struct zone *zone = NULL; |
| 1380 | struct page *page; |
| 1381 | int i; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame^] | 1382 | for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1383 | pfn < end_pfn; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame^] | 1384 | pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) { |
| 1385 | /* Make sure the memory section is present first */ |
| 1386 | if (!present_section_nr(pfn_to_section_nr(pfn))) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1387 | continue; |
Andrew Banman | 5f0f288 | 2015-12-29 14:54:25 -0800 | [diff] [blame^] | 1388 | for (; pfn < sec_end_pfn && pfn < end_pfn; |
| 1389 | pfn += MAX_ORDER_NR_PAGES) { |
| 1390 | i = 0; |
| 1391 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ |
| 1392 | while ((i < MAX_ORDER_NR_PAGES) && |
| 1393 | !pfn_valid_within(pfn + i)) |
| 1394 | i++; |
| 1395 | if (i == MAX_ORDER_NR_PAGES) |
| 1396 | continue; |
| 1397 | page = pfn_to_page(pfn + i); |
| 1398 | if (zone && page_zone(page) != zone) |
| 1399 | return 0; |
| 1400 | zone = page_zone(page); |
| 1401 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1402 | } |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1407 | * Scan pfn range [start,end) to find movable/migratable pages (LRU pages |
| 1408 | * and hugepages). We scan pfn because it's much easier than scanning over |
| 1409 | * linked list. This function returns the pfn of the first found movable |
| 1410 | * page if it's found, otherwise 0. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1411 | */ |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1412 | static unsigned long scan_movable_pages(unsigned long start, unsigned long end) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1413 | { |
| 1414 | unsigned long pfn; |
| 1415 | struct page *page; |
| 1416 | for (pfn = start; pfn < end; pfn++) { |
| 1417 | if (pfn_valid(pfn)) { |
| 1418 | page = pfn_to_page(pfn); |
| 1419 | if (PageLRU(page)) |
| 1420 | return pfn; |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1421 | if (PageHuge(page)) { |
Naoya Horiguchi | 7e1f049 | 2015-04-15 16:14:41 -0700 | [diff] [blame] | 1422 | if (page_huge_active(page)) |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1423 | return pfn; |
| 1424 | else |
| 1425 | pfn = round_up(pfn + 1, |
| 1426 | 1 << compound_order(page)) - 1; |
| 1427 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1433 | #define NR_OFFLINE_AT_ONCE_PAGES (256) |
| 1434 | static int |
| 1435 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) |
| 1436 | { |
| 1437 | unsigned long pfn; |
| 1438 | struct page *page; |
| 1439 | int move_pages = NR_OFFLINE_AT_ONCE_PAGES; |
| 1440 | int not_managed = 0; |
| 1441 | int ret = 0; |
| 1442 | LIST_HEAD(source); |
| 1443 | |
| 1444 | for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { |
| 1445 | if (!pfn_valid(pfn)) |
| 1446 | continue; |
| 1447 | page = pfn_to_page(pfn); |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1448 | |
| 1449 | if (PageHuge(page)) { |
| 1450 | struct page *head = compound_head(page); |
| 1451 | pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1; |
| 1452 | if (compound_order(head) > PFN_SECTION_SHIFT) { |
| 1453 | ret = -EBUSY; |
| 1454 | break; |
| 1455 | } |
| 1456 | if (isolate_huge_page(page, &source)) |
| 1457 | move_pages -= 1 << compound_order(head); |
| 1458 | continue; |
| 1459 | } |
| 1460 | |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1461 | if (!get_page_unless_zero(page)) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1462 | continue; |
| 1463 | /* |
| 1464 | * We can skip free pages. And we can only deal with pages on |
| 1465 | * LRU. |
| 1466 | */ |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1467 | ret = isolate_lru_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1468 | if (!ret) { /* Success */ |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1469 | put_page(page); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1470 | list_add_tail(&page->lru, &source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1471 | move_pages--; |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1472 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 1473 | page_is_file_cache(page)); |
| 1474 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1475 | } else { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1476 | #ifdef CONFIG_DEBUG_VM |
Wu Fengguang | 718a382 | 2010-03-10 15:20:43 -0800 | [diff] [blame] | 1477 | printk(KERN_ALERT "removing pfn %lx from LRU failed\n", |
| 1478 | pfn); |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1479 | dump_page(page, "failed to remove from LRU"); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1480 | #endif |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1481 | put_page(page); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1482 | /* Because we don't have big zone->lock. we should |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1483 | check this again here. */ |
| 1484 | if (page_count(page)) { |
| 1485 | not_managed++; |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1486 | ret = -EBUSY; |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1487 | break; |
| 1488 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1489 | } |
| 1490 | } |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1491 | if (!list_empty(&source)) { |
| 1492 | if (not_managed) { |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1493 | putback_movable_pages(&source); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1494 | goto out; |
| 1495 | } |
Minchan Kim | 74c08f9 | 2012-10-08 16:32:54 -0700 | [diff] [blame] | 1496 | |
| 1497 | /* |
| 1498 | * alloc_migrate_target should be improooooved!! |
| 1499 | * migrate_pages returns # of failed pages. |
| 1500 | */ |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1501 | ret = migrate_pages(&source, alloc_migrate_target, NULL, 0, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1502 | MIGRATE_SYNC, MR_MEMORY_HOTPLUG); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1503 | if (ret) |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1504 | putback_movable_pages(&source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1505 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1506 | out: |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * remove from free_area[] and mark all as Reserved. |
| 1512 | */ |
| 1513 | static int |
| 1514 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, |
| 1515 | void *data) |
| 1516 | { |
| 1517 | __offline_isolated_pages(start, start + nr_pages); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | static void |
| 1522 | offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 1523 | { |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1524 | walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1525 | offline_isolated_pages_cb); |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Check all pages in range, recoreded as memory resource, are isolated. |
| 1530 | */ |
| 1531 | static int |
| 1532 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, |
| 1533 | void *data) |
| 1534 | { |
| 1535 | int ret; |
| 1536 | long offlined = *(long *)data; |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1537 | ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1538 | offlined = nr_pages; |
| 1539 | if (!ret) |
| 1540 | *(long *)data += offlined; |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | static long |
| 1545 | check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) |
| 1546 | { |
| 1547 | long offlined = 0; |
| 1548 | int ret; |
| 1549 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1550 | ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1551 | check_pages_isolated_cb); |
| 1552 | if (ret < 0) |
| 1553 | offlined = (long)ret; |
| 1554 | return offlined; |
| 1555 | } |
| 1556 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1557 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1558 | /* |
| 1559 | * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have |
| 1560 | * normal memory. |
| 1561 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1562 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1563 | { |
| 1564 | return true; |
| 1565 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1566 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1567 | /* ensure the node has NORMAL memory if it is still online */ |
| 1568 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1569 | { |
| 1570 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1571 | unsigned long present_pages = 0; |
| 1572 | enum zone_type zt; |
| 1573 | |
| 1574 | for (zt = 0; zt <= ZONE_NORMAL; zt++) |
| 1575 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1576 | |
| 1577 | if (present_pages > nr_pages) |
| 1578 | return true; |
| 1579 | |
| 1580 | present_pages = 0; |
| 1581 | for (; zt <= ZONE_MOVABLE; zt++) |
| 1582 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1583 | |
| 1584 | /* |
| 1585 | * we can't offline the last normal memory until all |
| 1586 | * higher memory is offlined. |
| 1587 | */ |
| 1588 | return present_pages == 0; |
| 1589 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1590 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1591 | |
Tang Chen | c532092 | 2013-11-12 15:08:10 -0800 | [diff] [blame] | 1592 | static int __init cmdline_parse_movable_node(char *p) |
| 1593 | { |
| 1594 | #ifdef CONFIG_MOVABLE_NODE |
| 1595 | /* |
| 1596 | * Memory used by the kernel cannot be hot-removed because Linux |
| 1597 | * cannot migrate the kernel pages. When memory hotplug is |
| 1598 | * enabled, we should prevent memblock from allocating memory |
| 1599 | * for the kernel. |
| 1600 | * |
| 1601 | * ACPI SRAT records all hotpluggable memory ranges. But before |
| 1602 | * SRAT is parsed, we don't know about it. |
| 1603 | * |
| 1604 | * The kernel image is loaded into memory at very early time. We |
| 1605 | * cannot prevent this anyway. So on NUMA system, we set any |
| 1606 | * node the kernel resides in as un-hotpluggable. |
| 1607 | * |
| 1608 | * Since on modern servers, one node could have double-digit |
| 1609 | * gigabytes memory, we can assume the memory around the kernel |
| 1610 | * image is also un-hotpluggable. So before SRAT is parsed, just |
| 1611 | * allocate memory near the kernel image to try the best to keep |
| 1612 | * the kernel away from hotpluggable memory. |
| 1613 | */ |
| 1614 | memblock_set_bottom_up(true); |
Tang Chen | 55ac590 | 2014-01-21 15:49:35 -0800 | [diff] [blame] | 1615 | movable_node_enabled = true; |
Tang Chen | c532092 | 2013-11-12 15:08:10 -0800 | [diff] [blame] | 1616 | #else |
| 1617 | pr_warn("movable_node option not supported\n"); |
| 1618 | #endif |
| 1619 | return 0; |
| 1620 | } |
| 1621 | early_param("movable_node", cmdline_parse_movable_node); |
| 1622 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1623 | /* check which state of node_states will be changed when offline memory */ |
| 1624 | static void node_states_check_changes_offline(unsigned long nr_pages, |
| 1625 | struct zone *zone, struct memory_notify *arg) |
| 1626 | { |
| 1627 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1628 | unsigned long present_pages = 0; |
| 1629 | enum zone_type zt, zone_last = ZONE_NORMAL; |
| 1630 | |
| 1631 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1632 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 1633 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 1634 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1635 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1636 | * If we don't have HIGHMEM nor movable node, |
| 1637 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 1638 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1639 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1640 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1641 | zone_last = ZONE_MOVABLE; |
| 1642 | |
| 1643 | /* |
| 1644 | * check whether node_states[N_NORMAL_MEMORY] will be changed. |
| 1645 | * If the memory to be offline is in a zone of 0...zone_last, |
| 1646 | * and it is the last present memory, 0...zone_last will |
| 1647 | * become empty after offline , thus we can determind we will |
| 1648 | * need to clear the node from node_states[N_NORMAL_MEMORY]. |
| 1649 | */ |
| 1650 | for (zt = 0; zt <= zone_last; zt++) |
| 1651 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1652 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1653 | arg->status_change_nid_normal = zone_to_nid(zone); |
| 1654 | else |
| 1655 | arg->status_change_nid_normal = -1; |
| 1656 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1657 | #ifdef CONFIG_HIGHMEM |
| 1658 | /* |
| 1659 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 1660 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 1661 | * set zone_last to ZONE_HIGHMEM. |
| 1662 | * |
| 1663 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 1664 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 1665 | * set zone_last to ZONE_MOVABLE. |
| 1666 | */ |
| 1667 | zone_last = ZONE_HIGHMEM; |
| 1668 | if (N_MEMORY == N_HIGH_MEMORY) |
| 1669 | zone_last = ZONE_MOVABLE; |
| 1670 | |
| 1671 | for (; zt <= zone_last; zt++) |
| 1672 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1673 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1674 | arg->status_change_nid_high = zone_to_nid(zone); |
| 1675 | else |
| 1676 | arg->status_change_nid_high = -1; |
| 1677 | #else |
| 1678 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 1679 | #endif |
| 1680 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1681 | /* |
| 1682 | * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE |
| 1683 | */ |
| 1684 | zone_last = ZONE_MOVABLE; |
| 1685 | |
| 1686 | /* |
| 1687 | * check whether node_states[N_HIGH_MEMORY] will be changed |
| 1688 | * If we try to offline the last present @nr_pages from the node, |
| 1689 | * we can determind we will need to clear the node from |
| 1690 | * node_states[N_HIGH_MEMORY]. |
| 1691 | */ |
| 1692 | for (; zt <= zone_last; zt++) |
| 1693 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1694 | if (nr_pages >= present_pages) |
| 1695 | arg->status_change_nid = zone_to_nid(zone); |
| 1696 | else |
| 1697 | arg->status_change_nid = -1; |
| 1698 | } |
| 1699 | |
| 1700 | static void node_states_clear_node(int node, struct memory_notify *arg) |
| 1701 | { |
| 1702 | if (arg->status_change_nid_normal >= 0) |
| 1703 | node_clear_state(node, N_NORMAL_MEMORY); |
| 1704 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1705 | if ((N_MEMORY != N_NORMAL_MEMORY) && |
| 1706 | (arg->status_change_nid_high >= 0)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1707 | node_clear_state(node, N_HIGH_MEMORY); |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1708 | |
| 1709 | if ((N_MEMORY != N_HIGH_MEMORY) && |
| 1710 | (arg->status_change_nid >= 0)) |
| 1711 | node_clear_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1712 | } |
| 1713 | |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1714 | static int __ref __offline_pages(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1715 | unsigned long end_pfn, unsigned long timeout) |
| 1716 | { |
| 1717 | unsigned long pfn, nr_pages, expire; |
| 1718 | long offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1719 | int ret, drain, retry_max, node; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1720 | unsigned long flags; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1721 | struct zone *zone; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1722 | struct memory_notify arg; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1723 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1724 | /* at least, alignment against pageblock is necessary */ |
| 1725 | if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) |
| 1726 | return -EINVAL; |
| 1727 | if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) |
| 1728 | return -EINVAL; |
| 1729 | /* This makes hotplug much easier...and readable. |
| 1730 | we assume this for now. .*/ |
| 1731 | if (!test_pages_in_a_zone(start_pfn, end_pfn)) |
| 1732 | return -EINVAL; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1733 | |
| 1734 | zone = page_zone(pfn_to_page(start_pfn)); |
| 1735 | node = zone_to_nid(zone); |
| 1736 | nr_pages = end_pfn - start_pfn; |
| 1737 | |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1738 | if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages)) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1739 | return -EINVAL; |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1740 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1741 | /* set above range as isolated */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1742 | ret = start_isolate_page_range(start_pfn, end_pfn, |
| 1743 | MIGRATE_MOVABLE, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1744 | if (ret) |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1745 | return ret; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | arg.start_pfn = start_pfn; |
| 1748 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1749 | node_states_check_changes_offline(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1750 | |
| 1751 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); |
| 1752 | ret = notifier_to_errno(ret); |
| 1753 | if (ret) |
| 1754 | goto failed_removal; |
| 1755 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1756 | pfn = start_pfn; |
| 1757 | expire = jiffies + timeout; |
| 1758 | drain = 0; |
| 1759 | retry_max = 5; |
| 1760 | repeat: |
| 1761 | /* start memory hot removal */ |
| 1762 | ret = -EAGAIN; |
| 1763 | if (time_after(jiffies, expire)) |
| 1764 | goto failed_removal; |
| 1765 | ret = -EINTR; |
| 1766 | if (signal_pending(current)) |
| 1767 | goto failed_removal; |
| 1768 | ret = 0; |
| 1769 | if (drain) { |
| 1770 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1771 | cond_resched(); |
Vlastimil Babka | c055432 | 2014-12-10 15:43:10 -0800 | [diff] [blame] | 1772 | drain_all_pages(zone); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1775 | pfn = scan_movable_pages(start_pfn, end_pfn); |
| 1776 | if (pfn) { /* We have movable pages */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1777 | ret = do_migrate_range(pfn, end_pfn); |
| 1778 | if (!ret) { |
| 1779 | drain = 1; |
| 1780 | goto repeat; |
| 1781 | } else { |
| 1782 | if (ret < 0) |
| 1783 | if (--retry_max == 0) |
| 1784 | goto failed_removal; |
| 1785 | yield(); |
| 1786 | drain = 1; |
| 1787 | goto repeat; |
| 1788 | } |
| 1789 | } |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1790 | /* drain all zone's lru pagevec, this is asynchronous... */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1791 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1792 | yield(); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1793 | /* drain pcp pages, this is synchronous. */ |
Vlastimil Babka | c055432 | 2014-12-10 15:43:10 -0800 | [diff] [blame] | 1794 | drain_all_pages(zone); |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 1795 | /* |
| 1796 | * dissolve free hugepages in the memory block before doing offlining |
| 1797 | * actually in order to make hugetlbfs's object counting consistent. |
| 1798 | */ |
| 1799 | dissolve_free_huge_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1800 | /* check again */ |
| 1801 | offlined_pages = check_pages_isolated(start_pfn, end_pfn); |
| 1802 | if (offlined_pages < 0) { |
| 1803 | ret = -EBUSY; |
| 1804 | goto failed_removal; |
| 1805 | } |
| 1806 | printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1807 | /* Ok, all of our target is isolated. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1808 | We cannot do rollback at this point. */ |
| 1809 | offline_isolated_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | dbc0e4c | 2007-11-14 16:59:12 -0800 | [diff] [blame] | 1810 | /* reset pagetype flags and makes migrate type to be MOVABLE */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1811 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1812 | /* removal success */ |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 1813 | adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1814 | zone->present_pages -= offlined_pages; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1815 | |
| 1816 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1817 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
Cody P Schafer | d702909 | 2013-07-03 15:02:11 -0700 | [diff] [blame] | 1818 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1819 | |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1820 | init_per_zone_wmark_min(); |
| 1821 | |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1822 | if (!populated_zone(zone)) { |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1823 | zone_pcp_reset(zone); |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1824 | mutex_lock(&zonelists_mutex); |
| 1825 | build_all_zonelists(NULL, NULL); |
| 1826 | mutex_unlock(&zonelists_mutex); |
| 1827 | } else |
| 1828 | zone_pcp_update(zone); |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1829 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1830 | node_states_clear_node(node, &arg); |
| 1831 | if (arg.status_change_nid >= 0) |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 1832 | kswapd_stop(node); |
Minchan Kim | bce7394 | 2009-06-16 15:32:50 -0700 | [diff] [blame] | 1833 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1834 | vm_total_pages = nr_free_pagecache_pages(); |
| 1835 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | memory_notify(MEM_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1838 | return 0; |
| 1839 | |
| 1840 | failed_removal: |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 1841 | printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n", |
| 1842 | (unsigned long long) start_pfn << PAGE_SHIFT, |
| 1843 | ((unsigned long long) end_pfn << PAGE_SHIFT) - 1); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1844 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1845 | /* pushback to free area */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1846 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1847 | return ret; |
| 1848 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1849 | |
David Rientjes | 30467e0 | 2015-04-14 15:45:11 -0700 | [diff] [blame] | 1850 | /* Must be protected by mem_hotplug_begin() */ |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1851 | int offline_pages(unsigned long start_pfn, unsigned long nr_pages) |
| 1852 | { |
| 1853 | return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ); |
| 1854 | } |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1855 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1856 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1857 | /** |
| 1858 | * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn) |
| 1859 | * @start_pfn: start pfn of the memory range |
Toshi Kani | e05c4bb | 2013-04-29 15:06:16 -0700 | [diff] [blame] | 1860 | * @end_pfn: end pfn of the memory range |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1861 | * @arg: argument passed to func |
| 1862 | * @func: callback for each memory section walked |
| 1863 | * |
| 1864 | * This function walks through all present mem sections in range |
| 1865 | * [start_pfn, end_pfn) and call func on each mem section. |
| 1866 | * |
| 1867 | * Returns the return value of func. |
| 1868 | */ |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1869 | int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1870 | void *arg, int (*func)(struct memory_block *, void *)) |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1871 | { |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1872 | struct memory_block *mem = NULL; |
| 1873 | struct mem_section *section; |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1874 | unsigned long pfn, section_nr; |
| 1875 | int ret; |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1876 | |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1877 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 1878 | section_nr = pfn_to_section_nr(pfn); |
| 1879 | if (!present_section_nr(section_nr)) |
| 1880 | continue; |
| 1881 | |
| 1882 | section = __nr_to_section(section_nr); |
| 1883 | /* same memblock? */ |
| 1884 | if (mem) |
| 1885 | if ((section_nr >= mem->start_section_nr) && |
| 1886 | (section_nr <= mem->end_section_nr)) |
| 1887 | continue; |
| 1888 | |
| 1889 | mem = find_memory_block_hinted(section, mem); |
| 1890 | if (!mem) |
| 1891 | continue; |
| 1892 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1893 | ret = func(mem, arg); |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1894 | if (ret) { |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1895 | kobject_put(&mem->dev.kobj); |
| 1896 | return ret; |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | if (mem) |
| 1901 | kobject_put(&mem->dev.kobj); |
| 1902 | |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1903 | return 0; |
| 1904 | } |
| 1905 | |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 1906 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Xishi Qiu | d6de9d5 | 2013-11-12 15:07:20 -0800 | [diff] [blame] | 1907 | static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1908 | { |
| 1909 | int ret = !is_memblock_offlined(mem); |
| 1910 | |
Randy Dunlap | 349daa0 | 2013-04-29 15:08:49 -0700 | [diff] [blame] | 1911 | if (unlikely(ret)) { |
| 1912 | phys_addr_t beginpa, endpa; |
| 1913 | |
| 1914 | beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); |
| 1915 | 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] | 1916 | pr_warn("removing memory fails, because memory " |
Randy Dunlap | 349daa0 | 2013-04-29 15:08:49 -0700 | [diff] [blame] | 1917 | "[%pa-%pa] is onlined\n", |
| 1918 | &beginpa, &endpa); |
| 1919 | } |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 1920 | |
| 1921 | return ret; |
| 1922 | } |
| 1923 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1924 | static int check_cpu_on_node(pg_data_t *pgdat) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1925 | { |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1926 | int cpu; |
| 1927 | |
| 1928 | for_each_present_cpu(cpu) { |
| 1929 | if (cpu_to_node(cpu) == pgdat->node_id) |
| 1930 | /* |
| 1931 | * the cpu on this node isn't removed, and we can't |
| 1932 | * offline this node. |
| 1933 | */ |
| 1934 | return -EBUSY; |
| 1935 | } |
| 1936 | |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1940 | static void unmap_cpu_on_node(pg_data_t *pgdat) |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1941 | { |
| 1942 | #ifdef CONFIG_ACPI_NUMA |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1943 | int cpu; |
| 1944 | |
| 1945 | for_each_possible_cpu(cpu) |
| 1946 | if (cpu_to_node(cpu) == pgdat->node_id) |
| 1947 | numa_clear_node(cpu); |
| 1948 | #endif |
| 1949 | } |
| 1950 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1951 | static int check_and_unmap_cpu_on_node(pg_data_t *pgdat) |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1952 | { |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1953 | int ret; |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1954 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1955 | ret = check_cpu_on_node(pgdat); |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1956 | if (ret) |
| 1957 | return ret; |
| 1958 | |
| 1959 | /* |
| 1960 | * the node will be offlined when we come here, so we can clear |
| 1961 | * the cpu_to_node() now. |
| 1962 | */ |
| 1963 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1964 | unmap_cpu_on_node(pgdat); |
Wen Congyang | e13fe86 | 2013-02-22 16:33:31 -0800 | [diff] [blame] | 1965 | return 0; |
| 1966 | } |
| 1967 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 1968 | /** |
| 1969 | * try_offline_node |
| 1970 | * |
| 1971 | * Offline a node if all memory sections and cpus of the node are removed. |
| 1972 | * |
| 1973 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug |
| 1974 | * and online/offline operations before this call. |
| 1975 | */ |
Wen Congyang | 90b30cd | 2013-02-22 16:33:27 -0800 | [diff] [blame] | 1976 | void try_offline_node(int nid) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1977 | { |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 1978 | pg_data_t *pgdat = NODE_DATA(nid); |
| 1979 | unsigned long start_pfn = pgdat->node_start_pfn; |
| 1980 | unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1981 | unsigned long pfn; |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 1982 | int i; |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 1983 | |
| 1984 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 1985 | unsigned long section_nr = pfn_to_section_nr(pfn); |
| 1986 | |
| 1987 | if (!present_section_nr(section_nr)) |
| 1988 | continue; |
| 1989 | |
| 1990 | if (pfn_to_nid(pfn) != nid) |
| 1991 | continue; |
| 1992 | |
| 1993 | /* |
| 1994 | * some memory sections of this node are not removed, and we |
| 1995 | * can't offline node now. |
| 1996 | */ |
| 1997 | return; |
| 1998 | } |
| 1999 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2000 | if (check_and_unmap_cpu_on_node(pgdat)) |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2001 | return; |
| 2002 | |
| 2003 | /* |
| 2004 | * all memory/cpu of this node are removed, we can offline this |
| 2005 | * node now. |
| 2006 | */ |
| 2007 | node_set_offline(nid); |
| 2008 | unregister_one_node(nid); |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2009 | |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2010 | /* free waittable in each zone */ |
| 2011 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 2012 | struct zone *zone = pgdat->node_zones + i; |
| 2013 | |
Jianguo Wu | ca4b3f3 | 2013-03-22 15:04:50 -0700 | [diff] [blame] | 2014 | /* |
| 2015 | * wait_table may be allocated from boot memory, |
| 2016 | * here only free if it's allocated by vmalloc. |
| 2017 | */ |
Gu Zheng | 85bd839 | 2015-06-10 11:14:43 -0700 | [diff] [blame] | 2018 | if (is_vmalloc_addr(zone->wait_table)) { |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2019 | vfree(zone->wait_table); |
Gu Zheng | 85bd839 | 2015-06-10 11:14:43 -0700 | [diff] [blame] | 2020 | zone->wait_table = NULL; |
| 2021 | } |
Wen Congyang | d822b86 | 2013-02-22 16:33:16 -0800 | [diff] [blame] | 2022 | } |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2023 | } |
Wen Congyang | 90b30cd | 2013-02-22 16:33:27 -0800 | [diff] [blame] | 2024 | EXPORT_SYMBOL(try_offline_node); |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2025 | |
Toshi Kani | 0f1cfe9 | 2013-09-11 14:21:50 -0700 | [diff] [blame] | 2026 | /** |
| 2027 | * remove_memory |
| 2028 | * |
| 2029 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug |
| 2030 | * and online/offline operations before this call, as required by |
| 2031 | * try_offline_node(). |
| 2032 | */ |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2033 | void __ref remove_memory(int nid, u64 start, u64 size) |
Wen Congyang | bbc76be | 2013-02-22 16:32:54 -0800 | [diff] [blame] | 2034 | { |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2035 | int ret; |
Wen Congyang | 993c1aa | 2013-02-22 16:32:50 -0800 | [diff] [blame] | 2036 | |
Toshi Kani | 27356f5 | 2013-09-11 14:21:49 -0700 | [diff] [blame] | 2037 | BUG_ON(check_hotplug_memory_range(start, size)); |
| 2038 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2039 | mem_hotplug_begin(); |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2040 | |
| 2041 | /* |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2042 | * All memory blocks must be offlined before removing memory. Check |
| 2043 | * whether all memory blocks in question are offline and trigger a BUG() |
| 2044 | * if this is not the case. |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2045 | */ |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2046 | 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] | 2047 | check_memblock_offlined_cb); |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2048 | if (ret) |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 2049 | BUG(); |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 2050 | |
Yasuaki Ishimatsu | 46c66c4 | 2013-02-22 16:32:56 -0800 | [diff] [blame] | 2051 | /* remove memmap entry */ |
| 2052 | firmware_map_remove(start, start + size, "System RAM"); |
Xishi Qiu | f9126ab | 2015-08-14 15:35:16 -0700 | [diff] [blame] | 2053 | memblock_free(start, size); |
| 2054 | memblock_remove(start, size); |
Yasuaki Ishimatsu | 46c66c4 | 2013-02-22 16:32:56 -0800 | [diff] [blame] | 2055 | |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 2056 | arch_remove_memory(start, size); |
| 2057 | |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 2058 | try_offline_node(nid); |
| 2059 | |
Vladimir Davydov | bfc8c90 | 2014-06-04 16:07:18 -0700 | [diff] [blame] | 2060 | mem_hotplug_done(); |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 2061 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 2062 | EXPORT_SYMBOL_GPL(remove_memory); |
Rafael J. Wysocki | aba6efc | 2013-06-01 22:24:07 +0200 | [diff] [blame] | 2063 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |