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
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"
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"
46 QemuOptsList qemu_numa_opts
= {
48 .implied_opt_name
= "type",
49 .head
= QTAILQ_HEAD_INITIALIZER(qemu_numa_opts
.head
),
50 .desc
= { { 0 } } /* validated with OptsVisitor */
53 static int have_memdevs
;
55 static int max_numa_nodeid
; /* Highest specified NUMA node ID, plus one.
56 * For all nodes, nodeid < max_numa_nodeid
59 bool have_numa_distance
;
60 NodeInfo numa_info
[MAX_NODES
];
63 static void parse_numa_node(MachineState
*ms
, NumaNodeOptions
*node
,
68 uint16List
*cpus
= NULL
;
69 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
70 unsigned int max_cpus
= ms
->smp
.max_cpus
;
72 if (node
->has_nodeid
) {
73 nodenr
= node
->nodeid
;
75 nodenr
= nb_numa_nodes
;
78 if (nodenr
>= MAX_NODES
) {
79 error_setg(errp
, "Max number of NUMA nodes reached: %"
84 if (numa_info
[nodenr
].present
) {
85 error_setg(errp
, "Duplicate NUMA nodeid: %" PRIu16
, nodenr
);
89 if (!mc
->cpu_index_to_instance_props
|| !mc
->get_default_cpu_node_id
) {
90 error_setg(errp
, "NUMA is not supported by this machine-type");
93 for (cpus
= node
->cpus
; cpus
; cpus
= cpus
->next
) {
94 CpuInstanceProperties props
;
95 if (cpus
->value
>= max_cpus
) {
97 "CPU index (%" PRIu16
")"
98 " should be smaller than maxcpus (%d)",
99 cpus
->value
, max_cpus
);
102 props
= mc
->cpu_index_to_instance_props(ms
, cpus
->value
);
103 props
.node_id
= nodenr
;
104 props
.has_node_id
= true;
105 machine_set_cpu_numa_node(ms
, &props
, &err
);
107 error_propagate(errp
, err
);
112 have_memdevs
= have_memdevs
? : node
->has_memdev
;
113 have_mem
= have_mem
? : node
->has_mem
;
114 if ((node
->has_mem
&& have_memdevs
) || (node
->has_memdev
&& have_mem
)) {
115 error_setg(errp
, "numa configuration should use either mem= or memdev=,"
116 "mixing both is not allowed");
121 numa_info
[nodenr
].node_mem
= node
->mem
;
122 if (!qtest_enabled()) {
123 warn_report("Parameter -numa node,mem is deprecated,"
124 " use -numa node,memdev instead");
127 if (node
->has_memdev
) {
129 o
= object_resolve_path_type(node
->memdev
, TYPE_MEMORY_BACKEND
, NULL
);
131 error_setg(errp
, "memdev=%s is ambiguous", node
->memdev
);
136 numa_info
[nodenr
].node_mem
= object_property_get_uint(o
, "size", NULL
);
137 numa_info
[nodenr
].node_memdev
= MEMORY_BACKEND(o
);
139 numa_info
[nodenr
].present
= true;
140 max_numa_nodeid
= MAX(max_numa_nodeid
, nodenr
+ 1);
144 static void parse_numa_distance(NumaDistOptions
*dist
, Error
**errp
)
146 uint16_t src
= dist
->src
;
147 uint16_t dst
= dist
->dst
;
148 uint8_t val
= dist
->val
;
150 if (src
>= MAX_NODES
|| dst
>= MAX_NODES
) {
151 error_setg(errp
, "Parameter '%s' expects an integer between 0 and %d",
152 src
>= MAX_NODES
? "src" : "dst", MAX_NODES
- 1);
156 if (!numa_info
[src
].present
|| !numa_info
[dst
].present
) {
157 error_setg(errp
, "Source/Destination NUMA node is missing. "
158 "Please use '-numa node' option to declare it first.");
162 if (val
< NUMA_DISTANCE_MIN
) {
163 error_setg(errp
, "NUMA distance (%" PRIu8
") is invalid, "
164 "it shouldn't be less than %d.",
165 val
, NUMA_DISTANCE_MIN
);
169 if (src
== dst
&& val
!= NUMA_DISTANCE_MIN
) {
170 error_setg(errp
, "Local distance of node %d should be %d.",
171 src
, NUMA_DISTANCE_MIN
);
175 numa_info
[src
].distance
[dst
] = val
;
176 have_numa_distance
= true;
179 void set_numa_options(MachineState
*ms
, NumaOptions
*object
, Error
**errp
)
183 switch (object
->type
) {
184 case NUMA_OPTIONS_TYPE_NODE
:
185 parse_numa_node(ms
, &object
->u
.node
, &err
);
190 case NUMA_OPTIONS_TYPE_DIST
:
191 parse_numa_distance(&object
->u
.dist
, &err
);
196 case NUMA_OPTIONS_TYPE_CPU
:
197 if (!object
->u
.cpu
.has_node_id
) {
198 error_setg(&err
, "Missing mandatory node-id property");
201 if (!numa_info
[object
->u
.cpu
.node_id
].present
) {
202 error_setg(&err
, "Invalid node-id=%" PRId64
", NUMA node must be "
203 "defined with -numa node,nodeid=ID before it's used with "
204 "-numa cpu,node-id=ID", object
->u
.cpu
.node_id
);
208 machine_set_cpu_numa_node(ms
, qapi_NumaCpuOptions_base(&object
->u
.cpu
),
216 error_propagate(errp
, err
);
219 static int parse_numa(void *opaque
, QemuOpts
*opts
, Error
**errp
)
221 NumaOptions
*object
= NULL
;
222 MachineState
*ms
= MACHINE(opaque
);
224 Visitor
*v
= opts_visitor_new(opts
);
226 visit_type_NumaOptions(v
, NULL
, &object
, &err
);
232 /* Fix up legacy suffix-less format */
233 if ((object
->type
== NUMA_OPTIONS_TYPE_NODE
) && object
->u
.node
.has_mem
) {
234 const char *mem_str
= qemu_opt_get(opts
, "mem");
235 qemu_strtosz_MiB(mem_str
, NULL
, &object
->u
.node
.mem
);
238 set_numa_options(ms
, object
, &err
);
241 qapi_free_NumaOptions(object
);
243 error_propagate(errp
, err
);
250 /* If all node pair distances are symmetric, then only distances
251 * in one direction are enough. If there is even one asymmetric
252 * pair, though, then all distances must be provided. The
253 * distance from a node to itself is always NUMA_DISTANCE_MIN,
254 * so providing it is never necessary.
256 static void validate_numa_distance(void)
259 bool is_asymmetrical
= false;
261 for (src
= 0; src
< nb_numa_nodes
; src
++) {
262 for (dst
= src
; dst
< nb_numa_nodes
; dst
++) {
263 if (numa_info
[src
].distance
[dst
] == 0 &&
264 numa_info
[dst
].distance
[src
] == 0) {
266 error_report("The distance between node %d and %d is "
267 "missing, at least one distance value "
268 "between each nodes should be provided.",
274 if (numa_info
[src
].distance
[dst
] != 0 &&
275 numa_info
[dst
].distance
[src
] != 0 &&
276 numa_info
[src
].distance
[dst
] !=
277 numa_info
[dst
].distance
[src
]) {
278 is_asymmetrical
= true;
283 if (is_asymmetrical
) {
284 for (src
= 0; src
< nb_numa_nodes
; src
++) {
285 for (dst
= 0; dst
< nb_numa_nodes
; dst
++) {
286 if (src
!= dst
&& numa_info
[src
].distance
[dst
] == 0) {
287 error_report("At least one asymmetrical pair of "
288 "distances is given, please provide distances "
289 "for both directions of all node pairs.");
297 static void complete_init_numa_distance(void)
301 /* Fixup NUMA distance by symmetric policy because if it is an
302 * asymmetric distance table, it should be a complete table and
303 * there would not be any missing distance except local node, which
304 * is verified by validate_numa_distance above.
306 for (src
= 0; src
< nb_numa_nodes
; src
++) {
307 for (dst
= 0; dst
< nb_numa_nodes
; dst
++) {
308 if (numa_info
[src
].distance
[dst
] == 0) {
310 numa_info
[src
].distance
[dst
] = NUMA_DISTANCE_MIN
;
312 numa_info
[src
].distance
[dst
] = numa_info
[dst
].distance
[src
];
319 void numa_legacy_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
320 int nb_nodes
, ram_addr_t size
)
323 uint64_t usedmem
= 0;
325 /* Align each node according to the alignment
326 * requirements of the machine class
329 for (i
= 0; i
< nb_nodes
- 1; i
++) {
330 nodes
[i
].node_mem
= (size
/ nb_nodes
) &
331 ~((1 << mc
->numa_mem_align_shift
) - 1);
332 usedmem
+= nodes
[i
].node_mem
;
334 nodes
[i
].node_mem
= size
- usedmem
;
337 void numa_default_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
338 int nb_nodes
, ram_addr_t size
)
341 uint64_t usedmem
= 0, node_mem
;
342 uint64_t granularity
= size
/ nb_nodes
;
343 uint64_t propagate
= 0;
345 for (i
= 0; i
< nb_nodes
- 1; i
++) {
346 node_mem
= (granularity
+ propagate
) &
347 ~((1 << mc
->numa_mem_align_shift
) - 1);
348 propagate
= granularity
+ propagate
- node_mem
;
349 nodes
[i
].node_mem
= node_mem
;
352 nodes
[i
].node_mem
= size
- usedmem
;
355 void numa_complete_configuration(MachineState
*ms
)
358 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
361 * If memory hotplug is enabled (slots > 0) but without '-numa'
362 * options explicitly on CLI, guestes will break.
364 * Windows: won't enable memory hotplug without SRAT table at all
366 * Linux: if QEMU is started with initial memory all below 4Gb
367 * and no SRAT table present, guest kernel will use nommu DMA ops,
368 * which breaks 32bit hw drivers when memory is hotplugged and
369 * guest tries to use it with that drivers.
371 * Enable NUMA implicitly by adding a new NUMA node automatically.
373 if (ms
->ram_slots
> 0 && nb_numa_nodes
== 0 &&
374 mc
->auto_enable_numa_with_memhp
) {
375 NumaNodeOptions node
= { };
376 parse_numa_node(ms
, &node
, &error_abort
);
379 assert(max_numa_nodeid
<= MAX_NODES
);
381 /* No support for sparse NUMA node IDs yet: */
382 for (i
= max_numa_nodeid
- 1; i
>= 0; i
--) {
383 /* Report large node IDs first, to make mistakes easier to spot */
384 if (!numa_info
[i
].present
) {
385 error_report("numa: Node ID missing: %d", i
);
390 /* This must be always true if all nodes are present: */
391 assert(nb_numa_nodes
== max_numa_nodeid
);
393 if (nb_numa_nodes
> 0) {
396 if (nb_numa_nodes
> MAX_NODES
) {
397 nb_numa_nodes
= MAX_NODES
;
400 /* If no memory size is given for any node, assume the default case
401 * and distribute the available memory equally across all nodes
403 for (i
= 0; i
< nb_numa_nodes
; i
++) {
404 if (numa_info
[i
].node_mem
!= 0) {
408 if (i
== nb_numa_nodes
) {
409 assert(mc
->numa_auto_assign_ram
);
410 mc
->numa_auto_assign_ram(mc
, numa_info
, nb_numa_nodes
, ram_size
);
411 if (!qtest_enabled()) {
412 warn_report("Default splitting of RAM between nodes is deprecated,"
413 " Use '-numa node,memdev' to explictly define RAM"
414 " allocation per node");
419 for (i
= 0; i
< nb_numa_nodes
; i
++) {
420 numa_total
+= numa_info
[i
].node_mem
;
422 if (numa_total
!= ram_size
) {
423 error_report("total memory for NUMA nodes (0x%" PRIx64
")"
424 " should equal RAM size (0x" RAM_ADDR_FMT
")",
425 numa_total
, ram_size
);
429 /* QEMU needs at least all unique node pair distances to build
430 * the whole NUMA distance table. QEMU treats the distance table
431 * as symmetric by default, i.e. distance A->B == distance B->A.
432 * Thus, QEMU is able to complete the distance table
433 * initialization even though only distance A->B is provided and
434 * distance B->A is not. QEMU knows the distance of a node to
435 * itself is always 10, so A->A distances may be omitted. When
436 * the distances of two nodes of a pair differ, i.e. distance
437 * A->B != distance B->A, then that means the distance table is
438 * asymmetric. In this case, the distances for both directions
439 * of all node pairs are required.
441 if (have_numa_distance
) {
442 /* Validate enough NUMA distance information was provided. */
443 validate_numa_distance();
445 /* Validation succeeded, now fill in any missing distances. */
446 complete_init_numa_distance();
451 void parse_numa_opts(MachineState
*ms
)
453 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa
, ms
, &error_fatal
);
456 void numa_cpu_pre_plug(const CPUArchId
*slot
, DeviceState
*dev
, Error
**errp
)
458 int node_id
= object_property_get_int(OBJECT(dev
), "node-id", &error_abort
);
460 if (node_id
== CPU_UNSET_NUMA_NODE_ID
) {
461 /* due to bug in libvirt, it doesn't pass node-id from props on
462 * device_add as expected, so we have to fix it up here */
463 if (slot
->props
.has_node_id
) {
464 object_property_set_int(OBJECT(dev
), slot
->props
.node_id
,
467 } else if (node_id
!= slot
->props
.node_id
) {
468 error_setg(errp
, "invalid node-id, must be %"PRId64
,
469 slot
->props
.node_id
);
473 static void allocate_system_memory_nonnuma(MemoryRegion
*mr
, Object
*owner
,
480 memory_region_init_ram_from_file(mr
, owner
, name
, ram_size
, 0, 0,
483 error_report_err(err
);
487 warn_report("falling back to regular RAM allocation");
488 error_printf("This is deprecated. Make sure that -mem-path "
489 " specified path has sufficient resources to allocate"
490 " -m specified RAM amount");
491 /* Legacy behavior: if allocation failed, fall back to
492 * regular RAM allocation.
495 memory_region_init_ram_nomigrate(mr
, owner
, name
, ram_size
, &error_fatal
);
498 fprintf(stderr
, "-mem-path not supported on this host\n");
502 memory_region_init_ram_nomigrate(mr
, owner
, name
, ram_size
, &error_fatal
);
504 vmstate_register_ram_global(mr
);
507 void memory_region_allocate_system_memory(MemoryRegion
*mr
, Object
*owner
,
514 if (nb_numa_nodes
== 0 || !have_memdevs
) {
515 allocate_system_memory_nonnuma(mr
, owner
, name
, ram_size
);
519 memory_region_init(mr
, owner
, name
, ram_size
);
520 for (i
= 0; i
< nb_numa_nodes
; i
++) {
521 uint64_t size
= numa_info
[i
].node_mem
;
522 HostMemoryBackend
*backend
= numa_info
[i
].node_memdev
;
526 MemoryRegion
*seg
= host_memory_backend_get_memory(backend
);
528 if (memory_region_is_mapped(seg
)) {
529 char *path
= object_get_canonical_path_component(OBJECT(backend
));
530 error_report("memory backend %s is used multiple times. Each "
531 "-numa option must use a different memdev value.",
537 host_memory_backend_set_mapped(backend
, true);
538 memory_region_add_subregion(mr
, addr
, seg
);
539 vmstate_register_ram_global(seg
);
544 static void numa_stat_memory_devices(NumaNodeMem node_mem
[])
546 MemoryDeviceInfoList
*info_list
= qmp_memory_device_list();
547 MemoryDeviceInfoList
*info
;
548 PCDIMMDeviceInfo
*pcdimm_info
;
549 VirtioPMEMDeviceInfo
*vpi
;
551 for (info
= info_list
; info
; info
= info
->next
) {
552 MemoryDeviceInfo
*value
= info
->value
;
555 switch (value
->type
) {
556 case MEMORY_DEVICE_INFO_KIND_DIMM
:
557 case MEMORY_DEVICE_INFO_KIND_NVDIMM
:
558 pcdimm_info
= value
->type
== MEMORY_DEVICE_INFO_KIND_DIMM
?
559 value
->u
.dimm
.data
: value
->u
.nvdimm
.data
;
560 node_mem
[pcdimm_info
->node
].node_mem
+= pcdimm_info
->size
;
561 node_mem
[pcdimm_info
->node
].node_plugged_mem
+=
564 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM
:
565 vpi
= value
->u
.virtio_pmem
.data
;
566 /* TODO: once we support numa, assign to right node */
567 node_mem
[0].node_mem
+= vpi
->size
;
568 node_mem
[0].node_plugged_mem
+= vpi
->size
;
571 g_assert_not_reached();
575 qapi_free_MemoryDeviceInfoList(info_list
);
578 void query_numa_node_mem(NumaNodeMem node_mem
[])
582 if (nb_numa_nodes
<= 0) {
586 numa_stat_memory_devices(node_mem
);
587 for (i
= 0; i
< nb_numa_nodes
; i
++) {
588 node_mem
[i
].node_mem
+= numa_info
[i
].node_mem
;
592 void ram_block_notifier_add(RAMBlockNotifier
*n
)
594 QLIST_INSERT_HEAD(&ram_list
.ramblock_notifiers
, n
, next
);
597 void ram_block_notifier_remove(RAMBlockNotifier
*n
)
599 QLIST_REMOVE(n
, next
);
602 void ram_block_notify_add(void *host
, size_t size
)
604 RAMBlockNotifier
*notifier
;
606 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
607 notifier
->ram_block_added(notifier
, host
, size
);
611 void ram_block_notify_remove(void *host
, size_t size
)
613 RAMBlockNotifier
*notifier
;
615 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
616 notifier
->ram_block_removed(notifier
, host
, size
);