memory: make cpu_physical_memory_sync_dirty_bitmap() fully atomic
[qemu.git] / numa.c
blobc975fb268232a72b1e9d74ba78ef00c67b4b8931
1 /*
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
22 * THE SOFTWARE.
25 #include "sysemu/numa.h"
26 #include "exec/cpu-common.h"
27 #include "qemu/bitmap.h"
28 #include "qom/cpu.h"
29 #include "qemu/error-report.h"
30 #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
31 #include "qapi-visit.h"
32 #include "qapi/opts-visitor.h"
33 #include "qapi/dealloc-visitor.h"
34 #include "qapi/qmp/qerror.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 = {
43 .name = "numa",
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
53 int nb_numa_nodes;
54 NodeInfo numa_info[MAX_NODES];
56 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
58 uint16_t nodenr;
59 uint16List *cpus = NULL;
61 if (node->has_nodeid) {
62 nodenr = node->nodeid;
63 } else {
64 nodenr = nb_numa_nodes;
67 if (nodenr >= MAX_NODES) {
68 error_setg(errp, "Max number of NUMA nodes reached: %"
69 PRIu16 "", nodenr);
70 return;
73 if (numa_info[nodenr].present) {
74 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
75 return;
78 for (cpus = node->cpus; cpus; cpus = cpus->next) {
79 if (cpus->value >= max_cpus) {
80 error_setg(errp,
81 "CPU index (%" PRIu16 ")"
82 " should be smaller than maxcpus (%d)",
83 cpus->value, max_cpus);
84 return;
86 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
89 if (node->has_mem && node->has_memdev) {
90 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
91 return;
94 if (have_memdevs == -1) {
95 have_memdevs = node->has_memdev;
97 if (node->has_memdev != have_memdevs) {
98 error_setg(errp, "qemu: memdev option must be specified for either "
99 "all or no nodes");
100 return;
103 if (node->has_mem) {
104 uint64_t mem_size = node->mem;
105 const char *mem_str = qemu_opt_get(opts, "mem");
106 /* Fix up legacy suffix-less format */
107 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
108 mem_size <<= 20;
110 numa_info[nodenr].node_mem = mem_size;
112 if (node->has_memdev) {
113 Object *o;
114 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
115 if (!o) {
116 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
117 return;
120 object_ref(o);
121 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
122 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
124 numa_info[nodenr].present = true;
125 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
128 static int parse_numa(QemuOpts *opts, void *opaque)
130 NumaOptions *object = NULL;
131 Error *err = NULL;
134 OptsVisitor *ov = opts_visitor_new(opts);
135 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
136 opts_visitor_cleanup(ov);
139 if (err) {
140 goto error;
143 switch (object->kind) {
144 case NUMA_OPTIONS_KIND_NODE:
145 numa_node_parse(object->node, opts, &err);
146 if (err) {
147 goto error;
149 nb_numa_nodes++;
150 break;
151 default:
152 abort();
155 return 0;
157 error:
158 error_report_err(err);
160 if (object) {
161 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
162 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
163 &object, NULL, NULL);
164 qapi_dealloc_visitor_cleanup(dv);
167 return -1;
170 static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
172 int cpu;
173 bool first = true;
174 GString *s = g_string_new(NULL);
176 for (cpu = find_first_bit(cpus, max_cpus);
177 cpu < max_cpus;
178 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
179 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
180 first = false;
182 return g_string_free(s, FALSE);
185 static void validate_numa_cpus(void)
187 int i;
188 DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
190 bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
191 for (i = 0; i < nb_numa_nodes; i++) {
192 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
193 MAX_CPUMASK_BITS)) {
194 bitmap_and(seen_cpus, seen_cpus,
195 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
196 error_report("CPU(s) present in multiple NUMA nodes: %s",
197 enumerate_cpus(seen_cpus, max_cpus));;
198 exit(EXIT_FAILURE);
200 bitmap_or(seen_cpus, seen_cpus,
201 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
204 if (!bitmap_full(seen_cpus, max_cpus)) {
205 char *msg;
206 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
207 msg = enumerate_cpus(seen_cpus, max_cpus);
208 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
209 error_report("warning: All CPU(s) up to maxcpus should be described "
210 "in NUMA config");
211 g_free(msg);
215 void parse_numa_opts(MachineClass *mc)
217 int i;
219 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa,
220 NULL, 1) != 0) {
221 exit(1);
224 assert(max_numa_nodeid <= MAX_NODES);
226 /* No support for sparse NUMA node IDs yet: */
227 for (i = max_numa_nodeid - 1; i >= 0; i--) {
228 /* Report large node IDs first, to make mistakes easier to spot */
229 if (!numa_info[i].present) {
230 error_report("numa: Node ID missing: %d", i);
231 exit(1);
235 /* This must be always true if all nodes are present: */
236 assert(nb_numa_nodes == max_numa_nodeid);
238 if (nb_numa_nodes > 0) {
239 uint64_t numa_total;
241 if (nb_numa_nodes > MAX_NODES) {
242 nb_numa_nodes = MAX_NODES;
245 /* If no memory size is given for any node, assume the default case
246 * and distribute the available memory equally across all nodes
248 for (i = 0; i < nb_numa_nodes; i++) {
249 if (numa_info[i].node_mem != 0) {
250 break;
253 if (i == nb_numa_nodes) {
254 uint64_t usedmem = 0;
256 /* On Linux, each node's border has to be 8MB aligned,
257 * the final node gets the rest.
259 for (i = 0; i < nb_numa_nodes - 1; i++) {
260 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
261 ~((1 << 23UL) - 1);
262 usedmem += numa_info[i].node_mem;
264 numa_info[i].node_mem = ram_size - usedmem;
267 numa_total = 0;
268 for (i = 0; i < nb_numa_nodes; i++) {
269 numa_total += numa_info[i].node_mem;
271 if (numa_total != ram_size) {
272 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
273 " should equal RAM size (0x" RAM_ADDR_FMT ")",
274 numa_total, ram_size);
275 exit(1);
278 for (i = 0; i < nb_numa_nodes; i++) {
279 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
280 break;
283 /* Historically VCPUs were assigned in round-robin order to NUMA
284 * nodes. However it causes issues with guest not handling it nice
285 * in case where cores/threads from a multicore CPU appear on
286 * different nodes. So allow boards to override default distribution
287 * rule grouping VCPUs by socket so that VCPUs from the same socket
288 * would be on the same node.
290 if (i == nb_numa_nodes) {
291 for (i = 0; i < max_cpus; i++) {
292 unsigned node_id = i % nb_numa_nodes;
293 if (mc->cpu_index_to_socket_id) {
294 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
297 set_bit(i, numa_info[node_id].node_cpu);
301 validate_numa_cpus();
305 void numa_post_machine_init(void)
307 CPUState *cpu;
308 int i;
310 CPU_FOREACH(cpu) {
311 for (i = 0; i < nb_numa_nodes; i++) {
312 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
313 cpu->numa_node = i;
319 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
320 const char *name,
321 uint64_t ram_size)
323 if (mem_path) {
324 #ifdef __linux__
325 Error *err = NULL;
326 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
327 mem_path, &err);
329 /* Legacy behavior: if allocation failed, fall back to
330 * regular RAM allocation.
332 if (err) {
333 error_report_err(err);
334 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
336 #else
337 fprintf(stderr, "-mem-path not supported on this host\n");
338 exit(1);
339 #endif
340 } else {
341 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
343 vmstate_register_ram_global(mr);
346 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
347 const char *name,
348 uint64_t ram_size)
350 uint64_t addr = 0;
351 int i;
353 if (nb_numa_nodes == 0 || !have_memdevs) {
354 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
355 return;
358 memory_region_init(mr, owner, name, ram_size);
359 for (i = 0; i < MAX_NODES; i++) {
360 Error *local_err = NULL;
361 uint64_t size = numa_info[i].node_mem;
362 HostMemoryBackend *backend = numa_info[i].node_memdev;
363 if (!backend) {
364 continue;
366 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
367 if (local_err) {
368 error_report_err(local_err);
369 exit(1);
372 if (memory_region_is_mapped(seg)) {
373 char *path = object_get_canonical_path_component(OBJECT(backend));
374 error_report("memory backend %s is used multiple times. Each "
375 "-numa option must use a different memdev value.",
376 path);
377 exit(1);
380 memory_region_add_subregion(mr, addr, seg);
381 vmstate_register_ram_global(seg);
382 addr += size;
386 static void numa_stat_memory_devices(uint64_t node_mem[])
388 MemoryDeviceInfoList *info_list = NULL;
389 MemoryDeviceInfoList **prev = &info_list;
390 MemoryDeviceInfoList *info;
392 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
393 for (info = info_list; info; info = info->next) {
394 MemoryDeviceInfo *value = info->value;
396 if (value) {
397 switch (value->kind) {
398 case MEMORY_DEVICE_INFO_KIND_DIMM:
399 node_mem[value->dimm->node] += value->dimm->size;
400 break;
401 default:
402 break;
406 qapi_free_MemoryDeviceInfoList(info_list);
409 void query_numa_node_mem(uint64_t node_mem[])
411 int i;
413 if (nb_numa_nodes <= 0) {
414 return;
417 numa_stat_memory_devices(node_mem);
418 for (i = 0; i < nb_numa_nodes; i++) {
419 node_mem[i] += numa_info[i].node_mem;
423 static int query_memdev(Object *obj, void *opaque)
425 MemdevList **list = opaque;
426 MemdevList *m = NULL;
427 Error *err = NULL;
429 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
430 m = g_malloc0(sizeof(*m));
432 m->value = g_malloc0(sizeof(*m->value));
434 m->value->size = object_property_get_int(obj, "size",
435 &err);
436 if (err) {
437 goto error;
440 m->value->merge = object_property_get_bool(obj, "merge",
441 &err);
442 if (err) {
443 goto error;
446 m->value->dump = object_property_get_bool(obj, "dump",
447 &err);
448 if (err) {
449 goto error;
452 m->value->prealloc = object_property_get_bool(obj,
453 "prealloc", &err);
454 if (err) {
455 goto error;
458 m->value->policy = object_property_get_enum(obj,
459 "policy",
460 HostMemPolicy_lookup,
461 &err);
462 if (err) {
463 goto error;
466 object_property_get_uint16List(obj, "host-nodes",
467 &m->value->host_nodes, &err);
468 if (err) {
469 goto error;
472 m->next = *list;
473 *list = m;
476 return 0;
477 error:
478 g_free(m->value);
479 g_free(m);
481 return -1;
484 MemdevList *qmp_query_memdev(Error **errp)
486 Object *obj;
487 MemdevList *list = NULL;
489 obj = object_resolve_path("/objects", NULL);
490 if (obj == NULL) {
491 return NULL;
494 if (object_child_foreach(obj, query_memdev, &list) != 0) {
495 goto error;
498 return list;
500 error:
501 qapi_free_MemdevList(list);
502 return NULL;