blob: 1d3ed58f92abe199644399d0fe59da3f7906dcfb [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{
Dan Williams3fc21922017-02-24 14:55:48 -0800129 assert_held_device_hotplug();
130
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700131 mem_hotplug.active_writer = current;
132
133 memhp_lock_acquire();
134 for (;;) {
135 mutex_lock(&mem_hotplug.lock);
136 if (likely(!mem_hotplug.refcount))
137 break;
138 __set_current_state(TASK_UNINTERRUPTIBLE);
139 mutex_unlock(&mem_hotplug.lock);
140 schedule();
141 }
142}
143
David Rientjes30467e02015-04-14 15:45:11 -0700144void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700145{
146 mem_hotplug.active_writer = NULL;
147 mutex_unlock(&mem_hotplug.lock);
148 memhp_lock_release();
149}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800150
Keith Mannthey45e0b782006-09-30 23:27:09 -0700151/* add this memory to iomem resource */
152static struct resource *register_memory_resource(u64 start, u64 size)
153{
154 struct resource *res;
155 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800156 if (!res)
157 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700158
159 res->name = "System RAM";
160 res->start = start;
161 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100162 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700163 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700164 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700165 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800166 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700167 }
168 return res;
169}
170
171static void release_memory_resource(struct resource *res)
172{
173 if (!res)
174 return;
175 release_resource(res);
176 kfree(res);
177 return;
178}
179
Keith Mannthey53947022006-09-30 23:27:08 -0700180#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800181void get_page_bootmem(unsigned long info, struct page *page,
182 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700183{
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800184 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700185 SetPagePrivate(page);
186 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700187 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700188}
189
Jiang Liu170a5a72013-07-03 15:03:17 -0700190void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700191{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800192 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700193
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800194 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800195 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
196 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700197
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700198 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800199 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700200 ClearPagePrivate(page);
201 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800202 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700203 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700204 }
Yasunori Goto04753272008-04-28 02:13:31 -0700205}
206
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800207#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
208#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700209static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700210{
211 unsigned long *usemap, mapsize, section_nr, i;
212 struct mem_section *ms;
213 struct page *page, *memmap;
214
Yasunori Goto04753272008-04-28 02:13:31 -0700215 section_nr = pfn_to_section_nr(start_pfn);
216 ms = __nr_to_section(section_nr);
217
218 /* Get section's memmap address */
219 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
220
221 /*
222 * Get page for the memmap's phys address
223 * XXX: need more consideration for sparse_vmemmap...
224 */
225 page = virt_to_page(memmap);
226 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
227 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
228
229 /* remember memmap's page */
230 for (i = 0; i < mapsize; i++, page++)
231 get_page_bootmem(section_nr, page, SECTION_INFO);
232
233 usemap = __nr_to_section(section_nr)->pageblock_flags;
234 page = virt_to_page(usemap);
235
236 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
237
238 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700239 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700240
241}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800242#else /* CONFIG_SPARSEMEM_VMEMMAP */
243static void register_page_bootmem_info_section(unsigned long start_pfn)
244{
245 unsigned long *usemap, mapsize, section_nr, i;
246 struct mem_section *ms;
247 struct page *page, *memmap;
248
249 if (!pfn_valid(start_pfn))
250 return;
251
252 section_nr = pfn_to_section_nr(start_pfn);
253 ms = __nr_to_section(section_nr);
254
255 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
256
257 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
258
259 usemap = __nr_to_section(section_nr)->pageblock_flags;
260 page = virt_to_page(usemap);
261
262 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
263
264 for (i = 0; i < mapsize; i++, page++)
265 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
266}
267#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700268
Linus Torvalds7ded3842016-05-27 15:23:32 -0700269void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700270{
271 unsigned long i, pfn, end_pfn, nr_pages;
272 int node = pgdat->node_id;
273 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700274
275 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
276 page = virt_to_page(pgdat);
277
278 for (i = 0; i < nr_pages; i++, page++)
279 get_page_bootmem(node, page, NODE_INFO);
280
Yasunori Goto04753272008-04-28 02:13:31 -0700281 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800282 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700283
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700284 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700285 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
286 /*
287 * Some platforms can assign the same pfn to multiple nodes - on
288 * node0 as well as nodeN. To avoid registering a pfn against
289 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700290 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700291 */
Yang Shif65e91df2016-05-27 14:27:32 -0700292 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700293 register_page_bootmem_info_section(pfn);
294 }
Yasunori Goto04753272008-04-28 02:13:31 -0700295}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800296#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700297
Fabian Frederickf2765402014-08-06 16:04:57 -0700298static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
299 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700300{
301 unsigned long old_zone_end_pfn;
302
303 zone_span_writelock(zone);
304
Xishi Qiuc33bc312013-09-11 14:21:44 -0700305 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700306 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700307 zone->zone_start_pfn = start_pfn;
308
309 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
310 zone->zone_start_pfn;
311
312 zone_span_writeunlock(zone);
313}
314
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800315static void resize_zone(struct zone *zone, unsigned long start_pfn,
316 unsigned long end_pfn)
317{
318 zone_span_writelock(zone);
319
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800320 if (end_pfn - start_pfn) {
321 zone->zone_start_pfn = start_pfn;
322 zone->spanned_pages = end_pfn - start_pfn;
323 } else {
324 /*
325 * make it consist as free_area_init_core(),
326 * if spanned_pages = 0, then keep start_pfn = 0
327 */
328 zone->zone_start_pfn = 0;
329 zone->spanned_pages = 0;
330 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800331
332 zone_span_writeunlock(zone);
333}
334
335static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
336 unsigned long end_pfn)
337{
338 enum zone_type zid = zone_idx(zone);
339 int nid = zone->zone_pgdat->node_id;
340 unsigned long pfn;
341
342 for (pfn = start_pfn; pfn < end_pfn; pfn++)
343 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
344}
345
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800346/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800347 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800348static int __ref ensure_zone_is_initialized(struct zone *zone,
349 unsigned long start_pfn, unsigned long num_pages)
350{
351 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800352 return init_currently_empty_zone(zone, start_pfn, num_pages);
353
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800354 return 0;
355}
356
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800357static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800358 unsigned long start_pfn, unsigned long end_pfn)
359{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800360 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800361 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800362 unsigned long z1_start_pfn;
363
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800364 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
365 if (ret)
366 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800367
368 pgdat_resize_lock(z1->zone_pgdat, &flags);
369
370 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800371 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800372 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700373 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800374 if (start_pfn > z2->zone_start_pfn)
375 goto out_fail;
376 /* must included/overlap */
377 if (end_pfn <= z2->zone_start_pfn)
378 goto out_fail;
379
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800380 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700381 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800382 z1_start_pfn = z1->zone_start_pfn;
383 else
384 z1_start_pfn = start_pfn;
385
386 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800387 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800388
389 pgdat_resize_unlock(z1->zone_pgdat, &flags);
390
391 fix_zone_id(z1, start_pfn, end_pfn);
392
393 return 0;
394out_fail:
395 pgdat_resize_unlock(z1->zone_pgdat, &flags);
396 return -1;
397}
398
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800399static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800400 unsigned long start_pfn, unsigned long end_pfn)
401{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800402 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800403 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800404 unsigned long z2_end_pfn;
405
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800406 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
407 if (ret)
408 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800409
410 pgdat_resize_lock(z1->zone_pgdat, &flags);
411
412 /* can't move pfns which are lower than @z1 */
413 if (z1->zone_start_pfn > start_pfn)
414 goto out_fail;
415 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800416 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800417 goto out_fail;
418 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800419 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800420 goto out_fail;
421
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800422 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700423 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800424 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800425 else
426 z2_end_pfn = end_pfn;
427
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800428 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800429 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800430
431 pgdat_resize_unlock(z1->zone_pgdat, &flags);
432
433 fix_zone_id(z2, start_pfn, end_pfn);
434
435 return 0;
436out_fail:
437 pgdat_resize_unlock(z1->zone_pgdat, &flags);
438 return -1;
439}
440
Reza Arbabe51e6c82016-07-26 15:22:20 -0700441static struct zone * __meminit move_pfn_range(int zone_shift,
442 unsigned long start_pfn, unsigned long end_pfn)
443{
444 struct zone *zone = page_zone(pfn_to_page(start_pfn));
445 int ret = 0;
446
447 if (zone_shift < 0)
448 ret = move_pfn_range_left(zone + zone_shift, zone,
449 start_pfn, end_pfn);
450 else if (zone_shift)
451 ret = move_pfn_range_right(zone, zone + zone_shift,
452 start_pfn, end_pfn);
453
454 if (ret)
455 return NULL;
456
457 return zone + zone_shift;
458}
459
Fabian Frederickf2765402014-08-06 16:04:57 -0700460static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
461 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700462{
Xishi Qiu83285c72013-11-12 15:07:19 -0800463 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700464
Tang Chen712cd382012-12-11 16:01:07 -0800465 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700466 pgdat->node_start_pfn = start_pfn;
467
468 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
469 pgdat->node_start_pfn;
470}
471
Al Viro31168482008-11-22 17:33:24 +0000472static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700473{
474 struct pglist_data *pgdat = zone->zone_pgdat;
475 int nr_pages = PAGES_PER_SECTION;
476 int nid = pgdat->node_id;
477 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700478 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800479 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700480
481 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800482 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
483 if (ret)
484 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700485
Heiko Carstens76cdd582008-05-14 16:05:52 -0700486 pgdat_resize_lock(zone->zone_pgdat, &flags);
487 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
488 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
489 phys_start_pfn + nr_pages);
490 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800491 memmap_init_zone(nr_pages, nid, zone_type,
492 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700493
494 /* online_page_range is called later and expects pages reserved */
495 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
496 if (!pfn_valid(pfn))
497 continue;
498
499 SetPageReserved(pfn_to_page(pfn));
500 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700501 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700502}
503
Gary Hadec04fc582009-01-06 14:39:14 -0800504static int __meminit __add_section(int nid, struct zone *zone,
505 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700506{
Dave Hansen3947be12005-10-29 18:16:54 -0700507 int ret;
508
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700509 if (pfn_valid(phys_start_pfn))
510 return -EEXIST;
511
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800512 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700513
514 if (ret < 0)
515 return ret;
516
Yasunori Goto718127c2006-06-23 02:03:10 -0700517 ret = __add_zone(zone, phys_start_pfn);
518
519 if (ret < 0)
520 return ret;
521
Gary Hadec04fc582009-01-06 14:39:14 -0800522 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700523}
524
David Rientjes4edd7ce2013-04-29 15:08:22 -0700525/*
526 * Reasonably generic function for adding memory. It is
527 * expected that archs that support memory hotplug will
528 * call this function after deciding the zone to which to
529 * add the new pages.
530 */
531int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
532 unsigned long nr_pages)
533{
534 unsigned long i;
535 int err = 0;
536 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800537 struct vmem_altmap *altmap;
538
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700539 clear_zone_contiguous(zone);
540
David Rientjes4edd7ce2013-04-29 15:08:22 -0700541 /* during initialize mem_map, align hot-added range to section */
542 start_sec = pfn_to_section_nr(phys_start_pfn);
543 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
544
Dan Williams4b94ffd2016-01-15 16:56:22 -0800545 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
546 if (altmap) {
547 /*
548 * Validate altmap is within bounds of the total request
549 */
550 if (altmap->base_pfn != phys_start_pfn
551 || vmem_altmap_offset(altmap) > nr_pages) {
552 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700553 err = -EINVAL;
554 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800555 }
556 altmap->alloc = 0;
557 }
558
David Rientjes4edd7ce2013-04-29 15:08:22 -0700559 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700560 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700561
562 /*
563 * EEXIST is finally dealt with by ioresource collision
564 * check. see add_memory() => register_memory_resource()
565 * Warning will be printed if there is collision.
566 */
567 if (err && (err != -EEXIST))
568 break;
569 err = 0;
570 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700571 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700572out:
573 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700574 return err;
575}
576EXPORT_SYMBOL_GPL(__add_pages);
577
578#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800579/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
580static int find_smallest_section_pfn(int nid, struct zone *zone,
581 unsigned long start_pfn,
582 unsigned long end_pfn)
583{
584 struct mem_section *ms;
585
586 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
587 ms = __pfn_to_section(start_pfn);
588
589 if (unlikely(!valid_section(ms)))
590 continue;
591
592 if (unlikely(pfn_to_nid(start_pfn) != nid))
593 continue;
594
595 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
596 continue;
597
598 return start_pfn;
599 }
600
601 return 0;
602}
603
604/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
605static int find_biggest_section_pfn(int nid, struct zone *zone,
606 unsigned long start_pfn,
607 unsigned long end_pfn)
608{
609 struct mem_section *ms;
610 unsigned long pfn;
611
612 /* pfn is the end pfn of a memory section. */
613 pfn = end_pfn - 1;
614 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
615 ms = __pfn_to_section(pfn);
616
617 if (unlikely(!valid_section(ms)))
618 continue;
619
620 if (unlikely(pfn_to_nid(pfn) != nid))
621 continue;
622
623 if (zone && zone != page_zone(pfn_to_page(pfn)))
624 continue;
625
626 return pfn;
627 }
628
629 return 0;
630}
631
632static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
633 unsigned long end_pfn)
634{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700635 unsigned long zone_start_pfn = zone->zone_start_pfn;
636 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
637 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800638 unsigned long pfn;
639 struct mem_section *ms;
640 int nid = zone_to_nid(zone);
641
642 zone_span_writelock(zone);
643 if (zone_start_pfn == start_pfn) {
644 /*
645 * If the section is smallest section in the zone, it need
646 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
647 * In this case, we find second smallest valid mem_section
648 * for shrinking zone.
649 */
650 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
651 zone_end_pfn);
652 if (pfn) {
653 zone->zone_start_pfn = pfn;
654 zone->spanned_pages = zone_end_pfn - pfn;
655 }
656 } else if (zone_end_pfn == end_pfn) {
657 /*
658 * If the section is biggest section in the zone, it need
659 * shrink zone->spanned_pages.
660 * In this case, we find second biggest valid mem_section for
661 * shrinking zone.
662 */
663 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
664 start_pfn);
665 if (pfn)
666 zone->spanned_pages = pfn - zone_start_pfn + 1;
667 }
668
669 /*
670 * The section is not biggest or smallest mem_section in the zone, it
671 * only creates a hole in the zone. So in this case, we need not
672 * change the zone. But perhaps, the zone has only hole data. Thus
673 * it check the zone has only hole or not.
674 */
675 pfn = zone_start_pfn;
676 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
677 ms = __pfn_to_section(pfn);
678
679 if (unlikely(!valid_section(ms)))
680 continue;
681
682 if (page_zone(pfn_to_page(pfn)) != zone)
683 continue;
684
685 /* If the section is current section, it continues the loop */
686 if (start_pfn == pfn)
687 continue;
688
689 /* If we find valid section, we have nothing to do */
690 zone_span_writeunlock(zone);
691 return;
692 }
693
694 /* The zone has no valid section */
695 zone->zone_start_pfn = 0;
696 zone->spanned_pages = 0;
697 zone_span_writeunlock(zone);
698}
699
700static void shrink_pgdat_span(struct pglist_data *pgdat,
701 unsigned long start_pfn, unsigned long end_pfn)
702{
Xishi Qiu83285c72013-11-12 15:07:19 -0800703 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
704 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
705 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800706 unsigned long pfn;
707 struct mem_section *ms;
708 int nid = pgdat->node_id;
709
710 if (pgdat_start_pfn == start_pfn) {
711 /*
712 * If the section is smallest section in the pgdat, it need
713 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
714 * In this case, we find second smallest valid mem_section
715 * for shrinking zone.
716 */
717 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
718 pgdat_end_pfn);
719 if (pfn) {
720 pgdat->node_start_pfn = pfn;
721 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
722 }
723 } else if (pgdat_end_pfn == end_pfn) {
724 /*
725 * If the section is biggest section in the pgdat, it need
726 * shrink pgdat->node_spanned_pages.
727 * In this case, we find second biggest valid mem_section for
728 * shrinking zone.
729 */
730 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
731 start_pfn);
732 if (pfn)
733 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
734 }
735
736 /*
737 * If the section is not biggest or smallest mem_section in the pgdat,
738 * it only creates a hole in the pgdat. So in this case, we need not
739 * change the pgdat.
740 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
741 * has only hole or not.
742 */
743 pfn = pgdat_start_pfn;
744 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
745 ms = __pfn_to_section(pfn);
746
747 if (unlikely(!valid_section(ms)))
748 continue;
749
750 if (pfn_to_nid(pfn) != nid)
751 continue;
752
753 /* If the section is current section, it continues the loop */
754 if (start_pfn == pfn)
755 continue;
756
757 /* If we find valid section, we have nothing to do */
758 return;
759 }
760
761 /* The pgdat has no valid section */
762 pgdat->node_start_pfn = 0;
763 pgdat->node_spanned_pages = 0;
764}
765
766static void __remove_zone(struct zone *zone, unsigned long start_pfn)
767{
768 struct pglist_data *pgdat = zone->zone_pgdat;
769 int nr_pages = PAGES_PER_SECTION;
770 int zone_type;
771 unsigned long flags;
772
773 zone_type = zone - pgdat->node_zones;
774
775 pgdat_resize_lock(zone->zone_pgdat, &flags);
776 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
777 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
778 pgdat_resize_unlock(zone->zone_pgdat, &flags);
779}
780
Dan Williams4b94ffd2016-01-15 16:56:22 -0800781static int __remove_section(struct zone *zone, struct mem_section *ms,
782 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700783{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800784 unsigned long start_pfn;
785 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700786 int ret = -EINVAL;
787
788 if (!valid_section(ms))
789 return ret;
790
791 ret = unregister_memory_section(ms);
792 if (ret)
793 return ret;
794
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800795 scn_nr = __section_nr(ms);
796 start_pfn = section_nr_to_pfn(scn_nr);
797 __remove_zone(zone, start_pfn);
798
Dan Williams4b94ffd2016-01-15 16:56:22 -0800799 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700800 return 0;
801}
802
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700803/**
804 * __remove_pages() - remove sections of pages from a zone
805 * @zone: zone from which pages need to be removed
806 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
807 * @nr_pages: number of pages to remove (must be multiple of section size)
808 *
809 * Generic helper function to remove section mappings and sysfs entries
810 * for the section of the memory we are removing. Caller needs to make
811 * sure that pages are marked reserved and zones are adjust properly by
812 * calling offline_pages().
813 */
814int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
815 unsigned long nr_pages)
816{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700817 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800818 unsigned long map_offset = 0;
819 int sections_to_remove, ret = 0;
820
821 /* In the ZONE_DEVICE case device driver owns the memory region */
822 if (is_dev_zone(zone)) {
823 struct page *page = pfn_to_page(phys_start_pfn);
824 struct vmem_altmap *altmap;
825
826 altmap = to_vmem_altmap((unsigned long) page);
827 if (altmap)
828 map_offset = vmem_altmap_offset(altmap);
829 } else {
830 resource_size_t start, size;
831
832 start = phys_start_pfn << PAGE_SHIFT;
833 size = nr_pages * PAGE_SIZE;
834
835 ret = release_mem_region_adjustable(&iomem_resource, start,
836 size);
837 if (ret) {
838 resource_size_t endres = start + size - 1;
839
840 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
841 &start, &endres, ret);
842 }
843 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700844
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700845 clear_zone_contiguous(zone);
846
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700847 /*
848 * We can only remove entire sections
849 */
850 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
851 BUG_ON(nr_pages % PAGES_PER_SECTION);
852
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700853 sections_to_remove = nr_pages / PAGES_PER_SECTION;
854 for (i = 0; i < sections_to_remove; i++) {
855 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800856
857 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
858 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700859 if (ret)
860 break;
861 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700862
863 set_zone_contiguous(zone);
864
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700865 return ret;
866}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700867#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700868
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700869int set_online_page_callback(online_page_callback_t callback)
870{
871 int rc = -EINVAL;
872
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700873 get_online_mems();
874 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700875
876 if (online_page_callback == generic_online_page) {
877 online_page_callback = callback;
878 rc = 0;
879 }
880
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700881 mutex_unlock(&online_page_callback_lock);
882 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700883
884 return rc;
885}
886EXPORT_SYMBOL_GPL(set_online_page_callback);
887
888int restore_online_page_callback(online_page_callback_t callback)
889{
890 int rc = -EINVAL;
891
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700892 get_online_mems();
893 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700894
895 if (online_page_callback == callback) {
896 online_page_callback = generic_online_page;
897 rc = 0;
898 }
899
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700900 mutex_unlock(&online_page_callback_lock);
901 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700902
903 return rc;
904}
905EXPORT_SYMBOL_GPL(restore_online_page_callback);
906
907void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700908{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700909}
910EXPORT_SYMBOL_GPL(__online_page_set_limits);
911
912void __online_page_increment_counters(struct page *page)
913{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700914 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700915}
916EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700917
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700918void __online_page_free(struct page *page)
919{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700920 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700921}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700922EXPORT_SYMBOL_GPL(__online_page_free);
923
924static void generic_online_page(struct page *page)
925{
926 __online_page_set_limits(page);
927 __online_page_increment_counters(page);
928 __online_page_free(page);
929}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700930
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700931static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
932 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700933{
934 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700935 unsigned long onlined_pages = *(unsigned long *)arg;
936 struct page *page;
937 if (PageReserved(pfn_to_page(start_pfn)))
938 for (i = 0; i < nr_pages; i++) {
939 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700940 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700941 onlined_pages++;
942 }
943 *(unsigned long *)arg = onlined_pages;
944 return 0;
945}
946
Lai Jiangshan09285af2012-12-12 13:52:04 -0800947#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800948/*
949 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
950 * normal memory.
951 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800952static bool can_online_high_movable(struct zone *zone)
953{
954 return true;
955}
Tang Chen79a4dce2012-12-18 14:23:24 -0800956#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800957/* ensure every online node has NORMAL memory */
958static bool can_online_high_movable(struct zone *zone)
959{
960 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
961}
Tang Chen79a4dce2012-12-18 14:23:24 -0800962#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800963
Lai Jiangshand9713672012-12-11 16:01:03 -0800964/* check which state of node_states will be changed when online memory */
965static void node_states_check_changes_online(unsigned long nr_pages,
966 struct zone *zone, struct memory_notify *arg)
967{
968 int nid = zone_to_nid(zone);
969 enum zone_type zone_last = ZONE_NORMAL;
970
971 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800972 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
973 * contains nodes which have zones of 0...ZONE_NORMAL,
974 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800975 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800976 * If we don't have HIGHMEM nor movable node,
977 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
978 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800979 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800980 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800981 zone_last = ZONE_MOVABLE;
982
983 /*
984 * if the memory to be online is in a zone of 0...zone_last, and
985 * the zones of 0...zone_last don't have memory before online, we will
986 * need to set the node to node_states[N_NORMAL_MEMORY] after
987 * the memory is online.
988 */
989 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
990 arg->status_change_nid_normal = nid;
991 else
992 arg->status_change_nid_normal = -1;
993
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800994#ifdef CONFIG_HIGHMEM
995 /*
996 * If we have movable node, node_states[N_HIGH_MEMORY]
997 * contains nodes which have zones of 0...ZONE_HIGHMEM,
998 * set zone_last to ZONE_HIGHMEM.
999 *
1000 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1001 * contains nodes which have zones of 0...ZONE_MOVABLE,
1002 * set zone_last to ZONE_MOVABLE.
1003 */
1004 zone_last = ZONE_HIGHMEM;
1005 if (N_MEMORY == N_HIGH_MEMORY)
1006 zone_last = ZONE_MOVABLE;
1007
1008 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1009 arg->status_change_nid_high = nid;
1010 else
1011 arg->status_change_nid_high = -1;
1012#else
1013 arg->status_change_nid_high = arg->status_change_nid_normal;
1014#endif
1015
Lai Jiangshand9713672012-12-11 16:01:03 -08001016 /*
1017 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001018 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001019 * is online.
1020 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001021 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001022 arg->status_change_nid = nid;
1023 else
1024 arg->status_change_nid = -1;
1025}
1026
1027static void node_states_set_node(int node, struct memory_notify *arg)
1028{
1029 if (arg->status_change_nid_normal >= 0)
1030 node_set_state(node, N_NORMAL_MEMORY);
1031
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001032 if (arg->status_change_nid_high >= 0)
1033 node_set_state(node, N_HIGH_MEMORY);
1034
1035 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001036}
1037
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001038bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1039 enum zone_type target, int *zone_shift)
Reza Arbabdf429ac2016-07-26 15:22:23 -07001040{
1041 struct zone *zone = page_zone(pfn_to_page(pfn));
1042 enum zone_type idx = zone_idx(zone);
1043 int i;
1044
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001045 *zone_shift = 0;
1046
Reza Arbabdf429ac2016-07-26 15:22:23 -07001047 if (idx < target) {
1048 /* pages must be at end of current zone */
1049 if (pfn + nr_pages != zone_end_pfn(zone))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001050 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001051
1052 /* no zones in use between current zone and target */
1053 for (i = idx + 1; i < target; i++)
1054 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001055 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001056 }
1057
1058 if (target < idx) {
1059 /* pages must be at beginning of current zone */
1060 if (pfn != zone->zone_start_pfn)
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001061 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001062
1063 /* no zones in use between current zone and target */
1064 for (i = target + 1; i < idx; i++)
1065 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001066 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001067 }
1068
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001069 *zone_shift = target - idx;
1070 return true;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001071}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001072
David Rientjes30467e02015-04-14 15:45:11 -07001073/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001074int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001075{
Cody P Schaferaa472282013-07-03 15:02:10 -07001076 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001077 unsigned long onlined_pages = 0;
1078 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001079 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001080 int nid;
1081 int ret;
1082 struct memory_notify arg;
Reza Arbabe51e6c82016-07-26 15:22:20 -07001083 int zone_shift = 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001084
Lai Jiangshand9713672012-12-11 16:01:03 -08001085 /*
1086 * This doesn't need a lock to do pfn_to_page().
1087 * The section can't be removed here because of the
1088 * memory_block->state_mutex.
1089 */
1090 zone = page_zone(pfn_to_page(pfn));
1091
Tang Chen4f7c6b42014-08-06 16:05:13 -07001092 if ((zone_idx(zone) > ZONE_NORMAL ||
1093 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001094 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001095 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001096
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001097 if (online_type == MMOP_ONLINE_KERNEL) {
1098 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1099 return -EINVAL;
1100 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1101 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1102 return -EINVAL;
1103 }
Reza Arbabe51e6c82016-07-26 15:22:20 -07001104
1105 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1106 if (!zone)
1107 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001108
Yasunori Goto7b78d332007-10-21 16:41:36 -07001109 arg.start_pfn = pfn;
1110 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001111 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001112
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001113 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001114
1115 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1116 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001117 if (ret)
1118 goto failed_addition;
1119
Dave Hansen3947be12005-10-29 18:16:54 -07001120 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001121 * If this zone is not populated, then it is not in zonelist.
1122 * This means the page allocator ignores this zone.
1123 * So, zonelist must be updated after online.
1124 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001125 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001126 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001127 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001128 build_all_zonelists(NULL, zone);
1129 }
Yasunori Goto68113782006-06-23 02:03:11 -07001130
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001131 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001132 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001133 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001134 if (need_zonelists_rebuild)
1135 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001136 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001137 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001138 }
1139
Dave Hansen3947be12005-10-29 18:16:54 -07001140 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001141
1142 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001143 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001144 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1145
Jiang Liu08dff7b2012-07-31 16:43:30 -07001146 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001147 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001148 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001149 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001150 else
1151 zone_pcp_update(zone);
1152 }
Dave Hansen3947be12005-10-29 18:16:54 -07001153
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001154 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001155
1156 init_per_zone_wmark_min();
1157
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001158 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001159 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001160 kcompactd_run(nid);
1161 }
Dave Hansen61b13992005-10-29 18:16:56 -07001162
Haicheng Li1f522502010-05-24 14:32:51 -07001163 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001164
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001165 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001166
1167 if (onlined_pages)
1168 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001169 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001170
1171failed_addition:
1172 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1173 (unsigned long long) pfn << PAGE_SHIFT,
1174 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1175 memory_notify(MEM_CANCEL_ONLINE, &arg);
1176 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001177}
Keith Mannthey53947022006-09-30 23:27:08 -07001178#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001179
Tang Chen0bd85422014-11-13 15:19:41 -08001180static void reset_node_present_pages(pg_data_t *pgdat)
1181{
1182 struct zone *z;
1183
1184 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1185 z->present_pages = 0;
1186
1187 pgdat->node_present_pages = 0;
1188}
1189
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001190/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1191static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001192{
1193 struct pglist_data *pgdat;
1194 unsigned long zones_size[MAX_NR_ZONES] = {0};
1195 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001196 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001197
Tang Chena1e565a2013-02-22 16:33:18 -08001198 pgdat = NODE_DATA(nid);
1199 if (!pgdat) {
1200 pgdat = arch_alloc_nodedata(nid);
1201 if (!pgdat)
1202 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001203
Tang Chena1e565a2013-02-22 16:33:18 -08001204 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001205 } else {
Mel Gorman38087d92016-07-28 15:45:49 -07001206 /* Reset the nr_zones, order and classzone_idx before reuse */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001207 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001208 pgdat->kswapd_order = 0;
1209 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001210 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001211
1212 /* we can use NODE_DATA(nid) from here */
1213
1214 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001215 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001216 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001217
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001218 /*
1219 * The node we allocated has no zone fallback lists. For avoiding
1220 * to access not-initialized zonelist, build here.
1221 */
David Rientjesf957db42011-06-22 18:13:04 -07001222 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001223 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001224 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001225
Tang Chenf784a3f2014-11-13 15:19:39 -08001226 /*
1227 * zone->managed_pages is set to an approximate value in
1228 * free_area_init_core(), which will cause
1229 * /sys/device/system/node/nodeX/meminfo has wrong data.
1230 * So reset it to 0 before any memory is onlined.
1231 */
1232 reset_node_managed_pages(pgdat);
1233
Tang Chen0bd85422014-11-13 15:19:41 -08001234 /*
1235 * When memory is hot-added, all the memory is in offline state. So
1236 * clear all zones' present_pages because they will be updated in
1237 * online_pages() and offline_pages().
1238 */
1239 reset_node_present_pages(pgdat);
1240
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001241 return pgdat;
1242}
1243
1244static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1245{
1246 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001247 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001248 arch_free_nodedata(pgdat);
1249 return;
1250}
1251
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001252
Toshi Kani01b0f192013-11-12 15:07:25 -08001253/**
1254 * try_online_node - online a node if offlined
1255 *
minskey guocf234222010-05-24 14:32:41 -07001256 * called by cpu_up() to online a node without onlined memory.
1257 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001258int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001259{
1260 pg_data_t *pgdat;
1261 int ret;
1262
Toshi Kani01b0f192013-11-12 15:07:25 -08001263 if (node_online(nid))
1264 return 0;
1265
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001266 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001267 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001268 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001269 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001270 ret = -ENOMEM;
1271 goto out;
1272 }
1273 node_set_online(nid);
1274 ret = register_one_node(nid);
1275 BUG_ON(ret);
1276
Toshi Kani01b0f192013-11-12 15:07:25 -08001277 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1278 mutex_lock(&zonelists_mutex);
1279 build_all_zonelists(NULL, NULL);
1280 mutex_unlock(&zonelists_mutex);
1281 }
1282
minskey guocf234222010-05-24 14:32:41 -07001283out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001284 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001285 return ret;
1286}
1287
Toshi Kani27356f52013-09-11 14:21:49 -07001288static int check_hotplug_memory_range(u64 start, u64 size)
1289{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001290 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001291 u64 nr_pages = size >> PAGE_SHIFT;
1292
1293 /* Memory range must be aligned with section */
1294 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1295 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1296 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1297 (unsigned long long)start,
1298 (unsigned long long)size);
1299 return -EINVAL;
1300 }
1301
1302 return 0;
1303}
1304
Wang Nan63264402014-08-06 16:07:36 -07001305/*
1306 * If movable zone has already been setup, newly added memory should be check.
1307 * If its address is higher than movable zone, it should be added as movable.
1308 * Without this check, movable zone may overlap with other zone.
1309 */
1310static int should_add_memory_movable(int nid, u64 start, u64 size)
1311{
1312 unsigned long start_pfn = start >> PAGE_SHIFT;
1313 pg_data_t *pgdat = NODE_DATA(nid);
1314 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1315
1316 if (zone_is_empty(movable_zone))
1317 return 0;
1318
1319 if (movable_zone->zone_start_pfn <= start_pfn)
1320 return 1;
1321
1322 return 0;
1323}
1324
Dan Williams033fbae2015-08-09 15:29:06 -04001325int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1326 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001327{
Dan Williams033fbae2015-08-09 15:29:06 -04001328#ifdef CONFIG_ZONE_DEVICE
1329 if (for_device)
1330 return ZONE_DEVICE;
1331#endif
Wang Nan63264402014-08-06 16:07:36 -07001332 if (should_add_memory_movable(nid, start, size))
1333 return ZONE_MOVABLE;
1334
1335 return zone_default;
1336}
1337
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001338static int online_memory_block(struct memory_block *mem, void *arg)
1339{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001340 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001341}
1342
Al Viro31168482008-11-22 17:33:24 +00001343/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001344int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001345{
David Vrabel62cedb92015-06-25 16:35:49 +01001346 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001347 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001348 bool new_pgdat;
1349 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001350 int ret;
1351
David Vrabel62cedb92015-06-25 16:35:49 +01001352 start = res->start;
1353 size = resource_size(res);
1354
Toshi Kani27356f52013-09-11 14:21:49 -07001355 ret = check_hotplug_memory_range(start, size);
1356 if (ret)
1357 return ret;
1358
Tang Chena1e565a2013-02-22 16:33:18 -08001359 { /* Stupid hack to suppress address-never-null warning */
1360 void *p = NODE_DATA(nid);
1361 new_pgdat = !p;
1362 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001363
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001364 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001365
Tang Chen7f36e3e2015-09-04 15:42:32 -07001366 /*
1367 * Add new range to memblock so that when hotadd_new_pgdat() is called
1368 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1369 * this new range and calculate total pages correctly. The range will
1370 * be removed at hot-remove time.
1371 */
1372 memblock_add_node(start, size, nid);
1373
Tang Chena1e565a2013-02-22 16:33:18 -08001374 new_node = !node_online(nid);
1375 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001376 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001377 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001378 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001379 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001380 }
1381
Yasunori Gotobc02af92006-06-27 02:53:30 -07001382 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001383 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001384
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001385 if (ret < 0)
1386 goto error;
1387
Yasunori Goto0fc44152006-06-27 02:53:38 -07001388 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001389 node_set_online(nid);
1390
Tang Chena1e565a2013-02-22 16:33:18 -08001391 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001392 ret = register_one_node(nid);
1393 /*
1394 * If sysfs file of new node can't create, cpu on the node
1395 * can't be hot-added. There is no rollback way now.
1396 * So, check by BUG_ON() to catch it reluctantly..
1397 */
1398 BUG_ON(ret);
1399 }
1400
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001401 /* create new memmap entry */
1402 firmware_map_add_hotplug(start, start + size, "System RAM");
1403
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001404 /* online pages if requested */
1405 if (online)
1406 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1407 NULL, online_memory_block);
1408
Andi Kleen6ad696d2009-11-17 14:06:22 -08001409 goto out;
1410
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001411error:
1412 /* rollback pgdat allocation and others */
1413 if (new_pgdat)
1414 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001415 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001416
Andi Kleen6ad696d2009-11-17 14:06:22 -08001417out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001418 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001419 return ret;
1420}
David Vrabel62cedb92015-06-25 16:35:49 +01001421EXPORT_SYMBOL_GPL(add_memory_resource);
1422
1423int __ref add_memory(int nid, u64 start, u64 size)
1424{
1425 struct resource *res;
1426 int ret;
1427
1428 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001429 if (IS_ERR(res))
1430 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001431
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001432 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001433 if (ret < 0)
1434 release_memory_resource(res);
1435 return ret;
1436}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001437EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001438
1439#ifdef CONFIG_MEMORY_HOTREMOVE
1440/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001441 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1442 * set and the size of the free page is given by page_order(). Using this,
1443 * the function determines if the pageblock contains only free pages.
1444 * Due to buddy contraints, a free page at least the size of a pageblock will
1445 * be located at the start of the pageblock
1446 */
1447static inline int pageblock_free(struct page *page)
1448{
1449 return PageBuddy(page) && page_order(page) >= pageblock_order;
1450}
1451
1452/* Return the start of the next active pageblock after a given page */
1453static struct page *next_active_pageblock(struct page *page)
1454{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001455 /* Ensure the starting page is pageblock-aligned */
1456 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1457
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001458 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001459 if (pageblock_free(page)) {
1460 int order;
1461 /* be careful. we don't have locks, page_order can be changed.*/
1462 order = page_order(page);
1463 if ((order < MAX_ORDER) && (order >= pageblock_order))
1464 return page + (1 << order);
1465 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001466
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001467 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001468}
1469
1470/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001471bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001472{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001473 struct page *page = pfn_to_page(start_pfn);
1474 struct page *end_page = page + nr_pages;
1475
1476 /* Check the starting page of each pageblock within the range */
1477 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001478 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001479 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001480 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001481 }
1482
1483 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001484 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001485}
1486
1487/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001488 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001489 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001490 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001491int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1492 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001493{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001494 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001495 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001496 struct zone *zone = NULL;
1497 struct page *page;
1498 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001499 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001500 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001501 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001502 /* Make sure the memory section is present first */
1503 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001504 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001505 for (; pfn < sec_end_pfn && pfn < end_pfn;
1506 pfn += MAX_ORDER_NR_PAGES) {
1507 i = 0;
1508 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1509 while ((i < MAX_ORDER_NR_PAGES) &&
1510 !pfn_valid_within(pfn + i))
1511 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001512 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001513 continue;
1514 page = pfn_to_page(pfn + i);
1515 if (zone && page_zone(page) != zone)
1516 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001517 if (!zone)
1518 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001519 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001520 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001521 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001522 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001523
Toshi Kania96dfdd2017-02-03 13:13:23 -08001524 if (zone) {
1525 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001526 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001527 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001528 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001529 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001530 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001531}
1532
1533/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001534 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1535 * non-lru movable pages and hugepages). We scan pfn because it's much
1536 * easier than scanning over linked list. This function returns the pfn
1537 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001538 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001539static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001540{
1541 unsigned long pfn;
1542 struct page *page;
1543 for (pfn = start; pfn < end; pfn++) {
1544 if (pfn_valid(pfn)) {
1545 page = pfn_to_page(pfn);
1546 if (PageLRU(page))
1547 return pfn;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001548 if (__PageMovable(page))
1549 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001550 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001551 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001552 return pfn;
1553 else
1554 pfn = round_up(pfn + 1,
1555 1 << compound_order(page)) - 1;
1556 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001557 }
1558 }
1559 return 0;
1560}
1561
Xishi Qiu394e31d2016-07-28 15:48:53 -07001562static struct page *new_node_page(struct page *page, unsigned long private,
1563 int **result)
1564{
1565 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1566 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001567 nodemask_t nmask = node_states[N_MEMORY];
1568 struct page *new_page = NULL;
Xishi Qiu394e31d2016-07-28 15:48:53 -07001569
1570 /*
1571 * TODO: allocate a destination hugepage from a nearest neighbor node,
1572 * accordance with memory policy of the user process if possible. For
1573 * now as a simple work-around, we use the next node for destination.
1574 */
1575 if (PageHuge(page))
1576 return alloc_huge_page_node(page_hstate(compound_head(page)),
1577 next_node_in(nid, nmask));
1578
Li Zhong231e97e2016-09-28 15:22:38 -07001579 node_clear(nid, nmask);
Li Zhong9bb627b2016-09-19 14:43:52 -07001580
Xishi Qiu394e31d2016-07-28 15:48:53 -07001581 if (PageHighMem(page)
1582 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1583 gfp_mask |= __GFP_HIGHMEM;
1584
Li Zhong231e97e2016-09-28 15:22:38 -07001585 if (!nodes_empty(nmask))
1586 new_page = __alloc_pages_nodemask(gfp_mask, 0,
Xishi Qiu394e31d2016-07-28 15:48:53 -07001587 node_zonelist(nid, gfp_mask), &nmask);
1588 if (!new_page)
1589 new_page = __alloc_pages(gfp_mask, 0,
1590 node_zonelist(nid, gfp_mask));
1591
1592 return new_page;
1593}
1594
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001595#define NR_OFFLINE_AT_ONCE_PAGES (256)
1596static int
1597do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1598{
1599 unsigned long pfn;
1600 struct page *page;
1601 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1602 int not_managed = 0;
1603 int ret = 0;
1604 LIST_HEAD(source);
1605
1606 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1607 if (!pfn_valid(pfn))
1608 continue;
1609 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001610
1611 if (PageHuge(page)) {
1612 struct page *head = compound_head(page);
1613 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1614 if (compound_order(head) > PFN_SECTION_SHIFT) {
1615 ret = -EBUSY;
1616 break;
1617 }
1618 if (isolate_huge_page(page, &source))
1619 move_pages -= 1 << compound_order(head);
1620 continue;
1621 }
1622
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001623 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001624 continue;
1625 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001626 * We can skip free pages. And we can deal with pages on
1627 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001628 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001629 if (PageLRU(page))
1630 ret = isolate_lru_page(page);
1631 else
1632 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001633 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001634 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001635 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001636 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001637 if (!__PageMovable(page))
1638 inc_node_page_state(page, NR_ISOLATED_ANON +
1639 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001640
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001641 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001642#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001643 pr_alert("failed to isolate pfn %lx\n", pfn);
1644 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001645#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001646 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001647 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001648 check this again here. */
1649 if (page_count(page)) {
1650 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001651 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001652 break;
1653 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001654 }
1655 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001656 if (!list_empty(&source)) {
1657 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001658 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001659 goto out;
1660 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001661
Xishi Qiu394e31d2016-07-28 15:48:53 -07001662 /* Allocate a new page from the nearest neighbor node */
1663 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001664 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001665 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001666 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001667 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001668out:
1669 return ret;
1670}
1671
1672/*
1673 * remove from free_area[] and mark all as Reserved.
1674 */
1675static int
1676offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1677 void *data)
1678{
1679 __offline_isolated_pages(start, start + nr_pages);
1680 return 0;
1681}
1682
1683static void
1684offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1685{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001686 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001687 offline_isolated_pages_cb);
1688}
1689
1690/*
1691 * Check all pages in range, recoreded as memory resource, are isolated.
1692 */
1693static int
1694check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1695 void *data)
1696{
1697 int ret;
1698 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001699 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001700 offlined = nr_pages;
1701 if (!ret)
1702 *(long *)data += offlined;
1703 return ret;
1704}
1705
1706static long
1707check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1708{
1709 long offlined = 0;
1710 int ret;
1711
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001712 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001713 check_pages_isolated_cb);
1714 if (ret < 0)
1715 offlined = (long)ret;
1716 return offlined;
1717}
1718
Lai Jiangshan09285af2012-12-12 13:52:04 -08001719#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001720/*
1721 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1722 * normal memory.
1723 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001724static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1725{
1726 return true;
1727}
Tang Chen79a4dce2012-12-18 14:23:24 -08001728#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001729/* ensure the node has NORMAL memory if it is still online */
1730static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1731{
1732 struct pglist_data *pgdat = zone->zone_pgdat;
1733 unsigned long present_pages = 0;
1734 enum zone_type zt;
1735
1736 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1737 present_pages += pgdat->node_zones[zt].present_pages;
1738
1739 if (present_pages > nr_pages)
1740 return true;
1741
1742 present_pages = 0;
1743 for (; zt <= ZONE_MOVABLE; zt++)
1744 present_pages += pgdat->node_zones[zt].present_pages;
1745
1746 /*
1747 * we can't offline the last normal memory until all
1748 * higher memory is offlined.
1749 */
1750 return present_pages == 0;
1751}
Tang Chen79a4dce2012-12-18 14:23:24 -08001752#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001753
Tang Chenc5320922013-11-12 15:08:10 -08001754static int __init cmdline_parse_movable_node(char *p)
1755{
1756#ifdef CONFIG_MOVABLE_NODE
Tang Chen55ac5902014-01-21 15:49:35 -08001757 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001758#else
1759 pr_warn("movable_node option not supported\n");
1760#endif
1761 return 0;
1762}
1763early_param("movable_node", cmdline_parse_movable_node);
1764
Lai Jiangshand9713672012-12-11 16:01:03 -08001765/* check which state of node_states will be changed when offline memory */
1766static void node_states_check_changes_offline(unsigned long nr_pages,
1767 struct zone *zone, struct memory_notify *arg)
1768{
1769 struct pglist_data *pgdat = zone->zone_pgdat;
1770 unsigned long present_pages = 0;
1771 enum zone_type zt, zone_last = ZONE_NORMAL;
1772
1773 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001774 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1775 * contains nodes which have zones of 0...ZONE_NORMAL,
1776 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001777 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001778 * If we don't have HIGHMEM nor movable node,
1779 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1780 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001781 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001782 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001783 zone_last = ZONE_MOVABLE;
1784
1785 /*
1786 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1787 * If the memory to be offline is in a zone of 0...zone_last,
1788 * and it is the last present memory, 0...zone_last will
1789 * become empty after offline , thus we can determind we will
1790 * need to clear the node from node_states[N_NORMAL_MEMORY].
1791 */
1792 for (zt = 0; zt <= zone_last; zt++)
1793 present_pages += pgdat->node_zones[zt].present_pages;
1794 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1795 arg->status_change_nid_normal = zone_to_nid(zone);
1796 else
1797 arg->status_change_nid_normal = -1;
1798
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001799#ifdef CONFIG_HIGHMEM
1800 /*
1801 * If we have movable node, node_states[N_HIGH_MEMORY]
1802 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1803 * set zone_last to ZONE_HIGHMEM.
1804 *
1805 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1806 * contains nodes which have zones of 0...ZONE_MOVABLE,
1807 * set zone_last to ZONE_MOVABLE.
1808 */
1809 zone_last = ZONE_HIGHMEM;
1810 if (N_MEMORY == N_HIGH_MEMORY)
1811 zone_last = ZONE_MOVABLE;
1812
1813 for (; zt <= zone_last; zt++)
1814 present_pages += pgdat->node_zones[zt].present_pages;
1815 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1816 arg->status_change_nid_high = zone_to_nid(zone);
1817 else
1818 arg->status_change_nid_high = -1;
1819#else
1820 arg->status_change_nid_high = arg->status_change_nid_normal;
1821#endif
1822
Lai Jiangshand9713672012-12-11 16:01:03 -08001823 /*
1824 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1825 */
1826 zone_last = ZONE_MOVABLE;
1827
1828 /*
1829 * check whether node_states[N_HIGH_MEMORY] will be changed
1830 * If we try to offline the last present @nr_pages from the node,
1831 * we can determind we will need to clear the node from
1832 * node_states[N_HIGH_MEMORY].
1833 */
1834 for (; zt <= zone_last; zt++)
1835 present_pages += pgdat->node_zones[zt].present_pages;
1836 if (nr_pages >= present_pages)
1837 arg->status_change_nid = zone_to_nid(zone);
1838 else
1839 arg->status_change_nid = -1;
1840}
1841
1842static void node_states_clear_node(int node, struct memory_notify *arg)
1843{
1844 if (arg->status_change_nid_normal >= 0)
1845 node_clear_state(node, N_NORMAL_MEMORY);
1846
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001847 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1848 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001849 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001850
1851 if ((N_MEMORY != N_HIGH_MEMORY) &&
1852 (arg->status_change_nid >= 0))
1853 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001854}
1855
Wen Congyanga16cee12012-10-08 16:33:58 -07001856static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001857 unsigned long end_pfn, unsigned long timeout)
1858{
1859 unsigned long pfn, nr_pages, expire;
1860 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001861 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001862 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001863 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001864 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001865 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001866
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001867 /* at least, alignment against pageblock is necessary */
1868 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1869 return -EINVAL;
1870 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1871 return -EINVAL;
1872 /* This makes hotplug much easier...and readable.
1873 we assume this for now. .*/
Toshi Kania96dfdd2017-02-03 13:13:23 -08001874 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001875 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001876
Toshi Kania96dfdd2017-02-03 13:13:23 -08001877 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001878 node = zone_to_nid(zone);
1879 nr_pages = end_pfn - start_pfn;
1880
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001881 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001882 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001883
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001884 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001885 ret = start_isolate_page_range(start_pfn, end_pfn,
1886 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001887 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001888 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001889
1890 arg.start_pfn = start_pfn;
1891 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001892 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001893
1894 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1895 ret = notifier_to_errno(ret);
1896 if (ret)
1897 goto failed_removal;
1898
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001899 pfn = start_pfn;
1900 expire = jiffies + timeout;
1901 drain = 0;
1902 retry_max = 5;
1903repeat:
1904 /* start memory hot removal */
1905 ret = -EAGAIN;
1906 if (time_after(jiffies, expire))
1907 goto failed_removal;
1908 ret = -EINTR;
1909 if (signal_pending(current))
1910 goto failed_removal;
1911 ret = 0;
1912 if (drain) {
1913 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001914 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001915 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001916 }
1917
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001918 pfn = scan_movable_pages(start_pfn, end_pfn);
1919 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001920 ret = do_migrate_range(pfn, end_pfn);
1921 if (!ret) {
1922 drain = 1;
1923 goto repeat;
1924 } else {
1925 if (ret < 0)
1926 if (--retry_max == 0)
1927 goto failed_removal;
1928 yield();
1929 drain = 1;
1930 goto repeat;
1931 }
1932 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001933 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001934 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001935 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001936 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001937 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001938 /*
1939 * dissolve free hugepages in the memory block before doing offlining
1940 * actually in order to make hugetlbfs's object counting consistent.
1941 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001942 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1943 if (ret)
1944 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001945 /* check again */
1946 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1947 if (offlined_pages < 0) {
1948 ret = -EBUSY;
1949 goto failed_removal;
1950 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001951 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001952 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001953 We cannot do rollback at this point. */
1954 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001955 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001956 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001957 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001958 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001959 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001960
1961 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001962 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001963 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001964
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001965 init_per_zone_wmark_min();
1966
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001967 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001968 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001969 mutex_lock(&zonelists_mutex);
1970 build_all_zonelists(NULL, NULL);
1971 mutex_unlock(&zonelists_mutex);
1972 } else
1973 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001974
Lai Jiangshand9713672012-12-11 16:01:03 -08001975 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001976 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001977 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001978 kcompactd_stop(node);
1979 }
Minchan Kimbce73942009-06-16 15:32:50 -07001980
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001981 vm_total_pages = nr_free_pagecache_pages();
1982 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001983
1984 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001985 return 0;
1986
1987failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001988 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1989 (unsigned long long) start_pfn << PAGE_SHIFT,
1990 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001991 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001992 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001993 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001994 return ret;
1995}
Badari Pulavarty71088782008-10-18 20:25:58 -07001996
David Rientjes30467e02015-04-14 15:45:11 -07001997/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001998int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1999{
2000 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
2001}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002002#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07002003
Wen Congyangbbc76be2013-02-22 16:32:54 -08002004/**
2005 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2006 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07002007 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08002008 * @arg: argument passed to func
2009 * @func: callback for each memory section walked
2010 *
2011 * This function walks through all present mem sections in range
2012 * [start_pfn, end_pfn) and call func on each mem section.
2013 *
2014 * Returns the return value of func.
2015 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002016int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08002017 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07002018{
Wen Congyange90bdb72012-10-08 16:34:01 -07002019 struct memory_block *mem = NULL;
2020 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07002021 unsigned long pfn, section_nr;
2022 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07002023
Wen Congyange90bdb72012-10-08 16:34:01 -07002024 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2025 section_nr = pfn_to_section_nr(pfn);
2026 if (!present_section_nr(section_nr))
2027 continue;
2028
2029 section = __nr_to_section(section_nr);
2030 /* same memblock? */
2031 if (mem)
2032 if ((section_nr >= mem->start_section_nr) &&
2033 (section_nr <= mem->end_section_nr))
2034 continue;
2035
2036 mem = find_memory_block_hinted(section, mem);
2037 if (!mem)
2038 continue;
2039
Wen Congyangbbc76be2013-02-22 16:32:54 -08002040 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07002041 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08002042 kobject_put(&mem->dev.kobj);
2043 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07002044 }
2045 }
2046
2047 if (mem)
2048 kobject_put(&mem->dev.kobj);
2049
Wen Congyangbbc76be2013-02-22 16:32:54 -08002050 return 0;
2051}
2052
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002053#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08002054static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002055{
2056 int ret = !is_memblock_offlined(mem);
2057
Randy Dunlap349daa02013-04-29 15:08:49 -07002058 if (unlikely(ret)) {
2059 phys_addr_t beginpa, endpa;
2060
2061 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2062 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07002063 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07002064 &beginpa, &endpa);
2065 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08002066
2067 return ret;
2068}
2069
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002070static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08002071{
Tang Chen60a5a192013-02-22 16:33:14 -08002072 int cpu;
2073
2074 for_each_present_cpu(cpu) {
2075 if (cpu_to_node(cpu) == pgdat->node_id)
2076 /*
2077 * the cpu on this node isn't removed, and we can't
2078 * offline this node.
2079 */
2080 return -EBUSY;
2081 }
2082
2083 return 0;
2084}
2085
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002086static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002087{
2088#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002089 int cpu;
2090
2091 for_each_possible_cpu(cpu)
2092 if (cpu_to_node(cpu) == pgdat->node_id)
2093 numa_clear_node(cpu);
2094#endif
2095}
2096
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002097static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002098{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002099 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002100
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002101 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002102 if (ret)
2103 return ret;
2104
2105 /*
2106 * the node will be offlined when we come here, so we can clear
2107 * the cpu_to_node() now.
2108 */
2109
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002110 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002111 return 0;
2112}
2113
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002114/**
2115 * try_offline_node
2116 *
2117 * Offline a node if all memory sections and cpus of the node are removed.
2118 *
2119 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2120 * and online/offline operations before this call.
2121 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002122void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002123{
Wen Congyangd822b862013-02-22 16:33:16 -08002124 pg_data_t *pgdat = NODE_DATA(nid);
2125 unsigned long start_pfn = pgdat->node_start_pfn;
2126 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002127 unsigned long pfn;
2128
2129 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2130 unsigned long section_nr = pfn_to_section_nr(pfn);
2131
2132 if (!present_section_nr(section_nr))
2133 continue;
2134
2135 if (pfn_to_nid(pfn) != nid)
2136 continue;
2137
2138 /*
2139 * some memory sections of this node are not removed, and we
2140 * can't offline node now.
2141 */
2142 return;
2143 }
2144
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002145 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002146 return;
2147
2148 /*
2149 * all memory/cpu of this node are removed, we can offline this
2150 * node now.
2151 */
2152 node_set_offline(nid);
2153 unregister_one_node(nid);
2154}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002155EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002156
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002157/**
2158 * remove_memory
2159 *
2160 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2161 * and online/offline operations before this call, as required by
2162 * try_offline_node().
2163 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002164void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002165{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002166 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002167
Toshi Kani27356f52013-09-11 14:21:49 -07002168 BUG_ON(check_hotplug_memory_range(start, size));
2169
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002170 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002171
2172 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002173 * All memory blocks must be offlined before removing memory. Check
2174 * whether all memory blocks in question are offline and trigger a BUG()
2175 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002176 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002177 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002178 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002179 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002180 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002181
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002182 /* remove memmap entry */
2183 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002184 memblock_free(start, size);
2185 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002186
Wen Congyang24d335c2013-02-22 16:32:58 -08002187 arch_remove_memory(start, size);
2188
Tang Chen60a5a192013-02-22 16:33:14 -08002189 try_offline_node(nid);
2190
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002191 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002192}
Badari Pulavarty71088782008-10-18 20:25:58 -07002193EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002194#endif /* CONFIG_MEMORY_HOTREMOVE */