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> 2538837fc7SPaul Jackson #include <linux/cpuset.h> 263947be19SDave Hansen 273947be19SDave Hansen #include <asm/tlbflush.h> 283947be19SDave Hansen 2945e0b78bSKeith Mannthey /* add this memory to iomem resource */ 3045e0b78bSKeith Mannthey static struct resource *register_memory_resource(u64 start, u64 size) 3145e0b78bSKeith Mannthey { 3245e0b78bSKeith Mannthey struct resource *res; 3345e0b78bSKeith Mannthey res = kzalloc(sizeof(struct resource), GFP_KERNEL); 3445e0b78bSKeith Mannthey BUG_ON(!res); 3545e0b78bSKeith Mannthey 3645e0b78bSKeith Mannthey res->name = "System RAM"; 3745e0b78bSKeith Mannthey res->start = start; 3845e0b78bSKeith Mannthey res->end = start + size - 1; 3945e0b78bSKeith Mannthey res->flags = IORESOURCE_MEM; 4045e0b78bSKeith Mannthey if (request_resource(&iomem_resource, res) < 0) { 4145e0b78bSKeith Mannthey printk("System RAM resource %llx - %llx cannot be added\n", 4245e0b78bSKeith Mannthey (unsigned long long)res->start, (unsigned long long)res->end); 4345e0b78bSKeith Mannthey kfree(res); 4445e0b78bSKeith Mannthey res = NULL; 4545e0b78bSKeith Mannthey } 4645e0b78bSKeith Mannthey return res; 4745e0b78bSKeith Mannthey } 4845e0b78bSKeith Mannthey 4945e0b78bSKeith Mannthey static void release_memory_resource(struct resource *res) 5045e0b78bSKeith Mannthey { 5145e0b78bSKeith Mannthey if (!res) 5245e0b78bSKeith Mannthey return; 5345e0b78bSKeith Mannthey release_resource(res); 5445e0b78bSKeith Mannthey kfree(res); 5545e0b78bSKeith Mannthey return; 5645e0b78bSKeith Mannthey } 5745e0b78bSKeith Mannthey 5845e0b78bSKeith Mannthey 5953947027SKeith Mannthey #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 60718127ccSYasunori Goto static int __add_zone(struct zone *zone, unsigned long phys_start_pfn) 613947be19SDave Hansen { 623947be19SDave Hansen struct pglist_data *pgdat = zone->zone_pgdat; 633947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 643947be19SDave Hansen int nid = pgdat->node_id; 653947be19SDave Hansen int zone_type; 663947be19SDave Hansen 673947be19SDave Hansen zone_type = zone - pgdat->node_zones; 6813466c84SYasunori Goto if (!zone->wait_table) { 69718127ccSYasunori Goto int ret = 0; 70a2f3aa02SDave Hansen ret = init_currently_empty_zone(zone, phys_start_pfn, 71a2f3aa02SDave Hansen nr_pages, MEMMAP_HOTPLUG); 72718127ccSYasunori Goto if (ret < 0) 73718127ccSYasunori Goto return ret; 74718127ccSYasunori Goto } 75a2f3aa02SDave Hansen memmap_init_zone(nr_pages, nid, zone_type, 76a2f3aa02SDave Hansen phys_start_pfn, MEMMAP_HOTPLUG); 77718127ccSYasunori Goto return 0; 783947be19SDave Hansen } 793947be19SDave Hansen 803947be19SDave Hansen static int __add_section(struct zone *zone, unsigned long phys_start_pfn) 813947be19SDave Hansen { 823947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 833947be19SDave Hansen int ret; 843947be19SDave Hansen 85ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 86ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 87ebd15302SKAMEZAWA Hiroyuki 880b0acbecSDave Hansen ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); 893947be19SDave Hansen 903947be19SDave Hansen if (ret < 0) 913947be19SDave Hansen return ret; 923947be19SDave Hansen 93718127ccSYasunori Goto ret = __add_zone(zone, phys_start_pfn); 94718127ccSYasunori Goto 95718127ccSYasunori Goto if (ret < 0) 96718127ccSYasunori Goto return ret; 97718127ccSYasunori Goto 983947be19SDave Hansen return register_new_memory(__pfn_to_section(phys_start_pfn)); 993947be19SDave Hansen } 1003947be19SDave Hansen 1013947be19SDave Hansen /* 1023947be19SDave Hansen * Reasonably generic function for adding memory. It is 1033947be19SDave Hansen * expected that archs that support memory hotplug will 1043947be19SDave Hansen * call this function after deciding the zone to which to 1053947be19SDave Hansen * add the new pages. 1063947be19SDave Hansen */ 1073947be19SDave Hansen int __add_pages(struct zone *zone, unsigned long phys_start_pfn, 1083947be19SDave Hansen unsigned long nr_pages) 1093947be19SDave Hansen { 1103947be19SDave Hansen unsigned long i; 1113947be19SDave Hansen int err = 0; 1126f712711SKAMEZAWA Hiroyuki int start_sec, end_sec; 1136f712711SKAMEZAWA Hiroyuki /* during initialize mem_map, align hot-added range to section */ 1146f712711SKAMEZAWA Hiroyuki start_sec = pfn_to_section_nr(phys_start_pfn); 1156f712711SKAMEZAWA Hiroyuki end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 1163947be19SDave Hansen 1176f712711SKAMEZAWA Hiroyuki for (i = start_sec; i <= end_sec; i++) { 1186f712711SKAMEZAWA Hiroyuki err = __add_section(zone, i << PFN_SECTION_SHIFT); 1193947be19SDave Hansen 1206f712711SKAMEZAWA Hiroyuki /* 1216f712711SKAMEZAWA Hiroyuki * EEXIST is finally dealed with by ioresource collision 1226f712711SKAMEZAWA Hiroyuki * check. see add_memory() => register_memory_resource() 1236f712711SKAMEZAWA Hiroyuki * Warning will be printed if there is collision. 124bed120c6SJoel H Schopp */ 125bed120c6SJoel H Schopp if (err && (err != -EEXIST)) 1263947be19SDave Hansen break; 1276f712711SKAMEZAWA Hiroyuki err = 0; 1283947be19SDave Hansen } 1293947be19SDave Hansen 1303947be19SDave Hansen return err; 1313947be19SDave Hansen } 132bed120c6SJoel H Schopp EXPORT_SYMBOL_GPL(__add_pages); 1333947be19SDave Hansen 1343947be19SDave Hansen static void grow_zone_span(struct zone *zone, 1353947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 1363947be19SDave Hansen { 1373947be19SDave Hansen unsigned long old_zone_end_pfn; 1383947be19SDave Hansen 1393947be19SDave Hansen zone_span_writelock(zone); 1403947be19SDave Hansen 1413947be19SDave Hansen old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 1423947be19SDave Hansen if (start_pfn < zone->zone_start_pfn) 1433947be19SDave Hansen zone->zone_start_pfn = start_pfn; 1443947be19SDave Hansen 14525a6df95SYasunori Goto zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - 14625a6df95SYasunori Goto zone->zone_start_pfn; 1473947be19SDave Hansen 1483947be19SDave Hansen zone_span_writeunlock(zone); 1493947be19SDave Hansen } 1503947be19SDave Hansen 1513947be19SDave Hansen static void grow_pgdat_span(struct pglist_data *pgdat, 1523947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 1533947be19SDave Hansen { 1543947be19SDave Hansen unsigned long old_pgdat_end_pfn = 1553947be19SDave Hansen pgdat->node_start_pfn + pgdat->node_spanned_pages; 1563947be19SDave Hansen 1573947be19SDave Hansen if (start_pfn < pgdat->node_start_pfn) 1583947be19SDave Hansen pgdat->node_start_pfn = start_pfn; 1593947be19SDave Hansen 16025a6df95SYasunori Goto pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - 16125a6df95SYasunori Goto pgdat->node_start_pfn; 1623947be19SDave Hansen } 1633947be19SDave Hansen 164*75884fb1SKAMEZAWA Hiroyuki static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, 165*75884fb1SKAMEZAWA Hiroyuki void *arg) 1663947be19SDave Hansen { 1673947be19SDave Hansen unsigned long i; 168*75884fb1SKAMEZAWA Hiroyuki unsigned long onlined_pages = *(unsigned long *)arg; 169*75884fb1SKAMEZAWA Hiroyuki struct page *page; 170*75884fb1SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) 171*75884fb1SKAMEZAWA Hiroyuki for (i = 0; i < nr_pages; i++) { 172*75884fb1SKAMEZAWA Hiroyuki page = pfn_to_page(start_pfn + i); 173*75884fb1SKAMEZAWA Hiroyuki online_page(page); 174*75884fb1SKAMEZAWA Hiroyuki onlined_pages++; 175*75884fb1SKAMEZAWA Hiroyuki } 176*75884fb1SKAMEZAWA Hiroyuki *(unsigned long *)arg = onlined_pages; 177*75884fb1SKAMEZAWA Hiroyuki return 0; 178*75884fb1SKAMEZAWA Hiroyuki } 179*75884fb1SKAMEZAWA Hiroyuki 180*75884fb1SKAMEZAWA Hiroyuki 181*75884fb1SKAMEZAWA Hiroyuki int online_pages(unsigned long pfn, unsigned long nr_pages) 182*75884fb1SKAMEZAWA Hiroyuki { 1833947be19SDave Hansen unsigned long flags; 1843947be19SDave Hansen unsigned long onlined_pages = 0; 1853947be19SDave Hansen struct zone *zone; 1866811378eSYasunori Goto int need_zonelists_rebuild = 0; 1873947be19SDave Hansen 1883947be19SDave Hansen /* 1893947be19SDave Hansen * This doesn't need a lock to do pfn_to_page(). 1903947be19SDave Hansen * The section can't be removed here because of the 1913947be19SDave Hansen * memory_block->state_sem. 1923947be19SDave Hansen */ 1933947be19SDave Hansen zone = page_zone(pfn_to_page(pfn)); 1943947be19SDave Hansen pgdat_resize_lock(zone->zone_pgdat, &flags); 1953947be19SDave Hansen grow_zone_span(zone, pfn, pfn + nr_pages); 1963947be19SDave Hansen grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages); 1973947be19SDave Hansen pgdat_resize_unlock(zone->zone_pgdat, &flags); 1983947be19SDave Hansen 1996811378eSYasunori Goto /* 2006811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 2016811378eSYasunori Goto * This means the page allocator ignores this zone. 2026811378eSYasunori Goto * So, zonelist must be updated after online. 2036811378eSYasunori Goto */ 2046811378eSYasunori Goto if (!populated_zone(zone)) 2056811378eSYasunori Goto need_zonelists_rebuild = 1; 2066811378eSYasunori Goto 207*75884fb1SKAMEZAWA Hiroyuki walk_memory_resource(pfn, nr_pages, &onlined_pages, 208*75884fb1SKAMEZAWA Hiroyuki online_pages_range); 2093947be19SDave Hansen zone->present_pages += onlined_pages; 210f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 2113947be19SDave Hansen 21261b13993SDave Hansen setup_per_zone_pages_min(); 2137ea1530aSChristoph Lameter if (onlined_pages) { 2147ea1530aSChristoph Lameter kswapd_run(zone_to_nid(zone)); 2157ea1530aSChristoph Lameter node_set_state(zone_to_nid(zone), N_HIGH_MEMORY); 2167ea1530aSChristoph Lameter } 21761b13993SDave Hansen 2186811378eSYasunori Goto if (need_zonelists_rebuild) 2196811378eSYasunori Goto build_all_zonelists(); 2205a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 2212d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 2223947be19SDave Hansen return 0; 2233947be19SDave Hansen } 22453947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 225bc02af93SYasunori Goto 2269af3c2deSYasunori Goto static pg_data_t *hotadd_new_pgdat(int nid, u64 start) 2279af3c2deSYasunori Goto { 2289af3c2deSYasunori Goto struct pglist_data *pgdat; 2299af3c2deSYasunori Goto unsigned long zones_size[MAX_NR_ZONES] = {0}; 2309af3c2deSYasunori Goto unsigned long zholes_size[MAX_NR_ZONES] = {0}; 2319af3c2deSYasunori Goto unsigned long start_pfn = start >> PAGE_SHIFT; 2329af3c2deSYasunori Goto 2339af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 2349af3c2deSYasunori Goto if (!pgdat) 2359af3c2deSYasunori Goto return NULL; 2369af3c2deSYasunori Goto 2379af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 2389af3c2deSYasunori Goto 2399af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 2409af3c2deSYasunori Goto 2419af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 2429af3c2deSYasunori Goto free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size); 2439af3c2deSYasunori Goto 2449af3c2deSYasunori Goto return pgdat; 2459af3c2deSYasunori Goto } 2469af3c2deSYasunori Goto 2479af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat) 2489af3c2deSYasunori Goto { 2499af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 2509af3c2deSYasunori Goto arch_free_nodedata(pgdat); 2519af3c2deSYasunori Goto return; 2529af3c2deSYasunori Goto } 2539af3c2deSYasunori Goto 2540a547039SKAMEZAWA Hiroyuki 255bc02af93SYasunori Goto int add_memory(int nid, u64 start, u64 size) 256bc02af93SYasunori Goto { 2579af3c2deSYasunori Goto pg_data_t *pgdat = NULL; 2589af3c2deSYasunori Goto int new_pgdat = 0; 259ebd15302SKAMEZAWA Hiroyuki struct resource *res; 260bc02af93SYasunori Goto int ret; 261bc02af93SYasunori Goto 262ebd15302SKAMEZAWA Hiroyuki res = register_memory_resource(start, size); 263ebd15302SKAMEZAWA Hiroyuki if (!res) 264ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 265ebd15302SKAMEZAWA Hiroyuki 2669af3c2deSYasunori Goto if (!node_online(nid)) { 2679af3c2deSYasunori Goto pgdat = hotadd_new_pgdat(nid, start); 2689af3c2deSYasunori Goto if (!pgdat) 2699af3c2deSYasunori Goto return -ENOMEM; 2709af3c2deSYasunori Goto new_pgdat = 1; 2719af3c2deSYasunori Goto } 2729af3c2deSYasunori Goto 273bc02af93SYasunori Goto /* call arch's memory hotadd */ 274bc02af93SYasunori Goto ret = arch_add_memory(nid, start, size); 275bc02af93SYasunori Goto 2769af3c2deSYasunori Goto if (ret < 0) 2779af3c2deSYasunori Goto goto error; 2789af3c2deSYasunori Goto 2790fc44159SYasunori Goto /* we online node here. we can't roll back from here. */ 2809af3c2deSYasunori Goto node_set_online(nid); 2819af3c2deSYasunori Goto 28238837fc7SPaul Jackson cpuset_track_online_nodes(); 28338837fc7SPaul Jackson 2840fc44159SYasunori Goto if (new_pgdat) { 2850fc44159SYasunori Goto ret = register_one_node(nid); 2860fc44159SYasunori Goto /* 2870fc44159SYasunori Goto * If sysfs file of new node can't create, cpu on the node 2880fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 2890fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 2900fc44159SYasunori Goto */ 2910fc44159SYasunori Goto BUG_ON(ret); 2920fc44159SYasunori Goto } 2930fc44159SYasunori Goto 2949af3c2deSYasunori Goto return ret; 2959af3c2deSYasunori Goto error: 2969af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 2979af3c2deSYasunori Goto if (new_pgdat) 2989af3c2deSYasunori Goto rollback_node_hotadd(nid, pgdat); 299ebd15302SKAMEZAWA Hiroyuki if (res) 300ebd15302SKAMEZAWA Hiroyuki release_memory_resource(res); 3019af3c2deSYasunori Goto 302bc02af93SYasunori Goto return ret; 303bc02af93SYasunori Goto } 304bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 305