blob: c0147d3024eb756f8b2b916a668ce2802b00ee5b [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,
Michal Hocko1b862ae2017-07-06 15:37:45 -0700497 unsigned long phys_start_pfn, bool want_memblock)
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
Michal Hocko1b862ae2017-07-06 15:37:45 -0700514 if (!want_memblock)
515 return 0;
516
Gary Hadec04fc582009-01-06 14:39:14 -0800517 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700518}
519
David Rientjes4edd7ce2013-04-29 15:08:22 -0700520/*
521 * Reasonably generic function for adding memory. It is
522 * expected that archs that support memory hotplug will
523 * call this function after deciding the zone to which to
524 * add the new pages.
525 */
526int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
Michal Hocko1b862ae2017-07-06 15:37:45 -0700527 unsigned long nr_pages, bool want_memblock)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700528{
529 unsigned long i;
530 int err = 0;
531 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800532 struct vmem_altmap *altmap;
533
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700534 clear_zone_contiguous(zone);
535
David Rientjes4edd7ce2013-04-29 15:08:22 -0700536 /* during initialize mem_map, align hot-added range to section */
537 start_sec = pfn_to_section_nr(phys_start_pfn);
538 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
539
Dan Williams4b94ffd2016-01-15 16:56:22 -0800540 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
541 if (altmap) {
542 /*
543 * Validate altmap is within bounds of the total request
544 */
545 if (altmap->base_pfn != phys_start_pfn
546 || vmem_altmap_offset(altmap) > nr_pages) {
547 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700548 err = -EINVAL;
549 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800550 }
551 altmap->alloc = 0;
552 }
553
David Rientjes4edd7ce2013-04-29 15:08:22 -0700554 for (i = start_sec; i <= end_sec; i++) {
Michal Hocko1b862ae2017-07-06 15:37:45 -0700555 err = __add_section(nid, zone, section_nr_to_pfn(i), want_memblock);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700556
557 /*
558 * EEXIST is finally dealt with by ioresource collision
559 * check. see add_memory() => register_memory_resource()
560 * Warning will be printed if there is collision.
561 */
562 if (err && (err != -EEXIST))
563 break;
564 err = 0;
565 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700566 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700567out:
568 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700569 return err;
570}
571EXPORT_SYMBOL_GPL(__add_pages);
572
573#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800574/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
575static int find_smallest_section_pfn(int nid, struct zone *zone,
576 unsigned long start_pfn,
577 unsigned long end_pfn)
578{
579 struct mem_section *ms;
580
581 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
582 ms = __pfn_to_section(start_pfn);
583
584 if (unlikely(!valid_section(ms)))
585 continue;
586
587 if (unlikely(pfn_to_nid(start_pfn) != nid))
588 continue;
589
590 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
591 continue;
592
593 return start_pfn;
594 }
595
596 return 0;
597}
598
599/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
600static int find_biggest_section_pfn(int nid, struct zone *zone,
601 unsigned long start_pfn,
602 unsigned long end_pfn)
603{
604 struct mem_section *ms;
605 unsigned long pfn;
606
607 /* pfn is the end pfn of a memory section. */
608 pfn = end_pfn - 1;
609 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
610 ms = __pfn_to_section(pfn);
611
612 if (unlikely(!valid_section(ms)))
613 continue;
614
615 if (unlikely(pfn_to_nid(pfn) != nid))
616 continue;
617
618 if (zone && zone != page_zone(pfn_to_page(pfn)))
619 continue;
620
621 return pfn;
622 }
623
624 return 0;
625}
626
627static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
628 unsigned long end_pfn)
629{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700630 unsigned long zone_start_pfn = zone->zone_start_pfn;
631 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
632 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800633 unsigned long pfn;
634 struct mem_section *ms;
635 int nid = zone_to_nid(zone);
636
637 zone_span_writelock(zone);
638 if (zone_start_pfn == start_pfn) {
639 /*
640 * If the section is smallest section in the zone, it need
641 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
642 * In this case, we find second smallest valid mem_section
643 * for shrinking zone.
644 */
645 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
646 zone_end_pfn);
647 if (pfn) {
648 zone->zone_start_pfn = pfn;
649 zone->spanned_pages = zone_end_pfn - pfn;
650 }
651 } else if (zone_end_pfn == end_pfn) {
652 /*
653 * If the section is biggest section in the zone, it need
654 * shrink zone->spanned_pages.
655 * In this case, we find second biggest valid mem_section for
656 * shrinking zone.
657 */
658 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
659 start_pfn);
660 if (pfn)
661 zone->spanned_pages = pfn - zone_start_pfn + 1;
662 }
663
664 /*
665 * The section is not biggest or smallest mem_section in the zone, it
666 * only creates a hole in the zone. So in this case, we need not
667 * change the zone. But perhaps, the zone has only hole data. Thus
668 * it check the zone has only hole or not.
669 */
670 pfn = zone_start_pfn;
671 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
672 ms = __pfn_to_section(pfn);
673
674 if (unlikely(!valid_section(ms)))
675 continue;
676
677 if (page_zone(pfn_to_page(pfn)) != zone)
678 continue;
679
680 /* If the section is current section, it continues the loop */
681 if (start_pfn == pfn)
682 continue;
683
684 /* If we find valid section, we have nothing to do */
685 zone_span_writeunlock(zone);
686 return;
687 }
688
689 /* The zone has no valid section */
690 zone->zone_start_pfn = 0;
691 zone->spanned_pages = 0;
692 zone_span_writeunlock(zone);
693}
694
695static void shrink_pgdat_span(struct pglist_data *pgdat,
696 unsigned long start_pfn, unsigned long end_pfn)
697{
Xishi Qiu83285c72013-11-12 15:07:19 -0800698 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
699 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
700 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800701 unsigned long pfn;
702 struct mem_section *ms;
703 int nid = pgdat->node_id;
704
705 if (pgdat_start_pfn == start_pfn) {
706 /*
707 * If the section is smallest section in the pgdat, it need
708 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
709 * In this case, we find second smallest valid mem_section
710 * for shrinking zone.
711 */
712 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
713 pgdat_end_pfn);
714 if (pfn) {
715 pgdat->node_start_pfn = pfn;
716 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
717 }
718 } else if (pgdat_end_pfn == end_pfn) {
719 /*
720 * If the section is biggest section in the pgdat, it need
721 * shrink pgdat->node_spanned_pages.
722 * In this case, we find second biggest valid mem_section for
723 * shrinking zone.
724 */
725 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
726 start_pfn);
727 if (pfn)
728 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
729 }
730
731 /*
732 * If the section is not biggest or smallest mem_section in the pgdat,
733 * it only creates a hole in the pgdat. So in this case, we need not
734 * change the pgdat.
735 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
736 * has only hole or not.
737 */
738 pfn = pgdat_start_pfn;
739 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
740 ms = __pfn_to_section(pfn);
741
742 if (unlikely(!valid_section(ms)))
743 continue;
744
745 if (pfn_to_nid(pfn) != nid)
746 continue;
747
748 /* If the section is current section, it continues the loop */
749 if (start_pfn == pfn)
750 continue;
751
752 /* If we find valid section, we have nothing to do */
753 return;
754 }
755
756 /* The pgdat has no valid section */
757 pgdat->node_start_pfn = 0;
758 pgdat->node_spanned_pages = 0;
759}
760
761static void __remove_zone(struct zone *zone, unsigned long start_pfn)
762{
763 struct pglist_data *pgdat = zone->zone_pgdat;
764 int nr_pages = PAGES_PER_SECTION;
765 int zone_type;
766 unsigned long flags;
767
768 zone_type = zone - pgdat->node_zones;
769
770 pgdat_resize_lock(zone->zone_pgdat, &flags);
771 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
772 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
773 pgdat_resize_unlock(zone->zone_pgdat, &flags);
774}
775
Dan Williams4b94ffd2016-01-15 16:56:22 -0800776static int __remove_section(struct zone *zone, struct mem_section *ms,
777 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700778{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800779 unsigned long start_pfn;
780 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700781 int ret = -EINVAL;
782
783 if (!valid_section(ms))
784 return ret;
785
786 ret = unregister_memory_section(ms);
787 if (ret)
788 return ret;
789
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800790 scn_nr = __section_nr(ms);
791 start_pfn = section_nr_to_pfn(scn_nr);
792 __remove_zone(zone, start_pfn);
793
Dan Williams4b94ffd2016-01-15 16:56:22 -0800794 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700795 return 0;
796}
797
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700798/**
799 * __remove_pages() - remove sections of pages from a zone
800 * @zone: zone from which pages need to be removed
801 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
802 * @nr_pages: number of pages to remove (must be multiple of section size)
803 *
804 * Generic helper function to remove section mappings and sysfs entries
805 * for the section of the memory we are removing. Caller needs to make
806 * sure that pages are marked reserved and zones are adjust properly by
807 * calling offline_pages().
808 */
809int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
810 unsigned long nr_pages)
811{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700812 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800813 unsigned long map_offset = 0;
814 int sections_to_remove, ret = 0;
815
816 /* In the ZONE_DEVICE case device driver owns the memory region */
817 if (is_dev_zone(zone)) {
818 struct page *page = pfn_to_page(phys_start_pfn);
819 struct vmem_altmap *altmap;
820
821 altmap = to_vmem_altmap((unsigned long) page);
822 if (altmap)
823 map_offset = vmem_altmap_offset(altmap);
824 } else {
825 resource_size_t start, size;
826
827 start = phys_start_pfn << PAGE_SHIFT;
828 size = nr_pages * PAGE_SIZE;
829
830 ret = release_mem_region_adjustable(&iomem_resource, start,
831 size);
832 if (ret) {
833 resource_size_t endres = start + size - 1;
834
835 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
836 &start, &endres, ret);
837 }
838 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700839
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700840 clear_zone_contiguous(zone);
841
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700842 /*
843 * We can only remove entire sections
844 */
845 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
846 BUG_ON(nr_pages % PAGES_PER_SECTION);
847
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700848 sections_to_remove = nr_pages / PAGES_PER_SECTION;
849 for (i = 0; i < sections_to_remove; i++) {
850 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800851
852 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
853 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700854 if (ret)
855 break;
856 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700857
858 set_zone_contiguous(zone);
859
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700860 return ret;
861}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700862#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700863
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700864int set_online_page_callback(online_page_callback_t callback)
865{
866 int rc = -EINVAL;
867
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700868 get_online_mems();
869 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700870
871 if (online_page_callback == generic_online_page) {
872 online_page_callback = callback;
873 rc = 0;
874 }
875
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700876 mutex_unlock(&online_page_callback_lock);
877 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700878
879 return rc;
880}
881EXPORT_SYMBOL_GPL(set_online_page_callback);
882
883int restore_online_page_callback(online_page_callback_t callback)
884{
885 int rc = -EINVAL;
886
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700887 get_online_mems();
888 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700889
890 if (online_page_callback == callback) {
891 online_page_callback = generic_online_page;
892 rc = 0;
893 }
894
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700895 mutex_unlock(&online_page_callback_lock);
896 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700897
898 return rc;
899}
900EXPORT_SYMBOL_GPL(restore_online_page_callback);
901
902void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700903{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700904}
905EXPORT_SYMBOL_GPL(__online_page_set_limits);
906
907void __online_page_increment_counters(struct page *page)
908{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700909 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700910}
911EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700912
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700913void __online_page_free(struct page *page)
914{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700915 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700916}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700917EXPORT_SYMBOL_GPL(__online_page_free);
918
919static void generic_online_page(struct page *page)
920{
921 __online_page_set_limits(page);
922 __online_page_increment_counters(page);
923 __online_page_free(page);
924}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700925
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700926static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
927 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700928{
929 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700930 unsigned long onlined_pages = *(unsigned long *)arg;
931 struct page *page;
932 if (PageReserved(pfn_to_page(start_pfn)))
933 for (i = 0; i < nr_pages; i++) {
934 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700935 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700936 onlined_pages++;
937 }
938 *(unsigned long *)arg = onlined_pages;
939 return 0;
940}
941
Lai Jiangshan09285af2012-12-12 13:52:04 -0800942#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800943/*
944 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
945 * normal memory.
946 */
Michal Hockoc8f95652017-07-06 15:37:38 -0700947static bool can_online_high_movable(int nid)
Lai Jiangshan09285af2012-12-12 13:52:04 -0800948{
949 return true;
950}
Tang Chen79a4dce2012-12-18 14:23:24 -0800951#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800952/* ensure every online node has NORMAL memory */
Michal Hockoc8f95652017-07-06 15:37:38 -0700953static bool can_online_high_movable(int nid)
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800954{
Michal Hockoc8f95652017-07-06 15:37:38 -0700955 return node_state(nid, N_NORMAL_MEMORY);
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800956}
Tang Chen79a4dce2012-12-18 14:23:24 -0800957#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800958
Lai Jiangshand9713672012-12-11 16:01:03 -0800959/* check which state of node_states will be changed when online memory */
960static void node_states_check_changes_online(unsigned long nr_pages,
961 struct zone *zone, struct memory_notify *arg)
962{
963 int nid = zone_to_nid(zone);
964 enum zone_type zone_last = ZONE_NORMAL;
965
966 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800967 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
968 * contains nodes which have zones of 0...ZONE_NORMAL,
969 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800970 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800971 * If we don't have HIGHMEM nor movable node,
972 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
973 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800974 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800975 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800976 zone_last = ZONE_MOVABLE;
977
978 /*
979 * if the memory to be online is in a zone of 0...zone_last, and
980 * the zones of 0...zone_last don't have memory before online, we will
981 * need to set the node to node_states[N_NORMAL_MEMORY] after
982 * the memory is online.
983 */
984 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
985 arg->status_change_nid_normal = nid;
986 else
987 arg->status_change_nid_normal = -1;
988
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800989#ifdef CONFIG_HIGHMEM
990 /*
991 * If we have movable node, node_states[N_HIGH_MEMORY]
992 * contains nodes which have zones of 0...ZONE_HIGHMEM,
993 * set zone_last to ZONE_HIGHMEM.
994 *
995 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
996 * contains nodes which have zones of 0...ZONE_MOVABLE,
997 * set zone_last to ZONE_MOVABLE.
998 */
999 zone_last = ZONE_HIGHMEM;
1000 if (N_MEMORY == N_HIGH_MEMORY)
1001 zone_last = ZONE_MOVABLE;
1002
1003 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1004 arg->status_change_nid_high = nid;
1005 else
1006 arg->status_change_nid_high = -1;
1007#else
1008 arg->status_change_nid_high = arg->status_change_nid_normal;
1009#endif
1010
Lai Jiangshand9713672012-12-11 16:01:03 -08001011 /*
1012 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001013 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001014 * is online.
1015 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001016 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001017 arg->status_change_nid = nid;
1018 else
1019 arg->status_change_nid = -1;
1020}
1021
1022static void node_states_set_node(int node, struct memory_notify *arg)
1023{
1024 if (arg->status_change_nid_normal >= 0)
1025 node_set_state(node, N_NORMAL_MEMORY);
1026
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001027 if (arg->status_change_nid_high >= 0)
1028 node_set_state(node, N_HIGH_MEMORY);
1029
1030 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001031}
1032
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001033bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1034 enum zone_type target, int *zone_shift)
Reza Arbabdf429ac2016-07-26 15:22:23 -07001035{
1036 struct zone *zone = page_zone(pfn_to_page(pfn));
1037 enum zone_type idx = zone_idx(zone);
1038 int i;
1039
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001040 *zone_shift = 0;
1041
Reza Arbabdf429ac2016-07-26 15:22:23 -07001042 if (idx < target) {
1043 /* pages must be at end of current zone */
1044 if (pfn + nr_pages != zone_end_pfn(zone))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001045 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001046
1047 /* no zones in use between current zone and target */
1048 for (i = idx + 1; i < target; i++)
1049 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001050 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001051 }
1052
1053 if (target < idx) {
1054 /* pages must be at beginning of current zone */
1055 if (pfn != zone->zone_start_pfn)
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001056 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001057
1058 /* no zones in use between current zone and target */
1059 for (i = target + 1; i < idx; i++)
1060 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001061 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001062 }
1063
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001064 *zone_shift = target - idx;
1065 return true;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001066}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001067
David Rientjes30467e02015-04-14 15:45:11 -07001068/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001069int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001070{
Cody P Schaferaa472282013-07-03 15:02:10 -07001071 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001072 unsigned long onlined_pages = 0;
1073 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001074 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001075 int nid;
1076 int ret;
1077 struct memory_notify arg;
Reza Arbabe51e6c82016-07-26 15:22:20 -07001078 int zone_shift = 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001079
Lai Jiangshand9713672012-12-11 16:01:03 -08001080 /*
1081 * This doesn't need a lock to do pfn_to_page().
1082 * The section can't be removed here because of the
1083 * memory_block->state_mutex.
1084 */
1085 zone = page_zone(pfn_to_page(pfn));
1086
Tang Chen4f7c6b42014-08-06 16:05:13 -07001087 if ((zone_idx(zone) > ZONE_NORMAL ||
1088 online_type == MMOP_ONLINE_MOVABLE) &&
Michal Hockoc8f95652017-07-06 15:37:38 -07001089 !can_online_high_movable(pfn_to_nid(pfn)))
David Rientjes30467e02015-04-14 15:45:11 -07001090 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001091
Yasuaki Ishimatsu8a1f7802017-01-24 15:17:45 -08001092 if (online_type == MMOP_ONLINE_KERNEL) {
1093 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1094 return -EINVAL;
1095 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1096 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1097 return -EINVAL;
1098 }
Reza Arbabe51e6c82016-07-26 15:22:20 -07001099
1100 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1101 if (!zone)
1102 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001103
Yasunori Goto7b78d332007-10-21 16:41:36 -07001104 arg.start_pfn = pfn;
1105 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001106 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001107
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001108 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001109
1110 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1111 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001112 if (ret)
1113 goto failed_addition;
1114
Dave Hansen3947be12005-10-29 18:16:54 -07001115 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001116 * If this zone is not populated, then it is not in zonelist.
1117 * This means the page allocator ignores this zone.
1118 * So, zonelist must be updated after online.
1119 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001120 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001121 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001122 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001123 build_all_zonelists(NULL, zone);
1124 }
Yasunori Goto68113782006-06-23 02:03:11 -07001125
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001126 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001127 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001128 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001129 if (need_zonelists_rebuild)
1130 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001131 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001132 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001133 }
1134
Dave Hansen3947be12005-10-29 18:16:54 -07001135 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001136
1137 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001138 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001139 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1140
Jiang Liu08dff7b2012-07-31 16:43:30 -07001141 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001142 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001143 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001144 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001145 else
1146 zone_pcp_update(zone);
1147 }
Dave Hansen3947be12005-10-29 18:16:54 -07001148
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001149 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001150
1151 init_per_zone_wmark_min();
1152
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001153 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001154 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001155 kcompactd_run(nid);
1156 }
Dave Hansen61b13992005-10-29 18:16:56 -07001157
Haicheng Li1f522502010-05-24 14:32:51 -07001158 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001159
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001160 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001161
1162 if (onlined_pages)
1163 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001164 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001165
1166failed_addition:
1167 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1168 (unsigned long long) pfn << PAGE_SHIFT,
1169 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1170 memory_notify(MEM_CANCEL_ONLINE, &arg);
1171 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001172}
Keith Mannthey53947022006-09-30 23:27:08 -07001173#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001174
Tang Chen0bd85422014-11-13 15:19:41 -08001175static void reset_node_present_pages(pg_data_t *pgdat)
1176{
1177 struct zone *z;
1178
1179 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1180 z->present_pages = 0;
1181
1182 pgdat->node_present_pages = 0;
1183}
1184
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001185/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1186static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001187{
1188 struct pglist_data *pgdat;
1189 unsigned long zones_size[MAX_NR_ZONES] = {0};
1190 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001191 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001192
Tang Chena1e565a2013-02-22 16:33:18 -08001193 pgdat = NODE_DATA(nid);
1194 if (!pgdat) {
1195 pgdat = arch_alloc_nodedata(nid);
1196 if (!pgdat)
1197 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001198
Tang Chena1e565a2013-02-22 16:33:18 -08001199 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001200 } else {
Mel Gormane716f2e2017-05-03 14:53:45 -07001201 /*
1202 * Reset the nr_zones, order and classzone_idx before reuse.
1203 * Note that kswapd will init kswapd_classzone_idx properly
1204 * when it starts in the near future.
1205 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001206 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001207 pgdat->kswapd_order = 0;
1208 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001209 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001210
1211 /* we can use NODE_DATA(nid) from here */
1212
1213 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001214 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001215 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001216
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001217 /*
1218 * The node we allocated has no zone fallback lists. For avoiding
1219 * to access not-initialized zonelist, build here.
1220 */
David Rientjesf957db42011-06-22 18:13:04 -07001221 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001222 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001223 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001224
Tang Chenf784a3f2014-11-13 15:19:39 -08001225 /*
1226 * zone->managed_pages is set to an approximate value in
1227 * free_area_init_core(), which will cause
1228 * /sys/device/system/node/nodeX/meminfo has wrong data.
1229 * So reset it to 0 before any memory is onlined.
1230 */
1231 reset_node_managed_pages(pgdat);
1232
Tang Chen0bd85422014-11-13 15:19:41 -08001233 /*
1234 * When memory is hot-added, all the memory is in offline state. So
1235 * clear all zones' present_pages because they will be updated in
1236 * online_pages() and offline_pages().
1237 */
1238 reset_node_present_pages(pgdat);
1239
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001240 return pgdat;
1241}
1242
1243static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1244{
1245 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001246 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001247 arch_free_nodedata(pgdat);
1248 return;
1249}
1250
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001251
Toshi Kani01b0f192013-11-12 15:07:25 -08001252/**
1253 * try_online_node - online a node if offlined
1254 *
minskey guocf234222010-05-24 14:32:41 -07001255 * called by cpu_up() to online a node without onlined memory.
1256 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001257int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001258{
1259 pg_data_t *pgdat;
1260 int ret;
1261
Toshi Kani01b0f192013-11-12 15:07:25 -08001262 if (node_online(nid))
1263 return 0;
1264
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001265 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001266 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001267 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001268 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001269 ret = -ENOMEM;
1270 goto out;
1271 }
1272 node_set_online(nid);
1273 ret = register_one_node(nid);
1274 BUG_ON(ret);
1275
Toshi Kani01b0f192013-11-12 15:07:25 -08001276 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1277 mutex_lock(&zonelists_mutex);
1278 build_all_zonelists(NULL, NULL);
1279 mutex_unlock(&zonelists_mutex);
1280 }
1281
minskey guocf234222010-05-24 14:32:41 -07001282out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001283 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001284 return ret;
1285}
1286
Toshi Kani27356f52013-09-11 14:21:49 -07001287static int check_hotplug_memory_range(u64 start, u64 size)
1288{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001289 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001290 u64 nr_pages = size >> PAGE_SHIFT;
1291
1292 /* Memory range must be aligned with section */
1293 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1294 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1295 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1296 (unsigned long long)start,
1297 (unsigned long long)size);
1298 return -EINVAL;
1299 }
1300
1301 return 0;
1302}
1303
Wang Nan63264402014-08-06 16:07:36 -07001304/*
1305 * If movable zone has already been setup, newly added memory should be check.
1306 * If its address is higher than movable zone, it should be added as movable.
1307 * Without this check, movable zone may overlap with other zone.
1308 */
1309static int should_add_memory_movable(int nid, u64 start, u64 size)
1310{
1311 unsigned long start_pfn = start >> PAGE_SHIFT;
1312 pg_data_t *pgdat = NODE_DATA(nid);
1313 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1314
1315 if (zone_is_empty(movable_zone))
1316 return 0;
1317
1318 if (movable_zone->zone_start_pfn <= start_pfn)
1319 return 1;
1320
1321 return 0;
1322}
1323
Dan Williams033fbae2015-08-09 15:29:06 -04001324int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1325 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001326{
Dan Williams033fbae2015-08-09 15:29:06 -04001327#ifdef CONFIG_ZONE_DEVICE
1328 if (for_device)
1329 return ZONE_DEVICE;
1330#endif
Wang Nan63264402014-08-06 16:07:36 -07001331 if (should_add_memory_movable(nid, start, size))
1332 return ZONE_MOVABLE;
1333
1334 return zone_default;
1335}
1336
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001337static int online_memory_block(struct memory_block *mem, void *arg)
1338{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001339 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001340}
1341
Al Viro31168482008-11-22 17:33:24 +00001342/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001343int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001344{
David Vrabel62cedb92015-06-25 16:35:49 +01001345 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001346 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001347 bool new_pgdat;
1348 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001349 int ret;
1350
David Vrabel62cedb92015-06-25 16:35:49 +01001351 start = res->start;
1352 size = resource_size(res);
1353
Toshi Kani27356f52013-09-11 14:21:49 -07001354 ret = check_hotplug_memory_range(start, size);
1355 if (ret)
1356 return ret;
1357
Tang Chena1e565a2013-02-22 16:33:18 -08001358 { /* Stupid hack to suppress address-never-null warning */
1359 void *p = NODE_DATA(nid);
1360 new_pgdat = !p;
1361 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001362
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001363 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001364
Tang Chen7f36e3e2015-09-04 15:42:32 -07001365 /*
1366 * Add new range to memblock so that when hotadd_new_pgdat() is called
1367 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1368 * this new range and calculate total pages correctly. The range will
1369 * be removed at hot-remove time.
1370 */
1371 memblock_add_node(start, size, nid);
1372
Tang Chena1e565a2013-02-22 16:33:18 -08001373 new_node = !node_online(nid);
1374 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001375 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001376 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001377 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001378 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001379 }
1380
Yasunori Gotobc02af92006-06-27 02:53:30 -07001381 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001382 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001383
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001384 if (ret < 0)
1385 goto error;
1386
Yasunori Goto0fc44152006-06-27 02:53:38 -07001387 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001388 node_set_online(nid);
1389
Tang Chena1e565a2013-02-22 16:33:18 -08001390 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001391 ret = register_one_node(nid);
1392 /*
1393 * If sysfs file of new node can't create, cpu on the node
1394 * can't be hot-added. There is no rollback way now.
1395 * So, check by BUG_ON() to catch it reluctantly..
1396 */
1397 BUG_ON(ret);
1398 }
1399
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001400 /* create new memmap entry */
1401 firmware_map_add_hotplug(start, start + size, "System RAM");
1402
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001403 /* online pages if requested */
1404 if (online)
1405 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1406 NULL, online_memory_block);
1407
Andi Kleen6ad696d2009-11-17 14:06:22 -08001408 goto out;
1409
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001410error:
1411 /* rollback pgdat allocation and others */
1412 if (new_pgdat)
1413 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001414 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001415
Andi Kleen6ad696d2009-11-17 14:06:22 -08001416out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001417 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001418 return ret;
1419}
David Vrabel62cedb92015-06-25 16:35:49 +01001420EXPORT_SYMBOL_GPL(add_memory_resource);
1421
1422int __ref add_memory(int nid, u64 start, u64 size)
1423{
1424 struct resource *res;
1425 int ret;
1426
1427 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001428 if (IS_ERR(res))
1429 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001430
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001431 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001432 if (ret < 0)
1433 release_memory_resource(res);
1434 return ret;
1435}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001436EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001437
1438#ifdef CONFIG_MEMORY_HOTREMOVE
1439/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001440 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1441 * set and the size of the free page is given by page_order(). Using this,
1442 * the function determines if the pageblock contains only free pages.
1443 * Due to buddy contraints, a free page at least the size of a pageblock will
1444 * be located at the start of the pageblock
1445 */
1446static inline int pageblock_free(struct page *page)
1447{
1448 return PageBuddy(page) && page_order(page) >= pageblock_order;
1449}
1450
1451/* Return the start of the next active pageblock after a given page */
1452static struct page *next_active_pageblock(struct page *page)
1453{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001454 /* Ensure the starting page is pageblock-aligned */
1455 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1456
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001457 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001458 if (pageblock_free(page)) {
1459 int order;
1460 /* be careful. we don't have locks, page_order can be changed.*/
1461 order = page_order(page);
1462 if ((order < MAX_ORDER) && (order >= pageblock_order))
1463 return page + (1 << order);
1464 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001465
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001466 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001467}
1468
1469/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001470bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001471{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001472 struct page *page = pfn_to_page(start_pfn);
1473 struct page *end_page = page + nr_pages;
1474
1475 /* Check the starting page of each pageblock within the range */
1476 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001477 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001478 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001479 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001480 }
1481
1482 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001483 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001484}
1485
1486/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001487 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001488 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001489 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001490int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1491 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001492{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001493 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001494 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001495 struct zone *zone = NULL;
1496 struct page *page;
1497 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001498 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001499 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001500 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001501 /* Make sure the memory section is present first */
1502 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001503 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001504 for (; pfn < sec_end_pfn && pfn < end_pfn;
1505 pfn += MAX_ORDER_NR_PAGES) {
1506 i = 0;
1507 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1508 while ((i < MAX_ORDER_NR_PAGES) &&
1509 !pfn_valid_within(pfn + i))
1510 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001511 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001512 continue;
1513 page = pfn_to_page(pfn + i);
1514 if (zone && page_zone(page) != zone)
1515 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001516 if (!zone)
1517 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001518 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001519 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001520 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001521 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001522
Toshi Kania96dfdd2017-02-03 13:13:23 -08001523 if (zone) {
1524 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001525 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001526 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001527 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001528 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001529 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001530}
1531
1532/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001533 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1534 * non-lru movable pages and hugepages). We scan pfn because it's much
1535 * easier than scanning over linked list. This function returns the pfn
1536 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001537 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001538static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001539{
1540 unsigned long pfn;
1541 struct page *page;
1542 for (pfn = start; pfn < end; pfn++) {
1543 if (pfn_valid(pfn)) {
1544 page = pfn_to_page(pfn);
1545 if (PageLRU(page))
1546 return pfn;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001547 if (__PageMovable(page))
1548 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001549 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001550 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001551 return pfn;
1552 else
1553 pfn = round_up(pfn + 1,
1554 1 << compound_order(page)) - 1;
1555 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001556 }
1557 }
1558 return 0;
1559}
1560
Xishi Qiu394e31d2016-07-28 15:48:53 -07001561static struct page *new_node_page(struct page *page, unsigned long private,
1562 int **result)
1563{
1564 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1565 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001566 nodemask_t nmask = node_states[N_MEMORY];
1567 struct page *new_page = NULL;
Xishi Qiu394e31d2016-07-28 15:48:53 -07001568
1569 /*
1570 * TODO: allocate a destination hugepage from a nearest neighbor node,
1571 * accordance with memory policy of the user process if possible. For
1572 * now as a simple work-around, we use the next node for destination.
1573 */
1574 if (PageHuge(page))
1575 return alloc_huge_page_node(page_hstate(compound_head(page)),
1576 next_node_in(nid, nmask));
1577
Li Zhong231e97e2016-09-28 15:22:38 -07001578 node_clear(nid, nmask);
Li Zhong9bb627b2016-09-19 14:43:52 -07001579
Xishi Qiu394e31d2016-07-28 15:48:53 -07001580 if (PageHighMem(page)
1581 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1582 gfp_mask |= __GFP_HIGHMEM;
1583
Li Zhong231e97e2016-09-28 15:22:38 -07001584 if (!nodes_empty(nmask))
1585 new_page = __alloc_pages_nodemask(gfp_mask, 0,
Xishi Qiu394e31d2016-07-28 15:48:53 -07001586 node_zonelist(nid, gfp_mask), &nmask);
1587 if (!new_page)
1588 new_page = __alloc_pages(gfp_mask, 0,
1589 node_zonelist(nid, gfp_mask));
1590
1591 return new_page;
1592}
1593
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001594#define NR_OFFLINE_AT_ONCE_PAGES (256)
1595static int
1596do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1597{
1598 unsigned long pfn;
1599 struct page *page;
1600 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1601 int not_managed = 0;
1602 int ret = 0;
1603 LIST_HEAD(source);
1604
1605 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1606 if (!pfn_valid(pfn))
1607 continue;
1608 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001609
1610 if (PageHuge(page)) {
1611 struct page *head = compound_head(page);
1612 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1613 if (compound_order(head) > PFN_SECTION_SHIFT) {
1614 ret = -EBUSY;
1615 break;
1616 }
1617 if (isolate_huge_page(page, &source))
1618 move_pages -= 1 << compound_order(head);
1619 continue;
1620 }
1621
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001622 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001623 continue;
1624 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001625 * We can skip free pages. And we can deal with pages on
1626 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001627 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001628 if (PageLRU(page))
1629 ret = isolate_lru_page(page);
1630 else
1631 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001632 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001633 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001634 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001635 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001636 if (!__PageMovable(page))
1637 inc_node_page_state(page, NR_ISOLATED_ANON +
1638 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001639
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001640 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001641#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001642 pr_alert("failed to isolate pfn %lx\n", pfn);
1643 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001644#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001645 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001646 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001647 check this again here. */
1648 if (page_count(page)) {
1649 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001650 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001651 break;
1652 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001653 }
1654 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001655 if (!list_empty(&source)) {
1656 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001657 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001658 goto out;
1659 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001660
Xishi Qiu394e31d2016-07-28 15:48:53 -07001661 /* Allocate a new page from the nearest neighbor node */
1662 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001663 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001664 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001665 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001666 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001667out:
1668 return ret;
1669}
1670
1671/*
1672 * remove from free_area[] and mark all as Reserved.
1673 */
1674static int
1675offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1676 void *data)
1677{
1678 __offline_isolated_pages(start, start + nr_pages);
1679 return 0;
1680}
1681
1682static void
1683offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1684{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001685 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001686 offline_isolated_pages_cb);
1687}
1688
1689/*
1690 * Check all pages in range, recoreded as memory resource, are isolated.
1691 */
1692static int
1693check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1694 void *data)
1695{
1696 int ret;
1697 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001698 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001699 offlined = nr_pages;
1700 if (!ret)
1701 *(long *)data += offlined;
1702 return ret;
1703}
1704
1705static long
1706check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1707{
1708 long offlined = 0;
1709 int ret;
1710
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001711 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001712 check_pages_isolated_cb);
1713 if (ret < 0)
1714 offlined = (long)ret;
1715 return offlined;
1716}
1717
Lai Jiangshan09285af2012-12-12 13:52:04 -08001718#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001719/*
1720 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1721 * normal memory.
1722 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001723static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1724{
1725 return true;
1726}
Tang Chen79a4dce2012-12-18 14:23:24 -08001727#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001728/* ensure the node has NORMAL memory if it is still online */
1729static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1730{
1731 struct pglist_data *pgdat = zone->zone_pgdat;
1732 unsigned long present_pages = 0;
1733 enum zone_type zt;
1734
1735 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1736 present_pages += pgdat->node_zones[zt].present_pages;
1737
1738 if (present_pages > nr_pages)
1739 return true;
1740
1741 present_pages = 0;
1742 for (; zt <= ZONE_MOVABLE; zt++)
1743 present_pages += pgdat->node_zones[zt].present_pages;
1744
1745 /*
1746 * we can't offline the last normal memory until all
1747 * higher memory is offlined.
1748 */
1749 return present_pages == 0;
1750}
Tang Chen79a4dce2012-12-18 14:23:24 -08001751#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001752
Tang Chenc5320922013-11-12 15:08:10 -08001753static int __init cmdline_parse_movable_node(char *p)
1754{
1755#ifdef CONFIG_MOVABLE_NODE
Tang Chen55ac5902014-01-21 15:49:35 -08001756 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001757#else
1758 pr_warn("movable_node option not supported\n");
1759#endif
1760 return 0;
1761}
1762early_param("movable_node", cmdline_parse_movable_node);
1763
Lai Jiangshand9713672012-12-11 16:01:03 -08001764/* check which state of node_states will be changed when offline memory */
1765static void node_states_check_changes_offline(unsigned long nr_pages,
1766 struct zone *zone, struct memory_notify *arg)
1767{
1768 struct pglist_data *pgdat = zone->zone_pgdat;
1769 unsigned long present_pages = 0;
1770 enum zone_type zt, zone_last = ZONE_NORMAL;
1771
1772 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001773 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1774 * contains nodes which have zones of 0...ZONE_NORMAL,
1775 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001776 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001777 * If we don't have HIGHMEM nor movable node,
1778 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1779 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001780 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001781 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001782 zone_last = ZONE_MOVABLE;
1783
1784 /*
1785 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1786 * If the memory to be offline is in a zone of 0...zone_last,
1787 * and it is the last present memory, 0...zone_last will
1788 * become empty after offline , thus we can determind we will
1789 * need to clear the node from node_states[N_NORMAL_MEMORY].
1790 */
1791 for (zt = 0; zt <= zone_last; zt++)
1792 present_pages += pgdat->node_zones[zt].present_pages;
1793 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1794 arg->status_change_nid_normal = zone_to_nid(zone);
1795 else
1796 arg->status_change_nid_normal = -1;
1797
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001798#ifdef CONFIG_HIGHMEM
1799 /*
1800 * If we have movable node, node_states[N_HIGH_MEMORY]
1801 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1802 * set zone_last to ZONE_HIGHMEM.
1803 *
1804 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1805 * contains nodes which have zones of 0...ZONE_MOVABLE,
1806 * set zone_last to ZONE_MOVABLE.
1807 */
1808 zone_last = ZONE_HIGHMEM;
1809 if (N_MEMORY == N_HIGH_MEMORY)
1810 zone_last = ZONE_MOVABLE;
1811
1812 for (; zt <= zone_last; zt++)
1813 present_pages += pgdat->node_zones[zt].present_pages;
1814 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1815 arg->status_change_nid_high = zone_to_nid(zone);
1816 else
1817 arg->status_change_nid_high = -1;
1818#else
1819 arg->status_change_nid_high = arg->status_change_nid_normal;
1820#endif
1821
Lai Jiangshand9713672012-12-11 16:01:03 -08001822 /*
1823 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1824 */
1825 zone_last = ZONE_MOVABLE;
1826
1827 /*
1828 * check whether node_states[N_HIGH_MEMORY] will be changed
1829 * If we try to offline the last present @nr_pages from the node,
1830 * we can determind we will need to clear the node from
1831 * node_states[N_HIGH_MEMORY].
1832 */
1833 for (; zt <= zone_last; zt++)
1834 present_pages += pgdat->node_zones[zt].present_pages;
1835 if (nr_pages >= present_pages)
1836 arg->status_change_nid = zone_to_nid(zone);
1837 else
1838 arg->status_change_nid = -1;
1839}
1840
1841static void node_states_clear_node(int node, struct memory_notify *arg)
1842{
1843 if (arg->status_change_nid_normal >= 0)
1844 node_clear_state(node, N_NORMAL_MEMORY);
1845
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001846 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1847 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001848 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001849
1850 if ((N_MEMORY != N_HIGH_MEMORY) &&
1851 (arg->status_change_nid >= 0))
1852 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001853}
1854
Wen Congyanga16cee12012-10-08 16:33:58 -07001855static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001856 unsigned long end_pfn, unsigned long timeout)
1857{
1858 unsigned long pfn, nr_pages, expire;
1859 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001860 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001861 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001862 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001863 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001864 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001865
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001866 /* at least, alignment against pageblock is necessary */
1867 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1868 return -EINVAL;
1869 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1870 return -EINVAL;
1871 /* This makes hotplug much easier...and readable.
1872 we assume this for now. .*/
Toshi Kania96dfdd2017-02-03 13:13:23 -08001873 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001874 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001875
Toshi Kania96dfdd2017-02-03 13:13:23 -08001876 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001877 node = zone_to_nid(zone);
1878 nr_pages = end_pfn - start_pfn;
1879
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001880 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001881 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001882
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001883 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001884 ret = start_isolate_page_range(start_pfn, end_pfn,
1885 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001886 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001887 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001888
1889 arg.start_pfn = start_pfn;
1890 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001891 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001892
1893 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1894 ret = notifier_to_errno(ret);
1895 if (ret)
1896 goto failed_removal;
1897
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001898 pfn = start_pfn;
1899 expire = jiffies + timeout;
1900 drain = 0;
1901 retry_max = 5;
1902repeat:
1903 /* start memory hot removal */
1904 ret = -EAGAIN;
1905 if (time_after(jiffies, expire))
1906 goto failed_removal;
1907 ret = -EINTR;
1908 if (signal_pending(current))
1909 goto failed_removal;
1910 ret = 0;
1911 if (drain) {
1912 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001913 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001914 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001915 }
1916
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001917 pfn = scan_movable_pages(start_pfn, end_pfn);
1918 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001919 ret = do_migrate_range(pfn, end_pfn);
1920 if (!ret) {
1921 drain = 1;
1922 goto repeat;
1923 } else {
1924 if (ret < 0)
1925 if (--retry_max == 0)
1926 goto failed_removal;
1927 yield();
1928 drain = 1;
1929 goto repeat;
1930 }
1931 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001932 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001933 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001934 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001935 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001936 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001937 /*
1938 * dissolve free hugepages in the memory block before doing offlining
1939 * actually in order to make hugetlbfs's object counting consistent.
1940 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001941 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1942 if (ret)
1943 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001944 /* check again */
1945 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1946 if (offlined_pages < 0) {
1947 ret = -EBUSY;
1948 goto failed_removal;
1949 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001950 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001951 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001952 We cannot do rollback at this point. */
1953 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001954 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001955 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001956 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001957 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001958 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001959
1960 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001961 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001962 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001963
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001964 init_per_zone_wmark_min();
1965
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001966 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001967 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001968 mutex_lock(&zonelists_mutex);
1969 build_all_zonelists(NULL, NULL);
1970 mutex_unlock(&zonelists_mutex);
1971 } else
1972 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001973
Lai Jiangshand9713672012-12-11 16:01:03 -08001974 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001975 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001976 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001977 kcompactd_stop(node);
1978 }
Minchan Kimbce73942009-06-16 15:32:50 -07001979
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001980 vm_total_pages = nr_free_pagecache_pages();
1981 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001982
1983 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001984 return 0;
1985
1986failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001987 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1988 (unsigned long long) start_pfn << PAGE_SHIFT,
1989 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001990 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001991 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001992 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001993 return ret;
1994}
Badari Pulavarty71088782008-10-18 20:25:58 -07001995
David Rientjes30467e02015-04-14 15:45:11 -07001996/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001997int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1998{
1999 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
2000}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002001#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07002002
Wen Congyangbbc76be2013-02-22 16:32:54 -08002003/**
2004 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2005 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07002006 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08002007 * @arg: argument passed to func
2008 * @func: callback for each memory section walked
2009 *
2010 * This function walks through all present mem sections in range
2011 * [start_pfn, end_pfn) and call func on each mem section.
2012 *
2013 * Returns the return value of func.
2014 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002015int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08002016 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07002017{
Wen Congyange90bdb72012-10-08 16:34:01 -07002018 struct memory_block *mem = NULL;
2019 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07002020 unsigned long pfn, section_nr;
2021 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07002022
Wen Congyange90bdb72012-10-08 16:34:01 -07002023 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2024 section_nr = pfn_to_section_nr(pfn);
2025 if (!present_section_nr(section_nr))
2026 continue;
2027
2028 section = __nr_to_section(section_nr);
2029 /* same memblock? */
2030 if (mem)
2031 if ((section_nr >= mem->start_section_nr) &&
2032 (section_nr <= mem->end_section_nr))
2033 continue;
2034
2035 mem = find_memory_block_hinted(section, mem);
2036 if (!mem)
2037 continue;
2038
Wen Congyangbbc76be2013-02-22 16:32:54 -08002039 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07002040 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08002041 kobject_put(&mem->dev.kobj);
2042 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07002043 }
2044 }
2045
2046 if (mem)
2047 kobject_put(&mem->dev.kobj);
2048
Wen Congyangbbc76be2013-02-22 16:32:54 -08002049 return 0;
2050}
2051
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002052#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08002053static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002054{
2055 int ret = !is_memblock_offlined(mem);
2056
Randy Dunlap349daa02013-04-29 15:08:49 -07002057 if (unlikely(ret)) {
2058 phys_addr_t beginpa, endpa;
2059
2060 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2061 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07002062 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07002063 &beginpa, &endpa);
2064 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08002065
2066 return ret;
2067}
2068
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002069static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08002070{
Tang Chen60a5a192013-02-22 16:33:14 -08002071 int cpu;
2072
2073 for_each_present_cpu(cpu) {
2074 if (cpu_to_node(cpu) == pgdat->node_id)
2075 /*
2076 * the cpu on this node isn't removed, and we can't
2077 * offline this node.
2078 */
2079 return -EBUSY;
2080 }
2081
2082 return 0;
2083}
2084
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002085static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002086{
2087#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002088 int cpu;
2089
2090 for_each_possible_cpu(cpu)
2091 if (cpu_to_node(cpu) == pgdat->node_id)
2092 numa_clear_node(cpu);
2093#endif
2094}
2095
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002096static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002097{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002098 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002099
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002100 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002101 if (ret)
2102 return ret;
2103
2104 /*
2105 * the node will be offlined when we come here, so we can clear
2106 * the cpu_to_node() now.
2107 */
2108
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002109 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002110 return 0;
2111}
2112
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002113/**
2114 * try_offline_node
2115 *
2116 * Offline a node if all memory sections and cpus of the node are removed.
2117 *
2118 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2119 * and online/offline operations before this call.
2120 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002121void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002122{
Wen Congyangd822b862013-02-22 16:33:16 -08002123 pg_data_t *pgdat = NODE_DATA(nid);
2124 unsigned long start_pfn = pgdat->node_start_pfn;
2125 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002126 unsigned long pfn;
2127
2128 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2129 unsigned long section_nr = pfn_to_section_nr(pfn);
2130
2131 if (!present_section_nr(section_nr))
2132 continue;
2133
2134 if (pfn_to_nid(pfn) != nid)
2135 continue;
2136
2137 /*
2138 * some memory sections of this node are not removed, and we
2139 * can't offline node now.
2140 */
2141 return;
2142 }
2143
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002144 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002145 return;
2146
2147 /*
2148 * all memory/cpu of this node are removed, we can offline this
2149 * node now.
2150 */
2151 node_set_offline(nid);
2152 unregister_one_node(nid);
2153}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002154EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002155
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002156/**
2157 * remove_memory
2158 *
2159 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2160 * and online/offline operations before this call, as required by
2161 * try_offline_node().
2162 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002163void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002164{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002165 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002166
Toshi Kani27356f52013-09-11 14:21:49 -07002167 BUG_ON(check_hotplug_memory_range(start, size));
2168
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002169 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002170
2171 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002172 * All memory blocks must be offlined before removing memory. Check
2173 * whether all memory blocks in question are offline and trigger a BUG()
2174 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002175 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002176 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002177 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002178 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002179 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002180
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002181 /* remove memmap entry */
2182 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002183 memblock_free(start, size);
2184 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002185
Wen Congyang24d335c2013-02-22 16:32:58 -08002186 arch_remove_memory(start, size);
2187
Tang Chen60a5a192013-02-22 16:33:14 -08002188 try_offline_node(nid);
2189
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002190 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002191}
Badari Pulavarty71088782008-10-18 20:25:58 -07002192EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002193#endif /* CONFIG_MEMORY_HOTREMOVE */