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; 68718127ccSYasunori Goto if (!populated_zone(zone)) { 69718127ccSYasunori Goto int ret = 0; 70*a2f3aa02SDave Hansen ret = init_currently_empty_zone(zone, phys_start_pfn, 71*a2f3aa02SDave Hansen nr_pages, MEMMAP_HOTPLUG); 72718127ccSYasunori Goto if (ret < 0) 73718127ccSYasunori Goto return ret; 74718127ccSYasunori Goto } 75*a2f3aa02SDave Hansen memmap_init_zone(nr_pages, nid, zone_type, 76*a2f3aa02SDave 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 1643947be19SDave Hansen int online_pages(unsigned long pfn, unsigned long nr_pages) 1653947be19SDave Hansen { 1663947be19SDave Hansen unsigned long i; 1673947be19SDave Hansen unsigned long flags; 1683947be19SDave Hansen unsigned long onlined_pages = 0; 1692842f114SKAMEZAWA Hiroyuki struct resource res; 1702842f114SKAMEZAWA Hiroyuki u64 section_end; 1712842f114SKAMEZAWA Hiroyuki unsigned long start_pfn; 1723947be19SDave Hansen struct zone *zone; 1736811378eSYasunori Goto int need_zonelists_rebuild = 0; 1743947be19SDave Hansen 1753947be19SDave Hansen /* 1763947be19SDave Hansen * This doesn't need a lock to do pfn_to_page(). 1773947be19SDave Hansen * The section can't be removed here because of the 1783947be19SDave Hansen * memory_block->state_sem. 1793947be19SDave Hansen */ 1803947be19SDave Hansen zone = page_zone(pfn_to_page(pfn)); 1813947be19SDave Hansen pgdat_resize_lock(zone->zone_pgdat, &flags); 1823947be19SDave Hansen grow_zone_span(zone, pfn, pfn + nr_pages); 1833947be19SDave Hansen grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages); 1843947be19SDave Hansen pgdat_resize_unlock(zone->zone_pgdat, &flags); 1853947be19SDave Hansen 1866811378eSYasunori Goto /* 1876811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 1886811378eSYasunori Goto * This means the page allocator ignores this zone. 1896811378eSYasunori Goto * So, zonelist must be updated after online. 1906811378eSYasunori Goto */ 1916811378eSYasunori Goto if (!populated_zone(zone)) 1926811378eSYasunori Goto need_zonelists_rebuild = 1; 1936811378eSYasunori Goto 1942842f114SKAMEZAWA Hiroyuki res.start = (u64)pfn << PAGE_SHIFT; 1952842f114SKAMEZAWA Hiroyuki res.end = res.start + ((u64)nr_pages << PAGE_SHIFT) - 1; 1962842f114SKAMEZAWA Hiroyuki res.flags = IORESOURCE_MEM; /* we just need system ram */ 1972842f114SKAMEZAWA Hiroyuki section_end = res.end; 1982842f114SKAMEZAWA Hiroyuki 19958c1b5b0SKAMEZAWA Hiroyuki while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) { 2002842f114SKAMEZAWA Hiroyuki start_pfn = (unsigned long)(res.start >> PAGE_SHIFT); 2012842f114SKAMEZAWA Hiroyuki nr_pages = (unsigned long) 2022842f114SKAMEZAWA Hiroyuki ((res.end + 1 - res.start) >> PAGE_SHIFT); 2032842f114SKAMEZAWA Hiroyuki 2042842f114SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) { 2052842f114SKAMEZAWA Hiroyuki /* this region's page is not onlined now */ 2063947be19SDave Hansen for (i = 0; i < nr_pages; i++) { 2072842f114SKAMEZAWA Hiroyuki struct page *page = pfn_to_page(start_pfn + i); 2083947be19SDave Hansen online_page(page); 2093947be19SDave Hansen onlined_pages++; 2103947be19SDave Hansen } 2112842f114SKAMEZAWA Hiroyuki } 2122842f114SKAMEZAWA Hiroyuki 2132842f114SKAMEZAWA Hiroyuki res.start = res.end + 1; 2142842f114SKAMEZAWA Hiroyuki res.end = section_end; 2152842f114SKAMEZAWA Hiroyuki } 2163947be19SDave Hansen zone->present_pages += onlined_pages; 217f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 2183947be19SDave Hansen 21961b13993SDave Hansen setup_per_zone_pages_min(); 22061b13993SDave Hansen 2216811378eSYasunori Goto if (need_zonelists_rebuild) 2226811378eSYasunori Goto build_all_zonelists(); 2235a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 2242d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 2253947be19SDave Hansen return 0; 2263947be19SDave Hansen } 22753947027SKeith Mannthey #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 228bc02af93SYasunori Goto 2299af3c2deSYasunori Goto static pg_data_t *hotadd_new_pgdat(int nid, u64 start) 2309af3c2deSYasunori Goto { 2319af3c2deSYasunori Goto struct pglist_data *pgdat; 2329af3c2deSYasunori Goto unsigned long zones_size[MAX_NR_ZONES] = {0}; 2339af3c2deSYasunori Goto unsigned long zholes_size[MAX_NR_ZONES] = {0}; 2349af3c2deSYasunori Goto unsigned long start_pfn = start >> PAGE_SHIFT; 2359af3c2deSYasunori Goto 2369af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 2379af3c2deSYasunori Goto if (!pgdat) 2389af3c2deSYasunori Goto return NULL; 2399af3c2deSYasunori Goto 2409af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 2419af3c2deSYasunori Goto 2429af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 2439af3c2deSYasunori Goto 2449af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 2459af3c2deSYasunori Goto free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size); 2469af3c2deSYasunori Goto 2479af3c2deSYasunori Goto return pgdat; 2489af3c2deSYasunori Goto } 2499af3c2deSYasunori Goto 2509af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat) 2519af3c2deSYasunori Goto { 2529af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 2539af3c2deSYasunori Goto arch_free_nodedata(pgdat); 2549af3c2deSYasunori Goto return; 2559af3c2deSYasunori Goto } 2569af3c2deSYasunori Goto 2570a547039SKAMEZAWA Hiroyuki 258bc02af93SYasunori Goto int add_memory(int nid, u64 start, u64 size) 259bc02af93SYasunori Goto { 2609af3c2deSYasunori Goto pg_data_t *pgdat = NULL; 2619af3c2deSYasunori Goto int new_pgdat = 0; 262ebd15302SKAMEZAWA Hiroyuki struct resource *res; 263bc02af93SYasunori Goto int ret; 264bc02af93SYasunori Goto 265ebd15302SKAMEZAWA Hiroyuki res = register_memory_resource(start, size); 266ebd15302SKAMEZAWA Hiroyuki if (!res) 267ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 268ebd15302SKAMEZAWA Hiroyuki 2699af3c2deSYasunori Goto if (!node_online(nid)) { 2709af3c2deSYasunori Goto pgdat = hotadd_new_pgdat(nid, start); 2719af3c2deSYasunori Goto if (!pgdat) 2729af3c2deSYasunori Goto return -ENOMEM; 2739af3c2deSYasunori Goto new_pgdat = 1; 2749af3c2deSYasunori Goto ret = kswapd_run(nid); 2759af3c2deSYasunori Goto if (ret) 2769af3c2deSYasunori Goto goto error; 2779af3c2deSYasunori Goto } 2789af3c2deSYasunori Goto 279bc02af93SYasunori Goto /* call arch's memory hotadd */ 280bc02af93SYasunori Goto ret = arch_add_memory(nid, start, size); 281bc02af93SYasunori Goto 2829af3c2deSYasunori Goto if (ret < 0) 2839af3c2deSYasunori Goto goto error; 2849af3c2deSYasunori Goto 2850fc44159SYasunori Goto /* we online node here. we can't roll back from here. */ 2869af3c2deSYasunori Goto node_set_online(nid); 2879af3c2deSYasunori Goto 28838837fc7SPaul Jackson cpuset_track_online_nodes(); 28938837fc7SPaul Jackson 2900fc44159SYasunori Goto if (new_pgdat) { 2910fc44159SYasunori Goto ret = register_one_node(nid); 2920fc44159SYasunori Goto /* 2930fc44159SYasunori Goto * If sysfs file of new node can't create, cpu on the node 2940fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 2950fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 2960fc44159SYasunori Goto */ 2970fc44159SYasunori Goto BUG_ON(ret); 2980fc44159SYasunori Goto } 2990fc44159SYasunori Goto 3009af3c2deSYasunori Goto return ret; 3019af3c2deSYasunori Goto error: 3029af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 3039af3c2deSYasunori Goto if (new_pgdat) 3049af3c2deSYasunori Goto rollback_node_hotadd(nid, pgdat); 305ebd15302SKAMEZAWA Hiroyuki if (res) 306ebd15302SKAMEZAWA Hiroyuki release_memory_resource(res); 3079af3c2deSYasunori Goto 308bc02af93SYasunori Goto return ret; 309bc02af93SYasunori Goto } 310bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 311