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/vmalloc.h> 250a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 260c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 270c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 280c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 2971088785SBadari Pulavarty #include <linux/pfn.h> 306ad696d2SAndi Kleen #include <linux/suspend.h> 316d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 32d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h> 3360a5a19eSTang Chen #include <linux/stop_machine.h> 34c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h> 35c5320926STang Chen #include <linux/memblock.h> 36698b1b30SVlastimil Babka #include <linux/compaction.h> 37b15c8726SMichal Hocko #include <linux/rmap.h> 388581fd40SJakub Kicinski #include <linux/module.h> 393947be19SDave Hansen 403947be19SDave Hansen #include <asm/tlbflush.h> 413947be19SDave Hansen 421e5ad9a3SAdrian Bunk #include "internal.h" 43e900a918SDan Williams #include "shuffle.h" 441e5ad9a3SAdrian Bunk 45e3a9d9fcSOscar Salvador 46e3a9d9fcSOscar Salvador /* 47e3a9d9fcSOscar Salvador * memory_hotplug.memmap_on_memory parameter 48e3a9d9fcSOscar Salvador */ 49e3a9d9fcSOscar Salvador static bool memmap_on_memory __ro_after_init; 50e3a9d9fcSOscar Salvador #ifdef CONFIG_MHP_MEMMAP_ON_MEMORY 51e3a9d9fcSOscar Salvador module_param(memmap_on_memory, bool, 0444); 52e3a9d9fcSOscar Salvador MODULE_PARM_DESC(memmap_on_memory, "Enable memmap on memory for memory hotplug"); 53e3a9d9fcSOscar Salvador #endif 54a08a2ae3SOscar Salvador 55e83a437fSDavid Hildenbrand enum { 56e83a437fSDavid Hildenbrand ONLINE_POLICY_CONTIG_ZONES = 0, 57e83a437fSDavid Hildenbrand ONLINE_POLICY_AUTO_MOVABLE, 58e83a437fSDavid Hildenbrand }; 59e83a437fSDavid Hildenbrand 60ac62554bSTang Yizhou static const char * const online_policy_to_str[] = { 61e83a437fSDavid Hildenbrand [ONLINE_POLICY_CONTIG_ZONES] = "contig-zones", 62e83a437fSDavid Hildenbrand [ONLINE_POLICY_AUTO_MOVABLE] = "auto-movable", 63e83a437fSDavid Hildenbrand }; 64e83a437fSDavid Hildenbrand 65e83a437fSDavid Hildenbrand static int set_online_policy(const char *val, const struct kernel_param *kp) 66e83a437fSDavid Hildenbrand { 67e83a437fSDavid Hildenbrand int ret = sysfs_match_string(online_policy_to_str, val); 68e83a437fSDavid Hildenbrand 69e83a437fSDavid Hildenbrand if (ret < 0) 70e83a437fSDavid Hildenbrand return ret; 71e83a437fSDavid Hildenbrand *((int *)kp->arg) = ret; 72e83a437fSDavid Hildenbrand return 0; 73e83a437fSDavid Hildenbrand } 74e83a437fSDavid Hildenbrand 75e83a437fSDavid Hildenbrand static int get_online_policy(char *buffer, const struct kernel_param *kp) 76e83a437fSDavid Hildenbrand { 77e83a437fSDavid Hildenbrand return sprintf(buffer, "%s\n", online_policy_to_str[*((int *)kp->arg)]); 78e83a437fSDavid Hildenbrand } 79e83a437fSDavid Hildenbrand 80e83a437fSDavid Hildenbrand /* 81e83a437fSDavid Hildenbrand * memory_hotplug.online_policy: configure online behavior when onlining without 82e83a437fSDavid Hildenbrand * specifying a zone (MMOP_ONLINE) 83e83a437fSDavid Hildenbrand * 84e83a437fSDavid Hildenbrand * "contig-zones": keep zone contiguous 85e83a437fSDavid Hildenbrand * "auto-movable": online memory to ZONE_MOVABLE if the configuration 86e83a437fSDavid Hildenbrand * (auto_movable_ratio, auto_movable_numa_aware) allows for it 87e83a437fSDavid Hildenbrand */ 88e83a437fSDavid Hildenbrand static int online_policy __read_mostly = ONLINE_POLICY_CONTIG_ZONES; 89e83a437fSDavid Hildenbrand static const struct kernel_param_ops online_policy_ops = { 90e83a437fSDavid Hildenbrand .set = set_online_policy, 91e83a437fSDavid Hildenbrand .get = get_online_policy, 92e83a437fSDavid Hildenbrand }; 93e83a437fSDavid Hildenbrand module_param_cb(online_policy, &online_policy_ops, &online_policy, 0644); 94e83a437fSDavid Hildenbrand MODULE_PARM_DESC(online_policy, 95e83a437fSDavid Hildenbrand "Set the online policy (\"contig-zones\", \"auto-movable\") " 96e83a437fSDavid Hildenbrand "Default: \"contig-zones\""); 97e83a437fSDavid Hildenbrand 98e83a437fSDavid Hildenbrand /* 99e83a437fSDavid Hildenbrand * memory_hotplug.auto_movable_ratio: specify maximum MOVABLE:KERNEL ratio 100e83a437fSDavid Hildenbrand * 101e83a437fSDavid Hildenbrand * The ratio represent an upper limit and the kernel might decide to not 102e83a437fSDavid Hildenbrand * online some memory to ZONE_MOVABLE -- e.g., because hotplugged KERNEL memory 103e83a437fSDavid Hildenbrand * doesn't allow for more MOVABLE memory. 104e83a437fSDavid Hildenbrand */ 105e83a437fSDavid Hildenbrand static unsigned int auto_movable_ratio __read_mostly = 301; 106e83a437fSDavid Hildenbrand module_param(auto_movable_ratio, uint, 0644); 107e83a437fSDavid Hildenbrand MODULE_PARM_DESC(auto_movable_ratio, 108e83a437fSDavid Hildenbrand "Set the maximum ratio of MOVABLE:KERNEL memory in the system " 109e83a437fSDavid Hildenbrand "in percent for \"auto-movable\" online policy. Default: 301"); 110e83a437fSDavid Hildenbrand 111e83a437fSDavid Hildenbrand /* 112e83a437fSDavid Hildenbrand * memory_hotplug.auto_movable_numa_aware: consider numa node stats 113e83a437fSDavid Hildenbrand */ 114e83a437fSDavid Hildenbrand #ifdef CONFIG_NUMA 115e83a437fSDavid Hildenbrand static bool auto_movable_numa_aware __read_mostly = true; 116e83a437fSDavid Hildenbrand module_param(auto_movable_numa_aware, bool, 0644); 117e83a437fSDavid Hildenbrand MODULE_PARM_DESC(auto_movable_numa_aware, 118e83a437fSDavid Hildenbrand "Consider numa node stats in addition to global stats in " 119e83a437fSDavid Hildenbrand "\"auto-movable\" online policy. Default: true"); 120e83a437fSDavid Hildenbrand #endif /* CONFIG_NUMA */ 121e83a437fSDavid Hildenbrand 1229d0ad8caSDaniel Kiper /* 1239d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 1249d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 1259d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 1269d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 1279d0ad8caSDaniel Kiper */ 1289d0ad8caSDaniel Kiper 1299d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 130bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 1319d0ad8caSDaniel Kiper 1323f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 13320d6c96bSKOSAKI Motohiro 1343f906ba2SThomas Gleixner void get_online_mems(void) 1353f906ba2SThomas Gleixner { 1363f906ba2SThomas Gleixner percpu_down_read(&mem_hotplug_lock); 1373f906ba2SThomas Gleixner } 138bfc8c901SVladimir Davydov 1393f906ba2SThomas Gleixner void put_online_mems(void) 1403f906ba2SThomas Gleixner { 1413f906ba2SThomas Gleixner percpu_up_read(&mem_hotplug_lock); 1423f906ba2SThomas Gleixner } 143bfc8c901SVladimir Davydov 1444932381eSMichal Hocko bool movable_node_enabled = false; 1454932381eSMichal Hocko 1468604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 1471adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_OFFLINE; 1488604d9e5SVitaly Kuznetsov #else 1491adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_ONLINE; 1508604d9e5SVitaly Kuznetsov #endif 15131bc3858SVitaly Kuznetsov 15286dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str) 15386dd995dSVitaly Kuznetsov { 1541adf8b46SAnshuman Khandual const int online_type = mhp_online_type_from_str(str); 1555f47adf7SDavid Hildenbrand 1565f47adf7SDavid Hildenbrand if (online_type >= 0) 1571adf8b46SAnshuman Khandual mhp_default_online_type = online_type; 15886dd995dSVitaly Kuznetsov 15986dd995dSVitaly Kuznetsov return 1; 16086dd995dSVitaly Kuznetsov } 16186dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state); 16286dd995dSVitaly Kuznetsov 16330467e0bSDavid Rientjes void mem_hotplug_begin(void) 164bfc8c901SVladimir Davydov { 1653f906ba2SThomas Gleixner cpus_read_lock(); 1663f906ba2SThomas Gleixner percpu_down_write(&mem_hotplug_lock); 167bfc8c901SVladimir Davydov } 168bfc8c901SVladimir Davydov 16930467e0bSDavid Rientjes void mem_hotplug_done(void) 170bfc8c901SVladimir Davydov { 1713f906ba2SThomas Gleixner percpu_up_write(&mem_hotplug_lock); 1723f906ba2SThomas Gleixner cpus_read_unlock(); 173bfc8c901SVladimir Davydov } 17420d6c96bSKOSAKI Motohiro 175357b4da5SJuergen Gross u64 max_mem_size = U64_MAX; 176357b4da5SJuergen Gross 17745e0b78bSKeith Mannthey /* add this memory to iomem resource */ 1787b7b2721SDavid Hildenbrand static struct resource *register_memory_resource(u64 start, u64 size, 1797b7b2721SDavid Hildenbrand const char *resource_name) 18045e0b78bSKeith Mannthey { 1812794129eSDave Hansen struct resource *res; 1822794129eSDave Hansen unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1837b7b2721SDavid Hildenbrand 1847b7b2721SDavid Hildenbrand if (strcmp(resource_name, "System RAM")) 1857cf603d1SDavid Hildenbrand flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED; 186357b4da5SJuergen Gross 187bca3feaaSAnshuman Khandual if (!mhp_range_allowed(start, size, true)) 188bca3feaaSAnshuman Khandual return ERR_PTR(-E2BIG); 189bca3feaaSAnshuman Khandual 190f3cd4c86SBaoquan He /* 191f3cd4c86SBaoquan He * Make sure value parsed from 'mem=' only restricts memory adding 192f3cd4c86SBaoquan He * while booting, so that memory hotplug won't be impacted. Please 193f3cd4c86SBaoquan He * refer to document of 'mem=' in kernel-parameters.txt for more 194f3cd4c86SBaoquan He * details. 195f3cd4c86SBaoquan He */ 196f3cd4c86SBaoquan He if (start + size > max_mem_size && system_state < SYSTEM_RUNNING) 197357b4da5SJuergen Gross return ERR_PTR(-E2BIG); 198357b4da5SJuergen Gross 1992794129eSDave Hansen /* 2002794129eSDave Hansen * Request ownership of the new memory range. This might be 2012794129eSDave Hansen * a child of an existing resource that was present but 2022794129eSDave Hansen * not marked as busy. 2032794129eSDave Hansen */ 2042794129eSDave Hansen res = __request_region(&iomem_resource, start, size, 2052794129eSDave Hansen resource_name, flags); 20645e0b78bSKeith Mannthey 2072794129eSDave Hansen if (!res) { 2082794129eSDave Hansen pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", 2092794129eSDave Hansen start, start + size); 2106f754ba4SVitaly Kuznetsov return ERR_PTR(-EEXIST); 21145e0b78bSKeith Mannthey } 21245e0b78bSKeith Mannthey return res; 21345e0b78bSKeith Mannthey } 21445e0b78bSKeith Mannthey 21545e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 21645e0b78bSKeith Mannthey { 21745e0b78bSKeith Mannthey if (!res) 21845e0b78bSKeith Mannthey return; 21945e0b78bSKeith Mannthey release_resource(res); 22045e0b78bSKeith Mannthey kfree(res); 22145e0b78bSKeith Mannthey } 22245e0b78bSKeith Mannthey 2237ea62160SDan Williams static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, 2247ea62160SDan Williams const char *reason) 2257ea62160SDan Williams { 2267ea62160SDan Williams /* 2277ea62160SDan Williams * Disallow all operations smaller than a sub-section and only 2287ea62160SDan Williams * allow operations smaller than a section for 2297ea62160SDan Williams * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range() 2307ea62160SDan Williams * enforces a larger memory_block_size_bytes() granularity for 2317ea62160SDan Williams * memory that will be marked online, so this check should only 2327ea62160SDan Williams * fire for direct arch_{add,remove}_memory() users outside of 2337ea62160SDan Williams * add_memory_resource(). 2347ea62160SDan Williams */ 2357ea62160SDan Williams unsigned long min_align; 2367ea62160SDan Williams 2377ea62160SDan Williams if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 2387ea62160SDan Williams min_align = PAGES_PER_SUBSECTION; 2397ea62160SDan Williams else 2407ea62160SDan Williams min_align = PAGES_PER_SECTION; 2417ea62160SDan Williams if (!IS_ALIGNED(pfn, min_align) 2427ea62160SDan Williams || !IS_ALIGNED(nr_pages, min_align)) { 2437ea62160SDan Williams WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n", 2447ea62160SDan Williams reason, pfn, pfn + nr_pages - 1); 2457ea62160SDan Williams return -EINVAL; 2467ea62160SDan Williams } 2477ea62160SDan Williams return 0; 2487ea62160SDan Williams } 2497ea62160SDan Williams 2504edd7cefSDavid Rientjes /* 2519f605f26SDan Williams * Return page for the valid pfn only if the page is online. All pfn 2529f605f26SDan Williams * walkers which rely on the fully initialized page->flags and others 2539f605f26SDan Williams * should use this rather than pfn_valid && pfn_to_page 2549f605f26SDan Williams */ 2559f605f26SDan Williams struct page *pfn_to_online_page(unsigned long pfn) 2569f605f26SDan Williams { 2579f605f26SDan Williams unsigned long nr = pfn_to_section_nr(pfn); 2581f90a347SDan Williams struct dev_pagemap *pgmap; 2599f9b02e5SDan Williams struct mem_section *ms; 2609f605f26SDan Williams 2619f9b02e5SDan Williams if (nr >= NR_MEM_SECTIONS) 2629f605f26SDan Williams return NULL; 2639f9b02e5SDan Williams 2649f9b02e5SDan Williams ms = __nr_to_section(nr); 2659f9b02e5SDan Williams if (!online_section(ms)) 2669f9b02e5SDan Williams return NULL; 2679f9b02e5SDan Williams 2689f9b02e5SDan Williams /* 2699f9b02e5SDan Williams * Save some code text when online_section() + 2709f9b02e5SDan Williams * pfn_section_valid() are sufficient. 2719f9b02e5SDan Williams */ 2729f9b02e5SDan Williams if (IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) && !pfn_valid(pfn)) 2739f9b02e5SDan Williams return NULL; 2749f9b02e5SDan Williams 2759f9b02e5SDan Williams if (!pfn_section_valid(ms, pfn)) 2769f9b02e5SDan Williams return NULL; 2779f9b02e5SDan Williams 2781f90a347SDan Williams if (!online_device_section(ms)) 2791f90a347SDan Williams return pfn_to_page(pfn); 2801f90a347SDan Williams 2811f90a347SDan Williams /* 2821f90a347SDan Williams * Slowpath: when ZONE_DEVICE collides with 2831f90a347SDan Williams * ZONE_{NORMAL,MOVABLE} within the same section some pfns in 2841f90a347SDan Williams * the section may be 'offline' but 'valid'. Only 2851f90a347SDan Williams * get_dev_pagemap() can determine sub-section online status. 2861f90a347SDan Williams */ 2871f90a347SDan Williams pgmap = get_dev_pagemap(pfn, NULL); 2881f90a347SDan Williams put_dev_pagemap(pgmap); 2891f90a347SDan Williams 2901f90a347SDan Williams /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */ 2911f90a347SDan Williams if (pgmap) 2921f90a347SDan Williams return NULL; 2931f90a347SDan Williams 2949f9b02e5SDan Williams return pfn_to_page(pfn); 2959f605f26SDan Williams } 2969f605f26SDan Williams EXPORT_SYMBOL_GPL(pfn_to_online_page); 2979f605f26SDan Williams 2989f605f26SDan Williams /* 2994edd7cefSDavid Rientjes * Reasonably generic function for adding memory. It is 3004edd7cefSDavid Rientjes * expected that archs that support memory hotplug will 3014edd7cefSDavid Rientjes * call this function after deciding the zone to which to 3024edd7cefSDavid Rientjes * add the new pages. 3034edd7cefSDavid Rientjes */ 3047ea62160SDan Williams int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, 305f5637d3bSLogan Gunthorpe struct mhp_params *params) 3064edd7cefSDavid Rientjes { 3076cdd0b30SDavid Hildenbrand const unsigned long end_pfn = pfn + nr_pages; 3086cdd0b30SDavid Hildenbrand unsigned long cur_nr_pages; 3099a845030SDan Williams int err; 310f5637d3bSLogan Gunthorpe struct vmem_altmap *altmap = params->altmap; 3114b94ffdcSDan Williams 312bfeb022fSLogan Gunthorpe if (WARN_ON_ONCE(!params->pgprot.pgprot)) 313bfeb022fSLogan Gunthorpe return -EINVAL; 314bfeb022fSLogan Gunthorpe 315bca3feaaSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false)); 316dca4436dSAlastair D'Silva 3174b94ffdcSDan Williams if (altmap) { 3184b94ffdcSDan Williams /* 3194b94ffdcSDan Williams * Validate altmap is within bounds of the total request 3204b94ffdcSDan Williams */ 3217ea62160SDan Williams if (altmap->base_pfn != pfn 3224b94ffdcSDan Williams || vmem_altmap_offset(altmap) > nr_pages) { 3234b94ffdcSDan Williams pr_warn_once("memory add fail, invalid altmap\n"); 3247ea62160SDan Williams return -EINVAL; 3254b94ffdcSDan Williams } 3264b94ffdcSDan Williams altmap->alloc = 0; 3274b94ffdcSDan Williams } 3284b94ffdcSDan Williams 3297ea62160SDan Williams err = check_pfn_span(pfn, nr_pages, "add"); 3307ea62160SDan Williams if (err) 3317ea62160SDan Williams return err; 3327ea62160SDan Williams 3336cdd0b30SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 3346cdd0b30SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 3356cdd0b30SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 3366cdd0b30SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 3376cdd0b30SDavid Hildenbrand err = sparse_add_section(nid, pfn, cur_nr_pages, altmap); 338ba72b4c8SDan Williams if (err) 339ba72b4c8SDan Williams break; 340f64ac5e6SMichal Hocko cond_resched(); 3414edd7cefSDavid Rientjes } 342c435a390SZhu Guihua vmemmap_populate_print_last(); 3434edd7cefSDavid Rientjes return err; 3444edd7cefSDavid Rientjes } 3454edd7cefSDavid Rientjes 346815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 347d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, 348815121d2SYasuaki Ishimatsu unsigned long start_pfn, 349815121d2SYasuaki Ishimatsu unsigned long end_pfn) 350815121d2SYasuaki Ishimatsu { 35149ba3c6bSDan Williams for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) { 3527ce700bfSDavid Hildenbrand if (unlikely(!pfn_to_online_page(start_pfn))) 353815121d2SYasuaki Ishimatsu continue; 354815121d2SYasuaki Ishimatsu 355815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(start_pfn) != nid)) 356815121d2SYasuaki Ishimatsu continue; 357815121d2SYasuaki Ishimatsu 3589b05158fSDavid Hildenbrand if (zone != page_zone(pfn_to_page(start_pfn))) 359815121d2SYasuaki Ishimatsu continue; 360815121d2SYasuaki Ishimatsu 361815121d2SYasuaki Ishimatsu return start_pfn; 362815121d2SYasuaki Ishimatsu } 363815121d2SYasuaki Ishimatsu 364815121d2SYasuaki Ishimatsu return 0; 365815121d2SYasuaki Ishimatsu } 366815121d2SYasuaki Ishimatsu 367815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 368d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, 369815121d2SYasuaki Ishimatsu unsigned long start_pfn, 370815121d2SYasuaki Ishimatsu unsigned long end_pfn) 371815121d2SYasuaki Ishimatsu { 372815121d2SYasuaki Ishimatsu unsigned long pfn; 373815121d2SYasuaki Ishimatsu 374815121d2SYasuaki Ishimatsu /* pfn is the end pfn of a memory section. */ 375815121d2SYasuaki Ishimatsu pfn = end_pfn - 1; 37649ba3c6bSDan Williams for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) { 3777ce700bfSDavid Hildenbrand if (unlikely(!pfn_to_online_page(pfn))) 378815121d2SYasuaki Ishimatsu continue; 379815121d2SYasuaki Ishimatsu 380815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(pfn) != nid)) 381815121d2SYasuaki Ishimatsu continue; 382815121d2SYasuaki Ishimatsu 3839b05158fSDavid Hildenbrand if (zone != page_zone(pfn_to_page(pfn))) 384815121d2SYasuaki Ishimatsu continue; 385815121d2SYasuaki Ishimatsu 386815121d2SYasuaki Ishimatsu return pfn; 387815121d2SYasuaki Ishimatsu } 388815121d2SYasuaki Ishimatsu 389815121d2SYasuaki Ishimatsu return 0; 390815121d2SYasuaki Ishimatsu } 391815121d2SYasuaki Ishimatsu 392815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 393815121d2SYasuaki Ishimatsu unsigned long end_pfn) 394815121d2SYasuaki Ishimatsu { 395815121d2SYasuaki Ishimatsu unsigned long pfn; 396815121d2SYasuaki Ishimatsu int nid = zone_to_nid(zone); 397815121d2SYasuaki Ishimatsu 3985d12071cSDavid Hildenbrand if (zone->zone_start_pfn == start_pfn) { 399815121d2SYasuaki Ishimatsu /* 400815121d2SYasuaki Ishimatsu * If the section is smallest section in the zone, it need 401815121d2SYasuaki Ishimatsu * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 402815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 403815121d2SYasuaki Ishimatsu * for shrinking zone. 404815121d2SYasuaki Ishimatsu */ 405815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, zone, end_pfn, 4065d12071cSDavid Hildenbrand zone_end_pfn(zone)); 407815121d2SYasuaki Ishimatsu if (pfn) { 4085d12071cSDavid Hildenbrand zone->spanned_pages = zone_end_pfn(zone) - pfn; 409815121d2SYasuaki Ishimatsu zone->zone_start_pfn = pfn; 410950b68d9SDavid Hildenbrand } else { 411950b68d9SDavid Hildenbrand zone->zone_start_pfn = 0; 412950b68d9SDavid Hildenbrand zone->spanned_pages = 0; 413815121d2SYasuaki Ishimatsu } 4145d12071cSDavid Hildenbrand } else if (zone_end_pfn(zone) == end_pfn) { 415815121d2SYasuaki Ishimatsu /* 416815121d2SYasuaki Ishimatsu * If the section is biggest section in the zone, it need 417815121d2SYasuaki Ishimatsu * shrink zone->spanned_pages. 418815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 419815121d2SYasuaki Ishimatsu * shrinking zone. 420815121d2SYasuaki Ishimatsu */ 4215d12071cSDavid Hildenbrand pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn, 422815121d2SYasuaki Ishimatsu start_pfn); 423815121d2SYasuaki Ishimatsu if (pfn) 4245d12071cSDavid Hildenbrand zone->spanned_pages = pfn - zone->zone_start_pfn + 1; 425950b68d9SDavid Hildenbrand else { 426815121d2SYasuaki Ishimatsu zone->zone_start_pfn = 0; 427815121d2SYasuaki Ishimatsu zone->spanned_pages = 0; 428950b68d9SDavid Hildenbrand } 429950b68d9SDavid Hildenbrand } 430815121d2SYasuaki Ishimatsu } 431815121d2SYasuaki Ishimatsu 43200d6c019SDavid Hildenbrand static void update_pgdat_span(struct pglist_data *pgdat) 433815121d2SYasuaki Ishimatsu { 43400d6c019SDavid Hildenbrand unsigned long node_start_pfn = 0, node_end_pfn = 0; 43500d6c019SDavid Hildenbrand struct zone *zone; 436815121d2SYasuaki Ishimatsu 43700d6c019SDavid Hildenbrand for (zone = pgdat->node_zones; 43800d6c019SDavid Hildenbrand zone < pgdat->node_zones + MAX_NR_ZONES; zone++) { 4396c922cf7SMiaohe Lin unsigned long end_pfn = zone_end_pfn(zone); 44000d6c019SDavid Hildenbrand 44100d6c019SDavid Hildenbrand /* No need to lock the zones, they can't change. */ 442656d5711SDavid Hildenbrand if (!zone->spanned_pages) 443656d5711SDavid Hildenbrand continue; 444656d5711SDavid Hildenbrand if (!node_end_pfn) { 445656d5711SDavid Hildenbrand node_start_pfn = zone->zone_start_pfn; 4466c922cf7SMiaohe Lin node_end_pfn = end_pfn; 447656d5711SDavid Hildenbrand continue; 448656d5711SDavid Hildenbrand } 449656d5711SDavid Hildenbrand 4506c922cf7SMiaohe Lin if (end_pfn > node_end_pfn) 4516c922cf7SMiaohe Lin node_end_pfn = end_pfn; 45200d6c019SDavid Hildenbrand if (zone->zone_start_pfn < node_start_pfn) 45300d6c019SDavid Hildenbrand node_start_pfn = zone->zone_start_pfn; 454815121d2SYasuaki Ishimatsu } 455815121d2SYasuaki Ishimatsu 45600d6c019SDavid Hildenbrand pgdat->node_start_pfn = node_start_pfn; 45700d6c019SDavid Hildenbrand pgdat->node_spanned_pages = node_end_pfn - node_start_pfn; 458815121d2SYasuaki Ishimatsu } 459815121d2SYasuaki Ishimatsu 460feee6b29SDavid Hildenbrand void __ref remove_pfn_range_from_zone(struct zone *zone, 461feee6b29SDavid Hildenbrand unsigned long start_pfn, 4627ea62160SDan Williams unsigned long nr_pages) 463815121d2SYasuaki Ishimatsu { 464b7e3debdSBen Widawsky const unsigned long end_pfn = start_pfn + nr_pages; 465815121d2SYasuaki Ishimatsu struct pglist_data *pgdat = zone->zone_pgdat; 46627cacaadSOscar Salvador unsigned long pfn, cur_nr_pages; 467815121d2SYasuaki Ishimatsu 468d33695b1SDavid Hildenbrand /* Poison struct pages because they are now uninitialized again. */ 469b7e3debdSBen Widawsky for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) { 470b7e3debdSBen Widawsky cond_resched(); 471b7e3debdSBen Widawsky 472b7e3debdSBen Widawsky /* Select all remaining pages up to the next section boundary */ 473b7e3debdSBen Widawsky cur_nr_pages = 474b7e3debdSBen Widawsky min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn); 475b7e3debdSBen Widawsky page_init_poison(pfn_to_page(pfn), 476b7e3debdSBen Widawsky sizeof(struct page) * cur_nr_pages); 477b7e3debdSBen Widawsky } 478d33695b1SDavid Hildenbrand 4797ce700bfSDavid Hildenbrand /* 4807ce700bfSDavid Hildenbrand * Zone shrinking code cannot properly deal with ZONE_DEVICE. So 4817ce700bfSDavid Hildenbrand * we will not try to shrink the zones - which is okay as 4827ce700bfSDavid Hildenbrand * set_zone_contiguous() cannot deal with ZONE_DEVICE either way. 4837ce700bfSDavid Hildenbrand */ 4845ef5f810SMiaohe Lin if (zone_is_zone_device(zone)) 4857ce700bfSDavid Hildenbrand return; 4867ce700bfSDavid Hildenbrand 487feee6b29SDavid Hildenbrand clear_zone_contiguous(zone); 488feee6b29SDavid Hildenbrand 489815121d2SYasuaki Ishimatsu shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 49000d6c019SDavid Hildenbrand update_pgdat_span(pgdat); 491feee6b29SDavid Hildenbrand 492feee6b29SDavid Hildenbrand set_zone_contiguous(zone); 493815121d2SYasuaki Ishimatsu } 494815121d2SYasuaki Ishimatsu 495feee6b29SDavid Hildenbrand static void __remove_section(unsigned long pfn, unsigned long nr_pages, 496feee6b29SDavid Hildenbrand unsigned long map_offset, 4979d1d887dSDavid Hildenbrand struct vmem_altmap *altmap) 498ea01ea93SBadari Pulavarty { 49910404901Schenqiwu struct mem_section *ms = __pfn_to_section(pfn); 500ea01ea93SBadari Pulavarty 5019d1d887dSDavid Hildenbrand if (WARN_ON_ONCE(!valid_section(ms))) 5029d1d887dSDavid Hildenbrand return; 503ea01ea93SBadari Pulavarty 504ba72b4c8SDan Williams sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap); 505ea01ea93SBadari Pulavarty } 506ea01ea93SBadari Pulavarty 507ea01ea93SBadari Pulavarty /** 508feee6b29SDavid Hildenbrand * __remove_pages() - remove sections of pages 5097ea62160SDan Williams * @pfn: starting pageframe (must be aligned to start of a section) 510ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 511e8b098fcSMike Rapoport * @altmap: alternative device page map or %NULL if default memmap is used 512ea01ea93SBadari Pulavarty * 513ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 514ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 515ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 516ea01ea93SBadari Pulavarty * calling offline_pages(). 517ea01ea93SBadari Pulavarty */ 518feee6b29SDavid Hildenbrand void __remove_pages(unsigned long pfn, unsigned long nr_pages, 519feee6b29SDavid Hildenbrand struct vmem_altmap *altmap) 520ea01ea93SBadari Pulavarty { 52152fb87c8SDavid Hildenbrand const unsigned long end_pfn = pfn + nr_pages; 52252fb87c8SDavid Hildenbrand unsigned long cur_nr_pages; 5234b94ffdcSDan Williams unsigned long map_offset = 0; 5244b94ffdcSDan Williams 5254b94ffdcSDan Williams map_offset = vmem_altmap_offset(altmap); 526ea01ea93SBadari Pulavarty 5277ea62160SDan Williams if (check_pfn_span(pfn, nr_pages, "remove")) 5287ea62160SDan Williams return; 529ea01ea93SBadari Pulavarty 53052fb87c8SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 531dd33ad7bSMichal Hocko cond_resched(); 53252fb87c8SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 533a11b9419SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 534a11b9419SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 53552fb87c8SDavid Hildenbrand __remove_section(pfn, cur_nr_pages, map_offset, altmap); 5364b94ffdcSDan Williams map_offset = 0; 537ea01ea93SBadari Pulavarty } 538ea01ea93SBadari Pulavarty } 539ea01ea93SBadari Pulavarty 5409d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 5419d0ad8caSDaniel Kiper { 5429d0ad8caSDaniel Kiper int rc = -EINVAL; 5439d0ad8caSDaniel Kiper 544bfc8c901SVladimir Davydov get_online_mems(); 545bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5469d0ad8caSDaniel Kiper 5479d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 5489d0ad8caSDaniel Kiper online_page_callback = callback; 5499d0ad8caSDaniel Kiper rc = 0; 5509d0ad8caSDaniel Kiper } 5519d0ad8caSDaniel Kiper 552bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 553bfc8c901SVladimir Davydov put_online_mems(); 5549d0ad8caSDaniel Kiper 5559d0ad8caSDaniel Kiper return rc; 5569d0ad8caSDaniel Kiper } 5579d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 5589d0ad8caSDaniel Kiper 5599d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 5609d0ad8caSDaniel Kiper { 5619d0ad8caSDaniel Kiper int rc = -EINVAL; 5629d0ad8caSDaniel Kiper 563bfc8c901SVladimir Davydov get_online_mems(); 564bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5659d0ad8caSDaniel Kiper 5669d0ad8caSDaniel Kiper if (online_page_callback == callback) { 5679d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 5689d0ad8caSDaniel Kiper rc = 0; 5699d0ad8caSDaniel Kiper } 5709d0ad8caSDaniel Kiper 571bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 572bfc8c901SVladimir Davydov put_online_mems(); 5739d0ad8caSDaniel Kiper 5749d0ad8caSDaniel Kiper return rc; 5759d0ad8caSDaniel Kiper } 5769d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 5779d0ad8caSDaniel Kiper 57818db1491SDavid Hildenbrand void generic_online_page(struct page *page, unsigned int order) 5799d0ad8caSDaniel Kiper { 580c87cbc1fSVlastimil Babka /* 581c87cbc1fSVlastimil Babka * Freeing the page with debug_pagealloc enabled will try to unmap it, 582c87cbc1fSVlastimil Babka * so we should map it first. This is better than introducing a special 583c87cbc1fSVlastimil Babka * case in page freeing fast path. 584c87cbc1fSVlastimil Babka */ 58577bc7fd6SMike Rapoport debug_pagealloc_map_pages(page, 1 << order); 586a9cd410aSArun KS __free_pages_core(page, order); 587a9cd410aSArun KS totalram_pages_add(1UL << order); 588a9cd410aSArun KS } 58918db1491SDavid Hildenbrand EXPORT_SYMBOL_GPL(generic_online_page); 590a9cd410aSArun KS 591aac65321SDavid Hildenbrand static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages) 5923947be19SDave Hansen { 593b2c2ab20SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 594b2c2ab20SDavid Hildenbrand unsigned long pfn; 5952d070eabSMichal Hocko 596b2c2ab20SDavid Hildenbrand /* 597aac65321SDavid Hildenbrand * Online the pages in MAX_ORDER - 1 aligned chunks. The callback might 598aac65321SDavid Hildenbrand * decide to not expose all pages to the buddy (e.g., expose them 599aac65321SDavid Hildenbrand * later). We account all pages as being online and belonging to this 600aac65321SDavid Hildenbrand * zone ("present"). 601a08a2ae3SOscar Salvador * When using memmap_on_memory, the range might not be aligned to 602a08a2ae3SOscar Salvador * MAX_ORDER_NR_PAGES - 1, but pageblock aligned. __ffs() will detect 603a08a2ae3SOscar Salvador * this and the first chunk to online will be pageblock_nr_pages. 604b2c2ab20SDavid Hildenbrand */ 605a08a2ae3SOscar Salvador for (pfn = start_pfn; pfn < end_pfn;) { 606a08a2ae3SOscar Salvador int order = min(MAX_ORDER - 1UL, __ffs(pfn)); 607a08a2ae3SOscar Salvador 608a08a2ae3SOscar Salvador (*online_page_callback)(pfn_to_page(pfn), order); 609a08a2ae3SOscar Salvador pfn += (1UL << order); 610a08a2ae3SOscar Salvador } 6112d070eabSMichal Hocko 612b2c2ab20SDavid Hildenbrand /* mark all involved sections as online */ 613b2c2ab20SDavid Hildenbrand online_mem_sections(start_pfn, end_pfn); 61475884fb1SKAMEZAWA Hiroyuki } 61575884fb1SKAMEZAWA Hiroyuki 616d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 617d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 618d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 619d9713679SLai Jiangshan { 620d9713679SLai Jiangshan int nid = zone_to_nid(zone); 621d9713679SLai Jiangshan 62298fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 62398fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 6246715ddf9SLai Jiangshan 6256715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 626d9713679SLai Jiangshan arg->status_change_nid = nid; 6278efe33f4SOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 6288efe33f4SOscar Salvador arg->status_change_nid_normal = nid; 629d9713679SLai Jiangshan } 630d9713679SLai Jiangshan 631d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 632d9713679SLai Jiangshan { 633d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 634d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 635d9713679SLai Jiangshan 63683d83612SOscar Salvador if (arg->status_change_nid >= 0) 6376715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 638d9713679SLai Jiangshan } 639d9713679SLai Jiangshan 640f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 641f1dd2cd1SMichal Hocko unsigned long nr_pages) 642f1dd2cd1SMichal Hocko { 643f1dd2cd1SMichal Hocko unsigned long old_end_pfn = zone_end_pfn(zone); 644f1dd2cd1SMichal Hocko 645f1dd2cd1SMichal Hocko if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 646f1dd2cd1SMichal Hocko zone->zone_start_pfn = start_pfn; 647f1dd2cd1SMichal Hocko 648f1dd2cd1SMichal Hocko zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 649f1dd2cd1SMichal Hocko } 650f1dd2cd1SMichal Hocko 651f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 652f1dd2cd1SMichal Hocko unsigned long nr_pages) 653f1dd2cd1SMichal Hocko { 654f1dd2cd1SMichal Hocko unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 655f1dd2cd1SMichal Hocko 656f1dd2cd1SMichal Hocko if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 657f1dd2cd1SMichal Hocko pgdat->node_start_pfn = start_pfn; 658f1dd2cd1SMichal Hocko 659f1dd2cd1SMichal Hocko pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 660f1dd2cd1SMichal Hocko 6613fccb74cSDavid Hildenbrand } 6621f90a347SDan Williams 6631f90a347SDan Williams static void section_taint_zone_device(unsigned long pfn) 6641f90a347SDan Williams { 6651f90a347SDan Williams struct mem_section *ms = __pfn_to_section(pfn); 6661f90a347SDan Williams 6671f90a347SDan Williams ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE; 6681f90a347SDan Williams } 6691f90a347SDan Williams 6703fccb74cSDavid Hildenbrand /* 6713fccb74cSDavid Hildenbrand * Associate the pfn range with the given zone, initializing the memmaps 6723fccb74cSDavid Hildenbrand * and resizing the pgdat/zone data to span the added pages. After this 6733fccb74cSDavid Hildenbrand * call, all affected pages are PG_reserved. 674d882c006SDavid Hildenbrand * 675d882c006SDavid Hildenbrand * All aligned pageblocks are initialized to the specified migratetype 676d882c006SDavid Hildenbrand * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related 677d882c006SDavid Hildenbrand * zone stats (e.g., nr_isolate_pageblock) are touched. 6783fccb74cSDavid Hildenbrand */ 679a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 680d882c006SDavid Hildenbrand unsigned long nr_pages, 681d882c006SDavid Hildenbrand struct vmem_altmap *altmap, int migratetype) 682f1dd2cd1SMichal Hocko { 683f1dd2cd1SMichal Hocko struct pglist_data *pgdat = zone->zone_pgdat; 684f1dd2cd1SMichal Hocko int nid = pgdat->node_id; 685f1dd2cd1SMichal Hocko 686f1dd2cd1SMichal Hocko clear_zone_contiguous(zone); 687f1dd2cd1SMichal Hocko 688fa004ab7SWei Yang if (zone_is_empty(zone)) 689fa004ab7SWei Yang init_currently_empty_zone(zone, start_pfn, nr_pages); 690f1dd2cd1SMichal Hocko resize_zone_range(zone, start_pfn, nr_pages); 691f1dd2cd1SMichal Hocko resize_pgdat_range(pgdat, start_pfn, nr_pages); 692f1dd2cd1SMichal Hocko 693f1dd2cd1SMichal Hocko /* 6941f90a347SDan Williams * Subsection population requires care in pfn_to_online_page(). 6951f90a347SDan Williams * Set the taint to enable the slow path detection of 6961f90a347SDan Williams * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE} 6971f90a347SDan Williams * section. 6981f90a347SDan Williams */ 6991f90a347SDan Williams if (zone_is_zone_device(zone)) { 7001f90a347SDan Williams if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION)) 7011f90a347SDan Williams section_taint_zone_device(start_pfn); 7021f90a347SDan Williams if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)) 7031f90a347SDan Williams section_taint_zone_device(start_pfn + nr_pages); 7041f90a347SDan Williams } 7051f90a347SDan Williams 7061f90a347SDan Williams /* 707f1dd2cd1SMichal Hocko * TODO now we have a visible range of pages which are not associated 708f1dd2cd1SMichal Hocko * with their zone properly. Not nice but set_pfnblock_flags_mask 709f1dd2cd1SMichal Hocko * expects the zone spans the pfn range. All the pages in the range 710f1dd2cd1SMichal Hocko * are reserved so nobody should be touching them so we should be safe 711f1dd2cd1SMichal Hocko */ 712ab28cb6eSBaoquan He memmap_init_range(nr_pages, nid, zone_idx(zone), start_pfn, 0, 713d882c006SDavid Hildenbrand MEMINIT_HOTPLUG, altmap, migratetype); 714f1dd2cd1SMichal Hocko 715f1dd2cd1SMichal Hocko set_zone_contiguous(zone); 716f1dd2cd1SMichal Hocko } 717f1dd2cd1SMichal Hocko 718e83a437fSDavid Hildenbrand struct auto_movable_stats { 719e83a437fSDavid Hildenbrand unsigned long kernel_early_pages; 720e83a437fSDavid Hildenbrand unsigned long movable_pages; 721e83a437fSDavid Hildenbrand }; 722e83a437fSDavid Hildenbrand 723e83a437fSDavid Hildenbrand static void auto_movable_stats_account_zone(struct auto_movable_stats *stats, 724e83a437fSDavid Hildenbrand struct zone *zone) 725e83a437fSDavid Hildenbrand { 726e83a437fSDavid Hildenbrand if (zone_idx(zone) == ZONE_MOVABLE) { 727e83a437fSDavid Hildenbrand stats->movable_pages += zone->present_pages; 728e83a437fSDavid Hildenbrand } else { 729e83a437fSDavid Hildenbrand stats->kernel_early_pages += zone->present_early_pages; 730e83a437fSDavid Hildenbrand #ifdef CONFIG_CMA 731e83a437fSDavid Hildenbrand /* 732e83a437fSDavid Hildenbrand * CMA pages (never on hotplugged memory) behave like 733e83a437fSDavid Hildenbrand * ZONE_MOVABLE. 734e83a437fSDavid Hildenbrand */ 735e83a437fSDavid Hildenbrand stats->movable_pages += zone->cma_pages; 736e83a437fSDavid Hildenbrand stats->kernel_early_pages -= zone->cma_pages; 737e83a437fSDavid Hildenbrand #endif /* CONFIG_CMA */ 738e83a437fSDavid Hildenbrand } 739e83a437fSDavid Hildenbrand } 7403fcebf90SDavid Hildenbrand struct auto_movable_group_stats { 7413fcebf90SDavid Hildenbrand unsigned long movable_pages; 7423fcebf90SDavid Hildenbrand unsigned long req_kernel_early_pages; 7433fcebf90SDavid Hildenbrand }; 744e83a437fSDavid Hildenbrand 7453fcebf90SDavid Hildenbrand static int auto_movable_stats_account_group(struct memory_group *group, 7463fcebf90SDavid Hildenbrand void *arg) 747e83a437fSDavid Hildenbrand { 7483fcebf90SDavid Hildenbrand const int ratio = READ_ONCE(auto_movable_ratio); 7493fcebf90SDavid Hildenbrand struct auto_movable_group_stats *stats = arg; 7503fcebf90SDavid Hildenbrand long pages; 7513fcebf90SDavid Hildenbrand 7523fcebf90SDavid Hildenbrand /* 7533fcebf90SDavid Hildenbrand * We don't support modifying the config while the auto-movable online 7543fcebf90SDavid Hildenbrand * policy is already enabled. Just avoid the division by zero below. 7553fcebf90SDavid Hildenbrand */ 7563fcebf90SDavid Hildenbrand if (!ratio) 7573fcebf90SDavid Hildenbrand return 0; 7583fcebf90SDavid Hildenbrand 7593fcebf90SDavid Hildenbrand /* 7603fcebf90SDavid Hildenbrand * Calculate how many early kernel pages this group requires to 7613fcebf90SDavid Hildenbrand * satisfy the configured zone ratio. 7623fcebf90SDavid Hildenbrand */ 7633fcebf90SDavid Hildenbrand pages = group->present_movable_pages * 100 / ratio; 7643fcebf90SDavid Hildenbrand pages -= group->present_kernel_pages; 7653fcebf90SDavid Hildenbrand 7663fcebf90SDavid Hildenbrand if (pages > 0) 7673fcebf90SDavid Hildenbrand stats->req_kernel_early_pages += pages; 7683fcebf90SDavid Hildenbrand stats->movable_pages += group->present_movable_pages; 7693fcebf90SDavid Hildenbrand return 0; 7703fcebf90SDavid Hildenbrand } 7713fcebf90SDavid Hildenbrand 7723fcebf90SDavid Hildenbrand static bool auto_movable_can_online_movable(int nid, struct memory_group *group, 7733fcebf90SDavid Hildenbrand unsigned long nr_pages) 7743fcebf90SDavid Hildenbrand { 775e83a437fSDavid Hildenbrand unsigned long kernel_early_pages, movable_pages; 7763fcebf90SDavid Hildenbrand struct auto_movable_group_stats group_stats = {}; 7773fcebf90SDavid Hildenbrand struct auto_movable_stats stats = {}; 778e83a437fSDavid Hildenbrand pg_data_t *pgdat = NODE_DATA(nid); 779e83a437fSDavid Hildenbrand struct zone *zone; 780e83a437fSDavid Hildenbrand int i; 781e83a437fSDavid Hildenbrand 782e83a437fSDavid Hildenbrand /* Walk all relevant zones and collect MOVABLE vs. KERNEL stats. */ 783e83a437fSDavid Hildenbrand if (nid == NUMA_NO_NODE) { 784e83a437fSDavid Hildenbrand /* TODO: cache values */ 785e83a437fSDavid Hildenbrand for_each_populated_zone(zone) 786e83a437fSDavid Hildenbrand auto_movable_stats_account_zone(&stats, zone); 787e83a437fSDavid Hildenbrand } else { 788e83a437fSDavid Hildenbrand for (i = 0; i < MAX_NR_ZONES; i++) { 789e83a437fSDavid Hildenbrand zone = pgdat->node_zones + i; 790e83a437fSDavid Hildenbrand if (populated_zone(zone)) 791e83a437fSDavid Hildenbrand auto_movable_stats_account_zone(&stats, zone); 792e83a437fSDavid Hildenbrand } 793e83a437fSDavid Hildenbrand } 794e83a437fSDavid Hildenbrand 795e83a437fSDavid Hildenbrand kernel_early_pages = stats.kernel_early_pages; 796e83a437fSDavid Hildenbrand movable_pages = stats.movable_pages; 797e83a437fSDavid Hildenbrand 798e83a437fSDavid Hildenbrand /* 7993fcebf90SDavid Hildenbrand * Kernel memory inside dynamic memory group allows for more MOVABLE 8003fcebf90SDavid Hildenbrand * memory within the same group. Remove the effect of all but the 8013fcebf90SDavid Hildenbrand * current group from the stats. 8023fcebf90SDavid Hildenbrand */ 8033fcebf90SDavid Hildenbrand walk_dynamic_memory_groups(nid, auto_movable_stats_account_group, 8043fcebf90SDavid Hildenbrand group, &group_stats); 8053fcebf90SDavid Hildenbrand if (kernel_early_pages <= group_stats.req_kernel_early_pages) 8063fcebf90SDavid Hildenbrand return false; 8073fcebf90SDavid Hildenbrand kernel_early_pages -= group_stats.req_kernel_early_pages; 8083fcebf90SDavid Hildenbrand movable_pages -= group_stats.movable_pages; 8093fcebf90SDavid Hildenbrand 8103fcebf90SDavid Hildenbrand if (group && group->is_dynamic) 8113fcebf90SDavid Hildenbrand kernel_early_pages += group->present_kernel_pages; 8123fcebf90SDavid Hildenbrand 8133fcebf90SDavid Hildenbrand /* 814e83a437fSDavid Hildenbrand * Test if we could online the given number of pages to ZONE_MOVABLE 815e83a437fSDavid Hildenbrand * and still stay in the configured ratio. 816e83a437fSDavid Hildenbrand */ 817e83a437fSDavid Hildenbrand movable_pages += nr_pages; 818e83a437fSDavid Hildenbrand return movable_pages <= (auto_movable_ratio * kernel_early_pages) / 100; 819e83a437fSDavid Hildenbrand } 820e83a437fSDavid Hildenbrand 821f1dd2cd1SMichal Hocko /* 822c246a213SMichal Hocko * Returns a default kernel memory zone for the given pfn range. 823c246a213SMichal Hocko * If no kernel zone covers this pfn range it will automatically go 824c246a213SMichal Hocko * to the ZONE_NORMAL. 825c246a213SMichal Hocko */ 826c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 827c246a213SMichal Hocko unsigned long nr_pages) 828c246a213SMichal Hocko { 829c246a213SMichal Hocko struct pglist_data *pgdat = NODE_DATA(nid); 830c246a213SMichal Hocko int zid; 831c246a213SMichal Hocko 832c246a213SMichal Hocko for (zid = 0; zid <= ZONE_NORMAL; zid++) { 833c246a213SMichal Hocko struct zone *zone = &pgdat->node_zones[zid]; 834c246a213SMichal Hocko 835c246a213SMichal Hocko if (zone_intersects(zone, start_pfn, nr_pages)) 836c246a213SMichal Hocko return zone; 837c246a213SMichal Hocko } 838c246a213SMichal Hocko 839c246a213SMichal Hocko return &pgdat->node_zones[ZONE_NORMAL]; 840c246a213SMichal Hocko } 841c246a213SMichal Hocko 842e83a437fSDavid Hildenbrand /* 843e83a437fSDavid Hildenbrand * Determine to which zone to online memory dynamically based on user 844e83a437fSDavid Hildenbrand * configuration and system stats. We care about the following ratio: 845e83a437fSDavid Hildenbrand * 846e83a437fSDavid Hildenbrand * MOVABLE : KERNEL 847e83a437fSDavid Hildenbrand * 848e83a437fSDavid Hildenbrand * Whereby MOVABLE is memory in ZONE_MOVABLE and KERNEL is memory in 849e83a437fSDavid Hildenbrand * one of the kernel zones. CMA pages inside one of the kernel zones really 850e83a437fSDavid Hildenbrand * behaves like ZONE_MOVABLE, so we treat them accordingly. 851e83a437fSDavid Hildenbrand * 852e83a437fSDavid Hildenbrand * We don't allow for hotplugged memory in a KERNEL zone to increase the 853e83a437fSDavid Hildenbrand * amount of MOVABLE memory we can have, so we end up with: 854e83a437fSDavid Hildenbrand * 855e83a437fSDavid Hildenbrand * MOVABLE : KERNEL_EARLY 856e83a437fSDavid Hildenbrand * 857e83a437fSDavid Hildenbrand * Whereby KERNEL_EARLY is memory in one of the kernel zones, available sinze 858e83a437fSDavid Hildenbrand * boot. We base our calculation on KERNEL_EARLY internally, because: 859e83a437fSDavid Hildenbrand * 860e83a437fSDavid Hildenbrand * a) Hotplugged memory in one of the kernel zones can sometimes still get 861e83a437fSDavid Hildenbrand * hotunplugged, especially when hot(un)plugging individual memory blocks. 862e83a437fSDavid Hildenbrand * There is no coordination across memory devices, therefore "automatic" 863e83a437fSDavid Hildenbrand * hotunplugging, as implemented in hypervisors, could result in zone 864e83a437fSDavid Hildenbrand * imbalances. 865e83a437fSDavid Hildenbrand * b) Early/boot memory in one of the kernel zones can usually not get 866e83a437fSDavid Hildenbrand * hotunplugged again (e.g., no firmware interface to unplug, fragmented 867e83a437fSDavid Hildenbrand * with unmovable allocations). While there are corner cases where it might 868e83a437fSDavid Hildenbrand * still work, it is barely relevant in practice. 869e83a437fSDavid Hildenbrand * 8703fcebf90SDavid Hildenbrand * Exceptions are dynamic memory groups, which allow for more MOVABLE 8713fcebf90SDavid Hildenbrand * memory within the same memory group -- because in that case, there is 8723fcebf90SDavid Hildenbrand * coordination within the single memory device managed by a single driver. 8733fcebf90SDavid Hildenbrand * 874e83a437fSDavid Hildenbrand * We rely on "present pages" instead of "managed pages", as the latter is 875e83a437fSDavid Hildenbrand * highly unreliable and dynamic in virtualized environments, and does not 876e83a437fSDavid Hildenbrand * consider boot time allocations. For example, memory ballooning adjusts the 877e83a437fSDavid Hildenbrand * managed pages when inflating/deflating the balloon, and balloon compaction 878e83a437fSDavid Hildenbrand * can even migrate inflated pages between zones. 879e83a437fSDavid Hildenbrand * 880e83a437fSDavid Hildenbrand * Using "present pages" is better but some things to keep in mind are: 881e83a437fSDavid Hildenbrand * 882e83a437fSDavid Hildenbrand * a) Some memblock allocations, such as for the crashkernel area, are 883e83a437fSDavid Hildenbrand * effectively unused by the kernel, yet they account to "present pages". 884e83a437fSDavid Hildenbrand * Fortunately, these allocations are comparatively small in relevant setups 885e83a437fSDavid Hildenbrand * (e.g., fraction of system memory). 886e83a437fSDavid Hildenbrand * b) Some hotplugged memory blocks in virtualized environments, esecially 887e83a437fSDavid Hildenbrand * hotplugged by virtio-mem, look like they are completely present, however, 888e83a437fSDavid Hildenbrand * only parts of the memory block are actually currently usable. 889e83a437fSDavid Hildenbrand * "present pages" is an upper limit that can get reached at runtime. As 890e83a437fSDavid Hildenbrand * we base our calculations on KERNEL_EARLY, this is not an issue. 891e83a437fSDavid Hildenbrand */ 892445fcf7cSDavid Hildenbrand static struct zone *auto_movable_zone_for_pfn(int nid, 893445fcf7cSDavid Hildenbrand struct memory_group *group, 894445fcf7cSDavid Hildenbrand unsigned long pfn, 895e83a437fSDavid Hildenbrand unsigned long nr_pages) 896e83a437fSDavid Hildenbrand { 897445fcf7cSDavid Hildenbrand unsigned long online_pages = 0, max_pages, end_pfn; 898445fcf7cSDavid Hildenbrand struct page *page; 899445fcf7cSDavid Hildenbrand 900e83a437fSDavid Hildenbrand if (!auto_movable_ratio) 901e83a437fSDavid Hildenbrand goto kernel_zone; 902e83a437fSDavid Hildenbrand 903445fcf7cSDavid Hildenbrand if (group && !group->is_dynamic) { 904445fcf7cSDavid Hildenbrand max_pages = group->s.max_pages; 905445fcf7cSDavid Hildenbrand online_pages = group->present_movable_pages; 906445fcf7cSDavid Hildenbrand 907445fcf7cSDavid Hildenbrand /* If anything is !MOVABLE online the rest !MOVABLE. */ 908445fcf7cSDavid Hildenbrand if (group->present_kernel_pages) 909445fcf7cSDavid Hildenbrand goto kernel_zone; 910445fcf7cSDavid Hildenbrand } else if (!group || group->d.unit_pages == nr_pages) { 911445fcf7cSDavid Hildenbrand max_pages = nr_pages; 912445fcf7cSDavid Hildenbrand } else { 913445fcf7cSDavid Hildenbrand max_pages = group->d.unit_pages; 914445fcf7cSDavid Hildenbrand /* 915445fcf7cSDavid Hildenbrand * Take a look at all online sections in the current unit. 916445fcf7cSDavid Hildenbrand * We can safely assume that all pages within a section belong 917445fcf7cSDavid Hildenbrand * to the same zone, because dynamic memory groups only deal 918445fcf7cSDavid Hildenbrand * with hotplugged memory. 919445fcf7cSDavid Hildenbrand */ 920445fcf7cSDavid Hildenbrand pfn = ALIGN_DOWN(pfn, group->d.unit_pages); 921445fcf7cSDavid Hildenbrand end_pfn = pfn + group->d.unit_pages; 922445fcf7cSDavid Hildenbrand for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 923445fcf7cSDavid Hildenbrand page = pfn_to_online_page(pfn); 924445fcf7cSDavid Hildenbrand if (!page) 925445fcf7cSDavid Hildenbrand continue; 926445fcf7cSDavid Hildenbrand /* If anything is !MOVABLE online the rest !MOVABLE. */ 927445fcf7cSDavid Hildenbrand if (page_zonenum(page) != ZONE_MOVABLE) 928445fcf7cSDavid Hildenbrand goto kernel_zone; 929445fcf7cSDavid Hildenbrand online_pages += PAGES_PER_SECTION; 930445fcf7cSDavid Hildenbrand } 931445fcf7cSDavid Hildenbrand } 932445fcf7cSDavid Hildenbrand 933445fcf7cSDavid Hildenbrand /* 934445fcf7cSDavid Hildenbrand * Online MOVABLE if we could *currently* online all remaining parts 935445fcf7cSDavid Hildenbrand * MOVABLE. We expect to (add+) online them immediately next, so if 936445fcf7cSDavid Hildenbrand * nobody interferes, all will be MOVABLE if possible. 937445fcf7cSDavid Hildenbrand */ 938445fcf7cSDavid Hildenbrand nr_pages = max_pages - online_pages; 9393fcebf90SDavid Hildenbrand if (!auto_movable_can_online_movable(NUMA_NO_NODE, group, nr_pages)) 940e83a437fSDavid Hildenbrand goto kernel_zone; 941e83a437fSDavid Hildenbrand 942e83a437fSDavid Hildenbrand #ifdef CONFIG_NUMA 943e83a437fSDavid Hildenbrand if (auto_movable_numa_aware && 9443fcebf90SDavid Hildenbrand !auto_movable_can_online_movable(nid, group, nr_pages)) 945e83a437fSDavid Hildenbrand goto kernel_zone; 946e83a437fSDavid Hildenbrand #endif /* CONFIG_NUMA */ 947e83a437fSDavid Hildenbrand 948e83a437fSDavid Hildenbrand return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 949e83a437fSDavid Hildenbrand kernel_zone: 950e83a437fSDavid Hildenbrand return default_kernel_zone_for_pfn(nid, pfn, nr_pages); 951e83a437fSDavid Hildenbrand } 952e83a437fSDavid Hildenbrand 953c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 954c6f03e29SMichal Hocko unsigned long nr_pages) 955e5e68930SMichal Hocko { 956c6f03e29SMichal Hocko struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 957c6f03e29SMichal Hocko nr_pages); 958c6f03e29SMichal Hocko struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 959c6f03e29SMichal Hocko bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 960c6f03e29SMichal Hocko bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 961e5e68930SMichal Hocko 962e5e68930SMichal Hocko /* 963c6f03e29SMichal Hocko * We inherit the existing zone in a simple case where zones do not 964c6f03e29SMichal Hocko * overlap in the given range 965e5e68930SMichal Hocko */ 966c6f03e29SMichal Hocko if (in_kernel ^ in_movable) 967c6f03e29SMichal Hocko return (in_kernel) ? kernel_zone : movable_zone; 968e5e68930SMichal Hocko 969c6f03e29SMichal Hocko /* 970c6f03e29SMichal Hocko * If the range doesn't belong to any zone or two zones overlap in the 971c6f03e29SMichal Hocko * given range then we use movable zone only if movable_node is 972c6f03e29SMichal Hocko * enabled because we always online to a kernel zone by default. 973c6f03e29SMichal Hocko */ 974c6f03e29SMichal Hocko return movable_node_enabled ? movable_zone : kernel_zone; 9759f123ab5SMichal Hocko } 9769f123ab5SMichal Hocko 9777cf209baSDavid Hildenbrand struct zone *zone_for_pfn_range(int online_type, int nid, 978445fcf7cSDavid Hildenbrand struct memory_group *group, unsigned long start_pfn, 979e5e68930SMichal Hocko unsigned long nr_pages) 980f1dd2cd1SMichal Hocko { 981c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_KERNEL) 982c6f03e29SMichal Hocko return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 983f1dd2cd1SMichal Hocko 984c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_MOVABLE) 985c6f03e29SMichal Hocko return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 986f1dd2cd1SMichal Hocko 987e83a437fSDavid Hildenbrand if (online_policy == ONLINE_POLICY_AUTO_MOVABLE) 988445fcf7cSDavid Hildenbrand return auto_movable_zone_for_pfn(nid, group, start_pfn, nr_pages); 989e83a437fSDavid Hildenbrand 990c6f03e29SMichal Hocko return default_zone_for_pfn(nid, start_pfn, nr_pages); 991e5e68930SMichal Hocko } 992e5e68930SMichal Hocko 993a08a2ae3SOscar Salvador /* 994a08a2ae3SOscar Salvador * This function should only be called by memory_block_{online,offline}, 995a08a2ae3SOscar Salvador * and {online,offline}_pages. 996a08a2ae3SOscar Salvador */ 997836809ecSDavid Hildenbrand void adjust_present_page_count(struct page *page, struct memory_group *group, 998836809ecSDavid Hildenbrand long nr_pages) 999f9901144SDavid Hildenbrand { 10004b097002SDavid Hildenbrand struct zone *zone = page_zone(page); 1001836809ecSDavid Hildenbrand const bool movable = zone_idx(zone) == ZONE_MOVABLE; 10024b097002SDavid Hildenbrand 10034b097002SDavid Hildenbrand /* 10044b097002SDavid Hildenbrand * We only support onlining/offlining/adding/removing of complete 10054b097002SDavid Hildenbrand * memory blocks; therefore, either all is either early or hotplugged. 10064b097002SDavid Hildenbrand */ 10074b097002SDavid Hildenbrand if (early_section(__pfn_to_section(page_to_pfn(page)))) 10084b097002SDavid Hildenbrand zone->present_early_pages += nr_pages; 1009f9901144SDavid Hildenbrand zone->present_pages += nr_pages; 1010f9901144SDavid Hildenbrand zone->zone_pgdat->node_present_pages += nr_pages; 1011836809ecSDavid Hildenbrand 1012836809ecSDavid Hildenbrand if (group && movable) 1013836809ecSDavid Hildenbrand group->present_movable_pages += nr_pages; 1014836809ecSDavid Hildenbrand else if (group && !movable) 1015836809ecSDavid Hildenbrand group->present_kernel_pages += nr_pages; 1016f9901144SDavid Hildenbrand } 1017f9901144SDavid Hildenbrand 1018a08a2ae3SOscar Salvador int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages, 1019a08a2ae3SOscar Salvador struct zone *zone) 1020a08a2ae3SOscar Salvador { 1021a08a2ae3SOscar Salvador unsigned long end_pfn = pfn + nr_pages; 1022a08a2ae3SOscar Salvador int ret; 1023a08a2ae3SOscar Salvador 1024a08a2ae3SOscar Salvador ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages)); 1025a08a2ae3SOscar Salvador if (ret) 1026a08a2ae3SOscar Salvador return ret; 1027a08a2ae3SOscar Salvador 1028a08a2ae3SOscar Salvador move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_UNMOVABLE); 1029a08a2ae3SOscar Salvador 1030a08a2ae3SOscar Salvador /* 1031a08a2ae3SOscar Salvador * It might be that the vmemmap_pages fully span sections. If that is 1032a08a2ae3SOscar Salvador * the case, mark those sections online here as otherwise they will be 1033a08a2ae3SOscar Salvador * left offline. 1034a08a2ae3SOscar Salvador */ 1035a08a2ae3SOscar Salvador if (nr_pages >= PAGES_PER_SECTION) 1036a08a2ae3SOscar Salvador online_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION)); 1037a08a2ae3SOscar Salvador 1038a08a2ae3SOscar Salvador return ret; 1039a08a2ae3SOscar Salvador } 1040a08a2ae3SOscar Salvador 1041a08a2ae3SOscar Salvador void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages) 1042a08a2ae3SOscar Salvador { 1043a08a2ae3SOscar Salvador unsigned long end_pfn = pfn + nr_pages; 1044a08a2ae3SOscar Salvador 1045a08a2ae3SOscar Salvador /* 1046a08a2ae3SOscar Salvador * It might be that the vmemmap_pages fully span sections. If that is 1047a08a2ae3SOscar Salvador * the case, mark those sections offline here as otherwise they will be 1048a08a2ae3SOscar Salvador * left online. 1049a08a2ae3SOscar Salvador */ 1050a08a2ae3SOscar Salvador if (nr_pages >= PAGES_PER_SECTION) 1051a08a2ae3SOscar Salvador offline_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION)); 1052a08a2ae3SOscar Salvador 1053a08a2ae3SOscar Salvador /* 1054a08a2ae3SOscar Salvador * The pages associated with this vmemmap have been offlined, so 1055a08a2ae3SOscar Salvador * we can reset its state here. 1056a08a2ae3SOscar Salvador */ 1057a08a2ae3SOscar Salvador remove_pfn_range_from_zone(page_zone(pfn_to_page(pfn)), pfn, nr_pages); 1058a08a2ae3SOscar Salvador kasan_remove_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages)); 1059a08a2ae3SOscar Salvador } 1060a08a2ae3SOscar Salvador 1061836809ecSDavid Hildenbrand int __ref online_pages(unsigned long pfn, unsigned long nr_pages, 1062836809ecSDavid Hildenbrand struct zone *zone, struct memory_group *group) 106375884fb1SKAMEZAWA Hiroyuki { 1064aa47228aSCody P Schafer unsigned long flags; 10656811378eSYasunori Goto int need_zonelists_rebuild = 0; 1066a08a2ae3SOscar Salvador const int nid = zone_to_nid(zone); 10677b78d335SYasunori Goto int ret; 10687b78d335SYasunori Goto struct memory_notify arg; 10693947be19SDave Hansen 1070dd8e2f23SOscar Salvador /* 1071dd8e2f23SOscar Salvador * {on,off}lining is constrained to full memory sections (or more 1072041711ceSZhen Lei * precisely to memory blocks from the user space POV). 1073dd8e2f23SOscar Salvador * memmap_on_memory is an exception because it reserves initial part 1074dd8e2f23SOscar Salvador * of the physical memory space for vmemmaps. That space is pageblock 1075dd8e2f23SOscar Salvador * aligned. 1076dd8e2f23SOscar Salvador */ 10774986fac1SDavid Hildenbrand if (WARN_ON_ONCE(!nr_pages || 1078dd8e2f23SOscar Salvador !IS_ALIGNED(pfn, pageblock_nr_pages) || 1079dd8e2f23SOscar Salvador !IS_ALIGNED(pfn + nr_pages, PAGES_PER_SECTION))) 10804986fac1SDavid Hildenbrand return -EINVAL; 10814986fac1SDavid Hildenbrand 1082381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1083381eab4aSDavid Hildenbrand 1084f1dd2cd1SMichal Hocko /* associate pfn range with the zone */ 1085b30c5927SDavid Hildenbrand move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE); 1086511c2abaSLai Jiangshan 10877b78d335SYasunori Goto arg.start_pfn = pfn; 10887b78d335SYasunori Goto arg.nr_pages = nr_pages; 1089d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 10907b78d335SYasunori Goto 10917b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 10927b78d335SYasunori Goto ret = notifier_to_errno(ret); 1093e33e33b4SChen Yucong if (ret) 1094e33e33b4SChen Yucong goto failed_addition; 1095e33e33b4SChen Yucong 10963947be19SDave Hansen /* 1097b30c5927SDavid Hildenbrand * Fixup the number of isolated pageblocks before marking the sections 1098b30c5927SDavid Hildenbrand * onlining, such that undo_isolate_page_range() works correctly. 1099b30c5927SDavid Hildenbrand */ 1100b30c5927SDavid Hildenbrand spin_lock_irqsave(&zone->lock, flags); 1101b30c5927SDavid Hildenbrand zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages; 1102b30c5927SDavid Hildenbrand spin_unlock_irqrestore(&zone->lock, flags); 1103b30c5927SDavid Hildenbrand 1104b30c5927SDavid Hildenbrand /* 11056811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 11066811378eSYasunori Goto * This means the page allocator ignores this zone. 11076811378eSYasunori Goto * So, zonelist must be updated after online. 11086811378eSYasunori Goto */ 11096dcd73d7SWen Congyang if (!populated_zone(zone)) { 11106811378eSYasunori Goto need_zonelists_rebuild = 1; 111172675e13SMichal Hocko setup_zone_pageset(zone); 11126dcd73d7SWen Congyang } 11136811378eSYasunori Goto 1114aac65321SDavid Hildenbrand online_pages_range(pfn, nr_pages); 1115836809ecSDavid Hildenbrand adjust_present_page_count(pfn_to_page(pfn), group, nr_pages); 1116aa47228aSCody P Schafer 1117b30c5927SDavid Hildenbrand node_states_set_node(nid, &arg); 1118b30c5927SDavid Hildenbrand if (need_zonelists_rebuild) 1119b30c5927SDavid Hildenbrand build_all_zonelists(NULL); 1120b30c5927SDavid Hildenbrand 1121b30c5927SDavid Hildenbrand /* Basic onlining is complete, allow allocation of onlined pages. */ 1122b30c5927SDavid Hildenbrand undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE); 1123b30c5927SDavid Hildenbrand 112493146d98SDavid Hildenbrand /* 1125b86c5fc4SDavid Hildenbrand * Freshly onlined pages aren't shuffled (e.g., all pages are placed to 1126b86c5fc4SDavid Hildenbrand * the tail of the freelist when undoing isolation). Shuffle the whole 1127b86c5fc4SDavid Hildenbrand * zone to make sure the just onlined pages are properly distributed 1128b86c5fc4SDavid Hildenbrand * across the whole freelist - to create an initial shuffle. 112993146d98SDavid Hildenbrand */ 1130e900a918SDan Williams shuffle_zone(zone); 1131e900a918SDan Williams 1132b92ca18eSMel Gorman /* reinitialise watermarks and update pcp limits */ 11331b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 11341b79acc9SKOSAKI Motohiro 1135e888ca35SVlastimil Babka kswapd_run(nid); 1136698b1b30SVlastimil Babka kcompactd_run(nid); 113761b13993SDave Hansen 11382d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 11397b78d335SYasunori Goto 11407b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 1141381eab4aSDavid Hildenbrand mem_hotplug_done(); 114230467e0bSDavid Rientjes return 0; 1143e33e33b4SChen Yucong 1144e33e33b4SChen Yucong failed_addition: 1145e33e33b4SChen Yucong pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 1146e33e33b4SChen Yucong (unsigned long long) pfn << PAGE_SHIFT, 1147e33e33b4SChen Yucong (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 1148e33e33b4SChen Yucong memory_notify(MEM_CANCEL_ONLINE, &arg); 1149feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, pfn, nr_pages); 1150381eab4aSDavid Hildenbrand mem_hotplug_done(); 1151e33e33b4SChen Yucong return ret; 11523947be19SDave Hansen } 1153bc02af93SYasunori Goto 11540bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 11550bd85420STang Chen { 11560bd85420STang Chen struct zone *z; 11570bd85420STang Chen 11580bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 11590bd85420STang Chen z->present_pages = 0; 11600bd85420STang Chen 11610bd85420STang Chen pgdat->node_present_pages = 0; 11620bd85420STang Chen } 11630bd85420STang Chen 1164e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 1165c68ab18cSDavid Hildenbrand static pg_data_t __ref *hotadd_new_pgdat(int nid) 11669af3c2deSYasunori Goto { 11679af3c2deSYasunori Goto struct pglist_data *pgdat; 11689af3c2deSYasunori Goto 1169a1e565aaSTang Chen pgdat = NODE_DATA(nid); 1170a1e565aaSTang Chen if (!pgdat) { 11719af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 11729af3c2deSYasunori Goto if (!pgdat) 11739af3c2deSYasunori Goto return NULL; 11749af3c2deSYasunori Goto 117533fce011SWei Yang pgdat->per_cpu_nodestats = 117633fce011SWei Yang alloc_percpu(struct per_cpu_nodestat); 11779af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 1178b0dc3a34SGu Zheng } else { 117933fce011SWei Yang int cpu; 1180e716f2ebSMel Gorman /* 118197a225e6SJoonsoo Kim * Reset the nr_zones, order and highest_zoneidx before reuse. 118297a225e6SJoonsoo Kim * Note that kswapd will init kswapd_highest_zoneidx properly 1183e716f2ebSMel Gorman * when it starts in the near future. 1184e716f2ebSMel Gorman */ 1185b0dc3a34SGu Zheng pgdat->nr_zones = 0; 118638087d9bSMel Gorman pgdat->kswapd_order = 0; 118797a225e6SJoonsoo Kim pgdat->kswapd_highest_zoneidx = 0; 118833fce011SWei Yang for_each_online_cpu(cpu) { 118933fce011SWei Yang struct per_cpu_nodestat *p; 119033fce011SWei Yang 119133fce011SWei Yang p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); 119233fce011SWei Yang memset(p, 0, sizeof(*p)); 119333fce011SWei Yang } 1194a1e565aaSTang Chen } 11959af3c2deSYasunori Goto 11969af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 119703e85f9dSOscar Salvador pgdat->node_id = nid; 1198c68ab18cSDavid Hildenbrand pgdat->node_start_pfn = 0; 119903e85f9dSOscar Salvador 12009af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 120103e85f9dSOscar Salvador free_area_init_core_hotplug(nid); 12029af3c2deSYasunori Goto 1203959ecc48SKAMEZAWA Hiroyuki /* 1204959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 1205959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 1206959ecc48SKAMEZAWA Hiroyuki */ 120772675e13SMichal Hocko build_all_zonelists(pgdat); 1208959ecc48SKAMEZAWA Hiroyuki 1209f784a3f1STang Chen /* 12100bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 12110bd85420STang Chen * clear all zones' present_pages because they will be updated in 12120bd85420STang Chen * online_pages() and offline_pages(). 12130bd85420STang Chen */ 121403e85f9dSOscar Salvador reset_node_managed_pages(pgdat); 12150bd85420STang Chen reset_node_present_pages(pgdat); 12160bd85420STang Chen 12179af3c2deSYasunori Goto return pgdat; 12189af3c2deSYasunori Goto } 12199af3c2deSYasunori Goto 1220b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid) 12219af3c2deSYasunori Goto { 1222b9ff0360SOscar Salvador pg_data_t *pgdat = NODE_DATA(nid); 1223b9ff0360SOscar Salvador 12249af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 12255830169fSReza Arbab free_percpu(pgdat->per_cpu_nodestats); 12269af3c2deSYasunori Goto arch_free_nodedata(pgdat); 12279af3c2deSYasunori Goto } 12289af3c2deSYasunori Goto 12290a547039SKAMEZAWA Hiroyuki 1230ba2d2666SMel Gorman /* 1231ba2d2666SMel Gorman * __try_online_node - online a node if offlined 1232e8b098fcSMike Rapoport * @nid: the node ID 1233b9ff0360SOscar Salvador * @set_node_online: Whether we want to online the node 1234cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1235b9ff0360SOscar Salvador * 1236b9ff0360SOscar Salvador * Returns: 1237b9ff0360SOscar Salvador * 1 -> a new node has been allocated 1238b9ff0360SOscar Salvador * 0 -> the node is already online 1239b9ff0360SOscar Salvador * -ENOMEM -> the node could not be allocated 1240cf23422bSminskey guo */ 1241c68ab18cSDavid Hildenbrand static int __try_online_node(int nid, bool set_node_online) 1242cf23422bSminskey guo { 1243cf23422bSminskey guo pg_data_t *pgdat; 1244b9ff0360SOscar Salvador int ret = 1; 1245cf23422bSminskey guo 124601b0f197SToshi Kani if (node_online(nid)) 124701b0f197SToshi Kani return 0; 124801b0f197SToshi Kani 1249c68ab18cSDavid Hildenbrand pgdat = hotadd_new_pgdat(nid); 12507553e8f2SDavid Rientjes if (!pgdat) { 125101b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1252cf23422bSminskey guo ret = -ENOMEM; 1253cf23422bSminskey guo goto out; 1254cf23422bSminskey guo } 1255b9ff0360SOscar Salvador 1256b9ff0360SOscar Salvador if (set_node_online) { 1257cf23422bSminskey guo node_set_online(nid); 1258cf23422bSminskey guo ret = register_one_node(nid); 1259cf23422bSminskey guo BUG_ON(ret); 1260b9ff0360SOscar Salvador } 1261cf23422bSminskey guo out: 1262b9ff0360SOscar Salvador return ret; 1263b9ff0360SOscar Salvador } 1264b9ff0360SOscar Salvador 1265b9ff0360SOscar Salvador /* 1266b9ff0360SOscar Salvador * Users of this function always want to online/register the node 1267b9ff0360SOscar Salvador */ 1268b9ff0360SOscar Salvador int try_online_node(int nid) 1269b9ff0360SOscar Salvador { 1270b9ff0360SOscar Salvador int ret; 1271b9ff0360SOscar Salvador 1272b9ff0360SOscar Salvador mem_hotplug_begin(); 1273c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, true); 1274bfc8c901SVladimir Davydov mem_hotplug_done(); 1275cf23422bSminskey guo return ret; 1276cf23422bSminskey guo } 1277cf23422bSminskey guo 127827356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 127927356f54SToshi Kani { 1280ba325585SPavel Tatashin /* memory range must be block size aligned */ 1281cec3ebd0SDavid Hildenbrand if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || 1282cec3ebd0SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes())) { 1283ba325585SPavel Tatashin pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1284cec3ebd0SDavid Hildenbrand memory_block_size_bytes(), start, size); 128527356f54SToshi Kani return -EINVAL; 128627356f54SToshi Kani } 128727356f54SToshi Kani 128827356f54SToshi Kani return 0; 128927356f54SToshi Kani } 129027356f54SToshi Kani 129131bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg) 129231bc3858SVitaly Kuznetsov { 12931adf8b46SAnshuman Khandual mem->online_type = mhp_default_online_type; 1294dc18d706SNathan Fontenot return device_online(&mem->dev); 129531bc3858SVitaly Kuznetsov } 129631bc3858SVitaly Kuznetsov 1297a08a2ae3SOscar Salvador bool mhp_supports_memmap_on_memory(unsigned long size) 1298a08a2ae3SOscar Salvador { 1299a08a2ae3SOscar Salvador unsigned long nr_vmemmap_pages = size / PAGE_SIZE; 1300a08a2ae3SOscar Salvador unsigned long vmemmap_size = nr_vmemmap_pages * sizeof(struct page); 1301a08a2ae3SOscar Salvador unsigned long remaining_size = size - vmemmap_size; 1302a08a2ae3SOscar Salvador 1303a08a2ae3SOscar Salvador /* 1304a08a2ae3SOscar Salvador * Besides having arch support and the feature enabled at runtime, we 1305a08a2ae3SOscar Salvador * need a few more assumptions to hold true: 1306a08a2ae3SOscar Salvador * 1307a08a2ae3SOscar Salvador * a) We span a single memory block: memory onlining/offlinin;g happens 1308a08a2ae3SOscar Salvador * in memory block granularity. We don't want the vmemmap of online 1309a08a2ae3SOscar Salvador * memory blocks to reside on offline memory blocks. In the future, 1310a08a2ae3SOscar Salvador * we might want to support variable-sized memory blocks to make the 1311a08a2ae3SOscar Salvador * feature more versatile. 1312a08a2ae3SOscar Salvador * 1313a08a2ae3SOscar Salvador * b) The vmemmap pages span complete PMDs: We don't want vmemmap code 1314a08a2ae3SOscar Salvador * to populate memory from the altmap for unrelated parts (i.e., 1315a08a2ae3SOscar Salvador * other memory blocks) 1316a08a2ae3SOscar Salvador * 1317a08a2ae3SOscar Salvador * c) The vmemmap pages (and thereby the pages that will be exposed to 1318a08a2ae3SOscar Salvador * the buddy) have to cover full pageblocks: memory onlining/offlining 1319a08a2ae3SOscar Salvador * code requires applicable ranges to be page-aligned, for example, to 1320a08a2ae3SOscar Salvador * set the migratetypes properly. 1321a08a2ae3SOscar Salvador * 1322a08a2ae3SOscar Salvador * TODO: Although we have a check here to make sure that vmemmap pages 1323a08a2ae3SOscar Salvador * fully populate a PMD, it is not the right place to check for 1324a08a2ae3SOscar Salvador * this. A much better solution involves improving vmemmap code 1325a08a2ae3SOscar Salvador * to fallback to base pages when trying to populate vmemmap using 1326a08a2ae3SOscar Salvador * altmap as an alternative source of memory, and we do not exactly 1327a08a2ae3SOscar Salvador * populate a single PMD. 1328a08a2ae3SOscar Salvador */ 1329a08a2ae3SOscar Salvador return memmap_on_memory && 13302d7a2171SMuchun Song !hugetlb_free_vmemmap_enabled && 1331a08a2ae3SOscar Salvador IS_ENABLED(CONFIG_MHP_MEMMAP_ON_MEMORY) && 1332a08a2ae3SOscar Salvador size == memory_block_size_bytes() && 1333a08a2ae3SOscar Salvador IS_ALIGNED(vmemmap_size, PMD_SIZE) && 1334a08a2ae3SOscar Salvador IS_ALIGNED(remaining_size, (pageblock_nr_pages << PAGE_SHIFT)); 1335a08a2ae3SOscar Salvador } 1336a08a2ae3SOscar Salvador 13378df1d0e4SDavid Hildenbrand /* 13388df1d0e4SDavid Hildenbrand * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 13398df1d0e4SDavid Hildenbrand * and online/offline operations (triggered e.g. by sysfs). 13408df1d0e4SDavid Hildenbrand * 13418df1d0e4SDavid Hildenbrand * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 13428df1d0e4SDavid Hildenbrand */ 1343b6117199SDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) 1344bc02af93SYasunori Goto { 1345d15dfd31SCatalin Marinas struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; 134632befe9eSDavid Hildenbrand enum memblock_flags memblock_flags = MEMBLOCK_NONE; 1347a08a2ae3SOscar Salvador struct vmem_altmap mhp_altmap = {}; 1348028fc57aSDavid Hildenbrand struct memory_group *group = NULL; 134962cedb9fSDavid Vrabel u64 start, size; 1350b9ff0360SOscar Salvador bool new_node = false; 1351bc02af93SYasunori Goto int ret; 1352bc02af93SYasunori Goto 135362cedb9fSDavid Vrabel start = res->start; 135462cedb9fSDavid Vrabel size = resource_size(res); 135562cedb9fSDavid Vrabel 135627356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 135727356f54SToshi Kani if (ret) 135827356f54SToshi Kani return ret; 135927356f54SToshi Kani 1360028fc57aSDavid Hildenbrand if (mhp_flags & MHP_NID_IS_MGID) { 1361028fc57aSDavid Hildenbrand group = memory_group_find_by_id(nid); 1362028fc57aSDavid Hildenbrand if (!group) 1363028fc57aSDavid Hildenbrand return -EINVAL; 1364028fc57aSDavid Hildenbrand nid = group->nid; 1365028fc57aSDavid Hildenbrand } 1366028fc57aSDavid Hildenbrand 1367fa6d9ec7SVishal Verma if (!node_possible(nid)) { 1368fa6d9ec7SVishal Verma WARN(1, "node %d was absent from the node_possible_map\n", nid); 1369fa6d9ec7SVishal Verma return -EINVAL; 1370fa6d9ec7SVishal Verma } 1371fa6d9ec7SVishal Verma 1372bfc8c901SVladimir Davydov mem_hotplug_begin(); 1373ac13c462SNathan Zimmer 137453d38316SDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 137532befe9eSDavid Hildenbrand if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED) 137632befe9eSDavid Hildenbrand memblock_flags = MEMBLOCK_DRIVER_MANAGED; 137732befe9eSDavid Hildenbrand ret = memblock_add_node(start, size, nid, memblock_flags); 137853d38316SDavid Hildenbrand if (ret) 137953d38316SDavid Hildenbrand goto error_mem_hotplug_end; 138053d38316SDavid Hildenbrand } 13817f36e3e5STang Chen 1382c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, false); 1383b9ff0360SOscar Salvador if (ret < 0) 138441b9e2d7SWen Congyang goto error; 1385b9ff0360SOscar Salvador new_node = ret; 13869af3c2deSYasunori Goto 1387a08a2ae3SOscar Salvador /* 1388a08a2ae3SOscar Salvador * Self hosted memmap array 1389a08a2ae3SOscar Salvador */ 1390a08a2ae3SOscar Salvador if (mhp_flags & MHP_MEMMAP_ON_MEMORY) { 1391a08a2ae3SOscar Salvador if (!mhp_supports_memmap_on_memory(size)) { 1392a08a2ae3SOscar Salvador ret = -EINVAL; 1393a08a2ae3SOscar Salvador goto error; 1394a08a2ae3SOscar Salvador } 1395a08a2ae3SOscar Salvador mhp_altmap.free = PHYS_PFN(size); 1396a08a2ae3SOscar Salvador mhp_altmap.base_pfn = PHYS_PFN(start); 1397a08a2ae3SOscar Salvador params.altmap = &mhp_altmap; 1398a08a2ae3SOscar Salvador } 1399a08a2ae3SOscar Salvador 1400bc02af93SYasunori Goto /* call arch's memory hotadd */ 1401f5637d3bSLogan Gunthorpe ret = arch_add_memory(nid, start, size, ¶ms); 14029af3c2deSYasunori Goto if (ret < 0) 14039af3c2deSYasunori Goto goto error; 14049af3c2deSYasunori Goto 1405db051a0dSDavid Hildenbrand /* create memory block devices after memory was added */ 1406028fc57aSDavid Hildenbrand ret = create_memory_block_devices(start, size, mhp_altmap.alloc, 1407028fc57aSDavid Hildenbrand group); 1408db051a0dSDavid Hildenbrand if (ret) { 140965a2aa5fSDavid Hildenbrand arch_remove_memory(start, size, NULL); 1410db051a0dSDavid Hildenbrand goto error; 1411db051a0dSDavid Hildenbrand } 1412db051a0dSDavid Hildenbrand 1413a1e565aaSTang Chen if (new_node) { 1414d5b6f6a3SOscar Salvador /* If sysfs file of new node can't be created, cpu on the node 14150fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 14160fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 1417d5b6f6a3SOscar Salvador * We online node here. We can't roll back from here. 14180fc44159SYasunori Goto */ 1419d5b6f6a3SOscar Salvador node_set_online(nid); 1420d5b6f6a3SOscar Salvador ret = __register_one_node(nid); 14210fc44159SYasunori Goto BUG_ON(ret); 14220fc44159SYasunori Goto } 14230fc44159SYasunori Goto 1424d5b6f6a3SOscar Salvador /* link memory sections under this node.*/ 142590c7eaebSLaurent Dufour link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1), 1426f85086f9SLaurent Dufour MEMINIT_HOTPLUG); 1427d5b6f6a3SOscar Salvador 1428d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 14297b7b2721SDavid Hildenbrand if (!strcmp(res->name, "System RAM")) 1430d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1431d96ae530Sakpm@linux-foundation.org 1432381eab4aSDavid Hildenbrand /* device_online() will take the lock when calling online_pages() */ 1433381eab4aSDavid Hildenbrand mem_hotplug_done(); 1434381eab4aSDavid Hildenbrand 14359ca6551eSDavid Hildenbrand /* 14369ca6551eSDavid Hildenbrand * In case we're allowed to merge the resource, flag it and trigger 14379ca6551eSDavid Hildenbrand * merging now that adding succeeded. 14389ca6551eSDavid Hildenbrand */ 143926011267SDavid Hildenbrand if (mhp_flags & MHP_MERGE_RESOURCE) 14409ca6551eSDavid Hildenbrand merge_system_ram_resource(res); 14419ca6551eSDavid Hildenbrand 144231bc3858SVitaly Kuznetsov /* online pages if requested */ 14431adf8b46SAnshuman Khandual if (mhp_default_online_type != MMOP_OFFLINE) 1444fbcf73ceSDavid Hildenbrand walk_memory_blocks(start, size, NULL, online_memory_block); 144531bc3858SVitaly Kuznetsov 1446381eab4aSDavid Hildenbrand return ret; 14479af3c2deSYasunori Goto error: 14489af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 1449b9ff0360SOscar Salvador if (new_node) 1450b9ff0360SOscar Salvador rollback_node_hotadd(nid); 145152219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 14527f36e3e5STang Chen memblock_remove(start, size); 145353d38316SDavid Hildenbrand error_mem_hotplug_end: 1454bfc8c901SVladimir Davydov mem_hotplug_done(); 1455bc02af93SYasunori Goto return ret; 1456bc02af93SYasunori Goto } 145762cedb9fSDavid Vrabel 14588df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */ 1459b6117199SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 146062cedb9fSDavid Vrabel { 146162cedb9fSDavid Vrabel struct resource *res; 146262cedb9fSDavid Vrabel int ret; 146362cedb9fSDavid Vrabel 14647b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, "System RAM"); 14656f754ba4SVitaly Kuznetsov if (IS_ERR(res)) 14666f754ba4SVitaly Kuznetsov return PTR_ERR(res); 146762cedb9fSDavid Vrabel 1468b6117199SDavid Hildenbrand ret = add_memory_resource(nid, res, mhp_flags); 146962cedb9fSDavid Vrabel if (ret < 0) 147062cedb9fSDavid Vrabel release_memory_resource(res); 147162cedb9fSDavid Vrabel return ret; 147262cedb9fSDavid Vrabel } 14738df1d0e4SDavid Hildenbrand 1474b6117199SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 14758df1d0e4SDavid Hildenbrand { 14768df1d0e4SDavid Hildenbrand int rc; 14778df1d0e4SDavid Hildenbrand 14788df1d0e4SDavid Hildenbrand lock_device_hotplug(); 1479b6117199SDavid Hildenbrand rc = __add_memory(nid, start, size, mhp_flags); 14808df1d0e4SDavid Hildenbrand unlock_device_hotplug(); 14818df1d0e4SDavid Hildenbrand 14828df1d0e4SDavid Hildenbrand return rc; 14838df1d0e4SDavid Hildenbrand } 1484bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 14850c0e6195SKAMEZAWA Hiroyuki 14867b7b2721SDavid Hildenbrand /* 14877b7b2721SDavid Hildenbrand * Add special, driver-managed memory to the system as system RAM. Such 14887b7b2721SDavid Hildenbrand * memory is not exposed via the raw firmware-provided memmap as system 14897b7b2721SDavid Hildenbrand * RAM, instead, it is detected and added by a driver - during cold boot, 14907b7b2721SDavid Hildenbrand * after a reboot, and after kexec. 14917b7b2721SDavid Hildenbrand * 14927b7b2721SDavid Hildenbrand * Reasons why this memory should not be used for the initial memmap of a 14937b7b2721SDavid Hildenbrand * kexec kernel or for placing kexec images: 14947b7b2721SDavid Hildenbrand * - The booting kernel is in charge of determining how this memory will be 14957b7b2721SDavid Hildenbrand * used (e.g., use persistent memory as system RAM) 14967b7b2721SDavid Hildenbrand * - Coordination with a hypervisor is required before this memory 14977b7b2721SDavid Hildenbrand * can be used (e.g., inaccessible parts). 14987b7b2721SDavid Hildenbrand * 14997b7b2721SDavid Hildenbrand * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided 15007b7b2721SDavid Hildenbrand * memory map") are created. Also, the created memory resource is flagged 15017cf603d1SDavid Hildenbrand * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case 15027b7b2721SDavid Hildenbrand * this memory as well (esp., not place kexec images onto it). 15037b7b2721SDavid Hildenbrand * 15047b7b2721SDavid Hildenbrand * The resource_name (visible via /proc/iomem) has to have the format 15057b7b2721SDavid Hildenbrand * "System RAM ($DRIVER)". 15067b7b2721SDavid Hildenbrand */ 15077b7b2721SDavid Hildenbrand int add_memory_driver_managed(int nid, u64 start, u64 size, 1508b6117199SDavid Hildenbrand const char *resource_name, mhp_t mhp_flags) 15097b7b2721SDavid Hildenbrand { 15107b7b2721SDavid Hildenbrand struct resource *res; 15117b7b2721SDavid Hildenbrand int rc; 15127b7b2721SDavid Hildenbrand 15137b7b2721SDavid Hildenbrand if (!resource_name || 15147b7b2721SDavid Hildenbrand strstr(resource_name, "System RAM (") != resource_name || 15157b7b2721SDavid Hildenbrand resource_name[strlen(resource_name) - 1] != ')') 15167b7b2721SDavid Hildenbrand return -EINVAL; 15177b7b2721SDavid Hildenbrand 15187b7b2721SDavid Hildenbrand lock_device_hotplug(); 15197b7b2721SDavid Hildenbrand 15207b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, resource_name); 15217b7b2721SDavid Hildenbrand if (IS_ERR(res)) { 15227b7b2721SDavid Hildenbrand rc = PTR_ERR(res); 15237b7b2721SDavid Hildenbrand goto out_unlock; 15247b7b2721SDavid Hildenbrand } 15257b7b2721SDavid Hildenbrand 1526b6117199SDavid Hildenbrand rc = add_memory_resource(nid, res, mhp_flags); 15277b7b2721SDavid Hildenbrand if (rc < 0) 15287b7b2721SDavid Hildenbrand release_memory_resource(res); 15297b7b2721SDavid Hildenbrand 15307b7b2721SDavid Hildenbrand out_unlock: 15317b7b2721SDavid Hildenbrand unlock_device_hotplug(); 15327b7b2721SDavid Hildenbrand return rc; 15337b7b2721SDavid Hildenbrand } 15347b7b2721SDavid Hildenbrand EXPORT_SYMBOL_GPL(add_memory_driver_managed); 15357b7b2721SDavid Hildenbrand 1536bca3feaaSAnshuman Khandual /* 1537bca3feaaSAnshuman Khandual * Platforms should define arch_get_mappable_range() that provides 1538bca3feaaSAnshuman Khandual * maximum possible addressable physical memory range for which the 1539bca3feaaSAnshuman Khandual * linear mapping could be created. The platform returned address 1540bca3feaaSAnshuman Khandual * range must adhere to these following semantics. 1541bca3feaaSAnshuman Khandual * 1542bca3feaaSAnshuman Khandual * - range.start <= range.end 1543bca3feaaSAnshuman Khandual * - Range includes both end points [range.start..range.end] 1544bca3feaaSAnshuman Khandual * 1545bca3feaaSAnshuman Khandual * There is also a fallback definition provided here, allowing the 1546bca3feaaSAnshuman Khandual * entire possible physical address range in case any platform does 1547bca3feaaSAnshuman Khandual * not define arch_get_mappable_range(). 1548bca3feaaSAnshuman Khandual */ 1549bca3feaaSAnshuman Khandual struct range __weak arch_get_mappable_range(void) 1550bca3feaaSAnshuman Khandual { 1551bca3feaaSAnshuman Khandual struct range mhp_range = { 1552bca3feaaSAnshuman Khandual .start = 0UL, 1553bca3feaaSAnshuman Khandual .end = -1ULL, 1554bca3feaaSAnshuman Khandual }; 1555bca3feaaSAnshuman Khandual return mhp_range; 1556bca3feaaSAnshuman Khandual } 1557bca3feaaSAnshuman Khandual 1558bca3feaaSAnshuman Khandual struct range mhp_get_pluggable_range(bool need_mapping) 1559bca3feaaSAnshuman Khandual { 1560bca3feaaSAnshuman Khandual const u64 max_phys = (1ULL << MAX_PHYSMEM_BITS) - 1; 1561bca3feaaSAnshuman Khandual struct range mhp_range; 1562bca3feaaSAnshuman Khandual 1563bca3feaaSAnshuman Khandual if (need_mapping) { 1564bca3feaaSAnshuman Khandual mhp_range = arch_get_mappable_range(); 1565bca3feaaSAnshuman Khandual if (mhp_range.start > max_phys) { 1566bca3feaaSAnshuman Khandual mhp_range.start = 0; 1567bca3feaaSAnshuman Khandual mhp_range.end = 0; 1568bca3feaaSAnshuman Khandual } 1569bca3feaaSAnshuman Khandual mhp_range.end = min_t(u64, mhp_range.end, max_phys); 1570bca3feaaSAnshuman Khandual } else { 1571bca3feaaSAnshuman Khandual mhp_range.start = 0; 1572bca3feaaSAnshuman Khandual mhp_range.end = max_phys; 1573bca3feaaSAnshuman Khandual } 1574bca3feaaSAnshuman Khandual return mhp_range; 1575bca3feaaSAnshuman Khandual } 1576bca3feaaSAnshuman Khandual EXPORT_SYMBOL_GPL(mhp_get_pluggable_range); 1577bca3feaaSAnshuman Khandual 1578bca3feaaSAnshuman Khandual bool mhp_range_allowed(u64 start, u64 size, bool need_mapping) 1579bca3feaaSAnshuman Khandual { 1580bca3feaaSAnshuman Khandual struct range mhp_range = mhp_get_pluggable_range(need_mapping); 1581bca3feaaSAnshuman Khandual u64 end = start + size; 1582bca3feaaSAnshuman Khandual 1583bca3feaaSAnshuman Khandual if (start < end && start >= mhp_range.start && (end - 1) <= mhp_range.end) 1584bca3feaaSAnshuman Khandual return true; 1585bca3feaaSAnshuman Khandual 1586bca3feaaSAnshuman Khandual pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n", 1587bca3feaaSAnshuman Khandual start, end, mhp_range.start, mhp_range.end); 1588bca3feaaSAnshuman Khandual return false; 1589bca3feaaSAnshuman Khandual } 1590bca3feaaSAnshuman Khandual 15910c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 15920c0e6195SKAMEZAWA Hiroyuki /* 159392917998SDavid Hildenbrand * Confirm all pages in a range [start, end) belong to the same zone (skipping 159492917998SDavid Hildenbrand * memory holes). When true, return the zone. 15950c0e6195SKAMEZAWA Hiroyuki */ 159692917998SDavid Hildenbrand struct zone *test_pages_in_a_zone(unsigned long start_pfn, 159792917998SDavid Hildenbrand unsigned long end_pfn) 15980c0e6195SKAMEZAWA Hiroyuki { 15995f0f2887SAndrew Banman unsigned long pfn, sec_end_pfn; 16000c0e6195SKAMEZAWA Hiroyuki struct zone *zone = NULL; 16010c0e6195SKAMEZAWA Hiroyuki struct page *page; 1602673d40c8SMike Rapoport 1603deb88a2aSToshi Kani for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1); 16040c0e6195SKAMEZAWA Hiroyuki pfn < end_pfn; 1605deb88a2aSToshi Kani pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) { 16065f0f2887SAndrew Banman /* Make sure the memory section is present first */ 16075f0f2887SAndrew Banman if (!present_section_nr(pfn_to_section_nr(pfn))) 16085f0f2887SAndrew Banman continue; 16095f0f2887SAndrew Banman for (; pfn < sec_end_pfn && pfn < end_pfn; 16100c0e6195SKAMEZAWA Hiroyuki pfn += MAX_ORDER_NR_PAGES) { 161124feb47cSMikhail Zaslonko /* Check if we got outside of the zone */ 1612673d40c8SMike Rapoport if (zone && !zone_spans_pfn(zone, pfn)) 161392917998SDavid Hildenbrand return NULL; 1614673d40c8SMike Rapoport page = pfn_to_page(pfn); 16150c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 161692917998SDavid Hildenbrand return NULL; 16170c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 16180c0e6195SKAMEZAWA Hiroyuki } 16195f0f2887SAndrew Banman } 1620deb88a2aSToshi Kani 162192917998SDavid Hildenbrand return zone; 1622a96dfddbSToshi Kani } 16230c0e6195SKAMEZAWA Hiroyuki 16240c0e6195SKAMEZAWA Hiroyuki /* 16250efadf48SYisheng Xie * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 1626aa218795SDavid Hildenbrand * non-lru movable pages and hugepages). Will skip over most unmovable 1627aa218795SDavid Hildenbrand * pages (esp., pages that can be skipped when offlining), but bail out on 1628aa218795SDavid Hildenbrand * definitely unmovable pages. 1629aa218795SDavid Hildenbrand * 1630aa218795SDavid Hildenbrand * Returns: 1631aa218795SDavid Hildenbrand * 0 in case a movable page is found and movable_pfn was updated. 1632aa218795SDavid Hildenbrand * -ENOENT in case no movable page was found. 1633aa218795SDavid Hildenbrand * -EBUSY in case a definitely unmovable page was found. 16340c0e6195SKAMEZAWA Hiroyuki */ 1635aa218795SDavid Hildenbrand static int scan_movable_pages(unsigned long start, unsigned long end, 1636aa218795SDavid Hildenbrand unsigned long *movable_pfn) 16370c0e6195SKAMEZAWA Hiroyuki { 16380c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 1639eeb0efd0SOscar Salvador 16400c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 1641eeb0efd0SOscar Salvador struct page *page, *head; 1642eeb0efd0SOscar Salvador unsigned long skip; 1643eeb0efd0SOscar Salvador 1644eeb0efd0SOscar Salvador if (!pfn_valid(pfn)) 1645eeb0efd0SOscar Salvador continue; 16460c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 16470c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 1648aa218795SDavid Hildenbrand goto found; 16490efadf48SYisheng Xie if (__PageMovable(page)) 1650aa218795SDavid Hildenbrand goto found; 1651aa218795SDavid Hildenbrand 1652aa218795SDavid Hildenbrand /* 1653aa218795SDavid Hildenbrand * PageOffline() pages that are not marked __PageMovable() and 1654aa218795SDavid Hildenbrand * have a reference count > 0 (after MEM_GOING_OFFLINE) are 1655aa218795SDavid Hildenbrand * definitely unmovable. If their reference count would be 0, 1656aa218795SDavid Hildenbrand * they could at least be skipped when offlining memory. 1657aa218795SDavid Hildenbrand */ 1658aa218795SDavid Hildenbrand if (PageOffline(page) && page_count(page)) 1659aa218795SDavid Hildenbrand return -EBUSY; 1660eeb0efd0SOscar Salvador 1661eeb0efd0SOscar Salvador if (!PageHuge(page)) 1662eeb0efd0SOscar Salvador continue; 1663eeb0efd0SOscar Salvador head = compound_head(page); 16648f251a3dSMike Kravetz /* 16658f251a3dSMike Kravetz * This test is racy as we hold no reference or lock. The 16668f251a3dSMike Kravetz * hugetlb page could have been free'ed and head is no longer 16678f251a3dSMike Kravetz * a hugetlb page before the following check. In such unlikely 16688f251a3dSMike Kravetz * cases false positives and negatives are possible. Calling 16698f251a3dSMike Kravetz * code must deal with these scenarios. 16708f251a3dSMike Kravetz */ 16718f251a3dSMike Kravetz if (HPageMigratable(head)) 1672aa218795SDavid Hildenbrand goto found; 1673d8c6546bSMatthew Wilcox (Oracle) skip = compound_nr(head) - (page - head); 1674eeb0efd0SOscar Salvador pfn += skip - 1; 16750c0e6195SKAMEZAWA Hiroyuki } 1676aa218795SDavid Hildenbrand return -ENOENT; 1677aa218795SDavid Hildenbrand found: 1678aa218795SDavid Hildenbrand *movable_pfn = pfn; 16790c0e6195SKAMEZAWA Hiroyuki return 0; 16800c0e6195SKAMEZAWA Hiroyuki } 16810c0e6195SKAMEZAWA Hiroyuki 16820c0e6195SKAMEZAWA Hiroyuki static int 16830c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 16840c0e6195SKAMEZAWA Hiroyuki { 16850c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 16866c357848SMatthew Wilcox (Oracle) struct page *page, *head; 16870c0e6195SKAMEZAWA Hiroyuki int ret = 0; 16880c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 1689786dee86SLiam Mark static DEFINE_RATELIMIT_STATE(migrate_rs, DEFAULT_RATELIMIT_INTERVAL, 1690786dee86SLiam Mark DEFAULT_RATELIMIT_BURST); 16910c0e6195SKAMEZAWA Hiroyuki 1692a85009c3SMichal Hocko for (pfn = start_pfn; pfn < end_pfn; pfn++) { 1693*869f7ee6SMatthew Wilcox (Oracle) struct folio *folio; 1694*869f7ee6SMatthew Wilcox (Oracle) 16950c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 16960c0e6195SKAMEZAWA Hiroyuki continue; 16970c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 1698*869f7ee6SMatthew Wilcox (Oracle) folio = page_folio(page); 1699*869f7ee6SMatthew Wilcox (Oracle) head = &folio->page; 1700c8721bbbSNaoya Horiguchi 1701c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1702d8c6546bSMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + compound_nr(head) - 1; 1703daf3538aSOscar Salvador isolate_huge_page(head, &source); 1704c8721bbbSNaoya Horiguchi continue; 170594723aafSMichal Hocko } else if (PageTransHuge(page)) 17066c357848SMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + thp_nr_pages(page) - 1; 1707c8721bbbSNaoya Horiguchi 1708b15c8726SMichal Hocko /* 1709b15c8726SMichal Hocko * HWPoison pages have elevated reference counts so the migration would 1710b15c8726SMichal Hocko * fail on them. It also doesn't make any sense to migrate them in the 1711b15c8726SMichal Hocko * first place. Still try to unmap such a page in case it is still mapped 1712b15c8726SMichal Hocko * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep 1713b15c8726SMichal Hocko * the unmap as the catch all safety net). 1714b15c8726SMichal Hocko */ 1715b15c8726SMichal Hocko if (PageHWPoison(page)) { 1716*869f7ee6SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_lru(folio))) 1717*869f7ee6SMatthew Wilcox (Oracle) folio_isolate_lru(folio); 1718*869f7ee6SMatthew Wilcox (Oracle) if (folio_mapped(folio)) 1719*869f7ee6SMatthew Wilcox (Oracle) try_to_unmap(folio, TTU_IGNORE_MLOCK); 1720b15c8726SMichal Hocko continue; 1721b15c8726SMichal Hocko } 1722b15c8726SMichal Hocko 1723700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 17240c0e6195SKAMEZAWA Hiroyuki continue; 17250c0e6195SKAMEZAWA Hiroyuki /* 17260efadf48SYisheng Xie * We can skip free pages. And we can deal with pages on 17270efadf48SYisheng Xie * LRU and non-lru movable pages. 17280c0e6195SKAMEZAWA Hiroyuki */ 17290efadf48SYisheng Xie if (PageLRU(page)) 173062695a84SNick Piggin ret = isolate_lru_page(page); 17310efadf48SYisheng Xie else 17320efadf48SYisheng Xie ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 17330c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 173462695a84SNick Piggin list_add_tail(&page->lru, &source); 17350efadf48SYisheng Xie if (!__PageMovable(page)) 1736599d0c95SMel Gorman inc_node_page_state(page, NR_ISOLATED_ANON + 17379de4f22aSHuang Ying page_is_file_lru(page)); 17386d9c285aSKOSAKI Motohiro 17390c0e6195SKAMEZAWA Hiroyuki } else { 1740786dee86SLiam Mark if (__ratelimit(&migrate_rs)) { 17412932c8b0SMichal Hocko pr_warn("failed to isolate pfn %lx\n", pfn); 17420efadf48SYisheng Xie dump_page(page, "isolation failed"); 17431723058eSOscar Salvador } 1744786dee86SLiam Mark } 1745700c2a46SKonstantin Khlebnikov put_page(page); 17460c0e6195SKAMEZAWA Hiroyuki } 1747f3ab2636SBob Liu if (!list_empty(&source)) { 1748203e6e5cSJoonsoo Kim nodemask_t nmask = node_states[N_MEMORY]; 1749203e6e5cSJoonsoo Kim struct migration_target_control mtc = { 1750203e6e5cSJoonsoo Kim .nmask = &nmask, 1751203e6e5cSJoonsoo Kim .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, 1752203e6e5cSJoonsoo Kim }; 1753203e6e5cSJoonsoo Kim 1754203e6e5cSJoonsoo Kim /* 1755203e6e5cSJoonsoo Kim * We have checked that migration range is on a single zone so 1756203e6e5cSJoonsoo Kim * we can use the nid of the first page to all the others. 1757203e6e5cSJoonsoo Kim */ 1758203e6e5cSJoonsoo Kim mtc.nid = page_to_nid(list_first_entry(&source, struct page, lru)); 1759203e6e5cSJoonsoo Kim 1760203e6e5cSJoonsoo Kim /* 1761203e6e5cSJoonsoo Kim * try to allocate from a different node but reuse this node 1762203e6e5cSJoonsoo Kim * if there are no other online nodes to be used (e.g. we are 1763203e6e5cSJoonsoo Kim * offlining a part of the only existing node) 1764203e6e5cSJoonsoo Kim */ 1765203e6e5cSJoonsoo Kim node_clear(mtc.nid, nmask); 1766203e6e5cSJoonsoo Kim if (nodes_empty(nmask)) 1767203e6e5cSJoonsoo Kim node_set(mtc.nid, nmask); 1768203e6e5cSJoonsoo Kim ret = migrate_pages(&source, alloc_migration_target, NULL, 17695ac95884SYang Shi (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG, NULL); 17702932c8b0SMichal Hocko if (ret) { 17712932c8b0SMichal Hocko list_for_each_entry(page, &source, lru) { 1772786dee86SLiam Mark if (__ratelimit(&migrate_rs)) { 1773786dee86SLiam Mark pr_warn("migrating pfn %lx failed ret:%d\n", 17742932c8b0SMichal Hocko page_to_pfn(page), ret); 17752932c8b0SMichal Hocko dump_page(page, "migration failure"); 17762932c8b0SMichal Hocko } 1777786dee86SLiam Mark } 1778c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1779f3ab2636SBob Liu } 17802932c8b0SMichal Hocko } 17811723058eSOscar Salvador 17820c0e6195SKAMEZAWA Hiroyuki return ret; 17830c0e6195SKAMEZAWA Hiroyuki } 17840c0e6195SKAMEZAWA Hiroyuki 1785c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1786c5320926STang Chen { 178755ac590cSTang Chen movable_node_enabled = true; 1788c5320926STang Chen return 0; 1789c5320926STang Chen } 1790c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1791c5320926STang Chen 1792d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1793d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1794d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1795d9713679SLai Jiangshan { 1796d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1797d9713679SLai Jiangshan unsigned long present_pages = 0; 179886b27beaSOscar Salvador enum zone_type zt; 1799d9713679SLai Jiangshan 180098fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 180198fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 180286b27beaSOscar Salvador 180386b27beaSOscar Salvador /* 180486b27beaSOscar Salvador * Check whether node_states[N_NORMAL_MEMORY] will be changed. 180586b27beaSOscar Salvador * If the memory to be offline is within the range 180686b27beaSOscar Salvador * [0..ZONE_NORMAL], and it is the last present memory there, 180786b27beaSOscar Salvador * the zones in that range will become empty after the offlining, 180886b27beaSOscar Salvador * thus we can determine that we need to clear the node from 180986b27beaSOscar Salvador * node_states[N_NORMAL_MEMORY]. 181086b27beaSOscar Salvador */ 181186b27beaSOscar Salvador for (zt = 0; zt <= ZONE_NORMAL; zt++) 181286b27beaSOscar Salvador present_pages += pgdat->node_zones[zt].present_pages; 181386b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 181486b27beaSOscar Salvador arg->status_change_nid_normal = zone_to_nid(zone); 1815d9713679SLai Jiangshan 18166715ddf9SLai Jiangshan /* 18176b740c6cSDavid Hildenbrand * We have accounted the pages from [0..ZONE_NORMAL); ZONE_HIGHMEM 18186b740c6cSDavid Hildenbrand * does not apply as we don't support 32bit. 181986b27beaSOscar Salvador * Here we count the possible pages from ZONE_MOVABLE. 182086b27beaSOscar Salvador * If after having accounted all the pages, we see that the nr_pages 182186b27beaSOscar Salvador * to be offlined is over or equal to the accounted pages, 182286b27beaSOscar Salvador * we know that the node will become empty, and so, we can clear 182386b27beaSOscar Salvador * it for N_MEMORY as well. 1824d9713679SLai Jiangshan */ 182586b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1826d9713679SLai Jiangshan 1827d9713679SLai Jiangshan if (nr_pages >= present_pages) 1828d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1829d9713679SLai Jiangshan } 1830d9713679SLai Jiangshan 1831d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1832d9713679SLai Jiangshan { 1833d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1834d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1835d9713679SLai Jiangshan 1836cf01f6f5SOscar Salvador if (arg->status_change_nid >= 0) 18376715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1838d9713679SLai Jiangshan } 1839d9713679SLai Jiangshan 1840c5e79ef5SDavid Hildenbrand static int count_system_ram_pages_cb(unsigned long start_pfn, 1841c5e79ef5SDavid Hildenbrand unsigned long nr_pages, void *data) 1842c5e79ef5SDavid Hildenbrand { 1843c5e79ef5SDavid Hildenbrand unsigned long *nr_system_ram_pages = data; 1844c5e79ef5SDavid Hildenbrand 1845c5e79ef5SDavid Hildenbrand *nr_system_ram_pages += nr_pages; 1846c5e79ef5SDavid Hildenbrand return 0; 1847c5e79ef5SDavid Hildenbrand } 1848c5e79ef5SDavid Hildenbrand 1849836809ecSDavid Hildenbrand int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages, 1850836809ecSDavid Hildenbrand struct memory_group *group) 18510c0e6195SKAMEZAWA Hiroyuki { 185273a11c96SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 18530a1a9a00SDavid Hildenbrand unsigned long pfn, system_ram_pages = 0; 1854d702909fSCody P Schafer unsigned long flags; 18550c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 18567b78d335SYasunori Goto struct memory_notify arg; 1857ea15153cSDavid Hildenbrand int ret, node; 185879605093SMichal Hocko char *reason; 18590c0e6195SKAMEZAWA Hiroyuki 1860dd8e2f23SOscar Salvador /* 1861dd8e2f23SOscar Salvador * {on,off}lining is constrained to full memory sections (or more 1862041711ceSZhen Lei * precisely to memory blocks from the user space POV). 1863dd8e2f23SOscar Salvador * memmap_on_memory is an exception because it reserves initial part 1864dd8e2f23SOscar Salvador * of the physical memory space for vmemmaps. That space is pageblock 1865dd8e2f23SOscar Salvador * aligned. 1866dd8e2f23SOscar Salvador */ 18674986fac1SDavid Hildenbrand if (WARN_ON_ONCE(!nr_pages || 1868dd8e2f23SOscar Salvador !IS_ALIGNED(start_pfn, pageblock_nr_pages) || 1869dd8e2f23SOscar Salvador !IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION))) 18704986fac1SDavid Hildenbrand return -EINVAL; 18714986fac1SDavid Hildenbrand 1872381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1873381eab4aSDavid Hildenbrand 1874c5e79ef5SDavid Hildenbrand /* 1875c5e79ef5SDavid Hildenbrand * Don't allow to offline memory blocks that contain holes. 1876c5e79ef5SDavid Hildenbrand * Consequently, memory blocks with holes can never get onlined 1877c5e79ef5SDavid Hildenbrand * via the hotplug path - online_pages() - as hotplugged memory has 1878c5e79ef5SDavid Hildenbrand * no holes. This way, we e.g., don't have to worry about marking 1879c5e79ef5SDavid Hildenbrand * memory holes PG_reserved, don't need pfn_valid() checks, and can 1880c5e79ef5SDavid Hildenbrand * avoid using walk_system_ram_range() later. 1881c5e79ef5SDavid Hildenbrand */ 188273a11c96SDavid Hildenbrand walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages, 1883c5e79ef5SDavid Hildenbrand count_system_ram_pages_cb); 188473a11c96SDavid Hildenbrand if (system_ram_pages != nr_pages) { 1885c5e79ef5SDavid Hildenbrand ret = -EINVAL; 1886c5e79ef5SDavid Hildenbrand reason = "memory holes"; 1887c5e79ef5SDavid Hildenbrand goto failed_removal; 1888c5e79ef5SDavid Hildenbrand } 1889c5e79ef5SDavid Hildenbrand 18900c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 18910c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 189292917998SDavid Hildenbrand zone = test_pages_in_a_zone(start_pfn, end_pfn); 189392917998SDavid Hildenbrand if (!zone) { 189479605093SMichal Hocko ret = -EINVAL; 189579605093SMichal Hocko reason = "multizone range"; 189679605093SMichal Hocko goto failed_removal; 1897381eab4aSDavid Hildenbrand } 18987b78d335SYasunori Goto node = zone_to_nid(zone); 18997b78d335SYasunori Goto 1900ec6e8c7eSVlastimil Babka /* 1901ec6e8c7eSVlastimil Babka * Disable pcplists so that page isolation cannot race with freeing 1902ec6e8c7eSVlastimil Babka * in a way that pages from isolated pageblock are left on pcplists. 1903ec6e8c7eSVlastimil Babka */ 1904ec6e8c7eSVlastimil Babka zone_pcp_disable(zone); 1905d479960eSMinchan Kim lru_cache_disable(); 1906ec6e8c7eSVlastimil Babka 19070c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1908b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1909d381c547SMichal Hocko MIGRATE_MOVABLE, 1910756d25beSDavid Hildenbrand MEMORY_OFFLINE | REPORT_FAILURE); 19113fa0c7c7SDavid Hildenbrand if (ret) { 191279605093SMichal Hocko reason = "failure to isolate range"; 1913ec6e8c7eSVlastimil Babka goto failed_removal_pcplists_disabled; 1914381eab4aSDavid Hildenbrand } 19157b78d335SYasunori Goto 19167b78d335SYasunori Goto arg.start_pfn = start_pfn; 19177b78d335SYasunori Goto arg.nr_pages = nr_pages; 1918d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 19197b78d335SYasunori Goto 19207b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 19217b78d335SYasunori Goto ret = notifier_to_errno(ret); 192279605093SMichal Hocko if (ret) { 192379605093SMichal Hocko reason = "notifier failure"; 192479605093SMichal Hocko goto failed_removal_isolated; 192579605093SMichal Hocko } 19267b78d335SYasunori Goto 1927bb8965bdSMichal Hocko do { 1928aa218795SDavid Hildenbrand pfn = start_pfn; 1929aa218795SDavid Hildenbrand do { 193079605093SMichal Hocko if (signal_pending(current)) { 1931bb8965bdSMichal Hocko ret = -EINTR; 193279605093SMichal Hocko reason = "signal backoff"; 193379605093SMichal Hocko goto failed_removal_isolated; 193479605093SMichal Hocko } 193572b39cfcSMichal Hocko 19360c0e6195SKAMEZAWA Hiroyuki cond_resched(); 19370c0e6195SKAMEZAWA Hiroyuki 1938aa218795SDavid Hildenbrand ret = scan_movable_pages(pfn, end_pfn, &pfn); 1939aa218795SDavid Hildenbrand if (!ret) { 1940bb8965bdSMichal Hocko /* 1941bb8965bdSMichal Hocko * TODO: fatal migration failures should bail 1942bb8965bdSMichal Hocko * out 1943bb8965bdSMichal Hocko */ 1944bb8965bdSMichal Hocko do_migrate_range(pfn, end_pfn); 1945bb8965bdSMichal Hocko } 1946aa218795SDavid Hildenbrand } while (!ret); 1947aa218795SDavid Hildenbrand 1948aa218795SDavid Hildenbrand if (ret != -ENOENT) { 1949aa218795SDavid Hildenbrand reason = "unmovable page"; 1950aa218795SDavid Hildenbrand goto failed_removal_isolated; 19510c0e6195SKAMEZAWA Hiroyuki } 195272b39cfcSMichal Hocko 1953c8721bbbSNaoya Horiguchi /* 1954bb8965bdSMichal Hocko * Dissolve free hugepages in the memory block before doing 1955bb8965bdSMichal Hocko * offlining actually in order to make hugetlbfs's object 1956bb8965bdSMichal Hocko * counting consistent. 1957c8721bbbSNaoya Horiguchi */ 1958082d5b6bSGerald Schaefer ret = dissolve_free_huge_pages(start_pfn, end_pfn); 195979605093SMichal Hocko if (ret) { 196079605093SMichal Hocko reason = "failure to dissolve huge pages"; 196179605093SMichal Hocko goto failed_removal_isolated; 196279605093SMichal Hocko } 19630a1a9a00SDavid Hildenbrand 19640a1a9a00SDavid Hildenbrand ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE); 1965ec6e8c7eSVlastimil Babka 19665557c766SMichal Hocko } while (ret); 1967bb8965bdSMichal Hocko 19680a1a9a00SDavid Hildenbrand /* Mark all sections offline and remove free pages from the buddy. */ 19690a1a9a00SDavid Hildenbrand __offline_isolated_pages(start_pfn, end_pfn); 19707c33023aSLaurent Dufour pr_debug("Offlined Pages %ld\n", nr_pages); 19710a1a9a00SDavid Hildenbrand 19729b7ea46aSQian Cai /* 1973b30c5927SDavid Hildenbrand * The memory sections are marked offline, and the pageblock flags 1974b30c5927SDavid Hildenbrand * effectively stale; nobody should be touching them. Fixup the number 1975b30c5927SDavid Hildenbrand * of isolated pageblocks, memory onlining will properly revert this. 19769b7ea46aSQian Cai */ 19779b7ea46aSQian Cai spin_lock_irqsave(&zone->lock, flags); 1978ea15153cSDavid Hildenbrand zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages; 19799b7ea46aSQian Cai spin_unlock_irqrestore(&zone->lock, flags); 19809b7ea46aSQian Cai 1981d479960eSMinchan Kim lru_cache_enable(); 1982ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 1983ec6e8c7eSVlastimil Babka 19840c0e6195SKAMEZAWA Hiroyuki /* removal success */ 19850a1a9a00SDavid Hildenbrand adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages); 1986836809ecSDavid Hildenbrand adjust_present_page_count(pfn_to_page(start_pfn), group, -nr_pages); 19877b78d335SYasunori Goto 1988b92ca18eSMel Gorman /* reinitialise watermarks and update pcp limits */ 19891b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 19901b79acc9SKOSAKI Motohiro 19911e8537baSXishi Qiu if (!populated_zone(zone)) { 1992340175b7SJiang Liu zone_pcp_reset(zone); 199372675e13SMichal Hocko build_all_zonelists(NULL); 1994b92ca18eSMel Gorman } 1995340175b7SJiang Liu 1996d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1997698b1b30SVlastimil Babka if (arg.status_change_nid >= 0) { 19988fe23e05SDavid Rientjes kswapd_stop(node); 1999698b1b30SVlastimil Babka kcompactd_stop(node); 2000698b1b30SVlastimil Babka } 2001bce7394aSMinchan Kim 20020c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 20037b78d335SYasunori Goto 20047b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 2005feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, start_pfn, nr_pages); 2006381eab4aSDavid Hildenbrand mem_hotplug_done(); 20070c0e6195SKAMEZAWA Hiroyuki return 0; 20080c0e6195SKAMEZAWA Hiroyuki 200979605093SMichal Hocko failed_removal_isolated: 201079605093SMichal Hocko undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 2011c4efe484SQian Cai memory_notify(MEM_CANCEL_OFFLINE, &arg); 2012ec6e8c7eSVlastimil Babka failed_removal_pcplists_disabled: 2013946746d1SMiaohe Lin lru_cache_enable(); 2014ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 20150c0e6195SKAMEZAWA Hiroyuki failed_removal: 201679605093SMichal Hocko pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 2017a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 201879605093SMichal Hocko ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 201979605093SMichal Hocko reason); 20200c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 2021381eab4aSDavid Hildenbrand mem_hotplug_done(); 20220c0e6195SKAMEZAWA Hiroyuki return ret; 20230c0e6195SKAMEZAWA Hiroyuki } 202471088785SBadari Pulavarty 2025d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 2026bbc76be6SWen Congyang { 2027bbc76be6SWen Congyang int ret = !is_memblock_offlined(mem); 2028e1c158e4SDavid Hildenbrand int *nid = arg; 2029bbc76be6SWen Congyang 2030e1c158e4SDavid Hildenbrand *nid = mem->nid; 2031349daa0fSRandy Dunlap if (unlikely(ret)) { 2032349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 2033349daa0fSRandy Dunlap 2034349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 2035b6c88d3bSDavid Hildenbrand endpa = beginpa + memory_block_size_bytes() - 1; 2036756a025fSJoe Perches pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 2037349daa0fSRandy Dunlap &beginpa, &endpa); 2038bbc76be6SWen Congyang 2039eca499abSPavel Tatashin return -EBUSY; 2040eca499abSPavel Tatashin } 2041eca499abSPavel Tatashin return 0; 2042bbc76be6SWen Congyang } 2043bbc76be6SWen Congyang 2044a08a2ae3SOscar Salvador static int get_nr_vmemmap_pages_cb(struct memory_block *mem, void *arg) 2045a08a2ae3SOscar Salvador { 2046a08a2ae3SOscar Salvador /* 2047a08a2ae3SOscar Salvador * If not set, continue with the next block. 2048a08a2ae3SOscar Salvador */ 2049a08a2ae3SOscar Salvador return mem->nr_vmemmap_pages; 2050a08a2ae3SOscar Salvador } 2051a08a2ae3SOscar Salvador 20520f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat) 205360a5a19eSTang Chen { 205460a5a19eSTang Chen int cpu; 205560a5a19eSTang Chen 205660a5a19eSTang Chen for_each_present_cpu(cpu) { 205760a5a19eSTang Chen if (cpu_to_node(cpu) == pgdat->node_id) 205860a5a19eSTang Chen /* 205960a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 206060a5a19eSTang Chen * offline this node. 206160a5a19eSTang Chen */ 206260a5a19eSTang Chen return -EBUSY; 206360a5a19eSTang Chen } 206460a5a19eSTang Chen 206560a5a19eSTang Chen return 0; 206660a5a19eSTang Chen } 206760a5a19eSTang Chen 20682c91f8fcSDavid Hildenbrand static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg) 20692c91f8fcSDavid Hildenbrand { 20702c91f8fcSDavid Hildenbrand int nid = *(int *)arg; 20712c91f8fcSDavid Hildenbrand 20722c91f8fcSDavid Hildenbrand /* 20732c91f8fcSDavid Hildenbrand * If a memory block belongs to multiple nodes, the stored nid is not 20742c91f8fcSDavid Hildenbrand * reliable. However, such blocks are always online (e.g., cannot get 20752c91f8fcSDavid Hildenbrand * offlined) and, therefore, are still spanned by the node. 20762c91f8fcSDavid Hildenbrand */ 20772c91f8fcSDavid Hildenbrand return mem->nid == nid ? -EEXIST : 0; 20782c91f8fcSDavid Hildenbrand } 20792c91f8fcSDavid Hildenbrand 20800f1cfe9dSToshi Kani /** 20810f1cfe9dSToshi Kani * try_offline_node 2082e8b098fcSMike Rapoport * @nid: the node ID 20830f1cfe9dSToshi Kani * 20840f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 20850f1cfe9dSToshi Kani * 20860f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 20870f1cfe9dSToshi Kani * and online/offline operations before this call. 20880f1cfe9dSToshi Kani */ 208990b30cdcSWen Congyang void try_offline_node(int nid) 209060a5a19eSTang Chen { 2091d822b86aSWen Congyang pg_data_t *pgdat = NODE_DATA(nid); 20922c91f8fcSDavid Hildenbrand int rc; 209360a5a19eSTang Chen 209460a5a19eSTang Chen /* 20952c91f8fcSDavid Hildenbrand * If the node still spans pages (especially ZONE_DEVICE), don't 20962c91f8fcSDavid Hildenbrand * offline it. A node spans memory after move_pfn_range_to_zone(), 20972c91f8fcSDavid Hildenbrand * e.g., after the memory block was onlined. 209860a5a19eSTang Chen */ 20992c91f8fcSDavid Hildenbrand if (pgdat->node_spanned_pages) 210060a5a19eSTang Chen return; 21012c91f8fcSDavid Hildenbrand 21022c91f8fcSDavid Hildenbrand /* 21032c91f8fcSDavid Hildenbrand * Especially offline memory blocks might not be spanned by the 21042c91f8fcSDavid Hildenbrand * node. They will get spanned by the node once they get onlined. 21052c91f8fcSDavid Hildenbrand * However, they link to the node in sysfs and can get onlined later. 21062c91f8fcSDavid Hildenbrand */ 21072c91f8fcSDavid Hildenbrand rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb); 21082c91f8fcSDavid Hildenbrand if (rc) 21092c91f8fcSDavid Hildenbrand return; 211060a5a19eSTang Chen 211146a3679bSMichal Hocko if (check_cpu_on_node(pgdat)) 211260a5a19eSTang Chen return; 211360a5a19eSTang Chen 211460a5a19eSTang Chen /* 211560a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 211660a5a19eSTang Chen * node now. 211760a5a19eSTang Chen */ 211860a5a19eSTang Chen node_set_offline(nid); 211960a5a19eSTang Chen unregister_one_node(nid); 212060a5a19eSTang Chen } 212190b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 212260a5a19eSTang Chen 2123e1c158e4SDavid Hildenbrand static int __ref try_remove_memory(u64 start, u64 size) 2124bbc76be6SWen Congyang { 2125a08a2ae3SOscar Salvador struct vmem_altmap mhp_altmap = {}; 2126a08a2ae3SOscar Salvador struct vmem_altmap *altmap = NULL; 2127a08a2ae3SOscar Salvador unsigned long nr_vmemmap_pages; 2128e1c158e4SDavid Hildenbrand int rc = 0, nid = NUMA_NO_NODE; 2129993c1aadSWen Congyang 213027356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 213127356f54SToshi Kani 21326677e3eaSYasuaki Ishimatsu /* 2133242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 2134eca499abSPavel Tatashin * whether all memory blocks in question are offline and return error 2135242831ebSRafael J. Wysocki * if this is not the case. 2136e1c158e4SDavid Hildenbrand * 2137e1c158e4SDavid Hildenbrand * While at it, determine the nid. Note that if we'd have mixed nodes, 2138e1c158e4SDavid Hildenbrand * we'd only try to offline the last determined one -- which is good 2139e1c158e4SDavid Hildenbrand * enough for the cases we care about. 21406677e3eaSYasuaki Ishimatsu */ 2141e1c158e4SDavid Hildenbrand rc = walk_memory_blocks(start, size, &nid, check_memblock_offlined_cb); 2142eca499abSPavel Tatashin if (rc) 2143b4223a51SJia He return rc; 21446677e3eaSYasuaki Ishimatsu 2145a08a2ae3SOscar Salvador /* 2146a08a2ae3SOscar Salvador * We only support removing memory added with MHP_MEMMAP_ON_MEMORY in 2147a08a2ae3SOscar Salvador * the same granularity it was added - a single memory block. 2148a08a2ae3SOscar Salvador */ 2149a08a2ae3SOscar Salvador if (memmap_on_memory) { 2150a08a2ae3SOscar Salvador nr_vmemmap_pages = walk_memory_blocks(start, size, NULL, 2151a08a2ae3SOscar Salvador get_nr_vmemmap_pages_cb); 2152a08a2ae3SOscar Salvador if (nr_vmemmap_pages) { 2153a08a2ae3SOscar Salvador if (size != memory_block_size_bytes()) { 2154a08a2ae3SOscar Salvador pr_warn("Refuse to remove %#llx - %#llx," 2155a08a2ae3SOscar Salvador "wrong granularity\n", 2156a08a2ae3SOscar Salvador start, start + size); 2157a08a2ae3SOscar Salvador return -EINVAL; 2158a08a2ae3SOscar Salvador } 2159a08a2ae3SOscar Salvador 2160a08a2ae3SOscar Salvador /* 2161a08a2ae3SOscar Salvador * Let remove_pmd_table->free_hugepage_table do the 2162a08a2ae3SOscar Salvador * right thing if we used vmem_altmap when hot-adding 2163a08a2ae3SOscar Salvador * the range. 2164a08a2ae3SOscar Salvador */ 2165a08a2ae3SOscar Salvador mhp_altmap.alloc = nr_vmemmap_pages; 2166a08a2ae3SOscar Salvador altmap = &mhp_altmap; 2167a08a2ae3SOscar Salvador } 2168a08a2ae3SOscar Salvador } 2169a08a2ae3SOscar Salvador 217046c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 217146c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 217246c66c4bSYasuaki Ishimatsu 2173f1037ec0SDan Williams /* 2174f1037ec0SDan Williams * Memory block device removal under the device_hotplug_lock is 2175f1037ec0SDan Williams * a barrier against racing online attempts. 2176f1037ec0SDan Williams */ 21774c4b7f9bSDavid Hildenbrand remove_memory_block_devices(start, size); 21784c4b7f9bSDavid Hildenbrand 2179f1037ec0SDan Williams mem_hotplug_begin(); 2180f1037ec0SDan Williams 218165a2aa5fSDavid Hildenbrand arch_remove_memory(start, size, altmap); 218252219aeaSDavid Hildenbrand 218352219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 21843ecc6834SMike Rapoport memblock_phys_free(start, size); 218532d1fe8fSAnshuman Khandual memblock_remove(start, size); 218652219aeaSDavid Hildenbrand } 218752219aeaSDavid Hildenbrand 2188cb8e3c8bSDavid Hildenbrand release_mem_region_adjustable(start, size); 218924d335caSWen Congyang 2190e1c158e4SDavid Hildenbrand if (nid != NUMA_NO_NODE) 219160a5a19eSTang Chen try_offline_node(nid); 219260a5a19eSTang Chen 2193bfc8c901SVladimir Davydov mem_hotplug_done(); 2194b4223a51SJia He return 0; 219571088785SBadari Pulavarty } 2196d15e5926SDavid Hildenbrand 2197eca499abSPavel Tatashin /** 21985640c9caSMel Gorman * __remove_memory - Remove memory if every memory block is offline 2199eca499abSPavel Tatashin * @start: physical address of the region to remove 2200eca499abSPavel Tatashin * @size: size of the region to remove 2201eca499abSPavel Tatashin * 2202eca499abSPavel Tatashin * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 2203eca499abSPavel Tatashin * and online/offline operations before this call, as required by 2204eca499abSPavel Tatashin * try_offline_node(). 2205eca499abSPavel Tatashin */ 2206e1c158e4SDavid Hildenbrand void __remove_memory(u64 start, u64 size) 2207d15e5926SDavid Hildenbrand { 2208eca499abSPavel Tatashin 2209eca499abSPavel Tatashin /* 221029a90db9SSouptick Joarder * trigger BUG() if some memory is not offlined prior to calling this 2211eca499abSPavel Tatashin * function 2212eca499abSPavel Tatashin */ 2213e1c158e4SDavid Hildenbrand if (try_remove_memory(start, size)) 2214eca499abSPavel Tatashin BUG(); 2215eca499abSPavel Tatashin } 2216eca499abSPavel Tatashin 2217eca499abSPavel Tatashin /* 2218eca499abSPavel Tatashin * Remove memory if every memory block is offline, otherwise return -EBUSY is 2219eca499abSPavel Tatashin * some memory is not offline 2220eca499abSPavel Tatashin */ 2221e1c158e4SDavid Hildenbrand int remove_memory(u64 start, u64 size) 2222eca499abSPavel Tatashin { 2223eca499abSPavel Tatashin int rc; 2224eca499abSPavel Tatashin 2225d15e5926SDavid Hildenbrand lock_device_hotplug(); 2226e1c158e4SDavid Hildenbrand rc = try_remove_memory(start, size); 2227d15e5926SDavid Hildenbrand unlock_device_hotplug(); 2228eca499abSPavel Tatashin 2229eca499abSPavel Tatashin return rc; 2230d15e5926SDavid Hildenbrand } 223171088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 223208b3acd7SDavid Hildenbrand 22338dc4bb58SDavid Hildenbrand static int try_offline_memory_block(struct memory_block *mem, void *arg) 22348dc4bb58SDavid Hildenbrand { 22358dc4bb58SDavid Hildenbrand uint8_t online_type = MMOP_ONLINE_KERNEL; 22368dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 22378dc4bb58SDavid Hildenbrand struct page *page; 22388dc4bb58SDavid Hildenbrand int rc; 22398dc4bb58SDavid Hildenbrand 224008b3acd7SDavid Hildenbrand /* 22418dc4bb58SDavid Hildenbrand * Sense the online_type via the zone of the memory block. Offlining 22428dc4bb58SDavid Hildenbrand * with multiple zones within one memory block will be rejected 22438dc4bb58SDavid Hildenbrand * by offlining code ... so we don't care about that. 22448dc4bb58SDavid Hildenbrand */ 22458dc4bb58SDavid Hildenbrand page = pfn_to_online_page(section_nr_to_pfn(mem->start_section_nr)); 22468dc4bb58SDavid Hildenbrand if (page && zone_idx(page_zone(page)) == ZONE_MOVABLE) 22478dc4bb58SDavid Hildenbrand online_type = MMOP_ONLINE_MOVABLE; 22488dc4bb58SDavid Hildenbrand 22498dc4bb58SDavid Hildenbrand rc = device_offline(&mem->dev); 22508dc4bb58SDavid Hildenbrand /* 22518dc4bb58SDavid Hildenbrand * Default is MMOP_OFFLINE - change it only if offlining succeeded, 22528dc4bb58SDavid Hildenbrand * so try_reonline_memory_block() can do the right thing. 22538dc4bb58SDavid Hildenbrand */ 22548dc4bb58SDavid Hildenbrand if (!rc) 22558dc4bb58SDavid Hildenbrand **online_types = online_type; 22568dc4bb58SDavid Hildenbrand 22578dc4bb58SDavid Hildenbrand (*online_types)++; 22588dc4bb58SDavid Hildenbrand /* Ignore if already offline. */ 22598dc4bb58SDavid Hildenbrand return rc < 0 ? rc : 0; 22608dc4bb58SDavid Hildenbrand } 22618dc4bb58SDavid Hildenbrand 22628dc4bb58SDavid Hildenbrand static int try_reonline_memory_block(struct memory_block *mem, void *arg) 22638dc4bb58SDavid Hildenbrand { 22648dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 22658dc4bb58SDavid Hildenbrand int rc; 22668dc4bb58SDavid Hildenbrand 22678dc4bb58SDavid Hildenbrand if (**online_types != MMOP_OFFLINE) { 22688dc4bb58SDavid Hildenbrand mem->online_type = **online_types; 22698dc4bb58SDavid Hildenbrand rc = device_online(&mem->dev); 22708dc4bb58SDavid Hildenbrand if (rc < 0) 22718dc4bb58SDavid Hildenbrand pr_warn("%s: Failed to re-online memory: %d", 22728dc4bb58SDavid Hildenbrand __func__, rc); 22738dc4bb58SDavid Hildenbrand } 22748dc4bb58SDavid Hildenbrand 22758dc4bb58SDavid Hildenbrand /* Continue processing all remaining memory blocks. */ 22768dc4bb58SDavid Hildenbrand (*online_types)++; 22778dc4bb58SDavid Hildenbrand return 0; 22788dc4bb58SDavid Hildenbrand } 22798dc4bb58SDavid Hildenbrand 22808dc4bb58SDavid Hildenbrand /* 22818dc4bb58SDavid Hildenbrand * Try to offline and remove memory. Might take a long time to finish in case 22828dc4bb58SDavid Hildenbrand * memory is still in use. Primarily useful for memory devices that logically 22838dc4bb58SDavid Hildenbrand * unplugged all memory (so it's no longer in use) and want to offline + remove 22848dc4bb58SDavid Hildenbrand * that memory. 228508b3acd7SDavid Hildenbrand */ 2286e1c158e4SDavid Hildenbrand int offline_and_remove_memory(u64 start, u64 size) 228708b3acd7SDavid Hildenbrand { 22888dc4bb58SDavid Hildenbrand const unsigned long mb_count = size / memory_block_size_bytes(); 22898dc4bb58SDavid Hildenbrand uint8_t *online_types, *tmp; 22908dc4bb58SDavid Hildenbrand int rc; 229108b3acd7SDavid Hildenbrand 229208b3acd7SDavid Hildenbrand if (!IS_ALIGNED(start, memory_block_size_bytes()) || 22938dc4bb58SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes()) || !size) 22948dc4bb58SDavid Hildenbrand return -EINVAL; 229508b3acd7SDavid Hildenbrand 229608b3acd7SDavid Hildenbrand /* 22978dc4bb58SDavid Hildenbrand * We'll remember the old online type of each memory block, so we can 22988dc4bb58SDavid Hildenbrand * try to revert whatever we did when offlining one memory block fails 22998dc4bb58SDavid Hildenbrand * after offlining some others succeeded. 23008dc4bb58SDavid Hildenbrand */ 23018dc4bb58SDavid Hildenbrand online_types = kmalloc_array(mb_count, sizeof(*online_types), 23028dc4bb58SDavid Hildenbrand GFP_KERNEL); 23038dc4bb58SDavid Hildenbrand if (!online_types) 23048dc4bb58SDavid Hildenbrand return -ENOMEM; 23058dc4bb58SDavid Hildenbrand /* 23068dc4bb58SDavid Hildenbrand * Initialize all states to MMOP_OFFLINE, so when we abort processing in 23078dc4bb58SDavid Hildenbrand * try_offline_memory_block(), we'll skip all unprocessed blocks in 23088dc4bb58SDavid Hildenbrand * try_reonline_memory_block(). 23098dc4bb58SDavid Hildenbrand */ 23108dc4bb58SDavid Hildenbrand memset(online_types, MMOP_OFFLINE, mb_count); 23118dc4bb58SDavid Hildenbrand 23128dc4bb58SDavid Hildenbrand lock_device_hotplug(); 23138dc4bb58SDavid Hildenbrand 23148dc4bb58SDavid Hildenbrand tmp = online_types; 23158dc4bb58SDavid Hildenbrand rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block); 23168dc4bb58SDavid Hildenbrand 23178dc4bb58SDavid Hildenbrand /* 23188dc4bb58SDavid Hildenbrand * In case we succeeded to offline all memory, remove it. 231908b3acd7SDavid Hildenbrand * This cannot fail as it cannot get onlined in the meantime. 232008b3acd7SDavid Hildenbrand */ 232108b3acd7SDavid Hildenbrand if (!rc) { 2322e1c158e4SDavid Hildenbrand rc = try_remove_memory(start, size); 23238dc4bb58SDavid Hildenbrand if (rc) 23248dc4bb58SDavid Hildenbrand pr_err("%s: Failed to remove memory: %d", __func__, rc); 23258dc4bb58SDavid Hildenbrand } 23268dc4bb58SDavid Hildenbrand 23278dc4bb58SDavid Hildenbrand /* 23288dc4bb58SDavid Hildenbrand * Rollback what we did. While memory onlining might theoretically fail 23298dc4bb58SDavid Hildenbrand * (nacked by a notifier), it barely ever happens. 23308dc4bb58SDavid Hildenbrand */ 23318dc4bb58SDavid Hildenbrand if (rc) { 23328dc4bb58SDavid Hildenbrand tmp = online_types; 23338dc4bb58SDavid Hildenbrand walk_memory_blocks(start, size, &tmp, 23348dc4bb58SDavid Hildenbrand try_reonline_memory_block); 233508b3acd7SDavid Hildenbrand } 233608b3acd7SDavid Hildenbrand unlock_device_hotplug(); 233708b3acd7SDavid Hildenbrand 23388dc4bb58SDavid Hildenbrand kfree(online_types); 233908b3acd7SDavid Hildenbrand return rc; 234008b3acd7SDavid Hildenbrand } 234108b3acd7SDavid Hildenbrand EXPORT_SYMBOL_GPL(offline_and_remove_memory); 2342aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 2343