blob: 0eb1a1df649d8a149b02eb5989682a5c61e73b3f [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>
12#include <linux/bootmem.h>
13#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070016#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
21#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080032#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070033#include <linux/hugetlb.h>
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;
49
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080050DEFINE_MUTEX(mem_hotplug_mutex);
51
52void lock_memory_hotplug(void)
53{
54 mutex_lock(&mem_hotplug_mutex);
55
56 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
57 lock_system_sleep();
58}
59
60void unlock_memory_hotplug(void)
61{
62 unlock_system_sleep();
63 mutex_unlock(&mem_hotplug_mutex);
64}
65
66
Keith Mannthey45e0b782006-09-30 23:27:09 -070067/* add this memory to iomem resource */
68static struct resource *register_memory_resource(u64 start, u64 size)
69{
70 struct resource *res;
71 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
72 BUG_ON(!res);
73
74 res->name = "System RAM";
75 res->start = start;
76 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -080077 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -070078 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -070079 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -070080 kfree(res);
81 res = NULL;
82 }
83 return res;
84}
85
86static void release_memory_resource(struct resource *res)
87{
88 if (!res)
89 return;
90 release_resource(res);
91 kfree(res);
92 return;
93}
94
Keith Mannthey53947022006-09-30 23:27:08 -070095#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -080096void get_page_bootmem(unsigned long info, struct page *page,
97 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -070098{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080099 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700100 SetPagePrivate(page);
101 set_page_private(page, info);
102 atomic_inc(&page->_count);
103}
104
Jiang Liu170a5a72013-07-03 15:03:17 -0700105void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700106{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800107 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700108
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800109 type = (unsigned long) page->lru.next;
110 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
111 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700112
113 if (atomic_dec_return(&page->_count) == 1) {
114 ClearPagePrivate(page);
115 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800116 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700117 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700118 }
Yasunori Goto04753272008-04-28 02:13:31 -0700119}
120
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800121#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
122#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700123static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700124{
125 unsigned long *usemap, mapsize, section_nr, i;
126 struct mem_section *ms;
127 struct page *page, *memmap;
128
Yasunori Goto04753272008-04-28 02:13:31 -0700129 section_nr = pfn_to_section_nr(start_pfn);
130 ms = __nr_to_section(section_nr);
131
132 /* Get section's memmap address */
133 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
134
135 /*
136 * Get page for the memmap's phys address
137 * XXX: need more consideration for sparse_vmemmap...
138 */
139 page = virt_to_page(memmap);
140 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
141 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
142
143 /* remember memmap's page */
144 for (i = 0; i < mapsize; i++, page++)
145 get_page_bootmem(section_nr, page, SECTION_INFO);
146
147 usemap = __nr_to_section(section_nr)->pageblock_flags;
148 page = virt_to_page(usemap);
149
150 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
151
152 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700153 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700154
155}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800156#else /* CONFIG_SPARSEMEM_VMEMMAP */
157static void register_page_bootmem_info_section(unsigned long start_pfn)
158{
159 unsigned long *usemap, mapsize, section_nr, i;
160 struct mem_section *ms;
161 struct page *page, *memmap;
162
163 if (!pfn_valid(start_pfn))
164 return;
165
166 section_nr = pfn_to_section_nr(start_pfn);
167 ms = __nr_to_section(section_nr);
168
169 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
170
171 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
172
173 usemap = __nr_to_section(section_nr)->pageblock_flags;
174 page = virt_to_page(usemap);
175
176 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
177
178 for (i = 0; i < mapsize; i++, page++)
179 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
180}
181#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700182
183void register_page_bootmem_info_node(struct pglist_data *pgdat)
184{
185 unsigned long i, pfn, end_pfn, nr_pages;
186 int node = pgdat->node_id;
187 struct page *page;
188 struct zone *zone;
189
190 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
191 page = virt_to_page(pgdat);
192
193 for (i = 0; i < nr_pages; i++, page++)
194 get_page_bootmem(node, page, NODE_INFO);
195
196 zone = &pgdat->node_zones[0];
197 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700198 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700199 nr_pages = zone->wait_table_hash_nr_entries
200 * sizeof(wait_queue_head_t);
201 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
202 page = virt_to_page(zone->wait_table);
203
204 for (i = 0; i < nr_pages; i++, page++)
205 get_page_bootmem(node, page, NODE_INFO);
206 }
207 }
208
209 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800210 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700211
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700212 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700213 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
214 /*
215 * Some platforms can assign the same pfn to multiple nodes - on
216 * node0 as well as nodeN. To avoid registering a pfn against
217 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700218 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700219 */
220 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
221 register_page_bootmem_info_section(pfn);
222 }
Yasunori Goto04753272008-04-28 02:13:31 -0700223}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800224#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700225
Heiko Carstens76cdd582008-05-14 16:05:52 -0700226static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
227 unsigned long end_pfn)
228{
229 unsigned long old_zone_end_pfn;
230
231 zone_span_writelock(zone);
232
Xishi Qiuc33bc312013-09-11 14:21:44 -0700233 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700234 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700235 zone->zone_start_pfn = start_pfn;
236
237 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
238 zone->zone_start_pfn;
239
240 zone_span_writeunlock(zone);
241}
242
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800243static void resize_zone(struct zone *zone, unsigned long start_pfn,
244 unsigned long end_pfn)
245{
246 zone_span_writelock(zone);
247
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800248 if (end_pfn - start_pfn) {
249 zone->zone_start_pfn = start_pfn;
250 zone->spanned_pages = end_pfn - start_pfn;
251 } else {
252 /*
253 * make it consist as free_area_init_core(),
254 * if spanned_pages = 0, then keep start_pfn = 0
255 */
256 zone->zone_start_pfn = 0;
257 zone->spanned_pages = 0;
258 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800259
260 zone_span_writeunlock(zone);
261}
262
263static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
264 unsigned long end_pfn)
265{
266 enum zone_type zid = zone_idx(zone);
267 int nid = zone->zone_pgdat->node_id;
268 unsigned long pfn;
269
270 for (pfn = start_pfn; pfn < end_pfn; pfn++)
271 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
272}
273
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800274/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
275 * alloc_bootmem_node_nopanic() */
276static int __ref ensure_zone_is_initialized(struct zone *zone,
277 unsigned long start_pfn, unsigned long num_pages)
278{
279 if (!zone_is_initialized(zone))
280 return init_currently_empty_zone(zone, start_pfn, num_pages,
281 MEMMAP_HOTPLUG);
282 return 0;
283}
284
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800285static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800286 unsigned long start_pfn, unsigned long end_pfn)
287{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800288 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800289 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800290 unsigned long z1_start_pfn;
291
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800292 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
293 if (ret)
294 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800295
296 pgdat_resize_lock(z1->zone_pgdat, &flags);
297
298 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800299 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800300 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700301 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800302 if (start_pfn > z2->zone_start_pfn)
303 goto out_fail;
304 /* must included/overlap */
305 if (end_pfn <= z2->zone_start_pfn)
306 goto out_fail;
307
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800308 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700309 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800310 z1_start_pfn = z1->zone_start_pfn;
311 else
312 z1_start_pfn = start_pfn;
313
314 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800315 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800316
317 pgdat_resize_unlock(z1->zone_pgdat, &flags);
318
319 fix_zone_id(z1, start_pfn, end_pfn);
320
321 return 0;
322out_fail:
323 pgdat_resize_unlock(z1->zone_pgdat, &flags);
324 return -1;
325}
326
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800327static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800328 unsigned long start_pfn, unsigned long end_pfn)
329{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800330 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800331 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800332 unsigned long z2_end_pfn;
333
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800334 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
335 if (ret)
336 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800337
338 pgdat_resize_lock(z1->zone_pgdat, &flags);
339
340 /* can't move pfns which are lower than @z1 */
341 if (z1->zone_start_pfn > start_pfn)
342 goto out_fail;
343 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800344 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800345 goto out_fail;
346 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800347 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800348 goto out_fail;
349
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800350 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700351 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800352 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800353 else
354 z2_end_pfn = end_pfn;
355
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800356 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800357 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800358
359 pgdat_resize_unlock(z1->zone_pgdat, &flags);
360
361 fix_zone_id(z2, start_pfn, end_pfn);
362
363 return 0;
364out_fail:
365 pgdat_resize_unlock(z1->zone_pgdat, &flags);
366 return -1;
367}
368
Heiko Carstens76cdd582008-05-14 16:05:52 -0700369static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
370 unsigned long end_pfn)
371{
372 unsigned long old_pgdat_end_pfn =
373 pgdat->node_start_pfn + pgdat->node_spanned_pages;
374
Tang Chen712cd382012-12-11 16:01:07 -0800375 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700376 pgdat->node_start_pfn = start_pfn;
377
378 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
379 pgdat->node_start_pfn;
380}
381
Al Viro31168482008-11-22 17:33:24 +0000382static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700383{
384 struct pglist_data *pgdat = zone->zone_pgdat;
385 int nr_pages = PAGES_PER_SECTION;
386 int nid = pgdat->node_id;
387 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700388 unsigned long flags;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800389 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700390
391 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800392 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
393 if (ret)
394 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700395
Heiko Carstens76cdd582008-05-14 16:05:52 -0700396 pgdat_resize_lock(zone->zone_pgdat, &flags);
397 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
398 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
399 phys_start_pfn + nr_pages);
400 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa02007-01-10 23:15:30 -0800401 memmap_init_zone(nr_pages, nid, zone_type,
402 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700403 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700404}
405
Gary Hadec04fc582009-01-06 14:39:14 -0800406static int __meminit __add_section(int nid, struct zone *zone,
407 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700408{
Dave Hansen3947be12005-10-29 18:16:54 -0700409 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700410 int ret;
411
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700412 if (pfn_valid(phys_start_pfn))
413 return -EEXIST;
414
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700415 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700416
417 if (ret < 0)
418 return ret;
419
Yasunori Goto718127c2006-06-23 02:03:10 -0700420 ret = __add_zone(zone, phys_start_pfn);
421
422 if (ret < 0)
423 return ret;
424
Gary Hadec04fc582009-01-06 14:39:14 -0800425 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700426}
427
David Rientjes4edd7ce2013-04-29 15:08:22 -0700428/*
429 * Reasonably generic function for adding memory. It is
430 * expected that archs that support memory hotplug will
431 * call this function after deciding the zone to which to
432 * add the new pages.
433 */
434int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
435 unsigned long nr_pages)
436{
437 unsigned long i;
438 int err = 0;
439 int start_sec, end_sec;
440 /* during initialize mem_map, align hot-added range to section */
441 start_sec = pfn_to_section_nr(phys_start_pfn);
442 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
443
444 for (i = start_sec; i <= end_sec; i++) {
445 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
446
447 /*
448 * EEXIST is finally dealt with by ioresource collision
449 * check. see add_memory() => register_memory_resource()
450 * Warning will be printed if there is collision.
451 */
452 if (err && (err != -EEXIST))
453 break;
454 err = 0;
455 }
456
457 return err;
458}
459EXPORT_SYMBOL_GPL(__add_pages);
460
461#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800462/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
463static int find_smallest_section_pfn(int nid, struct zone *zone,
464 unsigned long start_pfn,
465 unsigned long end_pfn)
466{
467 struct mem_section *ms;
468
469 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
470 ms = __pfn_to_section(start_pfn);
471
472 if (unlikely(!valid_section(ms)))
473 continue;
474
475 if (unlikely(pfn_to_nid(start_pfn) != nid))
476 continue;
477
478 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
479 continue;
480
481 return start_pfn;
482 }
483
484 return 0;
485}
486
487/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
488static int find_biggest_section_pfn(int nid, struct zone *zone,
489 unsigned long start_pfn,
490 unsigned long end_pfn)
491{
492 struct mem_section *ms;
493 unsigned long pfn;
494
495 /* pfn is the end pfn of a memory section. */
496 pfn = end_pfn - 1;
497 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
498 ms = __pfn_to_section(pfn);
499
500 if (unlikely(!valid_section(ms)))
501 continue;
502
503 if (unlikely(pfn_to_nid(pfn) != nid))
504 continue;
505
506 if (zone && zone != page_zone(pfn_to_page(pfn)))
507 continue;
508
509 return pfn;
510 }
511
512 return 0;
513}
514
515static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
516 unsigned long end_pfn)
517{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700518 unsigned long zone_start_pfn = zone->zone_start_pfn;
519 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
520 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800521 unsigned long pfn;
522 struct mem_section *ms;
523 int nid = zone_to_nid(zone);
524
525 zone_span_writelock(zone);
526 if (zone_start_pfn == start_pfn) {
527 /*
528 * If the section is smallest section in the zone, it need
529 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
530 * In this case, we find second smallest valid mem_section
531 * for shrinking zone.
532 */
533 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
534 zone_end_pfn);
535 if (pfn) {
536 zone->zone_start_pfn = pfn;
537 zone->spanned_pages = zone_end_pfn - pfn;
538 }
539 } else if (zone_end_pfn == end_pfn) {
540 /*
541 * If the section is biggest section in the zone, it need
542 * shrink zone->spanned_pages.
543 * In this case, we find second biggest valid mem_section for
544 * shrinking zone.
545 */
546 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
547 start_pfn);
548 if (pfn)
549 zone->spanned_pages = pfn - zone_start_pfn + 1;
550 }
551
552 /*
553 * The section is not biggest or smallest mem_section in the zone, it
554 * only creates a hole in the zone. So in this case, we need not
555 * change the zone. But perhaps, the zone has only hole data. Thus
556 * it check the zone has only hole or not.
557 */
558 pfn = zone_start_pfn;
559 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
560 ms = __pfn_to_section(pfn);
561
562 if (unlikely(!valid_section(ms)))
563 continue;
564
565 if (page_zone(pfn_to_page(pfn)) != zone)
566 continue;
567
568 /* If the section is current section, it continues the loop */
569 if (start_pfn == pfn)
570 continue;
571
572 /* If we find valid section, we have nothing to do */
573 zone_span_writeunlock(zone);
574 return;
575 }
576
577 /* The zone has no valid section */
578 zone->zone_start_pfn = 0;
579 zone->spanned_pages = 0;
580 zone_span_writeunlock(zone);
581}
582
583static void shrink_pgdat_span(struct pglist_data *pgdat,
584 unsigned long start_pfn, unsigned long end_pfn)
585{
586 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
587 unsigned long pgdat_end_pfn =
588 pgdat->node_start_pfn + pgdat->node_spanned_pages;
589 unsigned long pfn;
590 struct mem_section *ms;
591 int nid = pgdat->node_id;
592
593 if (pgdat_start_pfn == start_pfn) {
594 /*
595 * If the section is smallest section in the pgdat, it need
596 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
597 * In this case, we find second smallest valid mem_section
598 * for shrinking zone.
599 */
600 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
601 pgdat_end_pfn);
602 if (pfn) {
603 pgdat->node_start_pfn = pfn;
604 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
605 }
606 } else if (pgdat_end_pfn == end_pfn) {
607 /*
608 * If the section is biggest section in the pgdat, it need
609 * shrink pgdat->node_spanned_pages.
610 * In this case, we find second biggest valid mem_section for
611 * shrinking zone.
612 */
613 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
614 start_pfn);
615 if (pfn)
616 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
617 }
618
619 /*
620 * If the section is not biggest or smallest mem_section in the pgdat,
621 * it only creates a hole in the pgdat. So in this case, we need not
622 * change the pgdat.
623 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
624 * has only hole or not.
625 */
626 pfn = pgdat_start_pfn;
627 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
628 ms = __pfn_to_section(pfn);
629
630 if (unlikely(!valid_section(ms)))
631 continue;
632
633 if (pfn_to_nid(pfn) != nid)
634 continue;
635
636 /* If the section is current section, it continues the loop */
637 if (start_pfn == pfn)
638 continue;
639
640 /* If we find valid section, we have nothing to do */
641 return;
642 }
643
644 /* The pgdat has no valid section */
645 pgdat->node_start_pfn = 0;
646 pgdat->node_spanned_pages = 0;
647}
648
649static void __remove_zone(struct zone *zone, unsigned long start_pfn)
650{
651 struct pglist_data *pgdat = zone->zone_pgdat;
652 int nr_pages = PAGES_PER_SECTION;
653 int zone_type;
654 unsigned long flags;
655
656 zone_type = zone - pgdat->node_zones;
657
658 pgdat_resize_lock(zone->zone_pgdat, &flags);
659 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
660 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
661 pgdat_resize_unlock(zone->zone_pgdat, &flags);
662}
663
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700664static int __remove_section(struct zone *zone, struct mem_section *ms)
665{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800666 unsigned long start_pfn;
667 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700668 int ret = -EINVAL;
669
670 if (!valid_section(ms))
671 return ret;
672
673 ret = unregister_memory_section(ms);
674 if (ret)
675 return ret;
676
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800677 scn_nr = __section_nr(ms);
678 start_pfn = section_nr_to_pfn(scn_nr);
679 __remove_zone(zone, start_pfn);
680
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700681 sparse_remove_one_section(zone, ms);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700682 return 0;
683}
684
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700685/**
686 * __remove_pages() - remove sections of pages from a zone
687 * @zone: zone from which pages need to be removed
688 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
689 * @nr_pages: number of pages to remove (must be multiple of section size)
690 *
691 * Generic helper function to remove section mappings and sysfs entries
692 * for the section of the memory we are removing. Caller needs to make
693 * sure that pages are marked reserved and zones are adjust properly by
694 * calling offline_pages().
695 */
696int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
697 unsigned long nr_pages)
698{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700699 unsigned long i;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700700 int sections_to_remove;
Toshi Kanife74ebb2013-04-29 15:08:20 -0700701 resource_size_t start, size;
702 int ret = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700703
704 /*
705 * We can only remove entire sections
706 */
707 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
708 BUG_ON(nr_pages % PAGES_PER_SECTION);
709
Toshi Kanife74ebb2013-04-29 15:08:20 -0700710 start = phys_start_pfn << PAGE_SHIFT;
711 size = nr_pages * PAGE_SIZE;
712 ret = release_mem_region_adjustable(&iomem_resource, start, size);
Randy Dunlap348f9f02013-05-24 15:55:30 -0700713 if (ret) {
714 resource_size_t endres = start + size - 1;
715
716 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
717 &start, &endres, ret);
718 }
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700719
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700720 sections_to_remove = nr_pages / PAGES_PER_SECTION;
721 for (i = 0; i < sections_to_remove; i++) {
722 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
723 ret = __remove_section(zone, __pfn_to_section(pfn));
724 if (ret)
725 break;
726 }
727 return ret;
728}
729EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700730#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700731
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700732int set_online_page_callback(online_page_callback_t callback)
733{
734 int rc = -EINVAL;
735
736 lock_memory_hotplug();
737
738 if (online_page_callback == generic_online_page) {
739 online_page_callback = callback;
740 rc = 0;
741 }
742
743 unlock_memory_hotplug();
744
745 return rc;
746}
747EXPORT_SYMBOL_GPL(set_online_page_callback);
748
749int restore_online_page_callback(online_page_callback_t callback)
750{
751 int rc = -EINVAL;
752
753 lock_memory_hotplug();
754
755 if (online_page_callback == callback) {
756 online_page_callback = generic_online_page;
757 rc = 0;
758 }
759
760 unlock_memory_hotplug();
761
762 return rc;
763}
764EXPORT_SYMBOL_GPL(restore_online_page_callback);
765
766void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700767{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700768}
769EXPORT_SYMBOL_GPL(__online_page_set_limits);
770
771void __online_page_increment_counters(struct page *page)
772{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700773 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700774}
775EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700776
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700777void __online_page_free(struct page *page)
778{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700779 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700780}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700781EXPORT_SYMBOL_GPL(__online_page_free);
782
783static void generic_online_page(struct page *page)
784{
785 __online_page_set_limits(page);
786 __online_page_increment_counters(page);
787 __online_page_free(page);
788}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700789
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700790static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
791 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700792{
793 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700794 unsigned long onlined_pages = *(unsigned long *)arg;
795 struct page *page;
796 if (PageReserved(pfn_to_page(start_pfn)))
797 for (i = 0; i < nr_pages; i++) {
798 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700799 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700800 onlined_pages++;
801 }
802 *(unsigned long *)arg = onlined_pages;
803 return 0;
804}
805
Lai Jiangshan09285af2012-12-12 13:52:04 -0800806#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800807/*
808 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
809 * normal memory.
810 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800811static bool can_online_high_movable(struct zone *zone)
812{
813 return true;
814}
Tang Chen79a4dce2012-12-18 14:23:24 -0800815#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800816/* ensure every online node has NORMAL memory */
817static bool can_online_high_movable(struct zone *zone)
818{
819 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
820}
Tang Chen79a4dce2012-12-18 14:23:24 -0800821#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800822
Lai Jiangshand9713672012-12-11 16:01:03 -0800823/* check which state of node_states will be changed when online memory */
824static void node_states_check_changes_online(unsigned long nr_pages,
825 struct zone *zone, struct memory_notify *arg)
826{
827 int nid = zone_to_nid(zone);
828 enum zone_type zone_last = ZONE_NORMAL;
829
830 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800831 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
832 * contains nodes which have zones of 0...ZONE_NORMAL,
833 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800834 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800835 * If we don't have HIGHMEM nor movable node,
836 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
837 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800838 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800839 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800840 zone_last = ZONE_MOVABLE;
841
842 /*
843 * if the memory to be online is in a zone of 0...zone_last, and
844 * the zones of 0...zone_last don't have memory before online, we will
845 * need to set the node to node_states[N_NORMAL_MEMORY] after
846 * the memory is online.
847 */
848 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
849 arg->status_change_nid_normal = nid;
850 else
851 arg->status_change_nid_normal = -1;
852
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800853#ifdef CONFIG_HIGHMEM
854 /*
855 * If we have movable node, node_states[N_HIGH_MEMORY]
856 * contains nodes which have zones of 0...ZONE_HIGHMEM,
857 * set zone_last to ZONE_HIGHMEM.
858 *
859 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
860 * contains nodes which have zones of 0...ZONE_MOVABLE,
861 * set zone_last to ZONE_MOVABLE.
862 */
863 zone_last = ZONE_HIGHMEM;
864 if (N_MEMORY == N_HIGH_MEMORY)
865 zone_last = ZONE_MOVABLE;
866
867 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
868 arg->status_change_nid_high = nid;
869 else
870 arg->status_change_nid_high = -1;
871#else
872 arg->status_change_nid_high = arg->status_change_nid_normal;
873#endif
874
Lai Jiangshand9713672012-12-11 16:01:03 -0800875 /*
876 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800877 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800878 * is online.
879 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800880 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800881 arg->status_change_nid = nid;
882 else
883 arg->status_change_nid = -1;
884}
885
886static void node_states_set_node(int node, struct memory_notify *arg)
887{
888 if (arg->status_change_nid_normal >= 0)
889 node_set_state(node, N_NORMAL_MEMORY);
890
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800891 if (arg->status_change_nid_high >= 0)
892 node_set_state(node, N_HIGH_MEMORY);
893
894 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800895}
896
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700897
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800898int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700899{
Cody P Schaferaa472282013-07-03 15:02:10 -0700900 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700901 unsigned long onlined_pages = 0;
902 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700903 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700904 int nid;
905 int ret;
906 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700907
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900908 lock_memory_hotplug();
Lai Jiangshand9713672012-12-11 16:01:03 -0800909 /*
910 * This doesn't need a lock to do pfn_to_page().
911 * The section can't be removed here because of the
912 * memory_block->state_mutex.
913 */
914 zone = page_zone(pfn_to_page(pfn));
915
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800916 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
917 !can_online_high_movable(zone)) {
918 unlock_memory_hotplug();
Toshi Kani0a1be152013-07-08 16:00:41 -0700919 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800920 }
921
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800922 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
923 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
924 unlock_memory_hotplug();
Toshi Kani0a1be152013-07-08 16:00:41 -0700925 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800926 }
927 }
928 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
929 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
930 unlock_memory_hotplug();
Toshi Kani0a1be152013-07-08 16:00:41 -0700931 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800932 }
933 }
934
935 /* Previous code may changed the zone of the pfn range */
936 zone = page_zone(pfn_to_page(pfn));
937
Yasunori Goto7b78d332007-10-21 16:41:36 -0700938 arg.start_pfn = pfn;
939 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800940 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700941
942 nid = page_to_nid(pfn_to_page(pfn));
Yasunori Goto7b78d332007-10-21 16:41:36 -0700943
944 ret = memory_notify(MEM_GOING_ONLINE, &arg);
945 ret = notifier_to_errno(ret);
946 if (ret) {
947 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900948 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700949 return ret;
950 }
Dave Hansen3947be12005-10-29 18:16:54 -0700951 /*
Yasunori Goto68113782006-06-23 02:03:11 -0700952 * If this zone is not populated, then it is not in zonelist.
953 * This means the page allocator ignores this zone.
954 * So, zonelist must be updated after online.
955 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700956 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800957 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -0700958 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800959 build_all_zonelists(NULL, zone);
960 }
Yasunori Goto68113782006-06-23 02:03:11 -0700961
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700962 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700963 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700964 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800965 if (need_zonelists_rebuild)
966 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700967 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -0700968 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
969 (unsigned long long) pfn << PAGE_SHIFT,
970 (((unsigned long long) pfn + nr_pages)
971 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700972 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900973 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700974 return ret;
975 }
976
Dave Hansen3947be12005-10-29 18:16:54 -0700977 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700978
979 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -0800980 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700981 pgdat_resize_unlock(zone->zone_pgdat, &flags);
982
Jiang Liu08dff7b2012-07-31 16:43:30 -0700983 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -0800984 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700985 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800986 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700987 else
988 zone_pcp_update(zone);
989 }
Dave Hansen3947be12005-10-29 18:16:54 -0700990
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700991 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700992
993 init_per_zone_wmark_min();
994
Jiang Liu08dff7b2012-07-31 16:43:30 -0700995 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -0700996 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -0700997
Haicheng Li1f522502010-05-24 14:32:51 -0700998 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700999
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001000 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001001
1002 if (onlined_pages)
1003 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +09001004 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001005
Dave Hansen3947be12005-10-29 18:16:54 -07001006 return 0;
1007}
Keith Mannthey53947022006-09-30 23:27:08 -07001008#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001009
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001010/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1011static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001012{
1013 struct pglist_data *pgdat;
1014 unsigned long zones_size[MAX_NR_ZONES] = {0};
1015 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1016 unsigned long start_pfn = start >> PAGE_SHIFT;
1017
Tang Chena1e565a2013-02-22 16:33:18 -08001018 pgdat = NODE_DATA(nid);
1019 if (!pgdat) {
1020 pgdat = arch_alloc_nodedata(nid);
1021 if (!pgdat)
1022 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001023
Tang Chena1e565a2013-02-22 16:33:18 -08001024 arch_refresh_nodedata(nid, pgdat);
1025 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001026
1027 /* we can use NODE_DATA(nid) from here */
1028
1029 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001030 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001031
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001032 /*
1033 * The node we allocated has no zone fallback lists. For avoiding
1034 * to access not-initialized zonelist, build here.
1035 */
David Rientjesf957db42011-06-22 18:13:04 -07001036 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001037 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001038 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001039
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001040 return pgdat;
1041}
1042
1043static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1044{
1045 arch_refresh_nodedata(nid, NULL);
1046 arch_free_nodedata(pgdat);
1047 return;
1048}
1049
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001050
minskey guocf234222010-05-24 14:32:41 -07001051/*
1052 * called by cpu_up() to online a node without onlined memory.
1053 */
1054int mem_online_node(int nid)
1055{
1056 pg_data_t *pgdat;
1057 int ret;
1058
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001059 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -07001060 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001061 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -07001062 ret = -ENOMEM;
1063 goto out;
1064 }
1065 node_set_online(nid);
1066 ret = register_one_node(nid);
1067 BUG_ON(ret);
1068
1069out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001070 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -07001071 return ret;
1072}
1073
Toshi Kani27356f52013-09-11 14:21:49 -07001074static int check_hotplug_memory_range(u64 start, u64 size)
1075{
1076 u64 start_pfn = start >> PAGE_SHIFT;
1077 u64 nr_pages = size >> PAGE_SHIFT;
1078
1079 /* Memory range must be aligned with section */
1080 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1081 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1082 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1083 (unsigned long long)start,
1084 (unsigned long long)size);
1085 return -EINVAL;
1086 }
1087
1088 return 0;
1089}
1090
Al Viro31168482008-11-22 17:33:24 +00001091/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1092int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001093{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001094 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001095 bool new_pgdat;
1096 bool new_node;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001097 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001098 int ret;
1099
Toshi Kani27356f52013-09-11 14:21:49 -07001100 ret = check_hotplug_memory_range(start, size);
1101 if (ret)
1102 return ret;
1103
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001104 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -08001105
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001106 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001107 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001108 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -08001109 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -07001110
Tang Chena1e565a2013-02-22 16:33:18 -08001111 { /* Stupid hack to suppress address-never-null warning */
1112 void *p = NODE_DATA(nid);
1113 new_pgdat = !p;
1114 }
1115 new_node = !node_online(nid);
1116 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001117 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001118 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001119 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001120 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001121 }
1122
Yasunori Gotobc02af92006-06-27 02:53:30 -07001123 /* call arch's memory hotadd */
1124 ret = arch_add_memory(nid, start, size);
1125
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001126 if (ret < 0)
1127 goto error;
1128
Yasunori Goto0fc44152006-06-27 02:53:38 -07001129 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001130 node_set_online(nid);
1131
Tang Chena1e565a2013-02-22 16:33:18 -08001132 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001133 ret = register_one_node(nid);
1134 /*
1135 * If sysfs file of new node can't create, cpu on the node
1136 * can't be hot-added. There is no rollback way now.
1137 * So, check by BUG_ON() to catch it reluctantly..
1138 */
1139 BUG_ON(ret);
1140 }
1141
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001142 /* create new memmap entry */
1143 firmware_map_add_hotplug(start, start + size, "System RAM");
1144
Andi Kleen6ad696d2009-11-17 14:06:22 -08001145 goto out;
1146
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001147error:
1148 /* rollback pgdat allocation and others */
1149 if (new_pgdat)
1150 rollback_node_hotadd(nid, pgdat);
Sasha Levina864b9d2013-02-22 16:32:48 -08001151 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001152
Andi Kleen6ad696d2009-11-17 14:06:22 -08001153out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001154 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001155 return ret;
1156}
1157EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001158
1159#ifdef CONFIG_MEMORY_HOTREMOVE
1160/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001161 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1162 * set and the size of the free page is given by page_order(). Using this,
1163 * the function determines if the pageblock contains only free pages.
1164 * Due to buddy contraints, a free page at least the size of a pageblock will
1165 * be located at the start of the pageblock
1166 */
1167static inline int pageblock_free(struct page *page)
1168{
1169 return PageBuddy(page) && page_order(page) >= pageblock_order;
1170}
1171
1172/* Return the start of the next active pageblock after a given page */
1173static struct page *next_active_pageblock(struct page *page)
1174{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001175 /* Ensure the starting page is pageblock-aligned */
1176 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1177
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001178 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001179 if (pageblock_free(page)) {
1180 int order;
1181 /* be careful. we don't have locks, page_order can be changed.*/
1182 order = page_order(page);
1183 if ((order < MAX_ORDER) && (order >= pageblock_order))
1184 return page + (1 << order);
1185 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001186
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001187 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001188}
1189
1190/* Checks if this range of memory is likely to be hot-removable. */
1191int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1192{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001193 struct page *page = pfn_to_page(start_pfn);
1194 struct page *end_page = page + nr_pages;
1195
1196 /* Check the starting page of each pageblock within the range */
1197 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001198 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001199 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001200 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001201 }
1202
1203 /* All pageblocks in the memory block are likely to be hot-removable */
1204 return 1;
1205}
1206
1207/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001208 * Confirm all pages in a range [start, end) is belongs to the same zone.
1209 */
1210static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1211{
1212 unsigned long pfn;
1213 struct zone *zone = NULL;
1214 struct page *page;
1215 int i;
1216 for (pfn = start_pfn;
1217 pfn < end_pfn;
1218 pfn += MAX_ORDER_NR_PAGES) {
1219 i = 0;
1220 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1221 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1222 i++;
1223 if (i == MAX_ORDER_NR_PAGES)
1224 continue;
1225 page = pfn_to_page(pfn + i);
1226 if (zone && page_zone(page) != zone)
1227 return 0;
1228 zone = page_zone(page);
1229 }
1230 return 1;
1231}
1232
1233/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001234 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1235 * and hugepages). We scan pfn because it's much easier than scanning over
1236 * linked list. This function returns the pfn of the first found movable
1237 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001238 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001239static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001240{
1241 unsigned long pfn;
1242 struct page *page;
1243 for (pfn = start; pfn < end; pfn++) {
1244 if (pfn_valid(pfn)) {
1245 page = pfn_to_page(pfn);
1246 if (PageLRU(page))
1247 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001248 if (PageHuge(page)) {
1249 if (is_hugepage_active(page))
1250 return pfn;
1251 else
1252 pfn = round_up(pfn + 1,
1253 1 << compound_order(page)) - 1;
1254 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001255 }
1256 }
1257 return 0;
1258}
1259
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001260#define NR_OFFLINE_AT_ONCE_PAGES (256)
1261static int
1262do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1263{
1264 unsigned long pfn;
1265 struct page *page;
1266 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1267 int not_managed = 0;
1268 int ret = 0;
1269 LIST_HEAD(source);
1270
1271 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1272 if (!pfn_valid(pfn))
1273 continue;
1274 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001275
1276 if (PageHuge(page)) {
1277 struct page *head = compound_head(page);
1278 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1279 if (compound_order(head) > PFN_SECTION_SHIFT) {
1280 ret = -EBUSY;
1281 break;
1282 }
1283 if (isolate_huge_page(page, &source))
1284 move_pages -= 1 << compound_order(head);
1285 continue;
1286 }
1287
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001288 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001289 continue;
1290 /*
1291 * We can skip free pages. And we can only deal with pages on
1292 * LRU.
1293 */
Nick Piggin62695a82008-10-18 20:26:09 -07001294 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001295 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001296 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001297 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001298 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001299 inc_zone_page_state(page, NR_ISOLATED_ANON +
1300 page_is_file_cache(page));
1301
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001302 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001303#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001304 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1305 pfn);
1306 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001307#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001308 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001309 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001310 check this again here. */
1311 if (page_count(page)) {
1312 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001313 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001314 break;
1315 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001316 }
1317 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001318 if (!list_empty(&source)) {
1319 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001320 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001321 goto out;
1322 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001323
1324 /*
1325 * alloc_migrate_target should be improooooved!!
1326 * migrate_pages returns # of failed pages.
1327 */
1328 ret = migrate_pages(&source, alloc_migrate_target, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001329 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001330 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001331 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001332 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001333out:
1334 return ret;
1335}
1336
1337/*
1338 * remove from free_area[] and mark all as Reserved.
1339 */
1340static int
1341offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1342 void *data)
1343{
1344 __offline_isolated_pages(start, start + nr_pages);
1345 return 0;
1346}
1347
1348static void
1349offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1350{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001351 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001352 offline_isolated_pages_cb);
1353}
1354
1355/*
1356 * Check all pages in range, recoreded as memory resource, are isolated.
1357 */
1358static int
1359check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1360 void *data)
1361{
1362 int ret;
1363 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001364 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001365 offlined = nr_pages;
1366 if (!ret)
1367 *(long *)data += offlined;
1368 return ret;
1369}
1370
1371static long
1372check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1373{
1374 long offlined = 0;
1375 int ret;
1376
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001377 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001378 check_pages_isolated_cb);
1379 if (ret < 0)
1380 offlined = (long)ret;
1381 return offlined;
1382}
1383
Lai Jiangshan09285af2012-12-12 13:52:04 -08001384#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001385/*
1386 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1387 * normal memory.
1388 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001389static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1390{
1391 return true;
1392}
Tang Chen79a4dce2012-12-18 14:23:24 -08001393#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001394/* ensure the node has NORMAL memory if it is still online */
1395static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1396{
1397 struct pglist_data *pgdat = zone->zone_pgdat;
1398 unsigned long present_pages = 0;
1399 enum zone_type zt;
1400
1401 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1402 present_pages += pgdat->node_zones[zt].present_pages;
1403
1404 if (present_pages > nr_pages)
1405 return true;
1406
1407 present_pages = 0;
1408 for (; zt <= ZONE_MOVABLE; zt++)
1409 present_pages += pgdat->node_zones[zt].present_pages;
1410
1411 /*
1412 * we can't offline the last normal memory until all
1413 * higher memory is offlined.
1414 */
1415 return present_pages == 0;
1416}
Tang Chen79a4dce2012-12-18 14:23:24 -08001417#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001418
Lai Jiangshand9713672012-12-11 16:01:03 -08001419/* check which state of node_states will be changed when offline memory */
1420static void node_states_check_changes_offline(unsigned long nr_pages,
1421 struct zone *zone, struct memory_notify *arg)
1422{
1423 struct pglist_data *pgdat = zone->zone_pgdat;
1424 unsigned long present_pages = 0;
1425 enum zone_type zt, zone_last = ZONE_NORMAL;
1426
1427 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001428 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1429 * contains nodes which have zones of 0...ZONE_NORMAL,
1430 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001431 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001432 * If we don't have HIGHMEM nor movable node,
1433 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1434 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001435 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001436 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001437 zone_last = ZONE_MOVABLE;
1438
1439 /*
1440 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1441 * If the memory to be offline is in a zone of 0...zone_last,
1442 * and it is the last present memory, 0...zone_last will
1443 * become empty after offline , thus we can determind we will
1444 * need to clear the node from node_states[N_NORMAL_MEMORY].
1445 */
1446 for (zt = 0; zt <= zone_last; zt++)
1447 present_pages += pgdat->node_zones[zt].present_pages;
1448 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1449 arg->status_change_nid_normal = zone_to_nid(zone);
1450 else
1451 arg->status_change_nid_normal = -1;
1452
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001453#ifdef CONFIG_HIGHMEM
1454 /*
1455 * If we have movable node, node_states[N_HIGH_MEMORY]
1456 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1457 * set zone_last to ZONE_HIGHMEM.
1458 *
1459 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1460 * contains nodes which have zones of 0...ZONE_MOVABLE,
1461 * set zone_last to ZONE_MOVABLE.
1462 */
1463 zone_last = ZONE_HIGHMEM;
1464 if (N_MEMORY == N_HIGH_MEMORY)
1465 zone_last = ZONE_MOVABLE;
1466
1467 for (; zt <= zone_last; zt++)
1468 present_pages += pgdat->node_zones[zt].present_pages;
1469 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1470 arg->status_change_nid_high = zone_to_nid(zone);
1471 else
1472 arg->status_change_nid_high = -1;
1473#else
1474 arg->status_change_nid_high = arg->status_change_nid_normal;
1475#endif
1476
Lai Jiangshand9713672012-12-11 16:01:03 -08001477 /*
1478 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1479 */
1480 zone_last = ZONE_MOVABLE;
1481
1482 /*
1483 * check whether node_states[N_HIGH_MEMORY] will be changed
1484 * If we try to offline the last present @nr_pages from the node,
1485 * we can determind we will need to clear the node from
1486 * node_states[N_HIGH_MEMORY].
1487 */
1488 for (; zt <= zone_last; zt++)
1489 present_pages += pgdat->node_zones[zt].present_pages;
1490 if (nr_pages >= present_pages)
1491 arg->status_change_nid = zone_to_nid(zone);
1492 else
1493 arg->status_change_nid = -1;
1494}
1495
1496static void node_states_clear_node(int node, struct memory_notify *arg)
1497{
1498 if (arg->status_change_nid_normal >= 0)
1499 node_clear_state(node, N_NORMAL_MEMORY);
1500
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001501 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1502 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001503 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001504
1505 if ((N_MEMORY != N_HIGH_MEMORY) &&
1506 (arg->status_change_nid >= 0))
1507 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001508}
1509
Wen Congyanga16cee12012-10-08 16:33:58 -07001510static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001511 unsigned long end_pfn, unsigned long timeout)
1512{
1513 unsigned long pfn, nr_pages, expire;
1514 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001515 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001516 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001517 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001518 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001519
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001520 /* at least, alignment against pageblock is necessary */
1521 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1522 return -EINVAL;
1523 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1524 return -EINVAL;
1525 /* This makes hotplug much easier...and readable.
1526 we assume this for now. .*/
1527 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1528 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001529
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001530 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -08001531
Yasunori Goto7b78d332007-10-21 16:41:36 -07001532 zone = page_zone(pfn_to_page(start_pfn));
1533 node = zone_to_nid(zone);
1534 nr_pages = end_pfn - start_pfn;
1535
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001536 ret = -EINVAL;
1537 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1538 goto out;
1539
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001540 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001541 ret = start_isolate_page_range(start_pfn, end_pfn,
1542 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001543 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -08001544 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001545
1546 arg.start_pfn = start_pfn;
1547 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001548 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001549
1550 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1551 ret = notifier_to_errno(ret);
1552 if (ret)
1553 goto failed_removal;
1554
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001555 pfn = start_pfn;
1556 expire = jiffies + timeout;
1557 drain = 0;
1558 retry_max = 5;
1559repeat:
1560 /* start memory hot removal */
1561 ret = -EAGAIN;
1562 if (time_after(jiffies, expire))
1563 goto failed_removal;
1564 ret = -EINTR;
1565 if (signal_pending(current))
1566 goto failed_removal;
1567 ret = 0;
1568 if (drain) {
1569 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001570 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001571 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001572 }
1573
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001574 pfn = scan_movable_pages(start_pfn, end_pfn);
1575 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001576 ret = do_migrate_range(pfn, end_pfn);
1577 if (!ret) {
1578 drain = 1;
1579 goto repeat;
1580 } else {
1581 if (ret < 0)
1582 if (--retry_max == 0)
1583 goto failed_removal;
1584 yield();
1585 drain = 1;
1586 goto repeat;
1587 }
1588 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001589 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001590 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001591 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001592 /* drain pcp pages, this is synchronous. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001593 drain_all_pages();
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001594 /*
1595 * dissolve free hugepages in the memory block before doing offlining
1596 * actually in order to make hugetlbfs's object counting consistent.
1597 */
1598 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001599 /* check again */
1600 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1601 if (offlined_pages < 0) {
1602 ret = -EBUSY;
1603 goto failed_removal;
1604 }
1605 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001606 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001607 We cannot do rollback at this point. */
1608 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001609 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d82012-04-03 15:06:15 +02001610 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001611 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001612 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001613 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001614
1615 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001616 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001617 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001618
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001619 init_per_zone_wmark_min();
1620
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001621 if (!populated_zone(zone)) {
Jiang Liu340175b7d12012-07-31 16:43:32 -07001622 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001623 mutex_lock(&zonelists_mutex);
1624 build_all_zonelists(NULL, NULL);
1625 mutex_unlock(&zonelists_mutex);
1626 } else
1627 zone_pcp_update(zone);
Jiang Liu340175b7d12012-07-31 16:43:32 -07001628
Lai Jiangshand9713672012-12-11 16:01:03 -08001629 node_states_clear_node(node, &arg);
1630 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001631 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001632
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001633 vm_total_pages = nr_free_pagecache_pages();
1634 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001635
1636 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001637 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001638 return 0;
1639
1640failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001641 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1642 (unsigned long long) start_pfn << PAGE_SHIFT,
1643 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001644 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001645 /* pushback to free area */
Michal Nazarewicz0815f3d82012-04-03 15:06:15 +02001646 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001647
Andi Kleen6ad696d2009-11-17 14:06:22 -08001648out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001649 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001650 return ret;
1651}
Badari Pulavarty71088782008-10-18 20:25:58 -07001652
Wen Congyanga16cee12012-10-08 16:33:58 -07001653int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1654{
1655 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1656}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001657#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001658
Wen Congyangbbc76be2013-02-22 16:32:54 -08001659/**
1660 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1661 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001662 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001663 * @arg: argument passed to func
1664 * @func: callback for each memory section walked
1665 *
1666 * This function walks through all present mem sections in range
1667 * [start_pfn, end_pfn) and call func on each mem section.
1668 *
1669 * Returns the return value of func.
1670 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001671int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001672 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001673{
Wen Congyange90bdb72012-10-08 16:34:01 -07001674 struct memory_block *mem = NULL;
1675 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001676 unsigned long pfn, section_nr;
1677 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001678
Wen Congyange90bdb72012-10-08 16:34:01 -07001679 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1680 section_nr = pfn_to_section_nr(pfn);
1681 if (!present_section_nr(section_nr))
1682 continue;
1683
1684 section = __nr_to_section(section_nr);
1685 /* same memblock? */
1686 if (mem)
1687 if ((section_nr >= mem->start_section_nr) &&
1688 (section_nr <= mem->end_section_nr))
1689 continue;
1690
1691 mem = find_memory_block_hinted(section, mem);
1692 if (!mem)
1693 continue;
1694
Wen Congyangbbc76be2013-02-22 16:32:54 -08001695 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001696 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001697 kobject_put(&mem->dev.kobj);
1698 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001699 }
1700 }
1701
1702 if (mem)
1703 kobject_put(&mem->dev.kobj);
1704
Wen Congyangbbc76be2013-02-22 16:32:54 -08001705 return 0;
1706}
1707
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001708#ifdef CONFIG_MEMORY_HOTREMOVE
Wen Congyangbbc76be2013-02-22 16:32:54 -08001709static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1710{
1711 int ret = !is_memblock_offlined(mem);
1712
Randy Dunlap349daa02013-04-29 15:08:49 -07001713 if (unlikely(ret)) {
1714 phys_addr_t beginpa, endpa;
1715
1716 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1717 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001718 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001719 "[%pa-%pa] is onlined\n",
1720 &beginpa, &endpa);
1721 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001722
1723 return ret;
1724}
1725
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001726static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001727{
Tang Chen60a5a192013-02-22 16:33:14 -08001728 int cpu;
1729
1730 for_each_present_cpu(cpu) {
1731 if (cpu_to_node(cpu) == pgdat->node_id)
1732 /*
1733 * the cpu on this node isn't removed, and we can't
1734 * offline this node.
1735 */
1736 return -EBUSY;
1737 }
1738
1739 return 0;
1740}
1741
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001742static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001743{
1744#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001745 int cpu;
1746
1747 for_each_possible_cpu(cpu)
1748 if (cpu_to_node(cpu) == pgdat->node_id)
1749 numa_clear_node(cpu);
1750#endif
1751}
1752
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001753static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001754{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001755 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001756
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001757 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001758 if (ret)
1759 return ret;
1760
1761 /*
1762 * the node will be offlined when we come here, so we can clear
1763 * the cpu_to_node() now.
1764 */
1765
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001766 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001767 return 0;
1768}
1769
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001770/**
1771 * try_offline_node
1772 *
1773 * Offline a node if all memory sections and cpus of the node are removed.
1774 *
1775 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1776 * and online/offline operations before this call.
1777 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001778void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001779{
Wen Congyangd822b862013-02-22 16:33:16 -08001780 pg_data_t *pgdat = NODE_DATA(nid);
1781 unsigned long start_pfn = pgdat->node_start_pfn;
1782 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001783 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08001784 struct page *pgdat_page = virt_to_page(pgdat);
1785 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08001786
1787 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1788 unsigned long section_nr = pfn_to_section_nr(pfn);
1789
1790 if (!present_section_nr(section_nr))
1791 continue;
1792
1793 if (pfn_to_nid(pfn) != nid)
1794 continue;
1795
1796 /*
1797 * some memory sections of this node are not removed, and we
1798 * can't offline node now.
1799 */
1800 return;
1801 }
1802
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001803 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001804 return;
1805
1806 /*
1807 * all memory/cpu of this node are removed, we can offline this
1808 * node now.
1809 */
1810 node_set_offline(nid);
1811 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08001812
1813 if (!PageSlab(pgdat_page) && !PageCompound(pgdat_page))
1814 /* node data is allocated from boot memory */
1815 return;
1816
1817 /* free waittable in each zone */
1818 for (i = 0; i < MAX_NR_ZONES; i++) {
1819 struct zone *zone = pgdat->node_zones + i;
1820
Jianguo Wuca4b3f32013-03-22 15:04:50 -07001821 /*
1822 * wait_table may be allocated from boot memory,
1823 * here only free if it's allocated by vmalloc.
1824 */
1825 if (is_vmalloc_addr(zone->wait_table))
Wen Congyangd822b862013-02-22 16:33:16 -08001826 vfree(zone->wait_table);
1827 }
1828
1829 /*
1830 * Since there is no way to guarentee the address of pgdat/zone is not
1831 * on stack of any kernel threads or used by other kernel objects
1832 * without reference counting or other symchronizing method, do not
1833 * reset node_data and free pgdat here. Just reset it to 0 and reuse
1834 * the memory when the node is online again.
1835 */
1836 memset(pgdat, 0, sizeof(*pgdat));
Tang Chen60a5a192013-02-22 16:33:14 -08001837}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001838EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001839
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001840/**
1841 * remove_memory
1842 *
1843 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1844 * and online/offline operations before this call, as required by
1845 * try_offline_node().
1846 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001847void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001848{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001849 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001850
Toshi Kani27356f52013-09-11 14:21:49 -07001851 BUG_ON(check_hotplug_memory_range(start, size));
1852
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001853 lock_memory_hotplug();
1854
1855 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001856 * All memory blocks must be offlined before removing memory. Check
1857 * whether all memory blocks in question are offline and trigger a BUG()
1858 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001859 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001860 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001861 is_memblock_offlined_cb);
1862 if (ret) {
1863 unlock_memory_hotplug();
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001864 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001865 }
1866
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001867 /* remove memmap entry */
1868 firmware_map_remove(start, start + size, "System RAM");
1869
Wen Congyang24d335c2013-02-22 16:32:58 -08001870 arch_remove_memory(start, size);
1871
Tang Chen60a5a192013-02-22 16:33:14 -08001872 try_offline_node(nid);
1873
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001874 unlock_memory_hotplug();
Badari Pulavarty71088782008-10-18 20:25:58 -07001875}
Badari Pulavarty71088782008-10-18 20:25:58 -07001876EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02001877#endif /* CONFIG_MEMORY_HOTREMOVE */