blob: a3797d3fd8a4b6ca3793c6e231222f08b884f428 [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>
Dave Hansen3947be12005-10-29 18:16:54 -070034
35#include <asm/tlbflush.h>
36
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030037#include "internal.h"
38
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070039/*
40 * online_page_callback contains pointer to current page onlining function.
41 * Initially it is generic_online_page(). If it is required it could be
42 * changed by calling set_online_page_callback() for callback registration
43 * and restore_online_page_callback() for generic callback restore.
44 */
45
46static void generic_online_page(struct page *page);
47
48static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070049static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070050
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070051/* The same as the cpu_hotplug lock, but for memory hotplug. */
52static struct {
53 struct task_struct *active_writer;
54 struct mutex lock; /* Synchronizes accesses to refcount, */
55 /*
56 * Also blocks the new readers during
57 * an ongoing mem hotplug operation.
58 */
59 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080060
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070061#ifdef CONFIG_DEBUG_LOCK_ALLOC
62 struct lockdep_map dep_map;
63#endif
64} mem_hotplug = {
65 .active_writer = NULL,
66 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
67 .refcount = 0,
68#ifdef CONFIG_DEBUG_LOCK_ALLOC
69 .dep_map = {.name = "mem_hotplug.lock" },
70#endif
71};
72
73/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
74#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
75#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
76#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
77
78void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080079{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070080 might_sleep();
81 if (mem_hotplug.active_writer == current)
82 return;
83 memhp_lock_acquire_read();
84 mutex_lock(&mem_hotplug.lock);
85 mem_hotplug.refcount++;
86 mutex_unlock(&mem_hotplug.lock);
87
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080088}
89
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070090void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080091{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070092 if (mem_hotplug.active_writer == current)
93 return;
94 mutex_lock(&mem_hotplug.lock);
95
96 if (WARN_ON(!mem_hotplug.refcount))
97 mem_hotplug.refcount++; /* try to fix things up */
98
99 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
100 wake_up_process(mem_hotplug.active_writer);
101 mutex_unlock(&mem_hotplug.lock);
102 memhp_lock_release();
103
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800104}
105
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700106static void mem_hotplug_begin(void)
107{
108 mem_hotplug.active_writer = current;
109
110 memhp_lock_acquire();
111 for (;;) {
112 mutex_lock(&mem_hotplug.lock);
113 if (likely(!mem_hotplug.refcount))
114 break;
115 __set_current_state(TASK_UNINTERRUPTIBLE);
116 mutex_unlock(&mem_hotplug.lock);
117 schedule();
118 }
119}
120
121static void mem_hotplug_done(void)
122{
123 mem_hotplug.active_writer = NULL;
124 mutex_unlock(&mem_hotplug.lock);
125 memhp_lock_release();
126}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800127
Keith Mannthey45e0b782006-09-30 23:27:09 -0700128/* add this memory to iomem resource */
129static struct resource *register_memory_resource(u64 start, u64 size)
130{
131 struct resource *res;
132 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
133 BUG_ON(!res);
134
135 res->name = "System RAM";
136 res->start = start;
137 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800138 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700139 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700140 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700141 kfree(res);
142 res = NULL;
143 }
144 return res;
145}
146
147static void release_memory_resource(struct resource *res)
148{
149 if (!res)
150 return;
151 release_resource(res);
152 kfree(res);
153 return;
154}
155
Keith Mannthey53947022006-09-30 23:27:08 -0700156#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800157void get_page_bootmem(unsigned long info, struct page *page,
158 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700159{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800160 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700161 SetPagePrivate(page);
162 set_page_private(page, info);
163 atomic_inc(&page->_count);
164}
165
Jiang Liu170a5a72013-07-03 15:03:17 -0700166void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700167{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800168 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700169
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800170 type = (unsigned long) page->lru.next;
171 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
172 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700173
174 if (atomic_dec_return(&page->_count) == 1) {
175 ClearPagePrivate(page);
176 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800177 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700178 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700179 }
Yasunori Goto04753272008-04-28 02:13:31 -0700180}
181
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800182#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
183#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700184static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700185{
186 unsigned long *usemap, mapsize, section_nr, i;
187 struct mem_section *ms;
188 struct page *page, *memmap;
189
Yasunori Goto04753272008-04-28 02:13:31 -0700190 section_nr = pfn_to_section_nr(start_pfn);
191 ms = __nr_to_section(section_nr);
192
193 /* Get section's memmap address */
194 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
195
196 /*
197 * Get page for the memmap's phys address
198 * XXX: need more consideration for sparse_vmemmap...
199 */
200 page = virt_to_page(memmap);
201 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
202 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
203
204 /* remember memmap's page */
205 for (i = 0; i < mapsize; i++, page++)
206 get_page_bootmem(section_nr, page, SECTION_INFO);
207
208 usemap = __nr_to_section(section_nr)->pageblock_flags;
209 page = virt_to_page(usemap);
210
211 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
212
213 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700214 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700215
216}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800217#else /* CONFIG_SPARSEMEM_VMEMMAP */
218static void register_page_bootmem_info_section(unsigned long start_pfn)
219{
220 unsigned long *usemap, mapsize, section_nr, i;
221 struct mem_section *ms;
222 struct page *page, *memmap;
223
224 if (!pfn_valid(start_pfn))
225 return;
226
227 section_nr = pfn_to_section_nr(start_pfn);
228 ms = __nr_to_section(section_nr);
229
230 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
231
232 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
233
234 usemap = __nr_to_section(section_nr)->pageblock_flags;
235 page = virt_to_page(usemap);
236
237 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
238
239 for (i = 0; i < mapsize; i++, page++)
240 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
241}
242#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700243
244void register_page_bootmem_info_node(struct pglist_data *pgdat)
245{
246 unsigned long i, pfn, end_pfn, nr_pages;
247 int node = pgdat->node_id;
248 struct page *page;
249 struct zone *zone;
250
251 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
252 page = virt_to_page(pgdat);
253
254 for (i = 0; i < nr_pages; i++, page++)
255 get_page_bootmem(node, page, NODE_INFO);
256
257 zone = &pgdat->node_zones[0];
258 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700259 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700260 nr_pages = zone->wait_table_hash_nr_entries
261 * sizeof(wait_queue_head_t);
262 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
263 page = virt_to_page(zone->wait_table);
264
265 for (i = 0; i < nr_pages; i++, page++)
266 get_page_bootmem(node, page, NODE_INFO);
267 }
268 }
269
270 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800271 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700272
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700273 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700274 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
275 /*
276 * Some platforms can assign the same pfn to multiple nodes - on
277 * node0 as well as nodeN. To avoid registering a pfn against
278 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700279 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700280 */
281 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
282 register_page_bootmem_info_section(pfn);
283 }
Yasunori Goto04753272008-04-28 02:13:31 -0700284}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800285#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700286
Fabian Frederickf2765402014-08-06 16:04:57 -0700287static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
288 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700289{
290 unsigned long old_zone_end_pfn;
291
292 zone_span_writelock(zone);
293
Xishi Qiuc33bc312013-09-11 14:21:44 -0700294 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700295 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700296 zone->zone_start_pfn = start_pfn;
297
298 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
299 zone->zone_start_pfn;
300
301 zone_span_writeunlock(zone);
302}
303
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800304static void resize_zone(struct zone *zone, unsigned long start_pfn,
305 unsigned long end_pfn)
306{
307 zone_span_writelock(zone);
308
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800309 if (end_pfn - start_pfn) {
310 zone->zone_start_pfn = start_pfn;
311 zone->spanned_pages = end_pfn - start_pfn;
312 } else {
313 /*
314 * make it consist as free_area_init_core(),
315 * if spanned_pages = 0, then keep start_pfn = 0
316 */
317 zone->zone_start_pfn = 0;
318 zone->spanned_pages = 0;
319 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800320
321 zone_span_writeunlock(zone);
322}
323
324static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
325 unsigned long end_pfn)
326{
327 enum zone_type zid = zone_idx(zone);
328 int nid = zone->zone_pgdat->node_id;
329 unsigned long pfn;
330
331 for (pfn = start_pfn; pfn < end_pfn; pfn++)
332 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
333}
334
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800335/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800336 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800337static int __ref ensure_zone_is_initialized(struct zone *zone,
338 unsigned long start_pfn, unsigned long num_pages)
339{
340 if (!zone_is_initialized(zone))
341 return init_currently_empty_zone(zone, start_pfn, num_pages,
342 MEMMAP_HOTPLUG);
343 return 0;
344}
345
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800346static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800347 unsigned long start_pfn, unsigned long end_pfn)
348{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800349 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800350 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800351 unsigned long z1_start_pfn;
352
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800353 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
354 if (ret)
355 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800356
357 pgdat_resize_lock(z1->zone_pgdat, &flags);
358
359 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800360 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800361 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700362 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800363 if (start_pfn > z2->zone_start_pfn)
364 goto out_fail;
365 /* must included/overlap */
366 if (end_pfn <= z2->zone_start_pfn)
367 goto out_fail;
368
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800369 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700370 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800371 z1_start_pfn = z1->zone_start_pfn;
372 else
373 z1_start_pfn = start_pfn;
374
375 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800376 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800377
378 pgdat_resize_unlock(z1->zone_pgdat, &flags);
379
380 fix_zone_id(z1, start_pfn, end_pfn);
381
382 return 0;
383out_fail:
384 pgdat_resize_unlock(z1->zone_pgdat, &flags);
385 return -1;
386}
387
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800388static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800389 unsigned long start_pfn, unsigned long end_pfn)
390{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800391 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800392 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800393 unsigned long z2_end_pfn;
394
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800395 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
396 if (ret)
397 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800398
399 pgdat_resize_lock(z1->zone_pgdat, &flags);
400
401 /* can't move pfns which are lower than @z1 */
402 if (z1->zone_start_pfn > start_pfn)
403 goto out_fail;
404 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800405 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800406 goto out_fail;
407 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800408 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800409 goto out_fail;
410
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800411 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700412 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800413 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800414 else
415 z2_end_pfn = end_pfn;
416
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800417 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800418 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800419
420 pgdat_resize_unlock(z1->zone_pgdat, &flags);
421
422 fix_zone_id(z2, start_pfn, end_pfn);
423
424 return 0;
425out_fail:
426 pgdat_resize_unlock(z1->zone_pgdat, &flags);
427 return -1;
428}
429
Fabian Frederickf2765402014-08-06 16:04:57 -0700430static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
431 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700432{
Xishi Qiu83285c72013-11-12 15:07:19 -0800433 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700434
Tang Chen712cd382012-12-11 16:01:07 -0800435 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700436 pgdat->node_start_pfn = start_pfn;
437
438 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
439 pgdat->node_start_pfn;
440}
441
Al Viro31168482008-11-22 17:33:24 +0000442static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700443{
444 struct pglist_data *pgdat = zone->zone_pgdat;
445 int nr_pages = PAGES_PER_SECTION;
446 int nid = pgdat->node_id;
447 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700448 unsigned long flags;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800449 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700450
451 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800452 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
453 if (ret)
454 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700455
Heiko Carstens76cdd582008-05-14 16:05:52 -0700456 pgdat_resize_lock(zone->zone_pgdat, &flags);
457 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
458 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
459 phys_start_pfn + nr_pages);
460 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800461 memmap_init_zone(nr_pages, nid, zone_type,
462 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700463 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700464}
465
Gary Hadec04fc582009-01-06 14:39:14 -0800466static int __meminit __add_section(int nid, struct zone *zone,
467 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700468{
Dave Hansen3947be12005-10-29 18:16:54 -0700469 int ret;
470
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700471 if (pfn_valid(phys_start_pfn))
472 return -EEXIST;
473
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800474 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700475
476 if (ret < 0)
477 return ret;
478
Yasunori Goto718127c2006-06-23 02:03:10 -0700479 ret = __add_zone(zone, phys_start_pfn);
480
481 if (ret < 0)
482 return ret;
483
Gary Hadec04fc582009-01-06 14:39:14 -0800484 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700485}
486
David Rientjes4edd7ce2013-04-29 15:08:22 -0700487/*
488 * Reasonably generic function for adding memory. It is
489 * expected that archs that support memory hotplug will
490 * call this function after deciding the zone to which to
491 * add the new pages.
492 */
493int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
494 unsigned long nr_pages)
495{
496 unsigned long i;
497 int err = 0;
498 int start_sec, end_sec;
499 /* during initialize mem_map, align hot-added range to section */
500 start_sec = pfn_to_section_nr(phys_start_pfn);
501 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
502
503 for (i = start_sec; i <= end_sec; i++) {
504 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
505
506 /*
507 * EEXIST is finally dealt with by ioresource collision
508 * check. see add_memory() => register_memory_resource()
509 * Warning will be printed if there is collision.
510 */
511 if (err && (err != -EEXIST))
512 break;
513 err = 0;
514 }
515
516 return err;
517}
518EXPORT_SYMBOL_GPL(__add_pages);
519
520#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800521/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
522static int find_smallest_section_pfn(int nid, struct zone *zone,
523 unsigned long start_pfn,
524 unsigned long end_pfn)
525{
526 struct mem_section *ms;
527
528 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
529 ms = __pfn_to_section(start_pfn);
530
531 if (unlikely(!valid_section(ms)))
532 continue;
533
534 if (unlikely(pfn_to_nid(start_pfn) != nid))
535 continue;
536
537 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
538 continue;
539
540 return start_pfn;
541 }
542
543 return 0;
544}
545
546/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
547static int find_biggest_section_pfn(int nid, struct zone *zone,
548 unsigned long start_pfn,
549 unsigned long end_pfn)
550{
551 struct mem_section *ms;
552 unsigned long pfn;
553
554 /* pfn is the end pfn of a memory section. */
555 pfn = end_pfn - 1;
556 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
557 ms = __pfn_to_section(pfn);
558
559 if (unlikely(!valid_section(ms)))
560 continue;
561
562 if (unlikely(pfn_to_nid(pfn) != nid))
563 continue;
564
565 if (zone && zone != page_zone(pfn_to_page(pfn)))
566 continue;
567
568 return pfn;
569 }
570
571 return 0;
572}
573
574static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
575 unsigned long end_pfn)
576{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700577 unsigned long zone_start_pfn = zone->zone_start_pfn;
578 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
579 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800580 unsigned long pfn;
581 struct mem_section *ms;
582 int nid = zone_to_nid(zone);
583
584 zone_span_writelock(zone);
585 if (zone_start_pfn == start_pfn) {
586 /*
587 * If the section is smallest section in the zone, it need
588 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
589 * In this case, we find second smallest valid mem_section
590 * for shrinking zone.
591 */
592 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
593 zone_end_pfn);
594 if (pfn) {
595 zone->zone_start_pfn = pfn;
596 zone->spanned_pages = zone_end_pfn - pfn;
597 }
598 } else if (zone_end_pfn == end_pfn) {
599 /*
600 * If the section is biggest section in the zone, it need
601 * shrink zone->spanned_pages.
602 * In this case, we find second biggest valid mem_section for
603 * shrinking zone.
604 */
605 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
606 start_pfn);
607 if (pfn)
608 zone->spanned_pages = pfn - zone_start_pfn + 1;
609 }
610
611 /*
612 * The section is not biggest or smallest mem_section in the zone, it
613 * only creates a hole in the zone. So in this case, we need not
614 * change the zone. But perhaps, the zone has only hole data. Thus
615 * it check the zone has only hole or not.
616 */
617 pfn = zone_start_pfn;
618 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
619 ms = __pfn_to_section(pfn);
620
621 if (unlikely(!valid_section(ms)))
622 continue;
623
624 if (page_zone(pfn_to_page(pfn)) != zone)
625 continue;
626
627 /* If the section is current section, it continues the loop */
628 if (start_pfn == pfn)
629 continue;
630
631 /* If we find valid section, we have nothing to do */
632 zone_span_writeunlock(zone);
633 return;
634 }
635
636 /* The zone has no valid section */
637 zone->zone_start_pfn = 0;
638 zone->spanned_pages = 0;
639 zone_span_writeunlock(zone);
640}
641
642static void shrink_pgdat_span(struct pglist_data *pgdat,
643 unsigned long start_pfn, unsigned long end_pfn)
644{
Xishi Qiu83285c72013-11-12 15:07:19 -0800645 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
646 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
647 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800648 unsigned long pfn;
649 struct mem_section *ms;
650 int nid = pgdat->node_id;
651
652 if (pgdat_start_pfn == start_pfn) {
653 /*
654 * If the section is smallest section in the pgdat, it need
655 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
656 * In this case, we find second smallest valid mem_section
657 * for shrinking zone.
658 */
659 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
660 pgdat_end_pfn);
661 if (pfn) {
662 pgdat->node_start_pfn = pfn;
663 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
664 }
665 } else if (pgdat_end_pfn == end_pfn) {
666 /*
667 * If the section is biggest section in the pgdat, it need
668 * shrink pgdat->node_spanned_pages.
669 * In this case, we find second biggest valid mem_section for
670 * shrinking zone.
671 */
672 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
673 start_pfn);
674 if (pfn)
675 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
676 }
677
678 /*
679 * If the section is not biggest or smallest mem_section in the pgdat,
680 * it only creates a hole in the pgdat. So in this case, we need not
681 * change the pgdat.
682 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
683 * has only hole or not.
684 */
685 pfn = pgdat_start_pfn;
686 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
687 ms = __pfn_to_section(pfn);
688
689 if (unlikely(!valid_section(ms)))
690 continue;
691
692 if (pfn_to_nid(pfn) != nid)
693 continue;
694
695 /* If the section is current section, it continues the loop */
696 if (start_pfn == pfn)
697 continue;
698
699 /* If we find valid section, we have nothing to do */
700 return;
701 }
702
703 /* The pgdat has no valid section */
704 pgdat->node_start_pfn = 0;
705 pgdat->node_spanned_pages = 0;
706}
707
708static void __remove_zone(struct zone *zone, unsigned long start_pfn)
709{
710 struct pglist_data *pgdat = zone->zone_pgdat;
711 int nr_pages = PAGES_PER_SECTION;
712 int zone_type;
713 unsigned long flags;
714
715 zone_type = zone - pgdat->node_zones;
716
717 pgdat_resize_lock(zone->zone_pgdat, &flags);
718 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
719 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
720 pgdat_resize_unlock(zone->zone_pgdat, &flags);
721}
722
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700723static int __remove_section(struct zone *zone, struct mem_section *ms)
724{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800725 unsigned long start_pfn;
726 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700727 int ret = -EINVAL;
728
729 if (!valid_section(ms))
730 return ret;
731
732 ret = unregister_memory_section(ms);
733 if (ret)
734 return ret;
735
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800736 scn_nr = __section_nr(ms);
737 start_pfn = section_nr_to_pfn(scn_nr);
738 __remove_zone(zone, start_pfn);
739
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700740 sparse_remove_one_section(zone, ms);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700741 return 0;
742}
743
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700744/**
745 * __remove_pages() - remove sections of pages from a zone
746 * @zone: zone from which pages need to be removed
747 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
748 * @nr_pages: number of pages to remove (must be multiple of section size)
749 *
750 * Generic helper function to remove section mappings and sysfs entries
751 * for the section of the memory we are removing. Caller needs to make
752 * sure that pages are marked reserved and zones are adjust properly by
753 * calling offline_pages().
754 */
755int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
756 unsigned long nr_pages)
757{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700758 unsigned long i;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700759 int sections_to_remove;
Toshi Kanife74ebb2013-04-29 15:08:20 -0700760 resource_size_t start, size;
761 int ret = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700762
763 /*
764 * We can only remove entire sections
765 */
766 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
767 BUG_ON(nr_pages % PAGES_PER_SECTION);
768
Toshi Kanife74ebb2013-04-29 15:08:20 -0700769 start = phys_start_pfn << PAGE_SHIFT;
770 size = nr_pages * PAGE_SIZE;
771 ret = release_mem_region_adjustable(&iomem_resource, start, size);
Randy Dunlap348f9f02013-05-24 15:55:30 -0700772 if (ret) {
773 resource_size_t endres = start + size - 1;
774
775 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
776 &start, &endres, ret);
777 }
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700778
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700779 sections_to_remove = nr_pages / PAGES_PER_SECTION;
780 for (i = 0; i < sections_to_remove; i++) {
781 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
782 ret = __remove_section(zone, __pfn_to_section(pfn));
783 if (ret)
784 break;
785 }
786 return ret;
787}
788EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700789#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700790
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700791int set_online_page_callback(online_page_callback_t callback)
792{
793 int rc = -EINVAL;
794
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700795 get_online_mems();
796 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700797
798 if (online_page_callback == generic_online_page) {
799 online_page_callback = callback;
800 rc = 0;
801 }
802
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700803 mutex_unlock(&online_page_callback_lock);
804 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700805
806 return rc;
807}
808EXPORT_SYMBOL_GPL(set_online_page_callback);
809
810int restore_online_page_callback(online_page_callback_t callback)
811{
812 int rc = -EINVAL;
813
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700814 get_online_mems();
815 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700816
817 if (online_page_callback == callback) {
818 online_page_callback = generic_online_page;
819 rc = 0;
820 }
821
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700822 mutex_unlock(&online_page_callback_lock);
823 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700824
825 return rc;
826}
827EXPORT_SYMBOL_GPL(restore_online_page_callback);
828
829void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700830{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700831}
832EXPORT_SYMBOL_GPL(__online_page_set_limits);
833
834void __online_page_increment_counters(struct page *page)
835{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700836 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700837}
838EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700839
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700840void __online_page_free(struct page *page)
841{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700842 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700843}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700844EXPORT_SYMBOL_GPL(__online_page_free);
845
846static void generic_online_page(struct page *page)
847{
848 __online_page_set_limits(page);
849 __online_page_increment_counters(page);
850 __online_page_free(page);
851}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700852
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700853static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
854 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700855{
856 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700857 unsigned long onlined_pages = *(unsigned long *)arg;
858 struct page *page;
859 if (PageReserved(pfn_to_page(start_pfn)))
860 for (i = 0; i < nr_pages; i++) {
861 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700862 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700863 onlined_pages++;
864 }
865 *(unsigned long *)arg = onlined_pages;
866 return 0;
867}
868
Lai Jiangshan09285af2012-12-12 13:52:04 -0800869#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800870/*
871 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
872 * normal memory.
873 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800874static bool can_online_high_movable(struct zone *zone)
875{
876 return true;
877}
Tang Chen79a4dce2012-12-18 14:23:24 -0800878#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800879/* ensure every online node has NORMAL memory */
880static bool can_online_high_movable(struct zone *zone)
881{
882 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
883}
Tang Chen79a4dce2012-12-18 14:23:24 -0800884#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800885
Lai Jiangshand9713672012-12-11 16:01:03 -0800886/* check which state of node_states will be changed when online memory */
887static void node_states_check_changes_online(unsigned long nr_pages,
888 struct zone *zone, struct memory_notify *arg)
889{
890 int nid = zone_to_nid(zone);
891 enum zone_type zone_last = ZONE_NORMAL;
892
893 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800894 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
895 * contains nodes which have zones of 0...ZONE_NORMAL,
896 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800897 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800898 * If we don't have HIGHMEM nor movable node,
899 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
900 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800901 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800902 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800903 zone_last = ZONE_MOVABLE;
904
905 /*
906 * if the memory to be online is in a zone of 0...zone_last, and
907 * the zones of 0...zone_last don't have memory before online, we will
908 * need to set the node to node_states[N_NORMAL_MEMORY] after
909 * the memory is online.
910 */
911 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
912 arg->status_change_nid_normal = nid;
913 else
914 arg->status_change_nid_normal = -1;
915
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800916#ifdef CONFIG_HIGHMEM
917 /*
918 * If we have movable node, node_states[N_HIGH_MEMORY]
919 * contains nodes which have zones of 0...ZONE_HIGHMEM,
920 * set zone_last to ZONE_HIGHMEM.
921 *
922 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
923 * contains nodes which have zones of 0...ZONE_MOVABLE,
924 * set zone_last to ZONE_MOVABLE.
925 */
926 zone_last = ZONE_HIGHMEM;
927 if (N_MEMORY == N_HIGH_MEMORY)
928 zone_last = ZONE_MOVABLE;
929
930 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
931 arg->status_change_nid_high = nid;
932 else
933 arg->status_change_nid_high = -1;
934#else
935 arg->status_change_nid_high = arg->status_change_nid_normal;
936#endif
937
Lai Jiangshand9713672012-12-11 16:01:03 -0800938 /*
939 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800940 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800941 * is online.
942 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800943 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800944 arg->status_change_nid = nid;
945 else
946 arg->status_change_nid = -1;
947}
948
949static void node_states_set_node(int node, struct memory_notify *arg)
950{
951 if (arg->status_change_nid_normal >= 0)
952 node_set_state(node, N_NORMAL_MEMORY);
953
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800954 if (arg->status_change_nid_high >= 0)
955 node_set_state(node, N_HIGH_MEMORY);
956
957 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800958}
959
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700960
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800961int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700962{
Cody P Schaferaa472282013-07-03 15:02:10 -0700963 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700964 unsigned long onlined_pages = 0;
965 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700966 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700967 int nid;
968 int ret;
969 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700970
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700971 mem_hotplug_begin();
Lai Jiangshand9713672012-12-11 16:01:03 -0800972 /*
973 * This doesn't need a lock to do pfn_to_page().
974 * The section can't be removed here because of the
975 * memory_block->state_mutex.
976 */
977 zone = page_zone(pfn_to_page(pfn));
978
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700979 ret = -EINVAL;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700980 if ((zone_idx(zone) > ZONE_NORMAL ||
981 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700982 !can_online_high_movable(zone))
983 goto out;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800984
Tang Chen4f7c6b42014-08-06 16:05:13 -0700985 if (online_type == MMOP_ONLINE_KERNEL &&
986 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700987 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
988 goto out;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800989 }
Tang Chen4f7c6b42014-08-06 16:05:13 -0700990 if (online_type == MMOP_ONLINE_MOVABLE &&
991 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700992 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
993 goto out;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800994 }
995
996 /* Previous code may changed the zone of the pfn range */
997 zone = page_zone(pfn_to_page(pfn));
998
Yasunori Goto7b78d332007-10-21 16:41:36 -0700999 arg.start_pfn = pfn;
1000 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001001 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001002
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001003 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001004
1005 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1006 ret = notifier_to_errno(ret);
1007 if (ret) {
1008 memory_notify(MEM_CANCEL_ONLINE, &arg);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001009 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001010 }
Dave Hansen3947be12005-10-29 18:16:54 -07001011 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001012 * If this zone is not populated, then it is not in zonelist.
1013 * This means the page allocator ignores this zone.
1014 * So, zonelist must be updated after online.
1015 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001016 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001017 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001018 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001019 build_all_zonelists(NULL, zone);
1020 }
Yasunori Goto68113782006-06-23 02:03:11 -07001021
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001022 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001023 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001024 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001025 if (need_zonelists_rebuild)
1026 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001027 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001028 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1029 (unsigned long long) pfn << PAGE_SHIFT,
1030 (((unsigned long long) pfn + nr_pages)
1031 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001032 memory_notify(MEM_CANCEL_ONLINE, &arg);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001033 goto out;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001034 }
1035
Dave Hansen3947be12005-10-29 18:16:54 -07001036 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001037
1038 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001039 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001040 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1041
Jiang Liu08dff7b2012-07-31 16:43:30 -07001042 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001043 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001044 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001045 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001046 else
1047 zone_pcp_update(zone);
1048 }
Dave Hansen3947be12005-10-29 18:16:54 -07001049
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001050 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001051
1052 init_per_zone_wmark_min();
1053
Jiang Liu08dff7b2012-07-31 16:43:30 -07001054 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001055 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001056
Haicheng Li1f522502010-05-24 14:32:51 -07001057 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001058
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001059 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001060
1061 if (onlined_pages)
1062 memory_notify(MEM_ONLINE, &arg);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001063out:
1064 mem_hotplug_done();
1065 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001066}
Keith Mannthey53947022006-09-30 23:27:08 -07001067#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001068
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001069/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1070static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001071{
1072 struct pglist_data *pgdat;
1073 unsigned long zones_size[MAX_NR_ZONES] = {0};
1074 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001075 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001076
Tang Chena1e565a2013-02-22 16:33:18 -08001077 pgdat = NODE_DATA(nid);
1078 if (!pgdat) {
1079 pgdat = arch_alloc_nodedata(nid);
1080 if (!pgdat)
1081 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001082
Tang Chena1e565a2013-02-22 16:33:18 -08001083 arch_refresh_nodedata(nid, pgdat);
1084 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001085
1086 /* we can use NODE_DATA(nid) from here */
1087
1088 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001089 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001090
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001091 /*
1092 * The node we allocated has no zone fallback lists. For avoiding
1093 * to access not-initialized zonelist, build here.
1094 */
David Rientjesf957db42011-06-22 18:13:04 -07001095 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001096 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001097 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001098
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001099 return pgdat;
1100}
1101
1102static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1103{
1104 arch_refresh_nodedata(nid, NULL);
1105 arch_free_nodedata(pgdat);
1106 return;
1107}
1108
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001109
Toshi Kani01b0f192013-11-12 15:07:25 -08001110/**
1111 * try_online_node - online a node if offlined
1112 *
minskey guocf234222010-05-24 14:32:41 -07001113 * called by cpu_up() to online a node without onlined memory.
1114 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001115int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001116{
1117 pg_data_t *pgdat;
1118 int ret;
1119
Toshi Kani01b0f192013-11-12 15:07:25 -08001120 if (node_online(nid))
1121 return 0;
1122
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001123 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001124 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001125 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001126 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001127 ret = -ENOMEM;
1128 goto out;
1129 }
1130 node_set_online(nid);
1131 ret = register_one_node(nid);
1132 BUG_ON(ret);
1133
Toshi Kani01b0f192013-11-12 15:07:25 -08001134 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1135 mutex_lock(&zonelists_mutex);
1136 build_all_zonelists(NULL, NULL);
1137 mutex_unlock(&zonelists_mutex);
1138 }
1139
minskey guocf234222010-05-24 14:32:41 -07001140out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001141 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001142 return ret;
1143}
1144
Toshi Kani27356f52013-09-11 14:21:49 -07001145static int check_hotplug_memory_range(u64 start, u64 size)
1146{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001147 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001148 u64 nr_pages = size >> PAGE_SHIFT;
1149
1150 /* Memory range must be aligned with section */
1151 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1152 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1153 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1154 (unsigned long long)start,
1155 (unsigned long long)size);
1156 return -EINVAL;
1157 }
1158
1159 return 0;
1160}
1161
Al Viro31168482008-11-22 17:33:24 +00001162/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1163int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001164{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001165 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001166 bool new_pgdat;
1167 bool new_node;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001168 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001169 int ret;
1170
Toshi Kani27356f52013-09-11 14:21:49 -07001171 ret = check_hotplug_memory_range(start, size);
1172 if (ret)
1173 return ret;
1174
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001175 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001176 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001177 if (!res)
Nathan Zimmerac13c462014-01-23 15:53:26 -08001178 return ret;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001179
Tang Chena1e565a2013-02-22 16:33:18 -08001180 { /* Stupid hack to suppress address-never-null warning */
1181 void *p = NODE_DATA(nid);
1182 new_pgdat = !p;
1183 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001184
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001185 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001186
Tang Chena1e565a2013-02-22 16:33:18 -08001187 new_node = !node_online(nid);
1188 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001189 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001190 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001191 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001192 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001193 }
1194
Yasunori Gotobc02af92006-06-27 02:53:30 -07001195 /* call arch's memory hotadd */
1196 ret = arch_add_memory(nid, start, size);
1197
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001198 if (ret < 0)
1199 goto error;
1200
Yasunori Goto0fc44152006-06-27 02:53:38 -07001201 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001202 node_set_online(nid);
1203
Tang Chena1e565a2013-02-22 16:33:18 -08001204 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001205 ret = register_one_node(nid);
1206 /*
1207 * If sysfs file of new node can't create, cpu on the node
1208 * can't be hot-added. There is no rollback way now.
1209 * So, check by BUG_ON() to catch it reluctantly..
1210 */
1211 BUG_ON(ret);
1212 }
1213
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001214 /* create new memmap entry */
1215 firmware_map_add_hotplug(start, start + size, "System RAM");
1216
Andi Kleen6ad696d2009-11-17 14:06:22 -08001217 goto out;
1218
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001219error:
1220 /* rollback pgdat allocation and others */
1221 if (new_pgdat)
1222 rollback_node_hotadd(nid, pgdat);
Sasha Levina864b9d2013-02-22 16:32:48 -08001223 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001224
Andi Kleen6ad696d2009-11-17 14:06:22 -08001225out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001226 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001227 return ret;
1228}
1229EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001230
1231#ifdef CONFIG_MEMORY_HOTREMOVE
1232/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001233 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1234 * set and the size of the free page is given by page_order(). Using this,
1235 * the function determines if the pageblock contains only free pages.
1236 * Due to buddy contraints, a free page at least the size of a pageblock will
1237 * be located at the start of the pageblock
1238 */
1239static inline int pageblock_free(struct page *page)
1240{
1241 return PageBuddy(page) && page_order(page) >= pageblock_order;
1242}
1243
1244/* Return the start of the next active pageblock after a given page */
1245static struct page *next_active_pageblock(struct page *page)
1246{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001247 /* Ensure the starting page is pageblock-aligned */
1248 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1249
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001250 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001251 if (pageblock_free(page)) {
1252 int order;
1253 /* be careful. we don't have locks, page_order can be changed.*/
1254 order = page_order(page);
1255 if ((order < MAX_ORDER) && (order >= pageblock_order))
1256 return page + (1 << order);
1257 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001258
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001259 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001260}
1261
1262/* Checks if this range of memory is likely to be hot-removable. */
1263int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1264{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001265 struct page *page = pfn_to_page(start_pfn);
1266 struct page *end_page = page + nr_pages;
1267
1268 /* Check the starting page of each pageblock within the range */
1269 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001270 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001271 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001272 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001273 }
1274
1275 /* All pageblocks in the memory block are likely to be hot-removable */
1276 return 1;
1277}
1278
1279/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001280 * Confirm all pages in a range [start, end) is belongs to the same zone.
1281 */
1282static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1283{
1284 unsigned long pfn;
1285 struct zone *zone = NULL;
1286 struct page *page;
1287 int i;
1288 for (pfn = start_pfn;
1289 pfn < end_pfn;
1290 pfn += MAX_ORDER_NR_PAGES) {
1291 i = 0;
1292 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1293 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1294 i++;
1295 if (i == MAX_ORDER_NR_PAGES)
1296 continue;
1297 page = pfn_to_page(pfn + i);
1298 if (zone && page_zone(page) != zone)
1299 return 0;
1300 zone = page_zone(page);
1301 }
1302 return 1;
1303}
1304
1305/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001306 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1307 * and hugepages). We scan pfn because it's much easier than scanning over
1308 * linked list. This function returns the pfn of the first found movable
1309 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001310 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001311static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001312{
1313 unsigned long pfn;
1314 struct page *page;
1315 for (pfn = start; pfn < end; pfn++) {
1316 if (pfn_valid(pfn)) {
1317 page = pfn_to_page(pfn);
1318 if (PageLRU(page))
1319 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001320 if (PageHuge(page)) {
1321 if (is_hugepage_active(page))
1322 return pfn;
1323 else
1324 pfn = round_up(pfn + 1,
1325 1 << compound_order(page)) - 1;
1326 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001327 }
1328 }
1329 return 0;
1330}
1331
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001332#define NR_OFFLINE_AT_ONCE_PAGES (256)
1333static int
1334do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1335{
1336 unsigned long pfn;
1337 struct page *page;
1338 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1339 int not_managed = 0;
1340 int ret = 0;
1341 LIST_HEAD(source);
1342
1343 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1344 if (!pfn_valid(pfn))
1345 continue;
1346 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001347
1348 if (PageHuge(page)) {
1349 struct page *head = compound_head(page);
1350 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1351 if (compound_order(head) > PFN_SECTION_SHIFT) {
1352 ret = -EBUSY;
1353 break;
1354 }
1355 if (isolate_huge_page(page, &source))
1356 move_pages -= 1 << compound_order(head);
1357 continue;
1358 }
1359
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001360 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001361 continue;
1362 /*
1363 * We can skip free pages. And we can only deal with pages on
1364 * LRU.
1365 */
Nick Piggin62695a82008-10-18 20:26:09 -07001366 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001367 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001368 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001369 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001370 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001371 inc_zone_page_state(page, NR_ISOLATED_ANON +
1372 page_is_file_cache(page));
1373
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001374 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001375#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001376 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1377 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001378 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001379#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001380 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001381 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001382 check this again here. */
1383 if (page_count(page)) {
1384 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001385 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001386 break;
1387 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001388 }
1389 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001390 if (!list_empty(&source)) {
1391 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001392 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001393 goto out;
1394 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001395
1396 /*
1397 * alloc_migrate_target should be improooooved!!
1398 * migrate_pages returns # of failed pages.
1399 */
David Rientjes68711a72014-06-04 16:08:25 -07001400 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001401 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001402 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001403 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001404 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001405out:
1406 return ret;
1407}
1408
1409/*
1410 * remove from free_area[] and mark all as Reserved.
1411 */
1412static int
1413offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1414 void *data)
1415{
1416 __offline_isolated_pages(start, start + nr_pages);
1417 return 0;
1418}
1419
1420static void
1421offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1422{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001423 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001424 offline_isolated_pages_cb);
1425}
1426
1427/*
1428 * Check all pages in range, recoreded as memory resource, are isolated.
1429 */
1430static int
1431check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1432 void *data)
1433{
1434 int ret;
1435 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001436 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001437 offlined = nr_pages;
1438 if (!ret)
1439 *(long *)data += offlined;
1440 return ret;
1441}
1442
1443static long
1444check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1445{
1446 long offlined = 0;
1447 int ret;
1448
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001449 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001450 check_pages_isolated_cb);
1451 if (ret < 0)
1452 offlined = (long)ret;
1453 return offlined;
1454}
1455
Lai Jiangshan09285af2012-12-12 13:52:04 -08001456#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001457/*
1458 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1459 * normal memory.
1460 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001461static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1462{
1463 return true;
1464}
Tang Chen79a4dce2012-12-18 14:23:24 -08001465#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001466/* ensure the node has NORMAL memory if it is still online */
1467static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1468{
1469 struct pglist_data *pgdat = zone->zone_pgdat;
1470 unsigned long present_pages = 0;
1471 enum zone_type zt;
1472
1473 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1474 present_pages += pgdat->node_zones[zt].present_pages;
1475
1476 if (present_pages > nr_pages)
1477 return true;
1478
1479 present_pages = 0;
1480 for (; zt <= ZONE_MOVABLE; zt++)
1481 present_pages += pgdat->node_zones[zt].present_pages;
1482
1483 /*
1484 * we can't offline the last normal memory until all
1485 * higher memory is offlined.
1486 */
1487 return present_pages == 0;
1488}
Tang Chen79a4dce2012-12-18 14:23:24 -08001489#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001490
Tang Chenc5320922013-11-12 15:08:10 -08001491static int __init cmdline_parse_movable_node(char *p)
1492{
1493#ifdef CONFIG_MOVABLE_NODE
1494 /*
1495 * Memory used by the kernel cannot be hot-removed because Linux
1496 * cannot migrate the kernel pages. When memory hotplug is
1497 * enabled, we should prevent memblock from allocating memory
1498 * for the kernel.
1499 *
1500 * ACPI SRAT records all hotpluggable memory ranges. But before
1501 * SRAT is parsed, we don't know about it.
1502 *
1503 * The kernel image is loaded into memory at very early time. We
1504 * cannot prevent this anyway. So on NUMA system, we set any
1505 * node the kernel resides in as un-hotpluggable.
1506 *
1507 * Since on modern servers, one node could have double-digit
1508 * gigabytes memory, we can assume the memory around the kernel
1509 * image is also un-hotpluggable. So before SRAT is parsed, just
1510 * allocate memory near the kernel image to try the best to keep
1511 * the kernel away from hotpluggable memory.
1512 */
1513 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001514 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001515#else
1516 pr_warn("movable_node option not supported\n");
1517#endif
1518 return 0;
1519}
1520early_param("movable_node", cmdline_parse_movable_node);
1521
Lai Jiangshand9713672012-12-11 16:01:03 -08001522/* check which state of node_states will be changed when offline memory */
1523static void node_states_check_changes_offline(unsigned long nr_pages,
1524 struct zone *zone, struct memory_notify *arg)
1525{
1526 struct pglist_data *pgdat = zone->zone_pgdat;
1527 unsigned long present_pages = 0;
1528 enum zone_type zt, zone_last = ZONE_NORMAL;
1529
1530 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001531 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1532 * contains nodes which have zones of 0...ZONE_NORMAL,
1533 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001534 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001535 * If we don't have HIGHMEM nor movable node,
1536 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1537 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001538 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001539 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001540 zone_last = ZONE_MOVABLE;
1541
1542 /*
1543 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1544 * If the memory to be offline is in a zone of 0...zone_last,
1545 * and it is the last present memory, 0...zone_last will
1546 * become empty after offline , thus we can determind we will
1547 * need to clear the node from node_states[N_NORMAL_MEMORY].
1548 */
1549 for (zt = 0; zt <= zone_last; zt++)
1550 present_pages += pgdat->node_zones[zt].present_pages;
1551 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1552 arg->status_change_nid_normal = zone_to_nid(zone);
1553 else
1554 arg->status_change_nid_normal = -1;
1555
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001556#ifdef CONFIG_HIGHMEM
1557 /*
1558 * If we have movable node, node_states[N_HIGH_MEMORY]
1559 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1560 * set zone_last to ZONE_HIGHMEM.
1561 *
1562 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1563 * contains nodes which have zones of 0...ZONE_MOVABLE,
1564 * set zone_last to ZONE_MOVABLE.
1565 */
1566 zone_last = ZONE_HIGHMEM;
1567 if (N_MEMORY == N_HIGH_MEMORY)
1568 zone_last = ZONE_MOVABLE;
1569
1570 for (; zt <= zone_last; zt++)
1571 present_pages += pgdat->node_zones[zt].present_pages;
1572 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1573 arg->status_change_nid_high = zone_to_nid(zone);
1574 else
1575 arg->status_change_nid_high = -1;
1576#else
1577 arg->status_change_nid_high = arg->status_change_nid_normal;
1578#endif
1579
Lai Jiangshand9713672012-12-11 16:01:03 -08001580 /*
1581 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1582 */
1583 zone_last = ZONE_MOVABLE;
1584
1585 /*
1586 * check whether node_states[N_HIGH_MEMORY] will be changed
1587 * If we try to offline the last present @nr_pages from the node,
1588 * we can determind we will need to clear the node from
1589 * node_states[N_HIGH_MEMORY].
1590 */
1591 for (; zt <= zone_last; zt++)
1592 present_pages += pgdat->node_zones[zt].present_pages;
1593 if (nr_pages >= present_pages)
1594 arg->status_change_nid = zone_to_nid(zone);
1595 else
1596 arg->status_change_nid = -1;
1597}
1598
1599static void node_states_clear_node(int node, struct memory_notify *arg)
1600{
1601 if (arg->status_change_nid_normal >= 0)
1602 node_clear_state(node, N_NORMAL_MEMORY);
1603
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001604 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1605 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001606 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001607
1608 if ((N_MEMORY != N_HIGH_MEMORY) &&
1609 (arg->status_change_nid >= 0))
1610 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001611}
1612
Wen Congyanga16cee12012-10-08 16:33:58 -07001613static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001614 unsigned long end_pfn, unsigned long timeout)
1615{
1616 unsigned long pfn, nr_pages, expire;
1617 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001618 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001619 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001620 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001621 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001622
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001623 /* at least, alignment against pageblock is necessary */
1624 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1625 return -EINVAL;
1626 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1627 return -EINVAL;
1628 /* This makes hotplug much easier...and readable.
1629 we assume this for now. .*/
1630 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1631 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001632
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001633 mem_hotplug_begin();
Andi Kleen6ad696d2009-11-17 14:06:22 -08001634
Yasunori Goto7b78d332007-10-21 16:41:36 -07001635 zone = page_zone(pfn_to_page(start_pfn));
1636 node = zone_to_nid(zone);
1637 nr_pages = end_pfn - start_pfn;
1638
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001639 ret = -EINVAL;
1640 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1641 goto out;
1642
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001643 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001644 ret = start_isolate_page_range(start_pfn, end_pfn,
1645 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001646 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -08001647 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001648
1649 arg.start_pfn = start_pfn;
1650 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001651 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001652
1653 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1654 ret = notifier_to_errno(ret);
1655 if (ret)
1656 goto failed_removal;
1657
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001658 pfn = start_pfn;
1659 expire = jiffies + timeout;
1660 drain = 0;
1661 retry_max = 5;
1662repeat:
1663 /* start memory hot removal */
1664 ret = -EAGAIN;
1665 if (time_after(jiffies, expire))
1666 goto failed_removal;
1667 ret = -EINTR;
1668 if (signal_pending(current))
1669 goto failed_removal;
1670 ret = 0;
1671 if (drain) {
1672 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001673 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001674 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001675 }
1676
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001677 pfn = scan_movable_pages(start_pfn, end_pfn);
1678 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001679 ret = do_migrate_range(pfn, end_pfn);
1680 if (!ret) {
1681 drain = 1;
1682 goto repeat;
1683 } else {
1684 if (ret < 0)
1685 if (--retry_max == 0)
1686 goto failed_removal;
1687 yield();
1688 drain = 1;
1689 goto repeat;
1690 }
1691 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001692 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001693 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001694 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001695 /* drain pcp pages, this is synchronous. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001696 drain_all_pages();
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001697 /*
1698 * dissolve free hugepages in the memory block before doing offlining
1699 * actually in order to make hugetlbfs's object counting consistent.
1700 */
1701 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001702 /* check again */
1703 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1704 if (offlined_pages < 0) {
1705 ret = -EBUSY;
1706 goto failed_removal;
1707 }
1708 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001709 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001710 We cannot do rollback at this point. */
1711 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001712 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001713 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001714 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001715 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001716 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001717
1718 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001719 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001720 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001721
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001722 init_per_zone_wmark_min();
1723
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001724 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001725 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001726 mutex_lock(&zonelists_mutex);
1727 build_all_zonelists(NULL, NULL);
1728 mutex_unlock(&zonelists_mutex);
1729 } else
1730 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001731
Lai Jiangshand9713672012-12-11 16:01:03 -08001732 node_states_clear_node(node, &arg);
1733 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001734 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001735
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001736 vm_total_pages = nr_free_pagecache_pages();
1737 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001738
1739 memory_notify(MEM_OFFLINE, &arg);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001740 mem_hotplug_done();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001741 return 0;
1742
1743failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001744 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1745 (unsigned long long) start_pfn << PAGE_SHIFT,
1746 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001747 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001748 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001749 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001750
Andi Kleen6ad696d2009-11-17 14:06:22 -08001751out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001752 mem_hotplug_done();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001753 return ret;
1754}
Badari Pulavarty71088782008-10-18 20:25:58 -07001755
Wen Congyanga16cee12012-10-08 16:33:58 -07001756int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1757{
1758 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1759}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001760#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001761
Wen Congyangbbc76be2013-02-22 16:32:54 -08001762/**
1763 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1764 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001765 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001766 * @arg: argument passed to func
1767 * @func: callback for each memory section walked
1768 *
1769 * This function walks through all present mem sections in range
1770 * [start_pfn, end_pfn) and call func on each mem section.
1771 *
1772 * Returns the return value of func.
1773 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001774int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001775 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001776{
Wen Congyange90bdb72012-10-08 16:34:01 -07001777 struct memory_block *mem = NULL;
1778 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001779 unsigned long pfn, section_nr;
1780 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001781
Wen Congyange90bdb72012-10-08 16:34:01 -07001782 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1783 section_nr = pfn_to_section_nr(pfn);
1784 if (!present_section_nr(section_nr))
1785 continue;
1786
1787 section = __nr_to_section(section_nr);
1788 /* same memblock? */
1789 if (mem)
1790 if ((section_nr >= mem->start_section_nr) &&
1791 (section_nr <= mem->end_section_nr))
1792 continue;
1793
1794 mem = find_memory_block_hinted(section, mem);
1795 if (!mem)
1796 continue;
1797
Wen Congyangbbc76be2013-02-22 16:32:54 -08001798 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001799 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001800 kobject_put(&mem->dev.kobj);
1801 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001802 }
1803 }
1804
1805 if (mem)
1806 kobject_put(&mem->dev.kobj);
1807
Wen Congyangbbc76be2013-02-22 16:32:54 -08001808 return 0;
1809}
1810
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001811#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001812static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001813{
1814 int ret = !is_memblock_offlined(mem);
1815
Randy Dunlap349daa02013-04-29 15:08:49 -07001816 if (unlikely(ret)) {
1817 phys_addr_t beginpa, endpa;
1818
1819 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1820 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001821 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001822 "[%pa-%pa] is onlined\n",
1823 &beginpa, &endpa);
1824 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001825
1826 return ret;
1827}
1828
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001829static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001830{
Tang Chen60a5a192013-02-22 16:33:14 -08001831 int cpu;
1832
1833 for_each_present_cpu(cpu) {
1834 if (cpu_to_node(cpu) == pgdat->node_id)
1835 /*
1836 * the cpu on this node isn't removed, and we can't
1837 * offline this node.
1838 */
1839 return -EBUSY;
1840 }
1841
1842 return 0;
1843}
1844
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001845static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001846{
1847#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001848 int cpu;
1849
1850 for_each_possible_cpu(cpu)
1851 if (cpu_to_node(cpu) == pgdat->node_id)
1852 numa_clear_node(cpu);
1853#endif
1854}
1855
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001856static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001857{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001858 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001859
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001860 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001861 if (ret)
1862 return ret;
1863
1864 /*
1865 * the node will be offlined when we come here, so we can clear
1866 * the cpu_to_node() now.
1867 */
1868
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001869 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001870 return 0;
1871}
1872
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001873/**
1874 * try_offline_node
1875 *
1876 * Offline a node if all memory sections and cpus of the node are removed.
1877 *
1878 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1879 * and online/offline operations before this call.
1880 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001881void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001882{
Wen Congyangd822b862013-02-22 16:33:16 -08001883 pg_data_t *pgdat = NODE_DATA(nid);
1884 unsigned long start_pfn = pgdat->node_start_pfn;
1885 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001886 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08001887 struct page *pgdat_page = virt_to_page(pgdat);
1888 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08001889
1890 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1891 unsigned long section_nr = pfn_to_section_nr(pfn);
1892
1893 if (!present_section_nr(section_nr))
1894 continue;
1895
1896 if (pfn_to_nid(pfn) != nid)
1897 continue;
1898
1899 /*
1900 * some memory sections of this node are not removed, and we
1901 * can't offline node now.
1902 */
1903 return;
1904 }
1905
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001906 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001907 return;
1908
1909 /*
1910 * all memory/cpu of this node are removed, we can offline this
1911 * node now.
1912 */
1913 node_set_offline(nid);
1914 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08001915
1916 if (!PageSlab(pgdat_page) && !PageCompound(pgdat_page))
1917 /* node data is allocated from boot memory */
1918 return;
1919
1920 /* free waittable in each zone */
1921 for (i = 0; i < MAX_NR_ZONES; i++) {
1922 struct zone *zone = pgdat->node_zones + i;
1923
Jianguo Wuca4b3f32013-03-22 15:04:50 -07001924 /*
1925 * wait_table may be allocated from boot memory,
1926 * here only free if it's allocated by vmalloc.
1927 */
1928 if (is_vmalloc_addr(zone->wait_table))
Wen Congyangd822b862013-02-22 16:33:16 -08001929 vfree(zone->wait_table);
1930 }
1931
1932 /*
1933 * Since there is no way to guarentee the address of pgdat/zone is not
1934 * on stack of any kernel threads or used by other kernel objects
1935 * without reference counting or other symchronizing method, do not
1936 * reset node_data and free pgdat here. Just reset it to 0 and reuse
1937 * the memory when the node is online again.
1938 */
1939 memset(pgdat, 0, sizeof(*pgdat));
Tang Chen60a5a192013-02-22 16:33:14 -08001940}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001941EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001942
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001943/**
1944 * remove_memory
1945 *
1946 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1947 * and online/offline operations before this call, as required by
1948 * try_offline_node().
1949 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001950void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001951{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001952 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001953
Toshi Kani27356f52013-09-11 14:21:49 -07001954 BUG_ON(check_hotplug_memory_range(start, size));
1955
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001956 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001957
1958 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001959 * All memory blocks must be offlined before removing memory. Check
1960 * whether all memory blocks in question are offline and trigger a BUG()
1961 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001962 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001963 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08001964 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001965 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001966 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001967
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001968 /* remove memmap entry */
1969 firmware_map_remove(start, start + size, "System RAM");
1970
Wen Congyang24d335c2013-02-22 16:32:58 -08001971 arch_remove_memory(start, size);
1972
Tang Chen60a5a192013-02-22 16:33:14 -08001973 try_offline_node(nid);
1974
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001975 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07001976}
Badari Pulavarty71088782008-10-18 20:25:58 -07001977EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02001978#endif /* CONFIG_MEMORY_HOTREMOVE */