2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
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"
23 * @TYPE_MEMORY_BACKEND_FILE:
24 * name of backend that uses mmap on a file descriptor
26 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
28 #define MEMORY_BACKEND_FILE(obj) \
29 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
31 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
33 struct HostMemoryBackendFile
{
34 HostMemoryBackend parent_obj
;
43 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
46 error_setg(errp
, "backend '%s' not supported on this host",
47 object_get_typename(OBJECT(backend
)));
49 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
53 error_setg(errp
, "can't create backend with size 0");
57 error_setg(errp
, "mem-path property not set");
62 * Verify pmem file size since starting a guest with an incorrect size
63 * leads to confusing failures inside the guest.
66 Error
*local_err
= NULL
;
69 size
= qemu_get_pmem_size(fb
->mem_path
, &local_err
);
71 error_propagate(errp
, local_err
);
75 if (backend
->size
> size
) {
76 error_setg(errp
, "size property %" PRIu64
" is larger than "
77 "pmem file \"%s\" size %" PRIu64
, backend
->size
,
83 backend
->force_prealloc
= mem_prealloc
;
84 name
= host_memory_backend_get_name(backend
);
85 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
87 backend
->size
, fb
->align
,
88 (backend
->share
? RAM_SHARED
: 0) |
89 (fb
->is_pmem
? RAM_PMEM
: 0),
95 static char *get_mem_path(Object
*o
, Error
**errp
)
97 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
99 return g_strdup(fb
->mem_path
);
102 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
104 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
105 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
107 if (host_memory_backend_mr_inited(backend
)) {
108 error_setg(errp
, "cannot change property 'mem-path' of %s",
109 object_get_typename(o
));
112 g_free(fb
->mem_path
);
113 fb
->mem_path
= g_strdup(str
);
116 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
118 return MEMORY_BACKEND_FILE(o
)->discard_data
;
121 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
124 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
127 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
128 const char *name
, void *opaque
,
131 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
132 uint64_t val
= fb
->align
;
134 visit_type_size(v
, name
, &val
, errp
);
137 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
138 const char *name
, void *opaque
,
141 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
142 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
143 Error
*local_err
= NULL
;
146 if (host_memory_backend_mr_inited(backend
)) {
147 error_setg(&local_err
, "cannot change property '%s' of %s",
148 name
, object_get_typename(o
));
152 visit_type_size(v
, name
, &val
, &local_err
);
159 error_propagate(errp
, local_err
);
162 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
164 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
167 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
169 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
170 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
172 if (host_memory_backend_mr_inited(backend
)) {
174 error_setg(errp
, "cannot change property 'pmem' of %s.",
175 object_get_typename(o
));
179 #ifndef CONFIG_LIBPMEM
181 Error
*local_err
= NULL
;
183 error_setg(&local_err
,
184 "Lack of libpmem support while setting the 'pmem=on'"
185 " of %s. We can't ensure data persistence.",
186 object_get_typename(o
));
187 error_propagate(errp
, local_err
);
195 static void file_backend_unparent(Object
*obj
)
197 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
198 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
200 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
201 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
202 uint64_t sz
= memory_region_size(&backend
->mr
);
204 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
209 file_backend_class_init(ObjectClass
*oc
, void *data
)
211 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
213 bc
->alloc
= file_backend_memory_alloc
;
214 oc
->unparent
= file_backend_unparent
;
216 object_class_property_add_bool(oc
, "discard-data",
217 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
,
219 object_class_property_add_str(oc
, "mem-path",
220 get_mem_path
, set_mem_path
,
222 object_class_property_add(oc
, "align", "int",
223 file_memory_backend_get_align
,
224 file_memory_backend_set_align
,
225 NULL
, NULL
, &error_abort
);
226 object_class_property_add_bool(oc
, "pmem",
227 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
,
231 static void file_backend_instance_finalize(Object
*o
)
233 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
235 g_free(fb
->mem_path
);
238 static const TypeInfo file_backend_info
= {
239 .name
= TYPE_MEMORY_BACKEND_FILE
,
240 .parent
= TYPE_MEMORY_BACKEND
,
241 .class_init
= file_backend_class_init
,
242 .instance_finalize
= file_backend_instance_finalize
,
243 .instance_size
= sizeof(HostMemoryBackendFile
),
246 static void register_types(void)
248 type_register_static(&file_backend_info
);
251 type_init(register_types
);