MAINTAINERS: add missing support status fields
[qemu/kevin.git] / backends / hostmem-memfd.c
blob98c9bf324073939c85ed4ab0c7ae9e0e400c0e48
1 /*
2 * QEMU host memfd memory backend
4 * Copyright (C) 2018 Red Hat Inc
6 * Authors:
7 * Marc-André Lureau <marcandre.lureau@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.
12 #include "qemu/osdep.h"
13 #include "qemu-common.h"
14 #include "sysemu/hostmem.h"
15 #include "sysemu/sysemu.h"
16 #include "qom/object_interfaces.h"
17 #include "qemu/memfd.h"
18 #include "qapi/error.h"
20 #define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd"
22 #define MEMORY_BACKEND_MEMFD(obj) \
23 OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD)
25 typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd;
27 struct HostMemoryBackendMemfd {
28 HostMemoryBackend parent_obj;
30 bool hugetlb;
31 uint64_t hugetlbsize;
32 bool seal;
35 static void
36 memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
38 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend);
39 char *name;
40 int fd;
42 if (!backend->size) {
43 error_setg(errp, "can't create backend with size 0");
44 return;
47 backend->force_prealloc = mem_prealloc;
48 fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
49 m->hugetlb, m->hugetlbsize, m->seal ?
50 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
51 errp);
52 if (fd == -1) {
53 return;
56 name = host_memory_backend_get_name(backend);
57 memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
58 name, backend->size,
59 backend->share, fd, errp);
60 g_free(name);
63 static bool
64 memfd_backend_get_hugetlb(Object *o, Error **errp)
66 return MEMORY_BACKEND_MEMFD(o)->hugetlb;
69 static void
70 memfd_backend_set_hugetlb(Object *o, bool value, Error **errp)
72 MEMORY_BACKEND_MEMFD(o)->hugetlb = value;
75 static void
76 memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name,
77 void *opaque, Error **errp)
79 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
80 Error *local_err = NULL;
81 uint64_t value;
83 if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) {
84 error_setg(&local_err, "cannot change property value");
85 goto out;
88 visit_type_size(v, name, &value, &local_err);
89 if (local_err) {
90 goto out;
92 if (!value) {
93 error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
94 PRIu64 "'", object_get_typename(obj), name, value);
95 goto out;
97 m->hugetlbsize = value;
98 out:
99 error_propagate(errp, local_err);
102 static void
103 memfd_backend_get_hugetlbsize(Object *obj, Visitor *v, const char *name,
104 void *opaque, Error **errp)
106 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
107 uint64_t value = m->hugetlbsize;
109 visit_type_size(v, name, &value, errp);
112 static bool
113 memfd_backend_get_seal(Object *o, Error **errp)
115 return MEMORY_BACKEND_MEMFD(o)->seal;
118 static void
119 memfd_backend_set_seal(Object *o, bool value, Error **errp)
121 MEMORY_BACKEND_MEMFD(o)->seal = value;
124 static void
125 memfd_backend_instance_init(Object *obj)
127 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
129 /* default to sealed file */
130 m->seal = true;
131 MEMORY_BACKEND(m)->share = true;
134 static void
135 memfd_backend_class_init(ObjectClass *oc, void *data)
137 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
139 bc->alloc = memfd_backend_memory_alloc;
141 if (qemu_memfd_check(MFD_HUGETLB)) {
142 object_class_property_add_bool(oc, "hugetlb",
143 memfd_backend_get_hugetlb,
144 memfd_backend_set_hugetlb,
145 &error_abort);
146 object_class_property_set_description(oc, "hugetlb",
147 "Use huge pages",
148 &error_abort);
149 object_class_property_add(oc, "hugetlbsize", "int",
150 memfd_backend_get_hugetlbsize,
151 memfd_backend_set_hugetlbsize,
152 NULL, NULL, &error_abort);
153 object_class_property_set_description(oc, "hugetlbsize",
154 "Huge pages size (ex: 2M, 1G)",
155 &error_abort);
157 if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
158 object_class_property_add_bool(oc, "seal",
159 memfd_backend_get_seal,
160 memfd_backend_set_seal,
161 &error_abort);
162 object_class_property_set_description(oc, "seal",
163 "Seal growing & shrinking",
164 &error_abort);
168 static const TypeInfo memfd_backend_info = {
169 .name = TYPE_MEMORY_BACKEND_MEMFD,
170 .parent = TYPE_MEMORY_BACKEND,
171 .instance_init = memfd_backend_instance_init,
172 .class_init = memfd_backend_class_init,
173 .instance_size = sizeof(HostMemoryBackendMemfd),
176 static void register_types(void)
178 if (qemu_memfd_check(0)) {
179 type_register_static(&memfd_backend_info);
183 type_init(register_types);