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/compiler.h> 13b95f1b31SPaul Gortmaker #include <linux/export.h> 143947be19SDave Hansen #include <linux/pagevec.h> 152d1d43f6SChandra Seetharaman #include <linux/writeback.h> 163947be19SDave Hansen #include <linux/slab.h> 173947be19SDave Hansen #include <linux/sysctl.h> 183947be19SDave Hansen #include <linux/cpu.h> 193947be19SDave Hansen #include <linux/memory.h> 203947be19SDave Hansen #include <linux/memory_hotplug.h> 213947be19SDave Hansen #include <linux/highmem.h> 223947be19SDave Hansen #include <linux/vmalloc.h> 230a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 240c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 250c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 260c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 2771088785SBadari Pulavarty #include <linux/pfn.h> 286ad696d2SAndi Kleen #include <linux/suspend.h> 296d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 30d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h> 3160a5a19eSTang Chen #include <linux/stop_machine.h> 32c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h> 33c5320926STang Chen #include <linux/memblock.h> 34f784a3f1STang Chen #include <linux/bootmem.h> 353947be19SDave Hansen 363947be19SDave Hansen #include <asm/tlbflush.h> 373947be19SDave Hansen 381e5ad9a3SAdrian Bunk #include "internal.h" 391e5ad9a3SAdrian Bunk 409d0ad8caSDaniel Kiper /* 419d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 429d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 439d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 449d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 459d0ad8caSDaniel Kiper */ 469d0ad8caSDaniel Kiper 479d0ad8caSDaniel Kiper static void generic_online_page(struct page *page); 489d0ad8caSDaniel Kiper 499d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 50bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 519d0ad8caSDaniel Kiper 52bfc8c901SVladimir Davydov /* The same as the cpu_hotplug lock, but for memory hotplug. */ 53bfc8c901SVladimir Davydov static struct { 54bfc8c901SVladimir Davydov struct task_struct *active_writer; 55bfc8c901SVladimir Davydov struct mutex lock; /* Synchronizes accesses to refcount, */ 56bfc8c901SVladimir Davydov /* 57bfc8c901SVladimir Davydov * Also blocks the new readers during 58bfc8c901SVladimir Davydov * an ongoing mem hotplug operation. 59bfc8c901SVladimir Davydov */ 60bfc8c901SVladimir Davydov int refcount; 6120d6c96bSKOSAKI Motohiro 62bfc8c901SVladimir Davydov #ifdef CONFIG_DEBUG_LOCK_ALLOC 63bfc8c901SVladimir Davydov struct lockdep_map dep_map; 64bfc8c901SVladimir Davydov #endif 65bfc8c901SVladimir Davydov } mem_hotplug = { 66bfc8c901SVladimir Davydov .active_writer = NULL, 67bfc8c901SVladimir Davydov .lock = __MUTEX_INITIALIZER(mem_hotplug.lock), 68bfc8c901SVladimir Davydov .refcount = 0, 69bfc8c901SVladimir Davydov #ifdef CONFIG_DEBUG_LOCK_ALLOC 70bfc8c901SVladimir Davydov .dep_map = {.name = "mem_hotplug.lock" }, 71bfc8c901SVladimir Davydov #endif 72bfc8c901SVladimir Davydov }; 73bfc8c901SVladimir Davydov 74bfc8c901SVladimir Davydov /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */ 75bfc8c901SVladimir Davydov #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map) 76bfc8c901SVladimir Davydov #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map) 77bfc8c901SVladimir Davydov #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map) 78bfc8c901SVladimir Davydov 79bfc8c901SVladimir Davydov void get_online_mems(void) 8020d6c96bSKOSAKI Motohiro { 81bfc8c901SVladimir Davydov might_sleep(); 82bfc8c901SVladimir Davydov if (mem_hotplug.active_writer == current) 83bfc8c901SVladimir Davydov return; 84bfc8c901SVladimir Davydov memhp_lock_acquire_read(); 85bfc8c901SVladimir Davydov mutex_lock(&mem_hotplug.lock); 86bfc8c901SVladimir Davydov mem_hotplug.refcount++; 87bfc8c901SVladimir Davydov mutex_unlock(&mem_hotplug.lock); 88bfc8c901SVladimir Davydov 8920d6c96bSKOSAKI Motohiro } 9020d6c96bSKOSAKI Motohiro 91bfc8c901SVladimir Davydov void put_online_mems(void) 9220d6c96bSKOSAKI Motohiro { 93bfc8c901SVladimir Davydov if (mem_hotplug.active_writer == current) 94bfc8c901SVladimir Davydov return; 95bfc8c901SVladimir Davydov mutex_lock(&mem_hotplug.lock); 96bfc8c901SVladimir Davydov 97bfc8c901SVladimir Davydov if (WARN_ON(!mem_hotplug.refcount)) 98bfc8c901SVladimir Davydov mem_hotplug.refcount++; /* try to fix things up */ 99bfc8c901SVladimir Davydov 100bfc8c901SVladimir Davydov if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer)) 101bfc8c901SVladimir Davydov wake_up_process(mem_hotplug.active_writer); 102bfc8c901SVladimir Davydov mutex_unlock(&mem_hotplug.lock); 103bfc8c901SVladimir Davydov memhp_lock_release(); 104bfc8c901SVladimir Davydov 10520d6c96bSKOSAKI Motohiro } 10620d6c96bSKOSAKI Motohiro 10730467e0bSDavid Rientjes void mem_hotplug_begin(void) 108bfc8c901SVladimir Davydov { 109bfc8c901SVladimir Davydov mem_hotplug.active_writer = current; 110bfc8c901SVladimir Davydov 111bfc8c901SVladimir Davydov memhp_lock_acquire(); 112bfc8c901SVladimir Davydov for (;;) { 113bfc8c901SVladimir Davydov mutex_lock(&mem_hotplug.lock); 114bfc8c901SVladimir Davydov if (likely(!mem_hotplug.refcount)) 115bfc8c901SVladimir Davydov break; 116bfc8c901SVladimir Davydov __set_current_state(TASK_UNINTERRUPTIBLE); 117bfc8c901SVladimir Davydov mutex_unlock(&mem_hotplug.lock); 118bfc8c901SVladimir Davydov schedule(); 119bfc8c901SVladimir Davydov } 120bfc8c901SVladimir Davydov } 121bfc8c901SVladimir Davydov 12230467e0bSDavid Rientjes void mem_hotplug_done(void) 123bfc8c901SVladimir Davydov { 124bfc8c901SVladimir Davydov mem_hotplug.active_writer = NULL; 125bfc8c901SVladimir Davydov mutex_unlock(&mem_hotplug.lock); 126bfc8c901SVladimir Davydov memhp_lock_release(); 127bfc8c901SVladimir Davydov } 12820d6c96bSKOSAKI Motohiro 12945e0b78bSKeith Mannthey /* add this memory to iomem resource */ 13045e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size) 13145e0b78bSKeith Mannthey { 13245e0b78bSKeith Mannthey struct resource *res; 13345e0b78bSKeith Mannthey res = kzalloc(sizeof(struct resource), GFP_KERNEL); 13445e0b78bSKeith Mannthey BUG_ON(!res); 13545e0b78bSKeith Mannthey 13645e0b78bSKeith Mannthey res->name = "System RAM"; 13745e0b78bSKeith Mannthey res->start = start; 13845e0b78bSKeith Mannthey res->end = start + size - 1; 139887c3cb1SYasunori Goto res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; 14045e0b78bSKeith Mannthey if (request_resource(&iomem_resource, res) < 0) { 1414996eed8SToshi Kani pr_debug("System RAM resource %pR cannot be added\n", res); 14245e0b78bSKeith Mannthey kfree(res); 14345e0b78bSKeith Mannthey res = NULL; 14445e0b78bSKeith Mannthey } 14545e0b78bSKeith Mannthey return res; 14645e0b78bSKeith Mannthey } 14745e0b78bSKeith Mannthey 14845e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 14945e0b78bSKeith Mannthey { 15045e0b78bSKeith Mannthey if (!res) 15145e0b78bSKeith Mannthey return; 15245e0b78bSKeith Mannthey release_resource(res); 15345e0b78bSKeith Mannthey kfree(res); 15445e0b78bSKeith Mannthey return; 15545e0b78bSKeith Mannthey } 15645e0b78bSKeith Mannthey 15753947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 15846723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info, struct page *page, 1595f24ce5fSAndrea Arcangeli unsigned long type) 16004753278SYasunori Goto { 1615f24ce5fSAndrea Arcangeli page->lru.next = (struct list_head *) type; 16204753278SYasunori Goto SetPagePrivate(page); 16304753278SYasunori Goto set_page_private(page, info); 16404753278SYasunori Goto atomic_inc(&page->_count); 16504753278SYasunori Goto } 16604753278SYasunori Goto 167170a5a7eSJiang Liu void put_page_bootmem(struct page *page) 16804753278SYasunori Goto { 1695f24ce5fSAndrea Arcangeli unsigned long type; 17004753278SYasunori Goto 1715f24ce5fSAndrea Arcangeli type = (unsigned long) page->lru.next; 1725f24ce5fSAndrea Arcangeli BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 1735f24ce5fSAndrea Arcangeli type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 17404753278SYasunori Goto 17504753278SYasunori Goto if (atomic_dec_return(&page->_count) == 1) { 17604753278SYasunori Goto ClearPagePrivate(page); 17704753278SYasunori Goto set_page_private(page, 0); 1785f24ce5fSAndrea Arcangeli INIT_LIST_HEAD(&page->lru); 179170a5a7eSJiang Liu free_reserved_page(page); 18004753278SYasunori Goto } 18104753278SYasunori Goto } 18204753278SYasunori Goto 18346723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 18446723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP 185d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn) 18604753278SYasunori Goto { 18704753278SYasunori Goto unsigned long *usemap, mapsize, section_nr, i; 18804753278SYasunori Goto struct mem_section *ms; 18904753278SYasunori Goto struct page *page, *memmap; 19004753278SYasunori Goto 19104753278SYasunori Goto section_nr = pfn_to_section_nr(start_pfn); 19204753278SYasunori Goto ms = __nr_to_section(section_nr); 19304753278SYasunori Goto 19404753278SYasunori Goto /* Get section's memmap address */ 19504753278SYasunori Goto memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 19604753278SYasunori Goto 19704753278SYasunori Goto /* 19804753278SYasunori Goto * Get page for the memmap's phys address 19904753278SYasunori Goto * XXX: need more consideration for sparse_vmemmap... 20004753278SYasunori Goto */ 20104753278SYasunori Goto page = virt_to_page(memmap); 20204753278SYasunori Goto mapsize = sizeof(struct page) * PAGES_PER_SECTION; 20304753278SYasunori Goto mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 20404753278SYasunori Goto 20504753278SYasunori Goto /* remember memmap's page */ 20604753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 20704753278SYasunori Goto get_page_bootmem(section_nr, page, SECTION_INFO); 20804753278SYasunori Goto 20904753278SYasunori Goto usemap = __nr_to_section(section_nr)->pageblock_flags; 21004753278SYasunori Goto page = virt_to_page(usemap); 21104753278SYasunori Goto 21204753278SYasunori Goto mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 21304753278SYasunori Goto 21404753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 215af370fb8SYasunori Goto get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 21604753278SYasunori Goto 21704753278SYasunori Goto } 21846723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */ 21946723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn) 22046723bfaSYasuaki Ishimatsu { 22146723bfaSYasuaki Ishimatsu unsigned long *usemap, mapsize, section_nr, i; 22246723bfaSYasuaki Ishimatsu struct mem_section *ms; 22346723bfaSYasuaki Ishimatsu struct page *page, *memmap; 22446723bfaSYasuaki Ishimatsu 22546723bfaSYasuaki Ishimatsu if (!pfn_valid(start_pfn)) 22646723bfaSYasuaki Ishimatsu return; 22746723bfaSYasuaki Ishimatsu 22846723bfaSYasuaki Ishimatsu section_nr = pfn_to_section_nr(start_pfn); 22946723bfaSYasuaki Ishimatsu ms = __nr_to_section(section_nr); 23046723bfaSYasuaki Ishimatsu 23146723bfaSYasuaki Ishimatsu memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 23246723bfaSYasuaki Ishimatsu 23346723bfaSYasuaki Ishimatsu register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); 23446723bfaSYasuaki Ishimatsu 23546723bfaSYasuaki Ishimatsu usemap = __nr_to_section(section_nr)->pageblock_flags; 23646723bfaSYasuaki Ishimatsu page = virt_to_page(usemap); 23746723bfaSYasuaki Ishimatsu 23846723bfaSYasuaki Ishimatsu mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 23946723bfaSYasuaki Ishimatsu 24046723bfaSYasuaki Ishimatsu for (i = 0; i < mapsize; i++, page++) 24146723bfaSYasuaki Ishimatsu get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 24246723bfaSYasuaki Ishimatsu } 24346723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 24404753278SYasunori Goto 24504753278SYasunori Goto void register_page_bootmem_info_node(struct pglist_data *pgdat) 24604753278SYasunori Goto { 24704753278SYasunori Goto unsigned long i, pfn, end_pfn, nr_pages; 24804753278SYasunori Goto int node = pgdat->node_id; 24904753278SYasunori Goto struct page *page; 25004753278SYasunori Goto struct zone *zone; 25104753278SYasunori Goto 25204753278SYasunori Goto nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 25304753278SYasunori Goto page = virt_to_page(pgdat); 25404753278SYasunori Goto 25504753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 25604753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 25704753278SYasunori Goto 25804753278SYasunori Goto zone = &pgdat->node_zones[0]; 25904753278SYasunori Goto for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) { 260139c2d75SXishi Qiu if (zone_is_initialized(zone)) { 26104753278SYasunori Goto nr_pages = zone->wait_table_hash_nr_entries 26204753278SYasunori Goto * sizeof(wait_queue_head_t); 26304753278SYasunori Goto nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT; 26404753278SYasunori Goto page = virt_to_page(zone->wait_table); 26504753278SYasunori Goto 26604753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 26704753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 26804753278SYasunori Goto } 26904753278SYasunori Goto } 27004753278SYasunori Goto 27104753278SYasunori Goto pfn = pgdat->node_start_pfn; 272c1f19495SCody P Schafer end_pfn = pgdat_end_pfn(pgdat); 27304753278SYasunori Goto 2747e9f5eb0STang Chen /* register section info */ 275f14851afSqiuxishi for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 276f14851afSqiuxishi /* 277f14851afSqiuxishi * Some platforms can assign the same pfn to multiple nodes - on 278f14851afSqiuxishi * node0 as well as nodeN. To avoid registering a pfn against 279f14851afSqiuxishi * multiple nodes we check that this pfn does not already 2807e9f5eb0STang Chen * reside in some other nodes. 281f14851afSqiuxishi */ 282f14851afSqiuxishi if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node)) 28304753278SYasunori Goto register_page_bootmem_info_section(pfn); 284f14851afSqiuxishi } 28504753278SYasunori Goto } 28646723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ 28704753278SYasunori Goto 288f2765404SFabian Frederick static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn, 28976cdd58eSHeiko Carstens unsigned long end_pfn) 29076cdd58eSHeiko Carstens { 29176cdd58eSHeiko Carstens unsigned long old_zone_end_pfn; 29276cdd58eSHeiko Carstens 29376cdd58eSHeiko Carstens zone_span_writelock(zone); 29476cdd58eSHeiko Carstens 295c33bc315SXishi Qiu old_zone_end_pfn = zone_end_pfn(zone); 2968080fc03SXishi Qiu if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 29776cdd58eSHeiko Carstens zone->zone_start_pfn = start_pfn; 29876cdd58eSHeiko Carstens 29976cdd58eSHeiko Carstens zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - 30076cdd58eSHeiko Carstens zone->zone_start_pfn; 30176cdd58eSHeiko Carstens 30276cdd58eSHeiko Carstens zone_span_writeunlock(zone); 30376cdd58eSHeiko Carstens } 30476cdd58eSHeiko Carstens 305511c2abaSLai Jiangshan static void resize_zone(struct zone *zone, unsigned long start_pfn, 306511c2abaSLai Jiangshan unsigned long end_pfn) 307511c2abaSLai Jiangshan { 308511c2abaSLai Jiangshan zone_span_writelock(zone); 309511c2abaSLai Jiangshan 310e455a9b9SLai Jiangshan if (end_pfn - start_pfn) { 311511c2abaSLai Jiangshan zone->zone_start_pfn = start_pfn; 312511c2abaSLai Jiangshan zone->spanned_pages = end_pfn - start_pfn; 313e455a9b9SLai Jiangshan } else { 314e455a9b9SLai Jiangshan /* 315e455a9b9SLai Jiangshan * make it consist as free_area_init_core(), 316e455a9b9SLai Jiangshan * if spanned_pages = 0, then keep start_pfn = 0 317e455a9b9SLai Jiangshan */ 318e455a9b9SLai Jiangshan zone->zone_start_pfn = 0; 319e455a9b9SLai Jiangshan zone->spanned_pages = 0; 320e455a9b9SLai Jiangshan } 321511c2abaSLai Jiangshan 322511c2abaSLai Jiangshan zone_span_writeunlock(zone); 323511c2abaSLai Jiangshan } 324511c2abaSLai Jiangshan 325511c2abaSLai Jiangshan static void fix_zone_id(struct zone *zone, unsigned long start_pfn, 326511c2abaSLai Jiangshan unsigned long end_pfn) 327511c2abaSLai Jiangshan { 328511c2abaSLai Jiangshan enum zone_type zid = zone_idx(zone); 329511c2abaSLai Jiangshan int nid = zone->zone_pgdat->node_id; 330511c2abaSLai Jiangshan unsigned long pfn; 331511c2abaSLai Jiangshan 332511c2abaSLai Jiangshan for (pfn = start_pfn; pfn < end_pfn; pfn++) 333511c2abaSLai Jiangshan set_page_links(pfn_to_page(pfn), zid, nid, pfn); 334511c2abaSLai Jiangshan } 335511c2abaSLai Jiangshan 336f6bbb78eSCody P Schafer /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or 3379e43aa2bSSantosh Shilimkar * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */ 338f6bbb78eSCody P Schafer static int __ref ensure_zone_is_initialized(struct zone *zone, 339f6bbb78eSCody P Schafer unsigned long start_pfn, unsigned long num_pages) 340f6bbb78eSCody P Schafer { 341f6bbb78eSCody P Schafer if (!zone_is_initialized(zone)) 342b171e409SYaowei Bai return init_currently_empty_zone(zone, start_pfn, num_pages); 343b171e409SYaowei Bai 344f6bbb78eSCody P Schafer return 0; 345f6bbb78eSCody P Schafer } 346f6bbb78eSCody P Schafer 347e455a9b9SLai Jiangshan static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2, 348511c2abaSLai Jiangshan unsigned long start_pfn, unsigned long end_pfn) 349511c2abaSLai Jiangshan { 350e455a9b9SLai Jiangshan int ret; 351511c2abaSLai Jiangshan unsigned long flags; 352e455a9b9SLai Jiangshan unsigned long z1_start_pfn; 353e455a9b9SLai Jiangshan 35464dd1b29SCody P Schafer ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn); 355e455a9b9SLai Jiangshan if (ret) 356e455a9b9SLai Jiangshan return ret; 357511c2abaSLai Jiangshan 358511c2abaSLai Jiangshan pgdat_resize_lock(z1->zone_pgdat, &flags); 359511c2abaSLai Jiangshan 360511c2abaSLai Jiangshan /* can't move pfns which are higher than @z2 */ 361108bcc96SCody P Schafer if (end_pfn > zone_end_pfn(z2)) 362511c2abaSLai Jiangshan goto out_fail; 363834405c3SJiang Liu /* the move out part must be at the left most of @z2 */ 364511c2abaSLai Jiangshan if (start_pfn > z2->zone_start_pfn) 365511c2abaSLai Jiangshan goto out_fail; 366511c2abaSLai Jiangshan /* must included/overlap */ 367511c2abaSLai Jiangshan if (end_pfn <= z2->zone_start_pfn) 368511c2abaSLai Jiangshan goto out_fail; 369511c2abaSLai Jiangshan 370e455a9b9SLai Jiangshan /* use start_pfn for z1's start_pfn if z1 is empty */ 3718080fc03SXishi Qiu if (!zone_is_empty(z1)) 372e455a9b9SLai Jiangshan z1_start_pfn = z1->zone_start_pfn; 373e455a9b9SLai Jiangshan else 374e455a9b9SLai Jiangshan z1_start_pfn = start_pfn; 375e455a9b9SLai Jiangshan 376e455a9b9SLai Jiangshan resize_zone(z1, z1_start_pfn, end_pfn); 377108bcc96SCody P Schafer resize_zone(z2, end_pfn, zone_end_pfn(z2)); 378511c2abaSLai Jiangshan 379511c2abaSLai Jiangshan pgdat_resize_unlock(z1->zone_pgdat, &flags); 380511c2abaSLai Jiangshan 381511c2abaSLai Jiangshan fix_zone_id(z1, start_pfn, end_pfn); 382511c2abaSLai Jiangshan 383511c2abaSLai Jiangshan return 0; 384511c2abaSLai Jiangshan out_fail: 385511c2abaSLai Jiangshan pgdat_resize_unlock(z1->zone_pgdat, &flags); 386511c2abaSLai Jiangshan return -1; 387511c2abaSLai Jiangshan } 388511c2abaSLai Jiangshan 389e455a9b9SLai Jiangshan static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2, 390511c2abaSLai Jiangshan unsigned long start_pfn, unsigned long end_pfn) 391511c2abaSLai Jiangshan { 392e455a9b9SLai Jiangshan int ret; 393511c2abaSLai Jiangshan unsigned long flags; 394e455a9b9SLai Jiangshan unsigned long z2_end_pfn; 395e455a9b9SLai Jiangshan 39664dd1b29SCody P Schafer ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn); 397e455a9b9SLai Jiangshan if (ret) 398e455a9b9SLai Jiangshan return ret; 399511c2abaSLai Jiangshan 400511c2abaSLai Jiangshan pgdat_resize_lock(z1->zone_pgdat, &flags); 401511c2abaSLai Jiangshan 402511c2abaSLai Jiangshan /* can't move pfns which are lower than @z1 */ 403511c2abaSLai Jiangshan if (z1->zone_start_pfn > start_pfn) 404511c2abaSLai Jiangshan goto out_fail; 405511c2abaSLai Jiangshan /* the move out part mast at the right most of @z1 */ 406108bcc96SCody P Schafer if (zone_end_pfn(z1) > end_pfn) 407511c2abaSLai Jiangshan goto out_fail; 408511c2abaSLai Jiangshan /* must included/overlap */ 409108bcc96SCody P Schafer if (start_pfn >= zone_end_pfn(z1)) 410511c2abaSLai Jiangshan goto out_fail; 411511c2abaSLai Jiangshan 412e455a9b9SLai Jiangshan /* use end_pfn for z2's end_pfn if z2 is empty */ 4138080fc03SXishi Qiu if (!zone_is_empty(z2)) 414108bcc96SCody P Schafer z2_end_pfn = zone_end_pfn(z2); 415e455a9b9SLai Jiangshan else 416e455a9b9SLai Jiangshan z2_end_pfn = end_pfn; 417e455a9b9SLai Jiangshan 418511c2abaSLai Jiangshan resize_zone(z1, z1->zone_start_pfn, start_pfn); 419e455a9b9SLai Jiangshan resize_zone(z2, start_pfn, z2_end_pfn); 420511c2abaSLai Jiangshan 421511c2abaSLai Jiangshan pgdat_resize_unlock(z1->zone_pgdat, &flags); 422511c2abaSLai Jiangshan 423511c2abaSLai Jiangshan fix_zone_id(z2, start_pfn, end_pfn); 424511c2abaSLai Jiangshan 425511c2abaSLai Jiangshan return 0; 426511c2abaSLai Jiangshan out_fail: 427511c2abaSLai Jiangshan pgdat_resize_unlock(z1->zone_pgdat, &flags); 428511c2abaSLai Jiangshan return -1; 429511c2abaSLai Jiangshan } 430511c2abaSLai Jiangshan 431f2765404SFabian Frederick static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn, 43276cdd58eSHeiko Carstens unsigned long end_pfn) 43376cdd58eSHeiko Carstens { 43483285c72SXishi Qiu unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat); 43576cdd58eSHeiko Carstens 436712cd386STang Chen if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 43776cdd58eSHeiko Carstens pgdat->node_start_pfn = start_pfn; 43876cdd58eSHeiko Carstens 43976cdd58eSHeiko Carstens pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - 44076cdd58eSHeiko Carstens pgdat->node_start_pfn; 44176cdd58eSHeiko Carstens } 44276cdd58eSHeiko Carstens 44331168481SAl Viro static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn) 4443947be19SDave Hansen { 4453947be19SDave Hansen struct pglist_data *pgdat = zone->zone_pgdat; 4463947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 4473947be19SDave Hansen int nid = pgdat->node_id; 4483947be19SDave Hansen int zone_type; 449e298ff75SMel Gorman unsigned long flags, pfn; 45076cdd58eSHeiko Carstens int ret; 45176cdd58eSHeiko Carstens 45264dd1b29SCody P Schafer zone_type = zone - pgdat->node_zones; 45364dd1b29SCody P Schafer ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages); 45476cdd58eSHeiko Carstens if (ret) 45576cdd58eSHeiko Carstens return ret; 45664dd1b29SCody P Schafer 45776cdd58eSHeiko Carstens pgdat_resize_lock(zone->zone_pgdat, &flags); 45876cdd58eSHeiko Carstens grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages); 45976cdd58eSHeiko Carstens grow_pgdat_span(zone->zone_pgdat, phys_start_pfn, 46076cdd58eSHeiko Carstens phys_start_pfn + nr_pages); 46176cdd58eSHeiko Carstens pgdat_resize_unlock(zone->zone_pgdat, &flags); 462a2f3aa02SDave Hansen memmap_init_zone(nr_pages, nid, zone_type, 463a2f3aa02SDave Hansen phys_start_pfn, MEMMAP_HOTPLUG); 464e298ff75SMel Gorman 465e298ff75SMel Gorman /* online_page_range is called later and expects pages reserved */ 466e298ff75SMel Gorman for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) { 467e298ff75SMel Gorman if (!pfn_valid(pfn)) 468e298ff75SMel Gorman continue; 469e298ff75SMel Gorman 470e298ff75SMel Gorman SetPageReserved(pfn_to_page(pfn)); 471e298ff75SMel Gorman } 472718127ccSYasunori Goto return 0; 4733947be19SDave Hansen } 4743947be19SDave Hansen 475c04fc586SGary Hade static int __meminit __add_section(int nid, struct zone *zone, 476c04fc586SGary Hade unsigned long phys_start_pfn) 4773947be19SDave Hansen { 4783947be19SDave Hansen int ret; 4793947be19SDave Hansen 480ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 481ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 482ebd15302SKAMEZAWA Hiroyuki 48385b35feaSZhang Yanfei ret = sparse_add_one_section(zone, phys_start_pfn); 4843947be19SDave Hansen 4853947be19SDave Hansen if (ret < 0) 4863947be19SDave Hansen return ret; 4873947be19SDave Hansen 488718127ccSYasunori Goto ret = __add_zone(zone, phys_start_pfn); 489718127ccSYasunori Goto 490718127ccSYasunori Goto if (ret < 0) 491718127ccSYasunori Goto return ret; 492718127ccSYasunori Goto 493c04fc586SGary Hade return register_new_memory(nid, __pfn_to_section(phys_start_pfn)); 4943947be19SDave Hansen } 4953947be19SDave Hansen 4964edd7cefSDavid Rientjes /* 4974edd7cefSDavid Rientjes * Reasonably generic function for adding memory. It is 4984edd7cefSDavid Rientjes * expected that archs that support memory hotplug will 4994edd7cefSDavid Rientjes * call this function after deciding the zone to which to 5004edd7cefSDavid Rientjes * add the new pages. 5014edd7cefSDavid Rientjes */ 5024edd7cefSDavid Rientjes int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn, 5034edd7cefSDavid Rientjes unsigned long nr_pages) 5044edd7cefSDavid Rientjes { 5054edd7cefSDavid Rientjes unsigned long i; 5064edd7cefSDavid Rientjes int err = 0; 5074edd7cefSDavid Rientjes int start_sec, end_sec; 5084edd7cefSDavid Rientjes /* during initialize mem_map, align hot-added range to section */ 5094edd7cefSDavid Rientjes start_sec = pfn_to_section_nr(phys_start_pfn); 5104edd7cefSDavid Rientjes end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 5114edd7cefSDavid Rientjes 5124edd7cefSDavid Rientjes for (i = start_sec; i <= end_sec; i++) { 51319c07d5eSSheng Yong err = __add_section(nid, zone, section_nr_to_pfn(i)); 5144edd7cefSDavid Rientjes 5154edd7cefSDavid Rientjes /* 5164edd7cefSDavid Rientjes * EEXIST is finally dealt with by ioresource collision 5174edd7cefSDavid Rientjes * check. see add_memory() => register_memory_resource() 5184edd7cefSDavid Rientjes * Warning will be printed if there is collision. 5194edd7cefSDavid Rientjes */ 5204edd7cefSDavid Rientjes if (err && (err != -EEXIST)) 5214edd7cefSDavid Rientjes break; 5224edd7cefSDavid Rientjes err = 0; 5234edd7cefSDavid Rientjes } 524c435a390SZhu Guihua vmemmap_populate_print_last(); 5254edd7cefSDavid Rientjes 5264edd7cefSDavid Rientjes return err; 5274edd7cefSDavid Rientjes } 5284edd7cefSDavid Rientjes EXPORT_SYMBOL_GPL(__add_pages); 5294edd7cefSDavid Rientjes 5304edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE 531815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 532815121d2SYasuaki Ishimatsu static int find_smallest_section_pfn(int nid, struct zone *zone, 533815121d2SYasuaki Ishimatsu unsigned long start_pfn, 534815121d2SYasuaki Ishimatsu unsigned long end_pfn) 535815121d2SYasuaki Ishimatsu { 536815121d2SYasuaki Ishimatsu struct mem_section *ms; 537815121d2SYasuaki Ishimatsu 538815121d2SYasuaki Ishimatsu for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) { 539815121d2SYasuaki Ishimatsu ms = __pfn_to_section(start_pfn); 540815121d2SYasuaki Ishimatsu 541815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 542815121d2SYasuaki Ishimatsu continue; 543815121d2SYasuaki Ishimatsu 544815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(start_pfn) != nid)) 545815121d2SYasuaki Ishimatsu continue; 546815121d2SYasuaki Ishimatsu 547815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(start_pfn))) 548815121d2SYasuaki Ishimatsu continue; 549815121d2SYasuaki Ishimatsu 550815121d2SYasuaki Ishimatsu return start_pfn; 551815121d2SYasuaki Ishimatsu } 552815121d2SYasuaki Ishimatsu 553815121d2SYasuaki Ishimatsu return 0; 554815121d2SYasuaki Ishimatsu } 555815121d2SYasuaki Ishimatsu 556815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 557815121d2SYasuaki Ishimatsu static int find_biggest_section_pfn(int nid, struct zone *zone, 558815121d2SYasuaki Ishimatsu unsigned long start_pfn, 559815121d2SYasuaki Ishimatsu unsigned long end_pfn) 560815121d2SYasuaki Ishimatsu { 561815121d2SYasuaki Ishimatsu struct mem_section *ms; 562815121d2SYasuaki Ishimatsu unsigned long pfn; 563815121d2SYasuaki Ishimatsu 564815121d2SYasuaki Ishimatsu /* pfn is the end pfn of a memory section. */ 565815121d2SYasuaki Ishimatsu pfn = end_pfn - 1; 566815121d2SYasuaki Ishimatsu for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) { 567815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 568815121d2SYasuaki Ishimatsu 569815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 570815121d2SYasuaki Ishimatsu continue; 571815121d2SYasuaki Ishimatsu 572815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(pfn) != nid)) 573815121d2SYasuaki Ishimatsu continue; 574815121d2SYasuaki Ishimatsu 575815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(pfn))) 576815121d2SYasuaki Ishimatsu continue; 577815121d2SYasuaki Ishimatsu 578815121d2SYasuaki Ishimatsu return pfn; 579815121d2SYasuaki Ishimatsu } 580815121d2SYasuaki Ishimatsu 581815121d2SYasuaki Ishimatsu return 0; 582815121d2SYasuaki Ishimatsu } 583815121d2SYasuaki Ishimatsu 584815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 585815121d2SYasuaki Ishimatsu unsigned long end_pfn) 586815121d2SYasuaki Ishimatsu { 587815121d2SYasuaki Ishimatsu unsigned long zone_start_pfn = zone->zone_start_pfn; 588c33bc315SXishi Qiu unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ 589c33bc315SXishi Qiu unsigned long zone_end_pfn = z; 590815121d2SYasuaki Ishimatsu unsigned long pfn; 591815121d2SYasuaki Ishimatsu struct mem_section *ms; 592815121d2SYasuaki Ishimatsu int nid = zone_to_nid(zone); 593815121d2SYasuaki Ishimatsu 594815121d2SYasuaki Ishimatsu zone_span_writelock(zone); 595815121d2SYasuaki Ishimatsu if (zone_start_pfn == start_pfn) { 596815121d2SYasuaki Ishimatsu /* 597815121d2SYasuaki Ishimatsu * If the section is smallest section in the zone, it need 598815121d2SYasuaki Ishimatsu * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 599815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 600815121d2SYasuaki Ishimatsu * for shrinking zone. 601815121d2SYasuaki Ishimatsu */ 602815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, zone, end_pfn, 603815121d2SYasuaki Ishimatsu zone_end_pfn); 604815121d2SYasuaki Ishimatsu if (pfn) { 605815121d2SYasuaki Ishimatsu zone->zone_start_pfn = pfn; 606815121d2SYasuaki Ishimatsu zone->spanned_pages = zone_end_pfn - pfn; 607815121d2SYasuaki Ishimatsu } 608815121d2SYasuaki Ishimatsu } else if (zone_end_pfn == end_pfn) { 609815121d2SYasuaki Ishimatsu /* 610815121d2SYasuaki Ishimatsu * If the section is biggest section in the zone, it need 611815121d2SYasuaki Ishimatsu * shrink zone->spanned_pages. 612815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 613815121d2SYasuaki Ishimatsu * shrinking zone. 614815121d2SYasuaki Ishimatsu */ 615815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, 616815121d2SYasuaki Ishimatsu start_pfn); 617815121d2SYasuaki Ishimatsu if (pfn) 618815121d2SYasuaki Ishimatsu zone->spanned_pages = pfn - zone_start_pfn + 1; 619815121d2SYasuaki Ishimatsu } 620815121d2SYasuaki Ishimatsu 621815121d2SYasuaki Ishimatsu /* 622815121d2SYasuaki Ishimatsu * The section is not biggest or smallest mem_section in the zone, it 623815121d2SYasuaki Ishimatsu * only creates a hole in the zone. So in this case, we need not 624815121d2SYasuaki Ishimatsu * change the zone. But perhaps, the zone has only hole data. Thus 625815121d2SYasuaki Ishimatsu * it check the zone has only hole or not. 626815121d2SYasuaki Ishimatsu */ 627815121d2SYasuaki Ishimatsu pfn = zone_start_pfn; 628815121d2SYasuaki Ishimatsu for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) { 629815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 630815121d2SYasuaki Ishimatsu 631815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 632815121d2SYasuaki Ishimatsu continue; 633815121d2SYasuaki Ishimatsu 634815121d2SYasuaki Ishimatsu if (page_zone(pfn_to_page(pfn)) != zone) 635815121d2SYasuaki Ishimatsu continue; 636815121d2SYasuaki Ishimatsu 637815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 638815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 639815121d2SYasuaki Ishimatsu continue; 640815121d2SYasuaki Ishimatsu 641815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 642815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 643815121d2SYasuaki Ishimatsu return; 644815121d2SYasuaki Ishimatsu } 645815121d2SYasuaki Ishimatsu 646815121d2SYasuaki Ishimatsu /* The zone has no valid section */ 647815121d2SYasuaki Ishimatsu zone->zone_start_pfn = 0; 648815121d2SYasuaki Ishimatsu zone->spanned_pages = 0; 649815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 650815121d2SYasuaki Ishimatsu } 651815121d2SYasuaki Ishimatsu 652815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat, 653815121d2SYasuaki Ishimatsu unsigned long start_pfn, unsigned long end_pfn) 654815121d2SYasuaki Ishimatsu { 655815121d2SYasuaki Ishimatsu unsigned long pgdat_start_pfn = pgdat->node_start_pfn; 65683285c72SXishi Qiu unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */ 65783285c72SXishi Qiu unsigned long pgdat_end_pfn = p; 658815121d2SYasuaki Ishimatsu unsigned long pfn; 659815121d2SYasuaki Ishimatsu struct mem_section *ms; 660815121d2SYasuaki Ishimatsu int nid = pgdat->node_id; 661815121d2SYasuaki Ishimatsu 662815121d2SYasuaki Ishimatsu if (pgdat_start_pfn == start_pfn) { 663815121d2SYasuaki Ishimatsu /* 664815121d2SYasuaki Ishimatsu * If the section is smallest section in the pgdat, it need 665815121d2SYasuaki Ishimatsu * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages. 666815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 667815121d2SYasuaki Ishimatsu * for shrinking zone. 668815121d2SYasuaki Ishimatsu */ 669815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, NULL, end_pfn, 670815121d2SYasuaki Ishimatsu pgdat_end_pfn); 671815121d2SYasuaki Ishimatsu if (pfn) { 672815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = pfn; 673815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pgdat_end_pfn - pfn; 674815121d2SYasuaki Ishimatsu } 675815121d2SYasuaki Ishimatsu } else if (pgdat_end_pfn == end_pfn) { 676815121d2SYasuaki Ishimatsu /* 677815121d2SYasuaki Ishimatsu * If the section is biggest section in the pgdat, it need 678815121d2SYasuaki Ishimatsu * shrink pgdat->node_spanned_pages. 679815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 680815121d2SYasuaki Ishimatsu * shrinking zone. 681815121d2SYasuaki Ishimatsu */ 682815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn, 683815121d2SYasuaki Ishimatsu start_pfn); 684815121d2SYasuaki Ishimatsu if (pfn) 685815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1; 686815121d2SYasuaki Ishimatsu } 687815121d2SYasuaki Ishimatsu 688815121d2SYasuaki Ishimatsu /* 689815121d2SYasuaki Ishimatsu * If the section is not biggest or smallest mem_section in the pgdat, 690815121d2SYasuaki Ishimatsu * it only creates a hole in the pgdat. So in this case, we need not 691815121d2SYasuaki Ishimatsu * change the pgdat. 692815121d2SYasuaki Ishimatsu * But perhaps, the pgdat has only hole data. Thus it check the pgdat 693815121d2SYasuaki Ishimatsu * has only hole or not. 694815121d2SYasuaki Ishimatsu */ 695815121d2SYasuaki Ishimatsu pfn = pgdat_start_pfn; 696815121d2SYasuaki Ishimatsu for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) { 697815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 698815121d2SYasuaki Ishimatsu 699815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 700815121d2SYasuaki Ishimatsu continue; 701815121d2SYasuaki Ishimatsu 702815121d2SYasuaki Ishimatsu if (pfn_to_nid(pfn) != nid) 703815121d2SYasuaki Ishimatsu continue; 704815121d2SYasuaki Ishimatsu 705815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 706815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 707815121d2SYasuaki Ishimatsu continue; 708815121d2SYasuaki Ishimatsu 709815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 710815121d2SYasuaki Ishimatsu return; 711815121d2SYasuaki Ishimatsu } 712815121d2SYasuaki Ishimatsu 713815121d2SYasuaki Ishimatsu /* The pgdat has no valid section */ 714815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = 0; 715815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = 0; 716815121d2SYasuaki Ishimatsu } 717815121d2SYasuaki Ishimatsu 718815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn) 719815121d2SYasuaki Ishimatsu { 720815121d2SYasuaki Ishimatsu struct pglist_data *pgdat = zone->zone_pgdat; 721815121d2SYasuaki Ishimatsu int nr_pages = PAGES_PER_SECTION; 722815121d2SYasuaki Ishimatsu int zone_type; 723815121d2SYasuaki Ishimatsu unsigned long flags; 724815121d2SYasuaki Ishimatsu 725815121d2SYasuaki Ishimatsu zone_type = zone - pgdat->node_zones; 726815121d2SYasuaki Ishimatsu 727815121d2SYasuaki Ishimatsu pgdat_resize_lock(zone->zone_pgdat, &flags); 728815121d2SYasuaki Ishimatsu shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 729815121d2SYasuaki Ishimatsu shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages); 730815121d2SYasuaki Ishimatsu pgdat_resize_unlock(zone->zone_pgdat, &flags); 731815121d2SYasuaki Ishimatsu } 732815121d2SYasuaki Ishimatsu 733ea01ea93SBadari Pulavarty static int __remove_section(struct zone *zone, struct mem_section *ms) 734ea01ea93SBadari Pulavarty { 735815121d2SYasuaki Ishimatsu unsigned long start_pfn; 736815121d2SYasuaki Ishimatsu int scn_nr; 737ea01ea93SBadari Pulavarty int ret = -EINVAL; 738ea01ea93SBadari Pulavarty 739ea01ea93SBadari Pulavarty if (!valid_section(ms)) 740ea01ea93SBadari Pulavarty return ret; 741ea01ea93SBadari Pulavarty 742ea01ea93SBadari Pulavarty ret = unregister_memory_section(ms); 743ea01ea93SBadari Pulavarty if (ret) 744ea01ea93SBadari Pulavarty return ret; 745ea01ea93SBadari Pulavarty 746815121d2SYasuaki Ishimatsu scn_nr = __section_nr(ms); 747815121d2SYasuaki Ishimatsu start_pfn = section_nr_to_pfn(scn_nr); 748815121d2SYasuaki Ishimatsu __remove_zone(zone, start_pfn); 749815121d2SYasuaki Ishimatsu 750ea01ea93SBadari Pulavarty sparse_remove_one_section(zone, ms); 751ea01ea93SBadari Pulavarty return 0; 752ea01ea93SBadari Pulavarty } 753ea01ea93SBadari Pulavarty 754ea01ea93SBadari Pulavarty /** 755ea01ea93SBadari Pulavarty * __remove_pages() - remove sections of pages from a zone 756ea01ea93SBadari Pulavarty * @zone: zone from which pages need to be removed 757ea01ea93SBadari Pulavarty * @phys_start_pfn: starting pageframe (must be aligned to start of a section) 758ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 759ea01ea93SBadari Pulavarty * 760ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 761ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 762ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 763ea01ea93SBadari Pulavarty * calling offline_pages(). 764ea01ea93SBadari Pulavarty */ 765ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, 766ea01ea93SBadari Pulavarty unsigned long nr_pages) 767ea01ea93SBadari Pulavarty { 768fe74ebb1SToshi Kani unsigned long i; 769ea01ea93SBadari Pulavarty int sections_to_remove; 770fe74ebb1SToshi Kani resource_size_t start, size; 771fe74ebb1SToshi Kani int ret = 0; 772ea01ea93SBadari Pulavarty 773ea01ea93SBadari Pulavarty /* 774ea01ea93SBadari Pulavarty * We can only remove entire sections 775ea01ea93SBadari Pulavarty */ 776ea01ea93SBadari Pulavarty BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); 777ea01ea93SBadari Pulavarty BUG_ON(nr_pages % PAGES_PER_SECTION); 778ea01ea93SBadari Pulavarty 779fe74ebb1SToshi Kani start = phys_start_pfn << PAGE_SHIFT; 780fe74ebb1SToshi Kani size = nr_pages * PAGE_SIZE; 781033fbae9SDan Williams 782033fbae9SDan Williams /* in the ZONE_DEVICE case device driver owns the memory region */ 783033fbae9SDan Williams if (!is_dev_zone(zone)) 784fe74ebb1SToshi Kani ret = release_mem_region_adjustable(&iomem_resource, start, size); 785348f9f05SRandy Dunlap if (ret) { 786348f9f05SRandy Dunlap resource_size_t endres = start + size - 1; 787348f9f05SRandy Dunlap 788348f9f05SRandy Dunlap pr_warn("Unable to release resource <%pa-%pa> (%d)\n", 789348f9f05SRandy Dunlap &start, &endres, ret); 790348f9f05SRandy Dunlap } 791d760afd4SYasuaki Ishimatsu 792ea01ea93SBadari Pulavarty sections_to_remove = nr_pages / PAGES_PER_SECTION; 793ea01ea93SBadari Pulavarty for (i = 0; i < sections_to_remove; i++) { 794ea01ea93SBadari Pulavarty unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; 795ea01ea93SBadari Pulavarty ret = __remove_section(zone, __pfn_to_section(pfn)); 796ea01ea93SBadari Pulavarty if (ret) 797ea01ea93SBadari Pulavarty break; 798ea01ea93SBadari Pulavarty } 799ea01ea93SBadari Pulavarty return ret; 800ea01ea93SBadari Pulavarty } 801ea01ea93SBadari Pulavarty EXPORT_SYMBOL_GPL(__remove_pages); 8024edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */ 803ea01ea93SBadari Pulavarty 8049d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 8059d0ad8caSDaniel Kiper { 8069d0ad8caSDaniel Kiper int rc = -EINVAL; 8079d0ad8caSDaniel Kiper 808bfc8c901SVladimir Davydov get_online_mems(); 809bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 8109d0ad8caSDaniel Kiper 8119d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 8129d0ad8caSDaniel Kiper online_page_callback = callback; 8139d0ad8caSDaniel Kiper rc = 0; 8149d0ad8caSDaniel Kiper } 8159d0ad8caSDaniel Kiper 816bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 817bfc8c901SVladimir Davydov put_online_mems(); 8189d0ad8caSDaniel Kiper 8199d0ad8caSDaniel Kiper return rc; 8209d0ad8caSDaniel Kiper } 8219d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 8229d0ad8caSDaniel Kiper 8239d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 8249d0ad8caSDaniel Kiper { 8259d0ad8caSDaniel Kiper int rc = -EINVAL; 8269d0ad8caSDaniel Kiper 827bfc8c901SVladimir Davydov get_online_mems(); 828bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 8299d0ad8caSDaniel Kiper 8309d0ad8caSDaniel Kiper if (online_page_callback == callback) { 8319d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 8329d0ad8caSDaniel Kiper rc = 0; 8339d0ad8caSDaniel Kiper } 8349d0ad8caSDaniel Kiper 835bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 836bfc8c901SVladimir Davydov put_online_mems(); 8379d0ad8caSDaniel Kiper 8389d0ad8caSDaniel Kiper return rc; 8399d0ad8caSDaniel Kiper } 8409d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 8419d0ad8caSDaniel Kiper 8429d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page) 843180c06efSJeremy Fitzhardinge { 8449d0ad8caSDaniel Kiper } 8459d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits); 8469d0ad8caSDaniel Kiper 8479d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page) 8489d0ad8caSDaniel Kiper { 8493dcc0571SJiang Liu adjust_managed_page_count(page, 1); 8509d0ad8caSDaniel Kiper } 8519d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters); 852180c06efSJeremy Fitzhardinge 8539d0ad8caSDaniel Kiper void __online_page_free(struct page *page) 8549d0ad8caSDaniel Kiper { 8553dcc0571SJiang Liu __free_reserved_page(page); 856180c06efSJeremy Fitzhardinge } 8579d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free); 8589d0ad8caSDaniel Kiper 8599d0ad8caSDaniel Kiper static void generic_online_page(struct page *page) 8609d0ad8caSDaniel Kiper { 8619d0ad8caSDaniel Kiper __online_page_set_limits(page); 8629d0ad8caSDaniel Kiper __online_page_increment_counters(page); 8639d0ad8caSDaniel Kiper __online_page_free(page); 8649d0ad8caSDaniel Kiper } 865180c06efSJeremy Fitzhardinge 86675884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, 86775884fb1SKAMEZAWA Hiroyuki void *arg) 8683947be19SDave Hansen { 8693947be19SDave Hansen unsigned long i; 87075884fb1SKAMEZAWA Hiroyuki unsigned long onlined_pages = *(unsigned long *)arg; 87175884fb1SKAMEZAWA Hiroyuki struct page *page; 87275884fb1SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) 87375884fb1SKAMEZAWA Hiroyuki for (i = 0; i < nr_pages; i++) { 87475884fb1SKAMEZAWA Hiroyuki page = pfn_to_page(start_pfn + i); 8759d0ad8caSDaniel Kiper (*online_page_callback)(page); 87675884fb1SKAMEZAWA Hiroyuki onlined_pages++; 87775884fb1SKAMEZAWA Hiroyuki } 87875884fb1SKAMEZAWA Hiroyuki *(unsigned long *)arg = onlined_pages; 87975884fb1SKAMEZAWA Hiroyuki return 0; 88075884fb1SKAMEZAWA Hiroyuki } 88175884fb1SKAMEZAWA Hiroyuki 88209285af7SLai Jiangshan #ifdef CONFIG_MOVABLE_NODE 88379a4dcefSTang Chen /* 88479a4dcefSTang Chen * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have 88579a4dcefSTang Chen * normal memory. 88679a4dcefSTang Chen */ 88709285af7SLai Jiangshan static bool can_online_high_movable(struct zone *zone) 88809285af7SLai Jiangshan { 88909285af7SLai Jiangshan return true; 89009285af7SLai Jiangshan } 89179a4dcefSTang Chen #else /* CONFIG_MOVABLE_NODE */ 89274d42d8fSLai Jiangshan /* ensure every online node has NORMAL memory */ 89374d42d8fSLai Jiangshan static bool can_online_high_movable(struct zone *zone) 89474d42d8fSLai Jiangshan { 89574d42d8fSLai Jiangshan return node_state(zone_to_nid(zone), N_NORMAL_MEMORY); 89674d42d8fSLai Jiangshan } 89779a4dcefSTang Chen #endif /* CONFIG_MOVABLE_NODE */ 89874d42d8fSLai Jiangshan 899d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 900d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 901d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 902d9713679SLai Jiangshan { 903d9713679SLai Jiangshan int nid = zone_to_nid(zone); 904d9713679SLai Jiangshan enum zone_type zone_last = ZONE_NORMAL; 905d9713679SLai Jiangshan 906d9713679SLai Jiangshan /* 9076715ddf9SLai Jiangshan * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] 9086715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_NORMAL, 9096715ddf9SLai Jiangshan * set zone_last to ZONE_NORMAL. 910d9713679SLai Jiangshan * 9116715ddf9SLai Jiangshan * If we don't have HIGHMEM nor movable node, 9126715ddf9SLai Jiangshan * node_states[N_NORMAL_MEMORY] contains nodes which have zones of 9136715ddf9SLai Jiangshan * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. 914d9713679SLai Jiangshan */ 9156715ddf9SLai Jiangshan if (N_MEMORY == N_NORMAL_MEMORY) 916d9713679SLai Jiangshan zone_last = ZONE_MOVABLE; 917d9713679SLai Jiangshan 918d9713679SLai Jiangshan /* 919d9713679SLai Jiangshan * if the memory to be online is in a zone of 0...zone_last, and 920d9713679SLai Jiangshan * the zones of 0...zone_last don't have memory before online, we will 921d9713679SLai Jiangshan * need to set the node to node_states[N_NORMAL_MEMORY] after 922d9713679SLai Jiangshan * the memory is online. 923d9713679SLai Jiangshan */ 924d9713679SLai Jiangshan if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY)) 925d9713679SLai Jiangshan arg->status_change_nid_normal = nid; 926d9713679SLai Jiangshan else 927d9713679SLai Jiangshan arg->status_change_nid_normal = -1; 928d9713679SLai Jiangshan 9296715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM 9306715ddf9SLai Jiangshan /* 9316715ddf9SLai Jiangshan * If we have movable node, node_states[N_HIGH_MEMORY] 9326715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_HIGHMEM, 9336715ddf9SLai Jiangshan * set zone_last to ZONE_HIGHMEM. 9346715ddf9SLai Jiangshan * 9356715ddf9SLai Jiangshan * If we don't have movable node, node_states[N_NORMAL_MEMORY] 9366715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_MOVABLE, 9376715ddf9SLai Jiangshan * set zone_last to ZONE_MOVABLE. 9386715ddf9SLai Jiangshan */ 9396715ddf9SLai Jiangshan zone_last = ZONE_HIGHMEM; 9406715ddf9SLai Jiangshan if (N_MEMORY == N_HIGH_MEMORY) 9416715ddf9SLai Jiangshan zone_last = ZONE_MOVABLE; 9426715ddf9SLai Jiangshan 9436715ddf9SLai Jiangshan if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY)) 9446715ddf9SLai Jiangshan arg->status_change_nid_high = nid; 9456715ddf9SLai Jiangshan else 9466715ddf9SLai Jiangshan arg->status_change_nid_high = -1; 9476715ddf9SLai Jiangshan #else 9486715ddf9SLai Jiangshan arg->status_change_nid_high = arg->status_change_nid_normal; 9496715ddf9SLai Jiangshan #endif 9506715ddf9SLai Jiangshan 951d9713679SLai Jiangshan /* 952d9713679SLai Jiangshan * if the node don't have memory befor online, we will need to 9536715ddf9SLai Jiangshan * set the node to node_states[N_MEMORY] after the memory 954d9713679SLai Jiangshan * is online. 955d9713679SLai Jiangshan */ 9566715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 957d9713679SLai Jiangshan arg->status_change_nid = nid; 958d9713679SLai Jiangshan else 959d9713679SLai Jiangshan arg->status_change_nid = -1; 960d9713679SLai Jiangshan } 961d9713679SLai Jiangshan 962d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 963d9713679SLai Jiangshan { 964d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 965d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 966d9713679SLai Jiangshan 9676715ddf9SLai Jiangshan if (arg->status_change_nid_high >= 0) 968d9713679SLai Jiangshan node_set_state(node, N_HIGH_MEMORY); 9696715ddf9SLai Jiangshan 9706715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 971d9713679SLai Jiangshan } 972d9713679SLai Jiangshan 97375884fb1SKAMEZAWA Hiroyuki 97430467e0bSDavid Rientjes /* Must be protected by mem_hotplug_begin() */ 975511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) 97675884fb1SKAMEZAWA Hiroyuki { 977aa47228aSCody P Schafer unsigned long flags; 9783947be19SDave Hansen unsigned long onlined_pages = 0; 9793947be19SDave Hansen struct zone *zone; 9806811378eSYasunori Goto int need_zonelists_rebuild = 0; 9817b78d335SYasunori Goto int nid; 9827b78d335SYasunori Goto int ret; 9837b78d335SYasunori Goto struct memory_notify arg; 9843947be19SDave Hansen 985d9713679SLai Jiangshan /* 986d9713679SLai Jiangshan * This doesn't need a lock to do pfn_to_page(). 987d9713679SLai Jiangshan * The section can't be removed here because of the 988d9713679SLai Jiangshan * memory_block->state_mutex. 989d9713679SLai Jiangshan */ 990d9713679SLai Jiangshan zone = page_zone(pfn_to_page(pfn)); 991d9713679SLai Jiangshan 9924f7c6b49STang Chen if ((zone_idx(zone) > ZONE_NORMAL || 9934f7c6b49STang Chen online_type == MMOP_ONLINE_MOVABLE) && 994bfc8c901SVladimir Davydov !can_online_high_movable(zone)) 99530467e0bSDavid Rientjes return -EINVAL; 99674d42d8fSLai Jiangshan 9974f7c6b49STang Chen if (online_type == MMOP_ONLINE_KERNEL && 9984f7c6b49STang Chen zone_idx(zone) == ZONE_MOVABLE) { 999bfc8c901SVladimir Davydov if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) 100030467e0bSDavid Rientjes return -EINVAL; 1001511c2abaSLai Jiangshan } 10024f7c6b49STang Chen if (online_type == MMOP_ONLINE_MOVABLE && 10034f7c6b49STang Chen zone_idx(zone) == ZONE_MOVABLE - 1) { 1004bfc8c901SVladimir Davydov if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) 100530467e0bSDavid Rientjes return -EINVAL; 1006511c2abaSLai Jiangshan } 1007511c2abaSLai Jiangshan 1008511c2abaSLai Jiangshan /* Previous code may changed the zone of the pfn range */ 1009511c2abaSLai Jiangshan zone = page_zone(pfn_to_page(pfn)); 1010511c2abaSLai Jiangshan 10117b78d335SYasunori Goto arg.start_pfn = pfn; 10127b78d335SYasunori Goto arg.nr_pages = nr_pages; 1013d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 10147b78d335SYasunori Goto 10159c2606b7SXishi Qiu nid = pfn_to_nid(pfn); 10167b78d335SYasunori Goto 10177b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 10187b78d335SYasunori Goto ret = notifier_to_errno(ret); 10197b78d335SYasunori Goto if (ret) { 10207b78d335SYasunori Goto memory_notify(MEM_CANCEL_ONLINE, &arg); 102130467e0bSDavid Rientjes return ret; 10227b78d335SYasunori Goto } 10233947be19SDave Hansen /* 10246811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 10256811378eSYasunori Goto * This means the page allocator ignores this zone. 10266811378eSYasunori Goto * So, zonelist must be updated after online. 10276811378eSYasunori Goto */ 10284eaf3f64SHaicheng Li mutex_lock(&zonelists_mutex); 10296dcd73d7SWen Congyang if (!populated_zone(zone)) { 10306811378eSYasunori Goto need_zonelists_rebuild = 1; 10316dcd73d7SWen Congyang build_all_zonelists(NULL, zone); 10326dcd73d7SWen Congyang } 10336811378eSYasunori Goto 1034908eedc6SKAMEZAWA Hiroyuki ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, 103575884fb1SKAMEZAWA Hiroyuki online_pages_range); 1036fd8a4221SGeoff Levand if (ret) { 10376dcd73d7SWen Congyang if (need_zonelists_rebuild) 10386dcd73d7SWen Congyang zone_pcp_reset(zone); 10394eaf3f64SHaicheng Li mutex_unlock(&zonelists_mutex); 1040a62e2f4fSBjorn Helgaas printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n", 1041a62e2f4fSBjorn Helgaas (unsigned long long) pfn << PAGE_SHIFT, 1042a62e2f4fSBjorn Helgaas (((unsigned long long) pfn + nr_pages) 1043a62e2f4fSBjorn Helgaas << PAGE_SHIFT) - 1); 1044fd8a4221SGeoff Levand memory_notify(MEM_CANCEL_ONLINE, &arg); 104530467e0bSDavid Rientjes return ret; 1046fd8a4221SGeoff Levand } 1047fd8a4221SGeoff Levand 10483947be19SDave Hansen zone->present_pages += onlined_pages; 1049aa47228aSCody P Schafer 1050aa47228aSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 1051f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 1052aa47228aSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 1053aa47228aSCody P Schafer 105408dff7b7SJiang Liu if (onlined_pages) { 1055d9713679SLai Jiangshan node_states_set_node(zone_to_nid(zone), &arg); 10561f522509SHaicheng Li if (need_zonelists_rebuild) 10576dcd73d7SWen Congyang build_all_zonelists(NULL, NULL); 10581f522509SHaicheng Li else 1059112067f0SShaohua Li zone_pcp_update(zone); 106008dff7b7SJiang Liu } 10611f522509SHaicheng Li 10624eaf3f64SHaicheng Li mutex_unlock(&zonelists_mutex); 10631b79acc9SKOSAKI Motohiro 10641b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 10651b79acc9SKOSAKI Motohiro 106608dff7b7SJiang Liu if (onlined_pages) 10677ea1530aSChristoph Lameter kswapd_run(zone_to_nid(zone)); 106861b13993SDave Hansen 10695a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 10702f7f24ecSKent Liu 10712d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 10727b78d335SYasunori Goto 10737b78d335SYasunori Goto if (onlined_pages) 10747b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 107530467e0bSDavid Rientjes return 0; 10763947be19SDave Hansen } 107753947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 1078bc02af93SYasunori Goto 10790bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 10800bd85420STang Chen { 10810bd85420STang Chen struct zone *z; 10820bd85420STang Chen 10830bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 10840bd85420STang Chen z->present_pages = 0; 10850bd85420STang Chen 10860bd85420STang Chen pgdat->node_present_pages = 0; 10870bd85420STang Chen } 10880bd85420STang Chen 1089e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 1090e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) 10919af3c2deSYasunori Goto { 10929af3c2deSYasunori Goto struct pglist_data *pgdat; 10939af3c2deSYasunori Goto unsigned long zones_size[MAX_NR_ZONES] = {0}; 10949af3c2deSYasunori Goto unsigned long zholes_size[MAX_NR_ZONES] = {0}; 1095c8e861a5SFabian Frederick unsigned long start_pfn = PFN_DOWN(start); 10969af3c2deSYasunori Goto 1097a1e565aaSTang Chen pgdat = NODE_DATA(nid); 1098a1e565aaSTang Chen if (!pgdat) { 10999af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 11009af3c2deSYasunori Goto if (!pgdat) 11019af3c2deSYasunori Goto return NULL; 11029af3c2deSYasunori Goto 11039af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 1104b0dc3a34SGu Zheng } else { 1105b0dc3a34SGu Zheng /* Reset the nr_zones and classzone_idx to 0 before reuse */ 1106b0dc3a34SGu Zheng pgdat->nr_zones = 0; 1107b0dc3a34SGu Zheng pgdat->classzone_idx = 0; 1108a1e565aaSTang Chen } 11099af3c2deSYasunori Goto 11109af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 11119af3c2deSYasunori Goto 11129af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 11139109fb7bSJohannes Weiner free_area_init_node(nid, zones_size, start_pfn, zholes_size); 11149af3c2deSYasunori Goto 1115959ecc48SKAMEZAWA Hiroyuki /* 1116959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 1117959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 1118959ecc48SKAMEZAWA Hiroyuki */ 1119f957db4fSDavid Rientjes mutex_lock(&zonelists_mutex); 11209adb62a5SJiang Liu build_all_zonelists(pgdat, NULL); 1121f957db4fSDavid Rientjes mutex_unlock(&zonelists_mutex); 1122959ecc48SKAMEZAWA Hiroyuki 1123f784a3f1STang Chen /* 1124f784a3f1STang Chen * zone->managed_pages is set to an approximate value in 1125f784a3f1STang Chen * free_area_init_core(), which will cause 1126f784a3f1STang Chen * /sys/device/system/node/nodeX/meminfo has wrong data. 1127f784a3f1STang Chen * So reset it to 0 before any memory is onlined. 1128f784a3f1STang Chen */ 1129f784a3f1STang Chen reset_node_managed_pages(pgdat); 1130f784a3f1STang Chen 11310bd85420STang Chen /* 11320bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 11330bd85420STang Chen * clear all zones' present_pages because they will be updated in 11340bd85420STang Chen * online_pages() and offline_pages(). 11350bd85420STang Chen */ 11360bd85420STang Chen reset_node_present_pages(pgdat); 11370bd85420STang Chen 11389af3c2deSYasunori Goto return pgdat; 11399af3c2deSYasunori Goto } 11409af3c2deSYasunori Goto 11419af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat) 11429af3c2deSYasunori Goto { 11439af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 11449af3c2deSYasunori Goto arch_free_nodedata(pgdat); 11459af3c2deSYasunori Goto return; 11469af3c2deSYasunori Goto } 11479af3c2deSYasunori Goto 11480a547039SKAMEZAWA Hiroyuki 114901b0f197SToshi Kani /** 115001b0f197SToshi Kani * try_online_node - online a node if offlined 115101b0f197SToshi Kani * 1152cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1153cf23422bSminskey guo */ 115401b0f197SToshi Kani int try_online_node(int nid) 1155cf23422bSminskey guo { 1156cf23422bSminskey guo pg_data_t *pgdat; 1157cf23422bSminskey guo int ret; 1158cf23422bSminskey guo 115901b0f197SToshi Kani if (node_online(nid)) 116001b0f197SToshi Kani return 0; 116101b0f197SToshi Kani 1162bfc8c901SVladimir Davydov mem_hotplug_begin(); 1163cf23422bSminskey guo pgdat = hotadd_new_pgdat(nid, 0); 11647553e8f2SDavid Rientjes if (!pgdat) { 116501b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1166cf23422bSminskey guo ret = -ENOMEM; 1167cf23422bSminskey guo goto out; 1168cf23422bSminskey guo } 1169cf23422bSminskey guo node_set_online(nid); 1170cf23422bSminskey guo ret = register_one_node(nid); 1171cf23422bSminskey guo BUG_ON(ret); 1172cf23422bSminskey guo 117301b0f197SToshi Kani if (pgdat->node_zonelists->_zonerefs->zone == NULL) { 117401b0f197SToshi Kani mutex_lock(&zonelists_mutex); 117501b0f197SToshi Kani build_all_zonelists(NULL, NULL); 117601b0f197SToshi Kani mutex_unlock(&zonelists_mutex); 117701b0f197SToshi Kani } 117801b0f197SToshi Kani 1179cf23422bSminskey guo out: 1180bfc8c901SVladimir Davydov mem_hotplug_done(); 1181cf23422bSminskey guo return ret; 1182cf23422bSminskey guo } 1183cf23422bSminskey guo 118427356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 118527356f54SToshi Kani { 1186c8e861a5SFabian Frederick u64 start_pfn = PFN_DOWN(start); 118727356f54SToshi Kani u64 nr_pages = size >> PAGE_SHIFT; 118827356f54SToshi Kani 118927356f54SToshi Kani /* Memory range must be aligned with section */ 119027356f54SToshi Kani if ((start_pfn & ~PAGE_SECTION_MASK) || 119127356f54SToshi Kani (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) { 119227356f54SToshi Kani pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n", 119327356f54SToshi Kani (unsigned long long)start, 119427356f54SToshi Kani (unsigned long long)size); 119527356f54SToshi Kani return -EINVAL; 119627356f54SToshi Kani } 119727356f54SToshi Kani 119827356f54SToshi Kani return 0; 119927356f54SToshi Kani } 120027356f54SToshi Kani 120163264400SWang Nan /* 120263264400SWang Nan * If movable zone has already been setup, newly added memory should be check. 120363264400SWang Nan * If its address is higher than movable zone, it should be added as movable. 120463264400SWang Nan * Without this check, movable zone may overlap with other zone. 120563264400SWang Nan */ 120663264400SWang Nan static int should_add_memory_movable(int nid, u64 start, u64 size) 120763264400SWang Nan { 120863264400SWang Nan unsigned long start_pfn = start >> PAGE_SHIFT; 120963264400SWang Nan pg_data_t *pgdat = NODE_DATA(nid); 121063264400SWang Nan struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE; 121163264400SWang Nan 121263264400SWang Nan if (zone_is_empty(movable_zone)) 121363264400SWang Nan return 0; 121463264400SWang Nan 121563264400SWang Nan if (movable_zone->zone_start_pfn <= start_pfn) 121663264400SWang Nan return 1; 121763264400SWang Nan 121863264400SWang Nan return 0; 121963264400SWang Nan } 122063264400SWang Nan 1221033fbae9SDan Williams int zone_for_memory(int nid, u64 start, u64 size, int zone_default, 1222033fbae9SDan Williams bool for_device) 122363264400SWang Nan { 1224033fbae9SDan Williams #ifdef CONFIG_ZONE_DEVICE 1225033fbae9SDan Williams if (for_device) 1226033fbae9SDan Williams return ZONE_DEVICE; 1227033fbae9SDan Williams #endif 122863264400SWang Nan if (should_add_memory_movable(nid, start, size)) 122963264400SWang Nan return ZONE_MOVABLE; 123063264400SWang Nan 123163264400SWang Nan return zone_default; 123263264400SWang Nan } 123363264400SWang Nan 123431168481SAl Viro /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 123562cedb9fSDavid Vrabel int __ref add_memory_resource(int nid, struct resource *res) 1236bc02af93SYasunori Goto { 123762cedb9fSDavid Vrabel u64 start, size; 12389af3c2deSYasunori Goto pg_data_t *pgdat = NULL; 1239a1e565aaSTang Chen bool new_pgdat; 1240a1e565aaSTang Chen bool new_node; 1241bc02af93SYasunori Goto int ret; 1242bc02af93SYasunori Goto 124362cedb9fSDavid Vrabel start = res->start; 124462cedb9fSDavid Vrabel size = resource_size(res); 124562cedb9fSDavid Vrabel 124627356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 124727356f54SToshi Kani if (ret) 124827356f54SToshi Kani return ret; 124927356f54SToshi Kani 1250a1e565aaSTang Chen { /* Stupid hack to suppress address-never-null warning */ 1251a1e565aaSTang Chen void *p = NODE_DATA(nid); 1252a1e565aaSTang Chen new_pgdat = !p; 1253a1e565aaSTang Chen } 1254ac13c462SNathan Zimmer 1255bfc8c901SVladimir Davydov mem_hotplug_begin(); 1256ac13c462SNathan Zimmer 12577f36e3e5STang Chen /* 12587f36e3e5STang Chen * Add new range to memblock so that when hotadd_new_pgdat() is called 12597f36e3e5STang Chen * to allocate new pgdat, get_pfn_range_for_nid() will be able to find 12607f36e3e5STang Chen * this new range and calculate total pages correctly. The range will 12617f36e3e5STang Chen * be removed at hot-remove time. 12627f36e3e5STang Chen */ 12637f36e3e5STang Chen memblock_add_node(start, size, nid); 12647f36e3e5STang Chen 1265a1e565aaSTang Chen new_node = !node_online(nid); 1266a1e565aaSTang Chen if (new_node) { 12679af3c2deSYasunori Goto pgdat = hotadd_new_pgdat(nid, start); 12686ad696d2SAndi Kleen ret = -ENOMEM; 12699af3c2deSYasunori Goto if (!pgdat) 127041b9e2d7SWen Congyang goto error; 12719af3c2deSYasunori Goto } 12729af3c2deSYasunori Goto 1273bc02af93SYasunori Goto /* call arch's memory hotadd */ 1274033fbae9SDan Williams ret = arch_add_memory(nid, start, size, false); 1275bc02af93SYasunori Goto 12769af3c2deSYasunori Goto if (ret < 0) 12779af3c2deSYasunori Goto goto error; 12789af3c2deSYasunori Goto 12790fc44159SYasunori Goto /* we online node here. we can't roll back from here. */ 12809af3c2deSYasunori Goto node_set_online(nid); 12819af3c2deSYasunori Goto 1282a1e565aaSTang Chen if (new_node) { 12830fc44159SYasunori Goto ret = register_one_node(nid); 12840fc44159SYasunori Goto /* 12850fc44159SYasunori Goto * If sysfs file of new node can't create, cpu on the node 12860fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 12870fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 12880fc44159SYasunori Goto */ 12890fc44159SYasunori Goto BUG_ON(ret); 12900fc44159SYasunori Goto } 12910fc44159SYasunori Goto 1292d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 1293d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1294d96ae530Sakpm@linux-foundation.org 12956ad696d2SAndi Kleen goto out; 12966ad696d2SAndi Kleen 12979af3c2deSYasunori Goto error: 12989af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 12999af3c2deSYasunori Goto if (new_pgdat) 13009af3c2deSYasunori Goto rollback_node_hotadd(nid, pgdat); 13017f36e3e5STang Chen memblock_remove(start, size); 13029af3c2deSYasunori Goto 13036ad696d2SAndi Kleen out: 1304bfc8c901SVladimir Davydov mem_hotplug_done(); 1305bc02af93SYasunori Goto return ret; 1306bc02af93SYasunori Goto } 130762cedb9fSDavid Vrabel EXPORT_SYMBOL_GPL(add_memory_resource); 130862cedb9fSDavid Vrabel 130962cedb9fSDavid Vrabel int __ref add_memory(int nid, u64 start, u64 size) 131062cedb9fSDavid Vrabel { 131162cedb9fSDavid Vrabel struct resource *res; 131262cedb9fSDavid Vrabel int ret; 131362cedb9fSDavid Vrabel 131462cedb9fSDavid Vrabel res = register_memory_resource(start, size); 131562cedb9fSDavid Vrabel if (!res) 131662cedb9fSDavid Vrabel return -EEXIST; 131762cedb9fSDavid Vrabel 131862cedb9fSDavid Vrabel ret = add_memory_resource(nid, res); 131962cedb9fSDavid Vrabel if (ret < 0) 132062cedb9fSDavid Vrabel release_memory_resource(res); 132162cedb9fSDavid Vrabel return ret; 132262cedb9fSDavid Vrabel } 1323bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 13240c0e6195SKAMEZAWA Hiroyuki 13250c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 13260c0e6195SKAMEZAWA Hiroyuki /* 13275c755e9fSBadari Pulavarty * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy 13285c755e9fSBadari Pulavarty * set and the size of the free page is given by page_order(). Using this, 13295c755e9fSBadari Pulavarty * the function determines if the pageblock contains only free pages. 13305c755e9fSBadari Pulavarty * Due to buddy contraints, a free page at least the size of a pageblock will 13315c755e9fSBadari Pulavarty * be located at the start of the pageblock 13325c755e9fSBadari Pulavarty */ 13335c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page) 13345c755e9fSBadari Pulavarty { 13355c755e9fSBadari Pulavarty return PageBuddy(page) && page_order(page) >= pageblock_order; 13365c755e9fSBadari Pulavarty } 13375c755e9fSBadari Pulavarty 13385c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */ 13395c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page) 13405c755e9fSBadari Pulavarty { 13415c755e9fSBadari Pulavarty /* Ensure the starting page is pageblock-aligned */ 13425c755e9fSBadari Pulavarty BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); 13435c755e9fSBadari Pulavarty 13445c755e9fSBadari Pulavarty /* If the entire pageblock is free, move to the end of free page */ 13450dcc48c1SKAMEZAWA Hiroyuki if (pageblock_free(page)) { 13460dcc48c1SKAMEZAWA Hiroyuki int order; 13470dcc48c1SKAMEZAWA Hiroyuki /* be careful. we don't have locks, page_order can be changed.*/ 13480dcc48c1SKAMEZAWA Hiroyuki order = page_order(page); 13490dcc48c1SKAMEZAWA Hiroyuki if ((order < MAX_ORDER) && (order >= pageblock_order)) 13500dcc48c1SKAMEZAWA Hiroyuki return page + (1 << order); 13510dcc48c1SKAMEZAWA Hiroyuki } 13525c755e9fSBadari Pulavarty 13530dcc48c1SKAMEZAWA Hiroyuki return page + pageblock_nr_pages; 13545c755e9fSBadari Pulavarty } 13555c755e9fSBadari Pulavarty 13565c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */ 13575c755e9fSBadari Pulavarty int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) 13585c755e9fSBadari Pulavarty { 13595c755e9fSBadari Pulavarty struct page *page = pfn_to_page(start_pfn); 13605c755e9fSBadari Pulavarty struct page *end_page = page + nr_pages; 13615c755e9fSBadari Pulavarty 13625c755e9fSBadari Pulavarty /* Check the starting page of each pageblock within the range */ 13635c755e9fSBadari Pulavarty for (; page < end_page; page = next_active_pageblock(page)) { 136449ac8255SKAMEZAWA Hiroyuki if (!is_pageblock_removable_nolock(page)) 13655c755e9fSBadari Pulavarty return 0; 136649ac8255SKAMEZAWA Hiroyuki cond_resched(); 13675c755e9fSBadari Pulavarty } 13685c755e9fSBadari Pulavarty 13695c755e9fSBadari Pulavarty /* All pageblocks in the memory block are likely to be hot-removable */ 13705c755e9fSBadari Pulavarty return 1; 13715c755e9fSBadari Pulavarty } 13725c755e9fSBadari Pulavarty 13735c755e9fSBadari Pulavarty /* 13740c0e6195SKAMEZAWA Hiroyuki * Confirm all pages in a range [start, end) is belongs to the same zone. 13750c0e6195SKAMEZAWA Hiroyuki */ 1376ed2f2400SZhang Zhen int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) 13770c0e6195SKAMEZAWA Hiroyuki { 1378*5f0f2887SAndrew Banman unsigned long pfn, sec_end_pfn; 13790c0e6195SKAMEZAWA Hiroyuki struct zone *zone = NULL; 13800c0e6195SKAMEZAWA Hiroyuki struct page *page; 13810c0e6195SKAMEZAWA Hiroyuki int i; 1382*5f0f2887SAndrew Banman for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn); 13830c0e6195SKAMEZAWA Hiroyuki pfn < end_pfn; 1384*5f0f2887SAndrew Banman pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) { 1385*5f0f2887SAndrew Banman /* Make sure the memory section is present first */ 1386*5f0f2887SAndrew Banman if (!present_section_nr(pfn_to_section_nr(pfn))) 1387*5f0f2887SAndrew Banman continue; 1388*5f0f2887SAndrew Banman for (; pfn < sec_end_pfn && pfn < end_pfn; 13890c0e6195SKAMEZAWA Hiroyuki pfn += MAX_ORDER_NR_PAGES) { 13900c0e6195SKAMEZAWA Hiroyuki i = 0; 13910c0e6195SKAMEZAWA Hiroyuki /* This is just a CONFIG_HOLES_IN_ZONE check.*/ 1392*5f0f2887SAndrew Banman while ((i < MAX_ORDER_NR_PAGES) && 1393*5f0f2887SAndrew Banman !pfn_valid_within(pfn + i)) 13940c0e6195SKAMEZAWA Hiroyuki i++; 13950c0e6195SKAMEZAWA Hiroyuki if (i == MAX_ORDER_NR_PAGES) 13960c0e6195SKAMEZAWA Hiroyuki continue; 13970c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn + i); 13980c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 13990c0e6195SKAMEZAWA Hiroyuki return 0; 14000c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 14010c0e6195SKAMEZAWA Hiroyuki } 1402*5f0f2887SAndrew Banman } 14030c0e6195SKAMEZAWA Hiroyuki return 1; 14040c0e6195SKAMEZAWA Hiroyuki } 14050c0e6195SKAMEZAWA Hiroyuki 14060c0e6195SKAMEZAWA Hiroyuki /* 1407c8721bbbSNaoya Horiguchi * Scan pfn range [start,end) to find movable/migratable pages (LRU pages 1408c8721bbbSNaoya Horiguchi * and hugepages). We scan pfn because it's much easier than scanning over 1409c8721bbbSNaoya Horiguchi * linked list. This function returns the pfn of the first found movable 1410c8721bbbSNaoya Horiguchi * page if it's found, otherwise 0. 14110c0e6195SKAMEZAWA Hiroyuki */ 1412c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end) 14130c0e6195SKAMEZAWA Hiroyuki { 14140c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 14150c0e6195SKAMEZAWA Hiroyuki struct page *page; 14160c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 14170c0e6195SKAMEZAWA Hiroyuki if (pfn_valid(pfn)) { 14180c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 14190c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 14200c0e6195SKAMEZAWA Hiroyuki return pfn; 1421c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 14227e1f049eSNaoya Horiguchi if (page_huge_active(page)) 1423c8721bbbSNaoya Horiguchi return pfn; 1424c8721bbbSNaoya Horiguchi else 1425c8721bbbSNaoya Horiguchi pfn = round_up(pfn + 1, 1426c8721bbbSNaoya Horiguchi 1 << compound_order(page)) - 1; 1427c8721bbbSNaoya Horiguchi } 14280c0e6195SKAMEZAWA Hiroyuki } 14290c0e6195SKAMEZAWA Hiroyuki } 14300c0e6195SKAMEZAWA Hiroyuki return 0; 14310c0e6195SKAMEZAWA Hiroyuki } 14320c0e6195SKAMEZAWA Hiroyuki 14330c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES (256) 14340c0e6195SKAMEZAWA Hiroyuki static int 14350c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 14360c0e6195SKAMEZAWA Hiroyuki { 14370c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 14380c0e6195SKAMEZAWA Hiroyuki struct page *page; 14390c0e6195SKAMEZAWA Hiroyuki int move_pages = NR_OFFLINE_AT_ONCE_PAGES; 14400c0e6195SKAMEZAWA Hiroyuki int not_managed = 0; 14410c0e6195SKAMEZAWA Hiroyuki int ret = 0; 14420c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 14430c0e6195SKAMEZAWA Hiroyuki 14440c0e6195SKAMEZAWA Hiroyuki for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { 14450c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 14460c0e6195SKAMEZAWA Hiroyuki continue; 14470c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 1448c8721bbbSNaoya Horiguchi 1449c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1450c8721bbbSNaoya Horiguchi struct page *head = compound_head(page); 1451c8721bbbSNaoya Horiguchi pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1; 1452c8721bbbSNaoya Horiguchi if (compound_order(head) > PFN_SECTION_SHIFT) { 1453c8721bbbSNaoya Horiguchi ret = -EBUSY; 1454c8721bbbSNaoya Horiguchi break; 1455c8721bbbSNaoya Horiguchi } 1456c8721bbbSNaoya Horiguchi if (isolate_huge_page(page, &source)) 1457c8721bbbSNaoya Horiguchi move_pages -= 1 << compound_order(head); 1458c8721bbbSNaoya Horiguchi continue; 1459c8721bbbSNaoya Horiguchi } 1460c8721bbbSNaoya Horiguchi 1461700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 14620c0e6195SKAMEZAWA Hiroyuki continue; 14630c0e6195SKAMEZAWA Hiroyuki /* 14640c0e6195SKAMEZAWA Hiroyuki * We can skip free pages. And we can only deal with pages on 14650c0e6195SKAMEZAWA Hiroyuki * LRU. 14660c0e6195SKAMEZAWA Hiroyuki */ 146762695a84SNick Piggin ret = isolate_lru_page(page); 14680c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 1469700c2a46SKonstantin Khlebnikov put_page(page); 147062695a84SNick Piggin list_add_tail(&page->lru, &source); 14710c0e6195SKAMEZAWA Hiroyuki move_pages--; 14726d9c285aSKOSAKI Motohiro inc_zone_page_state(page, NR_ISOLATED_ANON + 14736d9c285aSKOSAKI Motohiro page_is_file_cache(page)); 14746d9c285aSKOSAKI Motohiro 14750c0e6195SKAMEZAWA Hiroyuki } else { 14760c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_DEBUG_VM 1477718a3821SWu Fengguang printk(KERN_ALERT "removing pfn %lx from LRU failed\n", 1478718a3821SWu Fengguang pfn); 1479f0b791a3SDave Hansen dump_page(page, "failed to remove from LRU"); 14800c0e6195SKAMEZAWA Hiroyuki #endif 1481700c2a46SKonstantin Khlebnikov put_page(page); 148225985edcSLucas De Marchi /* Because we don't have big zone->lock. we should 1483809c4449SBob Liu check this again here. */ 1484809c4449SBob Liu if (page_count(page)) { 1485809c4449SBob Liu not_managed++; 1486f3ab2636SBob Liu ret = -EBUSY; 1487809c4449SBob Liu break; 1488809c4449SBob Liu } 14890c0e6195SKAMEZAWA Hiroyuki } 14900c0e6195SKAMEZAWA Hiroyuki } 1491f3ab2636SBob Liu if (!list_empty(&source)) { 14920c0e6195SKAMEZAWA Hiroyuki if (not_managed) { 1493c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 14940c0e6195SKAMEZAWA Hiroyuki goto out; 14950c0e6195SKAMEZAWA Hiroyuki } 149674c08f98SMinchan Kim 149774c08f98SMinchan Kim /* 149874c08f98SMinchan Kim * alloc_migrate_target should be improooooved!! 149974c08f98SMinchan Kim * migrate_pages returns # of failed pages. 150074c08f98SMinchan Kim */ 150168711a74SDavid Rientjes ret = migrate_pages(&source, alloc_migrate_target, NULL, 0, 15029c620e2bSHugh Dickins MIGRATE_SYNC, MR_MEMORY_HOTPLUG); 1503cf608ac1SMinchan Kim if (ret) 1504c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1505f3ab2636SBob Liu } 15060c0e6195SKAMEZAWA Hiroyuki out: 15070c0e6195SKAMEZAWA Hiroyuki return ret; 15080c0e6195SKAMEZAWA Hiroyuki } 15090c0e6195SKAMEZAWA Hiroyuki 15100c0e6195SKAMEZAWA Hiroyuki /* 15110c0e6195SKAMEZAWA Hiroyuki * remove from free_area[] and mark all as Reserved. 15120c0e6195SKAMEZAWA Hiroyuki */ 15130c0e6195SKAMEZAWA Hiroyuki static int 15140c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, 15150c0e6195SKAMEZAWA Hiroyuki void *data) 15160c0e6195SKAMEZAWA Hiroyuki { 15170c0e6195SKAMEZAWA Hiroyuki __offline_isolated_pages(start, start + nr_pages); 15180c0e6195SKAMEZAWA Hiroyuki return 0; 15190c0e6195SKAMEZAWA Hiroyuki } 15200c0e6195SKAMEZAWA Hiroyuki 15210c0e6195SKAMEZAWA Hiroyuki static void 15220c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) 15230c0e6195SKAMEZAWA Hiroyuki { 1524908eedc6SKAMEZAWA Hiroyuki walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, 15250c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb); 15260c0e6195SKAMEZAWA Hiroyuki } 15270c0e6195SKAMEZAWA Hiroyuki 15280c0e6195SKAMEZAWA Hiroyuki /* 15290c0e6195SKAMEZAWA Hiroyuki * Check all pages in range, recoreded as memory resource, are isolated. 15300c0e6195SKAMEZAWA Hiroyuki */ 15310c0e6195SKAMEZAWA Hiroyuki static int 15320c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, 15330c0e6195SKAMEZAWA Hiroyuki void *data) 15340c0e6195SKAMEZAWA Hiroyuki { 15350c0e6195SKAMEZAWA Hiroyuki int ret; 15360c0e6195SKAMEZAWA Hiroyuki long offlined = *(long *)data; 1537b023f468SWen Congyang ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true); 15380c0e6195SKAMEZAWA Hiroyuki offlined = nr_pages; 15390c0e6195SKAMEZAWA Hiroyuki if (!ret) 15400c0e6195SKAMEZAWA Hiroyuki *(long *)data += offlined; 15410c0e6195SKAMEZAWA Hiroyuki return ret; 15420c0e6195SKAMEZAWA Hiroyuki } 15430c0e6195SKAMEZAWA Hiroyuki 15440c0e6195SKAMEZAWA Hiroyuki static long 15450c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) 15460c0e6195SKAMEZAWA Hiroyuki { 15470c0e6195SKAMEZAWA Hiroyuki long offlined = 0; 15480c0e6195SKAMEZAWA Hiroyuki int ret; 15490c0e6195SKAMEZAWA Hiroyuki 1550908eedc6SKAMEZAWA Hiroyuki ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, 15510c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb); 15520c0e6195SKAMEZAWA Hiroyuki if (ret < 0) 15530c0e6195SKAMEZAWA Hiroyuki offlined = (long)ret; 15540c0e6195SKAMEZAWA Hiroyuki return offlined; 15550c0e6195SKAMEZAWA Hiroyuki } 15560c0e6195SKAMEZAWA Hiroyuki 155709285af7SLai Jiangshan #ifdef CONFIG_MOVABLE_NODE 155879a4dcefSTang Chen /* 155979a4dcefSTang Chen * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have 156079a4dcefSTang Chen * normal memory. 156179a4dcefSTang Chen */ 156209285af7SLai Jiangshan static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) 156309285af7SLai Jiangshan { 156409285af7SLai Jiangshan return true; 156509285af7SLai Jiangshan } 156679a4dcefSTang Chen #else /* CONFIG_MOVABLE_NODE */ 156774d42d8fSLai Jiangshan /* ensure the node has NORMAL memory if it is still online */ 156874d42d8fSLai Jiangshan static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) 156974d42d8fSLai Jiangshan { 157074d42d8fSLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 157174d42d8fSLai Jiangshan unsigned long present_pages = 0; 157274d42d8fSLai Jiangshan enum zone_type zt; 157374d42d8fSLai Jiangshan 157474d42d8fSLai Jiangshan for (zt = 0; zt <= ZONE_NORMAL; zt++) 157574d42d8fSLai Jiangshan present_pages += pgdat->node_zones[zt].present_pages; 157674d42d8fSLai Jiangshan 157774d42d8fSLai Jiangshan if (present_pages > nr_pages) 157874d42d8fSLai Jiangshan return true; 157974d42d8fSLai Jiangshan 158074d42d8fSLai Jiangshan present_pages = 0; 158174d42d8fSLai Jiangshan for (; zt <= ZONE_MOVABLE; zt++) 158274d42d8fSLai Jiangshan present_pages += pgdat->node_zones[zt].present_pages; 158374d42d8fSLai Jiangshan 158474d42d8fSLai Jiangshan /* 158574d42d8fSLai Jiangshan * we can't offline the last normal memory until all 158674d42d8fSLai Jiangshan * higher memory is offlined. 158774d42d8fSLai Jiangshan */ 158874d42d8fSLai Jiangshan return present_pages == 0; 158974d42d8fSLai Jiangshan } 159079a4dcefSTang Chen #endif /* CONFIG_MOVABLE_NODE */ 159174d42d8fSLai Jiangshan 1592c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1593c5320926STang Chen { 1594c5320926STang Chen #ifdef CONFIG_MOVABLE_NODE 1595c5320926STang Chen /* 1596c5320926STang Chen * Memory used by the kernel cannot be hot-removed because Linux 1597c5320926STang Chen * cannot migrate the kernel pages. When memory hotplug is 1598c5320926STang Chen * enabled, we should prevent memblock from allocating memory 1599c5320926STang Chen * for the kernel. 1600c5320926STang Chen * 1601c5320926STang Chen * ACPI SRAT records all hotpluggable memory ranges. But before 1602c5320926STang Chen * SRAT is parsed, we don't know about it. 1603c5320926STang Chen * 1604c5320926STang Chen * The kernel image is loaded into memory at very early time. We 1605c5320926STang Chen * cannot prevent this anyway. So on NUMA system, we set any 1606c5320926STang Chen * node the kernel resides in as un-hotpluggable. 1607c5320926STang Chen * 1608c5320926STang Chen * Since on modern servers, one node could have double-digit 1609c5320926STang Chen * gigabytes memory, we can assume the memory around the kernel 1610c5320926STang Chen * image is also un-hotpluggable. So before SRAT is parsed, just 1611c5320926STang Chen * allocate memory near the kernel image to try the best to keep 1612c5320926STang Chen * the kernel away from hotpluggable memory. 1613c5320926STang Chen */ 1614c5320926STang Chen memblock_set_bottom_up(true); 161555ac590cSTang Chen movable_node_enabled = true; 1616c5320926STang Chen #else 1617c5320926STang Chen pr_warn("movable_node option not supported\n"); 1618c5320926STang Chen #endif 1619c5320926STang Chen return 0; 1620c5320926STang Chen } 1621c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1622c5320926STang Chen 1623d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1624d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1625d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1626d9713679SLai Jiangshan { 1627d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1628d9713679SLai Jiangshan unsigned long present_pages = 0; 1629d9713679SLai Jiangshan enum zone_type zt, zone_last = ZONE_NORMAL; 1630d9713679SLai Jiangshan 1631d9713679SLai Jiangshan /* 16326715ddf9SLai Jiangshan * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] 16336715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_NORMAL, 16346715ddf9SLai Jiangshan * set zone_last to ZONE_NORMAL. 1635d9713679SLai Jiangshan * 16366715ddf9SLai Jiangshan * If we don't have HIGHMEM nor movable node, 16376715ddf9SLai Jiangshan * node_states[N_NORMAL_MEMORY] contains nodes which have zones of 16386715ddf9SLai Jiangshan * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. 1639d9713679SLai Jiangshan */ 16406715ddf9SLai Jiangshan if (N_MEMORY == N_NORMAL_MEMORY) 1641d9713679SLai Jiangshan zone_last = ZONE_MOVABLE; 1642d9713679SLai Jiangshan 1643d9713679SLai Jiangshan /* 1644d9713679SLai Jiangshan * check whether node_states[N_NORMAL_MEMORY] will be changed. 1645d9713679SLai Jiangshan * If the memory to be offline is in a zone of 0...zone_last, 1646d9713679SLai Jiangshan * and it is the last present memory, 0...zone_last will 1647d9713679SLai Jiangshan * become empty after offline , thus we can determind we will 1648d9713679SLai Jiangshan * need to clear the node from node_states[N_NORMAL_MEMORY]. 1649d9713679SLai Jiangshan */ 1650d9713679SLai Jiangshan for (zt = 0; zt <= zone_last; zt++) 1651d9713679SLai Jiangshan present_pages += pgdat->node_zones[zt].present_pages; 1652d9713679SLai Jiangshan if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) 1653d9713679SLai Jiangshan arg->status_change_nid_normal = zone_to_nid(zone); 1654d9713679SLai Jiangshan else 1655d9713679SLai Jiangshan arg->status_change_nid_normal = -1; 1656d9713679SLai Jiangshan 16576715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM 16586715ddf9SLai Jiangshan /* 16596715ddf9SLai Jiangshan * If we have movable node, node_states[N_HIGH_MEMORY] 16606715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_HIGHMEM, 16616715ddf9SLai Jiangshan * set zone_last to ZONE_HIGHMEM. 16626715ddf9SLai Jiangshan * 16636715ddf9SLai Jiangshan * If we don't have movable node, node_states[N_NORMAL_MEMORY] 16646715ddf9SLai Jiangshan * contains nodes which have zones of 0...ZONE_MOVABLE, 16656715ddf9SLai Jiangshan * set zone_last to ZONE_MOVABLE. 16666715ddf9SLai Jiangshan */ 16676715ddf9SLai Jiangshan zone_last = ZONE_HIGHMEM; 16686715ddf9SLai Jiangshan if (N_MEMORY == N_HIGH_MEMORY) 16696715ddf9SLai Jiangshan zone_last = ZONE_MOVABLE; 16706715ddf9SLai Jiangshan 16716715ddf9SLai Jiangshan for (; zt <= zone_last; zt++) 16726715ddf9SLai Jiangshan present_pages += pgdat->node_zones[zt].present_pages; 16736715ddf9SLai Jiangshan if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) 16746715ddf9SLai Jiangshan arg->status_change_nid_high = zone_to_nid(zone); 16756715ddf9SLai Jiangshan else 16766715ddf9SLai Jiangshan arg->status_change_nid_high = -1; 16776715ddf9SLai Jiangshan #else 16786715ddf9SLai Jiangshan arg->status_change_nid_high = arg->status_change_nid_normal; 16796715ddf9SLai Jiangshan #endif 16806715ddf9SLai Jiangshan 1681d9713679SLai Jiangshan /* 1682d9713679SLai Jiangshan * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE 1683d9713679SLai Jiangshan */ 1684d9713679SLai Jiangshan zone_last = ZONE_MOVABLE; 1685d9713679SLai Jiangshan 1686d9713679SLai Jiangshan /* 1687d9713679SLai Jiangshan * check whether node_states[N_HIGH_MEMORY] will be changed 1688d9713679SLai Jiangshan * If we try to offline the last present @nr_pages from the node, 1689d9713679SLai Jiangshan * we can determind we will need to clear the node from 1690d9713679SLai Jiangshan * node_states[N_HIGH_MEMORY]. 1691d9713679SLai Jiangshan */ 1692d9713679SLai Jiangshan for (; zt <= zone_last; zt++) 1693d9713679SLai Jiangshan present_pages += pgdat->node_zones[zt].present_pages; 1694d9713679SLai Jiangshan if (nr_pages >= present_pages) 1695d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1696d9713679SLai Jiangshan else 1697d9713679SLai Jiangshan arg->status_change_nid = -1; 1698d9713679SLai Jiangshan } 1699d9713679SLai Jiangshan 1700d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1701d9713679SLai Jiangshan { 1702d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1703d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1704d9713679SLai Jiangshan 17056715ddf9SLai Jiangshan if ((N_MEMORY != N_NORMAL_MEMORY) && 17066715ddf9SLai Jiangshan (arg->status_change_nid_high >= 0)) 1707d9713679SLai Jiangshan node_clear_state(node, N_HIGH_MEMORY); 17086715ddf9SLai Jiangshan 17096715ddf9SLai Jiangshan if ((N_MEMORY != N_HIGH_MEMORY) && 17106715ddf9SLai Jiangshan (arg->status_change_nid >= 0)) 17116715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1712d9713679SLai Jiangshan } 1713d9713679SLai Jiangshan 1714a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn, 17150c0e6195SKAMEZAWA Hiroyuki unsigned long end_pfn, unsigned long timeout) 17160c0e6195SKAMEZAWA Hiroyuki { 17170c0e6195SKAMEZAWA Hiroyuki unsigned long pfn, nr_pages, expire; 17180c0e6195SKAMEZAWA Hiroyuki long offlined_pages; 17197b78d335SYasunori Goto int ret, drain, retry_max, node; 1720d702909fSCody P Schafer unsigned long flags; 17210c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 17227b78d335SYasunori Goto struct memory_notify arg; 17230c0e6195SKAMEZAWA Hiroyuki 17240c0e6195SKAMEZAWA Hiroyuki /* at least, alignment against pageblock is necessary */ 17250c0e6195SKAMEZAWA Hiroyuki if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) 17260c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 17270c0e6195SKAMEZAWA Hiroyuki if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) 17280c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 17290c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 17300c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 17310c0e6195SKAMEZAWA Hiroyuki if (!test_pages_in_a_zone(start_pfn, end_pfn)) 17320c0e6195SKAMEZAWA Hiroyuki return -EINVAL; 17337b78d335SYasunori Goto 17347b78d335SYasunori Goto zone = page_zone(pfn_to_page(start_pfn)); 17357b78d335SYasunori Goto node = zone_to_nid(zone); 17367b78d335SYasunori Goto nr_pages = end_pfn - start_pfn; 17377b78d335SYasunori Goto 173874d42d8fSLai Jiangshan if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages)) 173930467e0bSDavid Rientjes return -EINVAL; 174074d42d8fSLai Jiangshan 17410c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1742b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1743b023f468SWen Congyang MIGRATE_MOVABLE, true); 17440c0e6195SKAMEZAWA Hiroyuki if (ret) 174530467e0bSDavid Rientjes return ret; 17467b78d335SYasunori Goto 17477b78d335SYasunori Goto arg.start_pfn = start_pfn; 17487b78d335SYasunori Goto arg.nr_pages = nr_pages; 1749d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 17507b78d335SYasunori Goto 17517b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 17527b78d335SYasunori Goto ret = notifier_to_errno(ret); 17537b78d335SYasunori Goto if (ret) 17547b78d335SYasunori Goto goto failed_removal; 17557b78d335SYasunori Goto 17560c0e6195SKAMEZAWA Hiroyuki pfn = start_pfn; 17570c0e6195SKAMEZAWA Hiroyuki expire = jiffies + timeout; 17580c0e6195SKAMEZAWA Hiroyuki drain = 0; 17590c0e6195SKAMEZAWA Hiroyuki retry_max = 5; 17600c0e6195SKAMEZAWA Hiroyuki repeat: 17610c0e6195SKAMEZAWA Hiroyuki /* start memory hot removal */ 17620c0e6195SKAMEZAWA Hiroyuki ret = -EAGAIN; 17630c0e6195SKAMEZAWA Hiroyuki if (time_after(jiffies, expire)) 17640c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 17650c0e6195SKAMEZAWA Hiroyuki ret = -EINTR; 17660c0e6195SKAMEZAWA Hiroyuki if (signal_pending(current)) 17670c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 17680c0e6195SKAMEZAWA Hiroyuki ret = 0; 17690c0e6195SKAMEZAWA Hiroyuki if (drain) { 17700c0e6195SKAMEZAWA Hiroyuki lru_add_drain_all(); 17710c0e6195SKAMEZAWA Hiroyuki cond_resched(); 1772c0554329SVlastimil Babka drain_all_pages(zone); 17730c0e6195SKAMEZAWA Hiroyuki } 17740c0e6195SKAMEZAWA Hiroyuki 1775c8721bbbSNaoya Horiguchi pfn = scan_movable_pages(start_pfn, end_pfn); 1776c8721bbbSNaoya Horiguchi if (pfn) { /* We have movable pages */ 17770c0e6195SKAMEZAWA Hiroyuki ret = do_migrate_range(pfn, end_pfn); 17780c0e6195SKAMEZAWA Hiroyuki if (!ret) { 17790c0e6195SKAMEZAWA Hiroyuki drain = 1; 17800c0e6195SKAMEZAWA Hiroyuki goto repeat; 17810c0e6195SKAMEZAWA Hiroyuki } else { 17820c0e6195SKAMEZAWA Hiroyuki if (ret < 0) 17830c0e6195SKAMEZAWA Hiroyuki if (--retry_max == 0) 17840c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 17850c0e6195SKAMEZAWA Hiroyuki yield(); 17860c0e6195SKAMEZAWA Hiroyuki drain = 1; 17870c0e6195SKAMEZAWA Hiroyuki goto repeat; 17880c0e6195SKAMEZAWA Hiroyuki } 17890c0e6195SKAMEZAWA Hiroyuki } 1790b3834be5SAdam Buchbinder /* drain all zone's lru pagevec, this is asynchronous... */ 17910c0e6195SKAMEZAWA Hiroyuki lru_add_drain_all(); 17920c0e6195SKAMEZAWA Hiroyuki yield(); 1793b3834be5SAdam Buchbinder /* drain pcp pages, this is synchronous. */ 1794c0554329SVlastimil Babka drain_all_pages(zone); 1795c8721bbbSNaoya Horiguchi /* 1796c8721bbbSNaoya Horiguchi * dissolve free hugepages in the memory block before doing offlining 1797c8721bbbSNaoya Horiguchi * actually in order to make hugetlbfs's object counting consistent. 1798c8721bbbSNaoya Horiguchi */ 1799c8721bbbSNaoya Horiguchi dissolve_free_huge_pages(start_pfn, end_pfn); 18000c0e6195SKAMEZAWA Hiroyuki /* check again */ 18010c0e6195SKAMEZAWA Hiroyuki offlined_pages = check_pages_isolated(start_pfn, end_pfn); 18020c0e6195SKAMEZAWA Hiroyuki if (offlined_pages < 0) { 18030c0e6195SKAMEZAWA Hiroyuki ret = -EBUSY; 18040c0e6195SKAMEZAWA Hiroyuki goto failed_removal; 18050c0e6195SKAMEZAWA Hiroyuki } 18060c0e6195SKAMEZAWA Hiroyuki printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); 1807b3834be5SAdam Buchbinder /* Ok, all of our target is isolated. 18080c0e6195SKAMEZAWA Hiroyuki We cannot do rollback at this point. */ 18090c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(start_pfn, end_pfn); 1810dbc0e4ceSKAMEZAWA Hiroyuki /* reset pagetype flags and makes migrate type to be MOVABLE */ 18110815f3d8SMichal Nazarewicz undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 18120c0e6195SKAMEZAWA Hiroyuki /* removal success */ 18133dcc0571SJiang Liu adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); 18140c0e6195SKAMEZAWA Hiroyuki zone->present_pages -= offlined_pages; 1815d702909fSCody P Schafer 1816d702909fSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 18170c0e6195SKAMEZAWA Hiroyuki zone->zone_pgdat->node_present_pages -= offlined_pages; 1818d702909fSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 18197b78d335SYasunori Goto 18201b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 18211b79acc9SKOSAKI Motohiro 18221e8537baSXishi Qiu if (!populated_zone(zone)) { 1823340175b7SJiang Liu zone_pcp_reset(zone); 18241e8537baSXishi Qiu mutex_lock(&zonelists_mutex); 18251e8537baSXishi Qiu build_all_zonelists(NULL, NULL); 18261e8537baSXishi Qiu mutex_unlock(&zonelists_mutex); 18271e8537baSXishi Qiu } else 18281e8537baSXishi Qiu zone_pcp_update(zone); 1829340175b7SJiang Liu 1830d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1831d9713679SLai Jiangshan if (arg.status_change_nid >= 0) 18328fe23e05SDavid Rientjes kswapd_stop(node); 1833bce7394aSMinchan Kim 18340c0e6195SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 18350c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 18367b78d335SYasunori Goto 18377b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 18380c0e6195SKAMEZAWA Hiroyuki return 0; 18390c0e6195SKAMEZAWA Hiroyuki 18400c0e6195SKAMEZAWA Hiroyuki failed_removal: 1841a62e2f4fSBjorn Helgaas printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n", 1842a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 1843a62e2f4fSBjorn Helgaas ((unsigned long long) end_pfn << PAGE_SHIFT) - 1); 18447b78d335SYasunori Goto memory_notify(MEM_CANCEL_OFFLINE, &arg); 18450c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 18460815f3d8SMichal Nazarewicz undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 18470c0e6195SKAMEZAWA Hiroyuki return ret; 18480c0e6195SKAMEZAWA Hiroyuki } 184971088785SBadari Pulavarty 185030467e0bSDavid Rientjes /* Must be protected by mem_hotplug_begin() */ 1851a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages) 1852a16cee10SWen Congyang { 1853a16cee10SWen Congyang return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ); 1854a16cee10SWen Congyang } 1855e2ff3940SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 1856a16cee10SWen Congyang 1857bbc76be6SWen Congyang /** 1858bbc76be6SWen Congyang * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn) 1859bbc76be6SWen Congyang * @start_pfn: start pfn of the memory range 1860e05c4bbfSToshi Kani * @end_pfn: end pfn of the memory range 1861bbc76be6SWen Congyang * @arg: argument passed to func 1862bbc76be6SWen Congyang * @func: callback for each memory section walked 1863bbc76be6SWen Congyang * 1864bbc76be6SWen Congyang * This function walks through all present mem sections in range 1865bbc76be6SWen Congyang * [start_pfn, end_pfn) and call func on each mem section. 1866bbc76be6SWen Congyang * 1867bbc76be6SWen Congyang * Returns the return value of func. 1868bbc76be6SWen Congyang */ 1869e2ff3940SRafael J. Wysocki int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, 1870bbc76be6SWen Congyang void *arg, int (*func)(struct memory_block *, void *)) 187171088785SBadari Pulavarty { 1872e90bdb7fSWen Congyang struct memory_block *mem = NULL; 1873e90bdb7fSWen Congyang struct mem_section *section; 1874e90bdb7fSWen Congyang unsigned long pfn, section_nr; 1875e90bdb7fSWen Congyang int ret; 187671088785SBadari Pulavarty 1877e90bdb7fSWen Congyang for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 1878e90bdb7fSWen Congyang section_nr = pfn_to_section_nr(pfn); 1879e90bdb7fSWen Congyang if (!present_section_nr(section_nr)) 1880e90bdb7fSWen Congyang continue; 1881e90bdb7fSWen Congyang 1882e90bdb7fSWen Congyang section = __nr_to_section(section_nr); 1883e90bdb7fSWen Congyang /* same memblock? */ 1884e90bdb7fSWen Congyang if (mem) 1885e90bdb7fSWen Congyang if ((section_nr >= mem->start_section_nr) && 1886e90bdb7fSWen Congyang (section_nr <= mem->end_section_nr)) 1887e90bdb7fSWen Congyang continue; 1888e90bdb7fSWen Congyang 1889e90bdb7fSWen Congyang mem = find_memory_block_hinted(section, mem); 1890e90bdb7fSWen Congyang if (!mem) 1891e90bdb7fSWen Congyang continue; 1892e90bdb7fSWen Congyang 1893bbc76be6SWen Congyang ret = func(mem, arg); 1894e90bdb7fSWen Congyang if (ret) { 1895e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1896e90bdb7fSWen Congyang return ret; 1897e90bdb7fSWen Congyang } 1898e90bdb7fSWen Congyang } 1899e90bdb7fSWen Congyang 1900e90bdb7fSWen Congyang if (mem) 1901e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1902e90bdb7fSWen Congyang 1903bbc76be6SWen Congyang return 0; 1904bbc76be6SWen Congyang } 1905bbc76be6SWen Congyang 1906e2ff3940SRafael J. Wysocki #ifdef CONFIG_MEMORY_HOTREMOVE 1907d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1908bbc76be6SWen Congyang { 1909bbc76be6SWen Congyang int ret = !is_memblock_offlined(mem); 1910bbc76be6SWen Congyang 1911349daa0fSRandy Dunlap if (unlikely(ret)) { 1912349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 1913349daa0fSRandy Dunlap 1914349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1915349daa0fSRandy Dunlap endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1; 1916bbc76be6SWen Congyang pr_warn("removing memory fails, because memory " 1917349daa0fSRandy Dunlap "[%pa-%pa] is onlined\n", 1918349daa0fSRandy Dunlap &beginpa, &endpa); 1919349daa0fSRandy Dunlap } 1920bbc76be6SWen Congyang 1921bbc76be6SWen Congyang return ret; 1922bbc76be6SWen Congyang } 1923bbc76be6SWen Congyang 19240f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat) 192560a5a19eSTang Chen { 192660a5a19eSTang Chen int cpu; 192760a5a19eSTang Chen 192860a5a19eSTang Chen for_each_present_cpu(cpu) { 192960a5a19eSTang Chen if (cpu_to_node(cpu) == pgdat->node_id) 193060a5a19eSTang Chen /* 193160a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 193260a5a19eSTang Chen * offline this node. 193360a5a19eSTang Chen */ 193460a5a19eSTang Chen return -EBUSY; 193560a5a19eSTang Chen } 193660a5a19eSTang Chen 193760a5a19eSTang Chen return 0; 193860a5a19eSTang Chen } 193960a5a19eSTang Chen 19400f1cfe9dSToshi Kani static void unmap_cpu_on_node(pg_data_t *pgdat) 1941e13fe869SWen Congyang { 1942e13fe869SWen Congyang #ifdef CONFIG_ACPI_NUMA 1943e13fe869SWen Congyang int cpu; 1944e13fe869SWen Congyang 1945e13fe869SWen Congyang for_each_possible_cpu(cpu) 1946e13fe869SWen Congyang if (cpu_to_node(cpu) == pgdat->node_id) 1947e13fe869SWen Congyang numa_clear_node(cpu); 1948e13fe869SWen Congyang #endif 1949e13fe869SWen Congyang } 1950e13fe869SWen Congyang 19510f1cfe9dSToshi Kani static int check_and_unmap_cpu_on_node(pg_data_t *pgdat) 1952e13fe869SWen Congyang { 19530f1cfe9dSToshi Kani int ret; 1954e13fe869SWen Congyang 19550f1cfe9dSToshi Kani ret = check_cpu_on_node(pgdat); 1956e13fe869SWen Congyang if (ret) 1957e13fe869SWen Congyang return ret; 1958e13fe869SWen Congyang 1959e13fe869SWen Congyang /* 1960e13fe869SWen Congyang * the node will be offlined when we come here, so we can clear 1961e13fe869SWen Congyang * the cpu_to_node() now. 1962e13fe869SWen Congyang */ 1963e13fe869SWen Congyang 19640f1cfe9dSToshi Kani unmap_cpu_on_node(pgdat); 1965e13fe869SWen Congyang return 0; 1966e13fe869SWen Congyang } 1967e13fe869SWen Congyang 19680f1cfe9dSToshi Kani /** 19690f1cfe9dSToshi Kani * try_offline_node 19700f1cfe9dSToshi Kani * 19710f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 19720f1cfe9dSToshi Kani * 19730f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 19740f1cfe9dSToshi Kani * and online/offline operations before this call. 19750f1cfe9dSToshi Kani */ 197690b30cdcSWen Congyang void try_offline_node(int nid) 197760a5a19eSTang Chen { 1978d822b86aSWen Congyang pg_data_t *pgdat = NODE_DATA(nid); 1979d822b86aSWen Congyang unsigned long start_pfn = pgdat->node_start_pfn; 1980d822b86aSWen Congyang unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; 198160a5a19eSTang Chen unsigned long pfn; 1982d822b86aSWen Congyang int i; 198360a5a19eSTang Chen 198460a5a19eSTang Chen for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 198560a5a19eSTang Chen unsigned long section_nr = pfn_to_section_nr(pfn); 198660a5a19eSTang Chen 198760a5a19eSTang Chen if (!present_section_nr(section_nr)) 198860a5a19eSTang Chen continue; 198960a5a19eSTang Chen 199060a5a19eSTang Chen if (pfn_to_nid(pfn) != nid) 199160a5a19eSTang Chen continue; 199260a5a19eSTang Chen 199360a5a19eSTang Chen /* 199460a5a19eSTang Chen * some memory sections of this node are not removed, and we 199560a5a19eSTang Chen * can't offline node now. 199660a5a19eSTang Chen */ 199760a5a19eSTang Chen return; 199860a5a19eSTang Chen } 199960a5a19eSTang Chen 20000f1cfe9dSToshi Kani if (check_and_unmap_cpu_on_node(pgdat)) 200160a5a19eSTang Chen return; 200260a5a19eSTang Chen 200360a5a19eSTang Chen /* 200460a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 200560a5a19eSTang Chen * node now. 200660a5a19eSTang Chen */ 200760a5a19eSTang Chen node_set_offline(nid); 200860a5a19eSTang Chen unregister_one_node(nid); 2009d822b86aSWen Congyang 2010d822b86aSWen Congyang /* free waittable in each zone */ 2011d822b86aSWen Congyang for (i = 0; i < MAX_NR_ZONES; i++) { 2012d822b86aSWen Congyang struct zone *zone = pgdat->node_zones + i; 2013d822b86aSWen Congyang 2014ca4b3f30SJianguo Wu /* 2015ca4b3f30SJianguo Wu * wait_table may be allocated from boot memory, 2016ca4b3f30SJianguo Wu * here only free if it's allocated by vmalloc. 2017ca4b3f30SJianguo Wu */ 201885bd8399SGu Zheng if (is_vmalloc_addr(zone->wait_table)) { 2019d822b86aSWen Congyang vfree(zone->wait_table); 202085bd8399SGu Zheng zone->wait_table = NULL; 202185bd8399SGu Zheng } 2022d822b86aSWen Congyang } 202360a5a19eSTang Chen } 202490b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 202560a5a19eSTang Chen 20260f1cfe9dSToshi Kani /** 20270f1cfe9dSToshi Kani * remove_memory 20280f1cfe9dSToshi Kani * 20290f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 20300f1cfe9dSToshi Kani * and online/offline operations before this call, as required by 20310f1cfe9dSToshi Kani * try_offline_node(). 20320f1cfe9dSToshi Kani */ 2033242831ebSRafael J. Wysocki void __ref remove_memory(int nid, u64 start, u64 size) 2034bbc76be6SWen Congyang { 2035242831ebSRafael J. Wysocki int ret; 2036993c1aadSWen Congyang 203727356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 203827356f54SToshi Kani 2039bfc8c901SVladimir Davydov mem_hotplug_begin(); 20406677e3eaSYasuaki Ishimatsu 20416677e3eaSYasuaki Ishimatsu /* 2042242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 2043242831ebSRafael J. Wysocki * whether all memory blocks in question are offline and trigger a BUG() 2044242831ebSRafael J. Wysocki * if this is not the case. 20456677e3eaSYasuaki Ishimatsu */ 2046242831ebSRafael J. Wysocki ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, 2047d6de9d53SXishi Qiu check_memblock_offlined_cb); 2048bfc8c901SVladimir Davydov if (ret) 2049242831ebSRafael J. Wysocki BUG(); 20506677e3eaSYasuaki Ishimatsu 205146c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 205246c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 2053f9126ab9SXishi Qiu memblock_free(start, size); 2054f9126ab9SXishi Qiu memblock_remove(start, size); 205546c66c4bSYasuaki Ishimatsu 205624d335caSWen Congyang arch_remove_memory(start, size); 205724d335caSWen Congyang 205860a5a19eSTang Chen try_offline_node(nid); 205960a5a19eSTang Chen 2060bfc8c901SVladimir Davydov mem_hotplug_done(); 206171088785SBadari Pulavarty } 206271088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 2063aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 2064