xref: /openbmc/linux/mm/memory_hotplug.c (revision f64ac5e6e30668216cf489d73ba8a96e372d78c6)
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>
36f784a3f1STang Chen #include <linux/bootmem.h>
37698b1b30SVlastimil Babka #include <linux/compaction.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 
9945e0b78bSKeith Mannthey /* add this memory to iomem resource */
10045e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
10145e0b78bSKeith Mannthey {
1025042db43SJérôme Glisse 	struct resource *res, *conflict;
10345e0b78bSKeith Mannthey 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1046f754ba4SVitaly Kuznetsov 	if (!res)
1056f754ba4SVitaly Kuznetsov 		return ERR_PTR(-ENOMEM);
10645e0b78bSKeith Mannthey 
10745e0b78bSKeith Mannthey 	res->name = "System RAM";
10845e0b78bSKeith Mannthey 	res->start = start;
10945e0b78bSKeith Mannthey 	res->end = start + size - 1;
110782b8664SToshi Kani 	res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1115042db43SJérôme Glisse 	conflict =  request_resource_conflict(&iomem_resource, res);
1125042db43SJérôme Glisse 	if (conflict) {
1135042db43SJérôme Glisse 		if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1145042db43SJérôme Glisse 			pr_debug("Device unaddressable memory block "
1155042db43SJérôme Glisse 				 "memory hotplug at %#010llx !\n",
1165042db43SJérôme Glisse 				 (unsigned long long)start);
1175042db43SJérôme Glisse 		}
1184996eed8SToshi Kani 		pr_debug("System RAM resource %pR cannot be added\n", res);
11945e0b78bSKeith Mannthey 		kfree(res);
1206f754ba4SVitaly Kuznetsov 		return ERR_PTR(-EEXIST);
12145e0b78bSKeith Mannthey 	}
12245e0b78bSKeith Mannthey 	return res;
12345e0b78bSKeith Mannthey }
12445e0b78bSKeith Mannthey 
12545e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
12645e0b78bSKeith Mannthey {
12745e0b78bSKeith Mannthey 	if (!res)
12845e0b78bSKeith Mannthey 		return;
12945e0b78bSKeith Mannthey 	release_resource(res);
13045e0b78bSKeith Mannthey 	kfree(res);
13145e0b78bSKeith Mannthey 	return;
13245e0b78bSKeith Mannthey }
13345e0b78bSKeith Mannthey 
13453947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
13546723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info,  struct page *page,
1365f24ce5fSAndrea Arcangeli 		      unsigned long type)
13704753278SYasunori Goto {
138ddffe98dSYasuaki Ishimatsu 	page->freelist = (void *)type;
13904753278SYasunori Goto 	SetPagePrivate(page);
14004753278SYasunori Goto 	set_page_private(page, info);
141fe896d18SJoonsoo Kim 	page_ref_inc(page);
14204753278SYasunori Goto }
14304753278SYasunori Goto 
144170a5a7eSJiang Liu void put_page_bootmem(struct page *page)
14504753278SYasunori Goto {
1465f24ce5fSAndrea Arcangeli 	unsigned long type;
14704753278SYasunori Goto 
148ddffe98dSYasuaki Ishimatsu 	type = (unsigned long) page->freelist;
1495f24ce5fSAndrea Arcangeli 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
1505f24ce5fSAndrea Arcangeli 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
15104753278SYasunori Goto 
152fe896d18SJoonsoo Kim 	if (page_ref_dec_return(page) == 1) {
153ddffe98dSYasuaki Ishimatsu 		page->freelist = NULL;
15404753278SYasunori Goto 		ClearPagePrivate(page);
15504753278SYasunori Goto 		set_page_private(page, 0);
1565f24ce5fSAndrea Arcangeli 		INIT_LIST_HEAD(&page->lru);
157170a5a7eSJiang Liu 		free_reserved_page(page);
15804753278SYasunori Goto 	}
15904753278SYasunori Goto }
16004753278SYasunori Goto 
16146723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
16246723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP
163d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
16404753278SYasunori Goto {
16504753278SYasunori Goto 	unsigned long *usemap, mapsize, section_nr, i;
16604753278SYasunori Goto 	struct mem_section *ms;
16704753278SYasunori Goto 	struct page *page, *memmap;
16804753278SYasunori Goto 
16904753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
17004753278SYasunori Goto 	ms = __nr_to_section(section_nr);
17104753278SYasunori Goto 
17204753278SYasunori Goto 	/* Get section's memmap address */
17304753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
17404753278SYasunori Goto 
17504753278SYasunori Goto 	/*
17604753278SYasunori Goto 	 * Get page for the memmap's phys address
17704753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
17804753278SYasunori Goto 	 */
17904753278SYasunori Goto 	page = virt_to_page(memmap);
18004753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
18104753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
18204753278SYasunori Goto 
18304753278SYasunori Goto 	/* remember memmap's page */
18404753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
18504753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
18604753278SYasunori Goto 
18704753278SYasunori Goto 	usemap = __nr_to_section(section_nr)->pageblock_flags;
18804753278SYasunori Goto 	page = virt_to_page(usemap);
18904753278SYasunori Goto 
19004753278SYasunori Goto 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
19104753278SYasunori Goto 
19204753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
193af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
19404753278SYasunori Goto 
19504753278SYasunori Goto }
19646723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */
19746723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn)
19846723bfaSYasuaki Ishimatsu {
19946723bfaSYasuaki Ishimatsu 	unsigned long *usemap, mapsize, section_nr, i;
20046723bfaSYasuaki Ishimatsu 	struct mem_section *ms;
20146723bfaSYasuaki Ishimatsu 	struct page *page, *memmap;
20246723bfaSYasuaki Ishimatsu 
20346723bfaSYasuaki Ishimatsu 	if (!pfn_valid(start_pfn))
20446723bfaSYasuaki Ishimatsu 		return;
20546723bfaSYasuaki Ishimatsu 
20646723bfaSYasuaki Ishimatsu 	section_nr = pfn_to_section_nr(start_pfn);
20746723bfaSYasuaki Ishimatsu 	ms = __nr_to_section(section_nr);
20846723bfaSYasuaki Ishimatsu 
20946723bfaSYasuaki Ishimatsu 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
21046723bfaSYasuaki Ishimatsu 
21146723bfaSYasuaki Ishimatsu 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
21246723bfaSYasuaki Ishimatsu 
21346723bfaSYasuaki Ishimatsu 	usemap = __nr_to_section(section_nr)->pageblock_flags;
21446723bfaSYasuaki Ishimatsu 	page = virt_to_page(usemap);
21546723bfaSYasuaki Ishimatsu 
21646723bfaSYasuaki Ishimatsu 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
21746723bfaSYasuaki Ishimatsu 
21846723bfaSYasuaki Ishimatsu 	for (i = 0; i < mapsize; i++, page++)
21946723bfaSYasuaki Ishimatsu 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
22046723bfaSYasuaki Ishimatsu }
22146723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
22204753278SYasunori Goto 
2237ded384aSLinus Torvalds void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
22404753278SYasunori Goto {
22504753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
22604753278SYasunori Goto 	int node = pgdat->node_id;
22704753278SYasunori Goto 	struct page *page;
22804753278SYasunori Goto 
22904753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
23004753278SYasunori Goto 	page = virt_to_page(pgdat);
23104753278SYasunori Goto 
23204753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
23304753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
23404753278SYasunori Goto 
23504753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
236c1f19495SCody P Schafer 	end_pfn = pgdat_end_pfn(pgdat);
23704753278SYasunori Goto 
2387e9f5eb0STang Chen 	/* register section info */
239f14851afSqiuxishi 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
240f14851afSqiuxishi 		/*
241f14851afSqiuxishi 		 * Some platforms can assign the same pfn to multiple nodes - on
242f14851afSqiuxishi 		 * node0 as well as nodeN.  To avoid registering a pfn against
243f14851afSqiuxishi 		 * multiple nodes we check that this pfn does not already
2447e9f5eb0STang Chen 		 * reside in some other nodes.
245f14851afSqiuxishi 		 */
246f65e91dfSYang Shi 		if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
24704753278SYasunori Goto 			register_page_bootmem_info_section(pfn);
248f14851afSqiuxishi 	}
24904753278SYasunori Goto }
25046723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
25104753278SYasunori Goto 
252f1dd2cd1SMichal Hocko static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
253f1dd2cd1SMichal Hocko 		bool want_memblock)
2543947be19SDave Hansen {
2553947be19SDave Hansen 	int ret;
256f1dd2cd1SMichal Hocko 	int i;
2573947be19SDave Hansen 
258ebd15302SKAMEZAWA Hiroyuki 	if (pfn_valid(phys_start_pfn))
259ebd15302SKAMEZAWA Hiroyuki 		return -EEXIST;
260ebd15302SKAMEZAWA Hiroyuki 
261f1dd2cd1SMichal Hocko 	ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn);
2623947be19SDave Hansen 	if (ret < 0)
2633947be19SDave Hansen 		return ret;
2643947be19SDave Hansen 
265f1dd2cd1SMichal Hocko 	/*
266f1dd2cd1SMichal Hocko 	 * Make all the pages reserved so that nobody will stumble over half
267f1dd2cd1SMichal Hocko 	 * initialized state.
268f1dd2cd1SMichal Hocko 	 * FIXME: We also have to associate it with a node because pfn_to_node
269f1dd2cd1SMichal Hocko 	 * relies on having page with the proper node.
270f1dd2cd1SMichal Hocko 	 */
271f1dd2cd1SMichal Hocko 	for (i = 0; i < PAGES_PER_SECTION; i++) {
272f1dd2cd1SMichal Hocko 		unsigned long pfn = phys_start_pfn + i;
273f1dd2cd1SMichal Hocko 		struct page *page;
274f1dd2cd1SMichal Hocko 		if (!pfn_valid(pfn))
275f1dd2cd1SMichal Hocko 			continue;
276718127ccSYasunori Goto 
277f1dd2cd1SMichal Hocko 		page = pfn_to_page(pfn);
278f1dd2cd1SMichal Hocko 		set_page_node(page, nid);
279f1dd2cd1SMichal Hocko 		SetPageReserved(page);
280f1dd2cd1SMichal Hocko 	}
281718127ccSYasunori Goto 
2821b862aecSMichal Hocko 	if (!want_memblock)
2831b862aecSMichal Hocko 		return 0;
2841b862aecSMichal Hocko 
285c04fc586SGary Hade 	return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
2863947be19SDave Hansen }
2873947be19SDave Hansen 
2884edd7cefSDavid Rientjes /*
2894edd7cefSDavid Rientjes  * Reasonably generic function for adding memory.  It is
2904edd7cefSDavid Rientjes  * expected that archs that support memory hotplug will
2914edd7cefSDavid Rientjes  * call this function after deciding the zone to which to
2924edd7cefSDavid Rientjes  * add the new pages.
2934edd7cefSDavid Rientjes  */
294f1dd2cd1SMichal Hocko int __ref __add_pages(int nid, unsigned long phys_start_pfn,
2951b862aecSMichal Hocko 			unsigned long nr_pages, bool want_memblock)
2964edd7cefSDavid Rientjes {
2974edd7cefSDavid Rientjes 	unsigned long i;
2984edd7cefSDavid Rientjes 	int err = 0;
2994edd7cefSDavid Rientjes 	int start_sec, end_sec;
3004b94ffdcSDan Williams 	struct vmem_altmap *altmap;
3014b94ffdcSDan Williams 
3024edd7cefSDavid Rientjes 	/* during initialize mem_map, align hot-added range to section */
3034edd7cefSDavid Rientjes 	start_sec = pfn_to_section_nr(phys_start_pfn);
3044edd7cefSDavid Rientjes 	end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
3054edd7cefSDavid Rientjes 
3064b94ffdcSDan Williams 	altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
3074b94ffdcSDan Williams 	if (altmap) {
3084b94ffdcSDan Williams 		/*
3094b94ffdcSDan Williams 		 * Validate altmap is within bounds of the total request
3104b94ffdcSDan Williams 		 */
3114b94ffdcSDan Williams 		if (altmap->base_pfn != phys_start_pfn
3124b94ffdcSDan Williams 				|| vmem_altmap_offset(altmap) > nr_pages) {
3134b94ffdcSDan Williams 			pr_warn_once("memory add fail, invalid altmap\n");
3147cf91a98SJoonsoo Kim 			err = -EINVAL;
3157cf91a98SJoonsoo Kim 			goto out;
3164b94ffdcSDan Williams 		}
3174b94ffdcSDan Williams 		altmap->alloc = 0;
3184b94ffdcSDan Williams 	}
3194b94ffdcSDan Williams 
3204edd7cefSDavid Rientjes 	for (i = start_sec; i <= end_sec; i++) {
321f1dd2cd1SMichal Hocko 		err = __add_section(nid, section_nr_to_pfn(i), want_memblock);
3224edd7cefSDavid Rientjes 
3234edd7cefSDavid Rientjes 		/*
3244edd7cefSDavid Rientjes 		 * EEXIST is finally dealt with by ioresource collision
3254edd7cefSDavid Rientjes 		 * check. see add_memory() => register_memory_resource()
3264edd7cefSDavid Rientjes 		 * Warning will be printed if there is collision.
3274edd7cefSDavid Rientjes 		 */
3284edd7cefSDavid Rientjes 		if (err && (err != -EEXIST))
3294edd7cefSDavid Rientjes 			break;
3304edd7cefSDavid Rientjes 		err = 0;
331*f64ac5e6SMichal Hocko 		cond_resched();
3324edd7cefSDavid Rientjes 	}
333c435a390SZhu Guihua 	vmemmap_populate_print_last();
3347cf91a98SJoonsoo Kim out:
3354edd7cefSDavid Rientjes 	return err;
3364edd7cefSDavid Rientjes }
3374edd7cefSDavid Rientjes EXPORT_SYMBOL_GPL(__add_pages);
3384edd7cefSDavid Rientjes 
3394edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE
340815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
341815121d2SYasuaki Ishimatsu static int find_smallest_section_pfn(int nid, struct zone *zone,
342815121d2SYasuaki Ishimatsu 				     unsigned long start_pfn,
343815121d2SYasuaki Ishimatsu 				     unsigned long end_pfn)
344815121d2SYasuaki Ishimatsu {
345815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
346815121d2SYasuaki Ishimatsu 
347815121d2SYasuaki Ishimatsu 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
348815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(start_pfn);
349815121d2SYasuaki Ishimatsu 
350815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
351815121d2SYasuaki Ishimatsu 			continue;
352815121d2SYasuaki Ishimatsu 
353815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(start_pfn) != nid))
354815121d2SYasuaki Ishimatsu 			continue;
355815121d2SYasuaki Ishimatsu 
356815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(start_pfn)))
357815121d2SYasuaki Ishimatsu 			continue;
358815121d2SYasuaki Ishimatsu 
359815121d2SYasuaki Ishimatsu 		return start_pfn;
360815121d2SYasuaki Ishimatsu 	}
361815121d2SYasuaki Ishimatsu 
362815121d2SYasuaki Ishimatsu 	return 0;
363815121d2SYasuaki Ishimatsu }
364815121d2SYasuaki Ishimatsu 
365815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
366815121d2SYasuaki Ishimatsu static int find_biggest_section_pfn(int nid, struct zone *zone,
367815121d2SYasuaki Ishimatsu 				    unsigned long start_pfn,
368815121d2SYasuaki Ishimatsu 				    unsigned long end_pfn)
369815121d2SYasuaki Ishimatsu {
370815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
371815121d2SYasuaki Ishimatsu 	unsigned long pfn;
372815121d2SYasuaki Ishimatsu 
373815121d2SYasuaki Ishimatsu 	/* pfn is the end pfn of a memory section. */
374815121d2SYasuaki Ishimatsu 	pfn = end_pfn - 1;
375815121d2SYasuaki Ishimatsu 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
376815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
377815121d2SYasuaki Ishimatsu 
378815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
379815121d2SYasuaki Ishimatsu 			continue;
380815121d2SYasuaki Ishimatsu 
381815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(pfn) != nid))
382815121d2SYasuaki Ishimatsu 			continue;
383815121d2SYasuaki Ishimatsu 
384815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(pfn)))
385815121d2SYasuaki Ishimatsu 			continue;
386815121d2SYasuaki Ishimatsu 
387815121d2SYasuaki Ishimatsu 		return pfn;
388815121d2SYasuaki Ishimatsu 	}
389815121d2SYasuaki Ishimatsu 
390815121d2SYasuaki Ishimatsu 	return 0;
391815121d2SYasuaki Ishimatsu }
392815121d2SYasuaki Ishimatsu 
393815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
394815121d2SYasuaki Ishimatsu 			     unsigned long end_pfn)
395815121d2SYasuaki Ishimatsu {
396815121d2SYasuaki Ishimatsu 	unsigned long zone_start_pfn = zone->zone_start_pfn;
397c33bc315SXishi Qiu 	unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
398c33bc315SXishi Qiu 	unsigned long zone_end_pfn = z;
399815121d2SYasuaki Ishimatsu 	unsigned long pfn;
400815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
401815121d2SYasuaki Ishimatsu 	int nid = zone_to_nid(zone);
402815121d2SYasuaki Ishimatsu 
403815121d2SYasuaki Ishimatsu 	zone_span_writelock(zone);
404815121d2SYasuaki Ishimatsu 	if (zone_start_pfn == start_pfn) {
405815121d2SYasuaki Ishimatsu 		/*
406815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the zone, it need
407815121d2SYasuaki Ishimatsu 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
408815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
409815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
410815121d2SYasuaki Ishimatsu 		 */
411815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
412815121d2SYasuaki Ishimatsu 						zone_end_pfn);
413815121d2SYasuaki Ishimatsu 		if (pfn) {
414815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = pfn;
415815121d2SYasuaki Ishimatsu 			zone->spanned_pages = zone_end_pfn - pfn;
416815121d2SYasuaki Ishimatsu 		}
417815121d2SYasuaki Ishimatsu 	} else if (zone_end_pfn == end_pfn) {
418815121d2SYasuaki Ishimatsu 		/*
419815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the zone, it need
420815121d2SYasuaki Ishimatsu 		 * shrink zone->spanned_pages.
421815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
422815121d2SYasuaki Ishimatsu 		 * shrinking zone.
423815121d2SYasuaki Ishimatsu 		 */
424815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
425815121d2SYasuaki Ishimatsu 					       start_pfn);
426815121d2SYasuaki Ishimatsu 		if (pfn)
427815121d2SYasuaki Ishimatsu 			zone->spanned_pages = pfn - zone_start_pfn + 1;
428815121d2SYasuaki Ishimatsu 	}
429815121d2SYasuaki Ishimatsu 
430815121d2SYasuaki Ishimatsu 	/*
431815121d2SYasuaki Ishimatsu 	 * The section is not biggest or smallest mem_section in the zone, it
432815121d2SYasuaki Ishimatsu 	 * only creates a hole in the zone. So in this case, we need not
433815121d2SYasuaki Ishimatsu 	 * change the zone. But perhaps, the zone has only hole data. Thus
434815121d2SYasuaki Ishimatsu 	 * it check the zone has only hole or not.
435815121d2SYasuaki Ishimatsu 	 */
436815121d2SYasuaki Ishimatsu 	pfn = zone_start_pfn;
437815121d2SYasuaki Ishimatsu 	for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
438815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
439815121d2SYasuaki Ishimatsu 
440815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
441815121d2SYasuaki Ishimatsu 			continue;
442815121d2SYasuaki Ishimatsu 
443815121d2SYasuaki Ishimatsu 		if (page_zone(pfn_to_page(pfn)) != zone)
444815121d2SYasuaki Ishimatsu 			continue;
445815121d2SYasuaki Ishimatsu 
446815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
447815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
448815121d2SYasuaki Ishimatsu 			continue;
449815121d2SYasuaki Ishimatsu 
450815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
451815121d2SYasuaki Ishimatsu 		zone_span_writeunlock(zone);
452815121d2SYasuaki Ishimatsu 		return;
453815121d2SYasuaki Ishimatsu 	}
454815121d2SYasuaki Ishimatsu 
455815121d2SYasuaki Ishimatsu 	/* The zone has no valid section */
456815121d2SYasuaki Ishimatsu 	zone->zone_start_pfn = 0;
457815121d2SYasuaki Ishimatsu 	zone->spanned_pages = 0;
458815121d2SYasuaki Ishimatsu 	zone_span_writeunlock(zone);
459815121d2SYasuaki Ishimatsu }
460815121d2SYasuaki Ishimatsu 
461815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat,
462815121d2SYasuaki Ishimatsu 			      unsigned long start_pfn, unsigned long end_pfn)
463815121d2SYasuaki Ishimatsu {
464815121d2SYasuaki Ishimatsu 	unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
46583285c72SXishi Qiu 	unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
46683285c72SXishi Qiu 	unsigned long pgdat_end_pfn = p;
467815121d2SYasuaki Ishimatsu 	unsigned long pfn;
468815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
469815121d2SYasuaki Ishimatsu 	int nid = pgdat->node_id;
470815121d2SYasuaki Ishimatsu 
471815121d2SYasuaki Ishimatsu 	if (pgdat_start_pfn == start_pfn) {
472815121d2SYasuaki Ishimatsu 		/*
473815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the pgdat, it need
474815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
475815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
476815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
477815121d2SYasuaki Ishimatsu 		 */
478815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
479815121d2SYasuaki Ishimatsu 						pgdat_end_pfn);
480815121d2SYasuaki Ishimatsu 		if (pfn) {
481815121d2SYasuaki Ishimatsu 			pgdat->node_start_pfn = pfn;
482815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
483815121d2SYasuaki Ishimatsu 		}
484815121d2SYasuaki Ishimatsu 	} else if (pgdat_end_pfn == end_pfn) {
485815121d2SYasuaki Ishimatsu 		/*
486815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the pgdat, it need
487815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_spanned_pages.
488815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
489815121d2SYasuaki Ishimatsu 		 * shrinking zone.
490815121d2SYasuaki Ishimatsu 		 */
491815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
492815121d2SYasuaki Ishimatsu 					       start_pfn);
493815121d2SYasuaki Ishimatsu 		if (pfn)
494815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
495815121d2SYasuaki Ishimatsu 	}
496815121d2SYasuaki Ishimatsu 
497815121d2SYasuaki Ishimatsu 	/*
498815121d2SYasuaki Ishimatsu 	 * If the section is not biggest or smallest mem_section in the pgdat,
499815121d2SYasuaki Ishimatsu 	 * it only creates a hole in the pgdat. So in this case, we need not
500815121d2SYasuaki Ishimatsu 	 * change the pgdat.
501815121d2SYasuaki Ishimatsu 	 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
502815121d2SYasuaki Ishimatsu 	 * has only hole or not.
503815121d2SYasuaki Ishimatsu 	 */
504815121d2SYasuaki Ishimatsu 	pfn = pgdat_start_pfn;
505815121d2SYasuaki Ishimatsu 	for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
506815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
507815121d2SYasuaki Ishimatsu 
508815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
509815121d2SYasuaki Ishimatsu 			continue;
510815121d2SYasuaki Ishimatsu 
511815121d2SYasuaki Ishimatsu 		if (pfn_to_nid(pfn) != nid)
512815121d2SYasuaki Ishimatsu 			continue;
513815121d2SYasuaki Ishimatsu 
514815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
515815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
516815121d2SYasuaki Ishimatsu 			continue;
517815121d2SYasuaki Ishimatsu 
518815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
519815121d2SYasuaki Ishimatsu 		return;
520815121d2SYasuaki Ishimatsu 	}
521815121d2SYasuaki Ishimatsu 
522815121d2SYasuaki Ishimatsu 	/* The pgdat has no valid section */
523815121d2SYasuaki Ishimatsu 	pgdat->node_start_pfn = 0;
524815121d2SYasuaki Ishimatsu 	pgdat->node_spanned_pages = 0;
525815121d2SYasuaki Ishimatsu }
526815121d2SYasuaki Ishimatsu 
527815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn)
528815121d2SYasuaki Ishimatsu {
529815121d2SYasuaki Ishimatsu 	struct pglist_data *pgdat = zone->zone_pgdat;
530815121d2SYasuaki Ishimatsu 	int nr_pages = PAGES_PER_SECTION;
531815121d2SYasuaki Ishimatsu 	unsigned long flags;
532815121d2SYasuaki Ishimatsu 
533815121d2SYasuaki Ishimatsu 	pgdat_resize_lock(zone->zone_pgdat, &flags);
534815121d2SYasuaki Ishimatsu 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
535815121d2SYasuaki Ishimatsu 	shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
536815121d2SYasuaki Ishimatsu 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
537815121d2SYasuaki Ishimatsu }
538815121d2SYasuaki Ishimatsu 
5394b94ffdcSDan Williams static int __remove_section(struct zone *zone, struct mem_section *ms,
5404b94ffdcSDan Williams 		unsigned long map_offset)
541ea01ea93SBadari Pulavarty {
542815121d2SYasuaki Ishimatsu 	unsigned long start_pfn;
543815121d2SYasuaki Ishimatsu 	int scn_nr;
544ea01ea93SBadari Pulavarty 	int ret = -EINVAL;
545ea01ea93SBadari Pulavarty 
546ea01ea93SBadari Pulavarty 	if (!valid_section(ms))
547ea01ea93SBadari Pulavarty 		return ret;
548ea01ea93SBadari Pulavarty 
549ea01ea93SBadari Pulavarty 	ret = unregister_memory_section(ms);
550ea01ea93SBadari Pulavarty 	if (ret)
551ea01ea93SBadari Pulavarty 		return ret;
552ea01ea93SBadari Pulavarty 
553815121d2SYasuaki Ishimatsu 	scn_nr = __section_nr(ms);
554815121d2SYasuaki Ishimatsu 	start_pfn = section_nr_to_pfn(scn_nr);
555815121d2SYasuaki Ishimatsu 	__remove_zone(zone, start_pfn);
556815121d2SYasuaki Ishimatsu 
5574b94ffdcSDan Williams 	sparse_remove_one_section(zone, ms, map_offset);
558ea01ea93SBadari Pulavarty 	return 0;
559ea01ea93SBadari Pulavarty }
560ea01ea93SBadari Pulavarty 
561ea01ea93SBadari Pulavarty /**
562ea01ea93SBadari Pulavarty  * __remove_pages() - remove sections of pages from a zone
563ea01ea93SBadari Pulavarty  * @zone: zone from which pages need to be removed
564ea01ea93SBadari Pulavarty  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
565ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
566ea01ea93SBadari Pulavarty  *
567ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
568ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
569ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
570ea01ea93SBadari Pulavarty  * calling offline_pages().
571ea01ea93SBadari Pulavarty  */
572ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
573ea01ea93SBadari Pulavarty 		 unsigned long nr_pages)
574ea01ea93SBadari Pulavarty {
575fe74ebb1SToshi Kani 	unsigned long i;
5764b94ffdcSDan Williams 	unsigned long map_offset = 0;
5774b94ffdcSDan Williams 	int sections_to_remove, ret = 0;
5784b94ffdcSDan Williams 
5794b94ffdcSDan Williams 	/* In the ZONE_DEVICE case device driver owns the memory region */
5804b94ffdcSDan Williams 	if (is_dev_zone(zone)) {
5814b94ffdcSDan Williams 		struct page *page = pfn_to_page(phys_start_pfn);
5824b94ffdcSDan Williams 		struct vmem_altmap *altmap;
5834b94ffdcSDan Williams 
5844b94ffdcSDan Williams 		altmap = to_vmem_altmap((unsigned long) page);
5854b94ffdcSDan Williams 		if (altmap)
5864b94ffdcSDan Williams 			map_offset = vmem_altmap_offset(altmap);
5874b94ffdcSDan Williams 	} else {
588fe74ebb1SToshi Kani 		resource_size_t start, size;
5894b94ffdcSDan Williams 
5904b94ffdcSDan Williams 		start = phys_start_pfn << PAGE_SHIFT;
5914b94ffdcSDan Williams 		size = nr_pages * PAGE_SIZE;
5924b94ffdcSDan Williams 
5934b94ffdcSDan Williams 		ret = release_mem_region_adjustable(&iomem_resource, start,
5944b94ffdcSDan Williams 					size);
5954b94ffdcSDan Williams 		if (ret) {
5964b94ffdcSDan Williams 			resource_size_t endres = start + size - 1;
5974b94ffdcSDan Williams 
5984b94ffdcSDan Williams 			pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
5994b94ffdcSDan Williams 					&start, &endres, ret);
6004b94ffdcSDan Williams 		}
6014b94ffdcSDan Williams 	}
602ea01ea93SBadari Pulavarty 
6037cf91a98SJoonsoo Kim 	clear_zone_contiguous(zone);
6047cf91a98SJoonsoo Kim 
605ea01ea93SBadari Pulavarty 	/*
606ea01ea93SBadari Pulavarty 	 * We can only remove entire sections
607ea01ea93SBadari Pulavarty 	 */
608ea01ea93SBadari Pulavarty 	BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
609ea01ea93SBadari Pulavarty 	BUG_ON(nr_pages % PAGES_PER_SECTION);
610ea01ea93SBadari Pulavarty 
611ea01ea93SBadari Pulavarty 	sections_to_remove = nr_pages / PAGES_PER_SECTION;
612ea01ea93SBadari Pulavarty 	for (i = 0; i < sections_to_remove; i++) {
613ea01ea93SBadari Pulavarty 		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
6144b94ffdcSDan Williams 
6154b94ffdcSDan Williams 		ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
6164b94ffdcSDan Williams 		map_offset = 0;
617ea01ea93SBadari Pulavarty 		if (ret)
618ea01ea93SBadari Pulavarty 			break;
619ea01ea93SBadari Pulavarty 	}
6207cf91a98SJoonsoo Kim 
6217cf91a98SJoonsoo Kim 	set_zone_contiguous(zone);
6227cf91a98SJoonsoo Kim 
623ea01ea93SBadari Pulavarty 	return ret;
624ea01ea93SBadari Pulavarty }
6254edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */
626ea01ea93SBadari Pulavarty 
6279d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback)
6289d0ad8caSDaniel Kiper {
6299d0ad8caSDaniel Kiper 	int rc = -EINVAL;
6309d0ad8caSDaniel Kiper 
631bfc8c901SVladimir Davydov 	get_online_mems();
632bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
6339d0ad8caSDaniel Kiper 
6349d0ad8caSDaniel Kiper 	if (online_page_callback == generic_online_page) {
6359d0ad8caSDaniel Kiper 		online_page_callback = callback;
6369d0ad8caSDaniel Kiper 		rc = 0;
6379d0ad8caSDaniel Kiper 	}
6389d0ad8caSDaniel Kiper 
639bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
640bfc8c901SVladimir Davydov 	put_online_mems();
6419d0ad8caSDaniel Kiper 
6429d0ad8caSDaniel Kiper 	return rc;
6439d0ad8caSDaniel Kiper }
6449d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback);
6459d0ad8caSDaniel Kiper 
6469d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback)
6479d0ad8caSDaniel Kiper {
6489d0ad8caSDaniel Kiper 	int rc = -EINVAL;
6499d0ad8caSDaniel Kiper 
650bfc8c901SVladimir Davydov 	get_online_mems();
651bfc8c901SVladimir Davydov 	mutex_lock(&online_page_callback_lock);
6529d0ad8caSDaniel Kiper 
6539d0ad8caSDaniel Kiper 	if (online_page_callback == callback) {
6549d0ad8caSDaniel Kiper 		online_page_callback = generic_online_page;
6559d0ad8caSDaniel Kiper 		rc = 0;
6569d0ad8caSDaniel Kiper 	}
6579d0ad8caSDaniel Kiper 
658bfc8c901SVladimir Davydov 	mutex_unlock(&online_page_callback_lock);
659bfc8c901SVladimir Davydov 	put_online_mems();
6609d0ad8caSDaniel Kiper 
6619d0ad8caSDaniel Kiper 	return rc;
6629d0ad8caSDaniel Kiper }
6639d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback);
6649d0ad8caSDaniel Kiper 
6659d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page)
666180c06efSJeremy Fitzhardinge {
6679d0ad8caSDaniel Kiper }
6689d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits);
6699d0ad8caSDaniel Kiper 
6709d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page)
6719d0ad8caSDaniel Kiper {
6723dcc0571SJiang Liu 	adjust_managed_page_count(page, 1);
6739d0ad8caSDaniel Kiper }
6749d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters);
675180c06efSJeremy Fitzhardinge 
6769d0ad8caSDaniel Kiper void __online_page_free(struct page *page)
6779d0ad8caSDaniel Kiper {
6783dcc0571SJiang Liu 	__free_reserved_page(page);
679180c06efSJeremy Fitzhardinge }
6809d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free);
6819d0ad8caSDaniel Kiper 
6829d0ad8caSDaniel Kiper static void generic_online_page(struct page *page)
6839d0ad8caSDaniel Kiper {
6849d0ad8caSDaniel Kiper 	__online_page_set_limits(page);
6859d0ad8caSDaniel Kiper 	__online_page_increment_counters(page);
6869d0ad8caSDaniel Kiper 	__online_page_free(page);
6879d0ad8caSDaniel Kiper }
688180c06efSJeremy Fitzhardinge 
68975884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
69075884fb1SKAMEZAWA Hiroyuki 			void *arg)
6913947be19SDave Hansen {
6923947be19SDave Hansen 	unsigned long i;
69375884fb1SKAMEZAWA Hiroyuki 	unsigned long onlined_pages = *(unsigned long *)arg;
69475884fb1SKAMEZAWA Hiroyuki 	struct page *page;
6952d070eabSMichal Hocko 
69675884fb1SKAMEZAWA Hiroyuki 	if (PageReserved(pfn_to_page(start_pfn)))
69775884fb1SKAMEZAWA Hiroyuki 		for (i = 0; i < nr_pages; i++) {
69875884fb1SKAMEZAWA Hiroyuki 			page = pfn_to_page(start_pfn + i);
6999d0ad8caSDaniel Kiper 			(*online_page_callback)(page);
70075884fb1SKAMEZAWA Hiroyuki 			onlined_pages++;
70175884fb1SKAMEZAWA Hiroyuki 		}
7022d070eabSMichal Hocko 
7032d070eabSMichal Hocko 	online_mem_sections(start_pfn, start_pfn + nr_pages);
7042d070eabSMichal Hocko 
70575884fb1SKAMEZAWA Hiroyuki 	*(unsigned long *)arg = onlined_pages;
70675884fb1SKAMEZAWA Hiroyuki 	return 0;
70775884fb1SKAMEZAWA Hiroyuki }
70875884fb1SKAMEZAWA Hiroyuki 
709d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */
710d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages,
711d9713679SLai Jiangshan 	struct zone *zone, struct memory_notify *arg)
712d9713679SLai Jiangshan {
713d9713679SLai Jiangshan 	int nid = zone_to_nid(zone);
714d9713679SLai Jiangshan 	enum zone_type zone_last = ZONE_NORMAL;
715d9713679SLai Jiangshan 
716d9713679SLai Jiangshan 	/*
7176715ddf9SLai Jiangshan 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
7186715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_NORMAL,
7196715ddf9SLai Jiangshan 	 * set zone_last to ZONE_NORMAL.
720d9713679SLai Jiangshan 	 *
7216715ddf9SLai Jiangshan 	 * If we don't have HIGHMEM nor movable node,
7226715ddf9SLai Jiangshan 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
7236715ddf9SLai Jiangshan 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
724d9713679SLai Jiangshan 	 */
7256715ddf9SLai Jiangshan 	if (N_MEMORY == N_NORMAL_MEMORY)
726d9713679SLai Jiangshan 		zone_last = ZONE_MOVABLE;
727d9713679SLai Jiangshan 
728d9713679SLai Jiangshan 	/*
729d9713679SLai Jiangshan 	 * if the memory to be online is in a zone of 0...zone_last, and
730d9713679SLai Jiangshan 	 * the zones of 0...zone_last don't have memory before online, we will
731d9713679SLai Jiangshan 	 * need to set the node to node_states[N_NORMAL_MEMORY] after
732d9713679SLai Jiangshan 	 * the memory is online.
733d9713679SLai Jiangshan 	 */
734d9713679SLai Jiangshan 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
735d9713679SLai Jiangshan 		arg->status_change_nid_normal = nid;
736d9713679SLai Jiangshan 	else
737d9713679SLai Jiangshan 		arg->status_change_nid_normal = -1;
738d9713679SLai Jiangshan 
7396715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
7406715ddf9SLai Jiangshan 	/*
7416715ddf9SLai Jiangshan 	 * If we have movable node, node_states[N_HIGH_MEMORY]
7426715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
7436715ddf9SLai Jiangshan 	 * set zone_last to ZONE_HIGHMEM.
7446715ddf9SLai Jiangshan 	 *
7456715ddf9SLai Jiangshan 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
7466715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
7476715ddf9SLai Jiangshan 	 * set zone_last to ZONE_MOVABLE.
7486715ddf9SLai Jiangshan 	 */
7496715ddf9SLai Jiangshan 	zone_last = ZONE_HIGHMEM;
7506715ddf9SLai Jiangshan 	if (N_MEMORY == N_HIGH_MEMORY)
7516715ddf9SLai Jiangshan 		zone_last = ZONE_MOVABLE;
7526715ddf9SLai Jiangshan 
7536715ddf9SLai Jiangshan 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
7546715ddf9SLai Jiangshan 		arg->status_change_nid_high = nid;
7556715ddf9SLai Jiangshan 	else
7566715ddf9SLai Jiangshan 		arg->status_change_nid_high = -1;
7576715ddf9SLai Jiangshan #else
7586715ddf9SLai Jiangshan 	arg->status_change_nid_high = arg->status_change_nid_normal;
7596715ddf9SLai Jiangshan #endif
7606715ddf9SLai Jiangshan 
761d9713679SLai Jiangshan 	/*
762d9713679SLai Jiangshan 	 * if the node don't have memory befor online, we will need to
7636715ddf9SLai Jiangshan 	 * set the node to node_states[N_MEMORY] after the memory
764d9713679SLai Jiangshan 	 * is online.
765d9713679SLai Jiangshan 	 */
7666715ddf9SLai Jiangshan 	if (!node_state(nid, N_MEMORY))
767d9713679SLai Jiangshan 		arg->status_change_nid = nid;
768d9713679SLai Jiangshan 	else
769d9713679SLai Jiangshan 		arg->status_change_nid = -1;
770d9713679SLai Jiangshan }
771d9713679SLai Jiangshan 
772d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg)
773d9713679SLai Jiangshan {
774d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
775d9713679SLai Jiangshan 		node_set_state(node, N_NORMAL_MEMORY);
776d9713679SLai Jiangshan 
7776715ddf9SLai Jiangshan 	if (arg->status_change_nid_high >= 0)
778d9713679SLai Jiangshan 		node_set_state(node, N_HIGH_MEMORY);
7796715ddf9SLai Jiangshan 
7806715ddf9SLai Jiangshan 	node_set_state(node, N_MEMORY);
781d9713679SLai Jiangshan }
782d9713679SLai Jiangshan 
783f1dd2cd1SMichal Hocko static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
784f1dd2cd1SMichal Hocko 		unsigned long nr_pages)
785f1dd2cd1SMichal Hocko {
786f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = zone_end_pfn(zone);
787f1dd2cd1SMichal Hocko 
788f1dd2cd1SMichal Hocko 	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
789f1dd2cd1SMichal Hocko 		zone->zone_start_pfn = start_pfn;
790f1dd2cd1SMichal Hocko 
791f1dd2cd1SMichal Hocko 	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
792f1dd2cd1SMichal Hocko }
793f1dd2cd1SMichal Hocko 
794f1dd2cd1SMichal Hocko static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
795f1dd2cd1SMichal Hocko                                      unsigned long nr_pages)
796f1dd2cd1SMichal Hocko {
797f1dd2cd1SMichal Hocko 	unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
798f1dd2cd1SMichal Hocko 
799f1dd2cd1SMichal Hocko 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
800f1dd2cd1SMichal Hocko 		pgdat->node_start_pfn = start_pfn;
801f1dd2cd1SMichal Hocko 
802f1dd2cd1SMichal Hocko 	pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
803f1dd2cd1SMichal Hocko }
804f1dd2cd1SMichal Hocko 
805cdf72f25SMichal Hocko void __ref move_pfn_range_to_zone(struct zone *zone,
806f1dd2cd1SMichal Hocko 		unsigned long start_pfn, unsigned long nr_pages)
807f1dd2cd1SMichal Hocko {
808f1dd2cd1SMichal Hocko 	struct pglist_data *pgdat = zone->zone_pgdat;
809f1dd2cd1SMichal Hocko 	int nid = pgdat->node_id;
810f1dd2cd1SMichal Hocko 	unsigned long flags;
811f1dd2cd1SMichal Hocko 
812f1dd2cd1SMichal Hocko 	if (zone_is_empty(zone))
813f1dd2cd1SMichal Hocko 		init_currently_empty_zone(zone, start_pfn, nr_pages);
814f1dd2cd1SMichal Hocko 
815f1dd2cd1SMichal Hocko 	clear_zone_contiguous(zone);
816f1dd2cd1SMichal Hocko 
817f1dd2cd1SMichal Hocko 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
818f1dd2cd1SMichal Hocko 	pgdat_resize_lock(pgdat, &flags);
819f1dd2cd1SMichal Hocko 	zone_span_writelock(zone);
820f1dd2cd1SMichal Hocko 	resize_zone_range(zone, start_pfn, nr_pages);
821f1dd2cd1SMichal Hocko 	zone_span_writeunlock(zone);
822f1dd2cd1SMichal Hocko 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
823f1dd2cd1SMichal Hocko 	pgdat_resize_unlock(pgdat, &flags);
824f1dd2cd1SMichal Hocko 
825f1dd2cd1SMichal Hocko 	/*
826f1dd2cd1SMichal Hocko 	 * TODO now we have a visible range of pages which are not associated
827f1dd2cd1SMichal Hocko 	 * with their zone properly. Not nice but set_pfnblock_flags_mask
828f1dd2cd1SMichal Hocko 	 * expects the zone spans the pfn range. All the pages in the range
829f1dd2cd1SMichal Hocko 	 * are reserved so nobody should be touching them so we should be safe
830f1dd2cd1SMichal Hocko 	 */
831f1dd2cd1SMichal Hocko 	memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, MEMMAP_HOTPLUG);
832f1dd2cd1SMichal Hocko 
833f1dd2cd1SMichal Hocko 	set_zone_contiguous(zone);
834f1dd2cd1SMichal Hocko }
835f1dd2cd1SMichal Hocko 
836f1dd2cd1SMichal Hocko /*
837c246a213SMichal Hocko  * Returns a default kernel memory zone for the given pfn range.
838c246a213SMichal Hocko  * If no kernel zone covers this pfn range it will automatically go
839c246a213SMichal Hocko  * to the ZONE_NORMAL.
840c246a213SMichal Hocko  */
841c6f03e29SMichal Hocko static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
842c246a213SMichal Hocko 		unsigned long nr_pages)
843c246a213SMichal Hocko {
844c246a213SMichal Hocko 	struct pglist_data *pgdat = NODE_DATA(nid);
845c246a213SMichal Hocko 	int zid;
846c246a213SMichal Hocko 
847c246a213SMichal Hocko 	for (zid = 0; zid <= ZONE_NORMAL; zid++) {
848c246a213SMichal Hocko 		struct zone *zone = &pgdat->node_zones[zid];
849c246a213SMichal Hocko 
850c246a213SMichal Hocko 		if (zone_intersects(zone, start_pfn, nr_pages))
851c246a213SMichal Hocko 			return zone;
852c246a213SMichal Hocko 	}
853c246a213SMichal Hocko 
854c246a213SMichal Hocko 	return &pgdat->node_zones[ZONE_NORMAL];
855c246a213SMichal Hocko }
856c246a213SMichal Hocko 
857c6f03e29SMichal Hocko static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
858c6f03e29SMichal Hocko 		unsigned long nr_pages)
859e5e68930SMichal Hocko {
860c6f03e29SMichal Hocko 	struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
861c6f03e29SMichal Hocko 			nr_pages);
862c6f03e29SMichal Hocko 	struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
863c6f03e29SMichal Hocko 	bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
864c6f03e29SMichal Hocko 	bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
865e5e68930SMichal Hocko 
866e5e68930SMichal Hocko 	/*
867c6f03e29SMichal Hocko 	 * We inherit the existing zone in a simple case where zones do not
868c6f03e29SMichal Hocko 	 * overlap in the given range
869e5e68930SMichal Hocko 	 */
870c6f03e29SMichal Hocko 	if (in_kernel ^ in_movable)
871c6f03e29SMichal Hocko 		return (in_kernel) ? kernel_zone : movable_zone;
872e5e68930SMichal Hocko 
873c6f03e29SMichal Hocko 	/*
874c6f03e29SMichal Hocko 	 * If the range doesn't belong to any zone or two zones overlap in the
875c6f03e29SMichal Hocko 	 * given range then we use movable zone only if movable_node is
876c6f03e29SMichal Hocko 	 * enabled because we always online to a kernel zone by default.
877c6f03e29SMichal Hocko 	 */
878c6f03e29SMichal Hocko 	return movable_node_enabled ? movable_zone : kernel_zone;
8799f123ab5SMichal Hocko }
8809f123ab5SMichal Hocko 
881e5e68930SMichal Hocko struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
882e5e68930SMichal Hocko 		unsigned long nr_pages)
883f1dd2cd1SMichal Hocko {
884c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_KERNEL)
885c6f03e29SMichal Hocko 		return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
886f1dd2cd1SMichal Hocko 
887c6f03e29SMichal Hocko 	if (online_type == MMOP_ONLINE_MOVABLE)
888c6f03e29SMichal Hocko 		return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
889f1dd2cd1SMichal Hocko 
890c6f03e29SMichal Hocko 	return default_zone_for_pfn(nid, start_pfn, nr_pages);
891e5e68930SMichal Hocko }
892e5e68930SMichal Hocko 
893e5e68930SMichal Hocko /*
894e5e68930SMichal Hocko  * Associates the given pfn range with the given node and the zone appropriate
895e5e68930SMichal Hocko  * for the given online type.
896e5e68930SMichal Hocko  */
897e5e68930SMichal Hocko static struct zone * __meminit move_pfn_range(int online_type, int nid,
898e5e68930SMichal Hocko 		unsigned long start_pfn, unsigned long nr_pages)
899e5e68930SMichal Hocko {
900e5e68930SMichal Hocko 	struct zone *zone;
901e5e68930SMichal Hocko 
902e5e68930SMichal Hocko 	zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
903f1dd2cd1SMichal Hocko 	move_pfn_range_to_zone(zone, start_pfn, nr_pages);
904f1dd2cd1SMichal Hocko 	return zone;
905df429ac0SReza Arbab }
90675884fb1SKAMEZAWA Hiroyuki 
907b93e0f32SMichal Hocko /* Must be protected by mem_hotplug_begin() or a device_lock */
908511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
90975884fb1SKAMEZAWA Hiroyuki {
910aa47228aSCody P Schafer 	unsigned long flags;
9113947be19SDave Hansen 	unsigned long onlined_pages = 0;
9123947be19SDave Hansen 	struct zone *zone;
9136811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
9147b78d335SYasunori Goto 	int nid;
9157b78d335SYasunori Goto 	int ret;
9167b78d335SYasunori Goto 	struct memory_notify arg;
9173947be19SDave Hansen 
918f1dd2cd1SMichal Hocko 	nid = pfn_to_nid(pfn);
919f1dd2cd1SMichal Hocko 	/* associate pfn range with the zone */
920f1dd2cd1SMichal Hocko 	zone = move_pfn_range(online_type, nid, pfn, nr_pages);
921511c2abaSLai Jiangshan 
9227b78d335SYasunori Goto 	arg.start_pfn = pfn;
9237b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
924d9713679SLai Jiangshan 	node_states_check_changes_online(nr_pages, zone, &arg);
9257b78d335SYasunori Goto 
9267b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
9277b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
928e33e33b4SChen Yucong 	if (ret)
929e33e33b4SChen Yucong 		goto failed_addition;
930e33e33b4SChen Yucong 
9313947be19SDave Hansen 	/*
9326811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
9336811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
9346811378eSYasunori Goto 	 * So, zonelist must be updated after online.
9356811378eSYasunori Goto 	 */
9366dcd73d7SWen Congyang 	if (!populated_zone(zone)) {
9376811378eSYasunori Goto 		need_zonelists_rebuild = 1;
93872675e13SMichal Hocko 		setup_zone_pageset(zone);
9396dcd73d7SWen Congyang 	}
9406811378eSYasunori Goto 
941908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
94275884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
943fd8a4221SGeoff Levand 	if (ret) {
9446dcd73d7SWen Congyang 		if (need_zonelists_rebuild)
9456dcd73d7SWen Congyang 			zone_pcp_reset(zone);
946e33e33b4SChen Yucong 		goto failed_addition;
947fd8a4221SGeoff Levand 	}
948fd8a4221SGeoff Levand 
9493947be19SDave Hansen 	zone->present_pages += onlined_pages;
950aa47228aSCody P Schafer 
951aa47228aSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
952f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
953aa47228aSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
954aa47228aSCody P Schafer 
95508dff7b7SJiang Liu 	if (onlined_pages) {
956e888ca35SVlastimil Babka 		node_states_set_node(nid, &arg);
9571f522509SHaicheng Li 		if (need_zonelists_rebuild)
95872675e13SMichal Hocko 			build_all_zonelists(NULL);
9591f522509SHaicheng Li 		else
960112067f0SShaohua Li 			zone_pcp_update(zone);
96108dff7b7SJiang Liu 	}
9621f522509SHaicheng Li 
9631b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
9641b79acc9SKOSAKI Motohiro 
965698b1b30SVlastimil Babka 	if (onlined_pages) {
966e888ca35SVlastimil Babka 		kswapd_run(nid);
967698b1b30SVlastimil Babka 		kcompactd_run(nid);
968698b1b30SVlastimil Babka 	}
96961b13993SDave Hansen 
9705a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
9712f7f24ecSKent Liu 
9722d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
9737b78d335SYasunori Goto 
9747b78d335SYasunori Goto 	if (onlined_pages)
9757b78d335SYasunori Goto 		memory_notify(MEM_ONLINE, &arg);
97630467e0bSDavid Rientjes 	return 0;
977e33e33b4SChen Yucong 
978e33e33b4SChen Yucong failed_addition:
979e33e33b4SChen Yucong 	pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
980e33e33b4SChen Yucong 		 (unsigned long long) pfn << PAGE_SHIFT,
981e33e33b4SChen Yucong 		 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
982e33e33b4SChen Yucong 	memory_notify(MEM_CANCEL_ONLINE, &arg);
983e33e33b4SChen Yucong 	return ret;
9843947be19SDave Hansen }
98553947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
986bc02af93SYasunori Goto 
9870bd85420STang Chen static void reset_node_present_pages(pg_data_t *pgdat)
9880bd85420STang Chen {
9890bd85420STang Chen 	struct zone *z;
9900bd85420STang Chen 
9910bd85420STang Chen 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
9920bd85420STang Chen 		z->present_pages = 0;
9930bd85420STang Chen 
9940bd85420STang Chen 	pgdat->node_present_pages = 0;
9950bd85420STang Chen }
9960bd85420STang Chen 
997e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
998e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9999af3c2deSYasunori Goto {
10009af3c2deSYasunori Goto 	struct pglist_data *pgdat;
10019af3c2deSYasunori Goto 	unsigned long zones_size[MAX_NR_ZONES] = {0};
10029af3c2deSYasunori Goto 	unsigned long zholes_size[MAX_NR_ZONES] = {0};
1003c8e861a5SFabian Frederick 	unsigned long start_pfn = PFN_DOWN(start);
10049af3c2deSYasunori Goto 
1005a1e565aaSTang Chen 	pgdat = NODE_DATA(nid);
1006a1e565aaSTang Chen 	if (!pgdat) {
10079af3c2deSYasunori Goto 		pgdat = arch_alloc_nodedata(nid);
10089af3c2deSYasunori Goto 		if (!pgdat)
10099af3c2deSYasunori Goto 			return NULL;
10109af3c2deSYasunori Goto 
10119af3c2deSYasunori Goto 		arch_refresh_nodedata(nid, pgdat);
1012b0dc3a34SGu Zheng 	} else {
1013e716f2ebSMel Gorman 		/*
1014e716f2ebSMel Gorman 		 * Reset the nr_zones, order and classzone_idx before reuse.
1015e716f2ebSMel Gorman 		 * Note that kswapd will init kswapd_classzone_idx properly
1016e716f2ebSMel Gorman 		 * when it starts in the near future.
1017e716f2ebSMel Gorman 		 */
1018b0dc3a34SGu Zheng 		pgdat->nr_zones = 0;
101938087d9bSMel Gorman 		pgdat->kswapd_order = 0;
102038087d9bSMel Gorman 		pgdat->kswapd_classzone_idx = 0;
1021a1e565aaSTang Chen 	}
10229af3c2deSYasunori Goto 
10239af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
10249af3c2deSYasunori Goto 
10259af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
10269109fb7bSJohannes Weiner 	free_area_init_node(nid, zones_size, start_pfn, zholes_size);
10275830169fSReza Arbab 	pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
10289af3c2deSYasunori Goto 
1029959ecc48SKAMEZAWA Hiroyuki 	/*
1030959ecc48SKAMEZAWA Hiroyuki 	 * The node we allocated has no zone fallback lists. For avoiding
1031959ecc48SKAMEZAWA Hiroyuki 	 * to access not-initialized zonelist, build here.
1032959ecc48SKAMEZAWA Hiroyuki 	 */
103372675e13SMichal Hocko 	build_all_zonelists(pgdat);
1034959ecc48SKAMEZAWA Hiroyuki 
1035f784a3f1STang Chen 	/*
1036f784a3f1STang Chen 	 * zone->managed_pages is set to an approximate value in
1037f784a3f1STang Chen 	 * free_area_init_core(), which will cause
1038f784a3f1STang Chen 	 * /sys/device/system/node/nodeX/meminfo has wrong data.
1039f784a3f1STang Chen 	 * So reset it to 0 before any memory is onlined.
1040f784a3f1STang Chen 	 */
1041f784a3f1STang Chen 	reset_node_managed_pages(pgdat);
1042f784a3f1STang Chen 
10430bd85420STang Chen 	/*
10440bd85420STang Chen 	 * When memory is hot-added, all the memory is in offline state. So
10450bd85420STang Chen 	 * clear all zones' present_pages because they will be updated in
10460bd85420STang Chen 	 * online_pages() and offline_pages().
10470bd85420STang Chen 	 */
10480bd85420STang Chen 	reset_node_present_pages(pgdat);
10490bd85420STang Chen 
10509af3c2deSYasunori Goto 	return pgdat;
10519af3c2deSYasunori Goto }
10529af3c2deSYasunori Goto 
10539af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
10549af3c2deSYasunori Goto {
10559af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
10565830169fSReza Arbab 	free_percpu(pgdat->per_cpu_nodestats);
10579af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
10589af3c2deSYasunori Goto 	return;
10599af3c2deSYasunori Goto }
10609af3c2deSYasunori Goto 
10610a547039SKAMEZAWA Hiroyuki 
106201b0f197SToshi Kani /**
106301b0f197SToshi Kani  * try_online_node - online a node if offlined
106401b0f197SToshi Kani  *
1065cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
1066cf23422bSminskey guo  */
106701b0f197SToshi Kani int try_online_node(int nid)
1068cf23422bSminskey guo {
1069cf23422bSminskey guo 	pg_data_t	*pgdat;
1070cf23422bSminskey guo 	int	ret;
1071cf23422bSminskey guo 
107201b0f197SToshi Kani 	if (node_online(nid))
107301b0f197SToshi Kani 		return 0;
107401b0f197SToshi Kani 
1075bfc8c901SVladimir Davydov 	mem_hotplug_begin();
1076cf23422bSminskey guo 	pgdat = hotadd_new_pgdat(nid, 0);
10777553e8f2SDavid Rientjes 	if (!pgdat) {
107801b0f197SToshi Kani 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1079cf23422bSminskey guo 		ret = -ENOMEM;
1080cf23422bSminskey guo 		goto out;
1081cf23422bSminskey guo 	}
1082cf23422bSminskey guo 	node_set_online(nid);
1083cf23422bSminskey guo 	ret = register_one_node(nid);
1084cf23422bSminskey guo 	BUG_ON(ret);
1085cf23422bSminskey guo out:
1086bfc8c901SVladimir Davydov 	mem_hotplug_done();
1087cf23422bSminskey guo 	return ret;
1088cf23422bSminskey guo }
1089cf23422bSminskey guo 
109027356f54SToshi Kani static int check_hotplug_memory_range(u64 start, u64 size)
109127356f54SToshi Kani {
1092c8e861a5SFabian Frederick 	u64 start_pfn = PFN_DOWN(start);
109327356f54SToshi Kani 	u64 nr_pages = size >> PAGE_SHIFT;
109427356f54SToshi Kani 
109527356f54SToshi Kani 	/* Memory range must be aligned with section */
109627356f54SToshi Kani 	if ((start_pfn & ~PAGE_SECTION_MASK) ||
109727356f54SToshi Kani 	    (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
109827356f54SToshi Kani 		pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
109927356f54SToshi Kani 				(unsigned long long)start,
110027356f54SToshi Kani 				(unsigned long long)size);
110127356f54SToshi Kani 		return -EINVAL;
110227356f54SToshi Kani 	}
110327356f54SToshi Kani 
110427356f54SToshi Kani 	return 0;
110527356f54SToshi Kani }
110627356f54SToshi Kani 
110731bc3858SVitaly Kuznetsov static int online_memory_block(struct memory_block *mem, void *arg)
110831bc3858SVitaly Kuznetsov {
1109dc18d706SNathan Fontenot 	return device_online(&mem->dev);
111031bc3858SVitaly Kuznetsov }
111131bc3858SVitaly Kuznetsov 
111231168481SAl Viro /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
111331bc3858SVitaly Kuznetsov int __ref add_memory_resource(int nid, struct resource *res, bool online)
1114bc02af93SYasunori Goto {
111562cedb9fSDavid Vrabel 	u64 start, size;
11169af3c2deSYasunori Goto 	pg_data_t *pgdat = NULL;
1117a1e565aaSTang Chen 	bool new_pgdat;
1118a1e565aaSTang Chen 	bool new_node;
1119bc02af93SYasunori Goto 	int ret;
1120bc02af93SYasunori Goto 
112162cedb9fSDavid Vrabel 	start = res->start;
112262cedb9fSDavid Vrabel 	size = resource_size(res);
112362cedb9fSDavid Vrabel 
112427356f54SToshi Kani 	ret = check_hotplug_memory_range(start, size);
112527356f54SToshi Kani 	if (ret)
112627356f54SToshi Kani 		return ret;
112727356f54SToshi Kani 
1128a1e565aaSTang Chen 	{	/* Stupid hack to suppress address-never-null warning */
1129a1e565aaSTang Chen 		void *p = NODE_DATA(nid);
1130a1e565aaSTang Chen 		new_pgdat = !p;
1131a1e565aaSTang Chen 	}
1132ac13c462SNathan Zimmer 
1133bfc8c901SVladimir Davydov 	mem_hotplug_begin();
1134ac13c462SNathan Zimmer 
11357f36e3e5STang Chen 	/*
11367f36e3e5STang Chen 	 * Add new range to memblock so that when hotadd_new_pgdat() is called
11377f36e3e5STang Chen 	 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
11387f36e3e5STang Chen 	 * this new range and calculate total pages correctly.  The range will
11397f36e3e5STang Chen 	 * be removed at hot-remove time.
11407f36e3e5STang Chen 	 */
11417f36e3e5STang Chen 	memblock_add_node(start, size, nid);
11427f36e3e5STang Chen 
1143a1e565aaSTang Chen 	new_node = !node_online(nid);
1144a1e565aaSTang Chen 	if (new_node) {
11459af3c2deSYasunori Goto 		pgdat = hotadd_new_pgdat(nid, start);
11466ad696d2SAndi Kleen 		ret = -ENOMEM;
11479af3c2deSYasunori Goto 		if (!pgdat)
114841b9e2d7SWen Congyang 			goto error;
11499af3c2deSYasunori Goto 	}
11509af3c2deSYasunori Goto 
1151bc02af93SYasunori Goto 	/* call arch's memory hotadd */
11523d79a728SMichal Hocko 	ret = arch_add_memory(nid, start, size, true);
1153bc02af93SYasunori Goto 
11549af3c2deSYasunori Goto 	if (ret < 0)
11559af3c2deSYasunori Goto 		goto error;
11569af3c2deSYasunori Goto 
11570fc44159SYasunori Goto 	/* we online node here. we can't roll back from here. */
11589af3c2deSYasunori Goto 	node_set_online(nid);
11599af3c2deSYasunori Goto 
1160a1e565aaSTang Chen 	if (new_node) {
11619037a993SMichal Hocko 		unsigned long start_pfn = start >> PAGE_SHIFT;
11629037a993SMichal Hocko 		unsigned long nr_pages = size >> PAGE_SHIFT;
11639037a993SMichal Hocko 
11649037a993SMichal Hocko 		ret = __register_one_node(nid);
11659037a993SMichal Hocko 		if (ret)
11669037a993SMichal Hocko 			goto register_fail;
11679037a993SMichal Hocko 
11689037a993SMichal Hocko 		/*
11699037a993SMichal Hocko 		 * link memory sections under this node. This is already
11709037a993SMichal Hocko 		 * done when creatig memory section in register_new_memory
11719037a993SMichal Hocko 		 * but that depends to have the node registered so offline
11729037a993SMichal Hocko 		 * nodes have to go through register_node.
11739037a993SMichal Hocko 		 * TODO clean up this mess.
11749037a993SMichal Hocko 		 */
11759037a993SMichal Hocko 		ret = link_mem_sections(nid, start_pfn, nr_pages);
11769037a993SMichal Hocko register_fail:
11770fc44159SYasunori Goto 		/*
11780fc44159SYasunori Goto 		 * If sysfs file of new node can't create, cpu on the node
11790fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
11800fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
11810fc44159SYasunori Goto 		 */
11820fc44159SYasunori Goto 		BUG_ON(ret);
11830fc44159SYasunori Goto 	}
11840fc44159SYasunori Goto 
1185d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
1186d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
1187d96ae530Sakpm@linux-foundation.org 
118831bc3858SVitaly Kuznetsov 	/* online pages if requested */
118931bc3858SVitaly Kuznetsov 	if (online)
119031bc3858SVitaly Kuznetsov 		walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
119131bc3858SVitaly Kuznetsov 				  NULL, online_memory_block);
119231bc3858SVitaly Kuznetsov 
11936ad696d2SAndi Kleen 	goto out;
11946ad696d2SAndi Kleen 
11959af3c2deSYasunori Goto error:
11969af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
1197dbac61a3SGustavo A. R. Silva 	if (new_pgdat && pgdat)
11989af3c2deSYasunori Goto 		rollback_node_hotadd(nid, pgdat);
11997f36e3e5STang Chen 	memblock_remove(start, size);
12009af3c2deSYasunori Goto 
12016ad696d2SAndi Kleen out:
1202bfc8c901SVladimir Davydov 	mem_hotplug_done();
1203bc02af93SYasunori Goto 	return ret;
1204bc02af93SYasunori Goto }
120562cedb9fSDavid Vrabel EXPORT_SYMBOL_GPL(add_memory_resource);
120662cedb9fSDavid Vrabel 
120762cedb9fSDavid Vrabel int __ref add_memory(int nid, u64 start, u64 size)
120862cedb9fSDavid Vrabel {
120962cedb9fSDavid Vrabel 	struct resource *res;
121062cedb9fSDavid Vrabel 	int ret;
121162cedb9fSDavid Vrabel 
121262cedb9fSDavid Vrabel 	res = register_memory_resource(start, size);
12136f754ba4SVitaly Kuznetsov 	if (IS_ERR(res))
12146f754ba4SVitaly Kuznetsov 		return PTR_ERR(res);
121562cedb9fSDavid Vrabel 
121631bc3858SVitaly Kuznetsov 	ret = add_memory_resource(nid, res, memhp_auto_online);
121762cedb9fSDavid Vrabel 	if (ret < 0)
121862cedb9fSDavid Vrabel 		release_memory_resource(res);
121962cedb9fSDavid Vrabel 	return ret;
122062cedb9fSDavid Vrabel }
1221bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
12220c0e6195SKAMEZAWA Hiroyuki 
12230c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
12240c0e6195SKAMEZAWA Hiroyuki /*
12255c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
12265c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
12275c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
12285c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
12295c755e9fSBadari Pulavarty  * be located at the start of the pageblock
12305c755e9fSBadari Pulavarty  */
12315c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
12325c755e9fSBadari Pulavarty {
12335c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
12345c755e9fSBadari Pulavarty }
12355c755e9fSBadari Pulavarty 
12365c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */
12375c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page)
12385c755e9fSBadari Pulavarty {
12395c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
12405c755e9fSBadari Pulavarty 	BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
12415c755e9fSBadari Pulavarty 
12425c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
12430dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
12440dcc48c1SKAMEZAWA Hiroyuki 		int order;
12450dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
12460dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
12470dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
12480dcc48c1SKAMEZAWA Hiroyuki 			return page + (1 << order);
12490dcc48c1SKAMEZAWA Hiroyuki 	}
12505c755e9fSBadari Pulavarty 
12510dcc48c1SKAMEZAWA Hiroyuki 	return page + pageblock_nr_pages;
12525c755e9fSBadari Pulavarty }
12535c755e9fSBadari Pulavarty 
12545c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
1255c98940f6SYaowei Bai bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
12565c755e9fSBadari Pulavarty {
12575c755e9fSBadari Pulavarty 	struct page *page = pfn_to_page(start_pfn);
12585c755e9fSBadari Pulavarty 	struct page *end_page = page + nr_pages;
12595c755e9fSBadari Pulavarty 
12605c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
12615c755e9fSBadari Pulavarty 	for (; page < end_page; page = next_active_pageblock(page)) {
126249ac8255SKAMEZAWA Hiroyuki 		if (!is_pageblock_removable_nolock(page))
1263c98940f6SYaowei Bai 			return false;
126449ac8255SKAMEZAWA Hiroyuki 		cond_resched();
12655c755e9fSBadari Pulavarty 	}
12665c755e9fSBadari Pulavarty 
12675c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
1268c98940f6SYaowei Bai 	return true;
12695c755e9fSBadari Pulavarty }
12705c755e9fSBadari Pulavarty 
12715c755e9fSBadari Pulavarty /*
1272deb88a2aSToshi Kani  * Confirm all pages in a range [start, end) belong to the same zone.
1273a96dfddbSToshi Kani  * When true, return its valid [start, end).
12740c0e6195SKAMEZAWA Hiroyuki  */
1275a96dfddbSToshi Kani int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1276a96dfddbSToshi Kani 			 unsigned long *valid_start, unsigned long *valid_end)
12770c0e6195SKAMEZAWA Hiroyuki {
12785f0f2887SAndrew Banman 	unsigned long pfn, sec_end_pfn;
1279a96dfddbSToshi Kani 	unsigned long start, end;
12800c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
12810c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12820c0e6195SKAMEZAWA Hiroyuki 	int i;
1283deb88a2aSToshi Kani 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
12840c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
1285deb88a2aSToshi Kani 	     pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
12865f0f2887SAndrew Banman 		/* Make sure the memory section is present first */
12875f0f2887SAndrew Banman 		if (!present_section_nr(pfn_to_section_nr(pfn)))
12885f0f2887SAndrew Banman 			continue;
12895f0f2887SAndrew Banman 		for (; pfn < sec_end_pfn && pfn < end_pfn;
12900c0e6195SKAMEZAWA Hiroyuki 		     pfn += MAX_ORDER_NR_PAGES) {
12910c0e6195SKAMEZAWA Hiroyuki 			i = 0;
12920c0e6195SKAMEZAWA Hiroyuki 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
12935f0f2887SAndrew Banman 			while ((i < MAX_ORDER_NR_PAGES) &&
12945f0f2887SAndrew Banman 				!pfn_valid_within(pfn + i))
12950c0e6195SKAMEZAWA Hiroyuki 				i++;
1296d6d8c8a4Szhong jiang 			if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
12970c0e6195SKAMEZAWA Hiroyuki 				continue;
12980c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn + i);
12990c0e6195SKAMEZAWA Hiroyuki 			if (zone && page_zone(page) != zone)
13000c0e6195SKAMEZAWA Hiroyuki 				return 0;
1301a96dfddbSToshi Kani 			if (!zone)
1302a96dfddbSToshi Kani 				start = pfn + i;
13030c0e6195SKAMEZAWA Hiroyuki 			zone = page_zone(page);
1304a96dfddbSToshi Kani 			end = pfn + MAX_ORDER_NR_PAGES;
13050c0e6195SKAMEZAWA Hiroyuki 		}
13065f0f2887SAndrew Banman 	}
1307deb88a2aSToshi Kani 
1308a96dfddbSToshi Kani 	if (zone) {
1309a96dfddbSToshi Kani 		*valid_start = start;
1310d6d8c8a4Szhong jiang 		*valid_end = min(end, end_pfn);
13110c0e6195SKAMEZAWA Hiroyuki 		return 1;
1312a96dfddbSToshi Kani 	} else {
1313deb88a2aSToshi Kani 		return 0;
13140c0e6195SKAMEZAWA Hiroyuki 	}
1315a96dfddbSToshi Kani }
13160c0e6195SKAMEZAWA Hiroyuki 
13170c0e6195SKAMEZAWA Hiroyuki /*
13180efadf48SYisheng Xie  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
13190efadf48SYisheng Xie  * non-lru movable pages and hugepages). We scan pfn because it's much
13200efadf48SYisheng Xie  * easier than scanning over linked list. This function returns the pfn
13210efadf48SYisheng Xie  * of the first found movable page if it's found, otherwise 0.
13220c0e6195SKAMEZAWA Hiroyuki  */
1323c8721bbbSNaoya Horiguchi static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
13240c0e6195SKAMEZAWA Hiroyuki {
13250c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
13260c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
13270c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
13280c0e6195SKAMEZAWA Hiroyuki 		if (pfn_valid(pfn)) {
13290c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn);
13300c0e6195SKAMEZAWA Hiroyuki 			if (PageLRU(page))
13310c0e6195SKAMEZAWA Hiroyuki 				return pfn;
13320efadf48SYisheng Xie 			if (__PageMovable(page))
13330efadf48SYisheng Xie 				return pfn;
1334c8721bbbSNaoya Horiguchi 			if (PageHuge(page)) {
13357e1f049eSNaoya Horiguchi 				if (page_huge_active(page))
1336c8721bbbSNaoya Horiguchi 					return pfn;
1337c8721bbbSNaoya Horiguchi 				else
1338c8721bbbSNaoya Horiguchi 					pfn = round_up(pfn + 1,
1339c8721bbbSNaoya Horiguchi 						1 << compound_order(page)) - 1;
1340c8721bbbSNaoya Horiguchi 			}
13410c0e6195SKAMEZAWA Hiroyuki 		}
13420c0e6195SKAMEZAWA Hiroyuki 	}
13430c0e6195SKAMEZAWA Hiroyuki 	return 0;
13440c0e6195SKAMEZAWA Hiroyuki }
13450c0e6195SKAMEZAWA Hiroyuki 
1346394e31d2SXishi Qiu static struct page *new_node_page(struct page *page, unsigned long private,
1347394e31d2SXishi Qiu 		int **result)
1348394e31d2SXishi Qiu {
1349394e31d2SXishi Qiu 	int nid = page_to_nid(page);
1350231e97e2SLi Zhong 	nodemask_t nmask = node_states[N_MEMORY];
13517f252f27SMichal Hocko 
13527f252f27SMichal Hocko 	/*
13537f252f27SMichal Hocko 	 * try to allocate from a different node but reuse this node if there
13547f252f27SMichal Hocko 	 * are no other online nodes to be used (e.g. we are offlining a part
13557f252f27SMichal Hocko 	 * of the only existing node)
13567f252f27SMichal Hocko 	 */
13577f252f27SMichal Hocko 	node_clear(nid, nmask);
13587f252f27SMichal Hocko 	if (nodes_empty(nmask))
13597f252f27SMichal Hocko 		node_set(nid, nmask);
1360394e31d2SXishi Qiu 
13618b913238SMichal Hocko 	return new_page_nodemask(page, nid, &nmask);
1362394e31d2SXishi Qiu }
1363394e31d2SXishi Qiu 
13640c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES	(256)
13650c0e6195SKAMEZAWA Hiroyuki static int
13660c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
13670c0e6195SKAMEZAWA Hiroyuki {
13680c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
13690c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
13700c0e6195SKAMEZAWA Hiroyuki 	int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
13710c0e6195SKAMEZAWA Hiroyuki 	int not_managed = 0;
13720c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
13730c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
13740c0e6195SKAMEZAWA Hiroyuki 
13750c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
13760c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
13770c0e6195SKAMEZAWA Hiroyuki 			continue;
13780c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
1379c8721bbbSNaoya Horiguchi 
1380c8721bbbSNaoya Horiguchi 		if (PageHuge(page)) {
1381c8721bbbSNaoya Horiguchi 			struct page *head = compound_head(page);
1382c8721bbbSNaoya Horiguchi 			pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1383c8721bbbSNaoya Horiguchi 			if (compound_order(head) > PFN_SECTION_SHIFT) {
1384c8721bbbSNaoya Horiguchi 				ret = -EBUSY;
1385c8721bbbSNaoya Horiguchi 				break;
1386c8721bbbSNaoya Horiguchi 			}
1387c8721bbbSNaoya Horiguchi 			if (isolate_huge_page(page, &source))
1388c8721bbbSNaoya Horiguchi 				move_pages -= 1 << compound_order(head);
1389c8721bbbSNaoya Horiguchi 			continue;
13908135d892SNaoya Horiguchi 		} else if (thp_migration_supported() && PageTransHuge(page))
13918135d892SNaoya Horiguchi 			pfn = page_to_pfn(compound_head(page))
13928135d892SNaoya Horiguchi 				+ hpage_nr_pages(page) - 1;
1393c8721bbbSNaoya Horiguchi 
1394700c2a46SKonstantin Khlebnikov 		if (!get_page_unless_zero(page))
13950c0e6195SKAMEZAWA Hiroyuki 			continue;
13960c0e6195SKAMEZAWA Hiroyuki 		/*
13970efadf48SYisheng Xie 		 * We can skip free pages. And we can deal with pages on
13980efadf48SYisheng Xie 		 * LRU and non-lru movable pages.
13990c0e6195SKAMEZAWA Hiroyuki 		 */
14000efadf48SYisheng Xie 		if (PageLRU(page))
140162695a84SNick Piggin 			ret = isolate_lru_page(page);
14020efadf48SYisheng Xie 		else
14030efadf48SYisheng Xie 			ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
14040c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
1405700c2a46SKonstantin Khlebnikov 			put_page(page);
140662695a84SNick Piggin 			list_add_tail(&page->lru, &source);
14070c0e6195SKAMEZAWA Hiroyuki 			move_pages--;
14080efadf48SYisheng Xie 			if (!__PageMovable(page))
1409599d0c95SMel Gorman 				inc_node_page_state(page, NR_ISOLATED_ANON +
14106d9c285aSKOSAKI Motohiro 						    page_is_file_cache(page));
14116d9c285aSKOSAKI Motohiro 
14120c0e6195SKAMEZAWA Hiroyuki 		} else {
14130c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_DEBUG_VM
14140efadf48SYisheng Xie 			pr_alert("failed to isolate pfn %lx\n", pfn);
14150efadf48SYisheng Xie 			dump_page(page, "isolation failed");
14160c0e6195SKAMEZAWA Hiroyuki #endif
1417700c2a46SKonstantin Khlebnikov 			put_page(page);
141825985edcSLucas De Marchi 			/* Because we don't have big zone->lock. we should
1419809c4449SBob Liu 			   check this again here. */
1420809c4449SBob Liu 			if (page_count(page)) {
1421809c4449SBob Liu 				not_managed++;
1422f3ab2636SBob Liu 				ret = -EBUSY;
1423809c4449SBob Liu 				break;
1424809c4449SBob Liu 			}
14250c0e6195SKAMEZAWA Hiroyuki 		}
14260c0e6195SKAMEZAWA Hiroyuki 	}
1427f3ab2636SBob Liu 	if (!list_empty(&source)) {
14280c0e6195SKAMEZAWA Hiroyuki 		if (not_managed) {
1429c8721bbbSNaoya Horiguchi 			putback_movable_pages(&source);
14300c0e6195SKAMEZAWA Hiroyuki 			goto out;
14310c0e6195SKAMEZAWA Hiroyuki 		}
143274c08f98SMinchan Kim 
1433394e31d2SXishi Qiu 		/* Allocate a new page from the nearest neighbor node */
1434394e31d2SXishi Qiu 		ret = migrate_pages(&source, new_node_page, NULL, 0,
14359c620e2bSHugh Dickins 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1436cf608ac1SMinchan Kim 		if (ret)
1437c8721bbbSNaoya Horiguchi 			putback_movable_pages(&source);
1438f3ab2636SBob Liu 	}
14390c0e6195SKAMEZAWA Hiroyuki out:
14400c0e6195SKAMEZAWA Hiroyuki 	return ret;
14410c0e6195SKAMEZAWA Hiroyuki }
14420c0e6195SKAMEZAWA Hiroyuki 
14430c0e6195SKAMEZAWA Hiroyuki /*
14440c0e6195SKAMEZAWA Hiroyuki  * remove from free_area[] and mark all as Reserved.
14450c0e6195SKAMEZAWA Hiroyuki  */
14460c0e6195SKAMEZAWA Hiroyuki static int
14470c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
14480c0e6195SKAMEZAWA Hiroyuki 			void *data)
14490c0e6195SKAMEZAWA Hiroyuki {
14500c0e6195SKAMEZAWA Hiroyuki 	__offline_isolated_pages(start, start + nr_pages);
14510c0e6195SKAMEZAWA Hiroyuki 	return 0;
14520c0e6195SKAMEZAWA Hiroyuki }
14530c0e6195SKAMEZAWA Hiroyuki 
14540c0e6195SKAMEZAWA Hiroyuki static void
14550c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
14560c0e6195SKAMEZAWA Hiroyuki {
1457908eedc6SKAMEZAWA Hiroyuki 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
14580c0e6195SKAMEZAWA Hiroyuki 				offline_isolated_pages_cb);
14590c0e6195SKAMEZAWA Hiroyuki }
14600c0e6195SKAMEZAWA Hiroyuki 
14610c0e6195SKAMEZAWA Hiroyuki /*
14620c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
14630c0e6195SKAMEZAWA Hiroyuki  */
14640c0e6195SKAMEZAWA Hiroyuki static int
14650c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
14660c0e6195SKAMEZAWA Hiroyuki 			void *data)
14670c0e6195SKAMEZAWA Hiroyuki {
14680c0e6195SKAMEZAWA Hiroyuki 	int ret;
14690c0e6195SKAMEZAWA Hiroyuki 	long offlined = *(long *)data;
1470b023f468SWen Congyang 	ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
14710c0e6195SKAMEZAWA Hiroyuki 	offlined = nr_pages;
14720c0e6195SKAMEZAWA Hiroyuki 	if (!ret)
14730c0e6195SKAMEZAWA Hiroyuki 		*(long *)data += offlined;
14740c0e6195SKAMEZAWA Hiroyuki 	return ret;
14750c0e6195SKAMEZAWA Hiroyuki }
14760c0e6195SKAMEZAWA Hiroyuki 
14770c0e6195SKAMEZAWA Hiroyuki static long
14780c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
14790c0e6195SKAMEZAWA Hiroyuki {
14800c0e6195SKAMEZAWA Hiroyuki 	long offlined = 0;
14810c0e6195SKAMEZAWA Hiroyuki 	int ret;
14820c0e6195SKAMEZAWA Hiroyuki 
1483908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
14840c0e6195SKAMEZAWA Hiroyuki 			check_pages_isolated_cb);
14850c0e6195SKAMEZAWA Hiroyuki 	if (ret < 0)
14860c0e6195SKAMEZAWA Hiroyuki 		offlined = (long)ret;
14870c0e6195SKAMEZAWA Hiroyuki 	return offlined;
14880c0e6195SKAMEZAWA Hiroyuki }
14890c0e6195SKAMEZAWA Hiroyuki 
1490c5320926STang Chen static int __init cmdline_parse_movable_node(char *p)
1491c5320926STang Chen {
14924932381eSMichal Hocko #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
149355ac590cSTang Chen 	movable_node_enabled = true;
14944932381eSMichal Hocko #else
14954932381eSMichal Hocko 	pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
14964932381eSMichal Hocko #endif
1497c5320926STang Chen 	return 0;
1498c5320926STang Chen }
1499c5320926STang Chen early_param("movable_node", cmdline_parse_movable_node);
1500c5320926STang Chen 
1501d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */
1502d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages,
1503d9713679SLai Jiangshan 		struct zone *zone, struct memory_notify *arg)
1504d9713679SLai Jiangshan {
1505d9713679SLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
1506d9713679SLai Jiangshan 	unsigned long present_pages = 0;
1507d9713679SLai Jiangshan 	enum zone_type zt, zone_last = ZONE_NORMAL;
1508d9713679SLai Jiangshan 
1509d9713679SLai Jiangshan 	/*
15106715ddf9SLai Jiangshan 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
15116715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_NORMAL,
15126715ddf9SLai Jiangshan 	 * set zone_last to ZONE_NORMAL.
1513d9713679SLai Jiangshan 	 *
15146715ddf9SLai Jiangshan 	 * If we don't have HIGHMEM nor movable node,
15156715ddf9SLai Jiangshan 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
15166715ddf9SLai Jiangshan 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1517d9713679SLai Jiangshan 	 */
15186715ddf9SLai Jiangshan 	if (N_MEMORY == N_NORMAL_MEMORY)
1519d9713679SLai Jiangshan 		zone_last = ZONE_MOVABLE;
1520d9713679SLai Jiangshan 
1521d9713679SLai Jiangshan 	/*
1522d9713679SLai Jiangshan 	 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1523d9713679SLai Jiangshan 	 * If the memory to be offline is in a zone of 0...zone_last,
1524d9713679SLai Jiangshan 	 * and it is the last present memory, 0...zone_last will
1525d9713679SLai Jiangshan 	 * become empty after offline , thus we can determind we will
1526d9713679SLai Jiangshan 	 * need to clear the node from node_states[N_NORMAL_MEMORY].
1527d9713679SLai Jiangshan 	 */
1528d9713679SLai Jiangshan 	for (zt = 0; zt <= zone_last; zt++)
1529d9713679SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
1530d9713679SLai Jiangshan 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1531d9713679SLai Jiangshan 		arg->status_change_nid_normal = zone_to_nid(zone);
1532d9713679SLai Jiangshan 	else
1533d9713679SLai Jiangshan 		arg->status_change_nid_normal = -1;
1534d9713679SLai Jiangshan 
15356715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
15366715ddf9SLai Jiangshan 	/*
15376715ddf9SLai Jiangshan 	 * If we have movable node, node_states[N_HIGH_MEMORY]
15386715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
15396715ddf9SLai Jiangshan 	 * set zone_last to ZONE_HIGHMEM.
15406715ddf9SLai Jiangshan 	 *
15416715ddf9SLai Jiangshan 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
15426715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
15436715ddf9SLai Jiangshan 	 * set zone_last to ZONE_MOVABLE.
15446715ddf9SLai Jiangshan 	 */
15456715ddf9SLai Jiangshan 	zone_last = ZONE_HIGHMEM;
15466715ddf9SLai Jiangshan 	if (N_MEMORY == N_HIGH_MEMORY)
15476715ddf9SLai Jiangshan 		zone_last = ZONE_MOVABLE;
15486715ddf9SLai Jiangshan 
15496715ddf9SLai Jiangshan 	for (; zt <= zone_last; zt++)
15506715ddf9SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
15516715ddf9SLai Jiangshan 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
15526715ddf9SLai Jiangshan 		arg->status_change_nid_high = zone_to_nid(zone);
15536715ddf9SLai Jiangshan 	else
15546715ddf9SLai Jiangshan 		arg->status_change_nid_high = -1;
15556715ddf9SLai Jiangshan #else
15566715ddf9SLai Jiangshan 	arg->status_change_nid_high = arg->status_change_nid_normal;
15576715ddf9SLai Jiangshan #endif
15586715ddf9SLai Jiangshan 
1559d9713679SLai Jiangshan 	/*
1560d9713679SLai Jiangshan 	 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1561d9713679SLai Jiangshan 	 */
1562d9713679SLai Jiangshan 	zone_last = ZONE_MOVABLE;
1563d9713679SLai Jiangshan 
1564d9713679SLai Jiangshan 	/*
1565d9713679SLai Jiangshan 	 * check whether node_states[N_HIGH_MEMORY] will be changed
1566d9713679SLai Jiangshan 	 * If we try to offline the last present @nr_pages from the node,
1567d9713679SLai Jiangshan 	 * we can determind we will need to clear the node from
1568d9713679SLai Jiangshan 	 * node_states[N_HIGH_MEMORY].
1569d9713679SLai Jiangshan 	 */
1570d9713679SLai Jiangshan 	for (; zt <= zone_last; zt++)
1571d9713679SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
1572d9713679SLai Jiangshan 	if (nr_pages >= present_pages)
1573d9713679SLai Jiangshan 		arg->status_change_nid = zone_to_nid(zone);
1574d9713679SLai Jiangshan 	else
1575d9713679SLai Jiangshan 		arg->status_change_nid = -1;
1576d9713679SLai Jiangshan }
1577d9713679SLai Jiangshan 
1578d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg)
1579d9713679SLai Jiangshan {
1580d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
1581d9713679SLai Jiangshan 		node_clear_state(node, N_NORMAL_MEMORY);
1582d9713679SLai Jiangshan 
15836715ddf9SLai Jiangshan 	if ((N_MEMORY != N_NORMAL_MEMORY) &&
15846715ddf9SLai Jiangshan 	    (arg->status_change_nid_high >= 0))
1585d9713679SLai Jiangshan 		node_clear_state(node, N_HIGH_MEMORY);
15866715ddf9SLai Jiangshan 
15876715ddf9SLai Jiangshan 	if ((N_MEMORY != N_HIGH_MEMORY) &&
15886715ddf9SLai Jiangshan 	    (arg->status_change_nid >= 0))
15896715ddf9SLai Jiangshan 		node_clear_state(node, N_MEMORY);
1590d9713679SLai Jiangshan }
1591d9713679SLai Jiangshan 
1592a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn,
15930c0e6195SKAMEZAWA Hiroyuki 		  unsigned long end_pfn, unsigned long timeout)
15940c0e6195SKAMEZAWA Hiroyuki {
15950c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn, nr_pages, expire;
15960c0e6195SKAMEZAWA Hiroyuki 	long offlined_pages;
15977b78d335SYasunori Goto 	int ret, drain, retry_max, node;
1598d702909fSCody P Schafer 	unsigned long flags;
1599a96dfddbSToshi Kani 	unsigned long valid_start, valid_end;
16000c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
16017b78d335SYasunori Goto 	struct memory_notify arg;
16020c0e6195SKAMEZAWA Hiroyuki 
16030c0e6195SKAMEZAWA Hiroyuki 	/* at least, alignment against pageblock is necessary */
16040c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
16050c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
16060c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
16070c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
16080c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
16090c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
1610a96dfddbSToshi Kani 	if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
16110c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
16127b78d335SYasunori Goto 
1613a96dfddbSToshi Kani 	zone = page_zone(pfn_to_page(valid_start));
16147b78d335SYasunori Goto 	node = zone_to_nid(zone);
16157b78d335SYasunori Goto 	nr_pages = end_pfn - start_pfn;
16167b78d335SYasunori Goto 
16170c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
1618b023f468SWen Congyang 	ret = start_isolate_page_range(start_pfn, end_pfn,
1619b023f468SWen Congyang 				       MIGRATE_MOVABLE, true);
16200c0e6195SKAMEZAWA Hiroyuki 	if (ret)
162130467e0bSDavid Rientjes 		return ret;
16227b78d335SYasunori Goto 
16237b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
16247b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
1625d9713679SLai Jiangshan 	node_states_check_changes_offline(nr_pages, zone, &arg);
16267b78d335SYasunori Goto 
16277b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
16287b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
16297b78d335SYasunori Goto 	if (ret)
16307b78d335SYasunori Goto 		goto failed_removal;
16317b78d335SYasunori Goto 
16320c0e6195SKAMEZAWA Hiroyuki 	pfn = start_pfn;
16330c0e6195SKAMEZAWA Hiroyuki 	expire = jiffies + timeout;
16340c0e6195SKAMEZAWA Hiroyuki 	drain = 0;
16350c0e6195SKAMEZAWA Hiroyuki 	retry_max = 5;
16360c0e6195SKAMEZAWA Hiroyuki repeat:
16370c0e6195SKAMEZAWA Hiroyuki 	/* start memory hot removal */
16380c0e6195SKAMEZAWA Hiroyuki 	ret = -EAGAIN;
16390c0e6195SKAMEZAWA Hiroyuki 	if (time_after(jiffies, expire))
16400c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
16410c0e6195SKAMEZAWA Hiroyuki 	ret = -EINTR;
16420c0e6195SKAMEZAWA Hiroyuki 	if (signal_pending(current))
16430c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
16440c0e6195SKAMEZAWA Hiroyuki 	ret = 0;
16450c0e6195SKAMEZAWA Hiroyuki 	if (drain) {
16463f906ba2SThomas Gleixner 		lru_add_drain_all_cpuslocked();
16470c0e6195SKAMEZAWA Hiroyuki 		cond_resched();
1648c0554329SVlastimil Babka 		drain_all_pages(zone);
16490c0e6195SKAMEZAWA Hiroyuki 	}
16500c0e6195SKAMEZAWA Hiroyuki 
1651c8721bbbSNaoya Horiguchi 	pfn = scan_movable_pages(start_pfn, end_pfn);
1652c8721bbbSNaoya Horiguchi 	if (pfn) { /* We have movable pages */
16530c0e6195SKAMEZAWA Hiroyuki 		ret = do_migrate_range(pfn, end_pfn);
16540c0e6195SKAMEZAWA Hiroyuki 		if (!ret) {
16550c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
16560c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
16570c0e6195SKAMEZAWA Hiroyuki 		} else {
16580c0e6195SKAMEZAWA Hiroyuki 			if (ret < 0)
16590c0e6195SKAMEZAWA Hiroyuki 				if (--retry_max == 0)
16600c0e6195SKAMEZAWA Hiroyuki 					goto failed_removal;
16610c0e6195SKAMEZAWA Hiroyuki 			yield();
16620c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
16630c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
16640c0e6195SKAMEZAWA Hiroyuki 		}
16650c0e6195SKAMEZAWA Hiroyuki 	}
1666b3834be5SAdam Buchbinder 	/* drain all zone's lru pagevec, this is asynchronous... */
16673f906ba2SThomas Gleixner 	lru_add_drain_all_cpuslocked();
16680c0e6195SKAMEZAWA Hiroyuki 	yield();
1669b3834be5SAdam Buchbinder 	/* drain pcp pages, this is synchronous. */
1670c0554329SVlastimil Babka 	drain_all_pages(zone);
1671c8721bbbSNaoya Horiguchi 	/*
1672c8721bbbSNaoya Horiguchi 	 * dissolve free hugepages in the memory block before doing offlining
1673c8721bbbSNaoya Horiguchi 	 * actually in order to make hugetlbfs's object counting consistent.
1674c8721bbbSNaoya Horiguchi 	 */
1675082d5b6bSGerald Schaefer 	ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1676082d5b6bSGerald Schaefer 	if (ret)
1677082d5b6bSGerald Schaefer 		goto failed_removal;
16780c0e6195SKAMEZAWA Hiroyuki 	/* check again */
16790c0e6195SKAMEZAWA Hiroyuki 	offlined_pages = check_pages_isolated(start_pfn, end_pfn);
16800c0e6195SKAMEZAWA Hiroyuki 	if (offlined_pages < 0) {
16810c0e6195SKAMEZAWA Hiroyuki 		ret = -EBUSY;
16820c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
16830c0e6195SKAMEZAWA Hiroyuki 	}
1684e33e33b4SChen Yucong 	pr_info("Offlined Pages %ld\n", offlined_pages);
1685b3834be5SAdam Buchbinder 	/* Ok, all of our target is isolated.
16860c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
16870c0e6195SKAMEZAWA Hiroyuki 	offline_isolated_pages(start_pfn, end_pfn);
1688dbc0e4ceSKAMEZAWA Hiroyuki 	/* reset pagetype flags and makes migrate type to be MOVABLE */
16890815f3d8SMichal Nazarewicz 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
16900c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
16913dcc0571SJiang Liu 	adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
16920c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
1693d702909fSCody P Schafer 
1694d702909fSCody P Schafer 	pgdat_resize_lock(zone->zone_pgdat, &flags);
16950c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
1696d702909fSCody P Schafer 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
16977b78d335SYasunori Goto 
16981b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
16991b79acc9SKOSAKI Motohiro 
17001e8537baSXishi Qiu 	if (!populated_zone(zone)) {
1701340175b7SJiang Liu 		zone_pcp_reset(zone);
170272675e13SMichal Hocko 		build_all_zonelists(NULL);
17031e8537baSXishi Qiu 	} else
17041e8537baSXishi Qiu 		zone_pcp_update(zone);
1705340175b7SJiang Liu 
1706d9713679SLai Jiangshan 	node_states_clear_node(node, &arg);
1707698b1b30SVlastimil Babka 	if (arg.status_change_nid >= 0) {
17088fe23e05SDavid Rientjes 		kswapd_stop(node);
1709698b1b30SVlastimil Babka 		kcompactd_stop(node);
1710698b1b30SVlastimil Babka 	}
1711bce7394aSMinchan Kim 
17120c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
17130c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
17147b78d335SYasunori Goto 
17157b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
17160c0e6195SKAMEZAWA Hiroyuki 	return 0;
17170c0e6195SKAMEZAWA Hiroyuki 
17180c0e6195SKAMEZAWA Hiroyuki failed_removal:
1719e33e33b4SChen Yucong 	pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1720a62e2f4fSBjorn Helgaas 		 (unsigned long long) start_pfn << PAGE_SHIFT,
1721a62e2f4fSBjorn Helgaas 		 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
17227b78d335SYasunori Goto 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
17230c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
17240815f3d8SMichal Nazarewicz 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
17250c0e6195SKAMEZAWA Hiroyuki 	return ret;
17260c0e6195SKAMEZAWA Hiroyuki }
172771088785SBadari Pulavarty 
1728b93e0f32SMichal Hocko /* Must be protected by mem_hotplug_begin() or a device_lock */
1729a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1730a16cee10SWen Congyang {
1731a16cee10SWen Congyang 	return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1732a16cee10SWen Congyang }
1733e2ff3940SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1734a16cee10SWen Congyang 
1735bbc76be6SWen Congyang /**
1736bbc76be6SWen Congyang  * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1737bbc76be6SWen Congyang  * @start_pfn: start pfn of the memory range
1738e05c4bbfSToshi Kani  * @end_pfn: end pfn of the memory range
1739bbc76be6SWen Congyang  * @arg: argument passed to func
1740bbc76be6SWen Congyang  * @func: callback for each memory section walked
1741bbc76be6SWen Congyang  *
1742bbc76be6SWen Congyang  * This function walks through all present mem sections in range
1743bbc76be6SWen Congyang  * [start_pfn, end_pfn) and call func on each mem section.
1744bbc76be6SWen Congyang  *
1745bbc76be6SWen Congyang  * Returns the return value of func.
1746bbc76be6SWen Congyang  */
1747e2ff3940SRafael J. Wysocki int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1748bbc76be6SWen Congyang 		void *arg, int (*func)(struct memory_block *, void *))
174971088785SBadari Pulavarty {
1750e90bdb7fSWen Congyang 	struct memory_block *mem = NULL;
1751e90bdb7fSWen Congyang 	struct mem_section *section;
1752e90bdb7fSWen Congyang 	unsigned long pfn, section_nr;
1753e90bdb7fSWen Congyang 	int ret;
175471088785SBadari Pulavarty 
1755e90bdb7fSWen Congyang 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1756e90bdb7fSWen Congyang 		section_nr = pfn_to_section_nr(pfn);
1757e90bdb7fSWen Congyang 		if (!present_section_nr(section_nr))
1758e90bdb7fSWen Congyang 			continue;
1759e90bdb7fSWen Congyang 
1760e90bdb7fSWen Congyang 		section = __nr_to_section(section_nr);
1761e90bdb7fSWen Congyang 		/* same memblock? */
1762e90bdb7fSWen Congyang 		if (mem)
1763e90bdb7fSWen Congyang 			if ((section_nr >= mem->start_section_nr) &&
1764e90bdb7fSWen Congyang 			    (section_nr <= mem->end_section_nr))
1765e90bdb7fSWen Congyang 				continue;
1766e90bdb7fSWen Congyang 
1767e90bdb7fSWen Congyang 		mem = find_memory_block_hinted(section, mem);
1768e90bdb7fSWen Congyang 		if (!mem)
1769e90bdb7fSWen Congyang 			continue;
1770e90bdb7fSWen Congyang 
1771bbc76be6SWen Congyang 		ret = func(mem, arg);
1772e90bdb7fSWen Congyang 		if (ret) {
1773e90bdb7fSWen Congyang 			kobject_put(&mem->dev.kobj);
1774e90bdb7fSWen Congyang 			return ret;
1775e90bdb7fSWen Congyang 		}
1776e90bdb7fSWen Congyang 	}
1777e90bdb7fSWen Congyang 
1778e90bdb7fSWen Congyang 	if (mem)
1779e90bdb7fSWen Congyang 		kobject_put(&mem->dev.kobj);
1780e90bdb7fSWen Congyang 
1781bbc76be6SWen Congyang 	return 0;
1782bbc76be6SWen Congyang }
1783bbc76be6SWen Congyang 
1784e2ff3940SRafael J. Wysocki #ifdef CONFIG_MEMORY_HOTREMOVE
1785d6de9d53SXishi Qiu static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1786bbc76be6SWen Congyang {
1787bbc76be6SWen Congyang 	int ret = !is_memblock_offlined(mem);
1788bbc76be6SWen Congyang 
1789349daa0fSRandy Dunlap 	if (unlikely(ret)) {
1790349daa0fSRandy Dunlap 		phys_addr_t beginpa, endpa;
1791349daa0fSRandy Dunlap 
1792349daa0fSRandy Dunlap 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1793349daa0fSRandy Dunlap 		endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1794756a025fSJoe Perches 		pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1795349daa0fSRandy Dunlap 			&beginpa, &endpa);
1796349daa0fSRandy Dunlap 	}
1797bbc76be6SWen Congyang 
1798bbc76be6SWen Congyang 	return ret;
1799bbc76be6SWen Congyang }
1800bbc76be6SWen Congyang 
18010f1cfe9dSToshi Kani static int check_cpu_on_node(pg_data_t *pgdat)
180260a5a19eSTang Chen {
180360a5a19eSTang Chen 	int cpu;
180460a5a19eSTang Chen 
180560a5a19eSTang Chen 	for_each_present_cpu(cpu) {
180660a5a19eSTang Chen 		if (cpu_to_node(cpu) == pgdat->node_id)
180760a5a19eSTang Chen 			/*
180860a5a19eSTang Chen 			 * the cpu on this node isn't removed, and we can't
180960a5a19eSTang Chen 			 * offline this node.
181060a5a19eSTang Chen 			 */
181160a5a19eSTang Chen 			return -EBUSY;
181260a5a19eSTang Chen 	}
181360a5a19eSTang Chen 
181460a5a19eSTang Chen 	return 0;
181560a5a19eSTang Chen }
181660a5a19eSTang Chen 
18170f1cfe9dSToshi Kani static void unmap_cpu_on_node(pg_data_t *pgdat)
1818e13fe869SWen Congyang {
1819e13fe869SWen Congyang #ifdef CONFIG_ACPI_NUMA
1820e13fe869SWen Congyang 	int cpu;
1821e13fe869SWen Congyang 
1822e13fe869SWen Congyang 	for_each_possible_cpu(cpu)
1823e13fe869SWen Congyang 		if (cpu_to_node(cpu) == pgdat->node_id)
1824e13fe869SWen Congyang 			numa_clear_node(cpu);
1825e13fe869SWen Congyang #endif
1826e13fe869SWen Congyang }
1827e13fe869SWen Congyang 
18280f1cfe9dSToshi Kani static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1829e13fe869SWen Congyang {
18300f1cfe9dSToshi Kani 	int ret;
1831e13fe869SWen Congyang 
18320f1cfe9dSToshi Kani 	ret = check_cpu_on_node(pgdat);
1833e13fe869SWen Congyang 	if (ret)
1834e13fe869SWen Congyang 		return ret;
1835e13fe869SWen Congyang 
1836e13fe869SWen Congyang 	/*
1837e13fe869SWen Congyang 	 * the node will be offlined when we come here, so we can clear
1838e13fe869SWen Congyang 	 * the cpu_to_node() now.
1839e13fe869SWen Congyang 	 */
1840e13fe869SWen Congyang 
18410f1cfe9dSToshi Kani 	unmap_cpu_on_node(pgdat);
1842e13fe869SWen Congyang 	return 0;
1843e13fe869SWen Congyang }
1844e13fe869SWen Congyang 
18450f1cfe9dSToshi Kani /**
18460f1cfe9dSToshi Kani  * try_offline_node
18470f1cfe9dSToshi Kani  *
18480f1cfe9dSToshi Kani  * Offline a node if all memory sections and cpus of the node are removed.
18490f1cfe9dSToshi Kani  *
18500f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
18510f1cfe9dSToshi Kani  * and online/offline operations before this call.
18520f1cfe9dSToshi Kani  */
185390b30cdcSWen Congyang void try_offline_node(int nid)
185460a5a19eSTang Chen {
1855d822b86aSWen Congyang 	pg_data_t *pgdat = NODE_DATA(nid);
1856d822b86aSWen Congyang 	unsigned long start_pfn = pgdat->node_start_pfn;
1857d822b86aSWen Congyang 	unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
185860a5a19eSTang Chen 	unsigned long pfn;
185960a5a19eSTang Chen 
186060a5a19eSTang Chen 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
186160a5a19eSTang Chen 		unsigned long section_nr = pfn_to_section_nr(pfn);
186260a5a19eSTang Chen 
186360a5a19eSTang Chen 		if (!present_section_nr(section_nr))
186460a5a19eSTang Chen 			continue;
186560a5a19eSTang Chen 
186660a5a19eSTang Chen 		if (pfn_to_nid(pfn) != nid)
186760a5a19eSTang Chen 			continue;
186860a5a19eSTang Chen 
186960a5a19eSTang Chen 		/*
187060a5a19eSTang Chen 		 * some memory sections of this node are not removed, and we
187160a5a19eSTang Chen 		 * can't offline node now.
187260a5a19eSTang Chen 		 */
187360a5a19eSTang Chen 		return;
187460a5a19eSTang Chen 	}
187560a5a19eSTang Chen 
18760f1cfe9dSToshi Kani 	if (check_and_unmap_cpu_on_node(pgdat))
187760a5a19eSTang Chen 		return;
187860a5a19eSTang Chen 
187960a5a19eSTang Chen 	/*
188060a5a19eSTang Chen 	 * all memory/cpu of this node are removed, we can offline this
188160a5a19eSTang Chen 	 * node now.
188260a5a19eSTang Chen 	 */
188360a5a19eSTang Chen 	node_set_offline(nid);
188460a5a19eSTang Chen 	unregister_one_node(nid);
188560a5a19eSTang Chen }
188690b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node);
188760a5a19eSTang Chen 
18880f1cfe9dSToshi Kani /**
18890f1cfe9dSToshi Kani  * remove_memory
18900f1cfe9dSToshi Kani  *
18910f1cfe9dSToshi Kani  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
18920f1cfe9dSToshi Kani  * and online/offline operations before this call, as required by
18930f1cfe9dSToshi Kani  * try_offline_node().
18940f1cfe9dSToshi Kani  */
1895242831ebSRafael J. Wysocki void __ref remove_memory(int nid, u64 start, u64 size)
1896bbc76be6SWen Congyang {
1897242831ebSRafael J. Wysocki 	int ret;
1898993c1aadSWen Congyang 
189927356f54SToshi Kani 	BUG_ON(check_hotplug_memory_range(start, size));
190027356f54SToshi Kani 
1901bfc8c901SVladimir Davydov 	mem_hotplug_begin();
19026677e3eaSYasuaki Ishimatsu 
19036677e3eaSYasuaki Ishimatsu 	/*
1904242831ebSRafael J. Wysocki 	 * All memory blocks must be offlined before removing memory.  Check
1905242831ebSRafael J. Wysocki 	 * whether all memory blocks in question are offline and trigger a BUG()
1906242831ebSRafael J. Wysocki 	 * if this is not the case.
19076677e3eaSYasuaki Ishimatsu 	 */
1908242831ebSRafael J. Wysocki 	ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1909d6de9d53SXishi Qiu 				check_memblock_offlined_cb);
1910bfc8c901SVladimir Davydov 	if (ret)
1911242831ebSRafael J. Wysocki 		BUG();
19126677e3eaSYasuaki Ishimatsu 
191346c66c4bSYasuaki Ishimatsu 	/* remove memmap entry */
191446c66c4bSYasuaki Ishimatsu 	firmware_map_remove(start, start + size, "System RAM");
1915f9126ab9SXishi Qiu 	memblock_free(start, size);
1916f9126ab9SXishi Qiu 	memblock_remove(start, size);
191746c66c4bSYasuaki Ishimatsu 
191824d335caSWen Congyang 	arch_remove_memory(start, size);
191924d335caSWen Congyang 
192060a5a19eSTang Chen 	try_offline_node(nid);
192160a5a19eSTang Chen 
1922bfc8c901SVladimir Davydov 	mem_hotplug_done();
192371088785SBadari Pulavarty }
192471088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
1925aba6efc4SRafael J. Wysocki #endif /* CONFIG_MEMORY_HOTREMOVE */
1926