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