]> git.proxmox.com Git - mirror_qemu.git/blame - numa.c
numa: Fix off-by-one error at MAX_CPUMASK_BITS check
[mirror_qemu.git] / numa.c
CommitLineData
96d0e26c
WG
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
e35704ba 25#include "sysemu/numa.h"
96d0e26c
WG
26#include "exec/cpu-common.h"
27#include "qemu/bitmap.h"
28#include "qom/cpu.h"
2b631ec2
WG
29#include "qemu/error-report.h"
30#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
0042109a
WG
31#include "qapi-visit.h"
32#include "qapi/opts-visitor.h"
33#include "qapi/dealloc-visitor.h"
34#include "qapi/qmp/qerror.h"
dfabb8b9 35#include "hw/boards.h"
7febe36f 36#include "sysemu/hostmem.h"
76b5d850 37#include "qmp-commands.h"
5b009e40 38#include "hw/mem/pc-dimm.h"
7dcd1d70
EH
39#include "qemu/option.h"
40#include "qemu/config-file.h"
0042109a
WG
41
42QemuOptsList qemu_numa_opts = {
43 .name = "numa",
44 .implied_opt_name = "type",
45 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
46 .desc = { { 0 } } /* validated with OptsVisitor */
47};
48
7febe36f 49static int have_memdevs = -1;
25712ffe
EH
50static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
52 */
de1a7c84 53int nb_numa_nodes;
de1a7c84 54NodeInfo numa_info[MAX_NODES];
7febe36f 55
0042109a 56static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
96d0e26c 57{
0042109a
WG
58 uint16_t nodenr;
59 uint16List *cpus = NULL;
96d0e26c 60
0042109a
WG
61 if (node->has_nodeid) {
62 nodenr = node->nodeid;
96d0e26c 63 } else {
0042109a 64 nodenr = nb_numa_nodes;
96d0e26c
WG
65 }
66
0042109a
WG
67 if (nodenr >= MAX_NODES) {
68 error_setg(errp, "Max number of NUMA nodes reached: %"
01bbbcf4 69 PRIu16 "", nodenr);
0042109a 70 return;
96d0e26c
WG
71 }
72
1945b9d8
EH
73 if (numa_info[nodenr].present) {
74 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
75 return;
76 }
77
0042109a 78 for (cpus = node->cpus; cpus; cpus = cpus->next) {
ed26b922 79 if (cpus->value >= MAX_CPUMASK_BITS) {
0042109a 80 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
ed26b922 81 cpus->value, MAX_CPUMASK_BITS - 1);
0042109a
WG
82 return;
83 }
84 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
96d0e26c
WG
85 }
86
7febe36f 87 if (node->has_mem && node->has_memdev) {
01bbbcf4 88 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
7febe36f
PB
89 return;
90 }
91
92 if (have_memdevs == -1) {
93 have_memdevs = node->has_memdev;
94 }
95 if (node->has_memdev != have_memdevs) {
96 error_setg(errp, "qemu: memdev option must be specified for either "
01bbbcf4 97 "all or no nodes");
7febe36f
PB
98 return;
99 }
100
0042109a
WG
101 if (node->has_mem) {
102 uint64_t mem_size = node->mem;
103 const char *mem_str = qemu_opt_get(opts, "mem");
104 /* Fix up legacy suffix-less format */
105 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
106 mem_size <<= 20;
107 }
108 numa_info[nodenr].node_mem = mem_size;
109 }
7febe36f
PB
110 if (node->has_memdev) {
111 Object *o;
112 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
113 if (!o) {
114 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
115 return;
116 }
117
118 object_ref(o);
119 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
120 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
121 }
1af878e0
EH
122 numa_info[nodenr].present = true;
123 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
96d0e26c
WG
124}
125
1c1e6732 126static int parse_numa(QemuOpts *opts, void *opaque)
96d0e26c 127{
0042109a
WG
128 NumaOptions *object = NULL;
129 Error *err = NULL;
96d0e26c 130
0042109a
WG
131 {
132 OptsVisitor *ov = opts_visitor_new(opts);
133 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
134 opts_visitor_cleanup(ov);
96d0e26c 135 }
96d0e26c 136
0042109a
WG
137 if (err) {
138 goto error;
139 }
96d0e26c 140
0042109a
WG
141 switch (object->kind) {
142 case NUMA_OPTIONS_KIND_NODE:
143 numa_node_parse(object->node, opts, &err);
144 if (err) {
145 goto error;
96d0e26c 146 }
0042109a
WG
147 nb_numa_nodes++;
148 break;
149 default:
150 abort();
151 }
96d0e26c 152
0042109a 153 return 0;
96d0e26c 154
0042109a 155error:
29b762f5 156 error_report_err(err);
0042109a
WG
157
158 if (object) {
159 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
160 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
161 &object, NULL, NULL);
162 qapi_dealloc_visitor_cleanup(dv);
96d0e26c 163 }
0042109a
WG
164
165 return -1;
96d0e26c
WG
166}
167
1c1e6732 168void parse_numa_opts(void)
96d0e26c 169{
12d6e464
EH
170 int i;
171
1c1e6732 172 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa,
7dcd1d70
EH
173 NULL, 1) != 0) {
174 exit(1);
175 }
176
12d6e464
EH
177 assert(max_numa_nodeid <= MAX_NODES);
178
179 /* No support for sparse NUMA node IDs yet: */
180 for (i = max_numa_nodeid - 1; i >= 0; i--) {
181 /* Report large node IDs first, to make mistakes easier to spot */
182 if (!numa_info[i].present) {
183 error_report("numa: Node ID missing: %d", i);
184 exit(1);
185 }
186 }
187
188 /* This must be always true if all nodes are present: */
189 assert(nb_numa_nodes == max_numa_nodeid);
190
96d0e26c 191 if (nb_numa_nodes > 0) {
2b631ec2 192 uint64_t numa_total;
96d0e26c
WG
193
194 if (nb_numa_nodes > MAX_NODES) {
195 nb_numa_nodes = MAX_NODES;
196 }
197
9851d0fe 198 /* If no memory size is given for any node, assume the default case
96d0e26c
WG
199 * and distribute the available memory equally across all nodes
200 */
201 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 202 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
203 break;
204 }
205 }
206 if (i == nb_numa_nodes) {
207 uint64_t usedmem = 0;
208
d75e2f68 209 /* On Linux, each node's border has to be 8MB aligned,
96d0e26c
WG
210 * the final node gets the rest.
211 */
212 for (i = 0; i < nb_numa_nodes - 1; i++) {
8c85901e
WG
213 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
214 ~((1 << 23UL) - 1);
215 usedmem += numa_info[i].node_mem;
96d0e26c 216 }
8c85901e 217 numa_info[i].node_mem = ram_size - usedmem;
96d0e26c
WG
218 }
219
2b631ec2
WG
220 numa_total = 0;
221 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 222 numa_total += numa_info[i].node_mem;
2b631ec2
WG
223 }
224 if (numa_total != ram_size) {
c68233ae
HT
225 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
226 " should equal RAM size (0x" RAM_ADDR_FMT ")",
2b631ec2
WG
227 numa_total, ram_size);
228 exit(1);
229 }
230
96d0e26c 231 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 232 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
96d0e26c
WG
233 break;
234 }
235 }
236 /* assigning the VCPUs round-robin is easier to implement, guest OSes
237 * must cope with this anyway, because there are BIOSes out there in
238 * real machines which also use this scheme.
239 */
240 if (i == nb_numa_nodes) {
241 for (i = 0; i < max_cpus; i++) {
8c85901e 242 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
96d0e26c
WG
243 }
244 }
245 }
246}
247
dde11116 248void numa_post_machine_init(void)
96d0e26c
WG
249{
250 CPUState *cpu;
251 int i;
252
253 CPU_FOREACH(cpu) {
254 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 255 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
96d0e26c
WG
256 cpu->numa_node = i;
257 }
258 }
259 }
260}
dfabb8b9 261
7febe36f
PB
262static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
263 const char *name,
264 uint64_t ram_size)
265{
0b183fc8
PB
266 if (mem_path) {
267#ifdef __linux__
7f56e740 268 Error *err = NULL;
dbcb8981 269 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
7f56e740
PB
270 mem_path, &err);
271
272 /* Legacy behavior: if allocation failed, fall back to
273 * regular RAM allocation.
274 */
c3ba3095 275 if (err) {
29b762f5 276 error_report_err(err);
49946538 277 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
7f56e740 278 }
0b183fc8
PB
279#else
280 fprintf(stderr, "-mem-path not supported on this host\n");
281 exit(1);
282#endif
283 } else {
49946538 284 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
0b183fc8 285 }
7febe36f
PB
286 vmstate_register_ram_global(mr);
287}
288
dfabb8b9
PB
289void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
290 const char *name,
291 uint64_t ram_size)
292{
7febe36f
PB
293 uint64_t addr = 0;
294 int i;
295
296 if (nb_numa_nodes == 0 || !have_memdevs) {
297 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
298 return;
299 }
300
301 memory_region_init(mr, owner, name, ram_size);
302 for (i = 0; i < MAX_NODES; i++) {
303 Error *local_err = NULL;
304 uint64_t size = numa_info[i].node_mem;
305 HostMemoryBackend *backend = numa_info[i].node_memdev;
306 if (!backend) {
307 continue;
308 }
309 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
310 if (local_err) {
29b762f5 311 error_report_err(local_err);
7febe36f
PB
312 exit(1);
313 }
314
0462faee
HT
315 if (memory_region_is_mapped(seg)) {
316 char *path = object_get_canonical_path_component(OBJECT(backend));
317 error_report("memory backend %s is used multiple times. Each "
318 "-numa option must use a different memdev value.",
319 path);
320 exit(1);
321 }
322
7febe36f
PB
323 memory_region_add_subregion(mr, addr, seg);
324 vmstate_register_ram_global(seg);
325 addr += size;
326 }
dfabb8b9 327}
76b5d850 328
5b009e40
HZ
329static void numa_stat_memory_devices(uint64_t node_mem[])
330{
331 MemoryDeviceInfoList *info_list = NULL;
332 MemoryDeviceInfoList **prev = &info_list;
333 MemoryDeviceInfoList *info;
334
335 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
336 for (info = info_list; info; info = info->next) {
337 MemoryDeviceInfo *value = info->value;
338
339 if (value) {
340 switch (value->kind) {
341 case MEMORY_DEVICE_INFO_KIND_DIMM:
342 node_mem[value->dimm->node] += value->dimm->size;
343 break;
344 default:
345 break;
346 }
347 }
348 }
349 qapi_free_MemoryDeviceInfoList(info_list);
350}
351
352void query_numa_node_mem(uint64_t node_mem[])
353{
354 int i;
355
356 if (nb_numa_nodes <= 0) {
357 return;
358 }
359
360 numa_stat_memory_devices(node_mem);
361 for (i = 0; i < nb_numa_nodes; i++) {
362 node_mem[i] += numa_info[i].node_mem;
363 }
364}
365
76b5d850
HT
366static int query_memdev(Object *obj, void *opaque)
367{
368 MemdevList **list = opaque;
b0e90181 369 MemdevList *m = NULL;
76b5d850
HT
370 Error *err = NULL;
371
372 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
b0e90181 373 m = g_malloc0(sizeof(*m));
76b5d850
HT
374
375 m->value = g_malloc0(sizeof(*m->value));
376
377 m->value->size = object_property_get_int(obj, "size",
378 &err);
379 if (err) {
380 goto error;
381 }
382
383 m->value->merge = object_property_get_bool(obj, "merge",
384 &err);
385 if (err) {
386 goto error;
387 }
388
389 m->value->dump = object_property_get_bool(obj, "dump",
390 &err);
391 if (err) {
392 goto error;
393 }
394
395 m->value->prealloc = object_property_get_bool(obj,
396 "prealloc", &err);
397 if (err) {
398 goto error;
399 }
400
401 m->value->policy = object_property_get_enum(obj,
402 "policy",
403 HostMemPolicy_lookup,
404 &err);
405 if (err) {
406 goto error;
407 }
408
409 object_property_get_uint16List(obj, "host-nodes",
410 &m->value->host_nodes, &err);
411 if (err) {
412 goto error;
413 }
414
415 m->next = *list;
416 *list = m;
417 }
418
419 return 0;
420error:
b0e90181
CF
421 g_free(m->value);
422 g_free(m);
423
76b5d850
HT
424 return -1;
425}
426
427MemdevList *qmp_query_memdev(Error **errp)
428{
429 Object *obj;
ecaf54a0 430 MemdevList *list = NULL;
76b5d850
HT
431
432 obj = object_resolve_path("/objects", NULL);
433 if (obj == NULL) {
434 return NULL;
435 }
436
437 if (object_child_foreach(obj, query_memdev, &list) != 0) {
438 goto error;
439 }
440
441 return list;
442
443error:
ecaf54a0 444 qapi_free_MemdevList(list);
76b5d850
HT
445 return NULL;
446}