]> git.proxmox.com Git - mirror_qemu.git/blame - numa.c
slirp/smbd: modify/set several parameters in generated smbd.conf
[mirror_qemu.git] / numa.c
CommitLineData
96d0e26c
WG
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 "sysemu/sysemu.h"
26#include "exec/cpu-common.h"
27#include "qemu/bitmap.h"
28#include "qom/cpu.h"
2b631ec2
WG
29#include "qemu/error-report.h"
30#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
0042109a
WG
31#include "qapi-visit.h"
32#include "qapi/opts-visitor.h"
33#include "qapi/dealloc-visitor.h"
34#include "qapi/qmp/qerror.h"
dfabb8b9 35#include "hw/boards.h"
7febe36f 36#include "sysemu/hostmem.h"
76b5d850 37#include "qmp-commands.h"
0042109a
WG
38
39QemuOptsList qemu_numa_opts = {
40 .name = "numa",
41 .implied_opt_name = "type",
42 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
43 .desc = { { 0 } } /* validated with OptsVisitor */
44};
45
7febe36f
PB
46static int have_memdevs = -1;
47
0042109a 48static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
96d0e26c 49{
0042109a
WG
50 uint16_t nodenr;
51 uint16List *cpus = NULL;
96d0e26c 52
0042109a
WG
53 if (node->has_nodeid) {
54 nodenr = node->nodeid;
96d0e26c 55 } else {
0042109a 56 nodenr = nb_numa_nodes;
96d0e26c
WG
57 }
58
0042109a
WG
59 if (nodenr >= MAX_NODES) {
60 error_setg(errp, "Max number of NUMA nodes reached: %"
61 PRIu16 "\n", nodenr);
62 return;
96d0e26c
WG
63 }
64
1945b9d8
EH
65 if (numa_info[nodenr].present) {
66 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
67 return;
68 }
69
0042109a
WG
70 for (cpus = node->cpus; cpus; cpus = cpus->next) {
71 if (cpus->value > MAX_CPUMASK_BITS) {
72 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
73 cpus->value, MAX_CPUMASK_BITS);
74 return;
75 }
76 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
96d0e26c
WG
77 }
78
7febe36f
PB
79 if (node->has_mem && node->has_memdev) {
80 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
81 return;
82 }
83
84 if (have_memdevs == -1) {
85 have_memdevs = node->has_memdev;
86 }
87 if (node->has_memdev != have_memdevs) {
88 error_setg(errp, "qemu: memdev option must be specified for either "
89 "all or no nodes\n");
90 return;
91 }
92
0042109a
WG
93 if (node->has_mem) {
94 uint64_t mem_size = node->mem;
95 const char *mem_str = qemu_opt_get(opts, "mem");
96 /* Fix up legacy suffix-less format */
97 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
98 mem_size <<= 20;
99 }
100 numa_info[nodenr].node_mem = mem_size;
101 }
7febe36f
PB
102 if (node->has_memdev) {
103 Object *o;
104 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
105 if (!o) {
106 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
107 return;
108 }
109
110 object_ref(o);
111 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
112 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
113 }
1af878e0
EH
114 numa_info[nodenr].present = true;
115 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
96d0e26c
WG
116}
117
0042109a 118int numa_init_func(QemuOpts *opts, void *opaque)
96d0e26c 119{
0042109a
WG
120 NumaOptions *object = NULL;
121 Error *err = NULL;
96d0e26c 122
0042109a
WG
123 {
124 OptsVisitor *ov = opts_visitor_new(opts);
125 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
126 opts_visitor_cleanup(ov);
96d0e26c 127 }
96d0e26c 128
0042109a
WG
129 if (err) {
130 goto error;
131 }
96d0e26c 132
0042109a
WG
133 switch (object->kind) {
134 case NUMA_OPTIONS_KIND_NODE:
135 numa_node_parse(object->node, opts, &err);
136 if (err) {
137 goto error;
96d0e26c 138 }
0042109a
WG
139 nb_numa_nodes++;
140 break;
141 default:
142 abort();
143 }
96d0e26c 144
0042109a 145 return 0;
96d0e26c 146
0042109a
WG
147error:
148 qerror_report_err(err);
149 error_free(err);
150
151 if (object) {
152 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
153 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
154 &object, NULL, NULL);
155 qapi_dealloc_visitor_cleanup(dv);
96d0e26c 156 }
0042109a
WG
157
158 return -1;
96d0e26c
WG
159}
160
161void set_numa_nodes(void)
162{
12d6e464
EH
163 int i;
164
165 assert(max_numa_nodeid <= MAX_NODES);
166
167 /* No support for sparse NUMA node IDs yet: */
168 for (i = max_numa_nodeid - 1; i >= 0; i--) {
169 /* Report large node IDs first, to make mistakes easier to spot */
170 if (!numa_info[i].present) {
171 error_report("numa: Node ID missing: %d", i);
172 exit(1);
173 }
174 }
175
176 /* This must be always true if all nodes are present: */
177 assert(nb_numa_nodes == max_numa_nodeid);
178
96d0e26c 179 if (nb_numa_nodes > 0) {
2b631ec2 180 uint64_t numa_total;
96d0e26c
WG
181
182 if (nb_numa_nodes > MAX_NODES) {
183 nb_numa_nodes = MAX_NODES;
184 }
185
9851d0fe 186 /* If no memory size is given for any node, assume the default case
96d0e26c
WG
187 * and distribute the available memory equally across all nodes
188 */
189 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 190 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
191 break;
192 }
193 }
194 if (i == nb_numa_nodes) {
195 uint64_t usedmem = 0;
196
d75e2f68 197 /* On Linux, each node's border has to be 8MB aligned,
96d0e26c
WG
198 * the final node gets the rest.
199 */
200 for (i = 0; i < nb_numa_nodes - 1; i++) {
8c85901e
WG
201 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
202 ~((1 << 23UL) - 1);
203 usedmem += numa_info[i].node_mem;
96d0e26c 204 }
8c85901e 205 numa_info[i].node_mem = ram_size - usedmem;
96d0e26c
WG
206 }
207
2b631ec2
WG
208 numa_total = 0;
209 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 210 numa_total += numa_info[i].node_mem;
2b631ec2
WG
211 }
212 if (numa_total != ram_size) {
c68233ae
HT
213 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
214 " should equal RAM size (0x" RAM_ADDR_FMT ")",
2b631ec2
WG
215 numa_total, ram_size);
216 exit(1);
217 }
218
96d0e26c 219 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 220 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
96d0e26c
WG
221 break;
222 }
223 }
224 /* assigning the VCPUs round-robin is easier to implement, guest OSes
225 * must cope with this anyway, because there are BIOSes out there in
226 * real machines which also use this scheme.
227 */
228 if (i == nb_numa_nodes) {
229 for (i = 0; i < max_cpus; i++) {
8c85901e 230 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
96d0e26c
WG
231 }
232 }
233 }
234}
235
236void set_numa_modes(void)
237{
238 CPUState *cpu;
239 int i;
240
241 CPU_FOREACH(cpu) {
242 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 243 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
96d0e26c
WG
244 cpu->numa_node = i;
245 }
246 }
247 }
248}
dfabb8b9 249
7febe36f
PB
250static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
251 const char *name,
252 uint64_t ram_size)
253{
0b183fc8
PB
254 if (mem_path) {
255#ifdef __linux__
7f56e740 256 Error *err = NULL;
dbcb8981 257 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
7f56e740
PB
258 mem_path, &err);
259
260 /* Legacy behavior: if allocation failed, fall back to
261 * regular RAM allocation.
262 */
c3ba3095 263 if (err) {
7f56e740
PB
264 qerror_report_err(err);
265 error_free(err);
49946538 266 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
7f56e740 267 }
0b183fc8
PB
268#else
269 fprintf(stderr, "-mem-path not supported on this host\n");
270 exit(1);
271#endif
272 } else {
49946538 273 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
0b183fc8 274 }
7febe36f
PB
275 vmstate_register_ram_global(mr);
276}
277
dfabb8b9
PB
278void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
279 const char *name,
280 uint64_t ram_size)
281{
7febe36f
PB
282 uint64_t addr = 0;
283 int i;
284
285 if (nb_numa_nodes == 0 || !have_memdevs) {
286 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
287 return;
288 }
289
290 memory_region_init(mr, owner, name, ram_size);
291 for (i = 0; i < MAX_NODES; i++) {
292 Error *local_err = NULL;
293 uint64_t size = numa_info[i].node_mem;
294 HostMemoryBackend *backend = numa_info[i].node_memdev;
295 if (!backend) {
296 continue;
297 }
298 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
299 if (local_err) {
300 qerror_report_err(local_err);
301 exit(1);
302 }
303
0462faee
HT
304 if (memory_region_is_mapped(seg)) {
305 char *path = object_get_canonical_path_component(OBJECT(backend));
306 error_report("memory backend %s is used multiple times. Each "
307 "-numa option must use a different memdev value.",
308 path);
309 exit(1);
310 }
311
7febe36f
PB
312 memory_region_add_subregion(mr, addr, seg);
313 vmstate_register_ram_global(seg);
314 addr += size;
315 }
dfabb8b9 316}
76b5d850
HT
317
318static int query_memdev(Object *obj, void *opaque)
319{
320 MemdevList **list = opaque;
b0e90181 321 MemdevList *m = NULL;
76b5d850
HT
322 Error *err = NULL;
323
324 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
b0e90181 325 m = g_malloc0(sizeof(*m));
76b5d850
HT
326
327 m->value = g_malloc0(sizeof(*m->value));
328
329 m->value->size = object_property_get_int(obj, "size",
330 &err);
331 if (err) {
332 goto error;
333 }
334
335 m->value->merge = object_property_get_bool(obj, "merge",
336 &err);
337 if (err) {
338 goto error;
339 }
340
341 m->value->dump = object_property_get_bool(obj, "dump",
342 &err);
343 if (err) {
344 goto error;
345 }
346
347 m->value->prealloc = object_property_get_bool(obj,
348 "prealloc", &err);
349 if (err) {
350 goto error;
351 }
352
353 m->value->policy = object_property_get_enum(obj,
354 "policy",
355 HostMemPolicy_lookup,
356 &err);
357 if (err) {
358 goto error;
359 }
360
361 object_property_get_uint16List(obj, "host-nodes",
362 &m->value->host_nodes, &err);
363 if (err) {
364 goto error;
365 }
366
367 m->next = *list;
368 *list = m;
369 }
370
371 return 0;
372error:
b0e90181
CF
373 g_free(m->value);
374 g_free(m);
375
76b5d850
HT
376 return -1;
377}
378
379MemdevList *qmp_query_memdev(Error **errp)
380{
381 Object *obj;
ecaf54a0 382 MemdevList *list = NULL;
76b5d850
HT
383
384 obj = object_resolve_path("/objects", NULL);
385 if (obj == NULL) {
386 return NULL;
387 }
388
389 if (object_child_foreach(obj, query_memdev, &list) != 0) {
390 goto error;
391 }
392
393 return list;
394
395error:
ecaf54a0 396 qapi_free_MemdevList(list);
76b5d850
HT
397 return NULL;
398}