blob: a739dd1de1a8a91808145d9be9c2a6977654369a [file] [log] [blame]
Ethan Chen02db7362012-11-18 12:50:07 -08001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
7#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>
16#include <linux/writeback.h>
17#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>
24#include <linux/ioport.h>
25#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
28#include <linux/pfn.h>
29#include <linux/suspend.h>
30#include <linux/mm_inline.h>
31#include <linux/firmware-map.h>
32
33#include <asm/tlbflush.h>
34
35#include "internal.h"
36
37DEFINE_MUTEX(mem_hotplug_mutex);
38
39void lock_memory_hotplug(void)
40{
41 mutex_lock(&mem_hotplug_mutex);
42
43 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
44 lock_system_sleep();
45}
46
47void unlock_memory_hotplug(void)
48{
49 unlock_system_sleep();
50 mutex_unlock(&mem_hotplug_mutex);
51}
52
53
54/* add this memory to iomem resource */
55static struct resource *register_memory_resource(u64 start, u64 size)
56{
57 struct resource *res;
58 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
59 BUG_ON(!res);
60
61 res->name = "System RAM";
62 res->start = start;
63 res->end = start + size - 1;
64 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
65 if (request_resource(&iomem_resource, res) < 0) {
66 printk("System RAM resource %llx - %llx cannot be added\n",
67 (unsigned long long)res->start, (unsigned long long)res->end);
68 kfree(res);
69 res = NULL;
70 }
71 return res;
72}
73
74static void release_memory_resource(struct resource *res)
75{
76 if (!res)
77 return;
78 release_resource(res);
79 kfree(res);
80 return;
81}
82
83#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
84#ifndef CONFIG_SPARSEMEM_VMEMMAP
85static void get_page_bootmem(unsigned long info, struct page *page,
86 unsigned long type)
87{
88 page->lru.next = (struct list_head *) type;
89 SetPagePrivate(page);
90 set_page_private(page, info);
91 atomic_inc(&page->_count);
92}
93
94/* reference to __meminit __free_pages_bootmem is valid
95 * so use __ref to tell modpost not to generate a warning */
96void __ref put_page_bootmem(struct page *page)
97{
98 unsigned long type;
99
100 type = (unsigned long) page->lru.next;
101 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
102 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
103
104 if (atomic_dec_return(&page->_count) == 1) {
105 ClearPagePrivate(page);
106 set_page_private(page, 0);
107 INIT_LIST_HEAD(&page->lru);
108 __free_pages_bootmem(page, 0);
109 }
110
111}
112
113static void register_page_bootmem_info_section(unsigned long start_pfn)
114{
115 unsigned long *usemap, mapsize, section_nr, i;
116 struct mem_section *ms;
117 struct page *page, *memmap;
118
Ethan Chen02db7362012-11-18 12:50:07 -0800119 section_nr = pfn_to_section_nr(start_pfn);
120 ms = __nr_to_section(section_nr);
121
122 /* Get section's memmap address */
123 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
124
125 /*
126 * Get page for the memmap's phys address
127 * XXX: need more consideration for sparse_vmemmap...
128 */
129 page = virt_to_page(memmap);
130 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
131 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
132
133 /* remember memmap's page */
134 for (i = 0; i < mapsize; i++, page++)
135 get_page_bootmem(section_nr, page, SECTION_INFO);
136
137 usemap = __nr_to_section(section_nr)->pageblock_flags;
138 page = virt_to_page(usemap);
139
140 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
141
142 for (i = 0; i < mapsize; i++, page++)
143 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
144
145}
146
147void register_page_bootmem_info_node(struct pglist_data *pgdat)
148{
149 unsigned long i, pfn, end_pfn, nr_pages;
150 int node = pgdat->node_id;
151 struct page *page;
152 struct zone *zone;
153
154 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
155 page = virt_to_page(pgdat);
156
157 for (i = 0; i < nr_pages; i++, page++)
158 get_page_bootmem(node, page, NODE_INFO);
159
160 zone = &pgdat->node_zones[0];
161 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
162 if (zone->wait_table) {
163 nr_pages = zone->wait_table_hash_nr_entries
164 * sizeof(wait_queue_head_t);
165 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
166 page = virt_to_page(zone->wait_table);
167
168 for (i = 0; i < nr_pages; i++, page++)
169 get_page_bootmem(node, page, NODE_INFO);
170 }
171 }
172
173 pfn = pgdat->node_start_pfn;
174 end_pfn = pfn + pgdat->node_spanned_pages;
175
176 /* register_section info */
Ethan Chend81cb522012-11-22 21:10:05 -0800177 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
178 /*
179 * Some platforms can assign the same pfn to multiple nodes - on
180 * node0 as well as nodeN. To avoid registering a pfn against
181 * multiple nodes we check that this pfn does not already
182 * reside in some other node.
183 */
184 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
185 register_page_bootmem_info_section(pfn);
186 }
Ethan Chen02db7362012-11-18 12:50:07 -0800187}
188#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
189
190static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
191 unsigned long end_pfn)
192{
193 unsigned long old_zone_end_pfn;
194
195 zone_span_writelock(zone);
196
197 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
198 if (start_pfn < zone->zone_start_pfn)
199 zone->zone_start_pfn = start_pfn;
200
201 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
202 zone->zone_start_pfn;
203
204 zone_span_writeunlock(zone);
205}
206
207static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
208 unsigned long end_pfn)
209{
210 unsigned long old_pgdat_end_pfn =
211 pgdat->node_start_pfn + pgdat->node_spanned_pages;
212
213 if (start_pfn < pgdat->node_start_pfn)
214 pgdat->node_start_pfn = start_pfn;
215
216 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
217 pgdat->node_start_pfn;
218}
219
220static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
221{
222 struct pglist_data *pgdat = zone->zone_pgdat;
223 int nr_pages = PAGES_PER_SECTION;
224 int nid = pgdat->node_id;
225 int zone_type;
226 unsigned long flags;
227
228 zone_type = zone - pgdat->node_zones;
229 if (!zone->wait_table) {
230 int ret;
231
232 ret = init_currently_empty_zone(zone, phys_start_pfn,
233 nr_pages, MEMMAP_HOTPLUG);
234 if (ret)
235 return ret;
236 }
237 pgdat_resize_lock(zone->zone_pgdat, &flags);
238 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
239 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
240 phys_start_pfn + nr_pages);
241 pgdat_resize_unlock(zone->zone_pgdat, &flags);
242 memmap_init_zone(nr_pages, nid, zone_type,
243 phys_start_pfn, MEMMAP_HOTPLUG);
244 return 0;
245}
246
247static int __meminit __add_section(int nid, struct zone *zone,
248 unsigned long phys_start_pfn)
249{
250 int nr_pages = PAGES_PER_SECTION;
251 int ret;
252
253 if (pfn_valid(phys_start_pfn))
254 return -EEXIST;
255
256 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
257
258 if (ret < 0)
259 return ret;
260
261 ret = __add_zone(zone, phys_start_pfn);
262
263 if (ret < 0)
264 return ret;
265
266 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
267}
268
269#ifdef CONFIG_SPARSEMEM_VMEMMAP
270static int __remove_section(struct zone *zone, struct mem_section *ms)
271{
272 /*
273 * XXX: Freeing memmap with vmemmap is not implement yet.
274 * This should be removed later.
275 */
276 return -EBUSY;
277}
278#else
279static int __remove_section(struct zone *zone, struct mem_section *ms)
280{
281 unsigned long flags;
282 struct pglist_data *pgdat = zone->zone_pgdat;
283 int ret = -EINVAL;
284
285 if (!valid_section(ms))
286 return ret;
287
288 ret = unregister_memory_section(ms);
289 if (ret)
290 return ret;
291
292 pgdat_resize_lock(pgdat, &flags);
293 sparse_remove_one_section(zone, ms);
294 pgdat_resize_unlock(pgdat, &flags);
295 return 0;
296}
297#endif
298
299/*
300 * Reasonably generic function for adding memory. It is
301 * expected that archs that support memory hotplug will
302 * call this function after deciding the zone to which to
303 * add the new pages.
304 */
305int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
306 unsigned long nr_pages)
307{
308 unsigned long i;
309 int err = 0;
310 int start_sec, end_sec;
311 /* during initialize mem_map, align hot-added range to section */
312 start_sec = pfn_to_section_nr(phys_start_pfn);
313 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
314
315 for (i = start_sec; i <= end_sec; i++) {
316 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
317
318 /*
319 * EEXIST is finally dealt with by ioresource collision
320 * check. see add_memory() => register_memory_resource()
321 * Warning will be printed if there is collision.
322 */
323 if (err && (err != -EEXIST))
324 break;
325 err = 0;
326 }
327
328 return err;
329}
330EXPORT_SYMBOL_GPL(__add_pages);
331
332/**
333 * __remove_pages() - remove sections of pages from a zone
334 * @zone: zone from which pages need to be removed
335 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
336 * @nr_pages: number of pages to remove (must be multiple of section size)
337 *
338 * Generic helper function to remove section mappings and sysfs entries
339 * for the section of the memory we are removing. Caller needs to make
340 * sure that pages are marked reserved and zones are adjust properly by
341 * calling offline_pages().
342 */
343int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
344 unsigned long nr_pages)
345{
346 unsigned long i, ret = 0;
347 int sections_to_remove;
348
349 /*
350 * We can only remove entire sections
351 */
352 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
353 BUG_ON(nr_pages % PAGES_PER_SECTION);
354
355 sections_to_remove = nr_pages / PAGES_PER_SECTION;
356 for (i = 0; i < sections_to_remove; i++) {
357 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
358 release_mem_region(pfn << PAGE_SHIFT,
359 PAGES_PER_SECTION << PAGE_SHIFT);
360 ret = __remove_section(zone, __pfn_to_section(pfn));
361 if (ret)
362 break;
363 }
364 return ret;
365}
366EXPORT_SYMBOL_GPL(__remove_pages);
367
368void online_page(struct page *page)
369{
370 unsigned long pfn = page_to_pfn(page);
371
372 totalram_pages++;
373 if (pfn >= num_physpages)
374 num_physpages = pfn + 1;
375
376#ifdef CONFIG_HIGHMEM
377 if (PageHighMem(page))
378 totalhigh_pages++;
379#endif
380
381 ClearPageReserved(page);
382 init_page_count(page);
383 __free_page(page);
384}
385
386static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
387 void *arg)
388{
389 unsigned long i;
390 unsigned long onlined_pages = *(unsigned long *)arg;
391 struct page *page;
392 if (PageReserved(pfn_to_page(start_pfn)))
393 for (i = 0; i < nr_pages; i++) {
394 page = pfn_to_page(start_pfn + i);
395 online_page(page);
396 onlined_pages++;
397 }
398 *(unsigned long *)arg = onlined_pages;
399 return 0;
400}
401
402
403int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
404{
405 unsigned long onlined_pages = 0;
406 struct zone *zone;
407 int need_zonelists_rebuild = 0;
408 int nid;
409 int ret;
410 struct memory_notify arg;
411
412 lock_memory_hotplug();
413 arg.start_pfn = pfn;
414 arg.nr_pages = nr_pages;
415 arg.status_change_nid = -1;
416
417 nid = page_to_nid(pfn_to_page(pfn));
418 if (node_present_pages(nid) == 0)
419 arg.status_change_nid = nid;
420
421 ret = memory_notify(MEM_GOING_ONLINE, &arg);
422 ret = notifier_to_errno(ret);
423 if (ret) {
424 memory_notify(MEM_CANCEL_ONLINE, &arg);
425 unlock_memory_hotplug();
426 return ret;
427 }
428 /*
429 * This doesn't need a lock to do pfn_to_page().
430 * The section can't be removed here because of the
431 * memory_block->state_mutex.
432 */
433 zone = page_zone(pfn_to_page(pfn));
434 /*
435 * If this zone is not populated, then it is not in zonelist.
436 * This means the page allocator ignores this zone.
437 * So, zonelist must be updated after online.
438 */
439 mutex_lock(&zonelists_mutex);
440 if (!populated_zone(zone))
441 need_zonelists_rebuild = 1;
442
443 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
444 online_pages_range);
445 if (ret) {
446 mutex_unlock(&zonelists_mutex);
447 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
448 nr_pages, pfn);
449 memory_notify(MEM_CANCEL_ONLINE, &arg);
450 unlock_memory_hotplug();
451 return ret;
452 }
453
454 zone->present_pages += onlined_pages;
455 zone->zone_pgdat->node_present_pages += onlined_pages;
Dorian Snyder2b34a732013-04-05 21:10:55 -0700456 if (onlined_pages) {
457 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
458 if (need_zonelists_rebuild)
459 build_all_zonelists(zone);
460 else
461 zone_pcp_update(zone);
462 }
Ethan Chen02db7362012-11-18 12:50:07 -0800463
464 mutex_unlock(&zonelists_mutex);
465
466 init_per_zone_wmark_min();
467
Dorian Snyder2b34a732013-04-05 21:10:55 -0700468 if (onlined_pages)
Ethan Chen02db7362012-11-18 12:50:07 -0800469 kswapd_run(zone_to_nid(zone));
Ethan Chen02db7362012-11-18 12:50:07 -0800470
471 vm_total_pages = nr_free_pagecache_pages();
472
473 writeback_set_ratelimit();
474
475 if (onlined_pages)
476 memory_notify(MEM_ONLINE, &arg);
477 unlock_memory_hotplug();
478
479 return 0;
480}
481#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
482
483/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
484static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
485{
486 struct pglist_data *pgdat;
487 unsigned long zones_size[MAX_NR_ZONES] = {0};
488 unsigned long zholes_size[MAX_NR_ZONES] = {0};
489 unsigned long start_pfn = start >> PAGE_SHIFT;
490
491 pgdat = arch_alloc_nodedata(nid);
492 if (!pgdat)
493 return NULL;
494
495 arch_refresh_nodedata(nid, pgdat);
496
497 /* we can use NODE_DATA(nid) from here */
498
499 /* init node's zones as empty zones, we don't have any present pages.*/
500 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
501
502 /*
503 * The node we allocated has no zone fallback lists. For avoiding
504 * to access not-initialized zonelist, build here.
505 */
506 mutex_lock(&zonelists_mutex);
507 build_all_zonelists(NULL);
508 mutex_unlock(&zonelists_mutex);
509
510 return pgdat;
511}
512
513static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
514{
515 arch_refresh_nodedata(nid, NULL);
516 arch_free_nodedata(pgdat);
517 return;
518}
519
520
521/*
522 * called by cpu_up() to online a node without onlined memory.
523 */
524int mem_online_node(int nid)
525{
526 pg_data_t *pgdat;
527 int ret;
528
529 lock_memory_hotplug();
530 pgdat = hotadd_new_pgdat(nid, 0);
531 if (!pgdat) {
532 ret = -ENOMEM;
533 goto out;
534 }
535 node_set_online(nid);
536 ret = register_one_node(nid);
537 BUG_ON(ret);
538
539out:
540 unlock_memory_hotplug();
541 return ret;
542}
543
544/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
545int __ref add_memory(int nid, u64 start, u64 size)
546{
547 pg_data_t *pgdat = NULL;
548 int new_pgdat = 0;
549 struct resource *res;
550 int ret;
551
552 lock_memory_hotplug();
553
554 res = register_memory_resource(start, size);
555 ret = -EEXIST;
556 if (!res)
557 goto out;
558
559 if (!node_online(nid)) {
560 pgdat = hotadd_new_pgdat(nid, start);
561 ret = -ENOMEM;
562 if (!pgdat)
563 goto out;
564 new_pgdat = 1;
565 }
566
567 /* call arch's memory hotadd */
568 ret = arch_add_memory(nid, start, size);
569
570 if (ret < 0)
571 goto error;
572
573 /* we online node here. we can't roll back from here. */
574 node_set_online(nid);
575
576 if (new_pgdat) {
577 ret = register_one_node(nid);
578 /*
579 * If sysfs file of new node can't create, cpu on the node
580 * can't be hot-added. There is no rollback way now.
581 * So, check by BUG_ON() to catch it reluctantly..
582 */
583 BUG_ON(ret);
584 }
585
586 /* create new memmap entry */
587 firmware_map_add_hotplug(start, start + size, "System RAM");
588
589 goto out;
590
591error:
592 /* rollback pgdat allocation and others */
593 if (new_pgdat)
594 rollback_node_hotadd(nid, pgdat);
595 if (res)
596 release_memory_resource(res);
597
598out:
599 unlock_memory_hotplug();
600 return ret;
601}
602EXPORT_SYMBOL_GPL(add_memory);
603
604#ifdef CONFIG_MEMORY_HOTREMOVE
605/*
606 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
607 * set and the size of the free page is given by page_order(). Using this,
608 * the function determines if the pageblock contains only free pages.
609 * Due to buddy contraints, a free page at least the size of a pageblock will
610 * be located at the start of the pageblock
611 */
612static inline int pageblock_free(struct page *page)
613{
614 return PageBuddy(page) && page_order(page) >= pageblock_order;
615}
616
617/* Return the start of the next active pageblock after a given page */
618static struct page *next_active_pageblock(struct page *page)
619{
620 /* Ensure the starting page is pageblock-aligned */
621 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
622
623 /* If the entire pageblock is free, move to the end of free page */
624 if (pageblock_free(page)) {
625 int order;
626 /* be careful. we don't have locks, page_order can be changed.*/
627 order = page_order(page);
628 if ((order < MAX_ORDER) && (order >= pageblock_order))
629 return page + (1 << order);
630 }
631
632 return page + pageblock_nr_pages;
633}
634
635/* Checks if this range of memory is likely to be hot-removable. */
636int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
637{
638 struct page *page = pfn_to_page(start_pfn);
639 struct page *end_page = page + nr_pages;
640
641 /* Check the starting page of each pageblock within the range */
642 for (; page < end_page; page = next_active_pageblock(page)) {
643 if (!is_pageblock_removable_nolock(page))
644 return 0;
645 cond_resched();
646 }
647
648 /* All pageblocks in the memory block are likely to be hot-removable */
649 return 1;
650}
651
652/*
653 * Confirm all pages in a range [start, end) is belongs to the same zone.
654 */
655static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
656{
657 unsigned long pfn;
658 struct zone *zone = NULL;
659 struct page *page;
660 int i;
661 for (pfn = start_pfn;
662 pfn < end_pfn;
663 pfn += MAX_ORDER_NR_PAGES) {
664 i = 0;
665 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
666 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
667 i++;
668 if (i == MAX_ORDER_NR_PAGES)
669 continue;
670 page = pfn_to_page(pfn + i);
671 if (zone && page_zone(page) != zone)
672 return 0;
673 zone = page_zone(page);
674 }
675 return 1;
676}
677
678/*
679 * Scanning pfn is much easier than scanning lru list.
680 * Scan pfn from start to end and Find LRU page.
681 */
682static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
683{
684 unsigned long pfn;
685 struct page *page;
686 for (pfn = start; pfn < end; pfn++) {
687 if (pfn_valid(pfn)) {
688 page = pfn_to_page(pfn);
689 if (PageLRU(page))
690 return pfn;
691 }
692 }
693 return 0;
694}
695
696static struct page *
697hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
698{
699 /* This should be improooooved!! */
700 return alloc_page(GFP_HIGHUSER_MOVABLE);
701}
702
703#define NR_OFFLINE_AT_ONCE_PAGES (256)
704static int
705do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
706{
707 unsigned long pfn;
708 struct page *page;
709 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
710 int not_managed = 0;
711 int ret = 0;
712 LIST_HEAD(source);
713
714 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
715 if (!pfn_valid(pfn))
716 continue;
717 page = pfn_to_page(pfn);
718 if (!get_page_unless_zero(page))
719 continue;
720 /*
721 * We can skip free pages. And we can only deal with pages on
722 * LRU.
723 */
724 ret = isolate_lru_page(page);
725 if (!ret) { /* Success */
726 put_page(page);
727 list_add_tail(&page->lru, &source);
728 move_pages--;
729 inc_zone_page_state(page, NR_ISOLATED_ANON +
730 page_is_file_cache(page));
731
732 } else {
733#ifdef CONFIG_DEBUG_VM
734 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
735 pfn);
736 dump_page(page);
737#endif
738 put_page(page);
739 /* Because we don't have big zone->lock. we should
740 check this again here. */
741 if (page_count(page)) {
742 not_managed++;
743 ret = -EBUSY;
744 break;
745 }
746 }
747 }
748 if (!list_empty(&source)) {
749 if (not_managed) {
750 putback_lru_pages(&source);
751 goto out;
752 }
753 /* this function returns # of failed pages */
754 ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
Ethan Chen8602f3a2012-09-02 19:35:14 -0700755 true, MIGRATE_SYNC);
Ethan Chen02db7362012-11-18 12:50:07 -0800756 if (ret)
757 putback_lru_pages(&source);
758 }
759out:
760 return ret;
761}
762
763/*
764 * remove from free_area[] and mark all as Reserved.
765 */
766static int
767offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
768 void *data)
769{
770 __offline_isolated_pages(start, start + nr_pages);
771 return 0;
772}
773
774static void
775offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
776{
777 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
778 offline_isolated_pages_cb);
779}
780
781/*
782 * Check all pages in range, recoreded as memory resource, are isolated.
783 */
784static int
785check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
786 void *data)
787{
788 int ret;
789 long offlined = *(long *)data;
790 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
791 offlined = nr_pages;
792 if (!ret)
793 *(long *)data += offlined;
794 return ret;
795}
796
797static long
798check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
799{
800 long offlined = 0;
801 int ret;
802
803 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
804 check_pages_isolated_cb);
805 if (ret < 0)
806 offlined = (long)ret;
807 return offlined;
808}
809
810static int __ref offline_pages(unsigned long start_pfn,
811 unsigned long end_pfn, unsigned long timeout)
812{
813 unsigned long pfn, nr_pages, expire;
814 long offlined_pages;
815 int ret, drain, retry_max, node;
816 struct zone *zone;
817 struct memory_notify arg;
818
819 BUG_ON(start_pfn >= end_pfn);
820 /* at least, alignment against pageblock is necessary */
821 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
822 return -EINVAL;
823 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
824 return -EINVAL;
825 /* This makes hotplug much easier...and readable.
826 we assume this for now. .*/
827 if (!test_pages_in_a_zone(start_pfn, end_pfn))
828 return -EINVAL;
829
830 lock_memory_hotplug();
831
832 zone = page_zone(pfn_to_page(start_pfn));
833 node = zone_to_nid(zone);
834 nr_pages = end_pfn - start_pfn;
835
836 /* set above range as isolated */
837 ret = start_isolate_page_range(start_pfn, end_pfn);
838 if (ret)
839 goto out;
840
841 arg.start_pfn = start_pfn;
842 arg.nr_pages = nr_pages;
843 arg.status_change_nid = -1;
844 if (nr_pages >= node_present_pages(node))
845 arg.status_change_nid = node;
846
847 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
848 ret = notifier_to_errno(ret);
849 if (ret)
850 goto failed_removal;
851
852 pfn = start_pfn;
853 expire = jiffies + timeout;
854 drain = 0;
855 retry_max = 5;
856repeat:
857 /* start memory hot removal */
858 ret = -EAGAIN;
859 if (time_after(jiffies, expire))
860 goto failed_removal;
861 ret = -EINTR;
862 if (signal_pending(current))
863 goto failed_removal;
864 ret = 0;
865 if (drain) {
866 lru_add_drain_all();
867 cond_resched();
868 drain_all_pages();
869 }
870
871 pfn = scan_lru_pages(start_pfn, end_pfn);
872 if (pfn) { /* We have page on LRU */
873 ret = do_migrate_range(pfn, end_pfn);
874 if (!ret) {
875 drain = 1;
876 goto repeat;
877 } else {
878 if (ret < 0)
879 if (--retry_max == 0)
880 goto failed_removal;
881 yield();
882 drain = 1;
883 goto repeat;
884 }
885 }
886 /* drain all zone's lru pagevec, this is asyncronous... */
887 lru_add_drain_all();
888 yield();
889 /* drain pcp pages , this is synchrouns. */
890 drain_all_pages();
891 /* check again */
892 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
893 if (offlined_pages < 0) {
894 ret = -EBUSY;
895 goto failed_removal;
896 }
897 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
898 /* Ok, all of our target is islaoted.
899 We cannot do rollback at this point. */
900 offline_isolated_pages(start_pfn, end_pfn);
901 /* reset pagetype flags and makes migrate type to be MOVABLE */
902 undo_isolate_page_range(start_pfn, end_pfn);
903 /* removal success */
904 zone->present_pages -= offlined_pages;
905 zone->zone_pgdat->node_present_pages -= offlined_pages;
906 totalram_pages -= offlined_pages;
907
908 init_per_zone_wmark_min();
909
910 if (!node_present_pages(node)) {
911 node_clear_state(node, N_HIGH_MEMORY);
912 kswapd_stop(node);
913 }
914
915 vm_total_pages = nr_free_pagecache_pages();
916 writeback_set_ratelimit();
917
918 memory_notify(MEM_OFFLINE, &arg);
919 unlock_memory_hotplug();
920 return 0;
921
922failed_removal:
923 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
924 start_pfn, end_pfn);
925 memory_notify(MEM_CANCEL_OFFLINE, &arg);
926 /* pushback to free area */
927 undo_isolate_page_range(start_pfn, end_pfn);
928
929out:
930 unlock_memory_hotplug();
931 return ret;
932}
933
934int remove_memory(u64 start, u64 size)
935{
936 unsigned long start_pfn, end_pfn;
937
938 start_pfn = PFN_DOWN(start);
939 end_pfn = start_pfn + PFN_DOWN(size);
940 return offline_pages(start_pfn, end_pfn, 120 * HZ);
941}
942#else
943int remove_memory(u64 start, u64 size)
944{
945 return -EINVAL;
946}
947#endif /* CONFIG_MEMORY_HOTREMOVE */
948EXPORT_SYMBOL_GPL(remove_memory);