xref: /openbmc/linux/mm/memory_hotplug.c (revision f5637d3b42ab0465ef71d5fb8461bce97fba95e8)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23947be19SDave Hansen /*
33947be19SDave Hansen  *  linux/mm/memory_hotplug.c
43947be19SDave Hansen  *
53947be19SDave Hansen  *  Copyright (C)
63947be19SDave Hansen  */
73947be19SDave Hansen 
83947be19SDave Hansen #include <linux/stddef.h>
93947be19SDave Hansen #include <linux/mm.h>
10174cd4b1SIngo Molnar #include <linux/sched/signal.h>
113947be19SDave Hansen #include <linux/swap.h>
123947be19SDave Hansen #include <linux/interrupt.h>
133947be19SDave Hansen #include <linux/pagemap.h>
143947be19SDave Hansen #include <linux/compiler.h>
15b95f1b31SPaul Gortmaker #include <linux/export.h>
163947be19SDave Hansen #include <linux/pagevec.h>
172d1d43f6SChandra Seetharaman #include <linux/writeback.h>
183947be19SDave Hansen #include <linux/slab.h>
193947be19SDave Hansen #include <linux/sysctl.h>
203947be19SDave Hansen #include <linux/cpu.h>
213947be19SDave Hansen #include <linux/memory.h>
224b94ffdcSDan Williams #include <linux/memremap.h>
233947be19SDave Hansen #include <linux/memory_hotplug.h>
243947be19SDave Hansen #include <linux/highmem.h>
253947be19SDave Hansen #include <linux/vmalloc.h>
260a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h>
270c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h>
280c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h>
290c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h>
3071088785SBadari Pulavarty #include <linux/pfn.h>
316ad696d2SAndi Kleen #include <linux/suspend.h>
326d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h>
33d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h>
3460a5a19eSTang Chen #include <linux/stop_machine.h>
35c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h>
36c5320926STang Chen #include <linux/memblock.h>
37698b1b30SVlastimil Babka #include <linux/compaction.h>
38b15c8726SMichal Hocko #include <linux/rmap.h>
393947be19SDave Hansen 
403947be19SDave Hansen #include <asm/tlbflush.h>
413947be19SDave Hansen 
421e5ad9a3SAdrian Bunk #include "internal.h"
43e900a918SDan Williams #include "shuffle.h"
441e5ad9a3SAdrian Bunk 
459d0ad8caSDaniel Kiper /*
469d0ad8caSDaniel Kiper  * online_page_callback contains pointer to current page onlining function.
479d0ad8caSDaniel Kiper  * Initially it is generic_online_page(). If it is required it could be
489d0ad8caSDaniel Kiper  * changed by calling set_online_page_callback() for callback registration
499d0ad8caSDaniel Kiper  * and restore_online_page_callback() for generic callback restore.
509d0ad8caSDaniel Kiper  */
519d0ad8caSDaniel Kiper 
529d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page;
53bfc8c901SVladimir Davydov static DEFINE_MUTEX(online_page_callback_lock);
549d0ad8caSDaniel Kiper 
553f906ba2SThomas Gleixner DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
5620d6c96bSKOSAKI Motohiro 
573f906ba2SThomas Gleixner void get_online_mems(void)
583f906ba2SThomas Gleixner {
593f906ba2SThomas Gleixner 	percpu_down_read(&mem_hotplug_lock);
603f906ba2SThomas Gleixner }
61bfc8c901SVladimir Davydov 
623f906ba2SThomas Gleixner void put_online_mems(void)
633f906ba2SThomas Gleixner {
643f906ba2SThomas Gleixner 	percpu_up_read(&mem_hotplug_lock);
653f906ba2SThomas Gleixner }
66bfc8c901SVladimir Davydov 
674932381eSMichal Hocko bool movable_node_enabled = false;
684932381eSMichal Hocko 
698604d9e5SVitaly Kuznetsov #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
70862919e5SDavid Hildenbrand int memhp_default_online_type = MMOP_OFFLINE;
718604d9e5SVitaly Kuznetsov #else
72862919e5SDavid Hildenbrand int memhp_default_online_type = MMOP_ONLINE;
738604d9e5SVitaly Kuznetsov #endif
7431bc3858SVitaly Kuznetsov 
7586dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str)
7686dd995dSVitaly Kuznetsov {
775f47adf7SDavid Hildenbrand 	const int online_type = memhp_online_type_from_str(str);
785f47adf7SDavid Hildenbrand 
795f47adf7SDavid Hildenbrand 	if (online_type >= 0)
805f47adf7SDavid Hildenbrand 		memhp_default_online_type = online_type;
8186dd995dSVitaly Kuznetsov 
8286dd995dSVitaly Kuznetsov 	return 1;
8386dd995dSVitaly Kuznetsov }
8486dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state);
8586dd995dSVitaly Kuznetsov 
8630467e0bSDavid Rientjes void mem_hotplug_begin(void)
87bfc8c901SVladimir Davydov {
883f906ba2SThomas Gleixner 	cpus_read_lock();
893f906ba2SThomas Gleixner 	percpu_down_write(&mem_hotplug_lock);
90bfc8c901SVladimir Davydov }
91bfc8c901SVladimir Davydov 
9230467e0bSDavid Rientjes void mem_hotplug_done(void)
93bfc8c901SVladimir Davydov {
943f906ba2SThomas Gleixner 	percpu_up_write(&mem_hotplug_lock);
953f906ba2SThomas Gleixner 	cpus_read_unlock();
96bfc8c901SVladimir Davydov }
9720d6c96bSKOSAKI Motohiro 
98357b4da5SJuergen Gross u64 max_mem_size = U64_MAX;
99357b4da5SJuergen Gross 
10045e0b78bSKeith Mannthey /* add this memory to iomem resource */
10145e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
10245e0b78bSKeith Mannthey {
1032794129eSDave Hansen 	struct resource *res;
1042794129eSDave Hansen 	unsigned long flags =  IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1052794129eSDave Hansen 	char *resource_name = "System RAM";
106357b4da5SJuergen Gross 
107f3cd4c86SBaoquan He 	/*
108f3cd4c86SBaoquan He 	 * Make sure value parsed from 'mem=' only restricts memory adding
109f3cd4c86SBaoquan He 	 * while booting, so that memory hotplug won't be impacted. Please
110f3cd4c86SBaoquan He 	 * refer to document of 'mem=' in kernel-parameters.txt for more
111f3cd4c86SBaoquan He 	 * details.
112f3cd4c86SBaoquan He 	 */
113f3cd4c86SBaoquan He 	if (start + size > max_mem_size && system_state < SYSTEM_RUNNING)
114357b4da5SJuergen Gross 		return ERR_PTR(-E2BIG);
115357b4da5SJuergen Gross 
1162794129eSDave Hansen 	/*
1172794129eSDave Hansen 	 * Request ownership of the new memory range.  This might be
1182794129eSDave Hansen 	 * a child of an existing resource that was present but
1192794129eSDave Hansen 	 * not marked as busy.
1202794129eSDave Hansen 	 */
1212794129eSDave Hansen 	res = __request_region(&iomem_resource, start, size,
1222794129eSDave Hansen 			       resource_name, flags);
12345e0b78bSKeith Mannthey 
1242794129eSDave Hansen 	if (!res) {
1252794129eSDave Hansen 		pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
1262794129eSDave Hansen 				start, start + size);
1276f754ba4SVitaly Kuznetsov 		return ERR_PTR(-EEXIST);
12845e0b78bSKeith Mannthey 	}
12945e0b78bSKeith Mannthey 	return res;
13045e0b78bSKeith Mannthey }
13145e0b78bSKeith Mannthey 
13245e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
13345e0b78bSKeith Mannthey {
13445e0b78bSKeith Mannthey 	if (!res)
13545e0b78bSKeith Mannthey 		return;
13645e0b78bSKeith Mannthey 	release_resource(res);
13745e0b78bSKeith Mannthey 	kfree(res);
13845e0b78bSKeith Mannthey }
13945e0b78bSKeith Mannthey 
14053947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
14146723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info,  struct page *page,
1425f24ce5fSAndrea Arcangeli 		      unsigned long type)
14304753278SYasunori Goto {
144ddffe98dSYasuaki Ishimatsu 	page->freelist = (void *)type;
14504753278SYasunori Goto 	SetPagePrivate(page);
14604753278SYasunori Goto 	set_page_private(page, info);
147fe896d18SJoonsoo Kim 	page_ref_inc(page);
14804753278SYasunori Goto }
14904753278SYasunori Goto 
150170a5a7eSJiang Liu void put_page_bootmem(struct page *page)
15104753278SYasunori Goto {
1525f24ce5fSAndrea Arcangeli 	unsigned long type;
15304753278SYasunori Goto 
154ddffe98dSYasuaki Ishimatsu 	type = (unsigned long) page->freelist;
1555f24ce5fSAndrea Arcangeli 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
1565f24ce5fSAndrea Arcangeli 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
15704753278SYasunori Goto 
158fe896d18SJoonsoo Kim 	if (page_ref_dec_return(page) == 1) {
159ddffe98dSYasuaki Ishimatsu 		page->freelist = NULL;
16004753278SYasunori Goto 		ClearPagePrivate(page);
16104753278SYasunori Goto 		set_page_private(page, 0);
1625f24ce5fSAndrea Arcangeli 		INIT_LIST_HEAD(&page->lru);
163170a5a7eSJiang Liu 		free_reserved_page(page);
16404753278SYasunori Goto 	}
16504753278SYasunori Goto }
16604753278SYasunori Goto 
16746723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
16846723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP
169d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
17004753278SYasunori Goto {
171f1eca35aSDan Williams 	unsigned long mapsize, section_nr, i;
17204753278SYasunori Goto 	struct mem_section *ms;
17304753278SYasunori Goto 	struct page *page, *memmap;
174f1eca35aSDan Williams 	struct mem_section_usage *usage;
17504753278SYasunori Goto 
17604753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
17704753278SYasunori Goto 	ms = __nr_to_section(section_nr);
17804753278SYasunori Goto 
17904753278SYasunori Goto 	/* Get section's memmap address */
18004753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
18104753278SYasunori Goto 
18204753278SYasunori Goto 	/*
18304753278SYasunori Goto 	 * Get page for the memmap's phys address
18404753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
18504753278SYasunori Goto 	 */
18604753278SYasunori Goto 	page = virt_to_page(memmap);
18704753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
18804753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
18904753278SYasunori Goto 
19004753278SYasunori Goto 	/* remember memmap's page */
19104753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
19204753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
19304753278SYasunori Goto 
194f1eca35aSDan Williams 	usage = ms->usage;
195f1eca35aSDan Williams 	page = virt_to_page(usage);
19604753278SYasunori Goto 
197f1eca35aSDan Williams 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
19804753278SYasunori Goto 
19904753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
200af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
20104753278SYasunori Goto 
20204753278SYasunori Goto }
20346723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */
20446723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn)
20546723bfaSYasuaki Ishimatsu {
206f1eca35aSDan Williams 	unsigned long mapsize, section_nr, i;
20746723bfaSYasuaki Ishimatsu 	struct mem_section *ms;
20846723bfaSYasuaki Ishimatsu 	struct page *page, *memmap;
209f1eca35aSDan Williams 	struct mem_section_usage *usage;
21046723bfaSYasuaki Ishimatsu 
21146723bfaSYasuaki Ishimatsu 	section_nr = pfn_to_section_nr(start_pfn);
21246723bfaSYasuaki Ishimatsu 	ms = __nr_to_section(section_nr);
21346723bfaSYasuaki Ishimatsu 
21446723bfaSYasuaki Ishimatsu 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
21546723bfaSYasuaki Ishimatsu 
21646723bfaSYasuaki Ishimatsu 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
21746723bfaSYasuaki Ishimatsu 
218f1eca35aSDan Williams 	usage = ms->usage;
219f1eca35aSDan Williams 	page = virt_to_page(usage);
22046723bfaSYasuaki Ishimatsu 
221f1eca35aSDan Williams 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
22246723bfaSYasuaki Ishimatsu 
22346723bfaSYasuaki Ishimatsu 	for (i = 0; i < mapsize; i++, page++)
22446723bfaSYasuaki Ishimatsu 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
22546723bfaSYasuaki Ishimatsu }
22646723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
22704753278SYasunori Goto 
2287ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
22904753278SYasunori Goto {
23004753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
23104753278SYasunori Goto 	int node = pgdat->node_id;
23204753278SYasunori Goto 	struct page *page;
23304753278SYasunori Goto 
23404753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
23504753278SYasunori Goto 	page = virt_to_page(pgdat);
23604753278SYasunori Goto 
23704753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
23804753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
23904753278SYasunori Goto 
24004753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
241c1f19495SCody P Schafer 	end_pfn = pgdat_end_pfn(pgdat);
24204753278SYasunori Goto 
2437e9f5eb0STang Chen 	/* register section info */
244f14851afSqiuxishi 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
245f14851afSqiuxishi 		/*
246f14851afSqiuxishi 		 * Some platforms can assign the same pfn to multiple nodes - on
247f14851afSqiuxishi 		 * node0 as well as nodeN.  To avoid registering a pfn against
248f14851afSqiuxishi 		 * multiple nodes we check that this pfn does not already
2497e9f5eb0STang Chen 		 * reside in some other nodes.
250f14851afSqiuxishi 		 */
251f65e91dfSYang Shi 		if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
25204753278SYasunori Goto 			register_page_bootmem_info_section(pfn);
253f14851afSqiuxishi 	}
25404753278SYasunori Goto }
25546723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
25604753278SYasunori Goto 
2577ea62160SDan Williams static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
2587ea62160SDan Williams 		const char *reason)
2597ea62160SDan Williams {
2607ea62160SDan Williams 	/*
2617ea62160SDan Williams 	 * Disallow all operations smaller than a sub-section and only
2627ea62160SDan Williams 	 * allow operations smaller than a section for
2637ea62160SDan Williams 	 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
2647ea62160SDan Williams 	 * enforces a larger memory_block_size_bytes() granularity for
2657ea62160SDan Williams 	 * memory that will be marked online, so this check should only
2667ea62160SDan Williams 	 * fire for direct arch_{add,remove}_memory() users outside of
2677ea62160SDan Williams 	 * add_memory_resource().
2687ea62160SDan Williams 	 */
2697ea62160SDan Williams 	unsigned long min_align;
2707ea62160SDan Williams 
2717ea62160SDan Williams 	if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
2727ea62160SDan Williams 		min_align = PAGES_PER_SUBSECTION;
2737ea62160SDan Williams 	else
2747ea62160SDan Williams 		min_align = PAGES_PER_SECTION;
2757ea62160SDan Williams 	if (!IS_ALIGNED(pfn, min_align)
2767ea62160SDan Williams 			|| !IS_ALIGNED(nr_pages, min_align)) {
2777ea62160SDan Williams 		WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n",
2787ea62160SDan Williams 				reason, pfn, pfn + nr_pages - 1);
2797ea62160SDan Williams 		return -EINVAL;
2807ea62160SDan Williams 	}
2817ea62160SDan Williams 	return 0;
2827ea62160SDan Williams }
2837ea62160SDan Williams 
284dca4436dSAlastair D'Silva static int check_hotplug_memory_addressable(unsigned long pfn,
285dca4436dSAlastair D'Silva 					    unsigned long nr_pages)
286dca4436dSAlastair D'Silva {
287dca4436dSAlastair D'Silva 	const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1;
288dca4436dSAlastair D'Silva 
289dca4436dSAlastair D'Silva 	if (max_addr >> MAX_PHYSMEM_BITS) {
290dca4436dSAlastair D'Silva 		const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1;
291dca4436dSAlastair D'Silva 		WARN(1,
292dca4436dSAlastair D'Silva 		     "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
293dca4436dSAlastair D'Silva 		     (u64)PFN_PHYS(pfn), max_addr, max_allowed);
294dca4436dSAlastair D'Silva 		return -E2BIG;
295dca4436dSAlastair D'Silva 	}
296dca4436dSAlastair D'Silva 
297dca4436dSAlastair D'Silva 	return 0;
298dca4436dSAlastair D'Silva }
299dca4436dSAlastair D'Silva 
3004edd7cefSDavid Rientjes /*
3014edd7cefSDavid Rientjes  * Reasonably generic function for adding memory.  It is
3024edd7cefSDavid Rientjes  * expected that archs that support memory hotplug will
3034edd7cefSDavid Rientjes  * call this function after deciding the zone to which to
3044edd7cefSDavid Rientjes  * add the new pages.
3054edd7cefSDavid Rientjes  */
3067ea62160SDan Williams int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
307*f5637d3bSLogan Gunthorpe 		struct mhp_params *params)
3084edd7cefSDavid Rientjes {
3096cdd0b30SDavid Hildenbrand 	const unsigned long end_pfn = pfn + nr_pages;
3106cdd0b30SDavid Hildenbrand 	unsigned long cur_nr_pages;
3119a845030SDan Williams 	int err;
312*f5637d3bSLogan Gunthorpe 	struct vmem_altmap *altmap = params->altmap;
3134b94ffdcSDan Williams 
314dca4436dSAlastair D'Silva 	err = check_hotplug_memory_addressable(pfn, nr_pages);
315dca4436dSAlastair D'Silva 	if (err)
316dca4436dSAlastair D'Silva 		return err;
317dca4436dSAlastair D'Silva 
3184b94ffdcSDan Williams 	if (altmap) {
3194b94ffdcSDan Williams 		/*
3204b94ffdcSDan Williams 		 * Validate altmap is within bounds of the total request
3214b94ffdcSDan Williams 		 */
3227ea62160SDan Williams 		if (altmap->base_pfn != pfn
3234b94ffdcSDan Williams 				|| vmem_altmap_offset(altmap) > nr_pages) {
3244b94ffdcSDan Williams 			pr_warn_once("memory add fail, invalid altmap\n");
3257ea62160SDan Williams 			return -EINVAL;
3264b94ffdcSDan Williams 		}
3274b94ffdcSDan Williams 		altmap->alloc = 0;
3284b94ffdcSDan Williams 	}
3294b94ffdcSDan Williams 
3307ea62160SDan Williams 	err = check_pfn_span(pfn, nr_pages, "add");
3317ea62160SDan Williams 	if (err)
3327ea62160SDan Williams 		return err;
3337ea62160SDan Williams 
3346cdd0b30SDavid Hildenbrand 	for (; pfn < end_pfn; pfn += cur_nr_pages) {
3356cdd0b30SDavid Hildenbrand 		/* Select all remaining pages up to the next section boundary */
3366cdd0b30SDavid Hildenbrand 		cur_nr_pages = min(end_pfn - pfn,
3376cdd0b30SDavid Hildenbrand 				   SECTION_ALIGN_UP(pfn + 1) - pfn);
3386cdd0b30SDavid Hildenbrand 		err = sparse_add_section(nid, pfn, cur_nr_pages, altmap);
339ba72b4c8SDan Williams 		if (err)
340ba72b4c8SDan Williams 			break;
341f64ac5e6SMichal Hocko 		cond_resched();
3424edd7cefSDavid Rientjes 	}
343c435a390SZhu Guihua 	vmemmap_populate_print_last();
3444edd7cefSDavid Rientjes 	return err;
3454edd7cefSDavid Rientjes }
3464edd7cefSDavid Rientjes 
347815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
348d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
349815121d2SYasuaki Ishimatsu 				     unsigned long start_pfn,
350815121d2SYasuaki Ishimatsu 				     unsigned long end_pfn)
351815121d2SYasuaki Ishimatsu {
35249ba3c6bSDan Williams 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
3537ce700bfSDavid Hildenbrand 		if (unlikely(!pfn_to_online_page(start_pfn)))
354815121d2SYasuaki Ishimatsu 			continue;
355815121d2SYasuaki Ishimatsu 
356815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(start_pfn) != nid))
357815121d2SYasuaki Ishimatsu 			continue;
358815121d2SYasuaki Ishimatsu 
3599b05158fSDavid Hildenbrand 		if (zone != page_zone(pfn_to_page(start_pfn)))
360815121d2SYasuaki Ishimatsu 			continue;
361815121d2SYasuaki Ishimatsu 
362815121d2SYasuaki Ishimatsu 		return start_pfn;
363815121d2SYasuaki Ishimatsu 	}
364815121d2SYasuaki Ishimatsu 
365815121d2SYasuaki Ishimatsu 	return 0;
366815121d2SYasuaki Ishimatsu }
367815121d2SYasuaki Ishimatsu 
368815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
369d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
370815121d2SYasuaki Ishimatsu 				    unsigned long start_pfn,
371815121d2SYasuaki Ishimatsu 				    unsigned long end_pfn)
372815121d2SYasuaki Ishimatsu {
373815121d2SYasuaki Ishimatsu 	unsigned long pfn;
374815121d2SYasuaki Ishimatsu 
375815121d2SYasuaki Ishimatsu 	/* pfn is the end pfn of a memory section. */
376815121d2SYasuaki Ishimatsu 	pfn = end_pfn - 1;
37749ba3c6bSDan Williams 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
3787ce700bfSDavid Hildenbrand 		if (unlikely(!pfn_to_online_page(pfn)))
379815121d2SYasuaki Ishimatsu 			continue;
380815121d2SYasuaki Ishimatsu 
381815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(pfn) != nid))
382815121d2SYasuaki Ishimatsu 			continue;
383815121d2SYasuaki Ishimatsu 
3849b05158fSDavid Hildenbrand 		if (zone != page_zone(pfn_to_page(pfn)))
385815121d2SYasuaki Ishimatsu 			continue;
386815121d2SYasuaki Ishimatsu 
387815121d2SYasuaki Ishimatsu 		return pfn;
388815121d2SYasuaki Ishimatsu 	}
389815121d2SYasuaki Ishimatsu 
390815121d2SYasuaki Ishimatsu 	return 0;
391815121d2SYasuaki Ishimatsu }
392815121d2SYasuaki Ishimatsu 
393815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
394815121d2SYasuaki Ishimatsu 			     unsigned long end_pfn)
395815121d2SYasuaki Ishimatsu {
396815121d2SYasuaki Ishimatsu 	unsigned long pfn;
397815121d2SYasuaki Ishimatsu 	int nid = zone_to_nid(zone);
398815121d2SYasuaki Ishimatsu 
399815121d2SYasuaki Ishimatsu 	zone_span_writelock(zone);
4005d12071cSDavid Hildenbrand 	if (zone->zone_start_pfn == start_pfn) {
401815121d2SYasuaki Ishimatsu 		/*
402815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the zone, it need
403815121d2SYasuaki Ishimatsu 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
404815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
405815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
406815121d2SYasuaki Ishimatsu 		 */
407815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
4085d12071cSDavid Hildenbrand 						zone_end_pfn(zone));
409815121d2SYasuaki Ishimatsu 		if (pfn) {
4105d12071cSDavid Hildenbrand 			zone->spanned_pages = zone_end_pfn(zone) - pfn;
411815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = pfn;
412950b68d9SDavid Hildenbrand 		} else {
413950b68d9SDavid Hildenbrand 			zone->zone_start_pfn = 0;
414950b68d9SDavid Hildenbrand 			zone->spanned_pages = 0;
415815121d2SYasuaki Ishimatsu 		}
4165d12071cSDavid Hildenbrand 	} else if (zone_end_pfn(zone) == end_pfn) {
417815121d2SYasuaki Ishimatsu 		/*
418815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the zone, it need
419815121d2SYasuaki Ishimatsu 		 * shrink zone->spanned_pages.
420815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
421815121d2SYasuaki Ishimatsu 		 * shrinking zone.
422815121d2SYasuaki Ishimatsu 		 */
4235d12071cSDavid Hildenbrand 		pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn,
424815121d2SYasuaki Ishimatsu 					       start_pfn);
425815121d2SYasuaki Ishimatsu 		if (pfn)
4265d12071cSDavid Hildenbrand 			zone->spanned_pages = pfn - zone->zone_start_pfn + 1;
427950b68d9SDavid Hildenbrand 		else {
428815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = 0;
429815121d2SYasuaki Ishimatsu 			zone->spanned_pages = 0;
430950b68d9SDavid Hildenbrand 		}
431950b68d9SDavid Hildenbrand 	}
432815121d2SYasuaki Ishimatsu 	zone_span_writeunlock(zone);
433815121d2SYasuaki Ishimatsu }
434815121d2SYasuaki Ishimatsu 
43500d6c019SDavid Hildenbrand static void update_pgdat_span(struct pglist_data *pgdat)
436815121d2SYasuaki Ishimatsu {
43700d6c019SDavid Hildenbrand 	unsigned long node_start_pfn = 0, node_end_pfn = 0;
43800d6c019SDavid Hildenbrand 	struct zone *zone;
439815121d2SYasuaki Ishimatsu 
44000d6c019SDavid Hildenbrand 	for (zone = pgdat->node_zones;
44100d6c019SDavid Hildenbrand 	     zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
44200d6c019SDavid Hildenbrand 		unsigned long zone_end_pfn = zone->zone_start_pfn +
44300d6c019SDavid Hildenbrand 					     zone->spanned_pages;
44400d6c019SDavid Hildenbrand 
44500d6c019SDavid Hildenbrand 		/* No need to lock the zones, they can't change. */
446656d5711SDavid Hildenbrand 		if (!zone->spanned_pages)
447656d5711SDavid Hildenbrand 			continue;
448656d5711SDavid Hildenbrand 		if (!node_end_pfn) {
449656d5711SDavid Hildenbrand 			node_start_pfn = zone->zone_start_pfn;
450656d5711SDavid Hildenbrand 			node_end_pfn = zone_end_pfn;
451656d5711SDavid Hildenbrand 			continue;
452656d5711SDavid Hildenbrand 		}
453656d5711SDavid Hildenbrand 
45400d6c019SDavid Hildenbrand 		if (zone_end_pfn > node_end_pfn)
45500d6c019SDavid Hildenbrand 			node_end_pfn = zone_end_pfn;
45600d6c019SDavid Hildenbrand 		if (zone->zone_start_pfn < node_start_pfn)
45700d6c019SDavid Hildenbrand 			node_start_pfn = zone->zone_start_pfn;
458815121d2SYasuaki Ishimatsu 	}
459815121d2SYasuaki Ishimatsu 
46000d6c019SDavid Hildenbrand 	pgdat->node_start_pfn = node_start_pfn;
46100d6c019SDavid Hildenbrand 	pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
462815121d2SYasuaki Ishimatsu }
463815121d2SYasuaki Ishimatsu 
464feee6b29SDavid Hildenbrand void __ref remove_pfn_range_from_zone(struct zone *zone,
465feee6b29SDavid Hildenbrand 				      unsigned long start_pfn,
4667ea62160SDan Williams 				      unsigned long nr_pages)
467815121d2SYasuaki Ishimatsu {
468815121d2SYasuaki Ishimatsu 	struct pglist_data *pgdat = zone->zone_pgdat;
469815121d2SYasuaki Ishimatsu 	unsigned long flags;
470815121d2SYasuaki Ishimatsu 
471d33695b1SDavid Hildenbrand 	/* Poison struct pages because they are now uninitialized again. */
472d33695b1SDavid Hildenbrand 	page_init_poison(pfn_to_page(start_pfn), sizeof(struct page) * nr_pages);
473d33695b1SDavid Hildenbrand 
4747ce700bfSDavid Hildenbrand #ifdef CONFIG_ZONE_DEVICE
4757ce700bfSDavid Hildenbrand 	/*
4767ce700bfSDavid Hildenbrand 	 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
4777ce700bfSDavid Hildenbrand 	 * we will not try to shrink the zones - which is okay as
4787ce700bfSDavid Hildenbrand 	 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
4797ce700bfSDavid Hildenbrand 	 */
4807ce700bfSDavid Hildenbrand 	if (zone_idx(zone) == ZONE_DEVICE)
4817ce700bfSDavid Hildenbrand 		return;
4827ce700bfSDavid Hildenbrand #endif
4837ce700bfSDavid Hildenbrand 
484feee6b29SDavid Hildenbrand 	clear_zone_contiguous(zone);
485feee6b29SDavid Hildenbrand 
486815121d2SYasuaki Ishimatsu 	pgdat_resize_lock(zone->zone_pgdat, &flags);
487815121d2SYasuaki Ishimatsu 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
48800d6c019SDavid Hildenbrand 	update_pgdat_span(pgdat);
489815121d2SYasuaki Ishimatsu 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
490feee6b29SDavid Hildenbrand 
491feee6b29SDavid Hildenbrand 	set_zone_contiguous(zone);
492815121d2SYasuaki Ishimatsu }
493815121d2SYasuaki Ishimatsu 
494feee6b29SDavid Hildenbrand static void __remove_section(unsigned long pfn, unsigned long nr_pages,
495feee6b29SDavid Hildenbrand 			     unsigned long map_offset,
4969d1d887dSDavid Hildenbrand 			     struct vmem_altmap *altmap)
497ea01ea93SBadari Pulavarty {
49810404901Schenqiwu 	struct mem_section *ms = __pfn_to_section(pfn);
499ea01ea93SBadari Pulavarty 
5009d1d887dSDavid Hildenbrand 	if (WARN_ON_ONCE(!valid_section(ms)))
5019d1d887dSDavid Hildenbrand 		return;
502ea01ea93SBadari Pulavarty 
503ba72b4c8SDan Williams 	sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
504ea01ea93SBadari Pulavarty }
505ea01ea93SBadari Pulavarty 
506ea01ea93SBadari Pulavarty /**
507feee6b29SDavid Hildenbrand  * __remove_pages() - remove sections of pages
5087ea62160SDan Williams  * @pfn: starting pageframe (must be aligned to start of a section)
509ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
510e8b098fcSMike Rapoport  * @altmap: alternative device page map or %NULL if default memmap is used
511ea01ea93SBadari Pulavarty  *
512ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
513ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
514ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
515ea01ea93SBadari Pulavarty  * calling offline_pages().
516ea01ea93SBadari Pulavarty  */
517feee6b29SDavid Hildenbrand void __remove_pages(unsigned long pfn, unsigned long nr_pages,
518feee6b29SDavid Hildenbrand 		    struct vmem_altmap *altmap)
519ea01ea93SBadari Pulavarty {
52052fb87c8SDavid Hildenbrand 	const unsigned long end_pfn = pfn + nr_pages;
52152fb87c8SDavid Hildenbrand 	unsigned long cur_nr_pages;
5224b94ffdcSDan Williams 	unsigned long map_offset = 0;
5234b94ffdcSDan Williams 
5244b94ffdcSDan Williams 	map_offset = vmem_altmap_offset(altmap);
525ea01ea93SBadari Pulavarty 
5267ea62160SDan Williams 	if (check_pfn_span(pfn, nr_pages, "remove"))
5277ea62160SDan Williams 		return;
528ea01ea93SBadari Pulavarty 
52952fb87c8SDavid Hildenbrand 	for (; pfn < end_pfn; pfn += cur_nr_pages) {
530dd33ad7bSMichal Hocko 		cond_resched();
53152fb87c8SDavid Hildenbrand 		/* Select all remaining pages up to the next section boundary */
532a11b9419SDavid Hildenbrand 		cur_nr_pages = min(end_pfn - pfn,
533a11b9419SDavid Hildenbrand 				   SECTION_ALIGN_UP(pfn + 1) - pfn);
53452fb87c8SDavid Hildenbrand 		__remove_section(pfn, cur_nr_pages, map_offset, altmap);
5354b94ffdcSDan Williams 		map_offset = 0;
536ea01ea93SBadari Pulavarty 	}
537ea01ea93SBadari Pulavarty }
538ea01ea93SBadari Pulavarty 
5399d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback)
5409d0ad8caSDaniel Kiper {
5419d0ad8caSDaniel Kiper 	int rc = -EINVAL;
5429d0ad8caSDaniel Kiper 
543bfc8c901SVladimir Davydov 	get_online_mems();
544bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
5459d0ad8caSDaniel Kiper 
5469d0ad8caSDaniel Kiper 	if (online_page_callback == generic_online_page) {
5479d0ad8caSDaniel Kiper 		online_page_callback = callback;
5489d0ad8caSDaniel Kiper 		rc = 0;
5499d0ad8caSDaniel Kiper 	}
5509d0ad8caSDaniel Kiper 
551bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
552bfc8c901SVladimir Davydov 	put_online_mems();
5539d0ad8caSDaniel Kiper 
5549d0ad8caSDaniel Kiper 	return rc;
5559d0ad8caSDaniel Kiper }
5569d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback);
5579d0ad8caSDaniel Kiper 
5589d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback)
5599d0ad8caSDaniel Kiper {
5609d0ad8caSDaniel Kiper 	int rc = -EINVAL;
5619d0ad8caSDaniel Kiper 
562bfc8c901SVladimir Davydov 	get_online_mems();
563bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
5649d0ad8caSDaniel Kiper 
5659d0ad8caSDaniel Kiper 	if (online_page_callback == callback) {
5669d0ad8caSDaniel Kiper 		online_page_callback = generic_online_page;
5679d0ad8caSDaniel Kiper 		rc = 0;
5689d0ad8caSDaniel Kiper 	}
5699d0ad8caSDaniel Kiper 
570bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
571bfc8c901SVladimir Davydov 	put_online_mems();
5729d0ad8caSDaniel Kiper 
5739d0ad8caSDaniel Kiper 	return rc;
5749d0ad8caSDaniel Kiper }
5759d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback);
5769d0ad8caSDaniel Kiper 
57718db1491SDavid Hildenbrand void generic_online_page(struct page *page, unsigned int order)
5789d0ad8caSDaniel Kiper {
579c87cbc1fSVlastimil Babka 	/*
580c87cbc1fSVlastimil Babka 	 * Freeing the page with debug_pagealloc enabled will try to unmap it,
581c87cbc1fSVlastimil Babka 	 * so we should map it first. This is better than introducing a special
582c87cbc1fSVlastimil Babka 	 * case in page freeing fast path.
583c87cbc1fSVlastimil Babka 	 */
584c87cbc1fSVlastimil Babka 	if (debug_pagealloc_enabled_static())
585cd02cf1aSQian Cai 		kernel_map_pages(page, 1 << order, 1);
586a9cd410aSArun KS 	__free_pages_core(page, order);
587a9cd410aSArun KS 	totalram_pages_add(1UL << order);
588a9cd410aSArun KS #ifdef CONFIG_HIGHMEM
589a9cd410aSArun KS 	if (PageHighMem(page))
590a9cd410aSArun KS 		totalhigh_pages_add(1UL << order);
591a9cd410aSArun KS #endif
592a9cd410aSArun KS }
59318db1491SDavid Hildenbrand EXPORT_SYMBOL_GPL(generic_online_page);
594a9cd410aSArun KS 
59575884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
59675884fb1SKAMEZAWA Hiroyuki 			void *arg)
5973947be19SDave Hansen {
598b2c2ab20SDavid Hildenbrand 	const unsigned long end_pfn = start_pfn + nr_pages;
599b2c2ab20SDavid Hildenbrand 	unsigned long pfn;
600b2c2ab20SDavid Hildenbrand 	int order;
6012d070eabSMichal Hocko 
602b2c2ab20SDavid Hildenbrand 	/*
603b2c2ab20SDavid Hildenbrand 	 * Online the pages. The callback might decide to keep some pages
604b2c2ab20SDavid Hildenbrand 	 * PG_reserved (to add them to the buddy later), but we still account
605b2c2ab20SDavid Hildenbrand 	 * them as being online/belonging to this zone ("present").
606b2c2ab20SDavid Hildenbrand 	 */
607b2c2ab20SDavid Hildenbrand 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1ul << order) {
608b2c2ab20SDavid Hildenbrand 		order = min(MAX_ORDER - 1, get_order(PFN_PHYS(end_pfn - pfn)));
609bd02cc01SDavid Hildenbrand 		/* __free_pages_core() wants pfns to be aligned to the order */
610bd02cc01SDavid Hildenbrand 		if (WARN_ON_ONCE(!IS_ALIGNED(pfn, 1ul << order)))
611bd02cc01SDavid Hildenbrand 			order = 0;
612b2c2ab20SDavid Hildenbrand 		(*online_page_callback)(pfn_to_page(pfn), order);
613b2c2ab20SDavid Hildenbrand 	}
6142d070eabSMichal Hocko 
615b2c2ab20SDavid Hildenbrand 	/* mark all involved sections as online */
616b2c2ab20SDavid Hildenbrand 	online_mem_sections(start_pfn, end_pfn);
617b2c2ab20SDavid Hildenbrand 
618b2c2ab20SDavid Hildenbrand 	*(unsigned long *)arg += nr_pages;
61975884fb1SKAMEZAWA Hiroyuki 	return 0;
62075884fb1SKAMEZAWA Hiroyuki }
62175884fb1SKAMEZAWA Hiroyuki 
622d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */
623d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages,
624d9713679SLai Jiangshan 	struct zone *zone, struct memory_notify *arg)
625d9713679SLai Jiangshan {
626d9713679SLai Jiangshan 	int nid = zone_to_nid(zone);
627d9713679SLai Jiangshan 
62898fa15f3SAnshuman Khandual 	arg->status_change_nid = NUMA_NO_NODE;
62998fa15f3SAnshuman Khandual 	arg->status_change_nid_normal = NUMA_NO_NODE;
63098fa15f3SAnshuman Khandual 	arg->status_change_nid_high = NUMA_NO_NODE;
6316715ddf9SLai Jiangshan 
6326715ddf9SLai Jiangshan 	if (!node_state(nid, N_MEMORY))
633d9713679SLai Jiangshan 		arg->status_change_nid = nid;
6348efe33f4SOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
6358efe33f4SOscar Salvador 		arg->status_change_nid_normal = nid;
6368efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM
637d3ba3ae1SBaoquan He 	if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
6388efe33f4SOscar Salvador 		arg->status_change_nid_high = nid;
6398efe33f4SOscar Salvador #endif
640d9713679SLai Jiangshan }
641d9713679SLai Jiangshan 
642d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg)
643d9713679SLai Jiangshan {
644d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
645d9713679SLai Jiangshan 		node_set_state(node, N_NORMAL_MEMORY);
646d9713679SLai Jiangshan 
6476715ddf9SLai Jiangshan 	if (arg->status_change_nid_high >= 0)
648d9713679SLai Jiangshan 		node_set_state(node, N_HIGH_MEMORY);
6496715ddf9SLai Jiangshan 
65083d83612SOscar Salvador 	if (arg->status_change_nid >= 0)
6516715ddf9SLai Jiangshan 		node_set_state(node, N_MEMORY);
652d9713679SLai Jiangshan }
653d9713679SLai Jiangshan 
654f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
655f1dd2cd1SMichal Hocko 		unsigned long nr_pages)
656f1dd2cd1SMichal Hocko {
657f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = zone_end_pfn(zone);
658f1dd2cd1SMichal Hocko 
659f1dd2cd1SMichal Hocko 	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
660f1dd2cd1SMichal Hocko 		zone->zone_start_pfn = start_pfn;
661f1dd2cd1SMichal Hocko 
662f1dd2cd1SMichal Hocko 	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
663f1dd2cd1SMichal Hocko }
664f1dd2cd1SMichal Hocko 
665f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
666f1dd2cd1SMichal Hocko                                      unsigned long nr_pages)
667f1dd2cd1SMichal Hocko {
668f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
669f1dd2cd1SMichal Hocko 
670f1dd2cd1SMichal Hocko 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
671f1dd2cd1SMichal Hocko 		pgdat->node_start_pfn = start_pfn;
672f1dd2cd1SMichal Hocko 
673f1dd2cd1SMichal Hocko 	pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
674f1dd2cd1SMichal Hocko 
6753fccb74cSDavid Hildenbrand }
6763fccb74cSDavid Hildenbrand /*
6773fccb74cSDavid Hildenbrand  * Associate the pfn range with the given zone, initializing the memmaps
6783fccb74cSDavid Hildenbrand  * and resizing the pgdat/zone data to span the added pages. After this
6793fccb74cSDavid Hildenbrand  * call, all affected pages are PG_reserved.
6803fccb74cSDavid Hildenbrand  */
681a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
682a99583e7SChristoph Hellwig 		unsigned long nr_pages, struct vmem_altmap *altmap)
683f1dd2cd1SMichal Hocko {
684f1dd2cd1SMichal Hocko 	struct pglist_data *pgdat = zone->zone_pgdat;
685f1dd2cd1SMichal Hocko 	int nid = pgdat->node_id;
686f1dd2cd1SMichal Hocko 	unsigned long flags;
687f1dd2cd1SMichal Hocko 
688f1dd2cd1SMichal Hocko 	clear_zone_contiguous(zone);
689f1dd2cd1SMichal Hocko 
690f1dd2cd1SMichal Hocko 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
691f1dd2cd1SMichal Hocko 	pgdat_resize_lock(pgdat, &flags);
692f1dd2cd1SMichal Hocko 	zone_span_writelock(zone);
693fa004ab7SWei Yang 	if (zone_is_empty(zone))
694fa004ab7SWei Yang 		init_currently_empty_zone(zone, start_pfn, nr_pages);
695f1dd2cd1SMichal Hocko 	resize_zone_range(zone, start_pfn, nr_pages);
696f1dd2cd1SMichal Hocko 	zone_span_writeunlock(zone);
697f1dd2cd1SMichal Hocko 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
698f1dd2cd1SMichal Hocko 	pgdat_resize_unlock(pgdat, &flags);
699f1dd2cd1SMichal Hocko 
700f1dd2cd1SMichal Hocko 	/*
701f1dd2cd1SMichal Hocko 	 * TODO now we have a visible range of pages which are not associated
702f1dd2cd1SMichal Hocko 	 * with their zone properly. Not nice but set_pfnblock_flags_mask
703f1dd2cd1SMichal Hocko 	 * expects the zone spans the pfn range. All the pages in the range
704f1dd2cd1SMichal Hocko 	 * are reserved so nobody should be touching them so we should be safe
705f1dd2cd1SMichal Hocko 	 */
706a99583e7SChristoph Hellwig 	memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
707a99583e7SChristoph Hellwig 			MEMMAP_HOTPLUG, altmap);
708f1dd2cd1SMichal Hocko 
709f1dd2cd1SMichal Hocko 	set_zone_contiguous(zone);
710f1dd2cd1SMichal Hocko }
711f1dd2cd1SMichal Hocko 
712f1dd2cd1SMichal Hocko /*
713c246a213SMichal Hocko  * Returns a default kernel memory zone for the given pfn range.
714c246a213SMichal Hocko  * If no kernel zone covers this pfn range it will automatically go
715c246a213SMichal Hocko  * to the ZONE_NORMAL.
716c246a213SMichal Hocko  */
717c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
718c246a213SMichal Hocko 		unsigned long nr_pages)
719c246a213SMichal Hocko {
720c246a213SMichal Hocko 	struct pglist_data *pgdat = NODE_DATA(nid);
721c246a213SMichal Hocko 	int zid;
722c246a213SMichal Hocko 
723c246a213SMichal Hocko 	for (zid = 0; zid <= ZONE_NORMAL; zid++) {
724c246a213SMichal Hocko 		struct zone *zone = &pgdat->node_zones[zid];
725c246a213SMichal Hocko 
726c246a213SMichal Hocko 		if (zone_intersects(zone, start_pfn, nr_pages))
727c246a213SMichal Hocko 			return zone;
728c246a213SMichal Hocko 	}
729c246a213SMichal Hocko 
730c246a213SMichal Hocko 	return &pgdat->node_zones[ZONE_NORMAL];
731c246a213SMichal Hocko }
732c246a213SMichal Hocko 
733c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
734c6f03e29SMichal Hocko 		unsigned long nr_pages)
735e5e68930SMichal Hocko {
736c6f03e29SMichal Hocko 	struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
737c6f03e29SMichal Hocko 			nr_pages);
738c6f03e29SMichal Hocko 	struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
739c6f03e29SMichal Hocko 	bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
740c6f03e29SMichal Hocko 	bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
741e5e68930SMichal Hocko 
742e5e68930SMichal Hocko 	/*
743c6f03e29SMichal Hocko 	 * We inherit the existing zone in a simple case where zones do not
744c6f03e29SMichal Hocko 	 * overlap in the given range
745e5e68930SMichal Hocko 	 */
746c6f03e29SMichal Hocko 	if (in_kernel ^ in_movable)
747c6f03e29SMichal Hocko 		return (in_kernel) ? kernel_zone : movable_zone;
748e5e68930SMichal Hocko 
749c6f03e29SMichal Hocko 	/*
750c6f03e29SMichal Hocko 	 * If the range doesn't belong to any zone or two zones overlap in the
751c6f03e29SMichal Hocko 	 * given range then we use movable zone only if movable_node is
752c6f03e29SMichal Hocko 	 * enabled because we always online to a kernel zone by default.
753c6f03e29SMichal Hocko 	 */
754c6f03e29SMichal Hocko 	return movable_node_enabled ? movable_zone : kernel_zone;
7559f123ab5SMichal Hocko }
7569f123ab5SMichal Hocko 
757e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
758e5e68930SMichal Hocko 		unsigned long nr_pages)
759f1dd2cd1SMichal Hocko {
760c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_KERNEL)
761c6f03e29SMichal Hocko 		return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
762f1dd2cd1SMichal Hocko 
763c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_MOVABLE)
764c6f03e29SMichal Hocko 		return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
765f1dd2cd1SMichal Hocko 
766c6f03e29SMichal Hocko 	return default_zone_for_pfn(nid, start_pfn, nr_pages);
767e5e68930SMichal Hocko }
768e5e68930SMichal Hocko 
769bd5c2344SDavid Hildenbrand int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
770bd5c2344SDavid Hildenbrand 		       int online_type, int nid)
77175884fb1SKAMEZAWA Hiroyuki {
772aa47228aSCody P Schafer 	unsigned long flags;
7733947be19SDave Hansen 	unsigned long onlined_pages = 0;
7743947be19SDave Hansen 	struct zone *zone;
7756811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
7767b78d335SYasunori Goto 	int ret;
7777b78d335SYasunori Goto 	struct memory_notify arg;
7783947be19SDave Hansen 
779381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
780381eab4aSDavid Hildenbrand 
781f1dd2cd1SMichal Hocko 	/* associate pfn range with the zone */
7823fccb74cSDavid Hildenbrand 	zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages);
7833fccb74cSDavid Hildenbrand 	move_pfn_range_to_zone(zone, pfn, nr_pages, NULL);
784511c2abaSLai Jiangshan 
7857b78d335SYasunori Goto 	arg.start_pfn = pfn;
7867b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
787d9713679SLai Jiangshan 	node_states_check_changes_online(nr_pages, zone, &arg);
7887b78d335SYasunori Goto 
7897b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
7907b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
791e33e33b4SChen Yucong 	if (ret)
792e33e33b4SChen Yucong 		goto failed_addition;
793e33e33b4SChen Yucong 
7943947be19SDave Hansen 	/*
7956811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
7966811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
7976811378eSYasunori Goto 	 * So, zonelist must be updated after online.
7986811378eSYasunori Goto 	 */
7996dcd73d7SWen Congyang 	if (!populated_zone(zone)) {
8006811378eSYasunori Goto 		need_zonelists_rebuild = 1;
80172675e13SMichal Hocko 		setup_zone_pageset(zone);
8026dcd73d7SWen Congyang 	}
8036811378eSYasunori Goto 
804908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
80575884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
806fd8a4221SGeoff Levand 	if (ret) {
807ca9a46f8SDavid Hildenbrand 		/* not a single memory resource was applicable */
8086dcd73d7SWen Congyang 		if (need_zonelists_rebuild)
8096dcd73d7SWen Congyang 			zone_pcp_reset(zone);
810e33e33b4SChen Yucong 		goto failed_addition;
811fd8a4221SGeoff Levand 	}
812fd8a4221SGeoff Levand 
8133947be19SDave Hansen 	zone->present_pages += onlined_pages;
814aa47228aSCody P Schafer 
815aa47228aSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
816f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
817aa47228aSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
818aa47228aSCody P Schafer 
819e900a918SDan Williams 	shuffle_zone(zone);
820e900a918SDan Williams 
821e888ca35SVlastimil Babka 	node_states_set_node(nid, &arg);
8221f522509SHaicheng Li 	if (need_zonelists_rebuild)
82372675e13SMichal Hocko 		build_all_zonelists(NULL);
8241f522509SHaicheng Li 	else
825112067f0SShaohua Li 		zone_pcp_update(zone);
8261f522509SHaicheng Li 
8271b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
8281b79acc9SKOSAKI Motohiro 
829e888ca35SVlastimil Babka 	kswapd_run(nid);
830698b1b30SVlastimil Babka 	kcompactd_run(nid);
83161b13993SDave Hansen 
8325a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
8332f7f24ecSKent Liu 
8342d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
8357b78d335SYasunori Goto 
8367b78d335SYasunori Goto 	memory_notify(MEM_ONLINE, &arg);
837381eab4aSDavid Hildenbrand 	mem_hotplug_done();
83830467e0bSDavid Rientjes 	return 0;
839e33e33b4SChen Yucong 
840e33e33b4SChen Yucong failed_addition:
841e33e33b4SChen Yucong 	pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
842e33e33b4SChen Yucong 		 (unsigned long long) pfn << PAGE_SHIFT,
843e33e33b4SChen Yucong 		 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
844e33e33b4SChen Yucong 	memory_notify(MEM_CANCEL_ONLINE, &arg);
845feee6b29SDavid Hildenbrand 	remove_pfn_range_from_zone(zone, pfn, nr_pages);
846381eab4aSDavid Hildenbrand 	mem_hotplug_done();
847e33e33b4SChen Yucong 	return ret;
8483947be19SDave Hansen }
84953947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
850bc02af93SYasunori Goto 
8510bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat)
8520bd85420STang Chen {
8530bd85420STang Chen 	struct zone *z;
8540bd85420STang Chen 
8550bd85420STang Chen 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
8560bd85420STang Chen 		z->present_pages = 0;
8570bd85420STang Chen 
8580bd85420STang Chen 	pgdat->node_present_pages = 0;
8590bd85420STang Chen }
8600bd85420STang Chen 
861e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
862e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
8639af3c2deSYasunori Goto {
8649af3c2deSYasunori Goto 	struct pglist_data *pgdat;
865c8e861a5SFabian Frederick 	unsigned long start_pfn = PFN_DOWN(start);
8669af3c2deSYasunori Goto 
867a1e565aaSTang Chen 	pgdat = NODE_DATA(nid);
868a1e565aaSTang Chen 	if (!pgdat) {
8699af3c2deSYasunori Goto 		pgdat = arch_alloc_nodedata(nid);
8709af3c2deSYasunori Goto 		if (!pgdat)
8719af3c2deSYasunori Goto 			return NULL;
8729af3c2deSYasunori Goto 
87333fce011SWei Yang 		pgdat->per_cpu_nodestats =
87433fce011SWei Yang 			alloc_percpu(struct per_cpu_nodestat);
8759af3c2deSYasunori Goto 		arch_refresh_nodedata(nid, pgdat);
876b0dc3a34SGu Zheng 	} else {
87733fce011SWei Yang 		int cpu;
878e716f2ebSMel Gorman 		/*
879e716f2ebSMel Gorman 		 * Reset the nr_zones, order and classzone_idx before reuse.
880e716f2ebSMel Gorman 		 * Note that kswapd will init kswapd_classzone_idx properly
881e716f2ebSMel Gorman 		 * when it starts in the near future.
882e716f2ebSMel Gorman 		 */
883b0dc3a34SGu Zheng 		pgdat->nr_zones = 0;
88438087d9bSMel Gorman 		pgdat->kswapd_order = 0;
88538087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = 0;
88633fce011SWei Yang 		for_each_online_cpu(cpu) {
88733fce011SWei Yang 			struct per_cpu_nodestat *p;
88833fce011SWei Yang 
88933fce011SWei Yang 			p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
89033fce011SWei Yang 			memset(p, 0, sizeof(*p));
89133fce011SWei Yang 		}
892a1e565aaSTang Chen 	}
8939af3c2deSYasunori Goto 
8949af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
8959af3c2deSYasunori Goto 
89603e85f9dSOscar Salvador 	pgdat->node_id = nid;
89703e85f9dSOscar Salvador 	pgdat->node_start_pfn = start_pfn;
89803e85f9dSOscar Salvador 
8999af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
90003e85f9dSOscar Salvador 	free_area_init_core_hotplug(nid);
9019af3c2deSYasunori Goto 
902959ecc48SKAMEZAWA Hiroyuki 	/*
903959ecc48SKAMEZAWA Hiroyuki 	 * The node we allocated has no zone fallback lists. For avoiding
904959ecc48SKAMEZAWA Hiroyuki 	 * to access not-initialized zonelist, build here.
905959ecc48SKAMEZAWA Hiroyuki 	 */
90672675e13SMichal Hocko 	build_all_zonelists(pgdat);
907959ecc48SKAMEZAWA Hiroyuki 
908f784a3f1STang Chen 	/*
9090bd85420STang Chen 	 * When memory is hot-added, all the memory is in offline state. So
9100bd85420STang Chen 	 * clear all zones' present_pages because they will be updated in
9110bd85420STang Chen 	 * online_pages() and offline_pages().
9120bd85420STang Chen 	 */
91303e85f9dSOscar Salvador 	reset_node_managed_pages(pgdat);
9140bd85420STang Chen 	reset_node_present_pages(pgdat);
9150bd85420STang Chen 
9169af3c2deSYasunori Goto 	return pgdat;
9179af3c2deSYasunori Goto }
9189af3c2deSYasunori Goto 
919b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid)
9209af3c2deSYasunori Goto {
921b9ff0360SOscar Salvador 	pg_data_t *pgdat = NODE_DATA(nid);
922b9ff0360SOscar Salvador 
9239af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
9245830169fSReza Arbab 	free_percpu(pgdat->per_cpu_nodestats);
9259af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
9269af3c2deSYasunori Goto }
9279af3c2deSYasunori Goto 
9280a547039SKAMEZAWA Hiroyuki 
92901b0f197SToshi Kani /**
93001b0f197SToshi Kani  * try_online_node - online a node if offlined
931e8b098fcSMike Rapoport  * @nid: the node ID
932b9ff0360SOscar Salvador  * @start: start addr of the node
933b9ff0360SOscar Salvador  * @set_node_online: Whether we want to online the node
934cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
935b9ff0360SOscar Salvador  *
936b9ff0360SOscar Salvador  * Returns:
937b9ff0360SOscar Salvador  * 1 -> a new node has been allocated
938b9ff0360SOscar Salvador  * 0 -> the node is already online
939b9ff0360SOscar Salvador  * -ENOMEM -> the node could not be allocated
940cf23422bSminskey guo  */
941b9ff0360SOscar Salvador static int __try_online_node(int nid, u64 start, bool set_node_online)
942cf23422bSminskey guo {
943cf23422bSminskey guo 	pg_data_t *pgdat;
944b9ff0360SOscar Salvador 	int ret = 1;
945cf23422bSminskey guo 
94601b0f197SToshi Kani 	if (node_online(nid))
94701b0f197SToshi Kani 		return 0;
94801b0f197SToshi Kani 
949b9ff0360SOscar Salvador 	pgdat = hotadd_new_pgdat(nid, start);
9507553e8f2SDavid Rientjes 	if (!pgdat) {
95101b0f197SToshi Kani 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
952cf23422bSminskey guo 		ret = -ENOMEM;
953cf23422bSminskey guo 		goto out;
954cf23422bSminskey guo 	}
955b9ff0360SOscar Salvador 
956b9ff0360SOscar Salvador 	if (set_node_online) {
957cf23422bSminskey guo 		node_set_online(nid);
958cf23422bSminskey guo 		ret = register_one_node(nid);
959cf23422bSminskey guo 		BUG_ON(ret);
960b9ff0360SOscar Salvador 	}
961cf23422bSminskey guo out:
962b9ff0360SOscar Salvador 	return ret;
963b9ff0360SOscar Salvador }
964b9ff0360SOscar Salvador 
965b9ff0360SOscar Salvador /*
966b9ff0360SOscar Salvador  * Users of this function always want to online/register the node
967b9ff0360SOscar Salvador  */
968b9ff0360SOscar Salvador int try_online_node(int nid)
969b9ff0360SOscar Salvador {
970b9ff0360SOscar Salvador 	int ret;
971b9ff0360SOscar Salvador 
972b9ff0360SOscar Salvador 	mem_hotplug_begin();
973b9ff0360SOscar Salvador 	ret =  __try_online_node(nid, 0, true);
974bfc8c901SVladimir Davydov 	mem_hotplug_done();
975cf23422bSminskey guo 	return ret;
976cf23422bSminskey guo }
977cf23422bSminskey guo 
97827356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size)
97927356f54SToshi Kani {
980ba325585SPavel Tatashin 	/* memory range must be block size aligned */
981cec3ebd0SDavid Hildenbrand 	if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
982cec3ebd0SDavid Hildenbrand 	    !IS_ALIGNED(size, memory_block_size_bytes())) {
983ba325585SPavel Tatashin 		pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
984cec3ebd0SDavid Hildenbrand 		       memory_block_size_bytes(), start, size);
98527356f54SToshi Kani 		return -EINVAL;
98627356f54SToshi Kani 	}
98727356f54SToshi Kani 
98827356f54SToshi Kani 	return 0;
98927356f54SToshi Kani }
99027356f54SToshi Kani 
99131bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg)
99231bc3858SVitaly Kuznetsov {
993862919e5SDavid Hildenbrand 	mem->online_type = memhp_default_online_type;
994dc18d706SNathan Fontenot 	return device_online(&mem->dev);
99531bc3858SVitaly Kuznetsov }
99631bc3858SVitaly Kuznetsov 
9978df1d0e4SDavid Hildenbrand /*
9988df1d0e4SDavid Hildenbrand  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
9998df1d0e4SDavid Hildenbrand  * and online/offline operations (triggered e.g. by sysfs).
10008df1d0e4SDavid Hildenbrand  *
10018df1d0e4SDavid Hildenbrand  * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
10028df1d0e4SDavid Hildenbrand  */
1003f29d8e9cSDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res)
1004bc02af93SYasunori Goto {
1005*f5637d3bSLogan Gunthorpe 	struct mhp_params params = {};
100662cedb9fSDavid Vrabel 	u64 start, size;
1007b9ff0360SOscar Salvador 	bool new_node = false;
1008bc02af93SYasunori Goto 	int ret;
1009bc02af93SYasunori Goto 
101062cedb9fSDavid Vrabel 	start = res->start;
101162cedb9fSDavid Vrabel 	size = resource_size(res);
101262cedb9fSDavid Vrabel 
101327356f54SToshi Kani 	ret = check_hotplug_memory_range(start, size);
101427356f54SToshi Kani 	if (ret)
101527356f54SToshi Kani 		return ret;
101627356f54SToshi Kani 
1017bfc8c901SVladimir Davydov 	mem_hotplug_begin();
1018ac13c462SNathan Zimmer 
10197f36e3e5STang Chen 	/*
10207f36e3e5STang Chen 	 * Add new range to memblock so that when hotadd_new_pgdat() is called
10217f36e3e5STang Chen 	 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
10227f36e3e5STang Chen 	 * this new range and calculate total pages correctly.  The range will
10237f36e3e5STang Chen 	 * be removed at hot-remove time.
10247f36e3e5STang Chen 	 */
10257f36e3e5STang Chen 	memblock_add_node(start, size, nid);
10267f36e3e5STang Chen 
1027b9ff0360SOscar Salvador 	ret = __try_online_node(nid, start, false);
1028b9ff0360SOscar Salvador 	if (ret < 0)
102941b9e2d7SWen Congyang 		goto error;
1030b9ff0360SOscar Salvador 	new_node = ret;
10319af3c2deSYasunori Goto 
1032bc02af93SYasunori Goto 	/* call arch's memory hotadd */
1033*f5637d3bSLogan Gunthorpe 	ret = arch_add_memory(nid, start, size, &params);
10349af3c2deSYasunori Goto 	if (ret < 0)
10359af3c2deSYasunori Goto 		goto error;
10369af3c2deSYasunori Goto 
1037db051a0dSDavid Hildenbrand 	/* create memory block devices after memory was added */
1038db051a0dSDavid Hildenbrand 	ret = create_memory_block_devices(start, size);
1039db051a0dSDavid Hildenbrand 	if (ret) {
1040db051a0dSDavid Hildenbrand 		arch_remove_memory(nid, start, size, NULL);
1041db051a0dSDavid Hildenbrand 		goto error;
1042db051a0dSDavid Hildenbrand 	}
1043db051a0dSDavid Hildenbrand 
1044a1e565aaSTang Chen 	if (new_node) {
1045d5b6f6a3SOscar Salvador 		/* If sysfs file of new node can't be created, cpu on the node
10460fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
10470fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
1048d5b6f6a3SOscar Salvador 		 * We online node here. We can't roll back from here.
10490fc44159SYasunori Goto 		 */
1050d5b6f6a3SOscar Salvador 		node_set_online(nid);
1051d5b6f6a3SOscar Salvador 		ret = __register_one_node(nid);
10520fc44159SYasunori Goto 		BUG_ON(ret);
10530fc44159SYasunori Goto 	}
10540fc44159SYasunori Goto 
1055d5b6f6a3SOscar Salvador 	/* link memory sections under this node.*/
10564fbce633SOscar Salvador 	ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
1057d5b6f6a3SOscar Salvador 	BUG_ON(ret);
1058d5b6f6a3SOscar Salvador 
1059d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
1060d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
1061d96ae530Sakpm@linux-foundation.org 
1062381eab4aSDavid Hildenbrand 	/* device_online() will take the lock when calling online_pages() */
1063381eab4aSDavid Hildenbrand 	mem_hotplug_done();
1064381eab4aSDavid Hildenbrand 
106531bc3858SVitaly Kuznetsov 	/* online pages if requested */
1066862919e5SDavid Hildenbrand 	if (memhp_default_online_type != MMOP_OFFLINE)
1067fbcf73ceSDavid Hildenbrand 		walk_memory_blocks(start, size, NULL, online_memory_block);
106831bc3858SVitaly Kuznetsov 
1069381eab4aSDavid Hildenbrand 	return ret;
10709af3c2deSYasunori Goto error:
10719af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
1072b9ff0360SOscar Salvador 	if (new_node)
1073b9ff0360SOscar Salvador 		rollback_node_hotadd(nid);
10747f36e3e5STang Chen 	memblock_remove(start, size);
1075bfc8c901SVladimir Davydov 	mem_hotplug_done();
1076bc02af93SYasunori Goto 	return ret;
1077bc02af93SYasunori Goto }
107862cedb9fSDavid Vrabel 
10798df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */
10808df1d0e4SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size)
108162cedb9fSDavid Vrabel {
108262cedb9fSDavid Vrabel 	struct resource *res;
108362cedb9fSDavid Vrabel 	int ret;
108462cedb9fSDavid Vrabel 
108562cedb9fSDavid Vrabel 	res = register_memory_resource(start, size);
10866f754ba4SVitaly Kuznetsov 	if (IS_ERR(res))
10876f754ba4SVitaly Kuznetsov 		return PTR_ERR(res);
108862cedb9fSDavid Vrabel 
1089f29d8e9cSDavid Hildenbrand 	ret = add_memory_resource(nid, res);
109062cedb9fSDavid Vrabel 	if (ret < 0)
109162cedb9fSDavid Vrabel 		release_memory_resource(res);
109262cedb9fSDavid Vrabel 	return ret;
109362cedb9fSDavid Vrabel }
10948df1d0e4SDavid Hildenbrand 
10958df1d0e4SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size)
10968df1d0e4SDavid Hildenbrand {
10978df1d0e4SDavid Hildenbrand 	int rc;
10988df1d0e4SDavid Hildenbrand 
10998df1d0e4SDavid Hildenbrand 	lock_device_hotplug();
11008df1d0e4SDavid Hildenbrand 	rc = __add_memory(nid, start, size);
11018df1d0e4SDavid Hildenbrand 	unlock_device_hotplug();
11028df1d0e4SDavid Hildenbrand 
11038df1d0e4SDavid Hildenbrand 	return rc;
11048df1d0e4SDavid Hildenbrand }
1105bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
11060c0e6195SKAMEZAWA Hiroyuki 
11070c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
11080c0e6195SKAMEZAWA Hiroyuki /*
11095c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
11105c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
11115c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
11125c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
11135c755e9fSBadari Pulavarty  * be located at the start of the pageblock
11145c755e9fSBadari Pulavarty  */
11155c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
11165c755e9fSBadari Pulavarty {
11175c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
11185c755e9fSBadari Pulavarty }
11195c755e9fSBadari Pulavarty 
1120891cb2a7SMichal Hocko /* Return the pfn of the start of the next active pageblock after a given pfn */
1121891cb2a7SMichal Hocko static unsigned long next_active_pageblock(unsigned long pfn)
11225c755e9fSBadari Pulavarty {
1123891cb2a7SMichal Hocko 	struct page *page = pfn_to_page(pfn);
1124891cb2a7SMichal Hocko 
11255c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
1126891cb2a7SMichal Hocko 	BUG_ON(pfn & (pageblock_nr_pages - 1));
11275c755e9fSBadari Pulavarty 
11285c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
11290dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
11300dcc48c1SKAMEZAWA Hiroyuki 		int order;
11310dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
11320dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
11330dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
1134891cb2a7SMichal Hocko 			return pfn + (1 << order);
11350dcc48c1SKAMEZAWA Hiroyuki 	}
11365c755e9fSBadari Pulavarty 
1137891cb2a7SMichal Hocko 	return pfn + pageblock_nr_pages;
11385c755e9fSBadari Pulavarty }
11395c755e9fSBadari Pulavarty 
1140891cb2a7SMichal Hocko static bool is_pageblock_removable_nolock(unsigned long pfn)
1141fb52bbaeSMathieu Malaterre {
1142891cb2a7SMichal Hocko 	struct page *page = pfn_to_page(pfn);
1143fb52bbaeSMathieu Malaterre 	struct zone *zone;
1144fb52bbaeSMathieu Malaterre 
1145fb52bbaeSMathieu Malaterre 	/*
1146fb52bbaeSMathieu Malaterre 	 * We have to be careful here because we are iterating over memory
1147fb52bbaeSMathieu Malaterre 	 * sections which are not zone aware so we might end up outside of
1148fb52bbaeSMathieu Malaterre 	 * the zone but still within the section.
1149fb52bbaeSMathieu Malaterre 	 * We have to take care about the node as well. If the node is offline
1150fb52bbaeSMathieu Malaterre 	 * its NODE_DATA will be NULL - see page_zone.
1151fb52bbaeSMathieu Malaterre 	 */
1152fb52bbaeSMathieu Malaterre 	if (!node_online(page_to_nid(page)))
1153fb52bbaeSMathieu Malaterre 		return false;
1154fb52bbaeSMathieu Malaterre 
1155fb52bbaeSMathieu Malaterre 	zone = page_zone(page);
1156fb52bbaeSMathieu Malaterre 	pfn = page_to_pfn(page);
1157fb52bbaeSMathieu Malaterre 	if (!zone_spans_pfn(zone, pfn))
1158fb52bbaeSMathieu Malaterre 		return false;
1159fb52bbaeSMathieu Malaterre 
1160fe4c86c9SDavid Hildenbrand 	return !has_unmovable_pages(zone, page, MIGRATE_MOVABLE,
1161756d25beSDavid Hildenbrand 				    MEMORY_OFFLINE);
1162fb52bbaeSMathieu Malaterre }
1163fb52bbaeSMathieu Malaterre 
11645c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
1165c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
11665c755e9fSBadari Pulavarty {
1167891cb2a7SMichal Hocko 	unsigned long end_pfn, pfn;
1168891cb2a7SMichal Hocko 
1169891cb2a7SMichal Hocko 	end_pfn = min(start_pfn + nr_pages,
1170891cb2a7SMichal Hocko 			zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
11715c755e9fSBadari Pulavarty 
11725c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
1173891cb2a7SMichal Hocko 	for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1174891cb2a7SMichal Hocko 		if (!is_pageblock_removable_nolock(pfn))
1175c98940f6SYaowei Bai 			return false;
117649ac8255SKAMEZAWA Hiroyuki 		cond_resched();
11775c755e9fSBadari Pulavarty 	}
11785c755e9fSBadari Pulavarty 
11795c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
1180c98940f6SYaowei Bai 	return true;
11815c755e9fSBadari Pulavarty }
11825c755e9fSBadari Pulavarty 
11835c755e9fSBadari Pulavarty /*
118492917998SDavid Hildenbrand  * Confirm all pages in a range [start, end) belong to the same zone (skipping
118592917998SDavid Hildenbrand  * memory holes). When true, return the zone.
11860c0e6195SKAMEZAWA Hiroyuki  */
118792917998SDavid Hildenbrand struct zone *test_pages_in_a_zone(unsigned long start_pfn,
118892917998SDavid Hildenbrand 				  unsigned long end_pfn)
11890c0e6195SKAMEZAWA Hiroyuki {
11905f0f2887SAndrew Banman 	unsigned long pfn, sec_end_pfn;
11910c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
11920c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
11930c0e6195SKAMEZAWA Hiroyuki 	int i;
1194deb88a2aSToshi Kani 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
11950c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
1196deb88a2aSToshi Kani 	     pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
11975f0f2887SAndrew Banman 		/* Make sure the memory section is present first */
11985f0f2887SAndrew Banman 		if (!present_section_nr(pfn_to_section_nr(pfn)))
11995f0f2887SAndrew Banman 			continue;
12005f0f2887SAndrew Banman 		for (; pfn < sec_end_pfn && pfn < end_pfn;
12010c0e6195SKAMEZAWA Hiroyuki 		     pfn += MAX_ORDER_NR_PAGES) {
12020c0e6195SKAMEZAWA Hiroyuki 			i = 0;
12030c0e6195SKAMEZAWA Hiroyuki 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
12045f0f2887SAndrew Banman 			while ((i < MAX_ORDER_NR_PAGES) &&
12055f0f2887SAndrew Banman 				!pfn_valid_within(pfn + i))
12060c0e6195SKAMEZAWA Hiroyuki 				i++;
1207d6d8c8a4Szhong jiang 			if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
12080c0e6195SKAMEZAWA Hiroyuki 				continue;
120924feb47cSMikhail Zaslonko 			/* Check if we got outside of the zone */
121024feb47cSMikhail Zaslonko 			if (zone && !zone_spans_pfn(zone, pfn + i))
121192917998SDavid Hildenbrand 				return NULL;
12120c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn + i);
12130c0e6195SKAMEZAWA Hiroyuki 			if (zone && page_zone(page) != zone)
121492917998SDavid Hildenbrand 				return NULL;
12150c0e6195SKAMEZAWA Hiroyuki 			zone = page_zone(page);
12160c0e6195SKAMEZAWA Hiroyuki 		}
12175f0f2887SAndrew Banman 	}
1218deb88a2aSToshi Kani 
121992917998SDavid Hildenbrand 	return zone;
1220a96dfddbSToshi Kani }
12210c0e6195SKAMEZAWA Hiroyuki 
12220c0e6195SKAMEZAWA Hiroyuki /*
12230efadf48SYisheng Xie  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
12240efadf48SYisheng Xie  * non-lru movable pages and hugepages). We scan pfn because it's much
12250efadf48SYisheng Xie  * easier than scanning over linked list. This function returns the pfn
12260efadf48SYisheng Xie  * of the first found movable page if it's found, otherwise 0.
12270c0e6195SKAMEZAWA Hiroyuki  */
1228c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
12290c0e6195SKAMEZAWA Hiroyuki {
12300c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
1231eeb0efd0SOscar Salvador 
12320c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
1233eeb0efd0SOscar Salvador 		struct page *page, *head;
1234eeb0efd0SOscar Salvador 		unsigned long skip;
1235eeb0efd0SOscar Salvador 
1236eeb0efd0SOscar Salvador 		if (!pfn_valid(pfn))
1237eeb0efd0SOscar Salvador 			continue;
12380c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
12390c0e6195SKAMEZAWA Hiroyuki 		if (PageLRU(page))
12400c0e6195SKAMEZAWA Hiroyuki 			return pfn;
12410efadf48SYisheng Xie 		if (__PageMovable(page))
12420efadf48SYisheng Xie 			return pfn;
1243eeb0efd0SOscar Salvador 
1244eeb0efd0SOscar Salvador 		if (!PageHuge(page))
1245eeb0efd0SOscar Salvador 			continue;
1246eeb0efd0SOscar Salvador 		head = compound_head(page);
124739186cbeSOscar Salvador 		if (page_huge_active(head))
1248c8721bbbSNaoya Horiguchi 			return pfn;
1249d8c6546bSMatthew Wilcox (Oracle) 		skip = compound_nr(head) - (page - head);
1250eeb0efd0SOscar Salvador 		pfn += skip - 1;
12510c0e6195SKAMEZAWA Hiroyuki 	}
12520c0e6195SKAMEZAWA Hiroyuki 	return 0;
12530c0e6195SKAMEZAWA Hiroyuki }
12540c0e6195SKAMEZAWA Hiroyuki 
1255666feb21SMichal Hocko static struct page *new_node_page(struct page *page, unsigned long private)
1256394e31d2SXishi Qiu {
1257394e31d2SXishi Qiu 	int nid = page_to_nid(page);
1258231e97e2SLi Zhong 	nodemask_t nmask = node_states[N_MEMORY];
12597f252f27SMichal Hocko 
12607f252f27SMichal Hocko 	/*
12617f252f27SMichal Hocko 	 * try to allocate from a different node but reuse this node if there
12627f252f27SMichal Hocko 	 * are no other online nodes to be used (e.g. we are offlining a part
12637f252f27SMichal Hocko 	 * of the only existing node)
12647f252f27SMichal Hocko 	 */
12657f252f27SMichal Hocko 	node_clear(nid, nmask);
12667f252f27SMichal Hocko 	if (nodes_empty(nmask))
12677f252f27SMichal Hocko 		node_set(nid, nmask);
1268394e31d2SXishi Qiu 
12698b913238SMichal Hocko 	return new_page_nodemask(page, nid, &nmask);
1270394e31d2SXishi Qiu }
1271394e31d2SXishi Qiu 
12720c0e6195SKAMEZAWA Hiroyuki static int
12730c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
12740c0e6195SKAMEZAWA Hiroyuki {
12750c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
12760c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12770c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
12780c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
12790c0e6195SKAMEZAWA Hiroyuki 
1280a85009c3SMichal Hocko 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
12810c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
12820c0e6195SKAMEZAWA Hiroyuki 			continue;
12830c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
1284c8721bbbSNaoya Horiguchi 
1285c8721bbbSNaoya Horiguchi 		if (PageHuge(page)) {
1286c8721bbbSNaoya Horiguchi 			struct page *head = compound_head(page);
1287d8c6546bSMatthew Wilcox (Oracle) 			pfn = page_to_pfn(head) + compound_nr(head) - 1;
1288daf3538aSOscar Salvador 			isolate_huge_page(head, &source);
1289c8721bbbSNaoya Horiguchi 			continue;
129094723aafSMichal Hocko 		} else if (PageTransHuge(page))
12918135d892SNaoya Horiguchi 			pfn = page_to_pfn(compound_head(page))
12928135d892SNaoya Horiguchi 				+ hpage_nr_pages(page) - 1;
1293c8721bbbSNaoya Horiguchi 
1294b15c8726SMichal Hocko 		/*
1295b15c8726SMichal Hocko 		 * HWPoison pages have elevated reference counts so the migration would
1296b15c8726SMichal Hocko 		 * fail on them. It also doesn't make any sense to migrate them in the
1297b15c8726SMichal Hocko 		 * first place. Still try to unmap such a page in case it is still mapped
1298b15c8726SMichal Hocko 		 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1299b15c8726SMichal Hocko 		 * the unmap as the catch all safety net).
1300b15c8726SMichal Hocko 		 */
1301b15c8726SMichal Hocko 		if (PageHWPoison(page)) {
1302b15c8726SMichal Hocko 			if (WARN_ON(PageLRU(page)))
1303b15c8726SMichal Hocko 				isolate_lru_page(page);
1304b15c8726SMichal Hocko 			if (page_mapped(page))
1305b15c8726SMichal Hocko 				try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1306b15c8726SMichal Hocko 			continue;
1307b15c8726SMichal Hocko 		}
1308b15c8726SMichal Hocko 
1309700c2a46SKonstantin Khlebnikov 		if (!get_page_unless_zero(page))
13100c0e6195SKAMEZAWA Hiroyuki 			continue;
13110c0e6195SKAMEZAWA Hiroyuki 		/*
13120efadf48SYisheng Xie 		 * We can skip free pages. And we can deal with pages on
13130efadf48SYisheng Xie 		 * LRU and non-lru movable pages.
13140c0e6195SKAMEZAWA Hiroyuki 		 */
13150efadf48SYisheng Xie 		if (PageLRU(page))
131662695a84SNick Piggin 			ret = isolate_lru_page(page);
13170efadf48SYisheng Xie 		else
13180efadf48SYisheng Xie 			ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
13190c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
132062695a84SNick Piggin 			list_add_tail(&page->lru, &source);
13210efadf48SYisheng Xie 			if (!__PageMovable(page))
1322599d0c95SMel Gorman 				inc_node_page_state(page, NR_ISOLATED_ANON +
13239de4f22aSHuang Ying 						    page_is_file_lru(page));
13246d9c285aSKOSAKI Motohiro 
13250c0e6195SKAMEZAWA Hiroyuki 		} else {
13262932c8b0SMichal Hocko 			pr_warn("failed to isolate pfn %lx\n", pfn);
13270efadf48SYisheng Xie 			dump_page(page, "isolation failed");
13281723058eSOscar Salvador 		}
1329700c2a46SKonstantin Khlebnikov 		put_page(page);
13300c0e6195SKAMEZAWA Hiroyuki 	}
1331f3ab2636SBob Liu 	if (!list_empty(&source)) {
1332394e31d2SXishi Qiu 		/* Allocate a new page from the nearest neighbor node */
1333394e31d2SXishi Qiu 		ret = migrate_pages(&source, new_node_page, NULL, 0,
13349c620e2bSHugh Dickins 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
13352932c8b0SMichal Hocko 		if (ret) {
13362932c8b0SMichal Hocko 			list_for_each_entry(page, &source, lru) {
13372932c8b0SMichal Hocko 				pr_warn("migrating pfn %lx failed ret:%d ",
13382932c8b0SMichal Hocko 				       page_to_pfn(page), ret);
13392932c8b0SMichal Hocko 				dump_page(page, "migration failure");
13402932c8b0SMichal Hocko 			}
1341c8721bbbSNaoya Horiguchi 			putback_movable_pages(&source);
1342f3ab2636SBob Liu 		}
13432932c8b0SMichal Hocko 	}
13441723058eSOscar Salvador 
13450c0e6195SKAMEZAWA Hiroyuki 	return ret;
13460c0e6195SKAMEZAWA Hiroyuki }
13470c0e6195SKAMEZAWA Hiroyuki 
13480ee5f4f3SDavid Hildenbrand /* Mark all sections offline and remove all free pages from the buddy. */
13490c0e6195SKAMEZAWA Hiroyuki static int
13500c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
13510c0e6195SKAMEZAWA Hiroyuki 			void *data)
13520c0e6195SKAMEZAWA Hiroyuki {
13535557c766SMichal Hocko 	unsigned long *offlined_pages = (unsigned long *)data;
13540c0e6195SKAMEZAWA Hiroyuki 
13555557c766SMichal Hocko 	*offlined_pages += __offline_isolated_pages(start, start + nr_pages);
13565557c766SMichal Hocko 	return 0;
13570c0e6195SKAMEZAWA Hiroyuki }
13580c0e6195SKAMEZAWA Hiroyuki 
13590c0e6195SKAMEZAWA Hiroyuki /*
13600c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
13610c0e6195SKAMEZAWA Hiroyuki  */
13620c0e6195SKAMEZAWA Hiroyuki static int
13630c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
13640c0e6195SKAMEZAWA Hiroyuki 			void *data)
13650c0e6195SKAMEZAWA Hiroyuki {
1366756d25beSDavid Hildenbrand 	return test_pages_isolated(start_pfn, start_pfn + nr_pages,
1367756d25beSDavid Hildenbrand 				   MEMORY_OFFLINE);
13680c0e6195SKAMEZAWA Hiroyuki }
13690c0e6195SKAMEZAWA Hiroyuki 
1370c5320926STang Chen static int __init cmdline_parse_movable_node(char *p)
1371c5320926STang Chen {
13724932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
137355ac590cSTang Chen 	movable_node_enabled = true;
13744932381eSMichal Hocko #else
13754932381eSMichal Hocko 	pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
13764932381eSMichal Hocko #endif
1377c5320926STang Chen 	return 0;
1378c5320926STang Chen }
1379c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node);
1380c5320926STang Chen 
1381d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */
1382d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages,
1383d9713679SLai Jiangshan 		struct zone *zone, struct memory_notify *arg)
1384d9713679SLai Jiangshan {
1385d9713679SLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
1386d9713679SLai Jiangshan 	unsigned long present_pages = 0;
138786b27beaSOscar Salvador 	enum zone_type zt;
1388d9713679SLai Jiangshan 
138998fa15f3SAnshuman Khandual 	arg->status_change_nid = NUMA_NO_NODE;
139098fa15f3SAnshuman Khandual 	arg->status_change_nid_normal = NUMA_NO_NODE;
139198fa15f3SAnshuman Khandual 	arg->status_change_nid_high = NUMA_NO_NODE;
139286b27beaSOscar Salvador 
139386b27beaSOscar Salvador 	/*
139486b27beaSOscar Salvador 	 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
139586b27beaSOscar Salvador 	 * If the memory to be offline is within the range
139686b27beaSOscar Salvador 	 * [0..ZONE_NORMAL], and it is the last present memory there,
139786b27beaSOscar Salvador 	 * the zones in that range will become empty after the offlining,
139886b27beaSOscar Salvador 	 * thus we can determine that we need to clear the node from
139986b27beaSOscar Salvador 	 * node_states[N_NORMAL_MEMORY].
140086b27beaSOscar Salvador 	 */
140186b27beaSOscar Salvador 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
140286b27beaSOscar Salvador 		present_pages += pgdat->node_zones[zt].present_pages;
140386b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
140486b27beaSOscar Salvador 		arg->status_change_nid_normal = zone_to_nid(zone);
1405d9713679SLai Jiangshan 
14066715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
14076715ddf9SLai Jiangshan 	/*
140886b27beaSOscar Salvador 	 * node_states[N_HIGH_MEMORY] contains nodes which
140986b27beaSOscar Salvador 	 * have normal memory or high memory.
141086b27beaSOscar Salvador 	 * Here we add the present_pages belonging to ZONE_HIGHMEM.
141186b27beaSOscar Salvador 	 * If the zone is within the range of [0..ZONE_HIGHMEM), and
141286b27beaSOscar Salvador 	 * we determine that the zones in that range become empty,
141386b27beaSOscar Salvador 	 * we need to clear the node for N_HIGH_MEMORY.
14146715ddf9SLai Jiangshan 	 */
141586b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
141686b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
14176715ddf9SLai Jiangshan 		arg->status_change_nid_high = zone_to_nid(zone);
14186715ddf9SLai Jiangshan #endif
14196715ddf9SLai Jiangshan 
1420d9713679SLai Jiangshan 	/*
142186b27beaSOscar Salvador 	 * We have accounted the pages from [0..ZONE_NORMAL), and
142286b27beaSOscar Salvador 	 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
142386b27beaSOscar Salvador 	 * as well.
142486b27beaSOscar Salvador 	 * Here we count the possible pages from ZONE_MOVABLE.
142586b27beaSOscar Salvador 	 * If after having accounted all the pages, we see that the nr_pages
142686b27beaSOscar Salvador 	 * to be offlined is over or equal to the accounted pages,
142786b27beaSOscar Salvador 	 * we know that the node will become empty, and so, we can clear
142886b27beaSOscar Salvador 	 * it for N_MEMORY as well.
1429d9713679SLai Jiangshan 	 */
143086b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1431d9713679SLai Jiangshan 
1432d9713679SLai Jiangshan 	if (nr_pages >= present_pages)
1433d9713679SLai Jiangshan 		arg->status_change_nid = zone_to_nid(zone);
1434d9713679SLai Jiangshan }
1435d9713679SLai Jiangshan 
1436d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg)
1437d9713679SLai Jiangshan {
1438d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
1439d9713679SLai Jiangshan 		node_clear_state(node, N_NORMAL_MEMORY);
1440d9713679SLai Jiangshan 
1441cf01f6f5SOscar Salvador 	if (arg->status_change_nid_high >= 0)
1442d9713679SLai Jiangshan 		node_clear_state(node, N_HIGH_MEMORY);
14436715ddf9SLai Jiangshan 
1444cf01f6f5SOscar Salvador 	if (arg->status_change_nid >= 0)
14456715ddf9SLai Jiangshan 		node_clear_state(node, N_MEMORY);
1446d9713679SLai Jiangshan }
1447d9713679SLai Jiangshan 
1448c5e79ef5SDavid Hildenbrand static int count_system_ram_pages_cb(unsigned long start_pfn,
1449c5e79ef5SDavid Hildenbrand 				     unsigned long nr_pages, void *data)
1450c5e79ef5SDavid Hildenbrand {
1451c5e79ef5SDavid Hildenbrand 	unsigned long *nr_system_ram_pages = data;
1452c5e79ef5SDavid Hildenbrand 
1453c5e79ef5SDavid Hildenbrand 	*nr_system_ram_pages += nr_pages;
1454c5e79ef5SDavid Hildenbrand 	return 0;
1455c5e79ef5SDavid Hildenbrand }
1456c5e79ef5SDavid Hildenbrand 
1457a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn,
1458ecde0f3eSMichal Hocko 		  unsigned long end_pfn)
14590c0e6195SKAMEZAWA Hiroyuki {
1460c5e79ef5SDavid Hildenbrand 	unsigned long pfn, nr_pages = 0;
14615557c766SMichal Hocko 	unsigned long offlined_pages = 0;
14629b7ea46aSQian Cai 	int ret, node, nr_isolate_pageblock;
1463d702909fSCody P Schafer 	unsigned long flags;
14640c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
14657b78d335SYasunori Goto 	struct memory_notify arg;
146679605093SMichal Hocko 	char *reason;
14670c0e6195SKAMEZAWA Hiroyuki 
1468381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
1469381eab4aSDavid Hildenbrand 
1470c5e79ef5SDavid Hildenbrand 	/*
1471c5e79ef5SDavid Hildenbrand 	 * Don't allow to offline memory blocks that contain holes.
1472c5e79ef5SDavid Hildenbrand 	 * Consequently, memory blocks with holes can never get onlined
1473c5e79ef5SDavid Hildenbrand 	 * via the hotplug path - online_pages() - as hotplugged memory has
1474c5e79ef5SDavid Hildenbrand 	 * no holes. This way, we e.g., don't have to worry about marking
1475c5e79ef5SDavid Hildenbrand 	 * memory holes PG_reserved, don't need pfn_valid() checks, and can
1476c5e79ef5SDavid Hildenbrand 	 * avoid using walk_system_ram_range() later.
1477c5e79ef5SDavid Hildenbrand 	 */
1478c5e79ef5SDavid Hildenbrand 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, &nr_pages,
1479c5e79ef5SDavid Hildenbrand 			      count_system_ram_pages_cb);
1480c5e79ef5SDavid Hildenbrand 	if (nr_pages != end_pfn - start_pfn) {
1481c5e79ef5SDavid Hildenbrand 		ret = -EINVAL;
1482c5e79ef5SDavid Hildenbrand 		reason = "memory holes";
1483c5e79ef5SDavid Hildenbrand 		goto failed_removal;
1484c5e79ef5SDavid Hildenbrand 	}
1485c5e79ef5SDavid Hildenbrand 
14860c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
14870c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
148892917998SDavid Hildenbrand 	zone = test_pages_in_a_zone(start_pfn, end_pfn);
148992917998SDavid Hildenbrand 	if (!zone) {
149079605093SMichal Hocko 		ret = -EINVAL;
149179605093SMichal Hocko 		reason = "multizone range";
149279605093SMichal Hocko 		goto failed_removal;
1493381eab4aSDavid Hildenbrand 	}
14947b78d335SYasunori Goto 	node = zone_to_nid(zone);
14957b78d335SYasunori Goto 
14960c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
1497b023f468SWen Congyang 	ret = start_isolate_page_range(start_pfn, end_pfn,
1498d381c547SMichal Hocko 				       MIGRATE_MOVABLE,
1499756d25beSDavid Hildenbrand 				       MEMORY_OFFLINE | REPORT_FAILURE);
15009b7ea46aSQian Cai 	if (ret < 0) {
150179605093SMichal Hocko 		reason = "failure to isolate range";
150279605093SMichal Hocko 		goto failed_removal;
1503381eab4aSDavid Hildenbrand 	}
15049b7ea46aSQian Cai 	nr_isolate_pageblock = ret;
15057b78d335SYasunori Goto 
15067b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
15077b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
1508d9713679SLai Jiangshan 	node_states_check_changes_offline(nr_pages, zone, &arg);
15097b78d335SYasunori Goto 
15107b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
15117b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
151279605093SMichal Hocko 	if (ret) {
151379605093SMichal Hocko 		reason = "notifier failure";
151479605093SMichal Hocko 		goto failed_removal_isolated;
151579605093SMichal Hocko 	}
15167b78d335SYasunori Goto 
1517bb8965bdSMichal Hocko 	do {
1518bb8965bdSMichal Hocko 		for (pfn = start_pfn; pfn;) {
151979605093SMichal Hocko 			if (signal_pending(current)) {
1520bb8965bdSMichal Hocko 				ret = -EINTR;
152179605093SMichal Hocko 				reason = "signal backoff";
152279605093SMichal Hocko 				goto failed_removal_isolated;
152379605093SMichal Hocko 			}
152472b39cfcSMichal Hocko 
15250c0e6195SKAMEZAWA Hiroyuki 			cond_resched();
15269852a721SMichal Hocko 			lru_add_drain_all();
15270c0e6195SKAMEZAWA Hiroyuki 
1528bb8965bdSMichal Hocko 			pfn = scan_movable_pages(pfn, end_pfn);
1529bb8965bdSMichal Hocko 			if (pfn) {
1530bb8965bdSMichal Hocko 				/*
1531bb8965bdSMichal Hocko 				 * TODO: fatal migration failures should bail
1532bb8965bdSMichal Hocko 				 * out
1533bb8965bdSMichal Hocko 				 */
1534bb8965bdSMichal Hocko 				do_migrate_range(pfn, end_pfn);
1535bb8965bdSMichal Hocko 			}
15360c0e6195SKAMEZAWA Hiroyuki 		}
153772b39cfcSMichal Hocko 
1538c8721bbbSNaoya Horiguchi 		/*
1539bb8965bdSMichal Hocko 		 * Dissolve free hugepages in the memory block before doing
1540bb8965bdSMichal Hocko 		 * offlining actually in order to make hugetlbfs's object
1541bb8965bdSMichal Hocko 		 * counting consistent.
1542c8721bbbSNaoya Horiguchi 		 */
1543082d5b6bSGerald Schaefer 		ret = dissolve_free_huge_pages(start_pfn, end_pfn);
154479605093SMichal Hocko 		if (ret) {
154579605093SMichal Hocko 			reason = "failure to dissolve huge pages";
154679605093SMichal Hocko 			goto failed_removal_isolated;
154779605093SMichal Hocko 		}
15480c0e6195SKAMEZAWA Hiroyuki 		/* check again */
15495557c766SMichal Hocko 		ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
15505557c766SMichal Hocko 					    NULL, check_pages_isolated_cb);
15515557c766SMichal Hocko 	} while (ret);
1552bb8965bdSMichal Hocko 
1553b3834be5SAdam Buchbinder 	/* Ok, all of our target is isolated.
15540c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
15555557c766SMichal Hocko 	walk_system_ram_range(start_pfn, end_pfn - start_pfn,
15565557c766SMichal Hocko 			      &offlined_pages, offline_isolated_pages_cb);
15575557c766SMichal Hocko 	pr_info("Offlined Pages %ld\n", offlined_pages);
15589b7ea46aSQian Cai 	/*
15599b7ea46aSQian Cai 	 * Onlining will reset pagetype flags and makes migrate type
15609b7ea46aSQian Cai 	 * MOVABLE, so just need to decrease the number of isolated
15619b7ea46aSQian Cai 	 * pageblocks zone counter here.
15629b7ea46aSQian Cai 	 */
15639b7ea46aSQian Cai 	spin_lock_irqsave(&zone->lock, flags);
15649b7ea46aSQian Cai 	zone->nr_isolate_pageblock -= nr_isolate_pageblock;
15659b7ea46aSQian Cai 	spin_unlock_irqrestore(&zone->lock, flags);
15669b7ea46aSQian Cai 
15670c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
15683dcc0571SJiang Liu 	adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
15690c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
1570d702909fSCody P Schafer 
1571d702909fSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
15720c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
1573d702909fSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
15747b78d335SYasunori Goto 
15751b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
15761b79acc9SKOSAKI Motohiro 
15771e8537baSXishi Qiu 	if (!populated_zone(zone)) {
1578340175b7SJiang Liu 		zone_pcp_reset(zone);
157972675e13SMichal Hocko 		build_all_zonelists(NULL);
15801e8537baSXishi Qiu 	} else
15811e8537baSXishi Qiu 		zone_pcp_update(zone);
1582340175b7SJiang Liu 
1583d9713679SLai Jiangshan 	node_states_clear_node(node, &arg);
1584698b1b30SVlastimil Babka 	if (arg.status_change_nid >= 0) {
15858fe23e05SDavid Rientjes 		kswapd_stop(node);
1586698b1b30SVlastimil Babka 		kcompactd_stop(node);
1587698b1b30SVlastimil Babka 	}
1588bce7394aSMinchan Kim 
15890c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
15900c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
15917b78d335SYasunori Goto 
15927b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
1593feee6b29SDavid Hildenbrand 	remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
1594381eab4aSDavid Hildenbrand 	mem_hotplug_done();
15950c0e6195SKAMEZAWA Hiroyuki 	return 0;
15960c0e6195SKAMEZAWA Hiroyuki 
159779605093SMichal Hocko failed_removal_isolated:
159879605093SMichal Hocko 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1599c4efe484SQian Cai 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
16000c0e6195SKAMEZAWA Hiroyuki failed_removal:
160179605093SMichal Hocko 	pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1602a62e2f4fSBjorn Helgaas 		 (unsigned long long) start_pfn << PAGE_SHIFT,
160379605093SMichal Hocko 		 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
160479605093SMichal Hocko 		 reason);
16050c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
1606381eab4aSDavid Hildenbrand 	mem_hotplug_done();
16070c0e6195SKAMEZAWA Hiroyuki 	return ret;
16080c0e6195SKAMEZAWA Hiroyuki }
160971088785SBadari Pulavarty 
1610a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1611a16cee10SWen Congyang {
1612ecde0f3eSMichal Hocko 	return __offline_pages(start_pfn, start_pfn + nr_pages);
1613a16cee10SWen Congyang }
1614a16cee10SWen Congyang 
1615d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1616bbc76be6SWen Congyang {
1617bbc76be6SWen Congyang 	int ret = !is_memblock_offlined(mem);
1618bbc76be6SWen Congyang 
1619349daa0fSRandy Dunlap 	if (unlikely(ret)) {
1620349daa0fSRandy Dunlap 		phys_addr_t beginpa, endpa;
1621349daa0fSRandy Dunlap 
1622349daa0fSRandy Dunlap 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1623b6c88d3bSDavid Hildenbrand 		endpa = beginpa + memory_block_size_bytes() - 1;
1624756a025fSJoe Perches 		pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1625349daa0fSRandy Dunlap 			&beginpa, &endpa);
1626bbc76be6SWen Congyang 
1627eca499abSPavel Tatashin 		return -EBUSY;
1628eca499abSPavel Tatashin 	}
1629eca499abSPavel Tatashin 	return 0;
1630bbc76be6SWen Congyang }
1631bbc76be6SWen Congyang 
16320f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat)
163360a5a19eSTang Chen {
163460a5a19eSTang Chen 	int cpu;
163560a5a19eSTang Chen 
163660a5a19eSTang Chen 	for_each_present_cpu(cpu) {
163760a5a19eSTang Chen 		if (cpu_to_node(cpu) == pgdat->node_id)
163860a5a19eSTang Chen 			/*
163960a5a19eSTang Chen 			 * the cpu on this node isn't removed, and we can't
164060a5a19eSTang Chen 			 * offline this node.
164160a5a19eSTang Chen 			 */
164260a5a19eSTang Chen 			return -EBUSY;
164360a5a19eSTang Chen 	}
164460a5a19eSTang Chen 
164560a5a19eSTang Chen 	return 0;
164660a5a19eSTang Chen }
164760a5a19eSTang Chen 
16482c91f8fcSDavid Hildenbrand static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
16492c91f8fcSDavid Hildenbrand {
16502c91f8fcSDavid Hildenbrand 	int nid = *(int *)arg;
16512c91f8fcSDavid Hildenbrand 
16522c91f8fcSDavid Hildenbrand 	/*
16532c91f8fcSDavid Hildenbrand 	 * If a memory block belongs to multiple nodes, the stored nid is not
16542c91f8fcSDavid Hildenbrand 	 * reliable. However, such blocks are always online (e.g., cannot get
16552c91f8fcSDavid Hildenbrand 	 * offlined) and, therefore, are still spanned by the node.
16562c91f8fcSDavid Hildenbrand 	 */
16572c91f8fcSDavid Hildenbrand 	return mem->nid == nid ? -EEXIST : 0;
16582c91f8fcSDavid Hildenbrand }
16592c91f8fcSDavid Hildenbrand 
16600f1cfe9dSToshi Kani /**
16610f1cfe9dSToshi Kani  * try_offline_node
1662e8b098fcSMike Rapoport  * @nid: the node ID
16630f1cfe9dSToshi Kani  *
16640f1cfe9dSToshi Kani  * Offline a node if all memory sections and cpus of the node are removed.
16650f1cfe9dSToshi Kani  *
16660f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
16670f1cfe9dSToshi Kani  * and online/offline operations before this call.
16680f1cfe9dSToshi Kani  */
166990b30cdcSWen Congyang void try_offline_node(int nid)
167060a5a19eSTang Chen {
1671d822b86aSWen Congyang 	pg_data_t *pgdat = NODE_DATA(nid);
16722c91f8fcSDavid Hildenbrand 	int rc;
167360a5a19eSTang Chen 
167460a5a19eSTang Chen 	/*
16752c91f8fcSDavid Hildenbrand 	 * If the node still spans pages (especially ZONE_DEVICE), don't
16762c91f8fcSDavid Hildenbrand 	 * offline it. A node spans memory after move_pfn_range_to_zone(),
16772c91f8fcSDavid Hildenbrand 	 * e.g., after the memory block was onlined.
167860a5a19eSTang Chen 	 */
16792c91f8fcSDavid Hildenbrand 	if (pgdat->node_spanned_pages)
168060a5a19eSTang Chen 		return;
16812c91f8fcSDavid Hildenbrand 
16822c91f8fcSDavid Hildenbrand 	/*
16832c91f8fcSDavid Hildenbrand 	 * Especially offline memory blocks might not be spanned by the
16842c91f8fcSDavid Hildenbrand 	 * node. They will get spanned by the node once they get onlined.
16852c91f8fcSDavid Hildenbrand 	 * However, they link to the node in sysfs and can get onlined later.
16862c91f8fcSDavid Hildenbrand 	 */
16872c91f8fcSDavid Hildenbrand 	rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
16882c91f8fcSDavid Hildenbrand 	if (rc)
16892c91f8fcSDavid Hildenbrand 		return;
169060a5a19eSTang Chen 
169146a3679bSMichal Hocko 	if (check_cpu_on_node(pgdat))
169260a5a19eSTang Chen 		return;
169360a5a19eSTang Chen 
169460a5a19eSTang Chen 	/*
169560a5a19eSTang Chen 	 * all memory/cpu of this node are removed, we can offline this
169660a5a19eSTang Chen 	 * node now.
169760a5a19eSTang Chen 	 */
169860a5a19eSTang Chen 	node_set_offline(nid);
169960a5a19eSTang Chen 	unregister_one_node(nid);
170060a5a19eSTang Chen }
170190b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node);
170260a5a19eSTang Chen 
1703d9eb1417SDavid Hildenbrand static void __release_memory_resource(resource_size_t start,
1704d9eb1417SDavid Hildenbrand 				      resource_size_t size)
1705d9eb1417SDavid Hildenbrand {
1706d9eb1417SDavid Hildenbrand 	int ret;
1707d9eb1417SDavid Hildenbrand 
1708d9eb1417SDavid Hildenbrand 	/*
1709d9eb1417SDavid Hildenbrand 	 * When removing memory in the same granularity as it was added,
1710d9eb1417SDavid Hildenbrand 	 * this function never fails. It might only fail if resources
1711d9eb1417SDavid Hildenbrand 	 * have to be adjusted or split. We'll ignore the error, as
1712d9eb1417SDavid Hildenbrand 	 * removing of memory cannot fail.
1713d9eb1417SDavid Hildenbrand 	 */
1714d9eb1417SDavid Hildenbrand 	ret = release_mem_region_adjustable(&iomem_resource, start, size);
1715d9eb1417SDavid Hildenbrand 	if (ret) {
1716d9eb1417SDavid Hildenbrand 		resource_size_t endres = start + size - 1;
1717d9eb1417SDavid Hildenbrand 
1718d9eb1417SDavid Hildenbrand 		pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1719d9eb1417SDavid Hildenbrand 			&start, &endres, ret);
1720d9eb1417SDavid Hildenbrand 	}
1721d9eb1417SDavid Hildenbrand }
1722d9eb1417SDavid Hildenbrand 
1723eca499abSPavel Tatashin static int __ref try_remove_memory(int nid, u64 start, u64 size)
1724bbc76be6SWen Congyang {
1725eca499abSPavel Tatashin 	int rc = 0;
1726993c1aadSWen Congyang 
172727356f54SToshi Kani 	BUG_ON(check_hotplug_memory_range(start, size));
172827356f54SToshi Kani 
17296677e3eaSYasuaki Ishimatsu 	/*
1730242831ebSRafael J. Wysocki 	 * All memory blocks must be offlined before removing memory.  Check
1731eca499abSPavel Tatashin 	 * whether all memory blocks in question are offline and return error
1732242831ebSRafael J. Wysocki 	 * if this is not the case.
17336677e3eaSYasuaki Ishimatsu 	 */
1734fbcf73ceSDavid Hildenbrand 	rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb);
1735eca499abSPavel Tatashin 	if (rc)
1736eca499abSPavel Tatashin 		goto done;
17376677e3eaSYasuaki Ishimatsu 
173846c66c4bSYasuaki Ishimatsu 	/* remove memmap entry */
173946c66c4bSYasuaki Ishimatsu 	firmware_map_remove(start, start + size, "System RAM");
174046c66c4bSYasuaki Ishimatsu 
1741f1037ec0SDan Williams 	/*
1742f1037ec0SDan Williams 	 * Memory block device removal under the device_hotplug_lock is
1743f1037ec0SDan Williams 	 * a barrier against racing online attempts.
1744f1037ec0SDan Williams 	 */
17454c4b7f9bSDavid Hildenbrand 	remove_memory_block_devices(start, size);
17464c4b7f9bSDavid Hildenbrand 
1747f1037ec0SDan Williams 	mem_hotplug_begin();
1748f1037ec0SDan Williams 
17492c2a5af6SOscar Salvador 	arch_remove_memory(nid, start, size, NULL);
175032d1fe8fSAnshuman Khandual 	memblock_free(start, size);
175132d1fe8fSAnshuman Khandual 	memblock_remove(start, size);
1752d9eb1417SDavid Hildenbrand 	__release_memory_resource(start, size);
175324d335caSWen Congyang 
175460a5a19eSTang Chen 	try_offline_node(nid);
175560a5a19eSTang Chen 
1756eca499abSPavel Tatashin done:
1757bfc8c901SVladimir Davydov 	mem_hotplug_done();
1758eca499abSPavel Tatashin 	return rc;
175971088785SBadari Pulavarty }
1760d15e5926SDavid Hildenbrand 
1761eca499abSPavel Tatashin /**
1762eca499abSPavel Tatashin  * remove_memory
1763eca499abSPavel Tatashin  * @nid: the node ID
1764eca499abSPavel Tatashin  * @start: physical address of the region to remove
1765eca499abSPavel Tatashin  * @size: size of the region to remove
1766eca499abSPavel Tatashin  *
1767eca499abSPavel Tatashin  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1768eca499abSPavel Tatashin  * and online/offline operations before this call, as required by
1769eca499abSPavel Tatashin  * try_offline_node().
1770eca499abSPavel Tatashin  */
1771eca499abSPavel Tatashin void __remove_memory(int nid, u64 start, u64 size)
1772d15e5926SDavid Hildenbrand {
1773eca499abSPavel Tatashin 
1774eca499abSPavel Tatashin 	/*
177529a90db9SSouptick Joarder 	 * trigger BUG() if some memory is not offlined prior to calling this
1776eca499abSPavel Tatashin 	 * function
1777eca499abSPavel Tatashin 	 */
1778eca499abSPavel Tatashin 	if (try_remove_memory(nid, start, size))
1779eca499abSPavel Tatashin 		BUG();
1780eca499abSPavel Tatashin }
1781eca499abSPavel Tatashin 
1782eca499abSPavel Tatashin /*
1783eca499abSPavel Tatashin  * Remove memory if every memory block is offline, otherwise return -EBUSY is
1784eca499abSPavel Tatashin  * some memory is not offline
1785eca499abSPavel Tatashin  */
1786eca499abSPavel Tatashin int remove_memory(int nid, u64 start, u64 size)
1787eca499abSPavel Tatashin {
1788eca499abSPavel Tatashin 	int rc;
1789eca499abSPavel Tatashin 
1790d15e5926SDavid Hildenbrand 	lock_device_hotplug();
1791eca499abSPavel Tatashin 	rc  = try_remove_memory(nid, start, size);
1792d15e5926SDavid Hildenbrand 	unlock_device_hotplug();
1793eca499abSPavel Tatashin 
1794eca499abSPavel Tatashin 	return rc;
1795d15e5926SDavid Hildenbrand }
179671088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
1797aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1798