blob: 4a737443f84db155dadf3dbfb756e936aff4f425 [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++;
Jan Beulich4738e1b2009-09-21 17:03:03 -0700382 if (pfn >= num_physpages)
383 num_physpages = pfn + 1;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700384
385#ifdef CONFIG_HIGHMEM
386 if (PageHighMem(page))
387 totalhigh_pages++;
388#endif
389
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700390 ClearPageReserved(page);
391 init_page_count(page);
392 __free_page(page);
393}
394
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700395static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
396 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700397{
398 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700399 unsigned long onlined_pages = *(unsigned long *)arg;
400 struct page *page;
401 if (PageReserved(pfn_to_page(start_pfn)))
402 for (i = 0; i < nr_pages; i++) {
403 page = pfn_to_page(start_pfn + i);
404 online_page(page);
405 onlined_pages++;
406 }
407 *(unsigned long *)arg = onlined_pages;
408 return 0;
409}
410
411
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700412int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700413{
Dave Hansen3947be12005-10-29 18:16:54 -0700414 unsigned long onlined_pages = 0;
415 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700416 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700417 int nid;
418 int ret;
419 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700420
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900421 lock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700422 arg.start_pfn = pfn;
423 arg.nr_pages = nr_pages;
424 arg.status_change_nid = -1;
425
426 nid = page_to_nid(pfn_to_page(pfn));
427 if (node_present_pages(nid) == 0)
428 arg.status_change_nid = nid;
429
430 ret = memory_notify(MEM_GOING_ONLINE, &arg);
431 ret = notifier_to_errno(ret);
432 if (ret) {
433 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900434 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700435 return ret;
436 }
Dave Hansen3947be12005-10-29 18:16:54 -0700437 /*
438 * This doesn't need a lock to do pfn_to_page().
439 * The section can't be removed here because of the
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800440 * memory_block->state_mutex.
Dave Hansen3947be12005-10-29 18:16:54 -0700441 */
442 zone = page_zone(pfn_to_page(pfn));
Yasunori Goto68113782006-06-23 02:03:11 -0700443 /*
444 * If this zone is not populated, then it is not in zonelist.
445 * This means the page allocator ignores this zone.
446 * So, zonelist must be updated after online.
447 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700448 mutex_lock(&zonelists_mutex);
Yasunori Goto68113782006-06-23 02:03:11 -0700449 if (!populated_zone(zone))
450 need_zonelists_rebuild = 1;
451
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700452 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700453 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700454 if (ret) {
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700455 mutex_unlock(&zonelists_mutex);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700456 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
457 nr_pages, pfn);
458 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900459 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700460 return ret;
461 }
462
Dave Hansen3947be12005-10-29 18:16:54 -0700463 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800464 zone->zone_pgdat->node_present_pages += onlined_pages;
Haicheng Li1f522502010-05-24 14:32:51 -0700465 if (need_zonelists_rebuild)
466 build_all_zonelists(zone);
467 else
468 zone_pcp_update(zone);
Dave Hansen3947be12005-10-29 18:16:54 -0700469
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700470 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700471
472 init_per_zone_wmark_min();
473
Christoph Lameter7ea15302007-10-16 01:25:29 -0700474 if (onlined_pages) {
475 kswapd_run(zone_to_nid(zone));
476 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
477 }
Dave Hansen61b13992005-10-29 18:16:56 -0700478
Haicheng Li1f522502010-05-24 14:32:51 -0700479 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700480
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700481 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700482
483 if (onlined_pages)
484 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900485 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700486
Dave Hansen3947be12005-10-29 18:16:54 -0700487 return 0;
488}
Keith Mannthey53947022006-09-30 23:27:08 -0700489#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700490
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800491/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
492static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700493{
494 struct pglist_data *pgdat;
495 unsigned long zones_size[MAX_NR_ZONES] = {0};
496 unsigned long zholes_size[MAX_NR_ZONES] = {0};
497 unsigned long start_pfn = start >> PAGE_SHIFT;
498
499 pgdat = arch_alloc_nodedata(nid);
500 if (!pgdat)
501 return NULL;
502
503 arch_refresh_nodedata(nid, pgdat);
504
505 /* we can use NODE_DATA(nid) from here */
506
507 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700508 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700509
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700510 /*
511 * The node we allocated has no zone fallback lists. For avoiding
512 * to access not-initialized zonelist, build here.
513 */
David Rientjesf957db42011-06-22 18:13:04 -0700514 mutex_lock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700515 build_all_zonelists(NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700516 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700517
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700518 return pgdat;
519}
520
521static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
522{
523 arch_refresh_nodedata(nid, NULL);
524 arch_free_nodedata(pgdat);
525 return;
526}
527
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700528
minskey guocf234222010-05-24 14:32:41 -0700529/*
530 * called by cpu_up() to online a node without onlined memory.
531 */
532int mem_online_node(int nid)
533{
534 pg_data_t *pgdat;
535 int ret;
536
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800537 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700538 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700539 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700540 ret = -ENOMEM;
541 goto out;
542 }
543 node_set_online(nid);
544 ret = register_one_node(nid);
545 BUG_ON(ret);
546
547out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800548 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700549 return ret;
550}
551
Al Viro31168482008-11-22 17:33:24 +0000552/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
553int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700554{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700555 pg_data_t *pgdat = NULL;
556 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700557 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700558 int ret;
559
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800560 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800561
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700562 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800563 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700564 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800565 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700566
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700567 if (!node_online(nid)) {
568 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800569 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700570 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800571 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700572 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700573 }
574
Yasunori Gotobc02af92006-06-27 02:53:30 -0700575 /* call arch's memory hotadd */
576 ret = arch_add_memory(nid, start, size);
577
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700578 if (ret < 0)
579 goto error;
580
Yasunori Goto0fc44152006-06-27 02:53:38 -0700581 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700582 node_set_online(nid);
583
Yasunori Goto0fc44152006-06-27 02:53:38 -0700584 if (new_pgdat) {
585 ret = register_one_node(nid);
586 /*
587 * If sysfs file of new node can't create, cpu on the node
588 * can't be hot-added. There is no rollback way now.
589 * So, check by BUG_ON() to catch it reluctantly..
590 */
591 BUG_ON(ret);
592 }
593
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800594 /* create new memmap entry */
595 firmware_map_add_hotplug(start, start + size, "System RAM");
596
Andi Kleen6ad696d2009-11-17 14:06:22 -0800597 goto out;
598
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700599error:
600 /* rollback pgdat allocation and others */
601 if (new_pgdat)
602 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700603 if (res)
604 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700605
Andi Kleen6ad696d2009-11-17 14:06:22 -0800606out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800607 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700608 return ret;
609}
610EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612int __ref physical_remove_memory(u64 start, u64 size)
613{
614 int ret;
615 struct resource *res, *res_old;
616 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
617 BUG_ON(!res);
618
619 ret = arch_physical_remove_memory(start, size);
620 if (ret) {
621 kfree(res);
622 return ret;
623 }
624
625 res->name = "System RAM";
626 res->start = start;
627 res->end = start + size - 1;
628 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
629
630 res_old = locate_resource(&iomem_resource, res);
631 if (res_old)
632 release_memory_resource(res_old);
633 kfree(res);
634
635 return ret;
636}
637EXPORT_SYMBOL_GPL(physical_remove_memory);
638
639int __ref physical_active_memory(u64 start, u64 size)
640{
641 int ret;
642
643 ret = arch_physical_active_memory(start, size);
644 return ret;
645}
646EXPORT_SYMBOL_GPL(physical_active_memory);
647
648int __ref physical_low_power_memory(u64 start, u64 size)
649{
650 int ret;
651
652 ret = arch_physical_low_power_memory(start, size);
653 return ret;
654}
655EXPORT_SYMBOL_GPL(physical_low_power_memory);
656
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700657#ifdef CONFIG_MEMORY_HOTREMOVE
658/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700659 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
660 * set and the size of the free page is given by page_order(). Using this,
661 * the function determines if the pageblock contains only free pages.
662 * Due to buddy contraints, a free page at least the size of a pageblock will
663 * be located at the start of the pageblock
664 */
665static inline int pageblock_free(struct page *page)
666{
667 return PageBuddy(page) && page_order(page) >= pageblock_order;
668}
669
670/* Return the start of the next active pageblock after a given page */
671static struct page *next_active_pageblock(struct page *page)
672{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700673 /* Ensure the starting page is pageblock-aligned */
674 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
675
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700676 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700677 if (pageblock_free(page)) {
678 int order;
679 /* be careful. we don't have locks, page_order can be changed.*/
680 order = page_order(page);
681 if ((order < MAX_ORDER) && (order >= pageblock_order))
682 return page + (1 << order);
683 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700684
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700685 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700686}
687
688/* Checks if this range of memory is likely to be hot-removable. */
689int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
690{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700691 struct page *page = pfn_to_page(start_pfn);
692 struct page *end_page = page + nr_pages;
693
694 /* Check the starting page of each pageblock within the range */
695 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700696 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700697 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700698 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700699 }
700
701 /* All pageblocks in the memory block are likely to be hot-removable */
702 return 1;
703}
704
705/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700706 * Confirm all pages in a range [start, end) is belongs to the same zone.
707 */
708static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
709{
710 unsigned long pfn;
711 struct zone *zone = NULL;
712 struct page *page;
713 int i;
714 for (pfn = start_pfn;
715 pfn < end_pfn;
716 pfn += MAX_ORDER_NR_PAGES) {
717 i = 0;
718 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
719 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
720 i++;
721 if (i == MAX_ORDER_NR_PAGES)
722 continue;
723 page = pfn_to_page(pfn + i);
724 if (zone && page_zone(page) != zone)
725 return 0;
726 zone = page_zone(page);
727 }
728 return 1;
729}
730
731/*
732 * Scanning pfn is much easier than scanning lru list.
733 * Scan pfn from start to end and Find LRU page.
734 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700735static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700736{
737 unsigned long pfn;
738 struct page *page;
739 for (pfn = start; pfn < end; pfn++) {
740 if (pfn_valid(pfn)) {
741 page = pfn_to_page(pfn);
742 if (PageLRU(page))
743 return pfn;
744 }
745 }
746 return 0;
747}
748
749static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800750hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700751{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800752 /* This should be improooooved!! */
753 return alloc_page(GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700754}
755
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700756#define NR_OFFLINE_AT_ONCE_PAGES (256)
757static int
758do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
759{
760 unsigned long pfn;
761 struct page *page;
762 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
763 int not_managed = 0;
764 int ret = 0;
765 LIST_HEAD(source);
766
767 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
768 if (!pfn_valid(pfn))
769 continue;
770 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700771 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700772 continue;
773 /*
774 * We can skip free pages. And we can only deal with pages on
775 * LRU.
776 */
Nick Piggin62695a82008-10-18 20:26:09 -0700777 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700778 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700779 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -0700780 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700781 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800782 inc_zone_page_state(page, NR_ISOLATED_ANON +
783 page_is_file_cache(page));
784
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700785 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700786#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -0800787 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
788 pfn);
789 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700790#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700791 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300792 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -0700793 check this again here. */
794 if (page_count(page)) {
795 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -0700796 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -0700797 break;
798 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700799 }
800 }
Bob Liuf3ab2632010-10-26 14:22:10 -0700801 if (!list_empty(&source)) {
802 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700803 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -0700804 goto out;
805 }
806 /* this function returns # of failed pages */
Mel Gorman77f1fe62011-01-13 15:45:57 -0800807 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800808 true, true);
Bob Liuf3ab2632010-10-26 14:22:10 -0700809 if (ret)
810 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700811 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700812out:
813 return ret;
814}
815
816/*
817 * remove from free_area[] and mark all as Reserved.
818 */
819static int
820offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
821 void *data)
822{
823 __offline_isolated_pages(start, start + nr_pages);
824 return 0;
825}
826
827static void
828offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
829{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700830 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700831 offline_isolated_pages_cb);
832}
833
834/*
835 * Check all pages in range, recoreded as memory resource, are isolated.
836 */
837static int
838check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
839 void *data)
840{
841 int ret;
842 long offlined = *(long *)data;
843 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
844 offlined = nr_pages;
845 if (!ret)
846 *(long *)data += offlined;
847 return ret;
848}
849
850static long
851check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
852{
853 long offlined = 0;
854 int ret;
855
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700856 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700857 check_pages_isolated_cb);
858 if (ret < 0)
859 offlined = (long)ret;
860 return offlined;
861}
862
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700863static int __ref offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700864 unsigned long end_pfn, unsigned long timeout)
865{
866 unsigned long pfn, nr_pages, expire;
867 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700868 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700869 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700870 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700871
872 BUG_ON(start_pfn >= end_pfn);
873 /* at least, alignment against pageblock is necessary */
874 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
875 return -EINVAL;
876 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
877 return -EINVAL;
878 /* This makes hotplug much easier...and readable.
879 we assume this for now. .*/
880 if (!test_pages_in_a_zone(start_pfn, end_pfn))
881 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700882
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800883 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800884
Yasunori Goto7b78d332007-10-21 16:41:36 -0700885 zone = page_zone(pfn_to_page(start_pfn));
886 node = zone_to_nid(zone);
887 nr_pages = end_pfn - start_pfn;
888
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700889 /* set above range as isolated */
890 ret = start_isolate_page_range(start_pfn, end_pfn);
891 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800892 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700893
894 arg.start_pfn = start_pfn;
895 arg.nr_pages = nr_pages;
896 arg.status_change_nid = -1;
897 if (nr_pages >= node_present_pages(node))
898 arg.status_change_nid = node;
899
900 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
901 ret = notifier_to_errno(ret);
902 if (ret)
903 goto failed_removal;
904
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700905 pfn = start_pfn;
906 expire = jiffies + timeout;
907 drain = 0;
908 retry_max = 5;
909repeat:
910 /* start memory hot removal */
911 ret = -EAGAIN;
912 if (time_after(jiffies, expire))
913 goto failed_removal;
914 ret = -EINTR;
915 if (signal_pending(current))
916 goto failed_removal;
917 ret = 0;
918 if (drain) {
919 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700920 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800921 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700922 }
923
924 pfn = scan_lru_pages(start_pfn, end_pfn);
925 if (pfn) { /* We have page on LRU */
926 ret = do_migrate_range(pfn, end_pfn);
927 if (!ret) {
928 drain = 1;
929 goto repeat;
930 } else {
931 if (ret < 0)
932 if (--retry_max == 0)
933 goto failed_removal;
934 yield();
935 drain = 1;
936 goto repeat;
937 }
938 }
939 /* drain all zone's lru pagevec, this is asyncronous... */
940 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700941 yield();
942 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800943 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700944 /* check again */
945 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
946 if (offlined_pages < 0) {
947 ret = -EBUSY;
948 goto failed_removal;
949 }
950 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
951 /* Ok, all of our target is islaoted.
952 We cannot do rollback at this point. */
953 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800954 /* reset pagetype flags and makes migrate type to be MOVABLE */
955 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700956 /* removal success */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700957 zone->present_pages -= offlined_pages;
958 zone->zone_pgdat->node_present_pages -= offlined_pages;
959 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700960
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700961 init_per_zone_wmark_min();
962
David Rientjes8fe23e02009-12-14 17:58:33 -0800963 if (!node_present_pages(node)) {
964 node_clear_state(node, N_HIGH_MEMORY);
965 kswapd_stop(node);
966 }
Minchan Kimbce73942009-06-16 15:32:50 -0700967
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700968 vm_total_pages = nr_free_pagecache_pages();
969 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700970
971 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800972 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700973 return 0;
974
975failed_removal:
976 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
977 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700978 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700979 /* pushback to free area */
980 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700981
Andi Kleen6ad696d2009-11-17 14:06:22 -0800982out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800983 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700984 return ret;
985}
Badari Pulavarty71088782008-10-18 20:25:58 -0700986
987int remove_memory(u64 start, u64 size)
988{
989 unsigned long start_pfn, end_pfn;
990
991 start_pfn = PFN_DOWN(start);
992 end_pfn = start_pfn + PFN_DOWN(size);
993 return offline_pages(start_pfn, end_pfn, 120 * HZ);
994}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -0700996#else
997int remove_memory(u64 start, u64 size)
998{
999 return -EINVAL;
1000}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001001#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001002EXPORT_SYMBOL_GPL(remove_memory);