]> git.proxmox.com Git - mirror_qemu.git/blob - hw/core/numa.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / hw / core / 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 "qemu/osdep.h"
26 #include "sysemu/numa.h"
27 #include "exec/cpu-common.h"
28 #include "exec/ramlist.h"
29 #include "qemu/bitmap.h"
30 #include "qemu/error-report.h"
31 #include "qapi/error.h"
32 #include "qapi/opts-visitor.h"
33 #include "qapi/qapi-visit-machine.h"
34 #include "hw/mem/pc-dimm.h"
35 #include "hw/mem/memory-device.h"
36 #include "qemu/option.h"
37 #include "qemu/config-file.h"
38 #include "qemu/cutils.h"
39
40 QemuOptsList 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
47 static int have_memdevs = -1;
48 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
49 * For all nodes, nodeid < max_numa_nodeid
50 */
51 int nb_numa_nodes;
52 bool have_numa_distance;
53 NodeInfo numa_info[MAX_NODES];
54
55
56 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
57 Error **errp)
58 {
59 Error *err = NULL;
60 uint16_t nodenr;
61 uint16List *cpus = NULL;
62 MachineClass *mc = MACHINE_GET_CLASS(ms);
63
64 if (node->has_nodeid) {
65 nodenr = node->nodeid;
66 } else {
67 nodenr = nb_numa_nodes;
68 }
69
70 if (nodenr >= MAX_NODES) {
71 error_setg(errp, "Max number of NUMA nodes reached: %"
72 PRIu16 "", nodenr);
73 return;
74 }
75
76 if (numa_info[nodenr].present) {
77 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
78 return;
79 }
80
81 if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
82 error_setg(errp, "NUMA is not supported by this machine-type");
83 return;
84 }
85 for (cpus = node->cpus; cpus; cpus = cpus->next) {
86 CpuInstanceProperties props;
87 if (cpus->value >= max_cpus) {
88 error_setg(errp,
89 "CPU index (%" PRIu16 ")"
90 " should be smaller than maxcpus (%d)",
91 cpus->value, max_cpus);
92 return;
93 }
94 props = mc->cpu_index_to_instance_props(ms, cpus->value);
95 props.node_id = nodenr;
96 props.has_node_id = true;
97 machine_set_cpu_numa_node(ms, &props, &err);
98 if (err) {
99 error_propagate(errp, err);
100 return;
101 }
102 }
103
104 if (node->has_mem && node->has_memdev) {
105 error_setg(errp, "cannot specify both mem= and memdev=");
106 return;
107 }
108
109 if (have_memdevs == -1) {
110 have_memdevs = node->has_memdev;
111 }
112 if (node->has_memdev != have_memdevs) {
113 error_setg(errp, "memdev option must be specified for either "
114 "all or no nodes");
115 return;
116 }
117
118 if (node->has_mem) {
119 numa_info[nodenr].node_mem = node->mem;
120 }
121 if (node->has_memdev) {
122 Object *o;
123 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
124 if (!o) {
125 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
126 return;
127 }
128
129 object_ref(o);
130 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
131 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
132 }
133 numa_info[nodenr].present = true;
134 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
135 nb_numa_nodes++;
136 }
137
138 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
139 {
140 uint16_t src = dist->src;
141 uint16_t dst = dist->dst;
142 uint8_t val = dist->val;
143
144 if (src >= MAX_NODES || dst >= MAX_NODES) {
145 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
146 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
147 return;
148 }
149
150 if (!numa_info[src].present || !numa_info[dst].present) {
151 error_setg(errp, "Source/Destination NUMA node is missing. "
152 "Please use '-numa node' option to declare it first.");
153 return;
154 }
155
156 if (val < NUMA_DISTANCE_MIN) {
157 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
158 "it shouldn't be less than %d.",
159 val, NUMA_DISTANCE_MIN);
160 return;
161 }
162
163 if (src == dst && val != NUMA_DISTANCE_MIN) {
164 error_setg(errp, "Local distance of node %d should be %d.",
165 src, NUMA_DISTANCE_MIN);
166 return;
167 }
168
169 numa_info[src].distance[dst] = val;
170 have_numa_distance = true;
171 }
172
173 void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
174 {
175 Error *err = NULL;
176
177 switch (object->type) {
178 case NUMA_OPTIONS_TYPE_NODE:
179 parse_numa_node(ms, &object->u.node, &err);
180 if (err) {
181 goto end;
182 }
183 break;
184 case NUMA_OPTIONS_TYPE_DIST:
185 parse_numa_distance(&object->u.dist, &err);
186 if (err) {
187 goto end;
188 }
189 break;
190 case NUMA_OPTIONS_TYPE_CPU:
191 if (!object->u.cpu.has_node_id) {
192 error_setg(&err, "Missing mandatory node-id property");
193 goto end;
194 }
195 if (!numa_info[object->u.cpu.node_id].present) {
196 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
197 "defined with -numa node,nodeid=ID before it's used with "
198 "-numa cpu,node-id=ID", object->u.cpu.node_id);
199 goto end;
200 }
201
202 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
203 &err);
204 break;
205 default:
206 abort();
207 }
208
209 end:
210 error_propagate(errp, err);
211 }
212
213 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
214 {
215 NumaOptions *object = NULL;
216 MachineState *ms = MACHINE(opaque);
217 Error *err = NULL;
218 Visitor *v = opts_visitor_new(opts);
219
220 visit_type_NumaOptions(v, NULL, &object, &err);
221 visit_free(v);
222 if (err) {
223 goto end;
224 }
225
226 /* Fix up legacy suffix-less format */
227 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
228 const char *mem_str = qemu_opt_get(opts, "mem");
229 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
230 }
231
232 set_numa_options(ms, object, &err);
233
234 end:
235 qapi_free_NumaOptions(object);
236 if (err) {
237 error_propagate(errp, err);
238 return -1;
239 }
240
241 return 0;
242 }
243
244 /* If all node pair distances are symmetric, then only distances
245 * in one direction are enough. If there is even one asymmetric
246 * pair, though, then all distances must be provided. The
247 * distance from a node to itself is always NUMA_DISTANCE_MIN,
248 * so providing it is never necessary.
249 */
250 static void validate_numa_distance(void)
251 {
252 int src, dst;
253 bool is_asymmetrical = false;
254
255 for (src = 0; src < nb_numa_nodes; src++) {
256 for (dst = src; dst < nb_numa_nodes; dst++) {
257 if (numa_info[src].distance[dst] == 0 &&
258 numa_info[dst].distance[src] == 0) {
259 if (src != dst) {
260 error_report("The distance between node %d and %d is "
261 "missing, at least one distance value "
262 "between each nodes should be provided.",
263 src, dst);
264 exit(EXIT_FAILURE);
265 }
266 }
267
268 if (numa_info[src].distance[dst] != 0 &&
269 numa_info[dst].distance[src] != 0 &&
270 numa_info[src].distance[dst] !=
271 numa_info[dst].distance[src]) {
272 is_asymmetrical = true;
273 }
274 }
275 }
276
277 if (is_asymmetrical) {
278 for (src = 0; src < nb_numa_nodes; src++) {
279 for (dst = 0; dst < nb_numa_nodes; dst++) {
280 if (src != dst && numa_info[src].distance[dst] == 0) {
281 error_report("At least one asymmetrical pair of "
282 "distances is given, please provide distances "
283 "for both directions of all node pairs.");
284 exit(EXIT_FAILURE);
285 }
286 }
287 }
288 }
289 }
290
291 static void complete_init_numa_distance(void)
292 {
293 int src, dst;
294
295 /* Fixup NUMA distance by symmetric policy because if it is an
296 * asymmetric distance table, it should be a complete table and
297 * there would not be any missing distance except local node, which
298 * is verified by validate_numa_distance above.
299 */
300 for (src = 0; src < nb_numa_nodes; src++) {
301 for (dst = 0; dst < nb_numa_nodes; dst++) {
302 if (numa_info[src].distance[dst] == 0) {
303 if (src == dst) {
304 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
305 } else {
306 numa_info[src].distance[dst] = numa_info[dst].distance[src];
307 }
308 }
309 }
310 }
311 }
312
313 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
314 int nb_nodes, ram_addr_t size)
315 {
316 int i;
317 uint64_t usedmem = 0;
318
319 /* Align each node according to the alignment
320 * requirements of the machine class
321 */
322
323 for (i = 0; i < nb_nodes - 1; i++) {
324 nodes[i].node_mem = (size / nb_nodes) &
325 ~((1 << mc->numa_mem_align_shift) - 1);
326 usedmem += nodes[i].node_mem;
327 }
328 nodes[i].node_mem = size - usedmem;
329 }
330
331 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
332 int nb_nodes, ram_addr_t size)
333 {
334 int i;
335 uint64_t usedmem = 0, node_mem;
336 uint64_t granularity = size / nb_nodes;
337 uint64_t propagate = 0;
338
339 for (i = 0; i < nb_nodes - 1; i++) {
340 node_mem = (granularity + propagate) &
341 ~((1 << mc->numa_mem_align_shift) - 1);
342 propagate = granularity + propagate - node_mem;
343 nodes[i].node_mem = node_mem;
344 usedmem += node_mem;
345 }
346 nodes[i].node_mem = size - usedmem;
347 }
348
349 void numa_complete_configuration(MachineState *ms)
350 {
351 int i;
352 MachineClass *mc = MACHINE_GET_CLASS(ms);
353
354 /*
355 * If memory hotplug is enabled (slots > 0) but without '-numa'
356 * options explicitly on CLI, guestes will break.
357 *
358 * Windows: won't enable memory hotplug without SRAT table at all
359 *
360 * Linux: if QEMU is started with initial memory all below 4Gb
361 * and no SRAT table present, guest kernel will use nommu DMA ops,
362 * which breaks 32bit hw drivers when memory is hotplugged and
363 * guest tries to use it with that drivers.
364 *
365 * Enable NUMA implicitly by adding a new NUMA node automatically.
366 */
367 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
368 mc->auto_enable_numa_with_memhp) {
369 NumaNodeOptions node = { };
370 parse_numa_node(ms, &node, &error_abort);
371 }
372
373 assert(max_numa_nodeid <= MAX_NODES);
374
375 /* No support for sparse NUMA node IDs yet: */
376 for (i = max_numa_nodeid - 1; i >= 0; i--) {
377 /* Report large node IDs first, to make mistakes easier to spot */
378 if (!numa_info[i].present) {
379 error_report("numa: Node ID missing: %d", i);
380 exit(1);
381 }
382 }
383
384 /* This must be always true if all nodes are present: */
385 assert(nb_numa_nodes == max_numa_nodeid);
386
387 if (nb_numa_nodes > 0) {
388 uint64_t numa_total;
389
390 if (nb_numa_nodes > MAX_NODES) {
391 nb_numa_nodes = MAX_NODES;
392 }
393
394 /* If no memory size is given for any node, assume the default case
395 * and distribute the available memory equally across all nodes
396 */
397 for (i = 0; i < nb_numa_nodes; i++) {
398 if (numa_info[i].node_mem != 0) {
399 break;
400 }
401 }
402 if (i == nb_numa_nodes) {
403 assert(mc->numa_auto_assign_ram);
404 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
405 }
406
407 numa_total = 0;
408 for (i = 0; i < nb_numa_nodes; i++) {
409 numa_total += numa_info[i].node_mem;
410 }
411 if (numa_total != ram_size) {
412 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
413 " should equal RAM size (0x" RAM_ADDR_FMT ")",
414 numa_total, ram_size);
415 exit(1);
416 }
417
418 /* QEMU needs at least all unique node pair distances to build
419 * the whole NUMA distance table. QEMU treats the distance table
420 * as symmetric by default, i.e. distance A->B == distance B->A.
421 * Thus, QEMU is able to complete the distance table
422 * initialization even though only distance A->B is provided and
423 * distance B->A is not. QEMU knows the distance of a node to
424 * itself is always 10, so A->A distances may be omitted. When
425 * the distances of two nodes of a pair differ, i.e. distance
426 * A->B != distance B->A, then that means the distance table is
427 * asymmetric. In this case, the distances for both directions
428 * of all node pairs are required.
429 */
430 if (have_numa_distance) {
431 /* Validate enough NUMA distance information was provided. */
432 validate_numa_distance();
433
434 /* Validation succeeded, now fill in any missing distances. */
435 complete_init_numa_distance();
436 }
437 }
438 }
439
440 void parse_numa_opts(MachineState *ms)
441 {
442 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
443 }
444
445 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
446 {
447 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
448
449 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
450 /* due to bug in libvirt, it doesn't pass node-id from props on
451 * device_add as expected, so we have to fix it up here */
452 if (slot->props.has_node_id) {
453 object_property_set_int(OBJECT(dev), slot->props.node_id,
454 "node-id", errp);
455 }
456 } else if (node_id != slot->props.node_id) {
457 error_setg(errp, "invalid node-id, must be %"PRId64,
458 slot->props.node_id);
459 }
460 }
461
462 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
463 const char *name,
464 uint64_t ram_size)
465 {
466 if (mem_path) {
467 #ifdef __linux__
468 Error *err = NULL;
469 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0,
470 mem_path, &err);
471 if (err) {
472 error_report_err(err);
473 if (mem_prealloc) {
474 exit(1);
475 }
476 error_report("falling back to regular RAM allocation.");
477
478 /* Legacy behavior: if allocation failed, fall back to
479 * regular RAM allocation.
480 */
481 mem_path = NULL;
482 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
483 }
484 #else
485 fprintf(stderr, "-mem-path not supported on this host\n");
486 exit(1);
487 #endif
488 } else {
489 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
490 }
491 vmstate_register_ram_global(mr);
492 }
493
494 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
495 const char *name,
496 uint64_t ram_size)
497 {
498 uint64_t addr = 0;
499 int i;
500
501 if (nb_numa_nodes == 0 || !have_memdevs) {
502 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
503 return;
504 }
505
506 memory_region_init(mr, owner, name, ram_size);
507 for (i = 0; i < nb_numa_nodes; i++) {
508 uint64_t size = numa_info[i].node_mem;
509 HostMemoryBackend *backend = numa_info[i].node_memdev;
510 if (!backend) {
511 continue;
512 }
513 MemoryRegion *seg = host_memory_backend_get_memory(backend);
514
515 if (memory_region_is_mapped(seg)) {
516 char *path = object_get_canonical_path_component(OBJECT(backend));
517 error_report("memory backend %s is used multiple times. Each "
518 "-numa option must use a different memdev value.",
519 path);
520 g_free(path);
521 exit(1);
522 }
523
524 host_memory_backend_set_mapped(backend, true);
525 memory_region_add_subregion(mr, addr, seg);
526 vmstate_register_ram_global(seg);
527 addr += size;
528 }
529 }
530
531 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
532 {
533 MemoryDeviceInfoList *info_list = qmp_memory_device_list();
534 MemoryDeviceInfoList *info;
535 PCDIMMDeviceInfo *pcdimm_info;
536 VirtioPMEMDeviceInfo *vpi;
537
538 for (info = info_list; info; info = info->next) {
539 MemoryDeviceInfo *value = info->value;
540
541 if (value) {
542 switch (value->type) {
543 case MEMORY_DEVICE_INFO_KIND_DIMM:
544 case MEMORY_DEVICE_INFO_KIND_NVDIMM:
545 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
546 value->u.dimm.data : value->u.nvdimm.data;
547 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
548 node_mem[pcdimm_info->node].node_plugged_mem +=
549 pcdimm_info->size;
550 break;
551 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
552 vpi = value->u.virtio_pmem.data;
553 /* TODO: once we support numa, assign to right node */
554 node_mem[0].node_mem += vpi->size;
555 node_mem[0].node_plugged_mem += vpi->size;
556 break;
557 default:
558 g_assert_not_reached();
559 }
560 }
561 }
562 qapi_free_MemoryDeviceInfoList(info_list);
563 }
564
565 void query_numa_node_mem(NumaNodeMem node_mem[])
566 {
567 int i;
568
569 if (nb_numa_nodes <= 0) {
570 return;
571 }
572
573 numa_stat_memory_devices(node_mem);
574 for (i = 0; i < nb_numa_nodes; i++) {
575 node_mem[i].node_mem += numa_info[i].node_mem;
576 }
577 }
578
579 void ram_block_notifier_add(RAMBlockNotifier *n)
580 {
581 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
582 }
583
584 void ram_block_notifier_remove(RAMBlockNotifier *n)
585 {
586 QLIST_REMOVE(n, next);
587 }
588
589 void ram_block_notify_add(void *host, size_t size)
590 {
591 RAMBlockNotifier *notifier;
592
593 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
594 notifier->ram_block_added(notifier, host, size);
595 }
596 }
597
598 void ram_block_notify_remove(void *host, size_t size)
599 {
600 RAMBlockNotifier *notifier;
601
602 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
603 notifier->ram_block_removed(notifier, host, size);
604 }
605 }