blob: bc5a08138f1e08c3af8ecf76594f9efc24ef0a2b [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>
Dave Hansen3947be12005-10-29 18:16:54 -070031
32#include <asm/tlbflush.h>
33
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030034#include "internal.h"
35
Keith Mannthey45e0b782006-09-30 23:27:09 -070036/* add this memory to iomem resource */
37static struct resource *register_memory_resource(u64 start, u64 size)
38{
39 struct resource *res;
40 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
41 BUG_ON(!res);
42
43 res->name = "System RAM";
44 res->start = start;
45 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -080046 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -070047 if (request_resource(&iomem_resource, res) < 0) {
48 printk("System RAM resource %llx - %llx cannot be added\n",
49 (unsigned long long)res->start, (unsigned long long)res->end);
50 kfree(res);
51 res = NULL;
52 }
53 return res;
54}
55
56static void release_memory_resource(struct resource *res)
57{
58 if (!res)
59 return;
60 release_resource(res);
61 kfree(res);
62 return;
63}
64
Keith Mannthey53947022006-09-30 23:27:08 -070065#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasunori Goto04753272008-04-28 02:13:31 -070066#ifndef CONFIG_SPARSEMEM_VMEMMAP
Yasunori Gotoaf370fb2008-07-23 21:28:17 -070067static void get_page_bootmem(unsigned long info, struct page *page, int type)
Yasunori Goto04753272008-04-28 02:13:31 -070068{
Yasunori Gotoaf370fb2008-07-23 21:28:17 -070069 atomic_set(&page->_mapcount, type);
Yasunori Goto04753272008-04-28 02:13:31 -070070 SetPagePrivate(page);
71 set_page_private(page, info);
72 atomic_inc(&page->_count);
73}
74
75void put_page_bootmem(struct page *page)
76{
Yasunori Gotoaf370fb2008-07-23 21:28:17 -070077 int type;
Yasunori Goto04753272008-04-28 02:13:31 -070078
Yasunori Gotoaf370fb2008-07-23 21:28:17 -070079 type = atomic_read(&page->_mapcount);
80 BUG_ON(type >= -1);
Yasunori Goto04753272008-04-28 02:13:31 -070081
82 if (atomic_dec_return(&page->_count) == 1) {
83 ClearPagePrivate(page);
84 set_page_private(page, 0);
85 reset_page_mapcount(page);
86 __free_pages_bootmem(page, 0);
87 }
88
89}
90
Adrian Bunkd92bc312008-07-23 21:28:12 -070091static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -070092{
93 unsigned long *usemap, mapsize, section_nr, i;
94 struct mem_section *ms;
95 struct page *page, *memmap;
96
97 if (!pfn_valid(start_pfn))
98 return;
99
100 section_nr = pfn_to_section_nr(start_pfn);
101 ms = __nr_to_section(section_nr);
102
103 /* Get section's memmap address */
104 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
105
106 /*
107 * Get page for the memmap's phys address
108 * XXX: need more consideration for sparse_vmemmap...
109 */
110 page = virt_to_page(memmap);
111 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
112 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
113
114 /* remember memmap's page */
115 for (i = 0; i < mapsize; i++, page++)
116 get_page_bootmem(section_nr, page, SECTION_INFO);
117
118 usemap = __nr_to_section(section_nr)->pageblock_flags;
119 page = virt_to_page(usemap);
120
121 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
122
123 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700124 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700125
126}
127
128void register_page_bootmem_info_node(struct pglist_data *pgdat)
129{
130 unsigned long i, pfn, end_pfn, nr_pages;
131 int node = pgdat->node_id;
132 struct page *page;
133 struct zone *zone;
134
135 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
136 page = virt_to_page(pgdat);
137
138 for (i = 0; i < nr_pages; i++, page++)
139 get_page_bootmem(node, page, NODE_INFO);
140
141 zone = &pgdat->node_zones[0];
142 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
143 if (zone->wait_table) {
144 nr_pages = zone->wait_table_hash_nr_entries
145 * sizeof(wait_queue_head_t);
146 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
147 page = virt_to_page(zone->wait_table);
148
149 for (i = 0; i < nr_pages; i++, page++)
150 get_page_bootmem(node, page, NODE_INFO);
151 }
152 }
153
154 pfn = pgdat->node_start_pfn;
155 end_pfn = pfn + pgdat->node_spanned_pages;
156
157 /* register_section info */
158 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
159 register_page_bootmem_info_section(pfn);
160
161}
162#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
163
Heiko Carstens76cdd582008-05-14 16:05:52 -0700164static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
165 unsigned long end_pfn)
166{
167 unsigned long old_zone_end_pfn;
168
169 zone_span_writelock(zone);
170
171 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
172 if (start_pfn < zone->zone_start_pfn)
173 zone->zone_start_pfn = start_pfn;
174
175 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
176 zone->zone_start_pfn;
177
178 zone_span_writeunlock(zone);
179}
180
181static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
182 unsigned long end_pfn)
183{
184 unsigned long old_pgdat_end_pfn =
185 pgdat->node_start_pfn + pgdat->node_spanned_pages;
186
187 if (start_pfn < pgdat->node_start_pfn)
188 pgdat->node_start_pfn = start_pfn;
189
190 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
191 pgdat->node_start_pfn;
192}
193
Al Viro31168482008-11-22 17:33:24 +0000194static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700195{
196 struct pglist_data *pgdat = zone->zone_pgdat;
197 int nr_pages = PAGES_PER_SECTION;
198 int nid = pgdat->node_id;
199 int zone_type;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700200 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700201
202 zone_type = zone - pgdat->node_zones;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700203 if (!zone->wait_table) {
204 int ret;
205
206 ret = init_currently_empty_zone(zone, phys_start_pfn,
207 nr_pages, MEMMAP_HOTPLUG);
208 if (ret)
209 return ret;
210 }
211 pgdat_resize_lock(zone->zone_pgdat, &flags);
212 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
213 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
214 phys_start_pfn + nr_pages);
215 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800216 memmap_init_zone(nr_pages, nid, zone_type,
217 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -0700218 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700219}
220
Gary Hadec04fc582009-01-06 14:39:14 -0800221static int __meminit __add_section(int nid, struct zone *zone,
222 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700223{
Dave Hansen3947be12005-10-29 18:16:54 -0700224 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -0700225 int ret;
226
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700227 if (pfn_valid(phys_start_pfn))
228 return -EEXIST;
229
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700230 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700231
232 if (ret < 0)
233 return ret;
234
Yasunori Goto718127c2006-06-23 02:03:10 -0700235 ret = __add_zone(zone, phys_start_pfn);
236
237 if (ret < 0)
238 return ret;
239
Gary Hadec04fc582009-01-06 14:39:14 -0800240 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700241}
242
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700243#ifdef CONFIG_SPARSEMEM_VMEMMAP
244static int __remove_section(struct zone *zone, struct mem_section *ms)
245{
246 /*
247 * XXX: Freeing memmap with vmemmap is not implement yet.
248 * This should be removed later.
249 */
250 return -EBUSY;
251}
252#else
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700253static int __remove_section(struct zone *zone, struct mem_section *ms)
254{
255 unsigned long flags;
256 struct pglist_data *pgdat = zone->zone_pgdat;
257 int ret = -EINVAL;
258
259 if (!valid_section(ms))
260 return ret;
261
262 ret = unregister_memory_section(ms);
263 if (ret)
264 return ret;
265
266 pgdat_resize_lock(pgdat, &flags);
267 sparse_remove_one_section(zone, ms);
268 pgdat_resize_unlock(pgdat, &flags);
269 return 0;
270}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700271#endif
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700272
Dave Hansen3947be12005-10-29 18:16:54 -0700273/*
274 * Reasonably generic function for adding memory. It is
275 * expected that archs that support memory hotplug will
276 * call this function after deciding the zone to which to
277 * add the new pages.
278 */
Gary Hadec04fc582009-01-06 14:39:14 -0800279int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
280 unsigned long nr_pages)
Dave Hansen3947be12005-10-29 18:16:54 -0700281{
282 unsigned long i;
283 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700284 int start_sec, end_sec;
285 /* during initialize mem_map, align hot-added range to section */
286 start_sec = pfn_to_section_nr(phys_start_pfn);
287 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700288
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700289 for (i = start_sec; i <= end_sec; i++) {
Gary Hadec04fc582009-01-06 14:39:14 -0800290 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700291
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700292 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200293 * EEXIST is finally dealt with by ioresource collision
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700294 * check. see add_memory() => register_memory_resource()
295 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700296 */
297 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700298 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700299 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700300 }
301
302 return err;
303}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700304EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700305
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700306/**
307 * __remove_pages() - remove sections of pages from a zone
308 * @zone: zone from which pages need to be removed
309 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
310 * @nr_pages: number of pages to remove (must be multiple of section size)
311 *
312 * Generic helper function to remove section mappings and sysfs entries
313 * for the section of the memory we are removing. Caller needs to make
314 * sure that pages are marked reserved and zones are adjust properly by
315 * calling offline_pages().
316 */
317int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
318 unsigned long nr_pages)
319{
320 unsigned long i, ret = 0;
321 int sections_to_remove;
322
323 /*
324 * We can only remove entire sections
325 */
326 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
327 BUG_ON(nr_pages % PAGES_PER_SECTION);
328
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700329 sections_to_remove = nr_pages / PAGES_PER_SECTION;
330 for (i = 0; i < sections_to_remove; i++) {
331 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Nathan Fontenotde7f0cb2008-10-18 20:27:14 -0700332 release_mem_region(pfn << PAGE_SHIFT,
333 PAGES_PER_SECTION << PAGE_SHIFT);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700334 ret = __remove_section(zone, __pfn_to_section(pfn));
335 if (ret)
336 break;
337 }
338 return ret;
339}
340EXPORT_SYMBOL_GPL(__remove_pages);
341
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700342void online_page(struct page *page)
343{
Jan Beulich4738e1b2009-09-21 17:03:03 -0700344 unsigned long pfn = page_to_pfn(page);
345
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700346 totalram_pages++;
Jan Beulich4738e1b2009-09-21 17:03:03 -0700347 if (pfn >= num_physpages)
348 num_physpages = pfn + 1;
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700349
350#ifdef CONFIG_HIGHMEM
351 if (PageHighMem(page))
352 totalhigh_pages++;
353#endif
354
355#ifdef CONFIG_FLATMEM
356 max_mapnr = max(page_to_pfn(page), max_mapnr);
357#endif
358
359 ClearPageReserved(page);
360 init_page_count(page);
361 __free_page(page);
362}
363
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700364static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
365 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700366{
367 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700368 unsigned long onlined_pages = *(unsigned long *)arg;
369 struct page *page;
370 if (PageReserved(pfn_to_page(start_pfn)))
371 for (i = 0; i < nr_pages; i++) {
372 page = pfn_to_page(start_pfn + i);
373 online_page(page);
374 onlined_pages++;
375 }
376 *(unsigned long *)arg = onlined_pages;
377 return 0;
378}
379
380
381int online_pages(unsigned long pfn, unsigned long nr_pages)
382{
Dave Hansen3947be12005-10-29 18:16:54 -0700383 unsigned long onlined_pages = 0;
384 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700385 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700386 int nid;
387 int ret;
388 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700389
Yasunori Goto7b78d332007-10-21 16:41:36 -0700390 arg.start_pfn = pfn;
391 arg.nr_pages = nr_pages;
392 arg.status_change_nid = -1;
393
394 nid = page_to_nid(pfn_to_page(pfn));
395 if (node_present_pages(nid) == 0)
396 arg.status_change_nid = nid;
397
398 ret = memory_notify(MEM_GOING_ONLINE, &arg);
399 ret = notifier_to_errno(ret);
400 if (ret) {
401 memory_notify(MEM_CANCEL_ONLINE, &arg);
402 return ret;
403 }
Dave Hansen3947be12005-10-29 18:16:54 -0700404 /*
405 * This doesn't need a lock to do pfn_to_page().
406 * The section can't be removed here because of the
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800407 * memory_block->state_mutex.
Dave Hansen3947be12005-10-29 18:16:54 -0700408 */
409 zone = page_zone(pfn_to_page(pfn));
Yasunori Goto68113782006-06-23 02:03:11 -0700410 /*
411 * If this zone is not populated, then it is not in zonelist.
412 * This means the page allocator ignores this zone.
413 * So, zonelist must be updated after online.
414 */
415 if (!populated_zone(zone))
416 need_zonelists_rebuild = 1;
417
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700418 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700419 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700420 if (ret) {
421 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
422 nr_pages, pfn);
423 memory_notify(MEM_CANCEL_ONLINE, &arg);
424 return ret;
425 }
426
Dave Hansen3947be12005-10-29 18:16:54 -0700427 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800428 zone->zone_pgdat->node_present_pages += onlined_pages;
Dave Hansen3947be12005-10-29 18:16:54 -0700429
Shaohua Li112067f2009-09-21 17:01:16 -0700430 zone_pcp_update(zone);
Minchan Kimbc75d332009-06-16 15:32:48 -0700431 setup_per_zone_wmarks();
Minchan Kimbce73942009-06-16 15:32:50 -0700432 calculate_zone_inactive_ratio(zone);
Christoph Lameter7ea15302007-10-16 01:25:29 -0700433 if (onlined_pages) {
434 kswapd_run(zone_to_nid(zone));
435 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
436 }
Dave Hansen61b13992005-10-29 18:16:56 -0700437
Yasunori Goto68113782006-06-23 02:03:11 -0700438 if (need_zonelists_rebuild)
439 build_all_zonelists();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700440 else
441 vm_total_pages = nr_free_pagecache_pages();
442
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700443 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700444
445 if (onlined_pages)
446 memory_notify(MEM_ONLINE, &arg);
447
Dave Hansen3947be12005-10-29 18:16:54 -0700448 return 0;
449}
Keith Mannthey53947022006-09-30 23:27:08 -0700450#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700451
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800452/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
453static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700454{
455 struct pglist_data *pgdat;
456 unsigned long zones_size[MAX_NR_ZONES] = {0};
457 unsigned long zholes_size[MAX_NR_ZONES] = {0};
458 unsigned long start_pfn = start >> PAGE_SHIFT;
459
460 pgdat = arch_alloc_nodedata(nid);
461 if (!pgdat)
462 return NULL;
463
464 arch_refresh_nodedata(nid, pgdat);
465
466 /* we can use NODE_DATA(nid) from here */
467
468 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -0700469 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700470
471 return pgdat;
472}
473
474static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
475{
476 arch_refresh_nodedata(nid, NULL);
477 arch_free_nodedata(pgdat);
478 return;
479}
480
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700481
Al Viro31168482008-11-22 17:33:24 +0000482/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
483int __ref add_memory(int nid, u64 start, u64 size)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700484{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700485 pg_data_t *pgdat = NULL;
486 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700487 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700488 int ret;
489
Andi Kleen6ad696d2009-11-17 14:06:22 -0800490 lock_system_sleep();
491
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700492 res = register_memory_resource(start, size);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800493 ret = -EEXIST;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700494 if (!res)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800495 goto out;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700496
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700497 if (!node_online(nid)) {
498 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800499 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700500 if (!pgdat)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800501 goto out;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700502 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700503 }
504
Yasunori Gotobc02af92006-06-27 02:53:30 -0700505 /* call arch's memory hotadd */
506 ret = arch_add_memory(nid, start, size);
507
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700508 if (ret < 0)
509 goto error;
510
Yasunori Goto0fc44152006-06-27 02:53:38 -0700511 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700512 node_set_online(nid);
513
Yasunori Goto0fc44152006-06-27 02:53:38 -0700514 if (new_pgdat) {
515 ret = register_one_node(nid);
516 /*
517 * If sysfs file of new node can't create, cpu on the node
518 * can't be hot-added. There is no rollback way now.
519 * So, check by BUG_ON() to catch it reluctantly..
520 */
521 BUG_ON(ret);
522 }
523
Andi Kleen6ad696d2009-11-17 14:06:22 -0800524 goto out;
525
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700526error:
527 /* rollback pgdat allocation and others */
528 if (new_pgdat)
529 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700530 if (res)
531 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700532
Andi Kleen6ad696d2009-11-17 14:06:22 -0800533out:
534 unlock_system_sleep();
Yasunori Gotobc02af92006-06-27 02:53:30 -0700535 return ret;
536}
537EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700538
539#ifdef CONFIG_MEMORY_HOTREMOVE
540/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700541 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
542 * set and the size of the free page is given by page_order(). Using this,
543 * the function determines if the pageblock contains only free pages.
544 * Due to buddy contraints, a free page at least the size of a pageblock will
545 * be located at the start of the pageblock
546 */
547static inline int pageblock_free(struct page *page)
548{
549 return PageBuddy(page) && page_order(page) >= pageblock_order;
550}
551
552/* Return the start of the next active pageblock after a given page */
553static struct page *next_active_pageblock(struct page *page)
554{
555 int pageblocks_stride;
556
557 /* Ensure the starting page is pageblock-aligned */
558 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
559
560 /* Move forward by at least 1 * pageblock_nr_pages */
561 pageblocks_stride = 1;
562
563 /* If the entire pageblock is free, move to the end of free page */
564 if (pageblock_free(page))
565 pageblocks_stride += page_order(page) - pageblock_order;
566
567 return page + (pageblocks_stride * pageblock_nr_pages);
568}
569
570/* Checks if this range of memory is likely to be hot-removable. */
571int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
572{
573 int type;
574 struct page *page = pfn_to_page(start_pfn);
575 struct page *end_page = page + nr_pages;
576
577 /* Check the starting page of each pageblock within the range */
578 for (; page < end_page; page = next_active_pageblock(page)) {
579 type = get_pageblock_migratetype(page);
580
581 /*
582 * A pageblock containing MOVABLE or free pages is considered
583 * removable
584 */
585 if (type != MIGRATE_MOVABLE && !pageblock_free(page))
586 return 0;
587
588 /*
589 * A pageblock starting with a PageReserved page is not
590 * considered removable.
591 */
592 if (PageReserved(page))
593 return 0;
594 }
595
596 /* All pageblocks in the memory block are likely to be hot-removable */
597 return 1;
598}
599
600/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700601 * Confirm all pages in a range [start, end) is belongs to the same zone.
602 */
603static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
604{
605 unsigned long pfn;
606 struct zone *zone = NULL;
607 struct page *page;
608 int i;
609 for (pfn = start_pfn;
610 pfn < end_pfn;
611 pfn += MAX_ORDER_NR_PAGES) {
612 i = 0;
613 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
614 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
615 i++;
616 if (i == MAX_ORDER_NR_PAGES)
617 continue;
618 page = pfn_to_page(pfn + i);
619 if (zone && page_zone(page) != zone)
620 return 0;
621 zone = page_zone(page);
622 }
623 return 1;
624}
625
626/*
627 * Scanning pfn is much easier than scanning lru list.
628 * Scan pfn from start to end and Find LRU page.
629 */
630int scan_lru_pages(unsigned long start, unsigned long end)
631{
632 unsigned long pfn;
633 struct page *page;
634 for (pfn = start; pfn < end; pfn++) {
635 if (pfn_valid(pfn)) {
636 page = pfn_to_page(pfn);
637 if (PageLRU(page))
638 return pfn;
639 }
640 }
641 return 0;
642}
643
644static struct page *
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800645hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700646{
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800647 /* This should be improooooved!! */
648 return alloc_page(GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700649}
650
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700651#define NR_OFFLINE_AT_ONCE_PAGES (256)
652static int
653do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
654{
655 unsigned long pfn;
656 struct page *page;
657 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
658 int not_managed = 0;
659 int ret = 0;
660 LIST_HEAD(source);
661
662 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
663 if (!pfn_valid(pfn))
664 continue;
665 page = pfn_to_page(pfn);
666 if (!page_count(page))
667 continue;
668 /*
669 * We can skip free pages. And we can only deal with pages on
670 * LRU.
671 */
Nick Piggin62695a82008-10-18 20:26:09 -0700672 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700673 if (!ret) { /* Success */
Nick Piggin62695a82008-10-18 20:26:09 -0700674 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700675 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800676 inc_zone_page_state(page, NR_ISOLATED_ANON +
677 page_is_file_cache(page));
678
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700679 } else {
680 /* Becasue we don't have big zone->lock. we should
681 check this again here. */
682 if (page_count(page))
683 not_managed++;
684#ifdef CONFIG_DEBUG_VM
685 printk(KERN_INFO "removing from LRU failed"
686 " %lx/%d/%lx\n",
687 pfn, page_count(page), page->flags);
688#endif
689 }
690 }
691 ret = -EBUSY;
692 if (not_managed) {
693 if (!list_empty(&source))
694 putback_lru_pages(&source);
695 goto out;
696 }
697 ret = 0;
698 if (list_empty(&source))
699 goto out;
700 /* this function returns # of failed pages */
701 ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
702
703out:
704 return ret;
705}
706
707/*
708 * remove from free_area[] and mark all as Reserved.
709 */
710static int
711offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
712 void *data)
713{
714 __offline_isolated_pages(start, start + nr_pages);
715 return 0;
716}
717
718static void
719offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
720{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700721 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700722 offline_isolated_pages_cb);
723}
724
725/*
726 * Check all pages in range, recoreded as memory resource, are isolated.
727 */
728static int
729check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
730 void *data)
731{
732 int ret;
733 long offlined = *(long *)data;
734 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
735 offlined = nr_pages;
736 if (!ret)
737 *(long *)data += offlined;
738 return ret;
739}
740
741static long
742check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
743{
744 long offlined = 0;
745 int ret;
746
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700747 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700748 check_pages_isolated_cb);
749 if (ret < 0)
750 offlined = (long)ret;
751 return offlined;
752}
753
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700754int offline_pages(unsigned long start_pfn,
755 unsigned long end_pfn, unsigned long timeout)
756{
757 unsigned long pfn, nr_pages, expire;
758 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700759 int ret, drain, retry_max, node;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700760 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700761 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700762
763 BUG_ON(start_pfn >= end_pfn);
764 /* at least, alignment against pageblock is necessary */
765 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
766 return -EINVAL;
767 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
768 return -EINVAL;
769 /* This makes hotplug much easier...and readable.
770 we assume this for now. .*/
771 if (!test_pages_in_a_zone(start_pfn, end_pfn))
772 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700773
Andi Kleen6ad696d2009-11-17 14:06:22 -0800774 lock_system_sleep();
775
Yasunori Goto7b78d332007-10-21 16:41:36 -0700776 zone = page_zone(pfn_to_page(start_pfn));
777 node = zone_to_nid(zone);
778 nr_pages = end_pfn - start_pfn;
779
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700780 /* set above range as isolated */
781 ret = start_isolate_page_range(start_pfn, end_pfn);
782 if (ret)
Andi Kleen6ad696d2009-11-17 14:06:22 -0800783 goto out;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700784
785 arg.start_pfn = start_pfn;
786 arg.nr_pages = nr_pages;
787 arg.status_change_nid = -1;
788 if (nr_pages >= node_present_pages(node))
789 arg.status_change_nid = node;
790
791 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
792 ret = notifier_to_errno(ret);
793 if (ret)
794 goto failed_removal;
795
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700796 pfn = start_pfn;
797 expire = jiffies + timeout;
798 drain = 0;
799 retry_max = 5;
800repeat:
801 /* start memory hot removal */
802 ret = -EAGAIN;
803 if (time_after(jiffies, expire))
804 goto failed_removal;
805 ret = -EINTR;
806 if (signal_pending(current))
807 goto failed_removal;
808 ret = 0;
809 if (drain) {
810 lru_add_drain_all();
811 flush_scheduled_work();
812 cond_resched();
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800813 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700814 }
815
816 pfn = scan_lru_pages(start_pfn, end_pfn);
817 if (pfn) { /* We have page on LRU */
818 ret = do_migrate_range(pfn, end_pfn);
819 if (!ret) {
820 drain = 1;
821 goto repeat;
822 } else {
823 if (ret < 0)
824 if (--retry_max == 0)
825 goto failed_removal;
826 yield();
827 drain = 1;
828 goto repeat;
829 }
830 }
831 /* drain all zone's lru pagevec, this is asyncronous... */
832 lru_add_drain_all();
833 flush_scheduled_work();
834 yield();
835 /* drain pcp pages , this is synchrouns. */
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800836 drain_all_pages();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700837 /* check again */
838 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
839 if (offlined_pages < 0) {
840 ret = -EBUSY;
841 goto failed_removal;
842 }
843 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
844 /* Ok, all of our target is islaoted.
845 We cannot do rollback at this point. */
846 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800847 /* reset pagetype flags and makes migrate type to be MOVABLE */
848 undo_isolate_page_range(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700849 /* removal success */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700850 zone->present_pages -= offlined_pages;
851 zone->zone_pgdat->node_present_pages -= offlined_pages;
852 totalram_pages -= offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700853
Minchan Kimbce73942009-06-16 15:32:50 -0700854 setup_per_zone_wmarks();
855 calculate_zone_inactive_ratio(zone);
David Rientjes8fe23e02009-12-14 17:58:33 -0800856 if (!node_present_pages(node)) {
857 node_clear_state(node, N_HIGH_MEMORY);
858 kswapd_stop(node);
859 }
Minchan Kimbce73942009-06-16 15:32:50 -0700860
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700861 vm_total_pages = nr_free_pagecache_pages();
862 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700863
864 memory_notify(MEM_OFFLINE, &arg);
Andi Kleen6ad696d2009-11-17 14:06:22 -0800865 unlock_system_sleep();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700866 return 0;
867
868failed_removal:
869 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
870 start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700871 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700872 /* pushback to free area */
873 undo_isolate_page_range(start_pfn, end_pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700874
Andi Kleen6ad696d2009-11-17 14:06:22 -0800875out:
876 unlock_system_sleep();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700877 return ret;
878}
Badari Pulavarty71088782008-10-18 20:25:58 -0700879
880int remove_memory(u64 start, u64 size)
881{
882 unsigned long start_pfn, end_pfn;
883
884 start_pfn = PFN_DOWN(start);
885 end_pfn = start_pfn + PFN_DOWN(size);
886 return offline_pages(start_pfn, end_pfn, 120 * HZ);
887}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -0700888#else
889int remove_memory(u64 start, u64 size)
890{
891 return -EINVAL;
892}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700893#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavarty71088782008-10-18 20:25:58 -0700894EXPORT_SYMBOL_GPL(remove_memory);