blob: 70ea0dfc3a5bda1d1d02651e61d46b571f724c9e [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;
Haicheng Li1f522502010-05-24 14:32:51 -0700469 if (need_zonelists_rebuild)
470 build_all_zonelists(zone);
471 else
472 zone_pcp_update(zone);
Dave Hansen3947be12005-10-29 18:16:54 -0700473
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700474 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700475
476 init_per_zone_wmark_min();
477
Christoph Lameter7ea15302007-10-16 01:25:29 -0700478 if (onlined_pages) {
479 kswapd_run(zone_to_nid(zone));
480 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
481 }
Dave Hansen61b13992005-10-29 18:16:56 -0700482
Haicheng Li1f522502010-05-24 14:32:51 -0700483 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700484
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700485 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700486
487 if (onlined_pages)
488 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900489 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700490
Dave Hansen3947be12005-10-29 18:16:54 -0700491 return 0;
492}
Keith Mannthey53947022006-09-30 23:27:08 -0700493#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700494
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800495/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
496static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700497{
498 struct pglist_data *pgdat;
499 unsigned long zones_size[MAX_NR_ZONES] = {0};
500 unsigned long zholes_size[MAX_NR_ZONES] = {0};
501 unsigned long start_pfn = start >> PAGE_SHIFT;
502
503 pgdat = arch_alloc_nodedata(nid);
504 if (!pgdat)
505 return NULL;
506
507 arch_refresh_nodedata(nid, pgdat);
508
509 /* we can use NODE_DATA(nid) from here */
510
511 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700512 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700513
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700514 /*
515 * The node we allocated has no zone fallback lists. For avoiding
516 * to access not-initialized zonelist, build here.
517 */
David Rientjesf957db42011-06-22 18:13:04 -0700518 mutex_lock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700519 build_all_zonelists(NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700520 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700521
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700522 return pgdat;
523}
524
525static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
526{
527 arch_refresh_nodedata(nid, NULL);
528 arch_free_nodedata(pgdat);
529 return;
530}
531
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700532
minskey guocf234222010-05-24 14:32:41 -0700533/*
534 * called by cpu_up() to online a node without onlined memory.
535 */
536int mem_online_node(int nid)
537{
538 pg_data_t *pgdat;
539 int ret;
540
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800541 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700542 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700543 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700544 ret = -ENOMEM;
545 goto out;
546 }
547 node_set_online(nid);
548 ret = register_one_node(nid);
549 BUG_ON(ret);
550
551out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800552 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700553 return ret;
554}
555
Al Viro31168482008-11-22 17:33:24 +0000556/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
557int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700558{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700559 pg_data_t *pgdat = NULL;
560 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700561 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700562 int ret;
563
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800564 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800565
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700566 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800567 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700568 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800569 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700570
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700571 if (!node_online(nid)) {
572 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800573 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700574 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800575 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700576 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700577 }
578
Yasunori Gotobc02af92006-06-27 02:53:30 -0700579 /* call arch's memory hotadd */
580 ret = arch_add_memory(nid, start, size);
581
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700582 if (ret < 0)
583 goto error;
584
Yasunori Goto0fc44152006-06-27 02:53:38 -0700585 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700586 node_set_online(nid);
587
Yasunori Goto0fc44152006-06-27 02:53:38 -0700588 if (new_pgdat) {
589 ret = register_one_node(nid);
590 /*
591 * If sysfs file of new node can't create, cpu on the node
592 * can't be hot-added. There is no rollback way now.
593 * So, check by BUG_ON() to catch it reluctantly..
594 */
595 BUG_ON(ret);
596 }
597
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800598 /* create new memmap entry */
599 firmware_map_add_hotplug(start, start + size, "System RAM");
600
Andi Kleen6ad696d2009-11-17 14:06:22 -0800601 goto out;
602
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700603error:
604 /* rollback pgdat allocation and others */
605 if (new_pgdat)
606 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700607 if (res)
608 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700609
Andi Kleen6ad696d2009-11-17 14:06:22 -0800610out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800611 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700612 return ret;
613}
614EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700615
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616int __ref physical_remove_memory(u64 start, u64 size)
617{
618 int ret;
619 struct resource *res, *res_old;
620 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
621 BUG_ON(!res);
622
623 ret = arch_physical_remove_memory(start, size);
Larry Bassel24217172011-10-14 10:36:45 -0700624 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 kfree(res);
Larry Bassel24217172011-10-14 10:36:45 -0700626 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 }
628
629 res->name = "System RAM";
630 res->start = start;
631 res->end = start + size - 1;
632 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
633
634 res_old = locate_resource(&iomem_resource, res);
Larry Bassel24217172011-10-14 10:36:45 -0700635 if (res_old) {
636 release_resource(res_old);
637 if (PageSlab(virt_to_head_page(res_old)))
638 kfree(res_old);
639 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 kfree(res);
641
642 return ret;
643}
644EXPORT_SYMBOL_GPL(physical_remove_memory);
645
646int __ref physical_active_memory(u64 start, u64 size)
647{
648 int ret;
649
650 ret = arch_physical_active_memory(start, size);
651 return ret;
652}
653EXPORT_SYMBOL_GPL(physical_active_memory);
654
655int __ref physical_low_power_memory(u64 start, u64 size)
656{
657 int ret;
658
659 ret = arch_physical_low_power_memory(start, size);
660 return ret;
661}
662EXPORT_SYMBOL_GPL(physical_low_power_memory);
663
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700664#ifdef CONFIG_MEMORY_HOTREMOVE
665/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700666 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
667 * set and the size of the free page is given by page_order(). Using this,
668 * the function determines if the pageblock contains only free pages.
669 * Due to buddy contraints, a free page at least the size of a pageblock will
670 * be located at the start of the pageblock
671 */
672static inline int pageblock_free(struct page *page)
673{
674 return PageBuddy(page) && page_order(page) >= pageblock_order;
675}
676
677/* Return the start of the next active pageblock after a given page */
678static struct page *next_active_pageblock(struct page *page)
679{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700680 /* Ensure the starting page is pageblock-aligned */
681 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
682
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700683 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700684 if (pageblock_free(page)) {
685 int order;
686 /* be careful. we don't have locks, page_order can be changed.*/
687 order = page_order(page);
688 if ((order < MAX_ORDER) && (order >= pageblock_order))
689 return page + (1 << order);
690 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700691
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700692 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700693}
694
695/* Checks if this range of memory is likely to be hot-removable. */
696int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
697{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700698 struct page *page = pfn_to_page(start_pfn);
699 struct page *end_page = page + nr_pages;
700
701 /* Check the starting page of each pageblock within the range */
702 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700703 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700704 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700705 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700706 }
707
708 /* All pageblocks in the memory block are likely to be hot-removable */
709 return 1;
710}
711
712/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700713 * Confirm all pages in a range [start, end) is belongs to the same zone.
714 */
715static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
716{
717 unsigned long pfn;
718 struct zone *zone = NULL;
719 struct page *page;
720 int i;
721 for (pfn = start_pfn;
722 pfn < end_pfn;
723 pfn += MAX_ORDER_NR_PAGES) {
724 i = 0;
725 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
726 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
727 i++;
728 if (i == MAX_ORDER_NR_PAGES)
729 continue;
730 page = pfn_to_page(pfn + i);
731 if (zone && page_zone(page) != zone)
732 return 0;
733 zone = page_zone(page);
734 }
735 return 1;
736}
737
738/*
739 * Scanning pfn is much easier than scanning lru list.
740 * Scan pfn from start to end and Find LRU page.
741 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700742static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700743{
744 unsigned long pfn;
745 struct page *page;
746 for (pfn = start; pfn < end; pfn++) {
747 if (pfn_valid(pfn)) {
748 page = pfn_to_page(pfn);
749 if (PageLRU(page))
750 return pfn;
751 }
752 }
753 return 0;
754}
755
756static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800757hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700758{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800759 /* This should be improooooved!! */
Jack Cheung1e6ec3c2011-11-21 16:44:05 -0800760 return alloc_page(GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN |
761 __GFP_NOMEMALLOC);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700762}
763
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700764#define NR_OFFLINE_AT_ONCE_PAGES (256)
765static int
766do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
767{
768 unsigned long pfn;
769 struct page *page;
770 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
771 int not_managed = 0;
772 int ret = 0;
773 LIST_HEAD(source);
774
775 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
776 if (!pfn_valid(pfn))
777 continue;
778 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700779 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700780 continue;
781 /*
782 * We can skip free pages. And we can only deal with pages on
783 * LRU.
784 */
Nick Piggin62695a82008-10-18 20:26:09 -0700785 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700786 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700787 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -0700788 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700789 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800790 inc_zone_page_state(page, NR_ISOLATED_ANON +
791 page_is_file_cache(page));
792
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700793 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700794#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -0800795 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
796 pfn);
797 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700798#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700799 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300800 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -0700801 check this again here. */
802 if (page_count(page)) {
803 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -0700804 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -0700805 break;
806 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700807 }
808 }
Bob Liuf3ab2632010-10-26 14:22:10 -0700809 if (!list_empty(&source)) {
810 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700811 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -0700812 goto out;
813 }
814 /* this function returns # of failed pages */
Mel Gorman77f1fe62011-01-13 15:45:57 -0800815 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800816 true, true);
Bob Liuf3ab2632010-10-26 14:22:10 -0700817 if (ret)
818 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700819 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700820out:
821 return ret;
822}
823
824/*
825 * remove from free_area[] and mark all as Reserved.
826 */
827static int
828offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
829 void *data)
830{
831 __offline_isolated_pages(start, start + nr_pages);
832 return 0;
833}
834
835static void
836offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
837{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700838 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700839 offline_isolated_pages_cb);
840}
841
842/*
843 * Check all pages in range, recoreded as memory resource, are isolated.
844 */
845static int
846check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
847 void *data)
848{
849 int ret;
850 long offlined = *(long *)data;
851 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
852 offlined = nr_pages;
853 if (!ret)
854 *(long *)data += offlined;
855 return ret;
856}
857
858static long
859check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
860{
861 long offlined = 0;
862 int ret;
863
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700864 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700865 check_pages_isolated_cb);
866 if (ret < 0)
867 offlined = (long)ret;
868 return offlined;
869}
870
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700871static int __ref offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700872 unsigned long end_pfn, unsigned long timeout)
873{
874 unsigned long pfn, nr_pages, expire;
875 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700876 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700877 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700878 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700879
880 BUG_ON(start_pfn >= end_pfn);
881 /* at least, alignment against pageblock is necessary */
882 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
883 return -EINVAL;
884 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
885 return -EINVAL;
886 /* This makes hotplug much easier...and readable.
887 we assume this for now. .*/
888 if (!test_pages_in_a_zone(start_pfn, end_pfn))
889 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700890
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800891 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800892
Yasunori Goto7b78d332007-10-21 16:41:36 -0700893 zone = page_zone(pfn_to_page(start_pfn));
894 node = zone_to_nid(zone);
895 nr_pages = end_pfn - start_pfn;
896
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700897 /* set above range as isolated */
898 ret = start_isolate_page_range(start_pfn, end_pfn);
899 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800900 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700901
902 arg.start_pfn = start_pfn;
903 arg.nr_pages = nr_pages;
904 arg.status_change_nid = -1;
905 if (nr_pages >= node_present_pages(node))
906 arg.status_change_nid = node;
907
908 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
909 ret = notifier_to_errno(ret);
910 if (ret)
911 goto failed_removal;
912
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700913 pfn = start_pfn;
914 expire = jiffies + timeout;
915 drain = 0;
916 retry_max = 5;
917repeat:
918 /* start memory hot removal */
919 ret = -EAGAIN;
920 if (time_after(jiffies, expire))
921 goto failed_removal;
922 ret = -EINTR;
923 if (signal_pending(current))
924 goto failed_removal;
925 ret = 0;
926 if (drain) {
927 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700928 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800929 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700930 }
931
932 pfn = scan_lru_pages(start_pfn, end_pfn);
933 if (pfn) { /* We have page on LRU */
934 ret = do_migrate_range(pfn, end_pfn);
935 if (!ret) {
936 drain = 1;
937 goto repeat;
938 } else {
939 if (ret < 0)
940 if (--retry_max == 0)
941 goto failed_removal;
942 yield();
943 drain = 1;
944 goto repeat;
945 }
946 }
947 /* drain all zone's lru pagevec, this is asyncronous... */
948 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700949 yield();
950 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800951 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700952 /* check again */
953 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
954 if (offlined_pages < 0) {
955 ret = -EBUSY;
956 goto failed_removal;
957 }
958 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
959 /* Ok, all of our target is islaoted.
960 We cannot do rollback at this point. */
961 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800962 /* reset pagetype flags and makes migrate type to be MOVABLE */
963 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700964 /* removal success */
Jack Cheung80c201e2011-11-29 16:53:34 -0800965 if (offlined_pages > zone->present_pages)
966 zone->present_pages = 0;
967 else
968 zone->present_pages -= offlined_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700969 zone->zone_pgdat->node_present_pages -= offlined_pages;
970 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700971
Jack Cheung59f9f1c2011-11-29 16:52:49 -0800972#ifdef CONFIG_FIX_MOVABLE_ZONE
973 if (zone_idx(zone) != ZONE_MOVABLE)
974 total_unmovable_pages -= offlined_pages;
975#endif
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700976 init_per_zone_wmark_min();
977
David Rientjes8fe23e02009-12-14 17:58:33 -0800978 if (!node_present_pages(node)) {
979 node_clear_state(node, N_HIGH_MEMORY);
980 kswapd_stop(node);
981 }
Minchan Kimbce73942009-06-16 15:32:50 -0700982
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700983 vm_total_pages = nr_free_pagecache_pages();
984 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700985
986 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800987 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700988 return 0;
989
990failed_removal:
991 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
992 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700993 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700994 /* pushback to free area */
995 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700996
Andi Kleen6ad696d2009-11-17 14:06:22 -0800997out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800998 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700999 return ret;
1000}
Badari Pulavarty71088782008-10-18 20:25:58 -07001001
1002int remove_memory(u64 start, u64 size)
1003{
1004 unsigned long start_pfn, end_pfn;
1005
1006 start_pfn = PFN_DOWN(start);
1007 end_pfn = start_pfn + PFN_DOWN(size);
1008 return offline_pages(start_pfn, end_pfn, 120 * HZ);
1009}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001011#else
1012int remove_memory(u64 start, u64 size)
1013{
1014 return -EINVAL;
1015}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001016#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001017EXPORT_SYMBOL_GPL(remove_memory);