blob: c9f715b2917f45819593d6030b34423c98b0620e [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#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 Hansen3947be12005-10-29 18:16:54 -070012#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070014#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070015#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/slab.h>
17#include <linux/sysctl.h>
18#include <linux/cpu.h>
19#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080020#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070021#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080032#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070033#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080034#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080035#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070036#include <linux/compaction.h>
Dave Hansen3947be12005-10-29 18:16:54 -070037
38#include <asm/tlbflush.h>
39
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030040#include "internal.h"
41
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070042/*
43 * online_page_callback contains pointer to current page onlining function.
44 * Initially it is generic_online_page(). If it is required it could be
45 * changed by calling set_online_page_callback() for callback registration
46 * and restore_online_page_callback() for generic callback restore.
47 */
48
49static void generic_online_page(struct page *page);
50
51static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070052static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070053
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070054/* The same as the cpu_hotplug lock, but for memory hotplug. */
55static struct {
56 struct task_struct *active_writer;
57 struct mutex lock; /* Synchronizes accesses to refcount, */
58 /*
59 * Also blocks the new readers during
60 * an ongoing mem hotplug operation.
61 */
62 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080063
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070064#ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
66#endif
67} mem_hotplug = {
68 .active_writer = NULL,
69 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
70 .refcount = 0,
71#ifdef CONFIG_DEBUG_LOCK_ALLOC
72 .dep_map = {.name = "mem_hotplug.lock" },
73#endif
74};
75
76/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
77#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
78#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
79#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
80
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070081#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070082bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070083#else
84bool memhp_auto_online = true;
85#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070086EXPORT_SYMBOL_GPL(memhp_auto_online);
87
Vitaly Kuznetsov86dd9952016-05-19 17:13:06 -070088static int __init setup_memhp_default_state(char *str)
89{
90 if (!strcmp(str, "online"))
91 memhp_auto_online = true;
92 else if (!strcmp(str, "offline"))
93 memhp_auto_online = false;
94
95 return 1;
96}
97__setup("memhp_default_state=", setup_memhp_default_state);
98
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070099void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800100{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700101 might_sleep();
102 if (mem_hotplug.active_writer == current)
103 return;
104 memhp_lock_acquire_read();
105 mutex_lock(&mem_hotplug.lock);
106 mem_hotplug.refcount++;
107 mutex_unlock(&mem_hotplug.lock);
108
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800109}
110
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700111void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800112{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700113 if (mem_hotplug.active_writer == current)
114 return;
115 mutex_lock(&mem_hotplug.lock);
116
117 if (WARN_ON(!mem_hotplug.refcount))
118 mem_hotplug.refcount++; /* try to fix things up */
119
120 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
121 wake_up_process(mem_hotplug.active_writer);
122 mutex_unlock(&mem_hotplug.lock);
123 memhp_lock_release();
124
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800125}
126
David Rientjes30467e02015-04-14 15:45:11 -0700127void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700128{
129 mem_hotplug.active_writer = current;
130
131 memhp_lock_acquire();
132 for (;;) {
133 mutex_lock(&mem_hotplug.lock);
134 if (likely(!mem_hotplug.refcount))
135 break;
136 __set_current_state(TASK_UNINTERRUPTIBLE);
137 mutex_unlock(&mem_hotplug.lock);
138 schedule();
139 }
140}
141
David Rientjes30467e02015-04-14 15:45:11 -0700142void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700143{
144 mem_hotplug.active_writer = NULL;
145 mutex_unlock(&mem_hotplug.lock);
146 memhp_lock_release();
147}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800148
Keith Mannthey45e0b782006-09-30 23:27:09 -0700149/* add this memory to iomem resource */
150static struct resource *register_memory_resource(u64 start, u64 size)
151{
152 struct resource *res;
153 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800154 if (!res)
155 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700156
157 res->name = "System RAM";
158 res->start = start;
159 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100160 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700161 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700162 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700163 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800164 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700165 }
166 return res;
167}
168
169static void release_memory_resource(struct resource *res)
170{
171 if (!res)
172 return;
173 release_resource(res);
174 kfree(res);
175 return;
176}
177
Keith Mannthey53947022006-09-30 23:27:08 -0700178#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800179void get_page_bootmem(unsigned long info, struct page *page,
180 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700181{
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800182 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700183 SetPagePrivate(page);
184 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700185 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700186}
187
Jiang Liu170a5a72013-07-03 15:03:17 -0700188void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700189{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800190 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700191
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800192 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800193 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
194 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700195
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700196 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800197 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700198 ClearPagePrivate(page);
199 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800200 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700201 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700202 }
Yasunori Goto04753272008-04-28 02:13:31 -0700203}
204
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800205#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
206#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700207static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700208{
209 unsigned long *usemap, mapsize, section_nr, i;
210 struct mem_section *ms;
211 struct page *page, *memmap;
212
Yasunori Goto04753272008-04-28 02:13:31 -0700213 section_nr = pfn_to_section_nr(start_pfn);
214 ms = __nr_to_section(section_nr);
215
216 /* Get section's memmap address */
217 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
218
219 /*
220 * Get page for the memmap's phys address
221 * XXX: need more consideration for sparse_vmemmap...
222 */
223 page = virt_to_page(memmap);
224 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
225 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
226
227 /* remember memmap's page */
228 for (i = 0; i < mapsize; i++, page++)
229 get_page_bootmem(section_nr, page, SECTION_INFO);
230
231 usemap = __nr_to_section(section_nr)->pageblock_flags;
232 page = virt_to_page(usemap);
233
234 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
235
236 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700237 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700238
239}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800240#else /* CONFIG_SPARSEMEM_VMEMMAP */
241static void register_page_bootmem_info_section(unsigned long start_pfn)
242{
243 unsigned long *usemap, mapsize, section_nr, i;
244 struct mem_section *ms;
245 struct page *page, *memmap;
246
247 if (!pfn_valid(start_pfn))
248 return;
249
250 section_nr = pfn_to_section_nr(start_pfn);
251 ms = __nr_to_section(section_nr);
252
253 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
254
255 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
256
257 usemap = __nr_to_section(section_nr)->pageblock_flags;
258 page = virt_to_page(usemap);
259
260 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
261
262 for (i = 0; i < mapsize; i++, page++)
263 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
264}
265#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700266
Linus Torvalds7ded3842016-05-27 15:23:32 -0700267void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700268{
269 unsigned long i, pfn, end_pfn, nr_pages;
270 int node = pgdat->node_id;
271 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700272
273 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
274 page = virt_to_page(pgdat);
275
276 for (i = 0; i < nr_pages; i++, page++)
277 get_page_bootmem(node, page, NODE_INFO);
278
Yasunori Goto04753272008-04-28 02:13:31 -0700279 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800280 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700281
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700282 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700283 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
284 /*
285 * Some platforms can assign the same pfn to multiple nodes - on
286 * node0 as well as nodeN. To avoid registering a pfn against
287 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700288 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700289 */
Yang Shif65e91df2016-05-27 14:27:32 -0700290 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700291 register_page_bootmem_info_section(pfn);
292 }
Yasunori Goto04753272008-04-28 02:13:31 -0700293}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800294#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700295
Fabian Frederickf2765402014-08-06 16:04:57 -0700296static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
297 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700298{
299 unsigned long old_zone_end_pfn;
300
301 zone_span_writelock(zone);
302
Xishi Qiuc33bc312013-09-11 14:21:44 -0700303 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700304 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700305 zone->zone_start_pfn = start_pfn;
306
307 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
308 zone->zone_start_pfn;
309
310 zone_span_writeunlock(zone);
311}
312
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800313static void resize_zone(struct zone *zone, unsigned long start_pfn,
314 unsigned long end_pfn)
315{
316 zone_span_writelock(zone);
317
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800318 if (end_pfn - start_pfn) {
319 zone->zone_start_pfn = start_pfn;
320 zone->spanned_pages = end_pfn - start_pfn;
321 } else {
322 /*
323 * make it consist as free_area_init_core(),
324 * if spanned_pages = 0, then keep start_pfn = 0
325 */
326 zone->zone_start_pfn = 0;
327 zone->spanned_pages = 0;
328 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800329
330 zone_span_writeunlock(zone);
331}
332
333static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
334 unsigned long end_pfn)
335{
336 enum zone_type zid = zone_idx(zone);
337 int nid = zone->zone_pgdat->node_id;
338 unsigned long pfn;
339
340 for (pfn = start_pfn; pfn < end_pfn; pfn++)
341 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
342}
343
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800344/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800345 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800346static int __ref ensure_zone_is_initialized(struct zone *zone,
347 unsigned long start_pfn, unsigned long num_pages)
348{
349 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800350 return init_currently_empty_zone(zone, start_pfn, num_pages);
351
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800352 return 0;
353}
354
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800355static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800356 unsigned long start_pfn, unsigned long end_pfn)
357{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800358 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800359 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800360 unsigned long z1_start_pfn;
361
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800362 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
363 if (ret)
364 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800365
366 pgdat_resize_lock(z1->zone_pgdat, &flags);
367
368 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800369 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800370 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700371 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800372 if (start_pfn > z2->zone_start_pfn)
373 goto out_fail;
374 /* must included/overlap */
375 if (end_pfn <= z2->zone_start_pfn)
376 goto out_fail;
377
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800378 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700379 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800380 z1_start_pfn = z1->zone_start_pfn;
381 else
382 z1_start_pfn = start_pfn;
383
384 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800385 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800386
387 pgdat_resize_unlock(z1->zone_pgdat, &flags);
388
389 fix_zone_id(z1, start_pfn, end_pfn);
390
391 return 0;
392out_fail:
393 pgdat_resize_unlock(z1->zone_pgdat, &flags);
394 return -1;
395}
396
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800397static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800398 unsigned long start_pfn, unsigned long end_pfn)
399{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800400 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800401 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800402 unsigned long z2_end_pfn;
403
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800404 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
405 if (ret)
406 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800407
408 pgdat_resize_lock(z1->zone_pgdat, &flags);
409
410 /* can't move pfns which are lower than @z1 */
411 if (z1->zone_start_pfn > start_pfn)
412 goto out_fail;
413 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800415 goto out_fail;
416 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800417 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800418 goto out_fail;
419
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800420 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700421 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800422 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800423 else
424 z2_end_pfn = end_pfn;
425
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800426 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800427 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800428
429 pgdat_resize_unlock(z1->zone_pgdat, &flags);
430
431 fix_zone_id(z2, start_pfn, end_pfn);
432
433 return 0;
434out_fail:
435 pgdat_resize_unlock(z1->zone_pgdat, &flags);
436 return -1;
437}
438
Reza Arbabe51e6c82016-07-26 15:22:20 -0700439static struct zone * __meminit move_pfn_range(int zone_shift,
440 unsigned long start_pfn, unsigned long end_pfn)
441{
442 struct zone *zone = page_zone(pfn_to_page(start_pfn));
443 int ret = 0;
444
445 if (zone_shift < 0)
446 ret = move_pfn_range_left(zone + zone_shift, zone,
447 start_pfn, end_pfn);
448 else if (zone_shift)
449 ret = move_pfn_range_right(zone, zone + zone_shift,
450 start_pfn, end_pfn);
451
452 if (ret)
453 return NULL;
454
455 return zone + zone_shift;
456}
457
Fabian Frederickf2765402014-08-06 16:04:57 -0700458static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
459 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700460{
Xishi Qiu83285c72013-11-12 15:07:19 -0800461 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700462
Tang Chen712cd382012-12-11 16:01:07 -0800463 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700464 pgdat->node_start_pfn = start_pfn;
465
466 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
467 pgdat->node_start_pfn;
468}
469
Al Viro31168482008-11-22 17:33:24 +0000470static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700471{
472 struct pglist_data *pgdat = zone->zone_pgdat;
473 int nr_pages = PAGES_PER_SECTION;
474 int nid = pgdat->node_id;
475 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700476 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800477 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700478
479 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800480 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
481 if (ret)
482 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700483
Heiko Carstens76cdd582008-05-14 16:05:52 -0700484 pgdat_resize_lock(zone->zone_pgdat, &flags);
485 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
486 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
487 phys_start_pfn + nr_pages);
488 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800489 memmap_init_zone(nr_pages, nid, zone_type,
490 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700491
492 /* online_page_range is called later and expects pages reserved */
493 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
494 if (!pfn_valid(pfn))
495 continue;
496
497 SetPageReserved(pfn_to_page(pfn));
498 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700499 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700500}
501
Gary Hadec04fc582009-01-06 14:39:14 -0800502static int __meminit __add_section(int nid, struct zone *zone,
503 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700504{
Dave Hansen3947be12005-10-29 18:16:54 -0700505 int ret;
506
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700507 if (pfn_valid(phys_start_pfn))
508 return -EEXIST;
509
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800510 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700511
512 if (ret < 0)
513 return ret;
514
Yasunori Goto718127c2006-06-23 02:03:10 -0700515 ret = __add_zone(zone, phys_start_pfn);
516
517 if (ret < 0)
518 return ret;
519
Gary Hadec04fc582009-01-06 14:39:14 -0800520 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700521}
522
David Rientjes4edd7ce2013-04-29 15:08:22 -0700523/*
524 * Reasonably generic function for adding memory. It is
525 * expected that archs that support memory hotplug will
526 * call this function after deciding the zone to which to
527 * add the new pages.
528 */
529int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
530 unsigned long nr_pages)
531{
532 unsigned long i;
533 int err = 0;
534 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800535 struct vmem_altmap *altmap;
536
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700537 clear_zone_contiguous(zone);
538
David Rientjes4edd7ce2013-04-29 15:08:22 -0700539 /* during initialize mem_map, align hot-added range to section */
540 start_sec = pfn_to_section_nr(phys_start_pfn);
541 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
542
Dan Williams4b94ffd2016-01-15 16:56:22 -0800543 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
544 if (altmap) {
545 /*
546 * Validate altmap is within bounds of the total request
547 */
548 if (altmap->base_pfn != phys_start_pfn
549 || vmem_altmap_offset(altmap) > nr_pages) {
550 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700551 err = -EINVAL;
552 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800553 }
554 altmap->alloc = 0;
555 }
556
David Rientjes4edd7ce2013-04-29 15:08:22 -0700557 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700558 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700559
560 /*
561 * EEXIST is finally dealt with by ioresource collision
562 * check. see add_memory() => register_memory_resource()
563 * Warning will be printed if there is collision.
564 */
565 if (err && (err != -EEXIST))
566 break;
567 err = 0;
568 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700569 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700570out:
571 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700572 return err;
573}
574EXPORT_SYMBOL_GPL(__add_pages);
575
576#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800577/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
578static int find_smallest_section_pfn(int nid, struct zone *zone,
579 unsigned long start_pfn,
580 unsigned long end_pfn)
581{
582 struct mem_section *ms;
583
584 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
585 ms = __pfn_to_section(start_pfn);
586
587 if (unlikely(!valid_section(ms)))
588 continue;
589
590 if (unlikely(pfn_to_nid(start_pfn) != nid))
591 continue;
592
593 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
594 continue;
595
596 return start_pfn;
597 }
598
599 return 0;
600}
601
602/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
603static int find_biggest_section_pfn(int nid, struct zone *zone,
604 unsigned long start_pfn,
605 unsigned long end_pfn)
606{
607 struct mem_section *ms;
608 unsigned long pfn;
609
610 /* pfn is the end pfn of a memory section. */
611 pfn = end_pfn - 1;
612 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
613 ms = __pfn_to_section(pfn);
614
615 if (unlikely(!valid_section(ms)))
616 continue;
617
618 if (unlikely(pfn_to_nid(pfn) != nid))
619 continue;
620
621 if (zone && zone != page_zone(pfn_to_page(pfn)))
622 continue;
623
624 return pfn;
625 }
626
627 return 0;
628}
629
630static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
631 unsigned long end_pfn)
632{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700633 unsigned long zone_start_pfn = zone->zone_start_pfn;
634 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
635 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800636 unsigned long pfn;
637 struct mem_section *ms;
638 int nid = zone_to_nid(zone);
639
640 zone_span_writelock(zone);
641 if (zone_start_pfn == start_pfn) {
642 /*
643 * If the section is smallest section in the zone, it need
644 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
645 * In this case, we find second smallest valid mem_section
646 * for shrinking zone.
647 */
648 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
649 zone_end_pfn);
650 if (pfn) {
651 zone->zone_start_pfn = pfn;
652 zone->spanned_pages = zone_end_pfn - pfn;
653 }
654 } else if (zone_end_pfn == end_pfn) {
655 /*
656 * If the section is biggest section in the zone, it need
657 * shrink zone->spanned_pages.
658 * In this case, we find second biggest valid mem_section for
659 * shrinking zone.
660 */
661 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
662 start_pfn);
663 if (pfn)
664 zone->spanned_pages = pfn - zone_start_pfn + 1;
665 }
666
667 /*
668 * The section is not biggest or smallest mem_section in the zone, it
669 * only creates a hole in the zone. So in this case, we need not
670 * change the zone. But perhaps, the zone has only hole data. Thus
671 * it check the zone has only hole or not.
672 */
673 pfn = zone_start_pfn;
674 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
675 ms = __pfn_to_section(pfn);
676
677 if (unlikely(!valid_section(ms)))
678 continue;
679
680 if (page_zone(pfn_to_page(pfn)) != zone)
681 continue;
682
683 /* If the section is current section, it continues the loop */
684 if (start_pfn == pfn)
685 continue;
686
687 /* If we find valid section, we have nothing to do */
688 zone_span_writeunlock(zone);
689 return;
690 }
691
692 /* The zone has no valid section */
693 zone->zone_start_pfn = 0;
694 zone->spanned_pages = 0;
695 zone_span_writeunlock(zone);
696}
697
698static void shrink_pgdat_span(struct pglist_data *pgdat,
699 unsigned long start_pfn, unsigned long end_pfn)
700{
Xishi Qiu83285c72013-11-12 15:07:19 -0800701 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
702 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
703 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800704 unsigned long pfn;
705 struct mem_section *ms;
706 int nid = pgdat->node_id;
707
708 if (pgdat_start_pfn == start_pfn) {
709 /*
710 * If the section is smallest section in the pgdat, it need
711 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
712 * In this case, we find second smallest valid mem_section
713 * for shrinking zone.
714 */
715 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
716 pgdat_end_pfn);
717 if (pfn) {
718 pgdat->node_start_pfn = pfn;
719 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
720 }
721 } else if (pgdat_end_pfn == end_pfn) {
722 /*
723 * If the section is biggest section in the pgdat, it need
724 * shrink pgdat->node_spanned_pages.
725 * In this case, we find second biggest valid mem_section for
726 * shrinking zone.
727 */
728 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
729 start_pfn);
730 if (pfn)
731 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
732 }
733
734 /*
735 * If the section is not biggest or smallest mem_section in the pgdat,
736 * it only creates a hole in the pgdat. So in this case, we need not
737 * change the pgdat.
738 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
739 * has only hole or not.
740 */
741 pfn = pgdat_start_pfn;
742 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
743 ms = __pfn_to_section(pfn);
744
745 if (unlikely(!valid_section(ms)))
746 continue;
747
748 if (pfn_to_nid(pfn) != nid)
749 continue;
750
751 /* If the section is current section, it continues the loop */
752 if (start_pfn == pfn)
753 continue;
754
755 /* If we find valid section, we have nothing to do */
756 return;
757 }
758
759 /* The pgdat has no valid section */
760 pgdat->node_start_pfn = 0;
761 pgdat->node_spanned_pages = 0;
762}
763
764static void __remove_zone(struct zone *zone, unsigned long start_pfn)
765{
766 struct pglist_data *pgdat = zone->zone_pgdat;
767 int nr_pages = PAGES_PER_SECTION;
768 int zone_type;
769 unsigned long flags;
770
771 zone_type = zone - pgdat->node_zones;
772
773 pgdat_resize_lock(zone->zone_pgdat, &flags);
774 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
775 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
776 pgdat_resize_unlock(zone->zone_pgdat, &flags);
777}
778
Dan Williams4b94ffd2016-01-15 16:56:22 -0800779static int __remove_section(struct zone *zone, struct mem_section *ms,
780 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700781{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800782 unsigned long start_pfn;
783 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700784 int ret = -EINVAL;
785
786 if (!valid_section(ms))
787 return ret;
788
789 ret = unregister_memory_section(ms);
790 if (ret)
791 return ret;
792
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800793 scn_nr = __section_nr(ms);
794 start_pfn = section_nr_to_pfn(scn_nr);
795 __remove_zone(zone, start_pfn);
796
Dan Williams4b94ffd2016-01-15 16:56:22 -0800797 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700798 return 0;
799}
800
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700801/**
802 * __remove_pages() - remove sections of pages from a zone
803 * @zone: zone from which pages need to be removed
804 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
805 * @nr_pages: number of pages to remove (must be multiple of section size)
806 *
807 * Generic helper function to remove section mappings and sysfs entries
808 * for the section of the memory we are removing. Caller needs to make
809 * sure that pages are marked reserved and zones are adjust properly by
810 * calling offline_pages().
811 */
812int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
813 unsigned long nr_pages)
814{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700815 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800816 unsigned long map_offset = 0;
817 int sections_to_remove, ret = 0;
818
819 /* In the ZONE_DEVICE case device driver owns the memory region */
820 if (is_dev_zone(zone)) {
821 struct page *page = pfn_to_page(phys_start_pfn);
822 struct vmem_altmap *altmap;
823
824 altmap = to_vmem_altmap((unsigned long) page);
825 if (altmap)
826 map_offset = vmem_altmap_offset(altmap);
827 } else {
828 resource_size_t start, size;
829
830 start = phys_start_pfn << PAGE_SHIFT;
831 size = nr_pages * PAGE_SIZE;
832
833 ret = release_mem_region_adjustable(&iomem_resource, start,
834 size);
835 if (ret) {
836 resource_size_t endres = start + size - 1;
837
838 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
839 &start, &endres, ret);
840 }
841 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700842
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700843 clear_zone_contiguous(zone);
844
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700845 /*
846 * We can only remove entire sections
847 */
848 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
849 BUG_ON(nr_pages % PAGES_PER_SECTION);
850
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700851 sections_to_remove = nr_pages / PAGES_PER_SECTION;
852 for (i = 0; i < sections_to_remove; i++) {
853 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800854
855 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
856 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700857 if (ret)
858 break;
859 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700860
861 set_zone_contiguous(zone);
862
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700863 return ret;
864}
865EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700866#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700867
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700868int set_online_page_callback(online_page_callback_t callback)
869{
870 int rc = -EINVAL;
871
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700872 get_online_mems();
873 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700874
875 if (online_page_callback == generic_online_page) {
876 online_page_callback = callback;
877 rc = 0;
878 }
879
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700880 mutex_unlock(&online_page_callback_lock);
881 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700882
883 return rc;
884}
885EXPORT_SYMBOL_GPL(set_online_page_callback);
886
887int restore_online_page_callback(online_page_callback_t callback)
888{
889 int rc = -EINVAL;
890
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700891 get_online_mems();
892 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700893
894 if (online_page_callback == callback) {
895 online_page_callback = generic_online_page;
896 rc = 0;
897 }
898
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700899 mutex_unlock(&online_page_callback_lock);
900 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700901
902 return rc;
903}
904EXPORT_SYMBOL_GPL(restore_online_page_callback);
905
906void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700907{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700908}
909EXPORT_SYMBOL_GPL(__online_page_set_limits);
910
911void __online_page_increment_counters(struct page *page)
912{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700913 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700914}
915EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700916
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700917void __online_page_free(struct page *page)
918{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700919 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700920}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700921EXPORT_SYMBOL_GPL(__online_page_free);
922
923static void generic_online_page(struct page *page)
924{
925 __online_page_set_limits(page);
926 __online_page_increment_counters(page);
927 __online_page_free(page);
928}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700929
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700930static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
931 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700932{
933 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700934 unsigned long onlined_pages = *(unsigned long *)arg;
935 struct page *page;
936 if (PageReserved(pfn_to_page(start_pfn)))
937 for (i = 0; i < nr_pages; i++) {
938 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700939 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700940 onlined_pages++;
941 }
942 *(unsigned long *)arg = onlined_pages;
943 return 0;
944}
945
Lai Jiangshan09285af2012-12-12 13:52:04 -0800946#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800947/*
948 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
949 * normal memory.
950 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800951static bool can_online_high_movable(struct zone *zone)
952{
953 return true;
954}
Tang Chen79a4dce2012-12-18 14:23:24 -0800955#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800956/* ensure every online node has NORMAL memory */
957static bool can_online_high_movable(struct zone *zone)
958{
959 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
960}
Tang Chen79a4dce2012-12-18 14:23:24 -0800961#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800962
Lai Jiangshand9713672012-12-11 16:01:03 -0800963/* check which state of node_states will be changed when online memory */
964static void node_states_check_changes_online(unsigned long nr_pages,
965 struct zone *zone, struct memory_notify *arg)
966{
967 int nid = zone_to_nid(zone);
968 enum zone_type zone_last = ZONE_NORMAL;
969
970 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800971 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
972 * contains nodes which have zones of 0...ZONE_NORMAL,
973 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800974 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800975 * If we don't have HIGHMEM nor movable node,
976 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
977 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800978 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800979 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800980 zone_last = ZONE_MOVABLE;
981
982 /*
983 * if the memory to be online is in a zone of 0...zone_last, and
984 * the zones of 0...zone_last don't have memory before online, we will
985 * need to set the node to node_states[N_NORMAL_MEMORY] after
986 * the memory is online.
987 */
988 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
989 arg->status_change_nid_normal = nid;
990 else
991 arg->status_change_nid_normal = -1;
992
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800993#ifdef CONFIG_HIGHMEM
994 /*
995 * If we have movable node, node_states[N_HIGH_MEMORY]
996 * contains nodes which have zones of 0...ZONE_HIGHMEM,
997 * set zone_last to ZONE_HIGHMEM.
998 *
999 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1000 * contains nodes which have zones of 0...ZONE_MOVABLE,
1001 * set zone_last to ZONE_MOVABLE.
1002 */
1003 zone_last = ZONE_HIGHMEM;
1004 if (N_MEMORY == N_HIGH_MEMORY)
1005 zone_last = ZONE_MOVABLE;
1006
1007 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1008 arg->status_change_nid_high = nid;
1009 else
1010 arg->status_change_nid_high = -1;
1011#else
1012 arg->status_change_nid_high = arg->status_change_nid_normal;
1013#endif
1014
Lai Jiangshand9713672012-12-11 16:01:03 -08001015 /*
1016 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001017 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001018 * is online.
1019 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001020 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001021 arg->status_change_nid = nid;
1022 else
1023 arg->status_change_nid = -1;
1024}
1025
1026static void node_states_set_node(int node, struct memory_notify *arg)
1027{
1028 if (arg->status_change_nid_normal >= 0)
1029 node_set_state(node, N_NORMAL_MEMORY);
1030
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001031 if (arg->status_change_nid_high >= 0)
1032 node_set_state(node, N_HIGH_MEMORY);
1033
1034 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001035}
1036
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001037bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1038 enum zone_type target, int *zone_shift)
Reza Arbabdf429ac2016-07-26 15:22:23 -07001039{
1040 struct zone *zone = page_zone(pfn_to_page(pfn));
1041 enum zone_type idx = zone_idx(zone);
1042 int i;
1043
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001044 *zone_shift = 0;
1045
Reza Arbabdf429ac2016-07-26 15:22:23 -07001046 if (idx < target) {
1047 /* pages must be at end of current zone */
1048 if (pfn + nr_pages != zone_end_pfn(zone))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001049 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001050
1051 /* no zones in use between current zone and target */
1052 for (i = idx + 1; i < target; i++)
1053 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001054 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001055 }
1056
1057 if (target < idx) {
1058 /* pages must be at beginning of current zone */
1059 if (pfn != zone->zone_start_pfn)
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001060 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001061
1062 /* no zones in use between current zone and target */
1063 for (i = target + 1; i < idx; i++)
1064 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001065 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001066 }
1067
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001068 *zone_shift = target - idx;
1069 return true;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001070}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001071
David Rientjes30467e02015-04-14 15:45:11 -07001072/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001073int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001074{
Cody P Schaferaa472282013-07-03 15:02:10 -07001075 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001076 unsigned long onlined_pages = 0;
1077 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001078 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001079 int nid;
1080 int ret;
1081 struct memory_notify arg;
Reza Arbabe51e6c82016-07-26 15:22:20 -07001082 int zone_shift = 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001083
Lai Jiangshand9713672012-12-11 16:01:03 -08001084 /*
1085 * This doesn't need a lock to do pfn_to_page().
1086 * The section can't be removed here because of the
1087 * memory_block->state_mutex.
1088 */
1089 zone = page_zone(pfn_to_page(pfn));
1090
Tang Chen4f7c6b42014-08-06 16:05:13 -07001091 if ((zone_idx(zone) > ZONE_NORMAL ||
1092 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001093 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001094 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001095
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001096 if (online_type == MMOP_ONLINE_KERNEL) {
1097 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1098 return -EINVAL;
1099 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1100 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1101 return -EINVAL;
1102 }
Reza Arbabe51e6c82016-07-26 15:22:20 -07001103
1104 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1105 if (!zone)
1106 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001107
Yasunori Goto7b78d332007-10-21 16:41:36 -07001108 arg.start_pfn = pfn;
1109 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001110 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001111
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001112 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001113
1114 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1115 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001116 if (ret)
1117 goto failed_addition;
1118
Dave Hansen3947be12005-10-29 18:16:54 -07001119 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001120 * If this zone is not populated, then it is not in zonelist.
1121 * This means the page allocator ignores this zone.
1122 * So, zonelist must be updated after online.
1123 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001124 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001125 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001126 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001127 build_all_zonelists(NULL, zone);
1128 }
Yasunori Goto68113782006-06-23 02:03:11 -07001129
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001130 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001131 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001132 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001133 if (need_zonelists_rebuild)
1134 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001135 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001136 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001137 }
1138
Dave Hansen3947be12005-10-29 18:16:54 -07001139 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001140
1141 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001142 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001143 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1144
Jiang Liu08dff7b2012-07-31 16:43:30 -07001145 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001146 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001147 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001148 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001149 else
1150 zone_pcp_update(zone);
1151 }
Dave Hansen3947be12005-10-29 18:16:54 -07001152
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001153 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001154
1155 init_per_zone_wmark_min();
1156
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001157 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001158 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001159 kcompactd_run(nid);
1160 }
Dave Hansen61b13992005-10-29 18:16:56 -07001161
Haicheng Li1f522502010-05-24 14:32:51 -07001162 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001163
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001164 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001165
1166 if (onlined_pages)
1167 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001168 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001169
1170failed_addition:
1171 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1172 (unsigned long long) pfn << PAGE_SHIFT,
1173 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1174 memory_notify(MEM_CANCEL_ONLINE, &arg);
1175 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001176}
Keith Mannthey53947022006-09-30 23:27:08 -07001177#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001178
Tang Chen0bd85422014-11-13 15:19:41 -08001179static void reset_node_present_pages(pg_data_t *pgdat)
1180{
1181 struct zone *z;
1182
1183 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1184 z->present_pages = 0;
1185
1186 pgdat->node_present_pages = 0;
1187}
1188
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001189/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1190static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001191{
1192 struct pglist_data *pgdat;
1193 unsigned long zones_size[MAX_NR_ZONES] = {0};
1194 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001195 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001196
Tang Chena1e565a2013-02-22 16:33:18 -08001197 pgdat = NODE_DATA(nid);
1198 if (!pgdat) {
1199 pgdat = arch_alloc_nodedata(nid);
1200 if (!pgdat)
1201 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001202
Tang Chena1e565a2013-02-22 16:33:18 -08001203 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001204 } else {
Mel Gorman38087d92016-07-28 15:45:49 -07001205 /* Reset the nr_zones, order and classzone_idx before reuse */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001206 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001207 pgdat->kswapd_order = 0;
1208 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001209 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001210
1211 /* we can use NODE_DATA(nid) from here */
1212
1213 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001214 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001215 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001216
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001217 /*
1218 * The node we allocated has no zone fallback lists. For avoiding
1219 * to access not-initialized zonelist, build here.
1220 */
David Rientjesf957db42011-06-22 18:13:04 -07001221 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001222 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001223 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001224
Tang Chenf784a3f2014-11-13 15:19:39 -08001225 /*
1226 * zone->managed_pages is set to an approximate value in
1227 * free_area_init_core(), which will cause
1228 * /sys/device/system/node/nodeX/meminfo has wrong data.
1229 * So reset it to 0 before any memory is onlined.
1230 */
1231 reset_node_managed_pages(pgdat);
1232
Tang Chen0bd85422014-11-13 15:19:41 -08001233 /*
1234 * When memory is hot-added, all the memory is in offline state. So
1235 * clear all zones' present_pages because they will be updated in
1236 * online_pages() and offline_pages().
1237 */
1238 reset_node_present_pages(pgdat);
1239
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001240 return pgdat;
1241}
1242
1243static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1244{
1245 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001246 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001247 arch_free_nodedata(pgdat);
1248 return;
1249}
1250
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001251
Toshi Kani01b0f192013-11-12 15:07:25 -08001252/**
1253 * try_online_node - online a node if offlined
1254 *
minskey guocf234222010-05-24 14:32:41 -07001255 * called by cpu_up() to online a node without onlined memory.
1256 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001257int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001258{
1259 pg_data_t *pgdat;
1260 int ret;
1261
Toshi Kani01b0f192013-11-12 15:07:25 -08001262 if (node_online(nid))
1263 return 0;
1264
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001265 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001266 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001267 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001268 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001269 ret = -ENOMEM;
1270 goto out;
1271 }
1272 node_set_online(nid);
1273 ret = register_one_node(nid);
1274 BUG_ON(ret);
1275
Toshi Kani01b0f192013-11-12 15:07:25 -08001276 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1277 mutex_lock(&zonelists_mutex);
1278 build_all_zonelists(NULL, NULL);
1279 mutex_unlock(&zonelists_mutex);
1280 }
1281
minskey guocf234222010-05-24 14:32:41 -07001282out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001283 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001284 return ret;
1285}
1286
Toshi Kani27356f52013-09-11 14:21:49 -07001287static int check_hotplug_memory_range(u64 start, u64 size)
1288{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001289 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001290 u64 nr_pages = size >> PAGE_SHIFT;
1291
1292 /* Memory range must be aligned with section */
1293 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1294 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1295 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1296 (unsigned long long)start,
1297 (unsigned long long)size);
1298 return -EINVAL;
1299 }
1300
1301 return 0;
1302}
1303
Wang Nan63264402014-08-06 16:07:36 -07001304/*
1305 * If movable zone has already been setup, newly added memory should be check.
1306 * If its address is higher than movable zone, it should be added as movable.
1307 * Without this check, movable zone may overlap with other zone.
1308 */
1309static int should_add_memory_movable(int nid, u64 start, u64 size)
1310{
1311 unsigned long start_pfn = start >> PAGE_SHIFT;
1312 pg_data_t *pgdat = NODE_DATA(nid);
1313 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1314
1315 if (zone_is_empty(movable_zone))
1316 return 0;
1317
1318 if (movable_zone->zone_start_pfn <= start_pfn)
1319 return 1;
1320
1321 return 0;
1322}
1323
Dan Williams033fbae2015-08-09 15:29:06 -04001324int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1325 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001326{
Dan Williams033fbae2015-08-09 15:29:06 -04001327#ifdef CONFIG_ZONE_DEVICE
1328 if (for_device)
1329 return ZONE_DEVICE;
1330#endif
Wang Nan63264402014-08-06 16:07:36 -07001331 if (should_add_memory_movable(nid, start, size))
1332 return ZONE_MOVABLE;
1333
1334 return zone_default;
1335}
1336
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001337static int online_memory_block(struct memory_block *mem, void *arg)
1338{
1339 return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1340}
1341
Al Viro31168482008-11-22 17:33:24 +00001342/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001343int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001344{
David Vrabel62cedb92015-06-25 16:35:49 +01001345 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001346 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001347 bool new_pgdat;
1348 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001349 int ret;
1350
David Vrabel62cedb92015-06-25 16:35:49 +01001351 start = res->start;
1352 size = resource_size(res);
1353
Toshi Kani27356f52013-09-11 14:21:49 -07001354 ret = check_hotplug_memory_range(start, size);
1355 if (ret)
1356 return ret;
1357
Tang Chena1e565a2013-02-22 16:33:18 -08001358 { /* Stupid hack to suppress address-never-null warning */
1359 void *p = NODE_DATA(nid);
1360 new_pgdat = !p;
1361 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001362
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001363 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001364
Tang Chen7f36e3e2015-09-04 15:42:32 -07001365 /*
1366 * Add new range to memblock so that when hotadd_new_pgdat() is called
1367 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1368 * this new range and calculate total pages correctly. The range will
1369 * be removed at hot-remove time.
1370 */
1371 memblock_add_node(start, size, nid);
1372
Tang Chena1e565a2013-02-22 16:33:18 -08001373 new_node = !node_online(nid);
1374 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001375 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001376 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001377 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001378 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001379 }
1380
Yasunori Gotobc02af92006-06-27 02:53:30 -07001381 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001382 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001383
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001384 if (ret < 0)
1385 goto error;
1386
Yasunori Goto0fc44152006-06-27 02:53:38 -07001387 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001388 node_set_online(nid);
1389
Tang Chena1e565a2013-02-22 16:33:18 -08001390 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001391 ret = register_one_node(nid);
1392 /*
1393 * If sysfs file of new node can't create, cpu on the node
1394 * can't be hot-added. There is no rollback way now.
1395 * So, check by BUG_ON() to catch it reluctantly..
1396 */
1397 BUG_ON(ret);
1398 }
1399
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001400 /* create new memmap entry */
1401 firmware_map_add_hotplug(start, start + size, "System RAM");
1402
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001403 /* online pages if requested */
1404 if (online)
1405 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1406 NULL, online_memory_block);
1407
Andi Kleen6ad696d2009-11-17 14:06:22 -08001408 goto out;
1409
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001410error:
1411 /* rollback pgdat allocation and others */
1412 if (new_pgdat)
1413 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001414 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001415
Andi Kleen6ad696d2009-11-17 14:06:22 -08001416out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001417 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001418 return ret;
1419}
David Vrabel62cedb92015-06-25 16:35:49 +01001420EXPORT_SYMBOL_GPL(add_memory_resource);
1421
1422int __ref add_memory(int nid, u64 start, u64 size)
1423{
1424 struct resource *res;
1425 int ret;
1426
1427 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001428 if (IS_ERR(res))
1429 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001430
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001431 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001432 if (ret < 0)
1433 release_memory_resource(res);
1434 return ret;
1435}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001436EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001437
1438#ifdef CONFIG_MEMORY_HOTREMOVE
1439/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001440 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1441 * set and the size of the free page is given by page_order(). Using this,
1442 * the function determines if the pageblock contains only free pages.
1443 * Due to buddy contraints, a free page at least the size of a pageblock will
1444 * be located at the start of the pageblock
1445 */
1446static inline int pageblock_free(struct page *page)
1447{
1448 return PageBuddy(page) && page_order(page) >= pageblock_order;
1449}
1450
1451/* Return the start of the next active pageblock after a given page */
1452static struct page *next_active_pageblock(struct page *page)
1453{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001454 /* Ensure the starting page is pageblock-aligned */
1455 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1456
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001457 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001458 if (pageblock_free(page)) {
1459 int order;
1460 /* be careful. we don't have locks, page_order can be changed.*/
1461 order = page_order(page);
1462 if ((order < MAX_ORDER) && (order >= pageblock_order))
1463 return page + (1 << order);
1464 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001465
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001466 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001467}
1468
1469/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001470bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001471{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001472 struct page *page = pfn_to_page(start_pfn);
1473 struct page *end_page = page + nr_pages;
1474
1475 /* Check the starting page of each pageblock within the range */
1476 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001477 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001478 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001479 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001480 }
1481
1482 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001483 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001484}
1485
1486/*
Toshi Kani72f74192017-02-03 13:13:20 -08001487 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kani6cb04972017-02-03 13:13:23 -08001488 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001489 */
Toshi Kani6cb04972017-02-03 13:13:23 -08001490int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1491 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001492{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001493 unsigned long pfn, sec_end_pfn;
Toshi Kani6cb04972017-02-03 13:13:23 -08001494 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001495 struct zone *zone = NULL;
1496 struct page *page;
1497 int i;
Toshi Kani72f74192017-02-03 13:13:20 -08001498 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001499 pfn < end_pfn;
Toshi Kani72f74192017-02-03 13:13:20 -08001500 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001501 /* Make sure the memory section is present first */
1502 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001503 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001504 for (; pfn < sec_end_pfn && pfn < end_pfn;
1505 pfn += MAX_ORDER_NR_PAGES) {
1506 i = 0;
1507 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1508 while ((i < MAX_ORDER_NR_PAGES) &&
1509 !pfn_valid_within(pfn + i))
1510 i++;
1511 if (i == MAX_ORDER_NR_PAGES)
1512 continue;
1513 page = pfn_to_page(pfn + i);
1514 if (zone && page_zone(page) != zone)
1515 return 0;
Toshi Kani6cb04972017-02-03 13:13:23 -08001516 if (!zone)
1517 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001518 zone = page_zone(page);
Toshi Kani6cb04972017-02-03 13:13:23 -08001519 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001520 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001521 }
Toshi Kani72f74192017-02-03 13:13:20 -08001522
Toshi Kani6cb04972017-02-03 13:13:23 -08001523 if (zone) {
1524 *valid_start = start;
1525 *valid_end = end;
Toshi Kani72f74192017-02-03 13:13:20 -08001526 return 1;
Toshi Kani6cb04972017-02-03 13:13:23 -08001527 } else {
Toshi Kani72f74192017-02-03 13:13:20 -08001528 return 0;
Toshi Kani6cb04972017-02-03 13:13:23 -08001529 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001530}
1531
1532/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001533 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1534 * and hugepages). We scan pfn because it's much easier than scanning over
1535 * linked list. This function returns the pfn of the first found movable
1536 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001537 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001538static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001539{
1540 unsigned long pfn;
1541 struct page *page;
1542 for (pfn = start; pfn < end; pfn++) {
1543 if (pfn_valid(pfn)) {
1544 page = pfn_to_page(pfn);
1545 if (PageLRU(page))
1546 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001547 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001548 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001549 return pfn;
1550 else
1551 pfn = round_up(pfn + 1,
1552 1 << compound_order(page)) - 1;
1553 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001554 }
1555 }
1556 return 0;
1557}
1558
Xishi Qiu394e31d2016-07-28 15:48:53 -07001559static struct page *new_node_page(struct page *page, unsigned long private,
1560 int **result)
1561{
1562 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1563 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001564 nodemask_t nmask = node_states[N_MEMORY];
1565 struct page *new_page = NULL;
Xishi Qiu394e31d2016-07-28 15:48:53 -07001566
1567 /*
1568 * TODO: allocate a destination hugepage from a nearest neighbor node,
1569 * accordance with memory policy of the user process if possible. For
1570 * now as a simple work-around, we use the next node for destination.
1571 */
1572 if (PageHuge(page))
1573 return alloc_huge_page_node(page_hstate(compound_head(page)),
1574 next_node_in(nid, nmask));
1575
Li Zhong231e97e2016-09-28 15:22:38 -07001576 node_clear(nid, nmask);
Li Zhong9bb627b2016-09-19 14:43:52 -07001577
Xishi Qiu394e31d2016-07-28 15:48:53 -07001578 if (PageHighMem(page)
1579 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1580 gfp_mask |= __GFP_HIGHMEM;
1581
Li Zhong231e97e2016-09-28 15:22:38 -07001582 if (!nodes_empty(nmask))
1583 new_page = __alloc_pages_nodemask(gfp_mask, 0,
Xishi Qiu394e31d2016-07-28 15:48:53 -07001584 node_zonelist(nid, gfp_mask), &nmask);
1585 if (!new_page)
1586 new_page = __alloc_pages(gfp_mask, 0,
1587 node_zonelist(nid, gfp_mask));
1588
1589 return new_page;
1590}
1591
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001592#define NR_OFFLINE_AT_ONCE_PAGES (256)
1593static int
1594do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1595{
1596 unsigned long pfn;
1597 struct page *page;
1598 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1599 int not_managed = 0;
1600 int ret = 0;
1601 LIST_HEAD(source);
1602
1603 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1604 if (!pfn_valid(pfn))
1605 continue;
1606 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001607
1608 if (PageHuge(page)) {
1609 struct page *head = compound_head(page);
1610 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1611 if (compound_order(head) > PFN_SECTION_SHIFT) {
1612 ret = -EBUSY;
1613 break;
1614 }
1615 if (isolate_huge_page(page, &source))
1616 move_pages -= 1 << compound_order(head);
1617 continue;
1618 }
1619
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001620 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001621 continue;
1622 /*
1623 * We can skip free pages. And we can only deal with pages on
1624 * LRU.
1625 */
Nick Piggin62695a82008-10-18 20:26:09 -07001626 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001627 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001628 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001629 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001630 move_pages--;
Mel Gorman599d0c92016-07-28 15:45:31 -07001631 inc_node_page_state(page, NR_ISOLATED_ANON +
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001632 page_is_file_cache(page));
1633
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001634 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001635#ifdef CONFIG_DEBUG_VM
Chen Yuconge33e33b2016-03-17 14:19:35 -07001636 pr_alert("removing pfn %lx from LRU failed\n", pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001637 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001638#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001639 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001640 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001641 check this again here. */
1642 if (page_count(page)) {
1643 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001644 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001645 break;
1646 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001647 }
1648 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001649 if (!list_empty(&source)) {
1650 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001651 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001652 goto out;
1653 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001654
Xishi Qiu394e31d2016-07-28 15:48:53 -07001655 /* Allocate a new page from the nearest neighbor node */
1656 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001657 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001658 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001659 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001660 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001661out:
1662 return ret;
1663}
1664
1665/*
1666 * remove from free_area[] and mark all as Reserved.
1667 */
1668static int
1669offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1670 void *data)
1671{
1672 __offline_isolated_pages(start, start + nr_pages);
1673 return 0;
1674}
1675
1676static void
1677offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1678{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001679 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001680 offline_isolated_pages_cb);
1681}
1682
1683/*
1684 * Check all pages in range, recoreded as memory resource, are isolated.
1685 */
1686static int
1687check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1688 void *data)
1689{
1690 int ret;
1691 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001692 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001693 offlined = nr_pages;
1694 if (!ret)
1695 *(long *)data += offlined;
1696 return ret;
1697}
1698
1699static long
1700check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1701{
1702 long offlined = 0;
1703 int ret;
1704
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001705 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001706 check_pages_isolated_cb);
1707 if (ret < 0)
1708 offlined = (long)ret;
1709 return offlined;
1710}
1711
Lai Jiangshan09285af2012-12-12 13:52:04 -08001712#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001713/*
1714 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1715 * normal memory.
1716 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001717static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1718{
1719 return true;
1720}
Tang Chen79a4dce2012-12-18 14:23:24 -08001721#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001722/* ensure the node has NORMAL memory if it is still online */
1723static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1724{
1725 struct pglist_data *pgdat = zone->zone_pgdat;
1726 unsigned long present_pages = 0;
1727 enum zone_type zt;
1728
1729 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1730 present_pages += pgdat->node_zones[zt].present_pages;
1731
1732 if (present_pages > nr_pages)
1733 return true;
1734
1735 present_pages = 0;
1736 for (; zt <= ZONE_MOVABLE; zt++)
1737 present_pages += pgdat->node_zones[zt].present_pages;
1738
1739 /*
1740 * we can't offline the last normal memory until all
1741 * higher memory is offlined.
1742 */
1743 return present_pages == 0;
1744}
Tang Chen79a4dce2012-12-18 14:23:24 -08001745#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001746
Tang Chenc5320922013-11-12 15:08:10 -08001747static int __init cmdline_parse_movable_node(char *p)
1748{
1749#ifdef CONFIG_MOVABLE_NODE
1750 /*
1751 * Memory used by the kernel cannot be hot-removed because Linux
1752 * cannot migrate the kernel pages. When memory hotplug is
1753 * enabled, we should prevent memblock from allocating memory
1754 * for the kernel.
1755 *
1756 * ACPI SRAT records all hotpluggable memory ranges. But before
1757 * SRAT is parsed, we don't know about it.
1758 *
1759 * The kernel image is loaded into memory at very early time. We
1760 * cannot prevent this anyway. So on NUMA system, we set any
1761 * node the kernel resides in as un-hotpluggable.
1762 *
1763 * Since on modern servers, one node could have double-digit
1764 * gigabytes memory, we can assume the memory around the kernel
1765 * image is also un-hotpluggable. So before SRAT is parsed, just
1766 * allocate memory near the kernel image to try the best to keep
1767 * the kernel away from hotpluggable memory.
1768 */
1769 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001770 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001771#else
1772 pr_warn("movable_node option not supported\n");
1773#endif
1774 return 0;
1775}
1776early_param("movable_node", cmdline_parse_movable_node);
1777
Lai Jiangshand9713672012-12-11 16:01:03 -08001778/* check which state of node_states will be changed when offline memory */
1779static void node_states_check_changes_offline(unsigned long nr_pages,
1780 struct zone *zone, struct memory_notify *arg)
1781{
1782 struct pglist_data *pgdat = zone->zone_pgdat;
1783 unsigned long present_pages = 0;
1784 enum zone_type zt, zone_last = ZONE_NORMAL;
1785
1786 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001787 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1788 * contains nodes which have zones of 0...ZONE_NORMAL,
1789 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001790 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001791 * If we don't have HIGHMEM nor movable node,
1792 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1793 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001794 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001795 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001796 zone_last = ZONE_MOVABLE;
1797
1798 /*
1799 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1800 * If the memory to be offline is in a zone of 0...zone_last,
1801 * and it is the last present memory, 0...zone_last will
1802 * become empty after offline , thus we can determind we will
1803 * need to clear the node from node_states[N_NORMAL_MEMORY].
1804 */
1805 for (zt = 0; zt <= zone_last; zt++)
1806 present_pages += pgdat->node_zones[zt].present_pages;
1807 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1808 arg->status_change_nid_normal = zone_to_nid(zone);
1809 else
1810 arg->status_change_nid_normal = -1;
1811
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001812#ifdef CONFIG_HIGHMEM
1813 /*
1814 * If we have movable node, node_states[N_HIGH_MEMORY]
1815 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1816 * set zone_last to ZONE_HIGHMEM.
1817 *
1818 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1819 * contains nodes which have zones of 0...ZONE_MOVABLE,
1820 * set zone_last to ZONE_MOVABLE.
1821 */
1822 zone_last = ZONE_HIGHMEM;
1823 if (N_MEMORY == N_HIGH_MEMORY)
1824 zone_last = ZONE_MOVABLE;
1825
1826 for (; zt <= zone_last; zt++)
1827 present_pages += pgdat->node_zones[zt].present_pages;
1828 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1829 arg->status_change_nid_high = zone_to_nid(zone);
1830 else
1831 arg->status_change_nid_high = -1;
1832#else
1833 arg->status_change_nid_high = arg->status_change_nid_normal;
1834#endif
1835
Lai Jiangshand9713672012-12-11 16:01:03 -08001836 /*
1837 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1838 */
1839 zone_last = ZONE_MOVABLE;
1840
1841 /*
1842 * check whether node_states[N_HIGH_MEMORY] will be changed
1843 * If we try to offline the last present @nr_pages from the node,
1844 * we can determind we will need to clear the node from
1845 * node_states[N_HIGH_MEMORY].
1846 */
1847 for (; zt <= zone_last; zt++)
1848 present_pages += pgdat->node_zones[zt].present_pages;
1849 if (nr_pages >= present_pages)
1850 arg->status_change_nid = zone_to_nid(zone);
1851 else
1852 arg->status_change_nid = -1;
1853}
1854
1855static void node_states_clear_node(int node, struct memory_notify *arg)
1856{
1857 if (arg->status_change_nid_normal >= 0)
1858 node_clear_state(node, N_NORMAL_MEMORY);
1859
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001860 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1861 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001862 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001863
1864 if ((N_MEMORY != N_HIGH_MEMORY) &&
1865 (arg->status_change_nid >= 0))
1866 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001867}
1868
Wen Congyanga16cee12012-10-08 16:33:58 -07001869static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001870 unsigned long end_pfn, unsigned long timeout)
1871{
1872 unsigned long pfn, nr_pages, expire;
1873 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001874 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001875 unsigned long flags;
Toshi Kani6cb04972017-02-03 13:13:23 -08001876 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001877 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001878 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001879
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001880 /* at least, alignment against pageblock is necessary */
1881 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1882 return -EINVAL;
1883 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1884 return -EINVAL;
1885 /* This makes hotplug much easier...and readable.
1886 we assume this for now. .*/
Toshi Kani6cb04972017-02-03 13:13:23 -08001887 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001888 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001889
Toshi Kani6cb04972017-02-03 13:13:23 -08001890 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001891 node = zone_to_nid(zone);
1892 nr_pages = end_pfn - start_pfn;
1893
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001894 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001895 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001896
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001897 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001898 ret = start_isolate_page_range(start_pfn, end_pfn,
1899 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001900 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001901 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001902
1903 arg.start_pfn = start_pfn;
1904 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001905 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001906
1907 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1908 ret = notifier_to_errno(ret);
1909 if (ret)
1910 goto failed_removal;
1911
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001912 pfn = start_pfn;
1913 expire = jiffies + timeout;
1914 drain = 0;
1915 retry_max = 5;
1916repeat:
1917 /* start memory hot removal */
1918 ret = -EAGAIN;
1919 if (time_after(jiffies, expire))
1920 goto failed_removal;
1921 ret = -EINTR;
1922 if (signal_pending(current))
1923 goto failed_removal;
1924 ret = 0;
1925 if (drain) {
1926 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001927 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001928 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001929 }
1930
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001931 pfn = scan_movable_pages(start_pfn, end_pfn);
1932 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001933 ret = do_migrate_range(pfn, end_pfn);
1934 if (!ret) {
1935 drain = 1;
1936 goto repeat;
1937 } else {
1938 if (ret < 0)
1939 if (--retry_max == 0)
1940 goto failed_removal;
1941 yield();
1942 drain = 1;
1943 goto repeat;
1944 }
1945 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001946 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001947 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001948 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001949 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001950 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001951 /*
1952 * dissolve free hugepages in the memory block before doing offlining
1953 * actually in order to make hugetlbfs's object counting consistent.
1954 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001955 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1956 if (ret)
1957 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001958 /* check again */
1959 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1960 if (offlined_pages < 0) {
1961 ret = -EBUSY;
1962 goto failed_removal;
1963 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001964 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001965 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001966 We cannot do rollback at this point. */
1967 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001968 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001969 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001970 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001971 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001972 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001973
1974 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001975 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001976 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001977
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001978 init_per_zone_wmark_min();
1979
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001980 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001981 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001982 mutex_lock(&zonelists_mutex);
1983 build_all_zonelists(NULL, NULL);
1984 mutex_unlock(&zonelists_mutex);
1985 } else
1986 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001987
Lai Jiangshand9713672012-12-11 16:01:03 -08001988 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001989 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001990 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001991 kcompactd_stop(node);
1992 }
Minchan Kimbce73942009-06-16 15:32:50 -07001993
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001994 vm_total_pages = nr_free_pagecache_pages();
1995 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001996
1997 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001998 return 0;
1999
2000failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07002001 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
2002 (unsigned long long) start_pfn << PAGE_SHIFT,
2003 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07002004 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002005 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02002006 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002007 return ret;
2008}
Badari Pulavarty71088782008-10-18 20:25:58 -07002009
David Rientjes30467e02015-04-14 15:45:11 -07002010/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07002011int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
2012{
2013 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
2014}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002015#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07002016
Wen Congyangbbc76be2013-02-22 16:32:54 -08002017/**
2018 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2019 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07002020 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08002021 * @arg: argument passed to func
2022 * @func: callback for each memory section walked
2023 *
2024 * This function walks through all present mem sections in range
2025 * [start_pfn, end_pfn) and call func on each mem section.
2026 *
2027 * Returns the return value of func.
2028 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002029int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08002030 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07002031{
Wen Congyange90bdb72012-10-08 16:34:01 -07002032 struct memory_block *mem = NULL;
2033 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07002034 unsigned long pfn, section_nr;
2035 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07002036
Wen Congyange90bdb72012-10-08 16:34:01 -07002037 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2038 section_nr = pfn_to_section_nr(pfn);
2039 if (!present_section_nr(section_nr))
2040 continue;
2041
2042 section = __nr_to_section(section_nr);
2043 /* same memblock? */
2044 if (mem)
2045 if ((section_nr >= mem->start_section_nr) &&
2046 (section_nr <= mem->end_section_nr))
2047 continue;
2048
2049 mem = find_memory_block_hinted(section, mem);
2050 if (!mem)
2051 continue;
2052
Wen Congyangbbc76be2013-02-22 16:32:54 -08002053 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07002054 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08002055 kobject_put(&mem->dev.kobj);
2056 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07002057 }
2058 }
2059
2060 if (mem)
2061 kobject_put(&mem->dev.kobj);
2062
Wen Congyangbbc76be2013-02-22 16:32:54 -08002063 return 0;
2064}
2065
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002066#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08002067static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002068{
2069 int ret = !is_memblock_offlined(mem);
2070
Randy Dunlap349daa02013-04-29 15:08:49 -07002071 if (unlikely(ret)) {
2072 phys_addr_t beginpa, endpa;
2073
2074 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2075 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a025f02016-03-17 14:19:47 -07002076 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07002077 &beginpa, &endpa);
2078 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08002079
2080 return ret;
2081}
2082
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002083static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08002084{
Tang Chen60a5a192013-02-22 16:33:14 -08002085 int cpu;
2086
2087 for_each_present_cpu(cpu) {
2088 if (cpu_to_node(cpu) == pgdat->node_id)
2089 /*
2090 * the cpu on this node isn't removed, and we can't
2091 * offline this node.
2092 */
2093 return -EBUSY;
2094 }
2095
2096 return 0;
2097}
2098
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002099static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002100{
2101#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002102 int cpu;
2103
2104 for_each_possible_cpu(cpu)
2105 if (cpu_to_node(cpu) == pgdat->node_id)
2106 numa_clear_node(cpu);
2107#endif
2108}
2109
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002110static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002111{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002112 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002113
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002114 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002115 if (ret)
2116 return ret;
2117
2118 /*
2119 * the node will be offlined when we come here, so we can clear
2120 * the cpu_to_node() now.
2121 */
2122
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002123 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002124 return 0;
2125}
2126
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002127/**
2128 * try_offline_node
2129 *
2130 * Offline a node if all memory sections and cpus of the node are removed.
2131 *
2132 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2133 * and online/offline operations before this call.
2134 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002135void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002136{
Wen Congyangd822b862013-02-22 16:33:16 -08002137 pg_data_t *pgdat = NODE_DATA(nid);
2138 unsigned long start_pfn = pgdat->node_start_pfn;
2139 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002140 unsigned long pfn;
2141
2142 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2143 unsigned long section_nr = pfn_to_section_nr(pfn);
2144
2145 if (!present_section_nr(section_nr))
2146 continue;
2147
2148 if (pfn_to_nid(pfn) != nid)
2149 continue;
2150
2151 /*
2152 * some memory sections of this node are not removed, and we
2153 * can't offline node now.
2154 */
2155 return;
2156 }
2157
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002158 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002159 return;
2160
2161 /*
2162 * all memory/cpu of this node are removed, we can offline this
2163 * node now.
2164 */
2165 node_set_offline(nid);
2166 unregister_one_node(nid);
2167}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002168EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002169
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002170/**
2171 * remove_memory
2172 *
2173 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2174 * and online/offline operations before this call, as required by
2175 * try_offline_node().
2176 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002177void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002178{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002179 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002180
Toshi Kani27356f52013-09-11 14:21:49 -07002181 BUG_ON(check_hotplug_memory_range(start, size));
2182
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002183 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002184
2185 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002186 * All memory blocks must be offlined before removing memory. Check
2187 * whether all memory blocks in question are offline and trigger a BUG()
2188 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002189 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002190 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002191 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002192 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002193 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002194
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002195 /* remove memmap entry */
2196 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002197 memblock_free(start, size);
2198 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002199
Wen Congyang24d335c2013-02-22 16:32:58 -08002200 arch_remove_memory(start, size);
2201
Tang Chen60a5a192013-02-22 16:33:14 -08002202 try_offline_node(nid);
2203
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002204 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002205}
Badari Pulavarty71088782008-10-18 20:25:58 -07002206EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002207#endif /* CONFIG_MEMORY_HOTREMOVE */