0b11a85434415b69064c26ede9669788920101d8
[safe/jmp/linux-2.6] / mm / memory_hotplug.c
1 /*
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>
24 #include <linux/ioport.h>
25
26 #include <asm/tlbflush.h>
27
28 extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
29                           unsigned long size);
30 static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
31 {
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;
38         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         }
44         memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn);
45         zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages);
46         return 0;
47 }
48
49 extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
50                                   int nr_pages);
51 static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
52 {
53         int nr_pages = PAGES_PER_SECTION;
54         int ret;
55
56         ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
57
58         if (ret < 0)
59                 return ret;
60
61         ret = __add_zone(zone, phys_start_pfn);
62
63         if (ret < 0)
64                 return ret;
65
66         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  */
75 int __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
84                 /* We want to keep adding the rest of the
85                  * sections if the first ones already exist
86                  */
87                 if (err && (err != -EEXIST))
88                         break;
89         }
90
91         return err;
92 }
93 EXPORT_SYMBOL_GPL(__add_pages);
94
95 static 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
106         zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
107                                 zone->zone_start_pfn;
108
109         zone_span_writeunlock(zone);
110 }
111
112 static 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
121         pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
122                                         pgdat->node_start_pfn;
123 }
124
125 int online_pages(unsigned long pfn, unsigned long nr_pages)
126 {
127         unsigned long i;
128         unsigned long flags;
129         unsigned long onlined_pages = 0;
130         struct zone *zone;
131         int need_zonelists_rebuild = 0;
132
133         /*
134          * This doesn't need a lock to do pfn_to_page().
135          * The section can't be removed here because of the
136          * memory_block->state_sem.
137          */
138         zone = page_zone(pfn_to_page(pfn));
139         pgdat_resize_lock(zone->zone_pgdat, &flags);
140         grow_zone_span(zone, pfn, pfn + nr_pages);
141         grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
142         pgdat_resize_unlock(zone->zone_pgdat, &flags);
143
144         /*
145          * If this zone is not populated, then it is not in zonelist.
146          * This means the page allocator ignores this zone.
147          * So, zonelist must be updated after online.
148          */
149         if (!populated_zone(zone))
150                 need_zonelists_rebuild = 1;
151
152         for (i = 0; i < nr_pages; i++) {
153                 struct page *page = pfn_to_page(pfn + i);
154                 online_page(page);
155                 onlined_pages++;
156         }
157         zone->present_pages += onlined_pages;
158         zone->zone_pgdat->node_present_pages += onlined_pages;
159
160         setup_per_zone_pages_min();
161
162         if (need_zonelists_rebuild)
163                 build_all_zonelists();
164         vm_total_pages = nr_free_pagecache_pages();
165         return 0;
166 }
167
168 static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
169 {
170         struct pglist_data *pgdat;
171         unsigned long zones_size[MAX_NR_ZONES] = {0};
172         unsigned long zholes_size[MAX_NR_ZONES] = {0};
173         unsigned long start_pfn = start >> PAGE_SHIFT;
174
175         pgdat = arch_alloc_nodedata(nid);
176         if (!pgdat)
177                 return NULL;
178
179         arch_refresh_nodedata(nid, pgdat);
180
181         /* we can use NODE_DATA(nid) from here */
182
183         /* init node's zones as empty zones, we don't have any present pages.*/
184         free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
185
186         return pgdat;
187 }
188
189 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
190 {
191         arch_refresh_nodedata(nid, NULL);
192         arch_free_nodedata(pgdat);
193         return;
194 }
195
196 /* add this memory to iomem resource */
197 static void register_memory_resource(u64 start, u64 size)
198 {
199         struct resource *res;
200
201         res = kzalloc(sizeof(struct resource), GFP_KERNEL);
202         BUG_ON(!res);
203
204         res->name = "System RAM";
205         res->start = start;
206         res->end = start + size - 1;
207         res->flags = IORESOURCE_MEM;
208         if (request_resource(&iomem_resource, res) < 0) {
209                 printk("System RAM resource %llx - %llx cannot be added\n",
210                 (unsigned long long)res->start, (unsigned long long)res->end);
211                 kfree(res);
212         }
213 }
214
215
216
217 int add_memory(int nid, u64 start, u64 size)
218 {
219         pg_data_t *pgdat = NULL;
220         int new_pgdat = 0;
221         int ret;
222
223         if (!node_online(nid)) {
224                 pgdat = hotadd_new_pgdat(nid, start);
225                 if (!pgdat)
226                         return -ENOMEM;
227                 new_pgdat = 1;
228                 ret = kswapd_run(nid);
229                 if (ret)
230                         goto error;
231         }
232
233         /* call arch's memory hotadd */
234         ret = arch_add_memory(nid, start, size);
235
236         if (ret < 0)
237                 goto error;
238
239         /* we online node here. we have no error path from here. */
240         node_set_online(nid);
241
242         /* register this memory as resource */
243         register_memory_resource(start, size);
244
245         return ret;
246 error:
247         /* rollback pgdat allocation and others */
248         if (new_pgdat)
249                 rollback_node_hotadd(nid, pgdat);
250
251         return ret;
252 }
253 EXPORT_SYMBOL_GPL(add_memory);