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