blob: 24ea06393816c58b43a857bb5e1cc05dbdfc4712 [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>
Dave Hansen3947be12005-10-29 18:16:54 -070036
37#include <asm/tlbflush.h>
38
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030039#include "internal.h"
40
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070041/*
42 * online_page_callback contains pointer to current page onlining function.
43 * Initially it is generic_online_page(). If it is required it could be
44 * changed by calling set_online_page_callback() for callback registration
45 * and restore_online_page_callback() for generic callback restore.
46 */
47
48static void generic_online_page(struct page *page);
49
50static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070051static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070052
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070053/* The same as the cpu_hotplug lock, but for memory hotplug. */
54static struct {
55 struct task_struct *active_writer;
56 struct mutex lock; /* Synchronizes accesses to refcount, */
57 /*
58 * Also blocks the new readers during
59 * an ongoing mem hotplug operation.
60 */
61 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080062
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070063#ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
65#endif
66} mem_hotplug = {
67 .active_writer = NULL,
68 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
69 .refcount = 0,
70#ifdef CONFIG_DEBUG_LOCK_ALLOC
71 .dep_map = {.name = "mem_hotplug.lock" },
72#endif
73};
74
75/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
76#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
77#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
78#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
79
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070080bool memhp_auto_online;
81EXPORT_SYMBOL_GPL(memhp_auto_online);
82
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070083void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080084{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070085 might_sleep();
86 if (mem_hotplug.active_writer == current)
87 return;
88 memhp_lock_acquire_read();
89 mutex_lock(&mem_hotplug.lock);
90 mem_hotplug.refcount++;
91 mutex_unlock(&mem_hotplug.lock);
92
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080093}
94
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070095void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080096{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070097 if (mem_hotplug.active_writer == current)
98 return;
99 mutex_lock(&mem_hotplug.lock);
100
101 if (WARN_ON(!mem_hotplug.refcount))
102 mem_hotplug.refcount++; /* try to fix things up */
103
104 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
105 wake_up_process(mem_hotplug.active_writer);
106 mutex_unlock(&mem_hotplug.lock);
107 memhp_lock_release();
108
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800109}
110
David Rientjes30467e02015-04-14 15:45:11 -0700111void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700112{
113 mem_hotplug.active_writer = current;
114
115 memhp_lock_acquire();
116 for (;;) {
117 mutex_lock(&mem_hotplug.lock);
118 if (likely(!mem_hotplug.refcount))
119 break;
120 __set_current_state(TASK_UNINTERRUPTIBLE);
121 mutex_unlock(&mem_hotplug.lock);
122 schedule();
123 }
124}
125
David Rientjes30467e02015-04-14 15:45:11 -0700126void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700127{
128 mem_hotplug.active_writer = NULL;
129 mutex_unlock(&mem_hotplug.lock);
130 memhp_lock_release();
131}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800132
Keith Mannthey45e0b782006-09-30 23:27:09 -0700133/* add this memory to iomem resource */
134static struct resource *register_memory_resource(u64 start, u64 size)
135{
136 struct resource *res;
137 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800138 if (!res)
139 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700140
141 res->name = "System RAM";
142 res->start = start;
143 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100144 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700145 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700146 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700147 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800148 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700149 }
150 return res;
151}
152
153static void release_memory_resource(struct resource *res)
154{
155 if (!res)
156 return;
157 release_resource(res);
158 kfree(res);
159 return;
160}
161
Keith Mannthey53947022006-09-30 23:27:08 -0700162#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800163void get_page_bootmem(unsigned long info, struct page *page,
164 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700165{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800166 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700167 SetPagePrivate(page);
168 set_page_private(page, info);
169 atomic_inc(&page->_count);
170}
171
Jiang Liu170a5a72013-07-03 15:03:17 -0700172void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700173{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800174 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700175
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800176 type = (unsigned long) page->lru.next;
177 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
178 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700179
180 if (atomic_dec_return(&page->_count) == 1) {
181 ClearPagePrivate(page);
182 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800183 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700184 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700185 }
Yasunori Goto04753272008-04-28 02:13:31 -0700186}
187
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800188#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
189#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700190static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700191{
192 unsigned long *usemap, mapsize, section_nr, i;
193 struct mem_section *ms;
194 struct page *page, *memmap;
195
Yasunori Goto04753272008-04-28 02:13:31 -0700196 section_nr = pfn_to_section_nr(start_pfn);
197 ms = __nr_to_section(section_nr);
198
199 /* Get section's memmap address */
200 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
201
202 /*
203 * Get page for the memmap's phys address
204 * XXX: need more consideration for sparse_vmemmap...
205 */
206 page = virt_to_page(memmap);
207 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
208 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
209
210 /* remember memmap's page */
211 for (i = 0; i < mapsize; i++, page++)
212 get_page_bootmem(section_nr, page, SECTION_INFO);
213
214 usemap = __nr_to_section(section_nr)->pageblock_flags;
215 page = virt_to_page(usemap);
216
217 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
218
219 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700220 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700221
222}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800223#else /* CONFIG_SPARSEMEM_VMEMMAP */
224static void register_page_bootmem_info_section(unsigned long start_pfn)
225{
226 unsigned long *usemap, mapsize, section_nr, i;
227 struct mem_section *ms;
228 struct page *page, *memmap;
229
230 if (!pfn_valid(start_pfn))
231 return;
232
233 section_nr = pfn_to_section_nr(start_pfn);
234 ms = __nr_to_section(section_nr);
235
236 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
237
238 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
239
240 usemap = __nr_to_section(section_nr)->pageblock_flags;
241 page = virt_to_page(usemap);
242
243 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
244
245 for (i = 0; i < mapsize; i++, page++)
246 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
247}
248#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700249
250void register_page_bootmem_info_node(struct pglist_data *pgdat)
251{
252 unsigned long i, pfn, end_pfn, nr_pages;
253 int node = pgdat->node_id;
254 struct page *page;
255 struct zone *zone;
256
257 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
258 page = virt_to_page(pgdat);
259
260 for (i = 0; i < nr_pages; i++, page++)
261 get_page_bootmem(node, page, NODE_INFO);
262
263 zone = &pgdat->node_zones[0];
264 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700265 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700266 nr_pages = zone->wait_table_hash_nr_entries
267 * sizeof(wait_queue_head_t);
268 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
269 page = virt_to_page(zone->wait_table);
270
271 for (i = 0; i < nr_pages; i++, page++)
272 get_page_bootmem(node, page, NODE_INFO);
273 }
274 }
275
276 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800277 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700278
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700279 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700280 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
281 /*
282 * Some platforms can assign the same pfn to multiple nodes - on
283 * node0 as well as nodeN. To avoid registering a pfn against
284 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700285 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700286 */
287 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
288 register_page_bootmem_info_section(pfn);
289 }
Yasunori Goto04753272008-04-28 02:13:31 -0700290}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800291#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700292
Fabian Frederickf2765402014-08-06 16:04:57 -0700293static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
294 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700295{
296 unsigned long old_zone_end_pfn;
297
298 zone_span_writelock(zone);
299
Xishi Qiuc33bc312013-09-11 14:21:44 -0700300 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700301 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700302 zone->zone_start_pfn = start_pfn;
303
304 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
305 zone->zone_start_pfn;
306
307 zone_span_writeunlock(zone);
308}
309
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800310static void resize_zone(struct zone *zone, unsigned long start_pfn,
311 unsigned long end_pfn)
312{
313 zone_span_writelock(zone);
314
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800315 if (end_pfn - start_pfn) {
316 zone->zone_start_pfn = start_pfn;
317 zone->spanned_pages = end_pfn - start_pfn;
318 } else {
319 /*
320 * make it consist as free_area_init_core(),
321 * if spanned_pages = 0, then keep start_pfn = 0
322 */
323 zone->zone_start_pfn = 0;
324 zone->spanned_pages = 0;
325 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800326
327 zone_span_writeunlock(zone);
328}
329
330static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
331 unsigned long end_pfn)
332{
333 enum zone_type zid = zone_idx(zone);
334 int nid = zone->zone_pgdat->node_id;
335 unsigned long pfn;
336
337 for (pfn = start_pfn; pfn < end_pfn; pfn++)
338 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
339}
340
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800341/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800342 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800343static int __ref ensure_zone_is_initialized(struct zone *zone,
344 unsigned long start_pfn, unsigned long num_pages)
345{
346 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800347 return init_currently_empty_zone(zone, start_pfn, num_pages);
348
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800349 return 0;
350}
351
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800352static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800353 unsigned long start_pfn, unsigned long end_pfn)
354{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800355 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800356 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800357 unsigned long z1_start_pfn;
358
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800359 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
360 if (ret)
361 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362
363 pgdat_resize_lock(z1->zone_pgdat, &flags);
364
365 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800366 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800367 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700368 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800369 if (start_pfn > z2->zone_start_pfn)
370 goto out_fail;
371 /* must included/overlap */
372 if (end_pfn <= z2->zone_start_pfn)
373 goto out_fail;
374
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800375 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700376 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800377 z1_start_pfn = z1->zone_start_pfn;
378 else
379 z1_start_pfn = start_pfn;
380
381 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800382 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800383
384 pgdat_resize_unlock(z1->zone_pgdat, &flags);
385
386 fix_zone_id(z1, start_pfn, end_pfn);
387
388 return 0;
389out_fail:
390 pgdat_resize_unlock(z1->zone_pgdat, &flags);
391 return -1;
392}
393
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800394static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800395 unsigned long start_pfn, unsigned long end_pfn)
396{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800397 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800398 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800399 unsigned long z2_end_pfn;
400
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800401 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
402 if (ret)
403 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800404
405 pgdat_resize_lock(z1->zone_pgdat, &flags);
406
407 /* can't move pfns which are lower than @z1 */
408 if (z1->zone_start_pfn > start_pfn)
409 goto out_fail;
410 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800411 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800412 goto out_fail;
413 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800415 goto out_fail;
416
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800417 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700418 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800419 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800420 else
421 z2_end_pfn = end_pfn;
422
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800423 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800424 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800425
426 pgdat_resize_unlock(z1->zone_pgdat, &flags);
427
428 fix_zone_id(z2, start_pfn, end_pfn);
429
430 return 0;
431out_fail:
432 pgdat_resize_unlock(z1->zone_pgdat, &flags);
433 return -1;
434}
435
Fabian Frederickf2765402014-08-06 16:04:57 -0700436static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
437 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700438{
Xishi Qiu83285c72013-11-12 15:07:19 -0800439 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700440
Tang Chen712cd382012-12-11 16:01:07 -0800441 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700442 pgdat->node_start_pfn = start_pfn;
443
444 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
445 pgdat->node_start_pfn;
446}
447
Al Viro31168482008-11-22 17:33:24 +0000448static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700449{
450 struct pglist_data *pgdat = zone->zone_pgdat;
451 int nr_pages = PAGES_PER_SECTION;
452 int nid = pgdat->node_id;
453 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700454 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800455 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700456
457 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800458 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
459 if (ret)
460 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700461
Heiko Carstens76cdd582008-05-14 16:05:52 -0700462 pgdat_resize_lock(zone->zone_pgdat, &flags);
463 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
464 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
465 phys_start_pfn + nr_pages);
466 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800467 memmap_init_zone(nr_pages, nid, zone_type,
468 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700469
470 /* online_page_range is called later and expects pages reserved */
471 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
472 if (!pfn_valid(pfn))
473 continue;
474
475 SetPageReserved(pfn_to_page(pfn));
476 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700477 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700478}
479
Gary Hadec04fc582009-01-06 14:39:14 -0800480static int __meminit __add_section(int nid, struct zone *zone,
481 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700482{
Dave Hansen3947be12005-10-29 18:16:54 -0700483 int ret;
484
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700485 if (pfn_valid(phys_start_pfn))
486 return -EEXIST;
487
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800488 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700489
490 if (ret < 0)
491 return ret;
492
Yasunori Goto718127c2006-06-23 02:03:10 -0700493 ret = __add_zone(zone, phys_start_pfn);
494
495 if (ret < 0)
496 return ret;
497
Gary Hadec04fc582009-01-06 14:39:14 -0800498 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700499}
500
David Rientjes4edd7ce2013-04-29 15:08:22 -0700501/*
502 * Reasonably generic function for adding memory. It is
503 * expected that archs that support memory hotplug will
504 * call this function after deciding the zone to which to
505 * add the new pages.
506 */
507int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
508 unsigned long nr_pages)
509{
510 unsigned long i;
511 int err = 0;
512 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800513 struct vmem_altmap *altmap;
514
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700515 clear_zone_contiguous(zone);
516
David Rientjes4edd7ce2013-04-29 15:08:22 -0700517 /* during initialize mem_map, align hot-added range to section */
518 start_sec = pfn_to_section_nr(phys_start_pfn);
519 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
520
Dan Williams4b94ffd2016-01-15 16:56:22 -0800521 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
522 if (altmap) {
523 /*
524 * Validate altmap is within bounds of the total request
525 */
526 if (altmap->base_pfn != phys_start_pfn
527 || vmem_altmap_offset(altmap) > nr_pages) {
528 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700529 err = -EINVAL;
530 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800531 }
532 altmap->alloc = 0;
533 }
534
David Rientjes4edd7ce2013-04-29 15:08:22 -0700535 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700536 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700537
538 /*
539 * EEXIST is finally dealt with by ioresource collision
540 * check. see add_memory() => register_memory_resource()
541 * Warning will be printed if there is collision.
542 */
543 if (err && (err != -EEXIST))
544 break;
545 err = 0;
546 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700547 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700548out:
549 set_zone_contiguous(zone);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700550 return err;
551}
552EXPORT_SYMBOL_GPL(__add_pages);
553
554#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800555/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
556static int find_smallest_section_pfn(int nid, struct zone *zone,
557 unsigned long start_pfn,
558 unsigned long end_pfn)
559{
560 struct mem_section *ms;
561
562 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
563 ms = __pfn_to_section(start_pfn);
564
565 if (unlikely(!valid_section(ms)))
566 continue;
567
568 if (unlikely(pfn_to_nid(start_pfn) != nid))
569 continue;
570
571 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
572 continue;
573
574 return start_pfn;
575 }
576
577 return 0;
578}
579
580/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
581static int find_biggest_section_pfn(int nid, struct zone *zone,
582 unsigned long start_pfn,
583 unsigned long end_pfn)
584{
585 struct mem_section *ms;
586 unsigned long pfn;
587
588 /* pfn is the end pfn of a memory section. */
589 pfn = end_pfn - 1;
590 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
591 ms = __pfn_to_section(pfn);
592
593 if (unlikely(!valid_section(ms)))
594 continue;
595
596 if (unlikely(pfn_to_nid(pfn) != nid))
597 continue;
598
599 if (zone && zone != page_zone(pfn_to_page(pfn)))
600 continue;
601
602 return pfn;
603 }
604
605 return 0;
606}
607
608static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
609 unsigned long end_pfn)
610{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700611 unsigned long zone_start_pfn = zone->zone_start_pfn;
612 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
613 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800614 unsigned long pfn;
615 struct mem_section *ms;
616 int nid = zone_to_nid(zone);
617
618 zone_span_writelock(zone);
619 if (zone_start_pfn == start_pfn) {
620 /*
621 * If the section is smallest section in the zone, it need
622 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
623 * In this case, we find second smallest valid mem_section
624 * for shrinking zone.
625 */
626 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
627 zone_end_pfn);
628 if (pfn) {
629 zone->zone_start_pfn = pfn;
630 zone->spanned_pages = zone_end_pfn - pfn;
631 }
632 } else if (zone_end_pfn == end_pfn) {
633 /*
634 * If the section is biggest section in the zone, it need
635 * shrink zone->spanned_pages.
636 * In this case, we find second biggest valid mem_section for
637 * shrinking zone.
638 */
639 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
640 start_pfn);
641 if (pfn)
642 zone->spanned_pages = pfn - zone_start_pfn + 1;
643 }
644
645 /*
646 * The section is not biggest or smallest mem_section in the zone, it
647 * only creates a hole in the zone. So in this case, we need not
648 * change the zone. But perhaps, the zone has only hole data. Thus
649 * it check the zone has only hole or not.
650 */
651 pfn = zone_start_pfn;
652 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
653 ms = __pfn_to_section(pfn);
654
655 if (unlikely(!valid_section(ms)))
656 continue;
657
658 if (page_zone(pfn_to_page(pfn)) != zone)
659 continue;
660
661 /* If the section is current section, it continues the loop */
662 if (start_pfn == pfn)
663 continue;
664
665 /* If we find valid section, we have nothing to do */
666 zone_span_writeunlock(zone);
667 return;
668 }
669
670 /* The zone has no valid section */
671 zone->zone_start_pfn = 0;
672 zone->spanned_pages = 0;
673 zone_span_writeunlock(zone);
674}
675
676static void shrink_pgdat_span(struct pglist_data *pgdat,
677 unsigned long start_pfn, unsigned long end_pfn)
678{
Xishi Qiu83285c72013-11-12 15:07:19 -0800679 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
680 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
681 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800682 unsigned long pfn;
683 struct mem_section *ms;
684 int nid = pgdat->node_id;
685
686 if (pgdat_start_pfn == start_pfn) {
687 /*
688 * If the section is smallest section in the pgdat, it need
689 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
690 * In this case, we find second smallest valid mem_section
691 * for shrinking zone.
692 */
693 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
694 pgdat_end_pfn);
695 if (pfn) {
696 pgdat->node_start_pfn = pfn;
697 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
698 }
699 } else if (pgdat_end_pfn == end_pfn) {
700 /*
701 * If the section is biggest section in the pgdat, it need
702 * shrink pgdat->node_spanned_pages.
703 * In this case, we find second biggest valid mem_section for
704 * shrinking zone.
705 */
706 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
707 start_pfn);
708 if (pfn)
709 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
710 }
711
712 /*
713 * If the section is not biggest or smallest mem_section in the pgdat,
714 * it only creates a hole in the pgdat. So in this case, we need not
715 * change the pgdat.
716 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
717 * has only hole or not.
718 */
719 pfn = pgdat_start_pfn;
720 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
721 ms = __pfn_to_section(pfn);
722
723 if (unlikely(!valid_section(ms)))
724 continue;
725
726 if (pfn_to_nid(pfn) != nid)
727 continue;
728
729 /* If the section is current section, it continues the loop */
730 if (start_pfn == pfn)
731 continue;
732
733 /* If we find valid section, we have nothing to do */
734 return;
735 }
736
737 /* The pgdat has no valid section */
738 pgdat->node_start_pfn = 0;
739 pgdat->node_spanned_pages = 0;
740}
741
742static void __remove_zone(struct zone *zone, unsigned long start_pfn)
743{
744 struct pglist_data *pgdat = zone->zone_pgdat;
745 int nr_pages = PAGES_PER_SECTION;
746 int zone_type;
747 unsigned long flags;
748
749 zone_type = zone - pgdat->node_zones;
750
751 pgdat_resize_lock(zone->zone_pgdat, &flags);
752 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
753 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
754 pgdat_resize_unlock(zone->zone_pgdat, &flags);
755}
756
Dan Williams4b94ffd2016-01-15 16:56:22 -0800757static int __remove_section(struct zone *zone, struct mem_section *ms,
758 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700759{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800760 unsigned long start_pfn;
761 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700762 int ret = -EINVAL;
763
764 if (!valid_section(ms))
765 return ret;
766
767 ret = unregister_memory_section(ms);
768 if (ret)
769 return ret;
770
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800771 scn_nr = __section_nr(ms);
772 start_pfn = section_nr_to_pfn(scn_nr);
773 __remove_zone(zone, start_pfn);
774
Dan Williams4b94ffd2016-01-15 16:56:22 -0800775 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700776 return 0;
777}
778
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700779/**
780 * __remove_pages() - remove sections of pages from a zone
781 * @zone: zone from which pages need to be removed
782 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
783 * @nr_pages: number of pages to remove (must be multiple of section size)
784 *
785 * Generic helper function to remove section mappings and sysfs entries
786 * for the section of the memory we are removing. Caller needs to make
787 * sure that pages are marked reserved and zones are adjust properly by
788 * calling offline_pages().
789 */
790int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
791 unsigned long nr_pages)
792{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700793 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800794 unsigned long map_offset = 0;
795 int sections_to_remove, ret = 0;
796
797 /* In the ZONE_DEVICE case device driver owns the memory region */
798 if (is_dev_zone(zone)) {
799 struct page *page = pfn_to_page(phys_start_pfn);
800 struct vmem_altmap *altmap;
801
802 altmap = to_vmem_altmap((unsigned long) page);
803 if (altmap)
804 map_offset = vmem_altmap_offset(altmap);
805 } else {
806 resource_size_t start, size;
807
808 start = phys_start_pfn << PAGE_SHIFT;
809 size = nr_pages * PAGE_SIZE;
810
811 ret = release_mem_region_adjustable(&iomem_resource, start,
812 size);
813 if (ret) {
814 resource_size_t endres = start + size - 1;
815
816 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
817 &start, &endres, ret);
818 }
819 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700820
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700821 clear_zone_contiguous(zone);
822
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700823 /*
824 * We can only remove entire sections
825 */
826 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
827 BUG_ON(nr_pages % PAGES_PER_SECTION);
828
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700829 sections_to_remove = nr_pages / PAGES_PER_SECTION;
830 for (i = 0; i < sections_to_remove; i++) {
831 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800832
833 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
834 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700835 if (ret)
836 break;
837 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700838
839 set_zone_contiguous(zone);
840
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700841 return ret;
842}
843EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700844#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700845
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700846int set_online_page_callback(online_page_callback_t callback)
847{
848 int rc = -EINVAL;
849
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700850 get_online_mems();
851 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700852
853 if (online_page_callback == generic_online_page) {
854 online_page_callback = callback;
855 rc = 0;
856 }
857
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700858 mutex_unlock(&online_page_callback_lock);
859 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700860
861 return rc;
862}
863EXPORT_SYMBOL_GPL(set_online_page_callback);
864
865int restore_online_page_callback(online_page_callback_t callback)
866{
867 int rc = -EINVAL;
868
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700869 get_online_mems();
870 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700871
872 if (online_page_callback == callback) {
873 online_page_callback = generic_online_page;
874 rc = 0;
875 }
876
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700877 mutex_unlock(&online_page_callback_lock);
878 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700879
880 return rc;
881}
882EXPORT_SYMBOL_GPL(restore_online_page_callback);
883
884void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700885{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700886}
887EXPORT_SYMBOL_GPL(__online_page_set_limits);
888
889void __online_page_increment_counters(struct page *page)
890{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700891 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700892}
893EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700894
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700895void __online_page_free(struct page *page)
896{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700897 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700898}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700899EXPORT_SYMBOL_GPL(__online_page_free);
900
901static void generic_online_page(struct page *page)
902{
903 __online_page_set_limits(page);
904 __online_page_increment_counters(page);
905 __online_page_free(page);
906}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700907
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700908static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
909 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700910{
911 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700912 unsigned long onlined_pages = *(unsigned long *)arg;
913 struct page *page;
914 if (PageReserved(pfn_to_page(start_pfn)))
915 for (i = 0; i < nr_pages; i++) {
916 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700917 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700918 onlined_pages++;
919 }
920 *(unsigned long *)arg = onlined_pages;
921 return 0;
922}
923
Lai Jiangshan09285af2012-12-12 13:52:04 -0800924#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800925/*
926 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
927 * normal memory.
928 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800929static bool can_online_high_movable(struct zone *zone)
930{
931 return true;
932}
Tang Chen79a4dce2012-12-18 14:23:24 -0800933#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800934/* ensure every online node has NORMAL memory */
935static bool can_online_high_movable(struct zone *zone)
936{
937 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
938}
Tang Chen79a4dce2012-12-18 14:23:24 -0800939#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800940
Lai Jiangshand9713672012-12-11 16:01:03 -0800941/* check which state of node_states will be changed when online memory */
942static void node_states_check_changes_online(unsigned long nr_pages,
943 struct zone *zone, struct memory_notify *arg)
944{
945 int nid = zone_to_nid(zone);
946 enum zone_type zone_last = ZONE_NORMAL;
947
948 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800949 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
950 * contains nodes which have zones of 0...ZONE_NORMAL,
951 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800952 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800953 * If we don't have HIGHMEM nor movable node,
954 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
955 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800956 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800957 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800958 zone_last = ZONE_MOVABLE;
959
960 /*
961 * if the memory to be online is in a zone of 0...zone_last, and
962 * the zones of 0...zone_last don't have memory before online, we will
963 * need to set the node to node_states[N_NORMAL_MEMORY] after
964 * the memory is online.
965 */
966 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
967 arg->status_change_nid_normal = nid;
968 else
969 arg->status_change_nid_normal = -1;
970
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800971#ifdef CONFIG_HIGHMEM
972 /*
973 * If we have movable node, node_states[N_HIGH_MEMORY]
974 * contains nodes which have zones of 0...ZONE_HIGHMEM,
975 * set zone_last to ZONE_HIGHMEM.
976 *
977 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
978 * contains nodes which have zones of 0...ZONE_MOVABLE,
979 * set zone_last to ZONE_MOVABLE.
980 */
981 zone_last = ZONE_HIGHMEM;
982 if (N_MEMORY == N_HIGH_MEMORY)
983 zone_last = ZONE_MOVABLE;
984
985 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
986 arg->status_change_nid_high = nid;
987 else
988 arg->status_change_nid_high = -1;
989#else
990 arg->status_change_nid_high = arg->status_change_nid_normal;
991#endif
992
Lai Jiangshand9713672012-12-11 16:01:03 -0800993 /*
994 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800995 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800996 * is online.
997 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800998 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800999 arg->status_change_nid = nid;
1000 else
1001 arg->status_change_nid = -1;
1002}
1003
1004static void node_states_set_node(int node, struct memory_notify *arg)
1005{
1006 if (arg->status_change_nid_normal >= 0)
1007 node_set_state(node, N_NORMAL_MEMORY);
1008
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001009 if (arg->status_change_nid_high >= 0)
1010 node_set_state(node, N_HIGH_MEMORY);
1011
1012 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001013}
1014
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001015
David Rientjes30467e02015-04-14 15:45:11 -07001016/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001017int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001018{
Cody P Schaferaa472282013-07-03 15:02:10 -07001019 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001020 unsigned long onlined_pages = 0;
1021 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001022 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001023 int nid;
1024 int ret;
1025 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -07001026
Lai Jiangshand9713672012-12-11 16:01:03 -08001027 /*
1028 * This doesn't need a lock to do pfn_to_page().
1029 * The section can't be removed here because of the
1030 * memory_block->state_mutex.
1031 */
1032 zone = page_zone(pfn_to_page(pfn));
1033
Tang Chen4f7c6b42014-08-06 16:05:13 -07001034 if ((zone_idx(zone) > ZONE_NORMAL ||
1035 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001036 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001037 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001038
Tang Chen4f7c6b42014-08-06 16:05:13 -07001039 if (online_type == MMOP_ONLINE_KERNEL &&
1040 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001041 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001042 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001043 }
Tang Chen4f7c6b42014-08-06 16:05:13 -07001044 if (online_type == MMOP_ONLINE_MOVABLE &&
1045 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001046 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001047 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001048 }
1049
1050 /* Previous code may changed the zone of the pfn range */
1051 zone = page_zone(pfn_to_page(pfn));
1052
Yasunori Goto7b78d332007-10-21 16:41:36 -07001053 arg.start_pfn = pfn;
1054 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001055 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001056
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001057 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001058
1059 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1060 ret = notifier_to_errno(ret);
1061 if (ret) {
1062 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001063 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001064 }
Dave Hansen3947be12005-10-29 18:16:54 -07001065 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001066 * If this zone is not populated, then it is not in zonelist.
1067 * This means the page allocator ignores this zone.
1068 * So, zonelist must be updated after online.
1069 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001070 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001071 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001072 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001073 build_all_zonelists(NULL, zone);
1074 }
Yasunori Goto68113782006-06-23 02:03:11 -07001075
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001076 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001077 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001078 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001079 if (need_zonelists_rebuild)
1080 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001081 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001082 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1083 (unsigned long long) pfn << PAGE_SHIFT,
1084 (((unsigned long long) pfn + nr_pages)
1085 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001086 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001087 return ret;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001088 }
1089
Dave Hansen3947be12005-10-29 18:16:54 -07001090 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001091
1092 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001093 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001094 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1095
Jiang Liu08dff7b2012-07-31 16:43:30 -07001096 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001097 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001098 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001099 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001100 else
1101 zone_pcp_update(zone);
1102 }
Dave Hansen3947be12005-10-29 18:16:54 -07001103
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001104 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001105
1106 init_per_zone_wmark_min();
1107
Jiang Liu08dff7b2012-07-31 16:43:30 -07001108 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001109 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001110
Haicheng Li1f522502010-05-24 14:32:51 -07001111 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001112
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001113 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001114
1115 if (onlined_pages)
1116 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001117 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001118}
Keith Mannthey53947022006-09-30 23:27:08 -07001119#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001120
Tang Chen0bd85422014-11-13 15:19:41 -08001121static void reset_node_present_pages(pg_data_t *pgdat)
1122{
1123 struct zone *z;
1124
1125 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1126 z->present_pages = 0;
1127
1128 pgdat->node_present_pages = 0;
1129}
1130
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001131/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1132static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001133{
1134 struct pglist_data *pgdat;
1135 unsigned long zones_size[MAX_NR_ZONES] = {0};
1136 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001137 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001138
Tang Chena1e565a2013-02-22 16:33:18 -08001139 pgdat = NODE_DATA(nid);
1140 if (!pgdat) {
1141 pgdat = arch_alloc_nodedata(nid);
1142 if (!pgdat)
1143 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001144
Tang Chena1e565a2013-02-22 16:33:18 -08001145 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001146 } else {
1147 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1148 pgdat->nr_zones = 0;
1149 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001150 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001151
1152 /* we can use NODE_DATA(nid) from here */
1153
1154 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001155 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001156
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001157 /*
1158 * The node we allocated has no zone fallback lists. For avoiding
1159 * to access not-initialized zonelist, build here.
1160 */
David Rientjesf957db42011-06-22 18:13:04 -07001161 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001162 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001163 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001164
Tang Chenf784a3f2014-11-13 15:19:39 -08001165 /*
1166 * zone->managed_pages is set to an approximate value in
1167 * free_area_init_core(), which will cause
1168 * /sys/device/system/node/nodeX/meminfo has wrong data.
1169 * So reset it to 0 before any memory is onlined.
1170 */
1171 reset_node_managed_pages(pgdat);
1172
Tang Chen0bd85422014-11-13 15:19:41 -08001173 /*
1174 * When memory is hot-added, all the memory is in offline state. So
1175 * clear all zones' present_pages because they will be updated in
1176 * online_pages() and offline_pages().
1177 */
1178 reset_node_present_pages(pgdat);
1179
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001180 return pgdat;
1181}
1182
1183static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1184{
1185 arch_refresh_nodedata(nid, NULL);
1186 arch_free_nodedata(pgdat);
1187 return;
1188}
1189
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001190
Toshi Kani01b0f192013-11-12 15:07:25 -08001191/**
1192 * try_online_node - online a node if offlined
1193 *
minskey guocf234222010-05-24 14:32:41 -07001194 * called by cpu_up() to online a node without onlined memory.
1195 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001196int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001197{
1198 pg_data_t *pgdat;
1199 int ret;
1200
Toshi Kani01b0f192013-11-12 15:07:25 -08001201 if (node_online(nid))
1202 return 0;
1203
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001204 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001205 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001206 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001207 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001208 ret = -ENOMEM;
1209 goto out;
1210 }
1211 node_set_online(nid);
1212 ret = register_one_node(nid);
1213 BUG_ON(ret);
1214
Toshi Kani01b0f192013-11-12 15:07:25 -08001215 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1216 mutex_lock(&zonelists_mutex);
1217 build_all_zonelists(NULL, NULL);
1218 mutex_unlock(&zonelists_mutex);
1219 }
1220
minskey guocf234222010-05-24 14:32:41 -07001221out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001222 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001223 return ret;
1224}
1225
Toshi Kani27356f52013-09-11 14:21:49 -07001226static int check_hotplug_memory_range(u64 start, u64 size)
1227{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001228 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001229 u64 nr_pages = size >> PAGE_SHIFT;
1230
1231 /* Memory range must be aligned with section */
1232 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1233 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1234 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1235 (unsigned long long)start,
1236 (unsigned long long)size);
1237 return -EINVAL;
1238 }
1239
1240 return 0;
1241}
1242
Wang Nan63264402014-08-06 16:07:36 -07001243/*
1244 * If movable zone has already been setup, newly added memory should be check.
1245 * If its address is higher than movable zone, it should be added as movable.
1246 * Without this check, movable zone may overlap with other zone.
1247 */
1248static int should_add_memory_movable(int nid, u64 start, u64 size)
1249{
1250 unsigned long start_pfn = start >> PAGE_SHIFT;
1251 pg_data_t *pgdat = NODE_DATA(nid);
1252 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1253
1254 if (zone_is_empty(movable_zone))
1255 return 0;
1256
1257 if (movable_zone->zone_start_pfn <= start_pfn)
1258 return 1;
1259
1260 return 0;
1261}
1262
Dan Williams033fbae2015-08-09 15:29:06 -04001263int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1264 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001265{
Dan Williams033fbae2015-08-09 15:29:06 -04001266#ifdef CONFIG_ZONE_DEVICE
1267 if (for_device)
1268 return ZONE_DEVICE;
1269#endif
Wang Nan63264402014-08-06 16:07:36 -07001270 if (should_add_memory_movable(nid, start, size))
1271 return ZONE_MOVABLE;
1272
1273 return zone_default;
1274}
1275
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001276static int online_memory_block(struct memory_block *mem, void *arg)
1277{
1278 return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1279}
1280
Al Viro31168482008-11-22 17:33:24 +00001281/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001282int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001283{
David Vrabel62cedb92015-06-25 16:35:49 +01001284 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001285 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001286 bool new_pgdat;
1287 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001288 int ret;
1289
David Vrabel62cedb92015-06-25 16:35:49 +01001290 start = res->start;
1291 size = resource_size(res);
1292
Toshi Kani27356f52013-09-11 14:21:49 -07001293 ret = check_hotplug_memory_range(start, size);
1294 if (ret)
1295 return ret;
1296
Tang Chena1e565a2013-02-22 16:33:18 -08001297 { /* Stupid hack to suppress address-never-null warning */
1298 void *p = NODE_DATA(nid);
1299 new_pgdat = !p;
1300 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001301
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001302 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001303
Tang Chen7f36e3e2015-09-04 15:42:32 -07001304 /*
1305 * Add new range to memblock so that when hotadd_new_pgdat() is called
1306 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1307 * this new range and calculate total pages correctly. The range will
1308 * be removed at hot-remove time.
1309 */
1310 memblock_add_node(start, size, nid);
1311
Tang Chena1e565a2013-02-22 16:33:18 -08001312 new_node = !node_online(nid);
1313 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001314 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001315 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001316 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001317 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001318 }
1319
Yasunori Gotobc02af92006-06-27 02:53:30 -07001320 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001321 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001322
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001323 if (ret < 0)
1324 goto error;
1325
Yasunori Goto0fc44152006-06-27 02:53:38 -07001326 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001327 node_set_online(nid);
1328
Tang Chena1e565a2013-02-22 16:33:18 -08001329 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001330 ret = register_one_node(nid);
1331 /*
1332 * If sysfs file of new node can't create, cpu on the node
1333 * can't be hot-added. There is no rollback way now.
1334 * So, check by BUG_ON() to catch it reluctantly..
1335 */
1336 BUG_ON(ret);
1337 }
1338
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001339 /* create new memmap entry */
1340 firmware_map_add_hotplug(start, start + size, "System RAM");
1341
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001342 /* online pages if requested */
1343 if (online)
1344 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1345 NULL, online_memory_block);
1346
Andi Kleen6ad696d2009-11-17 14:06:22 -08001347 goto out;
1348
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001349error:
1350 /* rollback pgdat allocation and others */
1351 if (new_pgdat)
1352 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001353 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001354
Andi Kleen6ad696d2009-11-17 14:06:22 -08001355out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001356 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001357 return ret;
1358}
David Vrabel62cedb92015-06-25 16:35:49 +01001359EXPORT_SYMBOL_GPL(add_memory_resource);
1360
1361int __ref add_memory(int nid, u64 start, u64 size)
1362{
1363 struct resource *res;
1364 int ret;
1365
1366 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001367 if (IS_ERR(res))
1368 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001369
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001370 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001371 if (ret < 0)
1372 release_memory_resource(res);
1373 return ret;
1374}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001375EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001376
1377#ifdef CONFIG_MEMORY_HOTREMOVE
1378/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001379 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1380 * set and the size of the free page is given by page_order(). Using this,
1381 * the function determines if the pageblock contains only free pages.
1382 * Due to buddy contraints, a free page at least the size of a pageblock will
1383 * be located at the start of the pageblock
1384 */
1385static inline int pageblock_free(struct page *page)
1386{
1387 return PageBuddy(page) && page_order(page) >= pageblock_order;
1388}
1389
1390/* Return the start of the next active pageblock after a given page */
1391static struct page *next_active_pageblock(struct page *page)
1392{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001393 /* Ensure the starting page is pageblock-aligned */
1394 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1395
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001396 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001397 if (pageblock_free(page)) {
1398 int order;
1399 /* be careful. we don't have locks, page_order can be changed.*/
1400 order = page_order(page);
1401 if ((order < MAX_ORDER) && (order >= pageblock_order))
1402 return page + (1 << order);
1403 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001404
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001405 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001406}
1407
1408/* Checks if this range of memory is likely to be hot-removable. */
1409int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1410{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001411 struct page *page = pfn_to_page(start_pfn);
1412 struct page *end_page = page + nr_pages;
1413
1414 /* Check the starting page of each pageblock within the range */
1415 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001416 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001417 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001418 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001419 }
1420
1421 /* All pageblocks in the memory block are likely to be hot-removable */
1422 return 1;
1423}
1424
1425/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001426 * Confirm all pages in a range [start, end) is belongs to the same zone.
1427 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001428int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001429{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001430 unsigned long pfn, sec_end_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001431 struct zone *zone = NULL;
1432 struct page *page;
1433 int i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001434 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001435 pfn < end_pfn;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001436 pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1437 /* Make sure the memory section is present first */
1438 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001439 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001440 for (; pfn < sec_end_pfn && pfn < end_pfn;
1441 pfn += MAX_ORDER_NR_PAGES) {
1442 i = 0;
1443 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1444 while ((i < MAX_ORDER_NR_PAGES) &&
1445 !pfn_valid_within(pfn + i))
1446 i++;
1447 if (i == MAX_ORDER_NR_PAGES)
1448 continue;
1449 page = pfn_to_page(pfn + i);
1450 if (zone && page_zone(page) != zone)
1451 return 0;
1452 zone = page_zone(page);
1453 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001454 }
1455 return 1;
1456}
1457
1458/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001459 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1460 * and hugepages). We scan pfn because it's much easier than scanning over
1461 * linked list. This function returns the pfn of the first found movable
1462 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001463 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001464static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001465{
1466 unsigned long pfn;
1467 struct page *page;
1468 for (pfn = start; pfn < end; pfn++) {
1469 if (pfn_valid(pfn)) {
1470 page = pfn_to_page(pfn);
1471 if (PageLRU(page))
1472 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001473 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001474 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001475 return pfn;
1476 else
1477 pfn = round_up(pfn + 1,
1478 1 << compound_order(page)) - 1;
1479 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001480 }
1481 }
1482 return 0;
1483}
1484
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001485#define NR_OFFLINE_AT_ONCE_PAGES (256)
1486static int
1487do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1488{
1489 unsigned long pfn;
1490 struct page *page;
1491 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1492 int not_managed = 0;
1493 int ret = 0;
1494 LIST_HEAD(source);
1495
1496 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1497 if (!pfn_valid(pfn))
1498 continue;
1499 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001500
1501 if (PageHuge(page)) {
1502 struct page *head = compound_head(page);
1503 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1504 if (compound_order(head) > PFN_SECTION_SHIFT) {
1505 ret = -EBUSY;
1506 break;
1507 }
1508 if (isolate_huge_page(page, &source))
1509 move_pages -= 1 << compound_order(head);
1510 continue;
1511 }
1512
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001513 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001514 continue;
1515 /*
1516 * We can skip free pages. And we can only deal with pages on
1517 * LRU.
1518 */
Nick Piggin62695a82008-10-18 20:26:09 -07001519 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001520 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001521 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001522 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001523 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001524 inc_zone_page_state(page, NR_ISOLATED_ANON +
1525 page_is_file_cache(page));
1526
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001527 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001528#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001529 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1530 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001531 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001532#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001533 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001534 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001535 check this again here. */
1536 if (page_count(page)) {
1537 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001538 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001539 break;
1540 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001541 }
1542 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001543 if (!list_empty(&source)) {
1544 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001545 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001546 goto out;
1547 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001548
1549 /*
1550 * alloc_migrate_target should be improooooved!!
1551 * migrate_pages returns # of failed pages.
1552 */
David Rientjes68711a72014-06-04 16:08:25 -07001553 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001554 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001555 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001556 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001557 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001558out:
1559 return ret;
1560}
1561
1562/*
1563 * remove from free_area[] and mark all as Reserved.
1564 */
1565static int
1566offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1567 void *data)
1568{
1569 __offline_isolated_pages(start, start + nr_pages);
1570 return 0;
1571}
1572
1573static void
1574offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1575{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001576 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001577 offline_isolated_pages_cb);
1578}
1579
1580/*
1581 * Check all pages in range, recoreded as memory resource, are isolated.
1582 */
1583static int
1584check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1585 void *data)
1586{
1587 int ret;
1588 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001589 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001590 offlined = nr_pages;
1591 if (!ret)
1592 *(long *)data += offlined;
1593 return ret;
1594}
1595
1596static long
1597check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1598{
1599 long offlined = 0;
1600 int ret;
1601
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001602 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001603 check_pages_isolated_cb);
1604 if (ret < 0)
1605 offlined = (long)ret;
1606 return offlined;
1607}
1608
Lai Jiangshan09285af2012-12-12 13:52:04 -08001609#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001610/*
1611 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1612 * normal memory.
1613 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001614static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1615{
1616 return true;
1617}
Tang Chen79a4dce2012-12-18 14:23:24 -08001618#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001619/* ensure the node has NORMAL memory if it is still online */
1620static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1621{
1622 struct pglist_data *pgdat = zone->zone_pgdat;
1623 unsigned long present_pages = 0;
1624 enum zone_type zt;
1625
1626 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1627 present_pages += pgdat->node_zones[zt].present_pages;
1628
1629 if (present_pages > nr_pages)
1630 return true;
1631
1632 present_pages = 0;
1633 for (; zt <= ZONE_MOVABLE; zt++)
1634 present_pages += pgdat->node_zones[zt].present_pages;
1635
1636 /*
1637 * we can't offline the last normal memory until all
1638 * higher memory is offlined.
1639 */
1640 return present_pages == 0;
1641}
Tang Chen79a4dce2012-12-18 14:23:24 -08001642#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001643
Tang Chenc5320922013-11-12 15:08:10 -08001644static int __init cmdline_parse_movable_node(char *p)
1645{
1646#ifdef CONFIG_MOVABLE_NODE
1647 /*
1648 * Memory used by the kernel cannot be hot-removed because Linux
1649 * cannot migrate the kernel pages. When memory hotplug is
1650 * enabled, we should prevent memblock from allocating memory
1651 * for the kernel.
1652 *
1653 * ACPI SRAT records all hotpluggable memory ranges. But before
1654 * SRAT is parsed, we don't know about it.
1655 *
1656 * The kernel image is loaded into memory at very early time. We
1657 * cannot prevent this anyway. So on NUMA system, we set any
1658 * node the kernel resides in as un-hotpluggable.
1659 *
1660 * Since on modern servers, one node could have double-digit
1661 * gigabytes memory, we can assume the memory around the kernel
1662 * image is also un-hotpluggable. So before SRAT is parsed, just
1663 * allocate memory near the kernel image to try the best to keep
1664 * the kernel away from hotpluggable memory.
1665 */
1666 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001667 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001668#else
1669 pr_warn("movable_node option not supported\n");
1670#endif
1671 return 0;
1672}
1673early_param("movable_node", cmdline_parse_movable_node);
1674
Lai Jiangshand9713672012-12-11 16:01:03 -08001675/* check which state of node_states will be changed when offline memory */
1676static void node_states_check_changes_offline(unsigned long nr_pages,
1677 struct zone *zone, struct memory_notify *arg)
1678{
1679 struct pglist_data *pgdat = zone->zone_pgdat;
1680 unsigned long present_pages = 0;
1681 enum zone_type zt, zone_last = ZONE_NORMAL;
1682
1683 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001684 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1685 * contains nodes which have zones of 0...ZONE_NORMAL,
1686 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001687 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001688 * If we don't have HIGHMEM nor movable node,
1689 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1690 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001691 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001692 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001693 zone_last = ZONE_MOVABLE;
1694
1695 /*
1696 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1697 * If the memory to be offline is in a zone of 0...zone_last,
1698 * and it is the last present memory, 0...zone_last will
1699 * become empty after offline , thus we can determind we will
1700 * need to clear the node from node_states[N_NORMAL_MEMORY].
1701 */
1702 for (zt = 0; zt <= zone_last; zt++)
1703 present_pages += pgdat->node_zones[zt].present_pages;
1704 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1705 arg->status_change_nid_normal = zone_to_nid(zone);
1706 else
1707 arg->status_change_nid_normal = -1;
1708
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001709#ifdef CONFIG_HIGHMEM
1710 /*
1711 * If we have movable node, node_states[N_HIGH_MEMORY]
1712 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1713 * set zone_last to ZONE_HIGHMEM.
1714 *
1715 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1716 * contains nodes which have zones of 0...ZONE_MOVABLE,
1717 * set zone_last to ZONE_MOVABLE.
1718 */
1719 zone_last = ZONE_HIGHMEM;
1720 if (N_MEMORY == N_HIGH_MEMORY)
1721 zone_last = ZONE_MOVABLE;
1722
1723 for (; zt <= zone_last; zt++)
1724 present_pages += pgdat->node_zones[zt].present_pages;
1725 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1726 arg->status_change_nid_high = zone_to_nid(zone);
1727 else
1728 arg->status_change_nid_high = -1;
1729#else
1730 arg->status_change_nid_high = arg->status_change_nid_normal;
1731#endif
1732
Lai Jiangshand9713672012-12-11 16:01:03 -08001733 /*
1734 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1735 */
1736 zone_last = ZONE_MOVABLE;
1737
1738 /*
1739 * check whether node_states[N_HIGH_MEMORY] will be changed
1740 * If we try to offline the last present @nr_pages from the node,
1741 * we can determind we will need to clear the node from
1742 * node_states[N_HIGH_MEMORY].
1743 */
1744 for (; zt <= zone_last; zt++)
1745 present_pages += pgdat->node_zones[zt].present_pages;
1746 if (nr_pages >= present_pages)
1747 arg->status_change_nid = zone_to_nid(zone);
1748 else
1749 arg->status_change_nid = -1;
1750}
1751
1752static void node_states_clear_node(int node, struct memory_notify *arg)
1753{
1754 if (arg->status_change_nid_normal >= 0)
1755 node_clear_state(node, N_NORMAL_MEMORY);
1756
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001757 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1758 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001759 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001760
1761 if ((N_MEMORY != N_HIGH_MEMORY) &&
1762 (arg->status_change_nid >= 0))
1763 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001764}
1765
Wen Congyanga16cee12012-10-08 16:33:58 -07001766static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001767 unsigned long end_pfn, unsigned long timeout)
1768{
1769 unsigned long pfn, nr_pages, expire;
1770 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001771 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001772 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001773 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001774 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001775
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001776 /* at least, alignment against pageblock is necessary */
1777 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1778 return -EINVAL;
1779 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1780 return -EINVAL;
1781 /* This makes hotplug much easier...and readable.
1782 we assume this for now. .*/
1783 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1784 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001785
1786 zone = page_zone(pfn_to_page(start_pfn));
1787 node = zone_to_nid(zone);
1788 nr_pages = end_pfn - start_pfn;
1789
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001790 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001791 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001792
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001793 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001794 ret = start_isolate_page_range(start_pfn, end_pfn,
1795 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001796 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001797 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001798
1799 arg.start_pfn = start_pfn;
1800 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001801 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001802
1803 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1804 ret = notifier_to_errno(ret);
1805 if (ret)
1806 goto failed_removal;
1807
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001808 pfn = start_pfn;
1809 expire = jiffies + timeout;
1810 drain = 0;
1811 retry_max = 5;
1812repeat:
1813 /* start memory hot removal */
1814 ret = -EAGAIN;
1815 if (time_after(jiffies, expire))
1816 goto failed_removal;
1817 ret = -EINTR;
1818 if (signal_pending(current))
1819 goto failed_removal;
1820 ret = 0;
1821 if (drain) {
1822 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001823 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001824 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001825 }
1826
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001827 pfn = scan_movable_pages(start_pfn, end_pfn);
1828 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001829 ret = do_migrate_range(pfn, end_pfn);
1830 if (!ret) {
1831 drain = 1;
1832 goto repeat;
1833 } else {
1834 if (ret < 0)
1835 if (--retry_max == 0)
1836 goto failed_removal;
1837 yield();
1838 drain = 1;
1839 goto repeat;
1840 }
1841 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001842 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001843 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001844 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001845 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001846 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001847 /*
1848 * dissolve free hugepages in the memory block before doing offlining
1849 * actually in order to make hugetlbfs's object counting consistent.
1850 */
1851 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001852 /* check again */
1853 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1854 if (offlined_pages < 0) {
1855 ret = -EBUSY;
1856 goto failed_removal;
1857 }
1858 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001859 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001860 We cannot do rollback at this point. */
1861 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001862 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001863 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001864 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001865 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001866 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001867
1868 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001869 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001870 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001871
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001872 init_per_zone_wmark_min();
1873
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001874 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001875 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001876 mutex_lock(&zonelists_mutex);
1877 build_all_zonelists(NULL, NULL);
1878 mutex_unlock(&zonelists_mutex);
1879 } else
1880 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001881
Lai Jiangshand9713672012-12-11 16:01:03 -08001882 node_states_clear_node(node, &arg);
1883 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001884 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001885
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001886 vm_total_pages = nr_free_pagecache_pages();
1887 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001888
1889 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001890 return 0;
1891
1892failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001893 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1894 (unsigned long long) start_pfn << PAGE_SHIFT,
1895 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001896 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001897 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001898 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001899 return ret;
1900}
Badari Pulavarty71088782008-10-18 20:25:58 -07001901
David Rientjes30467e02015-04-14 15:45:11 -07001902/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001903int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1904{
1905 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1906}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001907#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001908
Wen Congyangbbc76be2013-02-22 16:32:54 -08001909/**
1910 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1911 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001912 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001913 * @arg: argument passed to func
1914 * @func: callback for each memory section walked
1915 *
1916 * This function walks through all present mem sections in range
1917 * [start_pfn, end_pfn) and call func on each mem section.
1918 *
1919 * Returns the return value of func.
1920 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001921int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001922 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001923{
Wen Congyange90bdb72012-10-08 16:34:01 -07001924 struct memory_block *mem = NULL;
1925 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001926 unsigned long pfn, section_nr;
1927 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001928
Wen Congyange90bdb72012-10-08 16:34:01 -07001929 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1930 section_nr = pfn_to_section_nr(pfn);
1931 if (!present_section_nr(section_nr))
1932 continue;
1933
1934 section = __nr_to_section(section_nr);
1935 /* same memblock? */
1936 if (mem)
1937 if ((section_nr >= mem->start_section_nr) &&
1938 (section_nr <= mem->end_section_nr))
1939 continue;
1940
1941 mem = find_memory_block_hinted(section, mem);
1942 if (!mem)
1943 continue;
1944
Wen Congyangbbc76be2013-02-22 16:32:54 -08001945 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001946 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001947 kobject_put(&mem->dev.kobj);
1948 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001949 }
1950 }
1951
1952 if (mem)
1953 kobject_put(&mem->dev.kobj);
1954
Wen Congyangbbc76be2013-02-22 16:32:54 -08001955 return 0;
1956}
1957
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001958#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001959static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001960{
1961 int ret = !is_memblock_offlined(mem);
1962
Randy Dunlap349daa02013-04-29 15:08:49 -07001963 if (unlikely(ret)) {
1964 phys_addr_t beginpa, endpa;
1965
1966 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1967 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001968 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001969 "[%pa-%pa] is onlined\n",
1970 &beginpa, &endpa);
1971 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001972
1973 return ret;
1974}
1975
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001976static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001977{
Tang Chen60a5a192013-02-22 16:33:14 -08001978 int cpu;
1979
1980 for_each_present_cpu(cpu) {
1981 if (cpu_to_node(cpu) == pgdat->node_id)
1982 /*
1983 * the cpu on this node isn't removed, and we can't
1984 * offline this node.
1985 */
1986 return -EBUSY;
1987 }
1988
1989 return 0;
1990}
1991
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001992static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001993{
1994#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001995 int cpu;
1996
1997 for_each_possible_cpu(cpu)
1998 if (cpu_to_node(cpu) == pgdat->node_id)
1999 numa_clear_node(cpu);
2000#endif
2001}
2002
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002003static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08002004{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002005 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08002006
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002007 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002008 if (ret)
2009 return ret;
2010
2011 /*
2012 * the node will be offlined when we come here, so we can clear
2013 * the cpu_to_node() now.
2014 */
2015
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002016 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002017 return 0;
2018}
2019
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002020/**
2021 * try_offline_node
2022 *
2023 * Offline a node if all memory sections and cpus of the node are removed.
2024 *
2025 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2026 * and online/offline operations before this call.
2027 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002028void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002029{
Wen Congyangd822b862013-02-22 16:33:16 -08002030 pg_data_t *pgdat = NODE_DATA(nid);
2031 unsigned long start_pfn = pgdat->node_start_pfn;
2032 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002033 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08002034 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08002035
2036 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2037 unsigned long section_nr = pfn_to_section_nr(pfn);
2038
2039 if (!present_section_nr(section_nr))
2040 continue;
2041
2042 if (pfn_to_nid(pfn) != nid)
2043 continue;
2044
2045 /*
2046 * some memory sections of this node are not removed, and we
2047 * can't offline node now.
2048 */
2049 return;
2050 }
2051
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002052 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002053 return;
2054
2055 /*
2056 * all memory/cpu of this node are removed, we can offline this
2057 * node now.
2058 */
2059 node_set_offline(nid);
2060 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08002061
Wen Congyangd822b862013-02-22 16:33:16 -08002062 /* free waittable in each zone */
2063 for (i = 0; i < MAX_NR_ZONES; i++) {
2064 struct zone *zone = pgdat->node_zones + i;
2065
Jianguo Wuca4b3f32013-03-22 15:04:50 -07002066 /*
2067 * wait_table may be allocated from boot memory,
2068 * here only free if it's allocated by vmalloc.
2069 */
Gu Zheng85bd8392015-06-10 11:14:43 -07002070 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08002071 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07002072 zone->wait_table = NULL;
2073 }
Wen Congyangd822b862013-02-22 16:33:16 -08002074 }
Tang Chen60a5a192013-02-22 16:33:14 -08002075}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002076EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002077
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002078/**
2079 * remove_memory
2080 *
2081 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2082 * and online/offline operations before this call, as required by
2083 * try_offline_node().
2084 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002085void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002086{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002087 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002088
Toshi Kani27356f52013-09-11 14:21:49 -07002089 BUG_ON(check_hotplug_memory_range(start, size));
2090
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002091 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002092
2093 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002094 * All memory blocks must be offlined before removing memory. Check
2095 * whether all memory blocks in question are offline and trigger a BUG()
2096 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002097 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002098 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002099 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002100 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002101 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002102
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002103 /* remove memmap entry */
2104 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002105 memblock_free(start, size);
2106 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002107
Wen Congyang24d335c2013-02-22 16:32:58 -08002108 arch_remove_memory(start, size);
2109
Tang Chen60a5a192013-02-22 16:33:14 -08002110 try_offline_node(nid);
2111
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002112 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002113}
Badari Pulavarty71088782008-10-18 20:25:58 -07002114EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002115#endif /* CONFIG_MEMORY_HOTREMOVE */