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/numa.h"
27 #include "exec/cpu-common.h"
28 #include "exec/ramlist.h"
29 #include "qemu/bitmap.h"
31 #include "qemu/error-report.h"
32 #include "qapi/error.h"
33 #include "qapi/opts-visitor.h"
34 #include "qapi/qapi-commands-misc.h"
35 #include "qapi/qapi-visit-misc.h"
36 #include "hw/boards.h"
37 #include "sysemu/hostmem.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
= {
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
55 bool have_numa_distance
;
56 NodeInfo numa_info
[MAX_NODES
];
59 static void parse_numa_node(MachineState
*ms
, NumaNodeOptions
*node
,
63 uint16List
*cpus
= NULL
;
64 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
66 if (node
->has_nodeid
) {
67 nodenr
= node
->nodeid
;
69 nodenr
= nb_numa_nodes
;
72 if (nodenr
>= MAX_NODES
) {
73 error_setg(errp
, "Max number of NUMA nodes reached: %"
78 if (numa_info
[nodenr
].present
) {
79 error_setg(errp
, "Duplicate NUMA nodeid: %" PRIu16
, nodenr
);
83 if (!mc
->cpu_index_to_instance_props
|| !mc
->get_default_cpu_node_id
) {
84 error_report("NUMA is not supported by this machine-type");
87 for (cpus
= node
->cpus
; cpus
; cpus
= cpus
->next
) {
88 CpuInstanceProperties props
;
89 if (cpus
->value
>= max_cpus
) {
91 "CPU index (%" PRIu16
")"
92 " should be smaller than maxcpus (%d)",
93 cpus
->value
, max_cpus
);
96 props
= mc
->cpu_index_to_instance_props(ms
, cpus
->value
);
97 props
.node_id
= nodenr
;
98 props
.has_node_id
= true;
99 machine_set_cpu_numa_node(ms
, &props
, &error_fatal
);
102 if (node
->has_mem
&& node
->has_memdev
) {
103 error_setg(errp
, "cannot specify both mem= and memdev=");
107 if (have_memdevs
== -1) {
108 have_memdevs
= node
->has_memdev
;
110 if (node
->has_memdev
!= have_memdevs
) {
111 error_setg(errp
, "memdev option must be specified for either "
117 numa_info
[nodenr
].node_mem
= node
->mem
;
119 if (node
->has_memdev
) {
121 o
= object_resolve_path_type(node
->memdev
, TYPE_MEMORY_BACKEND
, NULL
);
123 error_setg(errp
, "memdev=%s is ambiguous", node
->memdev
);
128 numa_info
[nodenr
].node_mem
= object_property_get_uint(o
, "size", NULL
);
129 numa_info
[nodenr
].node_memdev
= MEMORY_BACKEND(o
);
131 numa_info
[nodenr
].present
= true;
132 max_numa_nodeid
= MAX(max_numa_nodeid
, nodenr
+ 1);
136 static void parse_numa_distance(NumaDistOptions
*dist
, Error
**errp
)
138 uint16_t src
= dist
->src
;
139 uint16_t dst
= dist
->dst
;
140 uint8_t val
= dist
->val
;
142 if (src
>= MAX_NODES
|| dst
>= MAX_NODES
) {
144 "Invalid node %d, max possible could be %d",
145 MAX(src
, dst
), MAX_NODES
);
149 if (!numa_info
[src
].present
|| !numa_info
[dst
].present
) {
150 error_setg(errp
, "Source/Destination NUMA node is missing. "
151 "Please use '-numa node' option to declare it first.");
155 if (val
< NUMA_DISTANCE_MIN
) {
156 error_setg(errp
, "NUMA distance (%" PRIu8
") is invalid, "
157 "it shouldn't be less than %d.",
158 val
, NUMA_DISTANCE_MIN
);
162 if (src
== dst
&& val
!= NUMA_DISTANCE_MIN
) {
163 error_setg(errp
, "Local distance of node %d should be %d.",
164 src
, NUMA_DISTANCE_MIN
);
168 numa_info
[src
].distance
[dst
] = val
;
169 have_numa_distance
= true;
172 static int parse_numa(void *opaque
, QemuOpts
*opts
, Error
**errp
)
174 NumaOptions
*object
= NULL
;
175 MachineState
*ms
= opaque
;
179 Visitor
*v
= opts_visitor_new(opts
);
180 visit_type_NumaOptions(v
, NULL
, &object
, &err
);
188 /* Fix up legacy suffix-less format */
189 if ((object
->type
== NUMA_OPTIONS_TYPE_NODE
) && object
->u
.node
.has_mem
) {
190 const char *mem_str
= qemu_opt_get(opts
, "mem");
191 qemu_strtosz_MiB(mem_str
, NULL
, &object
->u
.node
.mem
);
194 switch (object
->type
) {
195 case NUMA_OPTIONS_TYPE_NODE
:
196 parse_numa_node(ms
, &object
->u
.node
, &err
);
201 case NUMA_OPTIONS_TYPE_DIST
:
202 parse_numa_distance(&object
->u
.dist
, &err
);
207 case NUMA_OPTIONS_TYPE_CPU
:
208 if (!object
->u
.cpu
.has_node_id
) {
209 error_setg(&err
, "Missing mandatory node-id property");
212 if (!numa_info
[object
->u
.cpu
.node_id
].present
) {
213 error_setg(&err
, "Invalid node-id=%" PRId64
", NUMA node must be "
214 "defined with -numa node,nodeid=ID before it's used with "
215 "-numa cpu,node-id=ID", object
->u
.cpu
.node_id
);
219 machine_set_cpu_numa_node(ms
, qapi_NumaCpuOptions_base(&object
->u
.cpu
),
227 qapi_free_NumaOptions(object
);
229 error_report_err(err
);
236 /* If all node pair distances are symmetric, then only distances
237 * in one direction are enough. If there is even one asymmetric
238 * pair, though, then all distances must be provided. The
239 * distance from a node to itself is always NUMA_DISTANCE_MIN,
240 * so providing it is never necessary.
242 static void validate_numa_distance(void)
245 bool is_asymmetrical
= false;
247 for (src
= 0; src
< nb_numa_nodes
; src
++) {
248 for (dst
= src
; dst
< nb_numa_nodes
; dst
++) {
249 if (numa_info
[src
].distance
[dst
] == 0 &&
250 numa_info
[dst
].distance
[src
] == 0) {
252 error_report("The distance between node %d and %d is "
253 "missing, at least one distance value "
254 "between each nodes should be provided.",
260 if (numa_info
[src
].distance
[dst
] != 0 &&
261 numa_info
[dst
].distance
[src
] != 0 &&
262 numa_info
[src
].distance
[dst
] !=
263 numa_info
[dst
].distance
[src
]) {
264 is_asymmetrical
= true;
269 if (is_asymmetrical
) {
270 for (src
= 0; src
< nb_numa_nodes
; src
++) {
271 for (dst
= 0; dst
< nb_numa_nodes
; dst
++) {
272 if (src
!= dst
&& numa_info
[src
].distance
[dst
] == 0) {
273 error_report("At least one asymmetrical pair of "
274 "distances is given, please provide distances "
275 "for both directions of all node pairs.");
283 static void complete_init_numa_distance(void)
287 /* Fixup NUMA distance by symmetric policy because if it is an
288 * asymmetric distance table, it should be a complete table and
289 * there would not be any missing distance except local node, which
290 * is verified by validate_numa_distance above.
292 for (src
= 0; src
< nb_numa_nodes
; src
++) {
293 for (dst
= 0; dst
< nb_numa_nodes
; dst
++) {
294 if (numa_info
[src
].distance
[dst
] == 0) {
296 numa_info
[src
].distance
[dst
] = NUMA_DISTANCE_MIN
;
298 numa_info
[src
].distance
[dst
] = numa_info
[dst
].distance
[src
];
305 void numa_legacy_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
306 int nb_nodes
, ram_addr_t size
)
309 uint64_t usedmem
= 0;
311 /* Align each node according to the alignment
312 * requirements of the machine class
315 for (i
= 0; i
< nb_nodes
- 1; i
++) {
316 nodes
[i
].node_mem
= (size
/ nb_nodes
) &
317 ~((1 << mc
->numa_mem_align_shift
) - 1);
318 usedmem
+= nodes
[i
].node_mem
;
320 nodes
[i
].node_mem
= size
- usedmem
;
323 void numa_default_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
324 int nb_nodes
, ram_addr_t size
)
327 uint64_t usedmem
= 0, node_mem
;
328 uint64_t granularity
= size
/ nb_nodes
;
329 uint64_t propagate
= 0;
331 for (i
= 0; i
< nb_nodes
- 1; i
++) {
332 node_mem
= (granularity
+ propagate
) &
333 ~((1 << mc
->numa_mem_align_shift
) - 1);
334 propagate
= granularity
+ propagate
- node_mem
;
335 nodes
[i
].node_mem
= node_mem
;
338 nodes
[i
].node_mem
= size
- usedmem
;
341 void parse_numa_opts(MachineState
*ms
)
344 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
346 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa
, ms
, NULL
)) {
351 * If memory hotplug is enabled (slots > 0) but without '-numa'
352 * options explicitly on CLI, guestes will break.
354 * Windows: won't enable memory hotplug without SRAT table at all
356 * Linux: if QEMU is started with initial memory all below 4Gb
357 * and no SRAT table present, guest kernel will use nommu DMA ops,
358 * which breaks 32bit hw drivers when memory is hotplugged and
359 * guest tries to use it with that drivers.
361 * Enable NUMA implicitly by adding a new NUMA node automatically.
363 if (ms
->ram_slots
> 0 && nb_numa_nodes
== 0 &&
364 mc
->auto_enable_numa_with_memhp
) {
365 NumaNodeOptions node
= { };
366 parse_numa_node(ms
, &node
, NULL
);
369 assert(max_numa_nodeid
<= MAX_NODES
);
371 /* No support for sparse NUMA node IDs yet: */
372 for (i
= max_numa_nodeid
- 1; i
>= 0; i
--) {
373 /* Report large node IDs first, to make mistakes easier to spot */
374 if (!numa_info
[i
].present
) {
375 error_report("numa: Node ID missing: %d", i
);
380 /* This must be always true if all nodes are present: */
381 assert(nb_numa_nodes
== max_numa_nodeid
);
383 if (nb_numa_nodes
> 0) {
386 if (nb_numa_nodes
> MAX_NODES
) {
387 nb_numa_nodes
= MAX_NODES
;
390 /* If no memory size is given for any node, assume the default case
391 * and distribute the available memory equally across all nodes
393 for (i
= 0; i
< nb_numa_nodes
; i
++) {
394 if (numa_info
[i
].node_mem
!= 0) {
398 if (i
== nb_numa_nodes
) {
399 assert(mc
->numa_auto_assign_ram
);
400 mc
->numa_auto_assign_ram(mc
, numa_info
, nb_numa_nodes
, ram_size
);
404 for (i
= 0; i
< nb_numa_nodes
; i
++) {
405 numa_total
+= numa_info
[i
].node_mem
;
407 if (numa_total
!= ram_size
) {
408 error_report("total memory for NUMA nodes (0x%" PRIx64
")"
409 " should equal RAM size (0x" RAM_ADDR_FMT
")",
410 numa_total
, ram_size
);
414 /* QEMU needs at least all unique node pair distances to build
415 * the whole NUMA distance table. QEMU treats the distance table
416 * as symmetric by default, i.e. distance A->B == distance B->A.
417 * Thus, QEMU is able to complete the distance table
418 * initialization even though only distance A->B is provided and
419 * distance B->A is not. QEMU knows the distance of a node to
420 * itself is always 10, so A->A distances may be omitted. When
421 * the distances of two nodes of a pair differ, i.e. distance
422 * A->B != distance B->A, then that means the distance table is
423 * asymmetric. In this case, the distances for both directions
424 * of all node pairs are required.
426 if (have_numa_distance
) {
427 /* Validate enough NUMA distance information was provided. */
428 validate_numa_distance();
430 /* Validation succeeded, now fill in any missing distances. */
431 complete_init_numa_distance();
436 void numa_cpu_pre_plug(const CPUArchId
*slot
, DeviceState
*dev
, Error
**errp
)
438 int node_id
= object_property_get_int(OBJECT(dev
), "node-id", &error_abort
);
440 if (node_id
== CPU_UNSET_NUMA_NODE_ID
) {
441 /* due to bug in libvirt, it doesn't pass node-id from props on
442 * device_add as expected, so we have to fix it up here */
443 if (slot
->props
.has_node_id
) {
444 object_property_set_int(OBJECT(dev
), slot
->props
.node_id
,
447 } else if (node_id
!= slot
->props
.node_id
) {
448 error_setg(errp
, "node-id=%d must match numa node specified "
449 "with -numa option", node_id
);
453 static void allocate_system_memory_nonnuma(MemoryRegion
*mr
, Object
*owner
,
460 memory_region_init_ram_from_file(mr
, owner
, name
, ram_size
, 0, false,
463 error_report_err(err
);
467 error_report("falling back to regular RAM allocation.");
469 /* Legacy behavior: if allocation failed, fall back to
470 * regular RAM allocation.
472 memory_region_init_ram_nomigrate(mr
, owner
, name
, ram_size
, &error_fatal
);
475 fprintf(stderr
, "-mem-path not supported on this host\n");
479 memory_region_init_ram_nomigrate(mr
, owner
, name
, ram_size
, &error_fatal
);
481 vmstate_register_ram_global(mr
);
484 void memory_region_allocate_system_memory(MemoryRegion
*mr
, Object
*owner
,
491 if (nb_numa_nodes
== 0 || !have_memdevs
) {
492 allocate_system_memory_nonnuma(mr
, owner
, name
, ram_size
);
496 memory_region_init(mr
, owner
, name
, ram_size
);
497 for (i
= 0; i
< nb_numa_nodes
; i
++) {
498 uint64_t size
= numa_info
[i
].node_mem
;
499 HostMemoryBackend
*backend
= numa_info
[i
].node_memdev
;
503 MemoryRegion
*seg
= host_memory_backend_get_memory(backend
,
506 if (memory_region_is_mapped(seg
)) {
507 char *path
= object_get_canonical_path_component(OBJECT(backend
));
508 error_report("memory backend %s is used multiple times. Each "
509 "-numa option must use a different memdev value.",
514 host_memory_backend_set_mapped(backend
, true);
515 memory_region_add_subregion(mr
, addr
, seg
);
516 vmstate_register_ram_global(seg
);
521 static void numa_stat_memory_devices(NumaNodeMem node_mem
[])
523 MemoryDeviceInfoList
*info_list
= NULL
;
524 MemoryDeviceInfoList
**prev
= &info_list
;
525 MemoryDeviceInfoList
*info
;
526 PCDIMMDeviceInfo
*pcdimm_info
;
528 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
529 for (info
= info_list
; info
; info
= info
->next
) {
530 MemoryDeviceInfo
*value
= info
->value
;
533 switch (value
->type
) {
534 case MEMORY_DEVICE_INFO_KIND_DIMM
: {
535 pcdimm_info
= value
->u
.dimm
.data
;
536 node_mem
[pcdimm_info
->node
].node_mem
+= pcdimm_info
->size
;
537 if (pcdimm_info
->hotpluggable
&& pcdimm_info
->hotplugged
) {
538 node_mem
[pcdimm_info
->node
].node_plugged_mem
+=
549 qapi_free_MemoryDeviceInfoList(info_list
);
552 void query_numa_node_mem(NumaNodeMem node_mem
[])
556 if (nb_numa_nodes
<= 0) {
560 numa_stat_memory_devices(node_mem
);
561 for (i
= 0; i
< nb_numa_nodes
; i
++) {
562 node_mem
[i
].node_mem
+= numa_info
[i
].node_mem
;
566 static int query_memdev(Object
*obj
, void *opaque
)
568 MemdevList
**list
= opaque
;
569 MemdevList
*m
= NULL
;
571 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
572 m
= g_malloc0(sizeof(*m
));
574 m
->value
= g_malloc0(sizeof(*m
->value
));
576 m
->value
->id
= object_property_get_str(obj
, "id", NULL
);
577 m
->value
->has_id
= !!m
->value
->id
;
579 m
->value
->size
= object_property_get_uint(obj
, "size",
581 m
->value
->merge
= object_property_get_bool(obj
, "merge",
583 m
->value
->dump
= object_property_get_bool(obj
, "dump",
585 m
->value
->prealloc
= object_property_get_bool(obj
,
588 m
->value
->policy
= object_property_get_enum(obj
,
592 object_property_get_uint16List(obj
, "host-nodes",
593 &m
->value
->host_nodes
,
603 MemdevList
*qmp_query_memdev(Error
**errp
)
605 Object
*obj
= object_get_objects_root();
606 MemdevList
*list
= NULL
;
608 object_child_foreach(obj
, query_memdev
, &list
);
612 void ram_block_notifier_add(RAMBlockNotifier
*n
)
614 QLIST_INSERT_HEAD(&ram_list
.ramblock_notifiers
, n
, next
);
617 void ram_block_notifier_remove(RAMBlockNotifier
*n
)
619 QLIST_REMOVE(n
, next
);
622 void ram_block_notify_add(void *host
, size_t size
)
624 RAMBlockNotifier
*notifier
;
626 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
627 notifier
->ram_block_added(notifier
, host
, size
);
631 void ram_block_notify_remove(void *host
, size_t size
)
633 RAMBlockNotifier
*notifier
;
635 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
636 notifier
->ram_block_removed(notifier
, host
, size
);