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