blob: 6b6362819be26bdebc831e8b545c87bdd34b666c [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
Dave Hansen3947be12005-10-29 18:16:54 -070010#include <linux/swap.h>
11#include <linux/interrupt.h>
12#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070013#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070016#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080021#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070022#include <linux/memory_hotplug.h>
23#include <linux/highmem.h>
24#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070025#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070026#include <linux/delay.h>
27#include <linux/migrate.h>
28#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070029#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080030#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080031#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080032#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080033#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070034#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080035#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080036#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070037#include <linux/compaction.h>
Dave Hansen3947be12005-10-29 18:16:54 -070038
39#include <asm/tlbflush.h>
40
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030041#include "internal.h"
42
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070043/*
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
48 */
49
50static void generic_online_page(struct page *page);
51
52static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070053static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070054
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070055/* The same as the cpu_hotplug lock, but for memory hotplug. */
56static struct {
57 struct task_struct *active_writer;
58 struct mutex lock; /* Synchronizes accesses to refcount, */
59 /*
60 * Also blocks the new readers during
61 * an ongoing mem hotplug operation.
62 */
63 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080064
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070065#ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map;
67#endif
68} mem_hotplug = {
69 .active_writer = NULL,
70 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
71 .refcount = 0,
72#ifdef CONFIG_DEBUG_LOCK_ALLOC
73 .dep_map = {.name = "mem_hotplug.lock" },
74#endif
75};
76
77/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
78#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
79#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
80#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
81
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070082#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070083bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070084#else
85bool memhp_auto_online = true;
86#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070087EXPORT_SYMBOL_GPL(memhp_auto_online);
88
Vitaly Kuznetsov86dd9952016-05-19 17:13:06 -070089static int __init setup_memhp_default_state(char *str)
90{
91 if (!strcmp(str, "online"))
92 memhp_auto_online = true;
93 else if (!strcmp(str, "offline"))
94 memhp_auto_online = false;
95
96 return 1;
97}
98__setup("memhp_default_state=", setup_memhp_default_state);
99
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700100void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800101{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700102 might_sleep();
103 if (mem_hotplug.active_writer == current)
104 return;
105 memhp_lock_acquire_read();
106 mutex_lock(&mem_hotplug.lock);
107 mem_hotplug.refcount++;
108 mutex_unlock(&mem_hotplug.lock);
109
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800110}
111
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700112void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800113{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700114 if (mem_hotplug.active_writer == current)
115 return;
116 mutex_lock(&mem_hotplug.lock);
117
118 if (WARN_ON(!mem_hotplug.refcount))
119 mem_hotplug.refcount++; /* try to fix things up */
120
121 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
122 wake_up_process(mem_hotplug.active_writer);
123 mutex_unlock(&mem_hotplug.lock);
124 memhp_lock_release();
125
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800126}
127
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700128/* Serializes write accesses to mem_hotplug.active_writer. */
129static DEFINE_MUTEX(memory_add_remove_lock);
130
David Rientjes30467e02015-04-14 15:45:11 -0700131void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700132{
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700133 mutex_lock(&memory_add_remove_lock);
Dan Williams3fc21922017-02-24 14:55:48 -0800134
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700135 mem_hotplug.active_writer = current;
136
137 memhp_lock_acquire();
138 for (;;) {
139 mutex_lock(&mem_hotplug.lock);
140 if (likely(!mem_hotplug.refcount))
141 break;
142 __set_current_state(TASK_UNINTERRUPTIBLE);
143 mutex_unlock(&mem_hotplug.lock);
144 schedule();
145 }
146}
147
David Rientjes30467e02015-04-14 15:45:11 -0700148void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700149{
150 mem_hotplug.active_writer = NULL;
151 mutex_unlock(&mem_hotplug.lock);
152 memhp_lock_release();
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700153 mutex_unlock(&memory_add_remove_lock);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700154}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800155
Keith Mannthey45e0b782006-09-30 23:27:09 -0700156/* add this memory to iomem resource */
157static struct resource *register_memory_resource(u64 start, u64 size)
158{
159 struct resource *res;
160 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800161 if (!res)
162 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700163
164 res->name = "System RAM";
165 res->start = start;
166 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100167 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700168 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700169 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700170 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800171 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700172 }
173 return res;
174}
175
176static void release_memory_resource(struct resource *res)
177{
178 if (!res)
179 return;
180 release_resource(res);
181 kfree(res);
182 return;
183}
184
Keith Mannthey53947022006-09-30 23:27:08 -0700185#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800186void get_page_bootmem(unsigned long info, struct page *page,
187 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700188{
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800189 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700190 SetPagePrivate(page);
191 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700192 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700193}
194
Jiang Liu170a5a72013-07-03 15:03:17 -0700195void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700196{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800197 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700198
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800199 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800200 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
201 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700202
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700203 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800204 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700205 ClearPagePrivate(page);
206 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800207 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700208 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700209 }
Yasunori Goto04753272008-04-28 02:13:31 -0700210}
211
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800212#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
213#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700214static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700215{
216 unsigned long *usemap, mapsize, section_nr, i;
217 struct mem_section *ms;
218 struct page *page, *memmap;
219
Yasunori Goto04753272008-04-28 02:13:31 -0700220 section_nr = pfn_to_section_nr(start_pfn);
221 ms = __nr_to_section(section_nr);
222
223 /* Get section's memmap address */
224 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
225
226 /*
227 * Get page for the memmap's phys address
228 * XXX: need more consideration for sparse_vmemmap...
229 */
230 page = virt_to_page(memmap);
231 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
232 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
233
234 /* remember memmap's page */
235 for (i = 0; i < mapsize; i++, page++)
236 get_page_bootmem(section_nr, page, SECTION_INFO);
237
238 usemap = __nr_to_section(section_nr)->pageblock_flags;
239 page = virt_to_page(usemap);
240
241 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
242
243 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700244 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700245
246}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800247#else /* CONFIG_SPARSEMEM_VMEMMAP */
248static void register_page_bootmem_info_section(unsigned long start_pfn)
249{
250 unsigned long *usemap, mapsize, section_nr, i;
251 struct mem_section *ms;
252 struct page *page, *memmap;
253
254 if (!pfn_valid(start_pfn))
255 return;
256
257 section_nr = pfn_to_section_nr(start_pfn);
258 ms = __nr_to_section(section_nr);
259
260 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
261
262 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
263
264 usemap = __nr_to_section(section_nr)->pageblock_flags;
265 page = virt_to_page(usemap);
266
267 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
268
269 for (i = 0; i < mapsize; i++, page++)
270 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
271}
272#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700273
Linus Torvalds7ded3842016-05-27 15:23:32 -0700274void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700275{
276 unsigned long i, pfn, end_pfn, nr_pages;
277 int node = pgdat->node_id;
278 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700279
280 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
281 page = virt_to_page(pgdat);
282
283 for (i = 0; i < nr_pages; i++, page++)
284 get_page_bootmem(node, page, NODE_INFO);
285
Yasunori Goto04753272008-04-28 02:13:31 -0700286 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800287 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700288
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700289 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700290 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
291 /*
292 * Some platforms can assign the same pfn to multiple nodes - on
293 * node0 as well as nodeN. To avoid registering a pfn against
294 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700295 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700296 */
Yang Shif65e91df2016-05-27 14:27:32 -0700297 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700298 register_page_bootmem_info_section(pfn);
299 }
Yasunori Goto04753272008-04-28 02:13:31 -0700300}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800301#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700302
Fabian Frederickf2765402014-08-06 16:04:57 -0700303static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
304 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700305{
306 unsigned long old_zone_end_pfn;
307
308 zone_span_writelock(zone);
309
Xishi Qiuc33bc312013-09-11 14:21:44 -0700310 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700311 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700312 zone->zone_start_pfn = start_pfn;
313
314 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
315 zone->zone_start_pfn;
316
317 zone_span_writeunlock(zone);
318}
319
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800320static void resize_zone(struct zone *zone, unsigned long start_pfn,
321 unsigned long end_pfn)
322{
323 zone_span_writelock(zone);
324
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800325 if (end_pfn - start_pfn) {
326 zone->zone_start_pfn = start_pfn;
327 zone->spanned_pages = end_pfn - start_pfn;
328 } else {
329 /*
330 * make it consist as free_area_init_core(),
331 * if spanned_pages = 0, then keep start_pfn = 0
332 */
333 zone->zone_start_pfn = 0;
334 zone->spanned_pages = 0;
335 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800336
337 zone_span_writeunlock(zone);
338}
339
340static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
341 unsigned long end_pfn)
342{
343 enum zone_type zid = zone_idx(zone);
344 int nid = zone->zone_pgdat->node_id;
345 unsigned long pfn;
346
347 for (pfn = start_pfn; pfn < end_pfn; pfn++)
348 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
349}
350
Michal Hockodc0bbf32017-07-06 15:37:35 -0700351static void __ref ensure_zone_is_initialized(struct zone *zone,
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800352 unsigned long start_pfn, unsigned long num_pages)
353{
354 if (!zone_is_initialized(zone))
Michal Hockodc0bbf32017-07-06 15:37:35 -0700355 init_currently_empty_zone(zone, start_pfn, num_pages);
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800356}
357
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800358static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800359 unsigned long start_pfn, unsigned long end_pfn)
360{
361 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800362 unsigned long z1_start_pfn;
363
Michal Hockodc0bbf32017-07-06 15:37:35 -0700364 ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800365
366 pgdat_resize_lock(z1->zone_pgdat, &flags);
367
368 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800369 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800370 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700371 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800372 if (start_pfn > z2->zone_start_pfn)
373 goto out_fail;
374 /* must included/overlap */
375 if (end_pfn <= z2->zone_start_pfn)
376 goto out_fail;
377
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800378 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700379 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800380 z1_start_pfn = z1->zone_start_pfn;
381 else
382 z1_start_pfn = start_pfn;
383
384 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800385 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800386
387 pgdat_resize_unlock(z1->zone_pgdat, &flags);
388
389 fix_zone_id(z1, start_pfn, end_pfn);
390
391 return 0;
392out_fail:
393 pgdat_resize_unlock(z1->zone_pgdat, &flags);
394 return -1;
395}
396
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800397static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800398 unsigned long start_pfn, unsigned long end_pfn)
399{
400 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800401 unsigned long z2_end_pfn;
402
Michal Hockodc0bbf32017-07-06 15:37:35 -0700403 ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800404
405 pgdat_resize_lock(z1->zone_pgdat, &flags);
406
407 /* can't move pfns which are lower than @z1 */
408 if (z1->zone_start_pfn > start_pfn)
409 goto out_fail;
410 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800411 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800412 goto out_fail;
413 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800415 goto out_fail;
416
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800417 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700418 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800419 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800420 else
421 z2_end_pfn = end_pfn;
422
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800423 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800424 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800425
426 pgdat_resize_unlock(z1->zone_pgdat, &flags);
427
428 fix_zone_id(z2, start_pfn, end_pfn);
429
430 return 0;
431out_fail:
432 pgdat_resize_unlock(z1->zone_pgdat, &flags);
433 return -1;
434}
435
Reza Arbabe51e6c82016-07-26 15:22:20 -0700436static struct zone * __meminit move_pfn_range(int zone_shift,
437 unsigned long start_pfn, unsigned long end_pfn)
438{
439 struct zone *zone = page_zone(pfn_to_page(start_pfn));
440 int ret = 0;
441
442 if (zone_shift < 0)
443 ret = move_pfn_range_left(zone + zone_shift, zone,
444 start_pfn, end_pfn);
445 else if (zone_shift)
446 ret = move_pfn_range_right(zone, zone + zone_shift,
447 start_pfn, end_pfn);
448
449 if (ret)
450 return NULL;
451
452 return zone + zone_shift;
453}
454
Fabian Frederickf2765402014-08-06 16:04:57 -0700455static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
456 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700457{
Xishi Qiu83285c72013-11-12 15:07:19 -0800458 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700459
Tang Chen712cd382012-12-11 16:01:07 -0800460 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700461 pgdat->node_start_pfn = start_pfn;
462
463 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
464 pgdat->node_start_pfn;
465}
466
Al Viro31168482008-11-22 17:33:24 +0000467static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700468{
469 struct pglist_data *pgdat = zone->zone_pgdat;
470 int nr_pages = PAGES_PER_SECTION;
471 int nid = pgdat->node_id;
472 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700473 unsigned long flags, pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700474
475 zone_type = zone - pgdat->node_zones;
Michal Hockodc0bbf32017-07-06 15:37:35 -0700476 ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700477
Heiko Carstens76cdd582008-05-14 16:05:52 -0700478 pgdat_resize_lock(zone->zone_pgdat, &flags);
479 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
480 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
481 phys_start_pfn + nr_pages);
482 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800483 memmap_init_zone(nr_pages, nid, zone_type,
484 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700485
486 /* online_page_range is called later and expects pages reserved */
487 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
488 if (!pfn_valid(pfn))
489 continue;
490
491 SetPageReserved(pfn_to_page(pfn));
492 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700493 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700494}
495
Gary Hadec04fc582009-01-06 14:39:14 -0800496static int __meminit __add_section(int nid, struct zone *zone,
497 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700498{
Dave Hansen3947be12005-10-29 18:16:54 -0700499 int ret;
500
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700501 if (pfn_valid(phys_start_pfn))
502 return -EEXIST;
503
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800504 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700505
506 if (ret < 0)
507 return ret;
508
Yasunori Goto718127c2006-06-23 02:03:10 -0700509 ret = __add_zone(zone, phys_start_pfn);
510
511 if (ret < 0)
512 return ret;
513
Gary Hadec04fc582009-01-06 14:39:14 -0800514 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700515}
516
David Rientjes4edd7ce2013-04-29 15:08:22 -0700517/*
518 * Reasonably generic function for adding memory. It is
519 * expected that archs that support memory hotplug will
520 * call this function after deciding the zone to which to
521 * add the new pages.
522 */
523int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
524 unsigned long nr_pages)
525{
526 unsigned long i;
527 int err = 0;
528 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800529 struct vmem_altmap *altmap;
530
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700531 clear_zone_contiguous(zone);
532
David Rientjes4edd7ce2013-04-29 15:08:22 -0700533 /* during initialize mem_map, align hot-added range to section */
534 start_sec = pfn_to_section_nr(phys_start_pfn);
535 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
536
Dan Williams4b94ffd2016-01-15 16:56:22 -0800537 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
538 if (altmap) {
539 /*
540 * Validate altmap is within bounds of the total request
541 */
542 if (altmap->base_pfn != phys_start_pfn
543 || vmem_altmap_offset(altmap) > nr_pages) {
544 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700545 err = -EINVAL;
546 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800547 }
548 altmap->alloc = 0;
549 }
550
David Rientjes4edd7ce2013-04-29 15:08:22 -0700551 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700552 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700553
554 /*
555 * EEXIST is finally dealt with by ioresource collision
556 * check. see add_memory() => register_memory_resource()
557 * Warning will be printed if there is collision.
558 */
559 if (err && (err != -EEXIST))
560 break;
561 err = 0;
562 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700563 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700564out:
565 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700566 return err;
567}
568EXPORT_SYMBOL_GPL(__add_pages);
569
570#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800571/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
572static int find_smallest_section_pfn(int nid, struct zone *zone,
573 unsigned long start_pfn,
574 unsigned long end_pfn)
575{
576 struct mem_section *ms;
577
578 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
579 ms = __pfn_to_section(start_pfn);
580
581 if (unlikely(!valid_section(ms)))
582 continue;
583
584 if (unlikely(pfn_to_nid(start_pfn) != nid))
585 continue;
586
587 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
588 continue;
589
590 return start_pfn;
591 }
592
593 return 0;
594}
595
596/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
597static int find_biggest_section_pfn(int nid, struct zone *zone,
598 unsigned long start_pfn,
599 unsigned long end_pfn)
600{
601 struct mem_section *ms;
602 unsigned long pfn;
603
604 /* pfn is the end pfn of a memory section. */
605 pfn = end_pfn - 1;
606 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
607 ms = __pfn_to_section(pfn);
608
609 if (unlikely(!valid_section(ms)))
610 continue;
611
612 if (unlikely(pfn_to_nid(pfn) != nid))
613 continue;
614
615 if (zone && zone != page_zone(pfn_to_page(pfn)))
616 continue;
617
618 return pfn;
619 }
620
621 return 0;
622}
623
624static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
625 unsigned long end_pfn)
626{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700627 unsigned long zone_start_pfn = zone->zone_start_pfn;
628 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
629 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800630 unsigned long pfn;
631 struct mem_section *ms;
632 int nid = zone_to_nid(zone);
633
634 zone_span_writelock(zone);
635 if (zone_start_pfn == start_pfn) {
636 /*
637 * If the section is smallest section in the zone, it need
638 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
639 * In this case, we find second smallest valid mem_section
640 * for shrinking zone.
641 */
642 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
643 zone_end_pfn);
644 if (pfn) {
645 zone->zone_start_pfn = pfn;
646 zone->spanned_pages = zone_end_pfn - pfn;
647 }
648 } else if (zone_end_pfn == end_pfn) {
649 /*
650 * If the section is biggest section in the zone, it need
651 * shrink zone->spanned_pages.
652 * In this case, we find second biggest valid mem_section for
653 * shrinking zone.
654 */
655 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
656 start_pfn);
657 if (pfn)
658 zone->spanned_pages = pfn - zone_start_pfn + 1;
659 }
660
661 /*
662 * The section is not biggest or smallest mem_section in the zone, it
663 * only creates a hole in the zone. So in this case, we need not
664 * change the zone. But perhaps, the zone has only hole data. Thus
665 * it check the zone has only hole or not.
666 */
667 pfn = zone_start_pfn;
668 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
669 ms = __pfn_to_section(pfn);
670
671 if (unlikely(!valid_section(ms)))
672 continue;
673
674 if (page_zone(pfn_to_page(pfn)) != zone)
675 continue;
676
677 /* If the section is current section, it continues the loop */
678 if (start_pfn == pfn)
679 continue;
680
681 /* If we find valid section, we have nothing to do */
682 zone_span_writeunlock(zone);
683 return;
684 }
685
686 /* The zone has no valid section */
687 zone->zone_start_pfn = 0;
688 zone->spanned_pages = 0;
689 zone_span_writeunlock(zone);
690}
691
692static void shrink_pgdat_span(struct pglist_data *pgdat,
693 unsigned long start_pfn, unsigned long end_pfn)
694{
Xishi Qiu83285c72013-11-12 15:07:19 -0800695 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
696 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
697 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800698 unsigned long pfn;
699 struct mem_section *ms;
700 int nid = pgdat->node_id;
701
702 if (pgdat_start_pfn == start_pfn) {
703 /*
704 * If the section is smallest section in the pgdat, it need
705 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
706 * In this case, we find second smallest valid mem_section
707 * for shrinking zone.
708 */
709 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
710 pgdat_end_pfn);
711 if (pfn) {
712 pgdat->node_start_pfn = pfn;
713 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
714 }
715 } else if (pgdat_end_pfn == end_pfn) {
716 /*
717 * If the section is biggest section in the pgdat, it need
718 * shrink pgdat->node_spanned_pages.
719 * In this case, we find second biggest valid mem_section for
720 * shrinking zone.
721 */
722 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
723 start_pfn);
724 if (pfn)
725 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
726 }
727
728 /*
729 * If the section is not biggest or smallest mem_section in the pgdat,
730 * it only creates a hole in the pgdat. So in this case, we need not
731 * change the pgdat.
732 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
733 * has only hole or not.
734 */
735 pfn = pgdat_start_pfn;
736 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
737 ms = __pfn_to_section(pfn);
738
739 if (unlikely(!valid_section(ms)))
740 continue;
741
742 if (pfn_to_nid(pfn) != nid)
743 continue;
744
745 /* If the section is current section, it continues the loop */
746 if (start_pfn == pfn)
747 continue;
748
749 /* If we find valid section, we have nothing to do */
750 return;
751 }
752
753 /* The pgdat has no valid section */
754 pgdat->node_start_pfn = 0;
755 pgdat->node_spanned_pages = 0;
756}
757
758static void __remove_zone(struct zone *zone, unsigned long start_pfn)
759{
760 struct pglist_data *pgdat = zone->zone_pgdat;
761 int nr_pages = PAGES_PER_SECTION;
762 int zone_type;
763 unsigned long flags;
764
765 zone_type = zone - pgdat->node_zones;
766
767 pgdat_resize_lock(zone->zone_pgdat, &flags);
768 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
769 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
770 pgdat_resize_unlock(zone->zone_pgdat, &flags);
771}
772
Dan Williams4b94ffd2016-01-15 16:56:22 -0800773static int __remove_section(struct zone *zone, struct mem_section *ms,
774 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700775{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800776 unsigned long start_pfn;
777 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700778 int ret = -EINVAL;
779
780 if (!valid_section(ms))
781 return ret;
782
783 ret = unregister_memory_section(ms);
784 if (ret)
785 return ret;
786
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800787 scn_nr = __section_nr(ms);
788 start_pfn = section_nr_to_pfn(scn_nr);
789 __remove_zone(zone, start_pfn);
790
Dan Williams4b94ffd2016-01-15 16:56:22 -0800791 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700792 return 0;
793}
794
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700795/**
796 * __remove_pages() - remove sections of pages from a zone
797 * @zone: zone from which pages need to be removed
798 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
799 * @nr_pages: number of pages to remove (must be multiple of section size)
800 *
801 * Generic helper function to remove section mappings and sysfs entries
802 * for the section of the memory we are removing. Caller needs to make
803 * sure that pages are marked reserved and zones are adjust properly by
804 * calling offline_pages().
805 */
806int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
807 unsigned long nr_pages)
808{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700809 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800810 unsigned long map_offset = 0;
811 int sections_to_remove, ret = 0;
812
813 /* In the ZONE_DEVICE case device driver owns the memory region */
814 if (is_dev_zone(zone)) {
815 struct page *page = pfn_to_page(phys_start_pfn);
816 struct vmem_altmap *altmap;
817
818 altmap = to_vmem_altmap((unsigned long) page);
819 if (altmap)
820 map_offset = vmem_altmap_offset(altmap);
821 } else {
822 resource_size_t start, size;
823
824 start = phys_start_pfn << PAGE_SHIFT;
825 size = nr_pages * PAGE_SIZE;
826
827 ret = release_mem_region_adjustable(&iomem_resource, start,
828 size);
829 if (ret) {
830 resource_size_t endres = start + size - 1;
831
832 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
833 &start, &endres, ret);
834 }
835 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700836
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700837 clear_zone_contiguous(zone);
838
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700839 /*
840 * We can only remove entire sections
841 */
842 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
843 BUG_ON(nr_pages % PAGES_PER_SECTION);
844
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700845 sections_to_remove = nr_pages / PAGES_PER_SECTION;
846 for (i = 0; i < sections_to_remove; i++) {
847 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800848
849 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
850 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700851 if (ret)
852 break;
853 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700854
855 set_zone_contiguous(zone);
856
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700857 return ret;
858}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700859#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700860
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700861int set_online_page_callback(online_page_callback_t callback)
862{
863 int rc = -EINVAL;
864
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700865 get_online_mems();
866 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700867
868 if (online_page_callback == generic_online_page) {
869 online_page_callback = callback;
870 rc = 0;
871 }
872
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700873 mutex_unlock(&online_page_callback_lock);
874 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700875
876 return rc;
877}
878EXPORT_SYMBOL_GPL(set_online_page_callback);
879
880int restore_online_page_callback(online_page_callback_t callback)
881{
882 int rc = -EINVAL;
883
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700884 get_online_mems();
885 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700886
887 if (online_page_callback == callback) {
888 online_page_callback = generic_online_page;
889 rc = 0;
890 }
891
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700892 mutex_unlock(&online_page_callback_lock);
893 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700894
895 return rc;
896}
897EXPORT_SYMBOL_GPL(restore_online_page_callback);
898
899void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700900{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700901}
902EXPORT_SYMBOL_GPL(__online_page_set_limits);
903
904void __online_page_increment_counters(struct page *page)
905{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700906 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700907}
908EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700909
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700910void __online_page_free(struct page *page)
911{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700912 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700913}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700914EXPORT_SYMBOL_GPL(__online_page_free);
915
916static void generic_online_page(struct page *page)
917{
918 __online_page_set_limits(page);
919 __online_page_increment_counters(page);
920 __online_page_free(page);
921}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700922
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700923static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
924 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700925{
926 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700927 unsigned long onlined_pages = *(unsigned long *)arg;
928 struct page *page;
929 if (PageReserved(pfn_to_page(start_pfn)))
930 for (i = 0; i < nr_pages; i++) {
931 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700932 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700933 onlined_pages++;
934 }
935 *(unsigned long *)arg = onlined_pages;
936 return 0;
937}
938
Lai Jiangshan09285af2012-12-12 13:52:04 -0800939#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800940/*
941 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
942 * normal memory.
943 */
Michal Hockoc8f95652017-07-06 15:37:38 -0700944static bool can_online_high_movable(int nid)
Lai Jiangshan09285af2012-12-12 13:52:04 -0800945{
946 return true;
947}
Tang Chen79a4dce2012-12-18 14:23:24 -0800948#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800949/* ensure every online node has NORMAL memory */
Michal Hockoc8f95652017-07-06 15:37:38 -0700950static bool can_online_high_movable(int nid)
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800951{
Michal Hockoc8f95652017-07-06 15:37:38 -0700952 return node_state(nid, N_NORMAL_MEMORY);
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800953}
Tang Chen79a4dce2012-12-18 14:23:24 -0800954#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800955
Lai Jiangshand9713672012-12-11 16:01:03 -0800956/* check which state of node_states will be changed when online memory */
957static void node_states_check_changes_online(unsigned long nr_pages,
958 struct zone *zone, struct memory_notify *arg)
959{
960 int nid = zone_to_nid(zone);
961 enum zone_type zone_last = ZONE_NORMAL;
962
963 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800964 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
965 * contains nodes which have zones of 0...ZONE_NORMAL,
966 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800967 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800968 * If we don't have HIGHMEM nor movable node,
969 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
970 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800971 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800972 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800973 zone_last = ZONE_MOVABLE;
974
975 /*
976 * if the memory to be online is in a zone of 0...zone_last, and
977 * the zones of 0...zone_last don't have memory before online, we will
978 * need to set the node to node_states[N_NORMAL_MEMORY] after
979 * the memory is online.
980 */
981 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
982 arg->status_change_nid_normal = nid;
983 else
984 arg->status_change_nid_normal = -1;
985
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800986#ifdef CONFIG_HIGHMEM
987 /*
988 * If we have movable node, node_states[N_HIGH_MEMORY]
989 * contains nodes which have zones of 0...ZONE_HIGHMEM,
990 * set zone_last to ZONE_HIGHMEM.
991 *
992 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
993 * contains nodes which have zones of 0...ZONE_MOVABLE,
994 * set zone_last to ZONE_MOVABLE.
995 */
996 zone_last = ZONE_HIGHMEM;
997 if (N_MEMORY == N_HIGH_MEMORY)
998 zone_last = ZONE_MOVABLE;
999
1000 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1001 arg->status_change_nid_high = nid;
1002 else
1003 arg->status_change_nid_high = -1;
1004#else
1005 arg->status_change_nid_high = arg->status_change_nid_normal;
1006#endif
1007
Lai Jiangshand9713672012-12-11 16:01:03 -08001008 /*
1009 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001010 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001011 * is online.
1012 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001013 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001014 arg->status_change_nid = nid;
1015 else
1016 arg->status_change_nid = -1;
1017}
1018
1019static void node_states_set_node(int node, struct memory_notify *arg)
1020{
1021 if (arg->status_change_nid_normal >= 0)
1022 node_set_state(node, N_NORMAL_MEMORY);
1023
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001024 if (arg->status_change_nid_high >= 0)
1025 node_set_state(node, N_HIGH_MEMORY);
1026
1027 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001028}
1029
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001030bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1031 enum zone_type target, int *zone_shift)
Reza Arbabdf429ac2016-07-26 15:22:23 -07001032{
1033 struct zone *zone = page_zone(pfn_to_page(pfn));
1034 enum zone_type idx = zone_idx(zone);
1035 int i;
1036
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001037 *zone_shift = 0;
1038
Reza Arbabdf429ac2016-07-26 15:22:23 -07001039 if (idx < target) {
1040 /* pages must be at end of current zone */
1041 if (pfn + nr_pages != zone_end_pfn(zone))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001042 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001043
1044 /* no zones in use between current zone and target */
1045 for (i = idx + 1; i < target; i++)
1046 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001047 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001048 }
1049
1050 if (target < idx) {
1051 /* pages must be at beginning of current zone */
1052 if (pfn != zone->zone_start_pfn)
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001053 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001054
1055 /* no zones in use between current zone and target */
1056 for (i = target + 1; i < idx; i++)
1057 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001058 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001059 }
1060
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001061 *zone_shift = target - idx;
1062 return true;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001063}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001064
David Rientjes30467e02015-04-14 15:45:11 -07001065/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001066int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001067{
Cody P Schaferaa472282013-07-03 15:02:10 -07001068 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001069 unsigned long onlined_pages = 0;
1070 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001071 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001072 int nid;
1073 int ret;
1074 struct memory_notify arg;
Reza Arbabe51e6c82016-07-26 15:22:20 -07001075 int zone_shift = 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001076
Lai Jiangshand9713672012-12-11 16:01:03 -08001077 /*
1078 * This doesn't need a lock to do pfn_to_page().
1079 * The section can't be removed here because of the
1080 * memory_block->state_mutex.
1081 */
1082 zone = page_zone(pfn_to_page(pfn));
1083
Tang Chen4f7c6b42014-08-06 16:05:13 -07001084 if ((zone_idx(zone) > ZONE_NORMAL ||
1085 online_type == MMOP_ONLINE_MOVABLE) &&
Michal Hockoc8f95652017-07-06 15:37:38 -07001086 !can_online_high_movable(pfn_to_nid(pfn)))
David Rientjes30467e02015-04-14 15:45:11 -07001087 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001088
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001089 if (online_type == MMOP_ONLINE_KERNEL) {
1090 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1091 return -EINVAL;
1092 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1093 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1094 return -EINVAL;
1095 }
Reza Arbabe51e6c82016-07-26 15:22:20 -07001096
1097 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1098 if (!zone)
1099 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001100
Yasunori Goto7b78d332007-10-21 16:41:36 -07001101 arg.start_pfn = pfn;
1102 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001103 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001104
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001105 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001106
1107 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1108 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001109 if (ret)
1110 goto failed_addition;
1111
Dave Hansen3947be12005-10-29 18:16:54 -07001112 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001113 * If this zone is not populated, then it is not in zonelist.
1114 * This means the page allocator ignores this zone.
1115 * So, zonelist must be updated after online.
1116 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001117 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001118 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001119 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001120 build_all_zonelists(NULL, zone);
1121 }
Yasunori Goto68113782006-06-23 02:03:11 -07001122
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001123 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001124 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001125 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001126 if (need_zonelists_rebuild)
1127 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001128 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001129 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001130 }
1131
Dave Hansen3947be12005-10-29 18:16:54 -07001132 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001133
1134 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001135 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001136 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1137
Jiang Liu08dff7b2012-07-31 16:43:30 -07001138 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001139 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001140 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001141 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001142 else
1143 zone_pcp_update(zone);
1144 }
Dave Hansen3947be12005-10-29 18:16:54 -07001145
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001146 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001147
1148 init_per_zone_wmark_min();
1149
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001150 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001151 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001152 kcompactd_run(nid);
1153 }
Dave Hansen61b13992005-10-29 18:16:56 -07001154
Haicheng Li1f522502010-05-24 14:32:51 -07001155 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001156
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001157 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001158
1159 if (onlined_pages)
1160 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001161 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001162
1163failed_addition:
1164 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1165 (unsigned long long) pfn << PAGE_SHIFT,
1166 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1167 memory_notify(MEM_CANCEL_ONLINE, &arg);
1168 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001169}
Keith Mannthey53947022006-09-30 23:27:08 -07001170#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001171
Tang Chen0bd85422014-11-13 15:19:41 -08001172static void reset_node_present_pages(pg_data_t *pgdat)
1173{
1174 struct zone *z;
1175
1176 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1177 z->present_pages = 0;
1178
1179 pgdat->node_present_pages = 0;
1180}
1181
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001182/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1183static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001184{
1185 struct pglist_data *pgdat;
1186 unsigned long zones_size[MAX_NR_ZONES] = {0};
1187 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001188 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001189
Tang Chena1e565a2013-02-22 16:33:18 -08001190 pgdat = NODE_DATA(nid);
1191 if (!pgdat) {
1192 pgdat = arch_alloc_nodedata(nid);
1193 if (!pgdat)
1194 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001195
Tang Chena1e565a2013-02-22 16:33:18 -08001196 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001197 } else {
Mel Gormane716f2e2017-05-03 14:53:45 -07001198 /*
1199 * Reset the nr_zones, order and classzone_idx before reuse.
1200 * Note that kswapd will init kswapd_classzone_idx properly
1201 * when it starts in the near future.
1202 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001203 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001204 pgdat->kswapd_order = 0;
1205 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001206 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001207
1208 /* we can use NODE_DATA(nid) from here */
1209
1210 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001211 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001212 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001213
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001214 /*
1215 * The node we allocated has no zone fallback lists. For avoiding
1216 * to access not-initialized zonelist, build here.
1217 */
David Rientjesf957db42011-06-22 18:13:04 -07001218 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001219 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001220 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001221
Tang Chenf784a3f2014-11-13 15:19:39 -08001222 /*
1223 * zone->managed_pages is set to an approximate value in
1224 * free_area_init_core(), which will cause
1225 * /sys/device/system/node/nodeX/meminfo has wrong data.
1226 * So reset it to 0 before any memory is onlined.
1227 */
1228 reset_node_managed_pages(pgdat);
1229
Tang Chen0bd85422014-11-13 15:19:41 -08001230 /*
1231 * When memory is hot-added, all the memory is in offline state. So
1232 * clear all zones' present_pages because they will be updated in
1233 * online_pages() and offline_pages().
1234 */
1235 reset_node_present_pages(pgdat);
1236
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001237 return pgdat;
1238}
1239
1240static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1241{
1242 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001243 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001244 arch_free_nodedata(pgdat);
1245 return;
1246}
1247
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001248
Toshi Kani01b0f192013-11-12 15:07:25 -08001249/**
1250 * try_online_node - online a node if offlined
1251 *
minskey guocf234222010-05-24 14:32:41 -07001252 * called by cpu_up() to online a node without onlined memory.
1253 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001254int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001255{
1256 pg_data_t *pgdat;
1257 int ret;
1258
Toshi Kani01b0f192013-11-12 15:07:25 -08001259 if (node_online(nid))
1260 return 0;
1261
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001262 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001263 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001264 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001265 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001266 ret = -ENOMEM;
1267 goto out;
1268 }
1269 node_set_online(nid);
1270 ret = register_one_node(nid);
1271 BUG_ON(ret);
1272
Toshi Kani01b0f192013-11-12 15:07:25 -08001273 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1274 mutex_lock(&zonelists_mutex);
1275 build_all_zonelists(NULL, NULL);
1276 mutex_unlock(&zonelists_mutex);
1277 }
1278
minskey guocf234222010-05-24 14:32:41 -07001279out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001280 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001281 return ret;
1282}
1283
Toshi Kani27356f52013-09-11 14:21:49 -07001284static int check_hotplug_memory_range(u64 start, u64 size)
1285{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001286 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001287 u64 nr_pages = size >> PAGE_SHIFT;
1288
1289 /* Memory range must be aligned with section */
1290 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1291 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1292 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1293 (unsigned long long)start,
1294 (unsigned long long)size);
1295 return -EINVAL;
1296 }
1297
1298 return 0;
1299}
1300
Wang Nan63264402014-08-06 16:07:36 -07001301/*
1302 * If movable zone has already been setup, newly added memory should be check.
1303 * If its address is higher than movable zone, it should be added as movable.
1304 * Without this check, movable zone may overlap with other zone.
1305 */
1306static int should_add_memory_movable(int nid, u64 start, u64 size)
1307{
1308 unsigned long start_pfn = start >> PAGE_SHIFT;
1309 pg_data_t *pgdat = NODE_DATA(nid);
1310 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1311
1312 if (zone_is_empty(movable_zone))
1313 return 0;
1314
1315 if (movable_zone->zone_start_pfn <= start_pfn)
1316 return 1;
1317
1318 return 0;
1319}
1320
Dan Williams033fbae2015-08-09 15:29:06 -04001321int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1322 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001323{
Dan Williams033fbae2015-08-09 15:29:06 -04001324#ifdef CONFIG_ZONE_DEVICE
1325 if (for_device)
1326 return ZONE_DEVICE;
1327#endif
Wang Nan63264402014-08-06 16:07:36 -07001328 if (should_add_memory_movable(nid, start, size))
1329 return ZONE_MOVABLE;
1330
1331 return zone_default;
1332}
1333
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001334static int online_memory_block(struct memory_block *mem, void *arg)
1335{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001336 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001337}
1338
Al Viro31168482008-11-22 17:33:24 +00001339/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001340int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001341{
David Vrabel62cedb92015-06-25 16:35:49 +01001342 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001343 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001344 bool new_pgdat;
1345 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001346 int ret;
1347
David Vrabel62cedb92015-06-25 16:35:49 +01001348 start = res->start;
1349 size = resource_size(res);
1350
Toshi Kani27356f52013-09-11 14:21:49 -07001351 ret = check_hotplug_memory_range(start, size);
1352 if (ret)
1353 return ret;
1354
Tang Chena1e565a2013-02-22 16:33:18 -08001355 { /* Stupid hack to suppress address-never-null warning */
1356 void *p = NODE_DATA(nid);
1357 new_pgdat = !p;
1358 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001359
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001360 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001361
Tang Chen7f36e3e2015-09-04 15:42:32 -07001362 /*
1363 * Add new range to memblock so that when hotadd_new_pgdat() is called
1364 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1365 * this new range and calculate total pages correctly. The range will
1366 * be removed at hot-remove time.
1367 */
1368 memblock_add_node(start, size, nid);
1369
Tang Chena1e565a2013-02-22 16:33:18 -08001370 new_node = !node_online(nid);
1371 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001372 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001373 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001374 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001375 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001376 }
1377
Yasunori Gotobc02af92006-06-27 02:53:30 -07001378 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001379 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001380
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001381 if (ret < 0)
1382 goto error;
1383
Yasunori Goto0fc44152006-06-27 02:53:38 -07001384 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001385 node_set_online(nid);
1386
Tang Chena1e565a2013-02-22 16:33:18 -08001387 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001388 ret = register_one_node(nid);
1389 /*
1390 * If sysfs file of new node can't create, cpu on the node
1391 * can't be hot-added. There is no rollback way now.
1392 * So, check by BUG_ON() to catch it reluctantly..
1393 */
1394 BUG_ON(ret);
1395 }
1396
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001397 /* create new memmap entry */
1398 firmware_map_add_hotplug(start, start + size, "System RAM");
1399
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001400 /* online pages if requested */
1401 if (online)
1402 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1403 NULL, online_memory_block);
1404
Andi Kleen6ad696d2009-11-17 14:06:22 -08001405 goto out;
1406
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001407error:
1408 /* rollback pgdat allocation and others */
1409 if (new_pgdat)
1410 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001411 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001412
Andi Kleen6ad696d2009-11-17 14:06:22 -08001413out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001414 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001415 return ret;
1416}
David Vrabel62cedb92015-06-25 16:35:49 +01001417EXPORT_SYMBOL_GPL(add_memory_resource);
1418
1419int __ref add_memory(int nid, u64 start, u64 size)
1420{
1421 struct resource *res;
1422 int ret;
1423
1424 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001425 if (IS_ERR(res))
1426 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001427
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001428 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001429 if (ret < 0)
1430 release_memory_resource(res);
1431 return ret;
1432}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001433EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001434
1435#ifdef CONFIG_MEMORY_HOTREMOVE
1436/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001437 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1438 * set and the size of the free page is given by page_order(). Using this,
1439 * the function determines if the pageblock contains only free pages.
1440 * Due to buddy contraints, a free page at least the size of a pageblock will
1441 * be located at the start of the pageblock
1442 */
1443static inline int pageblock_free(struct page *page)
1444{
1445 return PageBuddy(page) && page_order(page) >= pageblock_order;
1446}
1447
1448/* Return the start of the next active pageblock after a given page */
1449static struct page *next_active_pageblock(struct page *page)
1450{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001451 /* Ensure the starting page is pageblock-aligned */
1452 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1453
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001454 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001455 if (pageblock_free(page)) {
1456 int order;
1457 /* be careful. we don't have locks, page_order can be changed.*/
1458 order = page_order(page);
1459 if ((order < MAX_ORDER) && (order >= pageblock_order))
1460 return page + (1 << order);
1461 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001462
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001463 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001464}
1465
1466/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001467bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001468{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001469 struct page *page = pfn_to_page(start_pfn);
1470 struct page *end_page = page + nr_pages;
1471
1472 /* Check the starting page of each pageblock within the range */
1473 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001474 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001475 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001476 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001477 }
1478
1479 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001480 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001481}
1482
1483/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001484 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001485 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001486 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001487int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1488 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001489{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001490 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001491 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001492 struct zone *zone = NULL;
1493 struct page *page;
1494 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001495 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001496 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001497 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001498 /* Make sure the memory section is present first */
1499 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001500 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001501 for (; pfn < sec_end_pfn && pfn < end_pfn;
1502 pfn += MAX_ORDER_NR_PAGES) {
1503 i = 0;
1504 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1505 while ((i < MAX_ORDER_NR_PAGES) &&
1506 !pfn_valid_within(pfn + i))
1507 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001508 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001509 continue;
1510 page = pfn_to_page(pfn + i);
1511 if (zone && page_zone(page) != zone)
1512 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001513 if (!zone)
1514 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001515 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001516 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001517 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001518 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001519
Toshi Kania96dfdd2017-02-03 13:13:23 -08001520 if (zone) {
1521 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001522 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001523 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001524 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001525 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001526 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001527}
1528
1529/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001530 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1531 * non-lru movable pages and hugepages). We scan pfn because it's much
1532 * easier than scanning over linked list. This function returns the pfn
1533 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001534 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001535static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001536{
1537 unsigned long pfn;
1538 struct page *page;
1539 for (pfn = start; pfn < end; pfn++) {
1540 if (pfn_valid(pfn)) {
1541 page = pfn_to_page(pfn);
1542 if (PageLRU(page))
1543 return pfn;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001544 if (__PageMovable(page))
1545 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001546 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001547 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001548 return pfn;
1549 else
1550 pfn = round_up(pfn + 1,
1551 1 << compound_order(page)) - 1;
1552 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001553 }
1554 }
1555 return 0;
1556}
1557
Xishi Qiu394e31d2016-07-28 15:48:53 -07001558static struct page *new_node_page(struct page *page, unsigned long private,
1559 int **result)
1560{
1561 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1562 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001563 nodemask_t nmask = node_states[N_MEMORY];
1564 struct page *new_page = NULL;
Xishi Qiu394e31d2016-07-28 15:48:53 -07001565
1566 /*
1567 * TODO: allocate a destination hugepage from a nearest neighbor node,
1568 * accordance with memory policy of the user process if possible. For
1569 * now as a simple work-around, we use the next node for destination.
1570 */
1571 if (PageHuge(page))
1572 return alloc_huge_page_node(page_hstate(compound_head(page)),
1573 next_node_in(nid, nmask));
1574
Li Zhong231e97e2016-09-28 15:22:38 -07001575 node_clear(nid, nmask);
Li Zhong9bb627b2016-09-19 14:43:52 -07001576
Xishi Qiu394e31d2016-07-28 15:48:53 -07001577 if (PageHighMem(page)
1578 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1579 gfp_mask |= __GFP_HIGHMEM;
1580
Li Zhong231e97e2016-09-28 15:22:38 -07001581 if (!nodes_empty(nmask))
1582 new_page = __alloc_pages_nodemask(gfp_mask, 0,
Xishi Qiu394e31d2016-07-28 15:48:53 -07001583 node_zonelist(nid, gfp_mask), &nmask);
1584 if (!new_page)
1585 new_page = __alloc_pages(gfp_mask, 0,
1586 node_zonelist(nid, gfp_mask));
1587
1588 return new_page;
1589}
1590
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001591#define NR_OFFLINE_AT_ONCE_PAGES (256)
1592static int
1593do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1594{
1595 unsigned long pfn;
1596 struct page *page;
1597 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1598 int not_managed = 0;
1599 int ret = 0;
1600 LIST_HEAD(source);
1601
1602 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1603 if (!pfn_valid(pfn))
1604 continue;
1605 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001606
1607 if (PageHuge(page)) {
1608 struct page *head = compound_head(page);
1609 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1610 if (compound_order(head) > PFN_SECTION_SHIFT) {
1611 ret = -EBUSY;
1612 break;
1613 }
1614 if (isolate_huge_page(page, &source))
1615 move_pages -= 1 << compound_order(head);
1616 continue;
1617 }
1618
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001619 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001620 continue;
1621 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001622 * We can skip free pages. And we can deal with pages on
1623 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001624 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001625 if (PageLRU(page))
1626 ret = isolate_lru_page(page);
1627 else
1628 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001629 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001630 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001631 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001632 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001633 if (!__PageMovable(page))
1634 inc_node_page_state(page, NR_ISOLATED_ANON +
1635 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001636
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001637 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001638#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001639 pr_alert("failed to isolate pfn %lx\n", pfn);
1640 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001641#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001642 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001643 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001644 check this again here. */
1645 if (page_count(page)) {
1646 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001647 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001648 break;
1649 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001650 }
1651 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001652 if (!list_empty(&source)) {
1653 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001654 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001655 goto out;
1656 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001657
Xishi Qiu394e31d2016-07-28 15:48:53 -07001658 /* Allocate a new page from the nearest neighbor node */
1659 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001660 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001661 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001662 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001663 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001664out:
1665 return ret;
1666}
1667
1668/*
1669 * remove from free_area[] and mark all as Reserved.
1670 */
1671static int
1672offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1673 void *data)
1674{
1675 __offline_isolated_pages(start, start + nr_pages);
1676 return 0;
1677}
1678
1679static void
1680offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1681{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001682 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001683 offline_isolated_pages_cb);
1684}
1685
1686/*
1687 * Check all pages in range, recoreded as memory resource, are isolated.
1688 */
1689static int
1690check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1691 void *data)
1692{
1693 int ret;
1694 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001695 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001696 offlined = nr_pages;
1697 if (!ret)
1698 *(long *)data += offlined;
1699 return ret;
1700}
1701
1702static long
1703check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1704{
1705 long offlined = 0;
1706 int ret;
1707
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001708 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001709 check_pages_isolated_cb);
1710 if (ret < 0)
1711 offlined = (long)ret;
1712 return offlined;
1713}
1714
Lai Jiangshan09285af2012-12-12 13:52:04 -08001715#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001716/*
1717 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1718 * normal memory.
1719 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001720static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1721{
1722 return true;
1723}
Tang Chen79a4dce2012-12-18 14:23:24 -08001724#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001725/* ensure the node has NORMAL memory if it is still online */
1726static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1727{
1728 struct pglist_data *pgdat = zone->zone_pgdat;
1729 unsigned long present_pages = 0;
1730 enum zone_type zt;
1731
1732 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1733 present_pages += pgdat->node_zones[zt].present_pages;
1734
1735 if (present_pages > nr_pages)
1736 return true;
1737
1738 present_pages = 0;
1739 for (; zt <= ZONE_MOVABLE; zt++)
1740 present_pages += pgdat->node_zones[zt].present_pages;
1741
1742 /*
1743 * we can't offline the last normal memory until all
1744 * higher memory is offlined.
1745 */
1746 return present_pages == 0;
1747}
Tang Chen79a4dce2012-12-18 14:23:24 -08001748#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001749
Tang Chenc5320922013-11-12 15:08:10 -08001750static int __init cmdline_parse_movable_node(char *p)
1751{
1752#ifdef CONFIG_MOVABLE_NODE
Tang Chen55ac5902014-01-21 15:49:35 -08001753 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001754#else
1755 pr_warn("movable_node option not supported\n");
1756#endif
1757 return 0;
1758}
1759early_param("movable_node", cmdline_parse_movable_node);
1760
Lai Jiangshand9713672012-12-11 16:01:03 -08001761/* check which state of node_states will be changed when offline memory */
1762static void node_states_check_changes_offline(unsigned long nr_pages,
1763 struct zone *zone, struct memory_notify *arg)
1764{
1765 struct pglist_data *pgdat = zone->zone_pgdat;
1766 unsigned long present_pages = 0;
1767 enum zone_type zt, zone_last = ZONE_NORMAL;
1768
1769 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001770 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1771 * contains nodes which have zones of 0...ZONE_NORMAL,
1772 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001773 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001774 * If we don't have HIGHMEM nor movable node,
1775 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1776 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001777 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001778 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001779 zone_last = ZONE_MOVABLE;
1780
1781 /*
1782 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1783 * If the memory to be offline is in a zone of 0...zone_last,
1784 * and it is the last present memory, 0...zone_last will
1785 * become empty after offline , thus we can determind we will
1786 * need to clear the node from node_states[N_NORMAL_MEMORY].
1787 */
1788 for (zt = 0; zt <= zone_last; zt++)
1789 present_pages += pgdat->node_zones[zt].present_pages;
1790 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1791 arg->status_change_nid_normal = zone_to_nid(zone);
1792 else
1793 arg->status_change_nid_normal = -1;
1794
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001795#ifdef CONFIG_HIGHMEM
1796 /*
1797 * If we have movable node, node_states[N_HIGH_MEMORY]
1798 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1799 * set zone_last to ZONE_HIGHMEM.
1800 *
1801 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1802 * contains nodes which have zones of 0...ZONE_MOVABLE,
1803 * set zone_last to ZONE_MOVABLE.
1804 */
1805 zone_last = ZONE_HIGHMEM;
1806 if (N_MEMORY == N_HIGH_MEMORY)
1807 zone_last = ZONE_MOVABLE;
1808
1809 for (; zt <= zone_last; zt++)
1810 present_pages += pgdat->node_zones[zt].present_pages;
1811 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1812 arg->status_change_nid_high = zone_to_nid(zone);
1813 else
1814 arg->status_change_nid_high = -1;
1815#else
1816 arg->status_change_nid_high = arg->status_change_nid_normal;
1817#endif
1818
Lai Jiangshand9713672012-12-11 16:01:03 -08001819 /*
1820 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1821 */
1822 zone_last = ZONE_MOVABLE;
1823
1824 /*
1825 * check whether node_states[N_HIGH_MEMORY] will be changed
1826 * If we try to offline the last present @nr_pages from the node,
1827 * we can determind we will need to clear the node from
1828 * node_states[N_HIGH_MEMORY].
1829 */
1830 for (; zt <= zone_last; zt++)
1831 present_pages += pgdat->node_zones[zt].present_pages;
1832 if (nr_pages >= present_pages)
1833 arg->status_change_nid = zone_to_nid(zone);
1834 else
1835 arg->status_change_nid = -1;
1836}
1837
1838static void node_states_clear_node(int node, struct memory_notify *arg)
1839{
1840 if (arg->status_change_nid_normal >= 0)
1841 node_clear_state(node, N_NORMAL_MEMORY);
1842
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001843 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1844 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001845 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001846
1847 if ((N_MEMORY != N_HIGH_MEMORY) &&
1848 (arg->status_change_nid >= 0))
1849 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001850}
1851
Wen Congyanga16cee12012-10-08 16:33:58 -07001852static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001853 unsigned long end_pfn, unsigned long timeout)
1854{
1855 unsigned long pfn, nr_pages, expire;
1856 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001857 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001858 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001859 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001860 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001861 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001862
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001863 /* at least, alignment against pageblock is necessary */
1864 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1865 return -EINVAL;
1866 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1867 return -EINVAL;
1868 /* This makes hotplug much easier...and readable.
1869 we assume this for now. .*/
Toshi Kania96dfdd2017-02-03 13:13:23 -08001870 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001871 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001872
Toshi Kania96dfdd2017-02-03 13:13:23 -08001873 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001874 node = zone_to_nid(zone);
1875 nr_pages = end_pfn - start_pfn;
1876
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001877 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001878 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001879
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001880 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001881 ret = start_isolate_page_range(start_pfn, end_pfn,
1882 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001883 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001884 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001885
1886 arg.start_pfn = start_pfn;
1887 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001888 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001889
1890 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1891 ret = notifier_to_errno(ret);
1892 if (ret)
1893 goto failed_removal;
1894
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001895 pfn = start_pfn;
1896 expire = jiffies + timeout;
1897 drain = 0;
1898 retry_max = 5;
1899repeat:
1900 /* start memory hot removal */
1901 ret = -EAGAIN;
1902 if (time_after(jiffies, expire))
1903 goto failed_removal;
1904 ret = -EINTR;
1905 if (signal_pending(current))
1906 goto failed_removal;
1907 ret = 0;
1908 if (drain) {
1909 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001910 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001911 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001912 }
1913
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001914 pfn = scan_movable_pages(start_pfn, end_pfn);
1915 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001916 ret = do_migrate_range(pfn, end_pfn);
1917 if (!ret) {
1918 drain = 1;
1919 goto repeat;
1920 } else {
1921 if (ret < 0)
1922 if (--retry_max == 0)
1923 goto failed_removal;
1924 yield();
1925 drain = 1;
1926 goto repeat;
1927 }
1928 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001929 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001930 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001931 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001932 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001933 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001934 /*
1935 * dissolve free hugepages in the memory block before doing offlining
1936 * actually in order to make hugetlbfs's object counting consistent.
1937 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001938 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1939 if (ret)
1940 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001941 /* check again */
1942 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1943 if (offlined_pages < 0) {
1944 ret = -EBUSY;
1945 goto failed_removal;
1946 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001947 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001948 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001949 We cannot do rollback at this point. */
1950 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001951 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001952 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001953 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001954 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001955 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001956
1957 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001958 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001959 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001960
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001961 init_per_zone_wmark_min();
1962
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001963 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001964 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001965 mutex_lock(&zonelists_mutex);
1966 build_all_zonelists(NULL, NULL);
1967 mutex_unlock(&zonelists_mutex);
1968 } else
1969 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001970
Lai Jiangshand9713672012-12-11 16:01:03 -08001971 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001972 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001973 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001974 kcompactd_stop(node);
1975 }
Minchan Kimbce73942009-06-16 15:32:50 -07001976
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001977 vm_total_pages = nr_free_pagecache_pages();
1978 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001979
1980 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001981 return 0;
1982
1983failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001984 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1985 (unsigned long long) start_pfn << PAGE_SHIFT,
1986 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001987 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001988 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001989 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001990 return ret;
1991}
Badari Pulavarty71088782008-10-18 20:25:58 -07001992
David Rientjes30467e02015-04-14 15:45:11 -07001993/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001994int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1995{
1996 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1997}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001998#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001999
Wen Congyangbbc76be2013-02-22 16:32:54 -08002000/**
2001 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2002 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07002003 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08002004 * @arg: argument passed to func
2005 * @func: callback for each memory section walked
2006 *
2007 * This function walks through all present mem sections in range
2008 * [start_pfn, end_pfn) and call func on each mem section.
2009 *
2010 * Returns the return value of func.
2011 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002012int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08002013 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07002014{
Wen Congyange90bdb72012-10-08 16:34:01 -07002015 struct memory_block *mem = NULL;
2016 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07002017 unsigned long pfn, section_nr;
2018 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07002019
Wen Congyange90bdb72012-10-08 16:34:01 -07002020 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2021 section_nr = pfn_to_section_nr(pfn);
2022 if (!present_section_nr(section_nr))
2023 continue;
2024
2025 section = __nr_to_section(section_nr);
2026 /* same memblock? */
2027 if (mem)
2028 if ((section_nr >= mem->start_section_nr) &&
2029 (section_nr <= mem->end_section_nr))
2030 continue;
2031
2032 mem = find_memory_block_hinted(section, mem);
2033 if (!mem)
2034 continue;
2035
Wen Congyangbbc76be2013-02-22 16:32:54 -08002036 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07002037 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08002038 kobject_put(&mem->dev.kobj);
2039 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07002040 }
2041 }
2042
2043 if (mem)
2044 kobject_put(&mem->dev.kobj);
2045
Wen Congyangbbc76be2013-02-22 16:32:54 -08002046 return 0;
2047}
2048
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002049#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08002050static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002051{
2052 int ret = !is_memblock_offlined(mem);
2053
Randy Dunlap349daa02013-04-29 15:08:49 -07002054 if (unlikely(ret)) {
2055 phys_addr_t beginpa, endpa;
2056
2057 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2058 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07002059 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07002060 &beginpa, &endpa);
2061 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08002062
2063 return ret;
2064}
2065
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002066static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08002067{
Tang Chen60a5a192013-02-22 16:33:14 -08002068 int cpu;
2069
2070 for_each_present_cpu(cpu) {
2071 if (cpu_to_node(cpu) == pgdat->node_id)
2072 /*
2073 * the cpu on this node isn't removed, and we can't
2074 * offline this node.
2075 */
2076 return -EBUSY;
2077 }
2078
2079 return 0;
2080}
2081
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002082static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002083{
2084#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002085 int cpu;
2086
2087 for_each_possible_cpu(cpu)
2088 if (cpu_to_node(cpu) == pgdat->node_id)
2089 numa_clear_node(cpu);
2090#endif
2091}
2092
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002093static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002094{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002095 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002096
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002097 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002098 if (ret)
2099 return ret;
2100
2101 /*
2102 * the node will be offlined when we come here, so we can clear
2103 * the cpu_to_node() now.
2104 */
2105
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002106 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002107 return 0;
2108}
2109
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002110/**
2111 * try_offline_node
2112 *
2113 * Offline a node if all memory sections and cpus of the node are removed.
2114 *
2115 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2116 * and online/offline operations before this call.
2117 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002118void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002119{
Wen Congyangd822b862013-02-22 16:33:16 -08002120 pg_data_t *pgdat = NODE_DATA(nid);
2121 unsigned long start_pfn = pgdat->node_start_pfn;
2122 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002123 unsigned long pfn;
2124
2125 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2126 unsigned long section_nr = pfn_to_section_nr(pfn);
2127
2128 if (!present_section_nr(section_nr))
2129 continue;
2130
2131 if (pfn_to_nid(pfn) != nid)
2132 continue;
2133
2134 /*
2135 * some memory sections of this node are not removed, and we
2136 * can't offline node now.
2137 */
2138 return;
2139 }
2140
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002141 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002142 return;
2143
2144 /*
2145 * all memory/cpu of this node are removed, we can offline this
2146 * node now.
2147 */
2148 node_set_offline(nid);
2149 unregister_one_node(nid);
2150}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002151EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002152
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002153/**
2154 * remove_memory
2155 *
2156 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2157 * and online/offline operations before this call, as required by
2158 * try_offline_node().
2159 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002160void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002161{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002162 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002163
Toshi Kani27356f52013-09-11 14:21:49 -07002164 BUG_ON(check_hotplug_memory_range(start, size));
2165
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002166 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002167
2168 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002169 * All memory blocks must be offlined before removing memory. Check
2170 * whether all memory blocks in question are offline and trigger a BUG()
2171 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002172 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002173 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002174 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002175 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002176 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002177
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002178 /* remove memmap entry */
2179 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002180 memblock_free(start, size);
2181 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002182
Wen Congyang24d335c2013-02-22 16:32:58 -08002183 arch_remove_memory(start, size);
2184
Tang Chen60a5a192013-02-22 16:33:14 -08002185 try_offline_node(nid);
2186
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002187 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002188}
Badari Pulavarty71088782008-10-18 20:25:58 -07002189EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002190#endif /* CONFIG_MEMORY_HOTREMOVE */