xref: /openbmc/qemu/backends/hostmem-file.c (revision ae3c12a0)
1 /*
2  * QEMU Host Memory Backend for hugetlbfs
3  *
4  * Copyright (C) 2013-2014 Red Hat Inc
5  *
6  * Authors:
7  *   Paolo Bonzini <pbonzini@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 "qapi/error.h"
14 #include "qemu-common.h"
15 #include "qemu/error-report.h"
16 #include "sysemu/hostmem.h"
17 #include "sysemu/sysemu.h"
18 #include "qom/object_interfaces.h"
19 
20 /* hostmem-file.c */
21 /**
22  * @TYPE_MEMORY_BACKEND_FILE:
23  * name of backend that uses mmap on a file descriptor
24  */
25 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
26 
27 #define MEMORY_BACKEND_FILE(obj) \
28     OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
29 
30 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
31 
32 struct HostMemoryBackendFile {
33     HostMemoryBackend parent_obj;
34 
35     char *mem_path;
36     uint64_t align;
37     bool discard_data;
38     bool is_pmem;
39 };
40 
41 static void
42 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
43 {
44 #ifndef CONFIG_POSIX
45     error_setg(errp, "backend '%s' not supported on this host",
46                object_get_typename(OBJECT(backend)));
47 #else
48     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
49     gchar *name;
50 
51     if (!backend->size) {
52         error_setg(errp, "can't create backend with size 0");
53         return;
54     }
55     if (!fb->mem_path) {
56         error_setg(errp, "mem-path property not set");
57         return;
58     }
59 
60     /*
61      * Verify pmem file size since starting a guest with an incorrect size
62      * leads to confusing failures inside the guest.
63      */
64     if (fb->is_pmem) {
65         Error *local_err = NULL;
66         uint64_t size;
67 
68         size = qemu_get_pmem_size(fb->mem_path, &local_err);
69         if (!size) {
70             error_propagate(errp, local_err);
71             return;
72         }
73 
74         if (backend->size > size) {
75             error_setg(errp, "size property %" PRIu64 " is larger than "
76                        "pmem file \"%s\" size %" PRIu64, backend->size,
77                        fb->mem_path, size);
78             return;
79         }
80     }
81 
82     backend->force_prealloc = mem_prealloc;
83     name = host_memory_backend_get_name(backend);
84     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
85                                      name,
86                                      backend->size, fb->align,
87                                      (backend->share ? RAM_SHARED : 0) |
88                                      (fb->is_pmem ? RAM_PMEM : 0),
89                                      fb->mem_path, errp);
90     g_free(name);
91 #endif
92 }
93 
94 static char *get_mem_path(Object *o, Error **errp)
95 {
96     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
97 
98     return g_strdup(fb->mem_path);
99 }
100 
101 static void set_mem_path(Object *o, const char *str, Error **errp)
102 {
103     HostMemoryBackend *backend = MEMORY_BACKEND(o);
104     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
105 
106     if (host_memory_backend_mr_inited(backend)) {
107         error_setg(errp, "cannot change property 'mem-path' of %s",
108                    object_get_typename(o));
109         return;
110     }
111     g_free(fb->mem_path);
112     fb->mem_path = g_strdup(str);
113 }
114 
115 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
116 {
117     return MEMORY_BACKEND_FILE(o)->discard_data;
118 }
119 
120 static void file_memory_backend_set_discard_data(Object *o, bool value,
121                                                Error **errp)
122 {
123     MEMORY_BACKEND_FILE(o)->discard_data = value;
124 }
125 
126 static void file_memory_backend_get_align(Object *o, Visitor *v,
127                                           const char *name, void *opaque,
128                                           Error **errp)
129 {
130     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
131     uint64_t val = fb->align;
132 
133     visit_type_size(v, name, &val, errp);
134 }
135 
136 static void file_memory_backend_set_align(Object *o, Visitor *v,
137                                           const char *name, void *opaque,
138                                           Error **errp)
139 {
140     HostMemoryBackend *backend = MEMORY_BACKEND(o);
141     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
142     Error *local_err = NULL;
143     uint64_t val;
144 
145     if (host_memory_backend_mr_inited(backend)) {
146         error_setg(&local_err, "cannot change property '%s' of %s",
147                    name, object_get_typename(o));
148         goto out;
149     }
150 
151     visit_type_size(v, name, &val, &local_err);
152     if (local_err) {
153         goto out;
154     }
155     fb->align = val;
156 
157  out:
158     error_propagate(errp, local_err);
159 }
160 
161 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
162 {
163     return MEMORY_BACKEND_FILE(o)->is_pmem;
164 }
165 
166 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
167 {
168     HostMemoryBackend *backend = MEMORY_BACKEND(o);
169     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
170 
171     if (host_memory_backend_mr_inited(backend)) {
172 
173         error_setg(errp, "cannot change property 'pmem' of %s.",
174                    object_get_typename(o));
175         return;
176     }
177 
178 #ifndef CONFIG_LIBPMEM
179     if (value) {
180         Error *local_err = NULL;
181 
182         error_setg(&local_err,
183                    "Lack of libpmem support while setting the 'pmem=on'"
184                    " of %s. We can't ensure data persistence.",
185                    object_get_typename(o));
186         error_propagate(errp, local_err);
187         return;
188     }
189 #endif
190 
191     fb->is_pmem = value;
192 }
193 
194 static void file_backend_unparent(Object *obj)
195 {
196     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
197     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
198 
199     if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
200         void *ptr = memory_region_get_ram_ptr(&backend->mr);
201         uint64_t sz = memory_region_size(&backend->mr);
202 
203         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
204     }
205 }
206 
207 static void
208 file_backend_class_init(ObjectClass *oc, void *data)
209 {
210     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
211 
212     bc->alloc = file_backend_memory_alloc;
213     oc->unparent = file_backend_unparent;
214 
215     object_class_property_add_bool(oc, "discard-data",
216         file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
217         &error_abort);
218     object_class_property_add_str(oc, "mem-path",
219         get_mem_path, set_mem_path,
220         &error_abort);
221     object_class_property_add(oc, "align", "int",
222         file_memory_backend_get_align,
223         file_memory_backend_set_align,
224         NULL, NULL, &error_abort);
225     object_class_property_add_bool(oc, "pmem",
226         file_memory_backend_get_pmem, file_memory_backend_set_pmem,
227         &error_abort);
228 }
229 
230 static void file_backend_instance_finalize(Object *o)
231 {
232     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
233 
234     g_free(fb->mem_path);
235 }
236 
237 static const TypeInfo file_backend_info = {
238     .name = TYPE_MEMORY_BACKEND_FILE,
239     .parent = TYPE_MEMORY_BACKEND,
240     .class_init = file_backend_class_init,
241     .instance_finalize = file_backend_instance_finalize,
242     .instance_size = sizeof(HostMemoryBackendFile),
243 };
244 
245 static void register_types(void)
246 {
247     type_register_static(&file_backend_info);
248 }
249 
250 type_init(register_types);
251