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