qapi: allow unions to contain further unions
[qemu/armbru.git] / backends / hostmem.c
blob747e7838c031c42aaab91c159cbaa9b96f7e5013
1 /*
2 * QEMU Host Memory Backend
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
7 * Igor Mammedov <imammedo@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "sysemu/hostmem.h"
15 #include "hw/boards.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-builtin-visit.h"
18 #include "qapi/visitor.h"
19 #include "qemu/config-file.h"
20 #include "qom/object_interfaces.h"
21 #include "qemu/mmap-alloc.h"
22 #include "qemu/madvise.h"
24 #ifdef CONFIG_NUMA
25 #include <numaif.h>
26 #include <numa.h>
27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
29 * HOST_MEM_POLICY_PREFERRED may either translate to MPOL_PREFERRED or
30 * MPOL_PREFERRED_MANY, see comments further below.
32 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
33 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
34 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
35 #endif
37 char *
38 host_memory_backend_get_name(HostMemoryBackend *backend)
40 if (!backend->use_canonical_path) {
41 return g_strdup(object_get_canonical_path_component(OBJECT(backend)));
44 return object_get_canonical_path(OBJECT(backend));
47 static void
48 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
49 void *opaque, Error **errp)
51 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
52 uint64_t value = backend->size;
54 visit_type_size(v, name, &value, errp);
57 static void
58 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
59 void *opaque, Error **errp)
61 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
62 uint64_t value;
64 if (host_memory_backend_mr_inited(backend)) {
65 error_setg(errp, "cannot change property %s of %s ", name,
66 object_get_typename(obj));
67 return;
70 if (!visit_type_size(v, name, &value, errp)) {
71 return;
73 if (!value) {
74 error_setg(errp,
75 "property '%s' of %s doesn't take value '%" PRIu64 "'",
76 name, object_get_typename(obj), value);
77 return;
79 backend->size = value;
82 static void
83 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
84 void *opaque, Error **errp)
86 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
87 uint16List *host_nodes = NULL;
88 uint16List **tail = &host_nodes;
89 unsigned long value;
91 value = find_first_bit(backend->host_nodes, MAX_NODES);
92 if (value == MAX_NODES) {
93 goto ret;
96 QAPI_LIST_APPEND(tail, value);
98 do {
99 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
100 if (value == MAX_NODES) {
101 break;
104 QAPI_LIST_APPEND(tail, value);
105 } while (true);
107 ret:
108 visit_type_uint16List(v, name, &host_nodes, errp);
109 qapi_free_uint16List(host_nodes);
112 static void
113 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
114 void *opaque, Error **errp)
116 #ifdef CONFIG_NUMA
117 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
118 uint16List *l, *host_nodes = NULL;
120 visit_type_uint16List(v, name, &host_nodes, errp);
122 for (l = host_nodes; l; l = l->next) {
123 if (l->value >= MAX_NODES) {
124 error_setg(errp, "Invalid host-nodes value: %d", l->value);
125 goto out;
129 for (l = host_nodes; l; l = l->next) {
130 bitmap_set(backend->host_nodes, l->value, 1);
133 out:
134 qapi_free_uint16List(host_nodes);
135 #else
136 error_setg(errp, "NUMA node binding are not supported by this QEMU");
137 #endif
140 static int
141 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
143 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
144 return backend->policy;
147 static void
148 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
150 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
151 backend->policy = policy;
153 #ifndef CONFIG_NUMA
154 if (policy != HOST_MEM_POLICY_DEFAULT) {
155 error_setg(errp, "NUMA policies are not supported by this QEMU");
157 #endif
160 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
162 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
164 return backend->merge;
167 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
169 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
171 if (!host_memory_backend_mr_inited(backend)) {
172 backend->merge = value;
173 return;
176 if (value != backend->merge) {
177 void *ptr = memory_region_get_ram_ptr(&backend->mr);
178 uint64_t sz = memory_region_size(&backend->mr);
180 qemu_madvise(ptr, sz,
181 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
182 backend->merge = value;
186 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
188 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
190 return backend->dump;
193 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
195 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
197 if (!host_memory_backend_mr_inited(backend)) {
198 backend->dump = value;
199 return;
202 if (value != backend->dump) {
203 void *ptr = memory_region_get_ram_ptr(&backend->mr);
204 uint64_t sz = memory_region_size(&backend->mr);
206 qemu_madvise(ptr, sz,
207 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
208 backend->dump = value;
212 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
214 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
216 return backend->prealloc;
219 static void host_memory_backend_set_prealloc(Object *obj, bool value,
220 Error **errp)
222 Error *local_err = NULL;
223 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
225 if (!backend->reserve && value) {
226 error_setg(errp, "'prealloc=on' and 'reserve=off' are incompatible");
227 return;
230 if (!host_memory_backend_mr_inited(backend)) {
231 backend->prealloc = value;
232 return;
235 if (value && !backend->prealloc) {
236 int fd = memory_region_get_fd(&backend->mr);
237 void *ptr = memory_region_get_ram_ptr(&backend->mr);
238 uint64_t sz = memory_region_size(&backend->mr);
240 qemu_prealloc_mem(fd, ptr, sz, backend->prealloc_threads,
241 backend->prealloc_context, &local_err);
242 if (local_err) {
243 error_propagate(errp, local_err);
244 return;
246 backend->prealloc = true;
250 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v,
251 const char *name, void *opaque, Error **errp)
253 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
254 visit_type_uint32(v, name, &backend->prealloc_threads, errp);
257 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
258 const char *name, void *opaque, Error **errp)
260 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
261 uint32_t value;
263 if (!visit_type_uint32(v, name, &value, errp)) {
264 return;
266 if (value <= 0) {
267 error_setg(errp, "property '%s' of %s doesn't take value '%d'", name,
268 object_get_typename(obj), value);
269 return;
271 backend->prealloc_threads = value;
274 static void host_memory_backend_init(Object *obj)
276 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
277 MachineState *machine = MACHINE(qdev_get_machine());
279 /* TODO: convert access to globals to compat properties */
280 backend->merge = machine_mem_merge(machine);
281 backend->dump = machine_dump_guest_core(machine);
282 backend->reserve = true;
283 backend->prealloc_threads = machine->smp.cpus;
286 static void host_memory_backend_post_init(Object *obj)
288 object_apply_compat_props(obj);
291 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
294 * NOTE: We forbid zero-length memory backend, so here zero means
295 * "we haven't inited the backend memory region yet".
297 return memory_region_size(&backend->mr) != 0;
300 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
302 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
305 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
307 backend->is_mapped = mapped;
310 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
312 return backend->is_mapped;
315 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
317 size_t pagesize = qemu_ram_pagesize(memdev->mr.ram_block);
318 g_assert(pagesize >= qemu_real_host_page_size());
319 return pagesize;
322 static void
323 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
325 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
326 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
327 Error *local_err = NULL;
328 void *ptr;
329 uint64_t sz;
331 if (bc->alloc) {
332 bc->alloc(backend, &local_err);
333 if (local_err) {
334 goto out;
337 ptr = memory_region_get_ram_ptr(&backend->mr);
338 sz = memory_region_size(&backend->mr);
340 if (backend->merge) {
341 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
343 if (!backend->dump) {
344 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
346 #ifdef CONFIG_NUMA
347 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
348 /* lastbit == MAX_NODES means maxnode = 0 */
349 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
350 /* ensure policy won't be ignored in case memory is preallocated
351 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
352 * this doesn't catch hugepage case. */
353 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
354 int mode = backend->policy;
356 /* check for invalid host-nodes and policies and give more verbose
357 * error messages than mbind(). */
358 if (maxnode && backend->policy == MPOL_DEFAULT) {
359 error_setg(errp, "host-nodes must be empty for policy default,"
360 " or you should explicitly specify a policy other"
361 " than default");
362 return;
363 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
364 error_setg(errp, "host-nodes must be set for policy %s",
365 HostMemPolicy_str(backend->policy));
366 return;
369 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
370 * as argument to mbind() due to an old Linux bug (feature?) which
371 * cuts off the last specified node. This means backend->host_nodes
372 * must have MAX_NODES+1 bits available.
374 assert(sizeof(backend->host_nodes) >=
375 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
376 assert(maxnode <= MAX_NODES);
378 #ifdef HAVE_NUMA_HAS_PREFERRED_MANY
379 if (mode == MPOL_PREFERRED && numa_has_preferred_many() > 0) {
381 * Replace with MPOL_PREFERRED_MANY otherwise the mbind() below
382 * silently picks the first node.
384 mode = MPOL_PREFERRED_MANY;
386 #endif
388 if (maxnode &&
389 mbind(ptr, sz, mode, backend->host_nodes, maxnode + 1, flags)) {
390 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
391 error_setg_errno(errp, errno,
392 "cannot bind memory to host NUMA nodes");
393 return;
396 #endif
397 /* Preallocate memory after the NUMA policy has been instantiated.
398 * This is necessary to guarantee memory is allocated with
399 * specified NUMA policy in place.
401 if (backend->prealloc) {
402 qemu_prealloc_mem(memory_region_get_fd(&backend->mr), ptr, sz,
403 backend->prealloc_threads,
404 backend->prealloc_context, &local_err);
405 if (local_err) {
406 goto out;
410 out:
411 error_propagate(errp, local_err);
414 static bool
415 host_memory_backend_can_be_deleted(UserCreatable *uc)
417 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
418 return false;
419 } else {
420 return true;
424 static bool host_memory_backend_get_share(Object *o, Error **errp)
426 HostMemoryBackend *backend = MEMORY_BACKEND(o);
428 return backend->share;
431 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
433 HostMemoryBackend *backend = MEMORY_BACKEND(o);
435 if (host_memory_backend_mr_inited(backend)) {
436 error_setg(errp, "cannot change property value");
437 return;
439 backend->share = value;
442 #ifdef CONFIG_LINUX
443 static bool host_memory_backend_get_reserve(Object *o, Error **errp)
445 HostMemoryBackend *backend = MEMORY_BACKEND(o);
447 return backend->reserve;
450 static void host_memory_backend_set_reserve(Object *o, bool value, Error **errp)
452 HostMemoryBackend *backend = MEMORY_BACKEND(o);
454 if (host_memory_backend_mr_inited(backend)) {
455 error_setg(errp, "cannot change property value");
456 return;
458 if (backend->prealloc && !value) {
459 error_setg(errp, "'prealloc=on' and 'reserve=off' are incompatible");
460 return;
462 backend->reserve = value;
464 #endif /* CONFIG_LINUX */
466 static bool
467 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
469 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
471 return backend->use_canonical_path;
474 static void
475 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
476 Error **errp)
478 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
480 backend->use_canonical_path = value;
483 static void
484 host_memory_backend_class_init(ObjectClass *oc, void *data)
486 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
488 ucc->complete = host_memory_backend_memory_complete;
489 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
491 object_class_property_add_bool(oc, "merge",
492 host_memory_backend_get_merge,
493 host_memory_backend_set_merge);
494 object_class_property_set_description(oc, "merge",
495 "Mark memory as mergeable");
496 object_class_property_add_bool(oc, "dump",
497 host_memory_backend_get_dump,
498 host_memory_backend_set_dump);
499 object_class_property_set_description(oc, "dump",
500 "Set to 'off' to exclude from core dump");
501 object_class_property_add_bool(oc, "prealloc",
502 host_memory_backend_get_prealloc,
503 host_memory_backend_set_prealloc);
504 object_class_property_set_description(oc, "prealloc",
505 "Preallocate memory");
506 object_class_property_add(oc, "prealloc-threads", "int",
507 host_memory_backend_get_prealloc_threads,
508 host_memory_backend_set_prealloc_threads,
509 NULL, NULL);
510 object_class_property_set_description(oc, "prealloc-threads",
511 "Number of CPU threads to use for prealloc");
512 object_class_property_add_link(oc, "prealloc-context",
513 TYPE_THREAD_CONTEXT, offsetof(HostMemoryBackend, prealloc_context),
514 object_property_allow_set_link, OBJ_PROP_LINK_STRONG);
515 object_class_property_set_description(oc, "prealloc-context",
516 "Context to use for creating CPU threads for preallocation");
517 object_class_property_add(oc, "size", "int",
518 host_memory_backend_get_size,
519 host_memory_backend_set_size,
520 NULL, NULL);
521 object_class_property_set_description(oc, "size",
522 "Size of the memory region (ex: 500M)");
523 object_class_property_add(oc, "host-nodes", "int",
524 host_memory_backend_get_host_nodes,
525 host_memory_backend_set_host_nodes,
526 NULL, NULL);
527 object_class_property_set_description(oc, "host-nodes",
528 "Binds memory to the list of NUMA host nodes");
529 object_class_property_add_enum(oc, "policy", "HostMemPolicy",
530 &HostMemPolicy_lookup,
531 host_memory_backend_get_policy,
532 host_memory_backend_set_policy);
533 object_class_property_set_description(oc, "policy",
534 "Set the NUMA policy");
535 object_class_property_add_bool(oc, "share",
536 host_memory_backend_get_share, host_memory_backend_set_share);
537 object_class_property_set_description(oc, "share",
538 "Mark the memory as private to QEMU or shared");
539 #ifdef CONFIG_LINUX
540 object_class_property_add_bool(oc, "reserve",
541 host_memory_backend_get_reserve, host_memory_backend_set_reserve);
542 object_class_property_set_description(oc, "reserve",
543 "Reserve swap space (or huge pages) if applicable");
544 #endif /* CONFIG_LINUX */
546 * Do not delete/rename option. This option must be considered stable
547 * (as if it didn't have the 'x-' prefix including deprecation period) as
548 * long as 4.0 and older machine types exists.
549 * Option will be used by upper layers to override (disable) canonical path
550 * for ramblock-id set by compat properties on old machine types ( <= 4.0),
551 * to keep migration working when backend is used for main RAM with
552 * -machine memory-backend= option (main RAM historically used prefix-less
553 * ramblock-id).
555 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
556 host_memory_backend_get_use_canonical_path,
557 host_memory_backend_set_use_canonical_path);
560 static const TypeInfo host_memory_backend_info = {
561 .name = TYPE_MEMORY_BACKEND,
562 .parent = TYPE_OBJECT,
563 .abstract = true,
564 .class_size = sizeof(HostMemoryBackendClass),
565 .class_init = host_memory_backend_class_init,
566 .instance_size = sizeof(HostMemoryBackend),
567 .instance_init = host_memory_backend_init,
568 .instance_post_init = host_memory_backend_post_init,
569 .interfaces = (InterfaceInfo[]) {
570 { TYPE_USER_CREATABLE },
575 static void register_types(void)
577 type_register_static(&host_memory_backend_info);
580 type_init(register_types);