blob: fcb34b4e24ac6bb3c78b535c598539c896f73a5d [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#include <linux/stddef.h>
8#include <linux/mm.h>
9#include <linux/swap.h>
10#include <linux/interrupt.h>
11#include <linux/pagemap.h>
12#include <linux/bootmem.h>
13#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070016#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
21#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Dave Hansen3947be12005-10-29 18:16:54 -070032
33#include <asm/tlbflush.h>
34
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030035#include "internal.h"
36
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070037/*
38 * online_page_callback contains pointer to current page onlining function.
39 * Initially it is generic_online_page(). If it is required it could be
40 * changed by calling set_online_page_callback() for callback registration
41 * and restore_online_page_callback() for generic callback restore.
42 */
43
44static void generic_online_page(struct page *page);
45
46static online_page_callback_t online_page_callback = generic_online_page;
47
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080048DEFINE_MUTEX(mem_hotplug_mutex);
49
50void lock_memory_hotplug(void)
51{
52 mutex_lock(&mem_hotplug_mutex);
53
54 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
55 lock_system_sleep();
56}
57
58void unlock_memory_hotplug(void)
59{
60 unlock_system_sleep();
61 mutex_unlock(&mem_hotplug_mutex);
62}
63
64
Keith Mannthey45e0b782006-09-30 23:27:09 -070065/* add this memory to iomem resource */
66static struct resource *register_memory_resource(u64 start, u64 size)
67{
68 struct resource *res;
69 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
70 BUG_ON(!res);
71
72 res->name = "System RAM";
73 res->start = start;
74 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -080075 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -070076 if (request_resource(&iomem_resource, res) < 0) {
77 printk("System RAM resource %llx - %llx cannot be added\n",
78 (unsigned long long)res->start, (unsigned long long)res->end);
79 kfree(res);
80 res = NULL;
81 }
82 return res;
83}
84
85static void release_memory_resource(struct resource *res)
86{
87 if (!res)
88 return;
89 release_resource(res);
90 kfree(res);
91 return;
92}
93
Keith Mannthey53947022006-09-30 23:27:08 -070094#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasunori Goto04753272008-04-28 02:13:31 -070095#ifndef CONFIG_SPARSEMEM_VMEMMAP
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080096static void get_page_bootmem(unsigned long info, struct page *page,
97 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -070098{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -080099 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700100 SetPagePrivate(page);
101 set_page_private(page, info);
102 atomic_inc(&page->_count);
103}
104
Rakib Mullick23ce9322009-12-14 17:59:44 -0800105/* reference to __meminit __free_pages_bootmem is valid
106 * so use __ref to tell modpost not to generate a warning */
107void __ref put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700108{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800109 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700110
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800111 type = (unsigned long) page->lru.next;
112 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
113 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700114
115 if (atomic_dec_return(&page->_count) == 1) {
116 ClearPagePrivate(page);
117 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800118 INIT_LIST_HEAD(&page->lru);
Yasunori Goto04753272008-04-28 02:13:31 -0700119 __free_pages_bootmem(page, 0);
120 }
121
122}
123
Adrian Bunkd92bc312008-07-23 21:28:12 -0700124static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700125{
Jesse Tannahill92ab47c2010-07-09 18:18:05 -0700126 unsigned long *usemap, mapsize, page_mapsize, section_nr, i, j;
Yasunori Goto04753272008-04-28 02:13:31 -0700127 struct mem_section *ms;
Jesse Tannahill92ab47c2010-07-09 18:18:05 -0700128 struct page *page, *memmap, *page_page;
129 int memmap_page_valid;
Yasunori Goto04753272008-04-28 02:13:31 -0700130
131 if (!pfn_valid(start_pfn))
132 return;
133
134 section_nr = pfn_to_section_nr(start_pfn);
135 ms = __nr_to_section(section_nr);
136
137 /* Get section's memmap address */
138 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
139
140 /*
141 * Get page for the memmap's phys address
142 * XXX: need more consideration for sparse_vmemmap...
143 */
144 page = virt_to_page(memmap);
145 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
146 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
147
Jesse Tannahill92ab47c2010-07-09 18:18:05 -0700148 page_mapsize = PAGE_SIZE/sizeof(struct page);
149
150 /* remember memmap's page, except those that reference only holes */
151 for (i = 0; i < mapsize; i++, page++) {
152 memmap_page_valid = 0;
153 page_page = __va(page_to_pfn(page) << PAGE_SHIFT);
154 for (j = 0; j < page_mapsize; j++, page_page++) {
155 if (early_pfn_valid(page_to_pfn(page_page))) {
156 memmap_page_valid = 1;
157 break;
158 }
159 }
160 if (memmap_page_valid)
161 get_page_bootmem(section_nr, page, SECTION_INFO);
162 }
Yasunori Goto04753272008-04-28 02:13:31 -0700163
164 usemap = __nr_to_section(section_nr)->pageblock_flags;
165 page = virt_to_page(usemap);
166
167 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
168
169 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700170 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700171
172}
173
174void register_page_bootmem_info_node(struct pglist_data *pgdat)
175{
176 unsigned long i, pfn, end_pfn, nr_pages;
177 int node = pgdat->node_id;
178 struct page *page;
179 struct zone *zone;
180
181 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
182 page = virt_to_page(pgdat);
183
184 for (i = 0; i < nr_pages; i++, page++)
185 get_page_bootmem(node, page, NODE_INFO);
186
187 zone = &pgdat->node_zones[0];
188 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
189 if (zone->wait_table) {
190 nr_pages = zone->wait_table_hash_nr_entries
191 * sizeof(wait_queue_head_t);
192 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
193 page = virt_to_page(zone->wait_table);
194
195 for (i = 0; i < nr_pages; i++, page++)
196 get_page_bootmem(node, page, NODE_INFO);
197 }
198 }
199
200 pfn = pgdat->node_start_pfn;
201 end_pfn = pfn + pgdat->node_spanned_pages;
202
203 /* register_section info */
204 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
205 register_page_bootmem_info_section(pfn);
206
207}
208#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
209
Heiko Carstens76cdd582008-05-14 16:05:52 -0700210static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
211 unsigned long end_pfn)
212{
213 unsigned long old_zone_end_pfn;
214
215 zone_span_writelock(zone);
216
217 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
218 if (start_pfn < zone->zone_start_pfn)
219 zone->zone_start_pfn = start_pfn;
220
221 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
222 zone->zone_start_pfn;
223
224 zone_span_writeunlock(zone);
225}
226
227static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
228 unsigned long end_pfn)
229{
230 unsigned long old_pgdat_end_pfn =
231 pgdat->node_start_pfn + pgdat->node_spanned_pages;
232
233 if (start_pfn < pgdat->node_start_pfn)
234 pgdat->node_start_pfn = start_pfn;
235
236 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
237 pgdat->node_start_pfn;
238}
239
Al Viro31168482008-11-22 17:33:24 +0000240static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700241{
242 struct pglist_data *pgdat = zone->zone_pgdat;
243 int nr_pages = PAGES_PER_SECTION;
244 int nid = pgdat->node_id;
245 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700246 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700247
248 zone_type = zone - pgdat->node_zones;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700249 if (!zone->wait_table) {
250 int ret;
251
252 ret = init_currently_empty_zone(zone, phys_start_pfn,
253 nr_pages, MEMMAP_HOTPLUG);
254 if (ret)
255 return ret;
256 }
257 pgdat_resize_lock(zone->zone_pgdat, &flags);
258 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
259 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
260 phys_start_pfn + nr_pages);
261 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa02007-01-10 23:15:30 -0800262 memmap_init_zone(nr_pages, nid, zone_type,
263 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700264 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700265}
266
Gary Hadec04fc582009-01-06 14:39:14 -0800267static int __meminit __add_section(int nid, struct zone *zone,
268 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700269{
Dave Hansen3947be12005-10-29 18:16:54 -0700270 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700271 int ret;
272
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700273 if (pfn_valid(phys_start_pfn))
274 return -EEXIST;
275
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700276 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700277
278 if (ret < 0)
279 return ret;
280
Yasunori Goto718127c2006-06-23 02:03:10 -0700281 ret = __add_zone(zone, phys_start_pfn);
282
283 if (ret < 0)
284 return ret;
285
Gary Hadec04fc582009-01-06 14:39:14 -0800286 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700287}
288
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700289#ifdef CONFIG_SPARSEMEM_VMEMMAP
290static int __remove_section(struct zone *zone, struct mem_section *ms)
291{
292 /*
293 * XXX: Freeing memmap with vmemmap is not implement yet.
294 * This should be removed later.
295 */
296 return -EBUSY;
297}
298#else
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700299static int __remove_section(struct zone *zone, struct mem_section *ms)
300{
301 unsigned long flags;
302 struct pglist_data *pgdat = zone->zone_pgdat;
303 int ret = -EINVAL;
304
305 if (!valid_section(ms))
306 return ret;
307
308 ret = unregister_memory_section(ms);
309 if (ret)
310 return ret;
311
312 pgdat_resize_lock(pgdat, &flags);
313 sparse_remove_one_section(zone, ms);
314 pgdat_resize_unlock(pgdat, &flags);
315 return 0;
316}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700317#endif
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700318
Dave Hansen3947be12005-10-29 18:16:54 -0700319/*
320 * Reasonably generic function for adding memory. It is
321 * expected that archs that support memory hotplug will
322 * call this function after deciding the zone to which to
323 * add the new pages.
324 */
Gary Hadec04fc582009-01-06 14:39:14 -0800325int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
326 unsigned long nr_pages)
Dave Hansen3947be12005-10-29 18:16:54 -0700327{
328 unsigned long i;
329 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700330 int start_sec, end_sec;
331 /* during initialize mem_map, align hot-added range to section */
332 start_sec = pfn_to_section_nr(phys_start_pfn);
333 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700334
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700335 for (i = start_sec; i <= end_sec; i++) {
Gary Hadec04fc582009-01-06 14:39:14 -0800336 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700337
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700338 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200339 * EEXIST is finally dealt with by ioresource collision
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700340 * check. see add_memory() => register_memory_resource()
341 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700342 */
343 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700344 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700345 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700346 }
347
348 return err;
349}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700350EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700351
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700352/**
353 * __remove_pages() - remove sections of pages from a zone
354 * @zone: zone from which pages need to be removed
355 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
356 * @nr_pages: number of pages to remove (must be multiple of section size)
357 *
358 * Generic helper function to remove section mappings and sysfs entries
359 * for the section of the memory we are removing. Caller needs to make
360 * sure that pages are marked reserved and zones are adjust properly by
361 * calling offline_pages().
362 */
363int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
364 unsigned long nr_pages)
365{
366 unsigned long i, ret = 0;
367 int sections_to_remove;
368
369 /*
370 * We can only remove entire sections
371 */
372 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
373 BUG_ON(nr_pages % PAGES_PER_SECTION);
374
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700375 sections_to_remove = nr_pages / PAGES_PER_SECTION;
376 for (i = 0; i < sections_to_remove; i++) {
377 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Nathan Fontenotde7f0cb2008-10-18 20:27:14 -0700378 release_mem_region(pfn << PAGE_SHIFT,
379 PAGES_PER_SECTION << PAGE_SHIFT);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700380 ret = __remove_section(zone, __pfn_to_section(pfn));
381 if (ret)
382 break;
383 }
384 return ret;
385}
386EXPORT_SYMBOL_GPL(__remove_pages);
387
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700388int set_online_page_callback(online_page_callback_t callback)
389{
390 int rc = -EINVAL;
391
392 lock_memory_hotplug();
393
394 if (online_page_callback == generic_online_page) {
395 online_page_callback = callback;
396 rc = 0;
397 }
398
399 unlock_memory_hotplug();
400
401 return rc;
402}
403EXPORT_SYMBOL_GPL(set_online_page_callback);
404
405int restore_online_page_callback(online_page_callback_t callback)
406{
407 int rc = -EINVAL;
408
409 lock_memory_hotplug();
410
411 if (online_page_callback == callback) {
412 online_page_callback = generic_online_page;
413 rc = 0;
414 }
415
416 unlock_memory_hotplug();
417
418 return rc;
419}
420EXPORT_SYMBOL_GPL(restore_online_page_callback);
421
422void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700423{
Jan Beulich4738e1b2009-09-21 17:03:03 -0700424 unsigned long pfn = page_to_pfn(page);
425
Jack Cheungf6e34be2011-11-29 16:52:49 -0800426 totalram_pages++;
427#ifdef CONFIG_FIX_MOVABLE_ZONE
428 if (zone_idx(page_zone(page)) != ZONE_MOVABLE)
429 total_unmovable_pages++;
430#endif
Jan Beulich4738e1b2009-09-21 17:03:03 -0700431 if (pfn >= num_physpages)
432 num_physpages = pfn + 1;
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700433}
434EXPORT_SYMBOL_GPL(__online_page_set_limits);
435
436void __online_page_increment_counters(struct page *page)
437{
438 totalram_pages++;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700439
440#ifdef CONFIG_HIGHMEM
441 if (PageHighMem(page))
442 totalhigh_pages++;
443#endif
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700444}
445EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700446
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700447void __online_page_free(struct page *page)
448{
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700449 ClearPageReserved(page);
450 init_page_count(page);
451 __free_page(page);
452}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700453EXPORT_SYMBOL_GPL(__online_page_free);
454
455static void generic_online_page(struct page *page)
456{
457 __online_page_set_limits(page);
458 __online_page_increment_counters(page);
459 __online_page_free(page);
460}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700461
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700462static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
463 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700464{
465 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700466 unsigned long onlined_pages = *(unsigned long *)arg;
467 struct page *page;
468 if (PageReserved(pfn_to_page(start_pfn)))
469 for (i = 0; i < nr_pages; i++) {
470 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700471 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700472 onlined_pages++;
473 }
474 *(unsigned long *)arg = onlined_pages;
475 return 0;
476}
477
478
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700479int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700480{
Dave Hansen3947be12005-10-29 18:16:54 -0700481 unsigned long onlined_pages = 0;
482 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700483 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700484 int nid;
485 int ret;
486 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700487
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900488 lock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700489 arg.start_pfn = pfn;
490 arg.nr_pages = nr_pages;
491 arg.status_change_nid = -1;
492
493 nid = page_to_nid(pfn_to_page(pfn));
494 if (node_present_pages(nid) == 0)
495 arg.status_change_nid = nid;
496
497 ret = memory_notify(MEM_GOING_ONLINE, &arg);
498 ret = notifier_to_errno(ret);
499 if (ret) {
500 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900501 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700502 return ret;
503 }
Dave Hansen3947be12005-10-29 18:16:54 -0700504 /*
505 * This doesn't need a lock to do pfn_to_page().
506 * The section can't be removed here because of the
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800507 * memory_block->state_mutex.
Dave Hansen3947be12005-10-29 18:16:54 -0700508 */
509 zone = page_zone(pfn_to_page(pfn));
Yasunori Goto68113782006-06-23 02:03:11 -0700510 /*
511 * If this zone is not populated, then it is not in zonelist.
512 * This means the page allocator ignores this zone.
513 * So, zonelist must be updated after online.
514 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700515 mutex_lock(&zonelists_mutex);
Yasunori Goto68113782006-06-23 02:03:11 -0700516 if (!populated_zone(zone))
517 need_zonelists_rebuild = 1;
518
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700519 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700520 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700521 if (ret) {
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700522 mutex_unlock(&zonelists_mutex);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700523 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
524 nr_pages, pfn);
525 memory_notify(MEM_CANCEL_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900526 unlock_memory_hotplug();
Geoff Levandfd8a4222008-05-14 16:05:50 -0700527 return ret;
528 }
529
Dave Hansen3947be12005-10-29 18:16:54 -0700530 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800531 zone->zone_pgdat->node_present_pages += onlined_pages;
Haicheng Li1f522502010-05-24 14:32:51 -0700532 if (need_zonelists_rebuild)
533 build_all_zonelists(zone);
534 else
535 zone_pcp_update(zone);
Dave Hansen3947be12005-10-29 18:16:54 -0700536
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700537 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700538
539 init_per_zone_wmark_min();
540
Christoph Lameter7ea15302007-10-16 01:25:29 -0700541 if (onlined_pages) {
542 kswapd_run(zone_to_nid(zone));
543 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
544 }
Dave Hansen61b13992005-10-29 18:16:56 -0700545
Haicheng Li1f522502010-05-24 14:32:51 -0700546 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700547
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700548 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700549
550 if (onlined_pages)
551 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900552 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700553
Dave Hansen3947be12005-10-29 18:16:54 -0700554 return 0;
555}
Keith Mannthey53947022006-09-30 23:27:08 -0700556#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700557
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800558/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
559static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700560{
561 struct pglist_data *pgdat;
562 unsigned long zones_size[MAX_NR_ZONES] = {0};
563 unsigned long zholes_size[MAX_NR_ZONES] = {0};
564 unsigned long start_pfn = start >> PAGE_SHIFT;
565
566 pgdat = arch_alloc_nodedata(nid);
567 if (!pgdat)
568 return NULL;
569
570 arch_refresh_nodedata(nid, pgdat);
571
572 /* we can use NODE_DATA(nid) from here */
573
574 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700575 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700576
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700577 /*
578 * The node we allocated has no zone fallback lists. For avoiding
579 * to access not-initialized zonelist, build here.
580 */
David Rientjesf957db42011-06-22 18:13:04 -0700581 mutex_lock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700582 build_all_zonelists(NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700583 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700584
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700585 return pgdat;
586}
587
588static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
589{
590 arch_refresh_nodedata(nid, NULL);
591 arch_free_nodedata(pgdat);
592 return;
593}
594
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700595
minskey guocf234222010-05-24 14:32:41 -0700596/*
597 * called by cpu_up() to online a node without onlined memory.
598 */
599int mem_online_node(int nid)
600{
601 pg_data_t *pgdat;
602 int ret;
603
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800604 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700605 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700606 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700607 ret = -ENOMEM;
608 goto out;
609 }
610 node_set_online(nid);
611 ret = register_one_node(nid);
612 BUG_ON(ret);
613
614out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800615 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700616 return ret;
617}
618
Al Viro31168482008-11-22 17:33:24 +0000619/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
620int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700621{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700622 pg_data_t *pgdat = NULL;
623 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700624 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700625 int ret;
626
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800627 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800628
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700629 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800630 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700631 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800632 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700633
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700634 if (!node_online(nid)) {
635 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800636 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700637 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800638 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700639 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700640 }
641
Yasunori Gotobc02af92006-06-27 02:53:30 -0700642 /* call arch's memory hotadd */
643 ret = arch_add_memory(nid, start, size);
644
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700645 if (ret < 0)
646 goto error;
647
Yasunori Goto0fc44152006-06-27 02:53:38 -0700648 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700649 node_set_online(nid);
650
Yasunori Goto0fc44152006-06-27 02:53:38 -0700651 if (new_pgdat) {
652 ret = register_one_node(nid);
653 /*
654 * If sysfs file of new node can't create, cpu on the node
655 * can't be hot-added. There is no rollback way now.
656 * So, check by BUG_ON() to catch it reluctantly..
657 */
658 BUG_ON(ret);
659 }
660
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800661 /* create new memmap entry */
662 firmware_map_add_hotplug(start, start + size, "System RAM");
663
Andi Kleen6ad696d2009-11-17 14:06:22 -0800664 goto out;
665
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700666error:
667 /* rollback pgdat allocation and others */
668 if (new_pgdat)
669 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700670 if (res)
671 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700672
Andi Kleen6ad696d2009-11-17 14:06:22 -0800673out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800674 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700675 return ret;
676}
677EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700678
679#ifdef CONFIG_MEMORY_HOTREMOVE
680/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700681 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
682 * set and the size of the free page is given by page_order(). Using this,
683 * the function determines if the pageblock contains only free pages.
684 * Due to buddy contraints, a free page at least the size of a pageblock will
685 * be located at the start of the pageblock
686 */
687static inline int pageblock_free(struct page *page)
688{
689 return PageBuddy(page) && page_order(page) >= pageblock_order;
690}
691
692/* Return the start of the next active pageblock after a given page */
693static struct page *next_active_pageblock(struct page *page)
694{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700695 /* Ensure the starting page is pageblock-aligned */
696 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
697
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700698 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700699 if (pageblock_free(page)) {
700 int order;
701 /* be careful. we don't have locks, page_order can be changed.*/
702 order = page_order(page);
703 if ((order < MAX_ORDER) && (order >= pageblock_order))
704 return page + (1 << order);
705 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700706
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700707 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700708}
709
710/* Checks if this range of memory is likely to be hot-removable. */
711int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
712{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700713 struct page *page = pfn_to_page(start_pfn);
714 struct page *end_page = page + nr_pages;
715
716 /* Check the starting page of each pageblock within the range */
717 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700718 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700719 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700720 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700721 }
722
723 /* All pageblocks in the memory block are likely to be hot-removable */
724 return 1;
725}
726
727/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700728 * Confirm all pages in a range [start, end) is belongs to the same zone.
729 */
730static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
731{
732 unsigned long pfn;
733 struct zone *zone = NULL;
734 struct page *page;
735 int i;
736 for (pfn = start_pfn;
737 pfn < end_pfn;
738 pfn += MAX_ORDER_NR_PAGES) {
739 i = 0;
740 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
741 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
742 i++;
743 if (i == MAX_ORDER_NR_PAGES)
744 continue;
745 page = pfn_to_page(pfn + i);
746 if (zone && page_zone(page) != zone)
747 return 0;
748 zone = page_zone(page);
749 }
750 return 1;
751}
752
753/*
754 * Scanning pfn is much easier than scanning lru list.
755 * Scan pfn from start to end and Find LRU page.
756 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700757static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700758{
759 unsigned long pfn;
760 struct page *page;
761 for (pfn = start; pfn < end; pfn++) {
762 if (pfn_valid(pfn)) {
763 page = pfn_to_page(pfn);
764 if (PageLRU(page))
765 return pfn;
766 }
767 }
768 return 0;
769}
770
771static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800772hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700773{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800774 /* This should be improooooved!! */
775 return alloc_page(GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700776}
777
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700778#define NR_OFFLINE_AT_ONCE_PAGES (256)
779static int
780do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
781{
782 unsigned long pfn;
783 struct page *page;
784 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
785 int not_managed = 0;
786 int ret = 0;
787 LIST_HEAD(source);
788
789 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
790 if (!pfn_valid(pfn))
791 continue;
792 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700793 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700794 continue;
795 /*
796 * We can skip free pages. And we can only deal with pages on
797 * LRU.
798 */
Nick Piggin62695a82008-10-18 20:26:09 -0700799 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700800 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700801 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -0700802 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700803 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800804 inc_zone_page_state(page, NR_ISOLATED_ANON +
805 page_is_file_cache(page));
806
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700807 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700808#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -0800809 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
810 pfn);
811 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700812#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700813 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300814 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -0700815 check this again here. */
816 if (page_count(page)) {
817 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -0700818 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -0700819 break;
820 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700821 }
822 }
Bob Liuf3ab2632010-10-26 14:22:10 -0700823 if (!list_empty(&source)) {
824 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700825 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -0700826 goto out;
827 }
828 /* this function returns # of failed pages */
Mel Gorman77f1fe62011-01-13 15:45:57 -0800829 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800830 true, MIGRATE_SYNC);
Bob Liuf3ab2632010-10-26 14:22:10 -0700831 if (ret)
832 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700833 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700834out:
835 return ret;
836}
837
838/*
839 * remove from free_area[] and mark all as Reserved.
840 */
841static int
842offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
843 void *data)
844{
845 __offline_isolated_pages(start, start + nr_pages);
846 return 0;
847}
848
849static void
850offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
851{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700852 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700853 offline_isolated_pages_cb);
854}
855
856/*
857 * Check all pages in range, recoreded as memory resource, are isolated.
858 */
859static int
860check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
861 void *data)
862{
863 int ret;
864 long offlined = *(long *)data;
865 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
866 offlined = nr_pages;
867 if (!ret)
868 *(long *)data += offlined;
869 return ret;
870}
871
872static long
873check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
874{
875 long offlined = 0;
876 int ret;
877
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700878 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700879 check_pages_isolated_cb);
880 if (ret < 0)
881 offlined = (long)ret;
882 return offlined;
883}
884
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700885static int __ref offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700886 unsigned long end_pfn, unsigned long timeout)
887{
888 unsigned long pfn, nr_pages, expire;
889 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700890 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700891 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700892 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700893
894 BUG_ON(start_pfn >= end_pfn);
895 /* at least, alignment against pageblock is necessary */
896 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
897 return -EINVAL;
898 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
899 return -EINVAL;
900 /* This makes hotplug much easier...and readable.
901 we assume this for now. .*/
902 if (!test_pages_in_a_zone(start_pfn, end_pfn))
903 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700904
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800905 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800906
Yasunori Goto7b78d332007-10-21 16:41:36 -0700907 zone = page_zone(pfn_to_page(start_pfn));
908 node = zone_to_nid(zone);
909 nr_pages = end_pfn - start_pfn;
910
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700911 /* set above range as isolated */
912 ret = start_isolate_page_range(start_pfn, end_pfn);
913 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800914 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700915
916 arg.start_pfn = start_pfn;
917 arg.nr_pages = nr_pages;
918 arg.status_change_nid = -1;
919 if (nr_pages >= node_present_pages(node))
920 arg.status_change_nid = node;
921
922 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
923 ret = notifier_to_errno(ret);
924 if (ret)
925 goto failed_removal;
926
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700927 pfn = start_pfn;
928 expire = jiffies + timeout;
929 drain = 0;
930 retry_max = 5;
931repeat:
932 /* start memory hot removal */
933 ret = -EAGAIN;
934 if (time_after(jiffies, expire))
935 goto failed_removal;
936 ret = -EINTR;
937 if (signal_pending(current))
938 goto failed_removal;
939 ret = 0;
940 if (drain) {
941 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700942 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800943 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700944 }
945
946 pfn = scan_lru_pages(start_pfn, end_pfn);
947 if (pfn) { /* We have page on LRU */
948 ret = do_migrate_range(pfn, end_pfn);
949 if (!ret) {
950 drain = 1;
951 goto repeat;
952 } else {
953 if (ret < 0)
954 if (--retry_max == 0)
955 goto failed_removal;
956 yield();
957 drain = 1;
958 goto repeat;
959 }
960 }
961 /* drain all zone's lru pagevec, this is asyncronous... */
962 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700963 yield();
964 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800965 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700966 /* check again */
967 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
968 if (offlined_pages < 0) {
969 ret = -EBUSY;
970 goto failed_removal;
971 }
972 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
973 /* Ok, all of our target is islaoted.
974 We cannot do rollback at this point. */
975 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800976 /* reset pagetype flags and makes migrate type to be MOVABLE */
977 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700978 /* removal success */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700979 zone->present_pages -= offlined_pages;
980 zone->zone_pgdat->node_present_pages -= offlined_pages;
981 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700982
Jack Cheungf6e34be2011-11-29 16:52:49 -0800983#ifdef CONFIG_FIX_MOVABLE_ZONE
984 if (zone_idx(zone) != ZONE_MOVABLE)
985 total_unmovable_pages -= offlined_pages;
986#endif
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700987 init_per_zone_wmark_min();
988
David Rientjes8fe23e02009-12-14 17:58:33 -0800989 if (!node_present_pages(node)) {
990 node_clear_state(node, N_HIGH_MEMORY);
991 kswapd_stop(node);
992 }
Minchan Kimbce73942009-06-16 15:32:50 -0700993
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700994 vm_total_pages = nr_free_pagecache_pages();
995 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700996
997 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800998 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700999 return 0;
1000
1001failed_removal:
1002 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
1003 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001004 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001005 /* pushback to free area */
1006 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001007
Andi Kleen6ad696d2009-11-17 14:06:22 -08001008out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001009 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001010 return ret;
1011}
Badari Pulavarty71088782008-10-18 20:25:58 -07001012
1013int remove_memory(u64 start, u64 size)
1014{
1015 unsigned long start_pfn, end_pfn;
1016
1017 start_pfn = PFN_DOWN(start);
1018 end_pfn = start_pfn + PFN_DOWN(size);
1019 return offline_pages(start_pfn, end_pfn, 120 * HZ);
1020}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001021#else
1022int remove_memory(u64 start, u64 size)
1023{
1024 return -EINVAL;
1025}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001026#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001027EXPORT_SYMBOL_GPL(remove_memory);