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 "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
= {
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
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.
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
);
88 static void numa_set_mem_ranges(void)
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
) {
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
)
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)) {
133 for (i
= 0; i
< nb_numa_nodes
; i
++) {
134 if (numa_addr_belongs_to_node(addr
, i
)) {
139 error_setg(errp
, "Address 0x" RAM_ADDR_FMT
" doesn't belong to any "
144 static void parse_numa_node(MachineState
*ms
, NumaNodeOptions
*node
,
145 QemuOpts
*opts
, Error
**errp
)
148 uint16List
*cpus
= NULL
;
149 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
151 if (node
->has_nodeid
) {
152 nodenr
= node
->nodeid
;
154 nodenr
= nb_numa_nodes
;
157 if (nodenr
>= MAX_NODES
) {
158 error_setg(errp
, "Max number of NUMA nodes reached: %"
163 if (numa_info
[nodenr
].present
) {
164 error_setg(errp
, "Duplicate NUMA nodeid: %" PRIu16
, nodenr
);
168 if (!mc
->cpu_index_to_instance_props
) {
169 error_report("NUMA is not supported by this machine-type");
172 for (cpus
= node
->cpus
; cpus
; cpus
= cpus
->next
) {
173 CpuInstanceProperties props
;
174 if (cpus
->value
>= max_cpus
) {
176 "CPU index (%" PRIu16
")"
177 " should be smaller than maxcpus (%d)",
178 cpus
->value
, max_cpus
);
181 props
= mc
->cpu_index_to_instance_props(ms
, cpus
->value
);
182 props
.node_id
= nodenr
;
183 props
.has_node_id
= true;
184 machine_set_cpu_numa_node(ms
, &props
, &error_fatal
);
187 if (node
->has_mem
&& node
->has_memdev
) {
188 error_setg(errp
, "cannot specify both mem= and memdev=");
192 if (have_memdevs
== -1) {
193 have_memdevs
= node
->has_memdev
;
195 if (node
->has_memdev
!= have_memdevs
) {
196 error_setg(errp
, "memdev option must be specified for either "
202 uint64_t mem_size
= node
->mem
;
203 const char *mem_str
= qemu_opt_get(opts
, "mem");
204 /* Fix up legacy suffix-less format */
205 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
208 numa_info
[nodenr
].node_mem
= mem_size
;
210 if (node
->has_memdev
) {
212 o
= object_resolve_path_type(node
->memdev
, TYPE_MEMORY_BACKEND
, NULL
);
214 error_setg(errp
, "memdev=%s is ambiguous", node
->memdev
);
219 numa_info
[nodenr
].node_mem
= object_property_get_int(o
, "size", NULL
);
220 numa_info
[nodenr
].node_memdev
= MEMORY_BACKEND(o
);
222 numa_info
[nodenr
].present
= true;
223 max_numa_nodeid
= MAX(max_numa_nodeid
, nodenr
+ 1);
226 static void parse_numa_distance(NumaDistOptions
*dist
, Error
**errp
)
228 uint16_t src
= dist
->src
;
229 uint16_t dst
= dist
->dst
;
230 uint8_t val
= dist
->val
;
232 if (src
>= MAX_NODES
|| dst
>= MAX_NODES
) {
234 "Invalid node %" PRIu16
235 ", max possible could be %" PRIu16
,
236 MAX(src
, dst
), MAX_NODES
);
240 if (!numa_info
[src
].present
|| !numa_info
[dst
].present
) {
241 error_setg(errp
, "Source/Destination NUMA node is missing. "
242 "Please use '-numa node' option to declare it first.");
246 if (val
< NUMA_DISTANCE_MIN
) {
247 error_setg(errp
, "NUMA distance (%" PRIu8
") is invalid, "
248 "it shouldn't be less than %d.",
249 val
, NUMA_DISTANCE_MIN
);
253 if (src
== dst
&& val
!= NUMA_DISTANCE_MIN
) {
254 error_setg(errp
, "Local distance of node %d should be %d.",
255 src
, NUMA_DISTANCE_MIN
);
259 numa_info
[src
].distance
[dst
] = val
;
260 have_numa_distance
= true;
263 static int parse_numa(void *opaque
, QemuOpts
*opts
, Error
**errp
)
265 NumaOptions
*object
= NULL
;
266 MachineState
*ms
= opaque
;
270 Visitor
*v
= opts_visitor_new(opts
);
271 visit_type_NumaOptions(v
, NULL
, &object
, &err
);
279 switch (object
->type
) {
280 case NUMA_OPTIONS_TYPE_NODE
:
281 parse_numa_node(ms
, &object
->u
.node
, opts
, &err
);
287 case NUMA_OPTIONS_TYPE_DIST
:
288 parse_numa_distance(&object
->u
.dist
, &err
);
293 case NUMA_OPTIONS_TYPE_CPU
:
294 if (!object
->u
.cpu
.has_node_id
) {
295 error_setg(&err
, "Missing mandatory node-id property");
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
);
305 machine_set_cpu_numa_node(ms
, qapi_NumaCpuOptions_base(&object
->u
.cpu
),
313 qapi_free_NumaOptions(object
);
315 error_report_err(err
);
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.
328 static void validate_numa_distance(void)
331 bool is_asymmetrical
= false;
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) {
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.",
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;
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.");
369 static void complete_init_numa_distance(void)
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.
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) {
382 numa_info
[src
].distance
[dst
] = NUMA_DISTANCE_MIN
;
384 numa_info
[src
].distance
[dst
] = numa_info
[dst
].distance
[src
];
391 void numa_legacy_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
392 int nb_nodes
, ram_addr_t size
)
395 uint64_t usedmem
= 0;
397 /* Align each node according to the alignment
398 * requirements of the machine class
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
;
406 nodes
[i
].node_mem
= size
- usedmem
;
409 void numa_default_auto_assign_ram(MachineClass
*mc
, NodeInfo
*nodes
,
410 int nb_nodes
, ram_addr_t size
)
413 uint64_t usedmem
= 0, node_mem
;
414 uint64_t granularity
= size
/ nb_nodes
;
415 uint64_t propagate
= 0;
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
;
424 nodes
[i
].node_mem
= size
- usedmem
;
427 void parse_numa_opts(MachineState
*ms
)
430 const CPUArchIdList
*possible_cpus
;
431 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
433 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa
, ms
, NULL
)) {
437 assert(max_numa_nodeid
<= MAX_NODES
);
439 /* No support for sparse NUMA node IDs yet: */
440 for (i
= max_numa_nodeid
- 1; i
>= 0; i
--) {
441 /* Report large node IDs first, to make mistakes easier to spot */
442 if (!numa_info
[i
].present
) {
443 error_report("numa: Node ID missing: %d", i
);
448 /* This must be always true if all nodes are present: */
449 assert(nb_numa_nodes
== max_numa_nodeid
);
451 if (nb_numa_nodes
> 0) {
454 if (nb_numa_nodes
> MAX_NODES
) {
455 nb_numa_nodes
= MAX_NODES
;
458 /* If no memory size is given for any node, assume the default case
459 * and distribute the available memory equally across all nodes
461 for (i
= 0; i
< nb_numa_nodes
; i
++) {
462 if (numa_info
[i
].node_mem
!= 0) {
466 if (i
== nb_numa_nodes
) {
467 assert(mc
->numa_auto_assign_ram
);
468 mc
->numa_auto_assign_ram(mc
, numa_info
, nb_numa_nodes
, ram_size
);
472 for (i
= 0; i
< nb_numa_nodes
; i
++) {
473 numa_total
+= numa_info
[i
].node_mem
;
475 if (numa_total
!= ram_size
) {
476 error_report("total memory for NUMA nodes (0x%" PRIx64
")"
477 " should equal RAM size (0x" RAM_ADDR_FMT
")",
478 numa_total
, ram_size
);
482 for (i
= 0; i
< nb_numa_nodes
; i
++) {
483 QLIST_INIT(&numa_info
[i
].addr
);
486 numa_set_mem_ranges();
488 /* assign CPUs to nodes using board provided default mapping */
489 if (!mc
->cpu_index_to_instance_props
|| !mc
->possible_cpu_arch_ids
) {
490 error_report("default CPUs to NUMA node mapping isn't supported");
494 possible_cpus
= mc
->possible_cpu_arch_ids(ms
);
495 for (i
= 0; i
< possible_cpus
->len
; i
++) {
496 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
501 /* no CPUs are assigned to NUMA nodes */
502 if (i
== possible_cpus
->len
) {
503 for (i
= 0; i
< max_cpus
; i
++) {
504 CpuInstanceProperties props
;
505 /* fetch default mapping from board and enable it */
506 props
= mc
->cpu_index_to_instance_props(ms
, i
);
507 props
.has_node_id
= true;
509 machine_set_cpu_numa_node(ms
, &props
, &error_fatal
);
513 /* QEMU needs at least all unique node pair distances to build
514 * the whole NUMA distance table. QEMU treats the distance table
515 * as symmetric by default, i.e. distance A->B == distance B->A.
516 * Thus, QEMU is able to complete the distance table
517 * initialization even though only distance A->B is provided and
518 * distance B->A is not. QEMU knows the distance of a node to
519 * itself is always 10, so A->A distances may be omitted. When
520 * the distances of two nodes of a pair differ, i.e. distance
521 * A->B != distance B->A, then that means the distance table is
522 * asymmetric. In this case, the distances for both directions
523 * of all node pairs are required.
525 if (have_numa_distance
) {
526 /* Validate enough NUMA distance information was provided. */
527 validate_numa_distance();
529 /* Validation succeeded, now fill in any missing distances. */
530 complete_init_numa_distance();
533 numa_set_mem_node_id(0, ram_size
, 0);
537 static void allocate_system_memory_nonnuma(MemoryRegion
*mr
, Object
*owner
,
544 memory_region_init_ram_from_file(mr
, owner
, name
, ram_size
, false,
547 error_report_err(err
);
552 /* Legacy behavior: if allocation failed, fall back to
553 * regular RAM allocation.
555 memory_region_init_ram(mr
, owner
, name
, ram_size
, &error_fatal
);
558 fprintf(stderr
, "-mem-path not supported on this host\n");
562 memory_region_init_ram(mr
, owner
, name
, ram_size
, &error_fatal
);
564 vmstate_register_ram_global(mr
);
567 void memory_region_allocate_system_memory(MemoryRegion
*mr
, Object
*owner
,
574 if (nb_numa_nodes
== 0 || !have_memdevs
) {
575 allocate_system_memory_nonnuma(mr
, owner
, name
, ram_size
);
579 memory_region_init(mr
, owner
, name
, ram_size
);
580 for (i
= 0; i
< MAX_NODES
; i
++) {
581 uint64_t size
= numa_info
[i
].node_mem
;
582 HostMemoryBackend
*backend
= numa_info
[i
].node_memdev
;
586 MemoryRegion
*seg
= host_memory_backend_get_memory(backend
,
589 if (memory_region_is_mapped(seg
)) {
590 char *path
= object_get_canonical_path_component(OBJECT(backend
));
591 error_report("memory backend %s is used multiple times. Each "
592 "-numa option must use a different memdev value.",
597 host_memory_backend_set_mapped(backend
, true);
598 memory_region_add_subregion(mr
, addr
, seg
);
599 vmstate_register_ram_global(seg
);
604 static void numa_stat_memory_devices(uint64_t node_mem
[])
606 MemoryDeviceInfoList
*info_list
= NULL
;
607 MemoryDeviceInfoList
**prev
= &info_list
;
608 MemoryDeviceInfoList
*info
;
610 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
611 for (info
= info_list
; info
; info
= info
->next
) {
612 MemoryDeviceInfo
*value
= info
->value
;
615 switch (value
->type
) {
616 case MEMORY_DEVICE_INFO_KIND_DIMM
:
617 node_mem
[value
->u
.dimm
.data
->node
] += value
->u
.dimm
.data
->size
;
624 qapi_free_MemoryDeviceInfoList(info_list
);
627 void query_numa_node_mem(uint64_t node_mem
[])
631 if (nb_numa_nodes
<= 0) {
635 numa_stat_memory_devices(node_mem
);
636 for (i
= 0; i
< nb_numa_nodes
; i
++) {
637 node_mem
[i
] += numa_info
[i
].node_mem
;
641 static int query_memdev(Object
*obj
, void *opaque
)
643 MemdevList
**list
= opaque
;
644 MemdevList
*m
= NULL
;
646 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
647 m
= g_malloc0(sizeof(*m
));
649 m
->value
= g_malloc0(sizeof(*m
->value
));
651 m
->value
->id
= object_property_get_str(obj
, "id", NULL
);
652 m
->value
->has_id
= !!m
->value
->id
;
654 m
->value
->size
= object_property_get_int(obj
, "size",
656 m
->value
->merge
= object_property_get_bool(obj
, "merge",
658 m
->value
->dump
= object_property_get_bool(obj
, "dump",
660 m
->value
->prealloc
= object_property_get_bool(obj
,
663 m
->value
->policy
= object_property_get_enum(obj
,
667 object_property_get_uint16List(obj
, "host-nodes",
668 &m
->value
->host_nodes
,
678 MemdevList
*qmp_query_memdev(Error
**errp
)
680 Object
*obj
= object_get_objects_root();
681 MemdevList
*list
= NULL
;
683 object_child_foreach(obj
, query_memdev
, &list
);
687 void ram_block_notifier_add(RAMBlockNotifier
*n
)
689 QLIST_INSERT_HEAD(&ram_list
.ramblock_notifiers
, n
, next
);
692 void ram_block_notifier_remove(RAMBlockNotifier
*n
)
694 QLIST_REMOVE(n
, next
);
697 void ram_block_notify_add(void *host
, size_t size
)
699 RAMBlockNotifier
*notifier
;
701 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
702 notifier
->ram_block_added(notifier
, host
, size
);
706 void ram_block_notify_remove(void *host
, size_t size
)
708 RAMBlockNotifier
*notifier
;
710 QLIST_FOREACH(notifier
, &ram_list
.ramblock_notifiers
, next
) {
711 notifier
->ram_block_removed(notifier
, host
, size
);