blob: fcafbfcff044e9e42a0e09e641741ee1178fd0c1 [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#include <linux/stddef.h>
8#include <linux/mm.h>
9#include <linux/swap.h>
10#include <linux/interrupt.h>
11#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070012#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070014#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070015#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/slab.h>
17#include <linux/sysctl.h>
18#include <linux/cpu.h>
19#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080020#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070021#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080032#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070033#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080034#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080035#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070036#include <linux/compaction.h>
Dave Hansen3947be12005-10-29 18:16:54 -070037
38#include <asm/tlbflush.h>
39
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030040#include "internal.h"
41
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070042/*
43 * online_page_callback contains pointer to current page onlining function.
44 * Initially it is generic_online_page(). If it is required it could be
45 * changed by calling set_online_page_callback() for callback registration
46 * and restore_online_page_callback() for generic callback restore.
47 */
48
49static void generic_online_page(struct page *page);
50
51static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070052static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070053
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070054/* The same as the cpu_hotplug lock, but for memory hotplug. */
55static struct {
56 struct task_struct *active_writer;
57 struct mutex lock; /* Synchronizes accesses to refcount, */
58 /*
59 * Also blocks the new readers during
60 * an ongoing mem hotplug operation.
61 */
62 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080063
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070064#ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
66#endif
67} mem_hotplug = {
68 .active_writer = NULL,
69 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
70 .refcount = 0,
71#ifdef CONFIG_DEBUG_LOCK_ALLOC
72 .dep_map = {.name = "mem_hotplug.lock" },
73#endif
74};
75
76/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
77#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
78#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
79#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
80
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070081#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070082bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070083#else
84bool memhp_auto_online = true;
85#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070086EXPORT_SYMBOL_GPL(memhp_auto_online);
87
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070088void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080089{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070090 might_sleep();
91 if (mem_hotplug.active_writer == current)
92 return;
93 memhp_lock_acquire_read();
94 mutex_lock(&mem_hotplug.lock);
95 mem_hotplug.refcount++;
96 mutex_unlock(&mem_hotplug.lock);
97
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080098}
99
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700100void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800101{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700102 if (mem_hotplug.active_writer == current)
103 return;
104 mutex_lock(&mem_hotplug.lock);
105
106 if (WARN_ON(!mem_hotplug.refcount))
107 mem_hotplug.refcount++; /* try to fix things up */
108
109 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
110 wake_up_process(mem_hotplug.active_writer);
111 mutex_unlock(&mem_hotplug.lock);
112 memhp_lock_release();
113
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800114}
115
David Rientjes30467e02015-04-14 15:45:11 -0700116void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700117{
118 mem_hotplug.active_writer = current;
119
120 memhp_lock_acquire();
121 for (;;) {
122 mutex_lock(&mem_hotplug.lock);
123 if (likely(!mem_hotplug.refcount))
124 break;
125 __set_current_state(TASK_UNINTERRUPTIBLE);
126 mutex_unlock(&mem_hotplug.lock);
127 schedule();
128 }
129}
130
David Rientjes30467e02015-04-14 15:45:11 -0700131void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700132{
133 mem_hotplug.active_writer = NULL;
134 mutex_unlock(&mem_hotplug.lock);
135 memhp_lock_release();
136}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800137
Keith Mannthey45e0b782006-09-30 23:27:09 -0700138/* add this memory to iomem resource */
139static struct resource *register_memory_resource(u64 start, u64 size)
140{
141 struct resource *res;
142 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800143 if (!res)
144 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700145
146 res->name = "System RAM";
147 res->start = start;
148 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100149 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700150 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700151 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700152 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800153 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700154 }
155 return res;
156}
157
158static void release_memory_resource(struct resource *res)
159{
160 if (!res)
161 return;
162 release_resource(res);
163 kfree(res);
164 return;
165}
166
Keith Mannthey53947022006-09-30 23:27:08 -0700167#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800168void get_page_bootmem(unsigned long info, struct page *page,
169 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700170{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800171 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700172 SetPagePrivate(page);
173 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700174 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700175}
176
Jiang Liu170a5a72013-07-03 15:03:17 -0700177void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700178{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800179 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700180
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800181 type = (unsigned long) page->lru.next;
182 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
183 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700184
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700185 if (page_ref_dec_return(page) == 1) {
Yasunori Goto04753272008-04-28 02:13:31 -0700186 ClearPagePrivate(page);
187 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800188 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700189 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700190 }
Yasunori Goto04753272008-04-28 02:13:31 -0700191}
192
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800193#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
194#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700195static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700196{
197 unsigned long *usemap, mapsize, section_nr, i;
198 struct mem_section *ms;
199 struct page *page, *memmap;
200
Yasunori Goto04753272008-04-28 02:13:31 -0700201 section_nr = pfn_to_section_nr(start_pfn);
202 ms = __nr_to_section(section_nr);
203
204 /* Get section's memmap address */
205 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
206
207 /*
208 * Get page for the memmap's phys address
209 * XXX: need more consideration for sparse_vmemmap...
210 */
211 page = virt_to_page(memmap);
212 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
213 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
214
215 /* remember memmap's page */
216 for (i = 0; i < mapsize; i++, page++)
217 get_page_bootmem(section_nr, page, SECTION_INFO);
218
219 usemap = __nr_to_section(section_nr)->pageblock_flags;
220 page = virt_to_page(usemap);
221
222 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
223
224 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700225 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700226
227}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800228#else /* CONFIG_SPARSEMEM_VMEMMAP */
229static void register_page_bootmem_info_section(unsigned long start_pfn)
230{
231 unsigned long *usemap, mapsize, section_nr, i;
232 struct mem_section *ms;
233 struct page *page, *memmap;
234
235 if (!pfn_valid(start_pfn))
236 return;
237
238 section_nr = pfn_to_section_nr(start_pfn);
239 ms = __nr_to_section(section_nr);
240
241 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
242
243 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
244
245 usemap = __nr_to_section(section_nr)->pageblock_flags;
246 page = virt_to_page(usemap);
247
248 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
249
250 for (i = 0; i < mapsize; i++, page++)
251 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
252}
253#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700254
255void register_page_bootmem_info_node(struct pglist_data *pgdat)
256{
257 unsigned long i, pfn, end_pfn, nr_pages;
258 int node = pgdat->node_id;
259 struct page *page;
260 struct zone *zone;
261
262 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
263 page = virt_to_page(pgdat);
264
265 for (i = 0; i < nr_pages; i++, page++)
266 get_page_bootmem(node, page, NODE_INFO);
267
268 zone = &pgdat->node_zones[0];
269 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700270 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700271 nr_pages = zone->wait_table_hash_nr_entries
272 * sizeof(wait_queue_head_t);
273 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
274 page = virt_to_page(zone->wait_table);
275
276 for (i = 0; i < nr_pages; i++, page++)
277 get_page_bootmem(node, page, NODE_INFO);
278 }
279 }
280
281 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800282 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700283
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700284 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700285 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
286 /*
287 * Some platforms can assign the same pfn to multiple nodes - on
288 * node0 as well as nodeN. To avoid registering a pfn against
289 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700290 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700291 */
292 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
293 register_page_bootmem_info_section(pfn);
294 }
Yasunori Goto04753272008-04-28 02:13:31 -0700295}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800296#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700297
Fabian Frederickf2765402014-08-06 16:04:57 -0700298static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
299 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700300{
301 unsigned long old_zone_end_pfn;
302
303 zone_span_writelock(zone);
304
Xishi Qiuc33bc312013-09-11 14:21:44 -0700305 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700306 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700307 zone->zone_start_pfn = start_pfn;
308
309 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
310 zone->zone_start_pfn;
311
312 zone_span_writeunlock(zone);
313}
314
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800315static void resize_zone(struct zone *zone, unsigned long start_pfn,
316 unsigned long end_pfn)
317{
318 zone_span_writelock(zone);
319
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800320 if (end_pfn - start_pfn) {
321 zone->zone_start_pfn = start_pfn;
322 zone->spanned_pages = end_pfn - start_pfn;
323 } else {
324 /*
325 * make it consist as free_area_init_core(),
326 * if spanned_pages = 0, then keep start_pfn = 0
327 */
328 zone->zone_start_pfn = 0;
329 zone->spanned_pages = 0;
330 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800331
332 zone_span_writeunlock(zone);
333}
334
335static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
336 unsigned long end_pfn)
337{
338 enum zone_type zid = zone_idx(zone);
339 int nid = zone->zone_pgdat->node_id;
340 unsigned long pfn;
341
342 for (pfn = start_pfn; pfn < end_pfn; pfn++)
343 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
344}
345
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800346/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800347 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800348static int __ref ensure_zone_is_initialized(struct zone *zone,
349 unsigned long start_pfn, unsigned long num_pages)
350{
351 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800352 return init_currently_empty_zone(zone, start_pfn, num_pages);
353
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800354 return 0;
355}
356
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800357static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800358 unsigned long start_pfn, unsigned long end_pfn)
359{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800360 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800361 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800362 unsigned long z1_start_pfn;
363
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800364 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
365 if (ret)
366 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800367
368 pgdat_resize_lock(z1->zone_pgdat, &flags);
369
370 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800371 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800372 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700373 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800374 if (start_pfn > z2->zone_start_pfn)
375 goto out_fail;
376 /* must included/overlap */
377 if (end_pfn <= z2->zone_start_pfn)
378 goto out_fail;
379
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800380 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700381 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800382 z1_start_pfn = z1->zone_start_pfn;
383 else
384 z1_start_pfn = start_pfn;
385
386 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800387 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800388
389 pgdat_resize_unlock(z1->zone_pgdat, &flags);
390
391 fix_zone_id(z1, start_pfn, end_pfn);
392
393 return 0;
394out_fail:
395 pgdat_resize_unlock(z1->zone_pgdat, &flags);
396 return -1;
397}
398
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800399static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800400 unsigned long start_pfn, unsigned long end_pfn)
401{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800402 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800403 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800404 unsigned long z2_end_pfn;
405
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800406 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
407 if (ret)
408 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800409
410 pgdat_resize_lock(z1->zone_pgdat, &flags);
411
412 /* can't move pfns which are lower than @z1 */
413 if (z1->zone_start_pfn > start_pfn)
414 goto out_fail;
415 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800416 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800417 goto out_fail;
418 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800419 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800420 goto out_fail;
421
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800422 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700423 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800424 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800425 else
426 z2_end_pfn = end_pfn;
427
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800428 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800429 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800430
431 pgdat_resize_unlock(z1->zone_pgdat, &flags);
432
433 fix_zone_id(z2, start_pfn, end_pfn);
434
435 return 0;
436out_fail:
437 pgdat_resize_unlock(z1->zone_pgdat, &flags);
438 return -1;
439}
440
Fabian Frederickf2765402014-08-06 16:04:57 -0700441static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
442 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700443{
Xishi Qiu83285c72013-11-12 15:07:19 -0800444 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700445
Tang Chen712cd382012-12-11 16:01:07 -0800446 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700447 pgdat->node_start_pfn = start_pfn;
448
449 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
450 pgdat->node_start_pfn;
451}
452
Al Viro31168482008-11-22 17:33:24 +0000453static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700454{
455 struct pglist_data *pgdat = zone->zone_pgdat;
456 int nr_pages = PAGES_PER_SECTION;
457 int nid = pgdat->node_id;
458 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700459 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800460 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700461
462 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800463 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
464 if (ret)
465 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700466
Heiko Carstens76cdd582008-05-14 16:05:52 -0700467 pgdat_resize_lock(zone->zone_pgdat, &flags);
468 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
469 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
470 phys_start_pfn + nr_pages);
471 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800472 memmap_init_zone(nr_pages, nid, zone_type,
473 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700474
475 /* online_page_range is called later and expects pages reserved */
476 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
477 if (!pfn_valid(pfn))
478 continue;
479
480 SetPageReserved(pfn_to_page(pfn));
481 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700482 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700483}
484
Gary Hadec04fc582009-01-06 14:39:14 -0800485static int __meminit __add_section(int nid, struct zone *zone,
486 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700487{
Dave Hansen3947be12005-10-29 18:16:54 -0700488 int ret;
489
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700490 if (pfn_valid(phys_start_pfn))
491 return -EEXIST;
492
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800493 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700494
495 if (ret < 0)
496 return ret;
497
Yasunori Goto718127c2006-06-23 02:03:10 -0700498 ret = __add_zone(zone, phys_start_pfn);
499
500 if (ret < 0)
501 return ret;
502
Gary Hadec04fc582009-01-06 14:39:14 -0800503 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700504}
505
David Rientjes4edd7ce2013-04-29 15:08:22 -0700506/*
507 * Reasonably generic function for adding memory. It is
508 * expected that archs that support memory hotplug will
509 * call this function after deciding the zone to which to
510 * add the new pages.
511 */
512int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
513 unsigned long nr_pages)
514{
515 unsigned long i;
516 int err = 0;
517 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800518 struct vmem_altmap *altmap;
519
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700520 clear_zone_contiguous(zone);
521
David Rientjes4edd7ce2013-04-29 15:08:22 -0700522 /* during initialize mem_map, align hot-added range to section */
523 start_sec = pfn_to_section_nr(phys_start_pfn);
524 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
525
Dan Williams4b94ffd2016-01-15 16:56:22 -0800526 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
527 if (altmap) {
528 /*
529 * Validate altmap is within bounds of the total request
530 */
531 if (altmap->base_pfn != phys_start_pfn
532 || vmem_altmap_offset(altmap) > nr_pages) {
533 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700534 err = -EINVAL;
535 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800536 }
537 altmap->alloc = 0;
538 }
539
David Rientjes4edd7ce2013-04-29 15:08:22 -0700540 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700541 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700542
543 /*
544 * EEXIST is finally dealt with by ioresource collision
545 * check. see add_memory() => register_memory_resource()
546 * Warning will be printed if there is collision.
547 */
548 if (err && (err != -EEXIST))
549 break;
550 err = 0;
551 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700552 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700553out:
554 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700555 return err;
556}
557EXPORT_SYMBOL_GPL(__add_pages);
558
559#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800560/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
561static int find_smallest_section_pfn(int nid, struct zone *zone,
562 unsigned long start_pfn,
563 unsigned long end_pfn)
564{
565 struct mem_section *ms;
566
567 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
568 ms = __pfn_to_section(start_pfn);
569
570 if (unlikely(!valid_section(ms)))
571 continue;
572
573 if (unlikely(pfn_to_nid(start_pfn) != nid))
574 continue;
575
576 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
577 continue;
578
579 return start_pfn;
580 }
581
582 return 0;
583}
584
585/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
586static int find_biggest_section_pfn(int nid, struct zone *zone,
587 unsigned long start_pfn,
588 unsigned long end_pfn)
589{
590 struct mem_section *ms;
591 unsigned long pfn;
592
593 /* pfn is the end pfn of a memory section. */
594 pfn = end_pfn - 1;
595 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
596 ms = __pfn_to_section(pfn);
597
598 if (unlikely(!valid_section(ms)))
599 continue;
600
601 if (unlikely(pfn_to_nid(pfn) != nid))
602 continue;
603
604 if (zone && zone != page_zone(pfn_to_page(pfn)))
605 continue;
606
607 return pfn;
608 }
609
610 return 0;
611}
612
613static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
614 unsigned long end_pfn)
615{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700616 unsigned long zone_start_pfn = zone->zone_start_pfn;
617 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
618 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800619 unsigned long pfn;
620 struct mem_section *ms;
621 int nid = zone_to_nid(zone);
622
623 zone_span_writelock(zone);
624 if (zone_start_pfn == start_pfn) {
625 /*
626 * If the section is smallest section in the zone, it need
627 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
628 * In this case, we find second smallest valid mem_section
629 * for shrinking zone.
630 */
631 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
632 zone_end_pfn);
633 if (pfn) {
634 zone->zone_start_pfn = pfn;
635 zone->spanned_pages = zone_end_pfn - pfn;
636 }
637 } else if (zone_end_pfn == end_pfn) {
638 /*
639 * If the section is biggest section in the zone, it need
640 * shrink zone->spanned_pages.
641 * In this case, we find second biggest valid mem_section for
642 * shrinking zone.
643 */
644 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
645 start_pfn);
646 if (pfn)
647 zone->spanned_pages = pfn - zone_start_pfn + 1;
648 }
649
650 /*
651 * The section is not biggest or smallest mem_section in the zone, it
652 * only creates a hole in the zone. So in this case, we need not
653 * change the zone. But perhaps, the zone has only hole data. Thus
654 * it check the zone has only hole or not.
655 */
656 pfn = zone_start_pfn;
657 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
658 ms = __pfn_to_section(pfn);
659
660 if (unlikely(!valid_section(ms)))
661 continue;
662
663 if (page_zone(pfn_to_page(pfn)) != zone)
664 continue;
665
666 /* If the section is current section, it continues the loop */
667 if (start_pfn == pfn)
668 continue;
669
670 /* If we find valid section, we have nothing to do */
671 zone_span_writeunlock(zone);
672 return;
673 }
674
675 /* The zone has no valid section */
676 zone->zone_start_pfn = 0;
677 zone->spanned_pages = 0;
678 zone_span_writeunlock(zone);
679}
680
681static void shrink_pgdat_span(struct pglist_data *pgdat,
682 unsigned long start_pfn, unsigned long end_pfn)
683{
Xishi Qiu83285c72013-11-12 15:07:19 -0800684 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
685 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
686 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800687 unsigned long pfn;
688 struct mem_section *ms;
689 int nid = pgdat->node_id;
690
691 if (pgdat_start_pfn == start_pfn) {
692 /*
693 * If the section is smallest section in the pgdat, it need
694 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
695 * In this case, we find second smallest valid mem_section
696 * for shrinking zone.
697 */
698 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
699 pgdat_end_pfn);
700 if (pfn) {
701 pgdat->node_start_pfn = pfn;
702 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
703 }
704 } else if (pgdat_end_pfn == end_pfn) {
705 /*
706 * If the section is biggest section in the pgdat, it need
707 * shrink pgdat->node_spanned_pages.
708 * In this case, we find second biggest valid mem_section for
709 * shrinking zone.
710 */
711 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
712 start_pfn);
713 if (pfn)
714 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
715 }
716
717 /*
718 * If the section is not biggest or smallest mem_section in the pgdat,
719 * it only creates a hole in the pgdat. So in this case, we need not
720 * change the pgdat.
721 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
722 * has only hole or not.
723 */
724 pfn = pgdat_start_pfn;
725 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
726 ms = __pfn_to_section(pfn);
727
728 if (unlikely(!valid_section(ms)))
729 continue;
730
731 if (pfn_to_nid(pfn) != nid)
732 continue;
733
734 /* If the section is current section, it continues the loop */
735 if (start_pfn == pfn)
736 continue;
737
738 /* If we find valid section, we have nothing to do */
739 return;
740 }
741
742 /* The pgdat has no valid section */
743 pgdat->node_start_pfn = 0;
744 pgdat->node_spanned_pages = 0;
745}
746
747static void __remove_zone(struct zone *zone, unsigned long start_pfn)
748{
749 struct pglist_data *pgdat = zone->zone_pgdat;
750 int nr_pages = PAGES_PER_SECTION;
751 int zone_type;
752 unsigned long flags;
753
754 zone_type = zone - pgdat->node_zones;
755
756 pgdat_resize_lock(zone->zone_pgdat, &flags);
757 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
758 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
759 pgdat_resize_unlock(zone->zone_pgdat, &flags);
760}
761
Dan Williams4b94ffd2016-01-15 16:56:22 -0800762static int __remove_section(struct zone *zone, struct mem_section *ms,
763 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700764{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800765 unsigned long start_pfn;
766 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700767 int ret = -EINVAL;
768
769 if (!valid_section(ms))
770 return ret;
771
772 ret = unregister_memory_section(ms);
773 if (ret)
774 return ret;
775
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800776 scn_nr = __section_nr(ms);
777 start_pfn = section_nr_to_pfn(scn_nr);
778 __remove_zone(zone, start_pfn);
779
Dan Williams4b94ffd2016-01-15 16:56:22 -0800780 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700781 return 0;
782}
783
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700784/**
785 * __remove_pages() - remove sections of pages from a zone
786 * @zone: zone from which pages need to be removed
787 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
788 * @nr_pages: number of pages to remove (must be multiple of section size)
789 *
790 * Generic helper function to remove section mappings and sysfs entries
791 * for the section of the memory we are removing. Caller needs to make
792 * sure that pages are marked reserved and zones are adjust properly by
793 * calling offline_pages().
794 */
795int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
796 unsigned long nr_pages)
797{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700798 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800799 unsigned long map_offset = 0;
800 int sections_to_remove, ret = 0;
801
802 /* In the ZONE_DEVICE case device driver owns the memory region */
803 if (is_dev_zone(zone)) {
804 struct page *page = pfn_to_page(phys_start_pfn);
805 struct vmem_altmap *altmap;
806
807 altmap = to_vmem_altmap((unsigned long) page);
808 if (altmap)
809 map_offset = vmem_altmap_offset(altmap);
810 } else {
811 resource_size_t start, size;
812
813 start = phys_start_pfn << PAGE_SHIFT;
814 size = nr_pages * PAGE_SIZE;
815
816 ret = release_mem_region_adjustable(&iomem_resource, start,
817 size);
818 if (ret) {
819 resource_size_t endres = start + size - 1;
820
821 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
822 &start, &endres, ret);
823 }
824 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700825
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700826 clear_zone_contiguous(zone);
827
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700828 /*
829 * We can only remove entire sections
830 */
831 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
832 BUG_ON(nr_pages % PAGES_PER_SECTION);
833
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700834 sections_to_remove = nr_pages / PAGES_PER_SECTION;
835 for (i = 0; i < sections_to_remove; i++) {
836 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800837
838 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
839 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700840 if (ret)
841 break;
842 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700843
844 set_zone_contiguous(zone);
845
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700846 return ret;
847}
848EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700849#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700850
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700851int set_online_page_callback(online_page_callback_t callback)
852{
853 int rc = -EINVAL;
854
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700855 get_online_mems();
856 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700857
858 if (online_page_callback == generic_online_page) {
859 online_page_callback = callback;
860 rc = 0;
861 }
862
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700863 mutex_unlock(&online_page_callback_lock);
864 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700865
866 return rc;
867}
868EXPORT_SYMBOL_GPL(set_online_page_callback);
869
870int restore_online_page_callback(online_page_callback_t callback)
871{
872 int rc = -EINVAL;
873
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700874 get_online_mems();
875 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700876
877 if (online_page_callback == callback) {
878 online_page_callback = generic_online_page;
879 rc = 0;
880 }
881
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700882 mutex_unlock(&online_page_callback_lock);
883 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700884
885 return rc;
886}
887EXPORT_SYMBOL_GPL(restore_online_page_callback);
888
889void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700890{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700891}
892EXPORT_SYMBOL_GPL(__online_page_set_limits);
893
894void __online_page_increment_counters(struct page *page)
895{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700896 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700897}
898EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700899
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700900void __online_page_free(struct page *page)
901{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700902 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700903}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700904EXPORT_SYMBOL_GPL(__online_page_free);
905
906static void generic_online_page(struct page *page)
907{
908 __online_page_set_limits(page);
909 __online_page_increment_counters(page);
910 __online_page_free(page);
911}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700912
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700913static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
914 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700915{
916 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700917 unsigned long onlined_pages = *(unsigned long *)arg;
918 struct page *page;
919 if (PageReserved(pfn_to_page(start_pfn)))
920 for (i = 0; i < nr_pages; i++) {
921 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700922 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700923 onlined_pages++;
924 }
925 *(unsigned long *)arg = onlined_pages;
926 return 0;
927}
928
Lai Jiangshan09285af2012-12-12 13:52:04 -0800929#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800930/*
931 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
932 * normal memory.
933 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800934static bool can_online_high_movable(struct zone *zone)
935{
936 return true;
937}
Tang Chen79a4dce2012-12-18 14:23:24 -0800938#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800939/* ensure every online node has NORMAL memory */
940static bool can_online_high_movable(struct zone *zone)
941{
942 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
943}
Tang Chen79a4dce2012-12-18 14:23:24 -0800944#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800945
Lai Jiangshand9713672012-12-11 16:01:03 -0800946/* check which state of node_states will be changed when online memory */
947static void node_states_check_changes_online(unsigned long nr_pages,
948 struct zone *zone, struct memory_notify *arg)
949{
950 int nid = zone_to_nid(zone);
951 enum zone_type zone_last = ZONE_NORMAL;
952
953 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800954 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
955 * contains nodes which have zones of 0...ZONE_NORMAL,
956 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800957 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800958 * If we don't have HIGHMEM nor movable node,
959 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
960 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800961 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800962 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800963 zone_last = ZONE_MOVABLE;
964
965 /*
966 * if the memory to be online is in a zone of 0...zone_last, and
967 * the zones of 0...zone_last don't have memory before online, we will
968 * need to set the node to node_states[N_NORMAL_MEMORY] after
969 * the memory is online.
970 */
971 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
972 arg->status_change_nid_normal = nid;
973 else
974 arg->status_change_nid_normal = -1;
975
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800976#ifdef CONFIG_HIGHMEM
977 /*
978 * If we have movable node, node_states[N_HIGH_MEMORY]
979 * contains nodes which have zones of 0...ZONE_HIGHMEM,
980 * set zone_last to ZONE_HIGHMEM.
981 *
982 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
983 * contains nodes which have zones of 0...ZONE_MOVABLE,
984 * set zone_last to ZONE_MOVABLE.
985 */
986 zone_last = ZONE_HIGHMEM;
987 if (N_MEMORY == N_HIGH_MEMORY)
988 zone_last = ZONE_MOVABLE;
989
990 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
991 arg->status_change_nid_high = nid;
992 else
993 arg->status_change_nid_high = -1;
994#else
995 arg->status_change_nid_high = arg->status_change_nid_normal;
996#endif
997
Lai Jiangshand9713672012-12-11 16:01:03 -0800998 /*
999 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001000 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -08001001 * is online.
1002 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001003 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -08001004 arg->status_change_nid = nid;
1005 else
1006 arg->status_change_nid = -1;
1007}
1008
1009static void node_states_set_node(int node, struct memory_notify *arg)
1010{
1011 if (arg->status_change_nid_normal >= 0)
1012 node_set_state(node, N_NORMAL_MEMORY);
1013
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001014 if (arg->status_change_nid_high >= 0)
1015 node_set_state(node, N_HIGH_MEMORY);
1016
1017 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001018}
1019
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001020
David Rientjes30467e02015-04-14 15:45:11 -07001021/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001022int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001023{
Cody P Schaferaa472282013-07-03 15:02:10 -07001024 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001025 unsigned long onlined_pages = 0;
1026 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001027 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001028 int nid;
1029 int ret;
1030 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -07001031
Lai Jiangshand9713672012-12-11 16:01:03 -08001032 /*
1033 * This doesn't need a lock to do pfn_to_page().
1034 * The section can't be removed here because of the
1035 * memory_block->state_mutex.
1036 */
1037 zone = page_zone(pfn_to_page(pfn));
1038
Tang Chen4f7c6b42014-08-06 16:05:13 -07001039 if ((zone_idx(zone) > ZONE_NORMAL ||
1040 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001041 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001042 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001043
Tang Chen4f7c6b42014-08-06 16:05:13 -07001044 if (online_type == MMOP_ONLINE_KERNEL &&
1045 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001046 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001047 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001048 }
Tang Chen4f7c6b42014-08-06 16:05:13 -07001049 if (online_type == MMOP_ONLINE_MOVABLE &&
1050 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001051 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001052 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001053 }
1054
1055 /* Previous code may changed the zone of the pfn range */
1056 zone = page_zone(pfn_to_page(pfn));
1057
Yasunori Goto7b78d332007-10-21 16:41:36 -07001058 arg.start_pfn = pfn;
1059 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001060 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001061
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001062 nid = zone_to_nid(zone);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001063
1064 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1065 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001066 if (ret)
1067 goto failed_addition;
1068
Dave Hansen3947be12005-10-29 18:16:54 -07001069 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001070 * If this zone is not populated, then it is not in zonelist.
1071 * This means the page allocator ignores this zone.
1072 * So, zonelist must be updated after online.
1073 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001074 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001075 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001076 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001077 build_all_zonelists(NULL, zone);
1078 }
Yasunori Goto68113782006-06-23 02:03:11 -07001079
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001080 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001081 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001082 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001083 if (need_zonelists_rebuild)
1084 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001085 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001086 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001087 }
1088
Dave Hansen3947be12005-10-29 18:16:54 -07001089 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001090
1091 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001092 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001093 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1094
Jiang Liu08dff7b2012-07-31 16:43:30 -07001095 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001096 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001097 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001098 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001099 else
1100 zone_pcp_update(zone);
1101 }
Dave Hansen3947be12005-10-29 18:16:54 -07001102
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001103 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001104
1105 init_per_zone_wmark_min();
1106
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001107 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001108 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001109 kcompactd_run(nid);
1110 }
Dave Hansen61b13992005-10-29 18:16:56 -07001111
Haicheng Li1f522502010-05-24 14:32:51 -07001112 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001113
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001114 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001115
1116 if (onlined_pages)
1117 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001118 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001119
1120failed_addition:
1121 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1122 (unsigned long long) pfn << PAGE_SHIFT,
1123 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1124 memory_notify(MEM_CANCEL_ONLINE, &arg);
1125 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001126}
Keith Mannthey53947022006-09-30 23:27:08 -07001127#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001128
Tang Chen0bd85422014-11-13 15:19:41 -08001129static void reset_node_present_pages(pg_data_t *pgdat)
1130{
1131 struct zone *z;
1132
1133 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1134 z->present_pages = 0;
1135
1136 pgdat->node_present_pages = 0;
1137}
1138
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001139/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1140static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001141{
1142 struct pglist_data *pgdat;
1143 unsigned long zones_size[MAX_NR_ZONES] = {0};
1144 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001145 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001146
Tang Chena1e565a2013-02-22 16:33:18 -08001147 pgdat = NODE_DATA(nid);
1148 if (!pgdat) {
1149 pgdat = arch_alloc_nodedata(nid);
1150 if (!pgdat)
1151 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001152
Tang Chena1e565a2013-02-22 16:33:18 -08001153 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001154 } else {
1155 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1156 pgdat->nr_zones = 0;
1157 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001158 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001159
1160 /* we can use NODE_DATA(nid) from here */
1161
1162 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001163 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001164
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001165 /*
1166 * The node we allocated has no zone fallback lists. For avoiding
1167 * to access not-initialized zonelist, build here.
1168 */
David Rientjesf957db42011-06-22 18:13:04 -07001169 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001170 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001171 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001172
Tang Chenf784a3f2014-11-13 15:19:39 -08001173 /*
1174 * zone->managed_pages is set to an approximate value in
1175 * free_area_init_core(), which will cause
1176 * /sys/device/system/node/nodeX/meminfo has wrong data.
1177 * So reset it to 0 before any memory is onlined.
1178 */
1179 reset_node_managed_pages(pgdat);
1180
Tang Chen0bd85422014-11-13 15:19:41 -08001181 /*
1182 * When memory is hot-added, all the memory is in offline state. So
1183 * clear all zones' present_pages because they will be updated in
1184 * online_pages() and offline_pages().
1185 */
1186 reset_node_present_pages(pgdat);
1187
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001188 return pgdat;
1189}
1190
1191static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1192{
1193 arch_refresh_nodedata(nid, NULL);
1194 arch_free_nodedata(pgdat);
1195 return;
1196}
1197
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001198
Toshi Kani01b0f192013-11-12 15:07:25 -08001199/**
1200 * try_online_node - online a node if offlined
1201 *
minskey guocf234222010-05-24 14:32:41 -07001202 * called by cpu_up() to online a node without onlined memory.
1203 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001204int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001205{
1206 pg_data_t *pgdat;
1207 int ret;
1208
Toshi Kani01b0f192013-11-12 15:07:25 -08001209 if (node_online(nid))
1210 return 0;
1211
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001212 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001213 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001214 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001215 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001216 ret = -ENOMEM;
1217 goto out;
1218 }
1219 node_set_online(nid);
1220 ret = register_one_node(nid);
1221 BUG_ON(ret);
1222
Toshi Kani01b0f192013-11-12 15:07:25 -08001223 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1224 mutex_lock(&zonelists_mutex);
1225 build_all_zonelists(NULL, NULL);
1226 mutex_unlock(&zonelists_mutex);
1227 }
1228
minskey guocf234222010-05-24 14:32:41 -07001229out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001230 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001231 return ret;
1232}
1233
Toshi Kani27356f52013-09-11 14:21:49 -07001234static int check_hotplug_memory_range(u64 start, u64 size)
1235{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001236 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001237 u64 nr_pages = size >> PAGE_SHIFT;
1238
1239 /* Memory range must be aligned with section */
1240 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1241 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1242 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1243 (unsigned long long)start,
1244 (unsigned long long)size);
1245 return -EINVAL;
1246 }
1247
1248 return 0;
1249}
1250
Wang Nan63264402014-08-06 16:07:36 -07001251/*
1252 * If movable zone has already been setup, newly added memory should be check.
1253 * If its address is higher than movable zone, it should be added as movable.
1254 * Without this check, movable zone may overlap with other zone.
1255 */
1256static int should_add_memory_movable(int nid, u64 start, u64 size)
1257{
1258 unsigned long start_pfn = start >> PAGE_SHIFT;
1259 pg_data_t *pgdat = NODE_DATA(nid);
1260 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1261
1262 if (zone_is_empty(movable_zone))
1263 return 0;
1264
1265 if (movable_zone->zone_start_pfn <= start_pfn)
1266 return 1;
1267
1268 return 0;
1269}
1270
Dan Williams033fbae2015-08-09 15:29:06 -04001271int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1272 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001273{
Dan Williams033fbae2015-08-09 15:29:06 -04001274#ifdef CONFIG_ZONE_DEVICE
1275 if (for_device)
1276 return ZONE_DEVICE;
1277#endif
Wang Nan63264402014-08-06 16:07:36 -07001278 if (should_add_memory_movable(nid, start, size))
1279 return ZONE_MOVABLE;
1280
1281 return zone_default;
1282}
1283
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001284static int online_memory_block(struct memory_block *mem, void *arg)
1285{
1286 return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1287}
1288
Al Viro31168482008-11-22 17:33:24 +00001289/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001290int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001291{
David Vrabel62cedb92015-06-25 16:35:49 +01001292 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001293 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001294 bool new_pgdat;
1295 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001296 int ret;
1297
David Vrabel62cedb92015-06-25 16:35:49 +01001298 start = res->start;
1299 size = resource_size(res);
1300
Toshi Kani27356f52013-09-11 14:21:49 -07001301 ret = check_hotplug_memory_range(start, size);
1302 if (ret)
1303 return ret;
1304
Tang Chena1e565a2013-02-22 16:33:18 -08001305 { /* Stupid hack to suppress address-never-null warning */
1306 void *p = NODE_DATA(nid);
1307 new_pgdat = !p;
1308 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001309
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001310 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001311
Tang Chen7f36e3e2015-09-04 15:42:32 -07001312 /*
1313 * Add new range to memblock so that when hotadd_new_pgdat() is called
1314 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1315 * this new range and calculate total pages correctly. The range will
1316 * be removed at hot-remove time.
1317 */
1318 memblock_add_node(start, size, nid);
1319
Tang Chena1e565a2013-02-22 16:33:18 -08001320 new_node = !node_online(nid);
1321 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001322 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001323 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001324 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001325 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001326 }
1327
Yasunori Gotobc02af92006-06-27 02:53:30 -07001328 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001329 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001330
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001331 if (ret < 0)
1332 goto error;
1333
Yasunori Goto0fc44152006-06-27 02:53:38 -07001334 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001335 node_set_online(nid);
1336
Tang Chena1e565a2013-02-22 16:33:18 -08001337 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001338 ret = register_one_node(nid);
1339 /*
1340 * If sysfs file of new node can't create, cpu on the node
1341 * can't be hot-added. There is no rollback way now.
1342 * So, check by BUG_ON() to catch it reluctantly..
1343 */
1344 BUG_ON(ret);
1345 }
1346
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001347 /* create new memmap entry */
1348 firmware_map_add_hotplug(start, start + size, "System RAM");
1349
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001350 /* online pages if requested */
1351 if (online)
1352 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1353 NULL, online_memory_block);
1354
Andi Kleen6ad696d2009-11-17 14:06:22 -08001355 goto out;
1356
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001357error:
1358 /* rollback pgdat allocation and others */
1359 if (new_pgdat)
1360 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001361 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001362
Andi Kleen6ad696d2009-11-17 14:06:22 -08001363out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001364 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001365 return ret;
1366}
David Vrabel62cedb92015-06-25 16:35:49 +01001367EXPORT_SYMBOL_GPL(add_memory_resource);
1368
1369int __ref add_memory(int nid, u64 start, u64 size)
1370{
1371 struct resource *res;
1372 int ret;
1373
1374 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001375 if (IS_ERR(res))
1376 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001377
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001378 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001379 if (ret < 0)
1380 release_memory_resource(res);
1381 return ret;
1382}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001383EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001384
1385#ifdef CONFIG_MEMORY_HOTREMOVE
1386/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001387 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1388 * set and the size of the free page is given by page_order(). Using this,
1389 * the function determines if the pageblock contains only free pages.
1390 * Due to buddy contraints, a free page at least the size of a pageblock will
1391 * be located at the start of the pageblock
1392 */
1393static inline int pageblock_free(struct page *page)
1394{
1395 return PageBuddy(page) && page_order(page) >= pageblock_order;
1396}
1397
1398/* Return the start of the next active pageblock after a given page */
1399static struct page *next_active_pageblock(struct page *page)
1400{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001401 /* Ensure the starting page is pageblock-aligned */
1402 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1403
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001404 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001405 if (pageblock_free(page)) {
1406 int order;
1407 /* be careful. we don't have locks, page_order can be changed.*/
1408 order = page_order(page);
1409 if ((order < MAX_ORDER) && (order >= pageblock_order))
1410 return page + (1 << order);
1411 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001412
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001413 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001414}
1415
1416/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001417bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001418{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001419 struct page *page = pfn_to_page(start_pfn);
1420 struct page *end_page = page + nr_pages;
1421
1422 /* Check the starting page of each pageblock within the range */
1423 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001424 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001425 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001426 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001427 }
1428
1429 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001430 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001431}
1432
1433/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001434 * Confirm all pages in a range [start, end) is belongs to the same zone.
1435 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001436int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001437{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001438 unsigned long pfn, sec_end_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001439 struct zone *zone = NULL;
1440 struct page *page;
1441 int i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001442 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001443 pfn < end_pfn;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001444 pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1445 /* Make sure the memory section is present first */
1446 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001447 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001448 for (; pfn < sec_end_pfn && pfn < end_pfn;
1449 pfn += MAX_ORDER_NR_PAGES) {
1450 i = 0;
1451 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1452 while ((i < MAX_ORDER_NR_PAGES) &&
1453 !pfn_valid_within(pfn + i))
1454 i++;
1455 if (i == MAX_ORDER_NR_PAGES)
1456 continue;
1457 page = pfn_to_page(pfn + i);
1458 if (zone && page_zone(page) != zone)
1459 return 0;
1460 zone = page_zone(page);
1461 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001462 }
1463 return 1;
1464}
1465
1466/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001467 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1468 * and hugepages). We scan pfn because it's much easier than scanning over
1469 * linked list. This function returns the pfn of the first found movable
1470 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001471 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001472static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001473{
1474 unsigned long pfn;
1475 struct page *page;
1476 for (pfn = start; pfn < end; pfn++) {
1477 if (pfn_valid(pfn)) {
1478 page = pfn_to_page(pfn);
1479 if (PageLRU(page))
1480 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001481 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001482 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001483 return pfn;
1484 else
1485 pfn = round_up(pfn + 1,
1486 1 << compound_order(page)) - 1;
1487 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001488 }
1489 }
1490 return 0;
1491}
1492
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001493#define NR_OFFLINE_AT_ONCE_PAGES (256)
1494static int
1495do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1496{
1497 unsigned long pfn;
1498 struct page *page;
1499 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1500 int not_managed = 0;
1501 int ret = 0;
1502 LIST_HEAD(source);
1503
1504 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1505 if (!pfn_valid(pfn))
1506 continue;
1507 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001508
1509 if (PageHuge(page)) {
1510 struct page *head = compound_head(page);
1511 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1512 if (compound_order(head) > PFN_SECTION_SHIFT) {
1513 ret = -EBUSY;
1514 break;
1515 }
1516 if (isolate_huge_page(page, &source))
1517 move_pages -= 1 << compound_order(head);
1518 continue;
1519 }
1520
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001521 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001522 continue;
1523 /*
1524 * We can skip free pages. And we can only deal with pages on
1525 * LRU.
1526 */
Nick Piggin62695a82008-10-18 20:26:09 -07001527 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001528 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001529 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001530 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001531 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001532 inc_zone_page_state(page, NR_ISOLATED_ANON +
1533 page_is_file_cache(page));
1534
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001535 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001536#ifdef CONFIG_DEBUG_VM
Chen Yuconge33e33b2016-03-17 14:19:35 -07001537 pr_alert("removing pfn %lx from LRU failed\n", pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001538 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001539#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001540 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001541 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001542 check this again here. */
1543 if (page_count(page)) {
1544 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001545 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001546 break;
1547 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001548 }
1549 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001550 if (!list_empty(&source)) {
1551 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001552 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001553 goto out;
1554 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001555
1556 /*
1557 * alloc_migrate_target should be improooooved!!
1558 * migrate_pages returns # of failed pages.
1559 */
David Rientjes68711a72014-06-04 16:08:25 -07001560 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001561 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001562 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001563 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001564 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001565out:
1566 return ret;
1567}
1568
1569/*
1570 * remove from free_area[] and mark all as Reserved.
1571 */
1572static int
1573offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1574 void *data)
1575{
1576 __offline_isolated_pages(start, start + nr_pages);
1577 return 0;
1578}
1579
1580static void
1581offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1582{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001583 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001584 offline_isolated_pages_cb);
1585}
1586
1587/*
1588 * Check all pages in range, recoreded as memory resource, are isolated.
1589 */
1590static int
1591check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1592 void *data)
1593{
1594 int ret;
1595 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001596 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001597 offlined = nr_pages;
1598 if (!ret)
1599 *(long *)data += offlined;
1600 return ret;
1601}
1602
1603static long
1604check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1605{
1606 long offlined = 0;
1607 int ret;
1608
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001609 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001610 check_pages_isolated_cb);
1611 if (ret < 0)
1612 offlined = (long)ret;
1613 return offlined;
1614}
1615
Lai Jiangshan09285af2012-12-12 13:52:04 -08001616#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001617/*
1618 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1619 * normal memory.
1620 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001621static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1622{
1623 return true;
1624}
Tang Chen79a4dce2012-12-18 14:23:24 -08001625#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001626/* ensure the node has NORMAL memory if it is still online */
1627static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1628{
1629 struct pglist_data *pgdat = zone->zone_pgdat;
1630 unsigned long present_pages = 0;
1631 enum zone_type zt;
1632
1633 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1634 present_pages += pgdat->node_zones[zt].present_pages;
1635
1636 if (present_pages > nr_pages)
1637 return true;
1638
1639 present_pages = 0;
1640 for (; zt <= ZONE_MOVABLE; zt++)
1641 present_pages += pgdat->node_zones[zt].present_pages;
1642
1643 /*
1644 * we can't offline the last normal memory until all
1645 * higher memory is offlined.
1646 */
1647 return present_pages == 0;
1648}
Tang Chen79a4dce2012-12-18 14:23:24 -08001649#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001650
Tang Chenc5320922013-11-12 15:08:10 -08001651static int __init cmdline_parse_movable_node(char *p)
1652{
1653#ifdef CONFIG_MOVABLE_NODE
1654 /*
1655 * Memory used by the kernel cannot be hot-removed because Linux
1656 * cannot migrate the kernel pages. When memory hotplug is
1657 * enabled, we should prevent memblock from allocating memory
1658 * for the kernel.
1659 *
1660 * ACPI SRAT records all hotpluggable memory ranges. But before
1661 * SRAT is parsed, we don't know about it.
1662 *
1663 * The kernel image is loaded into memory at very early time. We
1664 * cannot prevent this anyway. So on NUMA system, we set any
1665 * node the kernel resides in as un-hotpluggable.
1666 *
1667 * Since on modern servers, one node could have double-digit
1668 * gigabytes memory, we can assume the memory around the kernel
1669 * image is also un-hotpluggable. So before SRAT is parsed, just
1670 * allocate memory near the kernel image to try the best to keep
1671 * the kernel away from hotpluggable memory.
1672 */
1673 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001674 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001675#else
1676 pr_warn("movable_node option not supported\n");
1677#endif
1678 return 0;
1679}
1680early_param("movable_node", cmdline_parse_movable_node);
1681
Lai Jiangshand9713672012-12-11 16:01:03 -08001682/* check which state of node_states will be changed when offline memory */
1683static void node_states_check_changes_offline(unsigned long nr_pages,
1684 struct zone *zone, struct memory_notify *arg)
1685{
1686 struct pglist_data *pgdat = zone->zone_pgdat;
1687 unsigned long present_pages = 0;
1688 enum zone_type zt, zone_last = ZONE_NORMAL;
1689
1690 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001691 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1692 * contains nodes which have zones of 0...ZONE_NORMAL,
1693 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001694 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001695 * If we don't have HIGHMEM nor movable node,
1696 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1697 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001698 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001699 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001700 zone_last = ZONE_MOVABLE;
1701
1702 /*
1703 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1704 * If the memory to be offline is in a zone of 0...zone_last,
1705 * and it is the last present memory, 0...zone_last will
1706 * become empty after offline , thus we can determind we will
1707 * need to clear the node from node_states[N_NORMAL_MEMORY].
1708 */
1709 for (zt = 0; zt <= zone_last; zt++)
1710 present_pages += pgdat->node_zones[zt].present_pages;
1711 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1712 arg->status_change_nid_normal = zone_to_nid(zone);
1713 else
1714 arg->status_change_nid_normal = -1;
1715
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001716#ifdef CONFIG_HIGHMEM
1717 /*
1718 * If we have movable node, node_states[N_HIGH_MEMORY]
1719 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1720 * set zone_last to ZONE_HIGHMEM.
1721 *
1722 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1723 * contains nodes which have zones of 0...ZONE_MOVABLE,
1724 * set zone_last to ZONE_MOVABLE.
1725 */
1726 zone_last = ZONE_HIGHMEM;
1727 if (N_MEMORY == N_HIGH_MEMORY)
1728 zone_last = ZONE_MOVABLE;
1729
1730 for (; zt <= zone_last; zt++)
1731 present_pages += pgdat->node_zones[zt].present_pages;
1732 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1733 arg->status_change_nid_high = zone_to_nid(zone);
1734 else
1735 arg->status_change_nid_high = -1;
1736#else
1737 arg->status_change_nid_high = arg->status_change_nid_normal;
1738#endif
1739
Lai Jiangshand9713672012-12-11 16:01:03 -08001740 /*
1741 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1742 */
1743 zone_last = ZONE_MOVABLE;
1744
1745 /*
1746 * check whether node_states[N_HIGH_MEMORY] will be changed
1747 * If we try to offline the last present @nr_pages from the node,
1748 * we can determind we will need to clear the node from
1749 * node_states[N_HIGH_MEMORY].
1750 */
1751 for (; zt <= zone_last; zt++)
1752 present_pages += pgdat->node_zones[zt].present_pages;
1753 if (nr_pages >= present_pages)
1754 arg->status_change_nid = zone_to_nid(zone);
1755 else
1756 arg->status_change_nid = -1;
1757}
1758
1759static void node_states_clear_node(int node, struct memory_notify *arg)
1760{
1761 if (arg->status_change_nid_normal >= 0)
1762 node_clear_state(node, N_NORMAL_MEMORY);
1763
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001764 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1765 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001766 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001767
1768 if ((N_MEMORY != N_HIGH_MEMORY) &&
1769 (arg->status_change_nid >= 0))
1770 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001771}
1772
Wen Congyanga16cee12012-10-08 16:33:58 -07001773static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001774 unsigned long end_pfn, unsigned long timeout)
1775{
1776 unsigned long pfn, nr_pages, expire;
1777 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001778 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001779 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001780 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001781 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001782
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001783 /* at least, alignment against pageblock is necessary */
1784 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1785 return -EINVAL;
1786 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1787 return -EINVAL;
1788 /* This makes hotplug much easier...and readable.
1789 we assume this for now. .*/
1790 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1791 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001792
1793 zone = page_zone(pfn_to_page(start_pfn));
1794 node = zone_to_nid(zone);
1795 nr_pages = end_pfn - start_pfn;
1796
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001797 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001798 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001799
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001800 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001801 ret = start_isolate_page_range(start_pfn, end_pfn,
1802 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001803 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001804 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001805
1806 arg.start_pfn = start_pfn;
1807 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001808 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001809
1810 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1811 ret = notifier_to_errno(ret);
1812 if (ret)
1813 goto failed_removal;
1814
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001815 pfn = start_pfn;
1816 expire = jiffies + timeout;
1817 drain = 0;
1818 retry_max = 5;
1819repeat:
1820 /* start memory hot removal */
1821 ret = -EAGAIN;
1822 if (time_after(jiffies, expire))
1823 goto failed_removal;
1824 ret = -EINTR;
1825 if (signal_pending(current))
1826 goto failed_removal;
1827 ret = 0;
1828 if (drain) {
1829 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001830 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001831 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001832 }
1833
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001834 pfn = scan_movable_pages(start_pfn, end_pfn);
1835 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001836 ret = do_migrate_range(pfn, end_pfn);
1837 if (!ret) {
1838 drain = 1;
1839 goto repeat;
1840 } else {
1841 if (ret < 0)
1842 if (--retry_max == 0)
1843 goto failed_removal;
1844 yield();
1845 drain = 1;
1846 goto repeat;
1847 }
1848 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001849 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001850 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001851 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001852 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001853 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001854 /*
1855 * dissolve free hugepages in the memory block before doing offlining
1856 * actually in order to make hugetlbfs's object counting consistent.
1857 */
1858 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001859 /* check again */
1860 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1861 if (offlined_pages < 0) {
1862 ret = -EBUSY;
1863 goto failed_removal;
1864 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001865 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001866 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001867 We cannot do rollback at this point. */
1868 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001869 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001870 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001871 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001872 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001873 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001874
1875 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001876 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001877 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001878
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001879 init_per_zone_wmark_min();
1880
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001881 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001882 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001883 mutex_lock(&zonelists_mutex);
1884 build_all_zonelists(NULL, NULL);
1885 mutex_unlock(&zonelists_mutex);
1886 } else
1887 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001888
Lai Jiangshand9713672012-12-11 16:01:03 -08001889 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001890 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001891 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001892 kcompactd_stop(node);
1893 }
Minchan Kimbce73942009-06-16 15:32:50 -07001894
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001895 vm_total_pages = nr_free_pagecache_pages();
1896 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001897
1898 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001899 return 0;
1900
1901failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001902 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1903 (unsigned long long) start_pfn << PAGE_SHIFT,
1904 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001905 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001906 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001907 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001908 return ret;
1909}
Badari Pulavarty71088782008-10-18 20:25:58 -07001910
David Rientjes30467e02015-04-14 15:45:11 -07001911/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001912int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1913{
1914 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1915}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001916#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001917
Wen Congyangbbc76be2013-02-22 16:32:54 -08001918/**
1919 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1920 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001921 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001922 * @arg: argument passed to func
1923 * @func: callback for each memory section walked
1924 *
1925 * This function walks through all present mem sections in range
1926 * [start_pfn, end_pfn) and call func on each mem section.
1927 *
1928 * Returns the return value of func.
1929 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001930int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001931 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001932{
Wen Congyange90bdb72012-10-08 16:34:01 -07001933 struct memory_block *mem = NULL;
1934 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001935 unsigned long pfn, section_nr;
1936 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001937
Wen Congyange90bdb72012-10-08 16:34:01 -07001938 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1939 section_nr = pfn_to_section_nr(pfn);
1940 if (!present_section_nr(section_nr))
1941 continue;
1942
1943 section = __nr_to_section(section_nr);
1944 /* same memblock? */
1945 if (mem)
1946 if ((section_nr >= mem->start_section_nr) &&
1947 (section_nr <= mem->end_section_nr))
1948 continue;
1949
1950 mem = find_memory_block_hinted(section, mem);
1951 if (!mem)
1952 continue;
1953
Wen Congyangbbc76be2013-02-22 16:32:54 -08001954 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001955 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001956 kobject_put(&mem->dev.kobj);
1957 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001958 }
1959 }
1960
1961 if (mem)
1962 kobject_put(&mem->dev.kobj);
1963
Wen Congyangbbc76be2013-02-22 16:32:54 -08001964 return 0;
1965}
1966
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001967#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001968static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001969{
1970 int ret = !is_memblock_offlined(mem);
1971
Randy Dunlap349daa02013-04-29 15:08:49 -07001972 if (unlikely(ret)) {
1973 phys_addr_t beginpa, endpa;
1974
1975 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1976 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07001977 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07001978 &beginpa, &endpa);
1979 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001980
1981 return ret;
1982}
1983
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001984static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001985{
Tang Chen60a5a192013-02-22 16:33:14 -08001986 int cpu;
1987
1988 for_each_present_cpu(cpu) {
1989 if (cpu_to_node(cpu) == pgdat->node_id)
1990 /*
1991 * the cpu on this node isn't removed, and we can't
1992 * offline this node.
1993 */
1994 return -EBUSY;
1995 }
1996
1997 return 0;
1998}
1999
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002000static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002001{
2002#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08002003 int cpu;
2004
2005 for_each_possible_cpu(cpu)
2006 if (cpu_to_node(cpu) == pgdat->node_id)
2007 numa_clear_node(cpu);
2008#endif
2009}
2010
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002011static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002012{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002013 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002014
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002015 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002016 if (ret)
2017 return ret;
2018
2019 /*
2020 * the node will be offlined when we come here, so we can clear
2021 * the cpu_to_node() now.
2022 */
2023
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002024 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002025 return 0;
2026}
2027
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002028/**
2029 * try_offline_node
2030 *
2031 * Offline a node if all memory sections and cpus of the node are removed.
2032 *
2033 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2034 * and online/offline operations before this call.
2035 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002036void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002037{
Wen Congyangd822b862013-02-22 16:33:16 -08002038 pg_data_t *pgdat = NODE_DATA(nid);
2039 unsigned long start_pfn = pgdat->node_start_pfn;
2040 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002041 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08002042 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08002043
2044 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2045 unsigned long section_nr = pfn_to_section_nr(pfn);
2046
2047 if (!present_section_nr(section_nr))
2048 continue;
2049
2050 if (pfn_to_nid(pfn) != nid)
2051 continue;
2052
2053 /*
2054 * some memory sections of this node are not removed, and we
2055 * can't offline node now.
2056 */
2057 return;
2058 }
2059
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002060 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002061 return;
2062
2063 /*
2064 * all memory/cpu of this node are removed, we can offline this
2065 * node now.
2066 */
2067 node_set_offline(nid);
2068 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08002069
Wen Congyangd822b862013-02-22 16:33:16 -08002070 /* free waittable in each zone */
2071 for (i = 0; i < MAX_NR_ZONES; i++) {
2072 struct zone *zone = pgdat->node_zones + i;
2073
Jianguo Wuca4b3f32013-03-22 15:04:50 -07002074 /*
2075 * wait_table may be allocated from boot memory,
2076 * here only free if it's allocated by vmalloc.
2077 */
Gu Zheng85bd8392015-06-10 11:14:43 -07002078 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08002079 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07002080 zone->wait_table = NULL;
2081 }
Wen Congyangd822b862013-02-22 16:33:16 -08002082 }
Tang Chen60a5a192013-02-22 16:33:14 -08002083}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002084EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002085
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002086/**
2087 * remove_memory
2088 *
2089 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2090 * and online/offline operations before this call, as required by
2091 * try_offline_node().
2092 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002093void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002094{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002095 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002096
Toshi Kani27356f52013-09-11 14:21:49 -07002097 BUG_ON(check_hotplug_memory_range(start, size));
2098
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002099 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002100
2101 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002102 * All memory blocks must be offlined before removing memory. Check
2103 * whether all memory blocks in question are offline and trigger a BUG()
2104 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002105 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002106 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002107 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002108 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002109 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002110
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002111 /* remove memmap entry */
2112 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002113 memblock_free(start, size);
2114 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002115
Wen Congyang24d335c2013-02-22 16:32:58 -08002116 arch_remove_memory(start, size);
2117
Tang Chen60a5a192013-02-22 16:33:14 -08002118 try_offline_node(nid);
2119
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002120 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002121}
Badari Pulavarty71088782008-10-18 20:25:58 -07002122EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002123#endif /* CONFIG_MEMORY_HOTREMOVE */