xref: /openbmc/linux/mm/memory_hotplug.c (revision 925268a06dc2b1ff7bfcc37419a6827a0e739639)
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>
143947be19SDave Hansen #include <linux/module.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>
323947be19SDave Hansen 
333947be19SDave Hansen #include <asm/tlbflush.h>
343947be19SDave Hansen 
351e5ad9a3SAdrian Bunk #include "internal.h"
361e5ad9a3SAdrian Bunk 
3720d6c96bSKOSAKI Motohiro DEFINE_MUTEX(mem_hotplug_mutex);
3820d6c96bSKOSAKI Motohiro 
3920d6c96bSKOSAKI Motohiro void lock_memory_hotplug(void)
4020d6c96bSKOSAKI Motohiro {
4120d6c96bSKOSAKI Motohiro 	mutex_lock(&mem_hotplug_mutex);
4220d6c96bSKOSAKI Motohiro 
4320d6c96bSKOSAKI Motohiro 	/* for exclusive hibernation if CONFIG_HIBERNATION=y */
4420d6c96bSKOSAKI Motohiro 	lock_system_sleep();
4520d6c96bSKOSAKI Motohiro }
4620d6c96bSKOSAKI Motohiro 
4720d6c96bSKOSAKI Motohiro void unlock_memory_hotplug(void)
4820d6c96bSKOSAKI Motohiro {
4920d6c96bSKOSAKI Motohiro 	unlock_system_sleep();
5020d6c96bSKOSAKI Motohiro 	mutex_unlock(&mem_hotplug_mutex);
5120d6c96bSKOSAKI Motohiro }
5220d6c96bSKOSAKI Motohiro 
5320d6c96bSKOSAKI Motohiro 
5445e0b78bSKeith Mannthey /* add this memory to iomem resource */
5545e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size)
5645e0b78bSKeith Mannthey {
5745e0b78bSKeith Mannthey 	struct resource *res;
5845e0b78bSKeith Mannthey 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
5945e0b78bSKeith Mannthey 	BUG_ON(!res);
6045e0b78bSKeith Mannthey 
6145e0b78bSKeith Mannthey 	res->name = "System RAM";
6245e0b78bSKeith Mannthey 	res->start = start;
6345e0b78bSKeith Mannthey 	res->end = start + size - 1;
64887c3cb1SYasunori Goto 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
6545e0b78bSKeith Mannthey 	if (request_resource(&iomem_resource, res) < 0) {
6645e0b78bSKeith Mannthey 		printk("System RAM resource %llx - %llx cannot be added\n",
6745e0b78bSKeith Mannthey 		(unsigned long long)res->start, (unsigned long long)res->end);
6845e0b78bSKeith Mannthey 		kfree(res);
6945e0b78bSKeith Mannthey 		res = NULL;
7045e0b78bSKeith Mannthey 	}
7145e0b78bSKeith Mannthey 	return res;
7245e0b78bSKeith Mannthey }
7345e0b78bSKeith Mannthey 
7445e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res)
7545e0b78bSKeith Mannthey {
7645e0b78bSKeith Mannthey 	if (!res)
7745e0b78bSKeith Mannthey 		return;
7845e0b78bSKeith Mannthey 	release_resource(res);
7945e0b78bSKeith Mannthey 	kfree(res);
8045e0b78bSKeith Mannthey 	return;
8145e0b78bSKeith Mannthey }
8245e0b78bSKeith Mannthey 
8353947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
8404753278SYasunori Goto #ifndef CONFIG_SPARSEMEM_VMEMMAP
85af370fb8SYasunori Goto static void get_page_bootmem(unsigned long info,  struct page *page, int type)
8604753278SYasunori Goto {
87af370fb8SYasunori Goto 	atomic_set(&page->_mapcount, type);
8804753278SYasunori Goto 	SetPagePrivate(page);
8904753278SYasunori Goto 	set_page_private(page, info);
9004753278SYasunori Goto 	atomic_inc(&page->_count);
9104753278SYasunori Goto }
9204753278SYasunori Goto 
9323ce932aSRakib Mullick /* reference to __meminit __free_pages_bootmem is valid
9423ce932aSRakib Mullick  * so use __ref to tell modpost not to generate a warning */
9523ce932aSRakib Mullick void __ref put_page_bootmem(struct page *page)
9604753278SYasunori Goto {
97af370fb8SYasunori Goto 	int type;
9804753278SYasunori Goto 
99af370fb8SYasunori Goto 	type = atomic_read(&page->_mapcount);
100af370fb8SYasunori Goto 	BUG_ON(type >= -1);
10104753278SYasunori Goto 
10204753278SYasunori Goto 	if (atomic_dec_return(&page->_count) == 1) {
10304753278SYasunori Goto 		ClearPagePrivate(page);
10404753278SYasunori Goto 		set_page_private(page, 0);
10504753278SYasunori Goto 		reset_page_mapcount(page);
10604753278SYasunori Goto 		__free_pages_bootmem(page, 0);
10704753278SYasunori Goto 	}
10804753278SYasunori Goto 
10904753278SYasunori Goto }
11004753278SYasunori Goto 
111d92bc318SAdrian Bunk static void register_page_bootmem_info_section(unsigned long start_pfn)
11204753278SYasunori Goto {
11304753278SYasunori Goto 	unsigned long *usemap, mapsize, section_nr, i;
11404753278SYasunori Goto 	struct mem_section *ms;
11504753278SYasunori Goto 	struct page *page, *memmap;
11604753278SYasunori Goto 
11704753278SYasunori Goto 	if (!pfn_valid(start_pfn))
11804753278SYasunori Goto 		return;
11904753278SYasunori Goto 
12004753278SYasunori Goto 	section_nr = pfn_to_section_nr(start_pfn);
12104753278SYasunori Goto 	ms = __nr_to_section(section_nr);
12204753278SYasunori Goto 
12304753278SYasunori Goto 	/* Get section's memmap address */
12404753278SYasunori Goto 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
12504753278SYasunori Goto 
12604753278SYasunori Goto 	/*
12704753278SYasunori Goto 	 * Get page for the memmap's phys address
12804753278SYasunori Goto 	 * XXX: need more consideration for sparse_vmemmap...
12904753278SYasunori Goto 	 */
13004753278SYasunori Goto 	page = virt_to_page(memmap);
13104753278SYasunori Goto 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
13204753278SYasunori Goto 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
13304753278SYasunori Goto 
13404753278SYasunori Goto 	/* remember memmap's page */
13504753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
13604753278SYasunori Goto 		get_page_bootmem(section_nr, page, SECTION_INFO);
13704753278SYasunori Goto 
13804753278SYasunori Goto 	usemap = __nr_to_section(section_nr)->pageblock_flags;
13904753278SYasunori Goto 	page = virt_to_page(usemap);
14004753278SYasunori Goto 
14104753278SYasunori Goto 	mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
14204753278SYasunori Goto 
14304753278SYasunori Goto 	for (i = 0; i < mapsize; i++, page++)
144af370fb8SYasunori Goto 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
14504753278SYasunori Goto 
14604753278SYasunori Goto }
14704753278SYasunori Goto 
14804753278SYasunori Goto void register_page_bootmem_info_node(struct pglist_data *pgdat)
14904753278SYasunori Goto {
15004753278SYasunori Goto 	unsigned long i, pfn, end_pfn, nr_pages;
15104753278SYasunori Goto 	int node = pgdat->node_id;
15204753278SYasunori Goto 	struct page *page;
15304753278SYasunori Goto 	struct zone *zone;
15404753278SYasunori Goto 
15504753278SYasunori Goto 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
15604753278SYasunori Goto 	page = virt_to_page(pgdat);
15704753278SYasunori Goto 
15804753278SYasunori Goto 	for (i = 0; i < nr_pages; i++, page++)
15904753278SYasunori Goto 		get_page_bootmem(node, page, NODE_INFO);
16004753278SYasunori Goto 
16104753278SYasunori Goto 	zone = &pgdat->node_zones[0];
16204753278SYasunori Goto 	for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
16304753278SYasunori Goto 		if (zone->wait_table) {
16404753278SYasunori Goto 			nr_pages = zone->wait_table_hash_nr_entries
16504753278SYasunori Goto 				* sizeof(wait_queue_head_t);
16604753278SYasunori Goto 			nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
16704753278SYasunori Goto 			page = virt_to_page(zone->wait_table);
16804753278SYasunori Goto 
16904753278SYasunori Goto 			for (i = 0; i < nr_pages; i++, page++)
17004753278SYasunori Goto 				get_page_bootmem(node, page, NODE_INFO);
17104753278SYasunori Goto 		}
17204753278SYasunori Goto 	}
17304753278SYasunori Goto 
17404753278SYasunori Goto 	pfn = pgdat->node_start_pfn;
17504753278SYasunori Goto 	end_pfn = pfn + pgdat->node_spanned_pages;
17604753278SYasunori Goto 
17704753278SYasunori Goto 	/* register_section info */
17804753278SYasunori Goto 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
17904753278SYasunori Goto 		register_page_bootmem_info_section(pfn);
18004753278SYasunori Goto 
18104753278SYasunori Goto }
18204753278SYasunori Goto #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
18304753278SYasunori Goto 
18476cdd58eSHeiko Carstens static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
18576cdd58eSHeiko Carstens 			   unsigned long end_pfn)
18676cdd58eSHeiko Carstens {
18776cdd58eSHeiko Carstens 	unsigned long old_zone_end_pfn;
18876cdd58eSHeiko Carstens 
18976cdd58eSHeiko Carstens 	zone_span_writelock(zone);
19076cdd58eSHeiko Carstens 
19176cdd58eSHeiko Carstens 	old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
19276cdd58eSHeiko Carstens 	if (start_pfn < zone->zone_start_pfn)
19376cdd58eSHeiko Carstens 		zone->zone_start_pfn = start_pfn;
19476cdd58eSHeiko Carstens 
19576cdd58eSHeiko Carstens 	zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
19676cdd58eSHeiko Carstens 				zone->zone_start_pfn;
19776cdd58eSHeiko Carstens 
19876cdd58eSHeiko Carstens 	zone_span_writeunlock(zone);
19976cdd58eSHeiko Carstens }
20076cdd58eSHeiko Carstens 
20176cdd58eSHeiko Carstens static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
20276cdd58eSHeiko Carstens 			    unsigned long end_pfn)
20376cdd58eSHeiko Carstens {
20476cdd58eSHeiko Carstens 	unsigned long old_pgdat_end_pfn =
20576cdd58eSHeiko Carstens 		pgdat->node_start_pfn + pgdat->node_spanned_pages;
20676cdd58eSHeiko Carstens 
20776cdd58eSHeiko Carstens 	if (start_pfn < pgdat->node_start_pfn)
20876cdd58eSHeiko Carstens 		pgdat->node_start_pfn = start_pfn;
20976cdd58eSHeiko Carstens 
21076cdd58eSHeiko Carstens 	pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
21176cdd58eSHeiko Carstens 					pgdat->node_start_pfn;
21276cdd58eSHeiko Carstens }
21376cdd58eSHeiko Carstens 
21431168481SAl Viro static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
2153947be19SDave Hansen {
2163947be19SDave Hansen 	struct pglist_data *pgdat = zone->zone_pgdat;
2173947be19SDave Hansen 	int nr_pages = PAGES_PER_SECTION;
2183947be19SDave Hansen 	int nid = pgdat->node_id;
2193947be19SDave Hansen 	int zone_type;
22076cdd58eSHeiko Carstens 	unsigned long flags;
2213947be19SDave Hansen 
2223947be19SDave Hansen 	zone_type = zone - pgdat->node_zones;
22376cdd58eSHeiko Carstens 	if (!zone->wait_table) {
22476cdd58eSHeiko Carstens 		int ret;
22576cdd58eSHeiko Carstens 
22676cdd58eSHeiko Carstens 		ret = init_currently_empty_zone(zone, phys_start_pfn,
227a2f3aa02SDave Hansen 						nr_pages, MEMMAP_HOTPLUG);
22876cdd58eSHeiko Carstens 		if (ret)
22976cdd58eSHeiko Carstens 			return ret;
23076cdd58eSHeiko Carstens 	}
23176cdd58eSHeiko Carstens 	pgdat_resize_lock(zone->zone_pgdat, &flags);
23276cdd58eSHeiko Carstens 	grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
23376cdd58eSHeiko Carstens 	grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
23476cdd58eSHeiko Carstens 			phys_start_pfn + nr_pages);
23576cdd58eSHeiko Carstens 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
236a2f3aa02SDave Hansen 	memmap_init_zone(nr_pages, nid, zone_type,
237a2f3aa02SDave Hansen 			 phys_start_pfn, MEMMAP_HOTPLUG);
238718127ccSYasunori Goto 	return 0;
2393947be19SDave Hansen }
2403947be19SDave Hansen 
241c04fc586SGary Hade static int __meminit __add_section(int nid, struct zone *zone,
242c04fc586SGary Hade 					unsigned long phys_start_pfn)
2433947be19SDave Hansen {
2443947be19SDave Hansen 	int nr_pages = PAGES_PER_SECTION;
2453947be19SDave Hansen 	int ret;
2463947be19SDave Hansen 
247ebd15302SKAMEZAWA Hiroyuki 	if (pfn_valid(phys_start_pfn))
248ebd15302SKAMEZAWA Hiroyuki 		return -EEXIST;
249ebd15302SKAMEZAWA Hiroyuki 
2500b0acbecSDave Hansen 	ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
2513947be19SDave Hansen 
2523947be19SDave Hansen 	if (ret < 0)
2533947be19SDave Hansen 		return ret;
2543947be19SDave Hansen 
255718127ccSYasunori Goto 	ret = __add_zone(zone, phys_start_pfn);
256718127ccSYasunori Goto 
257718127ccSYasunori Goto 	if (ret < 0)
258718127ccSYasunori Goto 		return ret;
259718127ccSYasunori Goto 
260c04fc586SGary Hade 	return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
2613947be19SDave Hansen }
2623947be19SDave Hansen 
2630c0a4a51SYasunori Goto #ifdef CONFIG_SPARSEMEM_VMEMMAP
2640c0a4a51SYasunori Goto static int __remove_section(struct zone *zone, struct mem_section *ms)
2650c0a4a51SYasunori Goto {
2660c0a4a51SYasunori Goto 	/*
2670c0a4a51SYasunori Goto 	 * XXX: Freeing memmap with vmemmap is not implement yet.
2680c0a4a51SYasunori Goto 	 *      This should be removed later.
2690c0a4a51SYasunori Goto 	 */
2700c0a4a51SYasunori Goto 	return -EBUSY;
2710c0a4a51SYasunori Goto }
2720c0a4a51SYasunori Goto #else
273ea01ea93SBadari Pulavarty static int __remove_section(struct zone *zone, struct mem_section *ms)
274ea01ea93SBadari Pulavarty {
275ea01ea93SBadari Pulavarty 	unsigned long flags;
276ea01ea93SBadari Pulavarty 	struct pglist_data *pgdat = zone->zone_pgdat;
277ea01ea93SBadari Pulavarty 	int ret = -EINVAL;
278ea01ea93SBadari Pulavarty 
279ea01ea93SBadari Pulavarty 	if (!valid_section(ms))
280ea01ea93SBadari Pulavarty 		return ret;
281ea01ea93SBadari Pulavarty 
282ea01ea93SBadari Pulavarty 	ret = unregister_memory_section(ms);
283ea01ea93SBadari Pulavarty 	if (ret)
284ea01ea93SBadari Pulavarty 		return ret;
285ea01ea93SBadari Pulavarty 
286ea01ea93SBadari Pulavarty 	pgdat_resize_lock(pgdat, &flags);
287ea01ea93SBadari Pulavarty 	sparse_remove_one_section(zone, ms);
288ea01ea93SBadari Pulavarty 	pgdat_resize_unlock(pgdat, &flags);
289ea01ea93SBadari Pulavarty 	return 0;
290ea01ea93SBadari Pulavarty }
2910c0a4a51SYasunori Goto #endif
292ea01ea93SBadari Pulavarty 
2933947be19SDave Hansen /*
2943947be19SDave Hansen  * Reasonably generic function for adding memory.  It is
2953947be19SDave Hansen  * expected that archs that support memory hotplug will
2963947be19SDave Hansen  * call this function after deciding the zone to which to
2973947be19SDave Hansen  * add the new pages.
2983947be19SDave Hansen  */
299c04fc586SGary Hade int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
3003947be19SDave Hansen 			unsigned long nr_pages)
3013947be19SDave Hansen {
3023947be19SDave Hansen 	unsigned long i;
3033947be19SDave Hansen 	int err = 0;
3046f712711SKAMEZAWA Hiroyuki 	int start_sec, end_sec;
3056f712711SKAMEZAWA Hiroyuki 	/* during initialize mem_map, align hot-added range to section */
3066f712711SKAMEZAWA Hiroyuki 	start_sec = pfn_to_section_nr(phys_start_pfn);
3076f712711SKAMEZAWA Hiroyuki 	end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
3083947be19SDave Hansen 
3096f712711SKAMEZAWA Hiroyuki 	for (i = start_sec; i <= end_sec; i++) {
310c04fc586SGary Hade 		err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
3113947be19SDave Hansen 
3126f712711SKAMEZAWA Hiroyuki 		/*
313183ff22bSSimon Arlott 		 * EEXIST is finally dealt with by ioresource collision
3146f712711SKAMEZAWA Hiroyuki 		 * check. see add_memory() => register_memory_resource()
3156f712711SKAMEZAWA Hiroyuki 		 * Warning will be printed if there is collision.
316bed120c6SJoel H Schopp 		 */
317bed120c6SJoel H Schopp 		if (err && (err != -EEXIST))
3183947be19SDave Hansen 			break;
3196f712711SKAMEZAWA Hiroyuki 		err = 0;
3203947be19SDave Hansen 	}
3213947be19SDave Hansen 
3223947be19SDave Hansen 	return err;
3233947be19SDave Hansen }
324bed120c6SJoel H Schopp EXPORT_SYMBOL_GPL(__add_pages);
3253947be19SDave Hansen 
326ea01ea93SBadari Pulavarty /**
327ea01ea93SBadari Pulavarty  * __remove_pages() - remove sections of pages from a zone
328ea01ea93SBadari Pulavarty  * @zone: zone from which pages need to be removed
329ea01ea93SBadari Pulavarty  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
330ea01ea93SBadari Pulavarty  * @nr_pages: number of pages to remove (must be multiple of section size)
331ea01ea93SBadari Pulavarty  *
332ea01ea93SBadari Pulavarty  * Generic helper function to remove section mappings and sysfs entries
333ea01ea93SBadari Pulavarty  * for the section of the memory we are removing. Caller needs to make
334ea01ea93SBadari Pulavarty  * sure that pages are marked reserved and zones are adjust properly by
335ea01ea93SBadari Pulavarty  * calling offline_pages().
336ea01ea93SBadari Pulavarty  */
337ea01ea93SBadari Pulavarty int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
338ea01ea93SBadari Pulavarty 		 unsigned long nr_pages)
339ea01ea93SBadari Pulavarty {
340ea01ea93SBadari Pulavarty 	unsigned long i, ret = 0;
341ea01ea93SBadari Pulavarty 	int sections_to_remove;
342ea01ea93SBadari Pulavarty 
343ea01ea93SBadari Pulavarty 	/*
344ea01ea93SBadari Pulavarty 	 * We can only remove entire sections
345ea01ea93SBadari Pulavarty 	 */
346ea01ea93SBadari Pulavarty 	BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
347ea01ea93SBadari Pulavarty 	BUG_ON(nr_pages % PAGES_PER_SECTION);
348ea01ea93SBadari Pulavarty 
349ea01ea93SBadari Pulavarty 	sections_to_remove = nr_pages / PAGES_PER_SECTION;
350ea01ea93SBadari Pulavarty 	for (i = 0; i < sections_to_remove; i++) {
351ea01ea93SBadari Pulavarty 		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
352de7f0cbaSNathan Fontenot 		release_mem_region(pfn << PAGE_SHIFT,
353de7f0cbaSNathan Fontenot 				   PAGES_PER_SECTION << PAGE_SHIFT);
354ea01ea93SBadari Pulavarty 		ret = __remove_section(zone, __pfn_to_section(pfn));
355ea01ea93SBadari Pulavarty 		if (ret)
356ea01ea93SBadari Pulavarty 			break;
357ea01ea93SBadari Pulavarty 	}
358ea01ea93SBadari Pulavarty 	return ret;
359ea01ea93SBadari Pulavarty }
360ea01ea93SBadari Pulavarty EXPORT_SYMBOL_GPL(__remove_pages);
361ea01ea93SBadari Pulavarty 
362180c06efSJeremy Fitzhardinge void online_page(struct page *page)
363180c06efSJeremy Fitzhardinge {
3644738e1b9SJan Beulich 	unsigned long pfn = page_to_pfn(page);
3654738e1b9SJan Beulich 
366180c06efSJeremy Fitzhardinge 	totalram_pages++;
3674738e1b9SJan Beulich 	if (pfn >= num_physpages)
3684738e1b9SJan Beulich 		num_physpages = pfn + 1;
369180c06efSJeremy Fitzhardinge 
370180c06efSJeremy Fitzhardinge #ifdef CONFIG_HIGHMEM
371180c06efSJeremy Fitzhardinge 	if (PageHighMem(page))
372180c06efSJeremy Fitzhardinge 		totalhigh_pages++;
373180c06efSJeremy Fitzhardinge #endif
374180c06efSJeremy Fitzhardinge 
375180c06efSJeremy Fitzhardinge #ifdef CONFIG_FLATMEM
376180c06efSJeremy Fitzhardinge 	max_mapnr = max(page_to_pfn(page), max_mapnr);
377180c06efSJeremy Fitzhardinge #endif
378180c06efSJeremy Fitzhardinge 
379180c06efSJeremy Fitzhardinge 	ClearPageReserved(page);
380180c06efSJeremy Fitzhardinge 	init_page_count(page);
381180c06efSJeremy Fitzhardinge 	__free_page(page);
382180c06efSJeremy Fitzhardinge }
383180c06efSJeremy Fitzhardinge 
38475884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
38575884fb1SKAMEZAWA Hiroyuki 			void *arg)
3863947be19SDave Hansen {
3873947be19SDave Hansen 	unsigned long i;
38875884fb1SKAMEZAWA Hiroyuki 	unsigned long onlined_pages = *(unsigned long *)arg;
38975884fb1SKAMEZAWA Hiroyuki 	struct page *page;
39075884fb1SKAMEZAWA Hiroyuki 	if (PageReserved(pfn_to_page(start_pfn)))
39175884fb1SKAMEZAWA Hiroyuki 		for (i = 0; i < nr_pages; i++) {
39275884fb1SKAMEZAWA Hiroyuki 			page = pfn_to_page(start_pfn + i);
39375884fb1SKAMEZAWA Hiroyuki 			online_page(page);
39475884fb1SKAMEZAWA Hiroyuki 			onlined_pages++;
39575884fb1SKAMEZAWA Hiroyuki 		}
39675884fb1SKAMEZAWA Hiroyuki 	*(unsigned long *)arg = onlined_pages;
39775884fb1SKAMEZAWA Hiroyuki 	return 0;
39875884fb1SKAMEZAWA Hiroyuki }
39975884fb1SKAMEZAWA Hiroyuki 
40075884fb1SKAMEZAWA Hiroyuki 
40175884fb1SKAMEZAWA Hiroyuki int online_pages(unsigned long pfn, unsigned long nr_pages)
40275884fb1SKAMEZAWA Hiroyuki {
4033947be19SDave Hansen 	unsigned long onlined_pages = 0;
4043947be19SDave Hansen 	struct zone *zone;
4056811378eSYasunori Goto 	int need_zonelists_rebuild = 0;
4067b78d335SYasunori Goto 	int nid;
4077b78d335SYasunori Goto 	int ret;
4087b78d335SYasunori Goto 	struct memory_notify arg;
4093947be19SDave Hansen 
410*925268a0SKAMEZAWA Hiroyuki 	lock_memory_hotplug();
4117b78d335SYasunori Goto 	arg.start_pfn = pfn;
4127b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
4137b78d335SYasunori Goto 	arg.status_change_nid = -1;
4147b78d335SYasunori Goto 
4157b78d335SYasunori Goto 	nid = page_to_nid(pfn_to_page(pfn));
4167b78d335SYasunori Goto 	if (node_present_pages(nid) == 0)
4177b78d335SYasunori Goto 		arg.status_change_nid = nid;
4187b78d335SYasunori Goto 
4197b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
4207b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
4217b78d335SYasunori Goto 	if (ret) {
4227b78d335SYasunori Goto 		memory_notify(MEM_CANCEL_ONLINE, &arg);
423*925268a0SKAMEZAWA Hiroyuki 		unlock_memory_hotplug();
4247b78d335SYasunori Goto 		return ret;
4257b78d335SYasunori Goto 	}
4263947be19SDave Hansen 	/*
4273947be19SDave Hansen 	 * This doesn't need a lock to do pfn_to_page().
4283947be19SDave Hansen 	 * The section can't be removed here because of the
429da19cbcfSDaniel Walker 	 * memory_block->state_mutex.
4303947be19SDave Hansen 	 */
4313947be19SDave Hansen 	zone = page_zone(pfn_to_page(pfn));
4326811378eSYasunori Goto 	/*
4336811378eSYasunori Goto 	 * If this zone is not populated, then it is not in zonelist.
4346811378eSYasunori Goto 	 * This means the page allocator ignores this zone.
4356811378eSYasunori Goto 	 * So, zonelist must be updated after online.
4366811378eSYasunori Goto 	 */
4374eaf3f64SHaicheng Li 	mutex_lock(&zonelists_mutex);
4386811378eSYasunori Goto 	if (!populated_zone(zone))
4396811378eSYasunori Goto 		need_zonelists_rebuild = 1;
4406811378eSYasunori Goto 
441908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
44275884fb1SKAMEZAWA Hiroyuki 		online_pages_range);
443fd8a4221SGeoff Levand 	if (ret) {
4444eaf3f64SHaicheng Li 		mutex_unlock(&zonelists_mutex);
445fd8a4221SGeoff Levand 		printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
446fd8a4221SGeoff Levand 			nr_pages, pfn);
447fd8a4221SGeoff Levand 		memory_notify(MEM_CANCEL_ONLINE, &arg);
448*925268a0SKAMEZAWA Hiroyuki 		unlock_memory_hotplug();
449fd8a4221SGeoff Levand 		return ret;
450fd8a4221SGeoff Levand 	}
451fd8a4221SGeoff Levand 
4523947be19SDave Hansen 	zone->present_pages += onlined_pages;
453f2937be5SYasunori Goto 	zone->zone_pgdat->node_present_pages += onlined_pages;
4541f522509SHaicheng Li 	if (need_zonelists_rebuild)
4551f522509SHaicheng Li 		build_all_zonelists(zone);
4561f522509SHaicheng Li 	else
457112067f0SShaohua Li 		zone_pcp_update(zone);
4581f522509SHaicheng Li 
4594eaf3f64SHaicheng Li 	mutex_unlock(&zonelists_mutex);
460bc75d33fSMinchan Kim 	setup_per_zone_wmarks();
461bce7394aSMinchan Kim 	calculate_zone_inactive_ratio(zone);
4627ea1530aSChristoph Lameter 	if (onlined_pages) {
4637ea1530aSChristoph Lameter 		kswapd_run(zone_to_nid(zone));
4647ea1530aSChristoph Lameter 		node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
4657ea1530aSChristoph Lameter 	}
46661b13993SDave Hansen 
4675a4d4361SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
4682f7f24ecSKent Liu 
4692d1d43f6SChandra Seetharaman 	writeback_set_ratelimit();
4707b78d335SYasunori Goto 
4717b78d335SYasunori Goto 	if (onlined_pages)
4727b78d335SYasunori Goto 		memory_notify(MEM_ONLINE, &arg);
473*925268a0SKAMEZAWA Hiroyuki 	unlock_memory_hotplug();
4747b78d335SYasunori Goto 
4753947be19SDave Hansen 	return 0;
4763947be19SDave Hansen }
47753947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
478bc02af93SYasunori Goto 
479e1319331SHidetoshi Seto /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
480e1319331SHidetoshi Seto static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
4819af3c2deSYasunori Goto {
4829af3c2deSYasunori Goto 	struct pglist_data *pgdat;
4839af3c2deSYasunori Goto 	unsigned long zones_size[MAX_NR_ZONES] = {0};
4849af3c2deSYasunori Goto 	unsigned long zholes_size[MAX_NR_ZONES] = {0};
4859af3c2deSYasunori Goto 	unsigned long start_pfn = start >> PAGE_SHIFT;
4869af3c2deSYasunori Goto 
4879af3c2deSYasunori Goto 	pgdat = arch_alloc_nodedata(nid);
4889af3c2deSYasunori Goto 	if (!pgdat)
4899af3c2deSYasunori Goto 		return NULL;
4909af3c2deSYasunori Goto 
4919af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, pgdat);
4929af3c2deSYasunori Goto 
4939af3c2deSYasunori Goto 	/* we can use NODE_DATA(nid) from here */
4949af3c2deSYasunori Goto 
4959af3c2deSYasunori Goto 	/* init node's zones as empty zones, we don't have any present pages.*/
4969109fb7bSJohannes Weiner 	free_area_init_node(nid, zones_size, start_pfn, zholes_size);
4979af3c2deSYasunori Goto 
4989af3c2deSYasunori Goto 	return pgdat;
4999af3c2deSYasunori Goto }
5009af3c2deSYasunori Goto 
5019af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
5029af3c2deSYasunori Goto {
5039af3c2deSYasunori Goto 	arch_refresh_nodedata(nid, NULL);
5049af3c2deSYasunori Goto 	arch_free_nodedata(pgdat);
5059af3c2deSYasunori Goto 	return;
5069af3c2deSYasunori Goto }
5079af3c2deSYasunori Goto 
5080a547039SKAMEZAWA Hiroyuki 
509cf23422bSminskey guo /*
510cf23422bSminskey guo  * called by cpu_up() to online a node without onlined memory.
511cf23422bSminskey guo  */
512cf23422bSminskey guo int mem_online_node(int nid)
513cf23422bSminskey guo {
514cf23422bSminskey guo 	pg_data_t	*pgdat;
515cf23422bSminskey guo 	int	ret;
516cf23422bSminskey guo 
51720d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
518cf23422bSminskey guo 	pgdat = hotadd_new_pgdat(nid, 0);
519cf23422bSminskey guo 	if (pgdat) {
520cf23422bSminskey guo 		ret = -ENOMEM;
521cf23422bSminskey guo 		goto out;
522cf23422bSminskey guo 	}
523cf23422bSminskey guo 	node_set_online(nid);
524cf23422bSminskey guo 	ret = register_one_node(nid);
525cf23422bSminskey guo 	BUG_ON(ret);
526cf23422bSminskey guo 
527cf23422bSminskey guo out:
52820d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
529cf23422bSminskey guo 	return ret;
530cf23422bSminskey guo }
531cf23422bSminskey guo 
53231168481SAl Viro /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
53331168481SAl Viro int __ref add_memory(int nid, u64 start, u64 size)
534bc02af93SYasunori Goto {
5359af3c2deSYasunori Goto 	pg_data_t *pgdat = NULL;
5369af3c2deSYasunori Goto 	int new_pgdat = 0;
537ebd15302SKAMEZAWA Hiroyuki 	struct resource *res;
538bc02af93SYasunori Goto 	int ret;
539bc02af93SYasunori Goto 
54020d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
5416ad696d2SAndi Kleen 
542ebd15302SKAMEZAWA Hiroyuki 	res = register_memory_resource(start, size);
5436ad696d2SAndi Kleen 	ret = -EEXIST;
544ebd15302SKAMEZAWA Hiroyuki 	if (!res)
5456ad696d2SAndi Kleen 		goto out;
546ebd15302SKAMEZAWA Hiroyuki 
5479af3c2deSYasunori Goto 	if (!node_online(nid)) {
5489af3c2deSYasunori Goto 		pgdat = hotadd_new_pgdat(nid, start);
5496ad696d2SAndi Kleen 		ret = -ENOMEM;
5509af3c2deSYasunori Goto 		if (!pgdat)
5516ad696d2SAndi Kleen 			goto out;
5529af3c2deSYasunori Goto 		new_pgdat = 1;
5539af3c2deSYasunori Goto 	}
5549af3c2deSYasunori Goto 
555bc02af93SYasunori Goto 	/* call arch's memory hotadd */
556bc02af93SYasunori Goto 	ret = arch_add_memory(nid, start, size);
557bc02af93SYasunori Goto 
5589af3c2deSYasunori Goto 	if (ret < 0)
5599af3c2deSYasunori Goto 		goto error;
5609af3c2deSYasunori Goto 
5610fc44159SYasunori Goto 	/* we online node here. we can't roll back from here. */
5629af3c2deSYasunori Goto 	node_set_online(nid);
5639af3c2deSYasunori Goto 
5640fc44159SYasunori Goto 	if (new_pgdat) {
5650fc44159SYasunori Goto 		ret = register_one_node(nid);
5660fc44159SYasunori Goto 		/*
5670fc44159SYasunori Goto 		 * If sysfs file of new node can't create, cpu on the node
5680fc44159SYasunori Goto 		 * can't be hot-added. There is no rollback way now.
5690fc44159SYasunori Goto 		 * So, check by BUG_ON() to catch it reluctantly..
5700fc44159SYasunori Goto 		 */
5710fc44159SYasunori Goto 		BUG_ON(ret);
5720fc44159SYasunori Goto 	}
5730fc44159SYasunori Goto 
574d96ae530Sakpm@linux-foundation.org 	/* create new memmap entry */
575d96ae530Sakpm@linux-foundation.org 	firmware_map_add_hotplug(start, start + size, "System RAM");
576d96ae530Sakpm@linux-foundation.org 
5776ad696d2SAndi Kleen 	goto out;
5786ad696d2SAndi Kleen 
5799af3c2deSYasunori Goto error:
5809af3c2deSYasunori Goto 	/* rollback pgdat allocation and others */
5819af3c2deSYasunori Goto 	if (new_pgdat)
5829af3c2deSYasunori Goto 		rollback_node_hotadd(nid, pgdat);
583ebd15302SKAMEZAWA Hiroyuki 	if (res)
584ebd15302SKAMEZAWA Hiroyuki 		release_memory_resource(res);
5859af3c2deSYasunori Goto 
5866ad696d2SAndi Kleen out:
58720d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
588bc02af93SYasunori Goto 	return ret;
589bc02af93SYasunori Goto }
590bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory);
5910c0e6195SKAMEZAWA Hiroyuki 
5920c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_MEMORY_HOTREMOVE
5930c0e6195SKAMEZAWA Hiroyuki /*
5945c755e9fSBadari Pulavarty  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
5955c755e9fSBadari Pulavarty  * set and the size of the free page is given by page_order(). Using this,
5965c755e9fSBadari Pulavarty  * the function determines if the pageblock contains only free pages.
5975c755e9fSBadari Pulavarty  * Due to buddy contraints, a free page at least the size of a pageblock will
5985c755e9fSBadari Pulavarty  * be located at the start of the pageblock
5995c755e9fSBadari Pulavarty  */
6005c755e9fSBadari Pulavarty static inline int pageblock_free(struct page *page)
6015c755e9fSBadari Pulavarty {
6025c755e9fSBadari Pulavarty 	return PageBuddy(page) && page_order(page) >= pageblock_order;
6035c755e9fSBadari Pulavarty }
6045c755e9fSBadari Pulavarty 
6055c755e9fSBadari Pulavarty /* Return the start of the next active pageblock after a given page */
6065c755e9fSBadari Pulavarty static struct page *next_active_pageblock(struct page *page)
6075c755e9fSBadari Pulavarty {
6085c755e9fSBadari Pulavarty 	/* Ensure the starting page is pageblock-aligned */
6095c755e9fSBadari Pulavarty 	BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
6105c755e9fSBadari Pulavarty 
6115c755e9fSBadari Pulavarty 	/* If the entire pageblock is free, move to the end of free page */
6120dcc48c1SKAMEZAWA Hiroyuki 	if (pageblock_free(page)) {
6130dcc48c1SKAMEZAWA Hiroyuki 		int order;
6140dcc48c1SKAMEZAWA Hiroyuki 		/* be careful. we don't have locks, page_order can be changed.*/
6150dcc48c1SKAMEZAWA Hiroyuki 		order = page_order(page);
6160dcc48c1SKAMEZAWA Hiroyuki 		if ((order < MAX_ORDER) && (order >= pageblock_order))
6170dcc48c1SKAMEZAWA Hiroyuki 			return page + (1 << order);
6180dcc48c1SKAMEZAWA Hiroyuki 	}
6195c755e9fSBadari Pulavarty 
6200dcc48c1SKAMEZAWA Hiroyuki 	return page + pageblock_nr_pages;
6215c755e9fSBadari Pulavarty }
6225c755e9fSBadari Pulavarty 
6235c755e9fSBadari Pulavarty /* Checks if this range of memory is likely to be hot-removable. */
6245c755e9fSBadari Pulavarty int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
6255c755e9fSBadari Pulavarty {
6265c755e9fSBadari Pulavarty 	struct page *page = pfn_to_page(start_pfn);
6275c755e9fSBadari Pulavarty 	struct page *end_page = page + nr_pages;
6285c755e9fSBadari Pulavarty 
6295c755e9fSBadari Pulavarty 	/* Check the starting page of each pageblock within the range */
6305c755e9fSBadari Pulavarty 	for (; page < end_page; page = next_active_pageblock(page)) {
63149ac8255SKAMEZAWA Hiroyuki 		if (!is_pageblock_removable_nolock(page))
6325c755e9fSBadari Pulavarty 			return 0;
63349ac8255SKAMEZAWA Hiroyuki 		cond_resched();
6345c755e9fSBadari Pulavarty 	}
6355c755e9fSBadari Pulavarty 
6365c755e9fSBadari Pulavarty 	/* All pageblocks in the memory block are likely to be hot-removable */
6375c755e9fSBadari Pulavarty 	return 1;
6385c755e9fSBadari Pulavarty }
6395c755e9fSBadari Pulavarty 
6405c755e9fSBadari Pulavarty /*
6410c0e6195SKAMEZAWA Hiroyuki  * Confirm all pages in a range [start, end) is belongs to the same zone.
6420c0e6195SKAMEZAWA Hiroyuki  */
6430c0e6195SKAMEZAWA Hiroyuki static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
6440c0e6195SKAMEZAWA Hiroyuki {
6450c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
6460c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone = NULL;
6470c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
6480c0e6195SKAMEZAWA Hiroyuki 	int i;
6490c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start_pfn;
6500c0e6195SKAMEZAWA Hiroyuki 	     pfn < end_pfn;
6510c0e6195SKAMEZAWA Hiroyuki 	     pfn += MAX_ORDER_NR_PAGES) {
6520c0e6195SKAMEZAWA Hiroyuki 		i = 0;
6530c0e6195SKAMEZAWA Hiroyuki 		/* This is just a CONFIG_HOLES_IN_ZONE check.*/
6540c0e6195SKAMEZAWA Hiroyuki 		while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
6550c0e6195SKAMEZAWA Hiroyuki 			i++;
6560c0e6195SKAMEZAWA Hiroyuki 		if (i == MAX_ORDER_NR_PAGES)
6570c0e6195SKAMEZAWA Hiroyuki 			continue;
6580c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn + i);
6590c0e6195SKAMEZAWA Hiroyuki 		if (zone && page_zone(page) != zone)
6600c0e6195SKAMEZAWA Hiroyuki 			return 0;
6610c0e6195SKAMEZAWA Hiroyuki 		zone = page_zone(page);
6620c0e6195SKAMEZAWA Hiroyuki 	}
6630c0e6195SKAMEZAWA Hiroyuki 	return 1;
6640c0e6195SKAMEZAWA Hiroyuki }
6650c0e6195SKAMEZAWA Hiroyuki 
6660c0e6195SKAMEZAWA Hiroyuki /*
6670c0e6195SKAMEZAWA Hiroyuki  * Scanning pfn is much easier than scanning lru list.
6680c0e6195SKAMEZAWA Hiroyuki  * Scan pfn from start to end and Find LRU page.
6690c0e6195SKAMEZAWA Hiroyuki  */
6707bbc0905SAndrew Morton static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
6710c0e6195SKAMEZAWA Hiroyuki {
6720c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
6730c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
6740c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start; pfn < end; pfn++) {
6750c0e6195SKAMEZAWA Hiroyuki 		if (pfn_valid(pfn)) {
6760c0e6195SKAMEZAWA Hiroyuki 			page = pfn_to_page(pfn);
6770c0e6195SKAMEZAWA Hiroyuki 			if (PageLRU(page))
6780c0e6195SKAMEZAWA Hiroyuki 				return pfn;
6790c0e6195SKAMEZAWA Hiroyuki 		}
6800c0e6195SKAMEZAWA Hiroyuki 	}
6810c0e6195SKAMEZAWA Hiroyuki 	return 0;
6820c0e6195SKAMEZAWA Hiroyuki }
6830c0e6195SKAMEZAWA Hiroyuki 
6840c0e6195SKAMEZAWA Hiroyuki static struct page *
6853c1d4378SHugh Dickins hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
6860c0e6195SKAMEZAWA Hiroyuki {
6873c1d4378SHugh Dickins 	/* This should be improooooved!! */
6883c1d4378SHugh Dickins 	return alloc_page(GFP_HIGHUSER_MOVABLE);
6890c0e6195SKAMEZAWA Hiroyuki }
6900c0e6195SKAMEZAWA Hiroyuki 
6910c0e6195SKAMEZAWA Hiroyuki #define NR_OFFLINE_AT_ONCE_PAGES	(256)
6920c0e6195SKAMEZAWA Hiroyuki static int
6930c0e6195SKAMEZAWA Hiroyuki do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
6940c0e6195SKAMEZAWA Hiroyuki {
6950c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn;
6960c0e6195SKAMEZAWA Hiroyuki 	struct page *page;
6970c0e6195SKAMEZAWA Hiroyuki 	int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
6980c0e6195SKAMEZAWA Hiroyuki 	int not_managed = 0;
6990c0e6195SKAMEZAWA Hiroyuki 	int ret = 0;
7000c0e6195SKAMEZAWA Hiroyuki 	LIST_HEAD(source);
7010c0e6195SKAMEZAWA Hiroyuki 
7020c0e6195SKAMEZAWA Hiroyuki 	for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
7030c0e6195SKAMEZAWA Hiroyuki 		if (!pfn_valid(pfn))
7040c0e6195SKAMEZAWA Hiroyuki 			continue;
7050c0e6195SKAMEZAWA Hiroyuki 		page = pfn_to_page(pfn);
7060c0e6195SKAMEZAWA Hiroyuki 		if (!page_count(page))
7070c0e6195SKAMEZAWA Hiroyuki 			continue;
7080c0e6195SKAMEZAWA Hiroyuki 		/*
7090c0e6195SKAMEZAWA Hiroyuki 		 * We can skip free pages. And we can only deal with pages on
7100c0e6195SKAMEZAWA Hiroyuki 		 * LRU.
7110c0e6195SKAMEZAWA Hiroyuki 		 */
71262695a84SNick Piggin 		ret = isolate_lru_page(page);
7130c0e6195SKAMEZAWA Hiroyuki 		if (!ret) { /* Success */
71462695a84SNick Piggin 			list_add_tail(&page->lru, &source);
7150c0e6195SKAMEZAWA Hiroyuki 			move_pages--;
7166d9c285aSKOSAKI Motohiro 			inc_zone_page_state(page, NR_ISOLATED_ANON +
7176d9c285aSKOSAKI Motohiro 					    page_is_file_cache(page));
7186d9c285aSKOSAKI Motohiro 
7190c0e6195SKAMEZAWA Hiroyuki 		} else {
7200c0e6195SKAMEZAWA Hiroyuki #ifdef CONFIG_DEBUG_VM
721718a3821SWu Fengguang 			printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
722718a3821SWu Fengguang 			       pfn);
723718a3821SWu Fengguang 			dump_page(page);
7240c0e6195SKAMEZAWA Hiroyuki #endif
725809c4449SBob Liu 			/* Becasue we don't have big zone->lock. we should
726809c4449SBob Liu 			   check this again here. */
727809c4449SBob Liu 			if (page_count(page)) {
728809c4449SBob Liu 				not_managed++;
729f3ab2636SBob Liu 				ret = -EBUSY;
730809c4449SBob Liu 				break;
731809c4449SBob Liu 			}
7320c0e6195SKAMEZAWA Hiroyuki 		}
7330c0e6195SKAMEZAWA Hiroyuki 	}
734f3ab2636SBob Liu 	if (!list_empty(&source)) {
7350c0e6195SKAMEZAWA Hiroyuki 		if (not_managed) {
7360c0e6195SKAMEZAWA Hiroyuki 			putback_lru_pages(&source);
7370c0e6195SKAMEZAWA Hiroyuki 			goto out;
7380c0e6195SKAMEZAWA Hiroyuki 		}
7390c0e6195SKAMEZAWA Hiroyuki 		/* this function returns # of failed pages */
74062b61f61SHugh Dickins 		ret = migrate_pages(&source, hotremove_migrate_alloc, 0, 1);
741cf608ac1SMinchan Kim 		if (ret)
742cf608ac1SMinchan Kim 			putback_lru_pages(&source);
743f3ab2636SBob Liu 	}
7440c0e6195SKAMEZAWA Hiroyuki out:
7450c0e6195SKAMEZAWA Hiroyuki 	return ret;
7460c0e6195SKAMEZAWA Hiroyuki }
7470c0e6195SKAMEZAWA Hiroyuki 
7480c0e6195SKAMEZAWA Hiroyuki /*
7490c0e6195SKAMEZAWA Hiroyuki  * remove from free_area[] and mark all as Reserved.
7500c0e6195SKAMEZAWA Hiroyuki  */
7510c0e6195SKAMEZAWA Hiroyuki static int
7520c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
7530c0e6195SKAMEZAWA Hiroyuki 			void *data)
7540c0e6195SKAMEZAWA Hiroyuki {
7550c0e6195SKAMEZAWA Hiroyuki 	__offline_isolated_pages(start, start + nr_pages);
7560c0e6195SKAMEZAWA Hiroyuki 	return 0;
7570c0e6195SKAMEZAWA Hiroyuki }
7580c0e6195SKAMEZAWA Hiroyuki 
7590c0e6195SKAMEZAWA Hiroyuki static void
7600c0e6195SKAMEZAWA Hiroyuki offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
7610c0e6195SKAMEZAWA Hiroyuki {
762908eedc6SKAMEZAWA Hiroyuki 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
7630c0e6195SKAMEZAWA Hiroyuki 				offline_isolated_pages_cb);
7640c0e6195SKAMEZAWA Hiroyuki }
7650c0e6195SKAMEZAWA Hiroyuki 
7660c0e6195SKAMEZAWA Hiroyuki /*
7670c0e6195SKAMEZAWA Hiroyuki  * Check all pages in range, recoreded as memory resource, are isolated.
7680c0e6195SKAMEZAWA Hiroyuki  */
7690c0e6195SKAMEZAWA Hiroyuki static int
7700c0e6195SKAMEZAWA Hiroyuki check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
7710c0e6195SKAMEZAWA Hiroyuki 			void *data)
7720c0e6195SKAMEZAWA Hiroyuki {
7730c0e6195SKAMEZAWA Hiroyuki 	int ret;
7740c0e6195SKAMEZAWA Hiroyuki 	long offlined = *(long *)data;
7750c0e6195SKAMEZAWA Hiroyuki 	ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
7760c0e6195SKAMEZAWA Hiroyuki 	offlined = nr_pages;
7770c0e6195SKAMEZAWA Hiroyuki 	if (!ret)
7780c0e6195SKAMEZAWA Hiroyuki 		*(long *)data += offlined;
7790c0e6195SKAMEZAWA Hiroyuki 	return ret;
7800c0e6195SKAMEZAWA Hiroyuki }
7810c0e6195SKAMEZAWA Hiroyuki 
7820c0e6195SKAMEZAWA Hiroyuki static long
7830c0e6195SKAMEZAWA Hiroyuki check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
7840c0e6195SKAMEZAWA Hiroyuki {
7850c0e6195SKAMEZAWA Hiroyuki 	long offlined = 0;
7860c0e6195SKAMEZAWA Hiroyuki 	int ret;
7870c0e6195SKAMEZAWA Hiroyuki 
788908eedc6SKAMEZAWA Hiroyuki 	ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
7890c0e6195SKAMEZAWA Hiroyuki 			check_pages_isolated_cb);
7900c0e6195SKAMEZAWA Hiroyuki 	if (ret < 0)
7910c0e6195SKAMEZAWA Hiroyuki 		offlined = (long)ret;
7920c0e6195SKAMEZAWA Hiroyuki 	return offlined;
7930c0e6195SKAMEZAWA Hiroyuki }
7940c0e6195SKAMEZAWA Hiroyuki 
795b4e655a4SAndrew Morton static int offline_pages(unsigned long start_pfn,
7960c0e6195SKAMEZAWA Hiroyuki 		  unsigned long end_pfn, unsigned long timeout)
7970c0e6195SKAMEZAWA Hiroyuki {
7980c0e6195SKAMEZAWA Hiroyuki 	unsigned long pfn, nr_pages, expire;
7990c0e6195SKAMEZAWA Hiroyuki 	long offlined_pages;
8007b78d335SYasunori Goto 	int ret, drain, retry_max, node;
8010c0e6195SKAMEZAWA Hiroyuki 	struct zone *zone;
8027b78d335SYasunori Goto 	struct memory_notify arg;
8030c0e6195SKAMEZAWA Hiroyuki 
8040c0e6195SKAMEZAWA Hiroyuki 	BUG_ON(start_pfn >= end_pfn);
8050c0e6195SKAMEZAWA Hiroyuki 	/* at least, alignment against pageblock is necessary */
8060c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
8070c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
8080c0e6195SKAMEZAWA Hiroyuki 	if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
8090c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
8100c0e6195SKAMEZAWA Hiroyuki 	/* This makes hotplug much easier...and readable.
8110c0e6195SKAMEZAWA Hiroyuki 	   we assume this for now. .*/
8120c0e6195SKAMEZAWA Hiroyuki 	if (!test_pages_in_a_zone(start_pfn, end_pfn))
8130c0e6195SKAMEZAWA Hiroyuki 		return -EINVAL;
8147b78d335SYasunori Goto 
81520d6c96bSKOSAKI Motohiro 	lock_memory_hotplug();
8166ad696d2SAndi Kleen 
8177b78d335SYasunori Goto 	zone = page_zone(pfn_to_page(start_pfn));
8187b78d335SYasunori Goto 	node = zone_to_nid(zone);
8197b78d335SYasunori Goto 	nr_pages = end_pfn - start_pfn;
8207b78d335SYasunori Goto 
8210c0e6195SKAMEZAWA Hiroyuki 	/* set above range as isolated */
8220c0e6195SKAMEZAWA Hiroyuki 	ret = start_isolate_page_range(start_pfn, end_pfn);
8230c0e6195SKAMEZAWA Hiroyuki 	if (ret)
8246ad696d2SAndi Kleen 		goto out;
8257b78d335SYasunori Goto 
8267b78d335SYasunori Goto 	arg.start_pfn = start_pfn;
8277b78d335SYasunori Goto 	arg.nr_pages = nr_pages;
8287b78d335SYasunori Goto 	arg.status_change_nid = -1;
8297b78d335SYasunori Goto 	if (nr_pages >= node_present_pages(node))
8307b78d335SYasunori Goto 		arg.status_change_nid = node;
8317b78d335SYasunori Goto 
8327b78d335SYasunori Goto 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
8337b78d335SYasunori Goto 	ret = notifier_to_errno(ret);
8347b78d335SYasunori Goto 	if (ret)
8357b78d335SYasunori Goto 		goto failed_removal;
8367b78d335SYasunori Goto 
8370c0e6195SKAMEZAWA Hiroyuki 	pfn = start_pfn;
8380c0e6195SKAMEZAWA Hiroyuki 	expire = jiffies + timeout;
8390c0e6195SKAMEZAWA Hiroyuki 	drain = 0;
8400c0e6195SKAMEZAWA Hiroyuki 	retry_max = 5;
8410c0e6195SKAMEZAWA Hiroyuki repeat:
8420c0e6195SKAMEZAWA Hiroyuki 	/* start memory hot removal */
8430c0e6195SKAMEZAWA Hiroyuki 	ret = -EAGAIN;
8440c0e6195SKAMEZAWA Hiroyuki 	if (time_after(jiffies, expire))
8450c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
8460c0e6195SKAMEZAWA Hiroyuki 	ret = -EINTR;
8470c0e6195SKAMEZAWA Hiroyuki 	if (signal_pending(current))
8480c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
8490c0e6195SKAMEZAWA Hiroyuki 	ret = 0;
8500c0e6195SKAMEZAWA Hiroyuki 	if (drain) {
8510c0e6195SKAMEZAWA Hiroyuki 		lru_add_drain_all();
8520c0e6195SKAMEZAWA Hiroyuki 		cond_resched();
8539f8f2172SChristoph Lameter 		drain_all_pages();
8540c0e6195SKAMEZAWA Hiroyuki 	}
8550c0e6195SKAMEZAWA Hiroyuki 
8560c0e6195SKAMEZAWA Hiroyuki 	pfn = scan_lru_pages(start_pfn, end_pfn);
8570c0e6195SKAMEZAWA Hiroyuki 	if (pfn) { /* We have page on LRU */
8580c0e6195SKAMEZAWA Hiroyuki 		ret = do_migrate_range(pfn, end_pfn);
8590c0e6195SKAMEZAWA Hiroyuki 		if (!ret) {
8600c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
8610c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
8620c0e6195SKAMEZAWA Hiroyuki 		} else {
8630c0e6195SKAMEZAWA Hiroyuki 			if (ret < 0)
8640c0e6195SKAMEZAWA Hiroyuki 				if (--retry_max == 0)
8650c0e6195SKAMEZAWA Hiroyuki 					goto failed_removal;
8660c0e6195SKAMEZAWA Hiroyuki 			yield();
8670c0e6195SKAMEZAWA Hiroyuki 			drain = 1;
8680c0e6195SKAMEZAWA Hiroyuki 			goto repeat;
8690c0e6195SKAMEZAWA Hiroyuki 		}
8700c0e6195SKAMEZAWA Hiroyuki 	}
8710c0e6195SKAMEZAWA Hiroyuki 	/* drain all zone's lru pagevec, this is asyncronous... */
8720c0e6195SKAMEZAWA Hiroyuki 	lru_add_drain_all();
8730c0e6195SKAMEZAWA Hiroyuki 	yield();
8740c0e6195SKAMEZAWA Hiroyuki 	/* drain pcp pages , this is synchrouns. */
8759f8f2172SChristoph Lameter 	drain_all_pages();
8760c0e6195SKAMEZAWA Hiroyuki 	/* check again */
8770c0e6195SKAMEZAWA Hiroyuki 	offlined_pages = check_pages_isolated(start_pfn, end_pfn);
8780c0e6195SKAMEZAWA Hiroyuki 	if (offlined_pages < 0) {
8790c0e6195SKAMEZAWA Hiroyuki 		ret = -EBUSY;
8800c0e6195SKAMEZAWA Hiroyuki 		goto failed_removal;
8810c0e6195SKAMEZAWA Hiroyuki 	}
8820c0e6195SKAMEZAWA Hiroyuki 	printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
8830c0e6195SKAMEZAWA Hiroyuki 	/* Ok, all of our target is islaoted.
8840c0e6195SKAMEZAWA Hiroyuki 	   We cannot do rollback at this point. */
8850c0e6195SKAMEZAWA Hiroyuki 	offline_isolated_pages(start_pfn, end_pfn);
886dbc0e4ceSKAMEZAWA Hiroyuki 	/* reset pagetype flags and makes migrate type to be MOVABLE */
887dbc0e4ceSKAMEZAWA Hiroyuki 	undo_isolate_page_range(start_pfn, end_pfn);
8880c0e6195SKAMEZAWA Hiroyuki 	/* removal success */
8890c0e6195SKAMEZAWA Hiroyuki 	zone->present_pages -= offlined_pages;
8900c0e6195SKAMEZAWA Hiroyuki 	zone->zone_pgdat->node_present_pages -= offlined_pages;
8910c0e6195SKAMEZAWA Hiroyuki 	totalram_pages -= offlined_pages;
8927b78d335SYasunori Goto 
893bce7394aSMinchan Kim 	setup_per_zone_wmarks();
894bce7394aSMinchan Kim 	calculate_zone_inactive_ratio(zone);
8958fe23e05SDavid Rientjes 	if (!node_present_pages(node)) {
8968fe23e05SDavid Rientjes 		node_clear_state(node, N_HIGH_MEMORY);
8978fe23e05SDavid Rientjes 		kswapd_stop(node);
8988fe23e05SDavid Rientjes 	}
899bce7394aSMinchan Kim 
9000c0e6195SKAMEZAWA Hiroyuki 	vm_total_pages = nr_free_pagecache_pages();
9010c0e6195SKAMEZAWA Hiroyuki 	writeback_set_ratelimit();
9027b78d335SYasunori Goto 
9037b78d335SYasunori Goto 	memory_notify(MEM_OFFLINE, &arg);
90420d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
9050c0e6195SKAMEZAWA Hiroyuki 	return 0;
9060c0e6195SKAMEZAWA Hiroyuki 
9070c0e6195SKAMEZAWA Hiroyuki failed_removal:
9080c0e6195SKAMEZAWA Hiroyuki 	printk(KERN_INFO "memory offlining %lx to %lx failed\n",
9090c0e6195SKAMEZAWA Hiroyuki 		start_pfn, end_pfn);
9107b78d335SYasunori Goto 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
9110c0e6195SKAMEZAWA Hiroyuki 	/* pushback to free area */
9120c0e6195SKAMEZAWA Hiroyuki 	undo_isolate_page_range(start_pfn, end_pfn);
9137b78d335SYasunori Goto 
9146ad696d2SAndi Kleen out:
91520d6c96bSKOSAKI Motohiro 	unlock_memory_hotplug();
9160c0e6195SKAMEZAWA Hiroyuki 	return ret;
9170c0e6195SKAMEZAWA Hiroyuki }
91871088785SBadari Pulavarty 
91971088785SBadari Pulavarty int remove_memory(u64 start, u64 size)
92071088785SBadari Pulavarty {
92171088785SBadari Pulavarty 	unsigned long start_pfn, end_pfn;
92271088785SBadari Pulavarty 
92371088785SBadari Pulavarty 	start_pfn = PFN_DOWN(start);
92471088785SBadari Pulavarty 	end_pfn = start_pfn + PFN_DOWN(size);
92571088785SBadari Pulavarty 	return offline_pages(start_pfn, end_pfn, 120 * HZ);
92671088785SBadari Pulavarty }
92748e94196SKAMEZAWA Hiroyuki #else
92848e94196SKAMEZAWA Hiroyuki int remove_memory(u64 start, u64 size)
92948e94196SKAMEZAWA Hiroyuki {
93048e94196SKAMEZAWA Hiroyuki 	return -EINVAL;
93148e94196SKAMEZAWA Hiroyuki }
9320c0e6195SKAMEZAWA Hiroyuki #endif /* CONFIG_MEMORY_HOTREMOVE */
93371088785SBadari Pulavarty EXPORT_SYMBOL_GPL(remove_memory);
934