2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.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>
32 #include <asm/tlbflush.h>
36 /* add this memory to iomem resource */
37 static struct resource *register_memory_resource(u64 start, u64 size)
40 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
43 res->name = "System RAM";
45 res->end = start + size - 1;
46 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
47 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);
56 static void release_memory_resource(struct resource *res)
60 release_resource(res);
65 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
66 #ifndef CONFIG_SPARSEMEM_VMEMMAP
67 static void get_page_bootmem(unsigned long info, struct page *page, int type)
69 atomic_set(&page->_mapcount, type);
71 set_page_private(page, info);
72 atomic_inc(&page->_count);
75 void put_page_bootmem(struct page *page)
79 type = atomic_read(&page->_mapcount);
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);
91 static void register_page_bootmem_info_section(unsigned long start_pfn)
93 unsigned long *usemap, mapsize, section_nr, i;
94 struct mem_section *ms;
95 struct page *page, *memmap;
97 if (!pfn_valid(start_pfn))
100 section_nr = pfn_to_section_nr(start_pfn);
101 ms = __nr_to_section(section_nr);
103 /* Get section's memmap address */
104 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
107 * Get page for the memmap's phys address
108 * XXX: need more consideration for sparse_vmemmap...
110 page = virt_to_page(memmap);
111 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
112 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
114 /* remember memmap's page */
115 for (i = 0; i < mapsize; i++, page++)
116 get_page_bootmem(section_nr, page, SECTION_INFO);
118 usemap = __nr_to_section(section_nr)->pageblock_flags;
119 page = virt_to_page(usemap);
121 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
123 for (i = 0; i < mapsize; i++, page++)
124 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
128 void register_page_bootmem_info_node(struct pglist_data *pgdat)
130 unsigned long i, pfn, end_pfn, nr_pages;
131 int node = pgdat->node_id;
135 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
136 page = virt_to_page(pgdat);
138 for (i = 0; i < nr_pages; i++, page++)
139 get_page_bootmem(node, page, NODE_INFO);
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);
149 for (i = 0; i < nr_pages; i++, page++)
150 get_page_bootmem(node, page, NODE_INFO);
154 pfn = pgdat->node_start_pfn;
155 end_pfn = pfn + pgdat->node_spanned_pages;
157 /* register_section info */
158 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
159 register_page_bootmem_info_section(pfn);
162 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
164 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
165 unsigned long end_pfn)
167 unsigned long old_zone_end_pfn;
169 zone_span_writelock(zone);
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;
175 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
176 zone->zone_start_pfn;
178 zone_span_writeunlock(zone);
181 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
182 unsigned long end_pfn)
184 unsigned long old_pgdat_end_pfn =
185 pgdat->node_start_pfn + pgdat->node_spanned_pages;
187 if (start_pfn < pgdat->node_start_pfn)
188 pgdat->node_start_pfn = start_pfn;
190 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
191 pgdat->node_start_pfn;
194 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
196 struct pglist_data *pgdat = zone->zone_pgdat;
197 int nr_pages = PAGES_PER_SECTION;
198 int nid = pgdat->node_id;
202 zone_type = zone - pgdat->node_zones;
203 if (!zone->wait_table) {
206 ret = init_currently_empty_zone(zone, phys_start_pfn,
207 nr_pages, MEMMAP_HOTPLUG);
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);
216 memmap_init_zone(nr_pages, nid, zone_type,
217 phys_start_pfn, MEMMAP_HOTPLUG);
221 static int __meminit __add_section(int nid, struct zone *zone,
222 unsigned long phys_start_pfn)
224 int nr_pages = PAGES_PER_SECTION;
227 if (pfn_valid(phys_start_pfn))
230 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
235 ret = __add_zone(zone, phys_start_pfn);
240 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
243 #ifdef CONFIG_SPARSEMEM_VMEMMAP
244 static int __remove_section(struct zone *zone, struct mem_section *ms)
247 * XXX: Freeing memmap with vmemmap is not implement yet.
248 * This should be removed later.
253 static int __remove_section(struct zone *zone, struct mem_section *ms)
256 struct pglist_data *pgdat = zone->zone_pgdat;
259 if (!valid_section(ms))
262 ret = unregister_memory_section(ms);
266 pgdat_resize_lock(pgdat, &flags);
267 sparse_remove_one_section(zone, ms);
268 pgdat_resize_unlock(pgdat, &flags);
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
279 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
280 unsigned long nr_pages)
284 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);
289 for (i = start_sec; i <= end_sec; i++) {
290 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
293 * EEXIST is finally dealt with by ioresource collision
294 * check. see add_memory() => register_memory_resource()
295 * Warning will be printed if there is collision.
297 if (err && (err != -EEXIST))
304 EXPORT_SYMBOL_GPL(__add_pages);
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)
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().
317 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
318 unsigned long nr_pages)
320 unsigned long i, ret = 0;
321 int sections_to_remove;
324 * We can only remove entire sections
326 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
327 BUG_ON(nr_pages % PAGES_PER_SECTION);
329 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;
332 release_mem_region(pfn << PAGE_SHIFT,
333 PAGES_PER_SECTION << PAGE_SHIFT);
334 ret = __remove_section(zone, __pfn_to_section(pfn));
340 EXPORT_SYMBOL_GPL(__remove_pages);
342 void online_page(struct page *page)
344 unsigned long pfn = page_to_pfn(page);
347 if (pfn >= num_physpages)
348 num_physpages = pfn + 1;
350 #ifdef CONFIG_HIGHMEM
351 if (PageHighMem(page))
355 #ifdef CONFIG_FLATMEM
356 max_mapnr = max(page_to_pfn(page), max_mapnr);
359 ClearPageReserved(page);
360 init_page_count(page);
364 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
368 unsigned long onlined_pages = *(unsigned long *)arg;
370 if (PageReserved(pfn_to_page(start_pfn)))
371 for (i = 0; i < nr_pages; i++) {
372 page = pfn_to_page(start_pfn + i);
376 *(unsigned long *)arg = onlined_pages;
381 int online_pages(unsigned long pfn, unsigned long nr_pages)
383 unsigned long onlined_pages = 0;
385 int need_zonelists_rebuild = 0;
388 struct memory_notify arg;
391 arg.nr_pages = nr_pages;
392 arg.status_change_nid = -1;
394 nid = page_to_nid(pfn_to_page(pfn));
395 if (node_present_pages(nid) == 0)
396 arg.status_change_nid = nid;
398 ret = memory_notify(MEM_GOING_ONLINE, &arg);
399 ret = notifier_to_errno(ret);
401 memory_notify(MEM_CANCEL_ONLINE, &arg);
405 * This doesn't need a lock to do pfn_to_page().
406 * The section can't be removed here because of the
407 * memory_block->state_mutex.
409 zone = page_zone(pfn_to_page(pfn));
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.
415 if (!populated_zone(zone))
416 need_zonelists_rebuild = 1;
418 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
421 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
423 memory_notify(MEM_CANCEL_ONLINE, &arg);
427 zone->present_pages += onlined_pages;
428 zone->zone_pgdat->node_present_pages += onlined_pages;
430 zone_pcp_update(zone);
431 setup_per_zone_wmarks();
432 calculate_zone_inactive_ratio(zone);
434 kswapd_run(zone_to_nid(zone));
435 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
438 if (need_zonelists_rebuild)
439 build_all_zonelists();
441 vm_total_pages = nr_free_pagecache_pages();
443 writeback_set_ratelimit();
446 memory_notify(MEM_ONLINE, &arg);
450 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
452 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
453 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
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;
460 pgdat = arch_alloc_nodedata(nid);
464 arch_refresh_nodedata(nid, pgdat);
466 /* we can use NODE_DATA(nid) from here */
468 /* init node's zones as empty zones, we don't have any present pages.*/
469 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
474 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
476 arch_refresh_nodedata(nid, NULL);
477 arch_free_nodedata(pgdat);
482 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
483 int __ref add_memory(int nid, u64 start, u64 size)
485 pg_data_t *pgdat = NULL;
487 struct resource *res;
492 res = register_memory_resource(start, size);
497 if (!node_online(nid)) {
498 pgdat = hotadd_new_pgdat(nid, start);
505 /* call arch's memory hotadd */
506 ret = arch_add_memory(nid, start, size);
511 /* we online node here. we can't roll back from here. */
512 node_set_online(nid);
515 ret = register_one_node(nid);
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..
527 /* rollback pgdat allocation and others */
529 rollback_node_hotadd(nid, pgdat);
531 release_memory_resource(res);
534 unlock_system_sleep();
537 EXPORT_SYMBOL_GPL(add_memory);
539 #ifdef CONFIG_MEMORY_HOTREMOVE
541 * 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
547 static inline int pageblock_free(struct page *page)
549 return PageBuddy(page) && page_order(page) >= pageblock_order;
552 /* Return the start of the next active pageblock after a given page */
553 static struct page *next_active_pageblock(struct page *page)
555 int pageblocks_stride;
557 /* Ensure the starting page is pageblock-aligned */
558 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
560 /* Move forward by at least 1 * pageblock_nr_pages */
561 pageblocks_stride = 1;
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;
567 return page + (pageblocks_stride * pageblock_nr_pages);
570 /* Checks if this range of memory is likely to be hot-removable. */
571 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
574 struct page *page = pfn_to_page(start_pfn);
575 struct page *end_page = page + nr_pages;
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);
582 * A pageblock containing MOVABLE or free pages is considered
585 if (type != MIGRATE_MOVABLE && !pageblock_free(page))
589 * A pageblock starting with a PageReserved page is not
590 * considered removable.
592 if (PageReserved(page))
596 /* All pageblocks in the memory block are likely to be hot-removable */
601 * Confirm all pages in a range [start, end) is belongs to the same zone.
603 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
606 struct zone *zone = NULL;
609 for (pfn = start_pfn;
611 pfn += MAX_ORDER_NR_PAGES) {
613 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
614 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
616 if (i == MAX_ORDER_NR_PAGES)
618 page = pfn_to_page(pfn + i);
619 if (zone && page_zone(page) != zone)
621 zone = page_zone(page);
627 * Scanning pfn is much easier than scanning lru list.
628 * Scan pfn from start to end and Find LRU page.
630 int scan_lru_pages(unsigned long start, unsigned long end)
634 for (pfn = start; pfn < end; pfn++) {
635 if (pfn_valid(pfn)) {
636 page = pfn_to_page(pfn);
645 hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
647 /* This should be improooooved!! */
648 return alloc_page(GFP_HIGHUSER_MOVABLE);
651 #define NR_OFFLINE_AT_ONCE_PAGES (256)
653 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
657 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
662 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
665 page = pfn_to_page(pfn);
666 if (!page_count(page))
669 * We can skip free pages. And we can only deal with pages on
672 ret = isolate_lru_page(page);
673 if (!ret) { /* Success */
674 list_add_tail(&page->lru, &source);
676 inc_zone_page_state(page, NR_ISOLATED_ANON +
677 page_is_file_cache(page));
680 /* Becasue we don't have big zone->lock. we should
681 check this again here. */
682 if (page_count(page))
684 #ifdef CONFIG_DEBUG_VM
685 printk(KERN_INFO "removing from LRU failed"
687 pfn, page_count(page), page->flags);
693 if (!list_empty(&source))
694 putback_lru_pages(&source);
698 if (list_empty(&source))
700 /* this function returns # of failed pages */
701 ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
708 * remove from free_area[] and mark all as Reserved.
711 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
714 __offline_isolated_pages(start, start + nr_pages);
719 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
721 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
722 offline_isolated_pages_cb);
726 * Check all pages in range, recoreded as memory resource, are isolated.
729 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
733 long offlined = *(long *)data;
734 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
737 *(long *)data += offlined;
742 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
747 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
748 check_pages_isolated_cb);
750 offlined = (long)ret;
754 int offline_pages(unsigned long start_pfn,
755 unsigned long end_pfn, unsigned long timeout)
757 unsigned long pfn, nr_pages, expire;
759 int ret, drain, retry_max, node;
761 struct memory_notify arg;
763 BUG_ON(start_pfn >= end_pfn);
764 /* at least, alignment against pageblock is necessary */
765 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
767 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
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))
776 zone = page_zone(pfn_to_page(start_pfn));
777 node = zone_to_nid(zone);
778 nr_pages = end_pfn - start_pfn;
780 /* set above range as isolated */
781 ret = start_isolate_page_range(start_pfn, end_pfn);
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;
791 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
792 ret = notifier_to_errno(ret);
797 expire = jiffies + timeout;
801 /* start memory hot removal */
803 if (time_after(jiffies, expire))
806 if (signal_pending(current))
811 flush_scheduled_work();
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);
824 if (--retry_max == 0)
831 /* drain all zone's lru pagevec, this is asyncronous... */
833 flush_scheduled_work();
835 /* drain pcp pages , this is synchrouns. */
838 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
839 if (offlined_pages < 0) {
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);
847 /* reset pagetype flags and makes migrate type to be MOVABLE */
848 undo_isolate_page_range(start_pfn, end_pfn);
849 /* removal success */
850 zone->present_pages -= offlined_pages;
851 zone->zone_pgdat->node_present_pages -= offlined_pages;
852 totalram_pages -= offlined_pages;
854 setup_per_zone_wmarks();
855 calculate_zone_inactive_ratio(zone);
856 if (!node_present_pages(node)) {
857 node_clear_state(node, N_HIGH_MEMORY);
861 vm_total_pages = nr_free_pagecache_pages();
862 writeback_set_ratelimit();
864 memory_notify(MEM_OFFLINE, &arg);
865 unlock_system_sleep();
869 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
871 memory_notify(MEM_CANCEL_OFFLINE, &arg);
872 /* pushback to free area */
873 undo_isolate_page_range(start_pfn, end_pfn);
876 unlock_system_sleep();
880 int remove_memory(u64 start, u64 size)
882 unsigned long start_pfn, end_pfn;
884 start_pfn = PFN_DOWN(start);
885 end_pfn = start_pfn + PFN_DOWN(size);
886 return offline_pages(start_pfn, end_pfn, 120 * HZ);
889 int remove_memory(u64 start, u64 size)
893 #endif /* CONFIG_MEMORY_HOTREMOVE */
894 EXPORT_SYMBOL_GPL(remove_memory);