1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 23947be19SDave Hansen /* 33947be19SDave Hansen * linux/mm/memory_hotplug.c 43947be19SDave Hansen * 53947be19SDave Hansen * Copyright (C) 63947be19SDave Hansen */ 73947be19SDave Hansen 83947be19SDave Hansen #include <linux/stddef.h> 93947be19SDave Hansen #include <linux/mm.h> 10174cd4b1SIngo Molnar #include <linux/sched/signal.h> 113947be19SDave Hansen #include <linux/swap.h> 123947be19SDave Hansen #include <linux/interrupt.h> 133947be19SDave Hansen #include <linux/pagemap.h> 143947be19SDave Hansen #include <linux/compiler.h> 15b95f1b31SPaul Gortmaker #include <linux/export.h> 163947be19SDave Hansen #include <linux/pagevec.h> 172d1d43f6SChandra Seetharaman #include <linux/writeback.h> 183947be19SDave Hansen #include <linux/slab.h> 193947be19SDave Hansen #include <linux/sysctl.h> 203947be19SDave Hansen #include <linux/cpu.h> 213947be19SDave Hansen #include <linux/memory.h> 224b94ffdcSDan Williams #include <linux/memremap.h> 233947be19SDave Hansen #include <linux/memory_hotplug.h> 243947be19SDave Hansen #include <linux/highmem.h> 253947be19SDave Hansen #include <linux/vmalloc.h> 260a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 270c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 280c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 290c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 3071088785SBadari Pulavarty #include <linux/pfn.h> 316ad696d2SAndi Kleen #include <linux/suspend.h> 326d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 33d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h> 3460a5a19eSTang Chen #include <linux/stop_machine.h> 35c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h> 36c5320926STang Chen #include <linux/memblock.h> 37698b1b30SVlastimil Babka #include <linux/compaction.h> 38b15c8726SMichal Hocko #include <linux/rmap.h> 393947be19SDave Hansen 403947be19SDave Hansen #include <asm/tlbflush.h> 413947be19SDave Hansen 421e5ad9a3SAdrian Bunk #include "internal.h" 43e900a918SDan Williams #include "shuffle.h" 441e5ad9a3SAdrian Bunk 459d0ad8caSDaniel Kiper /* 469d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 479d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 489d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 499d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 509d0ad8caSDaniel Kiper */ 519d0ad8caSDaniel Kiper 529d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 53bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 549d0ad8caSDaniel Kiper 553f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 5620d6c96bSKOSAKI Motohiro 573f906ba2SThomas Gleixner void get_online_mems(void) 583f906ba2SThomas Gleixner { 593f906ba2SThomas Gleixner percpu_down_read(&mem_hotplug_lock); 603f906ba2SThomas Gleixner } 61bfc8c901SVladimir Davydov 623f906ba2SThomas Gleixner void put_online_mems(void) 633f906ba2SThomas Gleixner { 643f906ba2SThomas Gleixner percpu_up_read(&mem_hotplug_lock); 653f906ba2SThomas Gleixner } 66bfc8c901SVladimir Davydov 674932381eSMichal Hocko bool movable_node_enabled = false; 684932381eSMichal Hocko 698604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 701adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_OFFLINE; 718604d9e5SVitaly Kuznetsov #else 721adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_ONLINE; 738604d9e5SVitaly Kuznetsov #endif 7431bc3858SVitaly Kuznetsov 7586dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str) 7686dd995dSVitaly Kuznetsov { 771adf8b46SAnshuman Khandual const int online_type = mhp_online_type_from_str(str); 785f47adf7SDavid Hildenbrand 795f47adf7SDavid Hildenbrand if (online_type >= 0) 801adf8b46SAnshuman Khandual mhp_default_online_type = online_type; 8186dd995dSVitaly Kuznetsov 8286dd995dSVitaly Kuznetsov return 1; 8386dd995dSVitaly Kuznetsov } 8486dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state); 8586dd995dSVitaly Kuznetsov 8630467e0bSDavid Rientjes void mem_hotplug_begin(void) 87bfc8c901SVladimir Davydov { 883f906ba2SThomas Gleixner cpus_read_lock(); 893f906ba2SThomas Gleixner percpu_down_write(&mem_hotplug_lock); 90bfc8c901SVladimir Davydov } 91bfc8c901SVladimir Davydov 9230467e0bSDavid Rientjes void mem_hotplug_done(void) 93bfc8c901SVladimir Davydov { 943f906ba2SThomas Gleixner percpu_up_write(&mem_hotplug_lock); 953f906ba2SThomas Gleixner cpus_read_unlock(); 96bfc8c901SVladimir Davydov } 9720d6c96bSKOSAKI Motohiro 98357b4da5SJuergen Gross u64 max_mem_size = U64_MAX; 99357b4da5SJuergen Gross 10045e0b78bSKeith Mannthey /* add this memory to iomem resource */ 1017b7b2721SDavid Hildenbrand static struct resource *register_memory_resource(u64 start, u64 size, 1027b7b2721SDavid Hildenbrand const char *resource_name) 10345e0b78bSKeith Mannthey { 1042794129eSDave Hansen struct resource *res; 1052794129eSDave Hansen unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1067b7b2721SDavid Hildenbrand 1077b7b2721SDavid Hildenbrand if (strcmp(resource_name, "System RAM")) 1087cf603d1SDavid Hildenbrand flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED; 109357b4da5SJuergen Gross 110bca3feaaSAnshuman Khandual if (!mhp_range_allowed(start, size, true)) 111bca3feaaSAnshuman Khandual return ERR_PTR(-E2BIG); 112bca3feaaSAnshuman Khandual 113f3cd4c86SBaoquan He /* 114f3cd4c86SBaoquan He * Make sure value parsed from 'mem=' only restricts memory adding 115f3cd4c86SBaoquan He * while booting, so that memory hotplug won't be impacted. Please 116f3cd4c86SBaoquan He * refer to document of 'mem=' in kernel-parameters.txt for more 117f3cd4c86SBaoquan He * details. 118f3cd4c86SBaoquan He */ 119f3cd4c86SBaoquan He if (start + size > max_mem_size && system_state < SYSTEM_RUNNING) 120357b4da5SJuergen Gross return ERR_PTR(-E2BIG); 121357b4da5SJuergen Gross 1222794129eSDave Hansen /* 1232794129eSDave Hansen * Request ownership of the new memory range. This might be 1242794129eSDave Hansen * a child of an existing resource that was present but 1252794129eSDave Hansen * not marked as busy. 1262794129eSDave Hansen */ 1272794129eSDave Hansen res = __request_region(&iomem_resource, start, size, 1282794129eSDave Hansen resource_name, flags); 12945e0b78bSKeith Mannthey 1302794129eSDave Hansen if (!res) { 1312794129eSDave Hansen pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", 1322794129eSDave Hansen start, start + size); 1336f754ba4SVitaly Kuznetsov return ERR_PTR(-EEXIST); 13445e0b78bSKeith Mannthey } 13545e0b78bSKeith Mannthey return res; 13645e0b78bSKeith Mannthey } 13745e0b78bSKeith Mannthey 13845e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 13945e0b78bSKeith Mannthey { 14045e0b78bSKeith Mannthey if (!res) 14145e0b78bSKeith Mannthey return; 14245e0b78bSKeith Mannthey release_resource(res); 14345e0b78bSKeith Mannthey kfree(res); 14445e0b78bSKeith Mannthey } 14545e0b78bSKeith Mannthey 14653947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 14746723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info, struct page *page, 1485f24ce5fSAndrea Arcangeli unsigned long type) 14904753278SYasunori Goto { 150ddffe98dSYasuaki Ishimatsu page->freelist = (void *)type; 15104753278SYasunori Goto SetPagePrivate(page); 15204753278SYasunori Goto set_page_private(page, info); 153fe896d18SJoonsoo Kim page_ref_inc(page); 15404753278SYasunori Goto } 15504753278SYasunori Goto 156170a5a7eSJiang Liu void put_page_bootmem(struct page *page) 15704753278SYasunori Goto { 1585f24ce5fSAndrea Arcangeli unsigned long type; 15904753278SYasunori Goto 160ddffe98dSYasuaki Ishimatsu type = (unsigned long) page->freelist; 1615f24ce5fSAndrea Arcangeli BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 1625f24ce5fSAndrea Arcangeli type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 16304753278SYasunori Goto 164fe896d18SJoonsoo Kim if (page_ref_dec_return(page) == 1) { 165ddffe98dSYasuaki Ishimatsu page->freelist = NULL; 16604753278SYasunori Goto ClearPagePrivate(page); 16704753278SYasunori Goto set_page_private(page, 0); 1685f24ce5fSAndrea Arcangeli INIT_LIST_HEAD(&page->lru); 169170a5a7eSJiang Liu free_reserved_page(page); 17004753278SYasunori Goto } 17104753278SYasunori Goto } 17204753278SYasunori Goto 17346723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 17446723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP 175d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn) 17604753278SYasunori Goto { 177f1eca35aSDan Williams unsigned long mapsize, section_nr, i; 17804753278SYasunori Goto struct mem_section *ms; 17904753278SYasunori Goto struct page *page, *memmap; 180f1eca35aSDan Williams struct mem_section_usage *usage; 18104753278SYasunori Goto 18204753278SYasunori Goto section_nr = pfn_to_section_nr(start_pfn); 18304753278SYasunori Goto ms = __nr_to_section(section_nr); 18404753278SYasunori Goto 18504753278SYasunori Goto /* Get section's memmap address */ 18604753278SYasunori Goto memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 18704753278SYasunori Goto 18804753278SYasunori Goto /* 18904753278SYasunori Goto * Get page for the memmap's phys address 19004753278SYasunori Goto * XXX: need more consideration for sparse_vmemmap... 19104753278SYasunori Goto */ 19204753278SYasunori Goto page = virt_to_page(memmap); 19304753278SYasunori Goto mapsize = sizeof(struct page) * PAGES_PER_SECTION; 19404753278SYasunori Goto mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 19504753278SYasunori Goto 19604753278SYasunori Goto /* remember memmap's page */ 19704753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 19804753278SYasunori Goto get_page_bootmem(section_nr, page, SECTION_INFO); 19904753278SYasunori Goto 200f1eca35aSDan Williams usage = ms->usage; 201f1eca35aSDan Williams page = virt_to_page(usage); 20204753278SYasunori Goto 203f1eca35aSDan Williams mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; 20404753278SYasunori Goto 20504753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 206af370fb8SYasunori Goto get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 20704753278SYasunori Goto 20804753278SYasunori Goto } 20946723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */ 21046723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn) 21146723bfaSYasuaki Ishimatsu { 212f1eca35aSDan Williams unsigned long mapsize, section_nr, i; 21346723bfaSYasuaki Ishimatsu struct mem_section *ms; 21446723bfaSYasuaki Ishimatsu struct page *page, *memmap; 215f1eca35aSDan Williams struct mem_section_usage *usage; 21646723bfaSYasuaki Ishimatsu 21746723bfaSYasuaki Ishimatsu section_nr = pfn_to_section_nr(start_pfn); 21846723bfaSYasuaki Ishimatsu ms = __nr_to_section(section_nr); 21946723bfaSYasuaki Ishimatsu 22046723bfaSYasuaki Ishimatsu memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 22146723bfaSYasuaki Ishimatsu 22246723bfaSYasuaki Ishimatsu register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); 22346723bfaSYasuaki Ishimatsu 224f1eca35aSDan Williams usage = ms->usage; 225f1eca35aSDan Williams page = virt_to_page(usage); 22646723bfaSYasuaki Ishimatsu 227f1eca35aSDan Williams mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; 22846723bfaSYasuaki Ishimatsu 22946723bfaSYasuaki Ishimatsu for (i = 0; i < mapsize; i++, page++) 23046723bfaSYasuaki Ishimatsu get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 23146723bfaSYasuaki Ishimatsu } 23246723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 23304753278SYasunori Goto 2347ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat) 23504753278SYasunori Goto { 23604753278SYasunori Goto unsigned long i, pfn, end_pfn, nr_pages; 23704753278SYasunori Goto int node = pgdat->node_id; 23804753278SYasunori Goto struct page *page; 23904753278SYasunori Goto 24004753278SYasunori Goto nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 24104753278SYasunori Goto page = virt_to_page(pgdat); 24204753278SYasunori Goto 24304753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 24404753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 24504753278SYasunori Goto 24604753278SYasunori Goto pfn = pgdat->node_start_pfn; 247c1f19495SCody P Schafer end_pfn = pgdat_end_pfn(pgdat); 24804753278SYasunori Goto 2497e9f5eb0STang Chen /* register section info */ 250f14851afSqiuxishi for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 251f14851afSqiuxishi /* 252f14851afSqiuxishi * Some platforms can assign the same pfn to multiple nodes - on 253f14851afSqiuxishi * node0 as well as nodeN. To avoid registering a pfn against 254f14851afSqiuxishi * multiple nodes we check that this pfn does not already 2557e9f5eb0STang Chen * reside in some other nodes. 256f14851afSqiuxishi */ 257f65e91dfSYang Shi if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) 25804753278SYasunori Goto register_page_bootmem_info_section(pfn); 259f14851afSqiuxishi } 26004753278SYasunori Goto } 26146723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ 26204753278SYasunori Goto 2637ea62160SDan Williams static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, 2647ea62160SDan Williams const char *reason) 2657ea62160SDan Williams { 2667ea62160SDan Williams /* 2677ea62160SDan Williams * Disallow all operations smaller than a sub-section and only 2687ea62160SDan Williams * allow operations smaller than a section for 2697ea62160SDan Williams * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range() 2707ea62160SDan Williams * enforces a larger memory_block_size_bytes() granularity for 2717ea62160SDan Williams * memory that will be marked online, so this check should only 2727ea62160SDan Williams * fire for direct arch_{add,remove}_memory() users outside of 2737ea62160SDan Williams * add_memory_resource(). 2747ea62160SDan Williams */ 2757ea62160SDan Williams unsigned long min_align; 2767ea62160SDan Williams 2777ea62160SDan Williams if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 2787ea62160SDan Williams min_align = PAGES_PER_SUBSECTION; 2797ea62160SDan Williams else 2807ea62160SDan Williams min_align = PAGES_PER_SECTION; 2817ea62160SDan Williams if (!IS_ALIGNED(pfn, min_align) 2827ea62160SDan Williams || !IS_ALIGNED(nr_pages, min_align)) { 2837ea62160SDan Williams WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n", 2847ea62160SDan Williams reason, pfn, pfn + nr_pages - 1); 2857ea62160SDan Williams return -EINVAL; 2867ea62160SDan Williams } 2877ea62160SDan Williams return 0; 2887ea62160SDan Williams } 2897ea62160SDan Williams 2904edd7cefSDavid Rientjes /* 2919f605f26SDan Williams * Return page for the valid pfn only if the page is online. All pfn 2929f605f26SDan Williams * walkers which rely on the fully initialized page->flags and others 2939f605f26SDan Williams * should use this rather than pfn_valid && pfn_to_page 2949f605f26SDan Williams */ 2959f605f26SDan Williams struct page *pfn_to_online_page(unsigned long pfn) 2969f605f26SDan Williams { 2979f605f26SDan Williams unsigned long nr = pfn_to_section_nr(pfn); 2981f90a347SDan Williams struct dev_pagemap *pgmap; 2999f9b02e5SDan Williams struct mem_section *ms; 3009f605f26SDan Williams 3019f9b02e5SDan Williams if (nr >= NR_MEM_SECTIONS) 3029f605f26SDan Williams return NULL; 3039f9b02e5SDan Williams 3049f9b02e5SDan Williams ms = __nr_to_section(nr); 3059f9b02e5SDan Williams if (!online_section(ms)) 3069f9b02e5SDan Williams return NULL; 3079f9b02e5SDan Williams 3089f9b02e5SDan Williams /* 3099f9b02e5SDan Williams * Save some code text when online_section() + 3109f9b02e5SDan Williams * pfn_section_valid() are sufficient. 3119f9b02e5SDan Williams */ 3129f9b02e5SDan Williams if (IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) && !pfn_valid(pfn)) 3139f9b02e5SDan Williams return NULL; 3149f9b02e5SDan Williams 3159f9b02e5SDan Williams if (!pfn_section_valid(ms, pfn)) 3169f9b02e5SDan Williams return NULL; 3179f9b02e5SDan Williams 3181f90a347SDan Williams if (!online_device_section(ms)) 3191f90a347SDan Williams return pfn_to_page(pfn); 3201f90a347SDan Williams 3211f90a347SDan Williams /* 3221f90a347SDan Williams * Slowpath: when ZONE_DEVICE collides with 3231f90a347SDan Williams * ZONE_{NORMAL,MOVABLE} within the same section some pfns in 3241f90a347SDan Williams * the section may be 'offline' but 'valid'. Only 3251f90a347SDan Williams * get_dev_pagemap() can determine sub-section online status. 3261f90a347SDan Williams */ 3271f90a347SDan Williams pgmap = get_dev_pagemap(pfn, NULL); 3281f90a347SDan Williams put_dev_pagemap(pgmap); 3291f90a347SDan Williams 3301f90a347SDan Williams /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */ 3311f90a347SDan Williams if (pgmap) 3321f90a347SDan Williams return NULL; 3331f90a347SDan Williams 3349f9b02e5SDan Williams return pfn_to_page(pfn); 3359f605f26SDan Williams } 3369f605f26SDan Williams EXPORT_SYMBOL_GPL(pfn_to_online_page); 3379f605f26SDan Williams 3389f605f26SDan Williams /* 3394edd7cefSDavid Rientjes * Reasonably generic function for adding memory. It is 3404edd7cefSDavid Rientjes * expected that archs that support memory hotplug will 3414edd7cefSDavid Rientjes * call this function after deciding the zone to which to 3424edd7cefSDavid Rientjes * add the new pages. 3434edd7cefSDavid Rientjes */ 3447ea62160SDan Williams int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, 345f5637d3bSLogan Gunthorpe struct mhp_params *params) 3464edd7cefSDavid Rientjes { 3476cdd0b30SDavid Hildenbrand const unsigned long end_pfn = pfn + nr_pages; 3486cdd0b30SDavid Hildenbrand unsigned long cur_nr_pages; 3499a845030SDan Williams int err; 350f5637d3bSLogan Gunthorpe struct vmem_altmap *altmap = params->altmap; 3514b94ffdcSDan Williams 352bfeb022fSLogan Gunthorpe if (WARN_ON_ONCE(!params->pgprot.pgprot)) 353bfeb022fSLogan Gunthorpe return -EINVAL; 354bfeb022fSLogan Gunthorpe 355bca3feaaSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false)); 356dca4436dSAlastair D'Silva 3574b94ffdcSDan Williams if (altmap) { 3584b94ffdcSDan Williams /* 3594b94ffdcSDan Williams * Validate altmap is within bounds of the total request 3604b94ffdcSDan Williams */ 3617ea62160SDan Williams if (altmap->base_pfn != pfn 3624b94ffdcSDan Williams || vmem_altmap_offset(altmap) > nr_pages) { 3634b94ffdcSDan Williams pr_warn_once("memory add fail, invalid altmap\n"); 3647ea62160SDan Williams return -EINVAL; 3654b94ffdcSDan Williams } 3664b94ffdcSDan Williams altmap->alloc = 0; 3674b94ffdcSDan Williams } 3684b94ffdcSDan Williams 3697ea62160SDan Williams err = check_pfn_span(pfn, nr_pages, "add"); 3707ea62160SDan Williams if (err) 3717ea62160SDan Williams return err; 3727ea62160SDan Williams 3736cdd0b30SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 3746cdd0b30SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 3756cdd0b30SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 3766cdd0b30SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 3776cdd0b30SDavid Hildenbrand err = sparse_add_section(nid, pfn, cur_nr_pages, altmap); 378ba72b4c8SDan Williams if (err) 379ba72b4c8SDan Williams break; 380f64ac5e6SMichal Hocko cond_resched(); 3814edd7cefSDavid Rientjes } 382c435a390SZhu Guihua vmemmap_populate_print_last(); 3834edd7cefSDavid Rientjes return err; 3844edd7cefSDavid Rientjes } 3854edd7cefSDavid Rientjes 386815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 387d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, 388815121d2SYasuaki Ishimatsu unsigned long start_pfn, 389815121d2SYasuaki Ishimatsu unsigned long end_pfn) 390815121d2SYasuaki Ishimatsu { 39149ba3c6bSDan Williams for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) { 3927ce700bfSDavid Hildenbrand if (unlikely(!pfn_to_online_page(start_pfn))) 393815121d2SYasuaki Ishimatsu continue; 394815121d2SYasuaki Ishimatsu 395815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(start_pfn) != nid)) 396815121d2SYasuaki Ishimatsu continue; 397815121d2SYasuaki Ishimatsu 3989b05158fSDavid Hildenbrand if (zone != page_zone(pfn_to_page(start_pfn))) 399815121d2SYasuaki Ishimatsu continue; 400815121d2SYasuaki Ishimatsu 401815121d2SYasuaki Ishimatsu return start_pfn; 402815121d2SYasuaki Ishimatsu } 403815121d2SYasuaki Ishimatsu 404815121d2SYasuaki Ishimatsu return 0; 405815121d2SYasuaki Ishimatsu } 406815121d2SYasuaki Ishimatsu 407815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 408d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, 409815121d2SYasuaki Ishimatsu unsigned long start_pfn, 410815121d2SYasuaki Ishimatsu unsigned long end_pfn) 411815121d2SYasuaki Ishimatsu { 412815121d2SYasuaki Ishimatsu unsigned long pfn; 413815121d2SYasuaki Ishimatsu 414815121d2SYasuaki Ishimatsu /* pfn is the end pfn of a memory section. */ 415815121d2SYasuaki Ishimatsu pfn = end_pfn - 1; 41649ba3c6bSDan Williams for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) { 4177ce700bfSDavid Hildenbrand if (unlikely(!pfn_to_online_page(pfn))) 418815121d2SYasuaki Ishimatsu continue; 419815121d2SYasuaki Ishimatsu 420815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(pfn) != nid)) 421815121d2SYasuaki Ishimatsu continue; 422815121d2SYasuaki Ishimatsu 4239b05158fSDavid Hildenbrand if (zone != page_zone(pfn_to_page(pfn))) 424815121d2SYasuaki Ishimatsu continue; 425815121d2SYasuaki Ishimatsu 426815121d2SYasuaki Ishimatsu return pfn; 427815121d2SYasuaki Ishimatsu } 428815121d2SYasuaki Ishimatsu 429815121d2SYasuaki Ishimatsu return 0; 430815121d2SYasuaki Ishimatsu } 431815121d2SYasuaki Ishimatsu 432815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 433815121d2SYasuaki Ishimatsu unsigned long end_pfn) 434815121d2SYasuaki Ishimatsu { 435815121d2SYasuaki Ishimatsu unsigned long pfn; 436815121d2SYasuaki Ishimatsu int nid = zone_to_nid(zone); 437815121d2SYasuaki Ishimatsu 438815121d2SYasuaki Ishimatsu zone_span_writelock(zone); 4395d12071cSDavid Hildenbrand if (zone->zone_start_pfn == start_pfn) { 440815121d2SYasuaki Ishimatsu /* 441815121d2SYasuaki Ishimatsu * If the section is smallest section in the zone, it need 442815121d2SYasuaki Ishimatsu * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 443815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 444815121d2SYasuaki Ishimatsu * for shrinking zone. 445815121d2SYasuaki Ishimatsu */ 446815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, zone, end_pfn, 4475d12071cSDavid Hildenbrand zone_end_pfn(zone)); 448815121d2SYasuaki Ishimatsu if (pfn) { 4495d12071cSDavid Hildenbrand zone->spanned_pages = zone_end_pfn(zone) - pfn; 450815121d2SYasuaki Ishimatsu zone->zone_start_pfn = pfn; 451950b68d9SDavid Hildenbrand } else { 452950b68d9SDavid Hildenbrand zone->zone_start_pfn = 0; 453950b68d9SDavid Hildenbrand zone->spanned_pages = 0; 454815121d2SYasuaki Ishimatsu } 4555d12071cSDavid Hildenbrand } else if (zone_end_pfn(zone) == end_pfn) { 456815121d2SYasuaki Ishimatsu /* 457815121d2SYasuaki Ishimatsu * If the section is biggest section in the zone, it need 458815121d2SYasuaki Ishimatsu * shrink zone->spanned_pages. 459815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 460815121d2SYasuaki Ishimatsu * shrinking zone. 461815121d2SYasuaki Ishimatsu */ 4625d12071cSDavid Hildenbrand pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn, 463815121d2SYasuaki Ishimatsu start_pfn); 464815121d2SYasuaki Ishimatsu if (pfn) 4655d12071cSDavid Hildenbrand zone->spanned_pages = pfn - zone->zone_start_pfn + 1; 466950b68d9SDavid Hildenbrand else { 467815121d2SYasuaki Ishimatsu zone->zone_start_pfn = 0; 468815121d2SYasuaki Ishimatsu zone->spanned_pages = 0; 469950b68d9SDavid Hildenbrand } 470950b68d9SDavid Hildenbrand } 471815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 472815121d2SYasuaki Ishimatsu } 473815121d2SYasuaki Ishimatsu 47400d6c019SDavid Hildenbrand static void update_pgdat_span(struct pglist_data *pgdat) 475815121d2SYasuaki Ishimatsu { 47600d6c019SDavid Hildenbrand unsigned long node_start_pfn = 0, node_end_pfn = 0; 47700d6c019SDavid Hildenbrand struct zone *zone; 478815121d2SYasuaki Ishimatsu 47900d6c019SDavid Hildenbrand for (zone = pgdat->node_zones; 48000d6c019SDavid Hildenbrand zone < pgdat->node_zones + MAX_NR_ZONES; zone++) { 4816c922cf7SMiaohe Lin unsigned long end_pfn = zone_end_pfn(zone); 48200d6c019SDavid Hildenbrand 48300d6c019SDavid Hildenbrand /* No need to lock the zones, they can't change. */ 484656d5711SDavid Hildenbrand if (!zone->spanned_pages) 485656d5711SDavid Hildenbrand continue; 486656d5711SDavid Hildenbrand if (!node_end_pfn) { 487656d5711SDavid Hildenbrand node_start_pfn = zone->zone_start_pfn; 4886c922cf7SMiaohe Lin node_end_pfn = end_pfn; 489656d5711SDavid Hildenbrand continue; 490656d5711SDavid Hildenbrand } 491656d5711SDavid Hildenbrand 4926c922cf7SMiaohe Lin if (end_pfn > node_end_pfn) 4936c922cf7SMiaohe Lin node_end_pfn = end_pfn; 49400d6c019SDavid Hildenbrand if (zone->zone_start_pfn < node_start_pfn) 49500d6c019SDavid Hildenbrand node_start_pfn = zone->zone_start_pfn; 496815121d2SYasuaki Ishimatsu } 497815121d2SYasuaki Ishimatsu 49800d6c019SDavid Hildenbrand pgdat->node_start_pfn = node_start_pfn; 49900d6c019SDavid Hildenbrand pgdat->node_spanned_pages = node_end_pfn - node_start_pfn; 500815121d2SYasuaki Ishimatsu } 501815121d2SYasuaki Ishimatsu 502feee6b29SDavid Hildenbrand void __ref remove_pfn_range_from_zone(struct zone *zone, 503feee6b29SDavid Hildenbrand unsigned long start_pfn, 5047ea62160SDan Williams unsigned long nr_pages) 505815121d2SYasuaki Ishimatsu { 506b7e3debdSBen Widawsky const unsigned long end_pfn = start_pfn + nr_pages; 507815121d2SYasuaki Ishimatsu struct pglist_data *pgdat = zone->zone_pgdat; 508b7e3debdSBen Widawsky unsigned long pfn, cur_nr_pages, flags; 509815121d2SYasuaki Ishimatsu 510d33695b1SDavid Hildenbrand /* Poison struct pages because they are now uninitialized again. */ 511b7e3debdSBen Widawsky for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) { 512b7e3debdSBen Widawsky cond_resched(); 513b7e3debdSBen Widawsky 514b7e3debdSBen Widawsky /* Select all remaining pages up to the next section boundary */ 515b7e3debdSBen Widawsky cur_nr_pages = 516b7e3debdSBen Widawsky min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn); 517b7e3debdSBen Widawsky page_init_poison(pfn_to_page(pfn), 518b7e3debdSBen Widawsky sizeof(struct page) * cur_nr_pages); 519b7e3debdSBen Widawsky } 520d33695b1SDavid Hildenbrand 5217ce700bfSDavid Hildenbrand #ifdef CONFIG_ZONE_DEVICE 5227ce700bfSDavid Hildenbrand /* 5237ce700bfSDavid Hildenbrand * Zone shrinking code cannot properly deal with ZONE_DEVICE. So 5247ce700bfSDavid Hildenbrand * we will not try to shrink the zones - which is okay as 5257ce700bfSDavid Hildenbrand * set_zone_contiguous() cannot deal with ZONE_DEVICE either way. 5267ce700bfSDavid Hildenbrand */ 5277ce700bfSDavid Hildenbrand if (zone_idx(zone) == ZONE_DEVICE) 5287ce700bfSDavid Hildenbrand return; 5297ce700bfSDavid Hildenbrand #endif 5307ce700bfSDavid Hildenbrand 531feee6b29SDavid Hildenbrand clear_zone_contiguous(zone); 532feee6b29SDavid Hildenbrand 533815121d2SYasuaki Ishimatsu pgdat_resize_lock(zone->zone_pgdat, &flags); 534815121d2SYasuaki Ishimatsu shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 53500d6c019SDavid Hildenbrand update_pgdat_span(pgdat); 536815121d2SYasuaki Ishimatsu pgdat_resize_unlock(zone->zone_pgdat, &flags); 537feee6b29SDavid Hildenbrand 538feee6b29SDavid Hildenbrand set_zone_contiguous(zone); 539815121d2SYasuaki Ishimatsu } 540815121d2SYasuaki Ishimatsu 541feee6b29SDavid Hildenbrand static void __remove_section(unsigned long pfn, unsigned long nr_pages, 542feee6b29SDavid Hildenbrand unsigned long map_offset, 5439d1d887dSDavid Hildenbrand struct vmem_altmap *altmap) 544ea01ea93SBadari Pulavarty { 54510404901Schenqiwu struct mem_section *ms = __pfn_to_section(pfn); 546ea01ea93SBadari Pulavarty 5479d1d887dSDavid Hildenbrand if (WARN_ON_ONCE(!valid_section(ms))) 5489d1d887dSDavid Hildenbrand return; 549ea01ea93SBadari Pulavarty 550ba72b4c8SDan Williams sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap); 551ea01ea93SBadari Pulavarty } 552ea01ea93SBadari Pulavarty 553ea01ea93SBadari Pulavarty /** 554feee6b29SDavid Hildenbrand * __remove_pages() - remove sections of pages 5557ea62160SDan Williams * @pfn: starting pageframe (must be aligned to start of a section) 556ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 557e8b098fcSMike Rapoport * @altmap: alternative device page map or %NULL if default memmap is used 558ea01ea93SBadari Pulavarty * 559ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 560ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 561ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 562ea01ea93SBadari Pulavarty * calling offline_pages(). 563ea01ea93SBadari Pulavarty */ 564feee6b29SDavid Hildenbrand void __remove_pages(unsigned long pfn, unsigned long nr_pages, 565feee6b29SDavid Hildenbrand struct vmem_altmap *altmap) 566ea01ea93SBadari Pulavarty { 56752fb87c8SDavid Hildenbrand const unsigned long end_pfn = pfn + nr_pages; 56852fb87c8SDavid Hildenbrand unsigned long cur_nr_pages; 5694b94ffdcSDan Williams unsigned long map_offset = 0; 5704b94ffdcSDan Williams 5714b94ffdcSDan Williams map_offset = vmem_altmap_offset(altmap); 572ea01ea93SBadari Pulavarty 5737ea62160SDan Williams if (check_pfn_span(pfn, nr_pages, "remove")) 5747ea62160SDan Williams return; 575ea01ea93SBadari Pulavarty 57652fb87c8SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 577dd33ad7bSMichal Hocko cond_resched(); 57852fb87c8SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 579a11b9419SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 580a11b9419SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 58152fb87c8SDavid Hildenbrand __remove_section(pfn, cur_nr_pages, map_offset, altmap); 5824b94ffdcSDan Williams map_offset = 0; 583ea01ea93SBadari Pulavarty } 584ea01ea93SBadari Pulavarty } 585ea01ea93SBadari Pulavarty 5869d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 5879d0ad8caSDaniel Kiper { 5889d0ad8caSDaniel Kiper int rc = -EINVAL; 5899d0ad8caSDaniel Kiper 590bfc8c901SVladimir Davydov get_online_mems(); 591bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5929d0ad8caSDaniel Kiper 5939d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 5949d0ad8caSDaniel Kiper online_page_callback = callback; 5959d0ad8caSDaniel Kiper rc = 0; 5969d0ad8caSDaniel Kiper } 5979d0ad8caSDaniel Kiper 598bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 599bfc8c901SVladimir Davydov put_online_mems(); 6009d0ad8caSDaniel Kiper 6019d0ad8caSDaniel Kiper return rc; 6029d0ad8caSDaniel Kiper } 6039d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 6049d0ad8caSDaniel Kiper 6059d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 6069d0ad8caSDaniel Kiper { 6079d0ad8caSDaniel Kiper int rc = -EINVAL; 6089d0ad8caSDaniel Kiper 609bfc8c901SVladimir Davydov get_online_mems(); 610bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 6119d0ad8caSDaniel Kiper 6129d0ad8caSDaniel Kiper if (online_page_callback == callback) { 6139d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 6149d0ad8caSDaniel Kiper rc = 0; 6159d0ad8caSDaniel Kiper } 6169d0ad8caSDaniel Kiper 617bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 618bfc8c901SVladimir Davydov put_online_mems(); 6199d0ad8caSDaniel Kiper 6209d0ad8caSDaniel Kiper return rc; 6219d0ad8caSDaniel Kiper } 6229d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 6239d0ad8caSDaniel Kiper 62418db1491SDavid Hildenbrand void generic_online_page(struct page *page, unsigned int order) 6259d0ad8caSDaniel Kiper { 626c87cbc1fSVlastimil Babka /* 627c87cbc1fSVlastimil Babka * Freeing the page with debug_pagealloc enabled will try to unmap it, 628c87cbc1fSVlastimil Babka * so we should map it first. This is better than introducing a special 629c87cbc1fSVlastimil Babka * case in page freeing fast path. 630c87cbc1fSVlastimil Babka */ 63177bc7fd6SMike Rapoport debug_pagealloc_map_pages(page, 1 << order); 632a9cd410aSArun KS __free_pages_core(page, order); 633a9cd410aSArun KS totalram_pages_add(1UL << order); 634a9cd410aSArun KS #ifdef CONFIG_HIGHMEM 635a9cd410aSArun KS if (PageHighMem(page)) 636a9cd410aSArun KS totalhigh_pages_add(1UL << order); 637a9cd410aSArun KS #endif 638a9cd410aSArun KS } 63918db1491SDavid Hildenbrand EXPORT_SYMBOL_GPL(generic_online_page); 640a9cd410aSArun KS 641aac65321SDavid Hildenbrand static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages) 6423947be19SDave Hansen { 643b2c2ab20SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 644b2c2ab20SDavid Hildenbrand unsigned long pfn; 6452d070eabSMichal Hocko 646b2c2ab20SDavid Hildenbrand /* 647aac65321SDavid Hildenbrand * Online the pages in MAX_ORDER - 1 aligned chunks. The callback might 648aac65321SDavid Hildenbrand * decide to not expose all pages to the buddy (e.g., expose them 649aac65321SDavid Hildenbrand * later). We account all pages as being online and belonging to this 650aac65321SDavid Hildenbrand * zone ("present"). 651b2c2ab20SDavid Hildenbrand */ 652aac65321SDavid Hildenbrand for (pfn = start_pfn; pfn < end_pfn; pfn += MAX_ORDER_NR_PAGES) 653aac65321SDavid Hildenbrand (*online_page_callback)(pfn_to_page(pfn), MAX_ORDER - 1); 6542d070eabSMichal Hocko 655b2c2ab20SDavid Hildenbrand /* mark all involved sections as online */ 656b2c2ab20SDavid Hildenbrand online_mem_sections(start_pfn, end_pfn); 65775884fb1SKAMEZAWA Hiroyuki } 65875884fb1SKAMEZAWA Hiroyuki 659d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 660d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 661d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 662d9713679SLai Jiangshan { 663d9713679SLai Jiangshan int nid = zone_to_nid(zone); 664d9713679SLai Jiangshan 66598fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 66698fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 66798fa15f3SAnshuman Khandual arg->status_change_nid_high = NUMA_NO_NODE; 6686715ddf9SLai Jiangshan 6696715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 670d9713679SLai Jiangshan arg->status_change_nid = nid; 6718efe33f4SOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 6728efe33f4SOscar Salvador arg->status_change_nid_normal = nid; 6738efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM 674d3ba3ae1SBaoquan He if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY)) 6758efe33f4SOscar Salvador arg->status_change_nid_high = nid; 6768efe33f4SOscar Salvador #endif 677d9713679SLai Jiangshan } 678d9713679SLai Jiangshan 679d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 680d9713679SLai Jiangshan { 681d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 682d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 683d9713679SLai Jiangshan 6846715ddf9SLai Jiangshan if (arg->status_change_nid_high >= 0) 685d9713679SLai Jiangshan node_set_state(node, N_HIGH_MEMORY); 6866715ddf9SLai Jiangshan 68783d83612SOscar Salvador if (arg->status_change_nid >= 0) 6886715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 689d9713679SLai Jiangshan } 690d9713679SLai Jiangshan 691f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 692f1dd2cd1SMichal Hocko unsigned long nr_pages) 693f1dd2cd1SMichal Hocko { 694f1dd2cd1SMichal Hocko unsigned long old_end_pfn = zone_end_pfn(zone); 695f1dd2cd1SMichal Hocko 696f1dd2cd1SMichal Hocko if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 697f1dd2cd1SMichal Hocko zone->zone_start_pfn = start_pfn; 698f1dd2cd1SMichal Hocko 699f1dd2cd1SMichal Hocko zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 700f1dd2cd1SMichal Hocko } 701f1dd2cd1SMichal Hocko 702f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 703f1dd2cd1SMichal Hocko unsigned long nr_pages) 704f1dd2cd1SMichal Hocko { 705f1dd2cd1SMichal Hocko unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 706f1dd2cd1SMichal Hocko 707f1dd2cd1SMichal Hocko if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 708f1dd2cd1SMichal Hocko pgdat->node_start_pfn = start_pfn; 709f1dd2cd1SMichal Hocko 710f1dd2cd1SMichal Hocko pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 711f1dd2cd1SMichal Hocko 7123fccb74cSDavid Hildenbrand } 7131f90a347SDan Williams 7141f90a347SDan Williams static void section_taint_zone_device(unsigned long pfn) 7151f90a347SDan Williams { 7161f90a347SDan Williams struct mem_section *ms = __pfn_to_section(pfn); 7171f90a347SDan Williams 7181f90a347SDan Williams ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE; 7191f90a347SDan Williams } 7201f90a347SDan Williams 7213fccb74cSDavid Hildenbrand /* 7223fccb74cSDavid Hildenbrand * Associate the pfn range with the given zone, initializing the memmaps 7233fccb74cSDavid Hildenbrand * and resizing the pgdat/zone data to span the added pages. After this 7243fccb74cSDavid Hildenbrand * call, all affected pages are PG_reserved. 725d882c006SDavid Hildenbrand * 726d882c006SDavid Hildenbrand * All aligned pageblocks are initialized to the specified migratetype 727d882c006SDavid Hildenbrand * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related 728d882c006SDavid Hildenbrand * zone stats (e.g., nr_isolate_pageblock) are touched. 7293fccb74cSDavid Hildenbrand */ 730a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 731d882c006SDavid Hildenbrand unsigned long nr_pages, 732d882c006SDavid Hildenbrand struct vmem_altmap *altmap, int migratetype) 733f1dd2cd1SMichal Hocko { 734f1dd2cd1SMichal Hocko struct pglist_data *pgdat = zone->zone_pgdat; 735f1dd2cd1SMichal Hocko int nid = pgdat->node_id; 736f1dd2cd1SMichal Hocko unsigned long flags; 737f1dd2cd1SMichal Hocko 738f1dd2cd1SMichal Hocko clear_zone_contiguous(zone); 739f1dd2cd1SMichal Hocko 740f1dd2cd1SMichal Hocko /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ 741f1dd2cd1SMichal Hocko pgdat_resize_lock(pgdat, &flags); 742f1dd2cd1SMichal Hocko zone_span_writelock(zone); 743fa004ab7SWei Yang if (zone_is_empty(zone)) 744fa004ab7SWei Yang init_currently_empty_zone(zone, start_pfn, nr_pages); 745f1dd2cd1SMichal Hocko resize_zone_range(zone, start_pfn, nr_pages); 746f1dd2cd1SMichal Hocko zone_span_writeunlock(zone); 747f1dd2cd1SMichal Hocko resize_pgdat_range(pgdat, start_pfn, nr_pages); 748f1dd2cd1SMichal Hocko pgdat_resize_unlock(pgdat, &flags); 749f1dd2cd1SMichal Hocko 750f1dd2cd1SMichal Hocko /* 7511f90a347SDan Williams * Subsection population requires care in pfn_to_online_page(). 7521f90a347SDan Williams * Set the taint to enable the slow path detection of 7531f90a347SDan Williams * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE} 7541f90a347SDan Williams * section. 7551f90a347SDan Williams */ 7561f90a347SDan Williams if (zone_is_zone_device(zone)) { 7571f90a347SDan Williams if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION)) 7581f90a347SDan Williams section_taint_zone_device(start_pfn); 7591f90a347SDan Williams if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)) 7601f90a347SDan Williams section_taint_zone_device(start_pfn + nr_pages); 7611f90a347SDan Williams } 7621f90a347SDan Williams 7631f90a347SDan Williams /* 764f1dd2cd1SMichal Hocko * TODO now we have a visible range of pages which are not associated 765f1dd2cd1SMichal Hocko * with their zone properly. Not nice but set_pfnblock_flags_mask 766f1dd2cd1SMichal Hocko * expects the zone spans the pfn range. All the pages in the range 767f1dd2cd1SMichal Hocko * are reserved so nobody should be touching them so we should be safe 768f1dd2cd1SMichal Hocko */ 769ab28cb6eSBaoquan He memmap_init_range(nr_pages, nid, zone_idx(zone), start_pfn, 0, 770d882c006SDavid Hildenbrand MEMINIT_HOTPLUG, altmap, migratetype); 771f1dd2cd1SMichal Hocko 772f1dd2cd1SMichal Hocko set_zone_contiguous(zone); 773f1dd2cd1SMichal Hocko } 774f1dd2cd1SMichal Hocko 775f1dd2cd1SMichal Hocko /* 776c246a213SMichal Hocko * Returns a default kernel memory zone for the given pfn range. 777c246a213SMichal Hocko * If no kernel zone covers this pfn range it will automatically go 778c246a213SMichal Hocko * to the ZONE_NORMAL. 779c246a213SMichal Hocko */ 780c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 781c246a213SMichal Hocko unsigned long nr_pages) 782c246a213SMichal Hocko { 783c246a213SMichal Hocko struct pglist_data *pgdat = NODE_DATA(nid); 784c246a213SMichal Hocko int zid; 785c246a213SMichal Hocko 786c246a213SMichal Hocko for (zid = 0; zid <= ZONE_NORMAL; zid++) { 787c246a213SMichal Hocko struct zone *zone = &pgdat->node_zones[zid]; 788c246a213SMichal Hocko 789c246a213SMichal Hocko if (zone_intersects(zone, start_pfn, nr_pages)) 790c246a213SMichal Hocko return zone; 791c246a213SMichal Hocko } 792c246a213SMichal Hocko 793c246a213SMichal Hocko return &pgdat->node_zones[ZONE_NORMAL]; 794c246a213SMichal Hocko } 795c246a213SMichal Hocko 796c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 797c6f03e29SMichal Hocko unsigned long nr_pages) 798e5e68930SMichal Hocko { 799c6f03e29SMichal Hocko struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 800c6f03e29SMichal Hocko nr_pages); 801c6f03e29SMichal Hocko struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 802c6f03e29SMichal Hocko bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 803c6f03e29SMichal Hocko bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 804e5e68930SMichal Hocko 805e5e68930SMichal Hocko /* 806c6f03e29SMichal Hocko * We inherit the existing zone in a simple case where zones do not 807c6f03e29SMichal Hocko * overlap in the given range 808e5e68930SMichal Hocko */ 809c6f03e29SMichal Hocko if (in_kernel ^ in_movable) 810c6f03e29SMichal Hocko return (in_kernel) ? kernel_zone : movable_zone; 811e5e68930SMichal Hocko 812c6f03e29SMichal Hocko /* 813c6f03e29SMichal Hocko * If the range doesn't belong to any zone or two zones overlap in the 814c6f03e29SMichal Hocko * given range then we use movable zone only if movable_node is 815c6f03e29SMichal Hocko * enabled because we always online to a kernel zone by default. 816c6f03e29SMichal Hocko */ 817c6f03e29SMichal Hocko return movable_node_enabled ? movable_zone : kernel_zone; 8189f123ab5SMichal Hocko } 8199f123ab5SMichal Hocko 820e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn, 821e5e68930SMichal Hocko unsigned long nr_pages) 822f1dd2cd1SMichal Hocko { 823c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_KERNEL) 824c6f03e29SMichal Hocko return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 825f1dd2cd1SMichal Hocko 826c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_MOVABLE) 827c6f03e29SMichal Hocko return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 828f1dd2cd1SMichal Hocko 829c6f03e29SMichal Hocko return default_zone_for_pfn(nid, start_pfn, nr_pages); 830e5e68930SMichal Hocko } 831e5e68930SMichal Hocko 832bd5c2344SDavid Hildenbrand int __ref online_pages(unsigned long pfn, unsigned long nr_pages, 833bd5c2344SDavid Hildenbrand int online_type, int nid) 83475884fb1SKAMEZAWA Hiroyuki { 835aa47228aSCody P Schafer unsigned long flags; 8363947be19SDave Hansen struct zone *zone; 8376811378eSYasunori Goto int need_zonelists_rebuild = 0; 8387b78d335SYasunori Goto int ret; 8397b78d335SYasunori Goto struct memory_notify arg; 8403947be19SDave Hansen 8414986fac1SDavid Hildenbrand /* We can only online full sections (e.g., SECTION_IS_ONLINE) */ 8424986fac1SDavid Hildenbrand if (WARN_ON_ONCE(!nr_pages || 8434986fac1SDavid Hildenbrand !IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))) 8444986fac1SDavid Hildenbrand return -EINVAL; 8454986fac1SDavid Hildenbrand 846381eab4aSDavid Hildenbrand mem_hotplug_begin(); 847381eab4aSDavid Hildenbrand 848f1dd2cd1SMichal Hocko /* associate pfn range with the zone */ 8493fccb74cSDavid Hildenbrand zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages); 850b30c5927SDavid Hildenbrand move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE); 851511c2abaSLai Jiangshan 8527b78d335SYasunori Goto arg.start_pfn = pfn; 8537b78d335SYasunori Goto arg.nr_pages = nr_pages; 854d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 8557b78d335SYasunori Goto 8567b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 8577b78d335SYasunori Goto ret = notifier_to_errno(ret); 858e33e33b4SChen Yucong if (ret) 859e33e33b4SChen Yucong goto failed_addition; 860e33e33b4SChen Yucong 8613947be19SDave Hansen /* 862b30c5927SDavid Hildenbrand * Fixup the number of isolated pageblocks before marking the sections 863b30c5927SDavid Hildenbrand * onlining, such that undo_isolate_page_range() works correctly. 864b30c5927SDavid Hildenbrand */ 865b30c5927SDavid Hildenbrand spin_lock_irqsave(&zone->lock, flags); 866b30c5927SDavid Hildenbrand zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages; 867b30c5927SDavid Hildenbrand spin_unlock_irqrestore(&zone->lock, flags); 868b30c5927SDavid Hildenbrand 869b30c5927SDavid Hildenbrand /* 8706811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 8716811378eSYasunori Goto * This means the page allocator ignores this zone. 8726811378eSYasunori Goto * So, zonelist must be updated after online. 8736811378eSYasunori Goto */ 8746dcd73d7SWen Congyang if (!populated_zone(zone)) { 8756811378eSYasunori Goto need_zonelists_rebuild = 1; 87672675e13SMichal Hocko setup_zone_pageset(zone); 8776dcd73d7SWen Congyang } 8786811378eSYasunori Goto 879aac65321SDavid Hildenbrand online_pages_range(pfn, nr_pages); 880aac65321SDavid Hildenbrand zone->present_pages += nr_pages; 881aa47228aSCody P Schafer 882aa47228aSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 883aac65321SDavid Hildenbrand zone->zone_pgdat->node_present_pages += nr_pages; 884aa47228aSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 885aa47228aSCody P Schafer 886b30c5927SDavid Hildenbrand node_states_set_node(nid, &arg); 887b30c5927SDavid Hildenbrand if (need_zonelists_rebuild) 888b30c5927SDavid Hildenbrand build_all_zonelists(NULL); 889b30c5927SDavid Hildenbrand zone_pcp_update(zone); 890b30c5927SDavid Hildenbrand 891b30c5927SDavid Hildenbrand /* Basic onlining is complete, allow allocation of onlined pages. */ 892b30c5927SDavid Hildenbrand undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE); 893b30c5927SDavid Hildenbrand 89493146d98SDavid Hildenbrand /* 895b86c5fc4SDavid Hildenbrand * Freshly onlined pages aren't shuffled (e.g., all pages are placed to 896b86c5fc4SDavid Hildenbrand * the tail of the freelist when undoing isolation). Shuffle the whole 897b86c5fc4SDavid Hildenbrand * zone to make sure the just onlined pages are properly distributed 898b86c5fc4SDavid Hildenbrand * across the whole freelist - to create an initial shuffle. 89993146d98SDavid Hildenbrand */ 900e900a918SDan Williams shuffle_zone(zone); 901e900a918SDan Williams 9021b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 9031b79acc9SKOSAKI Motohiro 904e888ca35SVlastimil Babka kswapd_run(nid); 905698b1b30SVlastimil Babka kcompactd_run(nid); 90661b13993SDave Hansen 9072d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 9087b78d335SYasunori Goto 9097b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 910381eab4aSDavid Hildenbrand mem_hotplug_done(); 91130467e0bSDavid Rientjes return 0; 912e33e33b4SChen Yucong 913e33e33b4SChen Yucong failed_addition: 914e33e33b4SChen Yucong pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 915e33e33b4SChen Yucong (unsigned long long) pfn << PAGE_SHIFT, 916e33e33b4SChen Yucong (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 917e33e33b4SChen Yucong memory_notify(MEM_CANCEL_ONLINE, &arg); 918feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, pfn, nr_pages); 919381eab4aSDavid Hildenbrand mem_hotplug_done(); 920e33e33b4SChen Yucong return ret; 9213947be19SDave Hansen } 92253947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 923bc02af93SYasunori Goto 9240bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 9250bd85420STang Chen { 9260bd85420STang Chen struct zone *z; 9270bd85420STang Chen 9280bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 9290bd85420STang Chen z->present_pages = 0; 9300bd85420STang Chen 9310bd85420STang Chen pgdat->node_present_pages = 0; 9320bd85420STang Chen } 9330bd85420STang Chen 934e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 935c68ab18cSDavid Hildenbrand static pg_data_t __ref *hotadd_new_pgdat(int nid) 9369af3c2deSYasunori Goto { 9379af3c2deSYasunori Goto struct pglist_data *pgdat; 9389af3c2deSYasunori Goto 939a1e565aaSTang Chen pgdat = NODE_DATA(nid); 940a1e565aaSTang Chen if (!pgdat) { 9419af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 9429af3c2deSYasunori Goto if (!pgdat) 9439af3c2deSYasunori Goto return NULL; 9449af3c2deSYasunori Goto 94533fce011SWei Yang pgdat->per_cpu_nodestats = 94633fce011SWei Yang alloc_percpu(struct per_cpu_nodestat); 9479af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 948b0dc3a34SGu Zheng } else { 94933fce011SWei Yang int cpu; 950e716f2ebSMel Gorman /* 95197a225e6SJoonsoo Kim * Reset the nr_zones, order and highest_zoneidx before reuse. 95297a225e6SJoonsoo Kim * Note that kswapd will init kswapd_highest_zoneidx properly 953e716f2ebSMel Gorman * when it starts in the near future. 954e716f2ebSMel Gorman */ 955b0dc3a34SGu Zheng pgdat->nr_zones = 0; 95638087d9bSMel Gorman pgdat->kswapd_order = 0; 95797a225e6SJoonsoo Kim pgdat->kswapd_highest_zoneidx = 0; 95833fce011SWei Yang for_each_online_cpu(cpu) { 95933fce011SWei Yang struct per_cpu_nodestat *p; 96033fce011SWei Yang 96133fce011SWei Yang p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); 96233fce011SWei Yang memset(p, 0, sizeof(*p)); 96333fce011SWei Yang } 964a1e565aaSTang Chen } 9659af3c2deSYasunori Goto 9669af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 96703e85f9dSOscar Salvador pgdat->node_id = nid; 968c68ab18cSDavid Hildenbrand pgdat->node_start_pfn = 0; 96903e85f9dSOscar Salvador 9709af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 97103e85f9dSOscar Salvador free_area_init_core_hotplug(nid); 9729af3c2deSYasunori Goto 973959ecc48SKAMEZAWA Hiroyuki /* 974959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 975959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 976959ecc48SKAMEZAWA Hiroyuki */ 97772675e13SMichal Hocko build_all_zonelists(pgdat); 978959ecc48SKAMEZAWA Hiroyuki 979f784a3f1STang Chen /* 9800bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 9810bd85420STang Chen * clear all zones' present_pages because they will be updated in 9820bd85420STang Chen * online_pages() and offline_pages(). 9830bd85420STang Chen */ 98403e85f9dSOscar Salvador reset_node_managed_pages(pgdat); 9850bd85420STang Chen reset_node_present_pages(pgdat); 9860bd85420STang Chen 9879af3c2deSYasunori Goto return pgdat; 9889af3c2deSYasunori Goto } 9899af3c2deSYasunori Goto 990b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid) 9919af3c2deSYasunori Goto { 992b9ff0360SOscar Salvador pg_data_t *pgdat = NODE_DATA(nid); 993b9ff0360SOscar Salvador 9949af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 9955830169fSReza Arbab free_percpu(pgdat->per_cpu_nodestats); 9969af3c2deSYasunori Goto arch_free_nodedata(pgdat); 9979af3c2deSYasunori Goto } 9989af3c2deSYasunori Goto 9990a547039SKAMEZAWA Hiroyuki 100001b0f197SToshi Kani /** 100101b0f197SToshi Kani * try_online_node - online a node if offlined 1002e8b098fcSMike Rapoport * @nid: the node ID 1003b9ff0360SOscar Salvador * @set_node_online: Whether we want to online the node 1004cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1005b9ff0360SOscar Salvador * 1006b9ff0360SOscar Salvador * Returns: 1007b9ff0360SOscar Salvador * 1 -> a new node has been allocated 1008b9ff0360SOscar Salvador * 0 -> the node is already online 1009b9ff0360SOscar Salvador * -ENOMEM -> the node could not be allocated 1010cf23422bSminskey guo */ 1011c68ab18cSDavid Hildenbrand static int __try_online_node(int nid, bool set_node_online) 1012cf23422bSminskey guo { 1013cf23422bSminskey guo pg_data_t *pgdat; 1014b9ff0360SOscar Salvador int ret = 1; 1015cf23422bSminskey guo 101601b0f197SToshi Kani if (node_online(nid)) 101701b0f197SToshi Kani return 0; 101801b0f197SToshi Kani 1019c68ab18cSDavid Hildenbrand pgdat = hotadd_new_pgdat(nid); 10207553e8f2SDavid Rientjes if (!pgdat) { 102101b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1022cf23422bSminskey guo ret = -ENOMEM; 1023cf23422bSminskey guo goto out; 1024cf23422bSminskey guo } 1025b9ff0360SOscar Salvador 1026b9ff0360SOscar Salvador if (set_node_online) { 1027cf23422bSminskey guo node_set_online(nid); 1028cf23422bSminskey guo ret = register_one_node(nid); 1029cf23422bSminskey guo BUG_ON(ret); 1030b9ff0360SOscar Salvador } 1031cf23422bSminskey guo out: 1032b9ff0360SOscar Salvador return ret; 1033b9ff0360SOscar Salvador } 1034b9ff0360SOscar Salvador 1035b9ff0360SOscar Salvador /* 1036b9ff0360SOscar Salvador * Users of this function always want to online/register the node 1037b9ff0360SOscar Salvador */ 1038b9ff0360SOscar Salvador int try_online_node(int nid) 1039b9ff0360SOscar Salvador { 1040b9ff0360SOscar Salvador int ret; 1041b9ff0360SOscar Salvador 1042b9ff0360SOscar Salvador mem_hotplug_begin(); 1043c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, true); 1044bfc8c901SVladimir Davydov mem_hotplug_done(); 1045cf23422bSminskey guo return ret; 1046cf23422bSminskey guo } 1047cf23422bSminskey guo 104827356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 104927356f54SToshi Kani { 1050ba325585SPavel Tatashin /* memory range must be block size aligned */ 1051cec3ebd0SDavid Hildenbrand if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || 1052cec3ebd0SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes())) { 1053ba325585SPavel Tatashin pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1054cec3ebd0SDavid Hildenbrand memory_block_size_bytes(), start, size); 105527356f54SToshi Kani return -EINVAL; 105627356f54SToshi Kani } 105727356f54SToshi Kani 105827356f54SToshi Kani return 0; 105927356f54SToshi Kani } 106027356f54SToshi Kani 106131bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg) 106231bc3858SVitaly Kuznetsov { 10631adf8b46SAnshuman Khandual mem->online_type = mhp_default_online_type; 1064dc18d706SNathan Fontenot return device_online(&mem->dev); 106531bc3858SVitaly Kuznetsov } 106631bc3858SVitaly Kuznetsov 10678df1d0e4SDavid Hildenbrand /* 10688df1d0e4SDavid Hildenbrand * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 10698df1d0e4SDavid Hildenbrand * and online/offline operations (triggered e.g. by sysfs). 10708df1d0e4SDavid Hildenbrand * 10718df1d0e4SDavid Hildenbrand * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 10728df1d0e4SDavid Hildenbrand */ 1073b6117199SDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) 1074bc02af93SYasunori Goto { 1075*d15dfd31SCatalin Marinas struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; 107662cedb9fSDavid Vrabel u64 start, size; 1077b9ff0360SOscar Salvador bool new_node = false; 1078bc02af93SYasunori Goto int ret; 1079bc02af93SYasunori Goto 108062cedb9fSDavid Vrabel start = res->start; 108162cedb9fSDavid Vrabel size = resource_size(res); 108262cedb9fSDavid Vrabel 108327356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 108427356f54SToshi Kani if (ret) 108527356f54SToshi Kani return ret; 108627356f54SToshi Kani 1087fa6d9ec7SVishal Verma if (!node_possible(nid)) { 1088fa6d9ec7SVishal Verma WARN(1, "node %d was absent from the node_possible_map\n", nid); 1089fa6d9ec7SVishal Verma return -EINVAL; 1090fa6d9ec7SVishal Verma } 1091fa6d9ec7SVishal Verma 1092bfc8c901SVladimir Davydov mem_hotplug_begin(); 1093ac13c462SNathan Zimmer 109452219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 10957f36e3e5STang Chen memblock_add_node(start, size, nid); 10967f36e3e5STang Chen 1097c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, false); 1098b9ff0360SOscar Salvador if (ret < 0) 109941b9e2d7SWen Congyang goto error; 1100b9ff0360SOscar Salvador new_node = ret; 11019af3c2deSYasunori Goto 1102bc02af93SYasunori Goto /* call arch's memory hotadd */ 1103f5637d3bSLogan Gunthorpe ret = arch_add_memory(nid, start, size, ¶ms); 11049af3c2deSYasunori Goto if (ret < 0) 11059af3c2deSYasunori Goto goto error; 11069af3c2deSYasunori Goto 1107db051a0dSDavid Hildenbrand /* create memory block devices after memory was added */ 1108db051a0dSDavid Hildenbrand ret = create_memory_block_devices(start, size); 1109db051a0dSDavid Hildenbrand if (ret) { 1110db051a0dSDavid Hildenbrand arch_remove_memory(nid, start, size, NULL); 1111db051a0dSDavid Hildenbrand goto error; 1112db051a0dSDavid Hildenbrand } 1113db051a0dSDavid Hildenbrand 1114a1e565aaSTang Chen if (new_node) { 1115d5b6f6a3SOscar Salvador /* If sysfs file of new node can't be created, cpu on the node 11160fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 11170fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 1118d5b6f6a3SOscar Salvador * We online node here. We can't roll back from here. 11190fc44159SYasunori Goto */ 1120d5b6f6a3SOscar Salvador node_set_online(nid); 1121d5b6f6a3SOscar Salvador ret = __register_one_node(nid); 11220fc44159SYasunori Goto BUG_ON(ret); 11230fc44159SYasunori Goto } 11240fc44159SYasunori Goto 1125d5b6f6a3SOscar Salvador /* link memory sections under this node.*/ 112690c7eaebSLaurent Dufour link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1), 1127f85086f9SLaurent Dufour MEMINIT_HOTPLUG); 1128d5b6f6a3SOscar Salvador 1129d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 11307b7b2721SDavid Hildenbrand if (!strcmp(res->name, "System RAM")) 1131d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1132d96ae530Sakpm@linux-foundation.org 1133381eab4aSDavid Hildenbrand /* device_online() will take the lock when calling online_pages() */ 1134381eab4aSDavid Hildenbrand mem_hotplug_done(); 1135381eab4aSDavid Hildenbrand 11369ca6551eSDavid Hildenbrand /* 11379ca6551eSDavid Hildenbrand * In case we're allowed to merge the resource, flag it and trigger 11389ca6551eSDavid Hildenbrand * merging now that adding succeeded. 11399ca6551eSDavid Hildenbrand */ 114026011267SDavid Hildenbrand if (mhp_flags & MHP_MERGE_RESOURCE) 11419ca6551eSDavid Hildenbrand merge_system_ram_resource(res); 11429ca6551eSDavid Hildenbrand 114331bc3858SVitaly Kuznetsov /* online pages if requested */ 11441adf8b46SAnshuman Khandual if (mhp_default_online_type != MMOP_OFFLINE) 1145fbcf73ceSDavid Hildenbrand walk_memory_blocks(start, size, NULL, online_memory_block); 114631bc3858SVitaly Kuznetsov 1147381eab4aSDavid Hildenbrand return ret; 11489af3c2deSYasunori Goto error: 11499af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 1150b9ff0360SOscar Salvador if (new_node) 1151b9ff0360SOscar Salvador rollback_node_hotadd(nid); 115252219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 11537f36e3e5STang Chen memblock_remove(start, size); 1154bfc8c901SVladimir Davydov mem_hotplug_done(); 1155bc02af93SYasunori Goto return ret; 1156bc02af93SYasunori Goto } 115762cedb9fSDavid Vrabel 11588df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */ 1159b6117199SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 116062cedb9fSDavid Vrabel { 116162cedb9fSDavid Vrabel struct resource *res; 116262cedb9fSDavid Vrabel int ret; 116362cedb9fSDavid Vrabel 11647b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, "System RAM"); 11656f754ba4SVitaly Kuznetsov if (IS_ERR(res)) 11666f754ba4SVitaly Kuznetsov return PTR_ERR(res); 116762cedb9fSDavid Vrabel 1168b6117199SDavid Hildenbrand ret = add_memory_resource(nid, res, mhp_flags); 116962cedb9fSDavid Vrabel if (ret < 0) 117062cedb9fSDavid Vrabel release_memory_resource(res); 117162cedb9fSDavid Vrabel return ret; 117262cedb9fSDavid Vrabel } 11738df1d0e4SDavid Hildenbrand 1174b6117199SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 11758df1d0e4SDavid Hildenbrand { 11768df1d0e4SDavid Hildenbrand int rc; 11778df1d0e4SDavid Hildenbrand 11788df1d0e4SDavid Hildenbrand lock_device_hotplug(); 1179b6117199SDavid Hildenbrand rc = __add_memory(nid, start, size, mhp_flags); 11808df1d0e4SDavid Hildenbrand unlock_device_hotplug(); 11818df1d0e4SDavid Hildenbrand 11828df1d0e4SDavid Hildenbrand return rc; 11838df1d0e4SDavid Hildenbrand } 1184bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 11850c0e6195SKAMEZAWA Hiroyuki 11867b7b2721SDavid Hildenbrand /* 11877b7b2721SDavid Hildenbrand * Add special, driver-managed memory to the system as system RAM. Such 11887b7b2721SDavid Hildenbrand * memory is not exposed via the raw firmware-provided memmap as system 11897b7b2721SDavid Hildenbrand * RAM, instead, it is detected and added by a driver - during cold boot, 11907b7b2721SDavid Hildenbrand * after a reboot, and after kexec. 11917b7b2721SDavid Hildenbrand * 11927b7b2721SDavid Hildenbrand * Reasons why this memory should not be used for the initial memmap of a 11937b7b2721SDavid Hildenbrand * kexec kernel or for placing kexec images: 11947b7b2721SDavid Hildenbrand * - The booting kernel is in charge of determining how this memory will be 11957b7b2721SDavid Hildenbrand * used (e.g., use persistent memory as system RAM) 11967b7b2721SDavid Hildenbrand * - Coordination with a hypervisor is required before this memory 11977b7b2721SDavid Hildenbrand * can be used (e.g., inaccessible parts). 11987b7b2721SDavid Hildenbrand * 11997b7b2721SDavid Hildenbrand * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided 12007b7b2721SDavid Hildenbrand * memory map") are created. Also, the created memory resource is flagged 12017cf603d1SDavid Hildenbrand * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case 12027b7b2721SDavid Hildenbrand * this memory as well (esp., not place kexec images onto it). 12037b7b2721SDavid Hildenbrand * 12047b7b2721SDavid Hildenbrand * The resource_name (visible via /proc/iomem) has to have the format 12057b7b2721SDavid Hildenbrand * "System RAM ($DRIVER)". 12067b7b2721SDavid Hildenbrand */ 12077b7b2721SDavid Hildenbrand int add_memory_driver_managed(int nid, u64 start, u64 size, 1208b6117199SDavid Hildenbrand const char *resource_name, mhp_t mhp_flags) 12097b7b2721SDavid Hildenbrand { 12107b7b2721SDavid Hildenbrand struct resource *res; 12117b7b2721SDavid Hildenbrand int rc; 12127b7b2721SDavid Hildenbrand 12137b7b2721SDavid Hildenbrand if (!resource_name || 12147b7b2721SDavid Hildenbrand strstr(resource_name, "System RAM (") != resource_name || 12157b7b2721SDavid Hildenbrand resource_name[strlen(resource_name) - 1] != ')') 12167b7b2721SDavid Hildenbrand return -EINVAL; 12177b7b2721SDavid Hildenbrand 12187b7b2721SDavid Hildenbrand lock_device_hotplug(); 12197b7b2721SDavid Hildenbrand 12207b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, resource_name); 12217b7b2721SDavid Hildenbrand if (IS_ERR(res)) { 12227b7b2721SDavid Hildenbrand rc = PTR_ERR(res); 12237b7b2721SDavid Hildenbrand goto out_unlock; 12247b7b2721SDavid Hildenbrand } 12257b7b2721SDavid Hildenbrand 1226b6117199SDavid Hildenbrand rc = add_memory_resource(nid, res, mhp_flags); 12277b7b2721SDavid Hildenbrand if (rc < 0) 12287b7b2721SDavid Hildenbrand release_memory_resource(res); 12297b7b2721SDavid Hildenbrand 12307b7b2721SDavid Hildenbrand out_unlock: 12317b7b2721SDavid Hildenbrand unlock_device_hotplug(); 12327b7b2721SDavid Hildenbrand return rc; 12337b7b2721SDavid Hildenbrand } 12347b7b2721SDavid Hildenbrand EXPORT_SYMBOL_GPL(add_memory_driver_managed); 12357b7b2721SDavid Hildenbrand 1236bca3feaaSAnshuman Khandual /* 1237bca3feaaSAnshuman Khandual * Platforms should define arch_get_mappable_range() that provides 1238bca3feaaSAnshuman Khandual * maximum possible addressable physical memory range for which the 1239bca3feaaSAnshuman Khandual * linear mapping could be created. The platform returned address 1240bca3feaaSAnshuman Khandual * range must adhere to these following semantics. 1241bca3feaaSAnshuman Khandual * 1242bca3feaaSAnshuman Khandual * - range.start <= range.end 1243bca3feaaSAnshuman Khandual * - Range includes both end points [range.start..range.end] 1244bca3feaaSAnshuman Khandual * 1245bca3feaaSAnshuman Khandual * There is also a fallback definition provided here, allowing the 1246bca3feaaSAnshuman Khandual * entire possible physical address range in case any platform does 1247bca3feaaSAnshuman Khandual * not define arch_get_mappable_range(). 1248bca3feaaSAnshuman Khandual */ 1249bca3feaaSAnshuman Khandual struct range __weak arch_get_mappable_range(void) 1250bca3feaaSAnshuman Khandual { 1251bca3feaaSAnshuman Khandual struct range mhp_range = { 1252bca3feaaSAnshuman Khandual .start = 0UL, 1253bca3feaaSAnshuman Khandual .end = -1ULL, 1254bca3feaaSAnshuman Khandual }; 1255bca3feaaSAnshuman Khandual return mhp_range; 1256bca3feaaSAnshuman Khandual } 1257bca3feaaSAnshuman Khandual 1258bca3feaaSAnshuman Khandual struct range mhp_get_pluggable_range(bool need_mapping) 1259bca3feaaSAnshuman Khandual { 1260bca3feaaSAnshuman Khandual const u64 max_phys = (1ULL << MAX_PHYSMEM_BITS) - 1; 1261bca3feaaSAnshuman Khandual struct range mhp_range; 1262bca3feaaSAnshuman Khandual 1263bca3feaaSAnshuman Khandual if (need_mapping) { 1264bca3feaaSAnshuman Khandual mhp_range = arch_get_mappable_range(); 1265bca3feaaSAnshuman Khandual if (mhp_range.start > max_phys) { 1266bca3feaaSAnshuman Khandual mhp_range.start = 0; 1267bca3feaaSAnshuman Khandual mhp_range.end = 0; 1268bca3feaaSAnshuman Khandual } 1269bca3feaaSAnshuman Khandual mhp_range.end = min_t(u64, mhp_range.end, max_phys); 1270bca3feaaSAnshuman Khandual } else { 1271bca3feaaSAnshuman Khandual mhp_range.start = 0; 1272bca3feaaSAnshuman Khandual mhp_range.end = max_phys; 1273bca3feaaSAnshuman Khandual } 1274bca3feaaSAnshuman Khandual return mhp_range; 1275bca3feaaSAnshuman Khandual } 1276bca3feaaSAnshuman Khandual EXPORT_SYMBOL_GPL(mhp_get_pluggable_range); 1277bca3feaaSAnshuman Khandual 1278bca3feaaSAnshuman Khandual bool mhp_range_allowed(u64 start, u64 size, bool need_mapping) 1279bca3feaaSAnshuman Khandual { 1280bca3feaaSAnshuman Khandual struct range mhp_range = mhp_get_pluggable_range(need_mapping); 1281bca3feaaSAnshuman Khandual u64 end = start + size; 1282bca3feaaSAnshuman Khandual 1283bca3feaaSAnshuman Khandual if (start < end && start >= mhp_range.start && (end - 1) <= mhp_range.end) 1284bca3feaaSAnshuman Khandual return true; 1285bca3feaaSAnshuman Khandual 1286bca3feaaSAnshuman Khandual pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n", 1287bca3feaaSAnshuman Khandual start, end, mhp_range.start, mhp_range.end); 1288bca3feaaSAnshuman Khandual return false; 1289bca3feaaSAnshuman Khandual } 1290bca3feaaSAnshuman Khandual 12910c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 12920c0e6195SKAMEZAWA Hiroyuki /* 129392917998SDavid Hildenbrand * Confirm all pages in a range [start, end) belong to the same zone (skipping 129492917998SDavid Hildenbrand * memory holes). When true, return the zone. 12950c0e6195SKAMEZAWA Hiroyuki */ 129692917998SDavid Hildenbrand struct zone *test_pages_in_a_zone(unsigned long start_pfn, 129792917998SDavid Hildenbrand unsigned long end_pfn) 12980c0e6195SKAMEZAWA Hiroyuki { 12995f0f2887SAndrew Banman unsigned long pfn, sec_end_pfn; 13000c0e6195SKAMEZAWA Hiroyuki struct zone *zone = NULL; 13010c0e6195SKAMEZAWA Hiroyuki struct page *page; 13020c0e6195SKAMEZAWA Hiroyuki int i; 1303deb88a2aSToshi Kani for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1); 13040c0e6195SKAMEZAWA Hiroyuki pfn < end_pfn; 1305deb88a2aSToshi Kani pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) { 13065f0f2887SAndrew Banman /* Make sure the memory section is present first */ 13075f0f2887SAndrew Banman if (!present_section_nr(pfn_to_section_nr(pfn))) 13085f0f2887SAndrew Banman continue; 13095f0f2887SAndrew Banman for (; pfn < sec_end_pfn && pfn < end_pfn; 13100c0e6195SKAMEZAWA Hiroyuki pfn += MAX_ORDER_NR_PAGES) { 13110c0e6195SKAMEZAWA Hiroyuki i = 0; 13120c0e6195SKAMEZAWA Hiroyuki /* This is just a CONFIG_HOLES_IN_ZONE check.*/ 13135f0f2887SAndrew Banman while ((i < MAX_ORDER_NR_PAGES) && 13145f0f2887SAndrew Banman !pfn_valid_within(pfn + i)) 13150c0e6195SKAMEZAWA Hiroyuki i++; 1316d6d8c8a4Szhong jiang if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn) 13170c0e6195SKAMEZAWA Hiroyuki continue; 131824feb47cSMikhail Zaslonko /* Check if we got outside of the zone */ 131924feb47cSMikhail Zaslonko if (zone && !zone_spans_pfn(zone, pfn + i)) 132092917998SDavid Hildenbrand return NULL; 13210c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn + i); 13220c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 132392917998SDavid Hildenbrand return NULL; 13240c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 13250c0e6195SKAMEZAWA Hiroyuki } 13265f0f2887SAndrew Banman } 1327deb88a2aSToshi Kani 132892917998SDavid Hildenbrand return zone; 1329a96dfddbSToshi Kani } 13300c0e6195SKAMEZAWA Hiroyuki 13310c0e6195SKAMEZAWA Hiroyuki /* 13320efadf48SYisheng Xie * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 1333aa218795SDavid Hildenbrand * non-lru movable pages and hugepages). Will skip over most unmovable 1334aa218795SDavid Hildenbrand * pages (esp., pages that can be skipped when offlining), but bail out on 1335aa218795SDavid Hildenbrand * definitely unmovable pages. 1336aa218795SDavid Hildenbrand * 1337aa218795SDavid Hildenbrand * Returns: 1338aa218795SDavid Hildenbrand * 0 in case a movable page is found and movable_pfn was updated. 1339aa218795SDavid Hildenbrand * -ENOENT in case no movable page was found. 1340aa218795SDavid Hildenbrand * -EBUSY in case a definitely unmovable page was found. 13410c0e6195SKAMEZAWA Hiroyuki */ 1342aa218795SDavid Hildenbrand static int scan_movable_pages(unsigned long start, unsigned long end, 1343aa218795SDavid Hildenbrand unsigned long *movable_pfn) 13440c0e6195SKAMEZAWA Hiroyuki { 13450c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 1346eeb0efd0SOscar Salvador 13470c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 1348eeb0efd0SOscar Salvador struct page *page, *head; 1349eeb0efd0SOscar Salvador unsigned long skip; 1350eeb0efd0SOscar Salvador 1351eeb0efd0SOscar Salvador if (!pfn_valid(pfn)) 1352eeb0efd0SOscar Salvador continue; 13530c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 13540c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 1355aa218795SDavid Hildenbrand goto found; 13560efadf48SYisheng Xie if (__PageMovable(page)) 1357aa218795SDavid Hildenbrand goto found; 1358aa218795SDavid Hildenbrand 1359aa218795SDavid Hildenbrand /* 1360aa218795SDavid Hildenbrand * PageOffline() pages that are not marked __PageMovable() and 1361aa218795SDavid Hildenbrand * have a reference count > 0 (after MEM_GOING_OFFLINE) are 1362aa218795SDavid Hildenbrand * definitely unmovable. If their reference count would be 0, 1363aa218795SDavid Hildenbrand * they could at least be skipped when offlining memory. 1364aa218795SDavid Hildenbrand */ 1365aa218795SDavid Hildenbrand if (PageOffline(page) && page_count(page)) 1366aa218795SDavid Hildenbrand return -EBUSY; 1367eeb0efd0SOscar Salvador 1368eeb0efd0SOscar Salvador if (!PageHuge(page)) 1369eeb0efd0SOscar Salvador continue; 1370eeb0efd0SOscar Salvador head = compound_head(page); 13718f251a3dSMike Kravetz /* 13728f251a3dSMike Kravetz * This test is racy as we hold no reference or lock. The 13738f251a3dSMike Kravetz * hugetlb page could have been free'ed and head is no longer 13748f251a3dSMike Kravetz * a hugetlb page before the following check. In such unlikely 13758f251a3dSMike Kravetz * cases false positives and negatives are possible. Calling 13768f251a3dSMike Kravetz * code must deal with these scenarios. 13778f251a3dSMike Kravetz */ 13788f251a3dSMike Kravetz if (HPageMigratable(head)) 1379aa218795SDavid Hildenbrand goto found; 1380d8c6546bSMatthew Wilcox (Oracle) skip = compound_nr(head) - (page - head); 1381eeb0efd0SOscar Salvador pfn += skip - 1; 13820c0e6195SKAMEZAWA Hiroyuki } 1383aa218795SDavid Hildenbrand return -ENOENT; 1384aa218795SDavid Hildenbrand found: 1385aa218795SDavid Hildenbrand *movable_pfn = pfn; 13860c0e6195SKAMEZAWA Hiroyuki return 0; 13870c0e6195SKAMEZAWA Hiroyuki } 13880c0e6195SKAMEZAWA Hiroyuki 13890c0e6195SKAMEZAWA Hiroyuki static int 13900c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 13910c0e6195SKAMEZAWA Hiroyuki { 13920c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 13936c357848SMatthew Wilcox (Oracle) struct page *page, *head; 13940c0e6195SKAMEZAWA Hiroyuki int ret = 0; 13950c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 13960c0e6195SKAMEZAWA Hiroyuki 1397a85009c3SMichal Hocko for (pfn = start_pfn; pfn < end_pfn; pfn++) { 13980c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 13990c0e6195SKAMEZAWA Hiroyuki continue; 14000c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 14016c357848SMatthew Wilcox (Oracle) head = compound_head(page); 1402c8721bbbSNaoya Horiguchi 1403c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1404d8c6546bSMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + compound_nr(head) - 1; 1405daf3538aSOscar Salvador isolate_huge_page(head, &source); 1406c8721bbbSNaoya Horiguchi continue; 140794723aafSMichal Hocko } else if (PageTransHuge(page)) 14086c357848SMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + thp_nr_pages(page) - 1; 1409c8721bbbSNaoya Horiguchi 1410b15c8726SMichal Hocko /* 1411b15c8726SMichal Hocko * HWPoison pages have elevated reference counts so the migration would 1412b15c8726SMichal Hocko * fail on them. It also doesn't make any sense to migrate them in the 1413b15c8726SMichal Hocko * first place. Still try to unmap such a page in case it is still mapped 1414b15c8726SMichal Hocko * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep 1415b15c8726SMichal Hocko * the unmap as the catch all safety net). 1416b15c8726SMichal Hocko */ 1417b15c8726SMichal Hocko if (PageHWPoison(page)) { 1418b15c8726SMichal Hocko if (WARN_ON(PageLRU(page))) 1419b15c8726SMichal Hocko isolate_lru_page(page); 1420b15c8726SMichal Hocko if (page_mapped(page)) 1421013339dfSShakeel Butt try_to_unmap(page, TTU_IGNORE_MLOCK); 1422b15c8726SMichal Hocko continue; 1423b15c8726SMichal Hocko } 1424b15c8726SMichal Hocko 1425700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 14260c0e6195SKAMEZAWA Hiroyuki continue; 14270c0e6195SKAMEZAWA Hiroyuki /* 14280efadf48SYisheng Xie * We can skip free pages. And we can deal with pages on 14290efadf48SYisheng Xie * LRU and non-lru movable pages. 14300c0e6195SKAMEZAWA Hiroyuki */ 14310efadf48SYisheng Xie if (PageLRU(page)) 143262695a84SNick Piggin ret = isolate_lru_page(page); 14330efadf48SYisheng Xie else 14340efadf48SYisheng Xie ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 14350c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 143662695a84SNick Piggin list_add_tail(&page->lru, &source); 14370efadf48SYisheng Xie if (!__PageMovable(page)) 1438599d0c95SMel Gorman inc_node_page_state(page, NR_ISOLATED_ANON + 14399de4f22aSHuang Ying page_is_file_lru(page)); 14406d9c285aSKOSAKI Motohiro 14410c0e6195SKAMEZAWA Hiroyuki } else { 14422932c8b0SMichal Hocko pr_warn("failed to isolate pfn %lx\n", pfn); 14430efadf48SYisheng Xie dump_page(page, "isolation failed"); 14441723058eSOscar Salvador } 1445700c2a46SKonstantin Khlebnikov put_page(page); 14460c0e6195SKAMEZAWA Hiroyuki } 1447f3ab2636SBob Liu if (!list_empty(&source)) { 1448203e6e5cSJoonsoo Kim nodemask_t nmask = node_states[N_MEMORY]; 1449203e6e5cSJoonsoo Kim struct migration_target_control mtc = { 1450203e6e5cSJoonsoo Kim .nmask = &nmask, 1451203e6e5cSJoonsoo Kim .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, 1452203e6e5cSJoonsoo Kim }; 1453203e6e5cSJoonsoo Kim 1454203e6e5cSJoonsoo Kim /* 1455203e6e5cSJoonsoo Kim * We have checked that migration range is on a single zone so 1456203e6e5cSJoonsoo Kim * we can use the nid of the first page to all the others. 1457203e6e5cSJoonsoo Kim */ 1458203e6e5cSJoonsoo Kim mtc.nid = page_to_nid(list_first_entry(&source, struct page, lru)); 1459203e6e5cSJoonsoo Kim 1460203e6e5cSJoonsoo Kim /* 1461203e6e5cSJoonsoo Kim * try to allocate from a different node but reuse this node 1462203e6e5cSJoonsoo Kim * if there are no other online nodes to be used (e.g. we are 1463203e6e5cSJoonsoo Kim * offlining a part of the only existing node) 1464203e6e5cSJoonsoo Kim */ 1465203e6e5cSJoonsoo Kim node_clear(mtc.nid, nmask); 1466203e6e5cSJoonsoo Kim if (nodes_empty(nmask)) 1467203e6e5cSJoonsoo Kim node_set(mtc.nid, nmask); 1468203e6e5cSJoonsoo Kim ret = migrate_pages(&source, alloc_migration_target, NULL, 1469203e6e5cSJoonsoo Kim (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG); 14702932c8b0SMichal Hocko if (ret) { 14712932c8b0SMichal Hocko list_for_each_entry(page, &source, lru) { 14722932c8b0SMichal Hocko pr_warn("migrating pfn %lx failed ret:%d ", 14732932c8b0SMichal Hocko page_to_pfn(page), ret); 14742932c8b0SMichal Hocko dump_page(page, "migration failure"); 14752932c8b0SMichal Hocko } 1476c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1477f3ab2636SBob Liu } 14782932c8b0SMichal Hocko } 14791723058eSOscar Salvador 14800c0e6195SKAMEZAWA Hiroyuki return ret; 14810c0e6195SKAMEZAWA Hiroyuki } 14820c0e6195SKAMEZAWA Hiroyuki 1483c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1484c5320926STang Chen { 148555ac590cSTang Chen movable_node_enabled = true; 1486c5320926STang Chen return 0; 1487c5320926STang Chen } 1488c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1489c5320926STang Chen 1490d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1491d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1492d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1493d9713679SLai Jiangshan { 1494d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1495d9713679SLai Jiangshan unsigned long present_pages = 0; 149686b27beaSOscar Salvador enum zone_type zt; 1497d9713679SLai Jiangshan 149898fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 149998fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 150098fa15f3SAnshuman Khandual arg->status_change_nid_high = NUMA_NO_NODE; 150186b27beaSOscar Salvador 150286b27beaSOscar Salvador /* 150386b27beaSOscar Salvador * Check whether node_states[N_NORMAL_MEMORY] will be changed. 150486b27beaSOscar Salvador * If the memory to be offline is within the range 150586b27beaSOscar Salvador * [0..ZONE_NORMAL], and it is the last present memory there, 150686b27beaSOscar Salvador * the zones in that range will become empty after the offlining, 150786b27beaSOscar Salvador * thus we can determine that we need to clear the node from 150886b27beaSOscar Salvador * node_states[N_NORMAL_MEMORY]. 150986b27beaSOscar Salvador */ 151086b27beaSOscar Salvador for (zt = 0; zt <= ZONE_NORMAL; zt++) 151186b27beaSOscar Salvador present_pages += pgdat->node_zones[zt].present_pages; 151286b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 151386b27beaSOscar Salvador arg->status_change_nid_normal = zone_to_nid(zone); 1514d9713679SLai Jiangshan 15156715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM 15166715ddf9SLai Jiangshan /* 151786b27beaSOscar Salvador * node_states[N_HIGH_MEMORY] contains nodes which 151886b27beaSOscar Salvador * have normal memory or high memory. 151986b27beaSOscar Salvador * Here we add the present_pages belonging to ZONE_HIGHMEM. 152086b27beaSOscar Salvador * If the zone is within the range of [0..ZONE_HIGHMEM), and 152186b27beaSOscar Salvador * we determine that the zones in that range become empty, 152286b27beaSOscar Salvador * we need to clear the node for N_HIGH_MEMORY. 15236715ddf9SLai Jiangshan */ 152486b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages; 152586b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages) 15266715ddf9SLai Jiangshan arg->status_change_nid_high = zone_to_nid(zone); 15276715ddf9SLai Jiangshan #endif 15286715ddf9SLai Jiangshan 1529d9713679SLai Jiangshan /* 153086b27beaSOscar Salvador * We have accounted the pages from [0..ZONE_NORMAL), and 153186b27beaSOscar Salvador * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM 153286b27beaSOscar Salvador * as well. 153386b27beaSOscar Salvador * Here we count the possible pages from ZONE_MOVABLE. 153486b27beaSOscar Salvador * If after having accounted all the pages, we see that the nr_pages 153586b27beaSOscar Salvador * to be offlined is over or equal to the accounted pages, 153686b27beaSOscar Salvador * we know that the node will become empty, and so, we can clear 153786b27beaSOscar Salvador * it for N_MEMORY as well. 1538d9713679SLai Jiangshan */ 153986b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1540d9713679SLai Jiangshan 1541d9713679SLai Jiangshan if (nr_pages >= present_pages) 1542d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1543d9713679SLai Jiangshan } 1544d9713679SLai Jiangshan 1545d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1546d9713679SLai Jiangshan { 1547d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1548d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1549d9713679SLai Jiangshan 1550cf01f6f5SOscar Salvador if (arg->status_change_nid_high >= 0) 1551d9713679SLai Jiangshan node_clear_state(node, N_HIGH_MEMORY); 15526715ddf9SLai Jiangshan 1553cf01f6f5SOscar Salvador if (arg->status_change_nid >= 0) 15546715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1555d9713679SLai Jiangshan } 1556d9713679SLai Jiangshan 1557c5e79ef5SDavid Hildenbrand static int count_system_ram_pages_cb(unsigned long start_pfn, 1558c5e79ef5SDavid Hildenbrand unsigned long nr_pages, void *data) 1559c5e79ef5SDavid Hildenbrand { 1560c5e79ef5SDavid Hildenbrand unsigned long *nr_system_ram_pages = data; 1561c5e79ef5SDavid Hildenbrand 1562c5e79ef5SDavid Hildenbrand *nr_system_ram_pages += nr_pages; 1563c5e79ef5SDavid Hildenbrand return 0; 1564c5e79ef5SDavid Hildenbrand } 1565c5e79ef5SDavid Hildenbrand 156673a11c96SDavid Hildenbrand int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) 15670c0e6195SKAMEZAWA Hiroyuki { 156873a11c96SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 15690a1a9a00SDavid Hildenbrand unsigned long pfn, system_ram_pages = 0; 1570d702909fSCody P Schafer unsigned long flags; 15710c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 15727b78d335SYasunori Goto struct memory_notify arg; 1573ea15153cSDavid Hildenbrand int ret, node; 157479605093SMichal Hocko char *reason; 15750c0e6195SKAMEZAWA Hiroyuki 15764986fac1SDavid Hildenbrand /* We can only offline full sections (e.g., SECTION_IS_ONLINE) */ 15774986fac1SDavid Hildenbrand if (WARN_ON_ONCE(!nr_pages || 15784986fac1SDavid Hildenbrand !IS_ALIGNED(start_pfn | nr_pages, PAGES_PER_SECTION))) 15794986fac1SDavid Hildenbrand return -EINVAL; 15804986fac1SDavid Hildenbrand 1581381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1582381eab4aSDavid Hildenbrand 1583c5e79ef5SDavid Hildenbrand /* 1584c5e79ef5SDavid Hildenbrand * Don't allow to offline memory blocks that contain holes. 1585c5e79ef5SDavid Hildenbrand * Consequently, memory blocks with holes can never get onlined 1586c5e79ef5SDavid Hildenbrand * via the hotplug path - online_pages() - as hotplugged memory has 1587c5e79ef5SDavid Hildenbrand * no holes. This way, we e.g., don't have to worry about marking 1588c5e79ef5SDavid Hildenbrand * memory holes PG_reserved, don't need pfn_valid() checks, and can 1589c5e79ef5SDavid Hildenbrand * avoid using walk_system_ram_range() later. 1590c5e79ef5SDavid Hildenbrand */ 159173a11c96SDavid Hildenbrand walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages, 1592c5e79ef5SDavid Hildenbrand count_system_ram_pages_cb); 159373a11c96SDavid Hildenbrand if (system_ram_pages != nr_pages) { 1594c5e79ef5SDavid Hildenbrand ret = -EINVAL; 1595c5e79ef5SDavid Hildenbrand reason = "memory holes"; 1596c5e79ef5SDavid Hildenbrand goto failed_removal; 1597c5e79ef5SDavid Hildenbrand } 1598c5e79ef5SDavid Hildenbrand 15990c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 16000c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 160192917998SDavid Hildenbrand zone = test_pages_in_a_zone(start_pfn, end_pfn); 160292917998SDavid Hildenbrand if (!zone) { 160379605093SMichal Hocko ret = -EINVAL; 160479605093SMichal Hocko reason = "multizone range"; 160579605093SMichal Hocko goto failed_removal; 1606381eab4aSDavid Hildenbrand } 16077b78d335SYasunori Goto node = zone_to_nid(zone); 16087b78d335SYasunori Goto 1609ec6e8c7eSVlastimil Babka /* 1610ec6e8c7eSVlastimil Babka * Disable pcplists so that page isolation cannot race with freeing 1611ec6e8c7eSVlastimil Babka * in a way that pages from isolated pageblock are left on pcplists. 1612ec6e8c7eSVlastimil Babka */ 1613ec6e8c7eSVlastimil Babka zone_pcp_disable(zone); 1614ec6e8c7eSVlastimil Babka 16150c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1616b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1617d381c547SMichal Hocko MIGRATE_MOVABLE, 1618756d25beSDavid Hildenbrand MEMORY_OFFLINE | REPORT_FAILURE); 16193fa0c7c7SDavid Hildenbrand if (ret) { 162079605093SMichal Hocko reason = "failure to isolate range"; 1621ec6e8c7eSVlastimil Babka goto failed_removal_pcplists_disabled; 1622381eab4aSDavid Hildenbrand } 16237b78d335SYasunori Goto 16247b78d335SYasunori Goto arg.start_pfn = start_pfn; 16257b78d335SYasunori Goto arg.nr_pages = nr_pages; 1626d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 16277b78d335SYasunori Goto 16287b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 16297b78d335SYasunori Goto ret = notifier_to_errno(ret); 163079605093SMichal Hocko if (ret) { 163179605093SMichal Hocko reason = "notifier failure"; 163279605093SMichal Hocko goto failed_removal_isolated; 163379605093SMichal Hocko } 16347b78d335SYasunori Goto 1635bb8965bdSMichal Hocko do { 1636aa218795SDavid Hildenbrand pfn = start_pfn; 1637aa218795SDavid Hildenbrand do { 163879605093SMichal Hocko if (signal_pending(current)) { 1639bb8965bdSMichal Hocko ret = -EINTR; 164079605093SMichal Hocko reason = "signal backoff"; 164179605093SMichal Hocko goto failed_removal_isolated; 164279605093SMichal Hocko } 164372b39cfcSMichal Hocko 16440c0e6195SKAMEZAWA Hiroyuki cond_resched(); 16459852a721SMichal Hocko lru_add_drain_all(); 16460c0e6195SKAMEZAWA Hiroyuki 1647aa218795SDavid Hildenbrand ret = scan_movable_pages(pfn, end_pfn, &pfn); 1648aa218795SDavid Hildenbrand if (!ret) { 1649bb8965bdSMichal Hocko /* 1650bb8965bdSMichal Hocko * TODO: fatal migration failures should bail 1651bb8965bdSMichal Hocko * out 1652bb8965bdSMichal Hocko */ 1653bb8965bdSMichal Hocko do_migrate_range(pfn, end_pfn); 1654bb8965bdSMichal Hocko } 1655aa218795SDavid Hildenbrand } while (!ret); 1656aa218795SDavid Hildenbrand 1657aa218795SDavid Hildenbrand if (ret != -ENOENT) { 1658aa218795SDavid Hildenbrand reason = "unmovable page"; 1659aa218795SDavid Hildenbrand goto failed_removal_isolated; 16600c0e6195SKAMEZAWA Hiroyuki } 166172b39cfcSMichal Hocko 1662c8721bbbSNaoya Horiguchi /* 1663bb8965bdSMichal Hocko * Dissolve free hugepages in the memory block before doing 1664bb8965bdSMichal Hocko * offlining actually in order to make hugetlbfs's object 1665bb8965bdSMichal Hocko * counting consistent. 1666c8721bbbSNaoya Horiguchi */ 1667082d5b6bSGerald Schaefer ret = dissolve_free_huge_pages(start_pfn, end_pfn); 166879605093SMichal Hocko if (ret) { 166979605093SMichal Hocko reason = "failure to dissolve huge pages"; 167079605093SMichal Hocko goto failed_removal_isolated; 167179605093SMichal Hocko } 16720a1a9a00SDavid Hildenbrand 16730a1a9a00SDavid Hildenbrand ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE); 1674ec6e8c7eSVlastimil Babka 16755557c766SMichal Hocko } while (ret); 1676bb8965bdSMichal Hocko 16770a1a9a00SDavid Hildenbrand /* Mark all sections offline and remove free pages from the buddy. */ 16780a1a9a00SDavid Hildenbrand __offline_isolated_pages(start_pfn, end_pfn); 16797c33023aSLaurent Dufour pr_debug("Offlined Pages %ld\n", nr_pages); 16800a1a9a00SDavid Hildenbrand 16819b7ea46aSQian Cai /* 1682b30c5927SDavid Hildenbrand * The memory sections are marked offline, and the pageblock flags 1683b30c5927SDavid Hildenbrand * effectively stale; nobody should be touching them. Fixup the number 1684b30c5927SDavid Hildenbrand * of isolated pageblocks, memory onlining will properly revert this. 16859b7ea46aSQian Cai */ 16869b7ea46aSQian Cai spin_lock_irqsave(&zone->lock, flags); 1687ea15153cSDavid Hildenbrand zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages; 16889b7ea46aSQian Cai spin_unlock_irqrestore(&zone->lock, flags); 16899b7ea46aSQian Cai 1690ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 1691ec6e8c7eSVlastimil Babka 16920c0e6195SKAMEZAWA Hiroyuki /* removal success */ 16930a1a9a00SDavid Hildenbrand adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages); 16940a1a9a00SDavid Hildenbrand zone->present_pages -= nr_pages; 1695d702909fSCody P Schafer 1696d702909fSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 16970a1a9a00SDavid Hildenbrand zone->zone_pgdat->node_present_pages -= nr_pages; 1698d702909fSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 16997b78d335SYasunori Goto 17001b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 17011b79acc9SKOSAKI Motohiro 17021e8537baSXishi Qiu if (!populated_zone(zone)) { 1703340175b7SJiang Liu zone_pcp_reset(zone); 170472675e13SMichal Hocko build_all_zonelists(NULL); 17051e8537baSXishi Qiu } else 17061e8537baSXishi Qiu zone_pcp_update(zone); 1707340175b7SJiang Liu 1708d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1709698b1b30SVlastimil Babka if (arg.status_change_nid >= 0) { 17108fe23e05SDavid Rientjes kswapd_stop(node); 1711698b1b30SVlastimil Babka kcompactd_stop(node); 1712698b1b30SVlastimil Babka } 1713bce7394aSMinchan Kim 17140c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 17157b78d335SYasunori Goto 17167b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 1717feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, start_pfn, nr_pages); 1718381eab4aSDavid Hildenbrand mem_hotplug_done(); 17190c0e6195SKAMEZAWA Hiroyuki return 0; 17200c0e6195SKAMEZAWA Hiroyuki 172179605093SMichal Hocko failed_removal_isolated: 172279605093SMichal Hocko undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 1723c4efe484SQian Cai memory_notify(MEM_CANCEL_OFFLINE, &arg); 1724ec6e8c7eSVlastimil Babka failed_removal_pcplists_disabled: 1725ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 17260c0e6195SKAMEZAWA Hiroyuki failed_removal: 172779605093SMichal Hocko pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 1728a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 172979605093SMichal Hocko ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 173079605093SMichal Hocko reason); 17310c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 1732381eab4aSDavid Hildenbrand mem_hotplug_done(); 17330c0e6195SKAMEZAWA Hiroyuki return ret; 17340c0e6195SKAMEZAWA Hiroyuki } 173571088785SBadari Pulavarty 1736d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1737bbc76be6SWen Congyang { 1738bbc76be6SWen Congyang int ret = !is_memblock_offlined(mem); 1739bbc76be6SWen Congyang 1740349daa0fSRandy Dunlap if (unlikely(ret)) { 1741349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 1742349daa0fSRandy Dunlap 1743349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1744b6c88d3bSDavid Hildenbrand endpa = beginpa + memory_block_size_bytes() - 1; 1745756a025fSJoe Perches pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 1746349daa0fSRandy Dunlap &beginpa, &endpa); 1747bbc76be6SWen Congyang 1748eca499abSPavel Tatashin return -EBUSY; 1749eca499abSPavel Tatashin } 1750eca499abSPavel Tatashin return 0; 1751bbc76be6SWen Congyang } 1752bbc76be6SWen Congyang 17530f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat) 175460a5a19eSTang Chen { 175560a5a19eSTang Chen int cpu; 175660a5a19eSTang Chen 175760a5a19eSTang Chen for_each_present_cpu(cpu) { 175860a5a19eSTang Chen if (cpu_to_node(cpu) == pgdat->node_id) 175960a5a19eSTang Chen /* 176060a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 176160a5a19eSTang Chen * offline this node. 176260a5a19eSTang Chen */ 176360a5a19eSTang Chen return -EBUSY; 176460a5a19eSTang Chen } 176560a5a19eSTang Chen 176660a5a19eSTang Chen return 0; 176760a5a19eSTang Chen } 176860a5a19eSTang Chen 17692c91f8fcSDavid Hildenbrand static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg) 17702c91f8fcSDavid Hildenbrand { 17712c91f8fcSDavid Hildenbrand int nid = *(int *)arg; 17722c91f8fcSDavid Hildenbrand 17732c91f8fcSDavid Hildenbrand /* 17742c91f8fcSDavid Hildenbrand * If a memory block belongs to multiple nodes, the stored nid is not 17752c91f8fcSDavid Hildenbrand * reliable. However, such blocks are always online (e.g., cannot get 17762c91f8fcSDavid Hildenbrand * offlined) and, therefore, are still spanned by the node. 17772c91f8fcSDavid Hildenbrand */ 17782c91f8fcSDavid Hildenbrand return mem->nid == nid ? -EEXIST : 0; 17792c91f8fcSDavid Hildenbrand } 17802c91f8fcSDavid Hildenbrand 17810f1cfe9dSToshi Kani /** 17820f1cfe9dSToshi Kani * try_offline_node 1783e8b098fcSMike Rapoport * @nid: the node ID 17840f1cfe9dSToshi Kani * 17850f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 17860f1cfe9dSToshi Kani * 17870f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 17880f1cfe9dSToshi Kani * and online/offline operations before this call. 17890f1cfe9dSToshi Kani */ 179090b30cdcSWen Congyang void try_offline_node(int nid) 179160a5a19eSTang Chen { 1792d822b86aSWen Congyang pg_data_t *pgdat = NODE_DATA(nid); 17932c91f8fcSDavid Hildenbrand int rc; 179460a5a19eSTang Chen 179560a5a19eSTang Chen /* 17962c91f8fcSDavid Hildenbrand * If the node still spans pages (especially ZONE_DEVICE), don't 17972c91f8fcSDavid Hildenbrand * offline it. A node spans memory after move_pfn_range_to_zone(), 17982c91f8fcSDavid Hildenbrand * e.g., after the memory block was onlined. 179960a5a19eSTang Chen */ 18002c91f8fcSDavid Hildenbrand if (pgdat->node_spanned_pages) 180160a5a19eSTang Chen return; 18022c91f8fcSDavid Hildenbrand 18032c91f8fcSDavid Hildenbrand /* 18042c91f8fcSDavid Hildenbrand * Especially offline memory blocks might not be spanned by the 18052c91f8fcSDavid Hildenbrand * node. They will get spanned by the node once they get onlined. 18062c91f8fcSDavid Hildenbrand * However, they link to the node in sysfs and can get onlined later. 18072c91f8fcSDavid Hildenbrand */ 18082c91f8fcSDavid Hildenbrand rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb); 18092c91f8fcSDavid Hildenbrand if (rc) 18102c91f8fcSDavid Hildenbrand return; 181160a5a19eSTang Chen 181246a3679bSMichal Hocko if (check_cpu_on_node(pgdat)) 181360a5a19eSTang Chen return; 181460a5a19eSTang Chen 181560a5a19eSTang Chen /* 181660a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 181760a5a19eSTang Chen * node now. 181860a5a19eSTang Chen */ 181960a5a19eSTang Chen node_set_offline(nid); 182060a5a19eSTang Chen unregister_one_node(nid); 182160a5a19eSTang Chen } 182290b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 182360a5a19eSTang Chen 1824eca499abSPavel Tatashin static int __ref try_remove_memory(int nid, u64 start, u64 size) 1825bbc76be6SWen Congyang { 1826eca499abSPavel Tatashin int rc = 0; 1827993c1aadSWen Congyang 182827356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 182927356f54SToshi Kani 18306677e3eaSYasuaki Ishimatsu /* 1831242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 1832eca499abSPavel Tatashin * whether all memory blocks in question are offline and return error 1833242831ebSRafael J. Wysocki * if this is not the case. 18346677e3eaSYasuaki Ishimatsu */ 1835fbcf73ceSDavid Hildenbrand rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb); 1836eca499abSPavel Tatashin if (rc) 1837b4223a51SJia He return rc; 18386677e3eaSYasuaki Ishimatsu 183946c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 184046c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 184146c66c4bSYasuaki Ishimatsu 1842f1037ec0SDan Williams /* 1843f1037ec0SDan Williams * Memory block device removal under the device_hotplug_lock is 1844f1037ec0SDan Williams * a barrier against racing online attempts. 1845f1037ec0SDan Williams */ 18464c4b7f9bSDavid Hildenbrand remove_memory_block_devices(start, size); 18474c4b7f9bSDavid Hildenbrand 1848f1037ec0SDan Williams mem_hotplug_begin(); 1849f1037ec0SDan Williams 18502c2a5af6SOscar Salvador arch_remove_memory(nid, start, size, NULL); 185152219aeaSDavid Hildenbrand 185252219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 185332d1fe8fSAnshuman Khandual memblock_free(start, size); 185432d1fe8fSAnshuman Khandual memblock_remove(start, size); 185552219aeaSDavid Hildenbrand } 185652219aeaSDavid Hildenbrand 1857cb8e3c8bSDavid Hildenbrand release_mem_region_adjustable(start, size); 185824d335caSWen Congyang 185960a5a19eSTang Chen try_offline_node(nid); 186060a5a19eSTang Chen 1861bfc8c901SVladimir Davydov mem_hotplug_done(); 1862b4223a51SJia He return 0; 186371088785SBadari Pulavarty } 1864d15e5926SDavid Hildenbrand 1865eca499abSPavel Tatashin /** 1866eca499abSPavel Tatashin * remove_memory 1867eca499abSPavel Tatashin * @nid: the node ID 1868eca499abSPavel Tatashin * @start: physical address of the region to remove 1869eca499abSPavel Tatashin * @size: size of the region to remove 1870eca499abSPavel Tatashin * 1871eca499abSPavel Tatashin * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 1872eca499abSPavel Tatashin * and online/offline operations before this call, as required by 1873eca499abSPavel Tatashin * try_offline_node(). 1874eca499abSPavel Tatashin */ 1875eca499abSPavel Tatashin void __remove_memory(int nid, u64 start, u64 size) 1876d15e5926SDavid Hildenbrand { 1877eca499abSPavel Tatashin 1878eca499abSPavel Tatashin /* 187929a90db9SSouptick Joarder * trigger BUG() if some memory is not offlined prior to calling this 1880eca499abSPavel Tatashin * function 1881eca499abSPavel Tatashin */ 1882eca499abSPavel Tatashin if (try_remove_memory(nid, start, size)) 1883eca499abSPavel Tatashin BUG(); 1884eca499abSPavel Tatashin } 1885eca499abSPavel Tatashin 1886eca499abSPavel Tatashin /* 1887eca499abSPavel Tatashin * Remove memory if every memory block is offline, otherwise return -EBUSY is 1888eca499abSPavel Tatashin * some memory is not offline 1889eca499abSPavel Tatashin */ 1890eca499abSPavel Tatashin int remove_memory(int nid, u64 start, u64 size) 1891eca499abSPavel Tatashin { 1892eca499abSPavel Tatashin int rc; 1893eca499abSPavel Tatashin 1894d15e5926SDavid Hildenbrand lock_device_hotplug(); 1895eca499abSPavel Tatashin rc = try_remove_memory(nid, start, size); 1896d15e5926SDavid Hildenbrand unlock_device_hotplug(); 1897eca499abSPavel Tatashin 1898eca499abSPavel Tatashin return rc; 1899d15e5926SDavid Hildenbrand } 190071088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 190108b3acd7SDavid Hildenbrand 19028dc4bb58SDavid Hildenbrand static int try_offline_memory_block(struct memory_block *mem, void *arg) 19038dc4bb58SDavid Hildenbrand { 19048dc4bb58SDavid Hildenbrand uint8_t online_type = MMOP_ONLINE_KERNEL; 19058dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 19068dc4bb58SDavid Hildenbrand struct page *page; 19078dc4bb58SDavid Hildenbrand int rc; 19088dc4bb58SDavid Hildenbrand 190908b3acd7SDavid Hildenbrand /* 19108dc4bb58SDavid Hildenbrand * Sense the online_type via the zone of the memory block. Offlining 19118dc4bb58SDavid Hildenbrand * with multiple zones within one memory block will be rejected 19128dc4bb58SDavid Hildenbrand * by offlining code ... so we don't care about that. 19138dc4bb58SDavid Hildenbrand */ 19148dc4bb58SDavid Hildenbrand page = pfn_to_online_page(section_nr_to_pfn(mem->start_section_nr)); 19158dc4bb58SDavid Hildenbrand if (page && zone_idx(page_zone(page)) == ZONE_MOVABLE) 19168dc4bb58SDavid Hildenbrand online_type = MMOP_ONLINE_MOVABLE; 19178dc4bb58SDavid Hildenbrand 19188dc4bb58SDavid Hildenbrand rc = device_offline(&mem->dev); 19198dc4bb58SDavid Hildenbrand /* 19208dc4bb58SDavid Hildenbrand * Default is MMOP_OFFLINE - change it only if offlining succeeded, 19218dc4bb58SDavid Hildenbrand * so try_reonline_memory_block() can do the right thing. 19228dc4bb58SDavid Hildenbrand */ 19238dc4bb58SDavid Hildenbrand if (!rc) 19248dc4bb58SDavid Hildenbrand **online_types = online_type; 19258dc4bb58SDavid Hildenbrand 19268dc4bb58SDavid Hildenbrand (*online_types)++; 19278dc4bb58SDavid Hildenbrand /* Ignore if already offline. */ 19288dc4bb58SDavid Hildenbrand return rc < 0 ? rc : 0; 19298dc4bb58SDavid Hildenbrand } 19308dc4bb58SDavid Hildenbrand 19318dc4bb58SDavid Hildenbrand static int try_reonline_memory_block(struct memory_block *mem, void *arg) 19328dc4bb58SDavid Hildenbrand { 19338dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 19348dc4bb58SDavid Hildenbrand int rc; 19358dc4bb58SDavid Hildenbrand 19368dc4bb58SDavid Hildenbrand if (**online_types != MMOP_OFFLINE) { 19378dc4bb58SDavid Hildenbrand mem->online_type = **online_types; 19388dc4bb58SDavid Hildenbrand rc = device_online(&mem->dev); 19398dc4bb58SDavid Hildenbrand if (rc < 0) 19408dc4bb58SDavid Hildenbrand pr_warn("%s: Failed to re-online memory: %d", 19418dc4bb58SDavid Hildenbrand __func__, rc); 19428dc4bb58SDavid Hildenbrand } 19438dc4bb58SDavid Hildenbrand 19448dc4bb58SDavid Hildenbrand /* Continue processing all remaining memory blocks. */ 19458dc4bb58SDavid Hildenbrand (*online_types)++; 19468dc4bb58SDavid Hildenbrand return 0; 19478dc4bb58SDavid Hildenbrand } 19488dc4bb58SDavid Hildenbrand 19498dc4bb58SDavid Hildenbrand /* 19508dc4bb58SDavid Hildenbrand * Try to offline and remove memory. Might take a long time to finish in case 19518dc4bb58SDavid Hildenbrand * memory is still in use. Primarily useful for memory devices that logically 19528dc4bb58SDavid Hildenbrand * unplugged all memory (so it's no longer in use) and want to offline + remove 19538dc4bb58SDavid Hildenbrand * that memory. 195408b3acd7SDavid Hildenbrand */ 195508b3acd7SDavid Hildenbrand int offline_and_remove_memory(int nid, u64 start, u64 size) 195608b3acd7SDavid Hildenbrand { 19578dc4bb58SDavid Hildenbrand const unsigned long mb_count = size / memory_block_size_bytes(); 19588dc4bb58SDavid Hildenbrand uint8_t *online_types, *tmp; 19598dc4bb58SDavid Hildenbrand int rc; 196008b3acd7SDavid Hildenbrand 196108b3acd7SDavid Hildenbrand if (!IS_ALIGNED(start, memory_block_size_bytes()) || 19628dc4bb58SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes()) || !size) 19638dc4bb58SDavid Hildenbrand return -EINVAL; 196408b3acd7SDavid Hildenbrand 196508b3acd7SDavid Hildenbrand /* 19668dc4bb58SDavid Hildenbrand * We'll remember the old online type of each memory block, so we can 19678dc4bb58SDavid Hildenbrand * try to revert whatever we did when offlining one memory block fails 19688dc4bb58SDavid Hildenbrand * after offlining some others succeeded. 19698dc4bb58SDavid Hildenbrand */ 19708dc4bb58SDavid Hildenbrand online_types = kmalloc_array(mb_count, sizeof(*online_types), 19718dc4bb58SDavid Hildenbrand GFP_KERNEL); 19728dc4bb58SDavid Hildenbrand if (!online_types) 19738dc4bb58SDavid Hildenbrand return -ENOMEM; 19748dc4bb58SDavid Hildenbrand /* 19758dc4bb58SDavid Hildenbrand * Initialize all states to MMOP_OFFLINE, so when we abort processing in 19768dc4bb58SDavid Hildenbrand * try_offline_memory_block(), we'll skip all unprocessed blocks in 19778dc4bb58SDavid Hildenbrand * try_reonline_memory_block(). 19788dc4bb58SDavid Hildenbrand */ 19798dc4bb58SDavid Hildenbrand memset(online_types, MMOP_OFFLINE, mb_count); 19808dc4bb58SDavid Hildenbrand 19818dc4bb58SDavid Hildenbrand lock_device_hotplug(); 19828dc4bb58SDavid Hildenbrand 19838dc4bb58SDavid Hildenbrand tmp = online_types; 19848dc4bb58SDavid Hildenbrand rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block); 19858dc4bb58SDavid Hildenbrand 19868dc4bb58SDavid Hildenbrand /* 19878dc4bb58SDavid Hildenbrand * In case we succeeded to offline all memory, remove it. 198808b3acd7SDavid Hildenbrand * This cannot fail as it cannot get onlined in the meantime. 198908b3acd7SDavid Hildenbrand */ 199008b3acd7SDavid Hildenbrand if (!rc) { 199108b3acd7SDavid Hildenbrand rc = try_remove_memory(nid, start, size); 19928dc4bb58SDavid Hildenbrand if (rc) 19938dc4bb58SDavid Hildenbrand pr_err("%s: Failed to remove memory: %d", __func__, rc); 19948dc4bb58SDavid Hildenbrand } 19958dc4bb58SDavid Hildenbrand 19968dc4bb58SDavid Hildenbrand /* 19978dc4bb58SDavid Hildenbrand * Rollback what we did. While memory onlining might theoretically fail 19988dc4bb58SDavid Hildenbrand * (nacked by a notifier), it barely ever happens. 19998dc4bb58SDavid Hildenbrand */ 20008dc4bb58SDavid Hildenbrand if (rc) { 20018dc4bb58SDavid Hildenbrand tmp = online_types; 20028dc4bb58SDavid Hildenbrand walk_memory_blocks(start, size, &tmp, 20038dc4bb58SDavid Hildenbrand try_reonline_memory_block); 200408b3acd7SDavid Hildenbrand } 200508b3acd7SDavid Hildenbrand unlock_device_hotplug(); 200608b3acd7SDavid Hildenbrand 20078dc4bb58SDavid Hildenbrand kfree(online_types); 200808b3acd7SDavid Hildenbrand return rc; 200908b3acd7SDavid Hildenbrand } 201008b3acd7SDavid Hildenbrand EXPORT_SYMBOL_GPL(offline_and_remove_memory); 2011aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 2012