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> 16*2d1d43f6SChandra 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 293947be19SDave Hansen extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn, 303947be19SDave Hansen unsigned long size); 31718127ccSYasunori Goto static int __add_zone(struct zone *zone, unsigned long phys_start_pfn) 323947be19SDave Hansen { 333947be19SDave Hansen struct pglist_data *pgdat = zone->zone_pgdat; 343947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 353947be19SDave Hansen int nid = pgdat->node_id; 363947be19SDave Hansen int zone_type; 373947be19SDave Hansen 383947be19SDave Hansen zone_type = zone - pgdat->node_zones; 39718127ccSYasunori Goto if (!populated_zone(zone)) { 40718127ccSYasunori Goto int ret = 0; 41718127ccSYasunori Goto ret = init_currently_empty_zone(zone, phys_start_pfn, nr_pages); 42718127ccSYasunori Goto if (ret < 0) 43718127ccSYasunori Goto return ret; 44718127ccSYasunori Goto } 453947be19SDave Hansen memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn); 463947be19SDave Hansen zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages); 47718127ccSYasunori Goto return 0; 483947be19SDave Hansen } 493947be19SDave Hansen 500b0acbecSDave Hansen extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn, 510b0acbecSDave Hansen int nr_pages); 523947be19SDave Hansen static int __add_section(struct zone *zone, unsigned long phys_start_pfn) 533947be19SDave Hansen { 543947be19SDave Hansen int nr_pages = PAGES_PER_SECTION; 553947be19SDave Hansen int ret; 563947be19SDave Hansen 57ebd15302SKAMEZAWA Hiroyuki if (pfn_valid(phys_start_pfn)) 58ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 59ebd15302SKAMEZAWA Hiroyuki 600b0acbecSDave Hansen ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); 613947be19SDave Hansen 623947be19SDave Hansen if (ret < 0) 633947be19SDave Hansen return ret; 643947be19SDave Hansen 65718127ccSYasunori Goto ret = __add_zone(zone, phys_start_pfn); 66718127ccSYasunori Goto 67718127ccSYasunori Goto if (ret < 0) 68718127ccSYasunori Goto return ret; 69718127ccSYasunori Goto 703947be19SDave Hansen return register_new_memory(__pfn_to_section(phys_start_pfn)); 713947be19SDave Hansen } 723947be19SDave Hansen 733947be19SDave Hansen /* 743947be19SDave Hansen * Reasonably generic function for adding memory. It is 753947be19SDave Hansen * expected that archs that support memory hotplug will 763947be19SDave Hansen * call this function after deciding the zone to which to 773947be19SDave Hansen * add the new pages. 783947be19SDave Hansen */ 793947be19SDave Hansen int __add_pages(struct zone *zone, unsigned long phys_start_pfn, 803947be19SDave Hansen unsigned long nr_pages) 813947be19SDave Hansen { 823947be19SDave Hansen unsigned long i; 833947be19SDave Hansen int err = 0; 846f712711SKAMEZAWA Hiroyuki int start_sec, end_sec; 856f712711SKAMEZAWA Hiroyuki /* during initialize mem_map, align hot-added range to section */ 866f712711SKAMEZAWA Hiroyuki start_sec = pfn_to_section_nr(phys_start_pfn); 876f712711SKAMEZAWA Hiroyuki end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); 883947be19SDave Hansen 896f712711SKAMEZAWA Hiroyuki for (i = start_sec; i <= end_sec; i++) { 906f712711SKAMEZAWA Hiroyuki err = __add_section(zone, i << PFN_SECTION_SHIFT); 913947be19SDave Hansen 926f712711SKAMEZAWA Hiroyuki /* 936f712711SKAMEZAWA Hiroyuki * EEXIST is finally dealed with by ioresource collision 946f712711SKAMEZAWA Hiroyuki * check. see add_memory() => register_memory_resource() 956f712711SKAMEZAWA Hiroyuki * Warning will be printed if there is collision. 96bed120c6SJoel H Schopp */ 97bed120c6SJoel H Schopp if (err && (err != -EEXIST)) 983947be19SDave Hansen break; 996f712711SKAMEZAWA Hiroyuki err = 0; 1003947be19SDave Hansen } 1013947be19SDave Hansen 1023947be19SDave Hansen return err; 1033947be19SDave Hansen } 104bed120c6SJoel H Schopp EXPORT_SYMBOL_GPL(__add_pages); 1053947be19SDave Hansen 1063947be19SDave Hansen static void grow_zone_span(struct zone *zone, 1073947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 1083947be19SDave Hansen { 1093947be19SDave Hansen unsigned long old_zone_end_pfn; 1103947be19SDave Hansen 1113947be19SDave Hansen zone_span_writelock(zone); 1123947be19SDave Hansen 1133947be19SDave Hansen old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 1143947be19SDave Hansen if (start_pfn < zone->zone_start_pfn) 1153947be19SDave Hansen zone->zone_start_pfn = start_pfn; 1163947be19SDave Hansen 11725a6df95SYasunori Goto zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - 11825a6df95SYasunori Goto zone->zone_start_pfn; 1193947be19SDave Hansen 1203947be19SDave Hansen zone_span_writeunlock(zone); 1213947be19SDave Hansen } 1223947be19SDave Hansen 1233947be19SDave Hansen static void grow_pgdat_span(struct pglist_data *pgdat, 1243947be19SDave Hansen unsigned long start_pfn, unsigned long end_pfn) 1253947be19SDave Hansen { 1263947be19SDave Hansen unsigned long old_pgdat_end_pfn = 1273947be19SDave Hansen pgdat->node_start_pfn + pgdat->node_spanned_pages; 1283947be19SDave Hansen 1293947be19SDave Hansen if (start_pfn < pgdat->node_start_pfn) 1303947be19SDave Hansen pgdat->node_start_pfn = start_pfn; 1313947be19SDave Hansen 13225a6df95SYasunori Goto pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - 13325a6df95SYasunori Goto pgdat->node_start_pfn; 1343947be19SDave Hansen } 1353947be19SDave Hansen 1363947be19SDave Hansen int online_pages(unsigned long pfn, unsigned long nr_pages) 1373947be19SDave Hansen { 1383947be19SDave Hansen unsigned long i; 1393947be19SDave Hansen unsigned long flags; 1403947be19SDave Hansen unsigned long onlined_pages = 0; 1412842f114SKAMEZAWA Hiroyuki struct resource res; 1422842f114SKAMEZAWA Hiroyuki u64 section_end; 1432842f114SKAMEZAWA Hiroyuki unsigned long start_pfn; 1443947be19SDave Hansen struct zone *zone; 1456811378eSYasunori Goto int need_zonelists_rebuild = 0; 1463947be19SDave Hansen 1473947be19SDave Hansen /* 1483947be19SDave Hansen * This doesn't need a lock to do pfn_to_page(). 1493947be19SDave Hansen * The section can't be removed here because of the 1503947be19SDave Hansen * memory_block->state_sem. 1513947be19SDave Hansen */ 1523947be19SDave Hansen zone = page_zone(pfn_to_page(pfn)); 1533947be19SDave Hansen pgdat_resize_lock(zone->zone_pgdat, &flags); 1543947be19SDave Hansen grow_zone_span(zone, pfn, pfn + nr_pages); 1553947be19SDave Hansen grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages); 1563947be19SDave Hansen pgdat_resize_unlock(zone->zone_pgdat, &flags); 1573947be19SDave Hansen 1586811378eSYasunori Goto /* 1596811378eSYasunori Goto * If this zone is not populated, then it is not in zonelist. 1606811378eSYasunori Goto * This means the page allocator ignores this zone. 1616811378eSYasunori Goto * So, zonelist must be updated after online. 1626811378eSYasunori Goto */ 1636811378eSYasunori Goto if (!populated_zone(zone)) 1646811378eSYasunori Goto need_zonelists_rebuild = 1; 1656811378eSYasunori Goto 1662842f114SKAMEZAWA Hiroyuki res.start = (u64)pfn << PAGE_SHIFT; 1672842f114SKAMEZAWA Hiroyuki res.end = res.start + ((u64)nr_pages << PAGE_SHIFT) - 1; 1682842f114SKAMEZAWA Hiroyuki res.flags = IORESOURCE_MEM; /* we just need system ram */ 1692842f114SKAMEZAWA Hiroyuki section_end = res.end; 1702842f114SKAMEZAWA Hiroyuki 17158c1b5b0SKAMEZAWA Hiroyuki while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) { 1722842f114SKAMEZAWA Hiroyuki start_pfn = (unsigned long)(res.start >> PAGE_SHIFT); 1732842f114SKAMEZAWA Hiroyuki nr_pages = (unsigned long) 1742842f114SKAMEZAWA Hiroyuki ((res.end + 1 - res.start) >> PAGE_SHIFT); 1752842f114SKAMEZAWA Hiroyuki 1762842f114SKAMEZAWA Hiroyuki if (PageReserved(pfn_to_page(start_pfn))) { 1772842f114SKAMEZAWA Hiroyuki /* this region's page is not onlined now */ 1783947be19SDave Hansen for (i = 0; i < nr_pages; i++) { 1792842f114SKAMEZAWA Hiroyuki struct page *page = pfn_to_page(start_pfn + i); 1803947be19SDave Hansen online_page(page); 1813947be19SDave Hansen onlined_pages++; 1823947be19SDave Hansen } 1832842f114SKAMEZAWA Hiroyuki } 1842842f114SKAMEZAWA Hiroyuki 1852842f114SKAMEZAWA Hiroyuki res.start = res.end + 1; 1862842f114SKAMEZAWA Hiroyuki res.end = section_end; 1872842f114SKAMEZAWA Hiroyuki } 1883947be19SDave Hansen zone->present_pages += onlined_pages; 189f2937be5SYasunori Goto zone->zone_pgdat->node_present_pages += onlined_pages; 1903947be19SDave Hansen 19161b13993SDave Hansen setup_per_zone_pages_min(); 19261b13993SDave Hansen 1936811378eSYasunori Goto if (need_zonelists_rebuild) 1946811378eSYasunori Goto build_all_zonelists(); 1955a4d4361SKAMEZAWA Hiroyuki vm_total_pages = nr_free_pagecache_pages(); 196*2d1d43f6SChandra Seetharaman writeback_set_ratelimit(); 1973947be19SDave Hansen return 0; 1983947be19SDave Hansen } 199bc02af93SYasunori Goto 2009af3c2deSYasunori Goto static pg_data_t *hotadd_new_pgdat(int nid, u64 start) 2019af3c2deSYasunori Goto { 2029af3c2deSYasunori Goto struct pglist_data *pgdat; 2039af3c2deSYasunori Goto unsigned long zones_size[MAX_NR_ZONES] = {0}; 2049af3c2deSYasunori Goto unsigned long zholes_size[MAX_NR_ZONES] = {0}; 2059af3c2deSYasunori Goto unsigned long start_pfn = start >> PAGE_SHIFT; 2069af3c2deSYasunori Goto 2079af3c2deSYasunori Goto pgdat = arch_alloc_nodedata(nid); 2089af3c2deSYasunori Goto if (!pgdat) 2099af3c2deSYasunori Goto return NULL; 2109af3c2deSYasunori Goto 2119af3c2deSYasunori Goto arch_refresh_nodedata(nid, pgdat); 2129af3c2deSYasunori Goto 2139af3c2deSYasunori Goto /* we can use NODE_DATA(nid) from here */ 2149af3c2deSYasunori Goto 2159af3c2deSYasunori Goto /* init node's zones as empty zones, we don't have any present pages.*/ 2169af3c2deSYasunori Goto free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size); 2179af3c2deSYasunori Goto 2189af3c2deSYasunori Goto return pgdat; 2199af3c2deSYasunori Goto } 2209af3c2deSYasunori Goto 2219af3c2deSYasunori Goto static void rollback_node_hotadd(int nid, pg_data_t *pgdat) 2229af3c2deSYasunori Goto { 2239af3c2deSYasunori Goto arch_refresh_nodedata(nid, NULL); 2249af3c2deSYasunori Goto arch_free_nodedata(pgdat); 2259af3c2deSYasunori Goto return; 2269af3c2deSYasunori Goto } 2279af3c2deSYasunori Goto 2280a547039SKAMEZAWA Hiroyuki /* add this memory to iomem resource */ 229ebd15302SKAMEZAWA Hiroyuki static struct resource *register_memory_resource(u64 start, u64 size) 2300a547039SKAMEZAWA Hiroyuki { 2310a547039SKAMEZAWA Hiroyuki struct resource *res; 2320a547039SKAMEZAWA Hiroyuki res = kzalloc(sizeof(struct resource), GFP_KERNEL); 2330a547039SKAMEZAWA Hiroyuki BUG_ON(!res); 2340a547039SKAMEZAWA Hiroyuki 2350a547039SKAMEZAWA Hiroyuki res->name = "System RAM"; 2360a547039SKAMEZAWA Hiroyuki res->start = start; 2370a547039SKAMEZAWA Hiroyuki res->end = start + size - 1; 2380a547039SKAMEZAWA Hiroyuki res->flags = IORESOURCE_MEM; 2390a547039SKAMEZAWA Hiroyuki if (request_resource(&iomem_resource, res) < 0) { 2400a547039SKAMEZAWA Hiroyuki printk("System RAM resource %llx - %llx cannot be added\n", 2410a547039SKAMEZAWA Hiroyuki (unsigned long long)res->start, (unsigned long long)res->end); 2420a547039SKAMEZAWA Hiroyuki kfree(res); 243ebd15302SKAMEZAWA Hiroyuki res = NULL; 2440a547039SKAMEZAWA Hiroyuki } 245ebd15302SKAMEZAWA Hiroyuki return res; 246ebd15302SKAMEZAWA Hiroyuki } 247ebd15302SKAMEZAWA Hiroyuki 248ebd15302SKAMEZAWA Hiroyuki static void release_memory_resource(struct resource *res) 249ebd15302SKAMEZAWA Hiroyuki { 250ebd15302SKAMEZAWA Hiroyuki if (!res) 251ebd15302SKAMEZAWA Hiroyuki return; 252ebd15302SKAMEZAWA Hiroyuki release_resource(res); 253ebd15302SKAMEZAWA Hiroyuki kfree(res); 254ebd15302SKAMEZAWA Hiroyuki return; 2550a547039SKAMEZAWA Hiroyuki } 2560a547039SKAMEZAWA Hiroyuki 2570a547039SKAMEZAWA Hiroyuki 2580a547039SKAMEZAWA Hiroyuki 259bc02af93SYasunori Goto int add_memory(int nid, u64 start, u64 size) 260bc02af93SYasunori Goto { 2619af3c2deSYasunori Goto pg_data_t *pgdat = NULL; 2629af3c2deSYasunori Goto int new_pgdat = 0; 263ebd15302SKAMEZAWA Hiroyuki struct resource *res; 264bc02af93SYasunori Goto int ret; 265bc02af93SYasunori Goto 266ebd15302SKAMEZAWA Hiroyuki res = register_memory_resource(start, size); 267ebd15302SKAMEZAWA Hiroyuki if (!res) 268ebd15302SKAMEZAWA Hiroyuki return -EEXIST; 269ebd15302SKAMEZAWA Hiroyuki 2709af3c2deSYasunori Goto if (!node_online(nid)) { 2719af3c2deSYasunori Goto pgdat = hotadd_new_pgdat(nid, start); 2729af3c2deSYasunori Goto if (!pgdat) 2739af3c2deSYasunori Goto return -ENOMEM; 2749af3c2deSYasunori Goto new_pgdat = 1; 2759af3c2deSYasunori Goto ret = kswapd_run(nid); 2769af3c2deSYasunori Goto if (ret) 2779af3c2deSYasunori Goto goto error; 2789af3c2deSYasunori Goto } 2799af3c2deSYasunori Goto 280bc02af93SYasunori Goto /* call arch's memory hotadd */ 281bc02af93SYasunori Goto ret = arch_add_memory(nid, start, size); 282bc02af93SYasunori Goto 2839af3c2deSYasunori Goto if (ret < 0) 2849af3c2deSYasunori Goto goto error; 2859af3c2deSYasunori Goto 2860fc44159SYasunori Goto /* we online node here. we can't roll back from here. */ 2879af3c2deSYasunori Goto node_set_online(nid); 2889af3c2deSYasunori Goto 28938837fc7SPaul Jackson cpuset_track_online_nodes(); 29038837fc7SPaul Jackson 2910fc44159SYasunori Goto if (new_pgdat) { 2920fc44159SYasunori Goto ret = register_one_node(nid); 2930fc44159SYasunori Goto /* 2940fc44159SYasunori Goto * If sysfs file of new node can't create, cpu on the node 2950fc44159SYasunori Goto * can't be hot-added. There is no rollback way now. 2960fc44159SYasunori Goto * So, check by BUG_ON() to catch it reluctantly.. 2970fc44159SYasunori Goto */ 2980fc44159SYasunori Goto BUG_ON(ret); 2990fc44159SYasunori Goto } 3000fc44159SYasunori Goto 3019af3c2deSYasunori Goto return ret; 3029af3c2deSYasunori Goto error: 3039af3c2deSYasunori Goto /* rollback pgdat allocation and others */ 3049af3c2deSYasunori Goto if (new_pgdat) 3059af3c2deSYasunori Goto rollback_node_hotadd(nid, pgdat); 306ebd15302SKAMEZAWA Hiroyuki if (res) 307ebd15302SKAMEZAWA Hiroyuki release_memory_resource(res); 3089af3c2deSYasunori Goto 309bc02af93SYasunori Goto return ret; 310bc02af93SYasunori Goto } 311bc02af93SYasunori Goto EXPORT_SYMBOL_GPL(add_memory); 312