blob: b7c93ca896d6de8179697b908132b0d34dffb479 [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
Yasunori Goto04753272008-04-28 02:13:31 -070094#ifndef CONFIG_SPARSEMEM_VMEMMAP
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080095static void get_page_bootmem(unsigned long info, struct page *page,
96 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -070097{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080098 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -070099 SetPagePrivate(page);
100 set_page_private(page, info);
101 atomic_inc(&page->_count);
102}
103
Rakib Mullick23ce9322009-12-14 17:59:44 -0800104/* reference to __meminit __free_pages_bootmem is valid
105 * so use __ref to tell modpost not to generate a warning */
106void __ref put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700107{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800108 unsigned long type;
Jiang Liu9feedc92012-12-12 13:52:12 -0800109 static DEFINE_MUTEX(ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700110
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800111 type = (unsigned long) page->lru.next;
112 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
113 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700114
115 if (atomic_dec_return(&page->_count) == 1) {
116 ClearPagePrivate(page);
117 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800118 INIT_LIST_HEAD(&page->lru);
Jiang Liu9feedc92012-12-12 13:52:12 -0800119
120 /*
121 * Please refer to comment for __free_pages_bootmem()
122 * for why we serialize here.
123 */
124 mutex_lock(&ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700125 __free_pages_bootmem(page, 0);
Jiang Liu9feedc92012-12-12 13:52:12 -0800126 mutex_unlock(&ppb_lock);
Yasunori Goto04753272008-04-28 02:13:31 -0700127 }
128
129}
130
Adrian Bunkd92bc312008-07-23 21:28:12 -0700131static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700132{
133 unsigned long *usemap, mapsize, section_nr, i;
134 struct mem_section *ms;
135 struct page *page, *memmap;
136
Yasunori Goto04753272008-04-28 02:13:31 -0700137 section_nr = pfn_to_section_nr(start_pfn);
138 ms = __nr_to_section(section_nr);
139
140 /* Get section's memmap address */
141 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
142
143 /*
144 * Get page for the memmap's phys address
145 * XXX: need more consideration for sparse_vmemmap...
146 */
147 page = virt_to_page(memmap);
148 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
149 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
150
151 /* remember memmap's page */
152 for (i = 0; i < mapsize; i++, page++)
153 get_page_bootmem(section_nr, page, SECTION_INFO);
154
155 usemap = __nr_to_section(section_nr)->pageblock_flags;
156 page = virt_to_page(usemap);
157
158 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
159
160 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700161 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700162
163}
164
165void register_page_bootmem_info_node(struct pglist_data *pgdat)
166{
167 unsigned long i, pfn, end_pfn, nr_pages;
168 int node = pgdat->node_id;
169 struct page *page;
170 struct zone *zone;
171
172 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
173 page = virt_to_page(pgdat);
174
175 for (i = 0; i < nr_pages; i++, page++)
176 get_page_bootmem(node, page, NODE_INFO);
177
178 zone = &pgdat->node_zones[0];
179 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
180 if (zone->wait_table) {
181 nr_pages = zone->wait_table_hash_nr_entries
182 * sizeof(wait_queue_head_t);
183 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
184 page = virt_to_page(zone->wait_table);
185
186 for (i = 0; i < nr_pages; i++, page++)
187 get_page_bootmem(node, page, NODE_INFO);
188 }
189 }
190
191 pfn = pgdat->node_start_pfn;
192 end_pfn = pfn + pgdat->node_spanned_pages;
193
194 /* register_section info */
qiuxishif14851a2012-09-17 14:09:24 -0700195 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
196 /*
197 * Some platforms can assign the same pfn to multiple nodes - on
198 * node0 as well as nodeN. To avoid registering a pfn against
199 * multiple nodes we check that this pfn does not already
200 * reside in some other node.
201 */
202 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
203 register_page_bootmem_info_section(pfn);
204 }
Yasunori Goto04753272008-04-28 02:13:31 -0700205}
206#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
207
Heiko Carstens76cdd582008-05-14 16:05:52 -0700208static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
209 unsigned long end_pfn)
210{
211 unsigned long old_zone_end_pfn;
212
213 zone_span_writelock(zone);
214
215 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Tang Chen712cd382012-12-11 16:01:07 -0800216 if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700217 zone->zone_start_pfn = start_pfn;
218
219 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
220 zone->zone_start_pfn;
221
222 zone_span_writeunlock(zone);
223}
224
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800225static void resize_zone(struct zone *zone, unsigned long start_pfn,
226 unsigned long end_pfn)
227{
228 zone_span_writelock(zone);
229
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800230 if (end_pfn - start_pfn) {
231 zone->zone_start_pfn = start_pfn;
232 zone->spanned_pages = end_pfn - start_pfn;
233 } else {
234 /*
235 * make it consist as free_area_init_core(),
236 * if spanned_pages = 0, then keep start_pfn = 0
237 */
238 zone->zone_start_pfn = 0;
239 zone->spanned_pages = 0;
240 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800241
242 zone_span_writeunlock(zone);
243}
244
245static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
246 unsigned long end_pfn)
247{
248 enum zone_type zid = zone_idx(zone);
249 int nid = zone->zone_pgdat->node_id;
250 unsigned long pfn;
251
252 for (pfn = start_pfn; pfn < end_pfn; pfn++)
253 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
254}
255
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800256static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800257 unsigned long start_pfn, unsigned long end_pfn)
258{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800259 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800260 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800261 unsigned long z1_start_pfn;
262
263 if (!z1->wait_table) {
264 ret = init_currently_empty_zone(z1, start_pfn,
265 end_pfn - start_pfn, MEMMAP_HOTPLUG);
266 if (ret)
267 return ret;
268 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800269
270 pgdat_resize_lock(z1->zone_pgdat, &flags);
271
272 /* can't move pfns which are higher than @z2 */
273 if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
274 goto out_fail;
275 /* the move out part mast at the left most of @z2 */
276 if (start_pfn > z2->zone_start_pfn)
277 goto out_fail;
278 /* must included/overlap */
279 if (end_pfn <= z2->zone_start_pfn)
280 goto out_fail;
281
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800282 /* use start_pfn for z1's start_pfn if z1 is empty */
283 if (z1->spanned_pages)
284 z1_start_pfn = z1->zone_start_pfn;
285 else
286 z1_start_pfn = start_pfn;
287
288 resize_zone(z1, z1_start_pfn, end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800289 resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
290
291 pgdat_resize_unlock(z1->zone_pgdat, &flags);
292
293 fix_zone_id(z1, start_pfn, end_pfn);
294
295 return 0;
296out_fail:
297 pgdat_resize_unlock(z1->zone_pgdat, &flags);
298 return -1;
299}
300
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800301static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800302 unsigned long start_pfn, unsigned long end_pfn)
303{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800304 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800305 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800306 unsigned long z2_end_pfn;
307
308 if (!z2->wait_table) {
309 ret = init_currently_empty_zone(z2, start_pfn,
310 end_pfn - start_pfn, MEMMAP_HOTPLUG);
311 if (ret)
312 return ret;
313 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800314
315 pgdat_resize_lock(z1->zone_pgdat, &flags);
316
317 /* can't move pfns which are lower than @z1 */
318 if (z1->zone_start_pfn > start_pfn)
319 goto out_fail;
320 /* the move out part mast at the right most of @z1 */
321 if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
322 goto out_fail;
323 /* must included/overlap */
324 if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
325 goto out_fail;
326
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800327 /* use end_pfn for z2's end_pfn if z2 is empty */
328 if (z2->spanned_pages)
329 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
330 else
331 z2_end_pfn = end_pfn;
332
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800333 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800334 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800335
336 pgdat_resize_unlock(z1->zone_pgdat, &flags);
337
338 fix_zone_id(z2, start_pfn, end_pfn);
339
340 return 0;
341out_fail:
342 pgdat_resize_unlock(z1->zone_pgdat, &flags);
343 return -1;
344}
345
Heiko Carstens76cdd582008-05-14 16:05:52 -0700346static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
347 unsigned long end_pfn)
348{
349 unsigned long old_pgdat_end_pfn =
350 pgdat->node_start_pfn + pgdat->node_spanned_pages;
351
Tang Chen712cd382012-12-11 16:01:07 -0800352 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700353 pgdat->node_start_pfn = start_pfn;
354
355 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
356 pgdat->node_start_pfn;
357}
358
Al Viro31168482008-11-22 17:33:24 +0000359static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700360{
361 struct pglist_data *pgdat = zone->zone_pgdat;
362 int nr_pages = PAGES_PER_SECTION;
363 int nid = pgdat->node_id;
364 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700365 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700366
367 zone_type = zone - pgdat->node_zones;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700368 if (!zone->wait_table) {
369 int ret;
370
371 ret = init_currently_empty_zone(zone, phys_start_pfn,
372 nr_pages, MEMMAP_HOTPLUG);
373 if (ret)
374 return ret;
375 }
376 pgdat_resize_lock(zone->zone_pgdat, &flags);
377 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
378 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
379 phys_start_pfn + nr_pages);
380 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800381 memmap_init_zone(nr_pages, nid, zone_type,
382 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700383 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700384}
385
Gary Hadec04fc582009-01-06 14:39:14 -0800386static int __meminit __add_section(int nid, struct zone *zone,
387 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700388{
Dave Hansen3947be12005-10-29 18:16:54 -0700389 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700390 int ret;
391
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700392 if (pfn_valid(phys_start_pfn))
393 return -EEXIST;
394
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700395 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700396
397 if (ret < 0)
398 return ret;
399
Yasunori Goto718127c2006-06-23 02:03:10 -0700400 ret = __add_zone(zone, phys_start_pfn);
401
402 if (ret < 0)
403 return ret;
404
Gary Hadec04fc582009-01-06 14:39:14 -0800405 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700406}
407
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700408#ifdef CONFIG_SPARSEMEM_VMEMMAP
409static int __remove_section(struct zone *zone, struct mem_section *ms)
410{
411 /*
412 * XXX: Freeing memmap with vmemmap is not implement yet.
413 * This should be removed later.
414 */
415 return -EBUSY;
416}
417#else
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700418static int __remove_section(struct zone *zone, struct mem_section *ms)
419{
420 unsigned long flags;
421 struct pglist_data *pgdat = zone->zone_pgdat;
422 int ret = -EINVAL;
423
424 if (!valid_section(ms))
425 return ret;
426
427 ret = unregister_memory_section(ms);
428 if (ret)
429 return ret;
430
431 pgdat_resize_lock(pgdat, &flags);
432 sparse_remove_one_section(zone, ms);
433 pgdat_resize_unlock(pgdat, &flags);
434 return 0;
435}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700436#endif
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700437
Dave Hansen3947be12005-10-29 18:16:54 -0700438/*
439 * Reasonably generic function for adding memory. It is
440 * expected that archs that support memory hotplug will
441 * call this function after deciding the zone to which to
442 * add the new pages.
443 */
Gary Hadec04fc582009-01-06 14:39:14 -0800444int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
445 unsigned long nr_pages)
Dave Hansen3947be12005-10-29 18:16:54 -0700446{
447 unsigned long i;
448 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700449 int start_sec, end_sec;
450 /* during initialize mem_map, align hot-added range to section */
451 start_sec = pfn_to_section_nr(phys_start_pfn);
452 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700453
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700454 for (i = start_sec; i <= end_sec; i++) {
Gary Hadec04fc582009-01-06 14:39:14 -0800455 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700456
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700457 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200458 * EEXIST is finally dealt with by ioresource collision
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700459 * check. see add_memory() => register_memory_resource()
460 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700461 */
462 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700463 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700464 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700465 }
466
467 return err;
468}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700469EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700470
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700471/**
472 * __remove_pages() - remove sections of pages from a zone
473 * @zone: zone from which pages need to be removed
474 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
475 * @nr_pages: number of pages to remove (must be multiple of section size)
476 *
477 * Generic helper function to remove section mappings and sysfs entries
478 * for the section of the memory we are removing. Caller needs to make
479 * sure that pages are marked reserved and zones are adjust properly by
480 * calling offline_pages().
481 */
482int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
483 unsigned long nr_pages)
484{
485 unsigned long i, ret = 0;
486 int sections_to_remove;
487
488 /*
489 * We can only remove entire sections
490 */
491 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
492 BUG_ON(nr_pages % PAGES_PER_SECTION);
493
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700494 release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
495
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700496 sections_to_remove = nr_pages / PAGES_PER_SECTION;
497 for (i = 0; i < sections_to_remove; i++) {
498 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
499 ret = __remove_section(zone, __pfn_to_section(pfn));
500 if (ret)
501 break;
502 }
503 return ret;
504}
505EXPORT_SYMBOL_GPL(__remove_pages);
506
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700507int set_online_page_callback(online_page_callback_t callback)
508{
509 int rc = -EINVAL;
510
511 lock_memory_hotplug();
512
513 if (online_page_callback == generic_online_page) {
514 online_page_callback = callback;
515 rc = 0;
516 }
517
518 unlock_memory_hotplug();
519
520 return rc;
521}
522EXPORT_SYMBOL_GPL(set_online_page_callback);
523
524int restore_online_page_callback(online_page_callback_t callback)
525{
526 int rc = -EINVAL;
527
528 lock_memory_hotplug();
529
530 if (online_page_callback == callback) {
531 online_page_callback = generic_online_page;
532 rc = 0;
533 }
534
535 unlock_memory_hotplug();
536
537 return rc;
538}
539EXPORT_SYMBOL_GPL(restore_online_page_callback);
540
541void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700542{
Jan Beulich4738e1b2009-09-21 17:03:03 -0700543 unsigned long pfn = page_to_pfn(page);
544
Jan Beulich4738e1b2009-09-21 17:03:03 -0700545 if (pfn >= num_physpages)
546 num_physpages = pfn + 1;
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700547}
548EXPORT_SYMBOL_GPL(__online_page_set_limits);
549
550void __online_page_increment_counters(struct page *page)
551{
552 totalram_pages++;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700553
554#ifdef CONFIG_HIGHMEM
555 if (PageHighMem(page))
556 totalhigh_pages++;
557#endif
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700558}
559EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700560
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700561void __online_page_free(struct page *page)
562{
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700563 ClearPageReserved(page);
564 init_page_count(page);
565 __free_page(page);
566}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700567EXPORT_SYMBOL_GPL(__online_page_free);
568
569static void generic_online_page(struct page *page)
570{
571 __online_page_set_limits(page);
572 __online_page_increment_counters(page);
573 __online_page_free(page);
574}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700575
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700576static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
577 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700578{
579 unsigned long i;
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700580 unsigned long onlined_pages = *(unsigned long *)arg;
581 struct page *page;
582 if (PageReserved(pfn_to_page(start_pfn)))
583 for (i = 0; i < nr_pages; i++) {
584 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700585 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700586 onlined_pages++;
587 }
588 *(unsigned long *)arg = onlined_pages;
589 return 0;
590}
591
Lai Jiangshan09285af2012-12-12 13:52:04 -0800592#ifdef CONFIG_MOVABLE_NODE
593/* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
594static bool can_online_high_movable(struct zone *zone)
595{
596 return true;
597}
598#else /* #ifdef CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800599/* ensure every online node has NORMAL memory */
600static bool can_online_high_movable(struct zone *zone)
601{
602 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
603}
Lai Jiangshan09285af2012-12-12 13:52:04 -0800604#endif /* #ifdef CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800605
Lai Jiangshand9713672012-12-11 16:01:03 -0800606/* check which state of node_states will be changed when online memory */
607static void node_states_check_changes_online(unsigned long nr_pages,
608 struct zone *zone, struct memory_notify *arg)
609{
610 int nid = zone_to_nid(zone);
611 enum zone_type zone_last = ZONE_NORMAL;
612
613 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800614 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
615 * contains nodes which have zones of 0...ZONE_NORMAL,
616 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800617 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800618 * If we don't have HIGHMEM nor movable node,
619 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
620 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800621 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800622 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800623 zone_last = ZONE_MOVABLE;
624
625 /*
626 * if the memory to be online is in a zone of 0...zone_last, and
627 * the zones of 0...zone_last don't have memory before online, we will
628 * need to set the node to node_states[N_NORMAL_MEMORY] after
629 * the memory is online.
630 */
631 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
632 arg->status_change_nid_normal = nid;
633 else
634 arg->status_change_nid_normal = -1;
635
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800636#ifdef CONFIG_HIGHMEM
637 /*
638 * If we have movable node, node_states[N_HIGH_MEMORY]
639 * contains nodes which have zones of 0...ZONE_HIGHMEM,
640 * set zone_last to ZONE_HIGHMEM.
641 *
642 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
643 * contains nodes which have zones of 0...ZONE_MOVABLE,
644 * set zone_last to ZONE_MOVABLE.
645 */
646 zone_last = ZONE_HIGHMEM;
647 if (N_MEMORY == N_HIGH_MEMORY)
648 zone_last = ZONE_MOVABLE;
649
650 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
651 arg->status_change_nid_high = nid;
652 else
653 arg->status_change_nid_high = -1;
654#else
655 arg->status_change_nid_high = arg->status_change_nid_normal;
656#endif
657
Lai Jiangshand9713672012-12-11 16:01:03 -0800658 /*
659 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800660 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800661 * is online.
662 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800663 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800664 arg->status_change_nid = nid;
665 else
666 arg->status_change_nid = -1;
667}
668
669static void node_states_set_node(int node, struct memory_notify *arg)
670{
671 if (arg->status_change_nid_normal >= 0)
672 node_set_state(node, N_NORMAL_MEMORY);
673
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800674 if (arg->status_change_nid_high >= 0)
675 node_set_state(node, N_HIGH_MEMORY);
676
677 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800678}
679
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700680
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800681int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700682{
Dave Hansen3947be12005-10-29 18:16:54 -0700683 unsigned long onlined_pages = 0;
684 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700685 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700686 int nid;
687 int ret;
688 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700689
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900690 lock_memory_hotplug();
Lai Jiangshand9713672012-12-11 16:01:03 -0800691 /*
692 * This doesn't need a lock to do pfn_to_page().
693 * The section can't be removed here because of the
694 * memory_block->state_mutex.
695 */
696 zone = page_zone(pfn_to_page(pfn));
697
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800698 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
699 !can_online_high_movable(zone)) {
700 unlock_memory_hotplug();
701 return -1;
702 }
703
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800704 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
705 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
706 unlock_memory_hotplug();
707 return -1;
708 }
709 }
710 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
711 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
712 unlock_memory_hotplug();
713 return -1;
714 }
715 }
716
717 /* Previous code may changed the zone of the pfn range */
718 zone = page_zone(pfn_to_page(pfn));
719
Yasunori Goto7b78d332007-10-21 16:41:36 -0700720 arg.start_pfn = pfn;
721 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800722 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700723
724 nid = page_to_nid(pfn_to_page(pfn));
Yasunori Goto7b78d332007-10-21 16:41:36 -0700725
726 ret = memory_notify(MEM_GOING_ONLINE, &arg);
727 ret = notifier_to_errno(ret);
728 if (ret) {
729 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900730 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700731 return ret;
732 }
Dave Hansen3947be12005-10-29 18:16:54 -0700733 /*
Yasunori Goto68113782006-06-23 02:03:11 -0700734 * If this zone is not populated, then it is not in zonelist.
735 * This means the page allocator ignores this zone.
736 * So, zonelist must be updated after online.
737 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700738 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800739 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -0700740 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800741 build_all_zonelists(NULL, zone);
742 }
Yasunori Goto68113782006-06-23 02:03:11 -0700743
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700744 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700745 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700746 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800747 if (need_zonelists_rebuild)
748 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700749 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -0700750 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
751 (unsigned long long) pfn << PAGE_SHIFT,
752 (((unsigned long long) pfn + nr_pages)
753 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700754 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900755 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700756 return ret;
757 }
758
Jiang Liu9feedc92012-12-12 13:52:12 -0800759 zone->managed_pages += onlined_pages;
Dave Hansen3947be12005-10-29 18:16:54 -0700760 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800761 zone->zone_pgdat->node_present_pages += onlined_pages;
Jiang Liu08dff7b2012-07-31 16:43:30 -0700762 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -0800763 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700764 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800765 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700766 else
767 zone_pcp_update(zone);
768 }
Dave Hansen3947be12005-10-29 18:16:54 -0700769
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700770 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700771
772 init_per_zone_wmark_min();
773
Jiang Liu08dff7b2012-07-31 16:43:30 -0700774 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -0700775 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -0700776
Haicheng Li1f522502010-05-24 14:32:51 -0700777 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700778
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700779 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700780
781 if (onlined_pages)
782 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900783 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700784
Dave Hansen3947be12005-10-29 18:16:54 -0700785 return 0;
786}
Keith Mannthey53947022006-09-30 23:27:08 -0700787#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700788
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800789/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
790static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700791{
792 struct pglist_data *pgdat;
793 unsigned long zones_size[MAX_NR_ZONES] = {0};
794 unsigned long zholes_size[MAX_NR_ZONES] = {0};
795 unsigned long start_pfn = start >> PAGE_SHIFT;
796
797 pgdat = arch_alloc_nodedata(nid);
798 if (!pgdat)
799 return NULL;
800
801 arch_refresh_nodedata(nid, pgdat);
802
803 /* we can use NODE_DATA(nid) from here */
804
805 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700806 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700807
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700808 /*
809 * The node we allocated has no zone fallback lists. For avoiding
810 * to access not-initialized zonelist, build here.
811 */
David Rientjesf957db42011-06-22 18:13:04 -0700812 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -0700813 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700814 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700815
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700816 return pgdat;
817}
818
819static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
820{
821 arch_refresh_nodedata(nid, NULL);
822 arch_free_nodedata(pgdat);
823 return;
824}
825
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700826
minskey guocf234222010-05-24 14:32:41 -0700827/*
828 * called by cpu_up() to online a node without onlined memory.
829 */
830int mem_online_node(int nid)
831{
832 pg_data_t *pgdat;
833 int ret;
834
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800835 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700836 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700837 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700838 ret = -ENOMEM;
839 goto out;
840 }
841 node_set_online(nid);
842 ret = register_one_node(nid);
843 BUG_ON(ret);
844
845out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800846 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700847 return ret;
848}
849
Al Viro31168482008-11-22 17:33:24 +0000850/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
851int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700852{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700853 pg_data_t *pgdat = NULL;
854 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700855 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700856 int ret;
857
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800858 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800859
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700860 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800861 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700862 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800863 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700864
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700865 if (!node_online(nid)) {
866 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800867 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700868 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -0700869 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700870 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700871 }
872
Yasunori Gotobc02af92006-06-27 02:53:30 -0700873 /* call arch's memory hotadd */
874 ret = arch_add_memory(nid, start, size);
875
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700876 if (ret < 0)
877 goto error;
878
Yasunori Goto0fc44152006-06-27 02:53:38 -0700879 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700880 node_set_online(nid);
881
Yasunori Goto0fc44152006-06-27 02:53:38 -0700882 if (new_pgdat) {
883 ret = register_one_node(nid);
884 /*
885 * If sysfs file of new node can't create, cpu on the node
886 * can't be hot-added. There is no rollback way now.
887 * So, check by BUG_ON() to catch it reluctantly..
888 */
889 BUG_ON(ret);
890 }
891
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800892 /* create new memmap entry */
893 firmware_map_add_hotplug(start, start + size, "System RAM");
894
Andi Kleen6ad696d2009-11-17 14:06:22 -0800895 goto out;
896
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700897error:
898 /* rollback pgdat allocation and others */
899 if (new_pgdat)
900 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700901 if (res)
902 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700903
Andi Kleen6ad696d2009-11-17 14:06:22 -0800904out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800905 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700906 return ret;
907}
908EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700909
910#ifdef CONFIG_MEMORY_HOTREMOVE
911/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700912 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
913 * set and the size of the free page is given by page_order(). Using this,
914 * the function determines if the pageblock contains only free pages.
915 * Due to buddy contraints, a free page at least the size of a pageblock will
916 * be located at the start of the pageblock
917 */
918static inline int pageblock_free(struct page *page)
919{
920 return PageBuddy(page) && page_order(page) >= pageblock_order;
921}
922
923/* Return the start of the next active pageblock after a given page */
924static struct page *next_active_pageblock(struct page *page)
925{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700926 /* Ensure the starting page is pageblock-aligned */
927 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
928
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700929 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700930 if (pageblock_free(page)) {
931 int order;
932 /* be careful. we don't have locks, page_order can be changed.*/
933 order = page_order(page);
934 if ((order < MAX_ORDER) && (order >= pageblock_order))
935 return page + (1 << order);
936 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700937
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700938 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700939}
940
941/* Checks if this range of memory is likely to be hot-removable. */
942int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
943{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700944 struct page *page = pfn_to_page(start_pfn);
945 struct page *end_page = page + nr_pages;
946
947 /* Check the starting page of each pageblock within the range */
948 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700949 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700950 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700951 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700952 }
953
954 /* All pageblocks in the memory block are likely to be hot-removable */
955 return 1;
956}
957
958/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700959 * Confirm all pages in a range [start, end) is belongs to the same zone.
960 */
961static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
962{
963 unsigned long pfn;
964 struct zone *zone = NULL;
965 struct page *page;
966 int i;
967 for (pfn = start_pfn;
968 pfn < end_pfn;
969 pfn += MAX_ORDER_NR_PAGES) {
970 i = 0;
971 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
972 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
973 i++;
974 if (i == MAX_ORDER_NR_PAGES)
975 continue;
976 page = pfn_to_page(pfn + i);
977 if (zone && page_zone(page) != zone)
978 return 0;
979 zone = page_zone(page);
980 }
981 return 1;
982}
983
984/*
985 * Scanning pfn is much easier than scanning lru list.
986 * Scan pfn from start to end and Find LRU page.
987 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700988static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700989{
990 unsigned long pfn;
991 struct page *page;
992 for (pfn = start; pfn < end; pfn++) {
993 if (pfn_valid(pfn)) {
994 page = pfn_to_page(pfn);
995 if (PageLRU(page))
996 return pfn;
997 }
998 }
999 return 0;
1000}
1001
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001002#define NR_OFFLINE_AT_ONCE_PAGES (256)
1003static int
1004do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1005{
1006 unsigned long pfn;
1007 struct page *page;
1008 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1009 int not_managed = 0;
1010 int ret = 0;
1011 LIST_HEAD(source);
1012
1013 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1014 if (!pfn_valid(pfn))
1015 continue;
1016 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001017 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001018 continue;
1019 /*
1020 * We can skip free pages. And we can only deal with pages on
1021 * LRU.
1022 */
Nick Piggin62695a82008-10-18 20:26:09 -07001023 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001024 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001025 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001026 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001027 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001028 inc_zone_page_state(page, NR_ISOLATED_ANON +
1029 page_is_file_cache(page));
1030
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001031 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001032#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001033 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1034 pfn);
1035 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001036#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001037 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001039 check this again here. */
1040 if (page_count(page)) {
1041 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001042 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001043 break;
1044 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001045 }
1046 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001047 if (!list_empty(&source)) {
1048 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001049 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001050 goto out;
1051 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001052
1053 /*
1054 * alloc_migrate_target should be improooooved!!
1055 * migrate_pages returns # of failed pages.
1056 */
1057 ret = migrate_pages(&source, alloc_migrate_target, 0,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001058 true, MIGRATE_SYNC);
Bob Liuf3ab2632010-10-26 14:22:10 -07001059 if (ret)
1060 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001061 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001062out:
1063 return ret;
1064}
1065
1066/*
1067 * remove from free_area[] and mark all as Reserved.
1068 */
1069static int
1070offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1071 void *data)
1072{
1073 __offline_isolated_pages(start, start + nr_pages);
1074 return 0;
1075}
1076
1077static void
1078offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1079{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001080 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001081 offline_isolated_pages_cb);
1082}
1083
1084/*
1085 * Check all pages in range, recoreded as memory resource, are isolated.
1086 */
1087static int
1088check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1089 void *data)
1090{
1091 int ret;
1092 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001093 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001094 offlined = nr_pages;
1095 if (!ret)
1096 *(long *)data += offlined;
1097 return ret;
1098}
1099
1100static long
1101check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1102{
1103 long offlined = 0;
1104 int ret;
1105
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001106 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001107 check_pages_isolated_cb);
1108 if (ret < 0)
1109 offlined = (long)ret;
1110 return offlined;
1111}
1112
Lai Jiangshan09285af2012-12-12 13:52:04 -08001113#ifdef CONFIG_MOVABLE_NODE
1114/* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
1115static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1116{
1117 return true;
1118}
1119#else /* #ifdef CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001120/* ensure the node has NORMAL memory if it is still online */
1121static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1122{
1123 struct pglist_data *pgdat = zone->zone_pgdat;
1124 unsigned long present_pages = 0;
1125 enum zone_type zt;
1126
1127 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1128 present_pages += pgdat->node_zones[zt].present_pages;
1129
1130 if (present_pages > nr_pages)
1131 return true;
1132
1133 present_pages = 0;
1134 for (; zt <= ZONE_MOVABLE; zt++)
1135 present_pages += pgdat->node_zones[zt].present_pages;
1136
1137 /*
1138 * we can't offline the last normal memory until all
1139 * higher memory is offlined.
1140 */
1141 return present_pages == 0;
1142}
Lai Jiangshan09285af2012-12-12 13:52:04 -08001143#endif /* #ifdef CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001144
Lai Jiangshand9713672012-12-11 16:01:03 -08001145/* check which state of node_states will be changed when offline memory */
1146static void node_states_check_changes_offline(unsigned long nr_pages,
1147 struct zone *zone, struct memory_notify *arg)
1148{
1149 struct pglist_data *pgdat = zone->zone_pgdat;
1150 unsigned long present_pages = 0;
1151 enum zone_type zt, zone_last = ZONE_NORMAL;
1152
1153 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001154 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1155 * contains nodes which have zones of 0...ZONE_NORMAL,
1156 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001157 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001158 * If we don't have HIGHMEM nor movable node,
1159 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1160 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001161 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001162 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001163 zone_last = ZONE_MOVABLE;
1164
1165 /*
1166 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1167 * If the memory to be offline is in a zone of 0...zone_last,
1168 * and it is the last present memory, 0...zone_last will
1169 * become empty after offline , thus we can determind we will
1170 * need to clear the node from node_states[N_NORMAL_MEMORY].
1171 */
1172 for (zt = 0; zt <= zone_last; zt++)
1173 present_pages += pgdat->node_zones[zt].present_pages;
1174 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1175 arg->status_change_nid_normal = zone_to_nid(zone);
1176 else
1177 arg->status_change_nid_normal = -1;
1178
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001179#ifdef CONFIG_HIGHMEM
1180 /*
1181 * If we have movable node, node_states[N_HIGH_MEMORY]
1182 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1183 * set zone_last to ZONE_HIGHMEM.
1184 *
1185 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1186 * contains nodes which have zones of 0...ZONE_MOVABLE,
1187 * set zone_last to ZONE_MOVABLE.
1188 */
1189 zone_last = ZONE_HIGHMEM;
1190 if (N_MEMORY == N_HIGH_MEMORY)
1191 zone_last = ZONE_MOVABLE;
1192
1193 for (; zt <= zone_last; zt++)
1194 present_pages += pgdat->node_zones[zt].present_pages;
1195 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1196 arg->status_change_nid_high = zone_to_nid(zone);
1197 else
1198 arg->status_change_nid_high = -1;
1199#else
1200 arg->status_change_nid_high = arg->status_change_nid_normal;
1201#endif
1202
Lai Jiangshand9713672012-12-11 16:01:03 -08001203 /*
1204 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1205 */
1206 zone_last = ZONE_MOVABLE;
1207
1208 /*
1209 * check whether node_states[N_HIGH_MEMORY] will be changed
1210 * If we try to offline the last present @nr_pages from the node,
1211 * we can determind we will need to clear the node from
1212 * node_states[N_HIGH_MEMORY].
1213 */
1214 for (; zt <= zone_last; zt++)
1215 present_pages += pgdat->node_zones[zt].present_pages;
1216 if (nr_pages >= present_pages)
1217 arg->status_change_nid = zone_to_nid(zone);
1218 else
1219 arg->status_change_nid = -1;
1220}
1221
1222static void node_states_clear_node(int node, struct memory_notify *arg)
1223{
1224 if (arg->status_change_nid_normal >= 0)
1225 node_clear_state(node, N_NORMAL_MEMORY);
1226
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001227 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1228 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001229 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001230
1231 if ((N_MEMORY != N_HIGH_MEMORY) &&
1232 (arg->status_change_nid >= 0))
1233 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001234}
1235
Wen Congyanga16cee12012-10-08 16:33:58 -07001236static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001237 unsigned long end_pfn, unsigned long timeout)
1238{
1239 unsigned long pfn, nr_pages, expire;
1240 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001241 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001242 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001243 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001244
1245 BUG_ON(start_pfn >= end_pfn);
1246 /* at least, alignment against pageblock is necessary */
1247 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1248 return -EINVAL;
1249 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1250 return -EINVAL;
1251 /* This makes hotplug much easier...and readable.
1252 we assume this for now. .*/
1253 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1254 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001255
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001256 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -08001257
Yasunori Goto7b78d332007-10-21 16:41:36 -07001258 zone = page_zone(pfn_to_page(start_pfn));
1259 node = zone_to_nid(zone);
1260 nr_pages = end_pfn - start_pfn;
1261
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001262 ret = -EINVAL;
1263 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1264 goto out;
1265
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001266 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001267 ret = start_isolate_page_range(start_pfn, end_pfn,
1268 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001269 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -08001270 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001271
1272 arg.start_pfn = start_pfn;
1273 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001274 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001275
1276 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1277 ret = notifier_to_errno(ret);
1278 if (ret)
1279 goto failed_removal;
1280
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001281 pfn = start_pfn;
1282 expire = jiffies + timeout;
1283 drain = 0;
1284 retry_max = 5;
1285repeat:
1286 /* start memory hot removal */
1287 ret = -EAGAIN;
1288 if (time_after(jiffies, expire))
1289 goto failed_removal;
1290 ret = -EINTR;
1291 if (signal_pending(current))
1292 goto failed_removal;
1293 ret = 0;
1294 if (drain) {
1295 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001296 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001297 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001298 }
1299
1300 pfn = scan_lru_pages(start_pfn, end_pfn);
1301 if (pfn) { /* We have page on LRU */
1302 ret = do_migrate_range(pfn, end_pfn);
1303 if (!ret) {
1304 drain = 1;
1305 goto repeat;
1306 } else {
1307 if (ret < 0)
1308 if (--retry_max == 0)
1309 goto failed_removal;
1310 yield();
1311 drain = 1;
1312 goto repeat;
1313 }
1314 }
1315 /* drain all zone's lru pagevec, this is asyncronous... */
1316 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001317 yield();
1318 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001319 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001320 /* check again */
1321 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1322 if (offlined_pages < 0) {
1323 ret = -EBUSY;
1324 goto failed_removal;
1325 }
1326 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1327 /* Ok, all of our target is islaoted.
1328 We cannot do rollback at this point. */
1329 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001330 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001331 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001332 /* removal success */
Jiang Liu9feedc92012-12-12 13:52:12 -08001333 zone->managed_pages -= offlined_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001334 zone->present_pages -= offlined_pages;
1335 zone->zone_pgdat->node_present_pages -= offlined_pages;
1336 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001337
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001338 init_per_zone_wmark_min();
1339
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001340 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001341 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001342 mutex_lock(&zonelists_mutex);
1343 build_all_zonelists(NULL, NULL);
1344 mutex_unlock(&zonelists_mutex);
1345 } else
1346 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001347
Lai Jiangshand9713672012-12-11 16:01:03 -08001348 node_states_clear_node(node, &arg);
1349 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001350 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001351
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001352 vm_total_pages = nr_free_pagecache_pages();
1353 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001354
1355 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001356 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001357 return 0;
1358
1359failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001360 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1361 (unsigned long long) start_pfn << PAGE_SHIFT,
1362 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001363 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001364 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001365 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001366
Andi Kleen6ad696d2009-11-17 14:06:22 -08001367out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001368 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001369 return ret;
1370}
Badari Pulavarty71088782008-10-18 20:25:58 -07001371
Wen Congyanga16cee12012-10-08 16:33:58 -07001372int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1373{
1374 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1375}
1376
Badari Pulavarty71088782008-10-18 20:25:58 -07001377int remove_memory(u64 start, u64 size)
1378{
Wen Congyange90bdb72012-10-08 16:34:01 -07001379 struct memory_block *mem = NULL;
1380 struct mem_section *section;
Badari Pulavarty71088782008-10-18 20:25:58 -07001381 unsigned long start_pfn, end_pfn;
Wen Congyange90bdb72012-10-08 16:34:01 -07001382 unsigned long pfn, section_nr;
1383 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001384
1385 start_pfn = PFN_DOWN(start);
1386 end_pfn = start_pfn + PFN_DOWN(size);
Wen Congyange90bdb72012-10-08 16:34:01 -07001387
1388 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1389 section_nr = pfn_to_section_nr(pfn);
1390 if (!present_section_nr(section_nr))
1391 continue;
1392
1393 section = __nr_to_section(section_nr);
1394 /* same memblock? */
1395 if (mem)
1396 if ((section_nr >= mem->start_section_nr) &&
1397 (section_nr <= mem->end_section_nr))
1398 continue;
1399
1400 mem = find_memory_block_hinted(section, mem);
1401 if (!mem)
1402 continue;
1403
1404 ret = offline_memory_block(mem);
1405 if (ret) {
1406 kobject_put(&mem->dev.kobj);
1407 return ret;
1408 }
1409 }
1410
1411 if (mem)
1412 kobject_put(&mem->dev.kobj);
1413
1414 return 0;
Badari Pulavarty71088782008-10-18 20:25:58 -07001415}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001416#else
Wen Congyanga16cee12012-10-08 16:33:58 -07001417int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1418{
1419 return -EINVAL;
1420}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001421int remove_memory(u64 start, u64 size)
1422{
1423 return -EINVAL;
1424}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001425#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001426EXPORT_SYMBOL_GPL(remove_memory);