migration/rdma: Plug memory leaks in qemu_rdma_registration_stop()
[qemu/armbru.git] / backends / hostmem-file.c
blobcdabb412e699cf0a9cc6faa86adf8a4da51b96b2
1 /*
2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
7 * Paolo Bonzini <pbonzini@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 "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.h"
17 #include "sysemu/hostmem.h"
18 #include "sysemu/sysemu.h"
19 #include "qom/object_interfaces.h"
21 #define MEMORY_BACKEND_FILE(obj) \
22 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
24 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
26 struct HostMemoryBackendFile {
27 HostMemoryBackend parent_obj;
29 char *mem_path;
30 uint64_t align;
31 bool discard_data;
32 bool is_pmem;
35 static void
36 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
38 #ifndef CONFIG_POSIX
39 error_setg(errp, "backend '%s' not supported on this host",
40 object_get_typename(OBJECT(backend)));
41 #else
42 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
43 gchar *name;
45 if (!backend->size) {
46 error_setg(errp, "can't create backend with size 0");
47 return;
49 if (!fb->mem_path) {
50 error_setg(errp, "mem-path property not set");
51 return;
54 name = host_memory_backend_get_name(backend);
55 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
56 name,
57 backend->size, fb->align,
58 (backend->share ? RAM_SHARED : 0) |
59 (fb->is_pmem ? RAM_PMEM : 0),
60 fb->mem_path, errp);
61 g_free(name);
62 #endif
65 static char *get_mem_path(Object *o, Error **errp)
67 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
69 return g_strdup(fb->mem_path);
72 static void set_mem_path(Object *o, const char *str, Error **errp)
74 HostMemoryBackend *backend = MEMORY_BACKEND(o);
75 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
77 if (host_memory_backend_mr_inited(backend)) {
78 error_setg(errp, "cannot change property 'mem-path' of %s",
79 object_get_typename(o));
80 return;
82 g_free(fb->mem_path);
83 fb->mem_path = g_strdup(str);
86 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
88 return MEMORY_BACKEND_FILE(o)->discard_data;
91 static void file_memory_backend_set_discard_data(Object *o, bool value,
92 Error **errp)
94 MEMORY_BACKEND_FILE(o)->discard_data = value;
97 static void file_memory_backend_get_align(Object *o, Visitor *v,
98 const char *name, void *opaque,
99 Error **errp)
101 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
102 uint64_t val = fb->align;
104 visit_type_size(v, name, &val, errp);
107 static void file_memory_backend_set_align(Object *o, Visitor *v,
108 const char *name, void *opaque,
109 Error **errp)
111 HostMemoryBackend *backend = MEMORY_BACKEND(o);
112 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
113 Error *local_err = NULL;
114 uint64_t val;
116 if (host_memory_backend_mr_inited(backend)) {
117 error_setg(&local_err, "cannot change property '%s' of %s",
118 name, object_get_typename(o));
119 goto out;
122 visit_type_size(v, name, &val, &local_err);
123 if (local_err) {
124 goto out;
126 fb->align = val;
128 out:
129 error_propagate(errp, local_err);
132 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
134 return MEMORY_BACKEND_FILE(o)->is_pmem;
137 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
139 HostMemoryBackend *backend = MEMORY_BACKEND(o);
140 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
142 if (host_memory_backend_mr_inited(backend)) {
144 error_setg(errp, "cannot change property 'pmem' of %s.",
145 object_get_typename(o));
146 return;
149 #ifndef CONFIG_LIBPMEM
150 if (value) {
151 Error *local_err = NULL;
153 error_setg(&local_err,
154 "Lack of libpmem support while setting the 'pmem=on'"
155 " of %s. We can't ensure data persistence.",
156 object_get_typename(o));
157 error_propagate(errp, local_err);
158 return;
160 #endif
162 fb->is_pmem = value;
165 static void file_backend_unparent(Object *obj)
167 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
168 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
170 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
171 void *ptr = memory_region_get_ram_ptr(&backend->mr);
172 uint64_t sz = memory_region_size(&backend->mr);
174 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
178 static void
179 file_backend_class_init(ObjectClass *oc, void *data)
181 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
183 bc->alloc = file_backend_memory_alloc;
184 oc->unparent = file_backend_unparent;
186 object_class_property_add_bool(oc, "discard-data",
187 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
188 object_class_property_add_str(oc, "mem-path",
189 get_mem_path, set_mem_path);
190 object_class_property_add(oc, "align", "int",
191 file_memory_backend_get_align,
192 file_memory_backend_set_align,
193 NULL, NULL);
194 object_class_property_add_bool(oc, "pmem",
195 file_memory_backend_get_pmem, file_memory_backend_set_pmem);
198 static void file_backend_instance_finalize(Object *o)
200 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
202 g_free(fb->mem_path);
205 static const TypeInfo file_backend_info = {
206 .name = TYPE_MEMORY_BACKEND_FILE,
207 .parent = TYPE_MEMORY_BACKEND,
208 .class_init = file_backend_class_init,
209 .instance_finalize = file_backend_instance_finalize,
210 .instance_size = sizeof(HostMemoryBackendFile),
213 static void register_types(void)
215 type_register_static(&file_backend_info);
218 type_init(register_types);