tests: Remove deprecated migration tests commands
[qemu/ar7.git] / numa.c
blob83675a03f3cb65cc945d3bed92951464ae7256d0
1 /*
2 * NUMA parameter parsing routines
4 * Copyright (c) 2014 Fujitsu Ltd.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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 "qom/cpu.h"
31 #include "qemu/error-report.h"
32 #include "qapi-visit.h"
33 #include "qapi/opts-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 #include "qemu/cutils.h"
42 QemuOptsList 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 */
49 static int have_memdevs = -1;
50 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
53 int nb_numa_nodes;
54 bool have_numa_distance;
55 NodeInfo numa_info[MAX_NODES];
58 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
59 Error **errp)
61 uint16_t nodenr;
62 uint16List *cpus = NULL;
63 MachineClass *mc = MACHINE_GET_CLASS(ms);
65 if (node->has_nodeid) {
66 nodenr = node->nodeid;
67 } else {
68 nodenr = nb_numa_nodes;
71 if (nodenr >= MAX_NODES) {
72 error_setg(errp, "Max number of NUMA nodes reached: %"
73 PRIu16 "", nodenr);
74 return;
77 if (numa_info[nodenr].present) {
78 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
79 return;
82 if (!mc->cpu_index_to_instance_props) {
83 error_report("NUMA is not supported by this machine-type");
84 exit(1);
86 for (cpus = node->cpus; cpus; cpus = cpus->next) {
87 CpuInstanceProperties props;
88 if (cpus->value >= max_cpus) {
89 error_setg(errp,
90 "CPU index (%" PRIu16 ")"
91 " should be smaller than maxcpus (%d)",
92 cpus->value, max_cpus);
93 return;
95 props = mc->cpu_index_to_instance_props(ms, cpus->value);
96 props.node_id = nodenr;
97 props.has_node_id = true;
98 machine_set_cpu_numa_node(ms, &props, &error_fatal);
101 if (node->has_mem && node->has_memdev) {
102 error_setg(errp, "cannot specify both mem= and memdev=");
103 return;
106 if (have_memdevs == -1) {
107 have_memdevs = node->has_memdev;
109 if (node->has_memdev != have_memdevs) {
110 error_setg(errp, "memdev option must be specified for either "
111 "all or no nodes");
112 return;
115 if (node->has_mem) {
116 numa_info[nodenr].node_mem = node->mem;
118 if (node->has_memdev) {
119 Object *o;
120 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
121 if (!o) {
122 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
123 return;
126 object_ref(o);
127 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
128 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
130 numa_info[nodenr].present = true;
131 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
132 nb_numa_nodes++;
135 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
137 uint16_t src = dist->src;
138 uint16_t dst = dist->dst;
139 uint8_t val = dist->val;
141 if (src >= MAX_NODES || dst >= MAX_NODES) {
142 error_setg(errp,
143 "Invalid node %d, max possible could be %d",
144 MAX(src, dst), MAX_NODES);
145 return;
148 if (!numa_info[src].present || !numa_info[dst].present) {
149 error_setg(errp, "Source/Destination NUMA node is missing. "
150 "Please use '-numa node' option to declare it first.");
151 return;
154 if (val < NUMA_DISTANCE_MIN) {
155 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
156 "it shouldn't be less than %d.",
157 val, NUMA_DISTANCE_MIN);
158 return;
161 if (src == dst && val != NUMA_DISTANCE_MIN) {
162 error_setg(errp, "Local distance of node %d should be %d.",
163 src, NUMA_DISTANCE_MIN);
164 return;
167 numa_info[src].distance[dst] = val;
168 have_numa_distance = true;
171 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
173 NumaOptions *object = NULL;
174 MachineState *ms = opaque;
175 Error *err = NULL;
178 Visitor *v = opts_visitor_new(opts);
179 visit_type_NumaOptions(v, NULL, &object, &err);
180 visit_free(v);
183 if (err) {
184 goto end;
187 /* Fix up legacy suffix-less format */
188 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
189 const char *mem_str = qemu_opt_get(opts, "mem");
190 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
193 switch (object->type) {
194 case NUMA_OPTIONS_TYPE_NODE:
195 parse_numa_node(ms, &object->u.node, &err);
196 if (err) {
197 goto end;
199 break;
200 case NUMA_OPTIONS_TYPE_DIST:
201 parse_numa_distance(&object->u.dist, &err);
202 if (err) {
203 goto end;
205 break;
206 case NUMA_OPTIONS_TYPE_CPU:
207 if (!object->u.cpu.has_node_id) {
208 error_setg(&err, "Missing mandatory node-id property");
209 goto end;
211 if (!numa_info[object->u.cpu.node_id].present) {
212 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
213 "defined with -numa node,nodeid=ID before it's used with "
214 "-numa cpu,node-id=ID", object->u.cpu.node_id);
215 goto end;
218 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
219 &err);
220 break;
221 default:
222 abort();
225 end:
226 qapi_free_NumaOptions(object);
227 if (err) {
228 error_report_err(err);
229 return -1;
232 return 0;
235 /* If all node pair distances are symmetric, then only distances
236 * in one direction are enough. If there is even one asymmetric
237 * pair, though, then all distances must be provided. The
238 * distance from a node to itself is always NUMA_DISTANCE_MIN,
239 * so providing it is never necessary.
241 static void validate_numa_distance(void)
243 int src, dst;
244 bool is_asymmetrical = false;
246 for (src = 0; src < nb_numa_nodes; src++) {
247 for (dst = src; dst < nb_numa_nodes; dst++) {
248 if (numa_info[src].distance[dst] == 0 &&
249 numa_info[dst].distance[src] == 0) {
250 if (src != dst) {
251 error_report("The distance between node %d and %d is "
252 "missing, at least one distance value "
253 "between each nodes should be provided.",
254 src, dst);
255 exit(EXIT_FAILURE);
259 if (numa_info[src].distance[dst] != 0 &&
260 numa_info[dst].distance[src] != 0 &&
261 numa_info[src].distance[dst] !=
262 numa_info[dst].distance[src]) {
263 is_asymmetrical = true;
268 if (is_asymmetrical) {
269 for (src = 0; src < nb_numa_nodes; src++) {
270 for (dst = 0; dst < nb_numa_nodes; dst++) {
271 if (src != dst && numa_info[src].distance[dst] == 0) {
272 error_report("At least one asymmetrical pair of "
273 "distances is given, please provide distances "
274 "for both directions of all node pairs.");
275 exit(EXIT_FAILURE);
282 static void complete_init_numa_distance(void)
284 int src, dst;
286 /* Fixup NUMA distance by symmetric policy because if it is an
287 * asymmetric distance table, it should be a complete table and
288 * there would not be any missing distance except local node, which
289 * is verified by validate_numa_distance above.
291 for (src = 0; src < nb_numa_nodes; src++) {
292 for (dst = 0; dst < nb_numa_nodes; dst++) {
293 if (numa_info[src].distance[dst] == 0) {
294 if (src == dst) {
295 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
296 } else {
297 numa_info[src].distance[dst] = numa_info[dst].distance[src];
304 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
305 int nb_nodes, ram_addr_t size)
307 int i;
308 uint64_t usedmem = 0;
310 /* Align each node according to the alignment
311 * requirements of the machine class
314 for (i = 0; i < nb_nodes - 1; i++) {
315 nodes[i].node_mem = (size / nb_nodes) &
316 ~((1 << mc->numa_mem_align_shift) - 1);
317 usedmem += nodes[i].node_mem;
319 nodes[i].node_mem = size - usedmem;
322 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
323 int nb_nodes, ram_addr_t size)
325 int i;
326 uint64_t usedmem = 0, node_mem;
327 uint64_t granularity = size / nb_nodes;
328 uint64_t propagate = 0;
330 for (i = 0; i < nb_nodes - 1; i++) {
331 node_mem = (granularity + propagate) &
332 ~((1 << mc->numa_mem_align_shift) - 1);
333 propagate = granularity + propagate - node_mem;
334 nodes[i].node_mem = node_mem;
335 usedmem += node_mem;
337 nodes[i].node_mem = size - usedmem;
340 void parse_numa_opts(MachineState *ms)
342 int i;
343 MachineClass *mc = MACHINE_GET_CLASS(ms);
345 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
346 exit(1);
350 * If memory hotplug is enabled (slots > 0) but without '-numa'
351 * options explicitly on CLI, guestes will break.
353 * Windows: won't enable memory hotplug without SRAT table at all
355 * Linux: if QEMU is started with initial memory all below 4Gb
356 * and no SRAT table present, guest kernel will use nommu DMA ops,
357 * which breaks 32bit hw drivers when memory is hotplugged and
358 * guest tries to use it with that drivers.
360 * Enable NUMA implicitly by adding a new NUMA node automatically.
362 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
363 mc->auto_enable_numa_with_memhp) {
364 NumaNodeOptions node = { };
365 parse_numa_node(ms, &node, NULL);
368 assert(max_numa_nodeid <= MAX_NODES);
370 /* No support for sparse NUMA node IDs yet: */
371 for (i = max_numa_nodeid - 1; i >= 0; i--) {
372 /* Report large node IDs first, to make mistakes easier to spot */
373 if (!numa_info[i].present) {
374 error_report("numa: Node ID missing: %d", i);
375 exit(1);
379 /* This must be always true if all nodes are present: */
380 assert(nb_numa_nodes == max_numa_nodeid);
382 if (nb_numa_nodes > 0) {
383 uint64_t numa_total;
385 if (nb_numa_nodes > MAX_NODES) {
386 nb_numa_nodes = MAX_NODES;
389 /* If no memory size is given for any node, assume the default case
390 * and distribute the available memory equally across all nodes
392 for (i = 0; i < nb_numa_nodes; i++) {
393 if (numa_info[i].node_mem != 0) {
394 break;
397 if (i == nb_numa_nodes) {
398 assert(mc->numa_auto_assign_ram);
399 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
402 numa_total = 0;
403 for (i = 0; i < nb_numa_nodes; i++) {
404 numa_total += numa_info[i].node_mem;
406 if (numa_total != ram_size) {
407 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
408 " should equal RAM size (0x" RAM_ADDR_FMT ")",
409 numa_total, ram_size);
410 exit(1);
413 /* QEMU needs at least all unique node pair distances to build
414 * the whole NUMA distance table. QEMU treats the distance table
415 * as symmetric by default, i.e. distance A->B == distance B->A.
416 * Thus, QEMU is able to complete the distance table
417 * initialization even though only distance A->B is provided and
418 * distance B->A is not. QEMU knows the distance of a node to
419 * itself is always 10, so A->A distances may be omitted. When
420 * the distances of two nodes of a pair differ, i.e. distance
421 * A->B != distance B->A, then that means the distance table is
422 * asymmetric. In this case, the distances for both directions
423 * of all node pairs are required.
425 if (have_numa_distance) {
426 /* Validate enough NUMA distance information was provided. */
427 validate_numa_distance();
429 /* Validation succeeded, now fill in any missing distances. */
430 complete_init_numa_distance();
435 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
437 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
439 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
440 /* due to bug in libvirt, it doesn't pass node-id from props on
441 * device_add as expected, so we have to fix it up here */
442 if (slot->props.has_node_id) {
443 object_property_set_int(OBJECT(dev), slot->props.node_id,
444 "node-id", errp);
446 } else if (node_id != slot->props.node_id) {
447 error_setg(errp, "node-id=%d must match numa node specified "
448 "with -numa option", node_id);
452 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
453 const char *name,
454 uint64_t ram_size)
456 if (mem_path) {
457 #ifdef __linux__
458 Error *err = NULL;
459 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, false,
460 mem_path, &err);
461 if (err) {
462 error_report_err(err);
463 if (mem_prealloc) {
464 exit(1);
467 /* Legacy behavior: if allocation failed, fall back to
468 * regular RAM allocation.
470 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
472 #else
473 fprintf(stderr, "-mem-path not supported on this host\n");
474 exit(1);
475 #endif
476 } else {
477 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
479 vmstate_register_ram_global(mr);
482 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
483 const char *name,
484 uint64_t ram_size)
486 uint64_t addr = 0;
487 int i;
489 if (nb_numa_nodes == 0 || !have_memdevs) {
490 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
491 return;
494 memory_region_init(mr, owner, name, ram_size);
495 for (i = 0; i < nb_numa_nodes; i++) {
496 uint64_t size = numa_info[i].node_mem;
497 HostMemoryBackend *backend = numa_info[i].node_memdev;
498 if (!backend) {
499 continue;
501 MemoryRegion *seg = host_memory_backend_get_memory(backend,
502 &error_fatal);
504 if (memory_region_is_mapped(seg)) {
505 char *path = object_get_canonical_path_component(OBJECT(backend));
506 error_report("memory backend %s is used multiple times. Each "
507 "-numa option must use a different memdev value.",
508 path);
509 exit(1);
512 host_memory_backend_set_mapped(backend, true);
513 memory_region_add_subregion(mr, addr, seg);
514 vmstate_register_ram_global(seg);
515 addr += size;
519 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
521 MemoryDeviceInfoList *info_list = NULL;
522 MemoryDeviceInfoList **prev = &info_list;
523 MemoryDeviceInfoList *info;
524 PCDIMMDeviceInfo *pcdimm_info;
526 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
527 for (info = info_list; info; info = info->next) {
528 MemoryDeviceInfo *value = info->value;
530 if (value) {
531 switch (value->type) {
532 case MEMORY_DEVICE_INFO_KIND_DIMM: {
533 pcdimm_info = value->u.dimm.data;
534 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
535 if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
536 node_mem[pcdimm_info->node].node_plugged_mem +=
537 pcdimm_info->size;
539 break;
542 default:
543 break;
547 qapi_free_MemoryDeviceInfoList(info_list);
550 void query_numa_node_mem(NumaNodeMem node_mem[])
552 int i;
554 if (nb_numa_nodes <= 0) {
555 return;
558 numa_stat_memory_devices(node_mem);
559 for (i = 0; i < nb_numa_nodes; i++) {
560 node_mem[i].node_mem += numa_info[i].node_mem;
564 static int query_memdev(Object *obj, void *opaque)
566 MemdevList **list = opaque;
567 MemdevList *m = NULL;
569 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
570 m = g_malloc0(sizeof(*m));
572 m->value = g_malloc0(sizeof(*m->value));
574 m->value->id = object_property_get_str(obj, "id", NULL);
575 m->value->has_id = !!m->value->id;
577 m->value->size = object_property_get_uint(obj, "size",
578 &error_abort);
579 m->value->merge = object_property_get_bool(obj, "merge",
580 &error_abort);
581 m->value->dump = object_property_get_bool(obj, "dump",
582 &error_abort);
583 m->value->prealloc = object_property_get_bool(obj,
584 "prealloc",
585 &error_abort);
586 m->value->policy = object_property_get_enum(obj,
587 "policy",
588 "HostMemPolicy",
589 &error_abort);
590 object_property_get_uint16List(obj, "host-nodes",
591 &m->value->host_nodes,
592 &error_abort);
594 m->next = *list;
595 *list = m;
598 return 0;
601 MemdevList *qmp_query_memdev(Error **errp)
603 Object *obj = object_get_objects_root();
604 MemdevList *list = NULL;
606 object_child_foreach(obj, query_memdev, &list);
607 return list;
610 void ram_block_notifier_add(RAMBlockNotifier *n)
612 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
615 void ram_block_notifier_remove(RAMBlockNotifier *n)
617 QLIST_REMOVE(n, next);
620 void ram_block_notify_add(void *host, size_t size)
622 RAMBlockNotifier *notifier;
624 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
625 notifier->ram_block_added(notifier, host, size);
629 void ram_block_notify_remove(void *host, size_t size)
631 RAMBlockNotifier *notifier;
633 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
634 notifier->ram_block_removed(notifier, host, size);