blob: 46b15b916af630ee11ef0afdac302120c9b3ff9c [file] [log] [blame]
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +01001/*
2 * QEMU host memfd memory backend
3 *
4 * Copyright (C) 2018 Red Hat Inc
5 *
6 * Authors:
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
8 *
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.
11 */
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"
19
20#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd"
21
22#define MEMORY_BACKEND_MEMFD(obj) \
23 OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD)
24
25typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd;
26
27struct HostMemoryBackendMemfd {
28 HostMemoryBackend parent_obj;
29
30 bool hugetlb;
31 uint64_t hugetlbsize;
32 bool seal;
33};
34
35static void
36memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
37{
38 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend);
39 char *name;
40 int fd;
41
42 if (!backend->size) {
43 error_setg(errp, "can't create backend with size 0");
44 return;
45 }
46
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +010047 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;
54 }
55
Marc-André Lureaufa0cb342018-09-12 16:18:00 +040056 name = host_memory_backend_get_name(backend);
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +010057 memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
Marc-André Lureau36ea3972018-08-28 17:38:40 +020058 name, backend->size,
59 backend->share, fd, errp);
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +010060 g_free(name);
61}
62
63static bool
64memfd_backend_get_hugetlb(Object *o, Error **errp)
65{
66 return MEMORY_BACKEND_MEMFD(o)->hugetlb;
67}
68
69static void
70memfd_backend_set_hugetlb(Object *o, bool value, Error **errp)
71{
72 MEMORY_BACKEND_MEMFD(o)->hugetlb = value;
73}
74
75static void
76memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name,
77 void *opaque, Error **errp)
78{
79 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
80 Error *local_err = NULL;
81 uint64_t value;
82
83 if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) {
84 error_setg(&local_err, "cannot change property value");
85 goto out;
86 }
87
88 visit_type_size(v, name, &value, &local_err);
89 if (local_err) {
90 goto out;
91 }
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;
96 }
97 m->hugetlbsize = value;
98out:
99 error_propagate(errp, local_err);
100}
101
102static void
103memfd_backend_get_hugetlbsize(Object *obj, Visitor *v, const char *name,
104 void *opaque, Error **errp)
105{
106 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
107 uint64_t value = m->hugetlbsize;
108
109 visit_type_size(v, name, &value, errp);
110}
111
112static bool
113memfd_backend_get_seal(Object *o, Error **errp)
114{
115 return MEMORY_BACKEND_MEMFD(o)->seal;
116}
117
118static void
119memfd_backend_set_seal(Object *o, bool value, Error **errp)
120{
121 MEMORY_BACKEND_MEMFD(o)->seal = value;
122}
123
124static void
125memfd_backend_instance_init(Object *obj)
126{
127 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
128
129 /* default to sealed file */
130 m->seal = true;
Marc-André Lureau36ea3972018-08-28 17:38:40 +0200131 MEMORY_BACKEND(m)->share = true;
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +0100132}
133
134static void
135memfd_backend_class_init(ObjectClass *oc, void *data)
136{
137 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
138
139 bc->alloc = memfd_backend_memory_alloc;
140
Marc-André Lureau38296402018-09-06 20:14:15 +0400141 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);
Marc-André Lureau033bfc52018-09-06 18:39:08 +0400146 object_class_property_set_description(oc, "hugetlb",
147 "Use huge pages",
148 &error_abort);
Marc-André Lureau38296402018-09-06 20:14:15 +0400149 object_class_property_add(oc, "hugetlbsize", "int",
150 memfd_backend_get_hugetlbsize,
151 memfd_backend_set_hugetlbsize,
152 NULL, NULL, &error_abort);
Marc-André Lureau033bfc52018-09-06 18:39:08 +0400153 object_class_property_set_description(oc, "hugetlbsize",
154 "Huge pages size (ex: 2M, 1G)",
155 &error_abort);
Marc-André Lureau38296402018-09-06 20:14:15 +0400156 }
Ilya Maximets4a66c7a2019-03-11 16:58:47 +0300157 object_class_property_add_bool(oc, "seal",
158 memfd_backend_get_seal,
159 memfd_backend_set_seal,
160 &error_abort);
161 object_class_property_set_description(oc, "seal",
162 "Seal growing & shrinking",
163 &error_abort);
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +0100164}
165
166static const TypeInfo memfd_backend_info = {
167 .name = TYPE_MEMORY_BACKEND_MEMFD,
168 .parent = TYPE_MEMORY_BACKEND,
169 .instance_init = memfd_backend_instance_init,
170 .class_init = memfd_backend_class_init,
171 .instance_size = sizeof(HostMemoryBackendMemfd),
172};
173
174static void register_types(void)
175{
Ilya Maximets4a66c7a2019-03-11 16:58:47 +0300176 if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
Marc-André Lureau38296402018-09-06 20:14:15 +0400177 type_register_static(&memfd_backend_info);
178 }
Marc-André Lureaudbb9e0f2018-02-01 14:27:54 +0100179}
180
181type_init(register_types);