Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2017-11-14' into staging
[qemu/ar7.git] / numa.c
blob8d78d959f68c3e93a2aa95bdb744fad976441dc9
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 "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
33 #include "qapi-visit.h"
34 #include "qapi/opts-visitor.h"
35 #include "hw/boards.h"
36 #include "sysemu/hostmem.h"
37 #include "qmp-commands.h"
38 #include "hw/mem/pc-dimm.h"
39 #include "qemu/option.h"
40 #include "qemu/config-file.h"
41 #include "qemu/cutils.h"
43 QemuOptsList qemu_numa_opts = {
44 .name = "numa",
45 .implied_opt_name = "type",
46 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
47 .desc = { { 0 } } /* validated with OptsVisitor */
50 static int have_memdevs = -1;
51 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
52 * For all nodes, nodeid < max_numa_nodeid
54 int nb_numa_nodes;
55 bool have_numa_distance;
56 NodeInfo numa_info[MAX_NODES];
58 void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
60 struct numa_addr_range *range;
63 * Memory-less nodes can come here with 0 size in which case,
64 * there is nothing to do.
66 if (!size) {
67 return;
70 range = g_malloc0(sizeof(*range));
71 range->mem_start = addr;
72 range->mem_end = addr + size - 1;
73 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
76 void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
78 struct numa_addr_range *range, *next;
80 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
81 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
82 QLIST_REMOVE(range, entry);
83 g_free(range);
84 return;
89 static void numa_set_mem_ranges(void)
91 int i;
92 ram_addr_t mem_start = 0;
95 * Deduce start address of each node and use it to store
96 * the address range info in numa_info address range list
98 for (i = 0; i < nb_numa_nodes; i++) {
99 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
100 mem_start += numa_info[i].node_mem;
105 * Check if @addr falls under NUMA @node.
107 static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
109 struct numa_addr_range *range;
111 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
112 if (addr >= range->mem_start && addr <= range->mem_end) {
113 return true;
116 return false;
120 * Given an address, return the index of the NUMA node to which the
121 * address belongs to.
123 uint32_t numa_get_node(ram_addr_t addr, Error **errp)
125 uint32_t i;
127 /* For non NUMA configurations, check if the addr falls under node 0 */
128 if (!nb_numa_nodes) {
129 if (numa_addr_belongs_to_node(addr, 0)) {
130 return 0;
134 for (i = 0; i < nb_numa_nodes; i++) {
135 if (numa_addr_belongs_to_node(addr, i)) {
136 return i;
140 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
141 "NUMA node", addr);
142 return -1;
145 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
146 Error **errp)
148 uint16_t nodenr;
149 uint16List *cpus = NULL;
150 MachineClass *mc = MACHINE_GET_CLASS(ms);
152 if (node->has_nodeid) {
153 nodenr = node->nodeid;
154 } else {
155 nodenr = nb_numa_nodes;
158 if (nodenr >= MAX_NODES) {
159 error_setg(errp, "Max number of NUMA nodes reached: %"
160 PRIu16 "", nodenr);
161 return;
164 if (numa_info[nodenr].present) {
165 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
166 return;
169 if (!mc->cpu_index_to_instance_props) {
170 error_report("NUMA is not supported by this machine-type");
171 exit(1);
173 for (cpus = node->cpus; cpus; cpus = cpus->next) {
174 CpuInstanceProperties props;
175 if (cpus->value >= max_cpus) {
176 error_setg(errp,
177 "CPU index (%" PRIu16 ")"
178 " should be smaller than maxcpus (%d)",
179 cpus->value, max_cpus);
180 return;
182 props = mc->cpu_index_to_instance_props(ms, cpus->value);
183 props.node_id = nodenr;
184 props.has_node_id = true;
185 machine_set_cpu_numa_node(ms, &props, &error_fatal);
188 if (node->has_mem && node->has_memdev) {
189 error_setg(errp, "cannot specify both mem= and memdev=");
190 return;
193 if (have_memdevs == -1) {
194 have_memdevs = node->has_memdev;
196 if (node->has_memdev != have_memdevs) {
197 error_setg(errp, "memdev option must be specified for either "
198 "all or no nodes");
199 return;
202 if (node->has_mem) {
203 numa_info[nodenr].node_mem = node->mem;
205 if (node->has_memdev) {
206 Object *o;
207 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
208 if (!o) {
209 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
210 return;
213 object_ref(o);
214 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
215 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
217 numa_info[nodenr].present = true;
218 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
221 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
223 uint16_t src = dist->src;
224 uint16_t dst = dist->dst;
225 uint8_t val = dist->val;
227 if (src >= MAX_NODES || dst >= MAX_NODES) {
228 error_setg(errp,
229 "Invalid node %d, max possible could be %d",
230 MAX(src, dst), MAX_NODES);
231 return;
234 if (!numa_info[src].present || !numa_info[dst].present) {
235 error_setg(errp, "Source/Destination NUMA node is missing. "
236 "Please use '-numa node' option to declare it first.");
237 return;
240 if (val < NUMA_DISTANCE_MIN) {
241 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
242 "it shouldn't be less than %d.",
243 val, NUMA_DISTANCE_MIN);
244 return;
247 if (src == dst && val != NUMA_DISTANCE_MIN) {
248 error_setg(errp, "Local distance of node %d should be %d.",
249 src, NUMA_DISTANCE_MIN);
250 return;
253 numa_info[src].distance[dst] = val;
254 have_numa_distance = true;
257 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
259 NumaOptions *object = NULL;
260 MachineState *ms = opaque;
261 Error *err = NULL;
264 Visitor *v = opts_visitor_new(opts);
265 visit_type_NumaOptions(v, NULL, &object, &err);
266 visit_free(v);
269 if (err) {
270 goto end;
273 /* Fix up legacy suffix-less format */
274 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
275 const char *mem_str = qemu_opt_get(opts, "mem");
276 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
279 switch (object->type) {
280 case NUMA_OPTIONS_TYPE_NODE:
281 parse_numa_node(ms, &object->u.node, &err);
282 if (err) {
283 goto end;
285 nb_numa_nodes++;
286 break;
287 case NUMA_OPTIONS_TYPE_DIST:
288 parse_numa_distance(&object->u.dist, &err);
289 if (err) {
290 goto end;
292 break;
293 case NUMA_OPTIONS_TYPE_CPU:
294 if (!object->u.cpu.has_node_id) {
295 error_setg(&err, "Missing mandatory node-id property");
296 goto end;
298 if (!numa_info[object->u.cpu.node_id].present) {
299 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
300 "defined with -numa node,nodeid=ID before it's used with "
301 "-numa cpu,node-id=ID", object->u.cpu.node_id);
302 goto end;
305 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
306 &err);
307 break;
308 default:
309 abort();
312 end:
313 qapi_free_NumaOptions(object);
314 if (err) {
315 error_report_err(err);
316 return -1;
319 return 0;
322 /* If all node pair distances are symmetric, then only distances
323 * in one direction are enough. If there is even one asymmetric
324 * pair, though, then all distances must be provided. The
325 * distance from a node to itself is always NUMA_DISTANCE_MIN,
326 * so providing it is never necessary.
328 static void validate_numa_distance(void)
330 int src, dst;
331 bool is_asymmetrical = false;
333 for (src = 0; src < nb_numa_nodes; src++) {
334 for (dst = src; dst < nb_numa_nodes; dst++) {
335 if (numa_info[src].distance[dst] == 0 &&
336 numa_info[dst].distance[src] == 0) {
337 if (src != dst) {
338 error_report("The distance between node %d and %d is "
339 "missing, at least one distance value "
340 "between each nodes should be provided.",
341 src, dst);
342 exit(EXIT_FAILURE);
346 if (numa_info[src].distance[dst] != 0 &&
347 numa_info[dst].distance[src] != 0 &&
348 numa_info[src].distance[dst] !=
349 numa_info[dst].distance[src]) {
350 is_asymmetrical = true;
355 if (is_asymmetrical) {
356 for (src = 0; src < nb_numa_nodes; src++) {
357 for (dst = 0; dst < nb_numa_nodes; dst++) {
358 if (src != dst && numa_info[src].distance[dst] == 0) {
359 error_report("At least one asymmetrical pair of "
360 "distances is given, please provide distances "
361 "for both directions of all node pairs.");
362 exit(EXIT_FAILURE);
369 static void complete_init_numa_distance(void)
371 int src, dst;
373 /* Fixup NUMA distance by symmetric policy because if it is an
374 * asymmetric distance table, it should be a complete table and
375 * there would not be any missing distance except local node, which
376 * is verified by validate_numa_distance above.
378 for (src = 0; src < nb_numa_nodes; src++) {
379 for (dst = 0; dst < nb_numa_nodes; dst++) {
380 if (numa_info[src].distance[dst] == 0) {
381 if (src == dst) {
382 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
383 } else {
384 numa_info[src].distance[dst] = numa_info[dst].distance[src];
391 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
392 int nb_nodes, ram_addr_t size)
394 int i;
395 uint64_t usedmem = 0;
397 /* Align each node according to the alignment
398 * requirements of the machine class
401 for (i = 0; i < nb_nodes - 1; i++) {
402 nodes[i].node_mem = (size / nb_nodes) &
403 ~((1 << mc->numa_mem_align_shift) - 1);
404 usedmem += nodes[i].node_mem;
406 nodes[i].node_mem = size - usedmem;
409 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
410 int nb_nodes, ram_addr_t size)
412 int i;
413 uint64_t usedmem = 0, node_mem;
414 uint64_t granularity = size / nb_nodes;
415 uint64_t propagate = 0;
417 for (i = 0; i < nb_nodes - 1; i++) {
418 node_mem = (granularity + propagate) &
419 ~((1 << mc->numa_mem_align_shift) - 1);
420 propagate = granularity + propagate - node_mem;
421 nodes[i].node_mem = node_mem;
422 usedmem += node_mem;
424 nodes[i].node_mem = size - usedmem;
427 void parse_numa_opts(MachineState *ms)
429 int i;
430 MachineClass *mc = MACHINE_GET_CLASS(ms);
432 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
433 exit(1);
436 assert(max_numa_nodeid <= MAX_NODES);
438 /* No support for sparse NUMA node IDs yet: */
439 for (i = max_numa_nodeid - 1; i >= 0; i--) {
440 /* Report large node IDs first, to make mistakes easier to spot */
441 if (!numa_info[i].present) {
442 error_report("numa: Node ID missing: %d", i);
443 exit(1);
447 /* This must be always true if all nodes are present: */
448 assert(nb_numa_nodes == max_numa_nodeid);
450 if (nb_numa_nodes > 0) {
451 uint64_t numa_total;
453 if (nb_numa_nodes > MAX_NODES) {
454 nb_numa_nodes = MAX_NODES;
457 /* If no memory size is given for any node, assume the default case
458 * and distribute the available memory equally across all nodes
460 for (i = 0; i < nb_numa_nodes; i++) {
461 if (numa_info[i].node_mem != 0) {
462 break;
465 if (i == nb_numa_nodes) {
466 assert(mc->numa_auto_assign_ram);
467 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
470 numa_total = 0;
471 for (i = 0; i < nb_numa_nodes; i++) {
472 numa_total += numa_info[i].node_mem;
474 if (numa_total != ram_size) {
475 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
476 " should equal RAM size (0x" RAM_ADDR_FMT ")",
477 numa_total, ram_size);
478 exit(1);
481 for (i = 0; i < nb_numa_nodes; i++) {
482 QLIST_INIT(&numa_info[i].addr);
485 numa_set_mem_ranges();
487 /* QEMU needs at least all unique node pair distances to build
488 * the whole NUMA distance table. QEMU treats the distance table
489 * as symmetric by default, i.e. distance A->B == distance B->A.
490 * Thus, QEMU is able to complete the distance table
491 * initialization even though only distance A->B is provided and
492 * distance B->A is not. QEMU knows the distance of a node to
493 * itself is always 10, so A->A distances may be omitted. When
494 * the distances of two nodes of a pair differ, i.e. distance
495 * A->B != distance B->A, then that means the distance table is
496 * asymmetric. In this case, the distances for both directions
497 * of all node pairs are required.
499 if (have_numa_distance) {
500 /* Validate enough NUMA distance information was provided. */
501 validate_numa_distance();
503 /* Validation succeeded, now fill in any missing distances. */
504 complete_init_numa_distance();
506 } else {
507 numa_set_mem_node_id(0, ram_size, 0);
511 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
513 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
515 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
516 /* due to bug in libvirt, it doesn't pass node-id from props on
517 * device_add as expected, so we have to fix it up here */
518 if (slot->props.has_node_id) {
519 object_property_set_int(OBJECT(dev), slot->props.node_id,
520 "node-id", errp);
522 } else if (node_id != slot->props.node_id) {
523 error_setg(errp, "node-id=%d must match numa node specified "
524 "with -numa option", node_id);
528 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
529 const char *name,
530 uint64_t ram_size)
532 if (mem_path) {
533 #ifdef __linux__
534 Error *err = NULL;
535 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
536 mem_path, &err);
537 if (err) {
538 error_report_err(err);
539 if (mem_prealloc) {
540 exit(1);
543 /* Legacy behavior: if allocation failed, fall back to
544 * regular RAM allocation.
546 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
548 #else
549 fprintf(stderr, "-mem-path not supported on this host\n");
550 exit(1);
551 #endif
552 } else {
553 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
555 vmstate_register_ram_global(mr);
558 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
559 const char *name,
560 uint64_t ram_size)
562 uint64_t addr = 0;
563 int i;
565 if (nb_numa_nodes == 0 || !have_memdevs) {
566 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
567 return;
570 memory_region_init(mr, owner, name, ram_size);
571 for (i = 0; i < nb_numa_nodes; i++) {
572 uint64_t size = numa_info[i].node_mem;
573 HostMemoryBackend *backend = numa_info[i].node_memdev;
574 if (!backend) {
575 continue;
577 MemoryRegion *seg = host_memory_backend_get_memory(backend,
578 &error_fatal);
580 if (memory_region_is_mapped(seg)) {
581 char *path = object_get_canonical_path_component(OBJECT(backend));
582 error_report("memory backend %s is used multiple times. Each "
583 "-numa option must use a different memdev value.",
584 path);
585 exit(1);
588 host_memory_backend_set_mapped(backend, true);
589 memory_region_add_subregion(mr, addr, seg);
590 vmstate_register_ram_global(seg);
591 addr += size;
595 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
597 MemoryDeviceInfoList *info_list = NULL;
598 MemoryDeviceInfoList **prev = &info_list;
599 MemoryDeviceInfoList *info;
600 PCDIMMDeviceInfo *pcdimm_info;
602 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
603 for (info = info_list; info; info = info->next) {
604 MemoryDeviceInfo *value = info->value;
606 if (value) {
607 switch (value->type) {
608 case MEMORY_DEVICE_INFO_KIND_DIMM: {
609 pcdimm_info = value->u.dimm.data;
610 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
611 if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
612 node_mem[pcdimm_info->node].node_plugged_mem +=
613 pcdimm_info->size;
615 break;
618 default:
619 break;
623 qapi_free_MemoryDeviceInfoList(info_list);
626 void query_numa_node_mem(NumaNodeMem node_mem[])
628 int i;
630 if (nb_numa_nodes <= 0) {
631 return;
634 numa_stat_memory_devices(node_mem);
635 for (i = 0; i < nb_numa_nodes; i++) {
636 node_mem[i].node_mem += numa_info[i].node_mem;
640 static int query_memdev(Object *obj, void *opaque)
642 MemdevList **list = opaque;
643 MemdevList *m = NULL;
645 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
646 m = g_malloc0(sizeof(*m));
648 m->value = g_malloc0(sizeof(*m->value));
650 m->value->id = object_property_get_str(obj, "id", NULL);
651 m->value->has_id = !!m->value->id;
653 m->value->size = object_property_get_uint(obj, "size",
654 &error_abort);
655 m->value->merge = object_property_get_bool(obj, "merge",
656 &error_abort);
657 m->value->dump = object_property_get_bool(obj, "dump",
658 &error_abort);
659 m->value->prealloc = object_property_get_bool(obj,
660 "prealloc",
661 &error_abort);
662 m->value->policy = object_property_get_enum(obj,
663 "policy",
664 "HostMemPolicy",
665 &error_abort);
666 object_property_get_uint16List(obj, "host-nodes",
667 &m->value->host_nodes,
668 &error_abort);
670 m->next = *list;
671 *list = m;
674 return 0;
677 MemdevList *qmp_query_memdev(Error **errp)
679 Object *obj = object_get_objects_root();
680 MemdevList *list = NULL;
682 object_child_foreach(obj, query_memdev, &list);
683 return list;
686 void ram_block_notifier_add(RAMBlockNotifier *n)
688 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
691 void ram_block_notifier_remove(RAMBlockNotifier *n)
693 QLIST_REMOVE(n, next);
696 void ram_block_notify_add(void *host, size_t size)
698 RAMBlockNotifier *notifier;
700 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
701 notifier->ram_block_added(notifier, host, size);
705 void ram_block_notify_remove(void *host, size_t size)
707 RAMBlockNotifier *notifier;
709 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
710 notifier->ram_block_removed(notifier, host, size);