numa: add check that board supports cpu_index to node mapping
[qemu/ar7.git] / numa.c
blob718248161c586d88195921c9675fc175cd034423
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"
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];
57 void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
59 struct numa_addr_range *range;
62 * Memory-less nodes can come here with 0 size in which case,
63 * there is nothing to do.
65 if (!size) {
66 return;
69 range = g_malloc0(sizeof(*range));
70 range->mem_start = addr;
71 range->mem_end = addr + size - 1;
72 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
75 void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
77 struct numa_addr_range *range, *next;
79 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
80 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
81 QLIST_REMOVE(range, entry);
82 g_free(range);
83 return;
88 static void numa_set_mem_ranges(void)
90 int i;
91 ram_addr_t mem_start = 0;
94 * Deduce start address of each node and use it to store
95 * the address range info in numa_info address range list
97 for (i = 0; i < nb_numa_nodes; i++) {
98 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
99 mem_start += numa_info[i].node_mem;
104 * Check if @addr falls under NUMA @node.
106 static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
108 struct numa_addr_range *range;
110 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
111 if (addr >= range->mem_start && addr <= range->mem_end) {
112 return true;
115 return false;
119 * Given an address, return the index of the NUMA node to which the
120 * address belongs to.
122 uint32_t numa_get_node(ram_addr_t addr, Error **errp)
124 uint32_t i;
126 /* For non NUMA configurations, check if the addr falls under node 0 */
127 if (!nb_numa_nodes) {
128 if (numa_addr_belongs_to_node(addr, 0)) {
129 return 0;
133 for (i = 0; i < nb_numa_nodes; i++) {
134 if (numa_addr_belongs_to_node(addr, i)) {
135 return i;
139 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
140 "NUMA node", addr);
141 return -1;
144 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
145 QemuOpts *opts, Error **errp)
147 uint16_t nodenr;
148 uint16List *cpus = NULL;
149 MachineClass *mc = MACHINE_GET_CLASS(ms);
151 if (node->has_nodeid) {
152 nodenr = node->nodeid;
153 } else {
154 nodenr = nb_numa_nodes;
157 if (nodenr >= MAX_NODES) {
158 error_setg(errp, "Max number of NUMA nodes reached: %"
159 PRIu16 "", nodenr);
160 return;
163 if (numa_info[nodenr].present) {
164 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
165 return;
168 if (!mc->cpu_index_to_instance_props) {
169 error_report("NUMA is not supported by this machine-type");
170 exit(1);
172 for (cpus = node->cpus; cpus; cpus = cpus->next) {
173 if (cpus->value >= max_cpus) {
174 error_setg(errp,
175 "CPU index (%" PRIu16 ")"
176 " should be smaller than maxcpus (%d)",
177 cpus->value, max_cpus);
178 return;
180 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
183 if (node->has_mem && node->has_memdev) {
184 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
185 return;
188 if (have_memdevs == -1) {
189 have_memdevs = node->has_memdev;
191 if (node->has_memdev != have_memdevs) {
192 error_setg(errp, "qemu: memdev option must be specified for either "
193 "all or no nodes");
194 return;
197 if (node->has_mem) {
198 uint64_t mem_size = node->mem;
199 const char *mem_str = qemu_opt_get(opts, "mem");
200 /* Fix up legacy suffix-less format */
201 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
202 mem_size <<= 20;
204 numa_info[nodenr].node_mem = mem_size;
206 if (node->has_memdev) {
207 Object *o;
208 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
209 if (!o) {
210 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
211 return;
214 object_ref(o);
215 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
216 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
218 numa_info[nodenr].present = true;
219 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
222 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
224 uint16_t src = dist->src;
225 uint16_t dst = dist->dst;
226 uint8_t val = dist->val;
228 if (src >= MAX_NODES || dst >= MAX_NODES) {
229 error_setg(errp,
230 "Invalid node %" PRIu16
231 ", max possible could be %" PRIu16,
232 MAX(src, dst), MAX_NODES);
233 return;
236 if (!numa_info[src].present || !numa_info[dst].present) {
237 error_setg(errp, "Source/Destination NUMA node is missing. "
238 "Please use '-numa node' option to declare it first.");
239 return;
242 if (val < NUMA_DISTANCE_MIN) {
243 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
244 "it shouldn't be less than %d.",
245 val, NUMA_DISTANCE_MIN);
246 return;
249 if (src == dst && val != NUMA_DISTANCE_MIN) {
250 error_setg(errp, "Local distance of node %d should be %d.",
251 src, NUMA_DISTANCE_MIN);
252 return;
255 numa_info[src].distance[dst] = val;
256 have_numa_distance = true;
259 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
261 NumaOptions *object = NULL;
262 MachineState *ms = opaque;
263 Error *err = NULL;
266 Visitor *v = opts_visitor_new(opts);
267 visit_type_NumaOptions(v, NULL, &object, &err);
268 visit_free(v);
271 if (err) {
272 goto end;
275 switch (object->type) {
276 case NUMA_OPTIONS_TYPE_NODE:
277 parse_numa_node(ms, &object->u.node, opts, &err);
278 if (err) {
279 goto end;
281 nb_numa_nodes++;
282 break;
283 case NUMA_OPTIONS_TYPE_DIST:
284 parse_numa_distance(&object->u.dist, &err);
285 if (err) {
286 goto end;
288 break;
289 default:
290 abort();
293 end:
294 qapi_free_NumaOptions(object);
295 if (err) {
296 error_report_err(err);
297 return -1;
300 return 0;
303 static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
305 int cpu;
306 bool first = true;
307 GString *s = g_string_new(NULL);
309 for (cpu = find_first_bit(cpus, max_cpus);
310 cpu < max_cpus;
311 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
312 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
313 first = false;
315 return g_string_free(s, FALSE);
318 static void validate_numa_cpus(void)
320 int i;
321 unsigned long *seen_cpus = bitmap_new(max_cpus);
323 for (i = 0; i < nb_numa_nodes; i++) {
324 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
325 bitmap_and(seen_cpus, seen_cpus,
326 numa_info[i].node_cpu, max_cpus);
327 error_report("CPU(s) present in multiple NUMA nodes: %s",
328 enumerate_cpus(seen_cpus, max_cpus));
329 g_free(seen_cpus);
330 exit(EXIT_FAILURE);
332 bitmap_or(seen_cpus, seen_cpus,
333 numa_info[i].node_cpu, max_cpus);
336 if (!bitmap_full(seen_cpus, max_cpus)) {
337 char *msg;
338 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
339 msg = enumerate_cpus(seen_cpus, max_cpus);
340 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
341 error_report("warning: All CPU(s) up to maxcpus should be described "
342 "in NUMA config");
343 g_free(msg);
345 g_free(seen_cpus);
348 /* If all node pair distances are symmetric, then only distances
349 * in one direction are enough. If there is even one asymmetric
350 * pair, though, then all distances must be provided. The
351 * distance from a node to itself is always NUMA_DISTANCE_MIN,
352 * so providing it is never necessary.
354 static void validate_numa_distance(void)
356 int src, dst;
357 bool is_asymmetrical = false;
359 for (src = 0; src < nb_numa_nodes; src++) {
360 for (dst = src; dst < nb_numa_nodes; dst++) {
361 if (numa_info[src].distance[dst] == 0 &&
362 numa_info[dst].distance[src] == 0) {
363 if (src != dst) {
364 error_report("The distance between node %d and %d is "
365 "missing, at least one distance value "
366 "between each nodes should be provided.",
367 src, dst);
368 exit(EXIT_FAILURE);
372 if (numa_info[src].distance[dst] != 0 &&
373 numa_info[dst].distance[src] != 0 &&
374 numa_info[src].distance[dst] !=
375 numa_info[dst].distance[src]) {
376 is_asymmetrical = true;
381 if (is_asymmetrical) {
382 for (src = 0; src < nb_numa_nodes; src++) {
383 for (dst = 0; dst < nb_numa_nodes; dst++) {
384 if (src != dst && numa_info[src].distance[dst] == 0) {
385 error_report("At least one asymmetrical pair of "
386 "distances is given, please provide distances "
387 "for both directions of all node pairs.");
388 exit(EXIT_FAILURE);
395 static void complete_init_numa_distance(void)
397 int src, dst;
399 /* Fixup NUMA distance by symmetric policy because if it is an
400 * asymmetric distance table, it should be a complete table and
401 * there would not be any missing distance except local node, which
402 * is verified by validate_numa_distance above.
404 for (src = 0; src < nb_numa_nodes; src++) {
405 for (dst = 0; dst < nb_numa_nodes; dst++) {
406 if (numa_info[src].distance[dst] == 0) {
407 if (src == dst) {
408 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
409 } else {
410 numa_info[src].distance[dst] = numa_info[dst].distance[src];
417 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
418 int nb_nodes, ram_addr_t size)
420 int i;
421 uint64_t usedmem = 0;
423 /* Align each node according to the alignment
424 * requirements of the machine class
427 for (i = 0; i < nb_nodes - 1; i++) {
428 nodes[i].node_mem = (size / nb_nodes) &
429 ~((1 << mc->numa_mem_align_shift) - 1);
430 usedmem += nodes[i].node_mem;
432 nodes[i].node_mem = size - usedmem;
435 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
436 int nb_nodes, ram_addr_t size)
438 int i;
439 uint64_t usedmem = 0, node_mem;
440 uint64_t granularity = size / nb_nodes;
441 uint64_t propagate = 0;
443 for (i = 0; i < nb_nodes - 1; i++) {
444 node_mem = (granularity + propagate) &
445 ~((1 << mc->numa_mem_align_shift) - 1);
446 propagate = granularity + propagate - node_mem;
447 nodes[i].node_mem = node_mem;
448 usedmem += node_mem;
450 nodes[i].node_mem = size - usedmem;
453 void parse_numa_opts(MachineState *ms)
455 int i;
456 MachineClass *mc = MACHINE_GET_CLASS(ms);
458 for (i = 0; i < MAX_NODES; i++) {
459 numa_info[i].node_cpu = bitmap_new(max_cpus);
462 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
463 exit(1);
466 assert(max_numa_nodeid <= MAX_NODES);
468 /* No support for sparse NUMA node IDs yet: */
469 for (i = max_numa_nodeid - 1; i >= 0; i--) {
470 /* Report large node IDs first, to make mistakes easier to spot */
471 if (!numa_info[i].present) {
472 error_report("numa: Node ID missing: %d", i);
473 exit(1);
477 /* This must be always true if all nodes are present: */
478 assert(nb_numa_nodes == max_numa_nodeid);
480 if (nb_numa_nodes > 0) {
481 uint64_t numa_total;
483 if (nb_numa_nodes > MAX_NODES) {
484 nb_numa_nodes = MAX_NODES;
487 /* If no memory size is given for any node, assume the default case
488 * and distribute the available memory equally across all nodes
490 for (i = 0; i < nb_numa_nodes; i++) {
491 if (numa_info[i].node_mem != 0) {
492 break;
495 if (i == nb_numa_nodes) {
496 assert(mc->numa_auto_assign_ram);
497 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
500 numa_total = 0;
501 for (i = 0; i < nb_numa_nodes; i++) {
502 numa_total += numa_info[i].node_mem;
504 if (numa_total != ram_size) {
505 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
506 " should equal RAM size (0x" RAM_ADDR_FMT ")",
507 numa_total, ram_size);
508 exit(1);
511 for (i = 0; i < nb_numa_nodes; i++) {
512 QLIST_INIT(&numa_info[i].addr);
515 numa_set_mem_ranges();
517 for (i = 0; i < nb_numa_nodes; i++) {
518 if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
519 break;
523 /* assign CPUs to nodes using board provided default mapping */
524 if (!mc->cpu_index_to_instance_props) {
525 error_report("default CPUs to NUMA node mapping isn't supported");
526 exit(1);
528 if (i == nb_numa_nodes) {
529 for (i = 0; i < max_cpus; i++) {
530 CpuInstanceProperties props;
531 props = mc->cpu_index_to_instance_props(ms, i);
533 set_bit(i, numa_info[props.node_id].node_cpu);
537 validate_numa_cpus();
539 /* QEMU needs at least all unique node pair distances to build
540 * the whole NUMA distance table. QEMU treats the distance table
541 * as symmetric by default, i.e. distance A->B == distance B->A.
542 * Thus, QEMU is able to complete the distance table
543 * initialization even though only distance A->B is provided and
544 * distance B->A is not. QEMU knows the distance of a node to
545 * itself is always 10, so A->A distances may be omitted. When
546 * the distances of two nodes of a pair differ, i.e. distance
547 * A->B != distance B->A, then that means the distance table is
548 * asymmetric. In this case, the distances for both directions
549 * of all node pairs are required.
551 if (have_numa_distance) {
552 /* Validate enough NUMA distance information was provided. */
553 validate_numa_distance();
555 /* Validation succeeded, now fill in any missing distances. */
556 complete_init_numa_distance();
558 } else {
559 numa_set_mem_node_id(0, ram_size, 0);
563 void numa_post_machine_init(void)
565 CPUState *cpu;
566 int i;
568 CPU_FOREACH(cpu) {
569 for (i = 0; i < nb_numa_nodes; i++) {
570 assert(cpu->cpu_index < max_cpus);
571 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
572 cpu->numa_node = i;
578 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
579 const char *name,
580 uint64_t ram_size)
582 if (mem_path) {
583 #ifdef __linux__
584 Error *err = NULL;
585 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
586 mem_path, &err);
587 if (err) {
588 error_report_err(err);
589 if (mem_prealloc) {
590 exit(1);
593 /* Legacy behavior: if allocation failed, fall back to
594 * regular RAM allocation.
596 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
598 #else
599 fprintf(stderr, "-mem-path not supported on this host\n");
600 exit(1);
601 #endif
602 } else {
603 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
605 vmstate_register_ram_global(mr);
608 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
609 const char *name,
610 uint64_t ram_size)
612 uint64_t addr = 0;
613 int i;
615 if (nb_numa_nodes == 0 || !have_memdevs) {
616 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
617 return;
620 memory_region_init(mr, owner, name, ram_size);
621 for (i = 0; i < MAX_NODES; i++) {
622 uint64_t size = numa_info[i].node_mem;
623 HostMemoryBackend *backend = numa_info[i].node_memdev;
624 if (!backend) {
625 continue;
627 MemoryRegion *seg = host_memory_backend_get_memory(backend,
628 &error_fatal);
630 if (memory_region_is_mapped(seg)) {
631 char *path = object_get_canonical_path_component(OBJECT(backend));
632 error_report("memory backend %s is used multiple times. Each "
633 "-numa option must use a different memdev value.",
634 path);
635 exit(1);
638 host_memory_backend_set_mapped(backend, true);
639 memory_region_add_subregion(mr, addr, seg);
640 vmstate_register_ram_global(seg);
641 addr += size;
645 static void numa_stat_memory_devices(uint64_t node_mem[])
647 MemoryDeviceInfoList *info_list = NULL;
648 MemoryDeviceInfoList **prev = &info_list;
649 MemoryDeviceInfoList *info;
651 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
652 for (info = info_list; info; info = info->next) {
653 MemoryDeviceInfo *value = info->value;
655 if (value) {
656 switch (value->type) {
657 case MEMORY_DEVICE_INFO_KIND_DIMM:
658 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
659 break;
660 default:
661 break;
665 qapi_free_MemoryDeviceInfoList(info_list);
668 void query_numa_node_mem(uint64_t node_mem[])
670 int i;
672 if (nb_numa_nodes <= 0) {
673 return;
676 numa_stat_memory_devices(node_mem);
677 for (i = 0; i < nb_numa_nodes; i++) {
678 node_mem[i] += numa_info[i].node_mem;
682 static int query_memdev(Object *obj, void *opaque)
684 MemdevList **list = opaque;
685 MemdevList *m = NULL;
687 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
688 m = g_malloc0(sizeof(*m));
690 m->value = g_malloc0(sizeof(*m->value));
692 m->value->id = object_property_get_str(obj, "id", NULL);
693 m->value->has_id = !!m->value->id;
695 m->value->size = object_property_get_int(obj, "size",
696 &error_abort);
697 m->value->merge = object_property_get_bool(obj, "merge",
698 &error_abort);
699 m->value->dump = object_property_get_bool(obj, "dump",
700 &error_abort);
701 m->value->prealloc = object_property_get_bool(obj,
702 "prealloc",
703 &error_abort);
704 m->value->policy = object_property_get_enum(obj,
705 "policy",
706 "HostMemPolicy",
707 &error_abort);
708 object_property_get_uint16List(obj, "host-nodes",
709 &m->value->host_nodes,
710 &error_abort);
712 m->next = *list;
713 *list = m;
716 return 0;
719 MemdevList *qmp_query_memdev(Error **errp)
721 Object *obj = object_get_objects_root();
722 MemdevList *list = NULL;
724 object_child_foreach(obj, query_memdev, &list);
725 return list;
728 int numa_get_node_for_cpu(int idx)
730 int i;
732 assert(idx < max_cpus);
734 for (i = 0; i < nb_numa_nodes; i++) {
735 if (test_bit(idx, numa_info[i].node_cpu)) {
736 break;
739 return i;
742 void ram_block_notifier_add(RAMBlockNotifier *n)
744 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
747 void ram_block_notifier_remove(RAMBlockNotifier *n)
749 QLIST_REMOVE(n, next);
752 void ram_block_notify_add(void *host, size_t size)
754 RAMBlockNotifier *notifier;
756 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
757 notifier->ram_block_added(notifier, host, size);
761 void ram_block_notify_remove(void *host, size_t size)
763 RAMBlockNotifier *notifier;
765 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
766 notifier->ram_block_removed(notifier, host, size);