xref: /openbmc/linux/mm/memory_hotplug.c (revision 18db149120c106cf2b1a2595f82f3229f9d223b8)
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
7031bc3858SVitaly Kuznetsov bool memhp_auto_online;
718604d9e5SVitaly Kuznetsov #else
728604d9e5SVitaly Kuznetsov bool memhp_auto_online = true;
738604d9e5SVitaly Kuznetsov #endif
7431bc3858SVitaly Kuznetsov EXPORT_SYMBOL_GPL(memhp_auto_online);
7531bc3858SVitaly Kuznetsov 
7686dd995dSVitaly Kuznetsov static int __init setup_memhp_default_state(char *str)
7786dd995dSVitaly Kuznetsov {
7886dd995dSVitaly Kuznetsov 	if (!strcmp(str, "online"))
7986dd995dSVitaly Kuznetsov 		memhp_auto_online = true;
8086dd995dSVitaly Kuznetsov 	else if (!strcmp(str, "offline"))
8186dd995dSVitaly Kuznetsov 		memhp_auto_online = false;
8286dd995dSVitaly Kuznetsov 
8386dd995dSVitaly Kuznetsov 	return 1;
8486dd995dSVitaly Kuznetsov }
8586dd995dSVitaly Kuznetsov __setup("memhp_default_state=", setup_memhp_default_state);
8686dd995dSVitaly Kuznetsov 
8730467e0bSDavid Rientjes void mem_hotplug_begin(void)
88bfc8c901SVladimir Davydov {
893f906ba2SThomas Gleixner 	cpus_read_lock();
903f906ba2SThomas Gleixner 	percpu_down_write(&mem_hotplug_lock);
91bfc8c901SVladimir Davydov }
92bfc8c901SVladimir Davydov 
9330467e0bSDavid Rientjes void mem_hotplug_done(void)
94bfc8c901SVladimir Davydov {
953f906ba2SThomas Gleixner 	percpu_up_write(&mem_hotplug_lock);
963f906ba2SThomas Gleixner 	cpus_read_unlock();
97bfc8c901SVladimir Davydov }
9820d6c96bSKOSAKI Motohiro 
99357b4da5SJuergen Gross u64 max_mem_size = U64_MAX;
100357b4da5SJuergen Gross 
10145e0b78bSKeith Mannthey /* add this memory to iomem resource */
10245e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
10345e0b78bSKeith Mannthey {
1042794129eSDave Hansen 	struct resource *res;
1052794129eSDave Hansen 	unsigned long flags =  IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1062794129eSDave Hansen 	char *resource_name = "System RAM";
107357b4da5SJuergen Gross 
108357b4da5SJuergen Gross 	if (start + size > max_mem_size)
109357b4da5SJuergen Gross 		return ERR_PTR(-E2BIG);
110357b4da5SJuergen Gross 
1112794129eSDave Hansen 	/*
1122794129eSDave Hansen 	 * Request ownership of the new memory range.  This might be
1132794129eSDave Hansen 	 * a child of an existing resource that was present but
1142794129eSDave Hansen 	 * not marked as busy.
1152794129eSDave Hansen 	 */
1162794129eSDave Hansen 	res = __request_region(&iomem_resource, start, size,
1172794129eSDave Hansen 			       resource_name, flags);
11845e0b78bSKeith Mannthey 
1192794129eSDave Hansen 	if (!res) {
1202794129eSDave Hansen 		pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
1212794129eSDave Hansen 				start, start + size);
1226f754ba4SVitaly Kuznetsov 		return ERR_PTR(-EEXIST);
12345e0b78bSKeith Mannthey 	}
12445e0b78bSKeith Mannthey 	return res;
12545e0b78bSKeith Mannthey }
12645e0b78bSKeith Mannthey 
12745e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
12845e0b78bSKeith Mannthey {
12945e0b78bSKeith Mannthey 	if (!res)
13045e0b78bSKeith Mannthey 		return;
13145e0b78bSKeith Mannthey 	release_resource(res);
13245e0b78bSKeith Mannthey 	kfree(res);
13345e0b78bSKeith Mannthey }
13445e0b78bSKeith Mannthey 
13553947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
13646723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info,  struct page *page,
1375f24ce5fSAndrea Arcangeli 		      unsigned long type)
13804753278SYasunori Goto {
139ddffe98dSYasuaki Ishimatsu 	page->freelist = (void *)type;
14004753278SYasunori Goto 	SetPagePrivate(page);
14104753278SYasunori Goto 	set_page_private(page, info);
142fe896d18SJoonsoo Kim 	page_ref_inc(page);
14304753278SYasunori Goto }
14404753278SYasunori Goto 
145170a5a7eSJiang Liu void put_page_bootmem(struct page *page)
14604753278SYasunori Goto {
1475f24ce5fSAndrea Arcangeli 	unsigned long type;
14804753278SYasunori Goto 
149ddffe98dSYasuaki Ishimatsu 	type = (unsigned long) page->freelist;
1505f24ce5fSAndrea Arcangeli 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
1515f24ce5fSAndrea Arcangeli 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
15204753278SYasunori Goto 
153fe896d18SJoonsoo Kim 	if (page_ref_dec_return(page) == 1) {
154ddffe98dSYasuaki Ishimatsu 		page->freelist = NULL;
15504753278SYasunori Goto 		ClearPagePrivate(page);
15604753278SYasunori Goto 		set_page_private(page, 0);
1575f24ce5fSAndrea Arcangeli 		INIT_LIST_HEAD(&page->lru);
158170a5a7eSJiang Liu 		free_reserved_page(page);
15904753278SYasunori Goto 	}
16004753278SYasunori Goto }
16104753278SYasunori Goto 
16246723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
16346723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP
164d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
16504753278SYasunori Goto {
166f1eca35aSDan Williams 	unsigned long mapsize, section_nr, i;
16704753278SYasunori Goto 	struct mem_section *ms;
16804753278SYasunori Goto 	struct page *page, *memmap;
169f1eca35aSDan Williams 	struct mem_section_usage *usage;
17004753278SYasunori Goto 
17104753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
17204753278SYasunori Goto 	ms = __nr_to_section(section_nr);
17304753278SYasunori Goto 
17404753278SYasunori Goto 	/* Get section's memmap address */
17504753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
17604753278SYasunori Goto 
17704753278SYasunori Goto 	/*
17804753278SYasunori Goto 	 * Get page for the memmap's phys address
17904753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
18004753278SYasunori Goto 	 */
18104753278SYasunori Goto 	page = virt_to_page(memmap);
18204753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
18304753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
18404753278SYasunori Goto 
18504753278SYasunori Goto 	/* remember memmap's page */
18604753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
18704753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
18804753278SYasunori Goto 
189f1eca35aSDan Williams 	usage = ms->usage;
190f1eca35aSDan Williams 	page = virt_to_page(usage);
19104753278SYasunori Goto 
192f1eca35aSDan Williams 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
19304753278SYasunori Goto 
19404753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
195af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
19604753278SYasunori Goto 
19704753278SYasunori Goto }
19846723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */
19946723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn)
20046723bfaSYasuaki Ishimatsu {
201f1eca35aSDan Williams 	unsigned long mapsize, section_nr, i;
20246723bfaSYasuaki Ishimatsu 	struct mem_section *ms;
20346723bfaSYasuaki Ishimatsu 	struct page *page, *memmap;
204f1eca35aSDan Williams 	struct mem_section_usage *usage;
20546723bfaSYasuaki Ishimatsu 
20646723bfaSYasuaki Ishimatsu 	section_nr = pfn_to_section_nr(start_pfn);
20746723bfaSYasuaki Ishimatsu 	ms = __nr_to_section(section_nr);
20846723bfaSYasuaki Ishimatsu 
20946723bfaSYasuaki Ishimatsu 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
21046723bfaSYasuaki Ishimatsu 
21146723bfaSYasuaki Ishimatsu 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
21246723bfaSYasuaki Ishimatsu 
213f1eca35aSDan Williams 	usage = ms->usage;
214f1eca35aSDan Williams 	page = virt_to_page(usage);
21546723bfaSYasuaki Ishimatsu 
216f1eca35aSDan Williams 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
21746723bfaSYasuaki Ishimatsu 
21846723bfaSYasuaki Ishimatsu 	for (i = 0; i < mapsize; i++, page++)
21946723bfaSYasuaki Ishimatsu 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
22046723bfaSYasuaki Ishimatsu }
22146723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
22204753278SYasunori Goto 
2237ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
22404753278SYasunori Goto {
22504753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
22604753278SYasunori Goto 	int node = pgdat->node_id;
22704753278SYasunori Goto 	struct page *page;
22804753278SYasunori Goto 
22904753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
23004753278SYasunori Goto 	page = virt_to_page(pgdat);
23104753278SYasunori Goto 
23204753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
23304753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
23404753278SYasunori Goto 
23504753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
236c1f19495SCody P Schafer 	end_pfn = pgdat_end_pfn(pgdat);
23704753278SYasunori Goto 
2387e9f5eb0STang Chen 	/* register section info */
239f14851afSqiuxishi 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
240f14851afSqiuxishi 		/*
241f14851afSqiuxishi 		 * Some platforms can assign the same pfn to multiple nodes - on
242f14851afSqiuxishi 		 * node0 as well as nodeN.  To avoid registering a pfn against
243f14851afSqiuxishi 		 * multiple nodes we check that this pfn does not already
2447e9f5eb0STang Chen 		 * reside in some other nodes.
245f14851afSqiuxishi 		 */
246f65e91dfSYang Shi 		if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
24704753278SYasunori Goto 			register_page_bootmem_info_section(pfn);
248f14851afSqiuxishi 	}
24904753278SYasunori Goto }
25046723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
25104753278SYasunori Goto 
2527ea62160SDan Williams static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
2537ea62160SDan Williams 		const char *reason)
2547ea62160SDan Williams {
2557ea62160SDan Williams 	/*
2567ea62160SDan Williams 	 * Disallow all operations smaller than a sub-section and only
2577ea62160SDan Williams 	 * allow operations smaller than a section for
2587ea62160SDan Williams 	 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
2597ea62160SDan Williams 	 * enforces a larger memory_block_size_bytes() granularity for
2607ea62160SDan Williams 	 * memory that will be marked online, so this check should only
2617ea62160SDan Williams 	 * fire for direct arch_{add,remove}_memory() users outside of
2627ea62160SDan Williams 	 * add_memory_resource().
2637ea62160SDan Williams 	 */
2647ea62160SDan Williams 	unsigned long min_align;
2657ea62160SDan Williams 
2667ea62160SDan Williams 	if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
2677ea62160SDan Williams 		min_align = PAGES_PER_SUBSECTION;
2687ea62160SDan Williams 	else
2697ea62160SDan Williams 		min_align = PAGES_PER_SECTION;
2707ea62160SDan Williams 	if (!IS_ALIGNED(pfn, min_align)
2717ea62160SDan Williams 			|| !IS_ALIGNED(nr_pages, min_align)) {
2727ea62160SDan Williams 		WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n",
2737ea62160SDan Williams 				reason, pfn, pfn + nr_pages - 1);
2747ea62160SDan Williams 		return -EINVAL;
2757ea62160SDan Williams 	}
2767ea62160SDan Williams 	return 0;
2777ea62160SDan Williams }
2787ea62160SDan Williams 
279dca4436dSAlastair D'Silva static int check_hotplug_memory_addressable(unsigned long pfn,
280dca4436dSAlastair D'Silva 					    unsigned long nr_pages)
281dca4436dSAlastair D'Silva {
282dca4436dSAlastair D'Silva 	const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1;
283dca4436dSAlastair D'Silva 
284dca4436dSAlastair D'Silva 	if (max_addr >> MAX_PHYSMEM_BITS) {
285dca4436dSAlastair D'Silva 		const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1;
286dca4436dSAlastair D'Silva 		WARN(1,
287dca4436dSAlastair D'Silva 		     "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
288dca4436dSAlastair D'Silva 		     (u64)PFN_PHYS(pfn), max_addr, max_allowed);
289dca4436dSAlastair D'Silva 		return -E2BIG;
290dca4436dSAlastair D'Silva 	}
291dca4436dSAlastair D'Silva 
292dca4436dSAlastair D'Silva 	return 0;
293dca4436dSAlastair D'Silva }
294dca4436dSAlastair D'Silva 
2954edd7cefSDavid Rientjes /*
2964edd7cefSDavid Rientjes  * Reasonably generic function for adding memory.  It is
2974edd7cefSDavid Rientjes  * expected that archs that support memory hotplug will
2984edd7cefSDavid Rientjes  * call this function after deciding the zone to which to
2994edd7cefSDavid Rientjes  * add the new pages.
3004edd7cefSDavid Rientjes  */
3017ea62160SDan Williams int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
3027ea62160SDan Williams 		struct mhp_restrictions *restrictions)
3034edd7cefSDavid Rientjes {
3049a845030SDan Williams 	int err;
3059a845030SDan Williams 	unsigned long nr, start_sec, end_sec;
306940519f0SMichal Hocko 	struct vmem_altmap *altmap = restrictions->altmap;
3074b94ffdcSDan Williams 
308dca4436dSAlastair D'Silva 	err = check_hotplug_memory_addressable(pfn, nr_pages);
309dca4436dSAlastair D'Silva 	if (err)
310dca4436dSAlastair D'Silva 		return err;
311dca4436dSAlastair D'Silva 
3124b94ffdcSDan Williams 	if (altmap) {
3134b94ffdcSDan Williams 		/*
3144b94ffdcSDan Williams 		 * Validate altmap is within bounds of the total request
3154b94ffdcSDan Williams 		 */
3167ea62160SDan Williams 		if (altmap->base_pfn != pfn
3174b94ffdcSDan Williams 				|| vmem_altmap_offset(altmap) > nr_pages) {
3184b94ffdcSDan Williams 			pr_warn_once("memory add fail, invalid altmap\n");
3197ea62160SDan Williams 			return -EINVAL;
3204b94ffdcSDan Williams 		}
3214b94ffdcSDan Williams 		altmap->alloc = 0;
3224b94ffdcSDan Williams 	}
3234b94ffdcSDan Williams 
3247ea62160SDan Williams 	err = check_pfn_span(pfn, nr_pages, "add");
3257ea62160SDan Williams 	if (err)
3267ea62160SDan Williams 		return err;
3277ea62160SDan Williams 
3287ea62160SDan Williams 	start_sec = pfn_to_section_nr(pfn);
3297ea62160SDan Williams 	end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
3309a845030SDan Williams 	for (nr = start_sec; nr <= end_sec; nr++) {
3317ea62160SDan Williams 		unsigned long pfns;
3327ea62160SDan Williams 
3337ea62160SDan Williams 		pfns = min(nr_pages, PAGES_PER_SECTION
3347ea62160SDan Williams 				- (pfn & ~PAGE_SECTION_MASK));
335ba72b4c8SDan Williams 		err = sparse_add_section(nid, pfn, pfns, altmap);
336ba72b4c8SDan Williams 		if (err)
337ba72b4c8SDan Williams 			break;
3387ea62160SDan Williams 		pfn += pfns;
3397ea62160SDan Williams 		nr_pages -= pfns;
340f64ac5e6SMichal Hocko 		cond_resched();
3414edd7cefSDavid Rientjes 	}
342c435a390SZhu Guihua 	vmemmap_populate_print_last();
3434edd7cefSDavid Rientjes 	return err;
3444edd7cefSDavid Rientjes }
3454edd7cefSDavid Rientjes 
346815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
347d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
348815121d2SYasuaki Ishimatsu 				     unsigned long start_pfn,
349815121d2SYasuaki Ishimatsu 				     unsigned long end_pfn)
350815121d2SYasuaki Ishimatsu {
35149ba3c6bSDan Williams 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
3527ce700bfSDavid Hildenbrand 		if (unlikely(!pfn_to_online_page(start_pfn)))
353815121d2SYasuaki Ishimatsu 			continue;
354815121d2SYasuaki Ishimatsu 
355815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(start_pfn) != nid))
356815121d2SYasuaki Ishimatsu 			continue;
357815121d2SYasuaki Ishimatsu 
358815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(start_pfn)))
359815121d2SYasuaki Ishimatsu 			continue;
360815121d2SYasuaki Ishimatsu 
361815121d2SYasuaki Ishimatsu 		return start_pfn;
362815121d2SYasuaki Ishimatsu 	}
363815121d2SYasuaki Ishimatsu 
364815121d2SYasuaki Ishimatsu 	return 0;
365815121d2SYasuaki Ishimatsu }
366815121d2SYasuaki Ishimatsu 
367815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
368d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
369815121d2SYasuaki Ishimatsu 				    unsigned long start_pfn,
370815121d2SYasuaki Ishimatsu 				    unsigned long end_pfn)
371815121d2SYasuaki Ishimatsu {
372815121d2SYasuaki Ishimatsu 	unsigned long pfn;
373815121d2SYasuaki Ishimatsu 
374815121d2SYasuaki Ishimatsu 	/* pfn is the end pfn of a memory section. */
375815121d2SYasuaki Ishimatsu 	pfn = end_pfn - 1;
37649ba3c6bSDan Williams 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
3777ce700bfSDavid Hildenbrand 		if (unlikely(!pfn_to_online_page(pfn)))
378815121d2SYasuaki Ishimatsu 			continue;
379815121d2SYasuaki Ishimatsu 
380815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(pfn) != nid))
381815121d2SYasuaki Ishimatsu 			continue;
382815121d2SYasuaki Ishimatsu 
383815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(pfn)))
384815121d2SYasuaki Ishimatsu 			continue;
385815121d2SYasuaki Ishimatsu 
386815121d2SYasuaki Ishimatsu 		return pfn;
387815121d2SYasuaki Ishimatsu 	}
388815121d2SYasuaki Ishimatsu 
389815121d2SYasuaki Ishimatsu 	return 0;
390815121d2SYasuaki Ishimatsu }
391815121d2SYasuaki Ishimatsu 
392815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
393815121d2SYasuaki Ishimatsu 			     unsigned long end_pfn)
394815121d2SYasuaki Ishimatsu {
395815121d2SYasuaki Ishimatsu 	unsigned long zone_start_pfn = zone->zone_start_pfn;
396c33bc315SXishi Qiu 	unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
397c33bc315SXishi Qiu 	unsigned long zone_end_pfn = z;
398815121d2SYasuaki Ishimatsu 	unsigned long pfn;
399815121d2SYasuaki Ishimatsu 	int nid = zone_to_nid(zone);
400815121d2SYasuaki Ishimatsu 
401815121d2SYasuaki Ishimatsu 	zone_span_writelock(zone);
402815121d2SYasuaki Ishimatsu 	if (zone_start_pfn == start_pfn) {
403815121d2SYasuaki Ishimatsu 		/*
404815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the zone, it need
405815121d2SYasuaki Ishimatsu 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
406815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
407815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
408815121d2SYasuaki Ishimatsu 		 */
409815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
410815121d2SYasuaki Ishimatsu 						zone_end_pfn);
411815121d2SYasuaki Ishimatsu 		if (pfn) {
412815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = pfn;
413815121d2SYasuaki Ishimatsu 			zone->spanned_pages = zone_end_pfn - pfn;
414815121d2SYasuaki Ishimatsu 		}
415815121d2SYasuaki Ishimatsu 	} else if (zone_end_pfn == end_pfn) {
416815121d2SYasuaki Ishimatsu 		/*
417815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the zone, it need
418815121d2SYasuaki Ishimatsu 		 * shrink zone->spanned_pages.
419815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
420815121d2SYasuaki Ishimatsu 		 * shrinking zone.
421815121d2SYasuaki Ishimatsu 		 */
422815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
423815121d2SYasuaki Ishimatsu 					       start_pfn);
424815121d2SYasuaki Ishimatsu 		if (pfn)
425815121d2SYasuaki Ishimatsu 			zone->spanned_pages = pfn - zone_start_pfn + 1;
426815121d2SYasuaki Ishimatsu 	}
427815121d2SYasuaki Ishimatsu 
428815121d2SYasuaki Ishimatsu 	/*
429815121d2SYasuaki Ishimatsu 	 * The section is not biggest or smallest mem_section in the zone, it
430815121d2SYasuaki Ishimatsu 	 * only creates a hole in the zone. So in this case, we need not
431815121d2SYasuaki Ishimatsu 	 * change the zone. But perhaps, the zone has only hole data. Thus
432815121d2SYasuaki Ishimatsu 	 * it check the zone has only hole or not.
433815121d2SYasuaki Ishimatsu 	 */
434815121d2SYasuaki Ishimatsu 	pfn = zone_start_pfn;
43549ba3c6bSDan Williams 	for (; pfn < zone_end_pfn; pfn += PAGES_PER_SUBSECTION) {
4367ce700bfSDavid Hildenbrand 		if (unlikely(!pfn_to_online_page(pfn)))
437815121d2SYasuaki Ishimatsu 			continue;
438815121d2SYasuaki Ishimatsu 
439815121d2SYasuaki Ishimatsu 		if (page_zone(pfn_to_page(pfn)) != zone)
440815121d2SYasuaki Ishimatsu 			continue;
441815121d2SYasuaki Ishimatsu 
44249ba3c6bSDan Williams 		/* Skip range to be removed */
44349ba3c6bSDan Williams 		if (pfn >= start_pfn && pfn < end_pfn)
444815121d2SYasuaki Ishimatsu 			continue;
445815121d2SYasuaki Ishimatsu 
446815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
447815121d2SYasuaki Ishimatsu 		zone_span_writeunlock(zone);
448815121d2SYasuaki Ishimatsu 		return;
449815121d2SYasuaki Ishimatsu 	}
450815121d2SYasuaki Ishimatsu 
451815121d2SYasuaki Ishimatsu 	/* The zone has no valid section */
452815121d2SYasuaki Ishimatsu 	zone->zone_start_pfn = 0;
453815121d2SYasuaki Ishimatsu 	zone->spanned_pages = 0;
454815121d2SYasuaki Ishimatsu 	zone_span_writeunlock(zone);
455815121d2SYasuaki Ishimatsu }
456815121d2SYasuaki Ishimatsu 
45700d6c019SDavid Hildenbrand static void update_pgdat_span(struct pglist_data *pgdat)
458815121d2SYasuaki Ishimatsu {
45900d6c019SDavid Hildenbrand 	unsigned long node_start_pfn = 0, node_end_pfn = 0;
46000d6c019SDavid Hildenbrand 	struct zone *zone;
461815121d2SYasuaki Ishimatsu 
46200d6c019SDavid Hildenbrand 	for (zone = pgdat->node_zones;
46300d6c019SDavid Hildenbrand 	     zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
46400d6c019SDavid Hildenbrand 		unsigned long zone_end_pfn = zone->zone_start_pfn +
46500d6c019SDavid Hildenbrand 					     zone->spanned_pages;
46600d6c019SDavid Hildenbrand 
46700d6c019SDavid Hildenbrand 		/* No need to lock the zones, they can't change. */
468656d5711SDavid Hildenbrand 		if (!zone->spanned_pages)
469656d5711SDavid Hildenbrand 			continue;
470656d5711SDavid Hildenbrand 		if (!node_end_pfn) {
471656d5711SDavid Hildenbrand 			node_start_pfn = zone->zone_start_pfn;
472656d5711SDavid Hildenbrand 			node_end_pfn = zone_end_pfn;
473656d5711SDavid Hildenbrand 			continue;
474656d5711SDavid Hildenbrand 		}
475656d5711SDavid Hildenbrand 
47600d6c019SDavid Hildenbrand 		if (zone_end_pfn > node_end_pfn)
47700d6c019SDavid Hildenbrand 			node_end_pfn = zone_end_pfn;
47800d6c019SDavid Hildenbrand 		if (zone->zone_start_pfn < node_start_pfn)
47900d6c019SDavid Hildenbrand 			node_start_pfn = zone->zone_start_pfn;
480815121d2SYasuaki Ishimatsu 	}
481815121d2SYasuaki Ishimatsu 
48200d6c019SDavid Hildenbrand 	pgdat->node_start_pfn = node_start_pfn;
48300d6c019SDavid Hildenbrand 	pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
484815121d2SYasuaki Ishimatsu }
485815121d2SYasuaki Ishimatsu 
4867ea62160SDan Williams static void __remove_zone(struct zone *zone, unsigned long start_pfn,
4877ea62160SDan Williams 		unsigned long nr_pages)
488815121d2SYasuaki Ishimatsu {
489815121d2SYasuaki Ishimatsu 	struct pglist_data *pgdat = zone->zone_pgdat;
490815121d2SYasuaki Ishimatsu 	unsigned long flags;
491815121d2SYasuaki Ishimatsu 
4927ce700bfSDavid Hildenbrand #ifdef CONFIG_ZONE_DEVICE
4937ce700bfSDavid Hildenbrand 	/*
4947ce700bfSDavid Hildenbrand 	 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
4957ce700bfSDavid Hildenbrand 	 * we will not try to shrink the zones - which is okay as
4967ce700bfSDavid Hildenbrand 	 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
4977ce700bfSDavid Hildenbrand 	 */
4987ce700bfSDavid Hildenbrand 	if (zone_idx(zone) == ZONE_DEVICE)
4997ce700bfSDavid Hildenbrand 		return;
5007ce700bfSDavid Hildenbrand #endif
5017ce700bfSDavid Hildenbrand 
502815121d2SYasuaki Ishimatsu 	pgdat_resize_lock(zone->zone_pgdat, &flags);
503815121d2SYasuaki Ishimatsu 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
50400d6c019SDavid Hildenbrand 	update_pgdat_span(pgdat);
505815121d2SYasuaki Ishimatsu 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
506815121d2SYasuaki Ishimatsu }
507815121d2SYasuaki Ishimatsu 
5087ea62160SDan Williams static void __remove_section(struct zone *zone, unsigned long pfn,
5097ea62160SDan Williams 		unsigned long nr_pages, unsigned long map_offset,
5109d1d887dSDavid Hildenbrand 		struct vmem_altmap *altmap)
511ea01ea93SBadari Pulavarty {
5127ea62160SDan Williams 	struct mem_section *ms = __nr_to_section(pfn_to_section_nr(pfn));
513ea01ea93SBadari Pulavarty 
5149d1d887dSDavid Hildenbrand 	if (WARN_ON_ONCE(!valid_section(ms)))
5159d1d887dSDavid Hildenbrand 		return;
516ea01ea93SBadari Pulavarty 
5177ea62160SDan Williams 	__remove_zone(zone, pfn, nr_pages);
518ba72b4c8SDan Williams 	sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
519ea01ea93SBadari Pulavarty }
520ea01ea93SBadari Pulavarty 
521ea01ea93SBadari Pulavarty /**
522ea01ea93SBadari Pulavarty  * __remove_pages() - remove sections of pages from a zone
523ea01ea93SBadari Pulavarty  * @zone: zone from which pages need to be removed
5247ea62160SDan Williams  * @pfn: starting pageframe (must be aligned to start of a section)
525ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
526e8b098fcSMike Rapoport  * @altmap: alternative device page map or %NULL if default memmap is used
527ea01ea93SBadari Pulavarty  *
528ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
529ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
530ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
531ea01ea93SBadari Pulavarty  * calling offline_pages().
532ea01ea93SBadari Pulavarty  */
5337ea62160SDan Williams void __remove_pages(struct zone *zone, unsigned long pfn,
534da024512SChristoph Hellwig 		    unsigned long nr_pages, struct vmem_altmap *altmap)
535ea01ea93SBadari Pulavarty {
5364b94ffdcSDan Williams 	unsigned long map_offset = 0;
5379a845030SDan Williams 	unsigned long nr, start_sec, end_sec;
5384b94ffdcSDan Williams 
5394b94ffdcSDan Williams 	map_offset = vmem_altmap_offset(altmap);
540ea01ea93SBadari Pulavarty 
5417cf91a98SJoonsoo Kim 	clear_zone_contiguous(zone);
5427cf91a98SJoonsoo Kim 
5437ea62160SDan Williams 	if (check_pfn_span(pfn, nr_pages, "remove"))
5447ea62160SDan Williams 		return;
545ea01ea93SBadari Pulavarty 
5467ea62160SDan Williams 	start_sec = pfn_to_section_nr(pfn);
5477ea62160SDan Williams 	end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
5489a845030SDan Williams 	for (nr = start_sec; nr <= end_sec; nr++) {
5497ea62160SDan Williams 		unsigned long pfns;
5504b94ffdcSDan Williams 
551dd33ad7bSMichal Hocko 		cond_resched();
5527ea62160SDan Williams 		pfns = min(nr_pages, PAGES_PER_SECTION
5537ea62160SDan Williams 				- (pfn & ~PAGE_SECTION_MASK));
5547ea62160SDan Williams 		__remove_section(zone, pfn, pfns, map_offset, altmap);
5557ea62160SDan Williams 		pfn += pfns;
5567ea62160SDan Williams 		nr_pages -= pfns;
5574b94ffdcSDan Williams 		map_offset = 0;
558ea01ea93SBadari Pulavarty 	}
5597cf91a98SJoonsoo Kim 
5607cf91a98SJoonsoo Kim 	set_zone_contiguous(zone);
561ea01ea93SBadari Pulavarty }
562ea01ea93SBadari Pulavarty 
5639d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback)
5649d0ad8caSDaniel Kiper {
5659d0ad8caSDaniel Kiper 	int rc = -EINVAL;
5669d0ad8caSDaniel Kiper 
567bfc8c901SVladimir Davydov 	get_online_mems();
568bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
5699d0ad8caSDaniel Kiper 
5709d0ad8caSDaniel Kiper 	if (online_page_callback == generic_online_page) {
5719d0ad8caSDaniel Kiper 		online_page_callback = callback;
5729d0ad8caSDaniel Kiper 		rc = 0;
5739d0ad8caSDaniel Kiper 	}
5749d0ad8caSDaniel Kiper 
575bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
576bfc8c901SVladimir Davydov 	put_online_mems();
5779d0ad8caSDaniel Kiper 
5789d0ad8caSDaniel Kiper 	return rc;
5799d0ad8caSDaniel Kiper }
5809d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback);
5819d0ad8caSDaniel Kiper 
5829d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback)
5839d0ad8caSDaniel Kiper {
5849d0ad8caSDaniel Kiper 	int rc = -EINVAL;
5859d0ad8caSDaniel Kiper 
586bfc8c901SVladimir Davydov 	get_online_mems();
587bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
5889d0ad8caSDaniel Kiper 
5899d0ad8caSDaniel Kiper 	if (online_page_callback == callback) {
5909d0ad8caSDaniel Kiper 		online_page_callback = generic_online_page;
5919d0ad8caSDaniel Kiper 		rc = 0;
5929d0ad8caSDaniel Kiper 	}
5939d0ad8caSDaniel Kiper 
594bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
595bfc8c901SVladimir Davydov 	put_online_mems();
5969d0ad8caSDaniel Kiper 
5979d0ad8caSDaniel Kiper 	return rc;
5989d0ad8caSDaniel Kiper }
5999d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback);
6009d0ad8caSDaniel Kiper 
6019d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page)
602180c06efSJeremy Fitzhardinge {
6039d0ad8caSDaniel Kiper }
6049d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits);
6059d0ad8caSDaniel Kiper 
6069d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page)
6079d0ad8caSDaniel Kiper {
6083dcc0571SJiang Liu 	adjust_managed_page_count(page, 1);
6099d0ad8caSDaniel Kiper }
6109d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters);
611180c06efSJeremy Fitzhardinge 
6129d0ad8caSDaniel Kiper void __online_page_free(struct page *page)
6139d0ad8caSDaniel Kiper {
6143dcc0571SJiang Liu 	__free_reserved_page(page);
615180c06efSJeremy Fitzhardinge }
6169d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free);
6179d0ad8caSDaniel Kiper 
618*18db1491SDavid Hildenbrand void generic_online_page(struct page *page, unsigned int order)
6199d0ad8caSDaniel Kiper {
620cd02cf1aSQian Cai 	kernel_map_pages(page, 1 << order, 1);
621a9cd410aSArun KS 	__free_pages_core(page, order);
622a9cd410aSArun KS 	totalram_pages_add(1UL << order);
623a9cd410aSArun KS #ifdef CONFIG_HIGHMEM
624a9cd410aSArun KS 	if (PageHighMem(page))
625a9cd410aSArun KS 		totalhigh_pages_add(1UL << order);
626a9cd410aSArun KS #endif
627a9cd410aSArun KS }
628*18db1491SDavid Hildenbrand EXPORT_SYMBOL_GPL(generic_online_page);
629a9cd410aSArun KS 
63075884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
63175884fb1SKAMEZAWA Hiroyuki 			void *arg)
6323947be19SDave Hansen {
633b2c2ab20SDavid Hildenbrand 	const unsigned long end_pfn = start_pfn + nr_pages;
634b2c2ab20SDavid Hildenbrand 	unsigned long pfn;
635b2c2ab20SDavid Hildenbrand 	int order;
6362d070eabSMichal Hocko 
637b2c2ab20SDavid Hildenbrand 	/*
638b2c2ab20SDavid Hildenbrand 	 * Online the pages. The callback might decide to keep some pages
639b2c2ab20SDavid Hildenbrand 	 * PG_reserved (to add them to the buddy later), but we still account
640b2c2ab20SDavid Hildenbrand 	 * them as being online/belonging to this zone ("present").
641b2c2ab20SDavid Hildenbrand 	 */
642b2c2ab20SDavid Hildenbrand 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1ul << order) {
643b2c2ab20SDavid Hildenbrand 		order = min(MAX_ORDER - 1, get_order(PFN_PHYS(end_pfn - pfn)));
644bd02cc01SDavid Hildenbrand 		/* __free_pages_core() wants pfns to be aligned to the order */
645bd02cc01SDavid Hildenbrand 		if (WARN_ON_ONCE(!IS_ALIGNED(pfn, 1ul << order)))
646bd02cc01SDavid Hildenbrand 			order = 0;
647b2c2ab20SDavid Hildenbrand 		(*online_page_callback)(pfn_to_page(pfn), order);
648b2c2ab20SDavid Hildenbrand 	}
6492d070eabSMichal Hocko 
650b2c2ab20SDavid Hildenbrand 	/* mark all involved sections as online */
651b2c2ab20SDavid Hildenbrand 	online_mem_sections(start_pfn, end_pfn);
652b2c2ab20SDavid Hildenbrand 
653b2c2ab20SDavid Hildenbrand 	*(unsigned long *)arg += nr_pages;
65475884fb1SKAMEZAWA Hiroyuki 	return 0;
65575884fb1SKAMEZAWA Hiroyuki }
65675884fb1SKAMEZAWA Hiroyuki 
657d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */
658d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages,
659d9713679SLai Jiangshan 	struct zone *zone, struct memory_notify *arg)
660d9713679SLai Jiangshan {
661d9713679SLai Jiangshan 	int nid = zone_to_nid(zone);
662d9713679SLai Jiangshan 
66398fa15f3SAnshuman Khandual 	arg->status_change_nid = NUMA_NO_NODE;
66498fa15f3SAnshuman Khandual 	arg->status_change_nid_normal = NUMA_NO_NODE;
66598fa15f3SAnshuman Khandual 	arg->status_change_nid_high = NUMA_NO_NODE;
6666715ddf9SLai Jiangshan 
6676715ddf9SLai Jiangshan 	if (!node_state(nid, N_MEMORY))
668d9713679SLai Jiangshan 		arg->status_change_nid = nid;
6698efe33f4SOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
6708efe33f4SOscar Salvador 		arg->status_change_nid_normal = nid;
6718efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM
672d3ba3ae1SBaoquan He 	if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
6738efe33f4SOscar Salvador 		arg->status_change_nid_high = nid;
6748efe33f4SOscar Salvador #endif
675d9713679SLai Jiangshan }
676d9713679SLai Jiangshan 
677d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg)
678d9713679SLai Jiangshan {
679d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
680d9713679SLai Jiangshan 		node_set_state(node, N_NORMAL_MEMORY);
681d9713679SLai Jiangshan 
6826715ddf9SLai Jiangshan 	if (arg->status_change_nid_high >= 0)
683d9713679SLai Jiangshan 		node_set_state(node, N_HIGH_MEMORY);
6846715ddf9SLai Jiangshan 
68583d83612SOscar Salvador 	if (arg->status_change_nid >= 0)
6866715ddf9SLai Jiangshan 		node_set_state(node, N_MEMORY);
687d9713679SLai Jiangshan }
688d9713679SLai Jiangshan 
689f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
690f1dd2cd1SMichal Hocko 		unsigned long nr_pages)
691f1dd2cd1SMichal Hocko {
692f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = zone_end_pfn(zone);
693f1dd2cd1SMichal Hocko 
694f1dd2cd1SMichal Hocko 	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
695f1dd2cd1SMichal Hocko 		zone->zone_start_pfn = start_pfn;
696f1dd2cd1SMichal Hocko 
697f1dd2cd1SMichal Hocko 	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
698f1dd2cd1SMichal Hocko }
699f1dd2cd1SMichal Hocko 
700f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
701f1dd2cd1SMichal Hocko                                      unsigned long nr_pages)
702f1dd2cd1SMichal Hocko {
703f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
704f1dd2cd1SMichal Hocko 
705f1dd2cd1SMichal Hocko 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
706f1dd2cd1SMichal Hocko 		pgdat->node_start_pfn = start_pfn;
707f1dd2cd1SMichal Hocko 
708f1dd2cd1SMichal Hocko 	pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
709f1dd2cd1SMichal Hocko 
7103fccb74cSDavid Hildenbrand }
7113fccb74cSDavid Hildenbrand /*
7123fccb74cSDavid Hildenbrand  * Associate the pfn range with the given zone, initializing the memmaps
7133fccb74cSDavid Hildenbrand  * and resizing the pgdat/zone data to span the added pages. After this
7143fccb74cSDavid Hildenbrand  * call, all affected pages are PG_reserved.
7153fccb74cSDavid Hildenbrand  */
716a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
717a99583e7SChristoph Hellwig 		unsigned long nr_pages, struct vmem_altmap *altmap)
718f1dd2cd1SMichal Hocko {
719f1dd2cd1SMichal Hocko 	struct pglist_data *pgdat = zone->zone_pgdat;
720f1dd2cd1SMichal Hocko 	int nid = pgdat->node_id;
721f1dd2cd1SMichal Hocko 	unsigned long flags;
722f1dd2cd1SMichal Hocko 
723f1dd2cd1SMichal Hocko 	clear_zone_contiguous(zone);
724f1dd2cd1SMichal Hocko 
725f1dd2cd1SMichal Hocko 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
726f1dd2cd1SMichal Hocko 	pgdat_resize_lock(pgdat, &flags);
727f1dd2cd1SMichal Hocko 	zone_span_writelock(zone);
728fa004ab7SWei Yang 	if (zone_is_empty(zone))
729fa004ab7SWei Yang 		init_currently_empty_zone(zone, start_pfn, nr_pages);
730f1dd2cd1SMichal Hocko 	resize_zone_range(zone, start_pfn, nr_pages);
731f1dd2cd1SMichal Hocko 	zone_span_writeunlock(zone);
732f1dd2cd1SMichal Hocko 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
733f1dd2cd1SMichal Hocko 	pgdat_resize_unlock(pgdat, &flags);
734f1dd2cd1SMichal Hocko 
735f1dd2cd1SMichal Hocko 	/*
736f1dd2cd1SMichal Hocko 	 * TODO now we have a visible range of pages which are not associated
737f1dd2cd1SMichal Hocko 	 * with their zone properly. Not nice but set_pfnblock_flags_mask
738f1dd2cd1SMichal Hocko 	 * expects the zone spans the pfn range. All the pages in the range
739f1dd2cd1SMichal Hocko 	 * are reserved so nobody should be touching them so we should be safe
740f1dd2cd1SMichal Hocko 	 */
741a99583e7SChristoph Hellwig 	memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
742a99583e7SChristoph Hellwig 			MEMMAP_HOTPLUG, altmap);
743f1dd2cd1SMichal Hocko 
744f1dd2cd1SMichal Hocko 	set_zone_contiguous(zone);
745f1dd2cd1SMichal Hocko }
746f1dd2cd1SMichal Hocko 
747f1dd2cd1SMichal Hocko /*
748c246a213SMichal Hocko  * Returns a default kernel memory zone for the given pfn range.
749c246a213SMichal Hocko  * If no kernel zone covers this pfn range it will automatically go
750c246a213SMichal Hocko  * to the ZONE_NORMAL.
751c246a213SMichal Hocko  */
752c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
753c246a213SMichal Hocko 		unsigned long nr_pages)
754c246a213SMichal Hocko {
755c246a213SMichal Hocko 	struct pglist_data *pgdat = NODE_DATA(nid);
756c246a213SMichal Hocko 	int zid;
757c246a213SMichal Hocko 
758c246a213SMichal Hocko 	for (zid = 0; zid <= ZONE_NORMAL; zid++) {
759c246a213SMichal Hocko 		struct zone *zone = &pgdat->node_zones[zid];
760c246a213SMichal Hocko 
761c246a213SMichal Hocko 		if (zone_intersects(zone, start_pfn, nr_pages))
762c246a213SMichal Hocko 			return zone;
763c246a213SMichal Hocko 	}
764c246a213SMichal Hocko 
765c246a213SMichal Hocko 	return &pgdat->node_zones[ZONE_NORMAL];
766c246a213SMichal Hocko }
767c246a213SMichal Hocko 
768c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
769c6f03e29SMichal Hocko 		unsigned long nr_pages)
770e5e68930SMichal Hocko {
771c6f03e29SMichal Hocko 	struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
772c6f03e29SMichal Hocko 			nr_pages);
773c6f03e29SMichal Hocko 	struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
774c6f03e29SMichal Hocko 	bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
775c6f03e29SMichal Hocko 	bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
776e5e68930SMichal Hocko 
777e5e68930SMichal Hocko 	/*
778c6f03e29SMichal Hocko 	 * We inherit the existing zone in a simple case where zones do not
779c6f03e29SMichal Hocko 	 * overlap in the given range
780e5e68930SMichal Hocko 	 */
781c6f03e29SMichal Hocko 	if (in_kernel ^ in_movable)
782c6f03e29SMichal Hocko 		return (in_kernel) ? kernel_zone : movable_zone;
783e5e68930SMichal Hocko 
784c6f03e29SMichal Hocko 	/*
785c6f03e29SMichal Hocko 	 * If the range doesn't belong to any zone or two zones overlap in the
786c6f03e29SMichal Hocko 	 * given range then we use movable zone only if movable_node is
787c6f03e29SMichal Hocko 	 * enabled because we always online to a kernel zone by default.
788c6f03e29SMichal Hocko 	 */
789c6f03e29SMichal Hocko 	return movable_node_enabled ? movable_zone : kernel_zone;
7909f123ab5SMichal Hocko }
7919f123ab5SMichal Hocko 
792e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
793e5e68930SMichal Hocko 		unsigned long nr_pages)
794f1dd2cd1SMichal Hocko {
795c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_KERNEL)
796c6f03e29SMichal Hocko 		return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
797f1dd2cd1SMichal Hocko 
798c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_MOVABLE)
799c6f03e29SMichal Hocko 		return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
800f1dd2cd1SMichal Hocko 
801c6f03e29SMichal Hocko 	return default_zone_for_pfn(nid, start_pfn, nr_pages);
802e5e68930SMichal Hocko }
803e5e68930SMichal Hocko 
804511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
80575884fb1SKAMEZAWA Hiroyuki {
806aa47228aSCody P Schafer 	unsigned long flags;
8073947be19SDave Hansen 	unsigned long onlined_pages = 0;
8083947be19SDave Hansen 	struct zone *zone;
8096811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
8107b78d335SYasunori Goto 	int nid;
8117b78d335SYasunori Goto 	int ret;
8127b78d335SYasunori Goto 	struct memory_notify arg;
813d0dc12e8SPavel Tatashin 	struct memory_block *mem;
8143947be19SDave Hansen 
815381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
816381eab4aSDavid Hildenbrand 
817d0dc12e8SPavel Tatashin 	/*
818d0dc12e8SPavel Tatashin 	 * We can't use pfn_to_nid() because nid might be stored in struct page
819d0dc12e8SPavel Tatashin 	 * which is not yet initialized. Instead, we find nid from memory block.
820d0dc12e8SPavel Tatashin 	 */
821d0dc12e8SPavel Tatashin 	mem = find_memory_block(__pfn_to_section(pfn));
822d0dc12e8SPavel Tatashin 	nid = mem->nid;
82389c02e69SDavid Hildenbrand 	put_device(&mem->dev);
824d0dc12e8SPavel Tatashin 
825f1dd2cd1SMichal Hocko 	/* associate pfn range with the zone */
8263fccb74cSDavid Hildenbrand 	zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages);
8273fccb74cSDavid Hildenbrand 	move_pfn_range_to_zone(zone, pfn, nr_pages, NULL);
828511c2abaSLai Jiangshan 
8297b78d335SYasunori Goto 	arg.start_pfn = pfn;
8307b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
831d9713679SLai Jiangshan 	node_states_check_changes_online(nr_pages, zone, &arg);
8327b78d335SYasunori Goto 
8337b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
8347b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
835e33e33b4SChen Yucong 	if (ret)
836e33e33b4SChen Yucong 		goto failed_addition;
837e33e33b4SChen Yucong 
8383947be19SDave Hansen 	/*
8396811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
8406811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
8416811378eSYasunori Goto 	 * So, zonelist must be updated after online.
8426811378eSYasunori Goto 	 */
8436dcd73d7SWen Congyang 	if (!populated_zone(zone)) {
8446811378eSYasunori Goto 		need_zonelists_rebuild = 1;
84572675e13SMichal Hocko 		setup_zone_pageset(zone);
8466dcd73d7SWen Congyang 	}
8476811378eSYasunori Goto 
848908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
84975884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
850fd8a4221SGeoff Levand 	if (ret) {
851ca9a46f8SDavid Hildenbrand 		/* not a single memory resource was applicable */
8526dcd73d7SWen Congyang 		if (need_zonelists_rebuild)
8536dcd73d7SWen Congyang 			zone_pcp_reset(zone);
854e33e33b4SChen Yucong 		goto failed_addition;
855fd8a4221SGeoff Levand 	}
856fd8a4221SGeoff Levand 
8573947be19SDave Hansen 	zone->present_pages += onlined_pages;
858aa47228aSCody P Schafer 
859aa47228aSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
860f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
861aa47228aSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
862aa47228aSCody P Schafer 
863e900a918SDan Williams 	shuffle_zone(zone);
864e900a918SDan Williams 
865e888ca35SVlastimil Babka 	node_states_set_node(nid, &arg);
8661f522509SHaicheng Li 	if (need_zonelists_rebuild)
86772675e13SMichal Hocko 		build_all_zonelists(NULL);
8681f522509SHaicheng Li 	else
869112067f0SShaohua Li 		zone_pcp_update(zone);
8701f522509SHaicheng Li 
8711b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
8721b79acc9SKOSAKI Motohiro 
873e888ca35SVlastimil Babka 	kswapd_run(nid);
874698b1b30SVlastimil Babka 	kcompactd_run(nid);
87561b13993SDave Hansen 
8765a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
8772f7f24ecSKent Liu 
8782d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
8797b78d335SYasunori Goto 
8807b78d335SYasunori Goto 	memory_notify(MEM_ONLINE, &arg);
881381eab4aSDavid Hildenbrand 	mem_hotplug_done();
88230467e0bSDavid Rientjes 	return 0;
883e33e33b4SChen Yucong 
884e33e33b4SChen Yucong failed_addition:
885e33e33b4SChen Yucong 	pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
886e33e33b4SChen Yucong 		 (unsigned long long) pfn << PAGE_SHIFT,
887e33e33b4SChen Yucong 		 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
888e33e33b4SChen Yucong 	memory_notify(MEM_CANCEL_ONLINE, &arg);
889381eab4aSDavid Hildenbrand 	mem_hotplug_done();
890e33e33b4SChen Yucong 	return ret;
8913947be19SDave Hansen }
89253947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
893bc02af93SYasunori Goto 
8940bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat)
8950bd85420STang Chen {
8960bd85420STang Chen 	struct zone *z;
8970bd85420STang Chen 
8980bd85420STang Chen 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
8990bd85420STang Chen 		z->present_pages = 0;
9000bd85420STang Chen 
9010bd85420STang Chen 	pgdat->node_present_pages = 0;
9020bd85420STang Chen }
9030bd85420STang Chen 
904e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
905e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9069af3c2deSYasunori Goto {
9079af3c2deSYasunori Goto 	struct pglist_data *pgdat;
908c8e861a5SFabian Frederick 	unsigned long start_pfn = PFN_DOWN(start);
9099af3c2deSYasunori Goto 
910a1e565aaSTang Chen 	pgdat = NODE_DATA(nid);
911a1e565aaSTang Chen 	if (!pgdat) {
9129af3c2deSYasunori Goto 		pgdat = arch_alloc_nodedata(nid);
9139af3c2deSYasunori Goto 		if (!pgdat)
9149af3c2deSYasunori Goto 			return NULL;
9159af3c2deSYasunori Goto 
91633fce011SWei Yang 		pgdat->per_cpu_nodestats =
91733fce011SWei Yang 			alloc_percpu(struct per_cpu_nodestat);
9189af3c2deSYasunori Goto 		arch_refresh_nodedata(nid, pgdat);
919b0dc3a34SGu Zheng 	} else {
92033fce011SWei Yang 		int cpu;
921e716f2ebSMel Gorman 		/*
922e716f2ebSMel Gorman 		 * Reset the nr_zones, order and classzone_idx before reuse.
923e716f2ebSMel Gorman 		 * Note that kswapd will init kswapd_classzone_idx properly
924e716f2ebSMel Gorman 		 * when it starts in the near future.
925e716f2ebSMel Gorman 		 */
926b0dc3a34SGu Zheng 		pgdat->nr_zones = 0;
92738087d9bSMel Gorman 		pgdat->kswapd_order = 0;
92838087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = 0;
92933fce011SWei Yang 		for_each_online_cpu(cpu) {
93033fce011SWei Yang 			struct per_cpu_nodestat *p;
93133fce011SWei Yang 
93233fce011SWei Yang 			p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
93333fce011SWei Yang 			memset(p, 0, sizeof(*p));
93433fce011SWei Yang 		}
935a1e565aaSTang Chen 	}
9369af3c2deSYasunori Goto 
9379af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
9389af3c2deSYasunori Goto 
93903e85f9dSOscar Salvador 	pgdat->node_id = nid;
94003e85f9dSOscar Salvador 	pgdat->node_start_pfn = start_pfn;
94103e85f9dSOscar Salvador 
9429af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
94303e85f9dSOscar Salvador 	free_area_init_core_hotplug(nid);
9449af3c2deSYasunori Goto 
945959ecc48SKAMEZAWA Hiroyuki 	/*
946959ecc48SKAMEZAWA Hiroyuki 	 * The node we allocated has no zone fallback lists. For avoiding
947959ecc48SKAMEZAWA Hiroyuki 	 * to access not-initialized zonelist, build here.
948959ecc48SKAMEZAWA Hiroyuki 	 */
94972675e13SMichal Hocko 	build_all_zonelists(pgdat);
950959ecc48SKAMEZAWA Hiroyuki 
951f784a3f1STang Chen 	/*
9520bd85420STang Chen 	 * When memory is hot-added, all the memory is in offline state. So
9530bd85420STang Chen 	 * clear all zones' present_pages because they will be updated in
9540bd85420STang Chen 	 * online_pages() and offline_pages().
9550bd85420STang Chen 	 */
95603e85f9dSOscar Salvador 	reset_node_managed_pages(pgdat);
9570bd85420STang Chen 	reset_node_present_pages(pgdat);
9580bd85420STang Chen 
9599af3c2deSYasunori Goto 	return pgdat;
9609af3c2deSYasunori Goto }
9619af3c2deSYasunori Goto 
962b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid)
9639af3c2deSYasunori Goto {
964b9ff0360SOscar Salvador 	pg_data_t *pgdat = NODE_DATA(nid);
965b9ff0360SOscar Salvador 
9669af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
9675830169fSReza Arbab 	free_percpu(pgdat->per_cpu_nodestats);
9689af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
9699af3c2deSYasunori Goto }
9709af3c2deSYasunori Goto 
9710a547039SKAMEZAWA Hiroyuki 
97201b0f197SToshi Kani /**
97301b0f197SToshi Kani  * try_online_node - online a node if offlined
974e8b098fcSMike Rapoport  * @nid: the node ID
975b9ff0360SOscar Salvador  * @start: start addr of the node
976b9ff0360SOscar Salvador  * @set_node_online: Whether we want to online the node
977cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
978b9ff0360SOscar Salvador  *
979b9ff0360SOscar Salvador  * Returns:
980b9ff0360SOscar Salvador  * 1 -> a new node has been allocated
981b9ff0360SOscar Salvador  * 0 -> the node is already online
982b9ff0360SOscar Salvador  * -ENOMEM -> the node could not be allocated
983cf23422bSminskey guo  */
984b9ff0360SOscar Salvador static int __try_online_node(int nid, u64 start, bool set_node_online)
985cf23422bSminskey guo {
986cf23422bSminskey guo 	pg_data_t *pgdat;
987b9ff0360SOscar Salvador 	int ret = 1;
988cf23422bSminskey guo 
98901b0f197SToshi Kani 	if (node_online(nid))
99001b0f197SToshi Kani 		return 0;
99101b0f197SToshi Kani 
992b9ff0360SOscar Salvador 	pgdat = hotadd_new_pgdat(nid, start);
9937553e8f2SDavid Rientjes 	if (!pgdat) {
99401b0f197SToshi Kani 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
995cf23422bSminskey guo 		ret = -ENOMEM;
996cf23422bSminskey guo 		goto out;
997cf23422bSminskey guo 	}
998b9ff0360SOscar Salvador 
999b9ff0360SOscar Salvador 	if (set_node_online) {
1000cf23422bSminskey guo 		node_set_online(nid);
1001cf23422bSminskey guo 		ret = register_one_node(nid);
1002cf23422bSminskey guo 		BUG_ON(ret);
1003b9ff0360SOscar Salvador 	}
1004cf23422bSminskey guo out:
1005b9ff0360SOscar Salvador 	return ret;
1006b9ff0360SOscar Salvador }
1007b9ff0360SOscar Salvador 
1008b9ff0360SOscar Salvador /*
1009b9ff0360SOscar Salvador  * Users of this function always want to online/register the node
1010b9ff0360SOscar Salvador  */
1011b9ff0360SOscar Salvador int try_online_node(int nid)
1012b9ff0360SOscar Salvador {
1013b9ff0360SOscar Salvador 	int ret;
1014b9ff0360SOscar Salvador 
1015b9ff0360SOscar Salvador 	mem_hotplug_begin();
1016b9ff0360SOscar Salvador 	ret =  __try_online_node(nid, 0, true);
1017bfc8c901SVladimir Davydov 	mem_hotplug_done();
1018cf23422bSminskey guo 	return ret;
1019cf23422bSminskey guo }
1020cf23422bSminskey guo 
102127356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size)
102227356f54SToshi Kani {
1023ba325585SPavel Tatashin 	/* memory range must be block size aligned */
1024cec3ebd0SDavid Hildenbrand 	if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
1025cec3ebd0SDavid Hildenbrand 	    !IS_ALIGNED(size, memory_block_size_bytes())) {
1026ba325585SPavel Tatashin 		pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1027cec3ebd0SDavid Hildenbrand 		       memory_block_size_bytes(), start, size);
102827356f54SToshi Kani 		return -EINVAL;
102927356f54SToshi Kani 	}
103027356f54SToshi Kani 
103127356f54SToshi Kani 	return 0;
103227356f54SToshi Kani }
103327356f54SToshi Kani 
103431bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg)
103531bc3858SVitaly Kuznetsov {
1036dc18d706SNathan Fontenot 	return device_online(&mem->dev);
103731bc3858SVitaly Kuznetsov }
103831bc3858SVitaly Kuznetsov 
10398df1d0e4SDavid Hildenbrand /*
10408df1d0e4SDavid Hildenbrand  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
10418df1d0e4SDavid Hildenbrand  * and online/offline operations (triggered e.g. by sysfs).
10428df1d0e4SDavid Hildenbrand  *
10438df1d0e4SDavid Hildenbrand  * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
10448df1d0e4SDavid Hildenbrand  */
1045f29d8e9cSDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res)
1046bc02af93SYasunori Goto {
104705f800a0SDavid Hildenbrand 	struct mhp_restrictions restrictions = {};
104862cedb9fSDavid Vrabel 	u64 start, size;
1049b9ff0360SOscar Salvador 	bool new_node = false;
1050bc02af93SYasunori Goto 	int ret;
1051bc02af93SYasunori Goto 
105262cedb9fSDavid Vrabel 	start = res->start;
105362cedb9fSDavid Vrabel 	size = resource_size(res);
105462cedb9fSDavid Vrabel 
105527356f54SToshi Kani 	ret = check_hotplug_memory_range(start, size);
105627356f54SToshi Kani 	if (ret)
105727356f54SToshi Kani 		return ret;
105827356f54SToshi Kani 
1059bfc8c901SVladimir Davydov 	mem_hotplug_begin();
1060ac13c462SNathan Zimmer 
10617f36e3e5STang Chen 	/*
10627f36e3e5STang Chen 	 * Add new range to memblock so that when hotadd_new_pgdat() is called
10637f36e3e5STang Chen 	 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
10647f36e3e5STang Chen 	 * this new range and calculate total pages correctly.  The range will
10657f36e3e5STang Chen 	 * be removed at hot-remove time.
10667f36e3e5STang Chen 	 */
10677f36e3e5STang Chen 	memblock_add_node(start, size, nid);
10687f36e3e5STang Chen 
1069b9ff0360SOscar Salvador 	ret = __try_online_node(nid, start, false);
1070b9ff0360SOscar Salvador 	if (ret < 0)
107141b9e2d7SWen Congyang 		goto error;
1072b9ff0360SOscar Salvador 	new_node = ret;
10739af3c2deSYasunori Goto 
1074bc02af93SYasunori Goto 	/* call arch's memory hotadd */
1075940519f0SMichal Hocko 	ret = arch_add_memory(nid, start, size, &restrictions);
10769af3c2deSYasunori Goto 	if (ret < 0)
10779af3c2deSYasunori Goto 		goto error;
10789af3c2deSYasunori Goto 
1079db051a0dSDavid Hildenbrand 	/* create memory block devices after memory was added */
1080db051a0dSDavid Hildenbrand 	ret = create_memory_block_devices(start, size);
1081db051a0dSDavid Hildenbrand 	if (ret) {
1082db051a0dSDavid Hildenbrand 		arch_remove_memory(nid, start, size, NULL);
1083db051a0dSDavid Hildenbrand 		goto error;
1084db051a0dSDavid Hildenbrand 	}
1085db051a0dSDavid Hildenbrand 
1086a1e565aaSTang Chen 	if (new_node) {
1087d5b6f6a3SOscar Salvador 		/* If sysfs file of new node can't be created, cpu on the node
10880fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
10890fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
1090d5b6f6a3SOscar Salvador 		 * We online node here. We can't roll back from here.
10910fc44159SYasunori Goto 		 */
1092d5b6f6a3SOscar Salvador 		node_set_online(nid);
1093d5b6f6a3SOscar Salvador 		ret = __register_one_node(nid);
10940fc44159SYasunori Goto 		BUG_ON(ret);
10950fc44159SYasunori Goto 	}
10960fc44159SYasunori Goto 
1097d5b6f6a3SOscar Salvador 	/* link memory sections under this node.*/
10984fbce633SOscar Salvador 	ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
1099d5b6f6a3SOscar Salvador 	BUG_ON(ret);
1100d5b6f6a3SOscar Salvador 
1101d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
1102d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
1103d96ae530Sakpm@linux-foundation.org 
1104381eab4aSDavid Hildenbrand 	/* device_online() will take the lock when calling online_pages() */
1105381eab4aSDavid Hildenbrand 	mem_hotplug_done();
1106381eab4aSDavid Hildenbrand 
110731bc3858SVitaly Kuznetsov 	/* online pages if requested */
1108f29d8e9cSDavid Hildenbrand 	if (memhp_auto_online)
1109fbcf73ceSDavid Hildenbrand 		walk_memory_blocks(start, size, NULL, online_memory_block);
111031bc3858SVitaly Kuznetsov 
1111381eab4aSDavid Hildenbrand 	return ret;
11129af3c2deSYasunori Goto error:
11139af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
1114b9ff0360SOscar Salvador 	if (new_node)
1115b9ff0360SOscar Salvador 		rollback_node_hotadd(nid);
11167f36e3e5STang Chen 	memblock_remove(start, size);
1117bfc8c901SVladimir Davydov 	mem_hotplug_done();
1118bc02af93SYasunori Goto 	return ret;
1119bc02af93SYasunori Goto }
112062cedb9fSDavid Vrabel 
11218df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */
11228df1d0e4SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size)
112362cedb9fSDavid Vrabel {
112462cedb9fSDavid Vrabel 	struct resource *res;
112562cedb9fSDavid Vrabel 	int ret;
112662cedb9fSDavid Vrabel 
112762cedb9fSDavid Vrabel 	res = register_memory_resource(start, size);
11286f754ba4SVitaly Kuznetsov 	if (IS_ERR(res))
11296f754ba4SVitaly Kuznetsov 		return PTR_ERR(res);
113062cedb9fSDavid Vrabel 
1131f29d8e9cSDavid Hildenbrand 	ret = add_memory_resource(nid, res);
113262cedb9fSDavid Vrabel 	if (ret < 0)
113362cedb9fSDavid Vrabel 		release_memory_resource(res);
113462cedb9fSDavid Vrabel 	return ret;
113562cedb9fSDavid Vrabel }
11368df1d0e4SDavid Hildenbrand 
11378df1d0e4SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size)
11388df1d0e4SDavid Hildenbrand {
11398df1d0e4SDavid Hildenbrand 	int rc;
11408df1d0e4SDavid Hildenbrand 
11418df1d0e4SDavid Hildenbrand 	lock_device_hotplug();
11428df1d0e4SDavid Hildenbrand 	rc = __add_memory(nid, start, size);
11438df1d0e4SDavid Hildenbrand 	unlock_device_hotplug();
11448df1d0e4SDavid Hildenbrand 
11458df1d0e4SDavid Hildenbrand 	return rc;
11468df1d0e4SDavid Hildenbrand }
1147bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
11480c0e6195SKAMEZAWA Hiroyuki 
11490c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
11500c0e6195SKAMEZAWA Hiroyuki /*
11515c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
11525c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
11535c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
11545c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
11555c755e9fSBadari Pulavarty  * be located at the start of the pageblock
11565c755e9fSBadari Pulavarty  */
11575c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
11585c755e9fSBadari Pulavarty {
11595c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
11605c755e9fSBadari Pulavarty }
11615c755e9fSBadari Pulavarty 
1162891cb2a7SMichal Hocko /* Return the pfn of the start of the next active pageblock after a given pfn */
1163891cb2a7SMichal Hocko static unsigned long next_active_pageblock(unsigned long pfn)
11645c755e9fSBadari Pulavarty {
1165891cb2a7SMichal Hocko 	struct page *page = pfn_to_page(pfn);
1166891cb2a7SMichal Hocko 
11675c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
1168891cb2a7SMichal Hocko 	BUG_ON(pfn & (pageblock_nr_pages - 1));
11695c755e9fSBadari Pulavarty 
11705c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
11710dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
11720dcc48c1SKAMEZAWA Hiroyuki 		int order;
11730dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
11740dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
11750dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
1176891cb2a7SMichal Hocko 			return pfn + (1 << order);
11770dcc48c1SKAMEZAWA Hiroyuki 	}
11785c755e9fSBadari Pulavarty 
1179891cb2a7SMichal Hocko 	return pfn + pageblock_nr_pages;
11805c755e9fSBadari Pulavarty }
11815c755e9fSBadari Pulavarty 
1182891cb2a7SMichal Hocko static bool is_pageblock_removable_nolock(unsigned long pfn)
1183fb52bbaeSMathieu Malaterre {
1184891cb2a7SMichal Hocko 	struct page *page = pfn_to_page(pfn);
1185fb52bbaeSMathieu Malaterre 	struct zone *zone;
1186fb52bbaeSMathieu Malaterre 
1187fb52bbaeSMathieu Malaterre 	/*
1188fb52bbaeSMathieu Malaterre 	 * We have to be careful here because we are iterating over memory
1189fb52bbaeSMathieu Malaterre 	 * sections which are not zone aware so we might end up outside of
1190fb52bbaeSMathieu Malaterre 	 * the zone but still within the section.
1191fb52bbaeSMathieu Malaterre 	 * We have to take care about the node as well. If the node is offline
1192fb52bbaeSMathieu Malaterre 	 * its NODE_DATA will be NULL - see page_zone.
1193fb52bbaeSMathieu Malaterre 	 */
1194fb52bbaeSMathieu Malaterre 	if (!node_online(page_to_nid(page)))
1195fb52bbaeSMathieu Malaterre 		return false;
1196fb52bbaeSMathieu Malaterre 
1197fb52bbaeSMathieu Malaterre 	zone = page_zone(page);
1198fb52bbaeSMathieu Malaterre 	pfn = page_to_pfn(page);
1199fb52bbaeSMathieu Malaterre 	if (!zone_spans_pfn(zone, pfn))
1200fb52bbaeSMathieu Malaterre 		return false;
1201fb52bbaeSMathieu Malaterre 
1202d381c547SMichal Hocko 	return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON);
1203fb52bbaeSMathieu Malaterre }
1204fb52bbaeSMathieu Malaterre 
12055c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
1206c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
12075c755e9fSBadari Pulavarty {
1208891cb2a7SMichal Hocko 	unsigned long end_pfn, pfn;
1209891cb2a7SMichal Hocko 
1210891cb2a7SMichal Hocko 	end_pfn = min(start_pfn + nr_pages,
1211891cb2a7SMichal Hocko 			zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
12125c755e9fSBadari Pulavarty 
12135c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
1214891cb2a7SMichal Hocko 	for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1215891cb2a7SMichal Hocko 		if (!is_pageblock_removable_nolock(pfn))
1216c98940f6SYaowei Bai 			return false;
121749ac8255SKAMEZAWA Hiroyuki 		cond_resched();
12185c755e9fSBadari Pulavarty 	}
12195c755e9fSBadari Pulavarty 
12205c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
1221c98940f6SYaowei Bai 	return true;
12225c755e9fSBadari Pulavarty }
12235c755e9fSBadari Pulavarty 
12245c755e9fSBadari Pulavarty /*
1225deb88a2aSToshi Kani  * Confirm all pages in a range [start, end) belong to the same zone.
1226a96dfddbSToshi Kani  * When true, return its valid [start, end).
12270c0e6195SKAMEZAWA Hiroyuki  */
1228a96dfddbSToshi Kani int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1229a96dfddbSToshi Kani 			 unsigned long *valid_start, unsigned long *valid_end)
12300c0e6195SKAMEZAWA Hiroyuki {
12315f0f2887SAndrew Banman 	unsigned long pfn, sec_end_pfn;
1232a96dfddbSToshi Kani 	unsigned long start, end;
12330c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
12340c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12350c0e6195SKAMEZAWA Hiroyuki 	int i;
1236deb88a2aSToshi Kani 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
12370c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
1238deb88a2aSToshi Kani 	     pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
12395f0f2887SAndrew Banman 		/* Make sure the memory section is present first */
12405f0f2887SAndrew Banman 		if (!present_section_nr(pfn_to_section_nr(pfn)))
12415f0f2887SAndrew Banman 			continue;
12425f0f2887SAndrew Banman 		for (; pfn < sec_end_pfn && pfn < end_pfn;
12430c0e6195SKAMEZAWA Hiroyuki 		     pfn += MAX_ORDER_NR_PAGES) {
12440c0e6195SKAMEZAWA Hiroyuki 			i = 0;
12450c0e6195SKAMEZAWA Hiroyuki 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
12465f0f2887SAndrew Banman 			while ((i < MAX_ORDER_NR_PAGES) &&
12475f0f2887SAndrew Banman 				!pfn_valid_within(pfn + i))
12480c0e6195SKAMEZAWA Hiroyuki 				i++;
1249d6d8c8a4Szhong jiang 			if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
12500c0e6195SKAMEZAWA Hiroyuki 				continue;
125124feb47cSMikhail Zaslonko 			/* Check if we got outside of the zone */
125224feb47cSMikhail Zaslonko 			if (zone && !zone_spans_pfn(zone, pfn + i))
125324feb47cSMikhail Zaslonko 				return 0;
12540c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn + i);
12550c0e6195SKAMEZAWA Hiroyuki 			if (zone && page_zone(page) != zone)
12560c0e6195SKAMEZAWA Hiroyuki 				return 0;
1257a96dfddbSToshi Kani 			if (!zone)
1258a96dfddbSToshi Kani 				start = pfn + i;
12590c0e6195SKAMEZAWA Hiroyuki 			zone = page_zone(page);
1260a96dfddbSToshi Kani 			end = pfn + MAX_ORDER_NR_PAGES;
12610c0e6195SKAMEZAWA Hiroyuki 		}
12625f0f2887SAndrew Banman 	}
1263deb88a2aSToshi Kani 
1264a96dfddbSToshi Kani 	if (zone) {
1265a96dfddbSToshi Kani 		*valid_start = start;
1266d6d8c8a4Szhong jiang 		*valid_end = min(end, end_pfn);
12670c0e6195SKAMEZAWA Hiroyuki 		return 1;
1268a96dfddbSToshi Kani 	} else {
1269deb88a2aSToshi Kani 		return 0;
12700c0e6195SKAMEZAWA Hiroyuki 	}
1271a96dfddbSToshi Kani }
12720c0e6195SKAMEZAWA Hiroyuki 
12730c0e6195SKAMEZAWA Hiroyuki /*
12740efadf48SYisheng Xie  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
12750efadf48SYisheng Xie  * non-lru movable pages and hugepages). We scan pfn because it's much
12760efadf48SYisheng Xie  * easier than scanning over linked list. This function returns the pfn
12770efadf48SYisheng Xie  * of the first found movable page if it's found, otherwise 0.
12780c0e6195SKAMEZAWA Hiroyuki  */
1279c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
12800c0e6195SKAMEZAWA Hiroyuki {
12810c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
1282eeb0efd0SOscar Salvador 
12830c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
1284eeb0efd0SOscar Salvador 		struct page *page, *head;
1285eeb0efd0SOscar Salvador 		unsigned long skip;
1286eeb0efd0SOscar Salvador 
1287eeb0efd0SOscar Salvador 		if (!pfn_valid(pfn))
1288eeb0efd0SOscar Salvador 			continue;
12890c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
12900c0e6195SKAMEZAWA Hiroyuki 		if (PageLRU(page))
12910c0e6195SKAMEZAWA Hiroyuki 			return pfn;
12920efadf48SYisheng Xie 		if (__PageMovable(page))
12930efadf48SYisheng Xie 			return pfn;
1294eeb0efd0SOscar Salvador 
1295eeb0efd0SOscar Salvador 		if (!PageHuge(page))
1296eeb0efd0SOscar Salvador 			continue;
1297eeb0efd0SOscar Salvador 		head = compound_head(page);
129839186cbeSOscar Salvador 		if (page_huge_active(head))
1299c8721bbbSNaoya Horiguchi 			return pfn;
1300d8c6546bSMatthew Wilcox (Oracle) 		skip = compound_nr(head) - (page - head);
1301eeb0efd0SOscar Salvador 		pfn += skip - 1;
13020c0e6195SKAMEZAWA Hiroyuki 	}
13030c0e6195SKAMEZAWA Hiroyuki 	return 0;
13040c0e6195SKAMEZAWA Hiroyuki }
13050c0e6195SKAMEZAWA Hiroyuki 
1306666feb21SMichal Hocko static struct page *new_node_page(struct page *page, unsigned long private)
1307394e31d2SXishi Qiu {
1308394e31d2SXishi Qiu 	int nid = page_to_nid(page);
1309231e97e2SLi Zhong 	nodemask_t nmask = node_states[N_MEMORY];
13107f252f27SMichal Hocko 
13117f252f27SMichal Hocko 	/*
13127f252f27SMichal Hocko 	 * try to allocate from a different node but reuse this node if there
13137f252f27SMichal Hocko 	 * are no other online nodes to be used (e.g. we are offlining a part
13147f252f27SMichal Hocko 	 * of the only existing node)
13157f252f27SMichal Hocko 	 */
13167f252f27SMichal Hocko 	node_clear(nid, nmask);
13177f252f27SMichal Hocko 	if (nodes_empty(nmask))
13187f252f27SMichal Hocko 		node_set(nid, nmask);
1319394e31d2SXishi Qiu 
13208b913238SMichal Hocko 	return new_page_nodemask(page, nid, &nmask);
1321394e31d2SXishi Qiu }
1322394e31d2SXishi Qiu 
13230c0e6195SKAMEZAWA Hiroyuki static int
13240c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
13250c0e6195SKAMEZAWA Hiroyuki {
13260c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
13270c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
13280c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
13290c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
13300c0e6195SKAMEZAWA Hiroyuki 
1331a85009c3SMichal Hocko 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
13320c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
13330c0e6195SKAMEZAWA Hiroyuki 			continue;
13340c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
1335c8721bbbSNaoya Horiguchi 
1336c8721bbbSNaoya Horiguchi 		if (PageHuge(page)) {
1337c8721bbbSNaoya Horiguchi 			struct page *head = compound_head(page);
1338d8c6546bSMatthew Wilcox (Oracle) 			pfn = page_to_pfn(head) + compound_nr(head) - 1;
1339daf3538aSOscar Salvador 			isolate_huge_page(head, &source);
1340c8721bbbSNaoya Horiguchi 			continue;
134194723aafSMichal Hocko 		} else if (PageTransHuge(page))
13428135d892SNaoya Horiguchi 			pfn = page_to_pfn(compound_head(page))
13438135d892SNaoya Horiguchi 				+ hpage_nr_pages(page) - 1;
1344c8721bbbSNaoya Horiguchi 
1345b15c8726SMichal Hocko 		/*
1346b15c8726SMichal Hocko 		 * HWPoison pages have elevated reference counts so the migration would
1347b15c8726SMichal Hocko 		 * fail on them. It also doesn't make any sense to migrate them in the
1348b15c8726SMichal Hocko 		 * first place. Still try to unmap such a page in case it is still mapped
1349b15c8726SMichal Hocko 		 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1350b15c8726SMichal Hocko 		 * the unmap as the catch all safety net).
1351b15c8726SMichal Hocko 		 */
1352b15c8726SMichal Hocko 		if (PageHWPoison(page)) {
1353b15c8726SMichal Hocko 			if (WARN_ON(PageLRU(page)))
1354b15c8726SMichal Hocko 				isolate_lru_page(page);
1355b15c8726SMichal Hocko 			if (page_mapped(page))
1356b15c8726SMichal Hocko 				try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1357b15c8726SMichal Hocko 			continue;
1358b15c8726SMichal Hocko 		}
1359b15c8726SMichal Hocko 
1360700c2a46SKonstantin Khlebnikov 		if (!get_page_unless_zero(page))
13610c0e6195SKAMEZAWA Hiroyuki 			continue;
13620c0e6195SKAMEZAWA Hiroyuki 		/*
13630efadf48SYisheng Xie 		 * We can skip free pages. And we can deal with pages on
13640efadf48SYisheng Xie 		 * LRU and non-lru movable pages.
13650c0e6195SKAMEZAWA Hiroyuki 		 */
13660efadf48SYisheng Xie 		if (PageLRU(page))
136762695a84SNick Piggin 			ret = isolate_lru_page(page);
13680efadf48SYisheng Xie 		else
13690efadf48SYisheng Xie 			ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
13700c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
137162695a84SNick Piggin 			list_add_tail(&page->lru, &source);
13720efadf48SYisheng Xie 			if (!__PageMovable(page))
1373599d0c95SMel Gorman 				inc_node_page_state(page, NR_ISOLATED_ANON +
13746d9c285aSKOSAKI Motohiro 						    page_is_file_cache(page));
13756d9c285aSKOSAKI Motohiro 
13760c0e6195SKAMEZAWA Hiroyuki 		} else {
13772932c8b0SMichal Hocko 			pr_warn("failed to isolate pfn %lx\n", pfn);
13780efadf48SYisheng Xie 			dump_page(page, "isolation failed");
13791723058eSOscar Salvador 		}
1380700c2a46SKonstantin Khlebnikov 		put_page(page);
13810c0e6195SKAMEZAWA Hiroyuki 	}
1382f3ab2636SBob Liu 	if (!list_empty(&source)) {
1383394e31d2SXishi Qiu 		/* Allocate a new page from the nearest neighbor node */
1384394e31d2SXishi Qiu 		ret = migrate_pages(&source, new_node_page, NULL, 0,
13859c620e2bSHugh Dickins 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
13862932c8b0SMichal Hocko 		if (ret) {
13872932c8b0SMichal Hocko 			list_for_each_entry(page, &source, lru) {
13882932c8b0SMichal Hocko 				pr_warn("migrating pfn %lx failed ret:%d ",
13892932c8b0SMichal Hocko 				       page_to_pfn(page), ret);
13902932c8b0SMichal Hocko 				dump_page(page, "migration failure");
13912932c8b0SMichal Hocko 			}
1392c8721bbbSNaoya Horiguchi 			putback_movable_pages(&source);
1393f3ab2636SBob Liu 		}
13942932c8b0SMichal Hocko 	}
13951723058eSOscar Salvador 
13960c0e6195SKAMEZAWA Hiroyuki 	return ret;
13970c0e6195SKAMEZAWA Hiroyuki }
13980c0e6195SKAMEZAWA Hiroyuki 
13990c0e6195SKAMEZAWA Hiroyuki /*
14000c0e6195SKAMEZAWA Hiroyuki  * remove from free_area[] and mark all as Reserved.
14010c0e6195SKAMEZAWA Hiroyuki  */
14020c0e6195SKAMEZAWA Hiroyuki static int
14030c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
14040c0e6195SKAMEZAWA Hiroyuki 			void *data)
14050c0e6195SKAMEZAWA Hiroyuki {
14065557c766SMichal Hocko 	unsigned long *offlined_pages = (unsigned long *)data;
14070c0e6195SKAMEZAWA Hiroyuki 
14085557c766SMichal Hocko 	*offlined_pages += __offline_isolated_pages(start, start + nr_pages);
14095557c766SMichal Hocko 	return 0;
14100c0e6195SKAMEZAWA Hiroyuki }
14110c0e6195SKAMEZAWA Hiroyuki 
14120c0e6195SKAMEZAWA Hiroyuki /*
14130c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
14140c0e6195SKAMEZAWA Hiroyuki  */
14150c0e6195SKAMEZAWA Hiroyuki static int
14160c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
14170c0e6195SKAMEZAWA Hiroyuki 			void *data)
14180c0e6195SKAMEZAWA Hiroyuki {
14195557c766SMichal Hocko 	return test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
14200c0e6195SKAMEZAWA Hiroyuki }
14210c0e6195SKAMEZAWA Hiroyuki 
1422c5320926STang Chen static int __init cmdline_parse_movable_node(char *p)
1423c5320926STang Chen {
14244932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
142555ac590cSTang Chen 	movable_node_enabled = true;
14264932381eSMichal Hocko #else
14274932381eSMichal Hocko 	pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
14284932381eSMichal Hocko #endif
1429c5320926STang Chen 	return 0;
1430c5320926STang Chen }
1431c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node);
1432c5320926STang Chen 
1433d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */
1434d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages,
1435d9713679SLai Jiangshan 		struct zone *zone, struct memory_notify *arg)
1436d9713679SLai Jiangshan {
1437d9713679SLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
1438d9713679SLai Jiangshan 	unsigned long present_pages = 0;
143986b27beaSOscar Salvador 	enum zone_type zt;
1440d9713679SLai Jiangshan 
144198fa15f3SAnshuman Khandual 	arg->status_change_nid = NUMA_NO_NODE;
144298fa15f3SAnshuman Khandual 	arg->status_change_nid_normal = NUMA_NO_NODE;
144398fa15f3SAnshuman Khandual 	arg->status_change_nid_high = NUMA_NO_NODE;
144486b27beaSOscar Salvador 
144586b27beaSOscar Salvador 	/*
144686b27beaSOscar Salvador 	 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
144786b27beaSOscar Salvador 	 * If the memory to be offline is within the range
144886b27beaSOscar Salvador 	 * [0..ZONE_NORMAL], and it is the last present memory there,
144986b27beaSOscar Salvador 	 * the zones in that range will become empty after the offlining,
145086b27beaSOscar Salvador 	 * thus we can determine that we need to clear the node from
145186b27beaSOscar Salvador 	 * node_states[N_NORMAL_MEMORY].
145286b27beaSOscar Salvador 	 */
145386b27beaSOscar Salvador 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
145486b27beaSOscar Salvador 		present_pages += pgdat->node_zones[zt].present_pages;
145586b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
145686b27beaSOscar Salvador 		arg->status_change_nid_normal = zone_to_nid(zone);
1457d9713679SLai Jiangshan 
14586715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
14596715ddf9SLai Jiangshan 	/*
146086b27beaSOscar Salvador 	 * node_states[N_HIGH_MEMORY] contains nodes which
146186b27beaSOscar Salvador 	 * have normal memory or high memory.
146286b27beaSOscar Salvador 	 * Here we add the present_pages belonging to ZONE_HIGHMEM.
146386b27beaSOscar Salvador 	 * If the zone is within the range of [0..ZONE_HIGHMEM), and
146486b27beaSOscar Salvador 	 * we determine that the zones in that range become empty,
146586b27beaSOscar Salvador 	 * we need to clear the node for N_HIGH_MEMORY.
14666715ddf9SLai Jiangshan 	 */
146786b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
146886b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
14696715ddf9SLai Jiangshan 		arg->status_change_nid_high = zone_to_nid(zone);
14706715ddf9SLai Jiangshan #endif
14716715ddf9SLai Jiangshan 
1472d9713679SLai Jiangshan 	/*
147386b27beaSOscar Salvador 	 * We have accounted the pages from [0..ZONE_NORMAL), and
147486b27beaSOscar Salvador 	 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
147586b27beaSOscar Salvador 	 * as well.
147686b27beaSOscar Salvador 	 * Here we count the possible pages from ZONE_MOVABLE.
147786b27beaSOscar Salvador 	 * If after having accounted all the pages, we see that the nr_pages
147886b27beaSOscar Salvador 	 * to be offlined is over or equal to the accounted pages,
147986b27beaSOscar Salvador 	 * we know that the node will become empty, and so, we can clear
148086b27beaSOscar Salvador 	 * it for N_MEMORY as well.
1481d9713679SLai Jiangshan 	 */
148286b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1483d9713679SLai Jiangshan 
1484d9713679SLai Jiangshan 	if (nr_pages >= present_pages)
1485d9713679SLai Jiangshan 		arg->status_change_nid = zone_to_nid(zone);
1486d9713679SLai Jiangshan }
1487d9713679SLai Jiangshan 
1488d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg)
1489d9713679SLai Jiangshan {
1490d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
1491d9713679SLai Jiangshan 		node_clear_state(node, N_NORMAL_MEMORY);
1492d9713679SLai Jiangshan 
1493cf01f6f5SOscar Salvador 	if (arg->status_change_nid_high >= 0)
1494d9713679SLai Jiangshan 		node_clear_state(node, N_HIGH_MEMORY);
14956715ddf9SLai Jiangshan 
1496cf01f6f5SOscar Salvador 	if (arg->status_change_nid >= 0)
14976715ddf9SLai Jiangshan 		node_clear_state(node, N_MEMORY);
1498d9713679SLai Jiangshan }
1499d9713679SLai Jiangshan 
1500a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn,
1501ecde0f3eSMichal Hocko 		  unsigned long end_pfn)
15020c0e6195SKAMEZAWA Hiroyuki {
1503ecde0f3eSMichal Hocko 	unsigned long pfn, nr_pages;
15045557c766SMichal Hocko 	unsigned long offlined_pages = 0;
15059b7ea46aSQian Cai 	int ret, node, nr_isolate_pageblock;
1506d702909fSCody P Schafer 	unsigned long flags;
1507a96dfddbSToshi Kani 	unsigned long valid_start, valid_end;
15080c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
15097b78d335SYasunori Goto 	struct memory_notify arg;
151079605093SMichal Hocko 	char *reason;
15110c0e6195SKAMEZAWA Hiroyuki 
1512381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
1513381eab4aSDavid Hildenbrand 
15140c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
15150c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
1516381eab4aSDavid Hildenbrand 	if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1517381eab4aSDavid Hildenbrand 				  &valid_end)) {
151879605093SMichal Hocko 		ret = -EINVAL;
151979605093SMichal Hocko 		reason = "multizone range";
152079605093SMichal Hocko 		goto failed_removal;
1521381eab4aSDavid Hildenbrand 	}
15227b78d335SYasunori Goto 
1523a96dfddbSToshi Kani 	zone = page_zone(pfn_to_page(valid_start));
15247b78d335SYasunori Goto 	node = zone_to_nid(zone);
15257b78d335SYasunori Goto 	nr_pages = end_pfn - start_pfn;
15267b78d335SYasunori Goto 
15270c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
1528b023f468SWen Congyang 	ret = start_isolate_page_range(start_pfn, end_pfn,
1529d381c547SMichal Hocko 				       MIGRATE_MOVABLE,
1530d381c547SMichal Hocko 				       SKIP_HWPOISON | REPORT_FAILURE);
15319b7ea46aSQian Cai 	if (ret < 0) {
153279605093SMichal Hocko 		reason = "failure to isolate range";
153379605093SMichal Hocko 		goto failed_removal;
1534381eab4aSDavid Hildenbrand 	}
15359b7ea46aSQian Cai 	nr_isolate_pageblock = ret;
15367b78d335SYasunori Goto 
15377b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
15387b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
1539d9713679SLai Jiangshan 	node_states_check_changes_offline(nr_pages, zone, &arg);
15407b78d335SYasunori Goto 
15417b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
15427b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
154379605093SMichal Hocko 	if (ret) {
154479605093SMichal Hocko 		reason = "notifier failure";
154579605093SMichal Hocko 		goto failed_removal_isolated;
154679605093SMichal Hocko 	}
15477b78d335SYasunori Goto 
1548bb8965bdSMichal Hocko 	do {
1549bb8965bdSMichal Hocko 		for (pfn = start_pfn; pfn;) {
155079605093SMichal Hocko 			if (signal_pending(current)) {
1551bb8965bdSMichal Hocko 				ret = -EINTR;
155279605093SMichal Hocko 				reason = "signal backoff";
155379605093SMichal Hocko 				goto failed_removal_isolated;
155479605093SMichal Hocko 			}
155572b39cfcSMichal Hocko 
15560c0e6195SKAMEZAWA Hiroyuki 			cond_resched();
15579852a721SMichal Hocko 			lru_add_drain_all();
15580c0e6195SKAMEZAWA Hiroyuki 
1559bb8965bdSMichal Hocko 			pfn = scan_movable_pages(pfn, end_pfn);
1560bb8965bdSMichal Hocko 			if (pfn) {
1561bb8965bdSMichal Hocko 				/*
1562bb8965bdSMichal Hocko 				 * TODO: fatal migration failures should bail
1563bb8965bdSMichal Hocko 				 * out
1564bb8965bdSMichal Hocko 				 */
1565bb8965bdSMichal Hocko 				do_migrate_range(pfn, end_pfn);
1566bb8965bdSMichal Hocko 			}
15670c0e6195SKAMEZAWA Hiroyuki 		}
156872b39cfcSMichal Hocko 
1569c8721bbbSNaoya Horiguchi 		/*
1570bb8965bdSMichal Hocko 		 * Dissolve free hugepages in the memory block before doing
1571bb8965bdSMichal Hocko 		 * offlining actually in order to make hugetlbfs's object
1572bb8965bdSMichal Hocko 		 * counting consistent.
1573c8721bbbSNaoya Horiguchi 		 */
1574082d5b6bSGerald Schaefer 		ret = dissolve_free_huge_pages(start_pfn, end_pfn);
157579605093SMichal Hocko 		if (ret) {
157679605093SMichal Hocko 			reason = "failure to dissolve huge pages";
157779605093SMichal Hocko 			goto failed_removal_isolated;
157879605093SMichal Hocko 		}
15790c0e6195SKAMEZAWA Hiroyuki 		/* check again */
15805557c766SMichal Hocko 		ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
15815557c766SMichal Hocko 					    NULL, check_pages_isolated_cb);
15825557c766SMichal Hocko 	} while (ret);
1583bb8965bdSMichal Hocko 
1584b3834be5SAdam Buchbinder 	/* Ok, all of our target is isolated.
15850c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
15865557c766SMichal Hocko 	walk_system_ram_range(start_pfn, end_pfn - start_pfn,
15875557c766SMichal Hocko 			      &offlined_pages, offline_isolated_pages_cb);
15885557c766SMichal Hocko 	pr_info("Offlined Pages %ld\n", offlined_pages);
15899b7ea46aSQian Cai 	/*
15909b7ea46aSQian Cai 	 * Onlining will reset pagetype flags and makes migrate type
15919b7ea46aSQian Cai 	 * MOVABLE, so just need to decrease the number of isolated
15929b7ea46aSQian Cai 	 * pageblocks zone counter here.
15939b7ea46aSQian Cai 	 */
15949b7ea46aSQian Cai 	spin_lock_irqsave(&zone->lock, flags);
15959b7ea46aSQian Cai 	zone->nr_isolate_pageblock -= nr_isolate_pageblock;
15969b7ea46aSQian Cai 	spin_unlock_irqrestore(&zone->lock, flags);
15979b7ea46aSQian Cai 
15980c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
15993dcc0571SJiang Liu 	adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
16000c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
1601d702909fSCody P Schafer 
1602d702909fSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
16030c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
1604d702909fSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
16057b78d335SYasunori Goto 
16061b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
16071b79acc9SKOSAKI Motohiro 
16081e8537baSXishi Qiu 	if (!populated_zone(zone)) {
1609340175b7SJiang Liu 		zone_pcp_reset(zone);
161072675e13SMichal Hocko 		build_all_zonelists(NULL);
16111e8537baSXishi Qiu 	} else
16121e8537baSXishi Qiu 		zone_pcp_update(zone);
1613340175b7SJiang Liu 
1614d9713679SLai Jiangshan 	node_states_clear_node(node, &arg);
1615698b1b30SVlastimil Babka 	if (arg.status_change_nid >= 0) {
16168fe23e05SDavid Rientjes 		kswapd_stop(node);
1617698b1b30SVlastimil Babka 		kcompactd_stop(node);
1618698b1b30SVlastimil Babka 	}
1619bce7394aSMinchan Kim 
16200c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
16210c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
16227b78d335SYasunori Goto 
16237b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
1624381eab4aSDavid Hildenbrand 	mem_hotplug_done();
16250c0e6195SKAMEZAWA Hiroyuki 	return 0;
16260c0e6195SKAMEZAWA Hiroyuki 
162779605093SMichal Hocko failed_removal_isolated:
162879605093SMichal Hocko 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1629c4efe484SQian Cai 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
16300c0e6195SKAMEZAWA Hiroyuki failed_removal:
163179605093SMichal Hocko 	pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1632a62e2f4fSBjorn Helgaas 		 (unsigned long long) start_pfn << PAGE_SHIFT,
163379605093SMichal Hocko 		 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
163479605093SMichal Hocko 		 reason);
16350c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
1636381eab4aSDavid Hildenbrand 	mem_hotplug_done();
16370c0e6195SKAMEZAWA Hiroyuki 	return ret;
16380c0e6195SKAMEZAWA Hiroyuki }
163971088785SBadari Pulavarty 
1640a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1641a16cee10SWen Congyang {
1642ecde0f3eSMichal Hocko 	return __offline_pages(start_pfn, start_pfn + nr_pages);
1643a16cee10SWen Congyang }
1644a16cee10SWen Congyang 
1645d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1646bbc76be6SWen Congyang {
1647bbc76be6SWen Congyang 	int ret = !is_memblock_offlined(mem);
1648bbc76be6SWen Congyang 
1649349daa0fSRandy Dunlap 	if (unlikely(ret)) {
1650349daa0fSRandy Dunlap 		phys_addr_t beginpa, endpa;
1651349daa0fSRandy Dunlap 
1652349daa0fSRandy Dunlap 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1653b6c88d3bSDavid Hildenbrand 		endpa = beginpa + memory_block_size_bytes() - 1;
1654756a025fSJoe Perches 		pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1655349daa0fSRandy Dunlap 			&beginpa, &endpa);
1656bbc76be6SWen Congyang 
1657eca499abSPavel Tatashin 		return -EBUSY;
1658eca499abSPavel Tatashin 	}
1659eca499abSPavel Tatashin 	return 0;
1660bbc76be6SWen Congyang }
1661bbc76be6SWen Congyang 
16620f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat)
166360a5a19eSTang Chen {
166460a5a19eSTang Chen 	int cpu;
166560a5a19eSTang Chen 
166660a5a19eSTang Chen 	for_each_present_cpu(cpu) {
166760a5a19eSTang Chen 		if (cpu_to_node(cpu) == pgdat->node_id)
166860a5a19eSTang Chen 			/*
166960a5a19eSTang Chen 			 * the cpu on this node isn't removed, and we can't
167060a5a19eSTang Chen 			 * offline this node.
167160a5a19eSTang Chen 			 */
167260a5a19eSTang Chen 			return -EBUSY;
167360a5a19eSTang Chen 	}
167460a5a19eSTang Chen 
167560a5a19eSTang Chen 	return 0;
167660a5a19eSTang Chen }
167760a5a19eSTang Chen 
16782c91f8fcSDavid Hildenbrand static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
16792c91f8fcSDavid Hildenbrand {
16802c91f8fcSDavid Hildenbrand 	int nid = *(int *)arg;
16812c91f8fcSDavid Hildenbrand 
16822c91f8fcSDavid Hildenbrand 	/*
16832c91f8fcSDavid Hildenbrand 	 * If a memory block belongs to multiple nodes, the stored nid is not
16842c91f8fcSDavid Hildenbrand 	 * reliable. However, such blocks are always online (e.g., cannot get
16852c91f8fcSDavid Hildenbrand 	 * offlined) and, therefore, are still spanned by the node.
16862c91f8fcSDavid Hildenbrand 	 */
16872c91f8fcSDavid Hildenbrand 	return mem->nid == nid ? -EEXIST : 0;
16882c91f8fcSDavid Hildenbrand }
16892c91f8fcSDavid Hildenbrand 
16900f1cfe9dSToshi Kani /**
16910f1cfe9dSToshi Kani  * try_offline_node
1692e8b098fcSMike Rapoport  * @nid: the node ID
16930f1cfe9dSToshi Kani  *
16940f1cfe9dSToshi Kani  * Offline a node if all memory sections and cpus of the node are removed.
16950f1cfe9dSToshi Kani  *
16960f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
16970f1cfe9dSToshi Kani  * and online/offline operations before this call.
16980f1cfe9dSToshi Kani  */
169990b30cdcSWen Congyang void try_offline_node(int nid)
170060a5a19eSTang Chen {
1701d822b86aSWen Congyang 	pg_data_t *pgdat = NODE_DATA(nid);
17022c91f8fcSDavid Hildenbrand 	int rc;
170360a5a19eSTang Chen 
170460a5a19eSTang Chen 	/*
17052c91f8fcSDavid Hildenbrand 	 * If the node still spans pages (especially ZONE_DEVICE), don't
17062c91f8fcSDavid Hildenbrand 	 * offline it. A node spans memory after move_pfn_range_to_zone(),
17072c91f8fcSDavid Hildenbrand 	 * e.g., after the memory block was onlined.
170860a5a19eSTang Chen 	 */
17092c91f8fcSDavid Hildenbrand 	if (pgdat->node_spanned_pages)
171060a5a19eSTang Chen 		return;
17112c91f8fcSDavid Hildenbrand 
17122c91f8fcSDavid Hildenbrand 	/*
17132c91f8fcSDavid Hildenbrand 	 * Especially offline memory blocks might not be spanned by the
17142c91f8fcSDavid Hildenbrand 	 * node. They will get spanned by the node once they get onlined.
17152c91f8fcSDavid Hildenbrand 	 * However, they link to the node in sysfs and can get onlined later.
17162c91f8fcSDavid Hildenbrand 	 */
17172c91f8fcSDavid Hildenbrand 	rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
17182c91f8fcSDavid Hildenbrand 	if (rc)
17192c91f8fcSDavid Hildenbrand 		return;
172060a5a19eSTang Chen 
172146a3679bSMichal Hocko 	if (check_cpu_on_node(pgdat))
172260a5a19eSTang Chen 		return;
172360a5a19eSTang Chen 
172460a5a19eSTang Chen 	/*
172560a5a19eSTang Chen 	 * all memory/cpu of this node are removed, we can offline this
172660a5a19eSTang Chen 	 * node now.
172760a5a19eSTang Chen 	 */
172860a5a19eSTang Chen 	node_set_offline(nid);
172960a5a19eSTang Chen 	unregister_one_node(nid);
173060a5a19eSTang Chen }
173190b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node);
173260a5a19eSTang Chen 
1733d9eb1417SDavid Hildenbrand static void __release_memory_resource(resource_size_t start,
1734d9eb1417SDavid Hildenbrand 				      resource_size_t size)
1735d9eb1417SDavid Hildenbrand {
1736d9eb1417SDavid Hildenbrand 	int ret;
1737d9eb1417SDavid Hildenbrand 
1738d9eb1417SDavid Hildenbrand 	/*
1739d9eb1417SDavid Hildenbrand 	 * When removing memory in the same granularity as it was added,
1740d9eb1417SDavid Hildenbrand 	 * this function never fails. It might only fail if resources
1741d9eb1417SDavid Hildenbrand 	 * have to be adjusted or split. We'll ignore the error, as
1742d9eb1417SDavid Hildenbrand 	 * removing of memory cannot fail.
1743d9eb1417SDavid Hildenbrand 	 */
1744d9eb1417SDavid Hildenbrand 	ret = release_mem_region_adjustable(&iomem_resource, start, size);
1745d9eb1417SDavid Hildenbrand 	if (ret) {
1746d9eb1417SDavid Hildenbrand 		resource_size_t endres = start + size - 1;
1747d9eb1417SDavid Hildenbrand 
1748d9eb1417SDavid Hildenbrand 		pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1749d9eb1417SDavid Hildenbrand 			&start, &endres, ret);
1750d9eb1417SDavid Hildenbrand 	}
1751d9eb1417SDavid Hildenbrand }
1752d9eb1417SDavid Hildenbrand 
1753eca499abSPavel Tatashin static int __ref try_remove_memory(int nid, u64 start, u64 size)
1754bbc76be6SWen Congyang {
1755eca499abSPavel Tatashin 	int rc = 0;
1756993c1aadSWen Congyang 
175727356f54SToshi Kani 	BUG_ON(check_hotplug_memory_range(start, size));
175827356f54SToshi Kani 
1759bfc8c901SVladimir Davydov 	mem_hotplug_begin();
17606677e3eaSYasuaki Ishimatsu 
17616677e3eaSYasuaki Ishimatsu 	/*
1762242831ebSRafael J. Wysocki 	 * All memory blocks must be offlined before removing memory.  Check
1763eca499abSPavel Tatashin 	 * whether all memory blocks in question are offline and return error
1764242831ebSRafael J. Wysocki 	 * if this is not the case.
17656677e3eaSYasuaki Ishimatsu 	 */
1766fbcf73ceSDavid Hildenbrand 	rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb);
1767eca499abSPavel Tatashin 	if (rc)
1768eca499abSPavel Tatashin 		goto done;
17696677e3eaSYasuaki Ishimatsu 
177046c66c4bSYasuaki Ishimatsu 	/* remove memmap entry */
177146c66c4bSYasuaki Ishimatsu 	firmware_map_remove(start, start + size, "System RAM");
177246c66c4bSYasuaki Ishimatsu 
17734c4b7f9bSDavid Hildenbrand 	/* remove memory block devices before removing memory */
17744c4b7f9bSDavid Hildenbrand 	remove_memory_block_devices(start, size);
17754c4b7f9bSDavid Hildenbrand 
17762c2a5af6SOscar Salvador 	arch_remove_memory(nid, start, size, NULL);
177732d1fe8fSAnshuman Khandual 	memblock_free(start, size);
177832d1fe8fSAnshuman Khandual 	memblock_remove(start, size);
1779d9eb1417SDavid Hildenbrand 	__release_memory_resource(start, size);
178024d335caSWen Congyang 
178160a5a19eSTang Chen 	try_offline_node(nid);
178260a5a19eSTang Chen 
1783eca499abSPavel Tatashin done:
1784bfc8c901SVladimir Davydov 	mem_hotplug_done();
1785eca499abSPavel Tatashin 	return rc;
178671088785SBadari Pulavarty }
1787d15e5926SDavid Hildenbrand 
1788eca499abSPavel Tatashin /**
1789eca499abSPavel Tatashin  * remove_memory
1790eca499abSPavel Tatashin  * @nid: the node ID
1791eca499abSPavel Tatashin  * @start: physical address of the region to remove
1792eca499abSPavel Tatashin  * @size: size of the region to remove
1793eca499abSPavel Tatashin  *
1794eca499abSPavel Tatashin  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1795eca499abSPavel Tatashin  * and online/offline operations before this call, as required by
1796eca499abSPavel Tatashin  * try_offline_node().
1797eca499abSPavel Tatashin  */
1798eca499abSPavel Tatashin void __remove_memory(int nid, u64 start, u64 size)
1799d15e5926SDavid Hildenbrand {
1800eca499abSPavel Tatashin 
1801eca499abSPavel Tatashin 	/*
180229a90db9SSouptick Joarder 	 * trigger BUG() if some memory is not offlined prior to calling this
1803eca499abSPavel Tatashin 	 * function
1804eca499abSPavel Tatashin 	 */
1805eca499abSPavel Tatashin 	if (try_remove_memory(nid, start, size))
1806eca499abSPavel Tatashin 		BUG();
1807eca499abSPavel Tatashin }
1808eca499abSPavel Tatashin 
1809eca499abSPavel Tatashin /*
1810eca499abSPavel Tatashin  * Remove memory if every memory block is offline, otherwise return -EBUSY is
1811eca499abSPavel Tatashin  * some memory is not offline
1812eca499abSPavel Tatashin  */
1813eca499abSPavel Tatashin int remove_memory(int nid, u64 start, u64 size)
1814eca499abSPavel Tatashin {
1815eca499abSPavel Tatashin 	int rc;
1816eca499abSPavel Tatashin 
1817d15e5926SDavid Hildenbrand 	lock_device_hotplug();
1818eca499abSPavel Tatashin 	rc  = try_remove_memory(nid, start, size);
1819d15e5926SDavid Hildenbrand 	unlock_device_hotplug();
1820eca499abSPavel Tatashin 
1821eca499abSPavel Tatashin 	return rc;
1822d15e5926SDavid Hildenbrand }
182371088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
1824aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1825