blob: ea4038838b0a2b4c95a46cc3d4f1eaad25556f5f [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
7#include <linux/config.h>
8#include <linux/stddef.h>
9#include <linux/mm.h>
10#include <linux/swap.h>
11#include <linux/interrupt.h>
12#include <linux/pagemap.h>
13#include <linux/bootmem.h>
14#include <linux/compiler.h>
15#include <linux/module.h>
16#include <linux/pagevec.h>
17#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
21#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
Dave Hansen3947be12005-10-29 18:16:54 -070025
26#include <asm/tlbflush.h>
27
Dave Hansen3947be12005-10-29 18:16:54 -070028extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
29 unsigned long size);
Yasunori Goto718127c2006-06-23 02:03:10 -070030static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -070031{
32 struct pglist_data *pgdat = zone->zone_pgdat;
33 int nr_pages = PAGES_PER_SECTION;
34 int nid = pgdat->node_id;
35 int zone_type;
36
37 zone_type = zone - pgdat->node_zones;
Yasunori Goto718127c2006-06-23 02:03:10 -070038 if (!populated_zone(zone)) {
39 int ret = 0;
40 ret = init_currently_empty_zone(zone, phys_start_pfn, nr_pages);
41 if (ret < 0)
42 return ret;
43 }
Dave Hansen3947be12005-10-29 18:16:54 -070044 memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn);
45 zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages);
Yasunori Goto718127c2006-06-23 02:03:10 -070046 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -070047}
48
Dave Hansen0b0acbe2005-10-29 18:16:55 -070049extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
50 int nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -070051static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
52{
Dave Hansen3947be12005-10-29 18:16:54 -070053 int nr_pages = PAGES_PER_SECTION;
Dave Hansen3947be12005-10-29 18:16:54 -070054 int ret;
55
Dave Hansen0b0acbe2005-10-29 18:16:55 -070056 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -070057
58 if (ret < 0)
59 return ret;
60
Yasunori Goto718127c2006-06-23 02:03:10 -070061 ret = __add_zone(zone, phys_start_pfn);
62
63 if (ret < 0)
64 return ret;
65
Dave Hansen3947be12005-10-29 18:16:54 -070066 return register_new_memory(__pfn_to_section(phys_start_pfn));
67}
68
69/*
70 * Reasonably generic function for adding memory. It is
71 * expected that archs that support memory hotplug will
72 * call this function after deciding the zone to which to
73 * add the new pages.
74 */
75int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
76 unsigned long nr_pages)
77{
78 unsigned long i;
79 int err = 0;
80
81 for (i = 0; i < nr_pages; i += PAGES_PER_SECTION) {
82 err = __add_section(zone, phys_start_pfn + i);
83
Joel H Schoppbed120c2006-05-01 12:16:11 -070084 /* We want to keep adding the rest of the
85 * sections if the first ones already exist
86 */
87 if (err && (err != -EEXIST))
Dave Hansen3947be12005-10-29 18:16:54 -070088 break;
89 }
90
91 return err;
92}
Joel H Schoppbed120c2006-05-01 12:16:11 -070093EXPORT_SYMBOL_GPL(__add_pages);
Dave Hansen3947be12005-10-29 18:16:54 -070094
95static void grow_zone_span(struct zone *zone,
96 unsigned long start_pfn, unsigned long end_pfn)
97{
98 unsigned long old_zone_end_pfn;
99
100 zone_span_writelock(zone);
101
102 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
103 if (start_pfn < zone->zone_start_pfn)
104 zone->zone_start_pfn = start_pfn;
105
Yasunori Goto25a6df92006-05-30 21:25:42 -0700106 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
107 zone->zone_start_pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700108
109 zone_span_writeunlock(zone);
110}
111
112static void grow_pgdat_span(struct pglist_data *pgdat,
113 unsigned long start_pfn, unsigned long end_pfn)
114{
115 unsigned long old_pgdat_end_pfn =
116 pgdat->node_start_pfn + pgdat->node_spanned_pages;
117
118 if (start_pfn < pgdat->node_start_pfn)
119 pgdat->node_start_pfn = start_pfn;
120
Yasunori Goto25a6df92006-05-30 21:25:42 -0700121 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
122 pgdat->node_start_pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700123}
124
125int online_pages(unsigned long pfn, unsigned long nr_pages)
126{
127 unsigned long i;
128 unsigned long flags;
129 unsigned long onlined_pages = 0;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700130 struct resource res;
131 u64 section_end;
132 unsigned long start_pfn;
Dave Hansen3947be12005-10-29 18:16:54 -0700133 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700134 int need_zonelists_rebuild = 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700135
136 /*
137 * This doesn't need a lock to do pfn_to_page().
138 * The section can't be removed here because of the
139 * memory_block->state_sem.
140 */
141 zone = page_zone(pfn_to_page(pfn));
142 pgdat_resize_lock(zone->zone_pgdat, &flags);
143 grow_zone_span(zone, pfn, pfn + nr_pages);
144 grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
145 pgdat_resize_unlock(zone->zone_pgdat, &flags);
146
Yasunori Goto68113782006-06-23 02:03:11 -0700147 /*
148 * If this zone is not populated, then it is not in zonelist.
149 * This means the page allocator ignores this zone.
150 * So, zonelist must be updated after online.
151 */
152 if (!populated_zone(zone))
153 need_zonelists_rebuild = 1;
154
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700155 res.start = (u64)pfn << PAGE_SHIFT;
156 res.end = res.start + ((u64)nr_pages << PAGE_SHIFT) - 1;
157 res.flags = IORESOURCE_MEM; /* we just need system ram */
158 section_end = res.end;
159
160 while (find_next_system_ram(&res) >= 0) {
161 start_pfn = (unsigned long)(res.start >> PAGE_SHIFT);
162 nr_pages = (unsigned long)
163 ((res.end + 1 - res.start) >> PAGE_SHIFT);
164
165 if (PageReserved(pfn_to_page(start_pfn))) {
166 /* this region's page is not onlined now */
167 for (i = 0; i < nr_pages; i++) {
168 struct page *page = pfn_to_page(start_pfn + i);
169 online_page(page);
170 onlined_pages++;
171 }
172 }
173
174 res.start = res.end + 1;
175 res.end = section_end;
Dave Hansen3947be12005-10-29 18:16:54 -0700176 }
177 zone->present_pages += onlined_pages;
Yasunori Gotof2937be2006-03-09 17:33:51 -0800178 zone->zone_pgdat->node_present_pages += onlined_pages;
Dave Hansen3947be12005-10-29 18:16:54 -0700179
Dave Hansen61b13992005-10-29 18:16:56 -0700180 setup_per_zone_pages_min();
181
Yasunori Goto68113782006-06-23 02:03:11 -0700182 if (need_zonelists_rebuild)
183 build_all_zonelists();
KAMEZAWA Hiroyuki5a4d4362006-06-23 02:03:47 -0700184 vm_total_pages = nr_free_pagecache_pages();
Dave Hansen3947be12005-10-29 18:16:54 -0700185 return 0;
186}
Yasunori Gotobc02af92006-06-27 02:53:30 -0700187
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700188static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
189{
190 struct pglist_data *pgdat;
191 unsigned long zones_size[MAX_NR_ZONES] = {0};
192 unsigned long zholes_size[MAX_NR_ZONES] = {0};
193 unsigned long start_pfn = start >> PAGE_SHIFT;
194
195 pgdat = arch_alloc_nodedata(nid);
196 if (!pgdat)
197 return NULL;
198
199 arch_refresh_nodedata(nid, pgdat);
200
201 /* we can use NODE_DATA(nid) from here */
202
203 /* init node's zones as empty zones, we don't have any present pages.*/
204 free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
205
206 return pgdat;
207}
208
209static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
210{
211 arch_refresh_nodedata(nid, NULL);
212 arch_free_nodedata(pgdat);
213 return;
214}
215
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700216/* add this memory to iomem resource */
217static void register_memory_resource(u64 start, u64 size)
218{
219 struct resource *res;
220
221 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
222 BUG_ON(!res);
223
224 res->name = "System RAM";
225 res->start = start;
226 res->end = start + size - 1;
227 res->flags = IORESOURCE_MEM;
228 if (request_resource(&iomem_resource, res) < 0) {
229 printk("System RAM resource %llx - %llx cannot be added\n",
230 (unsigned long long)res->start, (unsigned long long)res->end);
231 kfree(res);
232 }
233}
234
235
236
Yasunori Gotobc02af92006-06-27 02:53:30 -0700237int add_memory(int nid, u64 start, u64 size)
238{
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700239 pg_data_t *pgdat = NULL;
240 int new_pgdat = 0;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700241 int ret;
242
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700243 if (!node_online(nid)) {
244 pgdat = hotadd_new_pgdat(nid, start);
245 if (!pgdat)
246 return -ENOMEM;
247 new_pgdat = 1;
248 ret = kswapd_run(nid);
249 if (ret)
250 goto error;
251 }
252
Yasunori Gotobc02af92006-06-27 02:53:30 -0700253 /* call arch's memory hotadd */
254 ret = arch_add_memory(nid, start, size);
255
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700256 if (ret < 0)
257 goto error;
258
Yasunori Goto0fc44152006-06-27 02:53:38 -0700259 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700260 node_set_online(nid);
261
Yasunori Goto0fc44152006-06-27 02:53:38 -0700262 if (new_pgdat) {
263 ret = register_one_node(nid);
264 /*
265 * If sysfs file of new node can't create, cpu on the node
266 * can't be hot-added. There is no rollback way now.
267 * So, check by BUG_ON() to catch it reluctantly..
268 */
269 BUG_ON(ret);
270 }
271
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700272 /* register this memory as resource */
273 register_memory_resource(start, size);
274
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700275 return ret;
276error:
277 /* rollback pgdat allocation and others */
278 if (new_pgdat)
279 rollback_node_hotadd(nid, pgdat);
280
Yasunori Gotobc02af92006-06-27 02:53:30 -0700281 return ret;
282}
283EXPORT_SYMBOL_GPL(add_memory);