xref: /openbmc/linux/mm/memory_hotplug.c (revision 357b4da50a62e2fd70eacee21cdbd22d4c7a7b60)
13947be19SDave Hansen /*
23947be19SDave Hansen  *  linux/mm/memory_hotplug.c
33947be19SDave Hansen  *
43947be19SDave Hansen  *  Copyright (C)
53947be19SDave Hansen  */
63947be19SDave Hansen 
73947be19SDave Hansen #include <linux/stddef.h>
83947be19SDave Hansen #include <linux/mm.h>
9174cd4b1SIngo Molnar #include <linux/sched/signal.h>
103947be19SDave Hansen #include <linux/swap.h>
113947be19SDave Hansen #include <linux/interrupt.h>
123947be19SDave Hansen #include <linux/pagemap.h>
133947be19SDave Hansen #include <linux/compiler.h>
14b95f1b31SPaul Gortmaker #include <linux/export.h>
153947be19SDave Hansen #include <linux/pagevec.h>
162d1d43f6SChandra Seetharaman #include <linux/writeback.h>
173947be19SDave Hansen #include <linux/slab.h>
183947be19SDave Hansen #include <linux/sysctl.h>
193947be19SDave Hansen #include <linux/cpu.h>
203947be19SDave Hansen #include <linux/memory.h>
214b94ffdcSDan Williams #include <linux/memremap.h>
223947be19SDave Hansen #include <linux/memory_hotplug.h>
233947be19SDave Hansen #include <linux/highmem.h>
243947be19SDave Hansen #include <linux/vmalloc.h>
250a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h>
260c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h>
270c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h>
280c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h>
2971088785SBadari Pulavarty #include <linux/pfn.h>
306ad696d2SAndi Kleen #include <linux/suspend.h>
316d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h>
32d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h>
3360a5a19eSTang Chen #include <linux/stop_machine.h>
34c8721bbbSNaoya Horiguchi #include <linux/hugetlb.h>
35c5320926STang Chen #include <linux/memblock.h>
36698b1b30SVlastimil Babka #include <linux/compaction.h>
37b15c8726SMichal Hocko #include <linux/rmap.h>
383947be19SDave Hansen 
393947be19SDave Hansen #include <asm/tlbflush.h>
403947be19SDave Hansen 
411e5ad9a3SAdrian Bunk #include "internal.h"
421e5ad9a3SAdrian Bunk 
439d0ad8caSDaniel Kiper /*
449d0ad8caSDaniel Kiper  * online_page_callback contains pointer to current page onlining function.
459d0ad8caSDaniel Kiper  * Initially it is generic_online_page(). If it is required it could be
469d0ad8caSDaniel Kiper  * changed by calling set_online_page_callback() for callback registration
479d0ad8caSDaniel Kiper  * and restore_online_page_callback() for generic callback restore.
489d0ad8caSDaniel Kiper  */
499d0ad8caSDaniel Kiper 
509d0ad8caSDaniel Kiper static void generic_online_page(struct page *page);
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 
99*357b4da5SJuergen Gross u64 max_mem_size = U64_MAX;
100*357b4da5SJuergen Gross 
10145e0b78bSKeith Mannthey /* add this memory to iomem resource */
10245e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
10345e0b78bSKeith Mannthey {
1045042db43SJérôme Glisse 	struct resource *res, *conflict;
105*357b4da5SJuergen Gross 
106*357b4da5SJuergen Gross 	if (start + size > max_mem_size)
107*357b4da5SJuergen Gross 		return ERR_PTR(-E2BIG);
108*357b4da5SJuergen Gross 
10945e0b78bSKeith Mannthey 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1106f754ba4SVitaly Kuznetsov 	if (!res)
1116f754ba4SVitaly Kuznetsov 		return ERR_PTR(-ENOMEM);
11245e0b78bSKeith Mannthey 
11345e0b78bSKeith Mannthey 	res->name = "System RAM";
11445e0b78bSKeith Mannthey 	res->start = start;
11545e0b78bSKeith Mannthey 	res->end = start + size - 1;
116782b8664SToshi Kani 	res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1175042db43SJérôme Glisse 	conflict =  request_resource_conflict(&iomem_resource, res);
1185042db43SJérôme Glisse 	if (conflict) {
1195042db43SJérôme Glisse 		if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1205042db43SJérôme Glisse 			pr_debug("Device unaddressable memory block "
1215042db43SJérôme Glisse 				 "memory hotplug at %#010llx !\n",
1225042db43SJérôme Glisse 				 (unsigned long long)start);
1235042db43SJérôme Glisse 		}
1244996eed8SToshi Kani 		pr_debug("System RAM resource %pR cannot be added\n", res);
12545e0b78bSKeith Mannthey 		kfree(res);
1266f754ba4SVitaly Kuznetsov 		return ERR_PTR(-EEXIST);
12745e0b78bSKeith Mannthey 	}
12845e0b78bSKeith Mannthey 	return res;
12945e0b78bSKeith Mannthey }
13045e0b78bSKeith Mannthey 
13145e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
13245e0b78bSKeith Mannthey {
13345e0b78bSKeith Mannthey 	if (!res)
13445e0b78bSKeith Mannthey 		return;
13545e0b78bSKeith Mannthey 	release_resource(res);
13645e0b78bSKeith Mannthey 	kfree(res);
13745e0b78bSKeith Mannthey 	return;
13845e0b78bSKeith Mannthey }
13945e0b78bSKeith Mannthey 
14053947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
14146723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info,  struct page *page,
1425f24ce5fSAndrea Arcangeli 		      unsigned long type)
14304753278SYasunori Goto {
144ddffe98dSYasuaki Ishimatsu 	page->freelist = (void *)type;
14504753278SYasunori Goto 	SetPagePrivate(page);
14604753278SYasunori Goto 	set_page_private(page, info);
147fe896d18SJoonsoo Kim 	page_ref_inc(page);
14804753278SYasunori Goto }
14904753278SYasunori Goto 
150170a5a7eSJiang Liu void put_page_bootmem(struct page *page)
15104753278SYasunori Goto {
1525f24ce5fSAndrea Arcangeli 	unsigned long type;
15304753278SYasunori Goto 
154ddffe98dSYasuaki Ishimatsu 	type = (unsigned long) page->freelist;
1555f24ce5fSAndrea Arcangeli 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
1565f24ce5fSAndrea Arcangeli 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
15704753278SYasunori Goto 
158fe896d18SJoonsoo Kim 	if (page_ref_dec_return(page) == 1) {
159ddffe98dSYasuaki Ishimatsu 		page->freelist = NULL;
16004753278SYasunori Goto 		ClearPagePrivate(page);
16104753278SYasunori Goto 		set_page_private(page, 0);
1625f24ce5fSAndrea Arcangeli 		INIT_LIST_HEAD(&page->lru);
163170a5a7eSJiang Liu 		free_reserved_page(page);
16404753278SYasunori Goto 	}
16504753278SYasunori Goto }
16604753278SYasunori Goto 
16746723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
16846723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP
169d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
17004753278SYasunori Goto {
17104753278SYasunori Goto 	unsigned long *usemap, mapsize, section_nr, i;
17204753278SYasunori Goto 	struct mem_section *ms;
17304753278SYasunori Goto 	struct page *page, *memmap;
17404753278SYasunori Goto 
17504753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
17604753278SYasunori Goto 	ms = __nr_to_section(section_nr);
17704753278SYasunori Goto 
17804753278SYasunori Goto 	/* Get section's memmap address */
17904753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
18004753278SYasunori Goto 
18104753278SYasunori Goto 	/*
18204753278SYasunori Goto 	 * Get page for the memmap's phys address
18304753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
18404753278SYasunori Goto 	 */
18504753278SYasunori Goto 	page = virt_to_page(memmap);
18604753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
18704753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
18804753278SYasunori Goto 
18904753278SYasunori Goto 	/* remember memmap's page */
19004753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
19104753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
19204753278SYasunori Goto 
1939ac9322dSOscar Salvador 	usemap = ms->pageblock_flags;
19404753278SYasunori Goto 	page = virt_to_page(usemap);
19504753278SYasunori Goto 
19604753278SYasunori Goto 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
19704753278SYasunori Goto 
19804753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
199af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
20004753278SYasunori Goto 
20104753278SYasunori Goto }
20246723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */
20346723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn)
20446723bfaSYasuaki Ishimatsu {
20546723bfaSYasuaki Ishimatsu 	unsigned long *usemap, mapsize, section_nr, i;
20646723bfaSYasuaki Ishimatsu 	struct mem_section *ms;
20746723bfaSYasuaki Ishimatsu 	struct page *page, *memmap;
20846723bfaSYasuaki Ishimatsu 
20946723bfaSYasuaki Ishimatsu 	section_nr = pfn_to_section_nr(start_pfn);
21046723bfaSYasuaki Ishimatsu 	ms = __nr_to_section(section_nr);
21146723bfaSYasuaki Ishimatsu 
21246723bfaSYasuaki Ishimatsu 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
21346723bfaSYasuaki Ishimatsu 
21446723bfaSYasuaki Ishimatsu 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
21546723bfaSYasuaki Ishimatsu 
2169ac9322dSOscar Salvador 	usemap = ms->pageblock_flags;
21746723bfaSYasuaki Ishimatsu 	page = virt_to_page(usemap);
21846723bfaSYasuaki Ishimatsu 
21946723bfaSYasuaki Ishimatsu 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
22046723bfaSYasuaki Ishimatsu 
22146723bfaSYasuaki Ishimatsu 	for (i = 0; i < mapsize; i++, page++)
22246723bfaSYasuaki Ishimatsu 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
22346723bfaSYasuaki Ishimatsu }
22446723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
22504753278SYasunori Goto 
2267ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
22704753278SYasunori Goto {
22804753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
22904753278SYasunori Goto 	int node = pgdat->node_id;
23004753278SYasunori Goto 	struct page *page;
23104753278SYasunori Goto 
23204753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
23304753278SYasunori Goto 	page = virt_to_page(pgdat);
23404753278SYasunori Goto 
23504753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
23604753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
23704753278SYasunori Goto 
23804753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
239c1f19495SCody P Schafer 	end_pfn = pgdat_end_pfn(pgdat);
24004753278SYasunori Goto 
2417e9f5eb0STang Chen 	/* register section info */
242f14851afSqiuxishi 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
243f14851afSqiuxishi 		/*
244f14851afSqiuxishi 		 * Some platforms can assign the same pfn to multiple nodes - on
245f14851afSqiuxishi 		 * node0 as well as nodeN.  To avoid registering a pfn against
246f14851afSqiuxishi 		 * multiple nodes we check that this pfn does not already
2477e9f5eb0STang Chen 		 * reside in some other nodes.
248f14851afSqiuxishi 		 */
249f65e91dfSYang Shi 		if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
25004753278SYasunori Goto 			register_page_bootmem_info_section(pfn);
251f14851afSqiuxishi 	}
25204753278SYasunori Goto }
25346723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
25404753278SYasunori Goto 
255f1dd2cd1SMichal Hocko static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
2567b73d978SChristoph Hellwig 		struct vmem_altmap *altmap, bool want_memblock)
2573947be19SDave Hansen {
2583947be19SDave Hansen 	int ret;
2593947be19SDave Hansen 
260ebd15302SKAMEZAWA Hiroyuki 	if (pfn_valid(phys_start_pfn))
261ebd15302SKAMEZAWA Hiroyuki 		return -EEXIST;
262ebd15302SKAMEZAWA Hiroyuki 
2634e0d2e7eSWei Yang 	ret = sparse_add_one_section(nid, phys_start_pfn, altmap);
2643947be19SDave Hansen 	if (ret < 0)
2653947be19SDave Hansen 		return ret;
2663947be19SDave Hansen 
2671b862aecSMichal Hocko 	if (!want_memblock)
2681b862aecSMichal Hocko 		return 0;
2691b862aecSMichal Hocko 
270fc44f7f9SPavel Tatashin 	return hotplug_memory_register(nid, __pfn_to_section(phys_start_pfn));
2713947be19SDave Hansen }
2723947be19SDave Hansen 
2734edd7cefSDavid Rientjes /*
2744edd7cefSDavid Rientjes  * Reasonably generic function for adding memory.  It is
2754edd7cefSDavid Rientjes  * expected that archs that support memory hotplug will
2764edd7cefSDavid Rientjes  * call this function after deciding the zone to which to
2774edd7cefSDavid Rientjes  * add the new pages.
2784edd7cefSDavid Rientjes  */
279f1dd2cd1SMichal Hocko int __ref __add_pages(int nid, unsigned long phys_start_pfn,
28024e6d5a5SChristoph Hellwig 		unsigned long nr_pages, struct vmem_altmap *altmap,
28124e6d5a5SChristoph Hellwig 		bool want_memblock)
2824edd7cefSDavid Rientjes {
2834edd7cefSDavid Rientjes 	unsigned long i;
2844edd7cefSDavid Rientjes 	int err = 0;
2854edd7cefSDavid Rientjes 	int start_sec, end_sec;
2864b94ffdcSDan Williams 
2874edd7cefSDavid Rientjes 	/* during initialize mem_map, align hot-added range to section */
2884edd7cefSDavid Rientjes 	start_sec = pfn_to_section_nr(phys_start_pfn);
2894edd7cefSDavid Rientjes 	end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
2904edd7cefSDavid Rientjes 
2914b94ffdcSDan Williams 	if (altmap) {
2924b94ffdcSDan Williams 		/*
2934b94ffdcSDan Williams 		 * Validate altmap is within bounds of the total request
2944b94ffdcSDan Williams 		 */
2954b94ffdcSDan Williams 		if (altmap->base_pfn != phys_start_pfn
2964b94ffdcSDan Williams 				|| vmem_altmap_offset(altmap) > nr_pages) {
2974b94ffdcSDan Williams 			pr_warn_once("memory add fail, invalid altmap\n");
2987cf91a98SJoonsoo Kim 			err = -EINVAL;
2997cf91a98SJoonsoo Kim 			goto out;
3004b94ffdcSDan Williams 		}
3014b94ffdcSDan Williams 		altmap->alloc = 0;
3024b94ffdcSDan Williams 	}
3034b94ffdcSDan Williams 
3044edd7cefSDavid Rientjes 	for (i = start_sec; i <= end_sec; i++) {
3057b73d978SChristoph Hellwig 		err = __add_section(nid, section_nr_to_pfn(i), altmap,
3067b73d978SChristoph Hellwig 				want_memblock);
3074edd7cefSDavid Rientjes 
3084edd7cefSDavid Rientjes 		/*
3094edd7cefSDavid Rientjes 		 * EEXIST is finally dealt with by ioresource collision
3104edd7cefSDavid Rientjes 		 * check. see add_memory() => register_memory_resource()
3114edd7cefSDavid Rientjes 		 * Warning will be printed if there is collision.
3124edd7cefSDavid Rientjes 		 */
3134edd7cefSDavid Rientjes 		if (err && (err != -EEXIST))
3144edd7cefSDavid Rientjes 			break;
3154edd7cefSDavid Rientjes 		err = 0;
316f64ac5e6SMichal Hocko 		cond_resched();
3174edd7cefSDavid Rientjes 	}
318c435a390SZhu Guihua 	vmemmap_populate_print_last();
3197cf91a98SJoonsoo Kim out:
3204edd7cefSDavid Rientjes 	return err;
3214edd7cefSDavid Rientjes }
3224edd7cefSDavid Rientjes 
3234edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE
324815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
325d09b0137SYASUAKI ISHIMATSU static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
326815121d2SYasuaki Ishimatsu 				     unsigned long start_pfn,
327815121d2SYasuaki Ishimatsu 				     unsigned long end_pfn)
328815121d2SYasuaki Ishimatsu {
329815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
330815121d2SYasuaki Ishimatsu 
331815121d2SYasuaki Ishimatsu 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
332815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(start_pfn);
333815121d2SYasuaki Ishimatsu 
334815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
335815121d2SYasuaki Ishimatsu 			continue;
336815121d2SYasuaki Ishimatsu 
337815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(start_pfn) != nid))
338815121d2SYasuaki Ishimatsu 			continue;
339815121d2SYasuaki Ishimatsu 
340815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(start_pfn)))
341815121d2SYasuaki Ishimatsu 			continue;
342815121d2SYasuaki Ishimatsu 
343815121d2SYasuaki Ishimatsu 		return start_pfn;
344815121d2SYasuaki Ishimatsu 	}
345815121d2SYasuaki Ishimatsu 
346815121d2SYasuaki Ishimatsu 	return 0;
347815121d2SYasuaki Ishimatsu }
348815121d2SYasuaki Ishimatsu 
349815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
350d09b0137SYASUAKI ISHIMATSU static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
351815121d2SYasuaki Ishimatsu 				    unsigned long start_pfn,
352815121d2SYasuaki Ishimatsu 				    unsigned long end_pfn)
353815121d2SYasuaki Ishimatsu {
354815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
355815121d2SYasuaki Ishimatsu 	unsigned long pfn;
356815121d2SYasuaki Ishimatsu 
357815121d2SYasuaki Ishimatsu 	/* pfn is the end pfn of a memory section. */
358815121d2SYasuaki Ishimatsu 	pfn = end_pfn - 1;
359815121d2SYasuaki Ishimatsu 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
360815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
361815121d2SYasuaki Ishimatsu 
362815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
363815121d2SYasuaki Ishimatsu 			continue;
364815121d2SYasuaki Ishimatsu 
365815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(pfn) != nid))
366815121d2SYasuaki Ishimatsu 			continue;
367815121d2SYasuaki Ishimatsu 
368815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(pfn)))
369815121d2SYasuaki Ishimatsu 			continue;
370815121d2SYasuaki Ishimatsu 
371815121d2SYasuaki Ishimatsu 		return pfn;
372815121d2SYasuaki Ishimatsu 	}
373815121d2SYasuaki Ishimatsu 
374815121d2SYasuaki Ishimatsu 	return 0;
375815121d2SYasuaki Ishimatsu }
376815121d2SYasuaki Ishimatsu 
377815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
378815121d2SYasuaki Ishimatsu 			     unsigned long end_pfn)
379815121d2SYasuaki Ishimatsu {
380815121d2SYasuaki Ishimatsu 	unsigned long zone_start_pfn = zone->zone_start_pfn;
381c33bc315SXishi Qiu 	unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
382c33bc315SXishi Qiu 	unsigned long zone_end_pfn = z;
383815121d2SYasuaki Ishimatsu 	unsigned long pfn;
384815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
385815121d2SYasuaki Ishimatsu 	int nid = zone_to_nid(zone);
386815121d2SYasuaki Ishimatsu 
387815121d2SYasuaki Ishimatsu 	zone_span_writelock(zone);
388815121d2SYasuaki Ishimatsu 	if (zone_start_pfn == start_pfn) {
389815121d2SYasuaki Ishimatsu 		/*
390815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the zone, it need
391815121d2SYasuaki Ishimatsu 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
392815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
393815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
394815121d2SYasuaki Ishimatsu 		 */
395815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
396815121d2SYasuaki Ishimatsu 						zone_end_pfn);
397815121d2SYasuaki Ishimatsu 		if (pfn) {
398815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = pfn;
399815121d2SYasuaki Ishimatsu 			zone->spanned_pages = zone_end_pfn - pfn;
400815121d2SYasuaki Ishimatsu 		}
401815121d2SYasuaki Ishimatsu 	} else if (zone_end_pfn == end_pfn) {
402815121d2SYasuaki Ishimatsu 		/*
403815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the zone, it need
404815121d2SYasuaki Ishimatsu 		 * shrink zone->spanned_pages.
405815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
406815121d2SYasuaki Ishimatsu 		 * shrinking zone.
407815121d2SYasuaki Ishimatsu 		 */
408815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
409815121d2SYasuaki Ishimatsu 					       start_pfn);
410815121d2SYasuaki Ishimatsu 		if (pfn)
411815121d2SYasuaki Ishimatsu 			zone->spanned_pages = pfn - zone_start_pfn + 1;
412815121d2SYasuaki Ishimatsu 	}
413815121d2SYasuaki Ishimatsu 
414815121d2SYasuaki Ishimatsu 	/*
415815121d2SYasuaki Ishimatsu 	 * The section is not biggest or smallest mem_section in the zone, it
416815121d2SYasuaki Ishimatsu 	 * only creates a hole in the zone. So in this case, we need not
417815121d2SYasuaki Ishimatsu 	 * change the zone. But perhaps, the zone has only hole data. Thus
418815121d2SYasuaki Ishimatsu 	 * it check the zone has only hole or not.
419815121d2SYasuaki Ishimatsu 	 */
420815121d2SYasuaki Ishimatsu 	pfn = zone_start_pfn;
421815121d2SYasuaki Ishimatsu 	for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
422815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
423815121d2SYasuaki Ishimatsu 
424815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
425815121d2SYasuaki Ishimatsu 			continue;
426815121d2SYasuaki Ishimatsu 
427815121d2SYasuaki Ishimatsu 		if (page_zone(pfn_to_page(pfn)) != zone)
428815121d2SYasuaki Ishimatsu 			continue;
429815121d2SYasuaki Ishimatsu 
430815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
431815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
432815121d2SYasuaki Ishimatsu 			continue;
433815121d2SYasuaki Ishimatsu 
434815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
435815121d2SYasuaki Ishimatsu 		zone_span_writeunlock(zone);
436815121d2SYasuaki Ishimatsu 		return;
437815121d2SYasuaki Ishimatsu 	}
438815121d2SYasuaki Ishimatsu 
439815121d2SYasuaki Ishimatsu 	/* The zone has no valid section */
440815121d2SYasuaki Ishimatsu 	zone->zone_start_pfn = 0;
441815121d2SYasuaki Ishimatsu 	zone->spanned_pages = 0;
442815121d2SYasuaki Ishimatsu 	zone_span_writeunlock(zone);
443815121d2SYasuaki Ishimatsu }
444815121d2SYasuaki Ishimatsu 
445815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat,
446815121d2SYasuaki Ishimatsu 			      unsigned long start_pfn, unsigned long end_pfn)
447815121d2SYasuaki Ishimatsu {
448815121d2SYasuaki Ishimatsu 	unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
44983285c72SXishi Qiu 	unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
45083285c72SXishi Qiu 	unsigned long pgdat_end_pfn = p;
451815121d2SYasuaki Ishimatsu 	unsigned long pfn;
452815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
453815121d2SYasuaki Ishimatsu 	int nid = pgdat->node_id;
454815121d2SYasuaki Ishimatsu 
455815121d2SYasuaki Ishimatsu 	if (pgdat_start_pfn == start_pfn) {
456815121d2SYasuaki Ishimatsu 		/*
457815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the pgdat, it need
458815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
459815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
460815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
461815121d2SYasuaki Ishimatsu 		 */
462815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
463815121d2SYasuaki Ishimatsu 						pgdat_end_pfn);
464815121d2SYasuaki Ishimatsu 		if (pfn) {
465815121d2SYasuaki Ishimatsu 			pgdat->node_start_pfn = pfn;
466815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
467815121d2SYasuaki Ishimatsu 		}
468815121d2SYasuaki Ishimatsu 	} else if (pgdat_end_pfn == end_pfn) {
469815121d2SYasuaki Ishimatsu 		/*
470815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the pgdat, it need
471815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_spanned_pages.
472815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
473815121d2SYasuaki Ishimatsu 		 * shrinking zone.
474815121d2SYasuaki Ishimatsu 		 */
475815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
476815121d2SYasuaki Ishimatsu 					       start_pfn);
477815121d2SYasuaki Ishimatsu 		if (pfn)
478815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
479815121d2SYasuaki Ishimatsu 	}
480815121d2SYasuaki Ishimatsu 
481815121d2SYasuaki Ishimatsu 	/*
482815121d2SYasuaki Ishimatsu 	 * If the section is not biggest or smallest mem_section in the pgdat,
483815121d2SYasuaki Ishimatsu 	 * it only creates a hole in the pgdat. So in this case, we need not
484815121d2SYasuaki Ishimatsu 	 * change the pgdat.
485815121d2SYasuaki Ishimatsu 	 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
486815121d2SYasuaki Ishimatsu 	 * has only hole or not.
487815121d2SYasuaki Ishimatsu 	 */
488815121d2SYasuaki Ishimatsu 	pfn = pgdat_start_pfn;
489815121d2SYasuaki Ishimatsu 	for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
490815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
491815121d2SYasuaki Ishimatsu 
492815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
493815121d2SYasuaki Ishimatsu 			continue;
494815121d2SYasuaki Ishimatsu 
495815121d2SYasuaki Ishimatsu 		if (pfn_to_nid(pfn) != nid)
496815121d2SYasuaki Ishimatsu 			continue;
497815121d2SYasuaki Ishimatsu 
498815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
499815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
500815121d2SYasuaki Ishimatsu 			continue;
501815121d2SYasuaki Ishimatsu 
502815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
503815121d2SYasuaki Ishimatsu 		return;
504815121d2SYasuaki Ishimatsu 	}
505815121d2SYasuaki Ishimatsu 
506815121d2SYasuaki Ishimatsu 	/* The pgdat has no valid section */
507815121d2SYasuaki Ishimatsu 	pgdat->node_start_pfn = 0;
508815121d2SYasuaki Ishimatsu 	pgdat->node_spanned_pages = 0;
509815121d2SYasuaki Ishimatsu }
510815121d2SYasuaki Ishimatsu 
511815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn)
512815121d2SYasuaki Ishimatsu {
513815121d2SYasuaki Ishimatsu 	struct pglist_data *pgdat = zone->zone_pgdat;
514815121d2SYasuaki Ishimatsu 	int nr_pages = PAGES_PER_SECTION;
515815121d2SYasuaki Ishimatsu 	unsigned long flags;
516815121d2SYasuaki Ishimatsu 
517815121d2SYasuaki Ishimatsu 	pgdat_resize_lock(zone->zone_pgdat, &flags);
518815121d2SYasuaki Ishimatsu 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
519815121d2SYasuaki Ishimatsu 	shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
520815121d2SYasuaki Ishimatsu 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
521815121d2SYasuaki Ishimatsu }
522815121d2SYasuaki Ishimatsu 
5234b94ffdcSDan Williams static int __remove_section(struct zone *zone, struct mem_section *ms,
52424b6d416SChristoph Hellwig 		unsigned long map_offset, struct vmem_altmap *altmap)
525ea01ea93SBadari Pulavarty {
526815121d2SYasuaki Ishimatsu 	unsigned long start_pfn;
527815121d2SYasuaki Ishimatsu 	int scn_nr;
528ea01ea93SBadari Pulavarty 	int ret = -EINVAL;
529ea01ea93SBadari Pulavarty 
530ea01ea93SBadari Pulavarty 	if (!valid_section(ms))
531ea01ea93SBadari Pulavarty 		return ret;
532ea01ea93SBadari Pulavarty 
533ea01ea93SBadari Pulavarty 	ret = unregister_memory_section(ms);
534ea01ea93SBadari Pulavarty 	if (ret)
535ea01ea93SBadari Pulavarty 		return ret;
536ea01ea93SBadari Pulavarty 
537815121d2SYasuaki Ishimatsu 	scn_nr = __section_nr(ms);
5381dd2bfc8SYASUAKI ISHIMATSU 	start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
539815121d2SYasuaki Ishimatsu 	__remove_zone(zone, start_pfn);
540815121d2SYasuaki Ishimatsu 
54124b6d416SChristoph Hellwig 	sparse_remove_one_section(zone, ms, map_offset, altmap);
542ea01ea93SBadari Pulavarty 	return 0;
543ea01ea93SBadari Pulavarty }
544ea01ea93SBadari Pulavarty 
545ea01ea93SBadari Pulavarty /**
546ea01ea93SBadari Pulavarty  * __remove_pages() - remove sections of pages from a zone
547ea01ea93SBadari Pulavarty  * @zone: zone from which pages need to be removed
548ea01ea93SBadari Pulavarty  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
549ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
550e8b098fcSMike Rapoport  * @altmap: alternative device page map or %NULL if default memmap is used
551ea01ea93SBadari Pulavarty  *
552ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
553ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
554ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
555ea01ea93SBadari Pulavarty  * calling offline_pages().
556ea01ea93SBadari Pulavarty  */
557ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
558da024512SChristoph Hellwig 		 unsigned long nr_pages, struct vmem_altmap *altmap)
559ea01ea93SBadari Pulavarty {
560fe74ebb1SToshi Kani 	unsigned long i;
5614b94ffdcSDan Williams 	unsigned long map_offset = 0;
5624b94ffdcSDan Williams 	int sections_to_remove, ret = 0;
5634b94ffdcSDan Williams 
5644b94ffdcSDan Williams 	/* In the ZONE_DEVICE case device driver owns the memory region */
5654b94ffdcSDan Williams 	if (is_dev_zone(zone)) {
5664b94ffdcSDan Williams 		if (altmap)
5674b94ffdcSDan Williams 			map_offset = vmem_altmap_offset(altmap);
5684b94ffdcSDan Williams 	} else {
569fe74ebb1SToshi Kani 		resource_size_t start, size;
5704b94ffdcSDan Williams 
5714b94ffdcSDan Williams 		start = phys_start_pfn << PAGE_SHIFT;
5724b94ffdcSDan Williams 		size = nr_pages * PAGE_SIZE;
5734b94ffdcSDan Williams 
5744b94ffdcSDan Williams 		ret = release_mem_region_adjustable(&iomem_resource, start,
5754b94ffdcSDan Williams 					size);
5764b94ffdcSDan Williams 		if (ret) {
5774b94ffdcSDan Williams 			resource_size_t endres = start + size - 1;
5784b94ffdcSDan Williams 
5794b94ffdcSDan Williams 			pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
5804b94ffdcSDan Williams 					&start, &endres, ret);
5814b94ffdcSDan Williams 		}
5824b94ffdcSDan Williams 	}
583ea01ea93SBadari Pulavarty 
5847cf91a98SJoonsoo Kim 	clear_zone_contiguous(zone);
5857cf91a98SJoonsoo Kim 
586ea01ea93SBadari Pulavarty 	/*
587ea01ea93SBadari Pulavarty 	 * We can only remove entire sections
588ea01ea93SBadari Pulavarty 	 */
589ea01ea93SBadari Pulavarty 	BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
590ea01ea93SBadari Pulavarty 	BUG_ON(nr_pages % PAGES_PER_SECTION);
591ea01ea93SBadari Pulavarty 
592ea01ea93SBadari Pulavarty 	sections_to_remove = nr_pages / PAGES_PER_SECTION;
593ea01ea93SBadari Pulavarty 	for (i = 0; i < sections_to_remove; i++) {
594ea01ea93SBadari Pulavarty 		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
5954b94ffdcSDan Williams 
596dd33ad7bSMichal Hocko 		cond_resched();
59724b6d416SChristoph Hellwig 		ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
59824b6d416SChristoph Hellwig 				altmap);
5994b94ffdcSDan Williams 		map_offset = 0;
600ea01ea93SBadari Pulavarty 		if (ret)
601ea01ea93SBadari Pulavarty 			break;
602ea01ea93SBadari Pulavarty 	}
6037cf91a98SJoonsoo Kim 
6047cf91a98SJoonsoo Kim 	set_zone_contiguous(zone);
6057cf91a98SJoonsoo Kim 
606ea01ea93SBadari Pulavarty 	return ret;
607ea01ea93SBadari Pulavarty }
6084edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */
609ea01ea93SBadari Pulavarty 
6109d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback)
6119d0ad8caSDaniel Kiper {
6129d0ad8caSDaniel Kiper 	int rc = -EINVAL;
6139d0ad8caSDaniel Kiper 
614bfc8c901SVladimir Davydov 	get_online_mems();
615bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
6169d0ad8caSDaniel Kiper 
6179d0ad8caSDaniel Kiper 	if (online_page_callback == generic_online_page) {
6189d0ad8caSDaniel Kiper 		online_page_callback = callback;
6199d0ad8caSDaniel Kiper 		rc = 0;
6209d0ad8caSDaniel Kiper 	}
6219d0ad8caSDaniel Kiper 
622bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
623bfc8c901SVladimir Davydov 	put_online_mems();
6249d0ad8caSDaniel Kiper 
6259d0ad8caSDaniel Kiper 	return rc;
6269d0ad8caSDaniel Kiper }
6279d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback);
6289d0ad8caSDaniel Kiper 
6299d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback)
6309d0ad8caSDaniel Kiper {
6319d0ad8caSDaniel Kiper 	int rc = -EINVAL;
6329d0ad8caSDaniel Kiper 
633bfc8c901SVladimir Davydov 	get_online_mems();
634bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
6359d0ad8caSDaniel Kiper 
6369d0ad8caSDaniel Kiper 	if (online_page_callback == callback) {
6379d0ad8caSDaniel Kiper 		online_page_callback = generic_online_page;
6389d0ad8caSDaniel Kiper 		rc = 0;
6399d0ad8caSDaniel Kiper 	}
6409d0ad8caSDaniel Kiper 
641bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
642bfc8c901SVladimir Davydov 	put_online_mems();
6439d0ad8caSDaniel Kiper 
6449d0ad8caSDaniel Kiper 	return rc;
6459d0ad8caSDaniel Kiper }
6469d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback);
6479d0ad8caSDaniel Kiper 
6489d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page)
649180c06efSJeremy Fitzhardinge {
6509d0ad8caSDaniel Kiper }
6519d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits);
6529d0ad8caSDaniel Kiper 
6539d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page)
6549d0ad8caSDaniel Kiper {
6553dcc0571SJiang Liu 	adjust_managed_page_count(page, 1);
6569d0ad8caSDaniel Kiper }
6579d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters);
658180c06efSJeremy Fitzhardinge 
6599d0ad8caSDaniel Kiper void __online_page_free(struct page *page)
6609d0ad8caSDaniel Kiper {
6613dcc0571SJiang Liu 	__free_reserved_page(page);
662180c06efSJeremy Fitzhardinge }
6639d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free);
6649d0ad8caSDaniel Kiper 
6659d0ad8caSDaniel Kiper static void generic_online_page(struct page *page)
6669d0ad8caSDaniel Kiper {
6679d0ad8caSDaniel Kiper 	__online_page_set_limits(page);
6689d0ad8caSDaniel Kiper 	__online_page_increment_counters(page);
6699d0ad8caSDaniel Kiper 	__online_page_free(page);
6709d0ad8caSDaniel Kiper }
671180c06efSJeremy Fitzhardinge 
67275884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
67375884fb1SKAMEZAWA Hiroyuki 			void *arg)
6743947be19SDave Hansen {
6753947be19SDave Hansen 	unsigned long i;
67675884fb1SKAMEZAWA Hiroyuki 	unsigned long onlined_pages = *(unsigned long *)arg;
67775884fb1SKAMEZAWA Hiroyuki 	struct page *page;
6782d070eabSMichal Hocko 
67975884fb1SKAMEZAWA Hiroyuki 	if (PageReserved(pfn_to_page(start_pfn)))
68075884fb1SKAMEZAWA Hiroyuki 		for (i = 0; i < nr_pages; i++) {
68175884fb1SKAMEZAWA Hiroyuki 			page = pfn_to_page(start_pfn + i);
6829d0ad8caSDaniel Kiper 			(*online_page_callback)(page);
68375884fb1SKAMEZAWA Hiroyuki 			onlined_pages++;
68475884fb1SKAMEZAWA Hiroyuki 		}
6852d070eabSMichal Hocko 
6862d070eabSMichal Hocko 	online_mem_sections(start_pfn, start_pfn + nr_pages);
6872d070eabSMichal Hocko 
68875884fb1SKAMEZAWA Hiroyuki 	*(unsigned long *)arg = onlined_pages;
68975884fb1SKAMEZAWA Hiroyuki 	return 0;
69075884fb1SKAMEZAWA Hiroyuki }
69175884fb1SKAMEZAWA Hiroyuki 
692d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */
693d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages,
694d9713679SLai Jiangshan 	struct zone *zone, struct memory_notify *arg)
695d9713679SLai Jiangshan {
696d9713679SLai Jiangshan 	int nid = zone_to_nid(zone);
697d9713679SLai Jiangshan 
6988efe33f4SOscar Salvador 	arg->status_change_nid = -1;
699d9713679SLai Jiangshan 	arg->status_change_nid_normal = -1;
7006715ddf9SLai Jiangshan 	arg->status_change_nid_high = -1;
7016715ddf9SLai Jiangshan 
7026715ddf9SLai Jiangshan 	if (!node_state(nid, N_MEMORY))
703d9713679SLai Jiangshan 		arg->status_change_nid = nid;
7048efe33f4SOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
7058efe33f4SOscar Salvador 		arg->status_change_nid_normal = nid;
7068efe33f4SOscar Salvador #ifdef CONFIG_HIGHMEM
7078efe33f4SOscar Salvador 	if (zone_idx(zone) <= N_HIGH_MEMORY && !node_state(nid, N_HIGH_MEMORY))
7088efe33f4SOscar Salvador 		arg->status_change_nid_high = nid;
7098efe33f4SOscar Salvador #endif
710d9713679SLai Jiangshan }
711d9713679SLai Jiangshan 
712d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg)
713d9713679SLai Jiangshan {
714d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
715d9713679SLai Jiangshan 		node_set_state(node, N_NORMAL_MEMORY);
716d9713679SLai Jiangshan 
7176715ddf9SLai Jiangshan 	if (arg->status_change_nid_high >= 0)
718d9713679SLai Jiangshan 		node_set_state(node, N_HIGH_MEMORY);
7196715ddf9SLai Jiangshan 
72083d83612SOscar Salvador 	if (arg->status_change_nid >= 0)
7216715ddf9SLai Jiangshan 		node_set_state(node, N_MEMORY);
722d9713679SLai Jiangshan }
723d9713679SLai Jiangshan 
724f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
725f1dd2cd1SMichal Hocko 		unsigned long nr_pages)
726f1dd2cd1SMichal Hocko {
727f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = zone_end_pfn(zone);
728f1dd2cd1SMichal Hocko 
729f1dd2cd1SMichal Hocko 	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
730f1dd2cd1SMichal Hocko 		zone->zone_start_pfn = start_pfn;
731f1dd2cd1SMichal Hocko 
732f1dd2cd1SMichal Hocko 	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
733f1dd2cd1SMichal Hocko }
734f1dd2cd1SMichal Hocko 
735f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
736f1dd2cd1SMichal Hocko                                      unsigned long nr_pages)
737f1dd2cd1SMichal Hocko {
738f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
739f1dd2cd1SMichal Hocko 
740f1dd2cd1SMichal Hocko 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
741f1dd2cd1SMichal Hocko 		pgdat->node_start_pfn = start_pfn;
742f1dd2cd1SMichal Hocko 
743f1dd2cd1SMichal Hocko 	pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
744f1dd2cd1SMichal Hocko }
745f1dd2cd1SMichal Hocko 
746a99583e7SChristoph Hellwig void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
747a99583e7SChristoph Hellwig 		unsigned long nr_pages, struct vmem_altmap *altmap)
748f1dd2cd1SMichal Hocko {
749f1dd2cd1SMichal Hocko 	struct pglist_data *pgdat = zone->zone_pgdat;
750f1dd2cd1SMichal Hocko 	int nid = pgdat->node_id;
751f1dd2cd1SMichal Hocko 	unsigned long flags;
752f1dd2cd1SMichal Hocko 
753f1dd2cd1SMichal Hocko 	clear_zone_contiguous(zone);
754f1dd2cd1SMichal Hocko 
755f1dd2cd1SMichal Hocko 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
756f1dd2cd1SMichal Hocko 	pgdat_resize_lock(pgdat, &flags);
757f1dd2cd1SMichal Hocko 	zone_span_writelock(zone);
758fa004ab7SWei Yang 	if (zone_is_empty(zone))
759fa004ab7SWei Yang 		init_currently_empty_zone(zone, start_pfn, nr_pages);
760f1dd2cd1SMichal Hocko 	resize_zone_range(zone, start_pfn, nr_pages);
761f1dd2cd1SMichal Hocko 	zone_span_writeunlock(zone);
762f1dd2cd1SMichal Hocko 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
763f1dd2cd1SMichal Hocko 	pgdat_resize_unlock(pgdat, &flags);
764f1dd2cd1SMichal Hocko 
765f1dd2cd1SMichal Hocko 	/*
766f1dd2cd1SMichal Hocko 	 * TODO now we have a visible range of pages which are not associated
767f1dd2cd1SMichal Hocko 	 * with their zone properly. Not nice but set_pfnblock_flags_mask
768f1dd2cd1SMichal Hocko 	 * expects the zone spans the pfn range. All the pages in the range
769f1dd2cd1SMichal Hocko 	 * are reserved so nobody should be touching them so we should be safe
770f1dd2cd1SMichal Hocko 	 */
771a99583e7SChristoph Hellwig 	memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
772a99583e7SChristoph Hellwig 			MEMMAP_HOTPLUG, altmap);
773f1dd2cd1SMichal Hocko 
774f1dd2cd1SMichal Hocko 	set_zone_contiguous(zone);
775f1dd2cd1SMichal Hocko }
776f1dd2cd1SMichal Hocko 
777f1dd2cd1SMichal Hocko /*
778c246a213SMichal Hocko  * Returns a default kernel memory zone for the given pfn range.
779c246a213SMichal Hocko  * If no kernel zone covers this pfn range it will automatically go
780c246a213SMichal Hocko  * to the ZONE_NORMAL.
781c246a213SMichal Hocko  */
782c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
783c246a213SMichal Hocko 		unsigned long nr_pages)
784c246a213SMichal Hocko {
785c246a213SMichal Hocko 	struct pglist_data *pgdat = NODE_DATA(nid);
786c246a213SMichal Hocko 	int zid;
787c246a213SMichal Hocko 
788c246a213SMichal Hocko 	for (zid = 0; zid <= ZONE_NORMAL; zid++) {
789c246a213SMichal Hocko 		struct zone *zone = &pgdat->node_zones[zid];
790c246a213SMichal Hocko 
791c246a213SMichal Hocko 		if (zone_intersects(zone, start_pfn, nr_pages))
792c246a213SMichal Hocko 			return zone;
793c246a213SMichal Hocko 	}
794c246a213SMichal Hocko 
795c246a213SMichal Hocko 	return &pgdat->node_zones[ZONE_NORMAL];
796c246a213SMichal Hocko }
797c246a213SMichal Hocko 
798c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
799c6f03e29SMichal Hocko 		unsigned long nr_pages)
800e5e68930SMichal Hocko {
801c6f03e29SMichal Hocko 	struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
802c6f03e29SMichal Hocko 			nr_pages);
803c6f03e29SMichal Hocko 	struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
804c6f03e29SMichal Hocko 	bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
805c6f03e29SMichal Hocko 	bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
806e5e68930SMichal Hocko 
807e5e68930SMichal Hocko 	/*
808c6f03e29SMichal Hocko 	 * We inherit the existing zone in a simple case where zones do not
809c6f03e29SMichal Hocko 	 * overlap in the given range
810e5e68930SMichal Hocko 	 */
811c6f03e29SMichal Hocko 	if (in_kernel ^ in_movable)
812c6f03e29SMichal Hocko 		return (in_kernel) ? kernel_zone : movable_zone;
813e5e68930SMichal Hocko 
814c6f03e29SMichal Hocko 	/*
815c6f03e29SMichal Hocko 	 * If the range doesn't belong to any zone or two zones overlap in the
816c6f03e29SMichal Hocko 	 * given range then we use movable zone only if movable_node is
817c6f03e29SMichal Hocko 	 * enabled because we always online to a kernel zone by default.
818c6f03e29SMichal Hocko 	 */
819c6f03e29SMichal Hocko 	return movable_node_enabled ? movable_zone : kernel_zone;
8209f123ab5SMichal Hocko }
8219f123ab5SMichal Hocko 
822e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
823e5e68930SMichal Hocko 		unsigned long nr_pages)
824f1dd2cd1SMichal Hocko {
825c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_KERNEL)
826c6f03e29SMichal Hocko 		return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
827f1dd2cd1SMichal Hocko 
828c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_MOVABLE)
829c6f03e29SMichal Hocko 		return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
830f1dd2cd1SMichal Hocko 
831c6f03e29SMichal Hocko 	return default_zone_for_pfn(nid, start_pfn, nr_pages);
832e5e68930SMichal Hocko }
833e5e68930SMichal Hocko 
834e5e68930SMichal Hocko /*
835e5e68930SMichal Hocko  * Associates the given pfn range with the given node and the zone appropriate
836e5e68930SMichal Hocko  * for the given online type.
837e5e68930SMichal Hocko  */
838e5e68930SMichal Hocko static struct zone * __meminit move_pfn_range(int online_type, int nid,
839e5e68930SMichal Hocko 		unsigned long start_pfn, unsigned long nr_pages)
840e5e68930SMichal Hocko {
841e5e68930SMichal Hocko 	struct zone *zone;
842e5e68930SMichal Hocko 
843e5e68930SMichal Hocko 	zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
844a99583e7SChristoph Hellwig 	move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
845f1dd2cd1SMichal Hocko 	return zone;
846df429ac0SReza Arbab }
84775884fb1SKAMEZAWA Hiroyuki 
848511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
84975884fb1SKAMEZAWA Hiroyuki {
850aa47228aSCody P Schafer 	unsigned long flags;
8513947be19SDave Hansen 	unsigned long onlined_pages = 0;
8523947be19SDave Hansen 	struct zone *zone;
8536811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
8547b78d335SYasunori Goto 	int nid;
8557b78d335SYasunori Goto 	int ret;
8567b78d335SYasunori Goto 	struct memory_notify arg;
857d0dc12e8SPavel Tatashin 	struct memory_block *mem;
8583947be19SDave Hansen 
859381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
860381eab4aSDavid Hildenbrand 
861d0dc12e8SPavel Tatashin 	/*
862d0dc12e8SPavel Tatashin 	 * We can't use pfn_to_nid() because nid might be stored in struct page
863d0dc12e8SPavel Tatashin 	 * which is not yet initialized. Instead, we find nid from memory block.
864d0dc12e8SPavel Tatashin 	 */
865d0dc12e8SPavel Tatashin 	mem = find_memory_block(__pfn_to_section(pfn));
866d0dc12e8SPavel Tatashin 	nid = mem->nid;
867d0dc12e8SPavel Tatashin 
868f1dd2cd1SMichal Hocko 	/* associate pfn range with the zone */
869f1dd2cd1SMichal Hocko 	zone = move_pfn_range(online_type, nid, pfn, nr_pages);
870511c2abaSLai Jiangshan 
8717b78d335SYasunori Goto 	arg.start_pfn = pfn;
8727b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
873d9713679SLai Jiangshan 	node_states_check_changes_online(nr_pages, zone, &arg);
8747b78d335SYasunori Goto 
8757b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
8767b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
877e33e33b4SChen Yucong 	if (ret)
878e33e33b4SChen Yucong 		goto failed_addition;
879e33e33b4SChen Yucong 
8803947be19SDave Hansen 	/*
8816811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
8826811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
8836811378eSYasunori Goto 	 * So, zonelist must be updated after online.
8846811378eSYasunori Goto 	 */
8856dcd73d7SWen Congyang 	if (!populated_zone(zone)) {
8866811378eSYasunori Goto 		need_zonelists_rebuild = 1;
88772675e13SMichal Hocko 		setup_zone_pageset(zone);
8886dcd73d7SWen Congyang 	}
8896811378eSYasunori Goto 
890908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
89175884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
892fd8a4221SGeoff Levand 	if (ret) {
8936dcd73d7SWen Congyang 		if (need_zonelists_rebuild)
8946dcd73d7SWen Congyang 			zone_pcp_reset(zone);
895e33e33b4SChen Yucong 		goto failed_addition;
896fd8a4221SGeoff Levand 	}
897fd8a4221SGeoff Levand 
8983947be19SDave Hansen 	zone->present_pages += onlined_pages;
899aa47228aSCody P Schafer 
900aa47228aSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
901f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
902aa47228aSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
903aa47228aSCody P Schafer 
90408dff7b7SJiang Liu 	if (onlined_pages) {
905e888ca35SVlastimil Babka 		node_states_set_node(nid, &arg);
9061f522509SHaicheng Li 		if (need_zonelists_rebuild)
90772675e13SMichal Hocko 			build_all_zonelists(NULL);
9081f522509SHaicheng Li 		else
909112067f0SShaohua Li 			zone_pcp_update(zone);
91008dff7b7SJiang Liu 	}
9111f522509SHaicheng Li 
9121b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
9131b79acc9SKOSAKI Motohiro 
914698b1b30SVlastimil Babka 	if (onlined_pages) {
915e888ca35SVlastimil Babka 		kswapd_run(nid);
916698b1b30SVlastimil Babka 		kcompactd_run(nid);
917698b1b30SVlastimil Babka 	}
91861b13993SDave Hansen 
9195a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
9202f7f24ecSKent Liu 
9212d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
9227b78d335SYasunori Goto 
9237b78d335SYasunori Goto 	if (onlined_pages)
9247b78d335SYasunori Goto 		memory_notify(MEM_ONLINE, &arg);
925381eab4aSDavid Hildenbrand 	mem_hotplug_done();
92630467e0bSDavid Rientjes 	return 0;
927e33e33b4SChen Yucong 
928e33e33b4SChen Yucong failed_addition:
929e33e33b4SChen Yucong 	pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
930e33e33b4SChen Yucong 		 (unsigned long long) pfn << PAGE_SHIFT,
931e33e33b4SChen Yucong 		 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
932e33e33b4SChen Yucong 	memory_notify(MEM_CANCEL_ONLINE, &arg);
933381eab4aSDavid Hildenbrand 	mem_hotplug_done();
934e33e33b4SChen Yucong 	return ret;
9353947be19SDave Hansen }
93653947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
937bc02af93SYasunori Goto 
9380bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat)
9390bd85420STang Chen {
9400bd85420STang Chen 	struct zone *z;
9410bd85420STang Chen 
9420bd85420STang Chen 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
9430bd85420STang Chen 		z->present_pages = 0;
9440bd85420STang Chen 
9450bd85420STang Chen 	pgdat->node_present_pages = 0;
9460bd85420STang Chen }
9470bd85420STang Chen 
948e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
949e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9509af3c2deSYasunori Goto {
9519af3c2deSYasunori Goto 	struct pglist_data *pgdat;
952c8e861a5SFabian Frederick 	unsigned long start_pfn = PFN_DOWN(start);
9539af3c2deSYasunori Goto 
954a1e565aaSTang Chen 	pgdat = NODE_DATA(nid);
955a1e565aaSTang Chen 	if (!pgdat) {
9569af3c2deSYasunori Goto 		pgdat = arch_alloc_nodedata(nid);
9579af3c2deSYasunori Goto 		if (!pgdat)
9589af3c2deSYasunori Goto 			return NULL;
9599af3c2deSYasunori Goto 
9609af3c2deSYasunori Goto 		arch_refresh_nodedata(nid, pgdat);
961b0dc3a34SGu Zheng 	} else {
962e716f2ebSMel Gorman 		/*
963e716f2ebSMel Gorman 		 * Reset the nr_zones, order and classzone_idx before reuse.
964e716f2ebSMel Gorman 		 * Note that kswapd will init kswapd_classzone_idx properly
965e716f2ebSMel Gorman 		 * when it starts in the near future.
966e716f2ebSMel Gorman 		 */
967b0dc3a34SGu Zheng 		pgdat->nr_zones = 0;
96838087d9bSMel Gorman 		pgdat->kswapd_order = 0;
96938087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = 0;
970a1e565aaSTang Chen 	}
9719af3c2deSYasunori Goto 
9729af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
9739af3c2deSYasunori Goto 
97403e85f9dSOscar Salvador 	pgdat->node_id = nid;
97503e85f9dSOscar Salvador 	pgdat->node_start_pfn = start_pfn;
97603e85f9dSOscar Salvador 
9779af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
97803e85f9dSOscar Salvador 	free_area_init_core_hotplug(nid);
9795830169fSReza Arbab 	pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
9809af3c2deSYasunori Goto 
981959ecc48SKAMEZAWA Hiroyuki 	/*
982959ecc48SKAMEZAWA Hiroyuki 	 * The node we allocated has no zone fallback lists. For avoiding
983959ecc48SKAMEZAWA Hiroyuki 	 * to access not-initialized zonelist, build here.
984959ecc48SKAMEZAWA Hiroyuki 	 */
98572675e13SMichal Hocko 	build_all_zonelists(pgdat);
986959ecc48SKAMEZAWA Hiroyuki 
987f784a3f1STang Chen 	/*
9880bd85420STang Chen 	 * When memory is hot-added, all the memory is in offline state. So
9890bd85420STang Chen 	 * clear all zones' present_pages because they will be updated in
9900bd85420STang Chen 	 * online_pages() and offline_pages().
9910bd85420STang Chen 	 */
99203e85f9dSOscar Salvador 	reset_node_managed_pages(pgdat);
9930bd85420STang Chen 	reset_node_present_pages(pgdat);
9940bd85420STang Chen 
9959af3c2deSYasunori Goto 	return pgdat;
9969af3c2deSYasunori Goto }
9979af3c2deSYasunori Goto 
998b9ff0360SOscar Salvador static void rollback_node_hotadd(int nid)
9999af3c2deSYasunori Goto {
1000b9ff0360SOscar Salvador 	pg_data_t *pgdat = NODE_DATA(nid);
1001b9ff0360SOscar Salvador 
10029af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
10035830169fSReza Arbab 	free_percpu(pgdat->per_cpu_nodestats);
10049af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
10059af3c2deSYasunori Goto 	return;
10069af3c2deSYasunori Goto }
10079af3c2deSYasunori Goto 
10080a547039SKAMEZAWA Hiroyuki 
100901b0f197SToshi Kani /**
101001b0f197SToshi Kani  * try_online_node - online a node if offlined
1011e8b098fcSMike Rapoport  * @nid: the node ID
1012b9ff0360SOscar Salvador  * @start: start addr of the node
1013b9ff0360SOscar Salvador  * @set_node_online: Whether we want to online the node
1014cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
1015b9ff0360SOscar Salvador  *
1016b9ff0360SOscar Salvador  * Returns:
1017b9ff0360SOscar Salvador  * 1 -> a new node has been allocated
1018b9ff0360SOscar Salvador  * 0 -> the node is already online
1019b9ff0360SOscar Salvador  * -ENOMEM -> the node could not be allocated
1020cf23422bSminskey guo  */
1021b9ff0360SOscar Salvador static int __try_online_node(int nid, u64 start, bool set_node_online)
1022cf23422bSminskey guo {
1023cf23422bSminskey guo 	pg_data_t *pgdat;
1024b9ff0360SOscar Salvador 	int ret = 1;
1025cf23422bSminskey guo 
102601b0f197SToshi Kani 	if (node_online(nid))
102701b0f197SToshi Kani 		return 0;
102801b0f197SToshi Kani 
1029b9ff0360SOscar Salvador 	pgdat = hotadd_new_pgdat(nid, start);
10307553e8f2SDavid Rientjes 	if (!pgdat) {
103101b0f197SToshi Kani 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1032cf23422bSminskey guo 		ret = -ENOMEM;
1033cf23422bSminskey guo 		goto out;
1034cf23422bSminskey guo 	}
1035b9ff0360SOscar Salvador 
1036b9ff0360SOscar Salvador 	if (set_node_online) {
1037cf23422bSminskey guo 		node_set_online(nid);
1038cf23422bSminskey guo 		ret = register_one_node(nid);
1039cf23422bSminskey guo 		BUG_ON(ret);
1040b9ff0360SOscar Salvador 	}
1041cf23422bSminskey guo out:
1042b9ff0360SOscar Salvador 	return ret;
1043b9ff0360SOscar Salvador }
1044b9ff0360SOscar Salvador 
1045b9ff0360SOscar Salvador /*
1046b9ff0360SOscar Salvador  * Users of this function always want to online/register the node
1047b9ff0360SOscar Salvador  */
1048b9ff0360SOscar Salvador int try_online_node(int nid)
1049b9ff0360SOscar Salvador {
1050b9ff0360SOscar Salvador 	int ret;
1051b9ff0360SOscar Salvador 
1052b9ff0360SOscar Salvador 	mem_hotplug_begin();
1053b9ff0360SOscar Salvador 	ret =  __try_online_node(nid, 0, true);
1054bfc8c901SVladimir Davydov 	mem_hotplug_done();
1055cf23422bSminskey guo 	return ret;
1056cf23422bSminskey guo }
1057cf23422bSminskey guo 
105827356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size)
105927356f54SToshi Kani {
1060ba325585SPavel Tatashin 	unsigned long block_sz = memory_block_size_bytes();
1061ba325585SPavel Tatashin 	u64 block_nr_pages = block_sz >> PAGE_SHIFT;
106227356f54SToshi Kani 	u64 nr_pages = size >> PAGE_SHIFT;
1063ba325585SPavel Tatashin 	u64 start_pfn = PFN_DOWN(start);
106427356f54SToshi Kani 
1065ba325585SPavel Tatashin 	/* memory range must be block size aligned */
1066ba325585SPavel Tatashin 	if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) ||
1067ba325585SPavel Tatashin 	    !IS_ALIGNED(nr_pages, block_nr_pages)) {
1068ba325585SPavel Tatashin 		pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1069ba325585SPavel Tatashin 		       block_sz, start, size);
107027356f54SToshi Kani 		return -EINVAL;
107127356f54SToshi Kani 	}
107227356f54SToshi Kani 
107327356f54SToshi Kani 	return 0;
107427356f54SToshi Kani }
107527356f54SToshi Kani 
107631bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg)
107731bc3858SVitaly Kuznetsov {
1078dc18d706SNathan Fontenot 	return device_online(&mem->dev);
107931bc3858SVitaly Kuznetsov }
108031bc3858SVitaly Kuznetsov 
10818df1d0e4SDavid Hildenbrand /*
10828df1d0e4SDavid Hildenbrand  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
10838df1d0e4SDavid Hildenbrand  * and online/offline operations (triggered e.g. by sysfs).
10848df1d0e4SDavid Hildenbrand  *
10858df1d0e4SDavid Hildenbrand  * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
10868df1d0e4SDavid Hildenbrand  */
1087f29d8e9cSDavid Hildenbrand int __ref add_memory_resource(int nid, struct resource *res)
1088bc02af93SYasunori Goto {
108962cedb9fSDavid Vrabel 	u64 start, size;
1090b9ff0360SOscar Salvador 	bool new_node = false;
1091bc02af93SYasunori Goto 	int ret;
1092bc02af93SYasunori Goto 
109362cedb9fSDavid Vrabel 	start = res->start;
109462cedb9fSDavid Vrabel 	size = resource_size(res);
109562cedb9fSDavid Vrabel 
109627356f54SToshi Kani 	ret = check_hotplug_memory_range(start, size);
109727356f54SToshi Kani 	if (ret)
109827356f54SToshi Kani 		return ret;
109927356f54SToshi Kani 
1100bfc8c901SVladimir Davydov 	mem_hotplug_begin();
1101ac13c462SNathan Zimmer 
11027f36e3e5STang Chen 	/*
11037f36e3e5STang Chen 	 * Add new range to memblock so that when hotadd_new_pgdat() is called
11047f36e3e5STang Chen 	 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
11057f36e3e5STang Chen 	 * this new range and calculate total pages correctly.  The range will
11067f36e3e5STang Chen 	 * be removed at hot-remove time.
11077f36e3e5STang Chen 	 */
11087f36e3e5STang Chen 	memblock_add_node(start, size, nid);
11097f36e3e5STang Chen 
1110b9ff0360SOscar Salvador 	ret = __try_online_node(nid, start, false);
1111b9ff0360SOscar Salvador 	if (ret < 0)
111241b9e2d7SWen Congyang 		goto error;
1113b9ff0360SOscar Salvador 	new_node = ret;
11149af3c2deSYasunori Goto 
1115bc02af93SYasunori Goto 	/* call arch's memory hotadd */
111624e6d5a5SChristoph Hellwig 	ret = arch_add_memory(nid, start, size, NULL, true);
11179af3c2deSYasunori Goto 	if (ret < 0)
11189af3c2deSYasunori Goto 		goto error;
11199af3c2deSYasunori Goto 
1120a1e565aaSTang Chen 	if (new_node) {
1121d5b6f6a3SOscar Salvador 		/* If sysfs file of new node can't be created, cpu on the node
11220fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
11230fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
1124d5b6f6a3SOscar Salvador 		 * We online node here. We can't roll back from here.
11250fc44159SYasunori Goto 		 */
1126d5b6f6a3SOscar Salvador 		node_set_online(nid);
1127d5b6f6a3SOscar Salvador 		ret = __register_one_node(nid);
11280fc44159SYasunori Goto 		BUG_ON(ret);
11290fc44159SYasunori Goto 	}
11300fc44159SYasunori Goto 
1131d5b6f6a3SOscar Salvador 	/* link memory sections under this node.*/
11324fbce633SOscar Salvador 	ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
1133d5b6f6a3SOscar Salvador 	BUG_ON(ret);
1134d5b6f6a3SOscar Salvador 
1135d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
1136d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
1137d96ae530Sakpm@linux-foundation.org 
1138381eab4aSDavid Hildenbrand 	/* device_online() will take the lock when calling online_pages() */
1139381eab4aSDavid Hildenbrand 	mem_hotplug_done();
1140381eab4aSDavid Hildenbrand 
114131bc3858SVitaly Kuznetsov 	/* online pages if requested */
1142f29d8e9cSDavid Hildenbrand 	if (memhp_auto_online)
114331bc3858SVitaly Kuznetsov 		walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
114431bc3858SVitaly Kuznetsov 				  NULL, online_memory_block);
114531bc3858SVitaly Kuznetsov 
1146381eab4aSDavid Hildenbrand 	return ret;
11479af3c2deSYasunori Goto error:
11489af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
1149b9ff0360SOscar Salvador 	if (new_node)
1150b9ff0360SOscar Salvador 		rollback_node_hotadd(nid);
11517f36e3e5STang Chen 	memblock_remove(start, size);
1152bfc8c901SVladimir Davydov 	mem_hotplug_done();
1153bc02af93SYasunori Goto 	return ret;
1154bc02af93SYasunori Goto }
115562cedb9fSDavid Vrabel 
11568df1d0e4SDavid Hildenbrand /* requires device_hotplug_lock, see add_memory_resource() */
11578df1d0e4SDavid Hildenbrand int __ref __add_memory(int nid, u64 start, u64 size)
115862cedb9fSDavid Vrabel {
115962cedb9fSDavid Vrabel 	struct resource *res;
116062cedb9fSDavid Vrabel 	int ret;
116162cedb9fSDavid Vrabel 
116262cedb9fSDavid Vrabel 	res = register_memory_resource(start, size);
11636f754ba4SVitaly Kuznetsov 	if (IS_ERR(res))
11646f754ba4SVitaly Kuznetsov 		return PTR_ERR(res);
116562cedb9fSDavid Vrabel 
1166f29d8e9cSDavid Hildenbrand 	ret = add_memory_resource(nid, res);
116762cedb9fSDavid Vrabel 	if (ret < 0)
116862cedb9fSDavid Vrabel 		release_memory_resource(res);
116962cedb9fSDavid Vrabel 	return ret;
117062cedb9fSDavid Vrabel }
11718df1d0e4SDavid Hildenbrand 
11728df1d0e4SDavid Hildenbrand int add_memory(int nid, u64 start, u64 size)
11738df1d0e4SDavid Hildenbrand {
11748df1d0e4SDavid Hildenbrand 	int rc;
11758df1d0e4SDavid Hildenbrand 
11768df1d0e4SDavid Hildenbrand 	lock_device_hotplug();
11778df1d0e4SDavid Hildenbrand 	rc = __add_memory(nid, start, size);
11788df1d0e4SDavid Hildenbrand 	unlock_device_hotplug();
11798df1d0e4SDavid Hildenbrand 
11808df1d0e4SDavid Hildenbrand 	return rc;
11818df1d0e4SDavid Hildenbrand }
1182bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
11830c0e6195SKAMEZAWA Hiroyuki 
11840c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
11850c0e6195SKAMEZAWA Hiroyuki /*
11865c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
11875c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
11885c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
11895c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
11905c755e9fSBadari Pulavarty  * be located at the start of the pageblock
11915c755e9fSBadari Pulavarty  */
11925c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
11935c755e9fSBadari Pulavarty {
11945c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
11955c755e9fSBadari Pulavarty }
11965c755e9fSBadari Pulavarty 
11975c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */
11985c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page)
11995c755e9fSBadari Pulavarty {
12005c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
12015c755e9fSBadari Pulavarty 	BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
12025c755e9fSBadari Pulavarty 
12035c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
12040dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
12050dcc48c1SKAMEZAWA Hiroyuki 		int order;
12060dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
12070dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
12080dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
12090dcc48c1SKAMEZAWA Hiroyuki 			return page + (1 << order);
12100dcc48c1SKAMEZAWA Hiroyuki 	}
12115c755e9fSBadari Pulavarty 
12120dcc48c1SKAMEZAWA Hiroyuki 	return page + pageblock_nr_pages;
12135c755e9fSBadari Pulavarty }
12145c755e9fSBadari Pulavarty 
1215fb52bbaeSMathieu Malaterre static bool is_pageblock_removable_nolock(struct page *page)
1216fb52bbaeSMathieu Malaterre {
1217fb52bbaeSMathieu Malaterre 	struct zone *zone;
1218fb52bbaeSMathieu Malaterre 	unsigned long pfn;
1219fb52bbaeSMathieu Malaterre 
1220fb52bbaeSMathieu Malaterre 	/*
1221fb52bbaeSMathieu Malaterre 	 * We have to be careful here because we are iterating over memory
1222fb52bbaeSMathieu Malaterre 	 * sections which are not zone aware so we might end up outside of
1223fb52bbaeSMathieu Malaterre 	 * the zone but still within the section.
1224fb52bbaeSMathieu Malaterre 	 * We have to take care about the node as well. If the node is offline
1225fb52bbaeSMathieu Malaterre 	 * its NODE_DATA will be NULL - see page_zone.
1226fb52bbaeSMathieu Malaterre 	 */
1227fb52bbaeSMathieu Malaterre 	if (!node_online(page_to_nid(page)))
1228fb52bbaeSMathieu Malaterre 		return false;
1229fb52bbaeSMathieu Malaterre 
1230fb52bbaeSMathieu Malaterre 	zone = page_zone(page);
1231fb52bbaeSMathieu Malaterre 	pfn = page_to_pfn(page);
1232fb52bbaeSMathieu Malaterre 	if (!zone_spans_pfn(zone, pfn))
1233fb52bbaeSMathieu Malaterre 		return false;
1234fb52bbaeSMathieu Malaterre 
1235d381c547SMichal Hocko 	return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON);
1236fb52bbaeSMathieu Malaterre }
1237fb52bbaeSMathieu Malaterre 
12385c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
1239c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
12405c755e9fSBadari Pulavarty {
12415c755e9fSBadari Pulavarty 	struct page *page = pfn_to_page(start_pfn);
1242efad4e47SMichal Hocko 	unsigned long end_pfn = min(start_pfn + nr_pages, zone_end_pfn(page_zone(page)));
1243efad4e47SMichal Hocko 	struct page *end_page = pfn_to_page(end_pfn);
12445c755e9fSBadari Pulavarty 
12455c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
12465c755e9fSBadari Pulavarty 	for (; page < end_page; page = next_active_pageblock(page)) {
124749ac8255SKAMEZAWA Hiroyuki 		if (!is_pageblock_removable_nolock(page))
1248c98940f6SYaowei Bai 			return false;
124949ac8255SKAMEZAWA Hiroyuki 		cond_resched();
12505c755e9fSBadari Pulavarty 	}
12515c755e9fSBadari Pulavarty 
12525c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
1253c98940f6SYaowei Bai 	return true;
12545c755e9fSBadari Pulavarty }
12555c755e9fSBadari Pulavarty 
12565c755e9fSBadari Pulavarty /*
1257deb88a2aSToshi Kani  * Confirm all pages in a range [start, end) belong to the same zone.
1258a96dfddbSToshi Kani  * When true, return its valid [start, end).
12590c0e6195SKAMEZAWA Hiroyuki  */
1260a96dfddbSToshi Kani int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1261a96dfddbSToshi Kani 			 unsigned long *valid_start, unsigned long *valid_end)
12620c0e6195SKAMEZAWA Hiroyuki {
12635f0f2887SAndrew Banman 	unsigned long pfn, sec_end_pfn;
1264a96dfddbSToshi Kani 	unsigned long start, end;
12650c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
12660c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12670c0e6195SKAMEZAWA Hiroyuki 	int i;
1268deb88a2aSToshi Kani 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
12690c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
1270deb88a2aSToshi Kani 	     pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
12715f0f2887SAndrew Banman 		/* Make sure the memory section is present first */
12725f0f2887SAndrew Banman 		if (!present_section_nr(pfn_to_section_nr(pfn)))
12735f0f2887SAndrew Banman 			continue;
12745f0f2887SAndrew Banman 		for (; pfn < sec_end_pfn && pfn < end_pfn;
12750c0e6195SKAMEZAWA Hiroyuki 		     pfn += MAX_ORDER_NR_PAGES) {
12760c0e6195SKAMEZAWA Hiroyuki 			i = 0;
12770c0e6195SKAMEZAWA Hiroyuki 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
12785f0f2887SAndrew Banman 			while ((i < MAX_ORDER_NR_PAGES) &&
12795f0f2887SAndrew Banman 				!pfn_valid_within(pfn + i))
12800c0e6195SKAMEZAWA Hiroyuki 				i++;
1281d6d8c8a4Szhong jiang 			if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
12820c0e6195SKAMEZAWA Hiroyuki 				continue;
128324feb47cSMikhail Zaslonko 			/* Check if we got outside of the zone */
128424feb47cSMikhail Zaslonko 			if (zone && !zone_spans_pfn(zone, pfn + i))
128524feb47cSMikhail Zaslonko 				return 0;
12860c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn + i);
12870c0e6195SKAMEZAWA Hiroyuki 			if (zone && page_zone(page) != zone)
12880c0e6195SKAMEZAWA Hiroyuki 				return 0;
1289a96dfddbSToshi Kani 			if (!zone)
1290a96dfddbSToshi Kani 				start = pfn + i;
12910c0e6195SKAMEZAWA Hiroyuki 			zone = page_zone(page);
1292a96dfddbSToshi Kani 			end = pfn + MAX_ORDER_NR_PAGES;
12930c0e6195SKAMEZAWA Hiroyuki 		}
12945f0f2887SAndrew Banman 	}
1295deb88a2aSToshi Kani 
1296a96dfddbSToshi Kani 	if (zone) {
1297a96dfddbSToshi Kani 		*valid_start = start;
1298d6d8c8a4Szhong jiang 		*valid_end = min(end, end_pfn);
12990c0e6195SKAMEZAWA Hiroyuki 		return 1;
1300a96dfddbSToshi Kani 	} else {
1301deb88a2aSToshi Kani 		return 0;
13020c0e6195SKAMEZAWA Hiroyuki 	}
1303a96dfddbSToshi Kani }
13040c0e6195SKAMEZAWA Hiroyuki 
13050c0e6195SKAMEZAWA Hiroyuki /*
13060efadf48SYisheng Xie  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
13070efadf48SYisheng Xie  * non-lru movable pages and hugepages). We scan pfn because it's much
13080efadf48SYisheng Xie  * easier than scanning over linked list. This function returns the pfn
13090efadf48SYisheng Xie  * of the first found movable page if it's found, otherwise 0.
13100c0e6195SKAMEZAWA Hiroyuki  */
1311c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
13120c0e6195SKAMEZAWA Hiroyuki {
13130c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
1314eeb0efd0SOscar Salvador 
13150c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
1316eeb0efd0SOscar Salvador 		struct page *page, *head;
1317eeb0efd0SOscar Salvador 		unsigned long skip;
1318eeb0efd0SOscar Salvador 
1319eeb0efd0SOscar Salvador 		if (!pfn_valid(pfn))
1320eeb0efd0SOscar Salvador 			continue;
13210c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
13220c0e6195SKAMEZAWA Hiroyuki 		if (PageLRU(page))
13230c0e6195SKAMEZAWA Hiroyuki 			return pfn;
13240efadf48SYisheng Xie 		if (__PageMovable(page))
13250efadf48SYisheng Xie 			return pfn;
1326eeb0efd0SOscar Salvador 
1327eeb0efd0SOscar Salvador 		if (!PageHuge(page))
1328eeb0efd0SOscar Salvador 			continue;
1329eeb0efd0SOscar Salvador 		head = compound_head(page);
1330eeb0efd0SOscar Salvador 		if (hugepage_migration_supported(page_hstate(head)) &&
1331eeb0efd0SOscar Salvador 		    page_huge_active(head))
1332c8721bbbSNaoya Horiguchi 			return pfn;
1333eeb0efd0SOscar Salvador 		skip = (1 << compound_order(head)) - (page - head);
1334eeb0efd0SOscar Salvador 		pfn += skip - 1;
13350c0e6195SKAMEZAWA Hiroyuki 	}
13360c0e6195SKAMEZAWA Hiroyuki 	return 0;
13370c0e6195SKAMEZAWA Hiroyuki }
13380c0e6195SKAMEZAWA Hiroyuki 
1339666feb21SMichal Hocko static struct page *new_node_page(struct page *page, unsigned long private)
1340394e31d2SXishi Qiu {
1341394e31d2SXishi Qiu 	int nid = page_to_nid(page);
1342231e97e2SLi Zhong 	nodemask_t nmask = node_states[N_MEMORY];
13437f252f27SMichal Hocko 
13447f252f27SMichal Hocko 	/*
13457f252f27SMichal Hocko 	 * try to allocate from a different node but reuse this node if there
13467f252f27SMichal Hocko 	 * are no other online nodes to be used (e.g. we are offlining a part
13477f252f27SMichal Hocko 	 * of the only existing node)
13487f252f27SMichal Hocko 	 */
13497f252f27SMichal Hocko 	node_clear(nid, nmask);
13507f252f27SMichal Hocko 	if (nodes_empty(nmask))
13517f252f27SMichal Hocko 		node_set(nid, nmask);
1352394e31d2SXishi Qiu 
13538b913238SMichal Hocko 	return new_page_nodemask(page, nid, &nmask);
1354394e31d2SXishi Qiu }
1355394e31d2SXishi Qiu 
13560c0e6195SKAMEZAWA Hiroyuki static int
13570c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
13580c0e6195SKAMEZAWA Hiroyuki {
13590c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
13600c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
13610c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
13620c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
13630c0e6195SKAMEZAWA Hiroyuki 
1364a85009c3SMichal Hocko 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
13650c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
13660c0e6195SKAMEZAWA Hiroyuki 			continue;
13670c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
1368c8721bbbSNaoya Horiguchi 
1369c8721bbbSNaoya Horiguchi 		if (PageHuge(page)) {
1370c8721bbbSNaoya Horiguchi 			struct page *head = compound_head(page);
1371c8721bbbSNaoya Horiguchi 			pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1372c8721bbbSNaoya Horiguchi 			if (compound_order(head) > PFN_SECTION_SHIFT) {
1373c8721bbbSNaoya Horiguchi 				ret = -EBUSY;
1374c8721bbbSNaoya Horiguchi 				break;
1375c8721bbbSNaoya Horiguchi 			}
1376a85009c3SMichal Hocko 			isolate_huge_page(page, &source);
1377c8721bbbSNaoya Horiguchi 			continue;
137894723aafSMichal Hocko 		} else if (PageTransHuge(page))
13798135d892SNaoya Horiguchi 			pfn = page_to_pfn(compound_head(page))
13808135d892SNaoya Horiguchi 				+ hpage_nr_pages(page) - 1;
1381c8721bbbSNaoya Horiguchi 
1382b15c8726SMichal Hocko 		/*
1383b15c8726SMichal Hocko 		 * HWPoison pages have elevated reference counts so the migration would
1384b15c8726SMichal Hocko 		 * fail on them. It also doesn't make any sense to migrate them in the
1385b15c8726SMichal Hocko 		 * first place. Still try to unmap such a page in case it is still mapped
1386b15c8726SMichal Hocko 		 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1387b15c8726SMichal Hocko 		 * the unmap as the catch all safety net).
1388b15c8726SMichal Hocko 		 */
1389b15c8726SMichal Hocko 		if (PageHWPoison(page)) {
1390b15c8726SMichal Hocko 			if (WARN_ON(PageLRU(page)))
1391b15c8726SMichal Hocko 				isolate_lru_page(page);
1392b15c8726SMichal Hocko 			if (page_mapped(page))
1393b15c8726SMichal Hocko 				try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1394b15c8726SMichal Hocko 			continue;
1395b15c8726SMichal Hocko 		}
1396b15c8726SMichal Hocko 
1397700c2a46SKonstantin Khlebnikov 		if (!get_page_unless_zero(page))
13980c0e6195SKAMEZAWA Hiroyuki 			continue;
13990c0e6195SKAMEZAWA Hiroyuki 		/*
14000efadf48SYisheng Xie 		 * We can skip free pages. And we can deal with pages on
14010efadf48SYisheng Xie 		 * LRU and non-lru movable pages.
14020c0e6195SKAMEZAWA Hiroyuki 		 */
14030efadf48SYisheng Xie 		if (PageLRU(page))
140462695a84SNick Piggin 			ret = isolate_lru_page(page);
14050efadf48SYisheng Xie 		else
14060efadf48SYisheng Xie 			ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
14070c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
140862695a84SNick Piggin 			list_add_tail(&page->lru, &source);
14090efadf48SYisheng Xie 			if (!__PageMovable(page))
1410599d0c95SMel Gorman 				inc_node_page_state(page, NR_ISOLATED_ANON +
14116d9c285aSKOSAKI Motohiro 						    page_is_file_cache(page));
14126d9c285aSKOSAKI Motohiro 
14130c0e6195SKAMEZAWA Hiroyuki 		} else {
14142932c8b0SMichal Hocko 			pr_warn("failed to isolate pfn %lx\n", pfn);
14150efadf48SYisheng Xie 			dump_page(page, "isolation failed");
14161723058eSOscar Salvador 		}
1417700c2a46SKonstantin Khlebnikov 		put_page(page);
14180c0e6195SKAMEZAWA Hiroyuki 	}
1419f3ab2636SBob Liu 	if (!list_empty(&source)) {
1420394e31d2SXishi Qiu 		/* Allocate a new page from the nearest neighbor node */
1421394e31d2SXishi Qiu 		ret = migrate_pages(&source, new_node_page, NULL, 0,
14229c620e2bSHugh Dickins 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
14232932c8b0SMichal Hocko 		if (ret) {
14242932c8b0SMichal Hocko 			list_for_each_entry(page, &source, lru) {
14252932c8b0SMichal Hocko 				pr_warn("migrating pfn %lx failed ret:%d ",
14262932c8b0SMichal Hocko 				       page_to_pfn(page), ret);
14272932c8b0SMichal Hocko 				dump_page(page, "migration failure");
14282932c8b0SMichal Hocko 			}
1429c8721bbbSNaoya Horiguchi 			putback_movable_pages(&source);
1430f3ab2636SBob Liu 		}
14312932c8b0SMichal Hocko 	}
14321723058eSOscar Salvador 
14330c0e6195SKAMEZAWA Hiroyuki 	return ret;
14340c0e6195SKAMEZAWA Hiroyuki }
14350c0e6195SKAMEZAWA Hiroyuki 
14360c0e6195SKAMEZAWA Hiroyuki /*
14370c0e6195SKAMEZAWA Hiroyuki  * remove from free_area[] and mark all as Reserved.
14380c0e6195SKAMEZAWA Hiroyuki  */
14390c0e6195SKAMEZAWA Hiroyuki static int
14400c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
14410c0e6195SKAMEZAWA Hiroyuki 			void *data)
14420c0e6195SKAMEZAWA Hiroyuki {
14430c0e6195SKAMEZAWA Hiroyuki 	__offline_isolated_pages(start, start + nr_pages);
14440c0e6195SKAMEZAWA Hiroyuki 	return 0;
14450c0e6195SKAMEZAWA Hiroyuki }
14460c0e6195SKAMEZAWA Hiroyuki 
14470c0e6195SKAMEZAWA Hiroyuki static void
14480c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
14490c0e6195SKAMEZAWA Hiroyuki {
1450908eedc6SKAMEZAWA Hiroyuki 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
14510c0e6195SKAMEZAWA Hiroyuki 				offline_isolated_pages_cb);
14520c0e6195SKAMEZAWA Hiroyuki }
14530c0e6195SKAMEZAWA Hiroyuki 
14540c0e6195SKAMEZAWA Hiroyuki /*
14550c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
14560c0e6195SKAMEZAWA Hiroyuki  */
14570c0e6195SKAMEZAWA Hiroyuki static int
14580c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
14590c0e6195SKAMEZAWA Hiroyuki 			void *data)
14600c0e6195SKAMEZAWA Hiroyuki {
14610c0e6195SKAMEZAWA Hiroyuki 	int ret;
14620c0e6195SKAMEZAWA Hiroyuki 	long offlined = *(long *)data;
1463b023f468SWen Congyang 	ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
14640c0e6195SKAMEZAWA Hiroyuki 	offlined = nr_pages;
14650c0e6195SKAMEZAWA Hiroyuki 	if (!ret)
14660c0e6195SKAMEZAWA Hiroyuki 		*(long *)data += offlined;
14670c0e6195SKAMEZAWA Hiroyuki 	return ret;
14680c0e6195SKAMEZAWA Hiroyuki }
14690c0e6195SKAMEZAWA Hiroyuki 
14700c0e6195SKAMEZAWA Hiroyuki static long
14710c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
14720c0e6195SKAMEZAWA Hiroyuki {
14730c0e6195SKAMEZAWA Hiroyuki 	long offlined = 0;
14740c0e6195SKAMEZAWA Hiroyuki 	int ret;
14750c0e6195SKAMEZAWA Hiroyuki 
1476908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
14770c0e6195SKAMEZAWA Hiroyuki 			check_pages_isolated_cb);
14780c0e6195SKAMEZAWA Hiroyuki 	if (ret < 0)
14790c0e6195SKAMEZAWA Hiroyuki 		offlined = (long)ret;
14800c0e6195SKAMEZAWA Hiroyuki 	return offlined;
14810c0e6195SKAMEZAWA Hiroyuki }
14820c0e6195SKAMEZAWA Hiroyuki 
1483c5320926STang Chen static int __init cmdline_parse_movable_node(char *p)
1484c5320926STang Chen {
14854932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
148655ac590cSTang Chen 	movable_node_enabled = true;
14874932381eSMichal Hocko #else
14884932381eSMichal Hocko 	pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
14894932381eSMichal Hocko #endif
1490c5320926STang Chen 	return 0;
1491c5320926STang Chen }
1492c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node);
1493c5320926STang Chen 
1494d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */
1495d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages,
1496d9713679SLai Jiangshan 		struct zone *zone, struct memory_notify *arg)
1497d9713679SLai Jiangshan {
1498d9713679SLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
1499d9713679SLai Jiangshan 	unsigned long present_pages = 0;
150086b27beaSOscar Salvador 	enum zone_type zt;
1501d9713679SLai Jiangshan 
150286b27beaSOscar Salvador 	arg->status_change_nid = -1;
1503d9713679SLai Jiangshan 	arg->status_change_nid_normal = -1;
150486b27beaSOscar Salvador 	arg->status_change_nid_high = -1;
150586b27beaSOscar Salvador 
150686b27beaSOscar Salvador 	/*
150786b27beaSOscar Salvador 	 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
150886b27beaSOscar Salvador 	 * If the memory to be offline is within the range
150986b27beaSOscar Salvador 	 * [0..ZONE_NORMAL], and it is the last present memory there,
151086b27beaSOscar Salvador 	 * the zones in that range will become empty after the offlining,
151186b27beaSOscar Salvador 	 * thus we can determine that we need to clear the node from
151286b27beaSOscar Salvador 	 * node_states[N_NORMAL_MEMORY].
151386b27beaSOscar Salvador 	 */
151486b27beaSOscar Salvador 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
151586b27beaSOscar Salvador 		present_pages += pgdat->node_zones[zt].present_pages;
151686b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
151786b27beaSOscar Salvador 		arg->status_change_nid_normal = zone_to_nid(zone);
1518d9713679SLai Jiangshan 
15196715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
15206715ddf9SLai Jiangshan 	/*
152186b27beaSOscar Salvador 	 * node_states[N_HIGH_MEMORY] contains nodes which
152286b27beaSOscar Salvador 	 * have normal memory or high memory.
152386b27beaSOscar Salvador 	 * Here we add the present_pages belonging to ZONE_HIGHMEM.
152486b27beaSOscar Salvador 	 * If the zone is within the range of [0..ZONE_HIGHMEM), and
152586b27beaSOscar Salvador 	 * we determine that the zones in that range become empty,
152686b27beaSOscar Salvador 	 * we need to clear the node for N_HIGH_MEMORY.
15276715ddf9SLai Jiangshan 	 */
152886b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
152986b27beaSOscar Salvador 	if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
15306715ddf9SLai Jiangshan 		arg->status_change_nid_high = zone_to_nid(zone);
15316715ddf9SLai Jiangshan #endif
15326715ddf9SLai Jiangshan 
1533d9713679SLai Jiangshan 	/*
153486b27beaSOscar Salvador 	 * We have accounted the pages from [0..ZONE_NORMAL), and
153586b27beaSOscar Salvador 	 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
153686b27beaSOscar Salvador 	 * as well.
153786b27beaSOscar Salvador 	 * Here we count the possible pages from ZONE_MOVABLE.
153886b27beaSOscar Salvador 	 * If after having accounted all the pages, we see that the nr_pages
153986b27beaSOscar Salvador 	 * to be offlined is over or equal to the accounted pages,
154086b27beaSOscar Salvador 	 * we know that the node will become empty, and so, we can clear
154186b27beaSOscar Salvador 	 * it for N_MEMORY as well.
1542d9713679SLai Jiangshan 	 */
154386b27beaSOscar Salvador 	present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1544d9713679SLai Jiangshan 
1545d9713679SLai Jiangshan 	if (nr_pages >= present_pages)
1546d9713679SLai Jiangshan 		arg->status_change_nid = zone_to_nid(zone);
1547d9713679SLai Jiangshan }
1548d9713679SLai Jiangshan 
1549d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg)
1550d9713679SLai Jiangshan {
1551d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
1552d9713679SLai Jiangshan 		node_clear_state(node, N_NORMAL_MEMORY);
1553d9713679SLai Jiangshan 
1554cf01f6f5SOscar Salvador 	if (arg->status_change_nid_high >= 0)
1555d9713679SLai Jiangshan 		node_clear_state(node, N_HIGH_MEMORY);
15566715ddf9SLai Jiangshan 
1557cf01f6f5SOscar Salvador 	if (arg->status_change_nid >= 0)
15586715ddf9SLai Jiangshan 		node_clear_state(node, N_MEMORY);
1559d9713679SLai Jiangshan }
1560d9713679SLai Jiangshan 
1561a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn,
1562ecde0f3eSMichal Hocko 		  unsigned long end_pfn)
15630c0e6195SKAMEZAWA Hiroyuki {
1564ecde0f3eSMichal Hocko 	unsigned long pfn, nr_pages;
15650c0e6195SKAMEZAWA Hiroyuki 	long offlined_pages;
156672b39cfcSMichal Hocko 	int ret, node;
1567d702909fSCody P Schafer 	unsigned long flags;
1568a96dfddbSToshi Kani 	unsigned long valid_start, valid_end;
15690c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
15707b78d335SYasunori Goto 	struct memory_notify arg;
157179605093SMichal Hocko 	char *reason;
15720c0e6195SKAMEZAWA Hiroyuki 
1573381eab4aSDavid Hildenbrand 	mem_hotplug_begin();
1574381eab4aSDavid Hildenbrand 
15750c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
15760c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
1577381eab4aSDavid Hildenbrand 	if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1578381eab4aSDavid Hildenbrand 				  &valid_end)) {
157979605093SMichal Hocko 		ret = -EINVAL;
158079605093SMichal Hocko 		reason = "multizone range";
158179605093SMichal Hocko 		goto failed_removal;
1582381eab4aSDavid Hildenbrand 	}
15837b78d335SYasunori Goto 
1584a96dfddbSToshi Kani 	zone = page_zone(pfn_to_page(valid_start));
15857b78d335SYasunori Goto 	node = zone_to_nid(zone);
15867b78d335SYasunori Goto 	nr_pages = end_pfn - start_pfn;
15877b78d335SYasunori Goto 
15880c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
1589b023f468SWen Congyang 	ret = start_isolate_page_range(start_pfn, end_pfn,
1590d381c547SMichal Hocko 				       MIGRATE_MOVABLE,
1591d381c547SMichal Hocko 				       SKIP_HWPOISON | REPORT_FAILURE);
1592381eab4aSDavid Hildenbrand 	if (ret) {
159379605093SMichal Hocko 		reason = "failure to isolate range";
159479605093SMichal Hocko 		goto failed_removal;
1595381eab4aSDavid Hildenbrand 	}
15967b78d335SYasunori Goto 
15977b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
15987b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
1599d9713679SLai Jiangshan 	node_states_check_changes_offline(nr_pages, zone, &arg);
16007b78d335SYasunori Goto 
16017b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
16027b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
160379605093SMichal Hocko 	if (ret) {
160479605093SMichal Hocko 		reason = "notifier failure";
160579605093SMichal Hocko 		goto failed_removal_isolated;
160679605093SMichal Hocko 	}
16077b78d335SYasunori Goto 
1608bb8965bdSMichal Hocko 	do {
1609bb8965bdSMichal Hocko 		for (pfn = start_pfn; pfn;) {
161079605093SMichal Hocko 			if (signal_pending(current)) {
1611bb8965bdSMichal Hocko 				ret = -EINTR;
161279605093SMichal Hocko 				reason = "signal backoff";
161379605093SMichal Hocko 				goto failed_removal_isolated;
161479605093SMichal Hocko 			}
161572b39cfcSMichal Hocko 
16160c0e6195SKAMEZAWA Hiroyuki 			cond_resched();
16179852a721SMichal Hocko 			lru_add_drain_all();
1618c0554329SVlastimil Babka 			drain_all_pages(zone);
16190c0e6195SKAMEZAWA Hiroyuki 
1620bb8965bdSMichal Hocko 			pfn = scan_movable_pages(pfn, end_pfn);
1621bb8965bdSMichal Hocko 			if (pfn) {
1622bb8965bdSMichal Hocko 				/*
1623bb8965bdSMichal Hocko 				 * TODO: fatal migration failures should bail
1624bb8965bdSMichal Hocko 				 * out
1625bb8965bdSMichal Hocko 				 */
1626bb8965bdSMichal Hocko 				do_migrate_range(pfn, end_pfn);
1627bb8965bdSMichal Hocko 			}
16280c0e6195SKAMEZAWA Hiroyuki 		}
162972b39cfcSMichal Hocko 
1630c8721bbbSNaoya Horiguchi 		/*
1631bb8965bdSMichal Hocko 		 * Dissolve free hugepages in the memory block before doing
1632bb8965bdSMichal Hocko 		 * offlining actually in order to make hugetlbfs's object
1633bb8965bdSMichal Hocko 		 * counting consistent.
1634c8721bbbSNaoya Horiguchi 		 */
1635082d5b6bSGerald Schaefer 		ret = dissolve_free_huge_pages(start_pfn, end_pfn);
163679605093SMichal Hocko 		if (ret) {
163779605093SMichal Hocko 			reason = "failure to dissolve huge pages";
163879605093SMichal Hocko 			goto failed_removal_isolated;
163979605093SMichal Hocko 		}
16400c0e6195SKAMEZAWA Hiroyuki 		/* check again */
16410c0e6195SKAMEZAWA Hiroyuki 		offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1642bb8965bdSMichal Hocko 	} while (offlined_pages < 0);
1643bb8965bdSMichal Hocko 
1644e33e33b4SChen Yucong 	pr_info("Offlined Pages %ld\n", offlined_pages);
1645b3834be5SAdam Buchbinder 	/* Ok, all of our target is isolated.
16460c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
16470c0e6195SKAMEZAWA Hiroyuki 	offline_isolated_pages(start_pfn, end_pfn);
1648dbc0e4ceSKAMEZAWA Hiroyuki 	/* reset pagetype flags and makes migrate type to be MOVABLE */
16490815f3d8SMichal Nazarewicz 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
16500c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
16513dcc0571SJiang Liu 	adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
16520c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
1653d702909fSCody P Schafer 
1654d702909fSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
16550c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
1656d702909fSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
16577b78d335SYasunori Goto 
16581b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
16591b79acc9SKOSAKI Motohiro 
16601e8537baSXishi Qiu 	if (!populated_zone(zone)) {
1661340175b7SJiang Liu 		zone_pcp_reset(zone);
166272675e13SMichal Hocko 		build_all_zonelists(NULL);
16631e8537baSXishi Qiu 	} else
16641e8537baSXishi Qiu 		zone_pcp_update(zone);
1665340175b7SJiang Liu 
1666d9713679SLai Jiangshan 	node_states_clear_node(node, &arg);
1667698b1b30SVlastimil Babka 	if (arg.status_change_nid >= 0) {
16688fe23e05SDavid Rientjes 		kswapd_stop(node);
1669698b1b30SVlastimil Babka 		kcompactd_stop(node);
1670698b1b30SVlastimil Babka 	}
1671bce7394aSMinchan Kim 
16720c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
16730c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
16747b78d335SYasunori Goto 
16757b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
1676381eab4aSDavid Hildenbrand 	mem_hotplug_done();
16770c0e6195SKAMEZAWA Hiroyuki 	return 0;
16780c0e6195SKAMEZAWA Hiroyuki 
167979605093SMichal Hocko failed_removal_isolated:
168079605093SMichal Hocko 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
16810c0e6195SKAMEZAWA Hiroyuki failed_removal:
168279605093SMichal Hocko 	pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1683a62e2f4fSBjorn Helgaas 		 (unsigned long long) start_pfn << PAGE_SHIFT,
168479605093SMichal Hocko 		 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
168579605093SMichal Hocko 		 reason);
16867b78d335SYasunori Goto 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
16870c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
1688381eab4aSDavid Hildenbrand 	mem_hotplug_done();
16890c0e6195SKAMEZAWA Hiroyuki 	return ret;
16900c0e6195SKAMEZAWA Hiroyuki }
169171088785SBadari Pulavarty 
1692a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1693a16cee10SWen Congyang {
1694ecde0f3eSMichal Hocko 	return __offline_pages(start_pfn, start_pfn + nr_pages);
1695a16cee10SWen Congyang }
1696e2ff3940SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1697a16cee10SWen Congyang 
1698bbc76be6SWen Congyang /**
1699bbc76be6SWen Congyang  * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1700bbc76be6SWen Congyang  * @start_pfn: start pfn of the memory range
1701e05c4bbfSToshi Kani  * @end_pfn: end pfn of the memory range
1702bbc76be6SWen Congyang  * @arg: argument passed to func
1703bbc76be6SWen Congyang  * @func: callback for each memory section walked
1704bbc76be6SWen Congyang  *
1705bbc76be6SWen Congyang  * This function walks through all present mem sections in range
1706bbc76be6SWen Congyang  * [start_pfn, end_pfn) and call func on each mem section.
1707bbc76be6SWen Congyang  *
1708bbc76be6SWen Congyang  * Returns the return value of func.
1709bbc76be6SWen Congyang  */
1710e2ff3940SRafael J. Wysocki int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1711bbc76be6SWen Congyang 		void *arg, int (*func)(struct memory_block *, void *))
171271088785SBadari Pulavarty {
1713e90bdb7fSWen Congyang 	struct memory_block *mem = NULL;
1714e90bdb7fSWen Congyang 	struct mem_section *section;
1715e90bdb7fSWen Congyang 	unsigned long pfn, section_nr;
1716e90bdb7fSWen Congyang 	int ret;
171771088785SBadari Pulavarty 
1718e90bdb7fSWen Congyang 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1719e90bdb7fSWen Congyang 		section_nr = pfn_to_section_nr(pfn);
1720e90bdb7fSWen Congyang 		if (!present_section_nr(section_nr))
1721e90bdb7fSWen Congyang 			continue;
1722e90bdb7fSWen Congyang 
1723e90bdb7fSWen Congyang 		section = __nr_to_section(section_nr);
1724e90bdb7fSWen Congyang 		/* same memblock? */
1725e90bdb7fSWen Congyang 		if (mem)
1726e90bdb7fSWen Congyang 			if ((section_nr >= mem->start_section_nr) &&
1727e90bdb7fSWen Congyang 			    (section_nr <= mem->end_section_nr))
1728e90bdb7fSWen Congyang 				continue;
1729e90bdb7fSWen Congyang 
1730e90bdb7fSWen Congyang 		mem = find_memory_block_hinted(section, mem);
1731e90bdb7fSWen Congyang 		if (!mem)
1732e90bdb7fSWen Congyang 			continue;
1733e90bdb7fSWen Congyang 
1734bbc76be6SWen Congyang 		ret = func(mem, arg);
1735e90bdb7fSWen Congyang 		if (ret) {
1736e90bdb7fSWen Congyang 			kobject_put(&mem->dev.kobj);
1737e90bdb7fSWen Congyang 			return ret;
1738e90bdb7fSWen Congyang 		}
1739e90bdb7fSWen Congyang 	}
1740e90bdb7fSWen Congyang 
1741e90bdb7fSWen Congyang 	if (mem)
1742e90bdb7fSWen Congyang 		kobject_put(&mem->dev.kobj);
1743e90bdb7fSWen Congyang 
1744bbc76be6SWen Congyang 	return 0;
1745bbc76be6SWen Congyang }
1746bbc76be6SWen Congyang 
1747e2ff3940SRafael J. Wysocki #ifdef CONFIG_MEMORY_HOTREMOVE
1748d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1749bbc76be6SWen Congyang {
1750bbc76be6SWen Congyang 	int ret = !is_memblock_offlined(mem);
1751bbc76be6SWen Congyang 
1752349daa0fSRandy Dunlap 	if (unlikely(ret)) {
1753349daa0fSRandy Dunlap 		phys_addr_t beginpa, endpa;
1754349daa0fSRandy Dunlap 
1755349daa0fSRandy Dunlap 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1756349daa0fSRandy Dunlap 		endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1757756a025fSJoe Perches 		pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1758349daa0fSRandy Dunlap 			&beginpa, &endpa);
1759349daa0fSRandy Dunlap 	}
1760bbc76be6SWen Congyang 
1761bbc76be6SWen Congyang 	return ret;
1762bbc76be6SWen Congyang }
1763bbc76be6SWen Congyang 
17640f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat)
176560a5a19eSTang Chen {
176660a5a19eSTang Chen 	int cpu;
176760a5a19eSTang Chen 
176860a5a19eSTang Chen 	for_each_present_cpu(cpu) {
176960a5a19eSTang Chen 		if (cpu_to_node(cpu) == pgdat->node_id)
177060a5a19eSTang Chen 			/*
177160a5a19eSTang Chen 			 * the cpu on this node isn't removed, and we can't
177260a5a19eSTang Chen 			 * offline this node.
177360a5a19eSTang Chen 			 */
177460a5a19eSTang Chen 			return -EBUSY;
177560a5a19eSTang Chen 	}
177660a5a19eSTang Chen 
177760a5a19eSTang Chen 	return 0;
177860a5a19eSTang Chen }
177960a5a19eSTang Chen 
17800f1cfe9dSToshi Kani /**
17810f1cfe9dSToshi Kani  * try_offline_node
1782e8b098fcSMike Rapoport  * @nid: the node ID
17830f1cfe9dSToshi Kani  *
17840f1cfe9dSToshi Kani  * Offline a node if all memory sections and cpus of the node are removed.
17850f1cfe9dSToshi Kani  *
17860f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
17870f1cfe9dSToshi Kani  * and online/offline operations before this call.
17880f1cfe9dSToshi Kani  */
178990b30cdcSWen Congyang void try_offline_node(int nid)
179060a5a19eSTang Chen {
1791d822b86aSWen Congyang 	pg_data_t *pgdat = NODE_DATA(nid);
1792d822b86aSWen Congyang 	unsigned long start_pfn = pgdat->node_start_pfn;
1793d822b86aSWen Congyang 	unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
179460a5a19eSTang Chen 	unsigned long pfn;
179560a5a19eSTang Chen 
179660a5a19eSTang Chen 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
179760a5a19eSTang Chen 		unsigned long section_nr = pfn_to_section_nr(pfn);
179860a5a19eSTang Chen 
179960a5a19eSTang Chen 		if (!present_section_nr(section_nr))
180060a5a19eSTang Chen 			continue;
180160a5a19eSTang Chen 
180260a5a19eSTang Chen 		if (pfn_to_nid(pfn) != nid)
180360a5a19eSTang Chen 			continue;
180460a5a19eSTang Chen 
180560a5a19eSTang Chen 		/*
180660a5a19eSTang Chen 		 * some memory sections of this node are not removed, and we
180760a5a19eSTang Chen 		 * can't offline node now.
180860a5a19eSTang Chen 		 */
180960a5a19eSTang Chen 		return;
181060a5a19eSTang Chen 	}
181160a5a19eSTang Chen 
181246a3679bSMichal Hocko 	if (check_cpu_on_node(pgdat))
181360a5a19eSTang Chen 		return;
181460a5a19eSTang Chen 
181560a5a19eSTang Chen 	/*
181660a5a19eSTang Chen 	 * all memory/cpu of this node are removed, we can offline this
181760a5a19eSTang Chen 	 * node now.
181860a5a19eSTang Chen 	 */
181960a5a19eSTang Chen 	node_set_offline(nid);
182060a5a19eSTang Chen 	unregister_one_node(nid);
182160a5a19eSTang Chen }
182290b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node);
182360a5a19eSTang Chen 
18240f1cfe9dSToshi Kani /**
18250f1cfe9dSToshi Kani  * remove_memory
1826e8b098fcSMike Rapoport  * @nid: the node ID
1827e8b098fcSMike Rapoport  * @start: physical address of the region to remove
1828e8b098fcSMike Rapoport  * @size: size of the region to remove
18290f1cfe9dSToshi Kani  *
18300f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
18310f1cfe9dSToshi Kani  * and online/offline operations before this call, as required by
18320f1cfe9dSToshi Kani  * try_offline_node().
18330f1cfe9dSToshi Kani  */
1834d15e5926SDavid Hildenbrand void __ref __remove_memory(int nid, u64 start, u64 size)
1835bbc76be6SWen Congyang {
1836242831ebSRafael J. Wysocki 	int ret;
1837993c1aadSWen Congyang 
183827356f54SToshi Kani 	BUG_ON(check_hotplug_memory_range(start, size));
183927356f54SToshi Kani 
1840bfc8c901SVladimir Davydov 	mem_hotplug_begin();
18416677e3eaSYasuaki Ishimatsu 
18426677e3eaSYasuaki Ishimatsu 	/*
1843242831ebSRafael J. Wysocki 	 * All memory blocks must be offlined before removing memory.  Check
1844242831ebSRafael J. Wysocki 	 * whether all memory blocks in question are offline and trigger a BUG()
1845242831ebSRafael J. Wysocki 	 * if this is not the case.
18466677e3eaSYasuaki Ishimatsu 	 */
1847242831ebSRafael J. Wysocki 	ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1848d6de9d53SXishi Qiu 				check_memblock_offlined_cb);
1849bfc8c901SVladimir Davydov 	if (ret)
1850242831ebSRafael J. Wysocki 		BUG();
18516677e3eaSYasuaki Ishimatsu 
185246c66c4bSYasuaki Ishimatsu 	/* remove memmap entry */
185346c66c4bSYasuaki Ishimatsu 	firmware_map_remove(start, start + size, "System RAM");
1854f9126ab9SXishi Qiu 	memblock_free(start, size);
1855f9126ab9SXishi Qiu 	memblock_remove(start, size);
185646c66c4bSYasuaki Ishimatsu 
18572c2a5af6SOscar Salvador 	arch_remove_memory(nid, start, size, NULL);
185824d335caSWen Congyang 
185960a5a19eSTang Chen 	try_offline_node(nid);
186060a5a19eSTang Chen 
1861bfc8c901SVladimir Davydov 	mem_hotplug_done();
186271088785SBadari Pulavarty }
1863d15e5926SDavid Hildenbrand 
1864d15e5926SDavid Hildenbrand void remove_memory(int nid, u64 start, u64 size)
1865d15e5926SDavid Hildenbrand {
1866d15e5926SDavid Hildenbrand 	lock_device_hotplug();
1867d15e5926SDavid Hildenbrand 	__remove_memory(nid, start, size);
1868d15e5926SDavid Hildenbrand 	unlock_device_hotplug();
1869d15e5926SDavid Hildenbrand }
187071088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
1871aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1872