]> git.proxmox.com Git - mirror_qemu.git/blob - numa.c
ui: fix dcl unregister
[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 "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"
42
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 */
48 };
49
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
53 */
54 int nb_numa_nodes;
55 bool have_numa_distance;
56 NodeInfo numa_info[MAX_NODES];
57
58 void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
59 {
60 struct numa_addr_range *range;
61
62 /*
63 * Memory-less nodes can come here with 0 size in which case,
64 * there is nothing to do.
65 */
66 if (!size) {
67 return;
68 }
69
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);
74 }
75
76 void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
77 {
78 struct numa_addr_range *range, *next;
79
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;
85 }
86 }
87 }
88
89 static void numa_set_mem_ranges(void)
90 {
91 int i;
92 ram_addr_t mem_start = 0;
93
94 /*
95 * Deduce start address of each node and use it to store
96 * the address range info in numa_info address range list
97 */
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;
101 }
102 }
103
104 /*
105 * Check if @addr falls under NUMA @node.
106 */
107 static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
108 {
109 struct numa_addr_range *range;
110
111 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
112 if (addr >= range->mem_start && addr <= range->mem_end) {
113 return true;
114 }
115 }
116 return false;
117 }
118
119 /*
120 * Given an address, return the index of the NUMA node to which the
121 * address belongs to.
122 */
123 uint32_t numa_get_node(ram_addr_t addr, Error **errp)
124 {
125 uint32_t i;
126
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;
131 }
132 }
133
134 for (i = 0; i < nb_numa_nodes; i++) {
135 if (numa_addr_belongs_to_node(addr, i)) {
136 return i;
137 }
138 }
139
140 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
141 "NUMA node", addr);
142 return -1;
143 }
144
145 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
146 Error **errp)
147 {
148 uint16_t nodenr;
149 uint16List *cpus = NULL;
150 MachineClass *mc = MACHINE_GET_CLASS(ms);
151
152 if (node->has_nodeid) {
153 nodenr = node->nodeid;
154 } else {
155 nodenr = nb_numa_nodes;
156 }
157
158 if (nodenr >= MAX_NODES) {
159 error_setg(errp, "Max number of NUMA nodes reached: %"
160 PRIu16 "", nodenr);
161 return;
162 }
163
164 if (numa_info[nodenr].present) {
165 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
166 return;
167 }
168
169 if (!mc->cpu_index_to_instance_props) {
170 error_report("NUMA is not supported by this machine-type");
171 exit(1);
172 }
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;
181 }
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);
186 }
187
188 if (node->has_mem && node->has_memdev) {
189 error_setg(errp, "cannot specify both mem= and memdev=");
190 return;
191 }
192
193 if (have_memdevs == -1) {
194 have_memdevs = node->has_memdev;
195 }
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;
200 }
201
202 if (node->has_mem) {
203 numa_info[nodenr].node_mem = node->mem;
204 }
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;
211 }
212
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);
216 }
217 numa_info[nodenr].present = true;
218 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
219 }
220
221 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
222 {
223 uint16_t src = dist->src;
224 uint16_t dst = dist->dst;
225 uint8_t val = dist->val;
226
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;
232 }
233
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;
238 }
239
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;
245 }
246
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;
251 }
252
253 numa_info[src].distance[dst] = val;
254 have_numa_distance = true;
255 }
256
257 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
258 {
259 NumaOptions *object = NULL;
260 MachineState *ms = opaque;
261 Error *err = NULL;
262
263 {
264 Visitor *v = opts_visitor_new(opts);
265 visit_type_NumaOptions(v, NULL, &object, &err);
266 visit_free(v);
267 }
268
269 if (err) {
270 goto end;
271 }
272
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);
277 }
278
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;
284 }
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;
291 }
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;
297 }
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;
303 }
304
305 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
306 &err);
307 break;
308 default:
309 abort();
310 }
311
312 end:
313 qapi_free_NumaOptions(object);
314 if (err) {
315 error_report_err(err);
316 return -1;
317 }
318
319 return 0;
320 }
321
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.
327 */
328 static void validate_numa_distance(void)
329 {
330 int src, dst;
331 bool is_asymmetrical = false;
332
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);
343 }
344 }
345
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;
351 }
352 }
353 }
354
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);
363 }
364 }
365 }
366 }
367 }
368
369 static void complete_init_numa_distance(void)
370 {
371 int src, dst;
372
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.
377 */
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];
385 }
386 }
387 }
388 }
389 }
390
391 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
392 int nb_nodes, ram_addr_t size)
393 {
394 int i;
395 uint64_t usedmem = 0;
396
397 /* Align each node according to the alignment
398 * requirements of the machine class
399 */
400
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;
405 }
406 nodes[i].node_mem = size - usedmem;
407 }
408
409 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
410 int nb_nodes, ram_addr_t size)
411 {
412 int i;
413 uint64_t usedmem = 0, node_mem;
414 uint64_t granularity = size / nb_nodes;
415 uint64_t propagate = 0;
416
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;
423 }
424 nodes[i].node_mem = size - usedmem;
425 }
426
427 void parse_numa_opts(MachineState *ms)
428 {
429 int i;
430 MachineClass *mc = MACHINE_GET_CLASS(ms);
431
432 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
433 exit(1);
434 }
435
436 assert(max_numa_nodeid <= MAX_NODES);
437
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);
444 }
445 }
446
447 /* This must be always true if all nodes are present: */
448 assert(nb_numa_nodes == max_numa_nodeid);
449
450 if (nb_numa_nodes > 0) {
451 uint64_t numa_total;
452
453 if (nb_numa_nodes > MAX_NODES) {
454 nb_numa_nodes = MAX_NODES;
455 }
456
457 /* If no memory size is given for any node, assume the default case
458 * and distribute the available memory equally across all nodes
459 */
460 for (i = 0; i < nb_numa_nodes; i++) {
461 if (numa_info[i].node_mem != 0) {
462 break;
463 }
464 }
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);
468 }
469
470 numa_total = 0;
471 for (i = 0; i < nb_numa_nodes; i++) {
472 numa_total += numa_info[i].node_mem;
473 }
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);
479 }
480
481 for (i = 0; i < nb_numa_nodes; i++) {
482 QLIST_INIT(&numa_info[i].addr);
483 }
484
485 numa_set_mem_ranges();
486
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.
498 */
499 if (have_numa_distance) {
500 /* Validate enough NUMA distance information was provided. */
501 validate_numa_distance();
502
503 /* Validation succeeded, now fill in any missing distances. */
504 complete_init_numa_distance();
505 }
506 } else {
507 numa_set_mem_node_id(0, ram_size, 0);
508 }
509 }
510
511 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
512 {
513 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
514
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);
521 }
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);
525 }
526 }
527
528 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
529 const char *name,
530 uint64_t ram_size)
531 {
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);
541 }
542
543 /* Legacy behavior: if allocation failed, fall back to
544 * regular RAM allocation.
545 */
546 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
547 }
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);
554 }
555 vmstate_register_ram_global(mr);
556 }
557
558 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
559 const char *name,
560 uint64_t ram_size)
561 {
562 uint64_t addr = 0;
563 int i;
564
565 if (nb_numa_nodes == 0 || !have_memdevs) {
566 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
567 return;
568 }
569
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;
576 }
577 MemoryRegion *seg = host_memory_backend_get_memory(backend,
578 &error_fatal);
579
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);
586 }
587
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;
592 }
593 }
594
595 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
596 {
597 MemoryDeviceInfoList *info_list = NULL;
598 MemoryDeviceInfoList **prev = &info_list;
599 MemoryDeviceInfoList *info;
600 PCDIMMDeviceInfo *pcdimm_info;
601
602 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
603 for (info = info_list; info; info = info->next) {
604 MemoryDeviceInfo *value = info->value;
605
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;
614 }
615 break;
616 }
617
618 default:
619 break;
620 }
621 }
622 }
623 qapi_free_MemoryDeviceInfoList(info_list);
624 }
625
626 void query_numa_node_mem(NumaNodeMem node_mem[])
627 {
628 int i;
629
630 if (nb_numa_nodes <= 0) {
631 return;
632 }
633
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;
637 }
638 }
639
640 static int query_memdev(Object *obj, void *opaque)
641 {
642 MemdevList **list = opaque;
643 MemdevList *m = NULL;
644
645 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
646 m = g_malloc0(sizeof(*m));
647
648 m->value = g_malloc0(sizeof(*m->value));
649
650 m->value->id = object_property_get_str(obj, "id", NULL);
651 m->value->has_id = !!m->value->id;
652
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);
669
670 m->next = *list;
671 *list = m;
672 }
673
674 return 0;
675 }
676
677 MemdevList *qmp_query_memdev(Error **errp)
678 {
679 Object *obj = object_get_objects_root();
680 MemdevList *list = NULL;
681
682 object_child_foreach(obj, query_memdev, &list);
683 return list;
684 }
685
686 void ram_block_notifier_add(RAMBlockNotifier *n)
687 {
688 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
689 }
690
691 void ram_block_notifier_remove(RAMBlockNotifier *n)
692 {
693 QLIST_REMOVE(n, next);
694 }
695
696 void ram_block_notify_add(void *host, size_t size)
697 {
698 RAMBlockNotifier *notifier;
699
700 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
701 notifier->ram_block_added(notifier, host, size);
702 }
703 }
704
705 void ram_block_notify_remove(void *host, size_t size)
706 {
707 RAMBlockNotifier *notifier;
708
709 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
710 notifier->ram_block_removed(notifier, host, size);
711 }
712 }