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 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" 20 21 /* hostmem-file.c */ 22 /** 23 * @TYPE_MEMORY_BACKEND_FILE: 24 * name of backend that uses mmap on a file descriptor 25 */ 26 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" 27 28 #define MEMORY_BACKEND_FILE(obj) \ 29 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE) 30 31 typedef struct HostMemoryBackendFile HostMemoryBackendFile; 32 33 struct HostMemoryBackendFile { 34 HostMemoryBackend parent_obj; 35 36 char *mem_path; 37 uint64_t align; 38 bool discard_data; 39 bool is_pmem; 40 }; 41 42 static void 43 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 44 { 45 #ifndef CONFIG_POSIX 46 error_setg(errp, "backend '%s' not supported on this host", 47 object_get_typename(OBJECT(backend))); 48 #else 49 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); 50 gchar *name; 51 52 if (!backend->size) { 53 error_setg(errp, "can't create backend with size 0"); 54 return; 55 } 56 if (!fb->mem_path) { 57 error_setg(errp, "mem-path property not set"); 58 return; 59 } 60 61 /* 62 * Verify pmem file size since starting a guest with an incorrect size 63 * leads to confusing failures inside the guest. 64 */ 65 if (fb->is_pmem) { 66 Error *local_err = NULL; 67 uint64_t size; 68 69 size = qemu_get_pmem_size(fb->mem_path, &local_err); 70 if (local_err) { 71 error_propagate(errp, local_err); 72 return; 73 } 74 75 if (size && backend->size > size) { 76 error_setg(errp, "size property %" PRIu64 " is larger than " 77 "pmem file \"%s\" size %" PRIu64, backend->size, 78 fb->mem_path, size); 79 return; 80 } 81 } 82 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), 86 name, 87 backend->size, fb->align, 88 (backend->share ? RAM_SHARED : 0) | 89 (fb->is_pmem ? RAM_PMEM : 0), 90 fb->mem_path, errp); 91 g_free(name); 92 #endif 93 } 94 95 static char *get_mem_path(Object *o, Error **errp) 96 { 97 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 98 99 return g_strdup(fb->mem_path); 100 } 101 102 static void set_mem_path(Object *o, const char *str, Error **errp) 103 { 104 HostMemoryBackend *backend = MEMORY_BACKEND(o); 105 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 106 107 if (host_memory_backend_mr_inited(backend)) { 108 error_setg(errp, "cannot change property 'mem-path' of %s", 109 object_get_typename(o)); 110 return; 111 } 112 g_free(fb->mem_path); 113 fb->mem_path = g_strdup(str); 114 } 115 116 static bool file_memory_backend_get_discard_data(Object *o, Error **errp) 117 { 118 return MEMORY_BACKEND_FILE(o)->discard_data; 119 } 120 121 static void file_memory_backend_set_discard_data(Object *o, bool value, 122 Error **errp) 123 { 124 MEMORY_BACKEND_FILE(o)->discard_data = value; 125 } 126 127 static void file_memory_backend_get_align(Object *o, Visitor *v, 128 const char *name, void *opaque, 129 Error **errp) 130 { 131 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 132 uint64_t val = fb->align; 133 134 visit_type_size(v, name, &val, errp); 135 } 136 137 static void file_memory_backend_set_align(Object *o, Visitor *v, 138 const char *name, void *opaque, 139 Error **errp) 140 { 141 HostMemoryBackend *backend = MEMORY_BACKEND(o); 142 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 143 Error *local_err = NULL; 144 uint64_t val; 145 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)); 149 goto out; 150 } 151 152 visit_type_size(v, name, &val, &local_err); 153 if (local_err) { 154 goto out; 155 } 156 fb->align = val; 157 158 out: 159 error_propagate(errp, local_err); 160 } 161 162 static bool file_memory_backend_get_pmem(Object *o, Error **errp) 163 { 164 return MEMORY_BACKEND_FILE(o)->is_pmem; 165 } 166 167 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp) 168 { 169 HostMemoryBackend *backend = MEMORY_BACKEND(o); 170 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 171 172 if (host_memory_backend_mr_inited(backend)) { 173 174 error_setg(errp, "cannot change property 'pmem' of %s.", 175 object_get_typename(o)); 176 return; 177 } 178 179 #ifndef CONFIG_LIBPMEM 180 if (value) { 181 Error *local_err = NULL; 182 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); 188 return; 189 } 190 #endif 191 192 fb->is_pmem = value; 193 } 194 195 static void file_backend_unparent(Object *obj) 196 { 197 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 198 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); 199 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); 203 204 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE); 205 } 206 } 207 208 static void 209 file_backend_class_init(ObjectClass *oc, void *data) 210 { 211 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); 212 213 bc->alloc = file_backend_memory_alloc; 214 oc->unparent = file_backend_unparent; 215 216 object_class_property_add_bool(oc, "discard-data", 217 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data, 218 &error_abort); 219 object_class_property_add_str(oc, "mem-path", 220 get_mem_path, set_mem_path, 221 &error_abort); 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, 228 &error_abort); 229 } 230 231 static void file_backend_instance_finalize(Object *o) 232 { 233 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 234 235 g_free(fb->mem_path); 236 } 237 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), 244 }; 245 246 static void register_types(void) 247 { 248 type_register_static(&file_backend_info); 249 } 250 251 type_init(register_types); 252