]> git.proxmox.com Git - mirror_qemu.git/blob - numa.c
numa,pc-dimm: Store pc-dimm memory information in numa_info
[mirror_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/numa.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 "hw/boards.h"
35 #include "sysemu/hostmem.h"
36 #include "qmp-commands.h"
37 #include "hw/mem/pc-dimm.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40
41 QemuOptsList qemu_numa_opts = {
42 .name = "numa",
43 .implied_opt_name = "type",
44 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
45 .desc = { { 0 } } /* validated with OptsVisitor */
46 };
47
48 static int have_memdevs = -1;
49 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
50 * For all nodes, nodeid < max_numa_nodeid
51 */
52 int nb_numa_nodes;
53 NodeInfo numa_info[MAX_NODES];
54
55 void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
56 {
57 struct numa_addr_range *range = g_malloc0(sizeof(*range));
58
59 range->mem_start = addr;
60 range->mem_end = addr + size - 1;
61 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
62 }
63
64 void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
65 {
66 struct numa_addr_range *range, *next;
67
68 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
69 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
70 QLIST_REMOVE(range, entry);
71 g_free(range);
72 return;
73 }
74 }
75 }
76
77 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
78 {
79 uint16_t nodenr;
80 uint16List *cpus = NULL;
81
82 if (node->has_nodeid) {
83 nodenr = node->nodeid;
84 } else {
85 nodenr = nb_numa_nodes;
86 }
87
88 if (nodenr >= MAX_NODES) {
89 error_setg(errp, "Max number of NUMA nodes reached: %"
90 PRIu16 "", nodenr);
91 return;
92 }
93
94 if (numa_info[nodenr].present) {
95 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
96 return;
97 }
98
99 for (cpus = node->cpus; cpus; cpus = cpus->next) {
100 if (cpus->value >= max_cpus) {
101 error_setg(errp,
102 "CPU index (%" PRIu16 ")"
103 " should be smaller than maxcpus (%d)",
104 cpus->value, max_cpus);
105 return;
106 }
107 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
108 }
109
110 if (node->has_mem && node->has_memdev) {
111 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
112 return;
113 }
114
115 if (have_memdevs == -1) {
116 have_memdevs = node->has_memdev;
117 }
118 if (node->has_memdev != have_memdevs) {
119 error_setg(errp, "qemu: memdev option must be specified for either "
120 "all or no nodes");
121 return;
122 }
123
124 if (node->has_mem) {
125 uint64_t mem_size = node->mem;
126 const char *mem_str = qemu_opt_get(opts, "mem");
127 /* Fix up legacy suffix-less format */
128 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
129 mem_size <<= 20;
130 }
131 numa_info[nodenr].node_mem = mem_size;
132 }
133 if (node->has_memdev) {
134 Object *o;
135 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
136 if (!o) {
137 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
138 return;
139 }
140
141 object_ref(o);
142 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
143 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
144 }
145 numa_info[nodenr].present = true;
146 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
147 }
148
149 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
150 {
151 NumaOptions *object = NULL;
152 Error *err = NULL;
153
154 {
155 OptsVisitor *ov = opts_visitor_new(opts);
156 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
157 opts_visitor_cleanup(ov);
158 }
159
160 if (err) {
161 goto error;
162 }
163
164 switch (object->kind) {
165 case NUMA_OPTIONS_KIND_NODE:
166 numa_node_parse(object->node, opts, &err);
167 if (err) {
168 goto error;
169 }
170 nb_numa_nodes++;
171 break;
172 default:
173 abort();
174 }
175
176 return 0;
177
178 error:
179 error_report_err(err);
180
181 if (object) {
182 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
183 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
184 &object, NULL, NULL);
185 qapi_dealloc_visitor_cleanup(dv);
186 }
187
188 return -1;
189 }
190
191 static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
192 {
193 int cpu;
194 bool first = true;
195 GString *s = g_string_new(NULL);
196
197 for (cpu = find_first_bit(cpus, max_cpus);
198 cpu < max_cpus;
199 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
200 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
201 first = false;
202 }
203 return g_string_free(s, FALSE);
204 }
205
206 static void validate_numa_cpus(void)
207 {
208 int i;
209 DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
210
211 bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
212 for (i = 0; i < nb_numa_nodes; i++) {
213 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
214 MAX_CPUMASK_BITS)) {
215 bitmap_and(seen_cpus, seen_cpus,
216 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
217 error_report("CPU(s) present in multiple NUMA nodes: %s",
218 enumerate_cpus(seen_cpus, max_cpus));;
219 exit(EXIT_FAILURE);
220 }
221 bitmap_or(seen_cpus, seen_cpus,
222 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
223 }
224
225 if (!bitmap_full(seen_cpus, max_cpus)) {
226 char *msg;
227 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
228 msg = enumerate_cpus(seen_cpus, max_cpus);
229 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
230 error_report("warning: All CPU(s) up to maxcpus should be described "
231 "in NUMA config");
232 g_free(msg);
233 }
234 }
235
236 void parse_numa_opts(MachineClass *mc)
237 {
238 int i;
239
240 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
241 exit(1);
242 }
243
244 assert(max_numa_nodeid <= MAX_NODES);
245
246 /* No support for sparse NUMA node IDs yet: */
247 for (i = max_numa_nodeid - 1; i >= 0; i--) {
248 /* Report large node IDs first, to make mistakes easier to spot */
249 if (!numa_info[i].present) {
250 error_report("numa: Node ID missing: %d", i);
251 exit(1);
252 }
253 }
254
255 /* This must be always true if all nodes are present: */
256 assert(nb_numa_nodes == max_numa_nodeid);
257
258 if (nb_numa_nodes > 0) {
259 uint64_t numa_total;
260
261 if (nb_numa_nodes > MAX_NODES) {
262 nb_numa_nodes = MAX_NODES;
263 }
264
265 /* If no memory size is given for any node, assume the default case
266 * and distribute the available memory equally across all nodes
267 */
268 for (i = 0; i < nb_numa_nodes; i++) {
269 if (numa_info[i].node_mem != 0) {
270 break;
271 }
272 }
273 if (i == nb_numa_nodes) {
274 uint64_t usedmem = 0;
275
276 /* On Linux, each node's border has to be 8MB aligned,
277 * the final node gets the rest.
278 */
279 for (i = 0; i < nb_numa_nodes - 1; i++) {
280 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
281 ~((1 << 23UL) - 1);
282 usedmem += numa_info[i].node_mem;
283 }
284 numa_info[i].node_mem = ram_size - usedmem;
285 }
286
287 numa_total = 0;
288 for (i = 0; i < nb_numa_nodes; i++) {
289 numa_total += numa_info[i].node_mem;
290 }
291 if (numa_total != ram_size) {
292 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
293 " should equal RAM size (0x" RAM_ADDR_FMT ")",
294 numa_total, ram_size);
295 exit(1);
296 }
297
298 for (i = 0; i < nb_numa_nodes; i++) {
299 QLIST_INIT(&numa_info[i].addr);
300 }
301
302 for (i = 0; i < nb_numa_nodes; i++) {
303 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
304 break;
305 }
306 }
307 /* Historically VCPUs were assigned in round-robin order to NUMA
308 * nodes. However it causes issues with guest not handling it nice
309 * in case where cores/threads from a multicore CPU appear on
310 * different nodes. So allow boards to override default distribution
311 * rule grouping VCPUs by socket so that VCPUs from the same socket
312 * would be on the same node.
313 */
314 if (i == nb_numa_nodes) {
315 for (i = 0; i < max_cpus; i++) {
316 unsigned node_id = i % nb_numa_nodes;
317 if (mc->cpu_index_to_socket_id) {
318 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
319 }
320
321 set_bit(i, numa_info[node_id].node_cpu);
322 }
323 }
324
325 validate_numa_cpus();
326 }
327 }
328
329 void numa_post_machine_init(void)
330 {
331 CPUState *cpu;
332 int i;
333
334 CPU_FOREACH(cpu) {
335 for (i = 0; i < nb_numa_nodes; i++) {
336 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
337 cpu->numa_node = i;
338 }
339 }
340 }
341 }
342
343 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
344 const char *name,
345 uint64_t ram_size)
346 {
347 if (mem_path) {
348 #ifdef __linux__
349 Error *err = NULL;
350 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
351 mem_path, &err);
352
353 /* Legacy behavior: if allocation failed, fall back to
354 * regular RAM allocation.
355 */
356 if (err) {
357 error_report_err(err);
358 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
359 }
360 #else
361 fprintf(stderr, "-mem-path not supported on this host\n");
362 exit(1);
363 #endif
364 } else {
365 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
366 }
367 vmstate_register_ram_global(mr);
368 }
369
370 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
371 const char *name,
372 uint64_t ram_size)
373 {
374 uint64_t addr = 0;
375 int i;
376
377 if (nb_numa_nodes == 0 || !have_memdevs) {
378 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
379 return;
380 }
381
382 memory_region_init(mr, owner, name, ram_size);
383 for (i = 0; i < MAX_NODES; i++) {
384 Error *local_err = NULL;
385 uint64_t size = numa_info[i].node_mem;
386 HostMemoryBackend *backend = numa_info[i].node_memdev;
387 if (!backend) {
388 continue;
389 }
390 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
391 if (local_err) {
392 error_report_err(local_err);
393 exit(1);
394 }
395
396 if (memory_region_is_mapped(seg)) {
397 char *path = object_get_canonical_path_component(OBJECT(backend));
398 error_report("memory backend %s is used multiple times. Each "
399 "-numa option must use a different memdev value.",
400 path);
401 exit(1);
402 }
403
404 memory_region_add_subregion(mr, addr, seg);
405 vmstate_register_ram_global(seg);
406 addr += size;
407 }
408 }
409
410 static void numa_stat_memory_devices(uint64_t node_mem[])
411 {
412 MemoryDeviceInfoList *info_list = NULL;
413 MemoryDeviceInfoList **prev = &info_list;
414 MemoryDeviceInfoList *info;
415
416 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
417 for (info = info_list; info; info = info->next) {
418 MemoryDeviceInfo *value = info->value;
419
420 if (value) {
421 switch (value->kind) {
422 case MEMORY_DEVICE_INFO_KIND_DIMM:
423 node_mem[value->dimm->node] += value->dimm->size;
424 break;
425 default:
426 break;
427 }
428 }
429 }
430 qapi_free_MemoryDeviceInfoList(info_list);
431 }
432
433 void query_numa_node_mem(uint64_t node_mem[])
434 {
435 int i;
436
437 if (nb_numa_nodes <= 0) {
438 return;
439 }
440
441 numa_stat_memory_devices(node_mem);
442 for (i = 0; i < nb_numa_nodes; i++) {
443 node_mem[i] += numa_info[i].node_mem;
444 }
445 }
446
447 static int query_memdev(Object *obj, void *opaque)
448 {
449 MemdevList **list = opaque;
450 MemdevList *m = NULL;
451 Error *err = NULL;
452
453 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
454 m = g_malloc0(sizeof(*m));
455
456 m->value = g_malloc0(sizeof(*m->value));
457
458 m->value->size = object_property_get_int(obj, "size",
459 &err);
460 if (err) {
461 goto error;
462 }
463
464 m->value->merge = object_property_get_bool(obj, "merge",
465 &err);
466 if (err) {
467 goto error;
468 }
469
470 m->value->dump = object_property_get_bool(obj, "dump",
471 &err);
472 if (err) {
473 goto error;
474 }
475
476 m->value->prealloc = object_property_get_bool(obj,
477 "prealloc", &err);
478 if (err) {
479 goto error;
480 }
481
482 m->value->policy = object_property_get_enum(obj,
483 "policy",
484 "HostMemPolicy",
485 &err);
486 if (err) {
487 goto error;
488 }
489
490 object_property_get_uint16List(obj, "host-nodes",
491 &m->value->host_nodes, &err);
492 if (err) {
493 goto error;
494 }
495
496 m->next = *list;
497 *list = m;
498 }
499
500 return 0;
501 error:
502 g_free(m->value);
503 g_free(m);
504
505 return -1;
506 }
507
508 MemdevList *qmp_query_memdev(Error **errp)
509 {
510 Object *obj;
511 MemdevList *list = NULL;
512
513 obj = object_get_objects_root();
514 if (obj == NULL) {
515 return NULL;
516 }
517
518 if (object_child_foreach(obj, query_memdev, &list) != 0) {
519 goto error;
520 }
521
522 return list;
523
524 error:
525 qapi_free_MemdevList(list);
526 return NULL;
527 }