blob: 091b9c6c252973dd1cf5e89ffcf73de1afba173c [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>
Paul Jackson38837fc2006-09-29 02:01:16 -070025#include <linux/cpuset.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070026#include <linux/delay.h>
27#include <linux/migrate.h>
28#include <linux/page-isolation.h>
Dave Hansen3947be12005-10-29 18:16:54 -070029
30#include <asm/tlbflush.h>
31
Keith Mannthey45e0b782006-09-30 23:27:09 -070032/* add this memory to iomem resource */
33static struct resource *register_memory_resource(u64 start, u64 size)
34{
35 struct resource *res;
36 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
37 BUG_ON(!res);
38
39 res->name = "System RAM";
40 res->start = start;
41 res->end = start + size - 1;
42 res->flags = IORESOURCE_MEM;
43 if (request_resource(&iomem_resource, res) < 0) {
44 printk("System RAM resource %llx - %llx cannot be added\n",
45 (unsigned long long)res->start, (unsigned long long)res->end);
46 kfree(res);
47 res = NULL;
48 }
49 return res;
50}
51
52static void release_memory_resource(struct resource *res)
53{
54 if (!res)
55 return;
56 release_resource(res);
57 kfree(res);
58 return;
59}
60
61
Keith Mannthey53947022006-09-30 23:27:08 -070062#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasunori Goto718127c2006-06-23 02:03:10 -070063static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -070064{
65 struct pglist_data *pgdat = zone->zone_pgdat;
66 int nr_pages = PAGES_PER_SECTION;
67 int nid = pgdat->node_id;
68 int zone_type;
69
70 zone_type = zone - pgdat->node_zones;
Yasunori Goto13466c82007-06-01 00:46:53 -070071 if (!zone->wait_table) {
Yasunori Goto718127c2006-06-23 02:03:10 -070072 int ret = 0;
Dave Hansena2f3aa022007-01-10 23:15:30 -080073 ret = init_currently_empty_zone(zone, phys_start_pfn,
74 nr_pages, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -070075 if (ret < 0)
76 return ret;
77 }
Dave Hansena2f3aa022007-01-10 23:15:30 -080078 memmap_init_zone(nr_pages, nid, zone_type,
79 phys_start_pfn, MEMMAP_HOTPLUG);
Yasunori Goto718127c2006-06-23 02:03:10 -070080 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -070081}
82
Dave Hansen3947be12005-10-29 18:16:54 -070083static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
84{
Dave Hansen3947be12005-10-29 18:16:54 -070085 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -070086 int ret;
87
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -070088 if (pfn_valid(phys_start_pfn))
89 return -EEXIST;
90
Dave Hansen0b0acbe2005-10-29 18:16:55 -070091 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -070092
93 if (ret < 0)
94 return ret;
95
Yasunori Goto718127c2006-06-23 02:03:10 -070096 ret = __add_zone(zone, phys_start_pfn);
97
98 if (ret < 0)
99 return ret;
100
Dave Hansen3947be12005-10-29 18:16:54 -0700101 return register_new_memory(__pfn_to_section(phys_start_pfn));
102}
103
104/*
105 * Reasonably generic function for adding memory. It is
106 * expected that archs that support memory hotplug will
107 * call this function after deciding the zone to which to
108 * add the new pages.
109 */
110int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
111 unsigned long nr_pages)
112{
113 unsigned long i;
114 int err = 0;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700115 int start_sec, end_sec;
116 /* during initialize mem_map, align hot-added range to section */
117 start_sec = pfn_to_section_nr(phys_start_pfn);
118 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
Dave Hansen3947be12005-10-29 18:16:54 -0700119
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700120 for (i = start_sec; i <= end_sec; i++) {
121 err = __add_section(zone, i << PFN_SECTION_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700122
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700123 /*
124 * EEXIST is finally dealed with by ioresource collision
125 * check. see add_memory() => register_memory_resource()
126 * Warning will be printed if there is collision.
Joel H Schoppbed120c2006-05-01 12:16:11 -0700127 */
128 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -0700129 break;
KAMEZAWA Hiroyuki6f712712006-08-05 12:14:58 -0700130 err = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700131 }
132
133 return err;
134}
Joel H Schoppbed120c2006-05-01 12:16:11 -0700135EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700136
137static void grow_zone_span(struct zone *zone,
138 unsigned long start_pfn, unsigned long end_pfn)
139{
140 unsigned long old_zone_end_pfn;
141
142 zone_span_writelock(zone);
143
144 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
145 if (start_pfn < zone->zone_start_pfn)
146 zone->zone_start_pfn = start_pfn;
147
Yasunori Goto25a6df92006-05-30 21:25:42 -0700148 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
149 zone->zone_start_pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700150
151 zone_span_writeunlock(zone);
152}
153
154static void grow_pgdat_span(struct pglist_data *pgdat,
155 unsigned long start_pfn, unsigned long end_pfn)
156{
157 unsigned long old_pgdat_end_pfn =
158 pgdat->node_start_pfn + pgdat->node_spanned_pages;
159
160 if (start_pfn < pgdat->node_start_pfn)
161 pgdat->node_start_pfn = start_pfn;
162
Yasunori Goto25a6df92006-05-30 21:25:42 -0700163 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
164 pgdat->node_start_pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700165}
166
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700167static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
168 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700169{
170 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700171 unsigned long onlined_pages = *(unsigned long *)arg;
172 struct page *page;
173 if (PageReserved(pfn_to_page(start_pfn)))
174 for (i = 0; i < nr_pages; i++) {
175 page = pfn_to_page(start_pfn + i);
176 online_page(page);
177 onlined_pages++;
178 }
179 *(unsigned long *)arg = onlined_pages;
180 return 0;
181}
182
183
184int online_pages(unsigned long pfn, unsigned long nr_pages)
185{
Dave Hansen3947be12005-10-29 18:16:54 -0700186 unsigned long flags;
187 unsigned long onlined_pages = 0;
188 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700189 int need_zonelists_rebuild = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700190
191 /*
192 * This doesn't need a lock to do pfn_to_page().
193 * The section can't be removed here because of the
194 * memory_block->state_sem.
195 */
196 zone = page_zone(pfn_to_page(pfn));
197 pgdat_resize_lock(zone->zone_pgdat, &flags);
198 grow_zone_span(zone, pfn, pfn + nr_pages);
199 grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
200 pgdat_resize_unlock(zone->zone_pgdat, &flags);
201
Yasunori Goto68113782006-06-23 02:03:11 -0700202 /*
203 * If this zone is not populated, then it is not in zonelist.
204 * This means the page allocator ignores this zone.
205 * So, zonelist must be updated after online.
206 */
207 if (!populated_zone(zone))
208 need_zonelists_rebuild = 1;
209
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700210 walk_memory_resource(pfn, nr_pages, &onlined_pages,
211 online_pages_range);
Dave Hansen3947be12005-10-29 18:16:54 -0700212 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800213 zone->zone_pgdat->node_present_pages += onlined_pages;
Dave Hansen3947be12005-10-29 18:16:54 -0700214
Dave Hansen61b13992005-10-29 18:16:56 -0700215 setup_per_zone_pages_min();
Christoph Lameter7ea15302007-10-16 01:25:29 -0700216 if (onlined_pages) {
217 kswapd_run(zone_to_nid(zone));
218 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
219 }
Dave Hansen61b13992005-10-29 18:16:56 -0700220
Yasunori Goto68113782006-06-23 02:03:11 -0700221 if (need_zonelists_rebuild)
222 build_all_zonelists();
KAMEZAWA Hiroyuki5a4d4362006-06-23 02:03:47 -0700223 vm_total_pages = nr_free_pagecache_pages();
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700224 writeback_set_ratelimit();
Dave Hansen3947be12005-10-29 18:16:54 -0700225 return 0;
226}
Keith Mannthey53947022006-09-30 23:27:08 -0700227#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700228
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700229static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
230{
231 struct pglist_data *pgdat;
232 unsigned long zones_size[MAX_NR_ZONES] = {0};
233 unsigned long zholes_size[MAX_NR_ZONES] = {0};
234 unsigned long start_pfn = start >> PAGE_SHIFT;
235
236 pgdat = arch_alloc_nodedata(nid);
237 if (!pgdat)
238 return NULL;
239
240 arch_refresh_nodedata(nid, pgdat);
241
242 /* we can use NODE_DATA(nid) from here */
243
244 /* init node's zones as empty zones, we don't have any present pages.*/
245 free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
246
247 return pgdat;
248}
249
250static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
251{
252 arch_refresh_nodedata(nid, NULL);
253 arch_free_nodedata(pgdat);
254 return;
255}
256
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700257
Yasunori Gotobc02af92006-06-27 02:53:30 -0700258int add_memory(int nid, u64 start, u64 size)
259{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700260 pg_data_t *pgdat = NULL;
261 int new_pgdat = 0;
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700262 struct resource *res;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700263 int ret;
264
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700265 res = register_memory_resource(start, size);
266 if (!res)
267 return -EEXIST;
268
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700269 if (!node_online(nid)) {
270 pgdat = hotadd_new_pgdat(nid, start);
271 if (!pgdat)
272 return -ENOMEM;
273 new_pgdat = 1;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700274 }
275
Yasunori Gotobc02af92006-06-27 02:53:30 -0700276 /* call arch's memory hotadd */
277 ret = arch_add_memory(nid, start, size);
278
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700279 if (ret < 0)
280 goto error;
281
Yasunori Goto0fc44152006-06-27 02:53:38 -0700282 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700283 node_set_online(nid);
284
Paul Jackson38837fc2006-09-29 02:01:16 -0700285 cpuset_track_online_nodes();
286
Yasunori Goto0fc44152006-06-27 02:53:38 -0700287 if (new_pgdat) {
288 ret = register_one_node(nid);
289 /*
290 * If sysfs file of new node can't create, cpu on the node
291 * can't be hot-added. There is no rollback way now.
292 * So, check by BUG_ON() to catch it reluctantly..
293 */
294 BUG_ON(ret);
295 }
296
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700297 return ret;
298error:
299 /* rollback pgdat allocation and others */
300 if (new_pgdat)
301 rollback_node_hotadd(nid, pgdat);
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700302 if (res)
303 release_memory_resource(res);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700304
Yasunori Gotobc02af92006-06-27 02:53:30 -0700305 return ret;
306}
307EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700308
309#ifdef CONFIG_MEMORY_HOTREMOVE
310/*
311 * Confirm all pages in a range [start, end) is belongs to the same zone.
312 */
313static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
314{
315 unsigned long pfn;
316 struct zone *zone = NULL;
317 struct page *page;
318 int i;
319 for (pfn = start_pfn;
320 pfn < end_pfn;
321 pfn += MAX_ORDER_NR_PAGES) {
322 i = 0;
323 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
324 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
325 i++;
326 if (i == MAX_ORDER_NR_PAGES)
327 continue;
328 page = pfn_to_page(pfn + i);
329 if (zone && page_zone(page) != zone)
330 return 0;
331 zone = page_zone(page);
332 }
333 return 1;
334}
335
336/*
337 * Scanning pfn is much easier than scanning lru list.
338 * Scan pfn from start to end and Find LRU page.
339 */
340int scan_lru_pages(unsigned long start, unsigned long end)
341{
342 unsigned long pfn;
343 struct page *page;
344 for (pfn = start; pfn < end; pfn++) {
345 if (pfn_valid(pfn)) {
346 page = pfn_to_page(pfn);
347 if (PageLRU(page))
348 return pfn;
349 }
350 }
351 return 0;
352}
353
354static struct page *
355hotremove_migrate_alloc(struct page *page,
356 unsigned long private,
357 int **x)
358{
359 /* This should be improoooooved!! */
360 return alloc_page(GFP_HIGHUSER_PAGECACHE);
361}
362
363
364#define NR_OFFLINE_AT_ONCE_PAGES (256)
365static int
366do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
367{
368 unsigned long pfn;
369 struct page *page;
370 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
371 int not_managed = 0;
372 int ret = 0;
373 LIST_HEAD(source);
374
375 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
376 if (!pfn_valid(pfn))
377 continue;
378 page = pfn_to_page(pfn);
379 if (!page_count(page))
380 continue;
381 /*
382 * We can skip free pages. And we can only deal with pages on
383 * LRU.
384 */
385 ret = isolate_lru_page(page, &source);
386 if (!ret) { /* Success */
387 move_pages--;
388 } else {
389 /* Becasue we don't have big zone->lock. we should
390 check this again here. */
391 if (page_count(page))
392 not_managed++;
393#ifdef CONFIG_DEBUG_VM
394 printk(KERN_INFO "removing from LRU failed"
395 " %lx/%d/%lx\n",
396 pfn, page_count(page), page->flags);
397#endif
398 }
399 }
400 ret = -EBUSY;
401 if (not_managed) {
402 if (!list_empty(&source))
403 putback_lru_pages(&source);
404 goto out;
405 }
406 ret = 0;
407 if (list_empty(&source))
408 goto out;
409 /* this function returns # of failed pages */
410 ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
411
412out:
413 return ret;
414}
415
416/*
417 * remove from free_area[] and mark all as Reserved.
418 */
419static int
420offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
421 void *data)
422{
423 __offline_isolated_pages(start, start + nr_pages);
424 return 0;
425}
426
427static void
428offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
429{
430 walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL,
431 offline_isolated_pages_cb);
432}
433
434/*
435 * Check all pages in range, recoreded as memory resource, are isolated.
436 */
437static int
438check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
439 void *data)
440{
441 int ret;
442 long offlined = *(long *)data;
443 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
444 offlined = nr_pages;
445 if (!ret)
446 *(long *)data += offlined;
447 return ret;
448}
449
450static long
451check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
452{
453 long offlined = 0;
454 int ret;
455
456 ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined,
457 check_pages_isolated_cb);
458 if (ret < 0)
459 offlined = (long)ret;
460 return offlined;
461}
462
463extern void drain_all_local_pages(void);
464
465int offline_pages(unsigned long start_pfn,
466 unsigned long end_pfn, unsigned long timeout)
467{
468 unsigned long pfn, nr_pages, expire;
469 long offlined_pages;
470 int ret, drain, retry_max;
471 struct zone *zone;
472
473 BUG_ON(start_pfn >= end_pfn);
474 /* at least, alignment against pageblock is necessary */
475 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
476 return -EINVAL;
477 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
478 return -EINVAL;
479 /* This makes hotplug much easier...and readable.
480 we assume this for now. .*/
481 if (!test_pages_in_a_zone(start_pfn, end_pfn))
482 return -EINVAL;
483 /* set above range as isolated */
484 ret = start_isolate_page_range(start_pfn, end_pfn);
485 if (ret)
486 return ret;
487 nr_pages = end_pfn - start_pfn;
488 pfn = start_pfn;
489 expire = jiffies + timeout;
490 drain = 0;
491 retry_max = 5;
492repeat:
493 /* start memory hot removal */
494 ret = -EAGAIN;
495 if (time_after(jiffies, expire))
496 goto failed_removal;
497 ret = -EINTR;
498 if (signal_pending(current))
499 goto failed_removal;
500 ret = 0;
501 if (drain) {
502 lru_add_drain_all();
503 flush_scheduled_work();
504 cond_resched();
505 drain_all_local_pages();
506 }
507
508 pfn = scan_lru_pages(start_pfn, end_pfn);
509 if (pfn) { /* We have page on LRU */
510 ret = do_migrate_range(pfn, end_pfn);
511 if (!ret) {
512 drain = 1;
513 goto repeat;
514 } else {
515 if (ret < 0)
516 if (--retry_max == 0)
517 goto failed_removal;
518 yield();
519 drain = 1;
520 goto repeat;
521 }
522 }
523 /* drain all zone's lru pagevec, this is asyncronous... */
524 lru_add_drain_all();
525 flush_scheduled_work();
526 yield();
527 /* drain pcp pages , this is synchrouns. */
528 drain_all_local_pages();
529 /* check again */
530 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
531 if (offlined_pages < 0) {
532 ret = -EBUSY;
533 goto failed_removal;
534 }
535 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
536 /* Ok, all of our target is islaoted.
537 We cannot do rollback at this point. */
538 offline_isolated_pages(start_pfn, end_pfn);
539 /* reset pagetype flags */
540 start_isolate_page_range(start_pfn, end_pfn);
541 /* removal success */
542 zone = page_zone(pfn_to_page(start_pfn));
543 zone->present_pages -= offlined_pages;
544 zone->zone_pgdat->node_present_pages -= offlined_pages;
545 totalram_pages -= offlined_pages;
546 num_physpages -= offlined_pages;
547 vm_total_pages = nr_free_pagecache_pages();
548 writeback_set_ratelimit();
549 return 0;
550
551failed_removal:
552 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
553 start_pfn, end_pfn);
554 /* pushback to free area */
555 undo_isolate_page_range(start_pfn, end_pfn);
556 return ret;
557}
KAMEZAWA Hiroyuki48e94192007-10-16 01:26:14 -0700558#else
559int remove_memory(u64 start, u64 size)
560{
561 return -EINVAL;
562}
563EXPORT_SYMBOL_GPL(remove_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -0700564#endif /* CONFIG_MEMORY_HOTREMOVE */