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 456e02c46bSMuchun Song #ifdef CONFIG_MHP_MEMMAP_ON_MEMORY 46e3a9d9fcSOscar Salvador /* 47e3a9d9fcSOscar Salvador * memory_hotplug.memmap_on_memory parameter 48e3a9d9fcSOscar Salvador */ 49e3a9d9fcSOscar Salvador static bool memmap_on_memory __ro_after_init; 5066361095SMuchun Song module_param(memmap_on_memory, bool, 0444); 51e3a9d9fcSOscar Salvador MODULE_PARM_DESC(memmap_on_memory, "Enable memmap on memory for memory hotplug"); 526e02c46bSMuchun Song 5366361095SMuchun Song static inline bool mhp_memmap_on_memory(void) 546e02c46bSMuchun Song { 556e02c46bSMuchun Song return memmap_on_memory; 566e02c46bSMuchun Song } 5766361095SMuchun Song #else 5866361095SMuchun Song static inline bool mhp_memmap_on_memory(void) 5966361095SMuchun Song { 6066361095SMuchun Song return false; 6166361095SMuchun Song } 62e3a9d9fcSOscar Salvador #endif 63a08a2ae3SOscar Salvador 64e83a437fSDavid Hildenbrand enum { 65e83a437fSDavid Hildenbrand ONLINE_POLICY_CONTIG_ZONES = 0, 66e83a437fSDavid Hildenbrand ONLINE_POLICY_AUTO_MOVABLE, 67e83a437fSDavid Hildenbrand }; 68e83a437fSDavid Hildenbrand 69ac62554bSTang Yizhou static const char * const online_policy_to_str[] = { 70e83a437fSDavid Hildenbrand [ONLINE_POLICY_CONTIG_ZONES] = "contig-zones", 71e83a437fSDavid Hildenbrand [ONLINE_POLICY_AUTO_MOVABLE] = "auto-movable", 72e83a437fSDavid Hildenbrand }; 73e83a437fSDavid Hildenbrand 74e83a437fSDavid Hildenbrand static int set_online_policy(const char *val, const struct kernel_param *kp) 75e83a437fSDavid Hildenbrand { 76e83a437fSDavid Hildenbrand int ret = sysfs_match_string(online_policy_to_str, val); 77e83a437fSDavid Hildenbrand 78e83a437fSDavid Hildenbrand if (ret < 0) 79e83a437fSDavid Hildenbrand return ret; 80e83a437fSDavid Hildenbrand *((int *)kp->arg) = ret; 81e83a437fSDavid Hildenbrand return 0; 82e83a437fSDavid Hildenbrand } 83e83a437fSDavid Hildenbrand 84e83a437fSDavid Hildenbrand static int get_online_policy(char *buffer, const struct kernel_param *kp) 85e83a437fSDavid Hildenbrand { 86e83a437fSDavid Hildenbrand return sprintf(buffer, "%s\n", online_policy_to_str[*((int *)kp->arg)]); 87e83a437fSDavid Hildenbrand } 88e83a437fSDavid Hildenbrand 89e83a437fSDavid Hildenbrand /* 90e83a437fSDavid Hildenbrand * memory_hotplug.online_policy: configure online behavior when onlining without 91e83a437fSDavid Hildenbrand * specifying a zone (MMOP_ONLINE) 92e83a437fSDavid Hildenbrand * 93e83a437fSDavid Hildenbrand * "contig-zones": keep zone contiguous 94e83a437fSDavid Hildenbrand * "auto-movable": online memory to ZONE_MOVABLE if the configuration 95e83a437fSDavid Hildenbrand * (auto_movable_ratio, auto_movable_numa_aware) allows for it 96e83a437fSDavid Hildenbrand */ 97e83a437fSDavid Hildenbrand static int online_policy __read_mostly = ONLINE_POLICY_CONTIG_ZONES; 98e83a437fSDavid Hildenbrand static const struct kernel_param_ops online_policy_ops = { 99e83a437fSDavid Hildenbrand .set = set_online_policy, 100e83a437fSDavid Hildenbrand .get = get_online_policy, 101e83a437fSDavid Hildenbrand }; 102e83a437fSDavid Hildenbrand module_param_cb(online_policy, &online_policy_ops, &online_policy, 0644); 103e83a437fSDavid Hildenbrand MODULE_PARM_DESC(online_policy, 104e83a437fSDavid Hildenbrand "Set the online policy (\"contig-zones\", \"auto-movable\") " 105e83a437fSDavid Hildenbrand "Default: \"contig-zones\""); 106e83a437fSDavid Hildenbrand 107e83a437fSDavid Hildenbrand /* 108e83a437fSDavid Hildenbrand * memory_hotplug.auto_movable_ratio: specify maximum MOVABLE:KERNEL ratio 109e83a437fSDavid Hildenbrand * 110e83a437fSDavid Hildenbrand * The ratio represent an upper limit and the kernel might decide to not 111e83a437fSDavid Hildenbrand * online some memory to ZONE_MOVABLE -- e.g., because hotplugged KERNEL memory 112e83a437fSDavid Hildenbrand * doesn't allow for more MOVABLE memory. 113e83a437fSDavid Hildenbrand */ 114e83a437fSDavid Hildenbrand static unsigned int auto_movable_ratio __read_mostly = 301; 115e83a437fSDavid Hildenbrand module_param(auto_movable_ratio, uint, 0644); 116e83a437fSDavid Hildenbrand MODULE_PARM_DESC(auto_movable_ratio, 117e83a437fSDavid Hildenbrand "Set the maximum ratio of MOVABLE:KERNEL memory in the system " 118e83a437fSDavid Hildenbrand "in percent for \"auto-movable\" online policy. Default: 301"); 119e83a437fSDavid Hildenbrand 120e83a437fSDavid Hildenbrand /* 121e83a437fSDavid Hildenbrand * memory_hotplug.auto_movable_numa_aware: consider numa node stats 122e83a437fSDavid Hildenbrand */ 123e83a437fSDavid Hildenbrand #ifdef CONFIG_NUMA 124e83a437fSDavid Hildenbrand static bool auto_movable_numa_aware __read_mostly = true; 125e83a437fSDavid Hildenbrand module_param(auto_movable_numa_aware, bool, 0644); 126e83a437fSDavid Hildenbrand MODULE_PARM_DESC(auto_movable_numa_aware, 127e83a437fSDavid Hildenbrand "Consider numa node stats in addition to global stats in " 128e83a437fSDavid Hildenbrand "\"auto-movable\" online policy. Default: true"); 129e83a437fSDavid Hildenbrand #endif /* CONFIG_NUMA */ 130e83a437fSDavid Hildenbrand 1319d0ad8caSDaniel Kiper /* 1329d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 1339d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 1349d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 1359d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 1369d0ad8caSDaniel Kiper */ 1379d0ad8caSDaniel Kiper 1389d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 139bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 1409d0ad8caSDaniel Kiper 1413f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 14220d6c96bSKOSAKI Motohiro 1433f906ba2SThomas Gleixner void get_online_mems(void) 1443f906ba2SThomas Gleixner { 1453f906ba2SThomas Gleixner percpu_down_read(&mem_hotplug_lock); 1463f906ba2SThomas Gleixner } 147bfc8c901SVladimir Davydov 1483f906ba2SThomas Gleixner void put_online_mems(void) 1493f906ba2SThomas Gleixner { 1503f906ba2SThomas Gleixner percpu_up_read(&mem_hotplug_lock); 1513f906ba2SThomas Gleixner } 152bfc8c901SVladimir Davydov 1534932381eSMichal Hocko bool movable_node_enabled = false; 1544932381eSMichal Hocko 1558604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 1561adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_OFFLINE; 1578604d9e5SVitaly Kuznetsov #else 1581adf8b46SAnshuman Khandual int mhp_default_online_type = MMOP_ONLINE; 1598604d9e5SVitaly Kuznetsov #endif 16031bc3858SVitaly Kuznetsov 16186dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str) 16286dd995dSVitaly Kuznetsov { 1631adf8b46SAnshuman Khandual const int online_type = mhp_online_type_from_str(str); 1645f47adf7SDavid Hildenbrand 1655f47adf7SDavid Hildenbrand if (online_type >= 0) 1661adf8b46SAnshuman Khandual mhp_default_online_type = online_type; 16786dd995dSVitaly Kuznetsov 16886dd995dSVitaly Kuznetsov return 1; 16986dd995dSVitaly Kuznetsov } 17086dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state); 17186dd995dSVitaly Kuznetsov 17230467e0bSDavid Rientjes void mem_hotplug_begin(void) 173bfc8c901SVladimir Davydov { 1743f906ba2SThomas Gleixner cpus_read_lock(); 1753f906ba2SThomas Gleixner percpu_down_write(&mem_hotplug_lock); 176bfc8c901SVladimir Davydov } 177bfc8c901SVladimir Davydov 17830467e0bSDavid Rientjes void mem_hotplug_done(void) 179bfc8c901SVladimir Davydov { 1803f906ba2SThomas Gleixner percpu_up_write(&mem_hotplug_lock); 1813f906ba2SThomas Gleixner cpus_read_unlock(); 182bfc8c901SVladimir Davydov } 18320d6c96bSKOSAKI Motohiro 184357b4da5SJuergen Gross u64 max_mem_size = U64_MAX; 185357b4da5SJuergen Gross 18645e0b78bSKeith Mannthey /* add this memory to iomem resource */ 1877b7b2721SDavid Hildenbrand static struct resource *register_memory_resource(u64 start, u64 size, 1887b7b2721SDavid Hildenbrand const char *resource_name) 18945e0b78bSKeith Mannthey { 1902794129eSDave Hansen struct resource *res; 1912794129eSDave Hansen unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1927b7b2721SDavid Hildenbrand 1937b7b2721SDavid Hildenbrand if (strcmp(resource_name, "System RAM")) 1947cf603d1SDavid Hildenbrand flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED; 195357b4da5SJuergen Gross 196bca3feaaSAnshuman Khandual if (!mhp_range_allowed(start, size, true)) 197bca3feaaSAnshuman Khandual return ERR_PTR(-E2BIG); 198bca3feaaSAnshuman Khandual 199f3cd4c86SBaoquan He /* 200f3cd4c86SBaoquan He * Make sure value parsed from 'mem=' only restricts memory adding 201f3cd4c86SBaoquan He * while booting, so that memory hotplug won't be impacted. Please 202f3cd4c86SBaoquan He * refer to document of 'mem=' in kernel-parameters.txt for more 203f3cd4c86SBaoquan He * details. 204f3cd4c86SBaoquan He */ 205f3cd4c86SBaoquan He if (start + size > max_mem_size && system_state < SYSTEM_RUNNING) 206357b4da5SJuergen Gross return ERR_PTR(-E2BIG); 207357b4da5SJuergen Gross 2082794129eSDave Hansen /* 2092794129eSDave Hansen * Request ownership of the new memory range. This might be 2102794129eSDave Hansen * a child of an existing resource that was present but 2112794129eSDave Hansen * not marked as busy. 2122794129eSDave Hansen */ 2132794129eSDave Hansen res = __request_region(&iomem_resource, start, size, 2142794129eSDave Hansen resource_name, flags); 21545e0b78bSKeith Mannthey 2162794129eSDave Hansen if (!res) { 2172794129eSDave Hansen pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", 2182794129eSDave Hansen start, start + size); 2196f754ba4SVitaly Kuznetsov return ERR_PTR(-EEXIST); 22045e0b78bSKeith Mannthey } 22145e0b78bSKeith Mannthey return res; 22245e0b78bSKeith Mannthey } 22345e0b78bSKeith Mannthey 22445e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 22545e0b78bSKeith Mannthey { 22645e0b78bSKeith Mannthey if (!res) 22745e0b78bSKeith Mannthey return; 22845e0b78bSKeith Mannthey release_resource(res); 22945e0b78bSKeith Mannthey kfree(res); 23045e0b78bSKeith Mannthey } 23145e0b78bSKeith Mannthey 232943189dbSAnshuman Khandual static int check_pfn_span(unsigned long pfn, unsigned long nr_pages) 2337ea62160SDan Williams { 2347ea62160SDan Williams /* 2357ea62160SDan Williams * Disallow all operations smaller than a sub-section and only 2367ea62160SDan Williams * allow operations smaller than a section for 2377ea62160SDan Williams * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range() 2387ea62160SDan Williams * enforces a larger memory_block_size_bytes() granularity for 2397ea62160SDan Williams * memory that will be marked online, so this check should only 2407ea62160SDan Williams * fire for direct arch_{add,remove}_memory() users outside of 2417ea62160SDan Williams * add_memory_resource(). 2427ea62160SDan Williams */ 2437ea62160SDan Williams unsigned long min_align; 2447ea62160SDan Williams 2457ea62160SDan Williams if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 2467ea62160SDan Williams min_align = PAGES_PER_SUBSECTION; 2477ea62160SDan Williams else 2487ea62160SDan Williams min_align = PAGES_PER_SECTION; 249943189dbSAnshuman Khandual if (!IS_ALIGNED(pfn | nr_pages, min_align)) 2507ea62160SDan Williams return -EINVAL; 2517ea62160SDan Williams return 0; 2527ea62160SDan Williams } 2537ea62160SDan Williams 2544edd7cefSDavid Rientjes /* 2559f605f26SDan Williams * Return page for the valid pfn only if the page is online. All pfn 2569f605f26SDan Williams * walkers which rely on the fully initialized page->flags and others 2579f605f26SDan Williams * should use this rather than pfn_valid && pfn_to_page 2589f605f26SDan Williams */ 2599f605f26SDan Williams struct page *pfn_to_online_page(unsigned long pfn) 2609f605f26SDan Williams { 2619f605f26SDan Williams unsigned long nr = pfn_to_section_nr(pfn); 2621f90a347SDan Williams struct dev_pagemap *pgmap; 2639f9b02e5SDan Williams struct mem_section *ms; 2649f605f26SDan Williams 2659f9b02e5SDan Williams if (nr >= NR_MEM_SECTIONS) 2669f605f26SDan Williams return NULL; 2679f9b02e5SDan Williams 2689f9b02e5SDan Williams ms = __nr_to_section(nr); 2699f9b02e5SDan Williams if (!online_section(ms)) 2709f9b02e5SDan Williams return NULL; 2719f9b02e5SDan Williams 2729f9b02e5SDan Williams /* 2739f9b02e5SDan Williams * Save some code text when online_section() + 2749f9b02e5SDan Williams * pfn_section_valid() are sufficient. 2759f9b02e5SDan Williams */ 2769f9b02e5SDan Williams if (IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) && !pfn_valid(pfn)) 2779f9b02e5SDan Williams return NULL; 2789f9b02e5SDan Williams 2799f9b02e5SDan Williams if (!pfn_section_valid(ms, pfn)) 2809f9b02e5SDan Williams return NULL; 2819f9b02e5SDan Williams 2821f90a347SDan Williams if (!online_device_section(ms)) 2831f90a347SDan Williams return pfn_to_page(pfn); 2841f90a347SDan Williams 2851f90a347SDan Williams /* 2861f90a347SDan Williams * Slowpath: when ZONE_DEVICE collides with 2871f90a347SDan Williams * ZONE_{NORMAL,MOVABLE} within the same section some pfns in 2881f90a347SDan Williams * the section may be 'offline' but 'valid'. Only 2891f90a347SDan Williams * get_dev_pagemap() can determine sub-section online status. 2901f90a347SDan Williams */ 2911f90a347SDan Williams pgmap = get_dev_pagemap(pfn, NULL); 2921f90a347SDan Williams put_dev_pagemap(pgmap); 2931f90a347SDan Williams 2941f90a347SDan Williams /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */ 2951f90a347SDan Williams if (pgmap) 2961f90a347SDan Williams return NULL; 2971f90a347SDan Williams 2989f9b02e5SDan Williams return pfn_to_page(pfn); 2999f605f26SDan Williams } 3009f605f26SDan Williams EXPORT_SYMBOL_GPL(pfn_to_online_page); 3019f605f26SDan Williams 3027ea62160SDan Williams int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, 303f5637d3bSLogan Gunthorpe struct mhp_params *params) 3044edd7cefSDavid Rientjes { 3056cdd0b30SDavid Hildenbrand const unsigned long end_pfn = pfn + nr_pages; 3066cdd0b30SDavid Hildenbrand unsigned long cur_nr_pages; 3079a845030SDan Williams int err; 308f5637d3bSLogan Gunthorpe struct vmem_altmap *altmap = params->altmap; 3094b94ffdcSDan Williams 3106366238bSliusongtang if (WARN_ON_ONCE(!pgprot_val(params->pgprot))) 311bfeb022fSLogan Gunthorpe return -EINVAL; 312bfeb022fSLogan Gunthorpe 313bca3feaaSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false)); 314dca4436dSAlastair D'Silva 3154b94ffdcSDan Williams if (altmap) { 3164b94ffdcSDan Williams /* 3174b94ffdcSDan Williams * Validate altmap is within bounds of the total request 3184b94ffdcSDan Williams */ 3197ea62160SDan Williams if (altmap->base_pfn != pfn 3204b94ffdcSDan Williams || vmem_altmap_offset(altmap) > nr_pages) { 3214b94ffdcSDan Williams pr_warn_once("memory add fail, invalid altmap\n"); 3227ea62160SDan Williams return -EINVAL; 3234b94ffdcSDan Williams } 3244b94ffdcSDan Williams altmap->alloc = 0; 3254b94ffdcSDan Williams } 3264b94ffdcSDan Williams 327943189dbSAnshuman Khandual if (check_pfn_span(pfn, nr_pages)) { 328943189dbSAnshuman Khandual WARN(1, "Misaligned %s start: %#lx end: #%lx\n", __func__, pfn, pfn + nr_pages - 1); 329943189dbSAnshuman Khandual return -EINVAL; 330943189dbSAnshuman Khandual } 3317ea62160SDan Williams 3326cdd0b30SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 3336cdd0b30SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 3346cdd0b30SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 3356cdd0b30SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 336e3246d8fSJoao Martins err = sparse_add_section(nid, pfn, cur_nr_pages, altmap, 337e3246d8fSJoao Martins params->pgmap); 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 527943189dbSAnshuman Khandual if (check_pfn_span(pfn, nr_pages)) { 528943189dbSAnshuman Khandual WARN(1, "Misaligned %s start: %#lx end: #%lx\n", __func__, pfn, pfn + nr_pages - 1); 5297ea62160SDan Williams return; 530943189dbSAnshuman Khandual } 531ea01ea93SBadari Pulavarty 53252fb87c8SDavid Hildenbrand for (; pfn < end_pfn; pfn += cur_nr_pages) { 533dd33ad7bSMichal Hocko cond_resched(); 53452fb87c8SDavid Hildenbrand /* Select all remaining pages up to the next section boundary */ 535a11b9419SDavid Hildenbrand cur_nr_pages = min(end_pfn - pfn, 536a11b9419SDavid Hildenbrand SECTION_ALIGN_UP(pfn + 1) - pfn); 53752fb87c8SDavid Hildenbrand __remove_section(pfn, cur_nr_pages, map_offset, altmap); 5384b94ffdcSDan Williams map_offset = 0; 539ea01ea93SBadari Pulavarty } 540ea01ea93SBadari Pulavarty } 541ea01ea93SBadari Pulavarty 5429d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 5439d0ad8caSDaniel Kiper { 5449d0ad8caSDaniel Kiper int rc = -EINVAL; 5459d0ad8caSDaniel Kiper 546bfc8c901SVladimir Davydov get_online_mems(); 547bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5489d0ad8caSDaniel Kiper 5499d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 5509d0ad8caSDaniel Kiper online_page_callback = callback; 5519d0ad8caSDaniel Kiper rc = 0; 5529d0ad8caSDaniel Kiper } 5539d0ad8caSDaniel Kiper 554bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 555bfc8c901SVladimir Davydov put_online_mems(); 5569d0ad8caSDaniel Kiper 5579d0ad8caSDaniel Kiper return rc; 5589d0ad8caSDaniel Kiper } 5599d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 5609d0ad8caSDaniel Kiper 5619d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 5629d0ad8caSDaniel Kiper { 5639d0ad8caSDaniel Kiper int rc = -EINVAL; 5649d0ad8caSDaniel Kiper 565bfc8c901SVladimir Davydov get_online_mems(); 566bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5679d0ad8caSDaniel Kiper 5689d0ad8caSDaniel Kiper if (online_page_callback == callback) { 5699d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 5709d0ad8caSDaniel Kiper rc = 0; 5719d0ad8caSDaniel Kiper } 5729d0ad8caSDaniel Kiper 573bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 574bfc8c901SVladimir Davydov put_online_mems(); 5759d0ad8caSDaniel Kiper 5769d0ad8caSDaniel Kiper return rc; 5779d0ad8caSDaniel Kiper } 5789d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 5799d0ad8caSDaniel Kiper 58018db1491SDavid Hildenbrand void generic_online_page(struct page *page, unsigned int order) 5819d0ad8caSDaniel Kiper { 582c87cbc1fSVlastimil Babka /* 583c87cbc1fSVlastimil Babka * Freeing the page with debug_pagealloc enabled will try to unmap it, 584c87cbc1fSVlastimil Babka * so we should map it first. This is better than introducing a special 585c87cbc1fSVlastimil Babka * case in page freeing fast path. 586c87cbc1fSVlastimil Babka */ 58777bc7fd6SMike Rapoport debug_pagealloc_map_pages(page, 1 << order); 588a9cd410aSArun KS __free_pages_core(page, order); 589a9cd410aSArun KS totalram_pages_add(1UL << order); 590a9cd410aSArun KS } 59118db1491SDavid Hildenbrand EXPORT_SYMBOL_GPL(generic_online_page); 592a9cd410aSArun KS 593aac65321SDavid Hildenbrand static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages) 5943947be19SDave Hansen { 595b2c2ab20SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 596b2c2ab20SDavid Hildenbrand unsigned long pfn; 5972d070eabSMichal Hocko 598b2c2ab20SDavid Hildenbrand /* 599aac65321SDavid Hildenbrand * Online the pages in MAX_ORDER - 1 aligned chunks. The callback might 600aac65321SDavid Hildenbrand * decide to not expose all pages to the buddy (e.g., expose them 601aac65321SDavid Hildenbrand * later). We account all pages as being online and belonging to this 602aac65321SDavid Hildenbrand * zone ("present"). 603a08a2ae3SOscar Salvador * When using memmap_on_memory, the range might not be aligned to 604a08a2ae3SOscar Salvador * MAX_ORDER_NR_PAGES - 1, but pageblock aligned. __ffs() will detect 605a08a2ae3SOscar Salvador * this and the first chunk to online will be pageblock_nr_pages. 606b2c2ab20SDavid Hildenbrand */ 607a08a2ae3SOscar Salvador for (pfn = start_pfn; pfn < end_pfn;) { 608a08a2ae3SOscar Salvador int order = min(MAX_ORDER - 1UL, __ffs(pfn)); 609a08a2ae3SOscar Salvador 610a08a2ae3SOscar Salvador (*online_page_callback)(pfn_to_page(pfn), order); 611a08a2ae3SOscar Salvador pfn += (1UL << order); 612a08a2ae3SOscar Salvador } 6132d070eabSMichal Hocko 614b2c2ab20SDavid Hildenbrand /* mark all involved sections as online */ 615b2c2ab20SDavid Hildenbrand online_mem_sections(start_pfn, end_pfn); 61675884fb1SKAMEZAWA Hiroyuki } 61775884fb1SKAMEZAWA Hiroyuki 618d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 619d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 620d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 621d9713679SLai Jiangshan { 622d9713679SLai Jiangshan int nid = zone_to_nid(zone); 623d9713679SLai Jiangshan 62498fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 62598fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 6266715ddf9SLai Jiangshan 6276715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 628d9713679SLai Jiangshan arg->status_change_nid = nid; 6298efe33f4SOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 6308efe33f4SOscar Salvador arg->status_change_nid_normal = nid; 631d9713679SLai Jiangshan } 632d9713679SLai Jiangshan 633d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 634d9713679SLai Jiangshan { 635d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 636d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 637d9713679SLai Jiangshan 63883d83612SOscar Salvador if (arg->status_change_nid >= 0) 6396715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 640d9713679SLai Jiangshan } 641d9713679SLai Jiangshan 642f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 643f1dd2cd1SMichal Hocko unsigned long nr_pages) 644f1dd2cd1SMichal Hocko { 645f1dd2cd1SMichal Hocko unsigned long old_end_pfn = zone_end_pfn(zone); 646f1dd2cd1SMichal Hocko 647f1dd2cd1SMichal Hocko if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 648f1dd2cd1SMichal Hocko zone->zone_start_pfn = start_pfn; 649f1dd2cd1SMichal Hocko 650f1dd2cd1SMichal Hocko zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 651f1dd2cd1SMichal Hocko } 652f1dd2cd1SMichal Hocko 653f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 654f1dd2cd1SMichal Hocko unsigned long nr_pages) 655f1dd2cd1SMichal Hocko { 656f1dd2cd1SMichal Hocko unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 657f1dd2cd1SMichal Hocko 658f1dd2cd1SMichal Hocko if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 659f1dd2cd1SMichal Hocko pgdat->node_start_pfn = start_pfn; 660f1dd2cd1SMichal Hocko 661f1dd2cd1SMichal Hocko pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 662f1dd2cd1SMichal Hocko 6633fccb74cSDavid Hildenbrand } 6641f90a347SDan Williams 665ed7802ddSMuchun Song #ifdef CONFIG_ZONE_DEVICE 6661f90a347SDan Williams static void section_taint_zone_device(unsigned long pfn) 6671f90a347SDan Williams { 6681f90a347SDan Williams struct mem_section *ms = __pfn_to_section(pfn); 6691f90a347SDan Williams 6701f90a347SDan Williams ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE; 6711f90a347SDan Williams } 672ed7802ddSMuchun Song #else 673ed7802ddSMuchun Song static inline void section_taint_zone_device(unsigned long pfn) 674ed7802ddSMuchun Song { 675ed7802ddSMuchun Song } 676ed7802ddSMuchun Song #endif 6771f90a347SDan Williams 6783fccb74cSDavid Hildenbrand /* 6793fccb74cSDavid Hildenbrand * Associate the pfn range with the given zone, initializing the memmaps 6803fccb74cSDavid Hildenbrand * and resizing the pgdat/zone data to span the added pages. After this 6813fccb74cSDavid Hildenbrand * call, all affected pages are PG_reserved. 682d882c006SDavid Hildenbrand * 683d882c006SDavid Hildenbrand * All aligned pageblocks are initialized to the specified migratetype 684d882c006SDavid Hildenbrand * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related 685d882c006SDavid Hildenbrand * zone stats (e.g., nr_isolate_pageblock) are touched. 6863fccb74cSDavid Hildenbrand */ 687a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 688d882c006SDavid Hildenbrand unsigned long nr_pages, 689d882c006SDavid Hildenbrand struct vmem_altmap *altmap, int migratetype) 690f1dd2cd1SMichal Hocko { 691f1dd2cd1SMichal Hocko struct pglist_data *pgdat = zone->zone_pgdat; 692f1dd2cd1SMichal Hocko int nid = pgdat->node_id; 693f1dd2cd1SMichal Hocko 694f1dd2cd1SMichal Hocko clear_zone_contiguous(zone); 695f1dd2cd1SMichal Hocko 696fa004ab7SWei Yang if (zone_is_empty(zone)) 697fa004ab7SWei Yang init_currently_empty_zone(zone, start_pfn, nr_pages); 698f1dd2cd1SMichal Hocko resize_zone_range(zone, start_pfn, nr_pages); 699f1dd2cd1SMichal Hocko resize_pgdat_range(pgdat, start_pfn, nr_pages); 700f1dd2cd1SMichal Hocko 701f1dd2cd1SMichal Hocko /* 7021f90a347SDan Williams * Subsection population requires care in pfn_to_online_page(). 7031f90a347SDan Williams * Set the taint to enable the slow path detection of 7041f90a347SDan Williams * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE} 7051f90a347SDan Williams * section. 7061f90a347SDan Williams */ 7071f90a347SDan Williams if (zone_is_zone_device(zone)) { 7081f90a347SDan Williams if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION)) 7091f90a347SDan Williams section_taint_zone_device(start_pfn); 7101f90a347SDan Williams if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)) 7111f90a347SDan Williams section_taint_zone_device(start_pfn + nr_pages); 7121f90a347SDan Williams } 7131f90a347SDan Williams 7141f90a347SDan Williams /* 715f1dd2cd1SMichal Hocko * TODO now we have a visible range of pages which are not associated 716f1dd2cd1SMichal Hocko * with their zone properly. Not nice but set_pfnblock_flags_mask 717f1dd2cd1SMichal Hocko * expects the zone spans the pfn range. All the pages in the range 718f1dd2cd1SMichal Hocko * are reserved so nobody should be touching them so we should be safe 719f1dd2cd1SMichal Hocko */ 720ab28cb6eSBaoquan He memmap_init_range(nr_pages, nid, zone_idx(zone), start_pfn, 0, 721d882c006SDavid Hildenbrand MEMINIT_HOTPLUG, altmap, migratetype); 722f1dd2cd1SMichal Hocko 723f1dd2cd1SMichal Hocko set_zone_contiguous(zone); 724f1dd2cd1SMichal Hocko } 725f1dd2cd1SMichal Hocko 726e83a437fSDavid Hildenbrand struct auto_movable_stats { 727e83a437fSDavid Hildenbrand unsigned long kernel_early_pages; 728e83a437fSDavid Hildenbrand unsigned long movable_pages; 729e83a437fSDavid Hildenbrand }; 730e83a437fSDavid Hildenbrand 731e83a437fSDavid Hildenbrand static void auto_movable_stats_account_zone(struct auto_movable_stats *stats, 732e83a437fSDavid Hildenbrand struct zone *zone) 733e83a437fSDavid Hildenbrand { 734e83a437fSDavid Hildenbrand if (zone_idx(zone) == ZONE_MOVABLE) { 735e83a437fSDavid Hildenbrand stats->movable_pages += zone->present_pages; 736e83a437fSDavid Hildenbrand } else { 737e83a437fSDavid Hildenbrand stats->kernel_early_pages += zone->present_early_pages; 738e83a437fSDavid Hildenbrand #ifdef CONFIG_CMA 739e83a437fSDavid Hildenbrand /* 740e83a437fSDavid Hildenbrand * CMA pages (never on hotplugged memory) behave like 741e83a437fSDavid Hildenbrand * ZONE_MOVABLE. 742e83a437fSDavid Hildenbrand */ 743e83a437fSDavid Hildenbrand stats->movable_pages += zone->cma_pages; 744e83a437fSDavid Hildenbrand stats->kernel_early_pages -= zone->cma_pages; 745e83a437fSDavid Hildenbrand #endif /* CONFIG_CMA */ 746e83a437fSDavid Hildenbrand } 747e83a437fSDavid Hildenbrand } 7483fcebf90SDavid Hildenbrand struct auto_movable_group_stats { 7493fcebf90SDavid Hildenbrand unsigned long movable_pages; 7503fcebf90SDavid Hildenbrand unsigned long req_kernel_early_pages; 7513fcebf90SDavid Hildenbrand }; 752e83a437fSDavid Hildenbrand 7533fcebf90SDavid Hildenbrand static int auto_movable_stats_account_group(struct memory_group *group, 7543fcebf90SDavid Hildenbrand void *arg) 755e83a437fSDavid Hildenbrand { 7563fcebf90SDavid Hildenbrand const int ratio = READ_ONCE(auto_movable_ratio); 7573fcebf90SDavid Hildenbrand struct auto_movable_group_stats *stats = arg; 7583fcebf90SDavid Hildenbrand long pages; 7593fcebf90SDavid Hildenbrand 7603fcebf90SDavid Hildenbrand /* 7613fcebf90SDavid Hildenbrand * We don't support modifying the config while the auto-movable online 7623fcebf90SDavid Hildenbrand * policy is already enabled. Just avoid the division by zero below. 7633fcebf90SDavid Hildenbrand */ 7643fcebf90SDavid Hildenbrand if (!ratio) 7653fcebf90SDavid Hildenbrand return 0; 7663fcebf90SDavid Hildenbrand 7673fcebf90SDavid Hildenbrand /* 7683fcebf90SDavid Hildenbrand * Calculate how many early kernel pages this group requires to 7693fcebf90SDavid Hildenbrand * satisfy the configured zone ratio. 7703fcebf90SDavid Hildenbrand */ 7713fcebf90SDavid Hildenbrand pages = group->present_movable_pages * 100 / ratio; 7723fcebf90SDavid Hildenbrand pages -= group->present_kernel_pages; 7733fcebf90SDavid Hildenbrand 7743fcebf90SDavid Hildenbrand if (pages > 0) 7753fcebf90SDavid Hildenbrand stats->req_kernel_early_pages += pages; 7763fcebf90SDavid Hildenbrand stats->movable_pages += group->present_movable_pages; 7773fcebf90SDavid Hildenbrand return 0; 7783fcebf90SDavid Hildenbrand } 7793fcebf90SDavid Hildenbrand 7803fcebf90SDavid Hildenbrand static bool auto_movable_can_online_movable(int nid, struct memory_group *group, 7813fcebf90SDavid Hildenbrand unsigned long nr_pages) 7823fcebf90SDavid Hildenbrand { 783e83a437fSDavid Hildenbrand unsigned long kernel_early_pages, movable_pages; 7843fcebf90SDavid Hildenbrand struct auto_movable_group_stats group_stats = {}; 7853fcebf90SDavid Hildenbrand struct auto_movable_stats stats = {}; 786e83a437fSDavid Hildenbrand pg_data_t *pgdat = NODE_DATA(nid); 787e83a437fSDavid Hildenbrand struct zone *zone; 788e83a437fSDavid Hildenbrand int i; 789e83a437fSDavid Hildenbrand 790e83a437fSDavid Hildenbrand /* Walk all relevant zones and collect MOVABLE vs. KERNEL stats. */ 791e83a437fSDavid Hildenbrand if (nid == NUMA_NO_NODE) { 792e83a437fSDavid Hildenbrand /* TODO: cache values */ 793e83a437fSDavid Hildenbrand for_each_populated_zone(zone) 794e83a437fSDavid Hildenbrand auto_movable_stats_account_zone(&stats, zone); 795e83a437fSDavid Hildenbrand } else { 796e83a437fSDavid Hildenbrand for (i = 0; i < MAX_NR_ZONES; i++) { 797e83a437fSDavid Hildenbrand zone = pgdat->node_zones + i; 798e83a437fSDavid Hildenbrand if (populated_zone(zone)) 799e83a437fSDavid Hildenbrand auto_movable_stats_account_zone(&stats, zone); 800e83a437fSDavid Hildenbrand } 801e83a437fSDavid Hildenbrand } 802e83a437fSDavid Hildenbrand 803e83a437fSDavid Hildenbrand kernel_early_pages = stats.kernel_early_pages; 804e83a437fSDavid Hildenbrand movable_pages = stats.movable_pages; 805e83a437fSDavid Hildenbrand 806e83a437fSDavid Hildenbrand /* 8073fcebf90SDavid Hildenbrand * Kernel memory inside dynamic memory group allows for more MOVABLE 8083fcebf90SDavid Hildenbrand * memory within the same group. Remove the effect of all but the 8093fcebf90SDavid Hildenbrand * current group from the stats. 8103fcebf90SDavid Hildenbrand */ 8113fcebf90SDavid Hildenbrand walk_dynamic_memory_groups(nid, auto_movable_stats_account_group, 8123fcebf90SDavid Hildenbrand group, &group_stats); 8133fcebf90SDavid Hildenbrand if (kernel_early_pages <= group_stats.req_kernel_early_pages) 8143fcebf90SDavid Hildenbrand return false; 8153fcebf90SDavid Hildenbrand kernel_early_pages -= group_stats.req_kernel_early_pages; 8163fcebf90SDavid Hildenbrand movable_pages -= group_stats.movable_pages; 8173fcebf90SDavid Hildenbrand 8183fcebf90SDavid Hildenbrand if (group && group->is_dynamic) 8193fcebf90SDavid Hildenbrand kernel_early_pages += group->present_kernel_pages; 8203fcebf90SDavid Hildenbrand 8213fcebf90SDavid Hildenbrand /* 822e83a437fSDavid Hildenbrand * Test if we could online the given number of pages to ZONE_MOVABLE 823e83a437fSDavid Hildenbrand * and still stay in the configured ratio. 824e83a437fSDavid Hildenbrand */ 825e83a437fSDavid Hildenbrand movable_pages += nr_pages; 826e83a437fSDavid Hildenbrand return movable_pages <= (auto_movable_ratio * kernel_early_pages) / 100; 827e83a437fSDavid Hildenbrand } 828e83a437fSDavid Hildenbrand 829f1dd2cd1SMichal Hocko /* 830c246a213SMichal Hocko * Returns a default kernel memory zone for the given pfn range. 831c246a213SMichal Hocko * If no kernel zone covers this pfn range it will automatically go 832c246a213SMichal Hocko * to the ZONE_NORMAL. 833c246a213SMichal Hocko */ 834c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 835c246a213SMichal Hocko unsigned long nr_pages) 836c246a213SMichal Hocko { 837c246a213SMichal Hocko struct pglist_data *pgdat = NODE_DATA(nid); 838c246a213SMichal Hocko int zid; 839c246a213SMichal Hocko 840d6aad201SMiaohe Lin for (zid = 0; zid < ZONE_NORMAL; zid++) { 841c246a213SMichal Hocko struct zone *zone = &pgdat->node_zones[zid]; 842c246a213SMichal Hocko 843c246a213SMichal Hocko if (zone_intersects(zone, start_pfn, nr_pages)) 844c246a213SMichal Hocko return zone; 845c246a213SMichal Hocko } 846c246a213SMichal Hocko 847c246a213SMichal Hocko return &pgdat->node_zones[ZONE_NORMAL]; 848c246a213SMichal Hocko } 849c246a213SMichal Hocko 850e83a437fSDavid Hildenbrand /* 851e83a437fSDavid Hildenbrand * Determine to which zone to online memory dynamically based on user 852e83a437fSDavid Hildenbrand * configuration and system stats. We care about the following ratio: 853e83a437fSDavid Hildenbrand * 854e83a437fSDavid Hildenbrand * MOVABLE : KERNEL 855e83a437fSDavid Hildenbrand * 856e83a437fSDavid Hildenbrand * Whereby MOVABLE is memory in ZONE_MOVABLE and KERNEL is memory in 857e83a437fSDavid Hildenbrand * one of the kernel zones. CMA pages inside one of the kernel zones really 858e83a437fSDavid Hildenbrand * behaves like ZONE_MOVABLE, so we treat them accordingly. 859e83a437fSDavid Hildenbrand * 860e83a437fSDavid Hildenbrand * We don't allow for hotplugged memory in a KERNEL zone to increase the 861e83a437fSDavid Hildenbrand * amount of MOVABLE memory we can have, so we end up with: 862e83a437fSDavid Hildenbrand * 863e83a437fSDavid Hildenbrand * MOVABLE : KERNEL_EARLY 864e83a437fSDavid Hildenbrand * 865e83a437fSDavid Hildenbrand * Whereby KERNEL_EARLY is memory in one of the kernel zones, available sinze 866e83a437fSDavid Hildenbrand * boot. We base our calculation on KERNEL_EARLY internally, because: 867e83a437fSDavid Hildenbrand * 868e83a437fSDavid Hildenbrand * a) Hotplugged memory in one of the kernel zones can sometimes still get 869e83a437fSDavid Hildenbrand * hotunplugged, especially when hot(un)plugging individual memory blocks. 870e83a437fSDavid Hildenbrand * There is no coordination across memory devices, therefore "automatic" 871e83a437fSDavid Hildenbrand * hotunplugging, as implemented in hypervisors, could result in zone 872e83a437fSDavid Hildenbrand * imbalances. 873e83a437fSDavid Hildenbrand * b) Early/boot memory in one of the kernel zones can usually not get 874e83a437fSDavid Hildenbrand * hotunplugged again (e.g., no firmware interface to unplug, fragmented 875e83a437fSDavid Hildenbrand * with unmovable allocations). While there are corner cases where it might 876e83a437fSDavid Hildenbrand * still work, it is barely relevant in practice. 877e83a437fSDavid Hildenbrand * 8783fcebf90SDavid Hildenbrand * Exceptions are dynamic memory groups, which allow for more MOVABLE 8793fcebf90SDavid Hildenbrand * memory within the same memory group -- because in that case, there is 8803fcebf90SDavid Hildenbrand * coordination within the single memory device managed by a single driver. 8813fcebf90SDavid Hildenbrand * 882e83a437fSDavid Hildenbrand * We rely on "present pages" instead of "managed pages", as the latter is 883e83a437fSDavid Hildenbrand * highly unreliable and dynamic in virtualized environments, and does not 884e83a437fSDavid Hildenbrand * consider boot time allocations. For example, memory ballooning adjusts the 885e83a437fSDavid Hildenbrand * managed pages when inflating/deflating the balloon, and balloon compaction 886e83a437fSDavid Hildenbrand * can even migrate inflated pages between zones. 887e83a437fSDavid Hildenbrand * 888e83a437fSDavid Hildenbrand * Using "present pages" is better but some things to keep in mind are: 889e83a437fSDavid Hildenbrand * 890e83a437fSDavid Hildenbrand * a) Some memblock allocations, such as for the crashkernel area, are 891e83a437fSDavid Hildenbrand * effectively unused by the kernel, yet they account to "present pages". 892e83a437fSDavid Hildenbrand * Fortunately, these allocations are comparatively small in relevant setups 893e83a437fSDavid Hildenbrand * (e.g., fraction of system memory). 894e83a437fSDavid Hildenbrand * b) Some hotplugged memory blocks in virtualized environments, esecially 895e83a437fSDavid Hildenbrand * hotplugged by virtio-mem, look like they are completely present, however, 896e83a437fSDavid Hildenbrand * only parts of the memory block are actually currently usable. 897e83a437fSDavid Hildenbrand * "present pages" is an upper limit that can get reached at runtime. As 898e83a437fSDavid Hildenbrand * we base our calculations on KERNEL_EARLY, this is not an issue. 899e83a437fSDavid Hildenbrand */ 900445fcf7cSDavid Hildenbrand static struct zone *auto_movable_zone_for_pfn(int nid, 901445fcf7cSDavid Hildenbrand struct memory_group *group, 902445fcf7cSDavid Hildenbrand unsigned long pfn, 903e83a437fSDavid Hildenbrand unsigned long nr_pages) 904e83a437fSDavid Hildenbrand { 905445fcf7cSDavid Hildenbrand unsigned long online_pages = 0, max_pages, end_pfn; 906445fcf7cSDavid Hildenbrand struct page *page; 907445fcf7cSDavid Hildenbrand 908e83a437fSDavid Hildenbrand if (!auto_movable_ratio) 909e83a437fSDavid Hildenbrand goto kernel_zone; 910e83a437fSDavid Hildenbrand 911445fcf7cSDavid Hildenbrand if (group && !group->is_dynamic) { 912445fcf7cSDavid Hildenbrand max_pages = group->s.max_pages; 913445fcf7cSDavid Hildenbrand online_pages = group->present_movable_pages; 914445fcf7cSDavid Hildenbrand 915445fcf7cSDavid Hildenbrand /* If anything is !MOVABLE online the rest !MOVABLE. */ 916445fcf7cSDavid Hildenbrand if (group->present_kernel_pages) 917445fcf7cSDavid Hildenbrand goto kernel_zone; 918445fcf7cSDavid Hildenbrand } else if (!group || group->d.unit_pages == nr_pages) { 919445fcf7cSDavid Hildenbrand max_pages = nr_pages; 920445fcf7cSDavid Hildenbrand } else { 921445fcf7cSDavid Hildenbrand max_pages = group->d.unit_pages; 922445fcf7cSDavid Hildenbrand /* 923445fcf7cSDavid Hildenbrand * Take a look at all online sections in the current unit. 924445fcf7cSDavid Hildenbrand * We can safely assume that all pages within a section belong 925445fcf7cSDavid Hildenbrand * to the same zone, because dynamic memory groups only deal 926445fcf7cSDavid Hildenbrand * with hotplugged memory. 927445fcf7cSDavid Hildenbrand */ 928445fcf7cSDavid Hildenbrand pfn = ALIGN_DOWN(pfn, group->d.unit_pages); 929445fcf7cSDavid Hildenbrand end_pfn = pfn + group->d.unit_pages; 930445fcf7cSDavid Hildenbrand for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 931445fcf7cSDavid Hildenbrand page = pfn_to_online_page(pfn); 932445fcf7cSDavid Hildenbrand if (!page) 933445fcf7cSDavid Hildenbrand continue; 934445fcf7cSDavid Hildenbrand /* If anything is !MOVABLE online the rest !MOVABLE. */ 93507252dfeSKefeng Wang if (!is_zone_movable_page(page)) 936445fcf7cSDavid Hildenbrand goto kernel_zone; 937445fcf7cSDavid Hildenbrand online_pages += PAGES_PER_SECTION; 938445fcf7cSDavid Hildenbrand } 939445fcf7cSDavid Hildenbrand } 940445fcf7cSDavid Hildenbrand 941445fcf7cSDavid Hildenbrand /* 942445fcf7cSDavid Hildenbrand * Online MOVABLE if we could *currently* online all remaining parts 943445fcf7cSDavid Hildenbrand * MOVABLE. We expect to (add+) online them immediately next, so if 944445fcf7cSDavid Hildenbrand * nobody interferes, all will be MOVABLE if possible. 945445fcf7cSDavid Hildenbrand */ 946445fcf7cSDavid Hildenbrand nr_pages = max_pages - online_pages; 9473fcebf90SDavid Hildenbrand if (!auto_movable_can_online_movable(NUMA_NO_NODE, group, nr_pages)) 948e83a437fSDavid Hildenbrand goto kernel_zone; 949e83a437fSDavid Hildenbrand 950e83a437fSDavid Hildenbrand #ifdef CONFIG_NUMA 951e83a437fSDavid Hildenbrand if (auto_movable_numa_aware && 9523fcebf90SDavid Hildenbrand !auto_movable_can_online_movable(nid, group, nr_pages)) 953e83a437fSDavid Hildenbrand goto kernel_zone; 954e83a437fSDavid Hildenbrand #endif /* CONFIG_NUMA */ 955e83a437fSDavid Hildenbrand 956e83a437fSDavid Hildenbrand return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 957e83a437fSDavid Hildenbrand kernel_zone: 958e83a437fSDavid Hildenbrand return default_kernel_zone_for_pfn(nid, pfn, nr_pages); 959e83a437fSDavid Hildenbrand } 960e83a437fSDavid Hildenbrand 961c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 962c6f03e29SMichal Hocko unsigned long nr_pages) 963e5e68930SMichal Hocko { 964c6f03e29SMichal Hocko struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 965c6f03e29SMichal Hocko nr_pages); 966c6f03e29SMichal Hocko struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 967c6f03e29SMichal Hocko bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 968c6f03e29SMichal Hocko bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 969e5e68930SMichal Hocko 970e5e68930SMichal Hocko /* 971c6f03e29SMichal Hocko * We inherit the existing zone in a simple case where zones do not 972c6f03e29SMichal Hocko * overlap in the given range 973e5e68930SMichal Hocko */ 974c6f03e29SMichal Hocko if (in_kernel ^ in_movable) 975c6f03e29SMichal Hocko return (in_kernel) ? kernel_zone : movable_zone; 976e5e68930SMichal Hocko 977c6f03e29SMichal Hocko /* 978c6f03e29SMichal Hocko * If the range doesn't belong to any zone or two zones overlap in the 979c6f03e29SMichal Hocko * given range then we use movable zone only if movable_node is 980c6f03e29SMichal Hocko * enabled because we always online to a kernel zone by default. 981c6f03e29SMichal Hocko */ 982c6f03e29SMichal Hocko return movable_node_enabled ? movable_zone : kernel_zone; 9839f123ab5SMichal Hocko } 9849f123ab5SMichal Hocko 9857cf209baSDavid Hildenbrand struct zone *zone_for_pfn_range(int online_type, int nid, 986445fcf7cSDavid Hildenbrand struct memory_group *group, unsigned long start_pfn, 987e5e68930SMichal Hocko unsigned long nr_pages) 988f1dd2cd1SMichal Hocko { 989c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_KERNEL) 990c6f03e29SMichal Hocko return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 991f1dd2cd1SMichal Hocko 992c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_MOVABLE) 993c6f03e29SMichal Hocko return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 994f1dd2cd1SMichal Hocko 995e83a437fSDavid Hildenbrand if (online_policy == ONLINE_POLICY_AUTO_MOVABLE) 996445fcf7cSDavid Hildenbrand return auto_movable_zone_for_pfn(nid, group, start_pfn, nr_pages); 997e83a437fSDavid Hildenbrand 998c6f03e29SMichal Hocko return default_zone_for_pfn(nid, start_pfn, nr_pages); 999e5e68930SMichal Hocko } 1000e5e68930SMichal Hocko 1001a08a2ae3SOscar Salvador /* 1002a08a2ae3SOscar Salvador * This function should only be called by memory_block_{online,offline}, 1003a08a2ae3SOscar Salvador * and {online,offline}_pages. 1004a08a2ae3SOscar Salvador */ 1005836809ecSDavid Hildenbrand void adjust_present_page_count(struct page *page, struct memory_group *group, 1006836809ecSDavid Hildenbrand long nr_pages) 1007f9901144SDavid Hildenbrand { 10084b097002SDavid Hildenbrand struct zone *zone = page_zone(page); 1009836809ecSDavid Hildenbrand const bool movable = zone_idx(zone) == ZONE_MOVABLE; 10104b097002SDavid Hildenbrand 10114b097002SDavid Hildenbrand /* 10124b097002SDavid Hildenbrand * We only support onlining/offlining/adding/removing of complete 10134b097002SDavid Hildenbrand * memory blocks; therefore, either all is either early or hotplugged. 10144b097002SDavid Hildenbrand */ 10154b097002SDavid Hildenbrand if (early_section(__pfn_to_section(page_to_pfn(page)))) 10164b097002SDavid Hildenbrand zone->present_early_pages += nr_pages; 1017f9901144SDavid Hildenbrand zone->present_pages += nr_pages; 1018f9901144SDavid Hildenbrand zone->zone_pgdat->node_present_pages += nr_pages; 1019836809ecSDavid Hildenbrand 1020836809ecSDavid Hildenbrand if (group && movable) 1021836809ecSDavid Hildenbrand group->present_movable_pages += nr_pages; 1022836809ecSDavid Hildenbrand else if (group && !movable) 1023836809ecSDavid Hildenbrand group->present_kernel_pages += nr_pages; 1024f9901144SDavid Hildenbrand } 1025f9901144SDavid Hildenbrand 1026a08a2ae3SOscar Salvador int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages, 1027a08a2ae3SOscar Salvador struct zone *zone) 1028a08a2ae3SOscar Salvador { 1029a08a2ae3SOscar Salvador unsigned long end_pfn = pfn + nr_pages; 103066361095SMuchun Song int ret, i; 1031a08a2ae3SOscar Salvador 1032a08a2ae3SOscar Salvador ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages)); 1033a08a2ae3SOscar Salvador if (ret) 1034a08a2ae3SOscar Salvador return ret; 1035a08a2ae3SOscar Salvador 1036a08a2ae3SOscar Salvador move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_UNMOVABLE); 1037a08a2ae3SOscar Salvador 103866361095SMuchun Song for (i = 0; i < nr_pages; i++) 103966361095SMuchun Song SetPageVmemmapSelfHosted(pfn_to_page(pfn + i)); 104066361095SMuchun Song 1041a08a2ae3SOscar Salvador /* 1042a08a2ae3SOscar Salvador * It might be that the vmemmap_pages fully span sections. If that is 1043a08a2ae3SOscar Salvador * the case, mark those sections online here as otherwise they will be 1044a08a2ae3SOscar Salvador * left offline. 1045a08a2ae3SOscar Salvador */ 1046a08a2ae3SOscar Salvador if (nr_pages >= PAGES_PER_SECTION) 1047a08a2ae3SOscar Salvador online_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION)); 1048a08a2ae3SOscar Salvador 1049a08a2ae3SOscar Salvador return ret; 1050a08a2ae3SOscar Salvador } 1051a08a2ae3SOscar Salvador 1052a08a2ae3SOscar Salvador void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages) 1053a08a2ae3SOscar Salvador { 1054a08a2ae3SOscar Salvador unsigned long end_pfn = pfn + nr_pages; 1055a08a2ae3SOscar Salvador 1056a08a2ae3SOscar Salvador /* 1057a08a2ae3SOscar Salvador * It might be that the vmemmap_pages fully span sections. If that is 1058a08a2ae3SOscar Salvador * the case, mark those sections offline here as otherwise they will be 1059a08a2ae3SOscar Salvador * left online. 1060a08a2ae3SOscar Salvador */ 1061a08a2ae3SOscar Salvador if (nr_pages >= PAGES_PER_SECTION) 1062a08a2ae3SOscar Salvador offline_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION)); 1063a08a2ae3SOscar Salvador 1064a08a2ae3SOscar Salvador /* 1065a08a2ae3SOscar Salvador * The pages associated with this vmemmap have been offlined, so 1066a08a2ae3SOscar Salvador * we can reset its state here. 1067a08a2ae3SOscar Salvador */ 1068a08a2ae3SOscar Salvador remove_pfn_range_from_zone(page_zone(pfn_to_page(pfn)), pfn, nr_pages); 1069a08a2ae3SOscar Salvador kasan_remove_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages)); 1070a08a2ae3SOscar Salvador } 1071a08a2ae3SOscar Salvador 1072836809ecSDavid Hildenbrand int __ref online_pages(unsigned long pfn, unsigned long nr_pages, 1073836809ecSDavid Hildenbrand struct zone *zone, struct memory_group *group) 107475884fb1SKAMEZAWA Hiroyuki { 1075aa47228aSCody P Schafer unsigned long flags; 10766811378eSYasunori Goto int need_zonelists_rebuild = 0; 1077a08a2ae3SOscar Salvador const int nid = zone_to_nid(zone); 10787b78d335SYasunori Goto int ret; 10797b78d335SYasunori Goto struct memory_notify arg; 10803947be19SDave Hansen 1081dd8e2f23SOscar Salvador /* 1082dd8e2f23SOscar Salvador * {on,off}lining is constrained to full memory sections (or more 1083041711ceSZhen Lei * precisely to memory blocks from the user space POV). 1084dd8e2f23SOscar Salvador * memmap_on_memory is an exception because it reserves initial part 1085dd8e2f23SOscar Salvador * of the physical memory space for vmemmaps. That space is pageblock 1086dd8e2f23SOscar Salvador * aligned. 1087dd8e2f23SOscar Salvador */ 1088ee0913c4SKefeng Wang if (WARN_ON_ONCE(!nr_pages || !pageblock_aligned(pfn) || 1089dd8e2f23SOscar Salvador !IS_ALIGNED(pfn + nr_pages, PAGES_PER_SECTION))) 10904986fac1SDavid Hildenbrand return -EINVAL; 10914986fac1SDavid Hildenbrand 1092381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1093381eab4aSDavid Hildenbrand 1094f1dd2cd1SMichal Hocko /* associate pfn range with the zone */ 1095b30c5927SDavid Hildenbrand move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE); 1096511c2abaSLai Jiangshan 10977b78d335SYasunori Goto arg.start_pfn = pfn; 10987b78d335SYasunori Goto arg.nr_pages = nr_pages; 1099d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 11007b78d335SYasunori Goto 11017b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 11027b78d335SYasunori Goto ret = notifier_to_errno(ret); 1103e33e33b4SChen Yucong if (ret) 1104e33e33b4SChen Yucong goto failed_addition; 1105e33e33b4SChen Yucong 11063947be19SDave Hansen /* 1107b30c5927SDavid Hildenbrand * Fixup the number of isolated pageblocks before marking the sections 1108b30c5927SDavid Hildenbrand * onlining, such that undo_isolate_page_range() works correctly. 1109b30c5927SDavid Hildenbrand */ 1110b30c5927SDavid Hildenbrand spin_lock_irqsave(&zone->lock, flags); 1111b30c5927SDavid Hildenbrand zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages; 1112b30c5927SDavid Hildenbrand spin_unlock_irqrestore(&zone->lock, flags); 1113b30c5927SDavid Hildenbrand 1114b30c5927SDavid Hildenbrand /* 11156811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 11166811378eSYasunori Goto * This means the page allocator ignores this zone. 11176811378eSYasunori Goto * So, zonelist must be updated after online. 11186811378eSYasunori Goto */ 11196dcd73d7SWen Congyang if (!populated_zone(zone)) { 11206811378eSYasunori Goto need_zonelists_rebuild = 1; 112172675e13SMichal Hocko setup_zone_pageset(zone); 11226dcd73d7SWen Congyang } 11236811378eSYasunori Goto 1124aac65321SDavid Hildenbrand online_pages_range(pfn, nr_pages); 1125836809ecSDavid Hildenbrand adjust_present_page_count(pfn_to_page(pfn), group, nr_pages); 1126aa47228aSCody P Schafer 1127b30c5927SDavid Hildenbrand node_states_set_node(nid, &arg); 1128b30c5927SDavid Hildenbrand if (need_zonelists_rebuild) 1129b30c5927SDavid Hildenbrand build_all_zonelists(NULL); 1130b30c5927SDavid Hildenbrand 1131b30c5927SDavid Hildenbrand /* Basic onlining is complete, allow allocation of onlined pages. */ 1132b30c5927SDavid Hildenbrand undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE); 1133b30c5927SDavid Hildenbrand 113493146d98SDavid Hildenbrand /* 1135b86c5fc4SDavid Hildenbrand * Freshly onlined pages aren't shuffled (e.g., all pages are placed to 1136b86c5fc4SDavid Hildenbrand * the tail of the freelist when undoing isolation). Shuffle the whole 1137b86c5fc4SDavid Hildenbrand * zone to make sure the just onlined pages are properly distributed 1138b86c5fc4SDavid Hildenbrand * across the whole freelist - to create an initial shuffle. 113993146d98SDavid Hildenbrand */ 1140e900a918SDan Williams shuffle_zone(zone); 1141e900a918SDan Williams 1142b92ca18eSMel Gorman /* reinitialise watermarks and update pcp limits */ 11431b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 11441b79acc9SKOSAKI Motohiro 1145e888ca35SVlastimil Babka kswapd_run(nid); 1146698b1b30SVlastimil Babka kcompactd_run(nid); 114761b13993SDave Hansen 11482d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 11497b78d335SYasunori Goto 11507b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 1151381eab4aSDavid Hildenbrand mem_hotplug_done(); 115230467e0bSDavid Rientjes return 0; 1153e33e33b4SChen Yucong 1154e33e33b4SChen Yucong failed_addition: 1155e33e33b4SChen Yucong pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 1156e33e33b4SChen Yucong (unsigned long long) pfn << PAGE_SHIFT, 1157e33e33b4SChen Yucong (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 1158e33e33b4SChen Yucong memory_notify(MEM_CANCEL_ONLINE, &arg); 1159feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, pfn, nr_pages); 1160381eab4aSDavid Hildenbrand mem_hotplug_done(); 1161e33e33b4SChen Yucong return ret; 11623947be19SDave Hansen } 1163bc02af93SYasunori Goto 11640bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 11650bd85420STang Chen { 11660bd85420STang Chen struct zone *z; 11670bd85420STang Chen 11680bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 11690bd85420STang Chen z->present_pages = 0; 11700bd85420STang Chen 11710bd85420STang Chen pgdat->node_present_pages = 0; 11720bd85420STang Chen } 11730bd85420STang Chen 1174e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 117509f49dcaSMichal Hocko static pg_data_t __ref *hotadd_init_pgdat(int nid) 11769af3c2deSYasunori Goto { 11779af3c2deSYasunori Goto struct pglist_data *pgdat; 11789af3c2deSYasunori Goto 117909f49dcaSMichal Hocko /* 118009f49dcaSMichal Hocko * NODE_DATA is preallocated (free_area_init) but its internal 118109f49dcaSMichal Hocko * state is not allocated completely. Add missing pieces. 118209f49dcaSMichal Hocko * Completely offline nodes stay around and they just need 118309f49dcaSMichal Hocko * reintialization. 118409f49dcaSMichal Hocko */ 118570b5b46aSMichal Hocko pgdat = NODE_DATA(nid); 118603e85f9dSOscar Salvador 11879af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 118870b5b46aSMichal Hocko free_area_init_core_hotplug(pgdat); 11899af3c2deSYasunori Goto 1190959ecc48SKAMEZAWA Hiroyuki /* 1191959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 1192959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 1193959ecc48SKAMEZAWA Hiroyuki */ 119472675e13SMichal Hocko build_all_zonelists(pgdat); 1195959ecc48SKAMEZAWA Hiroyuki 1196f784a3f1STang Chen /* 11970bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 11980bd85420STang Chen * clear all zones' present_pages because they will be updated in 11990bd85420STang Chen * online_pages() and offline_pages(). 120070b5b46aSMichal Hocko * TODO: should be in free_area_init_core_hotplug? 12010bd85420STang Chen */ 120203e85f9dSOscar Salvador reset_node_managed_pages(pgdat); 12030bd85420STang Chen reset_node_present_pages(pgdat); 12040bd85420STang Chen 12059af3c2deSYasunori Goto return pgdat; 12069af3c2deSYasunori Goto } 12079af3c2deSYasunori Goto 1208ba2d2666SMel Gorman /* 1209ba2d2666SMel Gorman * __try_online_node - online a node if offlined 1210e8b098fcSMike Rapoport * @nid: the node ID 1211b9ff0360SOscar Salvador * @set_node_online: Whether we want to online the node 1212cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1213b9ff0360SOscar Salvador * 1214b9ff0360SOscar Salvador * Returns: 1215b9ff0360SOscar Salvador * 1 -> a new node has been allocated 1216b9ff0360SOscar Salvador * 0 -> the node is already online 1217b9ff0360SOscar Salvador * -ENOMEM -> the node could not be allocated 1218cf23422bSminskey guo */ 1219c68ab18cSDavid Hildenbrand static int __try_online_node(int nid, bool set_node_online) 1220cf23422bSminskey guo { 1221cf23422bSminskey guo pg_data_t *pgdat; 1222b9ff0360SOscar Salvador int ret = 1; 1223cf23422bSminskey guo 122401b0f197SToshi Kani if (node_online(nid)) 122501b0f197SToshi Kani return 0; 122601b0f197SToshi Kani 122709f49dcaSMichal Hocko pgdat = hotadd_init_pgdat(nid); 12287553e8f2SDavid Rientjes if (!pgdat) { 122901b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1230cf23422bSminskey guo ret = -ENOMEM; 1231cf23422bSminskey guo goto out; 1232cf23422bSminskey guo } 1233b9ff0360SOscar Salvador 1234b9ff0360SOscar Salvador if (set_node_online) { 1235cf23422bSminskey guo node_set_online(nid); 1236cf23422bSminskey guo ret = register_one_node(nid); 1237cf23422bSminskey guo BUG_ON(ret); 1238b9ff0360SOscar Salvador } 1239cf23422bSminskey guo out: 1240b9ff0360SOscar Salvador return ret; 1241b9ff0360SOscar Salvador } 1242b9ff0360SOscar Salvador 1243b9ff0360SOscar Salvador /* 1244b9ff0360SOscar Salvador * Users of this function always want to online/register the node 1245b9ff0360SOscar Salvador */ 1246b9ff0360SOscar Salvador int try_online_node(int nid) 1247b9ff0360SOscar Salvador { 1248b9ff0360SOscar Salvador int ret; 1249b9ff0360SOscar Salvador 1250b9ff0360SOscar Salvador mem_hotplug_begin(); 1251c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, true); 1252bfc8c901SVladimir Davydov mem_hotplug_done(); 1253cf23422bSminskey guo return ret; 1254cf23422bSminskey guo } 1255cf23422bSminskey guo 125627356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 125727356f54SToshi Kani { 1258ba325585SPavel Tatashin /* memory range must be block size aligned */ 1259cec3ebd0SDavid Hildenbrand if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || 1260cec3ebd0SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes())) { 1261ba325585SPavel Tatashin pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1262cec3ebd0SDavid Hildenbrand memory_block_size_bytes(), start, size); 126327356f54SToshi Kani return -EINVAL; 126427356f54SToshi Kani } 126527356f54SToshi Kani 126627356f54SToshi Kani return 0; 126727356f54SToshi Kani } 126827356f54SToshi Kani 126931bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg) 127031bc3858SVitaly Kuznetsov { 12711adf8b46SAnshuman Khandual mem->online_type = mhp_default_online_type; 1272dc18d706SNathan Fontenot return device_online(&mem->dev); 127331bc3858SVitaly Kuznetsov } 127431bc3858SVitaly Kuznetsov 1275a08a2ae3SOscar Salvador bool mhp_supports_memmap_on_memory(unsigned long size) 1276a08a2ae3SOscar Salvador { 1277a08a2ae3SOscar Salvador unsigned long nr_vmemmap_pages = size / PAGE_SIZE; 1278a08a2ae3SOscar Salvador unsigned long vmemmap_size = nr_vmemmap_pages * sizeof(struct page); 1279a08a2ae3SOscar Salvador unsigned long remaining_size = size - vmemmap_size; 1280a08a2ae3SOscar Salvador 1281a08a2ae3SOscar Salvador /* 1282a08a2ae3SOscar Salvador * Besides having arch support and the feature enabled at runtime, we 1283a08a2ae3SOscar Salvador * need a few more assumptions to hold true: 1284a08a2ae3SOscar Salvador * 1285a08a2ae3SOscar Salvador * a) We span a single memory block: memory onlining/offlinin;g happens 1286a08a2ae3SOscar Salvador * in memory block granularity. We don't want the vmemmap of online 1287a08a2ae3SOscar Salvador * memory blocks to reside on offline memory blocks. In the future, 1288a08a2ae3SOscar Salvador * we might want to support variable-sized memory blocks to make the 1289a08a2ae3SOscar Salvador * feature more versatile. 1290a08a2ae3SOscar Salvador * 1291a08a2ae3SOscar Salvador * b) The vmemmap pages span complete PMDs: We don't want vmemmap code 1292a08a2ae3SOscar Salvador * to populate memory from the altmap for unrelated parts (i.e., 1293a08a2ae3SOscar Salvador * other memory blocks) 1294a08a2ae3SOscar Salvador * 1295a08a2ae3SOscar Salvador * c) The vmemmap pages (and thereby the pages that will be exposed to 1296a08a2ae3SOscar Salvador * the buddy) have to cover full pageblocks: memory onlining/offlining 1297a08a2ae3SOscar Salvador * code requires applicable ranges to be page-aligned, for example, to 1298a08a2ae3SOscar Salvador * set the migratetypes properly. 1299a08a2ae3SOscar Salvador * 1300a08a2ae3SOscar Salvador * TODO: Although we have a check here to make sure that vmemmap pages 1301a08a2ae3SOscar Salvador * fully populate a PMD, it is not the right place to check for 1302a08a2ae3SOscar Salvador * this. A much better solution involves improving vmemmap code 1303a08a2ae3SOscar Salvador * to fallback to base pages when trying to populate vmemmap using 1304a08a2ae3SOscar Salvador * altmap as an alternative source of memory, and we do not exactly 1305a08a2ae3SOscar Salvador * populate a single PMD. 1306a08a2ae3SOscar Salvador */ 13076e02c46bSMuchun Song return mhp_memmap_on_memory() && 1308a08a2ae3SOscar Salvador size == memory_block_size_bytes() && 1309a08a2ae3SOscar Salvador IS_ALIGNED(vmemmap_size, PMD_SIZE) && 1310a08a2ae3SOscar Salvador IS_ALIGNED(remaining_size, (pageblock_nr_pages << PAGE_SHIFT)); 1311a08a2ae3SOscar Salvador } 1312a08a2ae3SOscar Salvador 13138df1d0e4SDavid Hildenbrand /* 13148df1d0e4SDavid Hildenbrand * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 13158df1d0e4SDavid Hildenbrand * and online/offline operations (triggered e.g. by sysfs). 13168df1d0e4SDavid Hildenbrand * 13178df1d0e4SDavid Hildenbrand * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 13188df1d0e4SDavid Hildenbrand */ 1319b6117199SDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) 1320bc02af93SYasunori Goto { 1321d15dfd31SCatalin Marinas struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; 132232befe9eSDavid Hildenbrand enum memblock_flags memblock_flags = MEMBLOCK_NONE; 1323a08a2ae3SOscar Salvador struct vmem_altmap mhp_altmap = {}; 1324028fc57aSDavid Hildenbrand struct memory_group *group = NULL; 132562cedb9fSDavid Vrabel u64 start, size; 1326b9ff0360SOscar Salvador bool new_node = false; 1327bc02af93SYasunori Goto int ret; 1328bc02af93SYasunori Goto 132962cedb9fSDavid Vrabel start = res->start; 133062cedb9fSDavid Vrabel size = resource_size(res); 133162cedb9fSDavid Vrabel 133227356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 133327356f54SToshi Kani if (ret) 133427356f54SToshi Kani return ret; 133527356f54SToshi Kani 1336028fc57aSDavid Hildenbrand if (mhp_flags & MHP_NID_IS_MGID) { 1337028fc57aSDavid Hildenbrand group = memory_group_find_by_id(nid); 1338028fc57aSDavid Hildenbrand if (!group) 1339028fc57aSDavid Hildenbrand return -EINVAL; 1340028fc57aSDavid Hildenbrand nid = group->nid; 1341028fc57aSDavid Hildenbrand } 1342028fc57aSDavid Hildenbrand 1343fa6d9ec7SVishal Verma if (!node_possible(nid)) { 1344fa6d9ec7SVishal Verma WARN(1, "node %d was absent from the node_possible_map\n", nid); 1345fa6d9ec7SVishal Verma return -EINVAL; 1346fa6d9ec7SVishal Verma } 1347fa6d9ec7SVishal Verma 1348bfc8c901SVladimir Davydov mem_hotplug_begin(); 1349ac13c462SNathan Zimmer 135053d38316SDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 135132befe9eSDavid Hildenbrand if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED) 135232befe9eSDavid Hildenbrand memblock_flags = MEMBLOCK_DRIVER_MANAGED; 135332befe9eSDavid Hildenbrand ret = memblock_add_node(start, size, nid, memblock_flags); 135453d38316SDavid Hildenbrand if (ret) 135553d38316SDavid Hildenbrand goto error_mem_hotplug_end; 135653d38316SDavid Hildenbrand } 13577f36e3e5STang Chen 1358c68ab18cSDavid Hildenbrand ret = __try_online_node(nid, false); 1359b9ff0360SOscar Salvador if (ret < 0) 136041b9e2d7SWen Congyang goto error; 1361b9ff0360SOscar Salvador new_node = ret; 13629af3c2deSYasunori Goto 1363a08a2ae3SOscar Salvador /* 1364a08a2ae3SOscar Salvador * Self hosted memmap array 1365a08a2ae3SOscar Salvador */ 1366a08a2ae3SOscar Salvador if (mhp_flags & MHP_MEMMAP_ON_MEMORY) { 1367a08a2ae3SOscar Salvador if (!mhp_supports_memmap_on_memory(size)) { 1368a08a2ae3SOscar Salvador ret = -EINVAL; 1369a08a2ae3SOscar Salvador goto error; 1370a08a2ae3SOscar Salvador } 1371a08a2ae3SOscar Salvador mhp_altmap.free = PHYS_PFN(size); 1372a08a2ae3SOscar Salvador mhp_altmap.base_pfn = PHYS_PFN(start); 1373a08a2ae3SOscar Salvador params.altmap = &mhp_altmap; 1374a08a2ae3SOscar Salvador } 1375a08a2ae3SOscar Salvador 1376bc02af93SYasunori Goto /* call arch's memory hotadd */ 1377f5637d3bSLogan Gunthorpe ret = arch_add_memory(nid, start, size, ¶ms); 13789af3c2deSYasunori Goto if (ret < 0) 13799af3c2deSYasunori Goto goto error; 13809af3c2deSYasunori Goto 1381db051a0dSDavid Hildenbrand /* create memory block devices after memory was added */ 1382028fc57aSDavid Hildenbrand ret = create_memory_block_devices(start, size, mhp_altmap.alloc, 1383028fc57aSDavid Hildenbrand group); 1384db051a0dSDavid Hildenbrand if (ret) { 138565a2aa5fSDavid Hildenbrand arch_remove_memory(start, size, NULL); 1386db051a0dSDavid Hildenbrand goto error; 1387db051a0dSDavid Hildenbrand } 1388db051a0dSDavid Hildenbrand 1389a1e565aaSTang Chen if (new_node) { 1390d5b6f6a3SOscar Salvador /* If sysfs file of new node can't be created, cpu on the node 13910fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 13920fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 1393d5b6f6a3SOscar Salvador * We online node here. We can't roll back from here. 13940fc44159SYasunori Goto */ 1395d5b6f6a3SOscar Salvador node_set_online(nid); 1396d5b6f6a3SOscar Salvador ret = __register_one_node(nid); 13970fc44159SYasunori Goto BUG_ON(ret); 13980fc44159SYasunori Goto } 13990fc44159SYasunori Goto 1400cc651559SDavid Hildenbrand register_memory_blocks_under_node(nid, PFN_DOWN(start), 1401cc651559SDavid Hildenbrand PFN_UP(start + size - 1), 1402f85086f9SLaurent Dufour MEMINIT_HOTPLUG); 1403d5b6f6a3SOscar Salvador 1404d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 14057b7b2721SDavid Hildenbrand if (!strcmp(res->name, "System RAM")) 1406d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1407d96ae530Sakpm@linux-foundation.org 1408381eab4aSDavid Hildenbrand /* device_online() will take the lock when calling online_pages() */ 1409381eab4aSDavid Hildenbrand mem_hotplug_done(); 1410381eab4aSDavid Hildenbrand 14119ca6551eSDavid Hildenbrand /* 14129ca6551eSDavid Hildenbrand * In case we're allowed to merge the resource, flag it and trigger 14139ca6551eSDavid Hildenbrand * merging now that adding succeeded. 14149ca6551eSDavid Hildenbrand */ 141526011267SDavid Hildenbrand if (mhp_flags & MHP_MERGE_RESOURCE) 14169ca6551eSDavid Hildenbrand merge_system_ram_resource(res); 14179ca6551eSDavid Hildenbrand 141831bc3858SVitaly Kuznetsov /* online pages if requested */ 14191adf8b46SAnshuman Khandual if (mhp_default_online_type != MMOP_OFFLINE) 1420fbcf73ceSDavid Hildenbrand walk_memory_blocks(start, size, NULL, online_memory_block); 142131bc3858SVitaly Kuznetsov 1422381eab4aSDavid Hildenbrand return ret; 14239af3c2deSYasunori Goto error: 142452219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 14257f36e3e5STang Chen memblock_remove(start, size); 142653d38316SDavid Hildenbrand error_mem_hotplug_end: 1427bfc8c901SVladimir Davydov mem_hotplug_done(); 1428bc02af93SYasunori Goto return ret; 1429bc02af93SYasunori Goto } 143062cedb9fSDavid Vrabel 14318df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */ 1432b6117199SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 143362cedb9fSDavid Vrabel { 143462cedb9fSDavid Vrabel struct resource *res; 143562cedb9fSDavid Vrabel int ret; 143662cedb9fSDavid Vrabel 14377b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, "System RAM"); 14386f754ba4SVitaly Kuznetsov if (IS_ERR(res)) 14396f754ba4SVitaly Kuznetsov return PTR_ERR(res); 144062cedb9fSDavid Vrabel 1441b6117199SDavid Hildenbrand ret = add_memory_resource(nid, res, mhp_flags); 144262cedb9fSDavid Vrabel if (ret < 0) 144362cedb9fSDavid Vrabel release_memory_resource(res); 144462cedb9fSDavid Vrabel return ret; 144562cedb9fSDavid Vrabel } 14468df1d0e4SDavid Hildenbrand 1447b6117199SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 14488df1d0e4SDavid Hildenbrand { 14498df1d0e4SDavid Hildenbrand int rc; 14508df1d0e4SDavid Hildenbrand 14518df1d0e4SDavid Hildenbrand lock_device_hotplug(); 1452b6117199SDavid Hildenbrand rc = __add_memory(nid, start, size, mhp_flags); 14538df1d0e4SDavid Hildenbrand unlock_device_hotplug(); 14548df1d0e4SDavid Hildenbrand 14558df1d0e4SDavid Hildenbrand return rc; 14568df1d0e4SDavid Hildenbrand } 1457bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 14580c0e6195SKAMEZAWA Hiroyuki 14597b7b2721SDavid Hildenbrand /* 14607b7b2721SDavid Hildenbrand * Add special, driver-managed memory to the system as system RAM. Such 14617b7b2721SDavid Hildenbrand * memory is not exposed via the raw firmware-provided memmap as system 14627b7b2721SDavid Hildenbrand * RAM, instead, it is detected and added by a driver - during cold boot, 14637b7b2721SDavid Hildenbrand * after a reboot, and after kexec. 14647b7b2721SDavid Hildenbrand * 14657b7b2721SDavid Hildenbrand * Reasons why this memory should not be used for the initial memmap of a 14667b7b2721SDavid Hildenbrand * kexec kernel or for placing kexec images: 14677b7b2721SDavid Hildenbrand * - The booting kernel is in charge of determining how this memory will be 14687b7b2721SDavid Hildenbrand * used (e.g., use persistent memory as system RAM) 14697b7b2721SDavid Hildenbrand * - Coordination with a hypervisor is required before this memory 14707b7b2721SDavid Hildenbrand * can be used (e.g., inaccessible parts). 14717b7b2721SDavid Hildenbrand * 14727b7b2721SDavid Hildenbrand * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided 14737b7b2721SDavid Hildenbrand * memory map") are created. Also, the created memory resource is flagged 14747cf603d1SDavid Hildenbrand * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case 14757b7b2721SDavid Hildenbrand * this memory as well (esp., not place kexec images onto it). 14767b7b2721SDavid Hildenbrand * 14777b7b2721SDavid Hildenbrand * The resource_name (visible via /proc/iomem) has to have the format 14787b7b2721SDavid Hildenbrand * "System RAM ($DRIVER)". 14797b7b2721SDavid Hildenbrand */ 14807b7b2721SDavid Hildenbrand int add_memory_driver_managed(int nid, u64 start, u64 size, 1481b6117199SDavid Hildenbrand const char *resource_name, mhp_t mhp_flags) 14827b7b2721SDavid Hildenbrand { 14837b7b2721SDavid Hildenbrand struct resource *res; 14847b7b2721SDavid Hildenbrand int rc; 14857b7b2721SDavid Hildenbrand 14867b7b2721SDavid Hildenbrand if (!resource_name || 14877b7b2721SDavid Hildenbrand strstr(resource_name, "System RAM (") != resource_name || 14887b7b2721SDavid Hildenbrand resource_name[strlen(resource_name) - 1] != ')') 14897b7b2721SDavid Hildenbrand return -EINVAL; 14907b7b2721SDavid Hildenbrand 14917b7b2721SDavid Hildenbrand lock_device_hotplug(); 14927b7b2721SDavid Hildenbrand 14937b7b2721SDavid Hildenbrand res = register_memory_resource(start, size, resource_name); 14947b7b2721SDavid Hildenbrand if (IS_ERR(res)) { 14957b7b2721SDavid Hildenbrand rc = PTR_ERR(res); 14967b7b2721SDavid Hildenbrand goto out_unlock; 14977b7b2721SDavid Hildenbrand } 14987b7b2721SDavid Hildenbrand 1499b6117199SDavid Hildenbrand rc = add_memory_resource(nid, res, mhp_flags); 15007b7b2721SDavid Hildenbrand if (rc < 0) 15017b7b2721SDavid Hildenbrand release_memory_resource(res); 15027b7b2721SDavid Hildenbrand 15037b7b2721SDavid Hildenbrand out_unlock: 15047b7b2721SDavid Hildenbrand unlock_device_hotplug(); 15057b7b2721SDavid Hildenbrand return rc; 15067b7b2721SDavid Hildenbrand } 15077b7b2721SDavid Hildenbrand EXPORT_SYMBOL_GPL(add_memory_driver_managed); 15087b7b2721SDavid Hildenbrand 1509bca3feaaSAnshuman Khandual /* 1510bca3feaaSAnshuman Khandual * Platforms should define arch_get_mappable_range() that provides 1511bca3feaaSAnshuman Khandual * maximum possible addressable physical memory range for which the 1512bca3feaaSAnshuman Khandual * linear mapping could be created. The platform returned address 1513bca3feaaSAnshuman Khandual * range must adhere to these following semantics. 1514bca3feaaSAnshuman Khandual * 1515bca3feaaSAnshuman Khandual * - range.start <= range.end 1516bca3feaaSAnshuman Khandual * - Range includes both end points [range.start..range.end] 1517bca3feaaSAnshuman Khandual * 1518bca3feaaSAnshuman Khandual * There is also a fallback definition provided here, allowing the 1519bca3feaaSAnshuman Khandual * entire possible physical address range in case any platform does 1520bca3feaaSAnshuman Khandual * not define arch_get_mappable_range(). 1521bca3feaaSAnshuman Khandual */ 1522bca3feaaSAnshuman Khandual struct range __weak arch_get_mappable_range(void) 1523bca3feaaSAnshuman Khandual { 1524bca3feaaSAnshuman Khandual struct range mhp_range = { 1525bca3feaaSAnshuman Khandual .start = 0UL, 1526bca3feaaSAnshuman Khandual .end = -1ULL, 1527bca3feaaSAnshuman Khandual }; 1528bca3feaaSAnshuman Khandual return mhp_range; 1529bca3feaaSAnshuman Khandual } 1530bca3feaaSAnshuman Khandual 1531bca3feaaSAnshuman Khandual struct range mhp_get_pluggable_range(bool need_mapping) 1532bca3feaaSAnshuman Khandual { 1533bca3feaaSAnshuman Khandual const u64 max_phys = (1ULL << MAX_PHYSMEM_BITS) - 1; 1534bca3feaaSAnshuman Khandual struct range mhp_range; 1535bca3feaaSAnshuman Khandual 1536bca3feaaSAnshuman Khandual if (need_mapping) { 1537bca3feaaSAnshuman Khandual mhp_range = arch_get_mappable_range(); 1538bca3feaaSAnshuman Khandual if (mhp_range.start > max_phys) { 1539bca3feaaSAnshuman Khandual mhp_range.start = 0; 1540bca3feaaSAnshuman Khandual mhp_range.end = 0; 1541bca3feaaSAnshuman Khandual } 1542bca3feaaSAnshuman Khandual mhp_range.end = min_t(u64, mhp_range.end, max_phys); 1543bca3feaaSAnshuman Khandual } else { 1544bca3feaaSAnshuman Khandual mhp_range.start = 0; 1545bca3feaaSAnshuman Khandual mhp_range.end = max_phys; 1546bca3feaaSAnshuman Khandual } 1547bca3feaaSAnshuman Khandual return mhp_range; 1548bca3feaaSAnshuman Khandual } 1549bca3feaaSAnshuman Khandual EXPORT_SYMBOL_GPL(mhp_get_pluggable_range); 1550bca3feaaSAnshuman Khandual 1551bca3feaaSAnshuman Khandual bool mhp_range_allowed(u64 start, u64 size, bool need_mapping) 1552bca3feaaSAnshuman Khandual { 1553bca3feaaSAnshuman Khandual struct range mhp_range = mhp_get_pluggable_range(need_mapping); 1554bca3feaaSAnshuman Khandual u64 end = start + size; 1555bca3feaaSAnshuman Khandual 1556bca3feaaSAnshuman Khandual if (start < end && start >= mhp_range.start && (end - 1) <= mhp_range.end) 1557bca3feaaSAnshuman Khandual return true; 1558bca3feaaSAnshuman Khandual 1559bca3feaaSAnshuman Khandual pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n", 1560bca3feaaSAnshuman Khandual start, end, mhp_range.start, mhp_range.end); 1561bca3feaaSAnshuman Khandual return false; 1562bca3feaaSAnshuman Khandual } 1563bca3feaaSAnshuman Khandual 15640c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 15650c0e6195SKAMEZAWA Hiroyuki /* 15660efadf48SYisheng Xie * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 1567aa218795SDavid Hildenbrand * non-lru movable pages and hugepages). Will skip over most unmovable 1568aa218795SDavid Hildenbrand * pages (esp., pages that can be skipped when offlining), but bail out on 1569aa218795SDavid Hildenbrand * definitely unmovable pages. 1570aa218795SDavid Hildenbrand * 1571aa218795SDavid Hildenbrand * Returns: 1572aa218795SDavid Hildenbrand * 0 in case a movable page is found and movable_pfn was updated. 1573aa218795SDavid Hildenbrand * -ENOENT in case no movable page was found. 1574aa218795SDavid Hildenbrand * -EBUSY in case a definitely unmovable page was found. 15750c0e6195SKAMEZAWA Hiroyuki */ 1576aa218795SDavid Hildenbrand static int scan_movable_pages(unsigned long start, unsigned long end, 1577aa218795SDavid Hildenbrand unsigned long *movable_pfn) 15780c0e6195SKAMEZAWA Hiroyuki { 15790c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 1580eeb0efd0SOscar Salvador 15810c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 1582eeb0efd0SOscar Salvador struct page *page, *head; 1583eeb0efd0SOscar Salvador unsigned long skip; 1584eeb0efd0SOscar Salvador 1585eeb0efd0SOscar Salvador if (!pfn_valid(pfn)) 1586eeb0efd0SOscar Salvador continue; 15870c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 15880c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 1589aa218795SDavid Hildenbrand goto found; 15900efadf48SYisheng Xie if (__PageMovable(page)) 1591aa218795SDavid Hildenbrand goto found; 1592aa218795SDavid Hildenbrand 1593aa218795SDavid Hildenbrand /* 1594aa218795SDavid Hildenbrand * PageOffline() pages that are not marked __PageMovable() and 1595aa218795SDavid Hildenbrand * have a reference count > 0 (after MEM_GOING_OFFLINE) are 1596aa218795SDavid Hildenbrand * definitely unmovable. If their reference count would be 0, 1597aa218795SDavid Hildenbrand * they could at least be skipped when offlining memory. 1598aa218795SDavid Hildenbrand */ 1599aa218795SDavid Hildenbrand if (PageOffline(page) && page_count(page)) 1600aa218795SDavid Hildenbrand return -EBUSY; 1601eeb0efd0SOscar Salvador 1602eeb0efd0SOscar Salvador if (!PageHuge(page)) 1603eeb0efd0SOscar Salvador continue; 1604eeb0efd0SOscar Salvador head = compound_head(page); 16058f251a3dSMike Kravetz /* 16068f251a3dSMike Kravetz * This test is racy as we hold no reference or lock. The 16078f251a3dSMike Kravetz * hugetlb page could have been free'ed and head is no longer 16088f251a3dSMike Kravetz * a hugetlb page before the following check. In such unlikely 16098f251a3dSMike Kravetz * cases false positives and negatives are possible. Calling 16108f251a3dSMike Kravetz * code must deal with these scenarios. 16118f251a3dSMike Kravetz */ 16128f251a3dSMike Kravetz if (HPageMigratable(head)) 1613aa218795SDavid Hildenbrand goto found; 1614d8c6546bSMatthew Wilcox (Oracle) skip = compound_nr(head) - (page - head); 1615eeb0efd0SOscar Salvador pfn += skip - 1; 16160c0e6195SKAMEZAWA Hiroyuki } 1617aa218795SDavid Hildenbrand return -ENOENT; 1618aa218795SDavid Hildenbrand found: 1619aa218795SDavid Hildenbrand *movable_pfn = pfn; 16200c0e6195SKAMEZAWA Hiroyuki return 0; 16210c0e6195SKAMEZAWA Hiroyuki } 16220c0e6195SKAMEZAWA Hiroyuki 1623*32cf666eSSeongJae Park static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 16240c0e6195SKAMEZAWA Hiroyuki { 16250c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 16266c357848SMatthew Wilcox (Oracle) struct page *page, *head; 16270c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 1628786dee86SLiam Mark static DEFINE_RATELIMIT_STATE(migrate_rs, DEFAULT_RATELIMIT_INTERVAL, 1629786dee86SLiam Mark DEFAULT_RATELIMIT_BURST); 16300c0e6195SKAMEZAWA Hiroyuki 1631a85009c3SMichal Hocko for (pfn = start_pfn; pfn < end_pfn; pfn++) { 1632869f7ee6SMatthew Wilcox (Oracle) struct folio *folio; 1633f7f9c00dSBaolin Wang bool isolated; 1634869f7ee6SMatthew Wilcox (Oracle) 16350c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 16360c0e6195SKAMEZAWA Hiroyuki continue; 16370c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 1638869f7ee6SMatthew Wilcox (Oracle) folio = page_folio(page); 1639869f7ee6SMatthew Wilcox (Oracle) head = &folio->page; 1640c8721bbbSNaoya Horiguchi 1641c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1642d8c6546bSMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + compound_nr(head) - 1; 16436aa3a920SSidhartha Kumar isolate_hugetlb(folio, &source); 1644c8721bbbSNaoya Horiguchi continue; 164594723aafSMichal Hocko } else if (PageTransHuge(page)) 16466c357848SMatthew Wilcox (Oracle) pfn = page_to_pfn(head) + thp_nr_pages(page) - 1; 1647c8721bbbSNaoya Horiguchi 1648b15c8726SMichal Hocko /* 1649b15c8726SMichal Hocko * HWPoison pages have elevated reference counts so the migration would 1650b15c8726SMichal Hocko * fail on them. It also doesn't make any sense to migrate them in the 1651b15c8726SMichal Hocko * first place. Still try to unmap such a page in case it is still mapped 1652b15c8726SMichal Hocko * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep 1653b15c8726SMichal Hocko * the unmap as the catch all safety net). 1654b15c8726SMichal Hocko */ 1655b15c8726SMichal Hocko if (PageHWPoison(page)) { 1656869f7ee6SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_lru(folio))) 1657869f7ee6SMatthew Wilcox (Oracle) folio_isolate_lru(folio); 1658869f7ee6SMatthew Wilcox (Oracle) if (folio_mapped(folio)) 1659869f7ee6SMatthew Wilcox (Oracle) try_to_unmap(folio, TTU_IGNORE_MLOCK); 1660b15c8726SMichal Hocko continue; 1661b15c8726SMichal Hocko } 1662b15c8726SMichal Hocko 1663700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 16640c0e6195SKAMEZAWA Hiroyuki continue; 16650c0e6195SKAMEZAWA Hiroyuki /* 16660efadf48SYisheng Xie * We can skip free pages. And we can deal with pages on 16670efadf48SYisheng Xie * LRU and non-lru movable pages. 16680c0e6195SKAMEZAWA Hiroyuki */ 1669cd775580SBaolin Wang if (PageLRU(page)) 1670f7f9c00dSBaolin Wang isolated = isolate_lru_page(page); 1671cd775580SBaolin Wang else 1672cd775580SBaolin Wang isolated = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 1673cd775580SBaolin Wang if (isolated) { 167462695a84SNick Piggin list_add_tail(&page->lru, &source); 16750efadf48SYisheng Xie if (!__PageMovable(page)) 1676599d0c95SMel Gorman inc_node_page_state(page, NR_ISOLATED_ANON + 16779de4f22aSHuang Ying page_is_file_lru(page)); 16786d9c285aSKOSAKI Motohiro 16790c0e6195SKAMEZAWA Hiroyuki } else { 1680786dee86SLiam Mark if (__ratelimit(&migrate_rs)) { 16812932c8b0SMichal Hocko pr_warn("failed to isolate pfn %lx\n", pfn); 16820efadf48SYisheng Xie dump_page(page, "isolation failed"); 16831723058eSOscar Salvador } 1684786dee86SLiam Mark } 1685700c2a46SKonstantin Khlebnikov put_page(page); 16860c0e6195SKAMEZAWA Hiroyuki } 1687f3ab2636SBob Liu if (!list_empty(&source)) { 1688203e6e5cSJoonsoo Kim nodemask_t nmask = node_states[N_MEMORY]; 1689203e6e5cSJoonsoo Kim struct migration_target_control mtc = { 1690203e6e5cSJoonsoo Kim .nmask = &nmask, 1691203e6e5cSJoonsoo Kim .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, 1692203e6e5cSJoonsoo Kim }; 1693*32cf666eSSeongJae Park int ret; 1694203e6e5cSJoonsoo Kim 1695203e6e5cSJoonsoo Kim /* 1696203e6e5cSJoonsoo Kim * We have checked that migration range is on a single zone so 1697203e6e5cSJoonsoo Kim * we can use the nid of the first page to all the others. 1698203e6e5cSJoonsoo Kim */ 1699203e6e5cSJoonsoo Kim mtc.nid = page_to_nid(list_first_entry(&source, struct page, lru)); 1700203e6e5cSJoonsoo Kim 1701203e6e5cSJoonsoo Kim /* 1702203e6e5cSJoonsoo Kim * try to allocate from a different node but reuse this node 1703203e6e5cSJoonsoo Kim * if there are no other online nodes to be used (e.g. we are 1704203e6e5cSJoonsoo Kim * offlining a part of the only existing node) 1705203e6e5cSJoonsoo Kim */ 1706203e6e5cSJoonsoo Kim node_clear(mtc.nid, nmask); 1707203e6e5cSJoonsoo Kim if (nodes_empty(nmask)) 1708203e6e5cSJoonsoo Kim node_set(mtc.nid, nmask); 1709203e6e5cSJoonsoo Kim ret = migrate_pages(&source, alloc_migration_target, NULL, 17105ac95884SYang Shi (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG, NULL); 17112932c8b0SMichal Hocko if (ret) { 17122932c8b0SMichal Hocko list_for_each_entry(page, &source, lru) { 1713786dee86SLiam Mark if (__ratelimit(&migrate_rs)) { 1714786dee86SLiam Mark pr_warn("migrating pfn %lx failed ret:%d\n", 17152932c8b0SMichal Hocko page_to_pfn(page), ret); 17162932c8b0SMichal Hocko dump_page(page, "migration failure"); 17172932c8b0SMichal Hocko } 1718786dee86SLiam Mark } 1719c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1720f3ab2636SBob Liu } 17212932c8b0SMichal Hocko } 17220c0e6195SKAMEZAWA Hiroyuki } 17230c0e6195SKAMEZAWA Hiroyuki 1724c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1725c5320926STang Chen { 172655ac590cSTang Chen movable_node_enabled = true; 1727c5320926STang Chen return 0; 1728c5320926STang Chen } 1729c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1730c5320926STang Chen 1731d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1732d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1733d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1734d9713679SLai Jiangshan { 1735d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1736d9713679SLai Jiangshan unsigned long present_pages = 0; 173786b27beaSOscar Salvador enum zone_type zt; 1738d9713679SLai Jiangshan 173998fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 174098fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 174186b27beaSOscar Salvador 174286b27beaSOscar Salvador /* 174386b27beaSOscar Salvador * Check whether node_states[N_NORMAL_MEMORY] will be changed. 174486b27beaSOscar Salvador * If the memory to be offline is within the range 174586b27beaSOscar Salvador * [0..ZONE_NORMAL], and it is the last present memory there, 174686b27beaSOscar Salvador * the zones in that range will become empty after the offlining, 174786b27beaSOscar Salvador * thus we can determine that we need to clear the node from 174886b27beaSOscar Salvador * node_states[N_NORMAL_MEMORY]. 174986b27beaSOscar Salvador */ 175086b27beaSOscar Salvador for (zt = 0; zt <= ZONE_NORMAL; zt++) 175186b27beaSOscar Salvador present_pages += pgdat->node_zones[zt].present_pages; 175286b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 175386b27beaSOscar Salvador arg->status_change_nid_normal = zone_to_nid(zone); 1754d9713679SLai Jiangshan 17556715ddf9SLai Jiangshan /* 17566b740c6cSDavid Hildenbrand * We have accounted the pages from [0..ZONE_NORMAL); ZONE_HIGHMEM 17576b740c6cSDavid Hildenbrand * does not apply as we don't support 32bit. 175886b27beaSOscar Salvador * Here we count the possible pages from ZONE_MOVABLE. 175986b27beaSOscar Salvador * If after having accounted all the pages, we see that the nr_pages 176086b27beaSOscar Salvador * to be offlined is over or equal to the accounted pages, 176186b27beaSOscar Salvador * we know that the node will become empty, and so, we can clear 176286b27beaSOscar Salvador * it for N_MEMORY as well. 1763d9713679SLai Jiangshan */ 176486b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1765d9713679SLai Jiangshan 1766d9713679SLai Jiangshan if (nr_pages >= present_pages) 1767d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1768d9713679SLai Jiangshan } 1769d9713679SLai Jiangshan 1770d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1771d9713679SLai Jiangshan { 1772d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1773d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1774d9713679SLai Jiangshan 1775cf01f6f5SOscar Salvador if (arg->status_change_nid >= 0) 17766715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1777d9713679SLai Jiangshan } 1778d9713679SLai Jiangshan 1779c5e79ef5SDavid Hildenbrand static int count_system_ram_pages_cb(unsigned long start_pfn, 1780c5e79ef5SDavid Hildenbrand unsigned long nr_pages, void *data) 1781c5e79ef5SDavid Hildenbrand { 1782c5e79ef5SDavid Hildenbrand unsigned long *nr_system_ram_pages = data; 1783c5e79ef5SDavid Hildenbrand 1784c5e79ef5SDavid Hildenbrand *nr_system_ram_pages += nr_pages; 1785c5e79ef5SDavid Hildenbrand return 0; 1786c5e79ef5SDavid Hildenbrand } 1787c5e79ef5SDavid Hildenbrand 1788836809ecSDavid Hildenbrand int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages, 1789395f6081SDavid Hildenbrand struct zone *zone, struct memory_group *group) 17900c0e6195SKAMEZAWA Hiroyuki { 179173a11c96SDavid Hildenbrand const unsigned long end_pfn = start_pfn + nr_pages; 17920a1a9a00SDavid Hildenbrand unsigned long pfn, system_ram_pages = 0; 1793395f6081SDavid Hildenbrand const int node = zone_to_nid(zone); 1794d702909fSCody P Schafer unsigned long flags; 17957b78d335SYasunori Goto struct memory_notify arg; 179679605093SMichal Hocko char *reason; 1797395f6081SDavid Hildenbrand int ret; 17980c0e6195SKAMEZAWA Hiroyuki 1799dd8e2f23SOscar Salvador /* 1800dd8e2f23SOscar Salvador * {on,off}lining is constrained to full memory sections (or more 1801041711ceSZhen Lei * precisely to memory blocks from the user space POV). 1802dd8e2f23SOscar Salvador * memmap_on_memory is an exception because it reserves initial part 1803dd8e2f23SOscar Salvador * of the physical memory space for vmemmaps. That space is pageblock 1804dd8e2f23SOscar Salvador * aligned. 1805dd8e2f23SOscar Salvador */ 1806ee0913c4SKefeng Wang if (WARN_ON_ONCE(!nr_pages || !pageblock_aligned(start_pfn) || 1807dd8e2f23SOscar Salvador !IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION))) 18084986fac1SDavid Hildenbrand return -EINVAL; 18094986fac1SDavid Hildenbrand 1810381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1811381eab4aSDavid Hildenbrand 1812c5e79ef5SDavid Hildenbrand /* 1813c5e79ef5SDavid Hildenbrand * Don't allow to offline memory blocks that contain holes. 1814c5e79ef5SDavid Hildenbrand * Consequently, memory blocks with holes can never get onlined 1815c5e79ef5SDavid Hildenbrand * via the hotplug path - online_pages() - as hotplugged memory has 1816c5e79ef5SDavid Hildenbrand * no holes. This way, we e.g., don't have to worry about marking 1817c5e79ef5SDavid Hildenbrand * memory holes PG_reserved, don't need pfn_valid() checks, and can 1818c5e79ef5SDavid Hildenbrand * avoid using walk_system_ram_range() later. 1819c5e79ef5SDavid Hildenbrand */ 182073a11c96SDavid Hildenbrand walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages, 1821c5e79ef5SDavid Hildenbrand count_system_ram_pages_cb); 182273a11c96SDavid Hildenbrand if (system_ram_pages != nr_pages) { 1823c5e79ef5SDavid Hildenbrand ret = -EINVAL; 1824c5e79ef5SDavid Hildenbrand reason = "memory holes"; 1825c5e79ef5SDavid Hildenbrand goto failed_removal; 1826c5e79ef5SDavid Hildenbrand } 1827c5e79ef5SDavid Hildenbrand 1828395f6081SDavid Hildenbrand /* 1829395f6081SDavid Hildenbrand * We only support offlining of memory blocks managed by a single zone, 1830395f6081SDavid Hildenbrand * checked by calling code. This is just a sanity check that we might 1831395f6081SDavid Hildenbrand * want to remove in the future. 1832395f6081SDavid Hildenbrand */ 1833395f6081SDavid Hildenbrand if (WARN_ON_ONCE(page_zone(pfn_to_page(start_pfn)) != zone || 1834395f6081SDavid Hildenbrand page_zone(pfn_to_page(end_pfn - 1)) != zone)) { 183579605093SMichal Hocko ret = -EINVAL; 183679605093SMichal Hocko reason = "multizone range"; 183779605093SMichal Hocko goto failed_removal; 1838381eab4aSDavid Hildenbrand } 18397b78d335SYasunori Goto 1840ec6e8c7eSVlastimil Babka /* 1841ec6e8c7eSVlastimil Babka * Disable pcplists so that page isolation cannot race with freeing 1842ec6e8c7eSVlastimil Babka * in a way that pages from isolated pageblock are left on pcplists. 1843ec6e8c7eSVlastimil Babka */ 1844ec6e8c7eSVlastimil Babka zone_pcp_disable(zone); 1845d479960eSMinchan Kim lru_cache_disable(); 1846ec6e8c7eSVlastimil Babka 18470c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1848b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1849d381c547SMichal Hocko MIGRATE_MOVABLE, 1850b2c9e2fbSZi Yan MEMORY_OFFLINE | REPORT_FAILURE, 1851b2c9e2fbSZi Yan GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL); 18523fa0c7c7SDavid Hildenbrand if (ret) { 185379605093SMichal Hocko reason = "failure to isolate range"; 1854ec6e8c7eSVlastimil Babka goto failed_removal_pcplists_disabled; 1855381eab4aSDavid Hildenbrand } 18567b78d335SYasunori Goto 18577b78d335SYasunori Goto arg.start_pfn = start_pfn; 18587b78d335SYasunori Goto arg.nr_pages = nr_pages; 1859d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 18607b78d335SYasunori Goto 18617b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 18627b78d335SYasunori Goto ret = notifier_to_errno(ret); 186379605093SMichal Hocko if (ret) { 186479605093SMichal Hocko reason = "notifier failure"; 186579605093SMichal Hocko goto failed_removal_isolated; 186679605093SMichal Hocko } 18677b78d335SYasunori Goto 1868bb8965bdSMichal Hocko do { 1869aa218795SDavid Hildenbrand pfn = start_pfn; 1870aa218795SDavid Hildenbrand do { 187179605093SMichal Hocko if (signal_pending(current)) { 1872bb8965bdSMichal Hocko ret = -EINTR; 187379605093SMichal Hocko reason = "signal backoff"; 187479605093SMichal Hocko goto failed_removal_isolated; 187579605093SMichal Hocko } 187672b39cfcSMichal Hocko 18770c0e6195SKAMEZAWA Hiroyuki cond_resched(); 18780c0e6195SKAMEZAWA Hiroyuki 1879aa218795SDavid Hildenbrand ret = scan_movable_pages(pfn, end_pfn, &pfn); 1880aa218795SDavid Hildenbrand if (!ret) { 1881bb8965bdSMichal Hocko /* 1882bb8965bdSMichal Hocko * TODO: fatal migration failures should bail 1883bb8965bdSMichal Hocko * out 1884bb8965bdSMichal Hocko */ 1885bb8965bdSMichal Hocko do_migrate_range(pfn, end_pfn); 1886bb8965bdSMichal Hocko } 1887aa218795SDavid Hildenbrand } while (!ret); 1888aa218795SDavid Hildenbrand 1889aa218795SDavid Hildenbrand if (ret != -ENOENT) { 1890aa218795SDavid Hildenbrand reason = "unmovable page"; 1891aa218795SDavid Hildenbrand goto failed_removal_isolated; 18920c0e6195SKAMEZAWA Hiroyuki } 189372b39cfcSMichal Hocko 1894c8721bbbSNaoya Horiguchi /* 1895bb8965bdSMichal Hocko * Dissolve free hugepages in the memory block before doing 1896bb8965bdSMichal Hocko * offlining actually in order to make hugetlbfs's object 1897bb8965bdSMichal Hocko * counting consistent. 1898c8721bbbSNaoya Horiguchi */ 1899082d5b6bSGerald Schaefer ret = dissolve_free_huge_pages(start_pfn, end_pfn); 190079605093SMichal Hocko if (ret) { 190179605093SMichal Hocko reason = "failure to dissolve huge pages"; 190279605093SMichal Hocko goto failed_removal_isolated; 190379605093SMichal Hocko } 19040a1a9a00SDavid Hildenbrand 19050a1a9a00SDavid Hildenbrand ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE); 1906ec6e8c7eSVlastimil Babka 19075557c766SMichal Hocko } while (ret); 1908bb8965bdSMichal Hocko 19090a1a9a00SDavid Hildenbrand /* Mark all sections offline and remove free pages from the buddy. */ 19100a1a9a00SDavid Hildenbrand __offline_isolated_pages(start_pfn, end_pfn); 19117c33023aSLaurent Dufour pr_debug("Offlined Pages %ld\n", nr_pages); 19120a1a9a00SDavid Hildenbrand 19139b7ea46aSQian Cai /* 1914b30c5927SDavid Hildenbrand * The memory sections are marked offline, and the pageblock flags 1915b30c5927SDavid Hildenbrand * effectively stale; nobody should be touching them. Fixup the number 1916b30c5927SDavid Hildenbrand * of isolated pageblocks, memory onlining will properly revert this. 19179b7ea46aSQian Cai */ 19189b7ea46aSQian Cai spin_lock_irqsave(&zone->lock, flags); 1919ea15153cSDavid Hildenbrand zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages; 19209b7ea46aSQian Cai spin_unlock_irqrestore(&zone->lock, flags); 19219b7ea46aSQian Cai 1922d479960eSMinchan Kim lru_cache_enable(); 1923ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 1924ec6e8c7eSVlastimil Babka 19250c0e6195SKAMEZAWA Hiroyuki /* removal success */ 19260a1a9a00SDavid Hildenbrand adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages); 1927836809ecSDavid Hildenbrand adjust_present_page_count(pfn_to_page(start_pfn), group, -nr_pages); 19287b78d335SYasunori Goto 1929b92ca18eSMel Gorman /* reinitialise watermarks and update pcp limits */ 19301b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 19311b79acc9SKOSAKI Motohiro 19321e8537baSXishi Qiu if (!populated_zone(zone)) { 1933340175b7SJiang Liu zone_pcp_reset(zone); 193472675e13SMichal Hocko build_all_zonelists(NULL); 1935b92ca18eSMel Gorman } 1936340175b7SJiang Liu 1937d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1938698b1b30SVlastimil Babka if (arg.status_change_nid >= 0) { 1939698b1b30SVlastimil Babka kcompactd_stop(node); 1940b4a0215eSKefeng Wang kswapd_stop(node); 1941698b1b30SVlastimil Babka } 1942bce7394aSMinchan Kim 19430c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 19447b78d335SYasunori Goto 19457b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 1946feee6b29SDavid Hildenbrand remove_pfn_range_from_zone(zone, start_pfn, nr_pages); 1947381eab4aSDavid Hildenbrand mem_hotplug_done(); 19480c0e6195SKAMEZAWA Hiroyuki return 0; 19490c0e6195SKAMEZAWA Hiroyuki 195079605093SMichal Hocko failed_removal_isolated: 195136ba30bcSMiaohe Lin /* pushback to free area */ 195279605093SMichal Hocko undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 1953c4efe484SQian Cai memory_notify(MEM_CANCEL_OFFLINE, &arg); 1954ec6e8c7eSVlastimil Babka failed_removal_pcplists_disabled: 1955946746d1SMiaohe Lin lru_cache_enable(); 1956ec6e8c7eSVlastimil Babka zone_pcp_enable(zone); 19570c0e6195SKAMEZAWA Hiroyuki failed_removal: 195879605093SMichal Hocko pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 1959a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 196079605093SMichal Hocko ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 196179605093SMichal Hocko reason); 1962381eab4aSDavid Hildenbrand mem_hotplug_done(); 19630c0e6195SKAMEZAWA Hiroyuki return ret; 19640c0e6195SKAMEZAWA Hiroyuki } 196571088785SBadari Pulavarty 1966d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1967bbc76be6SWen Congyang { 1968e1c158e4SDavid Hildenbrand int *nid = arg; 1969bbc76be6SWen Congyang 1970e1c158e4SDavid Hildenbrand *nid = mem->nid; 1971639118d1SKefeng Wang if (unlikely(mem->state != MEM_OFFLINE)) { 1972349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 1973349daa0fSRandy Dunlap 1974349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1975b6c88d3bSDavid Hildenbrand endpa = beginpa + memory_block_size_bytes() - 1; 1976756a025fSJoe Perches pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 1977349daa0fSRandy Dunlap &beginpa, &endpa); 1978bbc76be6SWen Congyang 1979eca499abSPavel Tatashin return -EBUSY; 1980eca499abSPavel Tatashin } 1981eca499abSPavel Tatashin return 0; 1982bbc76be6SWen Congyang } 1983bbc76be6SWen Congyang 1984a08a2ae3SOscar Salvador static int get_nr_vmemmap_pages_cb(struct memory_block *mem, void *arg) 1985a08a2ae3SOscar Salvador { 1986a08a2ae3SOscar Salvador /* 1987a08a2ae3SOscar Salvador * If not set, continue with the next block. 1988a08a2ae3SOscar Salvador */ 1989a08a2ae3SOscar Salvador return mem->nr_vmemmap_pages; 1990a08a2ae3SOscar Salvador } 1991a08a2ae3SOscar Salvador 1992b27340a5SMiaohe Lin static int check_cpu_on_node(int nid) 199360a5a19eSTang Chen { 199460a5a19eSTang Chen int cpu; 199560a5a19eSTang Chen 199660a5a19eSTang Chen for_each_present_cpu(cpu) { 1997b27340a5SMiaohe Lin if (cpu_to_node(cpu) == nid) 199860a5a19eSTang Chen /* 199960a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 200060a5a19eSTang Chen * offline this node. 200160a5a19eSTang Chen */ 200260a5a19eSTang Chen return -EBUSY; 200360a5a19eSTang Chen } 200460a5a19eSTang Chen 200560a5a19eSTang Chen return 0; 200660a5a19eSTang Chen } 200760a5a19eSTang Chen 20082c91f8fcSDavid Hildenbrand static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg) 20092c91f8fcSDavid Hildenbrand { 20102c91f8fcSDavid Hildenbrand int nid = *(int *)arg; 20112c91f8fcSDavid Hildenbrand 20122c91f8fcSDavid Hildenbrand /* 20132c91f8fcSDavid Hildenbrand * If a memory block belongs to multiple nodes, the stored nid is not 20142c91f8fcSDavid Hildenbrand * reliable. However, such blocks are always online (e.g., cannot get 20152c91f8fcSDavid Hildenbrand * offlined) and, therefore, are still spanned by the node. 20162c91f8fcSDavid Hildenbrand */ 20172c91f8fcSDavid Hildenbrand return mem->nid == nid ? -EEXIST : 0; 20182c91f8fcSDavid Hildenbrand } 20192c91f8fcSDavid Hildenbrand 20200f1cfe9dSToshi Kani /** 20210f1cfe9dSToshi Kani * try_offline_node 2022e8b098fcSMike Rapoport * @nid: the node ID 20230f1cfe9dSToshi Kani * 20240f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 20250f1cfe9dSToshi Kani * 20260f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 20270f1cfe9dSToshi Kani * and online/offline operations before this call. 20280f1cfe9dSToshi Kani */ 202990b30cdcSWen Congyang void try_offline_node(int nid) 203060a5a19eSTang Chen { 20312c91f8fcSDavid Hildenbrand int rc; 203260a5a19eSTang Chen 203360a5a19eSTang Chen /* 20342c91f8fcSDavid Hildenbrand * If the node still spans pages (especially ZONE_DEVICE), don't 20352c91f8fcSDavid Hildenbrand * offline it. A node spans memory after move_pfn_range_to_zone(), 20362c91f8fcSDavid Hildenbrand * e.g., after the memory block was onlined. 203760a5a19eSTang Chen */ 2038b27340a5SMiaohe Lin if (node_spanned_pages(nid)) 203960a5a19eSTang Chen return; 20402c91f8fcSDavid Hildenbrand 20412c91f8fcSDavid Hildenbrand /* 20422c91f8fcSDavid Hildenbrand * Especially offline memory blocks might not be spanned by the 20432c91f8fcSDavid Hildenbrand * node. They will get spanned by the node once they get onlined. 20442c91f8fcSDavid Hildenbrand * However, they link to the node in sysfs and can get onlined later. 20452c91f8fcSDavid Hildenbrand */ 20462c91f8fcSDavid Hildenbrand rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb); 20472c91f8fcSDavid Hildenbrand if (rc) 20482c91f8fcSDavid Hildenbrand return; 204960a5a19eSTang Chen 2050b27340a5SMiaohe Lin if (check_cpu_on_node(nid)) 205160a5a19eSTang Chen return; 205260a5a19eSTang Chen 205360a5a19eSTang Chen /* 205460a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 205560a5a19eSTang Chen * node now. 205660a5a19eSTang Chen */ 205760a5a19eSTang Chen node_set_offline(nid); 205860a5a19eSTang Chen unregister_one_node(nid); 205960a5a19eSTang Chen } 206090b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 206160a5a19eSTang Chen 2062e1c158e4SDavid Hildenbrand static int __ref try_remove_memory(u64 start, u64 size) 2063bbc76be6SWen Congyang { 2064a08a2ae3SOscar Salvador struct vmem_altmap mhp_altmap = {}; 2065a08a2ae3SOscar Salvador struct vmem_altmap *altmap = NULL; 2066a08a2ae3SOscar Salvador unsigned long nr_vmemmap_pages; 2067e1c158e4SDavid Hildenbrand int rc = 0, nid = NUMA_NO_NODE; 2068993c1aadSWen Congyang 206927356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 207027356f54SToshi Kani 20716677e3eaSYasuaki Ishimatsu /* 2072242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 2073eca499abSPavel Tatashin * whether all memory blocks in question are offline and return error 2074242831ebSRafael J. Wysocki * if this is not the case. 2075e1c158e4SDavid Hildenbrand * 2076e1c158e4SDavid Hildenbrand * While at it, determine the nid. Note that if we'd have mixed nodes, 2077e1c158e4SDavid Hildenbrand * we'd only try to offline the last determined one -- which is good 2078e1c158e4SDavid Hildenbrand * enough for the cases we care about. 20796677e3eaSYasuaki Ishimatsu */ 2080e1c158e4SDavid Hildenbrand rc = walk_memory_blocks(start, size, &nid, check_memblock_offlined_cb); 2081eca499abSPavel Tatashin if (rc) 2082b4223a51SJia He return rc; 20836677e3eaSYasuaki Ishimatsu 2084a08a2ae3SOscar Salvador /* 2085a08a2ae3SOscar Salvador * We only support removing memory added with MHP_MEMMAP_ON_MEMORY in 2086a08a2ae3SOscar Salvador * the same granularity it was added - a single memory block. 2087a08a2ae3SOscar Salvador */ 20886e02c46bSMuchun Song if (mhp_memmap_on_memory()) { 2089a08a2ae3SOscar Salvador nr_vmemmap_pages = walk_memory_blocks(start, size, NULL, 2090a08a2ae3SOscar Salvador get_nr_vmemmap_pages_cb); 2091a08a2ae3SOscar Salvador if (nr_vmemmap_pages) { 2092a08a2ae3SOscar Salvador if (size != memory_block_size_bytes()) { 2093a08a2ae3SOscar Salvador pr_warn("Refuse to remove %#llx - %#llx," 2094a08a2ae3SOscar Salvador "wrong granularity\n", 2095a08a2ae3SOscar Salvador start, start + size); 2096a08a2ae3SOscar Salvador return -EINVAL; 2097a08a2ae3SOscar Salvador } 2098a08a2ae3SOscar Salvador 2099a08a2ae3SOscar Salvador /* 2100a08a2ae3SOscar Salvador * Let remove_pmd_table->free_hugepage_table do the 2101a08a2ae3SOscar Salvador * right thing if we used vmem_altmap when hot-adding 2102a08a2ae3SOscar Salvador * the range. 2103a08a2ae3SOscar Salvador */ 2104a08a2ae3SOscar Salvador mhp_altmap.alloc = nr_vmemmap_pages; 2105a08a2ae3SOscar Salvador altmap = &mhp_altmap; 2106a08a2ae3SOscar Salvador } 2107a08a2ae3SOscar Salvador } 2108a08a2ae3SOscar Salvador 210946c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 211046c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 211146c66c4bSYasuaki Ishimatsu 2112f1037ec0SDan Williams /* 2113f1037ec0SDan Williams * Memory block device removal under the device_hotplug_lock is 2114f1037ec0SDan Williams * a barrier against racing online attempts. 2115f1037ec0SDan Williams */ 21164c4b7f9bSDavid Hildenbrand remove_memory_block_devices(start, size); 21174c4b7f9bSDavid Hildenbrand 2118f1037ec0SDan Williams mem_hotplug_begin(); 2119f1037ec0SDan Williams 212065a2aa5fSDavid Hildenbrand arch_remove_memory(start, size, altmap); 212152219aeaSDavid Hildenbrand 212252219aeaSDavid Hildenbrand if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 21233ecc6834SMike Rapoport memblock_phys_free(start, size); 212432d1fe8fSAnshuman Khandual memblock_remove(start, size); 212552219aeaSDavid Hildenbrand } 212652219aeaSDavid Hildenbrand 2127cb8e3c8bSDavid Hildenbrand release_mem_region_adjustable(start, size); 212824d335caSWen Congyang 2129e1c158e4SDavid Hildenbrand if (nid != NUMA_NO_NODE) 213060a5a19eSTang Chen try_offline_node(nid); 213160a5a19eSTang Chen 2132bfc8c901SVladimir Davydov mem_hotplug_done(); 2133b4223a51SJia He return 0; 213471088785SBadari Pulavarty } 2135d15e5926SDavid Hildenbrand 2136eca499abSPavel Tatashin /** 21375640c9caSMel Gorman * __remove_memory - Remove memory if every memory block is offline 2138eca499abSPavel Tatashin * @start: physical address of the region to remove 2139eca499abSPavel Tatashin * @size: size of the region to remove 2140eca499abSPavel Tatashin * 2141eca499abSPavel Tatashin * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 2142eca499abSPavel Tatashin * and online/offline operations before this call, as required by 2143eca499abSPavel Tatashin * try_offline_node(). 2144eca499abSPavel Tatashin */ 2145e1c158e4SDavid Hildenbrand void __remove_memory(u64 start, u64 size) 2146d15e5926SDavid Hildenbrand { 2147eca499abSPavel Tatashin 2148eca499abSPavel Tatashin /* 214929a90db9SSouptick Joarder * trigger BUG() if some memory is not offlined prior to calling this 2150eca499abSPavel Tatashin * function 2151eca499abSPavel Tatashin */ 2152e1c158e4SDavid Hildenbrand if (try_remove_memory(start, size)) 2153eca499abSPavel Tatashin BUG(); 2154eca499abSPavel Tatashin } 2155eca499abSPavel Tatashin 2156eca499abSPavel Tatashin /* 2157eca499abSPavel Tatashin * Remove memory if every memory block is offline, otherwise return -EBUSY is 2158eca499abSPavel Tatashin * some memory is not offline 2159eca499abSPavel Tatashin */ 2160e1c158e4SDavid Hildenbrand int remove_memory(u64 start, u64 size) 2161eca499abSPavel Tatashin { 2162eca499abSPavel Tatashin int rc; 2163eca499abSPavel Tatashin 2164d15e5926SDavid Hildenbrand lock_device_hotplug(); 2165e1c158e4SDavid Hildenbrand rc = try_remove_memory(start, size); 2166d15e5926SDavid Hildenbrand unlock_device_hotplug(); 2167eca499abSPavel Tatashin 2168eca499abSPavel Tatashin return rc; 2169d15e5926SDavid Hildenbrand } 217071088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 217108b3acd7SDavid Hildenbrand 21728dc4bb58SDavid Hildenbrand static int try_offline_memory_block(struct memory_block *mem, void *arg) 21738dc4bb58SDavid Hildenbrand { 21748dc4bb58SDavid Hildenbrand uint8_t online_type = MMOP_ONLINE_KERNEL; 21758dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 21768dc4bb58SDavid Hildenbrand struct page *page; 21778dc4bb58SDavid Hildenbrand int rc; 21788dc4bb58SDavid Hildenbrand 217908b3acd7SDavid Hildenbrand /* 21808dc4bb58SDavid Hildenbrand * Sense the online_type via the zone of the memory block. Offlining 21818dc4bb58SDavid Hildenbrand * with multiple zones within one memory block will be rejected 21828dc4bb58SDavid Hildenbrand * by offlining code ... so we don't care about that. 21838dc4bb58SDavid Hildenbrand */ 21848dc4bb58SDavid Hildenbrand page = pfn_to_online_page(section_nr_to_pfn(mem->start_section_nr)); 21858dc4bb58SDavid Hildenbrand if (page && zone_idx(page_zone(page)) == ZONE_MOVABLE) 21868dc4bb58SDavid Hildenbrand online_type = MMOP_ONLINE_MOVABLE; 21878dc4bb58SDavid Hildenbrand 21888dc4bb58SDavid Hildenbrand rc = device_offline(&mem->dev); 21898dc4bb58SDavid Hildenbrand /* 21908dc4bb58SDavid Hildenbrand * Default is MMOP_OFFLINE - change it only if offlining succeeded, 21918dc4bb58SDavid Hildenbrand * so try_reonline_memory_block() can do the right thing. 21928dc4bb58SDavid Hildenbrand */ 21938dc4bb58SDavid Hildenbrand if (!rc) 21948dc4bb58SDavid Hildenbrand **online_types = online_type; 21958dc4bb58SDavid Hildenbrand 21968dc4bb58SDavid Hildenbrand (*online_types)++; 21978dc4bb58SDavid Hildenbrand /* Ignore if already offline. */ 21988dc4bb58SDavid Hildenbrand return rc < 0 ? rc : 0; 21998dc4bb58SDavid Hildenbrand } 22008dc4bb58SDavid Hildenbrand 22018dc4bb58SDavid Hildenbrand static int try_reonline_memory_block(struct memory_block *mem, void *arg) 22028dc4bb58SDavid Hildenbrand { 22038dc4bb58SDavid Hildenbrand uint8_t **online_types = arg; 22048dc4bb58SDavid Hildenbrand int rc; 22058dc4bb58SDavid Hildenbrand 22068dc4bb58SDavid Hildenbrand if (**online_types != MMOP_OFFLINE) { 22078dc4bb58SDavid Hildenbrand mem->online_type = **online_types; 22088dc4bb58SDavid Hildenbrand rc = device_online(&mem->dev); 22098dc4bb58SDavid Hildenbrand if (rc < 0) 22108dc4bb58SDavid Hildenbrand pr_warn("%s: Failed to re-online memory: %d", 22118dc4bb58SDavid Hildenbrand __func__, rc); 22128dc4bb58SDavid Hildenbrand } 22138dc4bb58SDavid Hildenbrand 22148dc4bb58SDavid Hildenbrand /* Continue processing all remaining memory blocks. */ 22158dc4bb58SDavid Hildenbrand (*online_types)++; 22168dc4bb58SDavid Hildenbrand return 0; 22178dc4bb58SDavid Hildenbrand } 22188dc4bb58SDavid Hildenbrand 22198dc4bb58SDavid Hildenbrand /* 22208dc4bb58SDavid Hildenbrand * Try to offline and remove memory. Might take a long time to finish in case 22218dc4bb58SDavid Hildenbrand * memory is still in use. Primarily useful for memory devices that logically 22228dc4bb58SDavid Hildenbrand * unplugged all memory (so it's no longer in use) and want to offline + remove 22238dc4bb58SDavid Hildenbrand * that memory. 222408b3acd7SDavid Hildenbrand */ 2225e1c158e4SDavid Hildenbrand int offline_and_remove_memory(u64 start, u64 size) 222608b3acd7SDavid Hildenbrand { 22278dc4bb58SDavid Hildenbrand const unsigned long mb_count = size / memory_block_size_bytes(); 22288dc4bb58SDavid Hildenbrand uint8_t *online_types, *tmp; 22298dc4bb58SDavid Hildenbrand int rc; 223008b3acd7SDavid Hildenbrand 223108b3acd7SDavid Hildenbrand if (!IS_ALIGNED(start, memory_block_size_bytes()) || 22328dc4bb58SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes()) || !size) 22338dc4bb58SDavid Hildenbrand return -EINVAL; 223408b3acd7SDavid Hildenbrand 223508b3acd7SDavid Hildenbrand /* 22368dc4bb58SDavid Hildenbrand * We'll remember the old online type of each memory block, so we can 22378dc4bb58SDavid Hildenbrand * try to revert whatever we did when offlining one memory block fails 22388dc4bb58SDavid Hildenbrand * after offlining some others succeeded. 22398dc4bb58SDavid Hildenbrand */ 22408dc4bb58SDavid Hildenbrand online_types = kmalloc_array(mb_count, sizeof(*online_types), 22418dc4bb58SDavid Hildenbrand GFP_KERNEL); 22428dc4bb58SDavid Hildenbrand if (!online_types) 22438dc4bb58SDavid Hildenbrand return -ENOMEM; 22448dc4bb58SDavid Hildenbrand /* 22458dc4bb58SDavid Hildenbrand * Initialize all states to MMOP_OFFLINE, so when we abort processing in 22468dc4bb58SDavid Hildenbrand * try_offline_memory_block(), we'll skip all unprocessed blocks in 22478dc4bb58SDavid Hildenbrand * try_reonline_memory_block(). 22488dc4bb58SDavid Hildenbrand */ 22498dc4bb58SDavid Hildenbrand memset(online_types, MMOP_OFFLINE, mb_count); 22508dc4bb58SDavid Hildenbrand 22518dc4bb58SDavid Hildenbrand lock_device_hotplug(); 22528dc4bb58SDavid Hildenbrand 22538dc4bb58SDavid Hildenbrand tmp = online_types; 22548dc4bb58SDavid Hildenbrand rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block); 22558dc4bb58SDavid Hildenbrand 22568dc4bb58SDavid Hildenbrand /* 22578dc4bb58SDavid Hildenbrand * In case we succeeded to offline all memory, remove it. 225808b3acd7SDavid Hildenbrand * This cannot fail as it cannot get onlined in the meantime. 225908b3acd7SDavid Hildenbrand */ 226008b3acd7SDavid Hildenbrand if (!rc) { 2261e1c158e4SDavid Hildenbrand rc = try_remove_memory(start, size); 22628dc4bb58SDavid Hildenbrand if (rc) 22638dc4bb58SDavid Hildenbrand pr_err("%s: Failed to remove memory: %d", __func__, rc); 22648dc4bb58SDavid Hildenbrand } 22658dc4bb58SDavid Hildenbrand 22668dc4bb58SDavid Hildenbrand /* 22678dc4bb58SDavid Hildenbrand * Rollback what we did. While memory onlining might theoretically fail 22688dc4bb58SDavid Hildenbrand * (nacked by a notifier), it barely ever happens. 22698dc4bb58SDavid Hildenbrand */ 22708dc4bb58SDavid Hildenbrand if (rc) { 22718dc4bb58SDavid Hildenbrand tmp = online_types; 22728dc4bb58SDavid Hildenbrand walk_memory_blocks(start, size, &tmp, 22738dc4bb58SDavid Hildenbrand try_reonline_memory_block); 227408b3acd7SDavid Hildenbrand } 227508b3acd7SDavid Hildenbrand unlock_device_hotplug(); 227608b3acd7SDavid Hildenbrand 22778dc4bb58SDavid Hildenbrand kfree(online_types); 227808b3acd7SDavid Hildenbrand return rc; 227908b3acd7SDavid Hildenbrand } 228008b3acd7SDavid Hildenbrand EXPORT_SYMBOL_GPL(offline_and_remove_memory); 2281aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 2282