xref: /openbmc/linux/mm/memory_hotplug.c (revision 4edd7ceff0662afde195da6f6c43e7cbe1ed2dc4)
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>
93947be19SDave Hansen #include <linux/swap.h>
103947be19SDave Hansen #include <linux/interrupt.h>
113947be19SDave Hansen #include <linux/pagemap.h>
123947be19SDave Hansen #include <linux/bootmem.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>
213947be19SDave Hansen #include <linux/memory_hotplug.h>
223947be19SDave Hansen #include <linux/highmem.h>
233947be19SDave Hansen #include <linux/vmalloc.h>
240a547039SKAMEZAWA Hiroyuki #include <linux/ioport.h>
250c0e6195SKAMEZAWA Hiroyuki #include <linux/delay.h>
260c0e6195SKAMEZAWA Hiroyuki #include <linux/migrate.h>
270c0e6195SKAMEZAWA Hiroyuki #include <linux/page-isolation.h>
2871088785SBadari Pulavarty #include <linux/pfn.h>
296ad696d2SAndi Kleen #include <linux/suspend.h>
306d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h>
31d96ae530Sakpm@linux-foundation.org #include <linux/firmware-map.h>
3260a5a19eSTang Chen #include <linux/stop_machine.h>
333947be19SDave Hansen 
343947be19SDave Hansen #include <asm/tlbflush.h>
353947be19SDave Hansen 
361e5ad9a3SAdrian Bunk #include "internal.h"
371e5ad9a3SAdrian Bunk 
389d0ad8caSDaniel Kiper /*
399d0ad8caSDaniel Kiper  * online_page_callback contains pointer to current page onlining function.
409d0ad8caSDaniel Kiper  * Initially it is generic_online_page(). If it is required it could be
419d0ad8caSDaniel Kiper  * changed by calling set_online_page_callback() for callback registration
429d0ad8caSDaniel Kiper  * and restore_online_page_callback() for generic callback restore.
439d0ad8caSDaniel Kiper  */
449d0ad8caSDaniel Kiper 
459d0ad8caSDaniel Kiper static void generic_online_page(struct page *page);
469d0ad8caSDaniel Kiper 
479d0ad8caSDaniel Kiper static online_page_callback_t online_page_callback = generic_online_page;
489d0ad8caSDaniel Kiper 
4920d6c96bSKOSAKI Motohiro DEFINE_MUTEX(mem_hotplug_mutex);
5020d6c96bSKOSAKI Motohiro 
5120d6c96bSKOSAKI Motohiro void lock_memory_hotplug(void)
5220d6c96bSKOSAKI Motohiro {
5320d6c96bSKOSAKI Motohiro 	mutex_lock(&mem_hotplug_mutex);
5420d6c96bSKOSAKI Motohiro 
5520d6c96bSKOSAKI Motohiro 	/* for exclusive hibernation if CONFIG_HIBERNATION=y */
5620d6c96bSKOSAKI Motohiro 	lock_system_sleep();
5720d6c96bSKOSAKI Motohiro }
5820d6c96bSKOSAKI Motohiro 
5920d6c96bSKOSAKI Motohiro void unlock_memory_hotplug(void)
6020d6c96bSKOSAKI Motohiro {
6120d6c96bSKOSAKI Motohiro 	unlock_system_sleep();
6220d6c96bSKOSAKI Motohiro 	mutex_unlock(&mem_hotplug_mutex);
6320d6c96bSKOSAKI Motohiro }
6420d6c96bSKOSAKI Motohiro 
6520d6c96bSKOSAKI Motohiro 
6645e0b78bSKeith Mannthey /* add this memory to iomem resource */
6745e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
6845e0b78bSKeith Mannthey {
6945e0b78bSKeith Mannthey 	struct resource *res;
7045e0b78bSKeith Mannthey 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
7145e0b78bSKeith Mannthey 	BUG_ON(!res);
7245e0b78bSKeith Mannthey 
7345e0b78bSKeith Mannthey 	res->name = "System RAM";
7445e0b78bSKeith Mannthey 	res->start = start;
7545e0b78bSKeith Mannthey 	res->end = start + size - 1;
76887c3cb1SYasunori Goto 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
7745e0b78bSKeith Mannthey 	if (request_resource(&iomem_resource, res) < 0) {
78a62e2f4fSBjorn Helgaas 		printk("System RAM resource %pR cannot be added\n", res);
7945e0b78bSKeith Mannthey 		kfree(res);
8045e0b78bSKeith Mannthey 		res = NULL;
8145e0b78bSKeith Mannthey 	}
8245e0b78bSKeith Mannthey 	return res;
8345e0b78bSKeith Mannthey }
8445e0b78bSKeith Mannthey 
8545e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
8645e0b78bSKeith Mannthey {
8745e0b78bSKeith Mannthey 	if (!res)
8845e0b78bSKeith Mannthey 		return;
8945e0b78bSKeith Mannthey 	release_resource(res);
9045e0b78bSKeith Mannthey 	kfree(res);
9145e0b78bSKeith Mannthey 	return;
9245e0b78bSKeith Mannthey }
9345e0b78bSKeith Mannthey 
9453947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
9546723bfaSYasuaki Ishimatsu void get_page_bootmem(unsigned long info,  struct page *page,
965f24ce5fSAndrea Arcangeli 		      unsigned long type)
9704753278SYasunori Goto {
985f24ce5fSAndrea Arcangeli 	page->lru.next = (struct list_head *) type;
9904753278SYasunori Goto 	SetPagePrivate(page);
10004753278SYasunori Goto 	set_page_private(page, info);
10104753278SYasunori Goto 	atomic_inc(&page->_count);
10204753278SYasunori Goto }
10304753278SYasunori Goto 
10423ce932aSRakib Mullick /* reference to __meminit __free_pages_bootmem is valid
10523ce932aSRakib Mullick  * so use __ref to tell modpost not to generate a warning */
10623ce932aSRakib Mullick void __ref put_page_bootmem(struct page *page)
10704753278SYasunori Goto {
1085f24ce5fSAndrea Arcangeli 	unsigned long type;
1099feedc9dSJiang Liu 	static DEFINE_MUTEX(ppb_lock);
11004753278SYasunori Goto 
1115f24ce5fSAndrea Arcangeli 	type = (unsigned long) page->lru.next;
1125f24ce5fSAndrea Arcangeli 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
1135f24ce5fSAndrea Arcangeli 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
11404753278SYasunori Goto 
11504753278SYasunori Goto 	if (atomic_dec_return(&page->_count) == 1) {
11604753278SYasunori Goto 		ClearPagePrivate(page);
11704753278SYasunori Goto 		set_page_private(page, 0);
1185f24ce5fSAndrea Arcangeli 		INIT_LIST_HEAD(&page->lru);
1199feedc9dSJiang Liu 
1209feedc9dSJiang Liu 		/*
1219feedc9dSJiang Liu 		 * Please refer to comment for __free_pages_bootmem()
1229feedc9dSJiang Liu 		 * for why we serialize here.
1239feedc9dSJiang Liu 		 */
1249feedc9dSJiang Liu 		mutex_lock(&ppb_lock);
12504753278SYasunori Goto 		__free_pages_bootmem(page, 0);
1269feedc9dSJiang Liu 		mutex_unlock(&ppb_lock);
127c60514b6SJiang Liu 		totalram_pages++;
12804753278SYasunori Goto 	}
12904753278SYasunori Goto 
13004753278SYasunori Goto }
13104753278SYasunori Goto 
13246723bfaSYasuaki Ishimatsu #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
13346723bfaSYasuaki Ishimatsu #ifndef CONFIG_SPARSEMEM_VMEMMAP
134d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
13504753278SYasunori Goto {
13604753278SYasunori Goto 	unsigned long *usemap, mapsize, section_nr, i;
13704753278SYasunori Goto 	struct mem_section *ms;
13804753278SYasunori Goto 	struct page *page, *memmap;
13904753278SYasunori Goto 
14004753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
14104753278SYasunori Goto 	ms = __nr_to_section(section_nr);
14204753278SYasunori Goto 
14304753278SYasunori Goto 	/* Get section's memmap address */
14404753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
14504753278SYasunori Goto 
14604753278SYasunori Goto 	/*
14704753278SYasunori Goto 	 * Get page for the memmap's phys address
14804753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
14904753278SYasunori Goto 	 */
15004753278SYasunori Goto 	page = virt_to_page(memmap);
15104753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
15204753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
15304753278SYasunori Goto 
15404753278SYasunori Goto 	/* remember memmap's page */
15504753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
15604753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
15704753278SYasunori Goto 
15804753278SYasunori Goto 	usemap = __nr_to_section(section_nr)->pageblock_flags;
15904753278SYasunori Goto 	page = virt_to_page(usemap);
16004753278SYasunori Goto 
16104753278SYasunori Goto 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
16204753278SYasunori Goto 
16304753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
164af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
16504753278SYasunori Goto 
16604753278SYasunori Goto }
16746723bfaSYasuaki Ishimatsu #else /* CONFIG_SPARSEMEM_VMEMMAP */
16846723bfaSYasuaki Ishimatsu static void register_page_bootmem_info_section(unsigned long start_pfn)
16946723bfaSYasuaki Ishimatsu {
17046723bfaSYasuaki Ishimatsu 	unsigned long *usemap, mapsize, section_nr, i;
17146723bfaSYasuaki Ishimatsu 	struct mem_section *ms;
17246723bfaSYasuaki Ishimatsu 	struct page *page, *memmap;
17346723bfaSYasuaki Ishimatsu 
17446723bfaSYasuaki Ishimatsu 	if (!pfn_valid(start_pfn))
17546723bfaSYasuaki Ishimatsu 		return;
17646723bfaSYasuaki Ishimatsu 
17746723bfaSYasuaki Ishimatsu 	section_nr = pfn_to_section_nr(start_pfn);
17846723bfaSYasuaki Ishimatsu 	ms = __nr_to_section(section_nr);
17946723bfaSYasuaki Ishimatsu 
18046723bfaSYasuaki Ishimatsu 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
18146723bfaSYasuaki Ishimatsu 
18246723bfaSYasuaki Ishimatsu 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
18346723bfaSYasuaki Ishimatsu 
18446723bfaSYasuaki Ishimatsu 	usemap = __nr_to_section(section_nr)->pageblock_flags;
18546723bfaSYasuaki Ishimatsu 	page = virt_to_page(usemap);
18646723bfaSYasuaki Ishimatsu 
18746723bfaSYasuaki Ishimatsu 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
18846723bfaSYasuaki Ishimatsu 
18946723bfaSYasuaki Ishimatsu 	for (i = 0; i < mapsize; i++, page++)
19046723bfaSYasuaki Ishimatsu 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
19146723bfaSYasuaki Ishimatsu }
19246723bfaSYasuaki Ishimatsu #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
19304753278SYasunori Goto 
19404753278SYasunori Goto void register_page_bootmem_info_node(struct pglist_data *pgdat)
19504753278SYasunori Goto {
19604753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
19704753278SYasunori Goto 	int node = pgdat->node_id;
19804753278SYasunori Goto 	struct page *page;
19904753278SYasunori Goto 	struct zone *zone;
20004753278SYasunori Goto 
20104753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
20204753278SYasunori Goto 	page = virt_to_page(pgdat);
20304753278SYasunori Goto 
20404753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
20504753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
20604753278SYasunori Goto 
20704753278SYasunori Goto 	zone = &pgdat->node_zones[0];
20804753278SYasunori Goto 	for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
20904753278SYasunori Goto 		if (zone->wait_table) {
21004753278SYasunori Goto 			nr_pages = zone->wait_table_hash_nr_entries
21104753278SYasunori Goto 				* sizeof(wait_queue_head_t);
21204753278SYasunori Goto 			nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
21304753278SYasunori Goto 			page = virt_to_page(zone->wait_table);
21404753278SYasunori Goto 
21504753278SYasunori Goto 			for (i = 0; i < nr_pages; i++, page++)
21604753278SYasunori Goto 				get_page_bootmem(node, page, NODE_INFO);
21704753278SYasunori Goto 		}
21804753278SYasunori Goto 	}
21904753278SYasunori Goto 
22004753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
221c1f19495SCody P Schafer 	end_pfn = pgdat_end_pfn(pgdat);
22204753278SYasunori Goto 
22304753278SYasunori Goto 	/* register_section info */
224f14851afSqiuxishi 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
225f14851afSqiuxishi 		/*
226f14851afSqiuxishi 		 * Some platforms can assign the same pfn to multiple nodes - on
227f14851afSqiuxishi 		 * node0 as well as nodeN.  To avoid registering a pfn against
228f14851afSqiuxishi 		 * multiple nodes we check that this pfn does not already
229f14851afSqiuxishi 		 * reside in some other node.
230f14851afSqiuxishi 		 */
231f14851afSqiuxishi 		if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
23204753278SYasunori Goto 			register_page_bootmem_info_section(pfn);
233f14851afSqiuxishi 	}
23404753278SYasunori Goto }
23546723bfaSYasuaki Ishimatsu #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
23604753278SYasunori Goto 
23776cdd58eSHeiko Carstens static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
23876cdd58eSHeiko Carstens 			   unsigned long end_pfn)
23976cdd58eSHeiko Carstens {
24076cdd58eSHeiko Carstens 	unsigned long old_zone_end_pfn;
24176cdd58eSHeiko Carstens 
24276cdd58eSHeiko Carstens 	zone_span_writelock(zone);
24376cdd58eSHeiko Carstens 
24476cdd58eSHeiko Carstens 	old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
245712cd386STang Chen 	if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
24676cdd58eSHeiko Carstens 		zone->zone_start_pfn = start_pfn;
24776cdd58eSHeiko Carstens 
24876cdd58eSHeiko Carstens 	zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
24976cdd58eSHeiko Carstens 				zone->zone_start_pfn;
25076cdd58eSHeiko Carstens 
25176cdd58eSHeiko Carstens 	zone_span_writeunlock(zone);
25276cdd58eSHeiko Carstens }
25376cdd58eSHeiko Carstens 
254511c2abaSLai Jiangshan static void resize_zone(struct zone *zone, unsigned long start_pfn,
255511c2abaSLai Jiangshan 		unsigned long end_pfn)
256511c2abaSLai Jiangshan {
257511c2abaSLai Jiangshan 	zone_span_writelock(zone);
258511c2abaSLai Jiangshan 
259e455a9b9SLai Jiangshan 	if (end_pfn - start_pfn) {
260511c2abaSLai Jiangshan 		zone->zone_start_pfn = start_pfn;
261511c2abaSLai Jiangshan 		zone->spanned_pages = end_pfn - start_pfn;
262e455a9b9SLai Jiangshan 	} else {
263e455a9b9SLai Jiangshan 		/*
264e455a9b9SLai Jiangshan 		 * make it consist as free_area_init_core(),
265e455a9b9SLai Jiangshan 		 * if spanned_pages = 0, then keep start_pfn = 0
266e455a9b9SLai Jiangshan 		 */
267e455a9b9SLai Jiangshan 		zone->zone_start_pfn = 0;
268e455a9b9SLai Jiangshan 		zone->spanned_pages = 0;
269e455a9b9SLai Jiangshan 	}
270511c2abaSLai Jiangshan 
271511c2abaSLai Jiangshan 	zone_span_writeunlock(zone);
272511c2abaSLai Jiangshan }
273511c2abaSLai Jiangshan 
274511c2abaSLai Jiangshan static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
275511c2abaSLai Jiangshan 		unsigned long end_pfn)
276511c2abaSLai Jiangshan {
277511c2abaSLai Jiangshan 	enum zone_type zid = zone_idx(zone);
278511c2abaSLai Jiangshan 	int nid = zone->zone_pgdat->node_id;
279511c2abaSLai Jiangshan 	unsigned long pfn;
280511c2abaSLai Jiangshan 
281511c2abaSLai Jiangshan 	for (pfn = start_pfn; pfn < end_pfn; pfn++)
282511c2abaSLai Jiangshan 		set_page_links(pfn_to_page(pfn), zid, nid, pfn);
283511c2abaSLai Jiangshan }
284511c2abaSLai Jiangshan 
285f6bbb78eSCody P Schafer /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
286f6bbb78eSCody P Schafer  * alloc_bootmem_node_nopanic() */
287f6bbb78eSCody P Schafer static int __ref ensure_zone_is_initialized(struct zone *zone,
288f6bbb78eSCody P Schafer 			unsigned long start_pfn, unsigned long num_pages)
289f6bbb78eSCody P Schafer {
290f6bbb78eSCody P Schafer 	if (!zone_is_initialized(zone))
291f6bbb78eSCody P Schafer 		return init_currently_empty_zone(zone, start_pfn, num_pages,
292f6bbb78eSCody P Schafer 						 MEMMAP_HOTPLUG);
293f6bbb78eSCody P Schafer 	return 0;
294f6bbb78eSCody P Schafer }
295f6bbb78eSCody P Schafer 
296e455a9b9SLai Jiangshan static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
297511c2abaSLai Jiangshan 		unsigned long start_pfn, unsigned long end_pfn)
298511c2abaSLai Jiangshan {
299e455a9b9SLai Jiangshan 	int ret;
300511c2abaSLai Jiangshan 	unsigned long flags;
301e455a9b9SLai Jiangshan 	unsigned long z1_start_pfn;
302e455a9b9SLai Jiangshan 
30364dd1b29SCody P Schafer 	ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
304e455a9b9SLai Jiangshan 	if (ret)
305e455a9b9SLai Jiangshan 		return ret;
306511c2abaSLai Jiangshan 
307511c2abaSLai Jiangshan 	pgdat_resize_lock(z1->zone_pgdat, &flags);
308511c2abaSLai Jiangshan 
309511c2abaSLai Jiangshan 	/* can't move pfns which are higher than @z2 */
310108bcc96SCody P Schafer 	if (end_pfn > zone_end_pfn(z2))
311511c2abaSLai Jiangshan 		goto out_fail;
312511c2abaSLai Jiangshan 	/* the move out part mast at the left most of @z2 */
313511c2abaSLai Jiangshan 	if (start_pfn > z2->zone_start_pfn)
314511c2abaSLai Jiangshan 		goto out_fail;
315511c2abaSLai Jiangshan 	/* must included/overlap */
316511c2abaSLai Jiangshan 	if (end_pfn <= z2->zone_start_pfn)
317511c2abaSLai Jiangshan 		goto out_fail;
318511c2abaSLai Jiangshan 
319e455a9b9SLai Jiangshan 	/* use start_pfn for z1's start_pfn if z1 is empty */
320e455a9b9SLai Jiangshan 	if (z1->spanned_pages)
321e455a9b9SLai Jiangshan 		z1_start_pfn = z1->zone_start_pfn;
322e455a9b9SLai Jiangshan 	else
323e455a9b9SLai Jiangshan 		z1_start_pfn = start_pfn;
324e455a9b9SLai Jiangshan 
325e455a9b9SLai Jiangshan 	resize_zone(z1, z1_start_pfn, end_pfn);
326108bcc96SCody P Schafer 	resize_zone(z2, end_pfn, zone_end_pfn(z2));
327511c2abaSLai Jiangshan 
328511c2abaSLai Jiangshan 	pgdat_resize_unlock(z1->zone_pgdat, &flags);
329511c2abaSLai Jiangshan 
330511c2abaSLai Jiangshan 	fix_zone_id(z1, start_pfn, end_pfn);
331511c2abaSLai Jiangshan 
332511c2abaSLai Jiangshan 	return 0;
333511c2abaSLai Jiangshan out_fail:
334511c2abaSLai Jiangshan 	pgdat_resize_unlock(z1->zone_pgdat, &flags);
335511c2abaSLai Jiangshan 	return -1;
336511c2abaSLai Jiangshan }
337511c2abaSLai Jiangshan 
338e455a9b9SLai Jiangshan static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
339511c2abaSLai Jiangshan 		unsigned long start_pfn, unsigned long end_pfn)
340511c2abaSLai Jiangshan {
341e455a9b9SLai Jiangshan 	int ret;
342511c2abaSLai Jiangshan 	unsigned long flags;
343e455a9b9SLai Jiangshan 	unsigned long z2_end_pfn;
344e455a9b9SLai Jiangshan 
34564dd1b29SCody P Schafer 	ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
346e455a9b9SLai Jiangshan 	if (ret)
347e455a9b9SLai Jiangshan 		return ret;
348511c2abaSLai Jiangshan 
349511c2abaSLai Jiangshan 	pgdat_resize_lock(z1->zone_pgdat, &flags);
350511c2abaSLai Jiangshan 
351511c2abaSLai Jiangshan 	/* can't move pfns which are lower than @z1 */
352511c2abaSLai Jiangshan 	if (z1->zone_start_pfn > start_pfn)
353511c2abaSLai Jiangshan 		goto out_fail;
354511c2abaSLai Jiangshan 	/* the move out part mast at the right most of @z1 */
355108bcc96SCody P Schafer 	if (zone_end_pfn(z1) >  end_pfn)
356511c2abaSLai Jiangshan 		goto out_fail;
357511c2abaSLai Jiangshan 	/* must included/overlap */
358108bcc96SCody P Schafer 	if (start_pfn >= zone_end_pfn(z1))
359511c2abaSLai Jiangshan 		goto out_fail;
360511c2abaSLai Jiangshan 
361e455a9b9SLai Jiangshan 	/* use end_pfn for z2's end_pfn if z2 is empty */
362e455a9b9SLai Jiangshan 	if (z2->spanned_pages)
363108bcc96SCody P Schafer 		z2_end_pfn = zone_end_pfn(z2);
364e455a9b9SLai Jiangshan 	else
365e455a9b9SLai Jiangshan 		z2_end_pfn = end_pfn;
366e455a9b9SLai Jiangshan 
367511c2abaSLai Jiangshan 	resize_zone(z1, z1->zone_start_pfn, start_pfn);
368e455a9b9SLai Jiangshan 	resize_zone(z2, start_pfn, z2_end_pfn);
369511c2abaSLai Jiangshan 
370511c2abaSLai Jiangshan 	pgdat_resize_unlock(z1->zone_pgdat, &flags);
371511c2abaSLai Jiangshan 
372511c2abaSLai Jiangshan 	fix_zone_id(z2, start_pfn, end_pfn);
373511c2abaSLai Jiangshan 
374511c2abaSLai Jiangshan 	return 0;
375511c2abaSLai Jiangshan out_fail:
376511c2abaSLai Jiangshan 	pgdat_resize_unlock(z1->zone_pgdat, &flags);
377511c2abaSLai Jiangshan 	return -1;
378511c2abaSLai Jiangshan }
379511c2abaSLai Jiangshan 
38076cdd58eSHeiko Carstens static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
38176cdd58eSHeiko Carstens 			    unsigned long end_pfn)
38276cdd58eSHeiko Carstens {
38376cdd58eSHeiko Carstens 	unsigned long old_pgdat_end_pfn =
38476cdd58eSHeiko Carstens 		pgdat->node_start_pfn + pgdat->node_spanned_pages;
38576cdd58eSHeiko Carstens 
386712cd386STang Chen 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
38776cdd58eSHeiko Carstens 		pgdat->node_start_pfn = start_pfn;
38876cdd58eSHeiko Carstens 
38976cdd58eSHeiko Carstens 	pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
39076cdd58eSHeiko Carstens 					pgdat->node_start_pfn;
39176cdd58eSHeiko Carstens }
39276cdd58eSHeiko Carstens 
39331168481SAl Viro static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
3943947be19SDave Hansen {
3953947be19SDave Hansen 	struct pglist_data *pgdat = zone->zone_pgdat;
3963947be19SDave Hansen 	int nr_pages = PAGES_PER_SECTION;
3973947be19SDave Hansen 	int nid = pgdat->node_id;
3983947be19SDave Hansen 	int zone_type;
39976cdd58eSHeiko Carstens 	unsigned long flags;
40076cdd58eSHeiko Carstens 	int ret;
40176cdd58eSHeiko Carstens 
40264dd1b29SCody P Schafer 	zone_type = zone - pgdat->node_zones;
40364dd1b29SCody P Schafer 	ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
40476cdd58eSHeiko Carstens 	if (ret)
40576cdd58eSHeiko Carstens 		return ret;
40664dd1b29SCody P Schafer 
40776cdd58eSHeiko Carstens 	pgdat_resize_lock(zone->zone_pgdat, &flags);
40876cdd58eSHeiko Carstens 	grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
40976cdd58eSHeiko Carstens 	grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
41076cdd58eSHeiko Carstens 			phys_start_pfn + nr_pages);
41176cdd58eSHeiko Carstens 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
412a2f3aa02SDave Hansen 	memmap_init_zone(nr_pages, nid, zone_type,
413a2f3aa02SDave Hansen 			 phys_start_pfn, MEMMAP_HOTPLUG);
414718127ccSYasunori Goto 	return 0;
4153947be19SDave Hansen }
4163947be19SDave Hansen 
417c04fc586SGary Hade static int __meminit __add_section(int nid, struct zone *zone,
418c04fc586SGary Hade 					unsigned long phys_start_pfn)
4193947be19SDave Hansen {
4203947be19SDave Hansen 	int nr_pages = PAGES_PER_SECTION;
4213947be19SDave Hansen 	int ret;
4223947be19SDave Hansen 
423ebd15302SKAMEZAWA Hiroyuki 	if (pfn_valid(phys_start_pfn))
424ebd15302SKAMEZAWA Hiroyuki 		return -EEXIST;
425ebd15302SKAMEZAWA Hiroyuki 
4260b0acbecSDave Hansen 	ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
4273947be19SDave Hansen 
4283947be19SDave Hansen 	if (ret < 0)
4293947be19SDave Hansen 		return ret;
4303947be19SDave Hansen 
431718127ccSYasunori Goto 	ret = __add_zone(zone, phys_start_pfn);
432718127ccSYasunori Goto 
433718127ccSYasunori Goto 	if (ret < 0)
434718127ccSYasunori Goto 		return ret;
435718127ccSYasunori Goto 
436c04fc586SGary Hade 	return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
4373947be19SDave Hansen }
4383947be19SDave Hansen 
439*4edd7cefSDavid Rientjes /*
440*4edd7cefSDavid Rientjes  * Reasonably generic function for adding memory.  It is
441*4edd7cefSDavid Rientjes  * expected that archs that support memory hotplug will
442*4edd7cefSDavid Rientjes  * call this function after deciding the zone to which to
443*4edd7cefSDavid Rientjes  * add the new pages.
444*4edd7cefSDavid Rientjes  */
445*4edd7cefSDavid Rientjes int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
446*4edd7cefSDavid Rientjes 			unsigned long nr_pages)
447*4edd7cefSDavid Rientjes {
448*4edd7cefSDavid Rientjes 	unsigned long i;
449*4edd7cefSDavid Rientjes 	int err = 0;
450*4edd7cefSDavid Rientjes 	int start_sec, end_sec;
451*4edd7cefSDavid Rientjes 	/* during initialize mem_map, align hot-added range to section */
452*4edd7cefSDavid Rientjes 	start_sec = pfn_to_section_nr(phys_start_pfn);
453*4edd7cefSDavid Rientjes 	end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
454*4edd7cefSDavid Rientjes 
455*4edd7cefSDavid Rientjes 	for (i = start_sec; i <= end_sec; i++) {
456*4edd7cefSDavid Rientjes 		err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
457*4edd7cefSDavid Rientjes 
458*4edd7cefSDavid Rientjes 		/*
459*4edd7cefSDavid Rientjes 		 * EEXIST is finally dealt with by ioresource collision
460*4edd7cefSDavid Rientjes 		 * check. see add_memory() => register_memory_resource()
461*4edd7cefSDavid Rientjes 		 * Warning will be printed if there is collision.
462*4edd7cefSDavid Rientjes 		 */
463*4edd7cefSDavid Rientjes 		if (err && (err != -EEXIST))
464*4edd7cefSDavid Rientjes 			break;
465*4edd7cefSDavid Rientjes 		err = 0;
466*4edd7cefSDavid Rientjes 	}
467*4edd7cefSDavid Rientjes 
468*4edd7cefSDavid Rientjes 	return err;
469*4edd7cefSDavid Rientjes }
470*4edd7cefSDavid Rientjes EXPORT_SYMBOL_GPL(__add_pages);
471*4edd7cefSDavid Rientjes 
472*4edd7cefSDavid Rientjes #ifdef CONFIG_MEMORY_HOTREMOVE
473815121d2SYasuaki Ishimatsu /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
474815121d2SYasuaki Ishimatsu static int find_smallest_section_pfn(int nid, struct zone *zone,
475815121d2SYasuaki Ishimatsu 				     unsigned long start_pfn,
476815121d2SYasuaki Ishimatsu 				     unsigned long end_pfn)
477815121d2SYasuaki Ishimatsu {
478815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
479815121d2SYasuaki Ishimatsu 
480815121d2SYasuaki Ishimatsu 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
481815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(start_pfn);
482815121d2SYasuaki Ishimatsu 
483815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
484815121d2SYasuaki Ishimatsu 			continue;
485815121d2SYasuaki Ishimatsu 
486815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(start_pfn) != nid))
487815121d2SYasuaki Ishimatsu 			continue;
488815121d2SYasuaki Ishimatsu 
489815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(start_pfn)))
490815121d2SYasuaki Ishimatsu 			continue;
491815121d2SYasuaki Ishimatsu 
492815121d2SYasuaki Ishimatsu 		return start_pfn;
493815121d2SYasuaki Ishimatsu 	}
494815121d2SYasuaki Ishimatsu 
495815121d2SYasuaki Ishimatsu 	return 0;
496815121d2SYasuaki Ishimatsu }
497815121d2SYasuaki Ishimatsu 
498815121d2SYasuaki Ishimatsu /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
499815121d2SYasuaki Ishimatsu static int find_biggest_section_pfn(int nid, struct zone *zone,
500815121d2SYasuaki Ishimatsu 				    unsigned long start_pfn,
501815121d2SYasuaki Ishimatsu 				    unsigned long end_pfn)
502815121d2SYasuaki Ishimatsu {
503815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
504815121d2SYasuaki Ishimatsu 	unsigned long pfn;
505815121d2SYasuaki Ishimatsu 
506815121d2SYasuaki Ishimatsu 	/* pfn is the end pfn of a memory section. */
507815121d2SYasuaki Ishimatsu 	pfn = end_pfn - 1;
508815121d2SYasuaki Ishimatsu 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
509815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
510815121d2SYasuaki Ishimatsu 
511815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
512815121d2SYasuaki Ishimatsu 			continue;
513815121d2SYasuaki Ishimatsu 
514815121d2SYasuaki Ishimatsu 		if (unlikely(pfn_to_nid(pfn) != nid))
515815121d2SYasuaki Ishimatsu 			continue;
516815121d2SYasuaki Ishimatsu 
517815121d2SYasuaki Ishimatsu 		if (zone && zone != page_zone(pfn_to_page(pfn)))
518815121d2SYasuaki Ishimatsu 			continue;
519815121d2SYasuaki Ishimatsu 
520815121d2SYasuaki Ishimatsu 		return pfn;
521815121d2SYasuaki Ishimatsu 	}
522815121d2SYasuaki Ishimatsu 
523815121d2SYasuaki Ishimatsu 	return 0;
524815121d2SYasuaki Ishimatsu }
525815121d2SYasuaki Ishimatsu 
526815121d2SYasuaki Ishimatsu static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
527815121d2SYasuaki Ishimatsu 			     unsigned long end_pfn)
528815121d2SYasuaki Ishimatsu {
529815121d2SYasuaki Ishimatsu 	unsigned long zone_start_pfn =  zone->zone_start_pfn;
530815121d2SYasuaki Ishimatsu 	unsigned long zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
531815121d2SYasuaki Ishimatsu 	unsigned long pfn;
532815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
533815121d2SYasuaki Ishimatsu 	int nid = zone_to_nid(zone);
534815121d2SYasuaki Ishimatsu 
535815121d2SYasuaki Ishimatsu 	zone_span_writelock(zone);
536815121d2SYasuaki Ishimatsu 	if (zone_start_pfn == start_pfn) {
537815121d2SYasuaki Ishimatsu 		/*
538815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the zone, it need
539815121d2SYasuaki Ishimatsu 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
540815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
541815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
542815121d2SYasuaki Ishimatsu 		 */
543815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
544815121d2SYasuaki Ishimatsu 						zone_end_pfn);
545815121d2SYasuaki Ishimatsu 		if (pfn) {
546815121d2SYasuaki Ishimatsu 			zone->zone_start_pfn = pfn;
547815121d2SYasuaki Ishimatsu 			zone->spanned_pages = zone_end_pfn - pfn;
548815121d2SYasuaki Ishimatsu 		}
549815121d2SYasuaki Ishimatsu 	} else if (zone_end_pfn == end_pfn) {
550815121d2SYasuaki Ishimatsu 		/*
551815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the zone, it need
552815121d2SYasuaki Ishimatsu 		 * shrink zone->spanned_pages.
553815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
554815121d2SYasuaki Ishimatsu 		 * shrinking zone.
555815121d2SYasuaki Ishimatsu 		 */
556815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
557815121d2SYasuaki Ishimatsu 					       start_pfn);
558815121d2SYasuaki Ishimatsu 		if (pfn)
559815121d2SYasuaki Ishimatsu 			zone->spanned_pages = pfn - zone_start_pfn + 1;
560815121d2SYasuaki Ishimatsu 	}
561815121d2SYasuaki Ishimatsu 
562815121d2SYasuaki Ishimatsu 	/*
563815121d2SYasuaki Ishimatsu 	 * The section is not biggest or smallest mem_section in the zone, it
564815121d2SYasuaki Ishimatsu 	 * only creates a hole in the zone. So in this case, we need not
565815121d2SYasuaki Ishimatsu 	 * change the zone. But perhaps, the zone has only hole data. Thus
566815121d2SYasuaki Ishimatsu 	 * it check the zone has only hole or not.
567815121d2SYasuaki Ishimatsu 	 */
568815121d2SYasuaki Ishimatsu 	pfn = zone_start_pfn;
569815121d2SYasuaki Ishimatsu 	for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
570815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
571815121d2SYasuaki Ishimatsu 
572815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
573815121d2SYasuaki Ishimatsu 			continue;
574815121d2SYasuaki Ishimatsu 
575815121d2SYasuaki Ishimatsu 		if (page_zone(pfn_to_page(pfn)) != zone)
576815121d2SYasuaki Ishimatsu 			continue;
577815121d2SYasuaki Ishimatsu 
578815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
579815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
580815121d2SYasuaki Ishimatsu 			continue;
581815121d2SYasuaki Ishimatsu 
582815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
583815121d2SYasuaki Ishimatsu 		zone_span_writeunlock(zone);
584815121d2SYasuaki Ishimatsu 		return;
585815121d2SYasuaki Ishimatsu 	}
586815121d2SYasuaki Ishimatsu 
587815121d2SYasuaki Ishimatsu 	/* The zone has no valid section */
588815121d2SYasuaki Ishimatsu 	zone->zone_start_pfn = 0;
589815121d2SYasuaki Ishimatsu 	zone->spanned_pages = 0;
590815121d2SYasuaki Ishimatsu 	zone_span_writeunlock(zone);
591815121d2SYasuaki Ishimatsu }
592815121d2SYasuaki Ishimatsu 
593815121d2SYasuaki Ishimatsu static void shrink_pgdat_span(struct pglist_data *pgdat,
594815121d2SYasuaki Ishimatsu 			      unsigned long start_pfn, unsigned long end_pfn)
595815121d2SYasuaki Ishimatsu {
596815121d2SYasuaki Ishimatsu 	unsigned long pgdat_start_pfn =  pgdat->node_start_pfn;
597815121d2SYasuaki Ishimatsu 	unsigned long pgdat_end_pfn =
598815121d2SYasuaki Ishimatsu 		pgdat->node_start_pfn + pgdat->node_spanned_pages;
599815121d2SYasuaki Ishimatsu 	unsigned long pfn;
600815121d2SYasuaki Ishimatsu 	struct mem_section *ms;
601815121d2SYasuaki Ishimatsu 	int nid = pgdat->node_id;
602815121d2SYasuaki Ishimatsu 
603815121d2SYasuaki Ishimatsu 	if (pgdat_start_pfn == start_pfn) {
604815121d2SYasuaki Ishimatsu 		/*
605815121d2SYasuaki Ishimatsu 		 * If the section is smallest section in the pgdat, it need
606815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
607815121d2SYasuaki Ishimatsu 		 * In this case, we find second smallest valid mem_section
608815121d2SYasuaki Ishimatsu 		 * for shrinking zone.
609815121d2SYasuaki Ishimatsu 		 */
610815121d2SYasuaki Ishimatsu 		pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
611815121d2SYasuaki Ishimatsu 						pgdat_end_pfn);
612815121d2SYasuaki Ishimatsu 		if (pfn) {
613815121d2SYasuaki Ishimatsu 			pgdat->node_start_pfn = pfn;
614815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
615815121d2SYasuaki Ishimatsu 		}
616815121d2SYasuaki Ishimatsu 	} else if (pgdat_end_pfn == end_pfn) {
617815121d2SYasuaki Ishimatsu 		/*
618815121d2SYasuaki Ishimatsu 		 * If the section is biggest section in the pgdat, it need
619815121d2SYasuaki Ishimatsu 		 * shrink pgdat->node_spanned_pages.
620815121d2SYasuaki Ishimatsu 		 * In this case, we find second biggest valid mem_section for
621815121d2SYasuaki Ishimatsu 		 * shrinking zone.
622815121d2SYasuaki Ishimatsu 		 */
623815121d2SYasuaki Ishimatsu 		pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
624815121d2SYasuaki Ishimatsu 					       start_pfn);
625815121d2SYasuaki Ishimatsu 		if (pfn)
626815121d2SYasuaki Ishimatsu 			pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
627815121d2SYasuaki Ishimatsu 	}
628815121d2SYasuaki Ishimatsu 
629815121d2SYasuaki Ishimatsu 	/*
630815121d2SYasuaki Ishimatsu 	 * If the section is not biggest or smallest mem_section in the pgdat,
631815121d2SYasuaki Ishimatsu 	 * it only creates a hole in the pgdat. So in this case, we need not
632815121d2SYasuaki Ishimatsu 	 * change the pgdat.
633815121d2SYasuaki Ishimatsu 	 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
634815121d2SYasuaki Ishimatsu 	 * has only hole or not.
635815121d2SYasuaki Ishimatsu 	 */
636815121d2SYasuaki Ishimatsu 	pfn = pgdat_start_pfn;
637815121d2SYasuaki Ishimatsu 	for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
638815121d2SYasuaki Ishimatsu 		ms = __pfn_to_section(pfn);
639815121d2SYasuaki Ishimatsu 
640815121d2SYasuaki Ishimatsu 		if (unlikely(!valid_section(ms)))
641815121d2SYasuaki Ishimatsu 			continue;
642815121d2SYasuaki Ishimatsu 
643815121d2SYasuaki Ishimatsu 		if (pfn_to_nid(pfn) != nid)
644815121d2SYasuaki Ishimatsu 			continue;
645815121d2SYasuaki Ishimatsu 
646815121d2SYasuaki Ishimatsu 		 /* If the section is current section, it continues the loop */
647815121d2SYasuaki Ishimatsu 		if (start_pfn == pfn)
648815121d2SYasuaki Ishimatsu 			continue;
649815121d2SYasuaki Ishimatsu 
650815121d2SYasuaki Ishimatsu 		/* If we find valid section, we have nothing to do */
651815121d2SYasuaki Ishimatsu 		return;
652815121d2SYasuaki Ishimatsu 	}
653815121d2SYasuaki Ishimatsu 
654815121d2SYasuaki Ishimatsu 	/* The pgdat has no valid section */
655815121d2SYasuaki Ishimatsu 	pgdat->node_start_pfn = 0;
656815121d2SYasuaki Ishimatsu 	pgdat->node_spanned_pages = 0;
657815121d2SYasuaki Ishimatsu }
658815121d2SYasuaki Ishimatsu 
659815121d2SYasuaki Ishimatsu static void __remove_zone(struct zone *zone, unsigned long start_pfn)
660815121d2SYasuaki Ishimatsu {
661815121d2SYasuaki Ishimatsu 	struct pglist_data *pgdat = zone->zone_pgdat;
662815121d2SYasuaki Ishimatsu 	int nr_pages = PAGES_PER_SECTION;
663815121d2SYasuaki Ishimatsu 	int zone_type;
664815121d2SYasuaki Ishimatsu 	unsigned long flags;
665815121d2SYasuaki Ishimatsu 
666815121d2SYasuaki Ishimatsu 	zone_type = zone - pgdat->node_zones;
667815121d2SYasuaki Ishimatsu 
668815121d2SYasuaki Ishimatsu 	pgdat_resize_lock(zone->zone_pgdat, &flags);
669815121d2SYasuaki Ishimatsu 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
670815121d2SYasuaki Ishimatsu 	shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
671815121d2SYasuaki Ishimatsu 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
672815121d2SYasuaki Ishimatsu }
673815121d2SYasuaki Ishimatsu 
674ea01ea93SBadari Pulavarty static int __remove_section(struct zone *zone, struct mem_section *ms)
675ea01ea93SBadari Pulavarty {
676815121d2SYasuaki Ishimatsu 	unsigned long start_pfn;
677815121d2SYasuaki Ishimatsu 	int scn_nr;
678ea01ea93SBadari Pulavarty 	int ret = -EINVAL;
679ea01ea93SBadari Pulavarty 
680ea01ea93SBadari Pulavarty 	if (!valid_section(ms))
681ea01ea93SBadari Pulavarty 		return ret;
682ea01ea93SBadari Pulavarty 
683ea01ea93SBadari Pulavarty 	ret = unregister_memory_section(ms);
684ea01ea93SBadari Pulavarty 	if (ret)
685ea01ea93SBadari Pulavarty 		return ret;
686ea01ea93SBadari Pulavarty 
687815121d2SYasuaki Ishimatsu 	scn_nr = __section_nr(ms);
688815121d2SYasuaki Ishimatsu 	start_pfn = section_nr_to_pfn(scn_nr);
689815121d2SYasuaki Ishimatsu 	__remove_zone(zone, start_pfn);
690815121d2SYasuaki Ishimatsu 
691ea01ea93SBadari Pulavarty 	sparse_remove_one_section(zone, ms);
692ea01ea93SBadari Pulavarty 	return 0;
693ea01ea93SBadari Pulavarty }
694ea01ea93SBadari Pulavarty 
695ea01ea93SBadari Pulavarty /**
696ea01ea93SBadari Pulavarty  * __remove_pages() - remove sections of pages from a zone
697ea01ea93SBadari Pulavarty  * @zone: zone from which pages need to be removed
698ea01ea93SBadari Pulavarty  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
699ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
700ea01ea93SBadari Pulavarty  *
701ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
702ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
703ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
704ea01ea93SBadari Pulavarty  * calling offline_pages().
705ea01ea93SBadari Pulavarty  */
706ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
707ea01ea93SBadari Pulavarty 		 unsigned long nr_pages)
708ea01ea93SBadari Pulavarty {
709fe74ebb1SToshi Kani 	unsigned long i;
710ea01ea93SBadari Pulavarty 	int sections_to_remove;
711fe74ebb1SToshi Kani 	resource_size_t start, size;
712fe74ebb1SToshi Kani 	int ret = 0;
713ea01ea93SBadari Pulavarty 
714ea01ea93SBadari Pulavarty 	/*
715ea01ea93SBadari Pulavarty 	 * We can only remove entire sections
716ea01ea93SBadari Pulavarty 	 */
717ea01ea93SBadari Pulavarty 	BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
718ea01ea93SBadari Pulavarty 	BUG_ON(nr_pages % PAGES_PER_SECTION);
719ea01ea93SBadari Pulavarty 
720fe74ebb1SToshi Kani 	start = phys_start_pfn << PAGE_SHIFT;
721fe74ebb1SToshi Kani 	size = nr_pages * PAGE_SIZE;
722fe74ebb1SToshi Kani 	ret = release_mem_region_adjustable(&iomem_resource, start, size);
723fe74ebb1SToshi Kani 	if (ret)
724fe74ebb1SToshi Kani 		pr_warn("Unable to release resource <%016llx-%016llx> (%d)\n",
725fe74ebb1SToshi Kani 				start, start + size - 1, ret);
726d760afd4SYasuaki Ishimatsu 
727ea01ea93SBadari Pulavarty 	sections_to_remove = nr_pages / PAGES_PER_SECTION;
728ea01ea93SBadari Pulavarty 	for (i = 0; i < sections_to_remove; i++) {
729ea01ea93SBadari Pulavarty 		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
730ea01ea93SBadari Pulavarty 		ret = __remove_section(zone, __pfn_to_section(pfn));
731ea01ea93SBadari Pulavarty 		if (ret)
732ea01ea93SBadari Pulavarty 			break;
733ea01ea93SBadari Pulavarty 	}
734ea01ea93SBadari Pulavarty 	return ret;
735ea01ea93SBadari Pulavarty }
736ea01ea93SBadari Pulavarty EXPORT_SYMBOL_GPL(__remove_pages);
737*4edd7cefSDavid Rientjes #endif /* CONFIG_MEMORY_HOTREMOVE */
738ea01ea93SBadari Pulavarty 
7399d0ad8caSDaniel Kiper int set_online_page_callback(online_page_callback_t callback)
7409d0ad8caSDaniel Kiper {
7419d0ad8caSDaniel Kiper 	int rc = -EINVAL;
7429d0ad8caSDaniel Kiper 
7439d0ad8caSDaniel Kiper 	lock_memory_hotplug();
7449d0ad8caSDaniel Kiper 
7459d0ad8caSDaniel Kiper 	if (online_page_callback == generic_online_page) {
7469d0ad8caSDaniel Kiper 		online_page_callback = callback;
7479d0ad8caSDaniel Kiper 		rc = 0;
7489d0ad8caSDaniel Kiper 	}
7499d0ad8caSDaniel Kiper 
7509d0ad8caSDaniel Kiper 	unlock_memory_hotplug();
7519d0ad8caSDaniel Kiper 
7529d0ad8caSDaniel Kiper 	return rc;
7539d0ad8caSDaniel Kiper }
7549d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(set_online_page_callback);
7559d0ad8caSDaniel Kiper 
7569d0ad8caSDaniel Kiper int restore_online_page_callback(online_page_callback_t callback)
7579d0ad8caSDaniel Kiper {
7589d0ad8caSDaniel Kiper 	int rc = -EINVAL;
7599d0ad8caSDaniel Kiper 
7609d0ad8caSDaniel Kiper 	lock_memory_hotplug();
7619d0ad8caSDaniel Kiper 
7629d0ad8caSDaniel Kiper 	if (online_page_callback == callback) {
7639d0ad8caSDaniel Kiper 		online_page_callback = generic_online_page;
7649d0ad8caSDaniel Kiper 		rc = 0;
7659d0ad8caSDaniel Kiper 	}
7669d0ad8caSDaniel Kiper 
7679d0ad8caSDaniel Kiper 	unlock_memory_hotplug();
7689d0ad8caSDaniel Kiper 
7699d0ad8caSDaniel Kiper 	return rc;
7709d0ad8caSDaniel Kiper }
7719d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(restore_online_page_callback);
7729d0ad8caSDaniel Kiper 
7739d0ad8caSDaniel Kiper void __online_page_set_limits(struct page *page)
774180c06efSJeremy Fitzhardinge {
7754738e1b9SJan Beulich 	unsigned long pfn = page_to_pfn(page);
7764738e1b9SJan Beulich 
7774738e1b9SJan Beulich 	if (pfn >= num_physpages)
7784738e1b9SJan Beulich 		num_physpages = pfn + 1;
7799d0ad8caSDaniel Kiper }
7809d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_set_limits);
7819d0ad8caSDaniel Kiper 
7829d0ad8caSDaniel Kiper void __online_page_increment_counters(struct page *page)
7839d0ad8caSDaniel Kiper {
7849d0ad8caSDaniel Kiper 	totalram_pages++;
785180c06efSJeremy Fitzhardinge 
786180c06efSJeremy Fitzhardinge #ifdef CONFIG_HIGHMEM
787180c06efSJeremy Fitzhardinge 	if (PageHighMem(page))
788180c06efSJeremy Fitzhardinge 		totalhigh_pages++;
789180c06efSJeremy Fitzhardinge #endif
7909d0ad8caSDaniel Kiper }
7919d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_increment_counters);
792180c06efSJeremy Fitzhardinge 
7939d0ad8caSDaniel Kiper void __online_page_free(struct page *page)
7949d0ad8caSDaniel Kiper {
795180c06efSJeremy Fitzhardinge 	ClearPageReserved(page);
796180c06efSJeremy Fitzhardinge 	init_page_count(page);
797180c06efSJeremy Fitzhardinge 	__free_page(page);
798180c06efSJeremy Fitzhardinge }
7999d0ad8caSDaniel Kiper EXPORT_SYMBOL_GPL(__online_page_free);
8009d0ad8caSDaniel Kiper 
8019d0ad8caSDaniel Kiper static void generic_online_page(struct page *page)
8029d0ad8caSDaniel Kiper {
8039d0ad8caSDaniel Kiper 	__online_page_set_limits(page);
8049d0ad8caSDaniel Kiper 	__online_page_increment_counters(page);
8059d0ad8caSDaniel Kiper 	__online_page_free(page);
8069d0ad8caSDaniel Kiper }
807180c06efSJeremy Fitzhardinge 
80875884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
80975884fb1SKAMEZAWA Hiroyuki 			void *arg)
8103947be19SDave Hansen {
8113947be19SDave Hansen 	unsigned long i;
81275884fb1SKAMEZAWA Hiroyuki 	unsigned long onlined_pages = *(unsigned long *)arg;
81375884fb1SKAMEZAWA Hiroyuki 	struct page *page;
81475884fb1SKAMEZAWA Hiroyuki 	if (PageReserved(pfn_to_page(start_pfn)))
81575884fb1SKAMEZAWA Hiroyuki 		for (i = 0; i < nr_pages; i++) {
81675884fb1SKAMEZAWA Hiroyuki 			page = pfn_to_page(start_pfn + i);
8179d0ad8caSDaniel Kiper 			(*online_page_callback)(page);
81875884fb1SKAMEZAWA Hiroyuki 			onlined_pages++;
81975884fb1SKAMEZAWA Hiroyuki 		}
82075884fb1SKAMEZAWA Hiroyuki 	*(unsigned long *)arg = onlined_pages;
82175884fb1SKAMEZAWA Hiroyuki 	return 0;
82275884fb1SKAMEZAWA Hiroyuki }
82375884fb1SKAMEZAWA Hiroyuki 
82409285af7SLai Jiangshan #ifdef CONFIG_MOVABLE_NODE
82579a4dcefSTang Chen /*
82679a4dcefSTang Chen  * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
82779a4dcefSTang Chen  * normal memory.
82879a4dcefSTang Chen  */
82909285af7SLai Jiangshan static bool can_online_high_movable(struct zone *zone)
83009285af7SLai Jiangshan {
83109285af7SLai Jiangshan 	return true;
83209285af7SLai Jiangshan }
83379a4dcefSTang Chen #else /* CONFIG_MOVABLE_NODE */
83474d42d8fSLai Jiangshan /* ensure every online node has NORMAL memory */
83574d42d8fSLai Jiangshan static bool can_online_high_movable(struct zone *zone)
83674d42d8fSLai Jiangshan {
83774d42d8fSLai Jiangshan 	return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
83874d42d8fSLai Jiangshan }
83979a4dcefSTang Chen #endif /* CONFIG_MOVABLE_NODE */
84074d42d8fSLai Jiangshan 
841d9713679SLai Jiangshan /* check which state of node_states will be changed when online memory */
842d9713679SLai Jiangshan static void node_states_check_changes_online(unsigned long nr_pages,
843d9713679SLai Jiangshan 	struct zone *zone, struct memory_notify *arg)
844d9713679SLai Jiangshan {
845d9713679SLai Jiangshan 	int nid = zone_to_nid(zone);
846d9713679SLai Jiangshan 	enum zone_type zone_last = ZONE_NORMAL;
847d9713679SLai Jiangshan 
848d9713679SLai Jiangshan 	/*
8496715ddf9SLai Jiangshan 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
8506715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_NORMAL,
8516715ddf9SLai Jiangshan 	 * set zone_last to ZONE_NORMAL.
852d9713679SLai Jiangshan 	 *
8536715ddf9SLai Jiangshan 	 * If we don't have HIGHMEM nor movable node,
8546715ddf9SLai Jiangshan 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
8556715ddf9SLai Jiangshan 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
856d9713679SLai Jiangshan 	 */
8576715ddf9SLai Jiangshan 	if (N_MEMORY == N_NORMAL_MEMORY)
858d9713679SLai Jiangshan 		zone_last = ZONE_MOVABLE;
859d9713679SLai Jiangshan 
860d9713679SLai Jiangshan 	/*
861d9713679SLai Jiangshan 	 * if the memory to be online is in a zone of 0...zone_last, and
862d9713679SLai Jiangshan 	 * the zones of 0...zone_last don't have memory before online, we will
863d9713679SLai Jiangshan 	 * need to set the node to node_states[N_NORMAL_MEMORY] after
864d9713679SLai Jiangshan 	 * the memory is online.
865d9713679SLai Jiangshan 	 */
866d9713679SLai Jiangshan 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
867d9713679SLai Jiangshan 		arg->status_change_nid_normal = nid;
868d9713679SLai Jiangshan 	else
869d9713679SLai Jiangshan 		arg->status_change_nid_normal = -1;
870d9713679SLai Jiangshan 
8716715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
8726715ddf9SLai Jiangshan 	/*
8736715ddf9SLai Jiangshan 	 * If we have movable node, node_states[N_HIGH_MEMORY]
8746715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
8756715ddf9SLai Jiangshan 	 * set zone_last to ZONE_HIGHMEM.
8766715ddf9SLai Jiangshan 	 *
8776715ddf9SLai Jiangshan 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
8786715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
8796715ddf9SLai Jiangshan 	 * set zone_last to ZONE_MOVABLE.
8806715ddf9SLai Jiangshan 	 */
8816715ddf9SLai Jiangshan 	zone_last = ZONE_HIGHMEM;
8826715ddf9SLai Jiangshan 	if (N_MEMORY == N_HIGH_MEMORY)
8836715ddf9SLai Jiangshan 		zone_last = ZONE_MOVABLE;
8846715ddf9SLai Jiangshan 
8856715ddf9SLai Jiangshan 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
8866715ddf9SLai Jiangshan 		arg->status_change_nid_high = nid;
8876715ddf9SLai Jiangshan 	else
8886715ddf9SLai Jiangshan 		arg->status_change_nid_high = -1;
8896715ddf9SLai Jiangshan #else
8906715ddf9SLai Jiangshan 	arg->status_change_nid_high = arg->status_change_nid_normal;
8916715ddf9SLai Jiangshan #endif
8926715ddf9SLai Jiangshan 
893d9713679SLai Jiangshan 	/*
894d9713679SLai Jiangshan 	 * if the node don't have memory befor online, we will need to
8956715ddf9SLai Jiangshan 	 * set the node to node_states[N_MEMORY] after the memory
896d9713679SLai Jiangshan 	 * is online.
897d9713679SLai Jiangshan 	 */
8986715ddf9SLai Jiangshan 	if (!node_state(nid, N_MEMORY))
899d9713679SLai Jiangshan 		arg->status_change_nid = nid;
900d9713679SLai Jiangshan 	else
901d9713679SLai Jiangshan 		arg->status_change_nid = -1;
902d9713679SLai Jiangshan }
903d9713679SLai Jiangshan 
904d9713679SLai Jiangshan static void node_states_set_node(int node, struct memory_notify *arg)
905d9713679SLai Jiangshan {
906d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
907d9713679SLai Jiangshan 		node_set_state(node, N_NORMAL_MEMORY);
908d9713679SLai Jiangshan 
9096715ddf9SLai Jiangshan 	if (arg->status_change_nid_high >= 0)
910d9713679SLai Jiangshan 		node_set_state(node, N_HIGH_MEMORY);
9116715ddf9SLai Jiangshan 
9126715ddf9SLai Jiangshan 	node_set_state(node, N_MEMORY);
913d9713679SLai Jiangshan }
914d9713679SLai Jiangshan 
91575884fb1SKAMEZAWA Hiroyuki 
916511c2abaSLai Jiangshan int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
91775884fb1SKAMEZAWA Hiroyuki {
9183947be19SDave Hansen 	unsigned long onlined_pages = 0;
9193947be19SDave Hansen 	struct zone *zone;
9206811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
9217b78d335SYasunori Goto 	int nid;
9227b78d335SYasunori Goto 	int ret;
9237b78d335SYasunori Goto 	struct memory_notify arg;
9243947be19SDave Hansen 
925925268a0SKAMEZAWA Hiroyuki 	lock_memory_hotplug();
926d9713679SLai Jiangshan 	/*
927d9713679SLai Jiangshan 	 * This doesn't need a lock to do pfn_to_page().
928d9713679SLai Jiangshan 	 * The section can't be removed here because of the
929d9713679SLai Jiangshan 	 * memory_block->state_mutex.
930d9713679SLai Jiangshan 	 */
931d9713679SLai Jiangshan 	zone = page_zone(pfn_to_page(pfn));
932d9713679SLai Jiangshan 
93374d42d8fSLai Jiangshan 	if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
93474d42d8fSLai Jiangshan 	    !can_online_high_movable(zone)) {
93574d42d8fSLai Jiangshan 		unlock_memory_hotplug();
93674d42d8fSLai Jiangshan 		return -1;
93774d42d8fSLai Jiangshan 	}
93874d42d8fSLai Jiangshan 
939511c2abaSLai Jiangshan 	if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
940511c2abaSLai Jiangshan 		if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
941511c2abaSLai Jiangshan 			unlock_memory_hotplug();
942511c2abaSLai Jiangshan 			return -1;
943511c2abaSLai Jiangshan 		}
944511c2abaSLai Jiangshan 	}
945511c2abaSLai Jiangshan 	if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
946511c2abaSLai Jiangshan 		if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
947511c2abaSLai Jiangshan 			unlock_memory_hotplug();
948511c2abaSLai Jiangshan 			return -1;
949511c2abaSLai Jiangshan 		}
950511c2abaSLai Jiangshan 	}
951511c2abaSLai Jiangshan 
952511c2abaSLai Jiangshan 	/* Previous code may changed the zone of the pfn range */
953511c2abaSLai Jiangshan 	zone = page_zone(pfn_to_page(pfn));
954511c2abaSLai Jiangshan 
9557b78d335SYasunori Goto 	arg.start_pfn = pfn;
9567b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
957d9713679SLai Jiangshan 	node_states_check_changes_online(nr_pages, zone, &arg);
9587b78d335SYasunori Goto 
9597b78d335SYasunori Goto 	nid = page_to_nid(pfn_to_page(pfn));
9607b78d335SYasunori Goto 
9617b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
9627b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
9637b78d335SYasunori Goto 	if (ret) {
9647b78d335SYasunori Goto 		memory_notify(MEM_CANCEL_ONLINE, &arg);
965925268a0SKAMEZAWA Hiroyuki 		unlock_memory_hotplug();
9667b78d335SYasunori Goto 		return ret;
9677b78d335SYasunori Goto 	}
9683947be19SDave Hansen 	/*
9696811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
9706811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
9716811378eSYasunori Goto 	 * So, zonelist must be updated after online.
9726811378eSYasunori Goto 	 */
9734eaf3f64SHaicheng Li 	mutex_lock(&zonelists_mutex);
9746dcd73d7SWen Congyang 	if (!populated_zone(zone)) {
9756811378eSYasunori Goto 		need_zonelists_rebuild = 1;
9766dcd73d7SWen Congyang 		build_all_zonelists(NULL, zone);
9776dcd73d7SWen Congyang 	}
9786811378eSYasunori Goto 
979908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
98075884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
981fd8a4221SGeoff Levand 	if (ret) {
9826dcd73d7SWen Congyang 		if (need_zonelists_rebuild)
9836dcd73d7SWen Congyang 			zone_pcp_reset(zone);
9844eaf3f64SHaicheng Li 		mutex_unlock(&zonelists_mutex);
985a62e2f4fSBjorn Helgaas 		printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
986a62e2f4fSBjorn Helgaas 		       (unsigned long long) pfn << PAGE_SHIFT,
987a62e2f4fSBjorn Helgaas 		       (((unsigned long long) pfn + nr_pages)
988a62e2f4fSBjorn Helgaas 			    << PAGE_SHIFT) - 1);
989fd8a4221SGeoff Levand 		memory_notify(MEM_CANCEL_ONLINE, &arg);
990925268a0SKAMEZAWA Hiroyuki 		unlock_memory_hotplug();
991fd8a4221SGeoff Levand 		return ret;
992fd8a4221SGeoff Levand 	}
993fd8a4221SGeoff Levand 
9949feedc9dSJiang Liu 	zone->managed_pages += onlined_pages;
9953947be19SDave Hansen 	zone->present_pages += onlined_pages;
996f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
99708dff7b7SJiang Liu 	if (onlined_pages) {
998d9713679SLai Jiangshan 		node_states_set_node(zone_to_nid(zone), &arg);
9991f522509SHaicheng Li 		if (need_zonelists_rebuild)
10006dcd73d7SWen Congyang 			build_all_zonelists(NULL, NULL);
10011f522509SHaicheng Li 		else
1002112067f0SShaohua Li 			zone_pcp_update(zone);
100308dff7b7SJiang Liu 	}
10041f522509SHaicheng Li 
10054eaf3f64SHaicheng Li 	mutex_unlock(&zonelists_mutex);
10061b79acc9SKOSAKI Motohiro 
10071b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
10081b79acc9SKOSAKI Motohiro 
100908dff7b7SJiang Liu 	if (onlined_pages)
10107ea1530aSChristoph Lameter 		kswapd_run(zone_to_nid(zone));
101161b13993SDave Hansen 
10125a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
10132f7f24ecSKent Liu 
10142d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
10157b78d335SYasunori Goto 
10167b78d335SYasunori Goto 	if (onlined_pages)
10177b78d335SYasunori Goto 		memory_notify(MEM_ONLINE, &arg);
1018925268a0SKAMEZAWA Hiroyuki 	unlock_memory_hotplug();
10197b78d335SYasunori Goto 
10203947be19SDave Hansen 	return 0;
10213947be19SDave Hansen }
102253947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1023bc02af93SYasunori Goto 
1024e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1025e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
10269af3c2deSYasunori Goto {
10279af3c2deSYasunori Goto 	struct pglist_data *pgdat;
10289af3c2deSYasunori Goto 	unsigned long zones_size[MAX_NR_ZONES] = {0};
10299af3c2deSYasunori Goto 	unsigned long zholes_size[MAX_NR_ZONES] = {0};
10309af3c2deSYasunori Goto 	unsigned long start_pfn = start >> PAGE_SHIFT;
10319af3c2deSYasunori Goto 
1032a1e565aaSTang Chen 	pgdat = NODE_DATA(nid);
1033a1e565aaSTang Chen 	if (!pgdat) {
10349af3c2deSYasunori Goto 		pgdat = arch_alloc_nodedata(nid);
10359af3c2deSYasunori Goto 		if (!pgdat)
10369af3c2deSYasunori Goto 			return NULL;
10379af3c2deSYasunori Goto 
10389af3c2deSYasunori Goto 		arch_refresh_nodedata(nid, pgdat);
1039a1e565aaSTang Chen 	}
10409af3c2deSYasunori Goto 
10419af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
10429af3c2deSYasunori Goto 
10439af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
10449109fb7bSJohannes Weiner 	free_area_init_node(nid, zones_size, start_pfn, zholes_size);
10459af3c2deSYasunori Goto 
1046959ecc48SKAMEZAWA Hiroyuki 	/*
1047959ecc48SKAMEZAWA Hiroyuki 	 * The node we allocated has no zone fallback lists. For avoiding
1048959ecc48SKAMEZAWA Hiroyuki 	 * to access not-initialized zonelist, build here.
1049959ecc48SKAMEZAWA Hiroyuki 	 */
1050f957db4fSDavid Rientjes 	mutex_lock(&zonelists_mutex);
10519adb62a5SJiang Liu 	build_all_zonelists(pgdat, NULL);
1052f957db4fSDavid Rientjes 	mutex_unlock(&zonelists_mutex);
1053959ecc48SKAMEZAWA Hiroyuki 
10549af3c2deSYasunori Goto 	return pgdat;
10559af3c2deSYasunori Goto }
10569af3c2deSYasunori Goto 
10579af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
10589af3c2deSYasunori Goto {
10599af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
10609af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
10619af3c2deSYasunori Goto 	return;
10629af3c2deSYasunori Goto }
10639af3c2deSYasunori Goto 
10640a547039SKAMEZAWA Hiroyuki 
1065cf23422bSminskey guo /*
1066cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
1067cf23422bSminskey guo  */
1068cf23422bSminskey guo int mem_online_node(int nid)
1069cf23422bSminskey guo {
1070cf23422bSminskey guo 	pg_data_t	*pgdat;
1071cf23422bSminskey guo 	int	ret;
1072cf23422bSminskey guo 
107320d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
1074cf23422bSminskey guo 	pgdat = hotadd_new_pgdat(nid, 0);
10757553e8f2SDavid Rientjes 	if (!pgdat) {
1076cf23422bSminskey guo 		ret = -ENOMEM;
1077cf23422bSminskey guo 		goto out;
1078cf23422bSminskey guo 	}
1079cf23422bSminskey guo 	node_set_online(nid);
1080cf23422bSminskey guo 	ret = register_one_node(nid);
1081cf23422bSminskey guo 	BUG_ON(ret);
1082cf23422bSminskey guo 
1083cf23422bSminskey guo out:
108420d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
1085cf23422bSminskey guo 	return ret;
1086cf23422bSminskey guo }
1087cf23422bSminskey guo 
108831168481SAl Viro /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
108931168481SAl Viro int __ref add_memory(int nid, u64 start, u64 size)
1090bc02af93SYasunori Goto {
10919af3c2deSYasunori Goto 	pg_data_t *pgdat = NULL;
1092a1e565aaSTang Chen 	bool new_pgdat;
1093a1e565aaSTang Chen 	bool new_node;
1094ebd15302SKAMEZAWA Hiroyuki 	struct resource *res;
1095bc02af93SYasunori Goto 	int ret;
1096bc02af93SYasunori Goto 
109720d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
10986ad696d2SAndi Kleen 
1099ebd15302SKAMEZAWA Hiroyuki 	res = register_memory_resource(start, size);
11006ad696d2SAndi Kleen 	ret = -EEXIST;
1101ebd15302SKAMEZAWA Hiroyuki 	if (!res)
11026ad696d2SAndi Kleen 		goto out;
1103ebd15302SKAMEZAWA Hiroyuki 
1104a1e565aaSTang Chen 	{	/* Stupid hack to suppress address-never-null warning */
1105a1e565aaSTang Chen 		void *p = NODE_DATA(nid);
1106a1e565aaSTang Chen 		new_pgdat = !p;
1107a1e565aaSTang Chen 	}
1108a1e565aaSTang Chen 	new_node = !node_online(nid);
1109a1e565aaSTang Chen 	if (new_node) {
11109af3c2deSYasunori Goto 		pgdat = hotadd_new_pgdat(nid, start);
11116ad696d2SAndi Kleen 		ret = -ENOMEM;
11129af3c2deSYasunori Goto 		if (!pgdat)
111341b9e2d7SWen Congyang 			goto error;
11149af3c2deSYasunori Goto 	}
11159af3c2deSYasunori Goto 
1116bc02af93SYasunori Goto 	/* call arch's memory hotadd */
1117bc02af93SYasunori Goto 	ret = arch_add_memory(nid, start, size);
1118bc02af93SYasunori Goto 
11199af3c2deSYasunori Goto 	if (ret < 0)
11209af3c2deSYasunori Goto 		goto error;
11219af3c2deSYasunori Goto 
11220fc44159SYasunori Goto 	/* we online node here. we can't roll back from here. */
11239af3c2deSYasunori Goto 	node_set_online(nid);
11249af3c2deSYasunori Goto 
1125a1e565aaSTang Chen 	if (new_node) {
11260fc44159SYasunori Goto 		ret = register_one_node(nid);
11270fc44159SYasunori Goto 		/*
11280fc44159SYasunori Goto 		 * If sysfs file of new node can't create, cpu on the node
11290fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
11300fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
11310fc44159SYasunori Goto 		 */
11320fc44159SYasunori Goto 		BUG_ON(ret);
11330fc44159SYasunori Goto 	}
11340fc44159SYasunori Goto 
1135d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
1136d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
1137d96ae530Sakpm@linux-foundation.org 
11386ad696d2SAndi Kleen 	goto out;
11396ad696d2SAndi Kleen 
11409af3c2deSYasunori Goto error:
11419af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
11429af3c2deSYasunori Goto 	if (new_pgdat)
11439af3c2deSYasunori Goto 		rollback_node_hotadd(nid, pgdat);
1144ebd15302SKAMEZAWA Hiroyuki 	release_memory_resource(res);
11459af3c2deSYasunori Goto 
11466ad696d2SAndi Kleen out:
114720d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
1148bc02af93SYasunori Goto 	return ret;
1149bc02af93SYasunori Goto }
1150bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
11510c0e6195SKAMEZAWA Hiroyuki 
11520c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
11530c0e6195SKAMEZAWA Hiroyuki /*
11545c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
11555c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
11565c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
11575c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
11585c755e9fSBadari Pulavarty  * be located at the start of the pageblock
11595c755e9fSBadari Pulavarty  */
11605c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
11615c755e9fSBadari Pulavarty {
11625c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
11635c755e9fSBadari Pulavarty }
11645c755e9fSBadari Pulavarty 
11655c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */
11665c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page)
11675c755e9fSBadari Pulavarty {
11685c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
11695c755e9fSBadari Pulavarty 	BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
11705c755e9fSBadari Pulavarty 
11715c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
11720dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
11730dcc48c1SKAMEZAWA Hiroyuki 		int order;
11740dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
11750dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
11760dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
11770dcc48c1SKAMEZAWA Hiroyuki 			return page + (1 << order);
11780dcc48c1SKAMEZAWA Hiroyuki 	}
11795c755e9fSBadari Pulavarty 
11800dcc48c1SKAMEZAWA Hiroyuki 	return page + pageblock_nr_pages;
11815c755e9fSBadari Pulavarty }
11825c755e9fSBadari Pulavarty 
11835c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
11845c755e9fSBadari Pulavarty int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
11855c755e9fSBadari Pulavarty {
11865c755e9fSBadari Pulavarty 	struct page *page = pfn_to_page(start_pfn);
11875c755e9fSBadari Pulavarty 	struct page *end_page = page + nr_pages;
11885c755e9fSBadari Pulavarty 
11895c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
11905c755e9fSBadari Pulavarty 	for (; page < end_page; page = next_active_pageblock(page)) {
119149ac8255SKAMEZAWA Hiroyuki 		if (!is_pageblock_removable_nolock(page))
11925c755e9fSBadari Pulavarty 			return 0;
119349ac8255SKAMEZAWA Hiroyuki 		cond_resched();
11945c755e9fSBadari Pulavarty 	}
11955c755e9fSBadari Pulavarty 
11965c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
11975c755e9fSBadari Pulavarty 	return 1;
11985c755e9fSBadari Pulavarty }
11995c755e9fSBadari Pulavarty 
12005c755e9fSBadari Pulavarty /*
12010c0e6195SKAMEZAWA Hiroyuki  * Confirm all pages in a range [start, end) is belongs to the same zone.
12020c0e6195SKAMEZAWA Hiroyuki  */
12030c0e6195SKAMEZAWA Hiroyuki static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
12040c0e6195SKAMEZAWA Hiroyuki {
12050c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
12060c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
12070c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12080c0e6195SKAMEZAWA Hiroyuki 	int i;
12090c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start_pfn;
12100c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
12110c0e6195SKAMEZAWA Hiroyuki 	     pfn += MAX_ORDER_NR_PAGES) {
12120c0e6195SKAMEZAWA Hiroyuki 		i = 0;
12130c0e6195SKAMEZAWA Hiroyuki 		/* This is just a CONFIG_HOLES_IN_ZONE check.*/
12140c0e6195SKAMEZAWA Hiroyuki 		while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
12150c0e6195SKAMEZAWA Hiroyuki 			i++;
12160c0e6195SKAMEZAWA Hiroyuki 		if (i == MAX_ORDER_NR_PAGES)
12170c0e6195SKAMEZAWA Hiroyuki 			continue;
12180c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn + i);
12190c0e6195SKAMEZAWA Hiroyuki 		if (zone && page_zone(page) != zone)
12200c0e6195SKAMEZAWA Hiroyuki 			return 0;
12210c0e6195SKAMEZAWA Hiroyuki 		zone = page_zone(page);
12220c0e6195SKAMEZAWA Hiroyuki 	}
12230c0e6195SKAMEZAWA Hiroyuki 	return 1;
12240c0e6195SKAMEZAWA Hiroyuki }
12250c0e6195SKAMEZAWA Hiroyuki 
12260c0e6195SKAMEZAWA Hiroyuki /*
12270c0e6195SKAMEZAWA Hiroyuki  * Scanning pfn is much easier than scanning lru list.
12280c0e6195SKAMEZAWA Hiroyuki  * Scan pfn from start to end and Find LRU page.
12290c0e6195SKAMEZAWA Hiroyuki  */
12307bbc0905SAndrew Morton static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
12310c0e6195SKAMEZAWA Hiroyuki {
12320c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
12330c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12340c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
12350c0e6195SKAMEZAWA Hiroyuki 		if (pfn_valid(pfn)) {
12360c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn);
12370c0e6195SKAMEZAWA Hiroyuki 			if (PageLRU(page))
12380c0e6195SKAMEZAWA Hiroyuki 				return pfn;
12390c0e6195SKAMEZAWA Hiroyuki 		}
12400c0e6195SKAMEZAWA Hiroyuki 	}
12410c0e6195SKAMEZAWA Hiroyuki 	return 0;
12420c0e6195SKAMEZAWA Hiroyuki }
12430c0e6195SKAMEZAWA Hiroyuki 
12440c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES	(256)
12450c0e6195SKAMEZAWA Hiroyuki static int
12460c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
12470c0e6195SKAMEZAWA Hiroyuki {
12480c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
12490c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
12500c0e6195SKAMEZAWA Hiroyuki 	int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
12510c0e6195SKAMEZAWA Hiroyuki 	int not_managed = 0;
12520c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
12530c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
12540c0e6195SKAMEZAWA Hiroyuki 
12550c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
12560c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
12570c0e6195SKAMEZAWA Hiroyuki 			continue;
12580c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
1259700c2a46SKonstantin Khlebnikov 		if (!get_page_unless_zero(page))
12600c0e6195SKAMEZAWA Hiroyuki 			continue;
12610c0e6195SKAMEZAWA Hiroyuki 		/*
12620c0e6195SKAMEZAWA Hiroyuki 		 * We can skip free pages. And we can only deal with pages on
12630c0e6195SKAMEZAWA Hiroyuki 		 * LRU.
12640c0e6195SKAMEZAWA Hiroyuki 		 */
126562695a84SNick Piggin 		ret = isolate_lru_page(page);
12660c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
1267700c2a46SKonstantin Khlebnikov 			put_page(page);
126862695a84SNick Piggin 			list_add_tail(&page->lru, &source);
12690c0e6195SKAMEZAWA Hiroyuki 			move_pages--;
12706d9c285aSKOSAKI Motohiro 			inc_zone_page_state(page, NR_ISOLATED_ANON +
12716d9c285aSKOSAKI Motohiro 					    page_is_file_cache(page));
12726d9c285aSKOSAKI Motohiro 
12730c0e6195SKAMEZAWA Hiroyuki 		} else {
12740c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_DEBUG_VM
1275718a3821SWu Fengguang 			printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1276718a3821SWu Fengguang 			       pfn);
1277718a3821SWu Fengguang 			dump_page(page);
12780c0e6195SKAMEZAWA Hiroyuki #endif
1279700c2a46SKonstantin Khlebnikov 			put_page(page);
128025985edcSLucas De Marchi 			/* Because we don't have big zone->lock. we should
1281809c4449SBob Liu 			   check this again here. */
1282809c4449SBob Liu 			if (page_count(page)) {
1283809c4449SBob Liu 				not_managed++;
1284f3ab2636SBob Liu 				ret = -EBUSY;
1285809c4449SBob Liu 				break;
1286809c4449SBob Liu 			}
12870c0e6195SKAMEZAWA Hiroyuki 		}
12880c0e6195SKAMEZAWA Hiroyuki 	}
1289f3ab2636SBob Liu 	if (!list_empty(&source)) {
12900c0e6195SKAMEZAWA Hiroyuki 		if (not_managed) {
12910c0e6195SKAMEZAWA Hiroyuki 			putback_lru_pages(&source);
12920c0e6195SKAMEZAWA Hiroyuki 			goto out;
12930c0e6195SKAMEZAWA Hiroyuki 		}
129474c08f98SMinchan Kim 
129574c08f98SMinchan Kim 		/*
129674c08f98SMinchan Kim 		 * alloc_migrate_target should be improooooved!!
129774c08f98SMinchan Kim 		 * migrate_pages returns # of failed pages.
129874c08f98SMinchan Kim 		 */
129974c08f98SMinchan Kim 		ret = migrate_pages(&source, alloc_migrate_target, 0,
13009c620e2bSHugh Dickins 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1301cf608ac1SMinchan Kim 		if (ret)
1302cf608ac1SMinchan Kim 			putback_lru_pages(&source);
1303f3ab2636SBob Liu 	}
13040c0e6195SKAMEZAWA Hiroyuki out:
13050c0e6195SKAMEZAWA Hiroyuki 	return ret;
13060c0e6195SKAMEZAWA Hiroyuki }
13070c0e6195SKAMEZAWA Hiroyuki 
13080c0e6195SKAMEZAWA Hiroyuki /*
13090c0e6195SKAMEZAWA Hiroyuki  * remove from free_area[] and mark all as Reserved.
13100c0e6195SKAMEZAWA Hiroyuki  */
13110c0e6195SKAMEZAWA Hiroyuki static int
13120c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
13130c0e6195SKAMEZAWA Hiroyuki 			void *data)
13140c0e6195SKAMEZAWA Hiroyuki {
13150c0e6195SKAMEZAWA Hiroyuki 	__offline_isolated_pages(start, start + nr_pages);
13160c0e6195SKAMEZAWA Hiroyuki 	return 0;
13170c0e6195SKAMEZAWA Hiroyuki }
13180c0e6195SKAMEZAWA Hiroyuki 
13190c0e6195SKAMEZAWA Hiroyuki static void
13200c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
13210c0e6195SKAMEZAWA Hiroyuki {
1322908eedc6SKAMEZAWA Hiroyuki 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
13230c0e6195SKAMEZAWA Hiroyuki 				offline_isolated_pages_cb);
13240c0e6195SKAMEZAWA Hiroyuki }
13250c0e6195SKAMEZAWA Hiroyuki 
13260c0e6195SKAMEZAWA Hiroyuki /*
13270c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
13280c0e6195SKAMEZAWA Hiroyuki  */
13290c0e6195SKAMEZAWA Hiroyuki static int
13300c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
13310c0e6195SKAMEZAWA Hiroyuki 			void *data)
13320c0e6195SKAMEZAWA Hiroyuki {
13330c0e6195SKAMEZAWA Hiroyuki 	int ret;
13340c0e6195SKAMEZAWA Hiroyuki 	long offlined = *(long *)data;
1335b023f468SWen Congyang 	ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
13360c0e6195SKAMEZAWA Hiroyuki 	offlined = nr_pages;
13370c0e6195SKAMEZAWA Hiroyuki 	if (!ret)
13380c0e6195SKAMEZAWA Hiroyuki 		*(long *)data += offlined;
13390c0e6195SKAMEZAWA Hiroyuki 	return ret;
13400c0e6195SKAMEZAWA Hiroyuki }
13410c0e6195SKAMEZAWA Hiroyuki 
13420c0e6195SKAMEZAWA Hiroyuki static long
13430c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
13440c0e6195SKAMEZAWA Hiroyuki {
13450c0e6195SKAMEZAWA Hiroyuki 	long offlined = 0;
13460c0e6195SKAMEZAWA Hiroyuki 	int ret;
13470c0e6195SKAMEZAWA Hiroyuki 
1348908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
13490c0e6195SKAMEZAWA Hiroyuki 			check_pages_isolated_cb);
13500c0e6195SKAMEZAWA Hiroyuki 	if (ret < 0)
13510c0e6195SKAMEZAWA Hiroyuki 		offlined = (long)ret;
13520c0e6195SKAMEZAWA Hiroyuki 	return offlined;
13530c0e6195SKAMEZAWA Hiroyuki }
13540c0e6195SKAMEZAWA Hiroyuki 
135509285af7SLai Jiangshan #ifdef CONFIG_MOVABLE_NODE
135679a4dcefSTang Chen /*
135779a4dcefSTang Chen  * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
135879a4dcefSTang Chen  * normal memory.
135979a4dcefSTang Chen  */
136009285af7SLai Jiangshan static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
136109285af7SLai Jiangshan {
136209285af7SLai Jiangshan 	return true;
136309285af7SLai Jiangshan }
136479a4dcefSTang Chen #else /* CONFIG_MOVABLE_NODE */
136574d42d8fSLai Jiangshan /* ensure the node has NORMAL memory if it is still online */
136674d42d8fSLai Jiangshan static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
136774d42d8fSLai Jiangshan {
136874d42d8fSLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
136974d42d8fSLai Jiangshan 	unsigned long present_pages = 0;
137074d42d8fSLai Jiangshan 	enum zone_type zt;
137174d42d8fSLai Jiangshan 
137274d42d8fSLai Jiangshan 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
137374d42d8fSLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
137474d42d8fSLai Jiangshan 
137574d42d8fSLai Jiangshan 	if (present_pages > nr_pages)
137674d42d8fSLai Jiangshan 		return true;
137774d42d8fSLai Jiangshan 
137874d42d8fSLai Jiangshan 	present_pages = 0;
137974d42d8fSLai Jiangshan 	for (; zt <= ZONE_MOVABLE; zt++)
138074d42d8fSLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
138174d42d8fSLai Jiangshan 
138274d42d8fSLai Jiangshan 	/*
138374d42d8fSLai Jiangshan 	 * we can't offline the last normal memory until all
138474d42d8fSLai Jiangshan 	 * higher memory is offlined.
138574d42d8fSLai Jiangshan 	 */
138674d42d8fSLai Jiangshan 	return present_pages == 0;
138774d42d8fSLai Jiangshan }
138879a4dcefSTang Chen #endif /* CONFIG_MOVABLE_NODE */
138974d42d8fSLai Jiangshan 
1390d9713679SLai Jiangshan /* check which state of node_states will be changed when offline memory */
1391d9713679SLai Jiangshan static void node_states_check_changes_offline(unsigned long nr_pages,
1392d9713679SLai Jiangshan 		struct zone *zone, struct memory_notify *arg)
1393d9713679SLai Jiangshan {
1394d9713679SLai Jiangshan 	struct pglist_data *pgdat = zone->zone_pgdat;
1395d9713679SLai Jiangshan 	unsigned long present_pages = 0;
1396d9713679SLai Jiangshan 	enum zone_type zt, zone_last = ZONE_NORMAL;
1397d9713679SLai Jiangshan 
1398d9713679SLai Jiangshan 	/*
13996715ddf9SLai Jiangshan 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
14006715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_NORMAL,
14016715ddf9SLai Jiangshan 	 * set zone_last to ZONE_NORMAL.
1402d9713679SLai Jiangshan 	 *
14036715ddf9SLai Jiangshan 	 * If we don't have HIGHMEM nor movable node,
14046715ddf9SLai Jiangshan 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
14056715ddf9SLai Jiangshan 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1406d9713679SLai Jiangshan 	 */
14076715ddf9SLai Jiangshan 	if (N_MEMORY == N_NORMAL_MEMORY)
1408d9713679SLai Jiangshan 		zone_last = ZONE_MOVABLE;
1409d9713679SLai Jiangshan 
1410d9713679SLai Jiangshan 	/*
1411d9713679SLai Jiangshan 	 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1412d9713679SLai Jiangshan 	 * If the memory to be offline is in a zone of 0...zone_last,
1413d9713679SLai Jiangshan 	 * and it is the last present memory, 0...zone_last will
1414d9713679SLai Jiangshan 	 * become empty after offline , thus we can determind we will
1415d9713679SLai Jiangshan 	 * need to clear the node from node_states[N_NORMAL_MEMORY].
1416d9713679SLai Jiangshan 	 */
1417d9713679SLai Jiangshan 	for (zt = 0; zt <= zone_last; zt++)
1418d9713679SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
1419d9713679SLai Jiangshan 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1420d9713679SLai Jiangshan 		arg->status_change_nid_normal = zone_to_nid(zone);
1421d9713679SLai Jiangshan 	else
1422d9713679SLai Jiangshan 		arg->status_change_nid_normal = -1;
1423d9713679SLai Jiangshan 
14246715ddf9SLai Jiangshan #ifdef CONFIG_HIGHMEM
14256715ddf9SLai Jiangshan 	/*
14266715ddf9SLai Jiangshan 	 * If we have movable node, node_states[N_HIGH_MEMORY]
14276715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
14286715ddf9SLai Jiangshan 	 * set zone_last to ZONE_HIGHMEM.
14296715ddf9SLai Jiangshan 	 *
14306715ddf9SLai Jiangshan 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
14316715ddf9SLai Jiangshan 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
14326715ddf9SLai Jiangshan 	 * set zone_last to ZONE_MOVABLE.
14336715ddf9SLai Jiangshan 	 */
14346715ddf9SLai Jiangshan 	zone_last = ZONE_HIGHMEM;
14356715ddf9SLai Jiangshan 	if (N_MEMORY == N_HIGH_MEMORY)
14366715ddf9SLai Jiangshan 		zone_last = ZONE_MOVABLE;
14376715ddf9SLai Jiangshan 
14386715ddf9SLai Jiangshan 	for (; zt <= zone_last; zt++)
14396715ddf9SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
14406715ddf9SLai Jiangshan 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
14416715ddf9SLai Jiangshan 		arg->status_change_nid_high = zone_to_nid(zone);
14426715ddf9SLai Jiangshan 	else
14436715ddf9SLai Jiangshan 		arg->status_change_nid_high = -1;
14446715ddf9SLai Jiangshan #else
14456715ddf9SLai Jiangshan 	arg->status_change_nid_high = arg->status_change_nid_normal;
14466715ddf9SLai Jiangshan #endif
14476715ddf9SLai Jiangshan 
1448d9713679SLai Jiangshan 	/*
1449d9713679SLai Jiangshan 	 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1450d9713679SLai Jiangshan 	 */
1451d9713679SLai Jiangshan 	zone_last = ZONE_MOVABLE;
1452d9713679SLai Jiangshan 
1453d9713679SLai Jiangshan 	/*
1454d9713679SLai Jiangshan 	 * check whether node_states[N_HIGH_MEMORY] will be changed
1455d9713679SLai Jiangshan 	 * If we try to offline the last present @nr_pages from the node,
1456d9713679SLai Jiangshan 	 * we can determind we will need to clear the node from
1457d9713679SLai Jiangshan 	 * node_states[N_HIGH_MEMORY].
1458d9713679SLai Jiangshan 	 */
1459d9713679SLai Jiangshan 	for (; zt <= zone_last; zt++)
1460d9713679SLai Jiangshan 		present_pages += pgdat->node_zones[zt].present_pages;
1461d9713679SLai Jiangshan 	if (nr_pages >= present_pages)
1462d9713679SLai Jiangshan 		arg->status_change_nid = zone_to_nid(zone);
1463d9713679SLai Jiangshan 	else
1464d9713679SLai Jiangshan 		arg->status_change_nid = -1;
1465d9713679SLai Jiangshan }
1466d9713679SLai Jiangshan 
1467d9713679SLai Jiangshan static void node_states_clear_node(int node, struct memory_notify *arg)
1468d9713679SLai Jiangshan {
1469d9713679SLai Jiangshan 	if (arg->status_change_nid_normal >= 0)
1470d9713679SLai Jiangshan 		node_clear_state(node, N_NORMAL_MEMORY);
1471d9713679SLai Jiangshan 
14726715ddf9SLai Jiangshan 	if ((N_MEMORY != N_NORMAL_MEMORY) &&
14736715ddf9SLai Jiangshan 	    (arg->status_change_nid_high >= 0))
1474d9713679SLai Jiangshan 		node_clear_state(node, N_HIGH_MEMORY);
14756715ddf9SLai Jiangshan 
14766715ddf9SLai Jiangshan 	if ((N_MEMORY != N_HIGH_MEMORY) &&
14776715ddf9SLai Jiangshan 	    (arg->status_change_nid >= 0))
14786715ddf9SLai Jiangshan 		node_clear_state(node, N_MEMORY);
1479d9713679SLai Jiangshan }
1480d9713679SLai Jiangshan 
1481a16cee10SWen Congyang static int __ref __offline_pages(unsigned long start_pfn,
14820c0e6195SKAMEZAWA Hiroyuki 		  unsigned long end_pfn, unsigned long timeout)
14830c0e6195SKAMEZAWA Hiroyuki {
14840c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn, nr_pages, expire;
14850c0e6195SKAMEZAWA Hiroyuki 	long offlined_pages;
14867b78d335SYasunori Goto 	int ret, drain, retry_max, node;
14870c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
14887b78d335SYasunori Goto 	struct memory_notify arg;
14890c0e6195SKAMEZAWA Hiroyuki 
14900c0e6195SKAMEZAWA Hiroyuki 	BUG_ON(start_pfn >= end_pfn);
14910c0e6195SKAMEZAWA Hiroyuki 	/* at least, alignment against pageblock is necessary */
14920c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
14930c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
14940c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
14950c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
14960c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
14970c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
14980c0e6195SKAMEZAWA Hiroyuki 	if (!test_pages_in_a_zone(start_pfn, end_pfn))
14990c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
15007b78d335SYasunori Goto 
150120d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
15026ad696d2SAndi Kleen 
15037b78d335SYasunori Goto 	zone = page_zone(pfn_to_page(start_pfn));
15047b78d335SYasunori Goto 	node = zone_to_nid(zone);
15057b78d335SYasunori Goto 	nr_pages = end_pfn - start_pfn;
15067b78d335SYasunori Goto 
150774d42d8fSLai Jiangshan 	ret = -EINVAL;
150874d42d8fSLai Jiangshan 	if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
150974d42d8fSLai Jiangshan 		goto out;
151074d42d8fSLai Jiangshan 
15110c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
1512b023f468SWen Congyang 	ret = start_isolate_page_range(start_pfn, end_pfn,
1513b023f468SWen Congyang 				       MIGRATE_MOVABLE, true);
15140c0e6195SKAMEZAWA Hiroyuki 	if (ret)
15156ad696d2SAndi Kleen 		goto out;
15167b78d335SYasunori Goto 
15177b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
15187b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
1519d9713679SLai Jiangshan 	node_states_check_changes_offline(nr_pages, zone, &arg);
15207b78d335SYasunori Goto 
15217b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
15227b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
15237b78d335SYasunori Goto 	if (ret)
15247b78d335SYasunori Goto 		goto failed_removal;
15257b78d335SYasunori Goto 
15260c0e6195SKAMEZAWA Hiroyuki 	pfn = start_pfn;
15270c0e6195SKAMEZAWA Hiroyuki 	expire = jiffies + timeout;
15280c0e6195SKAMEZAWA Hiroyuki 	drain = 0;
15290c0e6195SKAMEZAWA Hiroyuki 	retry_max = 5;
15300c0e6195SKAMEZAWA Hiroyuki repeat:
15310c0e6195SKAMEZAWA Hiroyuki 	/* start memory hot removal */
15320c0e6195SKAMEZAWA Hiroyuki 	ret = -EAGAIN;
15330c0e6195SKAMEZAWA Hiroyuki 	if (time_after(jiffies, expire))
15340c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
15350c0e6195SKAMEZAWA Hiroyuki 	ret = -EINTR;
15360c0e6195SKAMEZAWA Hiroyuki 	if (signal_pending(current))
15370c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
15380c0e6195SKAMEZAWA Hiroyuki 	ret = 0;
15390c0e6195SKAMEZAWA Hiroyuki 	if (drain) {
15400c0e6195SKAMEZAWA Hiroyuki 		lru_add_drain_all();
15410c0e6195SKAMEZAWA Hiroyuki 		cond_resched();
15429f8f2172SChristoph Lameter 		drain_all_pages();
15430c0e6195SKAMEZAWA Hiroyuki 	}
15440c0e6195SKAMEZAWA Hiroyuki 
15450c0e6195SKAMEZAWA Hiroyuki 	pfn = scan_lru_pages(start_pfn, end_pfn);
15460c0e6195SKAMEZAWA Hiroyuki 	if (pfn) { /* We have page on LRU */
15470c0e6195SKAMEZAWA Hiroyuki 		ret = do_migrate_range(pfn, end_pfn);
15480c0e6195SKAMEZAWA Hiroyuki 		if (!ret) {
15490c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
15500c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
15510c0e6195SKAMEZAWA Hiroyuki 		} else {
15520c0e6195SKAMEZAWA Hiroyuki 			if (ret < 0)
15530c0e6195SKAMEZAWA Hiroyuki 				if (--retry_max == 0)
15540c0e6195SKAMEZAWA Hiroyuki 					goto failed_removal;
15550c0e6195SKAMEZAWA Hiroyuki 			yield();
15560c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
15570c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
15580c0e6195SKAMEZAWA Hiroyuki 		}
15590c0e6195SKAMEZAWA Hiroyuki 	}
1560b3834be5SAdam Buchbinder 	/* drain all zone's lru pagevec, this is asynchronous... */
15610c0e6195SKAMEZAWA Hiroyuki 	lru_add_drain_all();
15620c0e6195SKAMEZAWA Hiroyuki 	yield();
1563b3834be5SAdam Buchbinder 	/* drain pcp pages, this is synchronous. */
15649f8f2172SChristoph Lameter 	drain_all_pages();
15650c0e6195SKAMEZAWA Hiroyuki 	/* check again */
15660c0e6195SKAMEZAWA Hiroyuki 	offlined_pages = check_pages_isolated(start_pfn, end_pfn);
15670c0e6195SKAMEZAWA Hiroyuki 	if (offlined_pages < 0) {
15680c0e6195SKAMEZAWA Hiroyuki 		ret = -EBUSY;
15690c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
15700c0e6195SKAMEZAWA Hiroyuki 	}
15710c0e6195SKAMEZAWA Hiroyuki 	printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1572b3834be5SAdam Buchbinder 	/* Ok, all of our target is isolated.
15730c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
15740c0e6195SKAMEZAWA Hiroyuki 	offline_isolated_pages(start_pfn, end_pfn);
1575dbc0e4ceSKAMEZAWA Hiroyuki 	/* reset pagetype flags and makes migrate type to be MOVABLE */
15760815f3d8SMichal Nazarewicz 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
15770c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
15789feedc9dSJiang Liu 	zone->managed_pages -= offlined_pages;
15790c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
15800c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
15810c0e6195SKAMEZAWA Hiroyuki 	totalram_pages -= offlined_pages;
15827b78d335SYasunori Goto 
15831b79acc9SKOSAKI Motohiro 	init_per_zone_wmark_min();
15841b79acc9SKOSAKI Motohiro 
15851e8537baSXishi Qiu 	if (!populated_zone(zone)) {
1586340175b7SJiang Liu 		zone_pcp_reset(zone);
15871e8537baSXishi Qiu 		mutex_lock(&zonelists_mutex);
15881e8537baSXishi Qiu 		build_all_zonelists(NULL, NULL);
15891e8537baSXishi Qiu 		mutex_unlock(&zonelists_mutex);
15901e8537baSXishi Qiu 	} else
15911e8537baSXishi Qiu 		zone_pcp_update(zone);
1592340175b7SJiang Liu 
1593d9713679SLai Jiangshan 	node_states_clear_node(node, &arg);
1594d9713679SLai Jiangshan 	if (arg.status_change_nid >= 0)
15958fe23e05SDavid Rientjes 		kswapd_stop(node);
1596bce7394aSMinchan Kim 
15970c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
15980c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
15997b78d335SYasunori Goto 
16007b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
160120d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
16020c0e6195SKAMEZAWA Hiroyuki 	return 0;
16030c0e6195SKAMEZAWA Hiroyuki 
16040c0e6195SKAMEZAWA Hiroyuki failed_removal:
1605a62e2f4fSBjorn Helgaas 	printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1606a62e2f4fSBjorn Helgaas 	       (unsigned long long) start_pfn << PAGE_SHIFT,
1607a62e2f4fSBjorn Helgaas 	       ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
16087b78d335SYasunori Goto 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
16090c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
16100815f3d8SMichal Nazarewicz 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
16117b78d335SYasunori Goto 
16126ad696d2SAndi Kleen out:
161320d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
16140c0e6195SKAMEZAWA Hiroyuki 	return ret;
16150c0e6195SKAMEZAWA Hiroyuki }
161671088785SBadari Pulavarty 
1617a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1618a16cee10SWen Congyang {
1619a16cee10SWen Congyang 	return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1620a16cee10SWen Congyang }
1621a16cee10SWen Congyang 
1622bbc76be6SWen Congyang /**
1623bbc76be6SWen Congyang  * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1624bbc76be6SWen Congyang  * @start_pfn: start pfn of the memory range
1625e05c4bbfSToshi Kani  * @end_pfn: end pfn of the memory range
1626bbc76be6SWen Congyang  * @arg: argument passed to func
1627bbc76be6SWen Congyang  * @func: callback for each memory section walked
1628bbc76be6SWen Congyang  *
1629bbc76be6SWen Congyang  * This function walks through all present mem sections in range
1630bbc76be6SWen Congyang  * [start_pfn, end_pfn) and call func on each mem section.
1631bbc76be6SWen Congyang  *
1632bbc76be6SWen Congyang  * Returns the return value of func.
1633bbc76be6SWen Congyang  */
1634bbc76be6SWen Congyang static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1635bbc76be6SWen Congyang 		void *arg, int (*func)(struct memory_block *, void *))
163671088785SBadari Pulavarty {
1637e90bdb7fSWen Congyang 	struct memory_block *mem = NULL;
1638e90bdb7fSWen Congyang 	struct mem_section *section;
1639e90bdb7fSWen Congyang 	unsigned long pfn, section_nr;
1640e90bdb7fSWen Congyang 	int ret;
164171088785SBadari Pulavarty 
1642e90bdb7fSWen Congyang 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1643e90bdb7fSWen Congyang 		section_nr = pfn_to_section_nr(pfn);
1644e90bdb7fSWen Congyang 		if (!present_section_nr(section_nr))
1645e90bdb7fSWen Congyang 			continue;
1646e90bdb7fSWen Congyang 
1647e90bdb7fSWen Congyang 		section = __nr_to_section(section_nr);
1648e90bdb7fSWen Congyang 		/* same memblock? */
1649e90bdb7fSWen Congyang 		if (mem)
1650e90bdb7fSWen Congyang 			if ((section_nr >= mem->start_section_nr) &&
1651e90bdb7fSWen Congyang 			    (section_nr <= mem->end_section_nr))
1652e90bdb7fSWen Congyang 				continue;
1653e90bdb7fSWen Congyang 
1654e90bdb7fSWen Congyang 		mem = find_memory_block_hinted(section, mem);
1655e90bdb7fSWen Congyang 		if (!mem)
1656e90bdb7fSWen Congyang 			continue;
1657e90bdb7fSWen Congyang 
1658bbc76be6SWen Congyang 		ret = func(mem, arg);
1659e90bdb7fSWen Congyang 		if (ret) {
1660e90bdb7fSWen Congyang 			kobject_put(&mem->dev.kobj);
1661e90bdb7fSWen Congyang 			return ret;
1662e90bdb7fSWen Congyang 		}
1663e90bdb7fSWen Congyang 	}
1664e90bdb7fSWen Congyang 
1665e90bdb7fSWen Congyang 	if (mem)
1666e90bdb7fSWen Congyang 		kobject_put(&mem->dev.kobj);
1667e90bdb7fSWen Congyang 
1668bbc76be6SWen Congyang 	return 0;
1669bbc76be6SWen Congyang }
1670bbc76be6SWen Congyang 
1671bbc76be6SWen Congyang /**
1672bbc76be6SWen Congyang  * offline_memory_block_cb - callback function for offlining memory block
1673bbc76be6SWen Congyang  * @mem: the memory block to be offlined
1674bbc76be6SWen Congyang  * @arg: buffer to hold error msg
1675bbc76be6SWen Congyang  *
1676bbc76be6SWen Congyang  * Always return 0, and put the error msg in arg if any.
1677bbc76be6SWen Congyang  */
1678bbc76be6SWen Congyang static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1679bbc76be6SWen Congyang {
1680bbc76be6SWen Congyang 	int *ret = arg;
1681bbc76be6SWen Congyang 	int error = offline_memory_block(mem);
1682bbc76be6SWen Congyang 
1683bbc76be6SWen Congyang 	if (error != 0 && *ret == 0)
1684bbc76be6SWen Congyang 		*ret = error;
1685bbc76be6SWen Congyang 
1686bbc76be6SWen Congyang 	return 0;
1687bbc76be6SWen Congyang }
1688bbc76be6SWen Congyang 
1689bbc76be6SWen Congyang static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1690bbc76be6SWen Congyang {
1691bbc76be6SWen Congyang 	int ret = !is_memblock_offlined(mem);
1692bbc76be6SWen Congyang 
1693bbc76be6SWen Congyang 	if (unlikely(ret))
1694bbc76be6SWen Congyang 		pr_warn("removing memory fails, because memory "
1695bbc76be6SWen Congyang 			"[%#010llx-%#010llx] is onlined\n",
1696bbc76be6SWen Congyang 			PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1697bbc76be6SWen Congyang 			PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1698bbc76be6SWen Congyang 
1699bbc76be6SWen Congyang 	return ret;
1700bbc76be6SWen Congyang }
1701bbc76be6SWen Congyang 
170260a5a19eSTang Chen static int check_cpu_on_node(void *data)
170360a5a19eSTang Chen {
170460a5a19eSTang Chen 	struct pglist_data *pgdat = data;
170560a5a19eSTang Chen 	int cpu;
170660a5a19eSTang Chen 
170760a5a19eSTang Chen 	for_each_present_cpu(cpu) {
170860a5a19eSTang Chen 		if (cpu_to_node(cpu) == pgdat->node_id)
170960a5a19eSTang Chen 			/*
171060a5a19eSTang Chen 			 * the cpu on this node isn't removed, and we can't
171160a5a19eSTang Chen 			 * offline this node.
171260a5a19eSTang Chen 			 */
171360a5a19eSTang Chen 			return -EBUSY;
171460a5a19eSTang Chen 	}
171560a5a19eSTang Chen 
171660a5a19eSTang Chen 	return 0;
171760a5a19eSTang Chen }
171860a5a19eSTang Chen 
1719e13fe869SWen Congyang static void unmap_cpu_on_node(void *data)
1720e13fe869SWen Congyang {
1721e13fe869SWen Congyang #ifdef CONFIG_ACPI_NUMA
1722e13fe869SWen Congyang 	struct pglist_data *pgdat = data;
1723e13fe869SWen Congyang 	int cpu;
1724e13fe869SWen Congyang 
1725e13fe869SWen Congyang 	for_each_possible_cpu(cpu)
1726e13fe869SWen Congyang 		if (cpu_to_node(cpu) == pgdat->node_id)
1727e13fe869SWen Congyang 			numa_clear_node(cpu);
1728e13fe869SWen Congyang #endif
1729e13fe869SWen Congyang }
1730e13fe869SWen Congyang 
1731e13fe869SWen Congyang static int check_and_unmap_cpu_on_node(void *data)
1732e13fe869SWen Congyang {
1733e13fe869SWen Congyang 	int ret = check_cpu_on_node(data);
1734e13fe869SWen Congyang 
1735e13fe869SWen Congyang 	if (ret)
1736e13fe869SWen Congyang 		return ret;
1737e13fe869SWen Congyang 
1738e13fe869SWen Congyang 	/*
1739e13fe869SWen Congyang 	 * the node will be offlined when we come here, so we can clear
1740e13fe869SWen Congyang 	 * the cpu_to_node() now.
1741e13fe869SWen Congyang 	 */
1742e13fe869SWen Congyang 
1743e13fe869SWen Congyang 	unmap_cpu_on_node(data);
1744e13fe869SWen Congyang 	return 0;
1745e13fe869SWen Congyang }
1746e13fe869SWen Congyang 
174760a5a19eSTang Chen /* offline the node if all memory sections of this node are removed */
174890b30cdcSWen Congyang void try_offline_node(int nid)
174960a5a19eSTang Chen {
1750d822b86aSWen Congyang 	pg_data_t *pgdat = NODE_DATA(nid);
1751d822b86aSWen Congyang 	unsigned long start_pfn = pgdat->node_start_pfn;
1752d822b86aSWen Congyang 	unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
175360a5a19eSTang Chen 	unsigned long pfn;
1754d822b86aSWen Congyang 	struct page *pgdat_page = virt_to_page(pgdat);
1755d822b86aSWen Congyang 	int i;
175660a5a19eSTang Chen 
175760a5a19eSTang Chen 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
175860a5a19eSTang Chen 		unsigned long section_nr = pfn_to_section_nr(pfn);
175960a5a19eSTang Chen 
176060a5a19eSTang Chen 		if (!present_section_nr(section_nr))
176160a5a19eSTang Chen 			continue;
176260a5a19eSTang Chen 
176360a5a19eSTang Chen 		if (pfn_to_nid(pfn) != nid)
176460a5a19eSTang Chen 			continue;
176560a5a19eSTang Chen 
176660a5a19eSTang Chen 		/*
176760a5a19eSTang Chen 		 * some memory sections of this node are not removed, and we
176860a5a19eSTang Chen 		 * can't offline node now.
176960a5a19eSTang Chen 		 */
177060a5a19eSTang Chen 		return;
177160a5a19eSTang Chen 	}
177260a5a19eSTang Chen 
1773e13fe869SWen Congyang 	if (stop_machine(check_and_unmap_cpu_on_node, pgdat, NULL))
177460a5a19eSTang Chen 		return;
177560a5a19eSTang Chen 
177660a5a19eSTang Chen 	/*
177760a5a19eSTang Chen 	 * all memory/cpu of this node are removed, we can offline this
177860a5a19eSTang Chen 	 * node now.
177960a5a19eSTang Chen 	 */
178060a5a19eSTang Chen 	node_set_offline(nid);
178160a5a19eSTang Chen 	unregister_one_node(nid);
1782d822b86aSWen Congyang 
1783d822b86aSWen Congyang 	if (!PageSlab(pgdat_page) && !PageCompound(pgdat_page))
1784d822b86aSWen Congyang 		/* node data is allocated from boot memory */
1785d822b86aSWen Congyang 		return;
1786d822b86aSWen Congyang 
1787d822b86aSWen Congyang 	/* free waittable in each zone */
1788d822b86aSWen Congyang 	for (i = 0; i < MAX_NR_ZONES; i++) {
1789d822b86aSWen Congyang 		struct zone *zone = pgdat->node_zones + i;
1790d822b86aSWen Congyang 
1791ca4b3f30SJianguo Wu 		/*
1792ca4b3f30SJianguo Wu 		 * wait_table may be allocated from boot memory,
1793ca4b3f30SJianguo Wu 		 * here only free if it's allocated by vmalloc.
1794ca4b3f30SJianguo Wu 		 */
1795ca4b3f30SJianguo Wu 		if (is_vmalloc_addr(zone->wait_table))
1796d822b86aSWen Congyang 			vfree(zone->wait_table);
1797d822b86aSWen Congyang 	}
1798d822b86aSWen Congyang 
1799d822b86aSWen Congyang 	/*
1800d822b86aSWen Congyang 	 * Since there is no way to guarentee the address of pgdat/zone is not
1801d822b86aSWen Congyang 	 * on stack of any kernel threads or used by other kernel objects
1802d822b86aSWen Congyang 	 * without reference counting or other symchronizing method, do not
1803d822b86aSWen Congyang 	 * reset node_data and free pgdat here. Just reset it to 0 and reuse
1804d822b86aSWen Congyang 	 * the memory when the node is online again.
1805d822b86aSWen Congyang 	 */
1806d822b86aSWen Congyang 	memset(pgdat, 0, sizeof(*pgdat));
180760a5a19eSTang Chen }
180890b30cdcSWen Congyang EXPORT_SYMBOL(try_offline_node);
180960a5a19eSTang Chen 
181060a5a19eSTang Chen int __ref remove_memory(int nid, u64 start, u64 size)
1811bbc76be6SWen Congyang {
1812bbc76be6SWen Congyang 	unsigned long start_pfn, end_pfn;
1813bbc76be6SWen Congyang 	int ret = 0;
1814bbc76be6SWen Congyang 	int retry = 1;
1815bbc76be6SWen Congyang 
1816bbc76be6SWen Congyang 	start_pfn = PFN_DOWN(start);
1817f8749452SToshi Kani 	end_pfn = PFN_UP(start + size - 1);
1818bbc76be6SWen Congyang 
1819bbc76be6SWen Congyang 	/*
1820bbc76be6SWen Congyang 	 * When CONFIG_MEMCG is on, one memory block may be used by other
1821bbc76be6SWen Congyang 	 * blocks to store page cgroup when onlining pages. But we don't know
1822bbc76be6SWen Congyang 	 * in what order pages are onlined. So we iterate twice to offline
1823bbc76be6SWen Congyang 	 * memory:
1824bbc76be6SWen Congyang 	 * 1st iterate: offline every non primary memory block.
1825bbc76be6SWen Congyang 	 * 2nd iterate: offline primary (i.e. first added) memory block.
1826bbc76be6SWen Congyang 	 */
1827bbc76be6SWen Congyang repeat:
1828bbc76be6SWen Congyang 	walk_memory_range(start_pfn, end_pfn, &ret,
1829bbc76be6SWen Congyang 			  offline_memory_block_cb);
1830bbc76be6SWen Congyang 	if (ret) {
1831bbc76be6SWen Congyang 		if (!retry)
1832bbc76be6SWen Congyang 			return ret;
1833bbc76be6SWen Congyang 
1834bbc76be6SWen Congyang 		retry = 0;
1835bbc76be6SWen Congyang 		ret = 0;
1836993c1aadSWen Congyang 		goto repeat;
1837993c1aadSWen Congyang 	}
1838993c1aadSWen Congyang 
18396677e3eaSYasuaki Ishimatsu 	lock_memory_hotplug();
18406677e3eaSYasuaki Ishimatsu 
18416677e3eaSYasuaki Ishimatsu 	/*
18426677e3eaSYasuaki Ishimatsu 	 * we have offlined all memory blocks like this:
18436677e3eaSYasuaki Ishimatsu 	 *   1. lock memory hotplug
18446677e3eaSYasuaki Ishimatsu 	 *   2. offline a memory block
18456677e3eaSYasuaki Ishimatsu 	 *   3. unlock memory hotplug
18466677e3eaSYasuaki Ishimatsu 	 *
18476677e3eaSYasuaki Ishimatsu 	 * repeat step1-3 to offline the memory block. All memory blocks
18486677e3eaSYasuaki Ishimatsu 	 * must be offlined before removing memory. But we don't hold the
18496677e3eaSYasuaki Ishimatsu 	 * lock in the whole operation. So we should check whether all
18506677e3eaSYasuaki Ishimatsu 	 * memory blocks are offlined.
18516677e3eaSYasuaki Ishimatsu 	 */
18526677e3eaSYasuaki Ishimatsu 
1853bbc76be6SWen Congyang 	ret = walk_memory_range(start_pfn, end_pfn, NULL,
1854bbc76be6SWen Congyang 				is_memblock_offlined_cb);
1855bbc76be6SWen Congyang 	if (ret) {
18566677e3eaSYasuaki Ishimatsu 		unlock_memory_hotplug();
18576677e3eaSYasuaki Ishimatsu 		return ret;
18586677e3eaSYasuaki Ishimatsu 	}
18596677e3eaSYasuaki Ishimatsu 
186046c66c4bSYasuaki Ishimatsu 	/* remove memmap entry */
186146c66c4bSYasuaki Ishimatsu 	firmware_map_remove(start, start + size, "System RAM");
186246c66c4bSYasuaki Ishimatsu 
186324d335caSWen Congyang 	arch_remove_memory(start, size);
186424d335caSWen Congyang 
186560a5a19eSTang Chen 	try_offline_node(nid);
186660a5a19eSTang Chen 
18676677e3eaSYasuaki Ishimatsu 	unlock_memory_hotplug();
18686677e3eaSYasuaki Ishimatsu 
1869e90bdb7fSWen Congyang 	return 0;
187071088785SBadari Pulavarty }
187148e94196SKAMEZAWA Hiroyuki #else
1872a16cee10SWen Congyang int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1873a16cee10SWen Congyang {
1874a16cee10SWen Congyang 	return -EINVAL;
1875a16cee10SWen Congyang }
187660a5a19eSTang Chen int remove_memory(int nid, u64 start, u64 size)
187748e94196SKAMEZAWA Hiroyuki {
187848e94196SKAMEZAWA Hiroyuki 	return -EINVAL;
187948e94196SKAMEZAWA Hiroyuki }
18800c0e6195SKAMEZAWA Hiroyuki #endif /* CONFIG_MEMORY_HOTREMOVE */
188171088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
1882