blob: 3c9c56268821dab7dfc03e5bfb60d2a577ae212b [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#include <linux/stddef.h>
8#include <linux/mm.h>
9#include <linux/swap.h>
10#include <linux/interrupt.h>
11#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070012#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070014#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070015#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/slab.h>
17#include <linux/sysctl.h>
18#include <linux/cpu.h>
19#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080020#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070021#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080032#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070033#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080034#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080035#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070036#include <linux/compaction.h>
Michal Hockocb1206e2018-12-28 00:38:01 -080037#include <linux/rmap.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
David Rientjes30467e02015-04-14 15:45:11 -0700128void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700129{
130 mem_hotplug.active_writer = current;
131
132 memhp_lock_acquire();
133 for (;;) {
134 mutex_lock(&mem_hotplug.lock);
135 if (likely(!mem_hotplug.refcount))
136 break;
137 __set_current_state(TASK_UNINTERRUPTIBLE);
138 mutex_unlock(&mem_hotplug.lock);
139 schedule();
140 }
141}
142
David Rientjes30467e02015-04-14 15:45:11 -0700143void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700144{
145 mem_hotplug.active_writer = NULL;
146 mutex_unlock(&mem_hotplug.lock);
147 memhp_lock_release();
148}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800149
Keith Mannthey45e0b782006-09-30 23:27:09 -0700150/* add this memory to iomem resource */
151static struct resource *register_memory_resource(u64 start, u64 size)
152{
153 struct resource *res;
154 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800155 if (!res)
156 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700157
158 res->name = "System RAM";
159 res->start = start;
160 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100161 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700162 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700163 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700164 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800165 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700166 }
167 return res;
168}
169
170static void release_memory_resource(struct resource *res)
171{
172 if (!res)
173 return;
174 release_resource(res);
175 kfree(res);
176 return;
177}
178
Keith Mannthey53947022006-09-30 23:27:08 -0700179#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800180void get_page_bootmem(unsigned long info, struct page *page,
181 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700182{
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800183 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700184 SetPagePrivate(page);
185 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700186 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700187}
188
Jiang Liu170a5a72013-07-03 15:03:17 -0700189void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700190{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800191 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700192
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800193 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800194 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
195 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700196
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700197 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsua5f043b2017-02-22 15:45:13 -0800198 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700199 ClearPagePrivate(page);
200 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800201 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700202 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700203 }
Yasunori Goto04753272008-04-28 02:13:31 -0700204}
205
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800206#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
207#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700208static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700209{
210 unsigned long *usemap, mapsize, section_nr, i;
211 struct mem_section *ms;
212 struct page *page, *memmap;
213
Yasunori Goto04753272008-04-28 02:13:31 -0700214 section_nr = pfn_to_section_nr(start_pfn);
215 ms = __nr_to_section(section_nr);
216
217 /* Get section's memmap address */
218 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
219
220 /*
221 * Get page for the memmap's phys address
222 * XXX: need more consideration for sparse_vmemmap...
223 */
224 page = virt_to_page(memmap);
225 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
226 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
227
228 /* remember memmap's page */
229 for (i = 0; i < mapsize; i++, page++)
230 get_page_bootmem(section_nr, page, SECTION_INFO);
231
232 usemap = __nr_to_section(section_nr)->pageblock_flags;
233 page = virt_to_page(usemap);
234
235 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
236
237 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700238 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700239
240}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800241#else /* CONFIG_SPARSEMEM_VMEMMAP */
242static void register_page_bootmem_info_section(unsigned long start_pfn)
243{
244 unsigned long *usemap, mapsize, section_nr, i;
245 struct mem_section *ms;
246 struct page *page, *memmap;
247
248 if (!pfn_valid(start_pfn))
249 return;
250
251 section_nr = pfn_to_section_nr(start_pfn);
252 ms = __nr_to_section(section_nr);
253
254 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
255
256 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
257
258 usemap = __nr_to_section(section_nr)->pageblock_flags;
259 page = virt_to_page(usemap);
260
261 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
262
263 for (i = 0; i < mapsize; i++, page++)
264 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
265}
266#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700267
Linus Torvalds7ded3842016-05-27 15:23:32 -0700268void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700269{
270 unsigned long i, pfn, end_pfn, nr_pages;
271 int node = pgdat->node_id;
272 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700273
274 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
275 page = virt_to_page(pgdat);
276
277 for (i = 0; i < nr_pages; i++, page++)
278 get_page_bootmem(node, page, NODE_INFO);
279
Yasunori Goto04753272008-04-28 02:13:31 -0700280 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800281 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700282
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700283 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700284 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
285 /*
286 * Some platforms can assign the same pfn to multiple nodes - on
287 * node0 as well as nodeN. To avoid registering a pfn against
288 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700289 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700290 */
Yang Shif65e91df2016-05-27 14:27:32 -0700291 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700292 register_page_bootmem_info_section(pfn);
293 }
Yasunori Goto04753272008-04-28 02:13:31 -0700294}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800295#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700296
Fabian Frederickf2765402014-08-06 16:04:57 -0700297static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
298 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700299{
300 unsigned long old_zone_end_pfn;
301
302 zone_span_writelock(zone);
303
Xishi Qiuc33bc312013-09-11 14:21:44 -0700304 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700305 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700306 zone->zone_start_pfn = start_pfn;
307
308 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
309 zone->zone_start_pfn;
310
311 zone_span_writeunlock(zone);
312}
313
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800314static void resize_zone(struct zone *zone, unsigned long start_pfn,
315 unsigned long end_pfn)
316{
317 zone_span_writelock(zone);
318
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800319 if (end_pfn - start_pfn) {
320 zone->zone_start_pfn = start_pfn;
321 zone->spanned_pages = end_pfn - start_pfn;
322 } else {
323 /*
324 * make it consist as free_area_init_core(),
325 * if spanned_pages = 0, then keep start_pfn = 0
326 */
327 zone->zone_start_pfn = 0;
328 zone->spanned_pages = 0;
329 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800330
331 zone_span_writeunlock(zone);
332}
333
334static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
335 unsigned long end_pfn)
336{
337 enum zone_type zid = zone_idx(zone);
338 int nid = zone->zone_pgdat->node_id;
339 unsigned long pfn;
340
341 for (pfn = start_pfn; pfn < end_pfn; pfn++)
342 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
343}
344
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800345/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800346 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800347static int __ref ensure_zone_is_initialized(struct zone *zone,
348 unsigned long start_pfn, unsigned long num_pages)
349{
350 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800351 return init_currently_empty_zone(zone, start_pfn, num_pages);
352
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800353 return 0;
354}
355
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800356static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800357 unsigned long start_pfn, unsigned long end_pfn)
358{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800359 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800360 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800361 unsigned long z1_start_pfn;
362
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800363 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
364 if (ret)
365 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800366
367 pgdat_resize_lock(z1->zone_pgdat, &flags);
368
369 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800370 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800371 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700372 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800373 if (start_pfn > z2->zone_start_pfn)
374 goto out_fail;
375 /* must included/overlap */
376 if (end_pfn <= z2->zone_start_pfn)
377 goto out_fail;
378
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800379 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700380 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800381 z1_start_pfn = z1->zone_start_pfn;
382 else
383 z1_start_pfn = start_pfn;
384
385 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800386 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800387
388 pgdat_resize_unlock(z1->zone_pgdat, &flags);
389
390 fix_zone_id(z1, start_pfn, end_pfn);
391
392 return 0;
393out_fail:
394 pgdat_resize_unlock(z1->zone_pgdat, &flags);
395 return -1;
396}
397
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800398static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800399 unsigned long start_pfn, unsigned long end_pfn)
400{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800401 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800402 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800403 unsigned long z2_end_pfn;
404
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800405 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
406 if (ret)
407 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800408
409 pgdat_resize_lock(z1->zone_pgdat, &flags);
410
411 /* can't move pfns which are lower than @z1 */
412 if (z1->zone_start_pfn > start_pfn)
413 goto out_fail;
414 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800415 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800416 goto out_fail;
417 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800418 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800419 goto out_fail;
420
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800421 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700422 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800423 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800424 else
425 z2_end_pfn = end_pfn;
426
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800427 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800428 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800429
430 pgdat_resize_unlock(z1->zone_pgdat, &flags);
431
432 fix_zone_id(z2, start_pfn, end_pfn);
433
434 return 0;
435out_fail:
436 pgdat_resize_unlock(z1->zone_pgdat, &flags);
437 return -1;
438}
439
Reza Arbabe51e6c82016-07-26 15:22:20 -0700440static struct zone * __meminit move_pfn_range(int zone_shift,
441 unsigned long start_pfn, unsigned long end_pfn)
442{
443 struct zone *zone = page_zone(pfn_to_page(start_pfn));
444 int ret = 0;
445
446 if (zone_shift < 0)
447 ret = move_pfn_range_left(zone + zone_shift, zone,
448 start_pfn, end_pfn);
449 else if (zone_shift)
450 ret = move_pfn_range_right(zone, zone + zone_shift,
451 start_pfn, end_pfn);
452
453 if (ret)
454 return NULL;
455
456 return zone + zone_shift;
457}
458
Fabian Frederickf2765402014-08-06 16:04:57 -0700459static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
460 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700461{
Xishi Qiu83285c72013-11-12 15:07:19 -0800462 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700463
Tang Chen712cd382012-12-11 16:01:07 -0800464 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700465 pgdat->node_start_pfn = start_pfn;
466
467 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
468 pgdat->node_start_pfn;
469}
470
Al Viro31168482008-11-22 17:33:24 +0000471static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700472{
473 struct pglist_data *pgdat = zone->zone_pgdat;
474 int nr_pages = PAGES_PER_SECTION;
475 int nid = pgdat->node_id;
476 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700477 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800478 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700479
480 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800481 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
482 if (ret)
483 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700484
Heiko Carstens76cdd582008-05-14 16:05:52 -0700485 pgdat_resize_lock(zone->zone_pgdat, &flags);
486 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
487 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
488 phys_start_pfn + nr_pages);
489 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800490 memmap_init_zone(nr_pages, nid, zone_type,
491 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700492
493 /* online_page_range is called later and expects pages reserved */
494 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
495 if (!pfn_valid(pfn))
496 continue;
497
498 SetPageReserved(pfn_to_page(pfn));
499 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700500 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700501}
502
Gary Hadec04fc582009-01-06 14:39:14 -0800503static int __meminit __add_section(int nid, struct zone *zone,
504 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700505{
Dave Hansen3947be12005-10-29 18:16:54 -0700506 int ret;
507
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700508 if (pfn_valid(phys_start_pfn))
509 return -EEXIST;
510
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800511 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700512
513 if (ret < 0)
514 return ret;
515
Yasunori Goto718127c2006-06-23 02:03:10 -0700516 ret = __add_zone(zone, phys_start_pfn);
517
518 if (ret < 0)
519 return ret;
520
Gary Hadec04fc582009-01-06 14:39:14 -0800521 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700522}
523
David Rientjes4edd7ce2013-04-29 15:08:22 -0700524/*
525 * Reasonably generic function for adding memory. It is
526 * expected that archs that support memory hotplug will
527 * call this function after deciding the zone to which to
528 * add the new pages.
529 */
530int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
531 unsigned long nr_pages)
532{
533 unsigned long i;
534 int err = 0;
535 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800536 struct vmem_altmap *altmap;
537
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700538 clear_zone_contiguous(zone);
539
David Rientjes4edd7ce2013-04-29 15:08:22 -0700540 /* during initialize mem_map, align hot-added range to section */
541 start_sec = pfn_to_section_nr(phys_start_pfn);
542 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
543
Dan Williams4b94ffd2016-01-15 16:56:22 -0800544 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
545 if (altmap) {
546 /*
547 * Validate altmap is within bounds of the total request
548 */
549 if (altmap->base_pfn != phys_start_pfn
550 || vmem_altmap_offset(altmap) > nr_pages) {
551 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700552 err = -EINVAL;
553 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800554 }
555 altmap->alloc = 0;
556 }
557
David Rientjes4edd7ce2013-04-29 15:08:22 -0700558 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700559 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700560
561 /*
562 * EEXIST is finally dealt with by ioresource collision
563 * check. see add_memory() => register_memory_resource()
564 * Warning will be printed if there is collision.
565 */
566 if (err && (err != -EEXIST))
567 break;
568 err = 0;
569 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700570 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700571out:
572 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700573 return err;
574}
575EXPORT_SYMBOL_GPL(__add_pages);
576
577#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800578/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
579static int find_smallest_section_pfn(int nid, struct zone *zone,
580 unsigned long start_pfn,
581 unsigned long end_pfn)
582{
583 struct mem_section *ms;
584
585 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
586 ms = __pfn_to_section(start_pfn);
587
588 if (unlikely(!valid_section(ms)))
589 continue;
590
591 if (unlikely(pfn_to_nid(start_pfn) != nid))
592 continue;
593
594 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
595 continue;
596
597 return start_pfn;
598 }
599
600 return 0;
601}
602
603/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
604static int find_biggest_section_pfn(int nid, struct zone *zone,
605 unsigned long start_pfn,
606 unsigned long end_pfn)
607{
608 struct mem_section *ms;
609 unsigned long pfn;
610
611 /* pfn is the end pfn of a memory section. */
612 pfn = end_pfn - 1;
613 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
614 ms = __pfn_to_section(pfn);
615
616 if (unlikely(!valid_section(ms)))
617 continue;
618
619 if (unlikely(pfn_to_nid(pfn) != nid))
620 continue;
621
622 if (zone && zone != page_zone(pfn_to_page(pfn)))
623 continue;
624
625 return pfn;
626 }
627
628 return 0;
629}
630
631static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
632 unsigned long end_pfn)
633{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700634 unsigned long zone_start_pfn = zone->zone_start_pfn;
635 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
636 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800637 unsigned long pfn;
638 struct mem_section *ms;
639 int nid = zone_to_nid(zone);
640
641 zone_span_writelock(zone);
642 if (zone_start_pfn == start_pfn) {
643 /*
644 * If the section is smallest section in the zone, it need
645 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
646 * In this case, we find second smallest valid mem_section
647 * for shrinking zone.
648 */
649 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
650 zone_end_pfn);
651 if (pfn) {
652 zone->zone_start_pfn = pfn;
653 zone->spanned_pages = zone_end_pfn - pfn;
654 }
655 } else if (zone_end_pfn == end_pfn) {
656 /*
657 * If the section is biggest section in the zone, it need
658 * shrink zone->spanned_pages.
659 * In this case, we find second biggest valid mem_section for
660 * shrinking zone.
661 */
662 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
663 start_pfn);
664 if (pfn)
665 zone->spanned_pages = pfn - zone_start_pfn + 1;
666 }
667
668 /*
669 * The section is not biggest or smallest mem_section in the zone, it
670 * only creates a hole in the zone. So in this case, we need not
671 * change the zone. But perhaps, the zone has only hole data. Thus
672 * it check the zone has only hole or not.
673 */
674 pfn = zone_start_pfn;
675 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
676 ms = __pfn_to_section(pfn);
677
678 if (unlikely(!valid_section(ms)))
679 continue;
680
681 if (page_zone(pfn_to_page(pfn)) != zone)
682 continue;
683
684 /* If the section is current section, it continues the loop */
685 if (start_pfn == pfn)
686 continue;
687
688 /* If we find valid section, we have nothing to do */
689 zone_span_writeunlock(zone);
690 return;
691 }
692
693 /* The zone has no valid section */
694 zone->zone_start_pfn = 0;
695 zone->spanned_pages = 0;
696 zone_span_writeunlock(zone);
697}
698
699static void shrink_pgdat_span(struct pglist_data *pgdat,
700 unsigned long start_pfn, unsigned long end_pfn)
701{
Xishi Qiu83285c72013-11-12 15:07:19 -0800702 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
703 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
704 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800705 unsigned long pfn;
706 struct mem_section *ms;
707 int nid = pgdat->node_id;
708
709 if (pgdat_start_pfn == start_pfn) {
710 /*
711 * If the section is smallest section in the pgdat, it need
712 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
713 * In this case, we find second smallest valid mem_section
714 * for shrinking zone.
715 */
716 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
717 pgdat_end_pfn);
718 if (pfn) {
719 pgdat->node_start_pfn = pfn;
720 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
721 }
722 } else if (pgdat_end_pfn == end_pfn) {
723 /*
724 * If the section is biggest section in the pgdat, it need
725 * shrink pgdat->node_spanned_pages.
726 * In this case, we find second biggest valid mem_section for
727 * shrinking zone.
728 */
729 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
730 start_pfn);
731 if (pfn)
732 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
733 }
734
735 /*
736 * If the section is not biggest or smallest mem_section in the pgdat,
737 * it only creates a hole in the pgdat. So in this case, we need not
738 * change the pgdat.
739 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
740 * has only hole or not.
741 */
742 pfn = pgdat_start_pfn;
743 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
744 ms = __pfn_to_section(pfn);
745
746 if (unlikely(!valid_section(ms)))
747 continue;
748
749 if (pfn_to_nid(pfn) != nid)
750 continue;
751
752 /* If the section is current section, it continues the loop */
753 if (start_pfn == pfn)
754 continue;
755
756 /* If we find valid section, we have nothing to do */
757 return;
758 }
759
760 /* The pgdat has no valid section */
761 pgdat->node_start_pfn = 0;
762 pgdat->node_spanned_pages = 0;
763}
764
765static void __remove_zone(struct zone *zone, unsigned long start_pfn)
766{
767 struct pglist_data *pgdat = zone->zone_pgdat;
768 int nr_pages = PAGES_PER_SECTION;
769 int zone_type;
770 unsigned long flags;
771
772 zone_type = zone - pgdat->node_zones;
773
774 pgdat_resize_lock(zone->zone_pgdat, &flags);
775 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
776 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
777 pgdat_resize_unlock(zone->zone_pgdat, &flags);
778}
779
Dan Williams4b94ffd2016-01-15 16:56:22 -0800780static int __remove_section(struct zone *zone, struct mem_section *ms,
781 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700782{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800783 unsigned long start_pfn;
784 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700785 int ret = -EINVAL;
786
787 if (!valid_section(ms))
788 return ret;
789
790 ret = unregister_memory_section(ms);
791 if (ret)
792 return ret;
793
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800794 scn_nr = __section_nr(ms);
795 start_pfn = section_nr_to_pfn(scn_nr);
796 __remove_zone(zone, start_pfn);
797
Dan Williams4b94ffd2016-01-15 16:56:22 -0800798 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700799 return 0;
800}
801
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700802/**
803 * __remove_pages() - remove sections of pages from a zone
804 * @zone: zone from which pages need to be removed
805 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
806 * @nr_pages: number of pages to remove (must be multiple of section size)
807 *
808 * Generic helper function to remove section mappings and sysfs entries
809 * for the section of the memory we are removing. Caller needs to make
810 * sure that pages are marked reserved and zones are adjust properly by
811 * calling offline_pages().
812 */
813int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
814 unsigned long nr_pages)
815{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700816 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800817 unsigned long map_offset = 0;
818 int sections_to_remove, ret = 0;
819
820 /* In the ZONE_DEVICE case device driver owns the memory region */
821 if (is_dev_zone(zone)) {
822 struct page *page = pfn_to_page(phys_start_pfn);
823 struct vmem_altmap *altmap;
824
825 altmap = to_vmem_altmap((unsigned long) page);
826 if (altmap)
827 map_offset = vmem_altmap_offset(altmap);
828 } else {
829 resource_size_t start, size;
830
831 start = phys_start_pfn << PAGE_SHIFT;
832 size = nr_pages * PAGE_SIZE;
833
834 ret = release_mem_region_adjustable(&iomem_resource, start,
835 size);
836 if (ret) {
837 resource_size_t endres = start + size - 1;
838
839 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
840 &start, &endres, ret);
841 }
842 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700843
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700844 clear_zone_contiguous(zone);
845
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700846 /*
847 * We can only remove entire sections
848 */
849 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
850 BUG_ON(nr_pages % PAGES_PER_SECTION);
851
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700852 sections_to_remove = nr_pages / PAGES_PER_SECTION;
853 for (i = 0; i < sections_to_remove; i++) {
854 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800855
856 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
857 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700858 if (ret)
859 break;
860 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700861
862 set_zone_contiguous(zone);
863
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700864 return ret;
865}
866EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700867#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700868
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700869int set_online_page_callback(online_page_callback_t callback)
870{
871 int rc = -EINVAL;
872
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700873 get_online_mems();
874 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700875
876 if (online_page_callback == generic_online_page) {
877 online_page_callback = callback;
878 rc = 0;
879 }
880
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700881 mutex_unlock(&online_page_callback_lock);
882 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700883
884 return rc;
885}
886EXPORT_SYMBOL_GPL(set_online_page_callback);
887
888int restore_online_page_callback(online_page_callback_t callback)
889{
890 int rc = -EINVAL;
891
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700892 get_online_mems();
893 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700894
895 if (online_page_callback == callback) {
896 online_page_callback = generic_online_page;
897 rc = 0;
898 }
899
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700900 mutex_unlock(&online_page_callback_lock);
901 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700902
903 return rc;
904}
905EXPORT_SYMBOL_GPL(restore_online_page_callback);
906
907void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700908{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700909}
910EXPORT_SYMBOL_GPL(__online_page_set_limits);
911
912void __online_page_increment_counters(struct page *page)
913{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700914 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700915}
916EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700917
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700918void __online_page_free(struct page *page)
919{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700920 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700921}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700922EXPORT_SYMBOL_GPL(__online_page_free);
923
924static void generic_online_page(struct page *page)
925{
926 __online_page_set_limits(page);
927 __online_page_increment_counters(page);
928 __online_page_free(page);
929}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700930
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700931static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
932 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700933{
934 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700935 unsigned long onlined_pages = *(unsigned long *)arg;
936 struct page *page;
937 if (PageReserved(pfn_to_page(start_pfn)))
938 for (i = 0; i < nr_pages; i++) {
939 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700940 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700941 onlined_pages++;
942 }
943 *(unsigned long *)arg = onlined_pages;
944 return 0;
945}
946
Lai Jiangshan09285af2012-12-12 13:52:04 -0800947#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800948/*
949 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
950 * normal memory.
951 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800952static bool can_online_high_movable(struct zone *zone)
953{
954 return true;
955}
Tang Chen79a4dce2012-12-18 14:23:24 -0800956#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800957/* ensure every online node has NORMAL memory */
958static bool can_online_high_movable(struct zone *zone)
959{
960 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
961}
Tang Chen79a4dce2012-12-18 14:23:24 -0800962#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800963
Lai Jiangshand9713672012-12-11 16:01:03 -0800964/* check which state of node_states will be changed when online memory */
965static void node_states_check_changes_online(unsigned long nr_pages,
966 struct zone *zone, struct memory_notify *arg)
967{
968 int nid = zone_to_nid(zone);
969 enum zone_type zone_last = ZONE_NORMAL;
970
971 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800972 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
973 * contains nodes which have zones of 0...ZONE_NORMAL,
974 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800975 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800976 * If we don't have HIGHMEM nor movable node,
977 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
978 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800979 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800980 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800981 zone_last = ZONE_MOVABLE;
982
983 /*
984 * if the memory to be online is in a zone of 0...zone_last, and
985 * the zones of 0...zone_last don't have memory before online, we will
986 * need to set the node to node_states[N_NORMAL_MEMORY] after
987 * the memory is online.
988 */
989 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
990 arg->status_change_nid_normal = nid;
991 else
992 arg->status_change_nid_normal = -1;
993
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800994#ifdef CONFIG_HIGHMEM
995 /*
996 * If we have movable node, node_states[N_HIGH_MEMORY]
997 * contains nodes which have zones of 0...ZONE_HIGHMEM,
998 * set zone_last to ZONE_HIGHMEM.
999 *
1000 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1001 * contains nodes which have zones of 0...ZONE_MOVABLE,
1002 * set zone_last to ZONE_MOVABLE.
1003 */
1004 zone_last = ZONE_HIGHMEM;
1005 if (N_MEMORY == N_HIGH_MEMORY)
1006 zone_last = ZONE_MOVABLE;
1007
1008 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1009 arg->status_change_nid_high = nid;
1010 else
1011 arg->status_change_nid_high = -1;
1012#else
1013 arg->status_change_nid_high = arg->status_change_nid_normal;
1014#endif
1015
Lai Jiangshand9713672012-12-11 16:01:03 -08001016 /*
1017 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001018 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001019 * is online.
1020 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001021 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001022 arg->status_change_nid = nid;
1023 else
1024 arg->status_change_nid = -1;
1025}
1026
1027static void node_states_set_node(int node, struct memory_notify *arg)
1028{
1029 if (arg->status_change_nid_normal >= 0)
1030 node_set_state(node, N_NORMAL_MEMORY);
1031
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001032 if (arg->status_change_nid_high >= 0)
1033 node_set_state(node, N_HIGH_MEMORY);
1034
1035 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001036}
1037
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001038bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1039 enum zone_type target, int *zone_shift)
Reza Arbabdf429ac2016-07-26 15:22:23 -07001040{
1041 struct zone *zone = page_zone(pfn_to_page(pfn));
1042 enum zone_type idx = zone_idx(zone);
1043 int i;
1044
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001045 *zone_shift = 0;
1046
Reza Arbabdf429ac2016-07-26 15:22:23 -07001047 if (idx < target) {
1048 /* pages must be at end of current zone */
1049 if (pfn + nr_pages != zone_end_pfn(zone))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001050 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001051
1052 /* no zones in use between current zone and target */
1053 for (i = idx + 1; i < target; i++)
1054 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001055 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001056 }
1057
1058 if (target < idx) {
1059 /* pages must be at beginning of current zone */
1060 if (pfn != zone->zone_start_pfn)
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001061 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001062
1063 /* no zones in use between current zone and target */
1064 for (i = target + 1; i < idx; i++)
1065 if (zone_is_initialized(zone - idx + i))
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001066 return false;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001067 }
1068
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001069 *zone_shift = target - idx;
1070 return true;
Reza Arbabdf429ac2016-07-26 15:22:23 -07001071}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001072
David Rientjes30467e02015-04-14 15:45:11 -07001073/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001074int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001075{
Cody P Schaferaa472282013-07-03 15:02:10 -07001076 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001077 unsigned long onlined_pages = 0;
1078 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001079 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001080 int nid;
1081 int ret;
1082 struct memory_notify arg;
Reza Arbabe51e6c82016-07-26 15:22:20 -07001083 int zone_shift = 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001084
Lai Jiangshand9713672012-12-11 16:01:03 -08001085 /*
1086 * This doesn't need a lock to do pfn_to_page().
1087 * The section can't be removed here because of the
1088 * memory_block->state_mutex.
1089 */
1090 zone = page_zone(pfn_to_page(pfn));
1091
Tang Chen4f7c6b42014-08-06 16:05:13 -07001092 if ((zone_idx(zone) > ZONE_NORMAL ||
1093 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001094 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001095 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001096
Yasuaki Ishimatsu143a9ad2017-01-24 15:17:45 -08001097 if (online_type == MMOP_ONLINE_KERNEL) {
1098 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1099 return -EINVAL;
1100 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1101 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1102 return -EINVAL;
1103 }
Reza Arbabe51e6c82016-07-26 15:22:20 -07001104
1105 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1106 if (!zone)
1107 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001108
Yasunori Goto7b78d332007-10-21 16:41:36 -07001109 arg.start_pfn = pfn;
1110 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001111 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001112
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001113 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001114
1115 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1116 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001117 if (ret)
1118 goto failed_addition;
1119
Dave Hansen3947be12005-10-29 18:16:54 -07001120 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001121 * If this zone is not populated, then it is not in zonelist.
1122 * This means the page allocator ignores this zone.
1123 * So, zonelist must be updated after online.
1124 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001125 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001126 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001127 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001128 build_all_zonelists(NULL, zone);
1129 }
Yasunori Goto68113782006-06-23 02:03:11 -07001130
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001131 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001132 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001133 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001134 if (need_zonelists_rebuild)
1135 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001136 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001137 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001138 }
1139
Dave Hansen3947be12005-10-29 18:16:54 -07001140 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001141
1142 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001143 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001144 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1145
Jiang Liu08dff7b2012-07-31 16:43:30 -07001146 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001147 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001148 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001149 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001150 else
1151 zone_pcp_update(zone);
1152 }
Dave Hansen3947be12005-10-29 18:16:54 -07001153
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001154 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001155
1156 init_per_zone_wmark_min();
1157
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001158 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001159 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001160 kcompactd_run(nid);
1161 }
Dave Hansen61b13992005-10-29 18:16:56 -07001162
Haicheng Li1f522502010-05-24 14:32:51 -07001163 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001164
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001165 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001166
1167 if (onlined_pages)
1168 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001169 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001170
1171failed_addition:
1172 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1173 (unsigned long long) pfn << PAGE_SHIFT,
1174 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1175 memory_notify(MEM_CANCEL_ONLINE, &arg);
1176 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001177}
Keith Mannthey53947022006-09-30 23:27:08 -07001178#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001179
Tang Chen0bd85422014-11-13 15:19:41 -08001180static void reset_node_present_pages(pg_data_t *pgdat)
1181{
1182 struct zone *z;
1183
1184 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1185 z->present_pages = 0;
1186
1187 pgdat->node_present_pages = 0;
1188}
1189
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001190/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1191static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001192{
1193 struct pglist_data *pgdat;
1194 unsigned long zones_size[MAX_NR_ZONES] = {0};
1195 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001196 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001197
Tang Chena1e565a2013-02-22 16:33:18 -08001198 pgdat = NODE_DATA(nid);
1199 if (!pgdat) {
1200 pgdat = arch_alloc_nodedata(nid);
1201 if (!pgdat)
1202 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001203
Tang Chena1e565a2013-02-22 16:33:18 -08001204 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001205 } else {
Mel Gorman7fcc2022017-05-03 14:53:45 -07001206 /*
1207 * Reset the nr_zones, order and classzone_idx before reuse.
1208 * Note that kswapd will init kswapd_classzone_idx properly
1209 * when it starts in the near future.
1210 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001211 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001212 pgdat->kswapd_order = 0;
1213 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001214 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001215
1216 /* we can use NODE_DATA(nid) from here */
1217
1218 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001219 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001220 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001221
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001222 /*
1223 * The node we allocated has no zone fallback lists. For avoiding
1224 * to access not-initialized zonelist, build here.
1225 */
David Rientjesf957db42011-06-22 18:13:04 -07001226 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001227 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001228 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001229
Tang Chenf784a3f2014-11-13 15:19:39 -08001230 /*
1231 * zone->managed_pages is set to an approximate value in
1232 * free_area_init_core(), which will cause
1233 * /sys/device/system/node/nodeX/meminfo has wrong data.
1234 * So reset it to 0 before any memory is onlined.
1235 */
1236 reset_node_managed_pages(pgdat);
1237
Tang Chen0bd85422014-11-13 15:19:41 -08001238 /*
1239 * When memory is hot-added, all the memory is in offline state. So
1240 * clear all zones' present_pages because they will be updated in
1241 * online_pages() and offline_pages().
1242 */
1243 reset_node_present_pages(pgdat);
1244
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001245 return pgdat;
1246}
1247
1248static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1249{
1250 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001251 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001252 arch_free_nodedata(pgdat);
1253 return;
1254}
1255
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001256
Toshi Kani01b0f192013-11-12 15:07:25 -08001257/**
1258 * try_online_node - online a node if offlined
1259 *
minskey guocf234222010-05-24 14:32:41 -07001260 * called by cpu_up() to online a node without onlined memory.
1261 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001262int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001263{
1264 pg_data_t *pgdat;
1265 int ret;
1266
Toshi Kani01b0f192013-11-12 15:07:25 -08001267 if (node_online(nid))
1268 return 0;
1269
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001270 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001271 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001272 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001273 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001274 ret = -ENOMEM;
1275 goto out;
1276 }
1277 node_set_online(nid);
1278 ret = register_one_node(nid);
1279 BUG_ON(ret);
1280
Toshi Kani01b0f192013-11-12 15:07:25 -08001281 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1282 mutex_lock(&zonelists_mutex);
1283 build_all_zonelists(NULL, NULL);
1284 mutex_unlock(&zonelists_mutex);
1285 }
1286
minskey guocf234222010-05-24 14:32:41 -07001287out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001288 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001289 return ret;
1290}
1291
Toshi Kani27356f52013-09-11 14:21:49 -07001292static int check_hotplug_memory_range(u64 start, u64 size)
1293{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001294 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001295 u64 nr_pages = size >> PAGE_SHIFT;
1296
1297 /* Memory range must be aligned with section */
1298 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1299 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1300 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1301 (unsigned long long)start,
1302 (unsigned long long)size);
1303 return -EINVAL;
1304 }
1305
1306 return 0;
1307}
1308
Wang Nan63264402014-08-06 16:07:36 -07001309/*
1310 * If movable zone has already been setup, newly added memory should be check.
1311 * If its address is higher than movable zone, it should be added as movable.
1312 * Without this check, movable zone may overlap with other zone.
1313 */
1314static int should_add_memory_movable(int nid, u64 start, u64 size)
1315{
1316 unsigned long start_pfn = start >> PAGE_SHIFT;
1317 pg_data_t *pgdat = NODE_DATA(nid);
1318 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1319
1320 if (zone_is_empty(movable_zone))
1321 return 0;
1322
1323 if (movable_zone->zone_start_pfn <= start_pfn)
1324 return 1;
1325
1326 return 0;
1327}
1328
Dan Williams033fbae2015-08-09 15:29:06 -04001329int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1330 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001331{
Dan Williams033fbae2015-08-09 15:29:06 -04001332#ifdef CONFIG_ZONE_DEVICE
1333 if (for_device)
1334 return ZONE_DEVICE;
1335#endif
Wang Nan63264402014-08-06 16:07:36 -07001336 if (should_add_memory_movable(nid, start, size))
1337 return ZONE_MOVABLE;
1338
1339 return zone_default;
1340}
1341
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001342static int online_memory_block(struct memory_block *mem, void *arg)
1343{
1344 return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1345}
1346
Al Viro31168482008-11-22 17:33:24 +00001347/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001348int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001349{
David Vrabel62cedb92015-06-25 16:35:49 +01001350 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001351 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001352 bool new_pgdat;
1353 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001354 int ret;
1355
David Vrabel62cedb92015-06-25 16:35:49 +01001356 start = res->start;
1357 size = resource_size(res);
1358
Toshi Kani27356f52013-09-11 14:21:49 -07001359 ret = check_hotplug_memory_range(start, size);
1360 if (ret)
1361 return ret;
1362
Tang Chena1e565a2013-02-22 16:33:18 -08001363 { /* Stupid hack to suppress address-never-null warning */
1364 void *p = NODE_DATA(nid);
1365 new_pgdat = !p;
1366 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001367
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001368 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001369
Tang Chen7f36e3e2015-09-04 15:42:32 -07001370 /*
1371 * Add new range to memblock so that when hotadd_new_pgdat() is called
1372 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1373 * this new range and calculate total pages correctly. The range will
1374 * be removed at hot-remove time.
1375 */
1376 memblock_add_node(start, size, nid);
1377
Tang Chena1e565a2013-02-22 16:33:18 -08001378 new_node = !node_online(nid);
1379 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001380 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001381 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001382 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001383 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001384 }
1385
Yasunori Gotobc02af92006-06-27 02:53:30 -07001386 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001387 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001388
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001389 if (ret < 0)
1390 goto error;
1391
Yasunori Goto0fc44152006-06-27 02:53:38 -07001392 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001393 node_set_online(nid);
1394
Tang Chena1e565a2013-02-22 16:33:18 -08001395 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001396 ret = register_one_node(nid);
1397 /*
1398 * If sysfs file of new node can't create, cpu on the node
1399 * can't be hot-added. There is no rollback way now.
1400 * So, check by BUG_ON() to catch it reluctantly..
1401 */
1402 BUG_ON(ret);
1403 }
1404
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001405 /* create new memmap entry */
1406 firmware_map_add_hotplug(start, start + size, "System RAM");
1407
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001408 /* online pages if requested */
1409 if (online)
1410 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1411 NULL, online_memory_block);
1412
Andi Kleen6ad696d2009-11-17 14:06:22 -08001413 goto out;
1414
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001415error:
1416 /* rollback pgdat allocation and others */
1417 if (new_pgdat)
1418 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001419 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001420
Andi Kleen6ad696d2009-11-17 14:06:22 -08001421out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001422 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001423 return ret;
1424}
David Vrabel62cedb92015-06-25 16:35:49 +01001425EXPORT_SYMBOL_GPL(add_memory_resource);
1426
1427int __ref add_memory(int nid, u64 start, u64 size)
1428{
1429 struct resource *res;
1430 int ret;
1431
1432 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001433 if (IS_ERR(res))
1434 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001435
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001436 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001437 if (ret < 0)
1438 release_memory_resource(res);
1439 return ret;
1440}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001441EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001442
1443#ifdef CONFIG_MEMORY_HOTREMOVE
1444/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001445 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1446 * set and the size of the free page is given by page_order(). Using this,
1447 * the function determines if the pageblock contains only free pages.
1448 * Due to buddy contraints, a free page at least the size of a pageblock will
1449 * be located at the start of the pageblock
1450 */
1451static inline int pageblock_free(struct page *page)
1452{
1453 return PageBuddy(page) && page_order(page) >= pageblock_order;
1454}
1455
1456/* Return the start of the next active pageblock after a given page */
1457static struct page *next_active_pageblock(struct page *page)
1458{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001459 /* Ensure the starting page is pageblock-aligned */
1460 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1461
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001462 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001463 if (pageblock_free(page)) {
1464 int order;
1465 /* be careful. we don't have locks, page_order can be changed.*/
1466 order = page_order(page);
1467 if ((order < MAX_ORDER) && (order >= pageblock_order))
1468 return page + (1 << order);
1469 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001470
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001471 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001472}
1473
1474/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001475bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001476{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001477 struct page *page = pfn_to_page(start_pfn);
Michal Hocko61fdbbb2019-02-01 14:20:34 -08001478 unsigned long end_pfn = min(start_pfn + nr_pages, zone_end_pfn(page_zone(page)));
1479 struct page *end_page = pfn_to_page(end_pfn);
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001480
1481 /* Check the starting page of each pageblock within the range */
1482 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001483 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001484 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001485 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001486 }
1487
1488 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001489 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001490}
1491
1492/*
Toshi Kani72f74192017-02-03 13:13:20 -08001493 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kani6cb04972017-02-03 13:13:23 -08001494 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001495 */
Toshi Kani6cb04972017-02-03 13:13:23 -08001496int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1497 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001498{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001499 unsigned long pfn, sec_end_pfn;
Toshi Kani6cb04972017-02-03 13:13:23 -08001500 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001501 struct zone *zone = NULL;
1502 struct page *page;
1503 int i;
Toshi Kani72f74192017-02-03 13:13:20 -08001504 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001505 pfn < end_pfn;
Toshi Kani72f74192017-02-03 13:13:20 -08001506 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001507 /* Make sure the memory section is present first */
1508 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001509 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001510 for (; pfn < sec_end_pfn && pfn < end_pfn;
1511 pfn += MAX_ORDER_NR_PAGES) {
1512 i = 0;
1513 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1514 while ((i < MAX_ORDER_NR_PAGES) &&
1515 !pfn_valid_within(pfn + i))
1516 i++;
zhong jiangde4c1752017-02-24 14:59:30 -08001517 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001518 continue;
Mikhail Zaslonko3016e962019-02-01 14:20:38 -08001519 /* Check if we got outside of the zone */
1520 if (zone && !zone_spans_pfn(zone, pfn + i))
1521 return 0;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001522 page = pfn_to_page(pfn + i);
1523 if (zone && page_zone(page) != zone)
1524 return 0;
Toshi Kani6cb04972017-02-03 13:13:23 -08001525 if (!zone)
1526 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001527 zone = page_zone(page);
Toshi Kani6cb04972017-02-03 13:13:23 -08001528 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001529 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001530 }
Toshi Kani72f74192017-02-03 13:13:20 -08001531
Toshi Kani6cb04972017-02-03 13:13:23 -08001532 if (zone) {
1533 *valid_start = start;
zhong jiangde4c1752017-02-24 14:59:30 -08001534 *valid_end = min(end, end_pfn);
Toshi Kani72f74192017-02-03 13:13:20 -08001535 return 1;
Toshi Kani6cb04972017-02-03 13:13:23 -08001536 } else {
Toshi Kani72f74192017-02-03 13:13:20 -08001537 return 0;
Toshi Kani6cb04972017-02-03 13:13:23 -08001538 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001539}
1540
1541/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001542 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1543 * and hugepages). We scan pfn because it's much easier than scanning over
1544 * linked list. This function returns the pfn of the first found movable
1545 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001546 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001547static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001548{
1549 unsigned long pfn;
1550 struct page *page;
1551 for (pfn = start; pfn < end; pfn++) {
1552 if (pfn_valid(pfn)) {
1553 page = pfn_to_page(pfn);
1554 if (PageLRU(page))
1555 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001556 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001557 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001558 return pfn;
1559 else
1560 pfn = round_up(pfn + 1,
1561 1 << compound_order(page)) - 1;
1562 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001563 }
1564 }
1565 return 0;
1566}
1567
Xishi Qiu394e31d2016-07-28 15:48:53 -07001568static struct page *new_node_page(struct page *page, unsigned long private,
1569 int **result)
1570{
1571 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1572 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001573 nodemask_t nmask = node_states[N_MEMORY];
1574 struct page *new_page = NULL;
Xishi Qiu394e31d2016-07-28 15:48:53 -07001575
1576 /*
1577 * TODO: allocate a destination hugepage from a nearest neighbor node,
1578 * accordance with memory policy of the user process if possible. For
1579 * now as a simple work-around, we use the next node for destination.
1580 */
1581 if (PageHuge(page))
1582 return alloc_huge_page_node(page_hstate(compound_head(page)),
1583 next_node_in(nid, nmask));
1584
Li Zhong231e97e2016-09-28 15:22:38 -07001585 node_clear(nid, nmask);
Li Zhong9bb627b2016-09-19 14:43:52 -07001586
Xishi Qiu394e31d2016-07-28 15:48:53 -07001587 if (PageHighMem(page)
1588 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1589 gfp_mask |= __GFP_HIGHMEM;
1590
Li Zhong231e97e2016-09-28 15:22:38 -07001591 if (!nodes_empty(nmask))
1592 new_page = __alloc_pages_nodemask(gfp_mask, 0,
Xishi Qiu394e31d2016-07-28 15:48:53 -07001593 node_zonelist(nid, gfp_mask), &nmask);
1594 if (!new_page)
1595 new_page = __alloc_pages(gfp_mask, 0,
1596 node_zonelist(nid, gfp_mask));
1597
1598 return new_page;
1599}
1600
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001601#define NR_OFFLINE_AT_ONCE_PAGES (256)
1602static int
1603do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1604{
1605 unsigned long pfn;
1606 struct page *page;
1607 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1608 int not_managed = 0;
1609 int ret = 0;
1610 LIST_HEAD(source);
1611
1612 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1613 if (!pfn_valid(pfn))
1614 continue;
1615 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001616
1617 if (PageHuge(page)) {
1618 struct page *head = compound_head(page);
1619 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1620 if (compound_order(head) > PFN_SECTION_SHIFT) {
1621 ret = -EBUSY;
1622 break;
1623 }
1624 if (isolate_huge_page(page, &source))
1625 move_pages -= 1 << compound_order(head);
1626 continue;
1627 }
1628
Michal Hockocb1206e2018-12-28 00:38:01 -08001629 /*
1630 * HWPoison pages have elevated reference counts so the migration would
1631 * fail on them. It also doesn't make any sense to migrate them in the
1632 * first place. Still try to unmap such a page in case it is still mapped
1633 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1634 * the unmap as the catch all safety net).
1635 */
1636 if (PageHWPoison(page)) {
1637 if (WARN_ON(PageLRU(page)))
1638 isolate_lru_page(page);
1639 if (page_mapped(page))
1640 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1641 continue;
1642 }
1643
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001644 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001645 continue;
1646 /*
1647 * We can skip free pages. And we can only deal with pages on
1648 * LRU.
1649 */
Nick Piggin62695a82008-10-18 20:26:09 -07001650 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001651 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001652 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001653 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001654 move_pages--;
Mel Gorman599d0c92016-07-28 15:45:31 -07001655 inc_node_page_state(page, NR_ISOLATED_ANON +
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001656 page_is_file_cache(page));
1657
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001658 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001659#ifdef CONFIG_DEBUG_VM
Chen Yuconge33e33b2016-03-17 14:19:35 -07001660 pr_alert("removing pfn %lx from LRU failed\n", pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001661 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001662#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001663 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001664 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001665 check this again here. */
1666 if (page_count(page)) {
1667 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001668 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001669 break;
1670 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001671 }
1672 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001673 if (!list_empty(&source)) {
1674 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001675 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001676 goto out;
1677 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001678
Xishi Qiu394e31d2016-07-28 15:48:53 -07001679 /* Allocate a new page from the nearest neighbor node */
1680 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001681 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001682 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001683 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001684 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001685out:
1686 return ret;
1687}
1688
1689/*
1690 * remove from free_area[] and mark all as Reserved.
1691 */
1692static int
1693offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1694 void *data)
1695{
1696 __offline_isolated_pages(start, start + nr_pages);
1697 return 0;
1698}
1699
1700static void
1701offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1702{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001703 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001704 offline_isolated_pages_cb);
1705}
1706
1707/*
1708 * Check all pages in range, recoreded as memory resource, are isolated.
1709 */
1710static int
1711check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1712 void *data)
1713{
1714 int ret;
1715 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001716 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001717 offlined = nr_pages;
1718 if (!ret)
1719 *(long *)data += offlined;
1720 return ret;
1721}
1722
1723static long
1724check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1725{
1726 long offlined = 0;
1727 int ret;
1728
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001729 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001730 check_pages_isolated_cb);
1731 if (ret < 0)
1732 offlined = (long)ret;
1733 return offlined;
1734}
1735
Lai Jiangshan09285af2012-12-12 13:52:04 -08001736#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001737/*
1738 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1739 * normal memory.
1740 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001741static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1742{
1743 return true;
1744}
Tang Chen79a4dce2012-12-18 14:23:24 -08001745#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001746/* ensure the node has NORMAL memory if it is still online */
1747static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1748{
1749 struct pglist_data *pgdat = zone->zone_pgdat;
1750 unsigned long present_pages = 0;
1751 enum zone_type zt;
1752
1753 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1754 present_pages += pgdat->node_zones[zt].present_pages;
1755
1756 if (present_pages > nr_pages)
1757 return true;
1758
1759 present_pages = 0;
1760 for (; zt <= ZONE_MOVABLE; zt++)
1761 present_pages += pgdat->node_zones[zt].present_pages;
1762
1763 /*
1764 * we can't offline the last normal memory until all
1765 * higher memory is offlined.
1766 */
1767 return present_pages == 0;
1768}
Tang Chen79a4dce2012-12-18 14:23:24 -08001769#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001770
Tang Chenc5320922013-11-12 15:08:10 -08001771static int __init cmdline_parse_movable_node(char *p)
1772{
1773#ifdef CONFIG_MOVABLE_NODE
1774 /*
1775 * Memory used by the kernel cannot be hot-removed because Linux
1776 * cannot migrate the kernel pages. When memory hotplug is
1777 * enabled, we should prevent memblock from allocating memory
1778 * for the kernel.
1779 *
1780 * ACPI SRAT records all hotpluggable memory ranges. But before
1781 * SRAT is parsed, we don't know about it.
1782 *
1783 * The kernel image is loaded into memory at very early time. We
1784 * cannot prevent this anyway. So on NUMA system, we set any
1785 * node the kernel resides in as un-hotpluggable.
1786 *
1787 * Since on modern servers, one node could have double-digit
1788 * gigabytes memory, we can assume the memory around the kernel
1789 * image is also un-hotpluggable. So before SRAT is parsed, just
1790 * allocate memory near the kernel image to try the best to keep
1791 * the kernel away from hotpluggable memory.
1792 */
1793 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001794 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001795#else
1796 pr_warn("movable_node option not supported\n");
1797#endif
1798 return 0;
1799}
1800early_param("movable_node", cmdline_parse_movable_node);
1801
Lai Jiangshand9713672012-12-11 16:01:03 -08001802/* check which state of node_states will be changed when offline memory */
1803static void node_states_check_changes_offline(unsigned long nr_pages,
1804 struct zone *zone, struct memory_notify *arg)
1805{
1806 struct pglist_data *pgdat = zone->zone_pgdat;
1807 unsigned long present_pages = 0;
1808 enum zone_type zt, zone_last = ZONE_NORMAL;
1809
1810 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001811 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1812 * contains nodes which have zones of 0...ZONE_NORMAL,
1813 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001814 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001815 * If we don't have HIGHMEM nor movable node,
1816 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1817 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001818 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001819 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001820 zone_last = ZONE_MOVABLE;
1821
1822 /*
1823 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1824 * If the memory to be offline is in a zone of 0...zone_last,
1825 * and it is the last present memory, 0...zone_last will
1826 * become empty after offline , thus we can determind we will
1827 * need to clear the node from node_states[N_NORMAL_MEMORY].
1828 */
1829 for (zt = 0; zt <= zone_last; zt++)
1830 present_pages += pgdat->node_zones[zt].present_pages;
1831 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1832 arg->status_change_nid_normal = zone_to_nid(zone);
1833 else
1834 arg->status_change_nid_normal = -1;
1835
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001836#ifdef CONFIG_HIGHMEM
1837 /*
1838 * If we have movable node, node_states[N_HIGH_MEMORY]
1839 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1840 * set zone_last to ZONE_HIGHMEM.
1841 *
1842 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1843 * contains nodes which have zones of 0...ZONE_MOVABLE,
1844 * set zone_last to ZONE_MOVABLE.
1845 */
1846 zone_last = ZONE_HIGHMEM;
1847 if (N_MEMORY == N_HIGH_MEMORY)
1848 zone_last = ZONE_MOVABLE;
1849
1850 for (; zt <= zone_last; zt++)
1851 present_pages += pgdat->node_zones[zt].present_pages;
1852 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1853 arg->status_change_nid_high = zone_to_nid(zone);
1854 else
1855 arg->status_change_nid_high = -1;
1856#else
1857 arg->status_change_nid_high = arg->status_change_nid_normal;
1858#endif
1859
Lai Jiangshand9713672012-12-11 16:01:03 -08001860 /*
1861 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1862 */
1863 zone_last = ZONE_MOVABLE;
1864
1865 /*
1866 * check whether node_states[N_HIGH_MEMORY] will be changed
1867 * If we try to offline the last present @nr_pages from the node,
1868 * we can determind we will need to clear the node from
1869 * node_states[N_HIGH_MEMORY].
1870 */
1871 for (; zt <= zone_last; zt++)
1872 present_pages += pgdat->node_zones[zt].present_pages;
1873 if (nr_pages >= present_pages)
1874 arg->status_change_nid = zone_to_nid(zone);
1875 else
1876 arg->status_change_nid = -1;
1877}
1878
1879static void node_states_clear_node(int node, struct memory_notify *arg)
1880{
1881 if (arg->status_change_nid_normal >= 0)
1882 node_clear_state(node, N_NORMAL_MEMORY);
1883
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001884 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1885 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001886 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001887
1888 if ((N_MEMORY != N_HIGH_MEMORY) &&
1889 (arg->status_change_nid >= 0))
1890 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001891}
1892
Wen Congyanga16cee12012-10-08 16:33:58 -07001893static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001894 unsigned long end_pfn, unsigned long timeout)
1895{
1896 unsigned long pfn, nr_pages, expire;
1897 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001898 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001899 unsigned long flags;
Toshi Kani6cb04972017-02-03 13:13:23 -08001900 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001901 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001902 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001903
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001904 /* at least, alignment against pageblock is necessary */
1905 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1906 return -EINVAL;
1907 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1908 return -EINVAL;
1909 /* This makes hotplug much easier...and readable.
1910 we assume this for now. .*/
Toshi Kani6cb04972017-02-03 13:13:23 -08001911 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001912 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001913
Toshi Kani6cb04972017-02-03 13:13:23 -08001914 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001915 node = zone_to_nid(zone);
1916 nr_pages = end_pfn - start_pfn;
1917
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001918 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001919 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001920
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001921 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001922 ret = start_isolate_page_range(start_pfn, end_pfn,
1923 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001924 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001925 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001926
1927 arg.start_pfn = start_pfn;
1928 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001929 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001930
1931 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1932 ret = notifier_to_errno(ret);
1933 if (ret)
1934 goto failed_removal;
1935
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001936 pfn = start_pfn;
1937 expire = jiffies + timeout;
1938 drain = 0;
1939 retry_max = 5;
1940repeat:
1941 /* start memory hot removal */
1942 ret = -EAGAIN;
1943 if (time_after(jiffies, expire))
1944 goto failed_removal;
1945 ret = -EINTR;
1946 if (signal_pending(current))
1947 goto failed_removal;
1948 ret = 0;
1949 if (drain) {
1950 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001951 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001952 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001953 }
1954
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001955 pfn = scan_movable_pages(start_pfn, end_pfn);
1956 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001957 ret = do_migrate_range(pfn, end_pfn);
1958 if (!ret) {
1959 drain = 1;
1960 goto repeat;
1961 } else {
1962 if (ret < 0)
1963 if (--retry_max == 0)
1964 goto failed_removal;
1965 yield();
1966 drain = 1;
1967 goto repeat;
1968 }
1969 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001970 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001971 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001972 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001973 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001974 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001975 /*
1976 * dissolve free hugepages in the memory block before doing offlining
1977 * actually in order to make hugetlbfs's object counting consistent.
1978 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001979 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1980 if (ret)
1981 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001982 /* check again */
1983 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1984 if (offlined_pages < 0) {
1985 ret = -EBUSY;
1986 goto failed_removal;
1987 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001988 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001989 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001990 We cannot do rollback at this point. */
1991 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001992 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001993 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001994 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001995 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001996 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001997
1998 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001999 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07002000 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07002001
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07002002 init_per_zone_wmark_min();
2003
Xishi Qiu1e8537b2012-10-08 16:31:51 -07002004 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07002005 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07002006 mutex_lock(&zonelists_mutex);
2007 build_all_zonelists(NULL, NULL);
2008 mutex_unlock(&zonelists_mutex);
2009 } else
2010 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07002011
Lai Jiangshand9713672012-12-11 16:01:03 -08002012 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002013 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08002014 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002015 kcompactd_stop(node);
2016 }
Minchan Kimbce73942009-06-16 15:32:50 -07002017
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002018 vm_total_pages = nr_free_pagecache_pages();
2019 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07002020
2021 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002022 return 0;
2023
2024failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07002025 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
2026 (unsigned long long) start_pfn << PAGE_SHIFT,
2027 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07002028 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002029 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02002030 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07002031 return ret;
2032}
Badari Pulavarty71088782008-10-18 20:25:58 -07002033
David Rientjes30467e02015-04-14 15:45:11 -07002034/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07002035int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
2036{
2037 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
2038}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002039#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07002040
Wen Congyangbbc76be2013-02-22 16:32:54 -08002041/**
2042 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2043 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07002044 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08002045 * @arg: argument passed to func
2046 * @func: callback for each memory section walked
2047 *
2048 * This function walks through all present mem sections in range
2049 * [start_pfn, end_pfn) and call func on each mem section.
2050 *
2051 * Returns the return value of func.
2052 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002053int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08002054 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07002055{
Wen Congyange90bdb72012-10-08 16:34:01 -07002056 struct memory_block *mem = NULL;
2057 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07002058 unsigned long pfn, section_nr;
2059 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07002060
Wen Congyange90bdb72012-10-08 16:34:01 -07002061 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2062 section_nr = pfn_to_section_nr(pfn);
2063 if (!present_section_nr(section_nr))
2064 continue;
2065
2066 section = __nr_to_section(section_nr);
2067 /* same memblock? */
2068 if (mem)
2069 if ((section_nr >= mem->start_section_nr) &&
2070 (section_nr <= mem->end_section_nr))
2071 continue;
2072
2073 mem = find_memory_block_hinted(section, mem);
2074 if (!mem)
2075 continue;
2076
Wen Congyangbbc76be2013-02-22 16:32:54 -08002077 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07002078 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08002079 kobject_put(&mem->dev.kobj);
2080 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07002081 }
2082 }
2083
2084 if (mem)
2085 kobject_put(&mem->dev.kobj);
2086
Wen Congyangbbc76be2013-02-22 16:32:54 -08002087 return 0;
2088}
2089
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02002090#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08002091static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002092{
2093 int ret = !is_memblock_offlined(mem);
2094
Randy Dunlap349daa02013-04-29 15:08:49 -07002095 if (unlikely(ret)) {
2096 phys_addr_t beginpa, endpa;
2097
2098 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2099 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a025f02016-03-17 14:19:47 -07002100 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07002101 &beginpa, &endpa);
2102 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08002103
2104 return ret;
2105}
2106
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002107static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08002108{
Tang Chen60a5a192013-02-22 16:33:14 -08002109 int cpu;
2110
2111 for_each_present_cpu(cpu) {
2112 if (cpu_to_node(cpu) == pgdat->node_id)
2113 /*
2114 * the cpu on this node isn't removed, and we can't
2115 * offline this node.
2116 */
2117 return -EBUSY;
2118 }
2119
2120 return 0;
2121}
2122
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002123static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002124{
2125#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002126 int cpu;
2127
2128 for_each_possible_cpu(cpu)
2129 if (cpu_to_node(cpu) == pgdat->node_id)
2130 numa_clear_node(cpu);
2131#endif
2132}
2133
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002134static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002135{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002136 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002137
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002138 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002139 if (ret)
2140 return ret;
2141
2142 /*
2143 * the node will be offlined when we come here, so we can clear
2144 * the cpu_to_node() now.
2145 */
2146
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002147 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002148 return 0;
2149}
2150
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002151/**
2152 * try_offline_node
2153 *
2154 * Offline a node if all memory sections and cpus of the node are removed.
2155 *
2156 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2157 * and online/offline operations before this call.
2158 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002159void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002160{
Wen Congyangd822b862013-02-22 16:33:16 -08002161 pg_data_t *pgdat = NODE_DATA(nid);
2162 unsigned long start_pfn = pgdat->node_start_pfn;
2163 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002164 unsigned long pfn;
2165
2166 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2167 unsigned long section_nr = pfn_to_section_nr(pfn);
2168
2169 if (!present_section_nr(section_nr))
2170 continue;
2171
2172 if (pfn_to_nid(pfn) != nid)
2173 continue;
2174
2175 /*
2176 * some memory sections of this node are not removed, and we
2177 * can't offline node now.
2178 */
2179 return;
2180 }
2181
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002182 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002183 return;
2184
2185 /*
2186 * all memory/cpu of this node are removed, we can offline this
2187 * node now.
2188 */
2189 node_set_offline(nid);
2190 unregister_one_node(nid);
2191}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002192EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002193
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002194/**
2195 * remove_memory
2196 *
2197 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2198 * and online/offline operations before this call, as required by
2199 * try_offline_node().
2200 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002201void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002202{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002203 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002204
Toshi Kani27356f52013-09-11 14:21:49 -07002205 BUG_ON(check_hotplug_memory_range(start, size));
2206
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002207 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002208
2209 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002210 * All memory blocks must be offlined before removing memory. Check
2211 * whether all memory blocks in question are offline and trigger a BUG()
2212 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002213 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002214 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002215 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002216 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002217 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002218
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002219 /* remove memmap entry */
2220 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002221 memblock_free(start, size);
2222 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002223
Wen Congyang24d335c2013-02-22 16:32:58 -08002224 arch_remove_memory(start, size);
2225
Tang Chen60a5a192013-02-22 16:33:14 -08002226 try_offline_node(nid);
2227
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002228 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002229}
Badari Pulavarty71088782008-10-18 20:25:58 -07002230EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002231#endif /* CONFIG_MEMORY_HOTREMOVE */