blob: 24e4c76c951be7704a7bdb386d24560e41bf22ed [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>
20#include <linux/memory_hotplug.h>
21#include <linux/highmem.h>
22#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070023#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070024#include <linux/delay.h>
25#include <linux/migrate.h>
26#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070027#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080028#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080029#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080030#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080031#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070032#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080033#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080034#include <linux/bootmem.h>
Dave Hansen3947be12005-10-29 18:16:54 -070035
36#include <asm/tlbflush.h>
37
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030038#include "internal.h"
39
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070040/*
41 * online_page_callback contains pointer to current page onlining function.
42 * Initially it is generic_online_page(). If it is required it could be
43 * changed by calling set_online_page_callback() for callback registration
44 * and restore_online_page_callback() for generic callback restore.
45 */
46
47static void generic_online_page(struct page *page);
48
49static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070050static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070051
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070052/* The same as the cpu_hotplug lock, but for memory hotplug. */
53static struct {
54 struct task_struct *active_writer;
55 struct mutex lock; /* Synchronizes accesses to refcount, */
56 /*
57 * Also blocks the new readers during
58 * an ongoing mem hotplug operation.
59 */
60 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080061
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070062#ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map;
64#endif
65} mem_hotplug = {
66 .active_writer = NULL,
67 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
68 .refcount = 0,
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map = {.name = "mem_hotplug.lock" },
71#endif
72};
73
74/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
75#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
76#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
77#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
78
79void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080080{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070081 might_sleep();
82 if (mem_hotplug.active_writer == current)
83 return;
84 memhp_lock_acquire_read();
85 mutex_lock(&mem_hotplug.lock);
86 mem_hotplug.refcount++;
87 mutex_unlock(&mem_hotplug.lock);
88
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080089}
90
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070091void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080092{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070093 if (mem_hotplug.active_writer == current)
94 return;
95 mutex_lock(&mem_hotplug.lock);
96
97 if (WARN_ON(!mem_hotplug.refcount))
98 mem_hotplug.refcount++; /* try to fix things up */
99
100 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
101 wake_up_process(mem_hotplug.active_writer);
102 mutex_unlock(&mem_hotplug.lock);
103 memhp_lock_release();
104
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800105}
106
David Rientjes30467e02015-04-14 15:45:11 -0700107void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700108{
109 mem_hotplug.active_writer = current;
110
111 memhp_lock_acquire();
112 for (;;) {
113 mutex_lock(&mem_hotplug.lock);
114 if (likely(!mem_hotplug.refcount))
115 break;
116 __set_current_state(TASK_UNINTERRUPTIBLE);
117 mutex_unlock(&mem_hotplug.lock);
118 schedule();
119 }
120}
121
David Rientjes30467e02015-04-14 15:45:11 -0700122void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700123{
124 mem_hotplug.active_writer = NULL;
125 mutex_unlock(&mem_hotplug.lock);
126 memhp_lock_release();
127}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800128
Keith Mannthey45e0b782006-09-30 23:27:09 -0700129/* add this memory to iomem resource */
130static struct resource *register_memory_resource(u64 start, u64 size)
131{
132 struct resource *res;
133 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
134 BUG_ON(!res);
135
136 res->name = "System RAM";
137 res->start = start;
138 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800139 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700140 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700141 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700142 kfree(res);
143 res = NULL;
144 }
145 return res;
146}
147
148static void release_memory_resource(struct resource *res)
149{
150 if (!res)
151 return;
152 release_resource(res);
153 kfree(res);
154 return;
155}
156
Keith Mannthey53947022006-09-30 23:27:08 -0700157#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800158void get_page_bootmem(unsigned long info, struct page *page,
159 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700160{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800161 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700162 SetPagePrivate(page);
163 set_page_private(page, info);
164 atomic_inc(&page->_count);
165}
166
Jiang Liu170a5a72013-07-03 15:03:17 -0700167void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700168{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800169 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700170
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800171 type = (unsigned long) page->lru.next;
172 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
173 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700174
175 if (atomic_dec_return(&page->_count) == 1) {
176 ClearPagePrivate(page);
177 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800178 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700179 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700180 }
Yasunori Goto04753272008-04-28 02:13:31 -0700181}
182
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800183#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
184#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700185static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700186{
187 unsigned long *usemap, mapsize, section_nr, i;
188 struct mem_section *ms;
189 struct page *page, *memmap;
190
Yasunori Goto04753272008-04-28 02:13:31 -0700191 section_nr = pfn_to_section_nr(start_pfn);
192 ms = __nr_to_section(section_nr);
193
194 /* Get section's memmap address */
195 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
196
197 /*
198 * Get page for the memmap's phys address
199 * XXX: need more consideration for sparse_vmemmap...
200 */
201 page = virt_to_page(memmap);
202 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
203 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
204
205 /* remember memmap's page */
206 for (i = 0; i < mapsize; i++, page++)
207 get_page_bootmem(section_nr, page, SECTION_INFO);
208
209 usemap = __nr_to_section(section_nr)->pageblock_flags;
210 page = virt_to_page(usemap);
211
212 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
213
214 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700215 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700216
217}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800218#else /* CONFIG_SPARSEMEM_VMEMMAP */
219static void register_page_bootmem_info_section(unsigned long start_pfn)
220{
221 unsigned long *usemap, mapsize, section_nr, i;
222 struct mem_section *ms;
223 struct page *page, *memmap;
224
225 if (!pfn_valid(start_pfn))
226 return;
227
228 section_nr = pfn_to_section_nr(start_pfn);
229 ms = __nr_to_section(section_nr);
230
231 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
232
233 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
234
235 usemap = __nr_to_section(section_nr)->pageblock_flags;
236 page = virt_to_page(usemap);
237
238 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
239
240 for (i = 0; i < mapsize; i++, page++)
241 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
242}
243#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700244
245void register_page_bootmem_info_node(struct pglist_data *pgdat)
246{
247 unsigned long i, pfn, end_pfn, nr_pages;
248 int node = pgdat->node_id;
249 struct page *page;
250 struct zone *zone;
251
252 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
253 page = virt_to_page(pgdat);
254
255 for (i = 0; i < nr_pages; i++, page++)
256 get_page_bootmem(node, page, NODE_INFO);
257
258 zone = &pgdat->node_zones[0];
259 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700260 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700261 nr_pages = zone->wait_table_hash_nr_entries
262 * sizeof(wait_queue_head_t);
263 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
264 page = virt_to_page(zone->wait_table);
265
266 for (i = 0; i < nr_pages; i++, page++)
267 get_page_bootmem(node, page, NODE_INFO);
268 }
269 }
270
271 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800272 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700273
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700274 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700275 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
276 /*
277 * Some platforms can assign the same pfn to multiple nodes - on
278 * node0 as well as nodeN. To avoid registering a pfn against
279 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700280 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700281 */
282 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
283 register_page_bootmem_info_section(pfn);
284 }
Yasunori Goto04753272008-04-28 02:13:31 -0700285}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800286#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700287
Fabian Frederickf2765402014-08-06 16:04:57 -0700288static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
289 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700290{
291 unsigned long old_zone_end_pfn;
292
293 zone_span_writelock(zone);
294
Xishi Qiuc33bc312013-09-11 14:21:44 -0700295 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700296 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700297 zone->zone_start_pfn = start_pfn;
298
299 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
300 zone->zone_start_pfn;
301
302 zone_span_writeunlock(zone);
303}
304
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800305static void resize_zone(struct zone *zone, unsigned long start_pfn,
306 unsigned long end_pfn)
307{
308 zone_span_writelock(zone);
309
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800310 if (end_pfn - start_pfn) {
311 zone->zone_start_pfn = start_pfn;
312 zone->spanned_pages = end_pfn - start_pfn;
313 } else {
314 /*
315 * make it consist as free_area_init_core(),
316 * if spanned_pages = 0, then keep start_pfn = 0
317 */
318 zone->zone_start_pfn = 0;
319 zone->spanned_pages = 0;
320 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800321
322 zone_span_writeunlock(zone);
323}
324
325static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
326 unsigned long end_pfn)
327{
328 enum zone_type zid = zone_idx(zone);
329 int nid = zone->zone_pgdat->node_id;
330 unsigned long pfn;
331
332 for (pfn = start_pfn; pfn < end_pfn; pfn++)
333 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
334}
335
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800336/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800337 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800338static int __ref ensure_zone_is_initialized(struct zone *zone,
339 unsigned long start_pfn, unsigned long num_pages)
340{
341 if (!zone_is_initialized(zone))
342 return init_currently_empty_zone(zone, start_pfn, num_pages,
343 MEMMAP_HOTPLUG);
344 return 0;
345}
346
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800347static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800348 unsigned long start_pfn, unsigned long end_pfn)
349{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800350 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800351 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800352 unsigned long z1_start_pfn;
353
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800354 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
355 if (ret)
356 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800357
358 pgdat_resize_lock(z1->zone_pgdat, &flags);
359
360 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800361 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700363 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800364 if (start_pfn > z2->zone_start_pfn)
365 goto out_fail;
366 /* must included/overlap */
367 if (end_pfn <= z2->zone_start_pfn)
368 goto out_fail;
369
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800370 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700371 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800372 z1_start_pfn = z1->zone_start_pfn;
373 else
374 z1_start_pfn = start_pfn;
375
376 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800377 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800378
379 pgdat_resize_unlock(z1->zone_pgdat, &flags);
380
381 fix_zone_id(z1, start_pfn, end_pfn);
382
383 return 0;
384out_fail:
385 pgdat_resize_unlock(z1->zone_pgdat, &flags);
386 return -1;
387}
388
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800389static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800390 unsigned long start_pfn, unsigned long end_pfn)
391{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800392 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800393 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800394 unsigned long z2_end_pfn;
395
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800396 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
397 if (ret)
398 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800399
400 pgdat_resize_lock(z1->zone_pgdat, &flags);
401
402 /* can't move pfns which are lower than @z1 */
403 if (z1->zone_start_pfn > start_pfn)
404 goto out_fail;
405 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800406 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800407 goto out_fail;
408 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800409 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800410 goto out_fail;
411
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800412 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700413 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800415 else
416 z2_end_pfn = end_pfn;
417
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800418 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800419 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800420
421 pgdat_resize_unlock(z1->zone_pgdat, &flags);
422
423 fix_zone_id(z2, start_pfn, end_pfn);
424
425 return 0;
426out_fail:
427 pgdat_resize_unlock(z1->zone_pgdat, &flags);
428 return -1;
429}
430
Fabian Frederickf2765402014-08-06 16:04:57 -0700431static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
432 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700433{
Xishi Qiu83285c72013-11-12 15:07:19 -0800434 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700435
Tang Chen712cd382012-12-11 16:01:07 -0800436 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700437 pgdat->node_start_pfn = start_pfn;
438
439 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
440 pgdat->node_start_pfn;
441}
442
Al Viro31168482008-11-22 17:33:24 +0000443static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700444{
445 struct pglist_data *pgdat = zone->zone_pgdat;
446 int nr_pages = PAGES_PER_SECTION;
447 int nid = pgdat->node_id;
448 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700449 unsigned long flags;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800450 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700451
452 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800453 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
454 if (ret)
455 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700456
Heiko Carstens76cdd582008-05-14 16:05:52 -0700457 pgdat_resize_lock(zone->zone_pgdat, &flags);
458 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
459 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
460 phys_start_pfn + nr_pages);
461 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800462 memmap_init_zone(nr_pages, nid, zone_type,
463 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700464 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700465}
466
Gary Hadec04fc582009-01-06 14:39:14 -0800467static int __meminit __add_section(int nid, struct zone *zone,
468 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700469{
Dave Hansen3947be12005-10-29 18:16:54 -0700470 int ret;
471
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700472 if (pfn_valid(phys_start_pfn))
473 return -EEXIST;
474
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800475 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700476
477 if (ret < 0)
478 return ret;
479
Yasunori Goto718127c2006-06-23 02:03:10 -0700480 ret = __add_zone(zone, phys_start_pfn);
481
482 if (ret < 0)
483 return ret;
484
Gary Hadec04fc582009-01-06 14:39:14 -0800485 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700486}
487
David Rientjes4edd7ce2013-04-29 15:08:22 -0700488/*
489 * Reasonably generic function for adding memory. It is
490 * expected that archs that support memory hotplug will
491 * call this function after deciding the zone to which to
492 * add the new pages.
493 */
494int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
495 unsigned long nr_pages)
496{
497 unsigned long i;
498 int err = 0;
499 int start_sec, end_sec;
500 /* during initialize mem_map, align hot-added range to section */
501 start_sec = pfn_to_section_nr(phys_start_pfn);
502 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
503
504 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700505 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700506
507 /*
508 * EEXIST is finally dealt with by ioresource collision
509 * check. see add_memory() => register_memory_resource()
510 * Warning will be printed if there is collision.
511 */
512 if (err && (err != -EEXIST))
513 break;
514 err = 0;
515 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700516 vmemmap_populate_print_last();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700517
518 return err;
519}
520EXPORT_SYMBOL_GPL(__add_pages);
521
522#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800523/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
524static int find_smallest_section_pfn(int nid, struct zone *zone,
525 unsigned long start_pfn,
526 unsigned long end_pfn)
527{
528 struct mem_section *ms;
529
530 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
531 ms = __pfn_to_section(start_pfn);
532
533 if (unlikely(!valid_section(ms)))
534 continue;
535
536 if (unlikely(pfn_to_nid(start_pfn) != nid))
537 continue;
538
539 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
540 continue;
541
542 return start_pfn;
543 }
544
545 return 0;
546}
547
548/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
549static int find_biggest_section_pfn(int nid, struct zone *zone,
550 unsigned long start_pfn,
551 unsigned long end_pfn)
552{
553 struct mem_section *ms;
554 unsigned long pfn;
555
556 /* pfn is the end pfn of a memory section. */
557 pfn = end_pfn - 1;
558 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
559 ms = __pfn_to_section(pfn);
560
561 if (unlikely(!valid_section(ms)))
562 continue;
563
564 if (unlikely(pfn_to_nid(pfn) != nid))
565 continue;
566
567 if (zone && zone != page_zone(pfn_to_page(pfn)))
568 continue;
569
570 return pfn;
571 }
572
573 return 0;
574}
575
576static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
577 unsigned long end_pfn)
578{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700579 unsigned long zone_start_pfn = zone->zone_start_pfn;
580 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
581 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800582 unsigned long pfn;
583 struct mem_section *ms;
584 int nid = zone_to_nid(zone);
585
586 zone_span_writelock(zone);
587 if (zone_start_pfn == start_pfn) {
588 /*
589 * If the section is smallest section in the zone, it need
590 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
591 * In this case, we find second smallest valid mem_section
592 * for shrinking zone.
593 */
594 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
595 zone_end_pfn);
596 if (pfn) {
597 zone->zone_start_pfn = pfn;
598 zone->spanned_pages = zone_end_pfn - pfn;
599 }
600 } else if (zone_end_pfn == end_pfn) {
601 /*
602 * If the section is biggest section in the zone, it need
603 * shrink zone->spanned_pages.
604 * In this case, we find second biggest valid mem_section for
605 * shrinking zone.
606 */
607 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
608 start_pfn);
609 if (pfn)
610 zone->spanned_pages = pfn - zone_start_pfn + 1;
611 }
612
613 /*
614 * The section is not biggest or smallest mem_section in the zone, it
615 * only creates a hole in the zone. So in this case, we need not
616 * change the zone. But perhaps, the zone has only hole data. Thus
617 * it check the zone has only hole or not.
618 */
619 pfn = zone_start_pfn;
620 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
621 ms = __pfn_to_section(pfn);
622
623 if (unlikely(!valid_section(ms)))
624 continue;
625
626 if (page_zone(pfn_to_page(pfn)) != zone)
627 continue;
628
629 /* If the section is current section, it continues the loop */
630 if (start_pfn == pfn)
631 continue;
632
633 /* If we find valid section, we have nothing to do */
634 zone_span_writeunlock(zone);
635 return;
636 }
637
638 /* The zone has no valid section */
639 zone->zone_start_pfn = 0;
640 zone->spanned_pages = 0;
641 zone_span_writeunlock(zone);
642}
643
644static void shrink_pgdat_span(struct pglist_data *pgdat,
645 unsigned long start_pfn, unsigned long end_pfn)
646{
Xishi Qiu83285c72013-11-12 15:07:19 -0800647 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
648 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
649 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800650 unsigned long pfn;
651 struct mem_section *ms;
652 int nid = pgdat->node_id;
653
654 if (pgdat_start_pfn == start_pfn) {
655 /*
656 * If the section is smallest section in the pgdat, it need
657 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
658 * In this case, we find second smallest valid mem_section
659 * for shrinking zone.
660 */
661 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
662 pgdat_end_pfn);
663 if (pfn) {
664 pgdat->node_start_pfn = pfn;
665 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
666 }
667 } else if (pgdat_end_pfn == end_pfn) {
668 /*
669 * If the section is biggest section in the pgdat, it need
670 * shrink pgdat->node_spanned_pages.
671 * In this case, we find second biggest valid mem_section for
672 * shrinking zone.
673 */
674 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
675 start_pfn);
676 if (pfn)
677 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
678 }
679
680 /*
681 * If the section is not biggest or smallest mem_section in the pgdat,
682 * it only creates a hole in the pgdat. So in this case, we need not
683 * change the pgdat.
684 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
685 * has only hole or not.
686 */
687 pfn = pgdat_start_pfn;
688 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
689 ms = __pfn_to_section(pfn);
690
691 if (unlikely(!valid_section(ms)))
692 continue;
693
694 if (pfn_to_nid(pfn) != nid)
695 continue;
696
697 /* If the section is current section, it continues the loop */
698 if (start_pfn == pfn)
699 continue;
700
701 /* If we find valid section, we have nothing to do */
702 return;
703 }
704
705 /* The pgdat has no valid section */
706 pgdat->node_start_pfn = 0;
707 pgdat->node_spanned_pages = 0;
708}
709
710static void __remove_zone(struct zone *zone, unsigned long start_pfn)
711{
712 struct pglist_data *pgdat = zone->zone_pgdat;
713 int nr_pages = PAGES_PER_SECTION;
714 int zone_type;
715 unsigned long flags;
716
717 zone_type = zone - pgdat->node_zones;
718
719 pgdat_resize_lock(zone->zone_pgdat, &flags);
720 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
721 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
722 pgdat_resize_unlock(zone->zone_pgdat, &flags);
723}
724
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700725static int __remove_section(struct zone *zone, struct mem_section *ms)
726{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800727 unsigned long start_pfn;
728 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700729 int ret = -EINVAL;
730
731 if (!valid_section(ms))
732 return ret;
733
734 ret = unregister_memory_section(ms);
735 if (ret)
736 return ret;
737
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800738 scn_nr = __section_nr(ms);
739 start_pfn = section_nr_to_pfn(scn_nr);
740 __remove_zone(zone, start_pfn);
741
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700742 sparse_remove_one_section(zone, ms);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700743 return 0;
744}
745
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700746/**
747 * __remove_pages() - remove sections of pages from a zone
748 * @zone: zone from which pages need to be removed
749 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
750 * @nr_pages: number of pages to remove (must be multiple of section size)
751 *
752 * Generic helper function to remove section mappings and sysfs entries
753 * for the section of the memory we are removing. Caller needs to make
754 * sure that pages are marked reserved and zones are adjust properly by
755 * calling offline_pages().
756 */
757int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
758 unsigned long nr_pages)
759{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700760 unsigned long i;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700761 int sections_to_remove;
Toshi Kanife74ebb2013-04-29 15:08:20 -0700762 resource_size_t start, size;
763 int ret = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700764
765 /*
766 * We can only remove entire sections
767 */
768 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
769 BUG_ON(nr_pages % PAGES_PER_SECTION);
770
Toshi Kanife74ebb2013-04-29 15:08:20 -0700771 start = phys_start_pfn << PAGE_SHIFT;
772 size = nr_pages * PAGE_SIZE;
Dan Williams033fbae2015-08-09 15:29:06 -0400773
774 /* in the ZONE_DEVICE case device driver owns the memory region */
775 if (!is_dev_zone(zone))
776 ret = release_mem_region_adjustable(&iomem_resource, start, size);
Randy Dunlap348f9f02013-05-24 15:55:30 -0700777 if (ret) {
778 resource_size_t endres = start + size - 1;
779
780 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
781 &start, &endres, ret);
782 }
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700783
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700784 sections_to_remove = nr_pages / PAGES_PER_SECTION;
785 for (i = 0; i < sections_to_remove; i++) {
786 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
787 ret = __remove_section(zone, __pfn_to_section(pfn));
788 if (ret)
789 break;
790 }
791 return ret;
792}
793EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700794#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700795
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700796int set_online_page_callback(online_page_callback_t callback)
797{
798 int rc = -EINVAL;
799
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700800 get_online_mems();
801 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700802
803 if (online_page_callback == generic_online_page) {
804 online_page_callback = callback;
805 rc = 0;
806 }
807
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700808 mutex_unlock(&online_page_callback_lock);
809 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700810
811 return rc;
812}
813EXPORT_SYMBOL_GPL(set_online_page_callback);
814
815int restore_online_page_callback(online_page_callback_t callback)
816{
817 int rc = -EINVAL;
818
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700819 get_online_mems();
820 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700821
822 if (online_page_callback == callback) {
823 online_page_callback = generic_online_page;
824 rc = 0;
825 }
826
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700827 mutex_unlock(&online_page_callback_lock);
828 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700829
830 return rc;
831}
832EXPORT_SYMBOL_GPL(restore_online_page_callback);
833
834void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700835{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700836}
837EXPORT_SYMBOL_GPL(__online_page_set_limits);
838
839void __online_page_increment_counters(struct page *page)
840{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700841 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700842}
843EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700844
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700845void __online_page_free(struct page *page)
846{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700847 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700848}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700849EXPORT_SYMBOL_GPL(__online_page_free);
850
851static void generic_online_page(struct page *page)
852{
853 __online_page_set_limits(page);
854 __online_page_increment_counters(page);
855 __online_page_free(page);
856}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700857
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700858static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
859 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700860{
861 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700862 unsigned long onlined_pages = *(unsigned long *)arg;
863 struct page *page;
864 if (PageReserved(pfn_to_page(start_pfn)))
865 for (i = 0; i < nr_pages; i++) {
866 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700867 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700868 onlined_pages++;
869 }
870 *(unsigned long *)arg = onlined_pages;
871 return 0;
872}
873
Lai Jiangshan09285af2012-12-12 13:52:04 -0800874#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800875/*
876 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
877 * normal memory.
878 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800879static bool can_online_high_movable(struct zone *zone)
880{
881 return true;
882}
Tang Chen79a4dce2012-12-18 14:23:24 -0800883#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800884/* ensure every online node has NORMAL memory */
885static bool can_online_high_movable(struct zone *zone)
886{
887 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
888}
Tang Chen79a4dce2012-12-18 14:23:24 -0800889#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800890
Lai Jiangshand9713672012-12-11 16:01:03 -0800891/* check which state of node_states will be changed when online memory */
892static void node_states_check_changes_online(unsigned long nr_pages,
893 struct zone *zone, struct memory_notify *arg)
894{
895 int nid = zone_to_nid(zone);
896 enum zone_type zone_last = ZONE_NORMAL;
897
898 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800899 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
900 * contains nodes which have zones of 0...ZONE_NORMAL,
901 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800902 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800903 * If we don't have HIGHMEM nor movable node,
904 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
905 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800906 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800907 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800908 zone_last = ZONE_MOVABLE;
909
910 /*
911 * if the memory to be online is in a zone of 0...zone_last, and
912 * the zones of 0...zone_last don't have memory before online, we will
913 * need to set the node to node_states[N_NORMAL_MEMORY] after
914 * the memory is online.
915 */
916 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
917 arg->status_change_nid_normal = nid;
918 else
919 arg->status_change_nid_normal = -1;
920
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800921#ifdef CONFIG_HIGHMEM
922 /*
923 * If we have movable node, node_states[N_HIGH_MEMORY]
924 * contains nodes which have zones of 0...ZONE_HIGHMEM,
925 * set zone_last to ZONE_HIGHMEM.
926 *
927 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
928 * contains nodes which have zones of 0...ZONE_MOVABLE,
929 * set zone_last to ZONE_MOVABLE.
930 */
931 zone_last = ZONE_HIGHMEM;
932 if (N_MEMORY == N_HIGH_MEMORY)
933 zone_last = ZONE_MOVABLE;
934
935 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
936 arg->status_change_nid_high = nid;
937 else
938 arg->status_change_nid_high = -1;
939#else
940 arg->status_change_nid_high = arg->status_change_nid_normal;
941#endif
942
Lai Jiangshand9713672012-12-11 16:01:03 -0800943 /*
944 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800945 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800946 * is online.
947 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800948 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800949 arg->status_change_nid = nid;
950 else
951 arg->status_change_nid = -1;
952}
953
954static void node_states_set_node(int node, struct memory_notify *arg)
955{
956 if (arg->status_change_nid_normal >= 0)
957 node_set_state(node, N_NORMAL_MEMORY);
958
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800959 if (arg->status_change_nid_high >= 0)
960 node_set_state(node, N_HIGH_MEMORY);
961
962 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800963}
964
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700965
David Rientjes30467e02015-04-14 15:45:11 -0700966/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800967int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700968{
Cody P Schaferaa472282013-07-03 15:02:10 -0700969 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700970 unsigned long onlined_pages = 0;
971 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700972 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700973 int nid;
974 int ret;
975 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700976
Lai Jiangshand9713672012-12-11 16:01:03 -0800977 /*
978 * This doesn't need a lock to do pfn_to_page().
979 * The section can't be removed here because of the
980 * memory_block->state_mutex.
981 */
982 zone = page_zone(pfn_to_page(pfn));
983
Tang Chen4f7c6b42014-08-06 16:05:13 -0700984 if ((zone_idx(zone) > ZONE_NORMAL ||
985 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700986 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -0700987 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800988
Tang Chen4f7c6b42014-08-06 16:05:13 -0700989 if (online_type == MMOP_ONLINE_KERNEL &&
990 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700991 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -0700992 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800993 }
Tang Chen4f7c6b42014-08-06 16:05:13 -0700994 if (online_type == MMOP_ONLINE_MOVABLE &&
995 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700996 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -0700997 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800998 }
999
1000 /* Previous code may changed the zone of the pfn range */
1001 zone = page_zone(pfn_to_page(pfn));
1002
Yasunori Goto7b78d332007-10-21 16:41:36 -07001003 arg.start_pfn = pfn;
1004 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001005 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001006
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001007 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001008
1009 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1010 ret = notifier_to_errno(ret);
1011 if (ret) {
1012 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001013 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001014 }
Dave Hansen3947be12005-10-29 18:16:54 -07001015 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001016 * If this zone is not populated, then it is not in zonelist.
1017 * This means the page allocator ignores this zone.
1018 * So, zonelist must be updated after online.
1019 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001020 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001021 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001022 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001023 build_all_zonelists(NULL, zone);
1024 }
Yasunori Goto68113782006-06-23 02:03:11 -07001025
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001026 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001027 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001028 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001029 if (need_zonelists_rebuild)
1030 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001031 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001032 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1033 (unsigned long long) pfn << PAGE_SHIFT,
1034 (((unsigned long long) pfn + nr_pages)
1035 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001036 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001037 return ret;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001038 }
1039
Dave Hansen3947be12005-10-29 18:16:54 -07001040 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001041
1042 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001043 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001044 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1045
Jiang Liu08dff7b2012-07-31 16:43:30 -07001046 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001047 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001048 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001049 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001050 else
1051 zone_pcp_update(zone);
1052 }
Dave Hansen3947be12005-10-29 18:16:54 -07001053
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001054 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001055
1056 init_per_zone_wmark_min();
1057
Jiang Liu08dff7b2012-07-31 16:43:30 -07001058 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001059 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001060
Haicheng Li1f522502010-05-24 14:32:51 -07001061 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001062
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001063 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001064
1065 if (onlined_pages)
1066 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001067 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001068}
Keith Mannthey53947022006-09-30 23:27:08 -07001069#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001070
Tang Chen0bd85422014-11-13 15:19:41 -08001071static void reset_node_present_pages(pg_data_t *pgdat)
1072{
1073 struct zone *z;
1074
1075 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1076 z->present_pages = 0;
1077
1078 pgdat->node_present_pages = 0;
1079}
1080
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001081/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1082static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001083{
1084 struct pglist_data *pgdat;
1085 unsigned long zones_size[MAX_NR_ZONES] = {0};
1086 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001087 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001088
Tang Chena1e565a2013-02-22 16:33:18 -08001089 pgdat = NODE_DATA(nid);
1090 if (!pgdat) {
1091 pgdat = arch_alloc_nodedata(nid);
1092 if (!pgdat)
1093 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001094
Tang Chena1e565a2013-02-22 16:33:18 -08001095 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001096 } else {
1097 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1098 pgdat->nr_zones = 0;
1099 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001100 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001101
1102 /* we can use NODE_DATA(nid) from here */
1103
1104 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001105 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001106
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001107 /*
1108 * The node we allocated has no zone fallback lists. For avoiding
1109 * to access not-initialized zonelist, build here.
1110 */
David Rientjesf957db42011-06-22 18:13:04 -07001111 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001112 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001113 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001114
Tang Chenf784a3f2014-11-13 15:19:39 -08001115 /*
1116 * zone->managed_pages is set to an approximate value in
1117 * free_area_init_core(), which will cause
1118 * /sys/device/system/node/nodeX/meminfo has wrong data.
1119 * So reset it to 0 before any memory is onlined.
1120 */
1121 reset_node_managed_pages(pgdat);
1122
Tang Chen0bd85422014-11-13 15:19:41 -08001123 /*
1124 * When memory is hot-added, all the memory is in offline state. So
1125 * clear all zones' present_pages because they will be updated in
1126 * online_pages() and offline_pages().
1127 */
1128 reset_node_present_pages(pgdat);
1129
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001130 return pgdat;
1131}
1132
1133static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1134{
1135 arch_refresh_nodedata(nid, NULL);
1136 arch_free_nodedata(pgdat);
1137 return;
1138}
1139
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001140
Toshi Kani01b0f192013-11-12 15:07:25 -08001141/**
1142 * try_online_node - online a node if offlined
1143 *
minskey guocf234222010-05-24 14:32:41 -07001144 * called by cpu_up() to online a node without onlined memory.
1145 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001146int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001147{
1148 pg_data_t *pgdat;
1149 int ret;
1150
Toshi Kani01b0f192013-11-12 15:07:25 -08001151 if (node_online(nid))
1152 return 0;
1153
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001154 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001155 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001156 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001157 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001158 ret = -ENOMEM;
1159 goto out;
1160 }
1161 node_set_online(nid);
1162 ret = register_one_node(nid);
1163 BUG_ON(ret);
1164
Toshi Kani01b0f192013-11-12 15:07:25 -08001165 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1166 mutex_lock(&zonelists_mutex);
1167 build_all_zonelists(NULL, NULL);
1168 mutex_unlock(&zonelists_mutex);
1169 }
1170
minskey guocf234222010-05-24 14:32:41 -07001171out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001172 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001173 return ret;
1174}
1175
Toshi Kani27356f52013-09-11 14:21:49 -07001176static int check_hotplug_memory_range(u64 start, u64 size)
1177{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001178 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001179 u64 nr_pages = size >> PAGE_SHIFT;
1180
1181 /* Memory range must be aligned with section */
1182 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1183 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1184 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1185 (unsigned long long)start,
1186 (unsigned long long)size);
1187 return -EINVAL;
1188 }
1189
1190 return 0;
1191}
1192
Wang Nan63264402014-08-06 16:07:36 -07001193/*
1194 * If movable zone has already been setup, newly added memory should be check.
1195 * If its address is higher than movable zone, it should be added as movable.
1196 * Without this check, movable zone may overlap with other zone.
1197 */
1198static int should_add_memory_movable(int nid, u64 start, u64 size)
1199{
1200 unsigned long start_pfn = start >> PAGE_SHIFT;
1201 pg_data_t *pgdat = NODE_DATA(nid);
1202 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1203
1204 if (zone_is_empty(movable_zone))
1205 return 0;
1206
1207 if (movable_zone->zone_start_pfn <= start_pfn)
1208 return 1;
1209
1210 return 0;
1211}
1212
Dan Williams033fbae2015-08-09 15:29:06 -04001213int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1214 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001215{
Dan Williams033fbae2015-08-09 15:29:06 -04001216#ifdef CONFIG_ZONE_DEVICE
1217 if (for_device)
1218 return ZONE_DEVICE;
1219#endif
Wang Nan63264402014-08-06 16:07:36 -07001220 if (should_add_memory_movable(nid, start, size))
1221 return ZONE_MOVABLE;
1222
1223 return zone_default;
1224}
1225
Al Viro31168482008-11-22 17:33:24 +00001226/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1227int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001228{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001229 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001230 bool new_pgdat;
1231 bool new_node;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001232 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001233 int ret;
1234
Toshi Kani27356f52013-09-11 14:21:49 -07001235 ret = check_hotplug_memory_range(start, size);
1236 if (ret)
1237 return ret;
1238
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001239 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001240 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001241 if (!res)
Nathan Zimmerac13c462014-01-23 15:53:26 -08001242 return ret;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001243
Tang Chena1e565a2013-02-22 16:33:18 -08001244 { /* Stupid hack to suppress address-never-null warning */
1245 void *p = NODE_DATA(nid);
1246 new_pgdat = !p;
1247 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001248
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001249 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001250
Tang Chena1e565a2013-02-22 16:33:18 -08001251 new_node = !node_online(nid);
1252 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001253 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001254 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001255 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001256 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001257 }
1258
Yasunori Gotobc02af92006-06-27 02:53:30 -07001259 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001260 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001261
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001262 if (ret < 0)
1263 goto error;
1264
Yasunori Goto0fc44152006-06-27 02:53:38 -07001265 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001266 node_set_online(nid);
1267
Tang Chena1e565a2013-02-22 16:33:18 -08001268 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001269 ret = register_one_node(nid);
1270 /*
1271 * If sysfs file of new node can't create, cpu on the node
1272 * can't be hot-added. There is no rollback way now.
1273 * So, check by BUG_ON() to catch it reluctantly..
1274 */
1275 BUG_ON(ret);
1276 }
1277
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001278 /* create new memmap entry */
1279 firmware_map_add_hotplug(start, start + size, "System RAM");
1280
Andi Kleen6ad696d2009-11-17 14:06:22 -08001281 goto out;
1282
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001283error:
1284 /* rollback pgdat allocation and others */
1285 if (new_pgdat)
1286 rollback_node_hotadd(nid, pgdat);
Sasha Levina864b9d2013-02-22 16:32:48 -08001287 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001288
Andi Kleen6ad696d2009-11-17 14:06:22 -08001289out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001290 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001291 return ret;
1292}
1293EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001294
1295#ifdef CONFIG_MEMORY_HOTREMOVE
1296/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001297 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1298 * set and the size of the free page is given by page_order(). Using this,
1299 * the function determines if the pageblock contains only free pages.
1300 * Due to buddy contraints, a free page at least the size of a pageblock will
1301 * be located at the start of the pageblock
1302 */
1303static inline int pageblock_free(struct page *page)
1304{
1305 return PageBuddy(page) && page_order(page) >= pageblock_order;
1306}
1307
1308/* Return the start of the next active pageblock after a given page */
1309static struct page *next_active_pageblock(struct page *page)
1310{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001311 /* Ensure the starting page is pageblock-aligned */
1312 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1313
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001314 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001315 if (pageblock_free(page)) {
1316 int order;
1317 /* be careful. we don't have locks, page_order can be changed.*/
1318 order = page_order(page);
1319 if ((order < MAX_ORDER) && (order >= pageblock_order))
1320 return page + (1 << order);
1321 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001322
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001323 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001324}
1325
1326/* Checks if this range of memory is likely to be hot-removable. */
1327int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1328{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001329 struct page *page = pfn_to_page(start_pfn);
1330 struct page *end_page = page + nr_pages;
1331
1332 /* Check the starting page of each pageblock within the range */
1333 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001334 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001335 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001336 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001337 }
1338
1339 /* All pageblocks in the memory block are likely to be hot-removable */
1340 return 1;
1341}
1342
1343/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001344 * Confirm all pages in a range [start, end) is belongs to the same zone.
1345 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001346int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001347{
1348 unsigned long pfn;
1349 struct zone *zone = NULL;
1350 struct page *page;
1351 int i;
1352 for (pfn = start_pfn;
1353 pfn < end_pfn;
1354 pfn += MAX_ORDER_NR_PAGES) {
1355 i = 0;
1356 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1357 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1358 i++;
1359 if (i == MAX_ORDER_NR_PAGES)
1360 continue;
1361 page = pfn_to_page(pfn + i);
1362 if (zone && page_zone(page) != zone)
1363 return 0;
1364 zone = page_zone(page);
1365 }
1366 return 1;
1367}
1368
1369/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001370 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1371 * and hugepages). We scan pfn because it's much easier than scanning over
1372 * linked list. This function returns the pfn of the first found movable
1373 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001374 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001375static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001376{
1377 unsigned long pfn;
1378 struct page *page;
1379 for (pfn = start; pfn < end; pfn++) {
1380 if (pfn_valid(pfn)) {
1381 page = pfn_to_page(pfn);
1382 if (PageLRU(page))
1383 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001384 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001385 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001386 return pfn;
1387 else
1388 pfn = round_up(pfn + 1,
1389 1 << compound_order(page)) - 1;
1390 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001391 }
1392 }
1393 return 0;
1394}
1395
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001396#define NR_OFFLINE_AT_ONCE_PAGES (256)
1397static int
1398do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1399{
1400 unsigned long pfn;
1401 struct page *page;
1402 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1403 int not_managed = 0;
1404 int ret = 0;
1405 LIST_HEAD(source);
1406
1407 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1408 if (!pfn_valid(pfn))
1409 continue;
1410 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001411
1412 if (PageHuge(page)) {
1413 struct page *head = compound_head(page);
1414 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1415 if (compound_order(head) > PFN_SECTION_SHIFT) {
1416 ret = -EBUSY;
1417 break;
1418 }
1419 if (isolate_huge_page(page, &source))
1420 move_pages -= 1 << compound_order(head);
1421 continue;
1422 }
1423
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001424 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001425 continue;
1426 /*
1427 * We can skip free pages. And we can only deal with pages on
1428 * LRU.
1429 */
Nick Piggin62695a82008-10-18 20:26:09 -07001430 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001431 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001432 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001433 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001434 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001435 inc_zone_page_state(page, NR_ISOLATED_ANON +
1436 page_is_file_cache(page));
1437
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001438 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001439#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001440 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1441 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001442 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001443#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001444 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001445 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001446 check this again here. */
1447 if (page_count(page)) {
1448 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001449 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001450 break;
1451 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001452 }
1453 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001454 if (!list_empty(&source)) {
1455 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001456 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001457 goto out;
1458 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001459
1460 /*
1461 * alloc_migrate_target should be improooooved!!
1462 * migrate_pages returns # of failed pages.
1463 */
David Rientjes68711a72014-06-04 16:08:25 -07001464 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001465 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001466 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001467 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001468 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001469out:
1470 return ret;
1471}
1472
1473/*
1474 * remove from free_area[] and mark all as Reserved.
1475 */
1476static int
1477offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1478 void *data)
1479{
1480 __offline_isolated_pages(start, start + nr_pages);
1481 return 0;
1482}
1483
1484static void
1485offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1486{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001487 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001488 offline_isolated_pages_cb);
1489}
1490
1491/*
1492 * Check all pages in range, recoreded as memory resource, are isolated.
1493 */
1494static int
1495check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1496 void *data)
1497{
1498 int ret;
1499 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001500 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001501 offlined = nr_pages;
1502 if (!ret)
1503 *(long *)data += offlined;
1504 return ret;
1505}
1506
1507static long
1508check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1509{
1510 long offlined = 0;
1511 int ret;
1512
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001513 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001514 check_pages_isolated_cb);
1515 if (ret < 0)
1516 offlined = (long)ret;
1517 return offlined;
1518}
1519
Lai Jiangshan09285af2012-12-12 13:52:04 -08001520#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001521/*
1522 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1523 * normal memory.
1524 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001525static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1526{
1527 return true;
1528}
Tang Chen79a4dce2012-12-18 14:23:24 -08001529#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001530/* ensure the node has NORMAL memory if it is still online */
1531static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1532{
1533 struct pglist_data *pgdat = zone->zone_pgdat;
1534 unsigned long present_pages = 0;
1535 enum zone_type zt;
1536
1537 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1538 present_pages += pgdat->node_zones[zt].present_pages;
1539
1540 if (present_pages > nr_pages)
1541 return true;
1542
1543 present_pages = 0;
1544 for (; zt <= ZONE_MOVABLE; zt++)
1545 present_pages += pgdat->node_zones[zt].present_pages;
1546
1547 /*
1548 * we can't offline the last normal memory until all
1549 * higher memory is offlined.
1550 */
1551 return present_pages == 0;
1552}
Tang Chen79a4dce2012-12-18 14:23:24 -08001553#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001554
Tang Chenc5320922013-11-12 15:08:10 -08001555static int __init cmdline_parse_movable_node(char *p)
1556{
1557#ifdef CONFIG_MOVABLE_NODE
1558 /*
1559 * Memory used by the kernel cannot be hot-removed because Linux
1560 * cannot migrate the kernel pages. When memory hotplug is
1561 * enabled, we should prevent memblock from allocating memory
1562 * for the kernel.
1563 *
1564 * ACPI SRAT records all hotpluggable memory ranges. But before
1565 * SRAT is parsed, we don't know about it.
1566 *
1567 * The kernel image is loaded into memory at very early time. We
1568 * cannot prevent this anyway. So on NUMA system, we set any
1569 * node the kernel resides in as un-hotpluggable.
1570 *
1571 * Since on modern servers, one node could have double-digit
1572 * gigabytes memory, we can assume the memory around the kernel
1573 * image is also un-hotpluggable. So before SRAT is parsed, just
1574 * allocate memory near the kernel image to try the best to keep
1575 * the kernel away from hotpluggable memory.
1576 */
1577 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001578 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001579#else
1580 pr_warn("movable_node option not supported\n");
1581#endif
1582 return 0;
1583}
1584early_param("movable_node", cmdline_parse_movable_node);
1585
Lai Jiangshand9713672012-12-11 16:01:03 -08001586/* check which state of node_states will be changed when offline memory */
1587static void node_states_check_changes_offline(unsigned long nr_pages,
1588 struct zone *zone, struct memory_notify *arg)
1589{
1590 struct pglist_data *pgdat = zone->zone_pgdat;
1591 unsigned long present_pages = 0;
1592 enum zone_type zt, zone_last = ZONE_NORMAL;
1593
1594 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001595 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1596 * contains nodes which have zones of 0...ZONE_NORMAL,
1597 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001598 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001599 * If we don't have HIGHMEM nor movable node,
1600 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1601 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001602 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001603 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001604 zone_last = ZONE_MOVABLE;
1605
1606 /*
1607 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1608 * If the memory to be offline is in a zone of 0...zone_last,
1609 * and it is the last present memory, 0...zone_last will
1610 * become empty after offline , thus we can determind we will
1611 * need to clear the node from node_states[N_NORMAL_MEMORY].
1612 */
1613 for (zt = 0; zt <= zone_last; zt++)
1614 present_pages += pgdat->node_zones[zt].present_pages;
1615 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1616 arg->status_change_nid_normal = zone_to_nid(zone);
1617 else
1618 arg->status_change_nid_normal = -1;
1619
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001620#ifdef CONFIG_HIGHMEM
1621 /*
1622 * If we have movable node, node_states[N_HIGH_MEMORY]
1623 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1624 * set zone_last to ZONE_HIGHMEM.
1625 *
1626 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1627 * contains nodes which have zones of 0...ZONE_MOVABLE,
1628 * set zone_last to ZONE_MOVABLE.
1629 */
1630 zone_last = ZONE_HIGHMEM;
1631 if (N_MEMORY == N_HIGH_MEMORY)
1632 zone_last = ZONE_MOVABLE;
1633
1634 for (; zt <= zone_last; zt++)
1635 present_pages += pgdat->node_zones[zt].present_pages;
1636 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1637 arg->status_change_nid_high = zone_to_nid(zone);
1638 else
1639 arg->status_change_nid_high = -1;
1640#else
1641 arg->status_change_nid_high = arg->status_change_nid_normal;
1642#endif
1643
Lai Jiangshand9713672012-12-11 16:01:03 -08001644 /*
1645 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1646 */
1647 zone_last = ZONE_MOVABLE;
1648
1649 /*
1650 * check whether node_states[N_HIGH_MEMORY] will be changed
1651 * If we try to offline the last present @nr_pages from the node,
1652 * we can determind we will need to clear the node from
1653 * node_states[N_HIGH_MEMORY].
1654 */
1655 for (; zt <= zone_last; zt++)
1656 present_pages += pgdat->node_zones[zt].present_pages;
1657 if (nr_pages >= present_pages)
1658 arg->status_change_nid = zone_to_nid(zone);
1659 else
1660 arg->status_change_nid = -1;
1661}
1662
1663static void node_states_clear_node(int node, struct memory_notify *arg)
1664{
1665 if (arg->status_change_nid_normal >= 0)
1666 node_clear_state(node, N_NORMAL_MEMORY);
1667
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001668 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1669 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001670 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001671
1672 if ((N_MEMORY != N_HIGH_MEMORY) &&
1673 (arg->status_change_nid >= 0))
1674 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001675}
1676
Wen Congyanga16cee12012-10-08 16:33:58 -07001677static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001678 unsigned long end_pfn, unsigned long timeout)
1679{
1680 unsigned long pfn, nr_pages, expire;
1681 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001682 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001683 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001684 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001685 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001686
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001687 /* at least, alignment against pageblock is necessary */
1688 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1689 return -EINVAL;
1690 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1691 return -EINVAL;
1692 /* This makes hotplug much easier...and readable.
1693 we assume this for now. .*/
1694 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1695 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001696
1697 zone = page_zone(pfn_to_page(start_pfn));
1698 node = zone_to_nid(zone);
1699 nr_pages = end_pfn - start_pfn;
1700
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001701 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001702 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001703
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001704 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001705 ret = start_isolate_page_range(start_pfn, end_pfn,
1706 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001707 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001708 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001709
1710 arg.start_pfn = start_pfn;
1711 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001712 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001713
1714 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1715 ret = notifier_to_errno(ret);
1716 if (ret)
1717 goto failed_removal;
1718
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001719 pfn = start_pfn;
1720 expire = jiffies + timeout;
1721 drain = 0;
1722 retry_max = 5;
1723repeat:
1724 /* start memory hot removal */
1725 ret = -EAGAIN;
1726 if (time_after(jiffies, expire))
1727 goto failed_removal;
1728 ret = -EINTR;
1729 if (signal_pending(current))
1730 goto failed_removal;
1731 ret = 0;
1732 if (drain) {
1733 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001734 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001735 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001736 }
1737
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001738 pfn = scan_movable_pages(start_pfn, end_pfn);
1739 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001740 ret = do_migrate_range(pfn, end_pfn);
1741 if (!ret) {
1742 drain = 1;
1743 goto repeat;
1744 } else {
1745 if (ret < 0)
1746 if (--retry_max == 0)
1747 goto failed_removal;
1748 yield();
1749 drain = 1;
1750 goto repeat;
1751 }
1752 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001753 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001754 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001755 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001756 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001757 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001758 /*
1759 * dissolve free hugepages in the memory block before doing offlining
1760 * actually in order to make hugetlbfs's object counting consistent.
1761 */
1762 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001763 /* check again */
1764 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1765 if (offlined_pages < 0) {
1766 ret = -EBUSY;
1767 goto failed_removal;
1768 }
1769 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001770 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001771 We cannot do rollback at this point. */
1772 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001773 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001774 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001775 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001776 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001777 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001778
1779 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001780 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001781 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001782
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001783 init_per_zone_wmark_min();
1784
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001785 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001786 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001787 mutex_lock(&zonelists_mutex);
1788 build_all_zonelists(NULL, NULL);
1789 mutex_unlock(&zonelists_mutex);
1790 } else
1791 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001792
Lai Jiangshand9713672012-12-11 16:01:03 -08001793 node_states_clear_node(node, &arg);
1794 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001795 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001796
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001797 vm_total_pages = nr_free_pagecache_pages();
1798 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001799
1800 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001801 return 0;
1802
1803failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001804 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1805 (unsigned long long) start_pfn << PAGE_SHIFT,
1806 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001807 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001808 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001809 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001810 return ret;
1811}
Badari Pulavarty71088782008-10-18 20:25:58 -07001812
David Rientjes30467e02015-04-14 15:45:11 -07001813/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001814int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1815{
1816 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1817}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001818#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001819
Wen Congyangbbc76be2013-02-22 16:32:54 -08001820/**
1821 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1822 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001823 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001824 * @arg: argument passed to func
1825 * @func: callback for each memory section walked
1826 *
1827 * This function walks through all present mem sections in range
1828 * [start_pfn, end_pfn) and call func on each mem section.
1829 *
1830 * Returns the return value of func.
1831 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001832int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001833 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001834{
Wen Congyange90bdb72012-10-08 16:34:01 -07001835 struct memory_block *mem = NULL;
1836 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001837 unsigned long pfn, section_nr;
1838 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001839
Wen Congyange90bdb72012-10-08 16:34:01 -07001840 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1841 section_nr = pfn_to_section_nr(pfn);
1842 if (!present_section_nr(section_nr))
1843 continue;
1844
1845 section = __nr_to_section(section_nr);
1846 /* same memblock? */
1847 if (mem)
1848 if ((section_nr >= mem->start_section_nr) &&
1849 (section_nr <= mem->end_section_nr))
1850 continue;
1851
1852 mem = find_memory_block_hinted(section, mem);
1853 if (!mem)
1854 continue;
1855
Wen Congyangbbc76be2013-02-22 16:32:54 -08001856 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001857 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001858 kobject_put(&mem->dev.kobj);
1859 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001860 }
1861 }
1862
1863 if (mem)
1864 kobject_put(&mem->dev.kobj);
1865
Wen Congyangbbc76be2013-02-22 16:32:54 -08001866 return 0;
1867}
1868
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001869#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001870static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001871{
1872 int ret = !is_memblock_offlined(mem);
1873
Randy Dunlap349daa02013-04-29 15:08:49 -07001874 if (unlikely(ret)) {
1875 phys_addr_t beginpa, endpa;
1876
1877 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1878 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001879 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001880 "[%pa-%pa] is onlined\n",
1881 &beginpa, &endpa);
1882 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001883
1884 return ret;
1885}
1886
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001887static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001888{
Tang Chen60a5a192013-02-22 16:33:14 -08001889 int cpu;
1890
1891 for_each_present_cpu(cpu) {
1892 if (cpu_to_node(cpu) == pgdat->node_id)
1893 /*
1894 * the cpu on this node isn't removed, and we can't
1895 * offline this node.
1896 */
1897 return -EBUSY;
1898 }
1899
1900 return 0;
1901}
1902
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001903static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001904{
1905#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001906 int cpu;
1907
1908 for_each_possible_cpu(cpu)
1909 if (cpu_to_node(cpu) == pgdat->node_id)
1910 numa_clear_node(cpu);
1911#endif
1912}
1913
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001914static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001915{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001916 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001917
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001918 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001919 if (ret)
1920 return ret;
1921
1922 /*
1923 * the node will be offlined when we come here, so we can clear
1924 * the cpu_to_node() now.
1925 */
1926
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001927 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001928 return 0;
1929}
1930
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001931/**
1932 * try_offline_node
1933 *
1934 * Offline a node if all memory sections and cpus of the node are removed.
1935 *
1936 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1937 * and online/offline operations before this call.
1938 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001939void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001940{
Wen Congyangd822b862013-02-22 16:33:16 -08001941 pg_data_t *pgdat = NODE_DATA(nid);
1942 unsigned long start_pfn = pgdat->node_start_pfn;
1943 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001944 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08001945 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08001946
1947 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1948 unsigned long section_nr = pfn_to_section_nr(pfn);
1949
1950 if (!present_section_nr(section_nr))
1951 continue;
1952
1953 if (pfn_to_nid(pfn) != nid)
1954 continue;
1955
1956 /*
1957 * some memory sections of this node are not removed, and we
1958 * can't offline node now.
1959 */
1960 return;
1961 }
1962
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001963 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001964 return;
1965
1966 /*
1967 * all memory/cpu of this node are removed, we can offline this
1968 * node now.
1969 */
1970 node_set_offline(nid);
1971 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08001972
Wen Congyangd822b862013-02-22 16:33:16 -08001973 /* free waittable in each zone */
1974 for (i = 0; i < MAX_NR_ZONES; i++) {
1975 struct zone *zone = pgdat->node_zones + i;
1976
Jianguo Wuca4b3f32013-03-22 15:04:50 -07001977 /*
1978 * wait_table may be allocated from boot memory,
1979 * here only free if it's allocated by vmalloc.
1980 */
Gu Zheng85bd8392015-06-10 11:14:43 -07001981 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08001982 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07001983 zone->wait_table = NULL;
1984 }
Wen Congyangd822b862013-02-22 16:33:16 -08001985 }
Tang Chen60a5a192013-02-22 16:33:14 -08001986}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001987EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001988
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001989/**
1990 * remove_memory
1991 *
1992 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1993 * and online/offline operations before this call, as required by
1994 * try_offline_node().
1995 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001996void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001997{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001998 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001999
Toshi Kani27356f52013-09-11 14:21:49 -07002000 BUG_ON(check_hotplug_memory_range(start, size));
2001
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002002 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002003
2004 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002005 * All memory blocks must be offlined before removing memory. Check
2006 * whether all memory blocks in question are offline and trigger a BUG()
2007 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002008 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002009 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002010 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002011 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002012 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002013
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002014 /* remove memmap entry */
2015 firmware_map_remove(start, start + size, "System RAM");
2016
Wen Congyang24d335c2013-02-22 16:32:58 -08002017 arch_remove_memory(start, size);
2018
Tang Chen60a5a192013-02-22 16:33:14 -08002019 try_offline_node(nid);
2020
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002021 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002022}
Badari Pulavarty71088782008-10-18 20:25:58 -07002023EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002024#endif /* CONFIG_MEMORY_HOTREMOVE */