13947be19SDave Hansen /* 23947be19SDave Hansen * linux/mm/memory_hotplug.c 33947be19SDave Hansen * 43947be19SDave Hansen * Copyright (C) 53947be19SDave Hansen */ 63947be19SDave Hansen 73947be19SDave Hansen #include <linux/stddef.h> 83947be19SDave Hansen #include <linux/mm.h> 93947be19SDave Hansen #include <linux/swap.h> 103947be19SDave Hansen #include <linux/interrupt.h> 113947be19SDave Hansen #include <linux/pagemap.h> 123947be19SDave Hansen #include <linux/bootmem.h> 133947be19SDave Hansen #include <linux/compiler.h> 143947be19SDave Hansen #include <linux/module.h> 153947be19SDave Hansen #include <linux/pagevec.h> 162d1d43f6SChandra Seetharaman #include <linux/writeback.h> 173947be19SDave Hansen #include <linux/slab.h> 183947be19SDave Hansen #include <linux/sysctl.h> 193947be19SDave Hansen #include <linux/cpu.h> 203947be19SDave Hansen #include <linux/memory.h> 213947be19SDave Hansen #include <linux/memory_hotplug.h> 223947be19SDave Hansen #include <linux/highmem.h> 233947be19SDave Hansen #include <linux/vmalloc.h> 240a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 2538837fc7SPaul Jackson #include <linux/cpuset.h> 260c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 270c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 280c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 293947be19SDave Hansen 303947be19SDave Hansen #include <asm/tlbflush.h> 313947be19SDave Hansen 321e5ad9a3SAdrian Bunk #include "internal.h" 331e5ad9a3SAdrian Bunk 3445e0b78bSKeith Mannthey /* add this memory to iomem resource */ 3545e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size) 3645e0b78bSKeith Mannthey { 3745e0b78bSKeith Mannthey struct resource *res; 3845e0b78bSKeith Mannthey res = kzalloc(sizeof(struct resource), GFP_KERNEL); 3945e0b78bSKeith Mannthey BUG_ON(!res); 4045e0b78bSKeith Mannthey 4145e0b78bSKeith Mannthey res->name = "System RAM"; 4245e0b78bSKeith Mannthey res->start = start; 4345e0b78bSKeith Mannthey res->end = start + size - 1; 44887c3cb1SYasunori Goto res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; 4545e0b78bSKeith Mannthey if (request_resource(&iomem_resource, res) < 0) { 4645e0b78bSKeith Mannthey printk("System RAM resource %llx - %llx cannot be added\n", 4745e0b78bSKeith Mannthey (unsigned long long)res->start, (unsigned long long)res->end); 4845e0b78bSKeith Mannthey kfree(res); 4945e0b78bSKeith Mannthey res = NULL; 5045e0b78bSKeith Mannthey } 5145e0b78bSKeith Mannthey return res; 5245e0b78bSKeith Mannthey } 5345e0b78bSKeith Mannthey 5445e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 5545e0b78bSKeith Mannthey { 5645e0b78bSKeith Mannthey if (!res) 5745e0b78bSKeith Mannthey return; 5845e0b78bSKeith Mannthey release_resource(res); 5945e0b78bSKeith Mannthey kfree(res); 6045e0b78bSKeith Mannthey return; 6145e0b78bSKeith Mannthey } 6245e0b78bSKeith Mannthey 6353947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 6404753278SYasunori Goto #ifndef CONFIG_SPARSEMEM_VMEMMAP 6504753278SYasunori Goto static void get_page_bootmem(unsigned long info, struct page *page, int magic) 6604753278SYasunori Goto { 6704753278SYasunori Goto atomic_set(&page->_mapcount, magic); 6804753278SYasunori Goto SetPagePrivate(page); 6904753278SYasunori Goto set_page_private(page, info); 7004753278SYasunori Goto atomic_inc(&page->_count); 7104753278SYasunori Goto } 7204753278SYasunori Goto 7304753278SYasunori Goto void put_page_bootmem(struct page *page) 7404753278SYasunori Goto { 7504753278SYasunori Goto int magic; 7604753278SYasunori Goto 7704753278SYasunori Goto magic = atomic_read(&page->_mapcount); 7804753278SYasunori Goto BUG_ON(magic >= -1); 7904753278SYasunori Goto 8004753278SYasunori Goto if (atomic_dec_return(&page->_count) == 1) { 8104753278SYasunori Goto ClearPagePrivate(page); 8204753278SYasunori Goto set_page_private(page, 0); 8304753278SYasunori Goto reset_page_mapcount(page); 8404753278SYasunori Goto __free_pages_bootmem(page, 0); 8504753278SYasunori Goto } 8604753278SYasunori Goto 8704753278SYasunori Goto } 8804753278SYasunori Goto 8904753278SYasunori Goto void register_page_bootmem_info_section(unsigned long start_pfn) 9004753278SYasunori Goto { 9104753278SYasunori Goto unsigned long *usemap, mapsize, section_nr, i; 9204753278SYasunori Goto struct mem_section *ms; 9304753278SYasunori Goto struct page *page, *memmap; 9404753278SYasunori Goto 9504753278SYasunori Goto if (!pfn_valid(start_pfn)) 9604753278SYasunori Goto return; 9704753278SYasunori Goto 9804753278SYasunori Goto section_nr = pfn_to_section_nr(start_pfn); 9904753278SYasunori Goto ms = __nr_to_section(section_nr); 10004753278SYasunori Goto 10104753278SYasunori Goto /* Get section's memmap address */ 10204753278SYasunori Goto memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 10304753278SYasunori Goto 10404753278SYasunori Goto /* 10504753278SYasunori Goto * Get page for the memmap's phys address 10604753278SYasunori Goto * XXX: need more consideration for sparse_vmemmap... 10704753278SYasunori Goto */ 10804753278SYasunori Goto page = virt_to_page(memmap); 10904753278SYasunori Goto mapsize = sizeof(struct page) * PAGES_PER_SECTION; 11004753278SYasunori Goto mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 11104753278SYasunori Goto 11204753278SYasunori Goto /* remember memmap's page */ 11304753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 11404753278SYasunori Goto get_page_bootmem(section_nr, page, SECTION_INFO); 11504753278SYasunori Goto 11604753278SYasunori Goto usemap = __nr_to_section(section_nr)->pageblock_flags; 11704753278SYasunori Goto page = virt_to_page(usemap); 11804753278SYasunori Goto 11904753278SYasunori Goto mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 12004753278SYasunori Goto 12104753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 12204753278SYasunori Goto get_page_bootmem(section_nr, page, MIX_INFO); 12304753278SYasunori Goto 12404753278SYasunori Goto } 12504753278SYasunori Goto 12604753278SYasunori Goto void register_page_bootmem_info_node(struct pglist_data *pgdat) 12704753278SYasunori Goto { 12804753278SYasunori Goto unsigned long i, pfn, end_pfn, nr_pages; 12904753278SYasunori Goto int node = pgdat->node_id; 13004753278SYasunori Goto struct page *page; 13104753278SYasunori Goto struct zone *zone; 13204753278SYasunori Goto 13304753278SYasunori Goto nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 13404753278SYasunori Goto page = virt_to_page(pgdat); 13504753278SYasunori Goto 13604753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 13704753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 13804753278SYasunori Goto 13904753278SYasunori Goto zone = &pgdat->node_zones[0]; 14004753278SYasunori Goto for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) { 14104753278SYasunori Goto if (zone->wait_table) { 14204753278SYasunori Goto nr_pages = zone->wait_table_hash_nr_entries 14304753278SYasunori Goto * sizeof(wait_queue_head_t); 14404753278SYasunori Goto nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT; 14504753278SYasunori Goto page = virt_to_page(zone->wait_table); 14604753278SYasunori Goto 14704753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 14804753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 14904753278SYasunori Goto } 15004753278SYasunori Goto } 15104753278SYasunori Goto 15204753278SYasunori Goto pfn = pgdat->node_start_pfn; 15304753278SYasunori Goto end_pfn = pfn + pgdat->node_spanned_pages; 15404753278SYasunori Goto 15504753278SYasunori Goto /* register_section info */ 15604753278SYasunori Goto for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) 15704753278SYasunori Goto register_page_bootmem_info_section(pfn); 15804753278SYasunori Goto 15904753278SYasunori Goto } 16004753278SYasunori Goto #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 16104753278SYasunori Goto 162718127ccSYasunori Goto static int __add_zone(struct zone *zone, unsigned long phys_start_pfn) 1633947be19SDave Hansen { 1643947be19SDave Hansen struct pglist_data *pgdat = zone->zone_pgdat; 1653947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 1663947be19SDave Hansen int nid = pgdat->node_id; 1673947be19SDave Hansen int zone_type; 1683947be19SDave Hansen 1693947be19SDave Hansen zone_type = zone - pgdat->node_zones; 170c3723ca3SHeiko Carstens if (!zone->wait_table) 171c3723ca3SHeiko Carstens return init_currently_empty_zone(zone, phys_start_pfn, 172a2f3aa02SDave Hansen nr_pages, MEMMAP_HOTPLUG); 173a2f3aa02SDave Hansen memmap_init_zone(nr_pages, nid, zone_type, 174a2f3aa02SDave Hansen phys_start_pfn, MEMMAP_HOTPLUG); 175718127ccSYasunori Goto return 0; 1763947be19SDave Hansen } 1773947be19SDave Hansen 1783947be19SDave Hansen static int __add_section(struct zone *zone, unsigned long phys_start_pfn) 1793947be19SDave Hansen { 1803947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 1813947be19SDave Hansen int ret; 1823947be19SDave Hansen 183ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 184ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 185ebd15302SKAMEZAWA Hiroyuki 1860b0acbecSDave Hansen ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); 1873947be19SDave Hansen 1883947be19SDave Hansen if (ret < 0) 1893947be19SDave Hansen return ret; 1903947be19SDave Hansen 191718127ccSYasunori Goto ret = __add_zone(zone, phys_start_pfn); 192718127ccSYasunori Goto 193718127ccSYasunori Goto if (ret < 0) 194718127ccSYasunori Goto return ret; 195718127ccSYasunori Goto 1963947be19SDave Hansen return register_new_memory(__pfn_to_section(phys_start_pfn)); 1973947be19SDave Hansen } 1983947be19SDave Hansen 1990c0a4a51SYasunori Goto #ifdef CONFIG_SPARSEMEM_VMEMMAP 2000c0a4a51SYasunori Goto static int __remove_section(struct zone *zone, struct mem_section *ms) 2010c0a4a51SYasunori Goto { 2020c0a4a51SYasunori Goto /* 2030c0a4a51SYasunori Goto * XXX: Freeing memmap with vmemmap is not implement yet. 2040c0a4a51SYasunori Goto * This should be removed later. 2050c0a4a51SYasunori Goto */ 2060c0a4a51SYasunori Goto return -EBUSY; 2070c0a4a51SYasunori Goto } 2080c0a4a51SYasunori Goto #else 209ea01ea93SBadari Pulavarty static int __remove_section(struct zone *zone, struct mem_section *ms) 210ea01ea93SBadari Pulavarty { 211ea01ea93SBadari Pulavarty unsigned long flags; 212ea01ea93SBadari Pulavarty struct pglist_data *pgdat = zone->zone_pgdat; 213ea01ea93SBadari Pulavarty int ret = -EINVAL; 214ea01ea93SBadari Pulavarty 215ea01ea93SBadari Pulavarty if (!valid_section(ms)) 216ea01ea93SBadari Pulavarty return ret; 217ea01ea93SBadari Pulavarty 218ea01ea93SBadari Pulavarty ret = unregister_memory_section(ms); 219ea01ea93SBadari Pulavarty if (ret) 220ea01ea93SBadari Pulavarty return ret; 221ea01ea93SBadari Pulavarty 222ea01ea93SBadari Pulavarty pgdat_resize_lock(pgdat, &flags); 223ea01ea93SBadari Pulavarty sparse_remove_one_section(zone, ms); 224ea01ea93SBadari Pulavarty pgdat_resize_unlock(pgdat, &flags); 225ea01ea93SBadari Pulavarty return 0; 226ea01ea93SBadari Pulavarty } 2270c0a4a51SYasunori Goto #endif 228ea01ea93SBadari Pulavarty 2293947be19SDave Hansen /* 2303947be19SDave Hansen * Reasonably generic function for adding memory. It is 2313947be19SDave Hansen * expected that archs that support memory hotplug will 2323947be19SDave Hansen * call this function after deciding the zone to which to 2333947be19SDave Hansen * add the new pages. 2343947be19SDave Hansen */ 2353947be19SDave Hansen int __add_pages(struct zone *zone, unsigned long phys_start_pfn, 2363947be19SDave Hansen unsigned long nr_pages) 2373947be19SDave Hansen { 2383947be19SDave Hansen unsigned long i; 2393947be19SDave Hansen int err = 0; 2406f712711SKAMEZAWA Hiroyuki int start_sec, end_sec; 2416f712711SKAMEZAWA Hiroyuki /* during initialize mem_map, align hot-added range to section */ 2426f712711SKAMEZAWA Hiroyuki start_sec = pfn_to_section_nr(phys_start_pfn); 2436f712711SKAMEZAWA Hiroyuki end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 2443947be19SDave Hansen 2456f712711SKAMEZAWA Hiroyuki for (i = start_sec; i <= end_sec; i++) { 2466f712711SKAMEZAWA Hiroyuki err = __add_section(zone, i << PFN_SECTION_SHIFT); 2473947be19SDave Hansen 2486f712711SKAMEZAWA Hiroyuki /* 249183ff22bSSimon Arlott * EEXIST is finally dealt with by ioresource collision 2506f712711SKAMEZAWA Hiroyuki * check. see add_memory() => register_memory_resource() 2516f712711SKAMEZAWA Hiroyuki * Warning will be printed if there is collision. 252bed120c6SJoel H Schopp */ 253bed120c6SJoel H Schopp if (err && (err != -EEXIST)) 2543947be19SDave Hansen break; 2556f712711SKAMEZAWA Hiroyuki err = 0; 2563947be19SDave Hansen } 2573947be19SDave Hansen 2583947be19SDave Hansen return err; 2593947be19SDave Hansen } 260bed120c6SJoel H Schopp EXPORT_SYMBOL_GPL(__add_pages); 2613947be19SDave Hansen 262ea01ea93SBadari Pulavarty /** 263ea01ea93SBadari Pulavarty * __remove_pages() - remove sections of pages from a zone 264ea01ea93SBadari Pulavarty * @zone: zone from which pages need to be removed 265ea01ea93SBadari Pulavarty * @phys_start_pfn: starting pageframe (must be aligned to start of a section) 266ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 267ea01ea93SBadari Pulavarty * 268ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 269ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 270ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 271ea01ea93SBadari Pulavarty * calling offline_pages(). 272ea01ea93SBadari Pulavarty */ 273ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, 274ea01ea93SBadari Pulavarty unsigned long nr_pages) 275ea01ea93SBadari Pulavarty { 276ea01ea93SBadari Pulavarty unsigned long i, ret = 0; 277ea01ea93SBadari Pulavarty int sections_to_remove; 278ea01ea93SBadari Pulavarty 279ea01ea93SBadari Pulavarty /* 280ea01ea93SBadari Pulavarty * We can only remove entire sections 281ea01ea93SBadari Pulavarty */ 282ea01ea93SBadari Pulavarty BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); 283ea01ea93SBadari Pulavarty BUG_ON(nr_pages % PAGES_PER_SECTION); 284ea01ea93SBadari Pulavarty 285ea01ea93SBadari Pulavarty release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE); 286ea01ea93SBadari Pulavarty 287ea01ea93SBadari Pulavarty sections_to_remove = nr_pages / PAGES_PER_SECTION; 288ea01ea93SBadari Pulavarty for (i = 0; i < sections_to_remove; i++) { 289ea01ea93SBadari Pulavarty unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; 290ea01ea93SBadari Pulavarty ret = __remove_section(zone, __pfn_to_section(pfn)); 291ea01ea93SBadari Pulavarty if (ret) 292ea01ea93SBadari Pulavarty break; 293ea01ea93SBadari Pulavarty } 294ea01ea93SBadari Pulavarty return ret; 295ea01ea93SBadari Pulavarty } 296ea01ea93SBadari Pulavarty EXPORT_SYMBOL_GPL(__remove_pages); 297ea01ea93SBadari Pulavarty 2983947be19SDave Hansen static void grow_zone_span(struct zone *zone, 2993947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 3003947be19SDave Hansen { 3013947be19SDave Hansen unsigned long old_zone_end_pfn; 3023947be19SDave Hansen 3033947be19SDave Hansen zone_span_writelock(zone); 3043947be19SDave Hansen 3053947be19SDave Hansen old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 3063947be19SDave Hansen if (start_pfn < zone->zone_start_pfn) 3073947be19SDave Hansen zone->zone_start_pfn = start_pfn; 3083947be19SDave Hansen 30925a6df95SYasunori Goto zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - 31025a6df95SYasunori Goto zone->zone_start_pfn; 3113947be19SDave Hansen 3123947be19SDave Hansen zone_span_writeunlock(zone); 3133947be19SDave Hansen } 3143947be19SDave Hansen 3153947be19SDave Hansen static void grow_pgdat_span(struct pglist_data *pgdat, 3163947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 3173947be19SDave Hansen { 3183947be19SDave Hansen unsigned long old_pgdat_end_pfn = 3193947be19SDave Hansen pgdat->node_start_pfn + pgdat->node_spanned_pages; 3203947be19SDave Hansen 3213947be19SDave Hansen if (start_pfn < pgdat->node_start_pfn) 3223947be19SDave Hansen pgdat->node_start_pfn = start_pfn; 3233947be19SDave Hansen 32425a6df95SYasunori Goto pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - 32525a6df95SYasunori Goto pgdat->node_start_pfn; 3263947be19SDave Hansen } 3273947be19SDave Hansen 328180c06efSJeremy Fitzhardinge void online_page(struct page *page) 329180c06efSJeremy Fitzhardinge { 330180c06efSJeremy Fitzhardinge totalram_pages++; 331180c06efSJeremy Fitzhardinge num_physpages++; 332180c06efSJeremy Fitzhardinge 333180c06efSJeremy Fitzhardinge #ifdef CONFIG_HIGHMEM 334180c06efSJeremy Fitzhardinge if (PageHighMem(page)) 335180c06efSJeremy Fitzhardinge totalhigh_pages++; 336180c06efSJeremy Fitzhardinge #endif 337180c06efSJeremy Fitzhardinge 338180c06efSJeremy Fitzhardinge #ifdef CONFIG_FLATMEM 339180c06efSJeremy Fitzhardinge max_mapnr = max(page_to_pfn(page), max_mapnr); 340180c06efSJeremy Fitzhardinge #endif 341180c06efSJeremy Fitzhardinge 342180c06efSJeremy Fitzhardinge ClearPageReserved(page); 343180c06efSJeremy Fitzhardinge init_page_count(page); 344180c06efSJeremy Fitzhardinge __free_page(page); 345180c06efSJeremy Fitzhardinge } 346180c06efSJeremy Fitzhardinge 34775884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, 34875884fb1SKAMEZAWA Hiroyuki void *arg) 3493947be19SDave Hansen { 3503947be19SDave Hansen unsigned long i; 35175884fb1SKAMEZAWA Hiroyuki unsigned long onlined_pages = *(unsigned long *)arg; 35275884fb1SKAMEZAWA Hiroyuki struct page *page; 35375884fb1SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) 35475884fb1SKAMEZAWA Hiroyuki for (i = 0; i < nr_pages; i++) { 35575884fb1SKAMEZAWA Hiroyuki page = pfn_to_page(start_pfn + i); 35675884fb1SKAMEZAWA Hiroyuki online_page(page); 35775884fb1SKAMEZAWA Hiroyuki onlined_pages++; 35875884fb1SKAMEZAWA Hiroyuki } 35975884fb1SKAMEZAWA Hiroyuki *(unsigned long *)arg = onlined_pages; 36075884fb1SKAMEZAWA Hiroyuki return 0; 36175884fb1SKAMEZAWA Hiroyuki } 36275884fb1SKAMEZAWA Hiroyuki 36375884fb1SKAMEZAWA Hiroyuki 36475884fb1SKAMEZAWA Hiroyuki int online_pages(unsigned long pfn, unsigned long nr_pages) 36575884fb1SKAMEZAWA Hiroyuki { 3663947be19SDave Hansen unsigned long flags; 3673947be19SDave Hansen unsigned long onlined_pages = 0; 3683947be19SDave Hansen struct zone *zone; 3696811378eSYasunori Goto int need_zonelists_rebuild = 0; 3707b78d335SYasunori Goto int nid; 3717b78d335SYasunori Goto int ret; 3727b78d335SYasunori Goto struct memory_notify arg; 3733947be19SDave Hansen 3747b78d335SYasunori Goto arg.start_pfn = pfn; 3757b78d335SYasunori Goto arg.nr_pages = nr_pages; 3767b78d335SYasunori Goto arg.status_change_nid = -1; 3777b78d335SYasunori Goto 3787b78d335SYasunori Goto nid = page_to_nid(pfn_to_page(pfn)); 3797b78d335SYasunori Goto if (node_present_pages(nid) == 0) 3807b78d335SYasunori Goto arg.status_change_nid = nid; 3817b78d335SYasunori Goto 3827b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 3837b78d335SYasunori Goto ret = notifier_to_errno(ret); 3847b78d335SYasunori Goto if (ret) { 3857b78d335SYasunori Goto memory_notify(MEM_CANCEL_ONLINE, &arg); 3867b78d335SYasunori Goto return ret; 3877b78d335SYasunori Goto } 3883947be19SDave Hansen /* 3893947be19SDave Hansen * This doesn't need a lock to do pfn_to_page(). 3903947be19SDave Hansen * The section can't be removed here because of the 391da19cbcfSDaniel Walker * memory_block->state_mutex. 3923947be19SDave Hansen */ 3933947be19SDave Hansen zone = page_zone(pfn_to_page(pfn)); 3943947be19SDave Hansen pgdat_resize_lock(zone->zone_pgdat, &flags); 3953947be19SDave Hansen grow_zone_span(zone, pfn, pfn + nr_pages); 3963947be19SDave Hansen grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages); 3973947be19SDave Hansen pgdat_resize_unlock(zone->zone_pgdat, &flags); 3983947be19SDave Hansen 3996811378eSYasunori Goto /* 4006811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 4016811378eSYasunori Goto * This means the page allocator ignores this zone. 4026811378eSYasunori Goto * So, zonelist must be updated after online. 4036811378eSYasunori Goto */ 4046811378eSYasunori Goto if (!populated_zone(zone)) 4056811378eSYasunori Goto need_zonelists_rebuild = 1; 4066811378eSYasunori Goto 407*fd8a4221SGeoff Levand ret = walk_memory_resource(pfn, nr_pages, &onlined_pages, 40875884fb1SKAMEZAWA Hiroyuki online_pages_range); 409*fd8a4221SGeoff Levand if (ret) { 410*fd8a4221SGeoff Levand printk(KERN_DEBUG "online_pages %lx at %lx failed\n", 411*fd8a4221SGeoff Levand nr_pages, pfn); 412*fd8a4221SGeoff Levand memory_notify(MEM_CANCEL_ONLINE, &arg); 413*fd8a4221SGeoff Levand return ret; 414*fd8a4221SGeoff Levand } 415*fd8a4221SGeoff Levand 4163947be19SDave Hansen zone->present_pages += onlined_pages; 417f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 4183947be19SDave Hansen 41961b13993SDave Hansen setup_per_zone_pages_min(); 4207ea1530aSChristoph Lameter if (onlined_pages) { 4217ea1530aSChristoph Lameter kswapd_run(zone_to_nid(zone)); 4227ea1530aSChristoph Lameter node_set_state(zone_to_nid(zone), N_HIGH_MEMORY); 4237ea1530aSChristoph Lameter } 42461b13993SDave Hansen 4256811378eSYasunori Goto if (need_zonelists_rebuild) 4266811378eSYasunori Goto build_all_zonelists(); 4275a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 4282d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 4297b78d335SYasunori Goto 4307b78d335SYasunori Goto if (onlined_pages) 4317b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 4327b78d335SYasunori Goto 4333947be19SDave Hansen return 0; 4343947be19SDave Hansen } 43553947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 436bc02af93SYasunori Goto 4379af3c2deSYasunori Goto static pg_data_t *hotadd_new_pgdat(int nid, u64 start) 4389af3c2deSYasunori Goto { 4399af3c2deSYasunori Goto struct pglist_data *pgdat; 4409af3c2deSYasunori Goto unsigned long zones_size[MAX_NR_ZONES] = {0}; 4419af3c2deSYasunori Goto unsigned long zholes_size[MAX_NR_ZONES] = {0}; 4429af3c2deSYasunori Goto unsigned long start_pfn = start >> PAGE_SHIFT; 4439af3c2deSYasunori Goto 4449af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 4459af3c2deSYasunori Goto if (!pgdat) 4469af3c2deSYasunori Goto return NULL; 4479af3c2deSYasunori Goto 4489af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 4499af3c2deSYasunori Goto 4509af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 4519af3c2deSYasunori Goto 4529af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 4539af3c2deSYasunori Goto free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size); 4549af3c2deSYasunori Goto 4559af3c2deSYasunori Goto return pgdat; 4569af3c2deSYasunori Goto } 4579af3c2deSYasunori Goto 4589af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat) 4599af3c2deSYasunori Goto { 4609af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 4619af3c2deSYasunori Goto arch_free_nodedata(pgdat); 4629af3c2deSYasunori Goto return; 4639af3c2deSYasunori Goto } 4649af3c2deSYasunori Goto 4650a547039SKAMEZAWA Hiroyuki 466bc02af93SYasunori Goto int add_memory(int nid, u64 start, u64 size) 467bc02af93SYasunori Goto { 4689af3c2deSYasunori Goto pg_data_t *pgdat = NULL; 4699af3c2deSYasunori Goto int new_pgdat = 0; 470ebd15302SKAMEZAWA Hiroyuki struct resource *res; 471bc02af93SYasunori Goto int ret; 472bc02af93SYasunori Goto 473ebd15302SKAMEZAWA Hiroyuki res = register_memory_resource(start, size); 474ebd15302SKAMEZAWA Hiroyuki if (!res) 475ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 476ebd15302SKAMEZAWA Hiroyuki 4779af3c2deSYasunori Goto if (!node_online(nid)) { 4789af3c2deSYasunori Goto pgdat = hotadd_new_pgdat(nid, start); 4799af3c2deSYasunori Goto if (!pgdat) 4809af3c2deSYasunori Goto return -ENOMEM; 4819af3c2deSYasunori Goto new_pgdat = 1; 4829af3c2deSYasunori Goto } 4839af3c2deSYasunori Goto 484bc02af93SYasunori Goto /* call arch's memory hotadd */ 485bc02af93SYasunori Goto ret = arch_add_memory(nid, start, size); 486bc02af93SYasunori Goto 4879af3c2deSYasunori Goto if (ret < 0) 4889af3c2deSYasunori Goto goto error; 4899af3c2deSYasunori Goto 4900fc44159SYasunori Goto /* we online node here. we can't roll back from here. */ 4919af3c2deSYasunori Goto node_set_online(nid); 4929af3c2deSYasunori Goto 49338837fc7SPaul Jackson cpuset_track_online_nodes(); 49438837fc7SPaul Jackson 4950fc44159SYasunori Goto if (new_pgdat) { 4960fc44159SYasunori Goto ret = register_one_node(nid); 4970fc44159SYasunori Goto /* 4980fc44159SYasunori Goto * If sysfs file of new node can't create, cpu on the node 4990fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 5000fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 5010fc44159SYasunori Goto */ 5020fc44159SYasunori Goto BUG_ON(ret); 5030fc44159SYasunori Goto } 5040fc44159SYasunori Goto 5059af3c2deSYasunori Goto return ret; 5069af3c2deSYasunori Goto error: 5079af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 5089af3c2deSYasunori Goto if (new_pgdat) 5099af3c2deSYasunori Goto rollback_node_hotadd(nid, pgdat); 510ebd15302SKAMEZAWA Hiroyuki if (res) 511ebd15302SKAMEZAWA Hiroyuki release_memory_resource(res); 5129af3c2deSYasunori Goto 513bc02af93SYasunori Goto return ret; 514bc02af93SYasunori Goto } 515bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 5160c0e6195SKAMEZAWA Hiroyuki 5170c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 5180c0e6195SKAMEZAWA Hiroyuki /* 5190c0e6195SKAMEZAWA Hiroyuki * Confirm all pages in a range [start, end) is belongs to the same zone. 5200c0e6195SKAMEZAWA Hiroyuki */ 5210c0e6195SKAMEZAWA Hiroyuki static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) 5220c0e6195SKAMEZAWA Hiroyuki { 5230c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 5240c0e6195SKAMEZAWA Hiroyuki struct zone *zone = NULL; 5250c0e6195SKAMEZAWA Hiroyuki struct page *page; 5260c0e6195SKAMEZAWA Hiroyuki int i; 5270c0e6195SKAMEZAWA Hiroyuki for (pfn = start_pfn; 5280c0e6195SKAMEZAWA Hiroyuki pfn < end_pfn; 5290c0e6195SKAMEZAWA Hiroyuki pfn += MAX_ORDER_NR_PAGES) { 5300c0e6195SKAMEZAWA Hiroyuki i = 0; 5310c0e6195SKAMEZAWA Hiroyuki /* This is just a CONFIG_HOLES_IN_ZONE check.*/ 5320c0e6195SKAMEZAWA Hiroyuki while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i)) 5330c0e6195SKAMEZAWA Hiroyuki i++; 5340c0e6195SKAMEZAWA Hiroyuki if (i == MAX_ORDER_NR_PAGES) 5350c0e6195SKAMEZAWA Hiroyuki continue; 5360c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn + i); 5370c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 5380c0e6195SKAMEZAWA Hiroyuki return 0; 5390c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 5400c0e6195SKAMEZAWA Hiroyuki } 5410c0e6195SKAMEZAWA Hiroyuki return 1; 5420c0e6195SKAMEZAWA Hiroyuki } 5430c0e6195SKAMEZAWA Hiroyuki 5440c0e6195SKAMEZAWA Hiroyuki /* 5450c0e6195SKAMEZAWA Hiroyuki * Scanning pfn is much easier than scanning lru list. 5460c0e6195SKAMEZAWA Hiroyuki * Scan pfn from start to end and Find LRU page. 5470c0e6195SKAMEZAWA Hiroyuki */ 5480c0e6195SKAMEZAWA Hiroyuki int scan_lru_pages(unsigned long start, unsigned long end) 5490c0e6195SKAMEZAWA Hiroyuki { 5500c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 5510c0e6195SKAMEZAWA Hiroyuki struct page *page; 5520c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 5530c0e6195SKAMEZAWA Hiroyuki if (pfn_valid(pfn)) { 5540c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 5550c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 5560c0e6195SKAMEZAWA Hiroyuki return pfn; 5570c0e6195SKAMEZAWA Hiroyuki } 5580c0e6195SKAMEZAWA Hiroyuki } 5590c0e6195SKAMEZAWA Hiroyuki return 0; 5600c0e6195SKAMEZAWA Hiroyuki } 5610c0e6195SKAMEZAWA Hiroyuki 5620c0e6195SKAMEZAWA Hiroyuki static struct page * 5630c0e6195SKAMEZAWA Hiroyuki hotremove_migrate_alloc(struct page *page, 5640c0e6195SKAMEZAWA Hiroyuki unsigned long private, 5650c0e6195SKAMEZAWA Hiroyuki int **x) 5660c0e6195SKAMEZAWA Hiroyuki { 5670c0e6195SKAMEZAWA Hiroyuki /* This should be improoooooved!! */ 5680c0e6195SKAMEZAWA Hiroyuki return alloc_page(GFP_HIGHUSER_PAGECACHE); 5690c0e6195SKAMEZAWA Hiroyuki } 5700c0e6195SKAMEZAWA Hiroyuki 5710c0e6195SKAMEZAWA Hiroyuki 5720c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES (256) 5730c0e6195SKAMEZAWA Hiroyuki static int 5740c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 5750c0e6195SKAMEZAWA Hiroyuki { 5760c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 5770c0e6195SKAMEZAWA Hiroyuki struct page *page; 5780c0e6195SKAMEZAWA Hiroyuki int move_pages = NR_OFFLINE_AT_ONCE_PAGES; 5790c0e6195SKAMEZAWA Hiroyuki int not_managed = 0; 5800c0e6195SKAMEZAWA Hiroyuki int ret = 0; 5810c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 5820c0e6195SKAMEZAWA Hiroyuki 5830c0e6195SKAMEZAWA Hiroyuki for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { 5840c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 5850c0e6195SKAMEZAWA Hiroyuki continue; 5860c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 5870c0e6195SKAMEZAWA Hiroyuki if (!page_count(page)) 5880c0e6195SKAMEZAWA Hiroyuki continue; 5890c0e6195SKAMEZAWA Hiroyuki /* 5900c0e6195SKAMEZAWA Hiroyuki * We can skip free pages. And we can only deal with pages on 5910c0e6195SKAMEZAWA Hiroyuki * LRU. 5920c0e6195SKAMEZAWA Hiroyuki */ 5930c0e6195SKAMEZAWA Hiroyuki ret = isolate_lru_page(page, &source); 5940c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 5950c0e6195SKAMEZAWA Hiroyuki move_pages--; 5960c0e6195SKAMEZAWA Hiroyuki } else { 5970c0e6195SKAMEZAWA Hiroyuki /* Becasue we don't have big zone->lock. we should 5980c0e6195SKAMEZAWA Hiroyuki check this again here. */ 5990c0e6195SKAMEZAWA Hiroyuki if (page_count(page)) 6000c0e6195SKAMEZAWA Hiroyuki not_managed++; 6010c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_DEBUG_VM 6020c0e6195SKAMEZAWA Hiroyuki printk(KERN_INFO "removing from LRU failed" 6030c0e6195SKAMEZAWA Hiroyuki " %lx/%d/%lx\n", 6040c0e6195SKAMEZAWA Hiroyuki pfn, page_count(page), page->flags); 6050c0e6195SKAMEZAWA Hiroyuki #endif 6060c0e6195SKAMEZAWA Hiroyuki } 6070c0e6195SKAMEZAWA Hiroyuki } 6080c0e6195SKAMEZAWA Hiroyuki ret = -EBUSY; 6090c0e6195SKAMEZAWA Hiroyuki if (not_managed) { 6100c0e6195SKAMEZAWA Hiroyuki if (!list_empty(&source)) 6110c0e6195SKAMEZAWA Hiroyuki putback_lru_pages(&source); 6120c0e6195SKAMEZAWA Hiroyuki goto out; 6130c0e6195SKAMEZAWA Hiroyuki } 6140c0e6195SKAMEZAWA Hiroyuki ret = 0; 6150c0e6195SKAMEZAWA Hiroyuki if (list_empty(&source)) 6160c0e6195SKAMEZAWA Hiroyuki goto out; 6170c0e6195SKAMEZAWA Hiroyuki /* this function returns # of failed pages */ 6180c0e6195SKAMEZAWA Hiroyuki ret = migrate_pages(&source, hotremove_migrate_alloc, 0); 6190c0e6195SKAMEZAWA Hiroyuki 6200c0e6195SKAMEZAWA Hiroyuki out: 6210c0e6195SKAMEZAWA Hiroyuki return ret; 6220c0e6195SKAMEZAWA Hiroyuki } 6230c0e6195SKAMEZAWA Hiroyuki 6240c0e6195SKAMEZAWA Hiroyuki /* 6250c0e6195SKAMEZAWA Hiroyuki * remove from free_area[] and mark all as Reserved. 6260c0e6195SKAMEZAWA Hiroyuki */ 6270c0e6195SKAMEZAWA Hiroyuki static int 6280c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, 6290c0e6195SKAMEZAWA Hiroyuki void *data) 6300c0e6195SKAMEZAWA Hiroyuki { 6310c0e6195SKAMEZAWA Hiroyuki __offline_isolated_pages(start, start + nr_pages); 6320c0e6195SKAMEZAWA Hiroyuki return 0; 6330c0e6195SKAMEZAWA Hiroyuki } 6340c0e6195SKAMEZAWA Hiroyuki 6350c0e6195SKAMEZAWA Hiroyuki static void 6360c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) 6370c0e6195SKAMEZAWA Hiroyuki { 6380c0e6195SKAMEZAWA Hiroyuki walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL, 6390c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb); 6400c0e6195SKAMEZAWA Hiroyuki } 6410c0e6195SKAMEZAWA Hiroyuki 6420c0e6195SKAMEZAWA Hiroyuki /* 6430c0e6195SKAMEZAWA Hiroyuki * Check all pages in range, recoreded as memory resource, are isolated. 6440c0e6195SKAMEZAWA Hiroyuki */ 6450c0e6195SKAMEZAWA Hiroyuki static int 6460c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, 6470c0e6195SKAMEZAWA Hiroyuki void *data) 6480c0e6195SKAMEZAWA Hiroyuki { 6490c0e6195SKAMEZAWA Hiroyuki int ret; 6500c0e6195SKAMEZAWA Hiroyuki long offlined = *(long *)data; 6510c0e6195SKAMEZAWA Hiroyuki ret = test_pages_isolated(start_pfn, start_pfn + nr_pages); 6520c0e6195SKAMEZAWA Hiroyuki offlined = nr_pages; 6530c0e6195SKAMEZAWA Hiroyuki if (!ret) 6540c0e6195SKAMEZAWA Hiroyuki *(long *)data += offlined; 6550c0e6195SKAMEZAWA Hiroyuki return ret; 6560c0e6195SKAMEZAWA Hiroyuki } 6570c0e6195SKAMEZAWA Hiroyuki 6580c0e6195SKAMEZAWA Hiroyuki static long 6590c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) 6600c0e6195SKAMEZAWA Hiroyuki { 6610c0e6195SKAMEZAWA Hiroyuki long offlined = 0; 6620c0e6195SKAMEZAWA Hiroyuki int ret; 6630c0e6195SKAMEZAWA Hiroyuki 6640c0e6195SKAMEZAWA Hiroyuki ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined, 6650c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb); 6660c0e6195SKAMEZAWA Hiroyuki if (ret < 0) 6670c0e6195SKAMEZAWA Hiroyuki offlined = (long)ret; 6680c0e6195SKAMEZAWA Hiroyuki return offlined; 6690c0e6195SKAMEZAWA Hiroyuki } 6700c0e6195SKAMEZAWA Hiroyuki 6710c0e6195SKAMEZAWA Hiroyuki int offline_pages(unsigned long start_pfn, 6720c0e6195SKAMEZAWA Hiroyuki unsigned long end_pfn, unsigned long timeout) 6730c0e6195SKAMEZAWA Hiroyuki { 6740c0e6195SKAMEZAWA Hiroyuki unsigned long pfn, nr_pages, expire; 6750c0e6195SKAMEZAWA Hiroyuki long offlined_pages; 6767b78d335SYasunori Goto int ret, drain, retry_max, node; 6770c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 6787b78d335SYasunori Goto struct memory_notify arg; 6790c0e6195SKAMEZAWA Hiroyuki 6800c0e6195SKAMEZAWA Hiroyuki BUG_ON(start_pfn >= end_pfn); 6810c0e6195SKAMEZAWA Hiroyuki /* at least, alignment against pageblock is necessary */ 6820c0e6195SKAMEZAWA Hiroyuki if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) 6830c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 6840c0e6195SKAMEZAWA Hiroyuki if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) 6850c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 6860c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 6870c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 6880c0e6195SKAMEZAWA Hiroyuki if (!test_pages_in_a_zone(start_pfn, end_pfn)) 6890c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 6907b78d335SYasunori Goto 6917b78d335SYasunori Goto zone = page_zone(pfn_to_page(start_pfn)); 6927b78d335SYasunori Goto node = zone_to_nid(zone); 6937b78d335SYasunori Goto nr_pages = end_pfn - start_pfn; 6947b78d335SYasunori Goto 6950c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 6960c0e6195SKAMEZAWA Hiroyuki ret = start_isolate_page_range(start_pfn, end_pfn); 6970c0e6195SKAMEZAWA Hiroyuki if (ret) 6980c0e6195SKAMEZAWA Hiroyuki return ret; 6997b78d335SYasunori Goto 7007b78d335SYasunori Goto arg.start_pfn = start_pfn; 7017b78d335SYasunori Goto arg.nr_pages = nr_pages; 7027b78d335SYasunori Goto arg.status_change_nid = -1; 7037b78d335SYasunori Goto if (nr_pages >= node_present_pages(node)) 7047b78d335SYasunori Goto arg.status_change_nid = node; 7057b78d335SYasunori Goto 7067b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 7077b78d335SYasunori Goto ret = notifier_to_errno(ret); 7087b78d335SYasunori Goto if (ret) 7097b78d335SYasunori Goto goto failed_removal; 7107b78d335SYasunori Goto 7110c0e6195SKAMEZAWA Hiroyuki pfn = start_pfn; 7120c0e6195SKAMEZAWA Hiroyuki expire = jiffies + timeout; 7130c0e6195SKAMEZAWA Hiroyuki drain = 0; 7140c0e6195SKAMEZAWA Hiroyuki retry_max = 5; 7150c0e6195SKAMEZAWA Hiroyuki repeat: 7160c0e6195SKAMEZAWA Hiroyuki /* start memory hot removal */ 7170c0e6195SKAMEZAWA Hiroyuki ret = -EAGAIN; 7180c0e6195SKAMEZAWA Hiroyuki if (time_after(jiffies, expire)) 7190c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 7200c0e6195SKAMEZAWA Hiroyuki ret = -EINTR; 7210c0e6195SKAMEZAWA Hiroyuki if (signal_pending(current)) 7220c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 7230c0e6195SKAMEZAWA Hiroyuki ret = 0; 7240c0e6195SKAMEZAWA Hiroyuki if (drain) { 7250c0e6195SKAMEZAWA Hiroyuki lru_add_drain_all(); 7260c0e6195SKAMEZAWA Hiroyuki flush_scheduled_work(); 7270c0e6195SKAMEZAWA Hiroyuki cond_resched(); 7289f8f2172SChristoph Lameter drain_all_pages(); 7290c0e6195SKAMEZAWA Hiroyuki } 7300c0e6195SKAMEZAWA Hiroyuki 7310c0e6195SKAMEZAWA Hiroyuki pfn = scan_lru_pages(start_pfn, end_pfn); 7320c0e6195SKAMEZAWA Hiroyuki if (pfn) { /* We have page on LRU */ 7330c0e6195SKAMEZAWA Hiroyuki ret = do_migrate_range(pfn, end_pfn); 7340c0e6195SKAMEZAWA Hiroyuki if (!ret) { 7350c0e6195SKAMEZAWA Hiroyuki drain = 1; 7360c0e6195SKAMEZAWA Hiroyuki goto repeat; 7370c0e6195SKAMEZAWA Hiroyuki } else { 7380c0e6195SKAMEZAWA Hiroyuki if (ret < 0) 7390c0e6195SKAMEZAWA Hiroyuki if (--retry_max == 0) 7400c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 7410c0e6195SKAMEZAWA Hiroyuki yield(); 7420c0e6195SKAMEZAWA Hiroyuki drain = 1; 7430c0e6195SKAMEZAWA Hiroyuki goto repeat; 7440c0e6195SKAMEZAWA Hiroyuki } 7450c0e6195SKAMEZAWA Hiroyuki } 7460c0e6195SKAMEZAWA Hiroyuki /* drain all zone's lru pagevec, this is asyncronous... */ 7470c0e6195SKAMEZAWA Hiroyuki lru_add_drain_all(); 7480c0e6195SKAMEZAWA Hiroyuki flush_scheduled_work(); 7490c0e6195SKAMEZAWA Hiroyuki yield(); 7500c0e6195SKAMEZAWA Hiroyuki /* drain pcp pages , this is synchrouns. */ 7519f8f2172SChristoph Lameter drain_all_pages(); 7520c0e6195SKAMEZAWA Hiroyuki /* check again */ 7530c0e6195SKAMEZAWA Hiroyuki offlined_pages = check_pages_isolated(start_pfn, end_pfn); 7540c0e6195SKAMEZAWA Hiroyuki if (offlined_pages < 0) { 7550c0e6195SKAMEZAWA Hiroyuki ret = -EBUSY; 7560c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 7570c0e6195SKAMEZAWA Hiroyuki } 7580c0e6195SKAMEZAWA Hiroyuki printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); 7590c0e6195SKAMEZAWA Hiroyuki /* Ok, all of our target is islaoted. 7600c0e6195SKAMEZAWA Hiroyuki We cannot do rollback at this point. */ 7610c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(start_pfn, end_pfn); 762dbc0e4ceSKAMEZAWA Hiroyuki /* reset pagetype flags and makes migrate type to be MOVABLE */ 763dbc0e4ceSKAMEZAWA Hiroyuki undo_isolate_page_range(start_pfn, end_pfn); 7640c0e6195SKAMEZAWA Hiroyuki /* removal success */ 7650c0e6195SKAMEZAWA Hiroyuki zone->present_pages -= offlined_pages; 7660c0e6195SKAMEZAWA Hiroyuki zone->zone_pgdat->node_present_pages -= offlined_pages; 7670c0e6195SKAMEZAWA Hiroyuki totalram_pages -= offlined_pages; 7680c0e6195SKAMEZAWA Hiroyuki num_physpages -= offlined_pages; 7697b78d335SYasunori Goto 7700c0e6195SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 7710c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 7727b78d335SYasunori Goto 7737b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 7740c0e6195SKAMEZAWA Hiroyuki return 0; 7750c0e6195SKAMEZAWA Hiroyuki 7760c0e6195SKAMEZAWA Hiroyuki failed_removal: 7770c0e6195SKAMEZAWA Hiroyuki printk(KERN_INFO "memory offlining %lx to %lx failed\n", 7780c0e6195SKAMEZAWA Hiroyuki start_pfn, end_pfn); 7797b78d335SYasunori Goto memory_notify(MEM_CANCEL_OFFLINE, &arg); 7800c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 7810c0e6195SKAMEZAWA Hiroyuki undo_isolate_page_range(start_pfn, end_pfn); 7827b78d335SYasunori Goto 7830c0e6195SKAMEZAWA Hiroyuki return ret; 7840c0e6195SKAMEZAWA Hiroyuki } 78548e94196SKAMEZAWA Hiroyuki #else 78648e94196SKAMEZAWA Hiroyuki int remove_memory(u64 start, u64 size) 78748e94196SKAMEZAWA Hiroyuki { 78848e94196SKAMEZAWA Hiroyuki return -EINVAL; 78948e94196SKAMEZAWA Hiroyuki } 79048e94196SKAMEZAWA Hiroyuki EXPORT_SYMBOL_GPL(remove_memory); 7910c0e6195SKAMEZAWA Hiroyuki #endif /* CONFIG_MEMORY_HOTREMOVE */ 792