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