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