blob: f8cf37d350fbc8c29cc48c784428b49d14a732f8 [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>
14#include <linux/module.h>
15#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
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080037DEFINE_MUTEX(mem_hotplug_mutex);
38
39void lock_memory_hotplug(void)
40{
41 mutex_lock(&mem_hotplug_mutex);
42
43 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
44 lock_system_sleep();
45}
46
47void unlock_memory_hotplug(void)
48{
49 unlock_system_sleep();
50 mutex_unlock(&mem_hotplug_mutex);
51}
52
53
Keith Mannthey45e0b782006-09-30 23:27:09 -070054/* add this memory to iomem resource */
55static struct resource *register_memory_resource(u64 start, u64 size)
56{
57 struct resource *res;
58 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
59 BUG_ON(!res);
60
61 res->name = "System RAM";
62 res->start = start;
63 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -080064 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -070065 if (request_resource(&iomem_resource, res) < 0) {
66 printk("System RAM resource %llx - %llx cannot be added\n",
67 (unsigned long long)res->start, (unsigned long long)res->end);
68 kfree(res);
69 res = NULL;
70 }
71 return res;
72}
73
74static void release_memory_resource(struct resource *res)
75{
76 if (!res)
77 return;
78 release_resource(res);
79 kfree(res);
80 return;
81}
82
Keith Mannthey53947022006-09-30 23:27:08 -070083#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasunori Goto04753272008-04-28 02:13:31 -070084#ifndef CONFIG_SPARSEMEM_VMEMMAP
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080085static void get_page_bootmem(unsigned long info, struct page *page,
86 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -070087{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080088 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -070089 SetPagePrivate(page);
90 set_page_private(page, info);
91 atomic_inc(&page->_count);
92}
93
Rakib Mullick23ce9322009-12-14 17:59:44 -080094/* reference to __meminit __free_pages_bootmem is valid
95 * so use __ref to tell modpost not to generate a warning */
96void __ref put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -070097{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080098 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -070099
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800100 type = (unsigned long) page->lru.next;
101 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
102 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700103
104 if (atomic_dec_return(&page->_count) == 1) {
105 ClearPagePrivate(page);
106 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800107 INIT_LIST_HEAD(&page->lru);
Yasunori Goto04753272008-04-28 02:13:31 -0700108 __free_pages_bootmem(page, 0);
109 }
110
111}
112
Adrian Bunkd92bc312008-07-23 21:28:12 -0700113static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700114{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 unsigned long *usemap, mapsize, page_mapsize, section_nr, i, j;
Yasunori Goto04753272008-04-28 02:13:31 -0700116 struct mem_section *ms;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117 struct page *page, *memmap, *page_page;
118 int memmap_page_valid;
Yasunori Goto04753272008-04-28 02:13:31 -0700119
120 if (!pfn_valid(start_pfn))
121 return;
122
123 section_nr = pfn_to_section_nr(start_pfn);
124 ms = __nr_to_section(section_nr);
125
126 /* Get section's memmap address */
127 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
128
129 /*
130 * Get page for the memmap's phys address
131 * XXX: need more consideration for sparse_vmemmap...
132 */
133 page = virt_to_page(memmap);
134 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
135 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 page_mapsize = PAGE_SIZE/sizeof(struct page);
138
139 /* remember memmap's page, except those that reference only holes */
140 for (i = 0; i < mapsize; i++, page++) {
141 memmap_page_valid = 0;
142 page_page = __va(page_to_pfn(page) << PAGE_SHIFT);
143 for (j = 0; j < page_mapsize; j++, page_page++) {
144 if (early_pfn_valid(page_to_pfn(page_page))) {
145 memmap_page_valid = 1;
146 break;
147 }
148 }
149 if (memmap_page_valid)
150 get_page_bootmem(section_nr, page, SECTION_INFO);
151 }
Yasunori Goto04753272008-04-28 02:13:31 -0700152
153 usemap = __nr_to_section(section_nr)->pageblock_flags;
154 page = virt_to_page(usemap);
155
156 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
157
158 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700159 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700160
161}
162
163void register_page_bootmem_info_node(struct pglist_data *pgdat)
164{
165 unsigned long i, pfn, end_pfn, nr_pages;
166 int node = pgdat->node_id;
167 struct page *page;
168 struct zone *zone;
169
170 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
171 page = virt_to_page(pgdat);
172
173 for (i = 0; i < nr_pages; i++, page++)
174 get_page_bootmem(node, page, NODE_INFO);
175
176 zone = &pgdat->node_zones[0];
177 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
178 if (zone->wait_table) {
179 nr_pages = zone->wait_table_hash_nr_entries
180 * sizeof(wait_queue_head_t);
181 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
182 page = virt_to_page(zone->wait_table);
183
184 for (i = 0; i < nr_pages; i++, page++)
185 get_page_bootmem(node, page, NODE_INFO);
186 }
187 }
188
189 pfn = pgdat->node_start_pfn;
190 end_pfn = pfn + pgdat->node_spanned_pages;
191
192 /* register_section info */
193 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
194 register_page_bootmem_info_section(pfn);
195
196}
197#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
198
Heiko Carstens76cdd582008-05-14 16:05:52 -0700199static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
200 unsigned long end_pfn)
201{
202 unsigned long old_zone_end_pfn;
203
204 zone_span_writelock(zone);
205
206 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
207 if (start_pfn < zone->zone_start_pfn)
208 zone->zone_start_pfn = start_pfn;
209
210 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
211 zone->zone_start_pfn;
212
213 zone_span_writeunlock(zone);
214}
215
216static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
217 unsigned long end_pfn)
218{
219 unsigned long old_pgdat_end_pfn =
220 pgdat->node_start_pfn + pgdat->node_spanned_pages;
221
222 if (start_pfn < pgdat->node_start_pfn)
223 pgdat->node_start_pfn = start_pfn;
224
225 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
226 pgdat->node_start_pfn;
227}
228
Al Viro31168482008-11-22 17:33:24 +0000229static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700230{
231 struct pglist_data *pgdat = zone->zone_pgdat;
232 int nr_pages = PAGES_PER_SECTION;
233 int nid = pgdat->node_id;
234 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700235 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700236
237 zone_type = zone - pgdat->node_zones;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700238 if (!zone->wait_table) {
239 int ret;
240
241 ret = init_currently_empty_zone(zone, phys_start_pfn,
242 nr_pages, MEMMAP_HOTPLUG);
243 if (ret)
244 return ret;
245 }
246 pgdat_resize_lock(zone->zone_pgdat, &flags);
247 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
248 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
249 phys_start_pfn + nr_pages);
250 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa02007-01-10 23:15:30 -0800251 memmap_init_zone(nr_pages, nid, zone_type,
252 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700253 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700254}
255
Gary Hadec04fc582009-01-06 14:39:14 -0800256static int __meminit __add_section(int nid, struct zone *zone,
257 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700258{
Dave Hansen3947be12005-10-29 18:16:54 -0700259 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700260 int ret;
261
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700262 if (pfn_valid(phys_start_pfn))
263 return -EEXIST;
264
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700265 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700266
267 if (ret < 0)
268 return ret;
269
Yasunori Goto718127c2006-06-23 02:03:10 -0700270 ret = __add_zone(zone, phys_start_pfn);
271
272 if (ret < 0)
273 return ret;
274
Gary Hadec04fc582009-01-06 14:39:14 -0800275 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700276}
277
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700278#ifdef CONFIG_SPARSEMEM_VMEMMAP
279static int __remove_section(struct zone *zone, struct mem_section *ms)
280{
281 /*
282 * XXX: Freeing memmap with vmemmap is not implement yet.
283 * This should be removed later.
284 */
285 return -EBUSY;
286}
287#else
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700288static int __remove_section(struct zone *zone, struct mem_section *ms)
289{
290 unsigned long flags;
291 struct pglist_data *pgdat = zone->zone_pgdat;
292 int ret = -EINVAL;
293
294 if (!valid_section(ms))
295 return ret;
296
297 ret = unregister_memory_section(ms);
298 if (ret)
299 return ret;
300
301 pgdat_resize_lock(pgdat, &flags);
302 sparse_remove_one_section(zone, ms);
303 pgdat_resize_unlock(pgdat, &flags);
304 return 0;
305}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700306#endif
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700307
Dave Hansen3947be12005-10-29 18:16:54 -0700308/*
309 * Reasonably generic function for adding memory. It is
310 * expected that archs that support memory hotplug will
311 * call this function after deciding the zone to which to
312 * add the new pages.
313 */
Gary Hadec04fc582009-01-06 14:39:14 -0800314int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
315 unsigned long nr_pages)
Dave Hansen3947be12005-10-29 18:16:54 -0700316{
317 unsigned long i;
318 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700319 int start_sec, end_sec;
320 /* during initialize mem_map, align hot-added range to section */
321 start_sec = pfn_to_section_nr(phys_start_pfn);
322 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700323
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700324 for (i = start_sec; i <= end_sec; i++) {
Gary Hadec04fc582009-01-06 14:39:14 -0800325 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700326
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700327 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200328 * EEXIST is finally dealt with by ioresource collision
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700329 * check. see add_memory() => register_memory_resource()
330 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700331 */
332 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700333 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700334 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700335 }
336
337 return err;
338}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700339EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700340
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700341/**
342 * __remove_pages() - remove sections of pages from a zone
343 * @zone: zone from which pages need to be removed
344 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
345 * @nr_pages: number of pages to remove (must be multiple of section size)
346 *
347 * Generic helper function to remove section mappings and sysfs entries
348 * for the section of the memory we are removing. Caller needs to make
349 * sure that pages are marked reserved and zones are adjust properly by
350 * calling offline_pages().
351 */
352int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
353 unsigned long nr_pages)
354{
355 unsigned long i, ret = 0;
356 int sections_to_remove;
357
358 /*
359 * We can only remove entire sections
360 */
361 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
362 BUG_ON(nr_pages % PAGES_PER_SECTION);
363
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700364 sections_to_remove = nr_pages / PAGES_PER_SECTION;
365 for (i = 0; i < sections_to_remove; i++) {
366 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Nathan Fontenotde7f0cb2008-10-18 20:27:14 -0700367 release_mem_region(pfn << PAGE_SHIFT,
368 PAGES_PER_SECTION << PAGE_SHIFT);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700369 ret = __remove_section(zone, __pfn_to_section(pfn));
370 if (ret)
371 break;
372 }
373 return ret;
374}
375EXPORT_SYMBOL_GPL(__remove_pages);
376
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700377void online_page(struct page *page)
378{
Jan Beulich4738e1b2009-09-21 17:03:03 -0700379 unsigned long pfn = page_to_pfn(page);
380
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700381 totalram_pages++;
Jack Cheung59f9f1c2011-11-29 16:52:49 -0800382#ifdef CONFIG_FIX_MOVABLE_ZONE
383 if (zone_idx(page_zone(page)) != ZONE_MOVABLE)
384 total_unmovable_pages++;
385#endif
Jan Beulich4738e1b2009-09-21 17:03:03 -0700386 if (pfn >= num_physpages)
387 num_physpages = pfn + 1;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700388
389#ifdef CONFIG_HIGHMEM
390 if (PageHighMem(page))
391 totalhigh_pages++;
392#endif
393
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700394 ClearPageReserved(page);
395 init_page_count(page);
396 __free_page(page);
397}
398
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700399static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
400 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700401{
402 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700403 unsigned long onlined_pages = *(unsigned long *)arg;
404 struct page *page;
405 if (PageReserved(pfn_to_page(start_pfn)))
406 for (i = 0; i < nr_pages; i++) {
407 page = pfn_to_page(start_pfn + i);
408 online_page(page);
409 onlined_pages++;
410 }
411 *(unsigned long *)arg = onlined_pages;
412 return 0;
413}
414
415
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700416int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700417{
Dave Hansen3947be12005-10-29 18:16:54 -0700418 unsigned long onlined_pages = 0;
419 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700420 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700421 int nid;
422 int ret;
423 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700424
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900425 lock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700426 arg.start_pfn = pfn;
427 arg.nr_pages = nr_pages;
428 arg.status_change_nid = -1;
429
430 nid = page_to_nid(pfn_to_page(pfn));
431 if (node_present_pages(nid) == 0)
432 arg.status_change_nid = nid;
433
434 ret = memory_notify(MEM_GOING_ONLINE, &arg);
435 ret = notifier_to_errno(ret);
436 if (ret) {
437 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900438 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700439 return ret;
440 }
Dave Hansen3947be12005-10-29 18:16:54 -0700441 /*
442 * This doesn't need a lock to do pfn_to_page().
443 * The section can't be removed here because of the
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800444 * memory_block->state_mutex.
Dave Hansen3947be12005-10-29 18:16:54 -0700445 */
446 zone = page_zone(pfn_to_page(pfn));
Yasunori Goto68113782006-06-23 02:03:11 -0700447 /*
448 * If this zone is not populated, then it is not in zonelist.
449 * This means the page allocator ignores this zone.
450 * So, zonelist must be updated after online.
451 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700452 mutex_lock(&zonelists_mutex);
Yasunori Goto68113782006-06-23 02:03:11 -0700453 if (!populated_zone(zone))
454 need_zonelists_rebuild = 1;
455
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700456 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700457 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700458 if (ret) {
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700459 mutex_unlock(&zonelists_mutex);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700460 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
461 nr_pages, pfn);
462 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900463 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700464 return ret;
465 }
466
Dave Hansen3947be12005-10-29 18:16:54 -0700467 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800468 zone->zone_pgdat->node_present_pages += onlined_pages;
Jack Cheungaa7e9de2012-01-19 17:06:04 -0800469 drain_all_pages();
Haicheng Li1f522502010-05-24 14:32:51 -0700470 if (need_zonelists_rebuild)
471 build_all_zonelists(zone);
472 else
473 zone_pcp_update(zone);
Dave Hansen3947be12005-10-29 18:16:54 -0700474
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700475 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700476
477 init_per_zone_wmark_min();
478
Christoph Lameter7ea15302007-10-16 01:25:29 -0700479 if (onlined_pages) {
480 kswapd_run(zone_to_nid(zone));
481 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
482 }
Dave Hansen61b13992005-10-29 18:16:56 -0700483
Haicheng Li1f522502010-05-24 14:32:51 -0700484 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700485
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700486 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700487
488 if (onlined_pages)
489 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900490 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700491
Dave Hansen3947be12005-10-29 18:16:54 -0700492 return 0;
493}
Keith Mannthey53947022006-09-30 23:27:08 -0700494#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700495
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800496/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
497static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700498{
499 struct pglist_data *pgdat;
500 unsigned long zones_size[MAX_NR_ZONES] = {0};
501 unsigned long zholes_size[MAX_NR_ZONES] = {0};
502 unsigned long start_pfn = start >> PAGE_SHIFT;
503
504 pgdat = arch_alloc_nodedata(nid);
505 if (!pgdat)
506 return NULL;
507
508 arch_refresh_nodedata(nid, pgdat);
509
510 /* we can use NODE_DATA(nid) from here */
511
512 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700513 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700514
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700515 /*
516 * The node we allocated has no zone fallback lists. For avoiding
517 * to access not-initialized zonelist, build here.
518 */
David Rientjesf957db42011-06-22 18:13:04 -0700519 mutex_lock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700520 build_all_zonelists(NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700521 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700522
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700523 return pgdat;
524}
525
526static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
527{
528 arch_refresh_nodedata(nid, NULL);
529 arch_free_nodedata(pgdat);
530 return;
531}
532
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700533
minskey guocf234222010-05-24 14:32:41 -0700534/*
535 * called by cpu_up() to online a node without onlined memory.
536 */
537int mem_online_node(int nid)
538{
539 pg_data_t *pgdat;
540 int ret;
541
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800542 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700543 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700544 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700545 ret = -ENOMEM;
546 goto out;
547 }
548 node_set_online(nid);
549 ret = register_one_node(nid);
550 BUG_ON(ret);
551
552out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800553 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700554 return ret;
555}
556
Al Viro31168482008-11-22 17:33:24 +0000557/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
558int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700559{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700560 pg_data_t *pgdat = NULL;
561 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700562 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700563 int ret;
564
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800565 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800566
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700567 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800568 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700569 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800570 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700571
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700572 if (!node_online(nid)) {
573 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800574 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700575 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800576 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700577 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700578 }
579
Yasunori Gotobc02af92006-06-27 02:53:30 -0700580 /* call arch's memory hotadd */
581 ret = arch_add_memory(nid, start, size);
582
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700583 if (ret < 0)
584 goto error;
585
Yasunori Goto0fc44152006-06-27 02:53:38 -0700586 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700587 node_set_online(nid);
588
Yasunori Goto0fc44152006-06-27 02:53:38 -0700589 if (new_pgdat) {
590 ret = register_one_node(nid);
591 /*
592 * If sysfs file of new node can't create, cpu on the node
593 * can't be hot-added. There is no rollback way now.
594 * So, check by BUG_ON() to catch it reluctantly..
595 */
596 BUG_ON(ret);
597 }
598
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800599 /* create new memmap entry */
600 firmware_map_add_hotplug(start, start + size, "System RAM");
601
Andi Kleen6ad696d2009-11-17 14:06:22 -0800602 goto out;
603
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700604error:
605 /* rollback pgdat allocation and others */
606 if (new_pgdat)
607 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700608 if (res)
609 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700610
Andi Kleen6ad696d2009-11-17 14:06:22 -0800611out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800612 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700613 return ret;
614}
615EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617int __ref physical_remove_memory(u64 start, u64 size)
618{
619 int ret;
620 struct resource *res, *res_old;
621 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
622 BUG_ON(!res);
623
624 ret = arch_physical_remove_memory(start, size);
Larry Bassel24217172011-10-14 10:36:45 -0700625 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626 kfree(res);
Larry Bassel24217172011-10-14 10:36:45 -0700627 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 }
629
630 res->name = "System RAM";
631 res->start = start;
632 res->end = start + size - 1;
633 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
634
635 res_old = locate_resource(&iomem_resource, res);
Larry Bassel24217172011-10-14 10:36:45 -0700636 if (res_old) {
637 release_resource(res_old);
638 if (PageSlab(virt_to_head_page(res_old)))
639 kfree(res_old);
640 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 kfree(res);
642
643 return ret;
644}
645EXPORT_SYMBOL_GPL(physical_remove_memory);
646
647int __ref physical_active_memory(u64 start, u64 size)
648{
649 int ret;
650
651 ret = arch_physical_active_memory(start, size);
652 return ret;
653}
654EXPORT_SYMBOL_GPL(physical_active_memory);
655
656int __ref physical_low_power_memory(u64 start, u64 size)
657{
658 int ret;
659
660 ret = arch_physical_low_power_memory(start, size);
661 return ret;
662}
663EXPORT_SYMBOL_GPL(physical_low_power_memory);
664
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700665#ifdef CONFIG_MEMORY_HOTREMOVE
666/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700667 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
668 * set and the size of the free page is given by page_order(). Using this,
669 * the function determines if the pageblock contains only free pages.
670 * Due to buddy contraints, a free page at least the size of a pageblock will
671 * be located at the start of the pageblock
672 */
673static inline int pageblock_free(struct page *page)
674{
675 return PageBuddy(page) && page_order(page) >= pageblock_order;
676}
677
678/* Return the start of the next active pageblock after a given page */
679static struct page *next_active_pageblock(struct page *page)
680{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700681 /* Ensure the starting page is pageblock-aligned */
682 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
683
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700684 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700685 if (pageblock_free(page)) {
686 int order;
687 /* be careful. we don't have locks, page_order can be changed.*/
688 order = page_order(page);
689 if ((order < MAX_ORDER) && (order >= pageblock_order))
690 return page + (1 << order);
691 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700692
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700693 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700694}
695
696/* Checks if this range of memory is likely to be hot-removable. */
697int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
698{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700699 struct page *page = pfn_to_page(start_pfn);
700 struct page *end_page = page + nr_pages;
701
702 /* Check the starting page of each pageblock within the range */
703 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700704 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700705 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700706 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700707 }
708
709 /* All pageblocks in the memory block are likely to be hot-removable */
710 return 1;
711}
712
713/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700714 * Confirm all pages in a range [start, end) is belongs to the same zone.
715 */
716static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
717{
718 unsigned long pfn;
719 struct zone *zone = NULL;
720 struct page *page;
721 int i;
722 for (pfn = start_pfn;
723 pfn < end_pfn;
724 pfn += MAX_ORDER_NR_PAGES) {
725 i = 0;
726 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
727 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
728 i++;
729 if (i == MAX_ORDER_NR_PAGES)
730 continue;
731 page = pfn_to_page(pfn + i);
732 if (zone && page_zone(page) != zone)
733 return 0;
734 zone = page_zone(page);
735 }
736 return 1;
737}
738
739/*
740 * Scanning pfn is much easier than scanning lru list.
741 * Scan pfn from start to end and Find LRU page.
742 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700743static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700744{
745 unsigned long pfn;
746 struct page *page;
747 for (pfn = start; pfn < end; pfn++) {
748 if (pfn_valid(pfn)) {
749 page = pfn_to_page(pfn);
750 if (PageLRU(page))
751 return pfn;
752 }
753 }
754 return 0;
755}
756
757static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800758hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700759{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800760 /* This should be improooooved!! */
Jack Cheung1e6ec3c2011-11-21 16:44:05 -0800761 return alloc_page(GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN |
762 __GFP_NOMEMALLOC);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700763}
764
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700765#define NR_OFFLINE_AT_ONCE_PAGES (256)
766static int
767do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
768{
769 unsigned long pfn;
770 struct page *page;
771 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
772 int not_managed = 0;
773 int ret = 0;
774 LIST_HEAD(source);
775
776 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
777 if (!pfn_valid(pfn))
778 continue;
779 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700780 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700781 continue;
782 /*
783 * We can skip free pages. And we can only deal with pages on
784 * LRU.
785 */
Nick Piggin62695a82008-10-18 20:26:09 -0700786 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700787 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700788 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -0700789 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700790 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800791 inc_zone_page_state(page, NR_ISOLATED_ANON +
792 page_is_file_cache(page));
793
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700794 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700795#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -0800796 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
797 pfn);
798 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700799#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700800 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -0700802 check this again here. */
803 if (page_count(page)) {
804 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -0700805 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -0700806 break;
807 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700808 }
809 }
Bob Liuf3ab2632010-10-26 14:22:10 -0700810 if (!list_empty(&source)) {
811 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700812 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -0700813 goto out;
814 }
815 /* this function returns # of failed pages */
Mel Gorman77f1fe62011-01-13 15:45:57 -0800816 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800817 true, true);
Bob Liuf3ab2632010-10-26 14:22:10 -0700818 if (ret)
819 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700820 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700821out:
822 return ret;
823}
824
825/*
826 * remove from free_area[] and mark all as Reserved.
827 */
828static int
829offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
830 void *data)
831{
832 __offline_isolated_pages(start, start + nr_pages);
833 return 0;
834}
835
836static void
837offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
838{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700839 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700840 offline_isolated_pages_cb);
841}
842
843/*
844 * Check all pages in range, recoreded as memory resource, are isolated.
845 */
846static int
847check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
848 void *data)
849{
850 int ret;
851 long offlined = *(long *)data;
852 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
853 offlined = nr_pages;
854 if (!ret)
855 *(long *)data += offlined;
856 return ret;
857}
858
859static long
860check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
861{
862 long offlined = 0;
863 int ret;
864
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700865 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700866 check_pages_isolated_cb);
867 if (ret < 0)
868 offlined = (long)ret;
869 return offlined;
870}
871
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700872static int __ref offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700873 unsigned long end_pfn, unsigned long timeout)
874{
875 unsigned long pfn, nr_pages, expire;
876 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700877 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700878 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700879 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700880
881 BUG_ON(start_pfn >= end_pfn);
882 /* at least, alignment against pageblock is necessary */
883 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
884 return -EINVAL;
885 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
886 return -EINVAL;
887 /* This makes hotplug much easier...and readable.
888 we assume this for now. .*/
889 if (!test_pages_in_a_zone(start_pfn, end_pfn))
890 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700891
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800892 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800893
Yasunori Goto7b78d332007-10-21 16:41:36 -0700894 zone = page_zone(pfn_to_page(start_pfn));
895 node = zone_to_nid(zone);
896 nr_pages = end_pfn - start_pfn;
897
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700898 /* set above range as isolated */
899 ret = start_isolate_page_range(start_pfn, end_pfn);
900 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800901 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700902
903 arg.start_pfn = start_pfn;
904 arg.nr_pages = nr_pages;
905 arg.status_change_nid = -1;
906 if (nr_pages >= node_present_pages(node))
907 arg.status_change_nid = node;
908
909 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
910 ret = notifier_to_errno(ret);
911 if (ret)
912 goto failed_removal;
913
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700914 pfn = start_pfn;
915 expire = jiffies + timeout;
916 drain = 0;
917 retry_max = 5;
918repeat:
919 /* start memory hot removal */
920 ret = -EAGAIN;
921 if (time_after(jiffies, expire))
922 goto failed_removal;
923 ret = -EINTR;
924 if (signal_pending(current))
925 goto failed_removal;
926 ret = 0;
927 if (drain) {
928 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700929 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800930 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700931 }
932
933 pfn = scan_lru_pages(start_pfn, end_pfn);
934 if (pfn) { /* We have page on LRU */
935 ret = do_migrate_range(pfn, end_pfn);
936 if (!ret) {
937 drain = 1;
938 goto repeat;
939 } else {
940 if (ret < 0)
941 if (--retry_max == 0)
942 goto failed_removal;
943 yield();
944 drain = 1;
945 goto repeat;
946 }
947 }
948 /* drain all zone's lru pagevec, this is asyncronous... */
949 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700950 yield();
951 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800952 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700953 /* check again */
954 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
955 if (offlined_pages < 0) {
956 ret = -EBUSY;
957 goto failed_removal;
958 }
959 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
960 /* Ok, all of our target is islaoted.
961 We cannot do rollback at this point. */
962 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800963 /* reset pagetype flags and makes migrate type to be MOVABLE */
964 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700965 /* removal success */
Jack Cheung80c201e2011-11-29 16:53:34 -0800966 if (offlined_pages > zone->present_pages)
967 zone->present_pages = 0;
968 else
969 zone->present_pages -= offlined_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700970 zone->zone_pgdat->node_present_pages -= offlined_pages;
971 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700972
Jack Cheung59f9f1c2011-11-29 16:52:49 -0800973#ifdef CONFIG_FIX_MOVABLE_ZONE
974 if (zone_idx(zone) != ZONE_MOVABLE)
975 total_unmovable_pages -= offlined_pages;
976#endif
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700977 init_per_zone_wmark_min();
978
David Rientjes8fe23e02009-12-14 17:58:33 -0800979 if (!node_present_pages(node)) {
980 node_clear_state(node, N_HIGH_MEMORY);
981 kswapd_stop(node);
982 }
Minchan Kimbce73942009-06-16 15:32:50 -0700983
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700984 vm_total_pages = nr_free_pagecache_pages();
985 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700986
987 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800988 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700989 return 0;
990
991failed_removal:
992 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
993 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700994 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700995 /* pushback to free area */
996 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700997
Andi Kleen6ad696d2009-11-17 14:06:22 -0800998out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800999 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001000 return ret;
1001}
Badari Pulavarty71088782008-10-18 20:25:58 -07001002
1003int remove_memory(u64 start, u64 size)
1004{
1005 unsigned long start_pfn, end_pfn;
1006
1007 start_pfn = PFN_DOWN(start);
1008 end_pfn = start_pfn + PFN_DOWN(size);
1009 return offline_pages(start_pfn, end_pfn, 120 * HZ);
1010}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001012#else
1013int remove_memory(u64 start, u64 size)
1014{
1015 return -EINVAL;
1016}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001017#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001018EXPORT_SYMBOL_GPL(remove_memory);