numa: handle mmaped memory allocation failure correctly
[qemu.git] / numa.c
1 /*
2 * NUMA parameter parsing routines
3 *
4 * Copyright (c) 2014 Fujitsu Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "sysemu/sysemu.h"
26 #include "exec/cpu-common.h"
27 #include "qemu/bitmap.h"
28 #include "qom/cpu.h"
29 #include "qemu/error-report.h"
30 #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
31 #include "qapi-visit.h"
32 #include "qapi/opts-visitor.h"
33 #include "qapi/dealloc-visitor.h"
34 #include "qapi/qmp/qerror.h"
35 #include "hw/boards.h"
36 #include "sysemu/hostmem.h"
37 #include "qmp-commands.h"
38
39 QemuOptsList qemu_numa_opts = {
40 .name = "numa",
41 .implied_opt_name = "type",
42 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
43 .desc = { { 0 } } /* validated with OptsVisitor */
44 };
45
46 static int have_memdevs = -1;
47
48 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
49 {
50 uint16_t nodenr;
51 uint16List *cpus = NULL;
52
53 if (node->has_nodeid) {
54 nodenr = node->nodeid;
55 } else {
56 nodenr = nb_numa_nodes;
57 }
58
59 if (nodenr >= MAX_NODES) {
60 error_setg(errp, "Max number of NUMA nodes reached: %"
61 PRIu16 "\n", nodenr);
62 return;
63 }
64
65 for (cpus = node->cpus; cpus; cpus = cpus->next) {
66 if (cpus->value > MAX_CPUMASK_BITS) {
67 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
68 cpus->value, MAX_CPUMASK_BITS);
69 return;
70 }
71 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
72 }
73
74 if (node->has_mem && node->has_memdev) {
75 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
76 return;
77 }
78
79 if (have_memdevs == -1) {
80 have_memdevs = node->has_memdev;
81 }
82 if (node->has_memdev != have_memdevs) {
83 error_setg(errp, "qemu: memdev option must be specified for either "
84 "all or no nodes\n");
85 return;
86 }
87
88 if (node->has_mem) {
89 uint64_t mem_size = node->mem;
90 const char *mem_str = qemu_opt_get(opts, "mem");
91 /* Fix up legacy suffix-less format */
92 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
93 mem_size <<= 20;
94 }
95 numa_info[nodenr].node_mem = mem_size;
96 }
97 if (node->has_memdev) {
98 Object *o;
99 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
100 if (!o) {
101 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
102 return;
103 }
104
105 object_ref(o);
106 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
107 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
108 }
109 }
110
111 int numa_init_func(QemuOpts *opts, void *opaque)
112 {
113 NumaOptions *object = NULL;
114 Error *err = NULL;
115
116 {
117 OptsVisitor *ov = opts_visitor_new(opts);
118 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
119 opts_visitor_cleanup(ov);
120 }
121
122 if (err) {
123 goto error;
124 }
125
126 switch (object->kind) {
127 case NUMA_OPTIONS_KIND_NODE:
128 numa_node_parse(object->node, opts, &err);
129 if (err) {
130 goto error;
131 }
132 nb_numa_nodes++;
133 break;
134 default:
135 abort();
136 }
137
138 return 0;
139
140 error:
141 qerror_report_err(err);
142 error_free(err);
143
144 if (object) {
145 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
146 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
147 &object, NULL, NULL);
148 qapi_dealloc_visitor_cleanup(dv);
149 }
150
151 return -1;
152 }
153
154 void set_numa_nodes(void)
155 {
156 if (nb_numa_nodes > 0) {
157 uint64_t numa_total;
158 int i;
159
160 if (nb_numa_nodes > MAX_NODES) {
161 nb_numa_nodes = MAX_NODES;
162 }
163
164 /* If no memory size if given for any node, assume the default case
165 * and distribute the available memory equally across all nodes
166 */
167 for (i = 0; i < nb_numa_nodes; i++) {
168 if (numa_info[i].node_mem != 0) {
169 break;
170 }
171 }
172 if (i == nb_numa_nodes) {
173 uint64_t usedmem = 0;
174
175 /* On Linux, the each node's border has to be 8MB aligned,
176 * the final node gets the rest.
177 */
178 for (i = 0; i < nb_numa_nodes - 1; i++) {
179 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
180 ~((1 << 23UL) - 1);
181 usedmem += numa_info[i].node_mem;
182 }
183 numa_info[i].node_mem = ram_size - usedmem;
184 }
185
186 numa_total = 0;
187 for (i = 0; i < nb_numa_nodes; i++) {
188 numa_total += numa_info[i].node_mem;
189 }
190 if (numa_total != ram_size) {
191 error_report("total memory for NUMA nodes (%" PRIu64 ")"
192 " should equal RAM size (" RAM_ADDR_FMT ")",
193 numa_total, ram_size);
194 exit(1);
195 }
196
197 for (i = 0; i < nb_numa_nodes; i++) {
198 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
199 break;
200 }
201 }
202 /* assigning the VCPUs round-robin is easier to implement, guest OSes
203 * must cope with this anyway, because there are BIOSes out there in
204 * real machines which also use this scheme.
205 */
206 if (i == nb_numa_nodes) {
207 for (i = 0; i < max_cpus; i++) {
208 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
209 }
210 }
211 }
212 }
213
214 void set_numa_modes(void)
215 {
216 CPUState *cpu;
217 int i;
218
219 CPU_FOREACH(cpu) {
220 for (i = 0; i < nb_numa_nodes; i++) {
221 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
222 cpu->numa_node = i;
223 }
224 }
225 }
226 }
227
228 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
229 const char *name,
230 uint64_t ram_size)
231 {
232 if (mem_path) {
233 #ifdef __linux__
234 Error *err = NULL;
235 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
236 mem_path, &err);
237
238 /* Legacy behavior: if allocation failed, fall back to
239 * regular RAM allocation.
240 */
241 if (err) {
242 qerror_report_err(err);
243 error_free(err);
244 memory_region_init_ram(mr, owner, name, ram_size);
245 }
246 #else
247 fprintf(stderr, "-mem-path not supported on this host\n");
248 exit(1);
249 #endif
250 } else {
251 memory_region_init_ram(mr, owner, name, ram_size);
252 }
253 vmstate_register_ram_global(mr);
254 }
255
256 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
257 const char *name,
258 uint64_t ram_size)
259 {
260 uint64_t addr = 0;
261 int i;
262
263 if (nb_numa_nodes == 0 || !have_memdevs) {
264 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
265 return;
266 }
267
268 memory_region_init(mr, owner, name, ram_size);
269 for (i = 0; i < MAX_NODES; i++) {
270 Error *local_err = NULL;
271 uint64_t size = numa_info[i].node_mem;
272 HostMemoryBackend *backend = numa_info[i].node_memdev;
273 if (!backend) {
274 continue;
275 }
276 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
277 if (local_err) {
278 qerror_report_err(local_err);
279 exit(1);
280 }
281
282 memory_region_add_subregion(mr, addr, seg);
283 vmstate_register_ram_global(seg);
284 addr += size;
285 }
286 }
287
288 static int query_memdev(Object *obj, void *opaque)
289 {
290 MemdevList **list = opaque;
291 Error *err = NULL;
292
293 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
294 MemdevList *m = g_malloc0(sizeof(*m));
295
296 m->value = g_malloc0(sizeof(*m->value));
297
298 m->value->size = object_property_get_int(obj, "size",
299 &err);
300 if (err) {
301 goto error;
302 }
303
304 m->value->merge = object_property_get_bool(obj, "merge",
305 &err);
306 if (err) {
307 goto error;
308 }
309
310 m->value->dump = object_property_get_bool(obj, "dump",
311 &err);
312 if (err) {
313 goto error;
314 }
315
316 m->value->prealloc = object_property_get_bool(obj,
317 "prealloc", &err);
318 if (err) {
319 goto error;
320 }
321
322 m->value->policy = object_property_get_enum(obj,
323 "policy",
324 HostMemPolicy_lookup,
325 &err);
326 if (err) {
327 goto error;
328 }
329
330 object_property_get_uint16List(obj, "host-nodes",
331 &m->value->host_nodes, &err);
332 if (err) {
333 goto error;
334 }
335
336 m->next = *list;
337 *list = m;
338 }
339
340 return 0;
341 error:
342 return -1;
343 }
344
345 MemdevList *qmp_query_memdev(Error **errp)
346 {
347 Object *obj;
348 MemdevList *list = NULL, *m;
349
350 obj = object_resolve_path("/objects", NULL);
351 if (obj == NULL) {
352 return NULL;
353 }
354
355 if (object_child_foreach(obj, query_memdev, &list) != 0) {
356 goto error;
357 }
358
359 return list;
360
361 error:
362 while (list) {
363 m = list;
364 list = list->next;
365 g_free(m->value);
366 g_free(m);
367 }
368 return NULL;
369 }