1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 23947be19SDave Hansen /* 33947be19SDave Hansen * linux/mm/memory_hotplug.c 43947be19SDave Hansen * 53947be19SDave Hansen * Copyright (C) 63947be19SDave Hansen */ 73947be19SDave Hansen 83947be19SDave Hansen #include <linux/stddef.h> 93947be19SDave Hansen #include <linux/mm.h> 10174cd4b1SIngo Molnar #include <linux/sched/signal.h> 113947be19SDave Hansen #include <linux/swap.h> 123947be19SDave Hansen #include <linux/interrupt.h> 133947be19SDave Hansen #include <linux/pagemap.h> 143947be19SDave Hansen #include <linux/compiler.h> 15b95f1b31SPaul Gortmaker #include <linux/export.h> 163947be19SDave Hansen #include <linux/pagevec.h> 172d1d43f6SChandra Seetharaman #include <linux/writeback.h> 183947be19SDave Hansen #include <linux/slab.h> 193947be19SDave Hansen #include <linux/sysctl.h> 203947be19SDave Hansen #include <linux/cpu.h> 213947be19SDave Hansen #include <linux/memory.h> 224b94ffdcSDan Williams #include <linux/memremap.h> 233947be19SDave Hansen #include <linux/memory_hotplug.h> 243947be19SDave Hansen #include <linux/highmem.h> 253947be19SDave Hansen #include <linux/vmalloc.h> 260a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 270c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 280c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 290c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 3071088785SBadari Pulavarty #include <linux/pfn.h> 316ad696d2SAndi Kleen #include <linux/suspend.h> 326d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 33d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h> 3460a5a19eSTang Chen #include <linux/stop_machine.h> 35c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h> 36c5320926STang Chen #include <linux/memblock.h> 37698b1b30SVlastimil Babka #include <linux/compaction.h> 38b15c8726SMichal Hocko #include <linux/rmap.h> 393947be19SDave Hansen 403947be19SDave Hansen #include <asm/tlbflush.h> 413947be19SDave Hansen 421e5ad9a3SAdrian Bunk #include "internal.h" 43e900a918SDan Williams #include "shuffle.h" 441e5ad9a3SAdrian Bunk 459d0ad8caSDaniel Kiper /* 469d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 479d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 489d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 499d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 509d0ad8caSDaniel Kiper */ 519d0ad8caSDaniel Kiper 52a9cd410aSArun KS static void generic_online_page(struct page *page, unsigned int order); 539d0ad8caSDaniel Kiper 549d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 55bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 569d0ad8caSDaniel Kiper 573f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 5820d6c96bSKOSAKI Motohiro 593f906ba2SThomas Gleixner void get_online_mems(void) 603f906ba2SThomas Gleixner { 613f906ba2SThomas Gleixner percpu_down_read(&mem_hotplug_lock); 623f906ba2SThomas Gleixner } 63bfc8c901SVladimir Davydov 643f906ba2SThomas Gleixner void put_online_mems(void) 653f906ba2SThomas Gleixner { 663f906ba2SThomas Gleixner percpu_up_read(&mem_hotplug_lock); 673f906ba2SThomas Gleixner } 68bfc8c901SVladimir Davydov 694932381eSMichal Hocko bool movable_node_enabled = false; 704932381eSMichal Hocko 718604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 7231bc3858SVitaly Kuznetsov bool memhp_auto_online; 738604d9e5SVitaly Kuznetsov #else 748604d9e5SVitaly Kuznetsov bool memhp_auto_online = true; 758604d9e5SVitaly Kuznetsov #endif 7631bc3858SVitaly Kuznetsov EXPORT_SYMBOL_GPL(memhp_auto_online); 7731bc3858SVitaly Kuznetsov 7886dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str) 7986dd995dSVitaly Kuznetsov { 8086dd995dSVitaly Kuznetsov if (!strcmp(str, "online")) 8186dd995dSVitaly Kuznetsov memhp_auto_online = true; 8286dd995dSVitaly Kuznetsov else if (!strcmp(str, "offline")) 8386dd995dSVitaly Kuznetsov memhp_auto_online = false; 8486dd995dSVitaly Kuznetsov 8586dd995dSVitaly Kuznetsov return 1; 8686dd995dSVitaly Kuznetsov } 8786dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state); 8886dd995dSVitaly Kuznetsov 8930467e0bSDavid Rientjes void mem_hotplug_begin(void) 90bfc8c901SVladimir Davydov { 913f906ba2SThomas Gleixner cpus_read_lock(); 923f906ba2SThomas Gleixner percpu_down_write(&mem_hotplug_lock); 93bfc8c901SVladimir Davydov } 94bfc8c901SVladimir Davydov 9530467e0bSDavid Rientjes void mem_hotplug_done(void) 96bfc8c901SVladimir Davydov { 973f906ba2SThomas Gleixner percpu_up_write(&mem_hotplug_lock); 983f906ba2SThomas Gleixner cpus_read_unlock(); 99bfc8c901SVladimir Davydov } 10020d6c96bSKOSAKI Motohiro 101357b4da5SJuergen Gross u64 max_mem_size = U64_MAX; 102357b4da5SJuergen Gross 10345e0b78bSKeith Mannthey /* add this memory to iomem resource */ 10445e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size) 10545e0b78bSKeith Mannthey { 1062794129eSDave Hansen struct resource *res; 1072794129eSDave Hansen unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1082794129eSDave Hansen char *resource_name = "System RAM"; 109357b4da5SJuergen Gross 110357b4da5SJuergen Gross if (start + size > max_mem_size) 111357b4da5SJuergen Gross return ERR_PTR(-E2BIG); 112357b4da5SJuergen Gross 1132794129eSDave Hansen /* 1142794129eSDave Hansen * Request ownership of the new memory range. This might be 1152794129eSDave Hansen * a child of an existing resource that was present but 1162794129eSDave Hansen * not marked as busy. 1172794129eSDave Hansen */ 1182794129eSDave Hansen res = __request_region(&iomem_resource, start, size, 1192794129eSDave Hansen resource_name, flags); 12045e0b78bSKeith Mannthey 1212794129eSDave Hansen if (!res) { 1222794129eSDave Hansen pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", 1232794129eSDave Hansen start, start + size); 1246f754ba4SVitaly Kuznetsov return ERR_PTR(-EEXIST); 12545e0b78bSKeith Mannthey } 12645e0b78bSKeith Mannthey return res; 12745e0b78bSKeith Mannthey } 12845e0b78bSKeith Mannthey 12945e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 13045e0b78bSKeith Mannthey { 13145e0b78bSKeith Mannthey if (!res) 13245e0b78bSKeith Mannthey return; 13345e0b78bSKeith Mannthey release_resource(res); 13445e0b78bSKeith Mannthey kfree(res); 13545e0b78bSKeith Mannthey return; 13645e0b78bSKeith Mannthey } 13745e0b78bSKeith Mannthey 13853947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 13946723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info, struct page *page, 1405f24ce5fSAndrea Arcangeli unsigned long type) 14104753278SYasunori Goto { 142ddffe98dSYasuaki Ishimatsu page->freelist = (void *)type; 14304753278SYasunori Goto SetPagePrivate(page); 14404753278SYasunori Goto set_page_private(page, info); 145fe896d18SJoonsoo Kim page_ref_inc(page); 14604753278SYasunori Goto } 14704753278SYasunori Goto 148170a5a7eSJiang Liu void put_page_bootmem(struct page *page) 14904753278SYasunori Goto { 1505f24ce5fSAndrea Arcangeli unsigned long type; 15104753278SYasunori Goto 152ddffe98dSYasuaki Ishimatsu type = (unsigned long) page->freelist; 1535f24ce5fSAndrea Arcangeli BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 1545f24ce5fSAndrea Arcangeli type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 15504753278SYasunori Goto 156fe896d18SJoonsoo Kim if (page_ref_dec_return(page) == 1) { 157ddffe98dSYasuaki Ishimatsu page->freelist = NULL; 15804753278SYasunori Goto ClearPagePrivate(page); 15904753278SYasunori Goto set_page_private(page, 0); 1605f24ce5fSAndrea Arcangeli INIT_LIST_HEAD(&page->lru); 161170a5a7eSJiang Liu free_reserved_page(page); 16204753278SYasunori Goto } 16304753278SYasunori Goto } 16404753278SYasunori Goto 16546723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 16646723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP 167d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn) 16804753278SYasunori Goto { 16904753278SYasunori Goto unsigned long *usemap, mapsize, section_nr, i; 17004753278SYasunori Goto struct mem_section *ms; 17104753278SYasunori Goto struct page *page, *memmap; 17204753278SYasunori Goto 17304753278SYasunori Goto section_nr = pfn_to_section_nr(start_pfn); 17404753278SYasunori Goto ms = __nr_to_section(section_nr); 17504753278SYasunori Goto 17604753278SYasunori Goto /* Get section's memmap address */ 17704753278SYasunori Goto memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 17804753278SYasunori Goto 17904753278SYasunori Goto /* 18004753278SYasunori Goto * Get page for the memmap's phys address 18104753278SYasunori Goto * XXX: need more consideration for sparse_vmemmap... 18204753278SYasunori Goto */ 18304753278SYasunori Goto page = virt_to_page(memmap); 18404753278SYasunori Goto mapsize = sizeof(struct page) * PAGES_PER_SECTION; 18504753278SYasunori Goto mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 18604753278SYasunori Goto 18704753278SYasunori Goto /* remember memmap's page */ 18804753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 18904753278SYasunori Goto get_page_bootmem(section_nr, page, SECTION_INFO); 19004753278SYasunori Goto 1919ac9322dSOscar Salvador usemap = ms->pageblock_flags; 19204753278SYasunori Goto page = virt_to_page(usemap); 19304753278SYasunori Goto 19404753278SYasunori Goto mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 19504753278SYasunori Goto 19604753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 197af370fb8SYasunori Goto get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 19804753278SYasunori Goto 19904753278SYasunori Goto } 20046723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */ 20146723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn) 20246723bfaSYasuaki Ishimatsu { 20346723bfaSYasuaki Ishimatsu unsigned long *usemap, mapsize, section_nr, i; 20446723bfaSYasuaki Ishimatsu struct mem_section *ms; 20546723bfaSYasuaki Ishimatsu struct page *page, *memmap; 20646723bfaSYasuaki Ishimatsu 20746723bfaSYasuaki Ishimatsu section_nr = pfn_to_section_nr(start_pfn); 20846723bfaSYasuaki Ishimatsu ms = __nr_to_section(section_nr); 20946723bfaSYasuaki Ishimatsu 21046723bfaSYasuaki Ishimatsu memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 21146723bfaSYasuaki Ishimatsu 21246723bfaSYasuaki Ishimatsu register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); 21346723bfaSYasuaki Ishimatsu 2149ac9322dSOscar Salvador usemap = ms->pageblock_flags; 21546723bfaSYasuaki Ishimatsu page = virt_to_page(usemap); 21646723bfaSYasuaki Ishimatsu 21746723bfaSYasuaki Ishimatsu mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 21846723bfaSYasuaki Ishimatsu 21946723bfaSYasuaki Ishimatsu for (i = 0; i < mapsize; i++, page++) 22046723bfaSYasuaki Ishimatsu get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 22146723bfaSYasuaki Ishimatsu } 22246723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 22304753278SYasunori Goto 2247ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat) 22504753278SYasunori Goto { 22604753278SYasunori Goto unsigned long i, pfn, end_pfn, nr_pages; 22704753278SYasunori Goto int node = pgdat->node_id; 22804753278SYasunori Goto struct page *page; 22904753278SYasunori Goto 23004753278SYasunori Goto nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 23104753278SYasunori Goto page = virt_to_page(pgdat); 23204753278SYasunori Goto 23304753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 23404753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 23504753278SYasunori Goto 23604753278SYasunori Goto pfn = pgdat->node_start_pfn; 237c1f19495SCody P Schafer end_pfn = pgdat_end_pfn(pgdat); 23804753278SYasunori Goto 2397e9f5eb0STang Chen /* register section info */ 240f14851afSqiuxishi for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 241f14851afSqiuxishi /* 242f14851afSqiuxishi * Some platforms can assign the same pfn to multiple nodes - on 243f14851afSqiuxishi * node0 as well as nodeN. To avoid registering a pfn against 244f14851afSqiuxishi * multiple nodes we check that this pfn does not already 2457e9f5eb0STang Chen * reside in some other nodes. 246f14851afSqiuxishi */ 247f65e91dfSYang Shi if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) 24804753278SYasunori Goto register_page_bootmem_info_section(pfn); 249f14851afSqiuxishi } 25004753278SYasunori Goto } 25146723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ 25204753278SYasunori Goto 253f1dd2cd1SMichal Hocko static int __meminit __add_section(int nid, unsigned long phys_start_pfn, 2547b73d978SChristoph Hellwig struct vmem_altmap *altmap, bool want_memblock) 2553947be19SDave Hansen { 2563947be19SDave Hansen int ret; 2573947be19SDave Hansen 258ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 259ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 260ebd15302SKAMEZAWA Hiroyuki 2614e0d2e7eSWei Yang ret = sparse_add_one_section(nid, phys_start_pfn, altmap); 2623947be19SDave Hansen if (ret < 0) 2633947be19SDave Hansen return ret; 2643947be19SDave Hansen 2651b862aecSMichal Hocko if (!want_memblock) 2661b862aecSMichal Hocko return 0; 2671b862aecSMichal Hocko 268fc44f7f9SPavel Tatashin return hotplug_memory_register(nid, __pfn_to_section(phys_start_pfn)); 2693947be19SDave Hansen } 2703947be19SDave Hansen 2714edd7cefSDavid Rientjes /* 2724edd7cefSDavid Rientjes * Reasonably generic function for adding memory. It is 2734edd7cefSDavid Rientjes * expected that archs that support memory hotplug will 2744edd7cefSDavid Rientjes * call this function after deciding the zone to which to 2754edd7cefSDavid Rientjes * add the new pages. 2764edd7cefSDavid Rientjes */ 277f1dd2cd1SMichal Hocko int __ref __add_pages(int nid, unsigned long phys_start_pfn, 278940519f0SMichal Hocko unsigned long nr_pages, struct mhp_restrictions *restrictions) 2794edd7cefSDavid Rientjes { 2804edd7cefSDavid Rientjes unsigned long i; 2814edd7cefSDavid Rientjes int err = 0; 2824edd7cefSDavid Rientjes int start_sec, end_sec; 283940519f0SMichal Hocko struct vmem_altmap *altmap = restrictions->altmap; 2844b94ffdcSDan Williams 2854edd7cefSDavid Rientjes /* during initialize mem_map, align hot-added range to section */ 2864edd7cefSDavid Rientjes start_sec = pfn_to_section_nr(phys_start_pfn); 2874edd7cefSDavid Rientjes end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 2884edd7cefSDavid Rientjes 2894b94ffdcSDan Williams if (altmap) { 2904b94ffdcSDan Williams /* 2914b94ffdcSDan Williams * Validate altmap is within bounds of the total request 2924b94ffdcSDan Williams */ 2934b94ffdcSDan Williams if (altmap->base_pfn != phys_start_pfn 2944b94ffdcSDan Williams || vmem_altmap_offset(altmap) > nr_pages) { 2954b94ffdcSDan Williams pr_warn_once("memory add fail, invalid altmap\n"); 2967cf91a98SJoonsoo Kim err = -EINVAL; 2977cf91a98SJoonsoo Kim goto out; 2984b94ffdcSDan Williams } 2994b94ffdcSDan Williams altmap->alloc = 0; 3004b94ffdcSDan Williams } 3014b94ffdcSDan Williams 3024edd7cefSDavid Rientjes for (i = start_sec; i <= end_sec; i++) { 3037b73d978SChristoph Hellwig err = __add_section(nid, section_nr_to_pfn(i), altmap, 304940519f0SMichal Hocko restrictions->flags & MHP_MEMBLOCK_API); 3054edd7cefSDavid Rientjes 3064edd7cefSDavid Rientjes /* 3074edd7cefSDavid Rientjes * EEXIST is finally dealt with by ioresource collision 3084edd7cefSDavid Rientjes * check. see add_memory() => register_memory_resource() 3094edd7cefSDavid Rientjes * Warning will be printed if there is collision. 3104edd7cefSDavid Rientjes */ 3114edd7cefSDavid Rientjes if (err && (err != -EEXIST)) 3124edd7cefSDavid Rientjes break; 3134edd7cefSDavid Rientjes err = 0; 314f64ac5e6SMichal Hocko cond_resched(); 3154edd7cefSDavid Rientjes } 316c435a390SZhu Guihua vmemmap_populate_print_last(); 3177cf91a98SJoonsoo Kim out: 3184edd7cefSDavid Rientjes return err; 3194edd7cefSDavid Rientjes } 3204edd7cefSDavid Rientjes 3214edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE 322815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 323d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, 324815121d2SYasuaki Ishimatsu unsigned long start_pfn, 325815121d2SYasuaki Ishimatsu unsigned long end_pfn) 326815121d2SYasuaki Ishimatsu { 327815121d2SYasuaki Ishimatsu struct mem_section *ms; 328815121d2SYasuaki Ishimatsu 329815121d2SYasuaki Ishimatsu for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) { 330815121d2SYasuaki Ishimatsu ms = __pfn_to_section(start_pfn); 331815121d2SYasuaki Ishimatsu 332815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 333815121d2SYasuaki Ishimatsu continue; 334815121d2SYasuaki Ishimatsu 335815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(start_pfn) != nid)) 336815121d2SYasuaki Ishimatsu continue; 337815121d2SYasuaki Ishimatsu 338815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(start_pfn))) 339815121d2SYasuaki Ishimatsu continue; 340815121d2SYasuaki Ishimatsu 341815121d2SYasuaki Ishimatsu return start_pfn; 342815121d2SYasuaki Ishimatsu } 343815121d2SYasuaki Ishimatsu 344815121d2SYasuaki Ishimatsu return 0; 345815121d2SYasuaki Ishimatsu } 346815121d2SYasuaki Ishimatsu 347815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 348d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, 349815121d2SYasuaki Ishimatsu unsigned long start_pfn, 350815121d2SYasuaki Ishimatsu unsigned long end_pfn) 351815121d2SYasuaki Ishimatsu { 352815121d2SYasuaki Ishimatsu struct mem_section *ms; 353815121d2SYasuaki Ishimatsu unsigned long pfn; 354815121d2SYasuaki Ishimatsu 355815121d2SYasuaki Ishimatsu /* pfn is the end pfn of a memory section. */ 356815121d2SYasuaki Ishimatsu pfn = end_pfn - 1; 357815121d2SYasuaki Ishimatsu for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) { 358815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 359815121d2SYasuaki Ishimatsu 360815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 361815121d2SYasuaki Ishimatsu continue; 362815121d2SYasuaki Ishimatsu 363815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(pfn) != nid)) 364815121d2SYasuaki Ishimatsu continue; 365815121d2SYasuaki Ishimatsu 366815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(pfn))) 367815121d2SYasuaki Ishimatsu continue; 368815121d2SYasuaki Ishimatsu 369815121d2SYasuaki Ishimatsu return pfn; 370815121d2SYasuaki Ishimatsu } 371815121d2SYasuaki Ishimatsu 372815121d2SYasuaki Ishimatsu return 0; 373815121d2SYasuaki Ishimatsu } 374815121d2SYasuaki Ishimatsu 375815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 376815121d2SYasuaki Ishimatsu unsigned long end_pfn) 377815121d2SYasuaki Ishimatsu { 378815121d2SYasuaki Ishimatsu unsigned long zone_start_pfn = zone->zone_start_pfn; 379c33bc315SXishi Qiu unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ 380c33bc315SXishi Qiu unsigned long zone_end_pfn = z; 381815121d2SYasuaki Ishimatsu unsigned long pfn; 382815121d2SYasuaki Ishimatsu struct mem_section *ms; 383815121d2SYasuaki Ishimatsu int nid = zone_to_nid(zone); 384815121d2SYasuaki Ishimatsu 385815121d2SYasuaki Ishimatsu zone_span_writelock(zone); 386815121d2SYasuaki Ishimatsu if (zone_start_pfn == start_pfn) { 387815121d2SYasuaki Ishimatsu /* 388815121d2SYasuaki Ishimatsu * If the section is smallest section in the zone, it need 389815121d2SYasuaki Ishimatsu * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 390815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 391815121d2SYasuaki Ishimatsu * for shrinking zone. 392815121d2SYasuaki Ishimatsu */ 393815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, zone, end_pfn, 394815121d2SYasuaki Ishimatsu zone_end_pfn); 395815121d2SYasuaki Ishimatsu if (pfn) { 396815121d2SYasuaki Ishimatsu zone->zone_start_pfn = pfn; 397815121d2SYasuaki Ishimatsu zone->spanned_pages = zone_end_pfn - pfn; 398815121d2SYasuaki Ishimatsu } 399815121d2SYasuaki Ishimatsu } else if (zone_end_pfn == end_pfn) { 400815121d2SYasuaki Ishimatsu /* 401815121d2SYasuaki Ishimatsu * If the section is biggest section in the zone, it need 402815121d2SYasuaki Ishimatsu * shrink zone->spanned_pages. 403815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 404815121d2SYasuaki Ishimatsu * shrinking zone. 405815121d2SYasuaki Ishimatsu */ 406815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, 407815121d2SYasuaki Ishimatsu start_pfn); 408815121d2SYasuaki Ishimatsu if (pfn) 409815121d2SYasuaki Ishimatsu zone->spanned_pages = pfn - zone_start_pfn + 1; 410815121d2SYasuaki Ishimatsu } 411815121d2SYasuaki Ishimatsu 412815121d2SYasuaki Ishimatsu /* 413815121d2SYasuaki Ishimatsu * The section is not biggest or smallest mem_section in the zone, it 414815121d2SYasuaki Ishimatsu * only creates a hole in the zone. So in this case, we need not 415815121d2SYasuaki Ishimatsu * change the zone. But perhaps, the zone has only hole data. Thus 416815121d2SYasuaki Ishimatsu * it check the zone has only hole or not. 417815121d2SYasuaki Ishimatsu */ 418815121d2SYasuaki Ishimatsu pfn = zone_start_pfn; 419815121d2SYasuaki Ishimatsu for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) { 420815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 421815121d2SYasuaki Ishimatsu 422815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 423815121d2SYasuaki Ishimatsu continue; 424815121d2SYasuaki Ishimatsu 425815121d2SYasuaki Ishimatsu if (page_zone(pfn_to_page(pfn)) != zone) 426815121d2SYasuaki Ishimatsu continue; 427815121d2SYasuaki Ishimatsu 428815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 429815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 430815121d2SYasuaki Ishimatsu continue; 431815121d2SYasuaki Ishimatsu 432815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 433815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 434815121d2SYasuaki Ishimatsu return; 435815121d2SYasuaki Ishimatsu } 436815121d2SYasuaki Ishimatsu 437815121d2SYasuaki Ishimatsu /* The zone has no valid section */ 438815121d2SYasuaki Ishimatsu zone->zone_start_pfn = 0; 439815121d2SYasuaki Ishimatsu zone->spanned_pages = 0; 440815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 441815121d2SYasuaki Ishimatsu } 442815121d2SYasuaki Ishimatsu 443815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat, 444815121d2SYasuaki Ishimatsu unsigned long start_pfn, unsigned long end_pfn) 445815121d2SYasuaki Ishimatsu { 446815121d2SYasuaki Ishimatsu unsigned long pgdat_start_pfn = pgdat->node_start_pfn; 44783285c72SXishi Qiu unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */ 44883285c72SXishi Qiu unsigned long pgdat_end_pfn = p; 449815121d2SYasuaki Ishimatsu unsigned long pfn; 450815121d2SYasuaki Ishimatsu struct mem_section *ms; 451815121d2SYasuaki Ishimatsu int nid = pgdat->node_id; 452815121d2SYasuaki Ishimatsu 453815121d2SYasuaki Ishimatsu if (pgdat_start_pfn == start_pfn) { 454815121d2SYasuaki Ishimatsu /* 455815121d2SYasuaki Ishimatsu * If the section is smallest section in the pgdat, it need 456815121d2SYasuaki Ishimatsu * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages. 457815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 458815121d2SYasuaki Ishimatsu * for shrinking zone. 459815121d2SYasuaki Ishimatsu */ 460815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, NULL, end_pfn, 461815121d2SYasuaki Ishimatsu pgdat_end_pfn); 462815121d2SYasuaki Ishimatsu if (pfn) { 463815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = pfn; 464815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pgdat_end_pfn - pfn; 465815121d2SYasuaki Ishimatsu } 466815121d2SYasuaki Ishimatsu } else if (pgdat_end_pfn == end_pfn) { 467815121d2SYasuaki Ishimatsu /* 468815121d2SYasuaki Ishimatsu * If the section is biggest section in the pgdat, it need 469815121d2SYasuaki Ishimatsu * shrink pgdat->node_spanned_pages. 470815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 471815121d2SYasuaki Ishimatsu * shrinking zone. 472815121d2SYasuaki Ishimatsu */ 473815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn, 474815121d2SYasuaki Ishimatsu start_pfn); 475815121d2SYasuaki Ishimatsu if (pfn) 476815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1; 477815121d2SYasuaki Ishimatsu } 478815121d2SYasuaki Ishimatsu 479815121d2SYasuaki Ishimatsu /* 480815121d2SYasuaki Ishimatsu * If the section is not biggest or smallest mem_section in the pgdat, 481815121d2SYasuaki Ishimatsu * it only creates a hole in the pgdat. So in this case, we need not 482815121d2SYasuaki Ishimatsu * change the pgdat. 483815121d2SYasuaki Ishimatsu * But perhaps, the pgdat has only hole data. Thus it check the pgdat 484815121d2SYasuaki Ishimatsu * has only hole or not. 485815121d2SYasuaki Ishimatsu */ 486815121d2SYasuaki Ishimatsu pfn = pgdat_start_pfn; 487815121d2SYasuaki Ishimatsu for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) { 488815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 489815121d2SYasuaki Ishimatsu 490815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 491815121d2SYasuaki Ishimatsu continue; 492815121d2SYasuaki Ishimatsu 493815121d2SYasuaki Ishimatsu if (pfn_to_nid(pfn) != nid) 494815121d2SYasuaki Ishimatsu continue; 495815121d2SYasuaki Ishimatsu 496815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 497815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 498815121d2SYasuaki Ishimatsu continue; 499815121d2SYasuaki Ishimatsu 500815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 501815121d2SYasuaki Ishimatsu return; 502815121d2SYasuaki Ishimatsu } 503815121d2SYasuaki Ishimatsu 504815121d2SYasuaki Ishimatsu /* The pgdat has no valid section */ 505815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = 0; 506815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = 0; 507815121d2SYasuaki Ishimatsu } 508815121d2SYasuaki Ishimatsu 509815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn) 510815121d2SYasuaki Ishimatsu { 511815121d2SYasuaki Ishimatsu struct pglist_data *pgdat = zone->zone_pgdat; 512815121d2SYasuaki Ishimatsu int nr_pages = PAGES_PER_SECTION; 513815121d2SYasuaki Ishimatsu unsigned long flags; 514815121d2SYasuaki Ishimatsu 515815121d2SYasuaki Ishimatsu pgdat_resize_lock(zone->zone_pgdat, &flags); 516815121d2SYasuaki Ishimatsu shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 517815121d2SYasuaki Ishimatsu shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages); 518815121d2SYasuaki Ishimatsu pgdat_resize_unlock(zone->zone_pgdat, &flags); 519815121d2SYasuaki Ishimatsu } 520815121d2SYasuaki Ishimatsu 5219d1d887dSDavid Hildenbrand static void __remove_section(struct zone *zone, struct mem_section *ms, 5229d1d887dSDavid Hildenbrand unsigned long map_offset, 5239d1d887dSDavid Hildenbrand struct vmem_altmap *altmap) 524ea01ea93SBadari Pulavarty { 525815121d2SYasuaki Ishimatsu unsigned long start_pfn; 526815121d2SYasuaki Ishimatsu int scn_nr; 527ea01ea93SBadari Pulavarty 5289d1d887dSDavid Hildenbrand if (WARN_ON_ONCE(!valid_section(ms))) 5299d1d887dSDavid Hildenbrand return; 530ea01ea93SBadari Pulavarty 531cb7b3a36SDavid Hildenbrand unregister_memory_section(ms); 532ea01ea93SBadari Pulavarty 533815121d2SYasuaki Ishimatsu scn_nr = __section_nr(ms); 5341dd2bfc8SYASUAKI ISHIMATSU start_pfn = section_nr_to_pfn((unsigned long)scn_nr); 535815121d2SYasuaki Ishimatsu __remove_zone(zone, start_pfn); 536815121d2SYasuaki Ishimatsu 53724b6d416SChristoph Hellwig sparse_remove_one_section(zone, ms, map_offset, altmap); 538ea01ea93SBadari Pulavarty } 539ea01ea93SBadari Pulavarty 540ea01ea93SBadari Pulavarty /** 541ea01ea93SBadari Pulavarty * __remove_pages() - remove sections of pages from a zone 542ea01ea93SBadari Pulavarty * @zone: zone from which pages need to be removed 543ea01ea93SBadari Pulavarty * @phys_start_pfn: starting pageframe (must be aligned to start of a section) 544ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 545e8b098fcSMike Rapoport * @altmap: alternative device page map or %NULL if default memmap is used 546ea01ea93SBadari Pulavarty * 547ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 548ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 549ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 550ea01ea93SBadari Pulavarty * calling offline_pages(). 551ea01ea93SBadari Pulavarty */ 552ac5c9426SDavid Hildenbrand void __remove_pages(struct zone *zone, unsigned long phys_start_pfn, 553da024512SChristoph Hellwig unsigned long nr_pages, struct vmem_altmap *altmap) 554ea01ea93SBadari Pulavarty { 555fe74ebb1SToshi Kani unsigned long i; 5564b94ffdcSDan Williams unsigned long map_offset = 0; 5579d1d887dSDavid Hildenbrand int sections_to_remove; 5584b94ffdcSDan Williams 5594b94ffdcSDan Williams /* In the ZONE_DEVICE case device driver owns the memory region */ 560514caf23SChristoph Hellwig if (is_dev_zone(zone)) 5614b94ffdcSDan Williams map_offset = vmem_altmap_offset(altmap); 562ea01ea93SBadari Pulavarty 5637cf91a98SJoonsoo Kim clear_zone_contiguous(zone); 5647cf91a98SJoonsoo Kim 565ea01ea93SBadari Pulavarty /* 566ea01ea93SBadari Pulavarty * We can only remove entire sections 567ea01ea93SBadari Pulavarty */ 568ea01ea93SBadari Pulavarty BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); 569ea01ea93SBadari Pulavarty BUG_ON(nr_pages % PAGES_PER_SECTION); 570ea01ea93SBadari Pulavarty 571ea01ea93SBadari Pulavarty sections_to_remove = nr_pages / PAGES_PER_SECTION; 572ea01ea93SBadari Pulavarty for (i = 0; i < sections_to_remove; i++) { 573ea01ea93SBadari Pulavarty unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; 5744b94ffdcSDan Williams 575dd33ad7bSMichal Hocko cond_resched(); 5769d1d887dSDavid Hildenbrand __remove_section(zone, __pfn_to_section(pfn), map_offset, 57724b6d416SChristoph Hellwig altmap); 5784b94ffdcSDan Williams map_offset = 0; 579ea01ea93SBadari Pulavarty } 5807cf91a98SJoonsoo Kim 5817cf91a98SJoonsoo Kim set_zone_contiguous(zone); 582ea01ea93SBadari Pulavarty } 5834edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */ 584ea01ea93SBadari Pulavarty 5859d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 5869d0ad8caSDaniel Kiper { 5879d0ad8caSDaniel Kiper int rc = -EINVAL; 5889d0ad8caSDaniel Kiper 589bfc8c901SVladimir Davydov get_online_mems(); 590bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 5919d0ad8caSDaniel Kiper 5929d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 5939d0ad8caSDaniel Kiper online_page_callback = callback; 5949d0ad8caSDaniel Kiper rc = 0; 5959d0ad8caSDaniel Kiper } 5969d0ad8caSDaniel Kiper 597bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 598bfc8c901SVladimir Davydov put_online_mems(); 5999d0ad8caSDaniel Kiper 6009d0ad8caSDaniel Kiper return rc; 6019d0ad8caSDaniel Kiper } 6029d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 6039d0ad8caSDaniel Kiper 6049d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 6059d0ad8caSDaniel Kiper { 6069d0ad8caSDaniel Kiper int rc = -EINVAL; 6079d0ad8caSDaniel Kiper 608bfc8c901SVladimir Davydov get_online_mems(); 609bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 6109d0ad8caSDaniel Kiper 6119d0ad8caSDaniel Kiper if (online_page_callback == callback) { 6129d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 6139d0ad8caSDaniel Kiper rc = 0; 6149d0ad8caSDaniel Kiper } 6159d0ad8caSDaniel Kiper 616bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 617bfc8c901SVladimir Davydov put_online_mems(); 6189d0ad8caSDaniel Kiper 6199d0ad8caSDaniel Kiper return rc; 6209d0ad8caSDaniel Kiper } 6219d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 6229d0ad8caSDaniel Kiper 6239d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page) 624180c06efSJeremy Fitzhardinge { 6259d0ad8caSDaniel Kiper } 6269d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits); 6279d0ad8caSDaniel Kiper 6289d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page) 6299d0ad8caSDaniel Kiper { 6303dcc0571SJiang Liu adjust_managed_page_count(page, 1); 6319d0ad8caSDaniel Kiper } 6329d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters); 633180c06efSJeremy Fitzhardinge 6349d0ad8caSDaniel Kiper void __online_page_free(struct page *page) 6359d0ad8caSDaniel Kiper { 6363dcc0571SJiang Liu __free_reserved_page(page); 637180c06efSJeremy Fitzhardinge } 6389d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free); 6399d0ad8caSDaniel Kiper 640a9cd410aSArun KS static void generic_online_page(struct page *page, unsigned int order) 6419d0ad8caSDaniel Kiper { 642cd02cf1aSQian Cai kernel_map_pages(page, 1 << order, 1); 643a9cd410aSArun KS __free_pages_core(page, order); 644a9cd410aSArun KS totalram_pages_add(1UL << order); 645a9cd410aSArun KS #ifdef CONFIG_HIGHMEM 646a9cd410aSArun KS if (PageHighMem(page)) 647a9cd410aSArun KS totalhigh_pages_add(1UL << order); 648a9cd410aSArun KS #endif 649a9cd410aSArun KS } 650a9cd410aSArun KS 651a9cd410aSArun KS static int online_pages_blocks(unsigned long start, unsigned long nr_pages) 652a9cd410aSArun KS { 653a9cd410aSArun KS unsigned long end = start + nr_pages; 654a9cd410aSArun KS int order, onlined_pages = 0; 655a9cd410aSArun KS 656a9cd410aSArun KS while (start < end) { 657a9cd410aSArun KS order = min(MAX_ORDER - 1, 658a9cd410aSArun KS get_order(PFN_PHYS(end) - PFN_PHYS(start))); 659a9cd410aSArun KS (*online_page_callback)(pfn_to_page(start), order); 660a9cd410aSArun KS 661a9cd410aSArun KS onlined_pages += (1UL << order); 662a9cd410aSArun KS start += (1UL << order); 663a9cd410aSArun KS } 664a9cd410aSArun KS return onlined_pages; 6659d0ad8caSDaniel Kiper } 666180c06efSJeremy Fitzhardinge 66775884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, 66875884fb1SKAMEZAWA Hiroyuki void *arg) 6693947be19SDave Hansen { 67075884fb1SKAMEZAWA Hiroyuki unsigned long onlined_pages = *(unsigned long *)arg; 6712d070eabSMichal Hocko 67275884fb1SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) 673a9cd410aSArun KS onlined_pages += online_pages_blocks(start_pfn, nr_pages); 6742d070eabSMichal Hocko 6752d070eabSMichal Hocko online_mem_sections(start_pfn, start_pfn + nr_pages); 6762d070eabSMichal Hocko 67775884fb1SKAMEZAWA Hiroyuki *(unsigned long *)arg = onlined_pages; 67875884fb1SKAMEZAWA Hiroyuki return 0; 67975884fb1SKAMEZAWA Hiroyuki } 68075884fb1SKAMEZAWA Hiroyuki 681d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 682d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 683d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 684d9713679SLai Jiangshan { 685d9713679SLai Jiangshan int nid = zone_to_nid(zone); 686d9713679SLai Jiangshan 68798fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 68898fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 68998fa15f3SAnshuman Khandual arg->status_change_nid_high = NUMA_NO_NODE; 6906715ddf9SLai Jiangshan 6916715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 692d9713679SLai Jiangshan arg->status_change_nid = nid; 6938efe33f4SOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 6948efe33f4SOscar Salvador arg->status_change_nid_normal = nid; 6958efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM 696d3ba3ae1SBaoquan He if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY)) 6978efe33f4SOscar Salvador arg->status_change_nid_high = nid; 6988efe33f4SOscar Salvador #endif 699d9713679SLai Jiangshan } 700d9713679SLai Jiangshan 701d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 702d9713679SLai Jiangshan { 703d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 704d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 705d9713679SLai Jiangshan 7066715ddf9SLai Jiangshan if (arg->status_change_nid_high >= 0) 707d9713679SLai Jiangshan node_set_state(node, N_HIGH_MEMORY); 7086715ddf9SLai Jiangshan 70983d83612SOscar Salvador if (arg->status_change_nid >= 0) 7106715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 711d9713679SLai Jiangshan } 712d9713679SLai Jiangshan 713f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 714f1dd2cd1SMichal Hocko unsigned long nr_pages) 715f1dd2cd1SMichal Hocko { 716f1dd2cd1SMichal Hocko unsigned long old_end_pfn = zone_end_pfn(zone); 717f1dd2cd1SMichal Hocko 718f1dd2cd1SMichal Hocko if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 719f1dd2cd1SMichal Hocko zone->zone_start_pfn = start_pfn; 720f1dd2cd1SMichal Hocko 721f1dd2cd1SMichal Hocko zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 722f1dd2cd1SMichal Hocko } 723f1dd2cd1SMichal Hocko 724f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 725f1dd2cd1SMichal Hocko unsigned long nr_pages) 726f1dd2cd1SMichal Hocko { 727f1dd2cd1SMichal Hocko unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 728f1dd2cd1SMichal Hocko 729f1dd2cd1SMichal Hocko if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 730f1dd2cd1SMichal Hocko pgdat->node_start_pfn = start_pfn; 731f1dd2cd1SMichal Hocko 732f1dd2cd1SMichal Hocko pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 733f1dd2cd1SMichal Hocko } 734f1dd2cd1SMichal Hocko 735a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 736a99583e7SChristoph Hellwig unsigned long nr_pages, struct vmem_altmap *altmap) 737f1dd2cd1SMichal Hocko { 738f1dd2cd1SMichal Hocko struct pglist_data *pgdat = zone->zone_pgdat; 739f1dd2cd1SMichal Hocko int nid = pgdat->node_id; 740f1dd2cd1SMichal Hocko unsigned long flags; 741f1dd2cd1SMichal Hocko 742f1dd2cd1SMichal Hocko clear_zone_contiguous(zone); 743f1dd2cd1SMichal Hocko 744f1dd2cd1SMichal Hocko /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ 745f1dd2cd1SMichal Hocko pgdat_resize_lock(pgdat, &flags); 746f1dd2cd1SMichal Hocko zone_span_writelock(zone); 747fa004ab7SWei Yang if (zone_is_empty(zone)) 748fa004ab7SWei Yang init_currently_empty_zone(zone, start_pfn, nr_pages); 749f1dd2cd1SMichal Hocko resize_zone_range(zone, start_pfn, nr_pages); 750f1dd2cd1SMichal Hocko zone_span_writeunlock(zone); 751f1dd2cd1SMichal Hocko resize_pgdat_range(pgdat, start_pfn, nr_pages); 752f1dd2cd1SMichal Hocko pgdat_resize_unlock(pgdat, &flags); 753f1dd2cd1SMichal Hocko 754f1dd2cd1SMichal Hocko /* 755f1dd2cd1SMichal Hocko * TODO now we have a visible range of pages which are not associated 756f1dd2cd1SMichal Hocko * with their zone properly. Not nice but set_pfnblock_flags_mask 757f1dd2cd1SMichal Hocko * expects the zone spans the pfn range. All the pages in the range 758f1dd2cd1SMichal Hocko * are reserved so nobody should be touching them so we should be safe 759f1dd2cd1SMichal Hocko */ 760a99583e7SChristoph Hellwig memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, 761a99583e7SChristoph Hellwig MEMMAP_HOTPLUG, altmap); 762f1dd2cd1SMichal Hocko 763f1dd2cd1SMichal Hocko set_zone_contiguous(zone); 764f1dd2cd1SMichal Hocko } 765f1dd2cd1SMichal Hocko 766f1dd2cd1SMichal Hocko /* 767c246a213SMichal Hocko * Returns a default kernel memory zone for the given pfn range. 768c246a213SMichal Hocko * If no kernel zone covers this pfn range it will automatically go 769c246a213SMichal Hocko * to the ZONE_NORMAL. 770c246a213SMichal Hocko */ 771c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 772c246a213SMichal Hocko unsigned long nr_pages) 773c246a213SMichal Hocko { 774c246a213SMichal Hocko struct pglist_data *pgdat = NODE_DATA(nid); 775c246a213SMichal Hocko int zid; 776c246a213SMichal Hocko 777c246a213SMichal Hocko for (zid = 0; zid <= ZONE_NORMAL; zid++) { 778c246a213SMichal Hocko struct zone *zone = &pgdat->node_zones[zid]; 779c246a213SMichal Hocko 780c246a213SMichal Hocko if (zone_intersects(zone, start_pfn, nr_pages)) 781c246a213SMichal Hocko return zone; 782c246a213SMichal Hocko } 783c246a213SMichal Hocko 784c246a213SMichal Hocko return &pgdat->node_zones[ZONE_NORMAL]; 785c246a213SMichal Hocko } 786c246a213SMichal Hocko 787c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 788c6f03e29SMichal Hocko unsigned long nr_pages) 789e5e68930SMichal Hocko { 790c6f03e29SMichal Hocko struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 791c6f03e29SMichal Hocko nr_pages); 792c6f03e29SMichal Hocko struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 793c6f03e29SMichal Hocko bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 794c6f03e29SMichal Hocko bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 795e5e68930SMichal Hocko 796e5e68930SMichal Hocko /* 797c6f03e29SMichal Hocko * We inherit the existing zone in a simple case where zones do not 798c6f03e29SMichal Hocko * overlap in the given range 799e5e68930SMichal Hocko */ 800c6f03e29SMichal Hocko if (in_kernel ^ in_movable) 801c6f03e29SMichal Hocko return (in_kernel) ? kernel_zone : movable_zone; 802e5e68930SMichal Hocko 803c6f03e29SMichal Hocko /* 804c6f03e29SMichal Hocko * If the range doesn't belong to any zone or two zones overlap in the 805c6f03e29SMichal Hocko * given range then we use movable zone only if movable_node is 806c6f03e29SMichal Hocko * enabled because we always online to a kernel zone by default. 807c6f03e29SMichal Hocko */ 808c6f03e29SMichal Hocko return movable_node_enabled ? movable_zone : kernel_zone; 8099f123ab5SMichal Hocko } 8109f123ab5SMichal Hocko 811e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn, 812e5e68930SMichal Hocko unsigned long nr_pages) 813f1dd2cd1SMichal Hocko { 814c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_KERNEL) 815c6f03e29SMichal Hocko return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 816f1dd2cd1SMichal Hocko 817c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_MOVABLE) 818c6f03e29SMichal Hocko return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 819f1dd2cd1SMichal Hocko 820c6f03e29SMichal Hocko return default_zone_for_pfn(nid, start_pfn, nr_pages); 821e5e68930SMichal Hocko } 822e5e68930SMichal Hocko 823e5e68930SMichal Hocko /* 824e5e68930SMichal Hocko * Associates the given pfn range with the given node and the zone appropriate 825e5e68930SMichal Hocko * for the given online type. 826e5e68930SMichal Hocko */ 827e5e68930SMichal Hocko static struct zone * __meminit move_pfn_range(int online_type, int nid, 828e5e68930SMichal Hocko unsigned long start_pfn, unsigned long nr_pages) 829e5e68930SMichal Hocko { 830e5e68930SMichal Hocko struct zone *zone; 831e5e68930SMichal Hocko 832e5e68930SMichal Hocko zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages); 833a99583e7SChristoph Hellwig move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL); 834f1dd2cd1SMichal Hocko return zone; 835df429ac0SReza Arbab } 83675884fb1SKAMEZAWA Hiroyuki 837511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) 83875884fb1SKAMEZAWA Hiroyuki { 839aa47228aSCody P Schafer unsigned long flags; 8403947be19SDave Hansen unsigned long onlined_pages = 0; 8413947be19SDave Hansen struct zone *zone; 8426811378eSYasunori Goto int need_zonelists_rebuild = 0; 8437b78d335SYasunori Goto int nid; 8447b78d335SYasunori Goto int ret; 8457b78d335SYasunori Goto struct memory_notify arg; 846d0dc12e8SPavel Tatashin struct memory_block *mem; 8473947be19SDave Hansen 848381eab4aSDavid Hildenbrand mem_hotplug_begin(); 849381eab4aSDavid Hildenbrand 850d0dc12e8SPavel Tatashin /* 851d0dc12e8SPavel Tatashin * We can't use pfn_to_nid() because nid might be stored in struct page 852d0dc12e8SPavel Tatashin * which is not yet initialized. Instead, we find nid from memory block. 853d0dc12e8SPavel Tatashin */ 854d0dc12e8SPavel Tatashin mem = find_memory_block(__pfn_to_section(pfn)); 855d0dc12e8SPavel Tatashin nid = mem->nid; 85689c02e69SDavid Hildenbrand put_device(&mem->dev); 857d0dc12e8SPavel Tatashin 858f1dd2cd1SMichal Hocko /* associate pfn range with the zone */ 859f1dd2cd1SMichal Hocko zone = move_pfn_range(online_type, nid, pfn, nr_pages); 860511c2abaSLai Jiangshan 8617b78d335SYasunori Goto arg.start_pfn = pfn; 8627b78d335SYasunori Goto arg.nr_pages = nr_pages; 863d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 8647b78d335SYasunori Goto 8657b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 8667b78d335SYasunori Goto ret = notifier_to_errno(ret); 867e33e33b4SChen Yucong if (ret) 868e33e33b4SChen Yucong goto failed_addition; 869e33e33b4SChen Yucong 8703947be19SDave Hansen /* 8716811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 8726811378eSYasunori Goto * This means the page allocator ignores this zone. 8736811378eSYasunori Goto * So, zonelist must be updated after online. 8746811378eSYasunori Goto */ 8756dcd73d7SWen Congyang if (!populated_zone(zone)) { 8766811378eSYasunori Goto need_zonelists_rebuild = 1; 87772675e13SMichal Hocko setup_zone_pageset(zone); 8786dcd73d7SWen Congyang } 8796811378eSYasunori Goto 880908eedc6SKAMEZAWA Hiroyuki ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, 88175884fb1SKAMEZAWA Hiroyuki online_pages_range); 882fd8a4221SGeoff Levand if (ret) { 8836dcd73d7SWen Congyang if (need_zonelists_rebuild) 8846dcd73d7SWen Congyang zone_pcp_reset(zone); 885e33e33b4SChen Yucong goto failed_addition; 886fd8a4221SGeoff Levand } 887fd8a4221SGeoff Levand 8883947be19SDave Hansen zone->present_pages += onlined_pages; 889aa47228aSCody P Schafer 890aa47228aSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 891f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 892aa47228aSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 893aa47228aSCody P Schafer 894e900a918SDan Williams shuffle_zone(zone); 895e900a918SDan Williams 89608dff7b7SJiang Liu if (onlined_pages) { 897e888ca35SVlastimil Babka node_states_set_node(nid, &arg); 8981f522509SHaicheng Li if (need_zonelists_rebuild) 89972675e13SMichal Hocko build_all_zonelists(NULL); 9001f522509SHaicheng Li else 901112067f0SShaohua Li zone_pcp_update(zone); 90208dff7b7SJiang Liu } 9031f522509SHaicheng Li 9041b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 9051b79acc9SKOSAKI Motohiro 906698b1b30SVlastimil Babka if (onlined_pages) { 907e888ca35SVlastimil Babka kswapd_run(nid); 908698b1b30SVlastimil Babka kcompactd_run(nid); 909698b1b30SVlastimil Babka } 91061b13993SDave Hansen 9115a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 9122f7f24ecSKent Liu 9132d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 9147b78d335SYasunori Goto 9157b78d335SYasunori Goto if (onlined_pages) 9167b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 917381eab4aSDavid Hildenbrand mem_hotplug_done(); 91830467e0bSDavid Rientjes return 0; 919e33e33b4SChen Yucong 920e33e33b4SChen Yucong failed_addition: 921e33e33b4SChen Yucong pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 922e33e33b4SChen Yucong (unsigned long long) pfn << PAGE_SHIFT, 923e33e33b4SChen Yucong (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 924e33e33b4SChen Yucong memory_notify(MEM_CANCEL_ONLINE, &arg); 925381eab4aSDavid Hildenbrand mem_hotplug_done(); 926e33e33b4SChen Yucong return ret; 9273947be19SDave Hansen } 92853947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 929bc02af93SYasunori Goto 9300bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 9310bd85420STang Chen { 9320bd85420STang Chen struct zone *z; 9330bd85420STang Chen 9340bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 9350bd85420STang Chen z->present_pages = 0; 9360bd85420STang Chen 9370bd85420STang Chen pgdat->node_present_pages = 0; 9380bd85420STang Chen } 9390bd85420STang Chen 940e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 941e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) 9429af3c2deSYasunori Goto { 9439af3c2deSYasunori Goto struct pglist_data *pgdat; 944c8e861a5SFabian Frederick unsigned long start_pfn = PFN_DOWN(start); 9459af3c2deSYasunori Goto 946a1e565aaSTang Chen pgdat = NODE_DATA(nid); 947a1e565aaSTang Chen if (!pgdat) { 9489af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 9499af3c2deSYasunori Goto if (!pgdat) 9509af3c2deSYasunori Goto return NULL; 9519af3c2deSYasunori Goto 9529af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 953b0dc3a34SGu Zheng } else { 954e716f2ebSMel Gorman /* 955e716f2ebSMel Gorman * Reset the nr_zones, order and classzone_idx before reuse. 956e716f2ebSMel Gorman * Note that kswapd will init kswapd_classzone_idx properly 957e716f2ebSMel Gorman * when it starts in the near future. 958e716f2ebSMel Gorman */ 959b0dc3a34SGu Zheng pgdat->nr_zones = 0; 96038087d9bSMel Gorman pgdat->kswapd_order = 0; 96138087d9bSMel Gorman pgdat->kswapd_classzone_idx = 0; 962a1e565aaSTang Chen } 9639af3c2deSYasunori Goto 9649af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 9659af3c2deSYasunori Goto 96603e85f9dSOscar Salvador pgdat->node_id = nid; 96703e85f9dSOscar Salvador pgdat->node_start_pfn = start_pfn; 96803e85f9dSOscar Salvador 9699af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 97003e85f9dSOscar Salvador free_area_init_core_hotplug(nid); 9715830169fSReza Arbab pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat); 9729af3c2deSYasunori Goto 973959ecc48SKAMEZAWA Hiroyuki /* 974959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 975959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 976959ecc48SKAMEZAWA Hiroyuki */ 97772675e13SMichal Hocko build_all_zonelists(pgdat); 978959ecc48SKAMEZAWA Hiroyuki 979f784a3f1STang Chen /* 9800bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 9810bd85420STang Chen * clear all zones' present_pages because they will be updated in 9820bd85420STang Chen * online_pages() and offline_pages(). 9830bd85420STang Chen */ 98403e85f9dSOscar Salvador reset_node_managed_pages(pgdat); 9850bd85420STang Chen reset_node_present_pages(pgdat); 9860bd85420STang Chen 9879af3c2deSYasunori Goto return pgdat; 9889af3c2deSYasunori Goto } 9899af3c2deSYasunori Goto 990b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid) 9919af3c2deSYasunori Goto { 992b9ff0360SOscar Salvador pg_data_t *pgdat = NODE_DATA(nid); 993b9ff0360SOscar Salvador 9949af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 9955830169fSReza Arbab free_percpu(pgdat->per_cpu_nodestats); 9969af3c2deSYasunori Goto arch_free_nodedata(pgdat); 9979af3c2deSYasunori Goto return; 9989af3c2deSYasunori Goto } 9999af3c2deSYasunori Goto 10000a547039SKAMEZAWA Hiroyuki 100101b0f197SToshi Kani /** 100201b0f197SToshi Kani * try_online_node - online a node if offlined 1003e8b098fcSMike Rapoport * @nid: the node ID 1004b9ff0360SOscar Salvador * @start: start addr of the node 1005b9ff0360SOscar Salvador * @set_node_online: Whether we want to online the node 1006cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1007b9ff0360SOscar Salvador * 1008b9ff0360SOscar Salvador * Returns: 1009b9ff0360SOscar Salvador * 1 -> a new node has been allocated 1010b9ff0360SOscar Salvador * 0 -> the node is already online 1011b9ff0360SOscar Salvador * -ENOMEM -> the node could not be allocated 1012cf23422bSminskey guo */ 1013b9ff0360SOscar Salvador static int __try_online_node(int nid, u64 start, bool set_node_online) 1014cf23422bSminskey guo { 1015cf23422bSminskey guo pg_data_t *pgdat; 1016b9ff0360SOscar Salvador int ret = 1; 1017cf23422bSminskey guo 101801b0f197SToshi Kani if (node_online(nid)) 101901b0f197SToshi Kani return 0; 102001b0f197SToshi Kani 1021b9ff0360SOscar Salvador pgdat = hotadd_new_pgdat(nid, start); 10227553e8f2SDavid Rientjes if (!pgdat) { 102301b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1024cf23422bSminskey guo ret = -ENOMEM; 1025cf23422bSminskey guo goto out; 1026cf23422bSminskey guo } 1027b9ff0360SOscar Salvador 1028b9ff0360SOscar Salvador if (set_node_online) { 1029cf23422bSminskey guo node_set_online(nid); 1030cf23422bSminskey guo ret = register_one_node(nid); 1031cf23422bSminskey guo BUG_ON(ret); 1032b9ff0360SOscar Salvador } 1033cf23422bSminskey guo out: 1034b9ff0360SOscar Salvador return ret; 1035b9ff0360SOscar Salvador } 1036b9ff0360SOscar Salvador 1037b9ff0360SOscar Salvador /* 1038b9ff0360SOscar Salvador * Users of this function always want to online/register the node 1039b9ff0360SOscar Salvador */ 1040b9ff0360SOscar Salvador int try_online_node(int nid) 1041b9ff0360SOscar Salvador { 1042b9ff0360SOscar Salvador int ret; 1043b9ff0360SOscar Salvador 1044b9ff0360SOscar Salvador mem_hotplug_begin(); 1045b9ff0360SOscar Salvador ret = __try_online_node(nid, 0, true); 1046bfc8c901SVladimir Davydov mem_hotplug_done(); 1047cf23422bSminskey guo return ret; 1048cf23422bSminskey guo } 1049cf23422bSminskey guo 105027356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 105127356f54SToshi Kani { 1052ba325585SPavel Tatashin /* memory range must be block size aligned */ 1053*cec3ebd0SDavid Hildenbrand if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || 1054*cec3ebd0SDavid Hildenbrand !IS_ALIGNED(size, memory_block_size_bytes())) { 1055ba325585SPavel Tatashin pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1056*cec3ebd0SDavid Hildenbrand memory_block_size_bytes(), start, size); 105727356f54SToshi Kani return -EINVAL; 105827356f54SToshi Kani } 105927356f54SToshi Kani 106027356f54SToshi Kani return 0; 106127356f54SToshi Kani } 106227356f54SToshi Kani 106331bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg) 106431bc3858SVitaly Kuznetsov { 1065dc18d706SNathan Fontenot return device_online(&mem->dev); 106631bc3858SVitaly Kuznetsov } 106731bc3858SVitaly Kuznetsov 10688df1d0e4SDavid Hildenbrand /* 10698df1d0e4SDavid Hildenbrand * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 10708df1d0e4SDavid Hildenbrand * and online/offline operations (triggered e.g. by sysfs). 10718df1d0e4SDavid Hildenbrand * 10728df1d0e4SDavid Hildenbrand * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 10738df1d0e4SDavid Hildenbrand */ 1074f29d8e9cSDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res) 1075bc02af93SYasunori Goto { 1076940519f0SMichal Hocko struct mhp_restrictions restrictions = { 1077940519f0SMichal Hocko .flags = MHP_MEMBLOCK_API, 1078940519f0SMichal Hocko }; 107962cedb9fSDavid Vrabel u64 start, size; 1080b9ff0360SOscar Salvador bool new_node = false; 1081bc02af93SYasunori Goto int ret; 1082bc02af93SYasunori Goto 108362cedb9fSDavid Vrabel start = res->start; 108462cedb9fSDavid Vrabel size = resource_size(res); 108562cedb9fSDavid Vrabel 108627356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 108727356f54SToshi Kani if (ret) 108827356f54SToshi Kani return ret; 108927356f54SToshi Kani 1090bfc8c901SVladimir Davydov mem_hotplug_begin(); 1091ac13c462SNathan Zimmer 10927f36e3e5STang Chen /* 10937f36e3e5STang Chen * Add new range to memblock so that when hotadd_new_pgdat() is called 10947f36e3e5STang Chen * to allocate new pgdat, get_pfn_range_for_nid() will be able to find 10957f36e3e5STang Chen * this new range and calculate total pages correctly. The range will 10967f36e3e5STang Chen * be removed at hot-remove time. 10977f36e3e5STang Chen */ 10987f36e3e5STang Chen memblock_add_node(start, size, nid); 10997f36e3e5STang Chen 1100b9ff0360SOscar Salvador ret = __try_online_node(nid, start, false); 1101b9ff0360SOscar Salvador if (ret < 0) 110241b9e2d7SWen Congyang goto error; 1103b9ff0360SOscar Salvador new_node = ret; 11049af3c2deSYasunori Goto 1105bc02af93SYasunori Goto /* call arch's memory hotadd */ 1106940519f0SMichal Hocko ret = arch_add_memory(nid, start, size, &restrictions); 11079af3c2deSYasunori Goto if (ret < 0) 11089af3c2deSYasunori Goto goto error; 11099af3c2deSYasunori Goto 1110a1e565aaSTang Chen if (new_node) { 1111d5b6f6a3SOscar Salvador /* If sysfs file of new node can't be created, cpu on the node 11120fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 11130fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 1114d5b6f6a3SOscar Salvador * We online node here. We can't roll back from here. 11150fc44159SYasunori Goto */ 1116d5b6f6a3SOscar Salvador node_set_online(nid); 1117d5b6f6a3SOscar Salvador ret = __register_one_node(nid); 11180fc44159SYasunori Goto BUG_ON(ret); 11190fc44159SYasunori Goto } 11200fc44159SYasunori Goto 1121d5b6f6a3SOscar Salvador /* link memory sections under this node.*/ 11224fbce633SOscar Salvador ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1)); 1123d5b6f6a3SOscar Salvador BUG_ON(ret); 1124d5b6f6a3SOscar Salvador 1125d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 1126d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1127d96ae530Sakpm@linux-foundation.org 1128381eab4aSDavid Hildenbrand /* device_online() will take the lock when calling online_pages() */ 1129381eab4aSDavid Hildenbrand mem_hotplug_done(); 1130381eab4aSDavid Hildenbrand 113131bc3858SVitaly Kuznetsov /* online pages if requested */ 1132f29d8e9cSDavid Hildenbrand if (memhp_auto_online) 113331bc3858SVitaly Kuznetsov walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), 113431bc3858SVitaly Kuznetsov NULL, online_memory_block); 113531bc3858SVitaly Kuznetsov 1136381eab4aSDavid Hildenbrand return ret; 11379af3c2deSYasunori Goto error: 11389af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 1139b9ff0360SOscar Salvador if (new_node) 1140b9ff0360SOscar Salvador rollback_node_hotadd(nid); 11417f36e3e5STang Chen memblock_remove(start, size); 1142bfc8c901SVladimir Davydov mem_hotplug_done(); 1143bc02af93SYasunori Goto return ret; 1144bc02af93SYasunori Goto } 114562cedb9fSDavid Vrabel 11468df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */ 11478df1d0e4SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size) 114862cedb9fSDavid Vrabel { 114962cedb9fSDavid Vrabel struct resource *res; 115062cedb9fSDavid Vrabel int ret; 115162cedb9fSDavid Vrabel 115262cedb9fSDavid Vrabel res = register_memory_resource(start, size); 11536f754ba4SVitaly Kuznetsov if (IS_ERR(res)) 11546f754ba4SVitaly Kuznetsov return PTR_ERR(res); 115562cedb9fSDavid Vrabel 1156f29d8e9cSDavid Hildenbrand ret = add_memory_resource(nid, res); 115762cedb9fSDavid Vrabel if (ret < 0) 115862cedb9fSDavid Vrabel release_memory_resource(res); 115962cedb9fSDavid Vrabel return ret; 116062cedb9fSDavid Vrabel } 11618df1d0e4SDavid Hildenbrand 11628df1d0e4SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size) 11638df1d0e4SDavid Hildenbrand { 11648df1d0e4SDavid Hildenbrand int rc; 11658df1d0e4SDavid Hildenbrand 11668df1d0e4SDavid Hildenbrand lock_device_hotplug(); 11678df1d0e4SDavid Hildenbrand rc = __add_memory(nid, start, size); 11688df1d0e4SDavid Hildenbrand unlock_device_hotplug(); 11698df1d0e4SDavid Hildenbrand 11708df1d0e4SDavid Hildenbrand return rc; 11718df1d0e4SDavid Hildenbrand } 1172bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 11730c0e6195SKAMEZAWA Hiroyuki 11740c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 11750c0e6195SKAMEZAWA Hiroyuki /* 11765c755e9fSBadari Pulavarty * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy 11775c755e9fSBadari Pulavarty * set and the size of the free page is given by page_order(). Using this, 11785c755e9fSBadari Pulavarty * the function determines if the pageblock contains only free pages. 11795c755e9fSBadari Pulavarty * Due to buddy contraints, a free page at least the size of a pageblock will 11805c755e9fSBadari Pulavarty * be located at the start of the pageblock 11815c755e9fSBadari Pulavarty */ 11825c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page) 11835c755e9fSBadari Pulavarty { 11845c755e9fSBadari Pulavarty return PageBuddy(page) && page_order(page) >= pageblock_order; 11855c755e9fSBadari Pulavarty } 11865c755e9fSBadari Pulavarty 1187891cb2a7SMichal Hocko /* Return the pfn of the start of the next active pageblock after a given pfn */ 1188891cb2a7SMichal Hocko static unsigned long next_active_pageblock(unsigned long pfn) 11895c755e9fSBadari Pulavarty { 1190891cb2a7SMichal Hocko struct page *page = pfn_to_page(pfn); 1191891cb2a7SMichal Hocko 11925c755e9fSBadari Pulavarty /* Ensure the starting page is pageblock-aligned */ 1193891cb2a7SMichal Hocko BUG_ON(pfn & (pageblock_nr_pages - 1)); 11945c755e9fSBadari Pulavarty 11955c755e9fSBadari Pulavarty /* If the entire pageblock is free, move to the end of free page */ 11960dcc48c1SKAMEZAWA Hiroyuki if (pageblock_free(page)) { 11970dcc48c1SKAMEZAWA Hiroyuki int order; 11980dcc48c1SKAMEZAWA Hiroyuki /* be careful. we don't have locks, page_order can be changed.*/ 11990dcc48c1SKAMEZAWA Hiroyuki order = page_order(page); 12000dcc48c1SKAMEZAWA Hiroyuki if ((order < MAX_ORDER) && (order >= pageblock_order)) 1201891cb2a7SMichal Hocko return pfn + (1 << order); 12020dcc48c1SKAMEZAWA Hiroyuki } 12035c755e9fSBadari Pulavarty 1204891cb2a7SMichal Hocko return pfn + pageblock_nr_pages; 12055c755e9fSBadari Pulavarty } 12065c755e9fSBadari Pulavarty 1207891cb2a7SMichal Hocko static bool is_pageblock_removable_nolock(unsigned long pfn) 1208fb52bbaeSMathieu Malaterre { 1209891cb2a7SMichal Hocko struct page *page = pfn_to_page(pfn); 1210fb52bbaeSMathieu Malaterre struct zone *zone; 1211fb52bbaeSMathieu Malaterre 1212fb52bbaeSMathieu Malaterre /* 1213fb52bbaeSMathieu Malaterre * We have to be careful here because we are iterating over memory 1214fb52bbaeSMathieu Malaterre * sections which are not zone aware so we might end up outside of 1215fb52bbaeSMathieu Malaterre * the zone but still within the section. 1216fb52bbaeSMathieu Malaterre * We have to take care about the node as well. If the node is offline 1217fb52bbaeSMathieu Malaterre * its NODE_DATA will be NULL - see page_zone. 1218fb52bbaeSMathieu Malaterre */ 1219fb52bbaeSMathieu Malaterre if (!node_online(page_to_nid(page))) 1220fb52bbaeSMathieu Malaterre return false; 1221fb52bbaeSMathieu Malaterre 1222fb52bbaeSMathieu Malaterre zone = page_zone(page); 1223fb52bbaeSMathieu Malaterre pfn = page_to_pfn(page); 1224fb52bbaeSMathieu Malaterre if (!zone_spans_pfn(zone, pfn)) 1225fb52bbaeSMathieu Malaterre return false; 1226fb52bbaeSMathieu Malaterre 1227d381c547SMichal Hocko return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON); 1228fb52bbaeSMathieu Malaterre } 1229fb52bbaeSMathieu Malaterre 12305c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */ 1231c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) 12325c755e9fSBadari Pulavarty { 1233891cb2a7SMichal Hocko unsigned long end_pfn, pfn; 1234891cb2a7SMichal Hocko 1235891cb2a7SMichal Hocko end_pfn = min(start_pfn + nr_pages, 1236891cb2a7SMichal Hocko zone_end_pfn(page_zone(pfn_to_page(start_pfn)))); 12375c755e9fSBadari Pulavarty 12385c755e9fSBadari Pulavarty /* Check the starting page of each pageblock within the range */ 1239891cb2a7SMichal Hocko for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) { 1240891cb2a7SMichal Hocko if (!is_pageblock_removable_nolock(pfn)) 1241c98940f6SYaowei Bai return false; 124249ac8255SKAMEZAWA Hiroyuki cond_resched(); 12435c755e9fSBadari Pulavarty } 12445c755e9fSBadari Pulavarty 12455c755e9fSBadari Pulavarty /* All pageblocks in the memory block are likely to be hot-removable */ 1246c98940f6SYaowei Bai return true; 12475c755e9fSBadari Pulavarty } 12485c755e9fSBadari Pulavarty 12495c755e9fSBadari Pulavarty /* 1250deb88a2aSToshi Kani * Confirm all pages in a range [start, end) belong to the same zone. 1251a96dfddbSToshi Kani * When true, return its valid [start, end). 12520c0e6195SKAMEZAWA Hiroyuki */ 1253a96dfddbSToshi Kani int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn, 1254a96dfddbSToshi Kani unsigned long *valid_start, unsigned long *valid_end) 12550c0e6195SKAMEZAWA Hiroyuki { 12565f0f2887SAndrew Banman unsigned long pfn, sec_end_pfn; 1257a96dfddbSToshi Kani unsigned long start, end; 12580c0e6195SKAMEZAWA Hiroyuki struct zone *zone = NULL; 12590c0e6195SKAMEZAWA Hiroyuki struct page *page; 12600c0e6195SKAMEZAWA Hiroyuki int i; 1261deb88a2aSToshi Kani for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1); 12620c0e6195SKAMEZAWA Hiroyuki pfn < end_pfn; 1263deb88a2aSToshi Kani pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) { 12645f0f2887SAndrew Banman /* Make sure the memory section is present first */ 12655f0f2887SAndrew Banman if (!present_section_nr(pfn_to_section_nr(pfn))) 12665f0f2887SAndrew Banman continue; 12675f0f2887SAndrew Banman for (; pfn < sec_end_pfn && pfn < end_pfn; 12680c0e6195SKAMEZAWA Hiroyuki pfn += MAX_ORDER_NR_PAGES) { 12690c0e6195SKAMEZAWA Hiroyuki i = 0; 12700c0e6195SKAMEZAWA Hiroyuki /* This is just a CONFIG_HOLES_IN_ZONE check.*/ 12715f0f2887SAndrew Banman while ((i < MAX_ORDER_NR_PAGES) && 12725f0f2887SAndrew Banman !pfn_valid_within(pfn + i)) 12730c0e6195SKAMEZAWA Hiroyuki i++; 1274d6d8c8a4Szhong jiang if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn) 12750c0e6195SKAMEZAWA Hiroyuki continue; 127624feb47cSMikhail Zaslonko /* Check if we got outside of the zone */ 127724feb47cSMikhail Zaslonko if (zone && !zone_spans_pfn(zone, pfn + i)) 127824feb47cSMikhail Zaslonko return 0; 12790c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn + i); 12800c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 12810c0e6195SKAMEZAWA Hiroyuki return 0; 1282a96dfddbSToshi Kani if (!zone) 1283a96dfddbSToshi Kani start = pfn + i; 12840c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 1285a96dfddbSToshi Kani end = pfn + MAX_ORDER_NR_PAGES; 12860c0e6195SKAMEZAWA Hiroyuki } 12875f0f2887SAndrew Banman } 1288deb88a2aSToshi Kani 1289a96dfddbSToshi Kani if (zone) { 1290a96dfddbSToshi Kani *valid_start = start; 1291d6d8c8a4Szhong jiang *valid_end = min(end, end_pfn); 12920c0e6195SKAMEZAWA Hiroyuki return 1; 1293a96dfddbSToshi Kani } else { 1294deb88a2aSToshi Kani return 0; 12950c0e6195SKAMEZAWA Hiroyuki } 1296a96dfddbSToshi Kani } 12970c0e6195SKAMEZAWA Hiroyuki 12980c0e6195SKAMEZAWA Hiroyuki /* 12990efadf48SYisheng Xie * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 13000efadf48SYisheng Xie * non-lru movable pages and hugepages). We scan pfn because it's much 13010efadf48SYisheng Xie * easier than scanning over linked list. This function returns the pfn 13020efadf48SYisheng Xie * of the first found movable page if it's found, otherwise 0. 13030c0e6195SKAMEZAWA Hiroyuki */ 1304c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end) 13050c0e6195SKAMEZAWA Hiroyuki { 13060c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 1307eeb0efd0SOscar Salvador 13080c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 1309eeb0efd0SOscar Salvador struct page *page, *head; 1310eeb0efd0SOscar Salvador unsigned long skip; 1311eeb0efd0SOscar Salvador 1312eeb0efd0SOscar Salvador if (!pfn_valid(pfn)) 1313eeb0efd0SOscar Salvador continue; 13140c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 13150c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 13160c0e6195SKAMEZAWA Hiroyuki return pfn; 13170efadf48SYisheng Xie if (__PageMovable(page)) 13180efadf48SYisheng Xie return pfn; 1319eeb0efd0SOscar Salvador 1320eeb0efd0SOscar Salvador if (!PageHuge(page)) 1321eeb0efd0SOscar Salvador continue; 1322eeb0efd0SOscar Salvador head = compound_head(page); 132339186cbeSOscar Salvador if (page_huge_active(head)) 1324c8721bbbSNaoya Horiguchi return pfn; 1325eeb0efd0SOscar Salvador skip = (1 << compound_order(head)) - (page - head); 1326eeb0efd0SOscar Salvador pfn += skip - 1; 13270c0e6195SKAMEZAWA Hiroyuki } 13280c0e6195SKAMEZAWA Hiroyuki return 0; 13290c0e6195SKAMEZAWA Hiroyuki } 13300c0e6195SKAMEZAWA Hiroyuki 1331666feb21SMichal Hocko static struct page *new_node_page(struct page *page, unsigned long private) 1332394e31d2SXishi Qiu { 1333394e31d2SXishi Qiu int nid = page_to_nid(page); 1334231e97e2SLi Zhong nodemask_t nmask = node_states[N_MEMORY]; 13357f252f27SMichal Hocko 13367f252f27SMichal Hocko /* 13377f252f27SMichal Hocko * try to allocate from a different node but reuse this node if there 13387f252f27SMichal Hocko * are no other online nodes to be used (e.g. we are offlining a part 13397f252f27SMichal Hocko * of the only existing node) 13407f252f27SMichal Hocko */ 13417f252f27SMichal Hocko node_clear(nid, nmask); 13427f252f27SMichal Hocko if (nodes_empty(nmask)) 13437f252f27SMichal Hocko node_set(nid, nmask); 1344394e31d2SXishi Qiu 13458b913238SMichal Hocko return new_page_nodemask(page, nid, &nmask); 1346394e31d2SXishi Qiu } 1347394e31d2SXishi Qiu 13480c0e6195SKAMEZAWA Hiroyuki static int 13490c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 13500c0e6195SKAMEZAWA Hiroyuki { 13510c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 13520c0e6195SKAMEZAWA Hiroyuki struct page *page; 13530c0e6195SKAMEZAWA Hiroyuki int ret = 0; 13540c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 13550c0e6195SKAMEZAWA Hiroyuki 1356a85009c3SMichal Hocko for (pfn = start_pfn; pfn < end_pfn; pfn++) { 13570c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 13580c0e6195SKAMEZAWA Hiroyuki continue; 13590c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 1360c8721bbbSNaoya Horiguchi 1361c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1362c8721bbbSNaoya Horiguchi struct page *head = compound_head(page); 1363daf3538aSOscar Salvador pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1; 1364daf3538aSOscar Salvador isolate_huge_page(head, &source); 1365c8721bbbSNaoya Horiguchi continue; 136694723aafSMichal Hocko } else if (PageTransHuge(page)) 13678135d892SNaoya Horiguchi pfn = page_to_pfn(compound_head(page)) 13688135d892SNaoya Horiguchi + hpage_nr_pages(page) - 1; 1369c8721bbbSNaoya Horiguchi 1370b15c8726SMichal Hocko /* 1371b15c8726SMichal Hocko * HWPoison pages have elevated reference counts so the migration would 1372b15c8726SMichal Hocko * fail on them. It also doesn't make any sense to migrate them in the 1373b15c8726SMichal Hocko * first place. Still try to unmap such a page in case it is still mapped 1374b15c8726SMichal Hocko * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep 1375b15c8726SMichal Hocko * the unmap as the catch all safety net). 1376b15c8726SMichal Hocko */ 1377b15c8726SMichal Hocko if (PageHWPoison(page)) { 1378b15c8726SMichal Hocko if (WARN_ON(PageLRU(page))) 1379b15c8726SMichal Hocko isolate_lru_page(page); 1380b15c8726SMichal Hocko if (page_mapped(page)) 1381b15c8726SMichal Hocko try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS); 1382b15c8726SMichal Hocko continue; 1383b15c8726SMichal Hocko } 1384b15c8726SMichal Hocko 1385700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 13860c0e6195SKAMEZAWA Hiroyuki continue; 13870c0e6195SKAMEZAWA Hiroyuki /* 13880efadf48SYisheng Xie * We can skip free pages. And we can deal with pages on 13890efadf48SYisheng Xie * LRU and non-lru movable pages. 13900c0e6195SKAMEZAWA Hiroyuki */ 13910efadf48SYisheng Xie if (PageLRU(page)) 139262695a84SNick Piggin ret = isolate_lru_page(page); 13930efadf48SYisheng Xie else 13940efadf48SYisheng Xie ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 13950c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 139662695a84SNick Piggin list_add_tail(&page->lru, &source); 13970efadf48SYisheng Xie if (!__PageMovable(page)) 1398599d0c95SMel Gorman inc_node_page_state(page, NR_ISOLATED_ANON + 13996d9c285aSKOSAKI Motohiro page_is_file_cache(page)); 14006d9c285aSKOSAKI Motohiro 14010c0e6195SKAMEZAWA Hiroyuki } else { 14022932c8b0SMichal Hocko pr_warn("failed to isolate pfn %lx\n", pfn); 14030efadf48SYisheng Xie dump_page(page, "isolation failed"); 14041723058eSOscar Salvador } 1405700c2a46SKonstantin Khlebnikov put_page(page); 14060c0e6195SKAMEZAWA Hiroyuki } 1407f3ab2636SBob Liu if (!list_empty(&source)) { 1408394e31d2SXishi Qiu /* Allocate a new page from the nearest neighbor node */ 1409394e31d2SXishi Qiu ret = migrate_pages(&source, new_node_page, NULL, 0, 14109c620e2bSHugh Dickins MIGRATE_SYNC, MR_MEMORY_HOTPLUG); 14112932c8b0SMichal Hocko if (ret) { 14122932c8b0SMichal Hocko list_for_each_entry(page, &source, lru) { 14132932c8b0SMichal Hocko pr_warn("migrating pfn %lx failed ret:%d ", 14142932c8b0SMichal Hocko page_to_pfn(page), ret); 14152932c8b0SMichal Hocko dump_page(page, "migration failure"); 14162932c8b0SMichal Hocko } 1417c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1418f3ab2636SBob Liu } 14192932c8b0SMichal Hocko } 14201723058eSOscar Salvador 14210c0e6195SKAMEZAWA Hiroyuki return ret; 14220c0e6195SKAMEZAWA Hiroyuki } 14230c0e6195SKAMEZAWA Hiroyuki 14240c0e6195SKAMEZAWA Hiroyuki /* 14250c0e6195SKAMEZAWA Hiroyuki * remove from free_area[] and mark all as Reserved. 14260c0e6195SKAMEZAWA Hiroyuki */ 14270c0e6195SKAMEZAWA Hiroyuki static int 14280c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, 14290c0e6195SKAMEZAWA Hiroyuki void *data) 14300c0e6195SKAMEZAWA Hiroyuki { 14315557c766SMichal Hocko unsigned long *offlined_pages = (unsigned long *)data; 14320c0e6195SKAMEZAWA Hiroyuki 14335557c766SMichal Hocko *offlined_pages += __offline_isolated_pages(start, start + nr_pages); 14345557c766SMichal Hocko return 0; 14350c0e6195SKAMEZAWA Hiroyuki } 14360c0e6195SKAMEZAWA Hiroyuki 14370c0e6195SKAMEZAWA Hiroyuki /* 14380c0e6195SKAMEZAWA Hiroyuki * Check all pages in range, recoreded as memory resource, are isolated. 14390c0e6195SKAMEZAWA Hiroyuki */ 14400c0e6195SKAMEZAWA Hiroyuki static int 14410c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, 14420c0e6195SKAMEZAWA Hiroyuki void *data) 14430c0e6195SKAMEZAWA Hiroyuki { 14445557c766SMichal Hocko return test_pages_isolated(start_pfn, start_pfn + nr_pages, true); 14450c0e6195SKAMEZAWA Hiroyuki } 14460c0e6195SKAMEZAWA Hiroyuki 1447c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1448c5320926STang Chen { 14494932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 145055ac590cSTang Chen movable_node_enabled = true; 14514932381eSMichal Hocko #else 14524932381eSMichal Hocko pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n"); 14534932381eSMichal Hocko #endif 1454c5320926STang Chen return 0; 1455c5320926STang Chen } 1456c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1457c5320926STang Chen 1458d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1459d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1460d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1461d9713679SLai Jiangshan { 1462d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1463d9713679SLai Jiangshan unsigned long present_pages = 0; 146486b27beaSOscar Salvador enum zone_type zt; 1465d9713679SLai Jiangshan 146698fa15f3SAnshuman Khandual arg->status_change_nid = NUMA_NO_NODE; 146798fa15f3SAnshuman Khandual arg->status_change_nid_normal = NUMA_NO_NODE; 146898fa15f3SAnshuman Khandual arg->status_change_nid_high = NUMA_NO_NODE; 146986b27beaSOscar Salvador 147086b27beaSOscar Salvador /* 147186b27beaSOscar Salvador * Check whether node_states[N_NORMAL_MEMORY] will be changed. 147286b27beaSOscar Salvador * If the memory to be offline is within the range 147386b27beaSOscar Salvador * [0..ZONE_NORMAL], and it is the last present memory there, 147486b27beaSOscar Salvador * the zones in that range will become empty after the offlining, 147586b27beaSOscar Salvador * thus we can determine that we need to clear the node from 147686b27beaSOscar Salvador * node_states[N_NORMAL_MEMORY]. 147786b27beaSOscar Salvador */ 147886b27beaSOscar Salvador for (zt = 0; zt <= ZONE_NORMAL; zt++) 147986b27beaSOscar Salvador present_pages += pgdat->node_zones[zt].present_pages; 148086b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 148186b27beaSOscar Salvador arg->status_change_nid_normal = zone_to_nid(zone); 1482d9713679SLai Jiangshan 14836715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM 14846715ddf9SLai Jiangshan /* 148586b27beaSOscar Salvador * node_states[N_HIGH_MEMORY] contains nodes which 148686b27beaSOscar Salvador * have normal memory or high memory. 148786b27beaSOscar Salvador * Here we add the present_pages belonging to ZONE_HIGHMEM. 148886b27beaSOscar Salvador * If the zone is within the range of [0..ZONE_HIGHMEM), and 148986b27beaSOscar Salvador * we determine that the zones in that range become empty, 149086b27beaSOscar Salvador * we need to clear the node for N_HIGH_MEMORY. 14916715ddf9SLai Jiangshan */ 149286b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages; 149386b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages) 14946715ddf9SLai Jiangshan arg->status_change_nid_high = zone_to_nid(zone); 14956715ddf9SLai Jiangshan #endif 14966715ddf9SLai Jiangshan 1497d9713679SLai Jiangshan /* 149886b27beaSOscar Salvador * We have accounted the pages from [0..ZONE_NORMAL), and 149986b27beaSOscar Salvador * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM 150086b27beaSOscar Salvador * as well. 150186b27beaSOscar Salvador * Here we count the possible pages from ZONE_MOVABLE. 150286b27beaSOscar Salvador * If after having accounted all the pages, we see that the nr_pages 150386b27beaSOscar Salvador * to be offlined is over or equal to the accounted pages, 150486b27beaSOscar Salvador * we know that the node will become empty, and so, we can clear 150586b27beaSOscar Salvador * it for N_MEMORY as well. 1506d9713679SLai Jiangshan */ 150786b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1508d9713679SLai Jiangshan 1509d9713679SLai Jiangshan if (nr_pages >= present_pages) 1510d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1511d9713679SLai Jiangshan } 1512d9713679SLai Jiangshan 1513d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1514d9713679SLai Jiangshan { 1515d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1516d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1517d9713679SLai Jiangshan 1518cf01f6f5SOscar Salvador if (arg->status_change_nid_high >= 0) 1519d9713679SLai Jiangshan node_clear_state(node, N_HIGH_MEMORY); 15206715ddf9SLai Jiangshan 1521cf01f6f5SOscar Salvador if (arg->status_change_nid >= 0) 15226715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1523d9713679SLai Jiangshan } 1524d9713679SLai Jiangshan 1525a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn, 1526ecde0f3eSMichal Hocko unsigned long end_pfn) 15270c0e6195SKAMEZAWA Hiroyuki { 1528ecde0f3eSMichal Hocko unsigned long pfn, nr_pages; 15295557c766SMichal Hocko unsigned long offlined_pages = 0; 15309b7ea46aSQian Cai int ret, node, nr_isolate_pageblock; 1531d702909fSCody P Schafer unsigned long flags; 1532a96dfddbSToshi Kani unsigned long valid_start, valid_end; 15330c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 15347b78d335SYasunori Goto struct memory_notify arg; 153579605093SMichal Hocko char *reason; 15360c0e6195SKAMEZAWA Hiroyuki 1537381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1538381eab4aSDavid Hildenbrand 15390c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 15400c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 1541381eab4aSDavid Hildenbrand if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, 1542381eab4aSDavid Hildenbrand &valid_end)) { 154379605093SMichal Hocko ret = -EINVAL; 154479605093SMichal Hocko reason = "multizone range"; 154579605093SMichal Hocko goto failed_removal; 1546381eab4aSDavid Hildenbrand } 15477b78d335SYasunori Goto 1548a96dfddbSToshi Kani zone = page_zone(pfn_to_page(valid_start)); 15497b78d335SYasunori Goto node = zone_to_nid(zone); 15507b78d335SYasunori Goto nr_pages = end_pfn - start_pfn; 15517b78d335SYasunori Goto 15520c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1553b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1554d381c547SMichal Hocko MIGRATE_MOVABLE, 1555d381c547SMichal Hocko SKIP_HWPOISON | REPORT_FAILURE); 15569b7ea46aSQian Cai if (ret < 0) { 155779605093SMichal Hocko reason = "failure to isolate range"; 155879605093SMichal Hocko goto failed_removal; 1559381eab4aSDavid Hildenbrand } 15609b7ea46aSQian Cai nr_isolate_pageblock = ret; 15617b78d335SYasunori Goto 15627b78d335SYasunori Goto arg.start_pfn = start_pfn; 15637b78d335SYasunori Goto arg.nr_pages = nr_pages; 1564d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 15657b78d335SYasunori Goto 15667b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 15677b78d335SYasunori Goto ret = notifier_to_errno(ret); 156879605093SMichal Hocko if (ret) { 156979605093SMichal Hocko reason = "notifier failure"; 157079605093SMichal Hocko goto failed_removal_isolated; 157179605093SMichal Hocko } 15727b78d335SYasunori Goto 1573bb8965bdSMichal Hocko do { 1574bb8965bdSMichal Hocko for (pfn = start_pfn; pfn;) { 157579605093SMichal Hocko if (signal_pending(current)) { 1576bb8965bdSMichal Hocko ret = -EINTR; 157779605093SMichal Hocko reason = "signal backoff"; 157879605093SMichal Hocko goto failed_removal_isolated; 157979605093SMichal Hocko } 158072b39cfcSMichal Hocko 15810c0e6195SKAMEZAWA Hiroyuki cond_resched(); 15829852a721SMichal Hocko lru_add_drain_all(); 15830c0e6195SKAMEZAWA Hiroyuki 1584bb8965bdSMichal Hocko pfn = scan_movable_pages(pfn, end_pfn); 1585bb8965bdSMichal Hocko if (pfn) { 1586bb8965bdSMichal Hocko /* 1587bb8965bdSMichal Hocko * TODO: fatal migration failures should bail 1588bb8965bdSMichal Hocko * out 1589bb8965bdSMichal Hocko */ 1590bb8965bdSMichal Hocko do_migrate_range(pfn, end_pfn); 1591bb8965bdSMichal Hocko } 15920c0e6195SKAMEZAWA Hiroyuki } 159372b39cfcSMichal Hocko 1594c8721bbbSNaoya Horiguchi /* 1595bb8965bdSMichal Hocko * Dissolve free hugepages in the memory block before doing 1596bb8965bdSMichal Hocko * offlining actually in order to make hugetlbfs's object 1597bb8965bdSMichal Hocko * counting consistent. 1598c8721bbbSNaoya Horiguchi */ 1599082d5b6bSGerald Schaefer ret = dissolve_free_huge_pages(start_pfn, end_pfn); 160079605093SMichal Hocko if (ret) { 160179605093SMichal Hocko reason = "failure to dissolve huge pages"; 160279605093SMichal Hocko goto failed_removal_isolated; 160379605093SMichal Hocko } 16040c0e6195SKAMEZAWA Hiroyuki /* check again */ 16055557c766SMichal Hocko ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, 16065557c766SMichal Hocko NULL, check_pages_isolated_cb); 16075557c766SMichal Hocko } while (ret); 1608bb8965bdSMichal Hocko 1609b3834be5SAdam Buchbinder /* Ok, all of our target is isolated. 16100c0e6195SKAMEZAWA Hiroyuki We cannot do rollback at this point. */ 16115557c766SMichal Hocko walk_system_ram_range(start_pfn, end_pfn - start_pfn, 16125557c766SMichal Hocko &offlined_pages, offline_isolated_pages_cb); 16135557c766SMichal Hocko pr_info("Offlined Pages %ld\n", offlined_pages); 16149b7ea46aSQian Cai /* 16159b7ea46aSQian Cai * Onlining will reset pagetype flags and makes migrate type 16169b7ea46aSQian Cai * MOVABLE, so just need to decrease the number of isolated 16179b7ea46aSQian Cai * pageblocks zone counter here. 16189b7ea46aSQian Cai */ 16199b7ea46aSQian Cai spin_lock_irqsave(&zone->lock, flags); 16209b7ea46aSQian Cai zone->nr_isolate_pageblock -= nr_isolate_pageblock; 16219b7ea46aSQian Cai spin_unlock_irqrestore(&zone->lock, flags); 16229b7ea46aSQian Cai 16230c0e6195SKAMEZAWA Hiroyuki /* removal success */ 16243dcc0571SJiang Liu adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); 16250c0e6195SKAMEZAWA Hiroyuki zone->present_pages -= offlined_pages; 1626d702909fSCody P Schafer 1627d702909fSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 16280c0e6195SKAMEZAWA Hiroyuki zone->zone_pgdat->node_present_pages -= offlined_pages; 1629d702909fSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 16307b78d335SYasunori Goto 16311b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 16321b79acc9SKOSAKI Motohiro 16331e8537baSXishi Qiu if (!populated_zone(zone)) { 1634340175b7SJiang Liu zone_pcp_reset(zone); 163572675e13SMichal Hocko build_all_zonelists(NULL); 16361e8537baSXishi Qiu } else 16371e8537baSXishi Qiu zone_pcp_update(zone); 1638340175b7SJiang Liu 1639d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1640698b1b30SVlastimil Babka if (arg.status_change_nid >= 0) { 16418fe23e05SDavid Rientjes kswapd_stop(node); 1642698b1b30SVlastimil Babka kcompactd_stop(node); 1643698b1b30SVlastimil Babka } 1644bce7394aSMinchan Kim 16450c0e6195SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 16460c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 16477b78d335SYasunori Goto 16487b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 1649381eab4aSDavid Hildenbrand mem_hotplug_done(); 16500c0e6195SKAMEZAWA Hiroyuki return 0; 16510c0e6195SKAMEZAWA Hiroyuki 165279605093SMichal Hocko failed_removal_isolated: 165379605093SMichal Hocko undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 1654c4efe484SQian Cai memory_notify(MEM_CANCEL_OFFLINE, &arg); 16550c0e6195SKAMEZAWA Hiroyuki failed_removal: 165679605093SMichal Hocko pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 1657a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 165879605093SMichal Hocko ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 165979605093SMichal Hocko reason); 16600c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 1661381eab4aSDavid Hildenbrand mem_hotplug_done(); 16620c0e6195SKAMEZAWA Hiroyuki return ret; 16630c0e6195SKAMEZAWA Hiroyuki } 166471088785SBadari Pulavarty 1665a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages) 1666a16cee10SWen Congyang { 1667ecde0f3eSMichal Hocko return __offline_pages(start_pfn, start_pfn + nr_pages); 1668a16cee10SWen Congyang } 1669e2ff3940SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 1670a16cee10SWen Congyang 1671bbc76be6SWen Congyang /** 1672bbc76be6SWen Congyang * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn) 1673bbc76be6SWen Congyang * @start_pfn: start pfn of the memory range 1674e05c4bbfSToshi Kani * @end_pfn: end pfn of the memory range 1675bbc76be6SWen Congyang * @arg: argument passed to func 1676bbc76be6SWen Congyang * @func: callback for each memory section walked 1677bbc76be6SWen Congyang * 1678bbc76be6SWen Congyang * This function walks through all present mem sections in range 1679bbc76be6SWen Congyang * [start_pfn, end_pfn) and call func on each mem section. 1680bbc76be6SWen Congyang * 1681bbc76be6SWen Congyang * Returns the return value of func. 1682bbc76be6SWen Congyang */ 1683e2ff3940SRafael J. Wysocki int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, 1684bbc76be6SWen Congyang void *arg, int (*func)(struct memory_block *, void *)) 168571088785SBadari Pulavarty { 1686e90bdb7fSWen Congyang struct memory_block *mem = NULL; 1687e90bdb7fSWen Congyang struct mem_section *section; 1688e90bdb7fSWen Congyang unsigned long pfn, section_nr; 1689e90bdb7fSWen Congyang int ret; 169071088785SBadari Pulavarty 1691e90bdb7fSWen Congyang for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 1692e90bdb7fSWen Congyang section_nr = pfn_to_section_nr(pfn); 1693e90bdb7fSWen Congyang if (!present_section_nr(section_nr)) 1694e90bdb7fSWen Congyang continue; 1695e90bdb7fSWen Congyang 1696e90bdb7fSWen Congyang section = __nr_to_section(section_nr); 1697e90bdb7fSWen Congyang /* same memblock? */ 1698e90bdb7fSWen Congyang if (mem) 1699e90bdb7fSWen Congyang if ((section_nr >= mem->start_section_nr) && 1700e90bdb7fSWen Congyang (section_nr <= mem->end_section_nr)) 1701e90bdb7fSWen Congyang continue; 1702e90bdb7fSWen Congyang 1703e90bdb7fSWen Congyang mem = find_memory_block_hinted(section, mem); 1704e90bdb7fSWen Congyang if (!mem) 1705e90bdb7fSWen Congyang continue; 1706e90bdb7fSWen Congyang 1707bbc76be6SWen Congyang ret = func(mem, arg); 1708e90bdb7fSWen Congyang if (ret) { 1709e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1710e90bdb7fSWen Congyang return ret; 1711e90bdb7fSWen Congyang } 1712e90bdb7fSWen Congyang } 1713e90bdb7fSWen Congyang 1714e90bdb7fSWen Congyang if (mem) 1715e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1716e90bdb7fSWen Congyang 1717bbc76be6SWen Congyang return 0; 1718bbc76be6SWen Congyang } 1719bbc76be6SWen Congyang 1720e2ff3940SRafael J. Wysocki #ifdef CONFIG_MEMORY_HOTREMOVE 1721d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1722bbc76be6SWen Congyang { 1723bbc76be6SWen Congyang int ret = !is_memblock_offlined(mem); 1724bbc76be6SWen Congyang 1725349daa0fSRandy Dunlap if (unlikely(ret)) { 1726349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 1727349daa0fSRandy Dunlap 1728349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1729349daa0fSRandy Dunlap endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1; 1730756a025fSJoe Perches pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 1731349daa0fSRandy Dunlap &beginpa, &endpa); 1732bbc76be6SWen Congyang 1733eca499abSPavel Tatashin return -EBUSY; 1734eca499abSPavel Tatashin } 1735eca499abSPavel Tatashin return 0; 1736bbc76be6SWen Congyang } 1737bbc76be6SWen Congyang 17380f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat) 173960a5a19eSTang Chen { 174060a5a19eSTang Chen int cpu; 174160a5a19eSTang Chen 174260a5a19eSTang Chen for_each_present_cpu(cpu) { 174360a5a19eSTang Chen if (cpu_to_node(cpu) == pgdat->node_id) 174460a5a19eSTang Chen /* 174560a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 174660a5a19eSTang Chen * offline this node. 174760a5a19eSTang Chen */ 174860a5a19eSTang Chen return -EBUSY; 174960a5a19eSTang Chen } 175060a5a19eSTang Chen 175160a5a19eSTang Chen return 0; 175260a5a19eSTang Chen } 175360a5a19eSTang Chen 17540f1cfe9dSToshi Kani /** 17550f1cfe9dSToshi Kani * try_offline_node 1756e8b098fcSMike Rapoport * @nid: the node ID 17570f1cfe9dSToshi Kani * 17580f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 17590f1cfe9dSToshi Kani * 17600f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 17610f1cfe9dSToshi Kani * and online/offline operations before this call. 17620f1cfe9dSToshi Kani */ 176390b30cdcSWen Congyang void try_offline_node(int nid) 176460a5a19eSTang Chen { 1765d822b86aSWen Congyang pg_data_t *pgdat = NODE_DATA(nid); 1766d822b86aSWen Congyang unsigned long start_pfn = pgdat->node_start_pfn; 1767d822b86aSWen Congyang unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; 176860a5a19eSTang Chen unsigned long pfn; 176960a5a19eSTang Chen 177060a5a19eSTang Chen for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 177160a5a19eSTang Chen unsigned long section_nr = pfn_to_section_nr(pfn); 177260a5a19eSTang Chen 177360a5a19eSTang Chen if (!present_section_nr(section_nr)) 177460a5a19eSTang Chen continue; 177560a5a19eSTang Chen 177660a5a19eSTang Chen if (pfn_to_nid(pfn) != nid) 177760a5a19eSTang Chen continue; 177860a5a19eSTang Chen 177960a5a19eSTang Chen /* 178060a5a19eSTang Chen * some memory sections of this node are not removed, and we 178160a5a19eSTang Chen * can't offline node now. 178260a5a19eSTang Chen */ 178360a5a19eSTang Chen return; 178460a5a19eSTang Chen } 178560a5a19eSTang Chen 178646a3679bSMichal Hocko if (check_cpu_on_node(pgdat)) 178760a5a19eSTang Chen return; 178860a5a19eSTang Chen 178960a5a19eSTang Chen /* 179060a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 179160a5a19eSTang Chen * node now. 179260a5a19eSTang Chen */ 179360a5a19eSTang Chen node_set_offline(nid); 179460a5a19eSTang Chen unregister_one_node(nid); 179560a5a19eSTang Chen } 179690b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 179760a5a19eSTang Chen 1798d9eb1417SDavid Hildenbrand static void __release_memory_resource(resource_size_t start, 1799d9eb1417SDavid Hildenbrand resource_size_t size) 1800d9eb1417SDavid Hildenbrand { 1801d9eb1417SDavid Hildenbrand int ret; 1802d9eb1417SDavid Hildenbrand 1803d9eb1417SDavid Hildenbrand /* 1804d9eb1417SDavid Hildenbrand * When removing memory in the same granularity as it was added, 1805d9eb1417SDavid Hildenbrand * this function never fails. It might only fail if resources 1806d9eb1417SDavid Hildenbrand * have to be adjusted or split. We'll ignore the error, as 1807d9eb1417SDavid Hildenbrand * removing of memory cannot fail. 1808d9eb1417SDavid Hildenbrand */ 1809d9eb1417SDavid Hildenbrand ret = release_mem_region_adjustable(&iomem_resource, start, size); 1810d9eb1417SDavid Hildenbrand if (ret) { 1811d9eb1417SDavid Hildenbrand resource_size_t endres = start + size - 1; 1812d9eb1417SDavid Hildenbrand 1813d9eb1417SDavid Hildenbrand pr_warn("Unable to release resource <%pa-%pa> (%d)\n", 1814d9eb1417SDavid Hildenbrand &start, &endres, ret); 1815d9eb1417SDavid Hildenbrand } 1816d9eb1417SDavid Hildenbrand } 1817d9eb1417SDavid Hildenbrand 1818eca499abSPavel Tatashin static int __ref try_remove_memory(int nid, u64 start, u64 size) 1819bbc76be6SWen Congyang { 1820eca499abSPavel Tatashin int rc = 0; 1821993c1aadSWen Congyang 182227356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 182327356f54SToshi Kani 1824bfc8c901SVladimir Davydov mem_hotplug_begin(); 18256677e3eaSYasuaki Ishimatsu 18266677e3eaSYasuaki Ishimatsu /* 1827242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 1828eca499abSPavel Tatashin * whether all memory blocks in question are offline and return error 1829242831ebSRafael J. Wysocki * if this is not the case. 18306677e3eaSYasuaki Ishimatsu */ 1831eca499abSPavel Tatashin rc = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, 1832d6de9d53SXishi Qiu check_memblock_offlined_cb); 1833eca499abSPavel Tatashin if (rc) 1834eca499abSPavel Tatashin goto done; 18356677e3eaSYasuaki Ishimatsu 183646c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 183746c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 1838f9126ab9SXishi Qiu memblock_free(start, size); 1839f9126ab9SXishi Qiu memblock_remove(start, size); 184046c66c4bSYasuaki Ishimatsu 18412c2a5af6SOscar Salvador arch_remove_memory(nid, start, size, NULL); 1842d9eb1417SDavid Hildenbrand __release_memory_resource(start, size); 184324d335caSWen Congyang 184460a5a19eSTang Chen try_offline_node(nid); 184560a5a19eSTang Chen 1846eca499abSPavel Tatashin done: 1847bfc8c901SVladimir Davydov mem_hotplug_done(); 1848eca499abSPavel Tatashin return rc; 184971088785SBadari Pulavarty } 1850d15e5926SDavid Hildenbrand 1851eca499abSPavel Tatashin /** 1852eca499abSPavel Tatashin * remove_memory 1853eca499abSPavel Tatashin * @nid: the node ID 1854eca499abSPavel Tatashin * @start: physical address of the region to remove 1855eca499abSPavel Tatashin * @size: size of the region to remove 1856eca499abSPavel Tatashin * 1857eca499abSPavel Tatashin * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 1858eca499abSPavel Tatashin * and online/offline operations before this call, as required by 1859eca499abSPavel Tatashin * try_offline_node(). 1860eca499abSPavel Tatashin */ 1861eca499abSPavel Tatashin void __remove_memory(int nid, u64 start, u64 size) 1862d15e5926SDavid Hildenbrand { 1863eca499abSPavel Tatashin 1864eca499abSPavel Tatashin /* 1865eca499abSPavel Tatashin * trigger BUG() is some memory is not offlined prior to calling this 1866eca499abSPavel Tatashin * function 1867eca499abSPavel Tatashin */ 1868eca499abSPavel Tatashin if (try_remove_memory(nid, start, size)) 1869eca499abSPavel Tatashin BUG(); 1870eca499abSPavel Tatashin } 1871eca499abSPavel Tatashin 1872eca499abSPavel Tatashin /* 1873eca499abSPavel Tatashin * Remove memory if every memory block is offline, otherwise return -EBUSY is 1874eca499abSPavel Tatashin * some memory is not offline 1875eca499abSPavel Tatashin */ 1876eca499abSPavel Tatashin int remove_memory(int nid, u64 start, u64 size) 1877eca499abSPavel Tatashin { 1878eca499abSPavel Tatashin int rc; 1879eca499abSPavel Tatashin 1880d15e5926SDavid Hildenbrand lock_device_hotplug(); 1881eca499abSPavel Tatashin rc = try_remove_memory(nid, start, size); 1882d15e5926SDavid Hildenbrand unlock_device_hotplug(); 1883eca499abSPavel Tatashin 1884eca499abSPavel Tatashin return rc; 1885d15e5926SDavid Hildenbrand } 188671088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 1887aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 1888