blob: b031f96f6ec94130c3437c8b4fa82213452099b7 [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{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 unsigned long *usemap, mapsize, page_mapsize, section_nr, i, j;
Yasunori Goto04753272008-04-28 02:13:31 -0700127 struct mem_section *ms;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -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
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700426 totalram_pages++;
Jack Cheung59f9f1c2011-11-29 16:52:49 -0800427#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;
Jack Cheungaa7e9de2012-01-19 17:06:04 -0800532 drain_all_pages();
Haicheng Li1f522502010-05-24 14:32:51 -0700533 if (need_zonelists_rebuild)
534 build_all_zonelists(zone);
535 else
536 zone_pcp_update(zone);
Dave Hansen3947be12005-10-29 18:16:54 -0700537
Haicheng Li4eaf3f62010-05-24 14:32:52 -0700538 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700539
540 init_per_zone_wmark_min();
541
Christoph Lameter7ea15302007-10-16 01:25:29 -0700542 if (onlined_pages) {
543 kswapd_run(zone_to_nid(zone));
544 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
545 }
Dave Hansen61b13992005-10-29 18:16:56 -0700546
Haicheng Li1f522502010-05-24 14:32:51 -0700547 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700548
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700549 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700550
551 if (onlined_pages)
552 memory_notify(MEM_ONLINE, &arg);
KAMEZAWA Hiroyuki925268a2011-01-11 16:44:01 +0900553 unlock_memory_hotplug();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700554
Dave Hansen3947be12005-10-29 18:16:54 -0700555 return 0;
556}
Keith Mannthey53947022006-09-30 23:27:08 -0700557#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700558
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800559/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
560static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700561{
562 struct pglist_data *pgdat;
563 unsigned long zones_size[MAX_NR_ZONES] = {0};
564 unsigned long zholes_size[MAX_NR_ZONES] = {0};
565 unsigned long start_pfn = start >> PAGE_SHIFT;
566
567 pgdat = arch_alloc_nodedata(nid);
568 if (!pgdat)
569 return NULL;
570
571 arch_refresh_nodedata(nid, pgdat);
572
573 /* we can use NODE_DATA(nid) from here */
574
575 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700576 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700577
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700578 /*
579 * The node we allocated has no zone fallback lists. For avoiding
580 * to access not-initialized zonelist, build here.
581 */
David Rientjesf957db42011-06-22 18:13:04 -0700582 mutex_lock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700583 build_all_zonelists(NULL);
David Rientjesf957db42011-06-22 18:13:04 -0700584 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700585
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700586 return pgdat;
587}
588
589static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
590{
591 arch_refresh_nodedata(nid, NULL);
592 arch_free_nodedata(pgdat);
593 return;
594}
595
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700596
minskey guocf234222010-05-24 14:32:41 -0700597/*
598 * called by cpu_up() to online a node without onlined memory.
599 */
600int mem_online_node(int nid)
601{
602 pg_data_t *pgdat;
603 int ret;
604
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800605 lock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700606 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -0700607 if (!pgdat) {
minskey guocf234222010-05-24 14:32:41 -0700608 ret = -ENOMEM;
609 goto out;
610 }
611 node_set_online(nid);
612 ret = register_one_node(nid);
613 BUG_ON(ret);
614
615out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800616 unlock_memory_hotplug();
minskey guocf234222010-05-24 14:32:41 -0700617 return ret;
618}
619
Al Viro31168482008-11-22 17:33:24 +0000620/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
621int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700622{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700623 pg_data_t *pgdat = NULL;
624 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700625 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700626 int ret;
627
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800628 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800629
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700630 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800631 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700632 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800633 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700634
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700635 if (!node_online(nid)) {
636 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800637 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700638 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800639 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700640 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700641 }
642
Yasunori Gotobc02af92006-06-27 02:53:30 -0700643 /* call arch's memory hotadd */
644 ret = arch_add_memory(nid, start, size);
645
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700646 if (ret < 0)
647 goto error;
648
Yasunori Goto0fc44152006-06-27 02:53:38 -0700649 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700650 node_set_online(nid);
651
Yasunori Goto0fc44152006-06-27 02:53:38 -0700652 if (new_pgdat) {
653 ret = register_one_node(nid);
654 /*
655 * If sysfs file of new node can't create, cpu on the node
656 * can't be hot-added. There is no rollback way now.
657 * So, check by BUG_ON() to catch it reluctantly..
658 */
659 BUG_ON(ret);
660 }
661
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800662 /* create new memmap entry */
663 firmware_map_add_hotplug(start, start + size, "System RAM");
664
Andi Kleen6ad696d2009-11-17 14:06:22 -0800665 goto out;
666
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700667error:
668 /* rollback pgdat allocation and others */
669 if (new_pgdat)
670 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700671 if (res)
672 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700673
Andi Kleen6ad696d2009-11-17 14:06:22 -0800674out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800675 unlock_memory_hotplug();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700676 return ret;
677}
678EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680int __ref physical_remove_memory(u64 start, u64 size)
681{
682 int ret;
683 struct resource *res, *res_old;
684 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
685 BUG_ON(!res);
686
687 ret = arch_physical_remove_memory(start, size);
Larry Bassel24217172011-10-14 10:36:45 -0700688 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 kfree(res);
Larry Bassel24217172011-10-14 10:36:45 -0700690 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 }
692
693 res->name = "System RAM";
694 res->start = start;
695 res->end = start + size - 1;
696 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
697
698 res_old = locate_resource(&iomem_resource, res);
Larry Bassel24217172011-10-14 10:36:45 -0700699 if (res_old) {
700 release_resource(res_old);
701 if (PageSlab(virt_to_head_page(res_old)))
702 kfree(res_old);
703 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 kfree(res);
705
706 return ret;
707}
708EXPORT_SYMBOL_GPL(physical_remove_memory);
709
710int __ref physical_active_memory(u64 start, u64 size)
711{
712 int ret;
713
714 ret = arch_physical_active_memory(start, size);
715 return ret;
716}
717EXPORT_SYMBOL_GPL(physical_active_memory);
718
719int __ref physical_low_power_memory(u64 start, u64 size)
720{
721 int ret;
722
723 ret = arch_physical_low_power_memory(start, size);
724 return ret;
725}
726EXPORT_SYMBOL_GPL(physical_low_power_memory);
727
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700728#ifdef CONFIG_MEMORY_HOTREMOVE
729/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700730 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
731 * set and the size of the free page is given by page_order(). Using this,
732 * the function determines if the pageblock contains only free pages.
733 * Due to buddy contraints, a free page at least the size of a pageblock will
734 * be located at the start of the pageblock
735 */
736static inline int pageblock_free(struct page *page)
737{
738 return PageBuddy(page) && page_order(page) >= pageblock_order;
739}
740
741/* Return the start of the next active pageblock after a given page */
742static struct page *next_active_pageblock(struct page *page)
743{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700744 /* Ensure the starting page is pageblock-aligned */
745 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
746
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700747 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700748 if (pageblock_free(page)) {
749 int order;
750 /* be careful. we don't have locks, page_order can be changed.*/
751 order = page_order(page);
752 if ((order < MAX_ORDER) && (order >= pageblock_order))
753 return page + (1 << order);
754 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700755
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -0700756 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700757}
758
759/* Checks if this range of memory is likely to be hot-removable. */
760int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
761{
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700762 struct page *page = pfn_to_page(start_pfn);
763 struct page *end_page = page + nr_pages;
764
765 /* Check the starting page of each pageblock within the range */
766 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700767 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700768 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -0700769 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700770 }
771
772 /* All pageblocks in the memory block are likely to be hot-removable */
773 return 1;
774}
775
776/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700777 * Confirm all pages in a range [start, end) is belongs to the same zone.
778 */
779static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
780{
781 unsigned long pfn;
782 struct zone *zone = NULL;
783 struct page *page;
784 int i;
785 for (pfn = start_pfn;
786 pfn < end_pfn;
787 pfn += MAX_ORDER_NR_PAGES) {
788 i = 0;
789 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
790 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
791 i++;
792 if (i == MAX_ORDER_NR_PAGES)
793 continue;
794 page = pfn_to_page(pfn + i);
795 if (zone && page_zone(page) != zone)
796 return 0;
797 zone = page_zone(page);
798 }
799 return 1;
800}
801
802/*
803 * Scanning pfn is much easier than scanning lru list.
804 * Scan pfn from start to end and Find LRU page.
805 */
Andrew Morton7bbc0902010-10-26 14:22:05 -0700806static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700807{
808 unsigned long pfn;
809 struct page *page;
810 for (pfn = start; pfn < end; pfn++) {
811 if (pfn_valid(pfn)) {
812 page = pfn_to_page(pfn);
813 if (PageLRU(page))
814 return pfn;
815 }
816 }
817 return 0;
818}
819
820static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800821hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700822{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800823 /* This should be improooooved!! */
Jack Cheung1e6ec3c2011-11-21 16:44:05 -0800824 return alloc_page(GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN |
825 __GFP_NOMEMALLOC);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700826}
827
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700828#define NR_OFFLINE_AT_ONCE_PAGES (256)
829static int
830do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
831{
832 unsigned long pfn;
833 struct page *page;
834 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
835 int not_managed = 0;
836 int ret = 0;
837 LIST_HEAD(source);
838
839 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
840 if (!pfn_valid(pfn))
841 continue;
842 page = pfn_to_page(pfn);
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700843 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700844 continue;
845 /*
846 * We can skip free pages. And we can only deal with pages on
847 * LRU.
848 */
Nick Piggin62695a82008-10-18 20:26:09 -0700849 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700850 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700851 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -0700852 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700853 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800854 inc_zone_page_state(page, NR_ISOLATED_ANON +
855 page_is_file_cache(page));
856
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700857 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700858#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -0800859 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
860 pfn);
861 dump_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700862#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -0700863 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -0700865 check this again here. */
866 if (page_count(page)) {
867 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -0700868 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -0700869 break;
870 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700871 }
872 }
Bob Liuf3ab2632010-10-26 14:22:10 -0700873 if (!list_empty(&source)) {
874 if (not_managed) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700875 putback_lru_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -0700876 goto out;
877 }
878 /* this function returns # of failed pages */
Mel Gorman77f1fe62011-01-13 15:45:57 -0800879 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800880 true, MIGRATE_SYNC);
Bob Liuf3ab2632010-10-26 14:22:10 -0700881 if (ret)
882 putback_lru_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700883 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700884out:
885 return ret;
886}
887
888/*
889 * remove from free_area[] and mark all as Reserved.
890 */
891static int
892offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
893 void *data)
894{
895 __offline_isolated_pages(start, start + nr_pages);
896 return 0;
897}
898
899static void
900offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
901{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700902 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700903 offline_isolated_pages_cb);
904}
905
906/*
907 * Check all pages in range, recoreded as memory resource, are isolated.
908 */
909static int
910check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
911 void *data)
912{
913 int ret;
914 long offlined = *(long *)data;
915 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
916 offlined = nr_pages;
917 if (!ret)
918 *(long *)data += offlined;
919 return ret;
920}
921
922static long
923check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
924{
925 long offlined = 0;
926 int ret;
927
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700928 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700929 check_pages_isolated_cb);
930 if (ret < 0)
931 offlined = (long)ret;
932 return offlined;
933}
934
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -0700935static int __ref offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700936 unsigned long end_pfn, unsigned long timeout)
937{
938 unsigned long pfn, nr_pages, expire;
939 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700940 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700941 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700942 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700943
944 BUG_ON(start_pfn >= end_pfn);
945 /* at least, alignment against pageblock is necessary */
946 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
947 return -EINVAL;
948 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
949 return -EINVAL;
950 /* This makes hotplug much easier...and readable.
951 we assume this for now. .*/
952 if (!test_pages_in_a_zone(start_pfn, end_pfn))
953 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700954
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800955 lock_memory_hotplug();
Andi Kleen6ad696d2009-11-17 14:06:22 -0800956
Yasunori Goto7b78d332007-10-21 16:41:36 -0700957 zone = page_zone(pfn_to_page(start_pfn));
958 node = zone_to_nid(zone);
959 nr_pages = end_pfn - start_pfn;
960
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700961 /* set above range as isolated */
962 ret = start_isolate_page_range(start_pfn, end_pfn);
963 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800964 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700965
966 arg.start_pfn = start_pfn;
967 arg.nr_pages = nr_pages;
968 arg.status_change_nid = -1;
969 if (nr_pages >= node_present_pages(node))
970 arg.status_change_nid = node;
971
972 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
973 ret = notifier_to_errno(ret);
974 if (ret)
975 goto failed_removal;
976
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700977 pfn = start_pfn;
978 expire = jiffies + timeout;
979 drain = 0;
980 retry_max = 5;
981repeat:
982 /* start memory hot removal */
983 ret = -EAGAIN;
984 if (time_after(jiffies, expire))
985 goto failed_removal;
986 ret = -EINTR;
987 if (signal_pending(current))
988 goto failed_removal;
989 ret = 0;
990 if (drain) {
991 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700992 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800993 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700994 }
995
996 pfn = scan_lru_pages(start_pfn, end_pfn);
997 if (pfn) { /* We have page on LRU */
998 ret = do_migrate_range(pfn, end_pfn);
999 if (!ret) {
1000 drain = 1;
1001 goto repeat;
1002 } else {
1003 if (ret < 0)
1004 if (--retry_max == 0)
1005 goto failed_removal;
1006 yield();
1007 drain = 1;
1008 goto repeat;
1009 }
1010 }
1011 /* drain all zone's lru pagevec, this is asyncronous... */
1012 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001013 yield();
1014 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001015 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001016 /* check again */
1017 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1018 if (offlined_pages < 0) {
1019 ret = -EBUSY;
1020 goto failed_removal;
1021 }
1022 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1023 /* Ok, all of our target is islaoted.
1024 We cannot do rollback at this point. */
1025 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001026 /* reset pagetype flags and makes migrate type to be MOVABLE */
1027 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001028 /* removal success */
Jack Cheung80c201e2011-11-29 16:53:34 -08001029 if (offlined_pages > zone->present_pages)
1030 zone->present_pages = 0;
1031 else
1032 zone->present_pages -= offlined_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001033 zone->zone_pgdat->node_present_pages -= offlined_pages;
1034 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001035
Jack Cheung59f9f1c2011-11-29 16:52:49 -08001036#ifdef CONFIG_FIX_MOVABLE_ZONE
1037 if (zone_idx(zone) != ZONE_MOVABLE)
1038 total_unmovable_pages -= offlined_pages;
1039#endif
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001040 init_per_zone_wmark_min();
1041
David Rientjes8fe23e02009-12-14 17:58:33 -08001042 if (!node_present_pages(node)) {
1043 node_clear_state(node, N_HIGH_MEMORY);
1044 kswapd_stop(node);
1045 }
Minchan Kimbce73942009-06-16 15:32:50 -07001046
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001047 vm_total_pages = nr_free_pagecache_pages();
1048 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001049
1050 memory_notify(MEM_OFFLINE, &arg);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001051 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001052 return 0;
1053
1054failed_removal:
1055 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
1056 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001057 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001058 /* pushback to free area */
1059 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001060
Andi Kleen6ad696d2009-11-17 14:06:22 -08001061out:
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -08001062 unlock_memory_hotplug();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001063 return ret;
1064}
Badari Pulavarty71088782008-10-18 20:25:58 -07001065
1066int remove_memory(u64 start, u64 size)
1067{
1068 unsigned long start_pfn, end_pfn;
1069
1070 start_pfn = PFN_DOWN(start);
1071 end_pfn = start_pfn + PFN_DOWN(size);
1072 return offline_pages(start_pfn, end_pfn, 120 * HZ);
1073}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -07001075#else
1076int remove_memory(u64 start, u64 size)
1077{
1078 return -EINVAL;
1079}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001080#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -07001081EXPORT_SYMBOL_GPL(remove_memory);