13947be19SDave Hansen /* 23947be19SDave Hansen * linux/mm/memory_hotplug.c 33947be19SDave Hansen * 43947be19SDave Hansen * Copyright (C) 53947be19SDave Hansen */ 63947be19SDave Hansen 73947be19SDave Hansen #include <linux/stddef.h> 83947be19SDave Hansen #include <linux/mm.h> 9174cd4b1SIngo Molnar #include <linux/sched/signal.h> 103947be19SDave Hansen #include <linux/swap.h> 113947be19SDave Hansen #include <linux/interrupt.h> 123947be19SDave Hansen #include <linux/pagemap.h> 133947be19SDave Hansen #include <linux/compiler.h> 14b95f1b31SPaul Gortmaker #include <linux/export.h> 153947be19SDave Hansen #include <linux/pagevec.h> 162d1d43f6SChandra Seetharaman #include <linux/writeback.h> 173947be19SDave Hansen #include <linux/slab.h> 183947be19SDave Hansen #include <linux/sysctl.h> 193947be19SDave Hansen #include <linux/cpu.h> 203947be19SDave Hansen #include <linux/memory.h> 214b94ffdcSDan Williams #include <linux/memremap.h> 223947be19SDave Hansen #include <linux/memory_hotplug.h> 233947be19SDave Hansen #include <linux/highmem.h> 243947be19SDave Hansen #include <linux/vmalloc.h> 250a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h> 260c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h> 270c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h> 280c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h> 2971088785SBadari Pulavarty #include <linux/pfn.h> 306ad696d2SAndi Kleen #include <linux/suspend.h> 316d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 32d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h> 3360a5a19eSTang Chen #include <linux/stop_machine.h> 34c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h> 35c5320926STang Chen #include <linux/memblock.h> 36698b1b30SVlastimil Babka #include <linux/compaction.h> 373947be19SDave Hansen 383947be19SDave Hansen #include <asm/tlbflush.h> 393947be19SDave Hansen 401e5ad9a3SAdrian Bunk #include "internal.h" 411e5ad9a3SAdrian Bunk 429d0ad8caSDaniel Kiper /* 439d0ad8caSDaniel Kiper * online_page_callback contains pointer to current page onlining function. 449d0ad8caSDaniel Kiper * Initially it is generic_online_page(). If it is required it could be 459d0ad8caSDaniel Kiper * changed by calling set_online_page_callback() for callback registration 469d0ad8caSDaniel Kiper * and restore_online_page_callback() for generic callback restore. 479d0ad8caSDaniel Kiper */ 489d0ad8caSDaniel Kiper 499d0ad8caSDaniel Kiper static void generic_online_page(struct page *page); 509d0ad8caSDaniel Kiper 519d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page; 52bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock); 539d0ad8caSDaniel Kiper 543f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 5520d6c96bSKOSAKI Motohiro 563f906ba2SThomas Gleixner void get_online_mems(void) 573f906ba2SThomas Gleixner { 583f906ba2SThomas Gleixner percpu_down_read(&mem_hotplug_lock); 593f906ba2SThomas Gleixner } 60bfc8c901SVladimir Davydov 613f906ba2SThomas Gleixner void put_online_mems(void) 623f906ba2SThomas Gleixner { 633f906ba2SThomas Gleixner percpu_up_read(&mem_hotplug_lock); 643f906ba2SThomas Gleixner } 65bfc8c901SVladimir Davydov 664932381eSMichal Hocko bool movable_node_enabled = false; 674932381eSMichal Hocko 688604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 6931bc3858SVitaly Kuznetsov bool memhp_auto_online; 708604d9e5SVitaly Kuznetsov #else 718604d9e5SVitaly Kuznetsov bool memhp_auto_online = true; 728604d9e5SVitaly Kuznetsov #endif 7331bc3858SVitaly Kuznetsov EXPORT_SYMBOL_GPL(memhp_auto_online); 7431bc3858SVitaly Kuznetsov 7586dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str) 7686dd995dSVitaly Kuznetsov { 7786dd995dSVitaly Kuznetsov if (!strcmp(str, "online")) 7886dd995dSVitaly Kuznetsov memhp_auto_online = true; 7986dd995dSVitaly Kuznetsov else if (!strcmp(str, "offline")) 8086dd995dSVitaly Kuznetsov memhp_auto_online = false; 8186dd995dSVitaly Kuznetsov 8286dd995dSVitaly Kuznetsov return 1; 8386dd995dSVitaly Kuznetsov } 8486dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state); 8586dd995dSVitaly Kuznetsov 8630467e0bSDavid Rientjes void mem_hotplug_begin(void) 87bfc8c901SVladimir Davydov { 883f906ba2SThomas Gleixner cpus_read_lock(); 893f906ba2SThomas Gleixner percpu_down_write(&mem_hotplug_lock); 90bfc8c901SVladimir Davydov } 91bfc8c901SVladimir Davydov 9230467e0bSDavid Rientjes void mem_hotplug_done(void) 93bfc8c901SVladimir Davydov { 943f906ba2SThomas Gleixner percpu_up_write(&mem_hotplug_lock); 953f906ba2SThomas Gleixner cpus_read_unlock(); 96bfc8c901SVladimir Davydov } 9720d6c96bSKOSAKI Motohiro 9845e0b78bSKeith Mannthey /* add this memory to iomem resource */ 9945e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size) 10045e0b78bSKeith Mannthey { 1015042db43SJérôme Glisse struct resource *res, *conflict; 10245e0b78bSKeith Mannthey res = kzalloc(sizeof(struct resource), GFP_KERNEL); 1036f754ba4SVitaly Kuznetsov if (!res) 1046f754ba4SVitaly Kuznetsov return ERR_PTR(-ENOMEM); 10545e0b78bSKeith Mannthey 10645e0b78bSKeith Mannthey res->name = "System RAM"; 10745e0b78bSKeith Mannthey res->start = start; 10845e0b78bSKeith Mannthey res->end = start + size - 1; 109782b8664SToshi Kani res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1105042db43SJérôme Glisse conflict = request_resource_conflict(&iomem_resource, res); 1115042db43SJérôme Glisse if (conflict) { 1125042db43SJérôme Glisse if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) { 1135042db43SJérôme Glisse pr_debug("Device unaddressable memory block " 1145042db43SJérôme Glisse "memory hotplug at %#010llx !\n", 1155042db43SJérôme Glisse (unsigned long long)start); 1165042db43SJérôme Glisse } 1174996eed8SToshi Kani pr_debug("System RAM resource %pR cannot be added\n", res); 11845e0b78bSKeith Mannthey kfree(res); 1196f754ba4SVitaly Kuznetsov return ERR_PTR(-EEXIST); 12045e0b78bSKeith Mannthey } 12145e0b78bSKeith Mannthey return res; 12245e0b78bSKeith Mannthey } 12345e0b78bSKeith Mannthey 12445e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 12545e0b78bSKeith Mannthey { 12645e0b78bSKeith Mannthey if (!res) 12745e0b78bSKeith Mannthey return; 12845e0b78bSKeith Mannthey release_resource(res); 12945e0b78bSKeith Mannthey kfree(res); 13045e0b78bSKeith Mannthey return; 13145e0b78bSKeith Mannthey } 13245e0b78bSKeith Mannthey 13353947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 13446723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info, struct page *page, 1355f24ce5fSAndrea Arcangeli unsigned long type) 13604753278SYasunori Goto { 137ddffe98dSYasuaki Ishimatsu page->freelist = (void *)type; 13804753278SYasunori Goto SetPagePrivate(page); 13904753278SYasunori Goto set_page_private(page, info); 140fe896d18SJoonsoo Kim page_ref_inc(page); 14104753278SYasunori Goto } 14204753278SYasunori Goto 143170a5a7eSJiang Liu void put_page_bootmem(struct page *page) 14404753278SYasunori Goto { 1455f24ce5fSAndrea Arcangeli unsigned long type; 14604753278SYasunori Goto 147ddffe98dSYasuaki Ishimatsu type = (unsigned long) page->freelist; 1485f24ce5fSAndrea Arcangeli BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 1495f24ce5fSAndrea Arcangeli type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 15004753278SYasunori Goto 151fe896d18SJoonsoo Kim if (page_ref_dec_return(page) == 1) { 152ddffe98dSYasuaki Ishimatsu page->freelist = NULL; 15304753278SYasunori Goto ClearPagePrivate(page); 15404753278SYasunori Goto set_page_private(page, 0); 1555f24ce5fSAndrea Arcangeli INIT_LIST_HEAD(&page->lru); 156170a5a7eSJiang Liu free_reserved_page(page); 15704753278SYasunori Goto } 15804753278SYasunori Goto } 15904753278SYasunori Goto 16046723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 16146723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP 162d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn) 16304753278SYasunori Goto { 16404753278SYasunori Goto unsigned long *usemap, mapsize, section_nr, i; 16504753278SYasunori Goto struct mem_section *ms; 16604753278SYasunori Goto struct page *page, *memmap; 16704753278SYasunori Goto 16804753278SYasunori Goto section_nr = pfn_to_section_nr(start_pfn); 16904753278SYasunori Goto ms = __nr_to_section(section_nr); 17004753278SYasunori Goto 17104753278SYasunori Goto /* Get section's memmap address */ 17204753278SYasunori Goto memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 17304753278SYasunori Goto 17404753278SYasunori Goto /* 17504753278SYasunori Goto * Get page for the memmap's phys address 17604753278SYasunori Goto * XXX: need more consideration for sparse_vmemmap... 17704753278SYasunori Goto */ 17804753278SYasunori Goto page = virt_to_page(memmap); 17904753278SYasunori Goto mapsize = sizeof(struct page) * PAGES_PER_SECTION; 18004753278SYasunori Goto mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 18104753278SYasunori Goto 18204753278SYasunori Goto /* remember memmap's page */ 18304753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 18404753278SYasunori Goto get_page_bootmem(section_nr, page, SECTION_INFO); 18504753278SYasunori Goto 1869ac9322dSOscar Salvador usemap = ms->pageblock_flags; 18704753278SYasunori Goto page = virt_to_page(usemap); 18804753278SYasunori Goto 18904753278SYasunori Goto mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 19004753278SYasunori Goto 19104753278SYasunori Goto for (i = 0; i < mapsize; i++, page++) 192af370fb8SYasunori Goto get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 19304753278SYasunori Goto 19404753278SYasunori Goto } 19546723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */ 19646723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn) 19746723bfaSYasuaki Ishimatsu { 19846723bfaSYasuaki Ishimatsu unsigned long *usemap, mapsize, section_nr, i; 19946723bfaSYasuaki Ishimatsu struct mem_section *ms; 20046723bfaSYasuaki Ishimatsu struct page *page, *memmap; 20146723bfaSYasuaki Ishimatsu 20246723bfaSYasuaki Ishimatsu section_nr = pfn_to_section_nr(start_pfn); 20346723bfaSYasuaki Ishimatsu ms = __nr_to_section(section_nr); 20446723bfaSYasuaki Ishimatsu 20546723bfaSYasuaki Ishimatsu memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 20646723bfaSYasuaki Ishimatsu 20746723bfaSYasuaki Ishimatsu register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); 20846723bfaSYasuaki Ishimatsu 2099ac9322dSOscar Salvador usemap = ms->pageblock_flags; 21046723bfaSYasuaki Ishimatsu page = virt_to_page(usemap); 21146723bfaSYasuaki Ishimatsu 21246723bfaSYasuaki Ishimatsu mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; 21346723bfaSYasuaki Ishimatsu 21446723bfaSYasuaki Ishimatsu for (i = 0; i < mapsize; i++, page++) 21546723bfaSYasuaki Ishimatsu get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 21646723bfaSYasuaki Ishimatsu } 21746723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 21804753278SYasunori Goto 2197ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat) 22004753278SYasunori Goto { 22104753278SYasunori Goto unsigned long i, pfn, end_pfn, nr_pages; 22204753278SYasunori Goto int node = pgdat->node_id; 22304753278SYasunori Goto struct page *page; 22404753278SYasunori Goto 22504753278SYasunori Goto nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 22604753278SYasunori Goto page = virt_to_page(pgdat); 22704753278SYasunori Goto 22804753278SYasunori Goto for (i = 0; i < nr_pages; i++, page++) 22904753278SYasunori Goto get_page_bootmem(node, page, NODE_INFO); 23004753278SYasunori Goto 23104753278SYasunori Goto pfn = pgdat->node_start_pfn; 232c1f19495SCody P Schafer end_pfn = pgdat_end_pfn(pgdat); 23304753278SYasunori Goto 2347e9f5eb0STang Chen /* register section info */ 235f14851afSqiuxishi for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 236f14851afSqiuxishi /* 237f14851afSqiuxishi * Some platforms can assign the same pfn to multiple nodes - on 238f14851afSqiuxishi * node0 as well as nodeN. To avoid registering a pfn against 239f14851afSqiuxishi * multiple nodes we check that this pfn does not already 2407e9f5eb0STang Chen * reside in some other nodes. 241f14851afSqiuxishi */ 242f65e91dfSYang Shi if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) 24304753278SYasunori Goto register_page_bootmem_info_section(pfn); 244f14851afSqiuxishi } 24504753278SYasunori Goto } 24646723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ 24704753278SYasunori Goto 248f1dd2cd1SMichal Hocko static int __meminit __add_section(int nid, unsigned long phys_start_pfn, 2497b73d978SChristoph Hellwig struct vmem_altmap *altmap, bool want_memblock) 2503947be19SDave Hansen { 2513947be19SDave Hansen int ret; 2523947be19SDave Hansen 253ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 254ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 255ebd15302SKAMEZAWA Hiroyuki 2567b73d978SChristoph Hellwig ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn, altmap); 2573947be19SDave Hansen if (ret < 0) 2583947be19SDave Hansen return ret; 2593947be19SDave Hansen 2601b862aecSMichal Hocko if (!want_memblock) 2611b862aecSMichal Hocko return 0; 2621b862aecSMichal Hocko 263fc44f7f9SPavel Tatashin return hotplug_memory_register(nid, __pfn_to_section(phys_start_pfn)); 2643947be19SDave Hansen } 2653947be19SDave Hansen 2664edd7cefSDavid Rientjes /* 2674edd7cefSDavid Rientjes * Reasonably generic function for adding memory. It is 2684edd7cefSDavid Rientjes * expected that archs that support memory hotplug will 2694edd7cefSDavid Rientjes * call this function after deciding the zone to which to 2704edd7cefSDavid Rientjes * add the new pages. 2714edd7cefSDavid Rientjes */ 272f1dd2cd1SMichal Hocko int __ref __add_pages(int nid, unsigned long phys_start_pfn, 27324e6d5a5SChristoph Hellwig unsigned long nr_pages, struct vmem_altmap *altmap, 27424e6d5a5SChristoph Hellwig bool want_memblock) 2754edd7cefSDavid Rientjes { 2764edd7cefSDavid Rientjes unsigned long i; 2774edd7cefSDavid Rientjes int err = 0; 2784edd7cefSDavid Rientjes int start_sec, end_sec; 2794b94ffdcSDan Williams 2804edd7cefSDavid Rientjes /* during initialize mem_map, align hot-added range to section */ 2814edd7cefSDavid Rientjes start_sec = pfn_to_section_nr(phys_start_pfn); 2824edd7cefSDavid Rientjes end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 2834edd7cefSDavid Rientjes 2844b94ffdcSDan Williams if (altmap) { 2854b94ffdcSDan Williams /* 2864b94ffdcSDan Williams * Validate altmap is within bounds of the total request 2874b94ffdcSDan Williams */ 2884b94ffdcSDan Williams if (altmap->base_pfn != phys_start_pfn 2894b94ffdcSDan Williams || vmem_altmap_offset(altmap) > nr_pages) { 2904b94ffdcSDan Williams pr_warn_once("memory add fail, invalid altmap\n"); 2917cf91a98SJoonsoo Kim err = -EINVAL; 2927cf91a98SJoonsoo Kim goto out; 2934b94ffdcSDan Williams } 2944b94ffdcSDan Williams altmap->alloc = 0; 2954b94ffdcSDan Williams } 2964b94ffdcSDan Williams 2974edd7cefSDavid Rientjes for (i = start_sec; i <= end_sec; i++) { 2987b73d978SChristoph Hellwig err = __add_section(nid, section_nr_to_pfn(i), altmap, 2997b73d978SChristoph Hellwig want_memblock); 3004edd7cefSDavid Rientjes 3014edd7cefSDavid Rientjes /* 3024edd7cefSDavid Rientjes * EEXIST is finally dealt with by ioresource collision 3034edd7cefSDavid Rientjes * check. see add_memory() => register_memory_resource() 3044edd7cefSDavid Rientjes * Warning will be printed if there is collision. 3054edd7cefSDavid Rientjes */ 3064edd7cefSDavid Rientjes if (err && (err != -EEXIST)) 3074edd7cefSDavid Rientjes break; 3084edd7cefSDavid Rientjes err = 0; 309f64ac5e6SMichal Hocko cond_resched(); 3104edd7cefSDavid Rientjes } 311c435a390SZhu Guihua vmemmap_populate_print_last(); 3127cf91a98SJoonsoo Kim out: 3134edd7cefSDavid Rientjes return err; 3144edd7cefSDavid Rientjes } 3154edd7cefSDavid Rientjes 3164edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE 317815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 318d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, 319815121d2SYasuaki Ishimatsu unsigned long start_pfn, 320815121d2SYasuaki Ishimatsu unsigned long end_pfn) 321815121d2SYasuaki Ishimatsu { 322815121d2SYasuaki Ishimatsu struct mem_section *ms; 323815121d2SYasuaki Ishimatsu 324815121d2SYasuaki Ishimatsu for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) { 325815121d2SYasuaki Ishimatsu ms = __pfn_to_section(start_pfn); 326815121d2SYasuaki Ishimatsu 327815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 328815121d2SYasuaki Ishimatsu continue; 329815121d2SYasuaki Ishimatsu 330815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(start_pfn) != nid)) 331815121d2SYasuaki Ishimatsu continue; 332815121d2SYasuaki Ishimatsu 333815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(start_pfn))) 334815121d2SYasuaki Ishimatsu continue; 335815121d2SYasuaki Ishimatsu 336815121d2SYasuaki Ishimatsu return start_pfn; 337815121d2SYasuaki Ishimatsu } 338815121d2SYasuaki Ishimatsu 339815121d2SYasuaki Ishimatsu return 0; 340815121d2SYasuaki Ishimatsu } 341815121d2SYasuaki Ishimatsu 342815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 343d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, 344815121d2SYasuaki Ishimatsu unsigned long start_pfn, 345815121d2SYasuaki Ishimatsu unsigned long end_pfn) 346815121d2SYasuaki Ishimatsu { 347815121d2SYasuaki Ishimatsu struct mem_section *ms; 348815121d2SYasuaki Ishimatsu unsigned long pfn; 349815121d2SYasuaki Ishimatsu 350815121d2SYasuaki Ishimatsu /* pfn is the end pfn of a memory section. */ 351815121d2SYasuaki Ishimatsu pfn = end_pfn - 1; 352815121d2SYasuaki Ishimatsu for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) { 353815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 354815121d2SYasuaki Ishimatsu 355815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 356815121d2SYasuaki Ishimatsu continue; 357815121d2SYasuaki Ishimatsu 358815121d2SYasuaki Ishimatsu if (unlikely(pfn_to_nid(pfn) != nid)) 359815121d2SYasuaki Ishimatsu continue; 360815121d2SYasuaki Ishimatsu 361815121d2SYasuaki Ishimatsu if (zone && zone != page_zone(pfn_to_page(pfn))) 362815121d2SYasuaki Ishimatsu continue; 363815121d2SYasuaki Ishimatsu 364815121d2SYasuaki Ishimatsu return pfn; 365815121d2SYasuaki Ishimatsu } 366815121d2SYasuaki Ishimatsu 367815121d2SYasuaki Ishimatsu return 0; 368815121d2SYasuaki Ishimatsu } 369815121d2SYasuaki Ishimatsu 370815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 371815121d2SYasuaki Ishimatsu unsigned long end_pfn) 372815121d2SYasuaki Ishimatsu { 373815121d2SYasuaki Ishimatsu unsigned long zone_start_pfn = zone->zone_start_pfn; 374c33bc315SXishi Qiu unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ 375c33bc315SXishi Qiu unsigned long zone_end_pfn = z; 376815121d2SYasuaki Ishimatsu unsigned long pfn; 377815121d2SYasuaki Ishimatsu struct mem_section *ms; 378815121d2SYasuaki Ishimatsu int nid = zone_to_nid(zone); 379815121d2SYasuaki Ishimatsu 380815121d2SYasuaki Ishimatsu zone_span_writelock(zone); 381815121d2SYasuaki Ishimatsu if (zone_start_pfn == start_pfn) { 382815121d2SYasuaki Ishimatsu /* 383815121d2SYasuaki Ishimatsu * If the section is smallest section in the zone, it need 384815121d2SYasuaki Ishimatsu * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 385815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 386815121d2SYasuaki Ishimatsu * for shrinking zone. 387815121d2SYasuaki Ishimatsu */ 388815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, zone, end_pfn, 389815121d2SYasuaki Ishimatsu zone_end_pfn); 390815121d2SYasuaki Ishimatsu if (pfn) { 391815121d2SYasuaki Ishimatsu zone->zone_start_pfn = pfn; 392815121d2SYasuaki Ishimatsu zone->spanned_pages = zone_end_pfn - pfn; 393815121d2SYasuaki Ishimatsu } 394815121d2SYasuaki Ishimatsu } else if (zone_end_pfn == end_pfn) { 395815121d2SYasuaki Ishimatsu /* 396815121d2SYasuaki Ishimatsu * If the section is biggest section in the zone, it need 397815121d2SYasuaki Ishimatsu * shrink zone->spanned_pages. 398815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 399815121d2SYasuaki Ishimatsu * shrinking zone. 400815121d2SYasuaki Ishimatsu */ 401815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, 402815121d2SYasuaki Ishimatsu start_pfn); 403815121d2SYasuaki Ishimatsu if (pfn) 404815121d2SYasuaki Ishimatsu zone->spanned_pages = pfn - zone_start_pfn + 1; 405815121d2SYasuaki Ishimatsu } 406815121d2SYasuaki Ishimatsu 407815121d2SYasuaki Ishimatsu /* 408815121d2SYasuaki Ishimatsu * The section is not biggest or smallest mem_section in the zone, it 409815121d2SYasuaki Ishimatsu * only creates a hole in the zone. So in this case, we need not 410815121d2SYasuaki Ishimatsu * change the zone. But perhaps, the zone has only hole data. Thus 411815121d2SYasuaki Ishimatsu * it check the zone has only hole or not. 412815121d2SYasuaki Ishimatsu */ 413815121d2SYasuaki Ishimatsu pfn = zone_start_pfn; 414815121d2SYasuaki Ishimatsu for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) { 415815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 416815121d2SYasuaki Ishimatsu 417815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 418815121d2SYasuaki Ishimatsu continue; 419815121d2SYasuaki Ishimatsu 420815121d2SYasuaki Ishimatsu if (page_zone(pfn_to_page(pfn)) != zone) 421815121d2SYasuaki Ishimatsu continue; 422815121d2SYasuaki Ishimatsu 423815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 424815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 425815121d2SYasuaki Ishimatsu continue; 426815121d2SYasuaki Ishimatsu 427815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 428815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 429815121d2SYasuaki Ishimatsu return; 430815121d2SYasuaki Ishimatsu } 431815121d2SYasuaki Ishimatsu 432815121d2SYasuaki Ishimatsu /* The zone has no valid section */ 433815121d2SYasuaki Ishimatsu zone->zone_start_pfn = 0; 434815121d2SYasuaki Ishimatsu zone->spanned_pages = 0; 435815121d2SYasuaki Ishimatsu zone_span_writeunlock(zone); 436815121d2SYasuaki Ishimatsu } 437815121d2SYasuaki Ishimatsu 438815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat, 439815121d2SYasuaki Ishimatsu unsigned long start_pfn, unsigned long end_pfn) 440815121d2SYasuaki Ishimatsu { 441815121d2SYasuaki Ishimatsu unsigned long pgdat_start_pfn = pgdat->node_start_pfn; 44283285c72SXishi Qiu unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */ 44383285c72SXishi Qiu unsigned long pgdat_end_pfn = p; 444815121d2SYasuaki Ishimatsu unsigned long pfn; 445815121d2SYasuaki Ishimatsu struct mem_section *ms; 446815121d2SYasuaki Ishimatsu int nid = pgdat->node_id; 447815121d2SYasuaki Ishimatsu 448815121d2SYasuaki Ishimatsu if (pgdat_start_pfn == start_pfn) { 449815121d2SYasuaki Ishimatsu /* 450815121d2SYasuaki Ishimatsu * If the section is smallest section in the pgdat, it need 451815121d2SYasuaki Ishimatsu * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages. 452815121d2SYasuaki Ishimatsu * In this case, we find second smallest valid mem_section 453815121d2SYasuaki Ishimatsu * for shrinking zone. 454815121d2SYasuaki Ishimatsu */ 455815121d2SYasuaki Ishimatsu pfn = find_smallest_section_pfn(nid, NULL, end_pfn, 456815121d2SYasuaki Ishimatsu pgdat_end_pfn); 457815121d2SYasuaki Ishimatsu if (pfn) { 458815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = pfn; 459815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pgdat_end_pfn - pfn; 460815121d2SYasuaki Ishimatsu } 461815121d2SYasuaki Ishimatsu } else if (pgdat_end_pfn == end_pfn) { 462815121d2SYasuaki Ishimatsu /* 463815121d2SYasuaki Ishimatsu * If the section is biggest section in the pgdat, it need 464815121d2SYasuaki Ishimatsu * shrink pgdat->node_spanned_pages. 465815121d2SYasuaki Ishimatsu * In this case, we find second biggest valid mem_section for 466815121d2SYasuaki Ishimatsu * shrinking zone. 467815121d2SYasuaki Ishimatsu */ 468815121d2SYasuaki Ishimatsu pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn, 469815121d2SYasuaki Ishimatsu start_pfn); 470815121d2SYasuaki Ishimatsu if (pfn) 471815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1; 472815121d2SYasuaki Ishimatsu } 473815121d2SYasuaki Ishimatsu 474815121d2SYasuaki Ishimatsu /* 475815121d2SYasuaki Ishimatsu * If the section is not biggest or smallest mem_section in the pgdat, 476815121d2SYasuaki Ishimatsu * it only creates a hole in the pgdat. So in this case, we need not 477815121d2SYasuaki Ishimatsu * change the pgdat. 478815121d2SYasuaki Ishimatsu * But perhaps, the pgdat has only hole data. Thus it check the pgdat 479815121d2SYasuaki Ishimatsu * has only hole or not. 480815121d2SYasuaki Ishimatsu */ 481815121d2SYasuaki Ishimatsu pfn = pgdat_start_pfn; 482815121d2SYasuaki Ishimatsu for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) { 483815121d2SYasuaki Ishimatsu ms = __pfn_to_section(pfn); 484815121d2SYasuaki Ishimatsu 485815121d2SYasuaki Ishimatsu if (unlikely(!valid_section(ms))) 486815121d2SYasuaki Ishimatsu continue; 487815121d2SYasuaki Ishimatsu 488815121d2SYasuaki Ishimatsu if (pfn_to_nid(pfn) != nid) 489815121d2SYasuaki Ishimatsu continue; 490815121d2SYasuaki Ishimatsu 491815121d2SYasuaki Ishimatsu /* If the section is current section, it continues the loop */ 492815121d2SYasuaki Ishimatsu if (start_pfn == pfn) 493815121d2SYasuaki Ishimatsu continue; 494815121d2SYasuaki Ishimatsu 495815121d2SYasuaki Ishimatsu /* If we find valid section, we have nothing to do */ 496815121d2SYasuaki Ishimatsu return; 497815121d2SYasuaki Ishimatsu } 498815121d2SYasuaki Ishimatsu 499815121d2SYasuaki Ishimatsu /* The pgdat has no valid section */ 500815121d2SYasuaki Ishimatsu pgdat->node_start_pfn = 0; 501815121d2SYasuaki Ishimatsu pgdat->node_spanned_pages = 0; 502815121d2SYasuaki Ishimatsu } 503815121d2SYasuaki Ishimatsu 504815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn) 505815121d2SYasuaki Ishimatsu { 506815121d2SYasuaki Ishimatsu struct pglist_data *pgdat = zone->zone_pgdat; 507815121d2SYasuaki Ishimatsu int nr_pages = PAGES_PER_SECTION; 508815121d2SYasuaki Ishimatsu unsigned long flags; 509815121d2SYasuaki Ishimatsu 510815121d2SYasuaki Ishimatsu pgdat_resize_lock(zone->zone_pgdat, &flags); 511815121d2SYasuaki Ishimatsu shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 512815121d2SYasuaki Ishimatsu shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages); 513815121d2SYasuaki Ishimatsu pgdat_resize_unlock(zone->zone_pgdat, &flags); 514815121d2SYasuaki Ishimatsu } 515815121d2SYasuaki Ishimatsu 5164b94ffdcSDan Williams static int __remove_section(struct zone *zone, struct mem_section *ms, 51724b6d416SChristoph Hellwig unsigned long map_offset, struct vmem_altmap *altmap) 518ea01ea93SBadari Pulavarty { 519815121d2SYasuaki Ishimatsu unsigned long start_pfn; 520815121d2SYasuaki Ishimatsu int scn_nr; 521ea01ea93SBadari Pulavarty int ret = -EINVAL; 522ea01ea93SBadari Pulavarty 523ea01ea93SBadari Pulavarty if (!valid_section(ms)) 524ea01ea93SBadari Pulavarty return ret; 525ea01ea93SBadari Pulavarty 526ea01ea93SBadari Pulavarty ret = unregister_memory_section(ms); 527ea01ea93SBadari Pulavarty if (ret) 528ea01ea93SBadari Pulavarty return ret; 529ea01ea93SBadari Pulavarty 530815121d2SYasuaki Ishimatsu scn_nr = __section_nr(ms); 5311dd2bfc8SYASUAKI ISHIMATSU start_pfn = section_nr_to_pfn((unsigned long)scn_nr); 532815121d2SYasuaki Ishimatsu __remove_zone(zone, start_pfn); 533815121d2SYasuaki Ishimatsu 53424b6d416SChristoph Hellwig sparse_remove_one_section(zone, ms, map_offset, altmap); 535ea01ea93SBadari Pulavarty return 0; 536ea01ea93SBadari Pulavarty } 537ea01ea93SBadari Pulavarty 538ea01ea93SBadari Pulavarty /** 539ea01ea93SBadari Pulavarty * __remove_pages() - remove sections of pages from a zone 540ea01ea93SBadari Pulavarty * @zone: zone from which pages need to be removed 541ea01ea93SBadari Pulavarty * @phys_start_pfn: starting pageframe (must be aligned to start of a section) 542ea01ea93SBadari Pulavarty * @nr_pages: number of pages to remove (must be multiple of section size) 543e8b098fcSMike Rapoport * @altmap: alternative device page map or %NULL if default memmap is used 544ea01ea93SBadari Pulavarty * 545ea01ea93SBadari Pulavarty * Generic helper function to remove section mappings and sysfs entries 546ea01ea93SBadari Pulavarty * for the section of the memory we are removing. Caller needs to make 547ea01ea93SBadari Pulavarty * sure that pages are marked reserved and zones are adjust properly by 548ea01ea93SBadari Pulavarty * calling offline_pages(). 549ea01ea93SBadari Pulavarty */ 550ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, 551da024512SChristoph Hellwig unsigned long nr_pages, struct vmem_altmap *altmap) 552ea01ea93SBadari Pulavarty { 553fe74ebb1SToshi Kani unsigned long i; 5544b94ffdcSDan Williams unsigned long map_offset = 0; 5554b94ffdcSDan Williams int sections_to_remove, ret = 0; 5564b94ffdcSDan Williams 5574b94ffdcSDan Williams /* In the ZONE_DEVICE case device driver owns the memory region */ 5584b94ffdcSDan Williams if (is_dev_zone(zone)) { 5594b94ffdcSDan Williams if (altmap) 5604b94ffdcSDan Williams map_offset = vmem_altmap_offset(altmap); 5614b94ffdcSDan Williams } else { 562fe74ebb1SToshi Kani resource_size_t start, size; 5634b94ffdcSDan Williams 5644b94ffdcSDan Williams start = phys_start_pfn << PAGE_SHIFT; 5654b94ffdcSDan Williams size = nr_pages * PAGE_SIZE; 5664b94ffdcSDan Williams 5674b94ffdcSDan Williams ret = release_mem_region_adjustable(&iomem_resource, start, 5684b94ffdcSDan Williams size); 5694b94ffdcSDan Williams if (ret) { 5704b94ffdcSDan Williams resource_size_t endres = start + size - 1; 5714b94ffdcSDan Williams 5724b94ffdcSDan Williams pr_warn("Unable to release resource <%pa-%pa> (%d)\n", 5734b94ffdcSDan Williams &start, &endres, ret); 5744b94ffdcSDan Williams } 5754b94ffdcSDan Williams } 576ea01ea93SBadari Pulavarty 5777cf91a98SJoonsoo Kim clear_zone_contiguous(zone); 5787cf91a98SJoonsoo Kim 579ea01ea93SBadari Pulavarty /* 580ea01ea93SBadari Pulavarty * We can only remove entire sections 581ea01ea93SBadari Pulavarty */ 582ea01ea93SBadari Pulavarty BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); 583ea01ea93SBadari Pulavarty BUG_ON(nr_pages % PAGES_PER_SECTION); 584ea01ea93SBadari Pulavarty 585ea01ea93SBadari Pulavarty sections_to_remove = nr_pages / PAGES_PER_SECTION; 586ea01ea93SBadari Pulavarty for (i = 0; i < sections_to_remove; i++) { 587ea01ea93SBadari Pulavarty unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; 5884b94ffdcSDan Williams 589dd33ad7bSMichal Hocko cond_resched(); 59024b6d416SChristoph Hellwig ret = __remove_section(zone, __pfn_to_section(pfn), map_offset, 59124b6d416SChristoph Hellwig altmap); 5924b94ffdcSDan Williams map_offset = 0; 593ea01ea93SBadari Pulavarty if (ret) 594ea01ea93SBadari Pulavarty break; 595ea01ea93SBadari Pulavarty } 5967cf91a98SJoonsoo Kim 5977cf91a98SJoonsoo Kim set_zone_contiguous(zone); 5987cf91a98SJoonsoo Kim 599ea01ea93SBadari Pulavarty return ret; 600ea01ea93SBadari Pulavarty } 6014edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */ 602ea01ea93SBadari Pulavarty 6039d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback) 6049d0ad8caSDaniel Kiper { 6059d0ad8caSDaniel Kiper int rc = -EINVAL; 6069d0ad8caSDaniel Kiper 607bfc8c901SVladimir Davydov get_online_mems(); 608bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 6099d0ad8caSDaniel Kiper 6109d0ad8caSDaniel Kiper if (online_page_callback == generic_online_page) { 6119d0ad8caSDaniel Kiper online_page_callback = callback; 6129d0ad8caSDaniel Kiper rc = 0; 6139d0ad8caSDaniel Kiper } 6149d0ad8caSDaniel Kiper 615bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 616bfc8c901SVladimir Davydov put_online_mems(); 6179d0ad8caSDaniel Kiper 6189d0ad8caSDaniel Kiper return rc; 6199d0ad8caSDaniel Kiper } 6209d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback); 6219d0ad8caSDaniel Kiper 6229d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback) 6239d0ad8caSDaniel Kiper { 6249d0ad8caSDaniel Kiper int rc = -EINVAL; 6259d0ad8caSDaniel Kiper 626bfc8c901SVladimir Davydov get_online_mems(); 627bfc8c901SVladimir Davydov mutex_lock(&online_page_callback_lock); 6289d0ad8caSDaniel Kiper 6299d0ad8caSDaniel Kiper if (online_page_callback == callback) { 6309d0ad8caSDaniel Kiper online_page_callback = generic_online_page; 6319d0ad8caSDaniel Kiper rc = 0; 6329d0ad8caSDaniel Kiper } 6339d0ad8caSDaniel Kiper 634bfc8c901SVladimir Davydov mutex_unlock(&online_page_callback_lock); 635bfc8c901SVladimir Davydov put_online_mems(); 6369d0ad8caSDaniel Kiper 6379d0ad8caSDaniel Kiper return rc; 6389d0ad8caSDaniel Kiper } 6399d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback); 6409d0ad8caSDaniel Kiper 6419d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page) 642180c06efSJeremy Fitzhardinge { 6439d0ad8caSDaniel Kiper } 6449d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits); 6459d0ad8caSDaniel Kiper 6469d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page) 6479d0ad8caSDaniel Kiper { 6483dcc0571SJiang Liu adjust_managed_page_count(page, 1); 6499d0ad8caSDaniel Kiper } 6509d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters); 651180c06efSJeremy Fitzhardinge 6529d0ad8caSDaniel Kiper void __online_page_free(struct page *page) 6539d0ad8caSDaniel Kiper { 6543dcc0571SJiang Liu __free_reserved_page(page); 655180c06efSJeremy Fitzhardinge } 6569d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free); 6579d0ad8caSDaniel Kiper 6589d0ad8caSDaniel Kiper static void generic_online_page(struct page *page) 6599d0ad8caSDaniel Kiper { 6609d0ad8caSDaniel Kiper __online_page_set_limits(page); 6619d0ad8caSDaniel Kiper __online_page_increment_counters(page); 6629d0ad8caSDaniel Kiper __online_page_free(page); 6639d0ad8caSDaniel Kiper } 664180c06efSJeremy Fitzhardinge 66575884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, 66675884fb1SKAMEZAWA Hiroyuki void *arg) 6673947be19SDave Hansen { 6683947be19SDave Hansen unsigned long i; 66975884fb1SKAMEZAWA Hiroyuki unsigned long onlined_pages = *(unsigned long *)arg; 67075884fb1SKAMEZAWA Hiroyuki struct page *page; 6712d070eabSMichal Hocko 67275884fb1SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) 67375884fb1SKAMEZAWA Hiroyuki for (i = 0; i < nr_pages; i++) { 67475884fb1SKAMEZAWA Hiroyuki page = pfn_to_page(start_pfn + i); 6759d0ad8caSDaniel Kiper (*online_page_callback)(page); 67675884fb1SKAMEZAWA Hiroyuki onlined_pages++; 67775884fb1SKAMEZAWA Hiroyuki } 6782d070eabSMichal Hocko 6792d070eabSMichal Hocko online_mem_sections(start_pfn, start_pfn + nr_pages); 6802d070eabSMichal Hocko 68175884fb1SKAMEZAWA Hiroyuki *(unsigned long *)arg = onlined_pages; 68275884fb1SKAMEZAWA Hiroyuki return 0; 68375884fb1SKAMEZAWA Hiroyuki } 68475884fb1SKAMEZAWA Hiroyuki 685d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */ 686d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages, 687d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 688d9713679SLai Jiangshan { 689d9713679SLai Jiangshan int nid = zone_to_nid(zone); 690d9713679SLai Jiangshan 6918efe33f4SOscar Salvador arg->status_change_nid = -1; 692d9713679SLai Jiangshan arg->status_change_nid_normal = -1; 6936715ddf9SLai Jiangshan arg->status_change_nid_high = -1; 6946715ddf9SLai Jiangshan 6956715ddf9SLai Jiangshan if (!node_state(nid, N_MEMORY)) 696d9713679SLai Jiangshan arg->status_change_nid = nid; 6978efe33f4SOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 6988efe33f4SOscar Salvador arg->status_change_nid_normal = nid; 6998efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM 7008efe33f4SOscar Salvador if (zone_idx(zone) <= N_HIGH_MEMORY && !node_state(nid, N_HIGH_MEMORY)) 7018efe33f4SOscar Salvador arg->status_change_nid_high = nid; 7028efe33f4SOscar Salvador #endif 703d9713679SLai Jiangshan } 704d9713679SLai Jiangshan 705d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg) 706d9713679SLai Jiangshan { 707d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 708d9713679SLai Jiangshan node_set_state(node, N_NORMAL_MEMORY); 709d9713679SLai Jiangshan 7106715ddf9SLai Jiangshan if (arg->status_change_nid_high >= 0) 711d9713679SLai Jiangshan node_set_state(node, N_HIGH_MEMORY); 7126715ddf9SLai Jiangshan 71383d83612SOscar Salvador if (arg->status_change_nid >= 0) 7146715ddf9SLai Jiangshan node_set_state(node, N_MEMORY); 715d9713679SLai Jiangshan } 716d9713679SLai Jiangshan 717f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 718f1dd2cd1SMichal Hocko unsigned long nr_pages) 719f1dd2cd1SMichal Hocko { 720f1dd2cd1SMichal Hocko unsigned long old_end_pfn = zone_end_pfn(zone); 721f1dd2cd1SMichal Hocko 722f1dd2cd1SMichal Hocko if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 723f1dd2cd1SMichal Hocko zone->zone_start_pfn = start_pfn; 724f1dd2cd1SMichal Hocko 725f1dd2cd1SMichal Hocko zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 726f1dd2cd1SMichal Hocko } 727f1dd2cd1SMichal Hocko 728f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 729f1dd2cd1SMichal Hocko unsigned long nr_pages) 730f1dd2cd1SMichal Hocko { 731f1dd2cd1SMichal Hocko unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 732f1dd2cd1SMichal Hocko 733f1dd2cd1SMichal Hocko if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 734f1dd2cd1SMichal Hocko pgdat->node_start_pfn = start_pfn; 735f1dd2cd1SMichal Hocko 736f1dd2cd1SMichal Hocko pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 737f1dd2cd1SMichal Hocko } 738f1dd2cd1SMichal Hocko 739a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 740a99583e7SChristoph Hellwig unsigned long nr_pages, struct vmem_altmap *altmap) 741f1dd2cd1SMichal Hocko { 742f1dd2cd1SMichal Hocko struct pglist_data *pgdat = zone->zone_pgdat; 743f1dd2cd1SMichal Hocko int nid = pgdat->node_id; 744f1dd2cd1SMichal Hocko unsigned long flags; 745f1dd2cd1SMichal Hocko 746f1dd2cd1SMichal Hocko if (zone_is_empty(zone)) 747f1dd2cd1SMichal Hocko init_currently_empty_zone(zone, start_pfn, nr_pages); 748f1dd2cd1SMichal Hocko 749f1dd2cd1SMichal Hocko clear_zone_contiguous(zone); 750f1dd2cd1SMichal Hocko 751f1dd2cd1SMichal Hocko /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ 752f1dd2cd1SMichal Hocko pgdat_resize_lock(pgdat, &flags); 753f1dd2cd1SMichal Hocko zone_span_writelock(zone); 754f1dd2cd1SMichal Hocko resize_zone_range(zone, start_pfn, nr_pages); 755f1dd2cd1SMichal Hocko zone_span_writeunlock(zone); 756f1dd2cd1SMichal Hocko resize_pgdat_range(pgdat, start_pfn, nr_pages); 757f1dd2cd1SMichal Hocko pgdat_resize_unlock(pgdat, &flags); 758f1dd2cd1SMichal Hocko 759f1dd2cd1SMichal Hocko /* 760f1dd2cd1SMichal Hocko * TODO now we have a visible range of pages which are not associated 761f1dd2cd1SMichal Hocko * with their zone properly. Not nice but set_pfnblock_flags_mask 762f1dd2cd1SMichal Hocko * expects the zone spans the pfn range. All the pages in the range 763f1dd2cd1SMichal Hocko * are reserved so nobody should be touching them so we should be safe 764f1dd2cd1SMichal Hocko */ 765a99583e7SChristoph Hellwig memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, 766a99583e7SChristoph Hellwig MEMMAP_HOTPLUG, altmap); 767f1dd2cd1SMichal Hocko 768f1dd2cd1SMichal Hocko set_zone_contiguous(zone); 769f1dd2cd1SMichal Hocko } 770f1dd2cd1SMichal Hocko 771f1dd2cd1SMichal Hocko /* 772c246a213SMichal Hocko * Returns a default kernel memory zone for the given pfn range. 773c246a213SMichal Hocko * If no kernel zone covers this pfn range it will automatically go 774c246a213SMichal Hocko * to the ZONE_NORMAL. 775c246a213SMichal Hocko */ 776c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 777c246a213SMichal Hocko unsigned long nr_pages) 778c246a213SMichal Hocko { 779c246a213SMichal Hocko struct pglist_data *pgdat = NODE_DATA(nid); 780c246a213SMichal Hocko int zid; 781c246a213SMichal Hocko 782c246a213SMichal Hocko for (zid = 0; zid <= ZONE_NORMAL; zid++) { 783c246a213SMichal Hocko struct zone *zone = &pgdat->node_zones[zid]; 784c246a213SMichal Hocko 785c246a213SMichal Hocko if (zone_intersects(zone, start_pfn, nr_pages)) 786c246a213SMichal Hocko return zone; 787c246a213SMichal Hocko } 788c246a213SMichal Hocko 789c246a213SMichal Hocko return &pgdat->node_zones[ZONE_NORMAL]; 790c246a213SMichal Hocko } 791c246a213SMichal Hocko 792c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 793c6f03e29SMichal Hocko unsigned long nr_pages) 794e5e68930SMichal Hocko { 795c6f03e29SMichal Hocko struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 796c6f03e29SMichal Hocko nr_pages); 797c6f03e29SMichal Hocko struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 798c6f03e29SMichal Hocko bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 799c6f03e29SMichal Hocko bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 800e5e68930SMichal Hocko 801e5e68930SMichal Hocko /* 802c6f03e29SMichal Hocko * We inherit the existing zone in a simple case where zones do not 803c6f03e29SMichal Hocko * overlap in the given range 804e5e68930SMichal Hocko */ 805c6f03e29SMichal Hocko if (in_kernel ^ in_movable) 806c6f03e29SMichal Hocko return (in_kernel) ? kernel_zone : movable_zone; 807e5e68930SMichal Hocko 808c6f03e29SMichal Hocko /* 809c6f03e29SMichal Hocko * If the range doesn't belong to any zone or two zones overlap in the 810c6f03e29SMichal Hocko * given range then we use movable zone only if movable_node is 811c6f03e29SMichal Hocko * enabled because we always online to a kernel zone by default. 812c6f03e29SMichal Hocko */ 813c6f03e29SMichal Hocko return movable_node_enabled ? movable_zone : kernel_zone; 8149f123ab5SMichal Hocko } 8159f123ab5SMichal Hocko 816e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn, 817e5e68930SMichal Hocko unsigned long nr_pages) 818f1dd2cd1SMichal Hocko { 819c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_KERNEL) 820c6f03e29SMichal Hocko return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 821f1dd2cd1SMichal Hocko 822c6f03e29SMichal Hocko if (online_type == MMOP_ONLINE_MOVABLE) 823c6f03e29SMichal Hocko return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 824f1dd2cd1SMichal Hocko 825c6f03e29SMichal Hocko return default_zone_for_pfn(nid, start_pfn, nr_pages); 826e5e68930SMichal Hocko } 827e5e68930SMichal Hocko 828e5e68930SMichal Hocko /* 829e5e68930SMichal Hocko * Associates the given pfn range with the given node and the zone appropriate 830e5e68930SMichal Hocko * for the given online type. 831e5e68930SMichal Hocko */ 832e5e68930SMichal Hocko static struct zone * __meminit move_pfn_range(int online_type, int nid, 833e5e68930SMichal Hocko unsigned long start_pfn, unsigned long nr_pages) 834e5e68930SMichal Hocko { 835e5e68930SMichal Hocko struct zone *zone; 836e5e68930SMichal Hocko 837e5e68930SMichal Hocko zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages); 838a99583e7SChristoph Hellwig move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL); 839f1dd2cd1SMichal Hocko return zone; 840df429ac0SReza Arbab } 84175884fb1SKAMEZAWA Hiroyuki 842511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) 84375884fb1SKAMEZAWA Hiroyuki { 844aa47228aSCody P Schafer unsigned long flags; 8453947be19SDave Hansen unsigned long onlined_pages = 0; 8463947be19SDave Hansen struct zone *zone; 8476811378eSYasunori Goto int need_zonelists_rebuild = 0; 8487b78d335SYasunori Goto int nid; 8497b78d335SYasunori Goto int ret; 8507b78d335SYasunori Goto struct memory_notify arg; 851d0dc12e8SPavel Tatashin struct memory_block *mem; 8523947be19SDave Hansen 853381eab4aSDavid Hildenbrand mem_hotplug_begin(); 854381eab4aSDavid Hildenbrand 855d0dc12e8SPavel Tatashin /* 856d0dc12e8SPavel Tatashin * We can't use pfn_to_nid() because nid might be stored in struct page 857d0dc12e8SPavel Tatashin * which is not yet initialized. Instead, we find nid from memory block. 858d0dc12e8SPavel Tatashin */ 859d0dc12e8SPavel Tatashin mem = find_memory_block(__pfn_to_section(pfn)); 860d0dc12e8SPavel Tatashin nid = mem->nid; 861d0dc12e8SPavel Tatashin 862f1dd2cd1SMichal Hocko /* associate pfn range with the zone */ 863f1dd2cd1SMichal Hocko zone = move_pfn_range(online_type, nid, pfn, nr_pages); 864511c2abaSLai Jiangshan 8657b78d335SYasunori Goto arg.start_pfn = pfn; 8667b78d335SYasunori Goto arg.nr_pages = nr_pages; 867d9713679SLai Jiangshan node_states_check_changes_online(nr_pages, zone, &arg); 8687b78d335SYasunori Goto 8697b78d335SYasunori Goto ret = memory_notify(MEM_GOING_ONLINE, &arg); 8707b78d335SYasunori Goto ret = notifier_to_errno(ret); 871e33e33b4SChen Yucong if (ret) 872e33e33b4SChen Yucong goto failed_addition; 873e33e33b4SChen Yucong 8743947be19SDave Hansen /* 8756811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 8766811378eSYasunori Goto * This means the page allocator ignores this zone. 8776811378eSYasunori Goto * So, zonelist must be updated after online. 8786811378eSYasunori Goto */ 8796dcd73d7SWen Congyang if (!populated_zone(zone)) { 8806811378eSYasunori Goto need_zonelists_rebuild = 1; 88172675e13SMichal Hocko setup_zone_pageset(zone); 8826dcd73d7SWen Congyang } 8836811378eSYasunori Goto 884908eedc6SKAMEZAWA Hiroyuki ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, 88575884fb1SKAMEZAWA Hiroyuki online_pages_range); 886fd8a4221SGeoff Levand if (ret) { 8876dcd73d7SWen Congyang if (need_zonelists_rebuild) 8886dcd73d7SWen Congyang zone_pcp_reset(zone); 889e33e33b4SChen Yucong goto failed_addition; 890fd8a4221SGeoff Levand } 891fd8a4221SGeoff Levand 8923947be19SDave Hansen zone->present_pages += onlined_pages; 893aa47228aSCody P Schafer 894aa47228aSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 895f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 896aa47228aSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 897aa47228aSCody P Schafer 89808dff7b7SJiang Liu if (onlined_pages) { 899e888ca35SVlastimil Babka node_states_set_node(nid, &arg); 9001f522509SHaicheng Li if (need_zonelists_rebuild) 90172675e13SMichal Hocko build_all_zonelists(NULL); 9021f522509SHaicheng Li else 903112067f0SShaohua Li zone_pcp_update(zone); 90408dff7b7SJiang Liu } 9051f522509SHaicheng Li 9061b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 9071b79acc9SKOSAKI Motohiro 908698b1b30SVlastimil Babka if (onlined_pages) { 909e888ca35SVlastimil Babka kswapd_run(nid); 910698b1b30SVlastimil Babka kcompactd_run(nid); 911698b1b30SVlastimil Babka } 91261b13993SDave Hansen 9135a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 9142f7f24ecSKent Liu 9152d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 9167b78d335SYasunori Goto 9177b78d335SYasunori Goto if (onlined_pages) 9187b78d335SYasunori Goto memory_notify(MEM_ONLINE, &arg); 919381eab4aSDavid Hildenbrand mem_hotplug_done(); 92030467e0bSDavid Rientjes return 0; 921e33e33b4SChen Yucong 922e33e33b4SChen Yucong failed_addition: 923e33e33b4SChen Yucong pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 924e33e33b4SChen Yucong (unsigned long long) pfn << PAGE_SHIFT, 925e33e33b4SChen Yucong (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 926e33e33b4SChen Yucong memory_notify(MEM_CANCEL_ONLINE, &arg); 927381eab4aSDavid Hildenbrand mem_hotplug_done(); 928e33e33b4SChen Yucong return ret; 9293947be19SDave Hansen } 93053947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 931bc02af93SYasunori Goto 9320bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat) 9330bd85420STang Chen { 9340bd85420STang Chen struct zone *z; 9350bd85420STang Chen 9360bd85420STang Chen for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 9370bd85420STang Chen z->present_pages = 0; 9380bd85420STang Chen 9390bd85420STang Chen pgdat->node_present_pages = 0; 9400bd85420STang Chen } 9410bd85420STang Chen 942e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 943e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) 9449af3c2deSYasunori Goto { 9459af3c2deSYasunori Goto struct pglist_data *pgdat; 946c8e861a5SFabian Frederick unsigned long start_pfn = PFN_DOWN(start); 9479af3c2deSYasunori Goto 948a1e565aaSTang Chen pgdat = NODE_DATA(nid); 949a1e565aaSTang Chen if (!pgdat) { 9509af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 9519af3c2deSYasunori Goto if (!pgdat) 9529af3c2deSYasunori Goto return NULL; 9539af3c2deSYasunori Goto 9549af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 955b0dc3a34SGu Zheng } else { 956e716f2ebSMel Gorman /* 957e716f2ebSMel Gorman * Reset the nr_zones, order and classzone_idx before reuse. 958e716f2ebSMel Gorman * Note that kswapd will init kswapd_classzone_idx properly 959e716f2ebSMel Gorman * when it starts in the near future. 960e716f2ebSMel Gorman */ 961b0dc3a34SGu Zheng pgdat->nr_zones = 0; 96238087d9bSMel Gorman pgdat->kswapd_order = 0; 96338087d9bSMel Gorman pgdat->kswapd_classzone_idx = 0; 964a1e565aaSTang Chen } 9659af3c2deSYasunori Goto 9669af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 9679af3c2deSYasunori Goto 96803e85f9dSOscar Salvador pgdat->node_id = nid; 96903e85f9dSOscar Salvador pgdat->node_start_pfn = start_pfn; 97003e85f9dSOscar Salvador 9719af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 97203e85f9dSOscar Salvador free_area_init_core_hotplug(nid); 9735830169fSReza Arbab pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat); 9749af3c2deSYasunori Goto 975959ecc48SKAMEZAWA Hiroyuki /* 976959ecc48SKAMEZAWA Hiroyuki * The node we allocated has no zone fallback lists. For avoiding 977959ecc48SKAMEZAWA Hiroyuki * to access not-initialized zonelist, build here. 978959ecc48SKAMEZAWA Hiroyuki */ 97972675e13SMichal Hocko build_all_zonelists(pgdat); 980959ecc48SKAMEZAWA Hiroyuki 981f784a3f1STang Chen /* 9820bd85420STang Chen * When memory is hot-added, all the memory is in offline state. So 9830bd85420STang Chen * clear all zones' present_pages because they will be updated in 9840bd85420STang Chen * online_pages() and offline_pages(). 9850bd85420STang Chen */ 98603e85f9dSOscar Salvador reset_node_managed_pages(pgdat); 9870bd85420STang Chen reset_node_present_pages(pgdat); 9880bd85420STang Chen 9899af3c2deSYasunori Goto return pgdat; 9909af3c2deSYasunori Goto } 9919af3c2deSYasunori Goto 992b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid) 9939af3c2deSYasunori Goto { 994b9ff0360SOscar Salvador pg_data_t *pgdat = NODE_DATA(nid); 995b9ff0360SOscar Salvador 9969af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 9975830169fSReza Arbab free_percpu(pgdat->per_cpu_nodestats); 9989af3c2deSYasunori Goto arch_free_nodedata(pgdat); 9999af3c2deSYasunori Goto return; 10009af3c2deSYasunori Goto } 10019af3c2deSYasunori Goto 10020a547039SKAMEZAWA Hiroyuki 100301b0f197SToshi Kani /** 100401b0f197SToshi Kani * try_online_node - online a node if offlined 1005e8b098fcSMike Rapoport * @nid: the node ID 1006b9ff0360SOscar Salvador * @start: start addr of the node 1007b9ff0360SOscar Salvador * @set_node_online: Whether we want to online the node 1008cf23422bSminskey guo * called by cpu_up() to online a node without onlined memory. 1009b9ff0360SOscar Salvador * 1010b9ff0360SOscar Salvador * Returns: 1011b9ff0360SOscar Salvador * 1 -> a new node has been allocated 1012b9ff0360SOscar Salvador * 0 -> the node is already online 1013b9ff0360SOscar Salvador * -ENOMEM -> the node could not be allocated 1014cf23422bSminskey guo */ 1015b9ff0360SOscar Salvador static int __try_online_node(int nid, u64 start, bool set_node_online) 1016cf23422bSminskey guo { 1017cf23422bSminskey guo pg_data_t *pgdat; 1018b9ff0360SOscar Salvador int ret = 1; 1019cf23422bSminskey guo 102001b0f197SToshi Kani if (node_online(nid)) 102101b0f197SToshi Kani return 0; 102201b0f197SToshi Kani 1023b9ff0360SOscar Salvador pgdat = hotadd_new_pgdat(nid, start); 10247553e8f2SDavid Rientjes if (!pgdat) { 102501b0f197SToshi Kani pr_err("Cannot online node %d due to NULL pgdat\n", nid); 1026cf23422bSminskey guo ret = -ENOMEM; 1027cf23422bSminskey guo goto out; 1028cf23422bSminskey guo } 1029b9ff0360SOscar Salvador 1030b9ff0360SOscar Salvador if (set_node_online) { 1031cf23422bSminskey guo node_set_online(nid); 1032cf23422bSminskey guo ret = register_one_node(nid); 1033cf23422bSminskey guo BUG_ON(ret); 1034b9ff0360SOscar Salvador } 1035cf23422bSminskey guo out: 1036b9ff0360SOscar Salvador return ret; 1037b9ff0360SOscar Salvador } 1038b9ff0360SOscar Salvador 1039b9ff0360SOscar Salvador /* 1040b9ff0360SOscar Salvador * Users of this function always want to online/register the node 1041b9ff0360SOscar Salvador */ 1042b9ff0360SOscar Salvador int try_online_node(int nid) 1043b9ff0360SOscar Salvador { 1044b9ff0360SOscar Salvador int ret; 1045b9ff0360SOscar Salvador 1046b9ff0360SOscar Salvador mem_hotplug_begin(); 1047b9ff0360SOscar Salvador ret = __try_online_node(nid, 0, true); 1048bfc8c901SVladimir Davydov mem_hotplug_done(); 1049cf23422bSminskey guo return ret; 1050cf23422bSminskey guo } 1051cf23422bSminskey guo 105227356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size) 105327356f54SToshi Kani { 1054ba325585SPavel Tatashin unsigned long block_sz = memory_block_size_bytes(); 1055ba325585SPavel Tatashin u64 block_nr_pages = block_sz >> PAGE_SHIFT; 105627356f54SToshi Kani u64 nr_pages = size >> PAGE_SHIFT; 1057ba325585SPavel Tatashin u64 start_pfn = PFN_DOWN(start); 105827356f54SToshi Kani 1059ba325585SPavel Tatashin /* memory range must be block size aligned */ 1060ba325585SPavel Tatashin if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) || 1061ba325585SPavel Tatashin !IS_ALIGNED(nr_pages, block_nr_pages)) { 1062ba325585SPavel Tatashin pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1063ba325585SPavel Tatashin block_sz, start, size); 106427356f54SToshi Kani return -EINVAL; 106527356f54SToshi Kani } 106627356f54SToshi Kani 106727356f54SToshi Kani return 0; 106827356f54SToshi Kani } 106927356f54SToshi Kani 107031bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg) 107131bc3858SVitaly Kuznetsov { 1072dc18d706SNathan Fontenot return device_online(&mem->dev); 107331bc3858SVitaly Kuznetsov } 107431bc3858SVitaly Kuznetsov 10758df1d0e4SDavid Hildenbrand /* 10768df1d0e4SDavid Hildenbrand * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 10778df1d0e4SDavid Hildenbrand * and online/offline operations (triggered e.g. by sysfs). 10788df1d0e4SDavid Hildenbrand * 10798df1d0e4SDavid Hildenbrand * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 10808df1d0e4SDavid Hildenbrand */ 1081f29d8e9cSDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res) 1082bc02af93SYasunori Goto { 108362cedb9fSDavid Vrabel u64 start, size; 1084b9ff0360SOscar Salvador bool new_node = false; 1085bc02af93SYasunori Goto int ret; 1086bc02af93SYasunori Goto 108762cedb9fSDavid Vrabel start = res->start; 108862cedb9fSDavid Vrabel size = resource_size(res); 108962cedb9fSDavid Vrabel 109027356f54SToshi Kani ret = check_hotplug_memory_range(start, size); 109127356f54SToshi Kani if (ret) 109227356f54SToshi Kani return ret; 109327356f54SToshi Kani 1094bfc8c901SVladimir Davydov mem_hotplug_begin(); 1095ac13c462SNathan Zimmer 10967f36e3e5STang Chen /* 10977f36e3e5STang Chen * Add new range to memblock so that when hotadd_new_pgdat() is called 10987f36e3e5STang Chen * to allocate new pgdat, get_pfn_range_for_nid() will be able to find 10997f36e3e5STang Chen * this new range and calculate total pages correctly. The range will 11007f36e3e5STang Chen * be removed at hot-remove time. 11017f36e3e5STang Chen */ 11027f36e3e5STang Chen memblock_add_node(start, size, nid); 11037f36e3e5STang Chen 1104b9ff0360SOscar Salvador ret = __try_online_node(nid, start, false); 1105b9ff0360SOscar Salvador if (ret < 0) 110641b9e2d7SWen Congyang goto error; 1107b9ff0360SOscar Salvador new_node = ret; 11089af3c2deSYasunori Goto 1109bc02af93SYasunori Goto /* call arch's memory hotadd */ 111024e6d5a5SChristoph Hellwig ret = arch_add_memory(nid, start, size, NULL, true); 11119af3c2deSYasunori Goto if (ret < 0) 11129af3c2deSYasunori Goto goto error; 11139af3c2deSYasunori Goto 1114a1e565aaSTang Chen if (new_node) { 1115d5b6f6a3SOscar Salvador /* If sysfs file of new node can't be created, cpu on the node 11160fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 11170fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 1118d5b6f6a3SOscar Salvador * We online node here. We can't roll back from here. 11190fc44159SYasunori Goto */ 1120d5b6f6a3SOscar Salvador node_set_online(nid); 1121d5b6f6a3SOscar Salvador ret = __register_one_node(nid); 11220fc44159SYasunori Goto BUG_ON(ret); 11230fc44159SYasunori Goto } 11240fc44159SYasunori Goto 1125d5b6f6a3SOscar Salvador /* link memory sections under this node.*/ 11264fbce633SOscar Salvador ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1)); 1127d5b6f6a3SOscar Salvador BUG_ON(ret); 1128d5b6f6a3SOscar Salvador 1129d96ae530Sakpm@linux-foundation.org /* create new memmap entry */ 1130d96ae530Sakpm@linux-foundation.org firmware_map_add_hotplug(start, start + size, "System RAM"); 1131d96ae530Sakpm@linux-foundation.org 1132381eab4aSDavid Hildenbrand /* device_online() will take the lock when calling online_pages() */ 1133381eab4aSDavid Hildenbrand mem_hotplug_done(); 1134381eab4aSDavid Hildenbrand 113531bc3858SVitaly Kuznetsov /* online pages if requested */ 1136f29d8e9cSDavid Hildenbrand if (memhp_auto_online) 113731bc3858SVitaly Kuznetsov walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), 113831bc3858SVitaly Kuznetsov NULL, online_memory_block); 113931bc3858SVitaly Kuznetsov 1140381eab4aSDavid Hildenbrand return ret; 11419af3c2deSYasunori Goto error: 11429af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 1143b9ff0360SOscar Salvador if (new_node) 1144b9ff0360SOscar Salvador rollback_node_hotadd(nid); 11457f36e3e5STang Chen memblock_remove(start, size); 1146bfc8c901SVladimir Davydov mem_hotplug_done(); 1147bc02af93SYasunori Goto return ret; 1148bc02af93SYasunori Goto } 114962cedb9fSDavid Vrabel 11508df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */ 11518df1d0e4SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size) 115262cedb9fSDavid Vrabel { 115362cedb9fSDavid Vrabel struct resource *res; 115462cedb9fSDavid Vrabel int ret; 115562cedb9fSDavid Vrabel 115662cedb9fSDavid Vrabel res = register_memory_resource(start, size); 11576f754ba4SVitaly Kuznetsov if (IS_ERR(res)) 11586f754ba4SVitaly Kuznetsov return PTR_ERR(res); 115962cedb9fSDavid Vrabel 1160f29d8e9cSDavid Hildenbrand ret = add_memory_resource(nid, res); 116162cedb9fSDavid Vrabel if (ret < 0) 116262cedb9fSDavid Vrabel release_memory_resource(res); 116362cedb9fSDavid Vrabel return ret; 116462cedb9fSDavid Vrabel } 11658df1d0e4SDavid Hildenbrand 11668df1d0e4SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size) 11678df1d0e4SDavid Hildenbrand { 11688df1d0e4SDavid Hildenbrand int rc; 11698df1d0e4SDavid Hildenbrand 11708df1d0e4SDavid Hildenbrand lock_device_hotplug(); 11718df1d0e4SDavid Hildenbrand rc = __add_memory(nid, start, size); 11728df1d0e4SDavid Hildenbrand unlock_device_hotplug(); 11738df1d0e4SDavid Hildenbrand 11748df1d0e4SDavid Hildenbrand return rc; 11758df1d0e4SDavid Hildenbrand } 1176bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 11770c0e6195SKAMEZAWA Hiroyuki 11780c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE 11790c0e6195SKAMEZAWA Hiroyuki /* 11805c755e9fSBadari Pulavarty * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy 11815c755e9fSBadari Pulavarty * set and the size of the free page is given by page_order(). Using this, 11825c755e9fSBadari Pulavarty * the function determines if the pageblock contains only free pages. 11835c755e9fSBadari Pulavarty * Due to buddy contraints, a free page at least the size of a pageblock will 11845c755e9fSBadari Pulavarty * be located at the start of the pageblock 11855c755e9fSBadari Pulavarty */ 11865c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page) 11875c755e9fSBadari Pulavarty { 11885c755e9fSBadari Pulavarty return PageBuddy(page) && page_order(page) >= pageblock_order; 11895c755e9fSBadari Pulavarty } 11905c755e9fSBadari Pulavarty 11915c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */ 11925c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page) 11935c755e9fSBadari Pulavarty { 11945c755e9fSBadari Pulavarty /* Ensure the starting page is pageblock-aligned */ 11955c755e9fSBadari Pulavarty BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); 11965c755e9fSBadari Pulavarty 11975c755e9fSBadari Pulavarty /* If the entire pageblock is free, move to the end of free page */ 11980dcc48c1SKAMEZAWA Hiroyuki if (pageblock_free(page)) { 11990dcc48c1SKAMEZAWA Hiroyuki int order; 12000dcc48c1SKAMEZAWA Hiroyuki /* be careful. we don't have locks, page_order can be changed.*/ 12010dcc48c1SKAMEZAWA Hiroyuki order = page_order(page); 12020dcc48c1SKAMEZAWA Hiroyuki if ((order < MAX_ORDER) && (order >= pageblock_order)) 12030dcc48c1SKAMEZAWA Hiroyuki return page + (1 << order); 12040dcc48c1SKAMEZAWA Hiroyuki } 12055c755e9fSBadari Pulavarty 12060dcc48c1SKAMEZAWA Hiroyuki return page + pageblock_nr_pages; 12075c755e9fSBadari Pulavarty } 12085c755e9fSBadari Pulavarty 1209fb52bbaeSMathieu Malaterre static bool is_pageblock_removable_nolock(struct page *page) 1210fb52bbaeSMathieu Malaterre { 1211fb52bbaeSMathieu Malaterre struct zone *zone; 1212fb52bbaeSMathieu Malaterre unsigned long pfn; 1213fb52bbaeSMathieu Malaterre 1214fb52bbaeSMathieu Malaterre /* 1215fb52bbaeSMathieu Malaterre * We have to be careful here because we are iterating over memory 1216fb52bbaeSMathieu Malaterre * sections which are not zone aware so we might end up outside of 1217fb52bbaeSMathieu Malaterre * the zone but still within the section. 1218fb52bbaeSMathieu Malaterre * We have to take care about the node as well. If the node is offline 1219fb52bbaeSMathieu Malaterre * its NODE_DATA will be NULL - see page_zone. 1220fb52bbaeSMathieu Malaterre */ 1221fb52bbaeSMathieu Malaterre if (!node_online(page_to_nid(page))) 1222fb52bbaeSMathieu Malaterre return false; 1223fb52bbaeSMathieu Malaterre 1224fb52bbaeSMathieu Malaterre zone = page_zone(page); 1225fb52bbaeSMathieu Malaterre pfn = page_to_pfn(page); 1226fb52bbaeSMathieu Malaterre if (!zone_spans_pfn(zone, pfn)) 1227fb52bbaeSMathieu Malaterre return false; 1228fb52bbaeSMathieu Malaterre 1229d381c547SMichal Hocko return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON); 1230fb52bbaeSMathieu Malaterre } 1231fb52bbaeSMathieu Malaterre 12325c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */ 1233c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) 12345c755e9fSBadari Pulavarty { 12355c755e9fSBadari Pulavarty struct page *page = pfn_to_page(start_pfn); 12365c755e9fSBadari Pulavarty struct page *end_page = page + nr_pages; 12375c755e9fSBadari Pulavarty 12385c755e9fSBadari Pulavarty /* Check the starting page of each pageblock within the range */ 12395c755e9fSBadari Pulavarty for (; page < end_page; page = next_active_pageblock(page)) { 124049ac8255SKAMEZAWA Hiroyuki if (!is_pageblock_removable_nolock(page)) 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; 12760c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn + i); 12770c0e6195SKAMEZAWA Hiroyuki if (zone && page_zone(page) != zone) 12780c0e6195SKAMEZAWA Hiroyuki return 0; 1279a96dfddbSToshi Kani if (!zone) 1280a96dfddbSToshi Kani start = pfn + i; 12810c0e6195SKAMEZAWA Hiroyuki zone = page_zone(page); 1282a96dfddbSToshi Kani end = pfn + MAX_ORDER_NR_PAGES; 12830c0e6195SKAMEZAWA Hiroyuki } 12845f0f2887SAndrew Banman } 1285deb88a2aSToshi Kani 1286a96dfddbSToshi Kani if (zone) { 1287a96dfddbSToshi Kani *valid_start = start; 1288d6d8c8a4Szhong jiang *valid_end = min(end, end_pfn); 12890c0e6195SKAMEZAWA Hiroyuki return 1; 1290a96dfddbSToshi Kani } else { 1291deb88a2aSToshi Kani return 0; 12920c0e6195SKAMEZAWA Hiroyuki } 1293a96dfddbSToshi Kani } 12940c0e6195SKAMEZAWA Hiroyuki 12950c0e6195SKAMEZAWA Hiroyuki /* 12960efadf48SYisheng Xie * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 12970efadf48SYisheng Xie * non-lru movable pages and hugepages). We scan pfn because it's much 12980efadf48SYisheng Xie * easier than scanning over linked list. This function returns the pfn 12990efadf48SYisheng Xie * of the first found movable page if it's found, otherwise 0. 13000c0e6195SKAMEZAWA Hiroyuki */ 1301c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end) 13020c0e6195SKAMEZAWA Hiroyuki { 13030c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 13040c0e6195SKAMEZAWA Hiroyuki struct page *page; 13050c0e6195SKAMEZAWA Hiroyuki for (pfn = start; pfn < end; pfn++) { 13060c0e6195SKAMEZAWA Hiroyuki if (pfn_valid(pfn)) { 13070c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 13080c0e6195SKAMEZAWA Hiroyuki if (PageLRU(page)) 13090c0e6195SKAMEZAWA Hiroyuki return pfn; 13100efadf48SYisheng Xie if (__PageMovable(page)) 13110efadf48SYisheng Xie return pfn; 1312c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1313464c7ffbSAneesh Kumar K.V if (hugepage_migration_supported(page_hstate(page)) && 1314464c7ffbSAneesh Kumar K.V page_huge_active(page)) 1315c8721bbbSNaoya Horiguchi return pfn; 1316c8721bbbSNaoya Horiguchi else 1317c8721bbbSNaoya Horiguchi pfn = round_up(pfn + 1, 1318c8721bbbSNaoya Horiguchi 1 << compound_order(page)) - 1; 1319c8721bbbSNaoya Horiguchi } 13200c0e6195SKAMEZAWA Hiroyuki } 13210c0e6195SKAMEZAWA Hiroyuki } 13220c0e6195SKAMEZAWA Hiroyuki return 0; 13230c0e6195SKAMEZAWA Hiroyuki } 13240c0e6195SKAMEZAWA Hiroyuki 1325666feb21SMichal Hocko static struct page *new_node_page(struct page *page, unsigned long private) 1326394e31d2SXishi Qiu { 1327394e31d2SXishi Qiu int nid = page_to_nid(page); 1328231e97e2SLi Zhong nodemask_t nmask = node_states[N_MEMORY]; 13297f252f27SMichal Hocko 13307f252f27SMichal Hocko /* 13317f252f27SMichal Hocko * try to allocate from a different node but reuse this node if there 13327f252f27SMichal Hocko * are no other online nodes to be used (e.g. we are offlining a part 13337f252f27SMichal Hocko * of the only existing node) 13347f252f27SMichal Hocko */ 13357f252f27SMichal Hocko node_clear(nid, nmask); 13367f252f27SMichal Hocko if (nodes_empty(nmask)) 13377f252f27SMichal Hocko node_set(nid, nmask); 1338394e31d2SXishi Qiu 13398b913238SMichal Hocko return new_page_nodemask(page, nid, &nmask); 1340394e31d2SXishi Qiu } 1341394e31d2SXishi Qiu 13420c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES (256) 13430c0e6195SKAMEZAWA Hiroyuki static int 13440c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 13450c0e6195SKAMEZAWA Hiroyuki { 13460c0e6195SKAMEZAWA Hiroyuki unsigned long pfn; 13470c0e6195SKAMEZAWA Hiroyuki struct page *page; 13480c0e6195SKAMEZAWA Hiroyuki int move_pages = NR_OFFLINE_AT_ONCE_PAGES; 13490c0e6195SKAMEZAWA Hiroyuki int not_managed = 0; 13500c0e6195SKAMEZAWA Hiroyuki int ret = 0; 13510c0e6195SKAMEZAWA Hiroyuki LIST_HEAD(source); 13520c0e6195SKAMEZAWA Hiroyuki 13530c0e6195SKAMEZAWA Hiroyuki for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { 13540c0e6195SKAMEZAWA Hiroyuki if (!pfn_valid(pfn)) 13550c0e6195SKAMEZAWA Hiroyuki continue; 13560c0e6195SKAMEZAWA Hiroyuki page = pfn_to_page(pfn); 1357c8721bbbSNaoya Horiguchi 1358c8721bbbSNaoya Horiguchi if (PageHuge(page)) { 1359c8721bbbSNaoya Horiguchi struct page *head = compound_head(page); 1360c8721bbbSNaoya Horiguchi pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1; 1361c8721bbbSNaoya Horiguchi if (compound_order(head) > PFN_SECTION_SHIFT) { 1362c8721bbbSNaoya Horiguchi ret = -EBUSY; 1363c8721bbbSNaoya Horiguchi break; 1364c8721bbbSNaoya Horiguchi } 1365c8721bbbSNaoya Horiguchi if (isolate_huge_page(page, &source)) 1366c8721bbbSNaoya Horiguchi move_pages -= 1 << compound_order(head); 1367c8721bbbSNaoya Horiguchi continue; 136894723aafSMichal Hocko } else if (PageTransHuge(page)) 13698135d892SNaoya Horiguchi pfn = page_to_pfn(compound_head(page)) 13708135d892SNaoya Horiguchi + hpage_nr_pages(page) - 1; 1371c8721bbbSNaoya Horiguchi 1372700c2a46SKonstantin Khlebnikov if (!get_page_unless_zero(page)) 13730c0e6195SKAMEZAWA Hiroyuki continue; 13740c0e6195SKAMEZAWA Hiroyuki /* 13750efadf48SYisheng Xie * We can skip free pages. And we can deal with pages on 13760efadf48SYisheng Xie * LRU and non-lru movable pages. 13770c0e6195SKAMEZAWA Hiroyuki */ 13780efadf48SYisheng Xie if (PageLRU(page)) 137962695a84SNick Piggin ret = isolate_lru_page(page); 13800efadf48SYisheng Xie else 13810efadf48SYisheng Xie ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 13820c0e6195SKAMEZAWA Hiroyuki if (!ret) { /* Success */ 1383700c2a46SKonstantin Khlebnikov put_page(page); 138462695a84SNick Piggin list_add_tail(&page->lru, &source); 13850c0e6195SKAMEZAWA Hiroyuki move_pages--; 13860efadf48SYisheng Xie if (!__PageMovable(page)) 1387599d0c95SMel Gorman inc_node_page_state(page, NR_ISOLATED_ANON + 13886d9c285aSKOSAKI Motohiro page_is_file_cache(page)); 13896d9c285aSKOSAKI Motohiro 13900c0e6195SKAMEZAWA Hiroyuki } else { 13912932c8b0SMichal Hocko pr_warn("failed to isolate pfn %lx\n", pfn); 13920efadf48SYisheng Xie dump_page(page, "isolation failed"); 1393700c2a46SKonstantin Khlebnikov put_page(page); 139425985edcSLucas De Marchi /* Because we don't have big zone->lock. we should 1395809c4449SBob Liu check this again here. */ 1396809c4449SBob Liu if (page_count(page)) { 1397809c4449SBob Liu not_managed++; 1398f3ab2636SBob Liu ret = -EBUSY; 1399809c4449SBob Liu break; 1400809c4449SBob Liu } 14010c0e6195SKAMEZAWA Hiroyuki } 14020c0e6195SKAMEZAWA Hiroyuki } 1403f3ab2636SBob Liu if (!list_empty(&source)) { 14040c0e6195SKAMEZAWA Hiroyuki if (not_managed) { 1405c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 14060c0e6195SKAMEZAWA Hiroyuki goto out; 14070c0e6195SKAMEZAWA Hiroyuki } 140874c08f98SMinchan Kim 1409394e31d2SXishi Qiu /* Allocate a new page from the nearest neighbor node */ 1410394e31d2SXishi Qiu ret = migrate_pages(&source, new_node_page, NULL, 0, 14119c620e2bSHugh Dickins MIGRATE_SYNC, MR_MEMORY_HOTPLUG); 14122932c8b0SMichal Hocko if (ret) { 14132932c8b0SMichal Hocko list_for_each_entry(page, &source, lru) { 14142932c8b0SMichal Hocko pr_warn("migrating pfn %lx failed ret:%d ", 14152932c8b0SMichal Hocko page_to_pfn(page), ret); 14162932c8b0SMichal Hocko dump_page(page, "migration failure"); 14172932c8b0SMichal Hocko } 1418c8721bbbSNaoya Horiguchi putback_movable_pages(&source); 1419f3ab2636SBob Liu } 14202932c8b0SMichal Hocko } 14210c0e6195SKAMEZAWA Hiroyuki out: 14220c0e6195SKAMEZAWA Hiroyuki return ret; 14230c0e6195SKAMEZAWA Hiroyuki } 14240c0e6195SKAMEZAWA Hiroyuki 14250c0e6195SKAMEZAWA Hiroyuki /* 14260c0e6195SKAMEZAWA Hiroyuki * remove from free_area[] and mark all as Reserved. 14270c0e6195SKAMEZAWA Hiroyuki */ 14280c0e6195SKAMEZAWA Hiroyuki static int 14290c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, 14300c0e6195SKAMEZAWA Hiroyuki void *data) 14310c0e6195SKAMEZAWA Hiroyuki { 14320c0e6195SKAMEZAWA Hiroyuki __offline_isolated_pages(start, start + nr_pages); 14330c0e6195SKAMEZAWA Hiroyuki return 0; 14340c0e6195SKAMEZAWA Hiroyuki } 14350c0e6195SKAMEZAWA Hiroyuki 14360c0e6195SKAMEZAWA Hiroyuki static void 14370c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) 14380c0e6195SKAMEZAWA Hiroyuki { 1439908eedc6SKAMEZAWA Hiroyuki walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, 14400c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb); 14410c0e6195SKAMEZAWA Hiroyuki } 14420c0e6195SKAMEZAWA Hiroyuki 14430c0e6195SKAMEZAWA Hiroyuki /* 14440c0e6195SKAMEZAWA Hiroyuki * Check all pages in range, recoreded as memory resource, are isolated. 14450c0e6195SKAMEZAWA Hiroyuki */ 14460c0e6195SKAMEZAWA Hiroyuki static int 14470c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, 14480c0e6195SKAMEZAWA Hiroyuki void *data) 14490c0e6195SKAMEZAWA Hiroyuki { 14500c0e6195SKAMEZAWA Hiroyuki int ret; 14510c0e6195SKAMEZAWA Hiroyuki long offlined = *(long *)data; 1452b023f468SWen Congyang ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true); 14530c0e6195SKAMEZAWA Hiroyuki offlined = nr_pages; 14540c0e6195SKAMEZAWA Hiroyuki if (!ret) 14550c0e6195SKAMEZAWA Hiroyuki *(long *)data += offlined; 14560c0e6195SKAMEZAWA Hiroyuki return ret; 14570c0e6195SKAMEZAWA Hiroyuki } 14580c0e6195SKAMEZAWA Hiroyuki 14590c0e6195SKAMEZAWA Hiroyuki static long 14600c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) 14610c0e6195SKAMEZAWA Hiroyuki { 14620c0e6195SKAMEZAWA Hiroyuki long offlined = 0; 14630c0e6195SKAMEZAWA Hiroyuki int ret; 14640c0e6195SKAMEZAWA Hiroyuki 1465908eedc6SKAMEZAWA Hiroyuki ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, 14660c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb); 14670c0e6195SKAMEZAWA Hiroyuki if (ret < 0) 14680c0e6195SKAMEZAWA Hiroyuki offlined = (long)ret; 14690c0e6195SKAMEZAWA Hiroyuki return offlined; 14700c0e6195SKAMEZAWA Hiroyuki } 14710c0e6195SKAMEZAWA Hiroyuki 1472c5320926STang Chen static int __init cmdline_parse_movable_node(char *p) 1473c5320926STang Chen { 14744932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP 147555ac590cSTang Chen movable_node_enabled = true; 14764932381eSMichal Hocko #else 14774932381eSMichal Hocko pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n"); 14784932381eSMichal Hocko #endif 1479c5320926STang Chen return 0; 1480c5320926STang Chen } 1481c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node); 1482c5320926STang Chen 1483d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */ 1484d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages, 1485d9713679SLai Jiangshan struct zone *zone, struct memory_notify *arg) 1486d9713679SLai Jiangshan { 1487d9713679SLai Jiangshan struct pglist_data *pgdat = zone->zone_pgdat; 1488d9713679SLai Jiangshan unsigned long present_pages = 0; 148986b27beaSOscar Salvador enum zone_type zt; 1490d9713679SLai Jiangshan 149186b27beaSOscar Salvador arg->status_change_nid = -1; 1492d9713679SLai Jiangshan arg->status_change_nid_normal = -1; 149386b27beaSOscar Salvador arg->status_change_nid_high = -1; 149486b27beaSOscar Salvador 149586b27beaSOscar Salvador /* 149686b27beaSOscar Salvador * Check whether node_states[N_NORMAL_MEMORY] will be changed. 149786b27beaSOscar Salvador * If the memory to be offline is within the range 149886b27beaSOscar Salvador * [0..ZONE_NORMAL], and it is the last present memory there, 149986b27beaSOscar Salvador * the zones in that range will become empty after the offlining, 150086b27beaSOscar Salvador * thus we can determine that we need to clear the node from 150186b27beaSOscar Salvador * node_states[N_NORMAL_MEMORY]. 150286b27beaSOscar Salvador */ 150386b27beaSOscar Salvador for (zt = 0; zt <= ZONE_NORMAL; zt++) 150486b27beaSOscar Salvador present_pages += pgdat->node_zones[zt].present_pages; 150586b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 150686b27beaSOscar Salvador arg->status_change_nid_normal = zone_to_nid(zone); 1507d9713679SLai Jiangshan 15086715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM 15096715ddf9SLai Jiangshan /* 151086b27beaSOscar Salvador * node_states[N_HIGH_MEMORY] contains nodes which 151186b27beaSOscar Salvador * have normal memory or high memory. 151286b27beaSOscar Salvador * Here we add the present_pages belonging to ZONE_HIGHMEM. 151386b27beaSOscar Salvador * If the zone is within the range of [0..ZONE_HIGHMEM), and 151486b27beaSOscar Salvador * we determine that the zones in that range become empty, 151586b27beaSOscar Salvador * we need to clear the node for N_HIGH_MEMORY. 15166715ddf9SLai Jiangshan */ 151786b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages; 151886b27beaSOscar Salvador if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages) 15196715ddf9SLai Jiangshan arg->status_change_nid_high = zone_to_nid(zone); 15206715ddf9SLai Jiangshan #endif 15216715ddf9SLai Jiangshan 1522d9713679SLai Jiangshan /* 152386b27beaSOscar Salvador * We have accounted the pages from [0..ZONE_NORMAL), and 152486b27beaSOscar Salvador * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM 152586b27beaSOscar Salvador * as well. 152686b27beaSOscar Salvador * Here we count the possible pages from ZONE_MOVABLE. 152786b27beaSOscar Salvador * If after having accounted all the pages, we see that the nr_pages 152886b27beaSOscar Salvador * to be offlined is over or equal to the accounted pages, 152986b27beaSOscar Salvador * we know that the node will become empty, and so, we can clear 153086b27beaSOscar Salvador * it for N_MEMORY as well. 1531d9713679SLai Jiangshan */ 153286b27beaSOscar Salvador present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1533d9713679SLai Jiangshan 1534d9713679SLai Jiangshan if (nr_pages >= present_pages) 1535d9713679SLai Jiangshan arg->status_change_nid = zone_to_nid(zone); 1536d9713679SLai Jiangshan } 1537d9713679SLai Jiangshan 1538d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg) 1539d9713679SLai Jiangshan { 1540d9713679SLai Jiangshan if (arg->status_change_nid_normal >= 0) 1541d9713679SLai Jiangshan node_clear_state(node, N_NORMAL_MEMORY); 1542d9713679SLai Jiangshan 1543cf01f6f5SOscar Salvador if (arg->status_change_nid_high >= 0) 1544d9713679SLai Jiangshan node_clear_state(node, N_HIGH_MEMORY); 15456715ddf9SLai Jiangshan 1546cf01f6f5SOscar Salvador if (arg->status_change_nid >= 0) 15476715ddf9SLai Jiangshan node_clear_state(node, N_MEMORY); 1548d9713679SLai Jiangshan } 1549d9713679SLai Jiangshan 1550a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn, 1551ecde0f3eSMichal Hocko unsigned long end_pfn) 15520c0e6195SKAMEZAWA Hiroyuki { 1553ecde0f3eSMichal Hocko unsigned long pfn, nr_pages; 15540c0e6195SKAMEZAWA Hiroyuki long offlined_pages; 155572b39cfcSMichal Hocko int ret, node; 1556d702909fSCody P Schafer unsigned long flags; 1557a96dfddbSToshi Kani unsigned long valid_start, valid_end; 15580c0e6195SKAMEZAWA Hiroyuki struct zone *zone; 15597b78d335SYasunori Goto struct memory_notify arg; 156079605093SMichal Hocko char *reason; 15610c0e6195SKAMEZAWA Hiroyuki 1562381eab4aSDavid Hildenbrand mem_hotplug_begin(); 1563381eab4aSDavid Hildenbrand 15640c0e6195SKAMEZAWA Hiroyuki /* This makes hotplug much easier...and readable. 15650c0e6195SKAMEZAWA Hiroyuki we assume this for now. .*/ 1566381eab4aSDavid Hildenbrand if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, 1567381eab4aSDavid Hildenbrand &valid_end)) { 1568381eab4aSDavid Hildenbrand mem_hotplug_done(); 156979605093SMichal Hocko ret = -EINVAL; 157079605093SMichal Hocko reason = "multizone range"; 157179605093SMichal Hocko goto failed_removal; 1572381eab4aSDavid Hildenbrand } 15737b78d335SYasunori Goto 1574a96dfddbSToshi Kani zone = page_zone(pfn_to_page(valid_start)); 15757b78d335SYasunori Goto node = zone_to_nid(zone); 15767b78d335SYasunori Goto nr_pages = end_pfn - start_pfn; 15777b78d335SYasunori Goto 15780c0e6195SKAMEZAWA Hiroyuki /* set above range as isolated */ 1579b023f468SWen Congyang ret = start_isolate_page_range(start_pfn, end_pfn, 1580d381c547SMichal Hocko MIGRATE_MOVABLE, 1581d381c547SMichal Hocko SKIP_HWPOISON | REPORT_FAILURE); 1582381eab4aSDavid Hildenbrand if (ret) { 1583381eab4aSDavid Hildenbrand mem_hotplug_done(); 158479605093SMichal Hocko reason = "failure to isolate range"; 158579605093SMichal Hocko goto failed_removal; 1586381eab4aSDavid Hildenbrand } 15877b78d335SYasunori Goto 15887b78d335SYasunori Goto arg.start_pfn = start_pfn; 15897b78d335SYasunori Goto arg.nr_pages = nr_pages; 1590d9713679SLai Jiangshan node_states_check_changes_offline(nr_pages, zone, &arg); 15917b78d335SYasunori Goto 15927b78d335SYasunori Goto ret = memory_notify(MEM_GOING_OFFLINE, &arg); 15937b78d335SYasunori Goto ret = notifier_to_errno(ret); 159479605093SMichal Hocko if (ret) { 159579605093SMichal Hocko reason = "notifier failure"; 159679605093SMichal Hocko goto failed_removal_isolated; 159779605093SMichal Hocko } 15987b78d335SYasunori Goto 15990c0e6195SKAMEZAWA Hiroyuki pfn = start_pfn; 16000c0e6195SKAMEZAWA Hiroyuki repeat: 16010c0e6195SKAMEZAWA Hiroyuki /* start memory hot removal */ 16020c0e6195SKAMEZAWA Hiroyuki ret = -EINTR; 160379605093SMichal Hocko if (signal_pending(current)) { 160479605093SMichal Hocko reason = "signal backoff"; 160579605093SMichal Hocko goto failed_removal_isolated; 160679605093SMichal Hocko } 160772b39cfcSMichal Hocko 16080c0e6195SKAMEZAWA Hiroyuki cond_resched(); 16099852a721SMichal Hocko lru_add_drain_all(); 1610c0554329SVlastimil Babka drain_all_pages(zone); 16110c0e6195SKAMEZAWA Hiroyuki 1612c8721bbbSNaoya Horiguchi pfn = scan_movable_pages(start_pfn, end_pfn); 1613c8721bbbSNaoya Horiguchi if (pfn) { /* We have movable pages */ 16140c0e6195SKAMEZAWA Hiroyuki ret = do_migrate_range(pfn, end_pfn); 16150c0e6195SKAMEZAWA Hiroyuki goto repeat; 16160c0e6195SKAMEZAWA Hiroyuki } 161772b39cfcSMichal Hocko 1618c8721bbbSNaoya Horiguchi /* 1619c8721bbbSNaoya Horiguchi * dissolve free hugepages in the memory block before doing offlining 1620c8721bbbSNaoya Horiguchi * actually in order to make hugetlbfs's object counting consistent. 1621c8721bbbSNaoya Horiguchi */ 1622082d5b6bSGerald Schaefer ret = dissolve_free_huge_pages(start_pfn, end_pfn); 162379605093SMichal Hocko if (ret) { 162479605093SMichal Hocko reason = "failure to dissolve huge pages"; 162579605093SMichal Hocko goto failed_removal_isolated; 162679605093SMichal Hocko } 16270c0e6195SKAMEZAWA Hiroyuki /* check again */ 16280c0e6195SKAMEZAWA Hiroyuki offlined_pages = check_pages_isolated(start_pfn, end_pfn); 162972b39cfcSMichal Hocko if (offlined_pages < 0) 163072b39cfcSMichal Hocko goto repeat; 1631e33e33b4SChen Yucong pr_info("Offlined Pages %ld\n", offlined_pages); 1632b3834be5SAdam Buchbinder /* Ok, all of our target is isolated. 16330c0e6195SKAMEZAWA Hiroyuki We cannot do rollback at this point. */ 16340c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(start_pfn, end_pfn); 1635dbc0e4ceSKAMEZAWA Hiroyuki /* reset pagetype flags and makes migrate type to be MOVABLE */ 16360815f3d8SMichal Nazarewicz undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 16370c0e6195SKAMEZAWA Hiroyuki /* removal success */ 16383dcc0571SJiang Liu adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); 16390c0e6195SKAMEZAWA Hiroyuki zone->present_pages -= offlined_pages; 1640d702909fSCody P Schafer 1641d702909fSCody P Schafer pgdat_resize_lock(zone->zone_pgdat, &flags); 16420c0e6195SKAMEZAWA Hiroyuki zone->zone_pgdat->node_present_pages -= offlined_pages; 1643d702909fSCody P Schafer pgdat_resize_unlock(zone->zone_pgdat, &flags); 16447b78d335SYasunori Goto 16451b79acc9SKOSAKI Motohiro init_per_zone_wmark_min(); 16461b79acc9SKOSAKI Motohiro 16471e8537baSXishi Qiu if (!populated_zone(zone)) { 1648340175b7SJiang Liu zone_pcp_reset(zone); 164972675e13SMichal Hocko build_all_zonelists(NULL); 16501e8537baSXishi Qiu } else 16511e8537baSXishi Qiu zone_pcp_update(zone); 1652340175b7SJiang Liu 1653d9713679SLai Jiangshan node_states_clear_node(node, &arg); 1654698b1b30SVlastimil Babka if (arg.status_change_nid >= 0) { 16558fe23e05SDavid Rientjes kswapd_stop(node); 1656698b1b30SVlastimil Babka kcompactd_stop(node); 1657698b1b30SVlastimil Babka } 1658bce7394aSMinchan Kim 16590c0e6195SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 16600c0e6195SKAMEZAWA Hiroyuki writeback_set_ratelimit(); 16617b78d335SYasunori Goto 16627b78d335SYasunori Goto memory_notify(MEM_OFFLINE, &arg); 1663381eab4aSDavid Hildenbrand mem_hotplug_done(); 16640c0e6195SKAMEZAWA Hiroyuki return 0; 16650c0e6195SKAMEZAWA Hiroyuki 166679605093SMichal Hocko failed_removal_isolated: 166779605093SMichal Hocko undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 16680c0e6195SKAMEZAWA Hiroyuki failed_removal: 166979605093SMichal Hocko pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 1670a62e2f4fSBjorn Helgaas (unsigned long long) start_pfn << PAGE_SHIFT, 167179605093SMichal Hocko ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 167279605093SMichal Hocko reason); 16737b78d335SYasunori Goto memory_notify(MEM_CANCEL_OFFLINE, &arg); 16740c0e6195SKAMEZAWA Hiroyuki /* pushback to free area */ 1675381eab4aSDavid Hildenbrand mem_hotplug_done(); 16760c0e6195SKAMEZAWA Hiroyuki return ret; 16770c0e6195SKAMEZAWA Hiroyuki } 167871088785SBadari Pulavarty 1679a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages) 1680a16cee10SWen Congyang { 1681ecde0f3eSMichal Hocko return __offline_pages(start_pfn, start_pfn + nr_pages); 1682a16cee10SWen Congyang } 1683e2ff3940SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 1684a16cee10SWen Congyang 1685bbc76be6SWen Congyang /** 1686bbc76be6SWen Congyang * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn) 1687bbc76be6SWen Congyang * @start_pfn: start pfn of the memory range 1688e05c4bbfSToshi Kani * @end_pfn: end pfn of the memory range 1689bbc76be6SWen Congyang * @arg: argument passed to func 1690bbc76be6SWen Congyang * @func: callback for each memory section walked 1691bbc76be6SWen Congyang * 1692bbc76be6SWen Congyang * This function walks through all present mem sections in range 1693bbc76be6SWen Congyang * [start_pfn, end_pfn) and call func on each mem section. 1694bbc76be6SWen Congyang * 1695bbc76be6SWen Congyang * Returns the return value of func. 1696bbc76be6SWen Congyang */ 1697e2ff3940SRafael J. Wysocki int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, 1698bbc76be6SWen Congyang void *arg, int (*func)(struct memory_block *, void *)) 169971088785SBadari Pulavarty { 1700e90bdb7fSWen Congyang struct memory_block *mem = NULL; 1701e90bdb7fSWen Congyang struct mem_section *section; 1702e90bdb7fSWen Congyang unsigned long pfn, section_nr; 1703e90bdb7fSWen Congyang int ret; 170471088785SBadari Pulavarty 1705e90bdb7fSWen Congyang for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 1706e90bdb7fSWen Congyang section_nr = pfn_to_section_nr(pfn); 1707e90bdb7fSWen Congyang if (!present_section_nr(section_nr)) 1708e90bdb7fSWen Congyang continue; 1709e90bdb7fSWen Congyang 1710e90bdb7fSWen Congyang section = __nr_to_section(section_nr); 1711e90bdb7fSWen Congyang /* same memblock? */ 1712e90bdb7fSWen Congyang if (mem) 1713e90bdb7fSWen Congyang if ((section_nr >= mem->start_section_nr) && 1714e90bdb7fSWen Congyang (section_nr <= mem->end_section_nr)) 1715e90bdb7fSWen Congyang continue; 1716e90bdb7fSWen Congyang 1717e90bdb7fSWen Congyang mem = find_memory_block_hinted(section, mem); 1718e90bdb7fSWen Congyang if (!mem) 1719e90bdb7fSWen Congyang continue; 1720e90bdb7fSWen Congyang 1721bbc76be6SWen Congyang ret = func(mem, arg); 1722e90bdb7fSWen Congyang if (ret) { 1723e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1724e90bdb7fSWen Congyang return ret; 1725e90bdb7fSWen Congyang } 1726e90bdb7fSWen Congyang } 1727e90bdb7fSWen Congyang 1728e90bdb7fSWen Congyang if (mem) 1729e90bdb7fSWen Congyang kobject_put(&mem->dev.kobj); 1730e90bdb7fSWen Congyang 1731bbc76be6SWen Congyang return 0; 1732bbc76be6SWen Congyang } 1733bbc76be6SWen Congyang 1734e2ff3940SRafael J. Wysocki #ifdef CONFIG_MEMORY_HOTREMOVE 1735d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1736bbc76be6SWen Congyang { 1737bbc76be6SWen Congyang int ret = !is_memblock_offlined(mem); 1738bbc76be6SWen Congyang 1739349daa0fSRandy Dunlap if (unlikely(ret)) { 1740349daa0fSRandy Dunlap phys_addr_t beginpa, endpa; 1741349daa0fSRandy Dunlap 1742349daa0fSRandy Dunlap beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1743349daa0fSRandy Dunlap endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1; 1744756a025fSJoe Perches pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 1745349daa0fSRandy Dunlap &beginpa, &endpa); 1746349daa0fSRandy Dunlap } 1747bbc76be6SWen Congyang 1748bbc76be6SWen Congyang return ret; 1749bbc76be6SWen Congyang } 1750bbc76be6SWen Congyang 17510f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat) 175260a5a19eSTang Chen { 175360a5a19eSTang Chen int cpu; 175460a5a19eSTang Chen 175560a5a19eSTang Chen for_each_present_cpu(cpu) { 175660a5a19eSTang Chen if (cpu_to_node(cpu) == pgdat->node_id) 175760a5a19eSTang Chen /* 175860a5a19eSTang Chen * the cpu on this node isn't removed, and we can't 175960a5a19eSTang Chen * offline this node. 176060a5a19eSTang Chen */ 176160a5a19eSTang Chen return -EBUSY; 176260a5a19eSTang Chen } 176360a5a19eSTang Chen 176460a5a19eSTang Chen return 0; 176560a5a19eSTang Chen } 176660a5a19eSTang Chen 17670f1cfe9dSToshi Kani /** 17680f1cfe9dSToshi Kani * try_offline_node 1769e8b098fcSMike Rapoport * @nid: the node ID 17700f1cfe9dSToshi Kani * 17710f1cfe9dSToshi Kani * Offline a node if all memory sections and cpus of the node are removed. 17720f1cfe9dSToshi Kani * 17730f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 17740f1cfe9dSToshi Kani * and online/offline operations before this call. 17750f1cfe9dSToshi Kani */ 177690b30cdcSWen Congyang void try_offline_node(int nid) 177760a5a19eSTang Chen { 1778d822b86aSWen Congyang pg_data_t *pgdat = NODE_DATA(nid); 1779d822b86aSWen Congyang unsigned long start_pfn = pgdat->node_start_pfn; 1780d822b86aSWen Congyang unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; 178160a5a19eSTang Chen unsigned long pfn; 178260a5a19eSTang Chen 178360a5a19eSTang Chen for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 178460a5a19eSTang Chen unsigned long section_nr = pfn_to_section_nr(pfn); 178560a5a19eSTang Chen 178660a5a19eSTang Chen if (!present_section_nr(section_nr)) 178760a5a19eSTang Chen continue; 178860a5a19eSTang Chen 178960a5a19eSTang Chen if (pfn_to_nid(pfn) != nid) 179060a5a19eSTang Chen continue; 179160a5a19eSTang Chen 179260a5a19eSTang Chen /* 179360a5a19eSTang Chen * some memory sections of this node are not removed, and we 179460a5a19eSTang Chen * can't offline node now. 179560a5a19eSTang Chen */ 179660a5a19eSTang Chen return; 179760a5a19eSTang Chen } 179860a5a19eSTang Chen 179946a3679bSMichal Hocko if (check_cpu_on_node(pgdat)) 180060a5a19eSTang Chen return; 180160a5a19eSTang Chen 180260a5a19eSTang Chen /* 180360a5a19eSTang Chen * all memory/cpu of this node are removed, we can offline this 180460a5a19eSTang Chen * node now. 180560a5a19eSTang Chen */ 180660a5a19eSTang Chen node_set_offline(nid); 180760a5a19eSTang Chen unregister_one_node(nid); 180860a5a19eSTang Chen } 180990b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node); 181060a5a19eSTang Chen 18110f1cfe9dSToshi Kani /** 18120f1cfe9dSToshi Kani * remove_memory 1813e8b098fcSMike Rapoport * @nid: the node ID 1814e8b098fcSMike Rapoport * @start: physical address of the region to remove 1815e8b098fcSMike Rapoport * @size: size of the region to remove 18160f1cfe9dSToshi Kani * 18170f1cfe9dSToshi Kani * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 18180f1cfe9dSToshi Kani * and online/offline operations before this call, as required by 18190f1cfe9dSToshi Kani * try_offline_node(). 18200f1cfe9dSToshi Kani */ 1821d15e5926SDavid Hildenbrand void __ref __remove_memory(int nid, u64 start, u64 size) 1822bbc76be6SWen Congyang { 1823242831ebSRafael J. Wysocki int ret; 1824993c1aadSWen Congyang 182527356f54SToshi Kani BUG_ON(check_hotplug_memory_range(start, size)); 182627356f54SToshi Kani 1827bfc8c901SVladimir Davydov mem_hotplug_begin(); 18286677e3eaSYasuaki Ishimatsu 18296677e3eaSYasuaki Ishimatsu /* 1830242831ebSRafael J. Wysocki * All memory blocks must be offlined before removing memory. Check 1831242831ebSRafael J. Wysocki * whether all memory blocks in question are offline and trigger a BUG() 1832242831ebSRafael J. Wysocki * if this is not the case. 18336677e3eaSYasuaki Ishimatsu */ 1834242831ebSRafael J. Wysocki ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, 1835d6de9d53SXishi Qiu check_memblock_offlined_cb); 1836bfc8c901SVladimir Davydov if (ret) 1837242831ebSRafael J. Wysocki BUG(); 18386677e3eaSYasuaki Ishimatsu 183946c66c4bSYasuaki Ishimatsu /* remove memmap entry */ 184046c66c4bSYasuaki Ishimatsu firmware_map_remove(start, start + size, "System RAM"); 1841f9126ab9SXishi Qiu memblock_free(start, size); 1842f9126ab9SXishi Qiu memblock_remove(start, size); 184346c66c4bSYasuaki Ishimatsu 1844*2c2a5af6SOscar Salvador arch_remove_memory(nid, start, size, NULL); 184524d335caSWen Congyang 184660a5a19eSTang Chen try_offline_node(nid); 184760a5a19eSTang Chen 1848bfc8c901SVladimir Davydov mem_hotplug_done(); 184971088785SBadari Pulavarty } 1850d15e5926SDavid Hildenbrand 1851d15e5926SDavid Hildenbrand void remove_memory(int nid, u64 start, u64 size) 1852d15e5926SDavid Hildenbrand { 1853d15e5926SDavid Hildenbrand lock_device_hotplug(); 1854d15e5926SDavid Hildenbrand __remove_memory(nid, start, size); 1855d15e5926SDavid Hildenbrand unlock_device_hotplug(); 1856d15e5926SDavid Hildenbrand } 185771088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory); 1858aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */ 1859