blob: 6c90d222ec0ac4529598f7aae3b2d2e26eaad289 [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>
Dave Hansen3947be12005-10-29 18:16:54 -070032
33#include <asm/tlbflush.h>
34
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030035#include "internal.h"
36
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070037/*
38 * online_page_callback contains pointer to current page onlining function.
39 * Initially it is generic_online_page(). If it is required it could be
40 * changed by calling set_online_page_callback() for callback registration
41 * and restore_online_page_callback() for generic callback restore.
42 */
43
44static void generic_online_page(struct page *page);
45
46static online_page_callback_t online_page_callback = generic_online_page;
47
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080048DEFINE_MUTEX(mem_hotplug_mutex);
49
50void lock_memory_hotplug(void)
51{
52 mutex_lock(&mem_hotplug_mutex);
53
54 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
55 lock_system_sleep();
56}
57
58void unlock_memory_hotplug(void)
59{
60 unlock_system_sleep();
61 mutex_unlock(&mem_hotplug_mutex);
62}
63
64
Keith Mannthey45e0b782006-09-30 23:27:09 -070065/* add this memory to iomem resource */
66static struct resource *register_memory_resource(u64 start, u64 size)
67{
68 struct resource *res;
69 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
70 BUG_ON(!res);
71
72 res->name = "System RAM";
73 res->start = start;
74 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -080075 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -070076 if (request_resource(&iomem_resource, res) < 0) {
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -070077 printk("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -070078 kfree(res);
79 res = NULL;
80 }
81 return res;
82}
83
84static void release_memory_resource(struct resource *res)
85{
86 if (!res)
87 return;
88 release_resource(res);
89 kfree(res);
90 return;
91}
92
Keith Mannthey53947022006-09-30 23:27:08 -070093#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -080094void get_page_bootmem(unsigned long info, struct page *page,
95 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -070096{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080097 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -070098 SetPagePrivate(page);
99 set_page_private(page, info);
100 atomic_inc(&page->_count);
101}
102
Rakib Mullick23ce9322009-12-14 17:59:44 -0800103/* reference to __meminit __free_pages_bootmem is valid
104 * so use __ref to tell modpost not to generate a warning */
105void __ref 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;
Jiang Liu9feedc92012-12-12 13:52:12 -0800108 static DEFINE_MUTEX(ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700109
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800110 type = (unsigned long) page->lru.next;
111 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700113
114 if (atomic_dec_return(&page->_count) == 1) {
115 ClearPagePrivate(page);
116 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800117 INIT_LIST_HEAD(&page->lru);
Jiang Liu9feedc92012-12-12 13:52:12 -0800118
119 /*
120 * Please refer to comment for __free_pages_bootmem()
121 * for why we serialize here.
122 */
123 mutex_lock(&ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700124 __free_pages_bootmem(page, 0);
Jiang Liu9feedc92012-12-12 13:52:12 -0800125 mutex_unlock(&ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700126 }
127
128}
129
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800130#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
131#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700132static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700133{
134 unsigned long *usemap, mapsize, section_nr, i;
135 struct mem_section *ms;
136 struct page *page, *memmap;
137
Yasunori Goto04753272008-04-28 02:13:31 -0700138 section_nr = pfn_to_section_nr(start_pfn);
139 ms = __nr_to_section(section_nr);
140
141 /* Get section's memmap address */
142 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
143
144 /*
145 * Get page for the memmap's phys address
146 * XXX: need more consideration for sparse_vmemmap...
147 */
148 page = virt_to_page(memmap);
149 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
150 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
151
152 /* remember memmap's page */
153 for (i = 0; i < mapsize; i++, page++)
154 get_page_bootmem(section_nr, page, SECTION_INFO);
155
156 usemap = __nr_to_section(section_nr)->pageblock_flags;
157 page = virt_to_page(usemap);
158
159 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
160
161 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700162 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700163
164}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800165#else /* CONFIG_SPARSEMEM_VMEMMAP */
166static void register_page_bootmem_info_section(unsigned long start_pfn)
167{
168 unsigned long *usemap, mapsize, section_nr, i;
169 struct mem_section *ms;
170 struct page *page, *memmap;
171
172 if (!pfn_valid(start_pfn))
173 return;
174
175 section_nr = pfn_to_section_nr(start_pfn);
176 ms = __nr_to_section(section_nr);
177
178 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
179
180 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
181
182 usemap = __nr_to_section(section_nr)->pageblock_flags;
183 page = virt_to_page(usemap);
184
185 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
186
187 for (i = 0; i < mapsize; i++, page++)
188 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
189}
190#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700191
192void register_page_bootmem_info_node(struct pglist_data *pgdat)
193{
194 unsigned long i, pfn, end_pfn, nr_pages;
195 int node = pgdat->node_id;
196 struct page *page;
197 struct zone *zone;
198
199 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
200 page = virt_to_page(pgdat);
201
202 for (i = 0; i < nr_pages; i++, page++)
203 get_page_bootmem(node, page, NODE_INFO);
204
205 zone = &pgdat->node_zones[0];
206 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
207 if (zone->wait_table) {
208 nr_pages = zone->wait_table_hash_nr_entries
209 * sizeof(wait_queue_head_t);
210 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
211 page = virt_to_page(zone->wait_table);
212
213 for (i = 0; i < nr_pages; i++, page++)
214 get_page_bootmem(node, page, NODE_INFO);
215 }
216 }
217
218 pfn = pgdat->node_start_pfn;
219 end_pfn = pfn + pgdat->node_spanned_pages;
220
221 /* register_section info */
qiuxishif14851a2012-09-17 14:09:24 -0700222 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
223 /*
224 * Some platforms can assign the same pfn to multiple nodes - on
225 * node0 as well as nodeN. To avoid registering a pfn against
226 * multiple nodes we check that this pfn does not already
227 * reside in some other node.
228 */
229 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
230 register_page_bootmem_info_section(pfn);
231 }
Yasunori Goto04753272008-04-28 02:13:31 -0700232}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800233#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700234
Heiko Carstens76cdd582008-05-14 16:05:52 -0700235static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
236 unsigned long end_pfn)
237{
238 unsigned long old_zone_end_pfn;
239
240 zone_span_writelock(zone);
241
242 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Tang Chen712cd382012-12-11 16:01:07 -0800243 if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700244 zone->zone_start_pfn = start_pfn;
245
246 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
247 zone->zone_start_pfn;
248
249 zone_span_writeunlock(zone);
250}
251
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800252static void resize_zone(struct zone *zone, unsigned long start_pfn,
253 unsigned long end_pfn)
254{
255 zone_span_writelock(zone);
256
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800257 if (end_pfn - start_pfn) {
258 zone->zone_start_pfn = start_pfn;
259 zone->spanned_pages = end_pfn - start_pfn;
260 } else {
261 /*
262 * make it consist as free_area_init_core(),
263 * if spanned_pages = 0, then keep start_pfn = 0
264 */
265 zone->zone_start_pfn = 0;
266 zone->spanned_pages = 0;
267 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800268
269 zone_span_writeunlock(zone);
270}
271
272static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
273 unsigned long end_pfn)
274{
275 enum zone_type zid = zone_idx(zone);
276 int nid = zone->zone_pgdat->node_id;
277 unsigned long pfn;
278
279 for (pfn = start_pfn; pfn < end_pfn; pfn++)
280 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
281}
282
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800283static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800284 unsigned long start_pfn, unsigned long end_pfn)
285{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800286 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800287 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800288 unsigned long z1_start_pfn;
289
290 if (!z1->wait_table) {
291 ret = init_currently_empty_zone(z1, start_pfn,
292 end_pfn - start_pfn, MEMMAP_HOTPLUG);
293 if (ret)
294 return ret;
295 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800296
297 pgdat_resize_lock(z1->zone_pgdat, &flags);
298
299 /* can't move pfns which are higher than @z2 */
300 if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
301 goto out_fail;
302 /* the move out part mast at the left most of @z2 */
303 if (start_pfn > z2->zone_start_pfn)
304 goto out_fail;
305 /* must included/overlap */
306 if (end_pfn <= z2->zone_start_pfn)
307 goto out_fail;
308
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800309 /* use start_pfn for z1's start_pfn if z1 is empty */
310 if (z1->spanned_pages)
311 z1_start_pfn = z1->zone_start_pfn;
312 else
313 z1_start_pfn = start_pfn;
314
315 resize_zone(z1, z1_start_pfn, end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800316 resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
317
318 pgdat_resize_unlock(z1->zone_pgdat, &flags);
319
320 fix_zone_id(z1, start_pfn, end_pfn);
321
322 return 0;
323out_fail:
324 pgdat_resize_unlock(z1->zone_pgdat, &flags);
325 return -1;
326}
327
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800328static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800329 unsigned long start_pfn, unsigned long end_pfn)
330{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800331 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800332 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800333 unsigned long z2_end_pfn;
334
335 if (!z2->wait_table) {
336 ret = init_currently_empty_zone(z2, start_pfn,
337 end_pfn - start_pfn, MEMMAP_HOTPLUG);
338 if (ret)
339 return ret;
340 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800341
342 pgdat_resize_lock(z1->zone_pgdat, &flags);
343
344 /* can't move pfns which are lower than @z1 */
345 if (z1->zone_start_pfn > start_pfn)
346 goto out_fail;
347 /* the move out part mast at the right most of @z1 */
348 if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
349 goto out_fail;
350 /* must included/overlap */
351 if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
352 goto out_fail;
353
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800354 /* use end_pfn for z2's end_pfn if z2 is empty */
355 if (z2->spanned_pages)
356 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
357 else
358 z2_end_pfn = end_pfn;
359
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800360 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800361 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362
363 pgdat_resize_unlock(z1->zone_pgdat, &flags);
364
365 fix_zone_id(z2, start_pfn, end_pfn);
366
367 return 0;
368out_fail:
369 pgdat_resize_unlock(z1->zone_pgdat, &flags);
370 return -1;
371}
372
Heiko Carstens76cdd582008-05-14 16:05:52 -0700373static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
374 unsigned long end_pfn)
375{
376 unsigned long old_pgdat_end_pfn =
377 pgdat->node_start_pfn + pgdat->node_spanned_pages;
378
Tang Chen712cd382012-12-11 16:01:07 -0800379 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700380 pgdat->node_start_pfn = start_pfn;
381
382 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
383 pgdat->node_start_pfn;
384}
385
Al Viro31168482008-11-22 17:33:24 +0000386static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700387{
388 struct pglist_data *pgdat = zone->zone_pgdat;
389 int nr_pages = PAGES_PER_SECTION;
390 int nid = pgdat->node_id;
391 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700392 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700393
394 zone_type = zone - pgdat->node_zones;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700395 if (!zone->wait_table) {
396 int ret;
397
398 ret = init_currently_empty_zone(zone, phys_start_pfn,
399 nr_pages, MEMMAP_HOTPLUG);
400 if (ret)
401 return ret;
402 }
403 pgdat_resize_lock(zone->zone_pgdat, &flags);
404 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
405 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
406 phys_start_pfn + nr_pages);
407 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800408 memmap_init_zone(nr_pages, nid, zone_type,
409 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700410 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700411}
412
Gary Hadec04fc582009-01-06 14:39:14 -0800413static int __meminit __add_section(int nid, struct zone *zone,
414 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700415{
Dave Hansen3947be12005-10-29 18:16:54 -0700416 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700417 int ret;
418
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700419 if (pfn_valid(phys_start_pfn))
420 return -EEXIST;
421
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700422 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700423
424 if (ret < 0)
425 return ret;
426
Yasunori Goto718127c2006-06-23 02:03:10 -0700427 ret = __add_zone(zone, phys_start_pfn);
428
429 if (ret < 0)
430 return ret;
431
Gary Hadec04fc582009-01-06 14:39:14 -0800432 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700433}
434
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700435#ifdef CONFIG_SPARSEMEM_VMEMMAP
436static int __remove_section(struct zone *zone, struct mem_section *ms)
437{
438 /*
439 * XXX: Freeing memmap with vmemmap is not implement yet.
440 * This should be removed later.
441 */
442 return -EBUSY;
443}
444#else
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700445static int __remove_section(struct zone *zone, struct mem_section *ms)
446{
447 unsigned long flags;
448 struct pglist_data *pgdat = zone->zone_pgdat;
449 int ret = -EINVAL;
450
451 if (!valid_section(ms))
452 return ret;
453
454 ret = unregister_memory_section(ms);
455 if (ret)
456 return ret;
457
458 pgdat_resize_lock(pgdat, &flags);
459 sparse_remove_one_section(zone, ms);
460 pgdat_resize_unlock(pgdat, &flags);
461 return 0;
462}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700463#endif
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700464
Dave Hansen3947be12005-10-29 18:16:54 -0700465/*
466 * Reasonably generic function for adding memory. It is
467 * expected that archs that support memory hotplug will
468 * call this function after deciding the zone to which to
469 * add the new pages.
470 */
Gary Hadec04fc582009-01-06 14:39:14 -0800471int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
472 unsigned long nr_pages)
Dave Hansen3947be12005-10-29 18:16:54 -0700473{
474 unsigned long i;
475 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700476 int start_sec, end_sec;
477 /* during initialize mem_map, align hot-added range to section */
478 start_sec = pfn_to_section_nr(phys_start_pfn);
479 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700480
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700481 for (i = start_sec; i <= end_sec; i++) {
Gary Hadec04fc582009-01-06 14:39:14 -0800482 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700483
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700484 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200485 * EEXIST is finally dealt with by ioresource collision
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700486 * check. see add_memory() => register_memory_resource()
487 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700488 */
489 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700490 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700491 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700492 }
493
494 return err;
495}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700496EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700497
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700498/**
499 * __remove_pages() - remove sections of pages from a zone
500 * @zone: zone from which pages need to be removed
501 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
502 * @nr_pages: number of pages to remove (must be multiple of section size)
503 *
504 * Generic helper function to remove section mappings and sysfs entries
505 * for the section of the memory we are removing. Caller needs to make
506 * sure that pages are marked reserved and zones are adjust properly by
507 * calling offline_pages().
508 */
509int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
510 unsigned long nr_pages)
511{
512 unsigned long i, ret = 0;
513 int sections_to_remove;
514
515 /*
516 * We can only remove entire sections
517 */
518 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
519 BUG_ON(nr_pages % PAGES_PER_SECTION);
520
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700521 release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
522
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700523 sections_to_remove = nr_pages / PAGES_PER_SECTION;
524 for (i = 0; i < sections_to_remove; i++) {
525 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
526 ret = __remove_section(zone, __pfn_to_section(pfn));
527 if (ret)
528 break;
529 }
530 return ret;
531}
532EXPORT_SYMBOL_GPL(__remove_pages);
533
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700534int set_online_page_callback(online_page_callback_t callback)
535{
536 int rc = -EINVAL;
537
538 lock_memory_hotplug();
539
540 if (online_page_callback == generic_online_page) {
541 online_page_callback = callback;
542 rc = 0;
543 }
544
545 unlock_memory_hotplug();
546
547 return rc;
548}
549EXPORT_SYMBOL_GPL(set_online_page_callback);
550
551int restore_online_page_callback(online_page_callback_t callback)
552{
553 int rc = -EINVAL;
554
555 lock_memory_hotplug();
556
557 if (online_page_callback == callback) {
558 online_page_callback = generic_online_page;
559 rc = 0;
560 }
561
562 unlock_memory_hotplug();
563
564 return rc;
565}
566EXPORT_SYMBOL_GPL(restore_online_page_callback);
567
568void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700569{
Jan Beulich4738e1b2009-09-21 17:03:03 -0700570 unsigned long pfn = page_to_pfn(page);
571
Jan Beulich4738e1b2009-09-21 17:03:03 -0700572 if (pfn >= num_physpages)
573 num_physpages = pfn + 1;
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700574}
575EXPORT_SYMBOL_GPL(__online_page_set_limits);
576
577void __online_page_increment_counters(struct page *page)
578{
579 totalram_pages++;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700580
581#ifdef CONFIG_HIGHMEM
582 if (PageHighMem(page))
583 totalhigh_pages++;
584#endif
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700585}
586EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700587
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700588void __online_page_free(struct page *page)
589{
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700590 ClearPageReserved(page);
591 init_page_count(page);
592 __free_page(page);
593}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700594EXPORT_SYMBOL_GPL(__online_page_free);
595
596static void generic_online_page(struct page *page)
597{
598 __online_page_set_limits(page);
599 __online_page_increment_counters(page);
600 __online_page_free(page);
601}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700602
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700603static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
604 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700605{
606 unsigned long i;
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700607 unsigned long onlined_pages = *(unsigned long *)arg;
608 struct page *page;
609 if (PageReserved(pfn_to_page(start_pfn)))
610 for (i = 0; i < nr_pages; i++) {
611 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700612 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700613 onlined_pages++;
614 }
615 *(unsigned long *)arg = onlined_pages;
616 return 0;
617}
618
Lai Jiangshan09285af2012-12-12 13:52:04 -0800619#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800620/*
621 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
622 * normal memory.
623 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800624static bool can_online_high_movable(struct zone *zone)
625{
626 return true;
627}
Tang Chen79a4dce2012-12-18 14:23:24 -0800628#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800629/* ensure every online node has NORMAL memory */
630static bool can_online_high_movable(struct zone *zone)
631{
632 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
633}
Tang Chen79a4dce2012-12-18 14:23:24 -0800634#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800635
Lai Jiangshand9713672012-12-11 16:01:03 -0800636/* check which state of node_states will be changed when online memory */
637static void node_states_check_changes_online(unsigned long nr_pages,
638 struct zone *zone, struct memory_notify *arg)
639{
640 int nid = zone_to_nid(zone);
641 enum zone_type zone_last = ZONE_NORMAL;
642
643 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800644 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
645 * contains nodes which have zones of 0...ZONE_NORMAL,
646 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800647 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800648 * If we don't have HIGHMEM nor movable node,
649 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
650 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800651 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800652 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800653 zone_last = ZONE_MOVABLE;
654
655 /*
656 * if the memory to be online is in a zone of 0...zone_last, and
657 * the zones of 0...zone_last don't have memory before online, we will
658 * need to set the node to node_states[N_NORMAL_MEMORY] after
659 * the memory is online.
660 */
661 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
662 arg->status_change_nid_normal = nid;
663 else
664 arg->status_change_nid_normal = -1;
665
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800666#ifdef CONFIG_HIGHMEM
667 /*
668 * If we have movable node, node_states[N_HIGH_MEMORY]
669 * contains nodes which have zones of 0...ZONE_HIGHMEM,
670 * set zone_last to ZONE_HIGHMEM.
671 *
672 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
673 * contains nodes which have zones of 0...ZONE_MOVABLE,
674 * set zone_last to ZONE_MOVABLE.
675 */
676 zone_last = ZONE_HIGHMEM;
677 if (N_MEMORY == N_HIGH_MEMORY)
678 zone_last = ZONE_MOVABLE;
679
680 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
681 arg->status_change_nid_high = nid;
682 else
683 arg->status_change_nid_high = -1;
684#else
685 arg->status_change_nid_high = arg->status_change_nid_normal;
686#endif
687
Lai Jiangshand9713672012-12-11 16:01:03 -0800688 /*
689 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800690 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800691 * is online.
692 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800693 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800694 arg->status_change_nid = nid;
695 else
696 arg->status_change_nid = -1;
697}
698
699static void node_states_set_node(int node, struct memory_notify *arg)
700{
701 if (arg->status_change_nid_normal >= 0)
702 node_set_state(node, N_NORMAL_MEMORY);
703
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800704 if (arg->status_change_nid_high >= 0)
705 node_set_state(node, N_HIGH_MEMORY);
706
707 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800708}
709
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700710
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800711int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700712{
Dave Hansen3947be12005-10-29 18:16:54 -0700713 unsigned long onlined_pages = 0;
714 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700715 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700716 int nid;
717 int ret;
718 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700719
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900720 lock_memory_hotplug();
Lai Jiangshand9713672012-12-11 16:01:03 -0800721 /*
722 * This doesn't need a lock to do pfn_to_page().
723 * The section can't be removed here because of the
724 * memory_block->state_mutex.
725 */
726 zone = page_zone(pfn_to_page(pfn));
727
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800728 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
729 !can_online_high_movable(zone)) {
730 unlock_memory_hotplug();
731 return -1;
732 }
733
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800734 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
735 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
736 unlock_memory_hotplug();
737 return -1;
738 }
739 }
740 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
741 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
742 unlock_memory_hotplug();
743 return -1;
744 }
745 }
746
747 /* Previous code may changed the zone of the pfn range */
748 zone = page_zone(pfn_to_page(pfn));
749
Yasunori Goto7b78d332007-10-21 16:41:36 -0700750 arg.start_pfn = pfn;
751 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800752 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700753
754 nid = page_to_nid(pfn_to_page(pfn));
Yasunori Goto7b78d332007-10-21 16:41:36 -0700755
756 ret = memory_notify(MEM_GOING_ONLINE, &arg);
757 ret = notifier_to_errno(ret);
758 if (ret) {
759 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900760 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700761 return ret;
762 }
Dave Hansen3947be12005-10-29 18:16:54 -0700763 /*
Yasunori Goto68113782006-06-23 02:03:11 -0700764 * If this zone is not populated, then it is not in zonelist.
765 * This means the page allocator ignores this zone.
766 * So, zonelist must be updated after online.
767 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700768 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800769 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -0700770 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800771 build_all_zonelists(NULL, zone);
772 }
Yasunori Goto68113782006-06-23 02:03:11 -0700773
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700774 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700775 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700776 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800777 if (need_zonelists_rebuild)
778 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700779 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -0700780 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
781 (unsigned long long) pfn << PAGE_SHIFT,
782 (((unsigned long long) pfn + nr_pages)
783 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700784 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900785 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700786 return ret;
787 }
788
Jiang Liu9feedc92012-12-12 13:52:12 -0800789 zone->managed_pages += onlined_pages;
Dave Hansen3947be12005-10-29 18:16:54 -0700790 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800791 zone->zone_pgdat->node_present_pages += onlined_pages;
Jiang Liu08dff7b2012-07-31 16:43:30 -0700792 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -0800793 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700794 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800795 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700796 else
797 zone_pcp_update(zone);
798 }
Dave Hansen3947be12005-10-29 18:16:54 -0700799
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700800 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700801
802 init_per_zone_wmark_min();
803
Jiang Liu08dff7b2012-07-31 16:43:30 -0700804 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -0700805 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -0700806
Haicheng Li1f522502010-05-24 14:32:51 -0700807 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700808
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700809 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700810
811 if (onlined_pages)
812 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900813 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700814
Dave Hansen3947be12005-10-29 18:16:54 -0700815 return 0;
816}
Keith Mannthey53947022006-09-30 23:27:08 -0700817#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700818
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800819/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
820static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700821{
822 struct pglist_data *pgdat;
823 unsigned long zones_size[MAX_NR_ZONES] = {0};
824 unsigned long zholes_size[MAX_NR_ZONES] = {0};
825 unsigned long start_pfn = start >> PAGE_SHIFT;
826
827 pgdat = arch_alloc_nodedata(nid);
828 if (!pgdat)
829 return NULL;
830
831 arch_refresh_nodedata(nid, pgdat);
832
833 /* we can use NODE_DATA(nid) from here */
834
835 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700836 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700837
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700838 /*
839 * The node we allocated has no zone fallback lists. For avoiding
840 * to access not-initialized zonelist, build here.
841 */
David Rientjesf957db42011-06-22 18:13:04 -0700842 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -0700843 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700844 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700845
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700846 return pgdat;
847}
848
849static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
850{
851 arch_refresh_nodedata(nid, NULL);
852 arch_free_nodedata(pgdat);
853 return;
854}
855
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700856
minskey guocf234222010-05-24 14:32:41 -0700857/*
858 * called by cpu_up() to online a node without onlined memory.
859 */
860int mem_online_node(int nid)
861{
862 pg_data_t *pgdat;
863 int ret;
864
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800865 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700866 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700867 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700868 ret = -ENOMEM;
869 goto out;
870 }
871 node_set_online(nid);
872 ret = register_one_node(nid);
873 BUG_ON(ret);
874
875out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800876 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700877 return ret;
878}
879
Al Viro31168482008-11-22 17:33:24 +0000880/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
881int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700882{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700883 pg_data_t *pgdat = NULL;
884 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700885 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700886 int ret;
887
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800888 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800889
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700890 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800891 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700892 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800893 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700894
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700895 if (!node_online(nid)) {
896 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800897 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700898 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -0700899 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700900 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700901 }
902
Yasunori Gotobc02af92006-06-27 02:53:30 -0700903 /* call arch's memory hotadd */
904 ret = arch_add_memory(nid, start, size);
905
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700906 if (ret < 0)
907 goto error;
908
Yasunori Goto0fc44152006-06-27 02:53:38 -0700909 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700910 node_set_online(nid);
911
Yasunori Goto0fc44152006-06-27 02:53:38 -0700912 if (new_pgdat) {
913 ret = register_one_node(nid);
914 /*
915 * If sysfs file of new node can't create, cpu on the node
916 * can't be hot-added. There is no rollback way now.
917 * So, check by BUG_ON() to catch it reluctantly..
918 */
919 BUG_ON(ret);
920 }
921
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800922 /* create new memmap entry */
923 firmware_map_add_hotplug(start, start + size, "System RAM");
924
Andi Kleen6ad696d2009-11-17 14:06:22 -0800925 goto out;
926
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700927error:
928 /* rollback pgdat allocation and others */
929 if (new_pgdat)
930 rollback_node_hotadd(nid, pgdat);
Sasha Levina864b9d2013-02-22 16:32:48 -0800931 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700932
Andi Kleen6ad696d2009-11-17 14:06:22 -0800933out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800934 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700935 return ret;
936}
937EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700938
939#ifdef CONFIG_MEMORY_HOTREMOVE
940/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700941 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
942 * set and the size of the free page is given by page_order(). Using this,
943 * the function determines if the pageblock contains only free pages.
944 * Due to buddy contraints, a free page at least the size of a pageblock will
945 * be located at the start of the pageblock
946 */
947static inline int pageblock_free(struct page *page)
948{
949 return PageBuddy(page) && page_order(page) >= pageblock_order;
950}
951
952/* Return the start of the next active pageblock after a given page */
953static struct page *next_active_pageblock(struct page *page)
954{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700955 /* Ensure the starting page is pageblock-aligned */
956 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
957
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700958 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700959 if (pageblock_free(page)) {
960 int order;
961 /* be careful. we don't have locks, page_order can be changed.*/
962 order = page_order(page);
963 if ((order < MAX_ORDER) && (order >= pageblock_order))
964 return page + (1 << order);
965 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700966
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700967 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700968}
969
970/* Checks if this range of memory is likely to be hot-removable. */
971int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
972{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700973 struct page *page = pfn_to_page(start_pfn);
974 struct page *end_page = page + nr_pages;
975
976 /* Check the starting page of each pageblock within the range */
977 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700978 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700979 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700980 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700981 }
982
983 /* All pageblocks in the memory block are likely to be hot-removable */
984 return 1;
985}
986
987/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700988 * Confirm all pages in a range [start, end) is belongs to the same zone.
989 */
990static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
991{
992 unsigned long pfn;
993 struct zone *zone = NULL;
994 struct page *page;
995 int i;
996 for (pfn = start_pfn;
997 pfn < end_pfn;
998 pfn += MAX_ORDER_NR_PAGES) {
999 i = 0;
1000 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1001 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1002 i++;
1003 if (i == MAX_ORDER_NR_PAGES)
1004 continue;
1005 page = pfn_to_page(pfn + i);
1006 if (zone && page_zone(page) != zone)
1007 return 0;
1008 zone = page_zone(page);
1009 }
1010 return 1;
1011}
1012
1013/*
1014 * Scanning pfn is much easier than scanning lru list.
1015 * Scan pfn from start to end and Find LRU page.
1016 */
Andrew Morton7bbc0902010-10-26 14:22:05 -07001017static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001018{
1019 unsigned long pfn;
1020 struct page *page;
1021 for (pfn = start; pfn < end; pfn++) {
1022 if (pfn_valid(pfn)) {
1023 page = pfn_to_page(pfn);
1024 if (PageLRU(page))
1025 return pfn;
1026 }
1027 }
1028 return 0;
1029}
1030
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001031#define NR_OFFLINE_AT_ONCE_PAGES (256)
1032static int
1033do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1034{
1035 unsigned long pfn;
1036 struct page *page;
1037 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1038 int not_managed = 0;
1039 int ret = 0;
1040 LIST_HEAD(source);
1041
1042 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1043 if (!pfn_valid(pfn))
1044 continue;
1045 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001046 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001047 continue;
1048 /*
1049 * We can skip free pages. And we can only deal with pages on
1050 * LRU.
1051 */
Nick Piggin62695a82008-10-18 20:26:09 -07001052 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001053 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001054 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001055 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001056 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001057 inc_zone_page_state(page, NR_ISOLATED_ANON +
1058 page_is_file_cache(page));
1059
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001060 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001061#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001062 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1063 pfn);
1064 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001065#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001066 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001067 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001068 check this again here. */
1069 if (page_count(page)) {
1070 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001071 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001072 break;
1073 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001074 }
1075 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001076 if (!list_empty(&source)) {
1077 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001078 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001079 goto out;
1080 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001081
1082 /*
1083 * alloc_migrate_target should be improooooved!!
1084 * migrate_pages returns # of failed pages.
1085 */
1086 ret = migrate_pages(&source, alloc_migrate_target, 0,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001087 true, MIGRATE_SYNC,
1088 MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001089 if (ret)
1090 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001091 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001092out:
1093 return ret;
1094}
1095
1096/*
1097 * remove from free_area[] and mark all as Reserved.
1098 */
1099static int
1100offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1101 void *data)
1102{
1103 __offline_isolated_pages(start, start + nr_pages);
1104 return 0;
1105}
1106
1107static void
1108offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1109{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001110 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001111 offline_isolated_pages_cb);
1112}
1113
1114/*
1115 * Check all pages in range, recoreded as memory resource, are isolated.
1116 */
1117static int
1118check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1119 void *data)
1120{
1121 int ret;
1122 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001123 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001124 offlined = nr_pages;
1125 if (!ret)
1126 *(long *)data += offlined;
1127 return ret;
1128}
1129
1130static long
1131check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1132{
1133 long offlined = 0;
1134 int ret;
1135
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001136 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001137 check_pages_isolated_cb);
1138 if (ret < 0)
1139 offlined = (long)ret;
1140 return offlined;
1141}
1142
Lai Jiangshan09285af2012-12-12 13:52:04 -08001143#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001144/*
1145 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1146 * normal memory.
1147 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001148static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1149{
1150 return true;
1151}
Tang Chen79a4dce2012-12-18 14:23:24 -08001152#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001153/* ensure the node has NORMAL memory if it is still online */
1154static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1155{
1156 struct pglist_data *pgdat = zone->zone_pgdat;
1157 unsigned long present_pages = 0;
1158 enum zone_type zt;
1159
1160 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1161 present_pages += pgdat->node_zones[zt].present_pages;
1162
1163 if (present_pages > nr_pages)
1164 return true;
1165
1166 present_pages = 0;
1167 for (; zt <= ZONE_MOVABLE; zt++)
1168 present_pages += pgdat->node_zones[zt].present_pages;
1169
1170 /*
1171 * we can't offline the last normal memory until all
1172 * higher memory is offlined.
1173 */
1174 return present_pages == 0;
1175}
Tang Chen79a4dce2012-12-18 14:23:24 -08001176#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001177
Lai Jiangshand9713672012-12-11 16:01:03 -08001178/* check which state of node_states will be changed when offline memory */
1179static void node_states_check_changes_offline(unsigned long nr_pages,
1180 struct zone *zone, struct memory_notify *arg)
1181{
1182 struct pglist_data *pgdat = zone->zone_pgdat;
1183 unsigned long present_pages = 0;
1184 enum zone_type zt, zone_last = ZONE_NORMAL;
1185
1186 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001187 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1188 * contains nodes which have zones of 0...ZONE_NORMAL,
1189 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001190 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001191 * If we don't have HIGHMEM nor movable node,
1192 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1193 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001194 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001195 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001196 zone_last = ZONE_MOVABLE;
1197
1198 /*
1199 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1200 * If the memory to be offline is in a zone of 0...zone_last,
1201 * and it is the last present memory, 0...zone_last will
1202 * become empty after offline , thus we can determind we will
1203 * need to clear the node from node_states[N_NORMAL_MEMORY].
1204 */
1205 for (zt = 0; zt <= zone_last; zt++)
1206 present_pages += pgdat->node_zones[zt].present_pages;
1207 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1208 arg->status_change_nid_normal = zone_to_nid(zone);
1209 else
1210 arg->status_change_nid_normal = -1;
1211
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001212#ifdef CONFIG_HIGHMEM
1213 /*
1214 * If we have movable node, node_states[N_HIGH_MEMORY]
1215 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1216 * set zone_last to ZONE_HIGHMEM.
1217 *
1218 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1219 * contains nodes which have zones of 0...ZONE_MOVABLE,
1220 * set zone_last to ZONE_MOVABLE.
1221 */
1222 zone_last = ZONE_HIGHMEM;
1223 if (N_MEMORY == N_HIGH_MEMORY)
1224 zone_last = ZONE_MOVABLE;
1225
1226 for (; zt <= zone_last; zt++)
1227 present_pages += pgdat->node_zones[zt].present_pages;
1228 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1229 arg->status_change_nid_high = zone_to_nid(zone);
1230 else
1231 arg->status_change_nid_high = -1;
1232#else
1233 arg->status_change_nid_high = arg->status_change_nid_normal;
1234#endif
1235
Lai Jiangshand9713672012-12-11 16:01:03 -08001236 /*
1237 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1238 */
1239 zone_last = ZONE_MOVABLE;
1240
1241 /*
1242 * check whether node_states[N_HIGH_MEMORY] will be changed
1243 * If we try to offline the last present @nr_pages from the node,
1244 * we can determind we will need to clear the node from
1245 * node_states[N_HIGH_MEMORY].
1246 */
1247 for (; zt <= zone_last; zt++)
1248 present_pages += pgdat->node_zones[zt].present_pages;
1249 if (nr_pages >= present_pages)
1250 arg->status_change_nid = zone_to_nid(zone);
1251 else
1252 arg->status_change_nid = -1;
1253}
1254
1255static void node_states_clear_node(int node, struct memory_notify *arg)
1256{
1257 if (arg->status_change_nid_normal >= 0)
1258 node_clear_state(node, N_NORMAL_MEMORY);
1259
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001260 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1261 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001262 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001263
1264 if ((N_MEMORY != N_HIGH_MEMORY) &&
1265 (arg->status_change_nid >= 0))
1266 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001267}
1268
Wen Congyanga16cee12012-10-08 16:33:58 -07001269static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001270 unsigned long end_pfn, unsigned long timeout)
1271{
1272 unsigned long pfn, nr_pages, expire;
1273 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001274 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001275 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001276 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001277
1278 BUG_ON(start_pfn >= end_pfn);
1279 /* at least, alignment against pageblock is necessary */
1280 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1281 return -EINVAL;
1282 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1283 return -EINVAL;
1284 /* This makes hotplug much easier...and readable.
1285 we assume this for now. .*/
1286 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1287 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001288
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001289 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -08001290
Yasunori Goto7b78d332007-10-21 16:41:36 -07001291 zone = page_zone(pfn_to_page(start_pfn));
1292 node = zone_to_nid(zone);
1293 nr_pages = end_pfn - start_pfn;
1294
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001295 ret = -EINVAL;
1296 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1297 goto out;
1298
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001299 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001300 ret = start_isolate_page_range(start_pfn, end_pfn,
1301 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001302 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -08001303 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001304
1305 arg.start_pfn = start_pfn;
1306 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001307 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001308
1309 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1310 ret = notifier_to_errno(ret);
1311 if (ret)
1312 goto failed_removal;
1313
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001314 pfn = start_pfn;
1315 expire = jiffies + timeout;
1316 drain = 0;
1317 retry_max = 5;
1318repeat:
1319 /* start memory hot removal */
1320 ret = -EAGAIN;
1321 if (time_after(jiffies, expire))
1322 goto failed_removal;
1323 ret = -EINTR;
1324 if (signal_pending(current))
1325 goto failed_removal;
1326 ret = 0;
1327 if (drain) {
1328 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001329 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001330 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001331 }
1332
1333 pfn = scan_lru_pages(start_pfn, end_pfn);
1334 if (pfn) { /* We have page on LRU */
1335 ret = do_migrate_range(pfn, end_pfn);
1336 if (!ret) {
1337 drain = 1;
1338 goto repeat;
1339 } else {
1340 if (ret < 0)
1341 if (--retry_max == 0)
1342 goto failed_removal;
1343 yield();
1344 drain = 1;
1345 goto repeat;
1346 }
1347 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001348 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001349 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001350 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001351 /* drain pcp pages, this is synchronous. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001352 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001353 /* check again */
1354 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1355 if (offlined_pages < 0) {
1356 ret = -EBUSY;
1357 goto failed_removal;
1358 }
1359 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001360 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001361 We cannot do rollback at this point. */
1362 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001363 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001364 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001365 /* removal success */
Jiang Liu9feedc92012-12-12 13:52:12 -08001366 zone->managed_pages -= offlined_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001367 zone->present_pages -= offlined_pages;
1368 zone->zone_pgdat->node_present_pages -= offlined_pages;
1369 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001370
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001371 init_per_zone_wmark_min();
1372
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001373 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001374 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001375 mutex_lock(&zonelists_mutex);
1376 build_all_zonelists(NULL, NULL);
1377 mutex_unlock(&zonelists_mutex);
1378 } else
1379 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001380
Lai Jiangshand9713672012-12-11 16:01:03 -08001381 node_states_clear_node(node, &arg);
1382 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001383 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001384
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001385 vm_total_pages = nr_free_pagecache_pages();
1386 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001387
1388 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001389 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001390 return 0;
1391
1392failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001393 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1394 (unsigned long long) start_pfn << PAGE_SHIFT,
1395 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001396 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001397 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001398 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001399
Andi Kleen6ad696d2009-11-17 14:06:22 -08001400out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001401 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001402 return ret;
1403}
Badari Pulavarty71088782008-10-18 20:25:58 -07001404
Wen Congyanga16cee12012-10-08 16:33:58 -07001405int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1406{
1407 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1408}
1409
Wen Congyangbbc76be2013-02-22 16:32:54 -08001410/**
1411 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1412 * @start_pfn: start pfn of the memory range
1413 * @end_pfn: end pft of the memory range
1414 * @arg: argument passed to func
1415 * @func: callback for each memory section walked
1416 *
1417 * This function walks through all present mem sections in range
1418 * [start_pfn, end_pfn) and call func on each mem section.
1419 *
1420 * Returns the return value of func.
1421 */
1422static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1423 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001424{
Wen Congyange90bdb72012-10-08 16:34:01 -07001425 struct memory_block *mem = NULL;
1426 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001427 unsigned long pfn, section_nr;
1428 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001429
Wen Congyange90bdb72012-10-08 16:34:01 -07001430 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1431 section_nr = pfn_to_section_nr(pfn);
1432 if (!present_section_nr(section_nr))
1433 continue;
1434
1435 section = __nr_to_section(section_nr);
1436 /* same memblock? */
1437 if (mem)
1438 if ((section_nr >= mem->start_section_nr) &&
1439 (section_nr <= mem->end_section_nr))
1440 continue;
1441
1442 mem = find_memory_block_hinted(section, mem);
1443 if (!mem)
1444 continue;
1445
Wen Congyangbbc76be2013-02-22 16:32:54 -08001446 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001447 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001448 kobject_put(&mem->dev.kobj);
1449 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001450 }
1451 }
1452
1453 if (mem)
1454 kobject_put(&mem->dev.kobj);
1455
Wen Congyangbbc76be2013-02-22 16:32:54 -08001456 return 0;
1457}
1458
1459/**
1460 * offline_memory_block_cb - callback function for offlining memory block
1461 * @mem: the memory block to be offlined
1462 * @arg: buffer to hold error msg
1463 *
1464 * Always return 0, and put the error msg in arg if any.
1465 */
1466static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1467{
1468 int *ret = arg;
1469 int error = offline_memory_block(mem);
1470
1471 if (error != 0 && *ret == 0)
1472 *ret = error;
1473
1474 return 0;
1475}
1476
1477static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1478{
1479 int ret = !is_memblock_offlined(mem);
1480
1481 if (unlikely(ret))
1482 pr_warn("removing memory fails, because memory "
1483 "[%#010llx-%#010llx] is onlined\n",
1484 PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1485 PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1486
1487 return ret;
1488}
1489
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001490int __ref remove_memory(u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001491{
1492 unsigned long start_pfn, end_pfn;
1493 int ret = 0;
1494 int retry = 1;
1495
1496 start_pfn = PFN_DOWN(start);
1497 end_pfn = start_pfn + PFN_DOWN(size);
1498
1499 /*
1500 * When CONFIG_MEMCG is on, one memory block may be used by other
1501 * blocks to store page cgroup when onlining pages. But we don't know
1502 * in what order pages are onlined. So we iterate twice to offline
1503 * memory:
1504 * 1st iterate: offline every non primary memory block.
1505 * 2nd iterate: offline primary (i.e. first added) memory block.
1506 */
1507repeat:
1508 walk_memory_range(start_pfn, end_pfn, &ret,
1509 offline_memory_block_cb);
1510 if (ret) {
1511 if (!retry)
1512 return ret;
1513
1514 retry = 0;
1515 ret = 0;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001516 goto repeat;
1517 }
1518
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001519 lock_memory_hotplug();
1520
1521 /*
1522 * we have offlined all memory blocks like this:
1523 * 1. lock memory hotplug
1524 * 2. offline a memory block
1525 * 3. unlock memory hotplug
1526 *
1527 * repeat step1-3 to offline the memory block. All memory blocks
1528 * must be offlined before removing memory. But we don't hold the
1529 * lock in the whole operation. So we should check whether all
1530 * memory blocks are offlined.
1531 */
1532
Wen Congyangbbc76be2013-02-22 16:32:54 -08001533 ret = walk_memory_range(start_pfn, end_pfn, NULL,
1534 is_memblock_offlined_cb);
1535 if (ret) {
1536 unlock_memory_hotplug();
1537 return ret;
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001538 }
1539
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001540 /* remove memmap entry */
1541 firmware_map_remove(start, start + size, "System RAM");
1542
Wen Congyang24d335c2013-02-22 16:32:58 -08001543 arch_remove_memory(start, size);
1544
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001545 unlock_memory_hotplug();
1546
Wen Congyange90bdb72012-10-08 16:34:01 -07001547 return 0;
Badari Pulavarty71088782008-10-18 20:25:58 -07001548}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001549#else
Wen Congyanga16cee12012-10-08 16:33:58 -07001550int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1551{
1552 return -EINVAL;
1553}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001554int remove_memory(u64 start, u64 size)
1555{
1556 return -EINVAL;
1557}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001558#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001559EXPORT_SYMBOL_GPL(remove_memory);