1 /* 2 * QEMU Host Memory Backend 3 * 4 * Copyright (C) 2013-2014 Red Hat Inc 5 * 6 * Authors: 7 * Igor Mammedov <imammedo@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 "sysemu/hostmem.h" 13 #include "hw/boards.h" 14 #include "qapi/visitor.h" 15 #include "qapi-types.h" 16 #include "qapi-visit.h" 17 #include "qemu/config-file.h" 18 #include "qom/object_interfaces.h" 19 20 #ifdef CONFIG_NUMA 21 #include <numaif.h> 22 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT); 23 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED); 24 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND); 25 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE); 26 #endif 27 28 static void 29 host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque, 30 const char *name, Error **errp) 31 { 32 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 33 uint64_t value = backend->size; 34 35 visit_type_size(v, &value, name, errp); 36 } 37 38 static void 39 host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque, 40 const char *name, Error **errp) 41 { 42 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 43 Error *local_err = NULL; 44 uint64_t value; 45 46 if (memory_region_size(&backend->mr)) { 47 error_setg(&local_err, "cannot change property value"); 48 goto out; 49 } 50 51 visit_type_size(v, &value, name, &local_err); 52 if (local_err) { 53 goto out; 54 } 55 if (!value) { 56 error_setg(&local_err, "Property '%s.%s' doesn't take value '%" 57 PRIu64 "'", object_get_typename(obj), name, value); 58 goto out; 59 } 60 backend->size = value; 61 out: 62 error_propagate(errp, local_err); 63 } 64 65 static void 66 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, void *opaque, 67 const char *name, Error **errp) 68 { 69 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 70 uint16List *host_nodes = NULL; 71 uint16List **node = &host_nodes; 72 unsigned long value; 73 74 value = find_first_bit(backend->host_nodes, MAX_NODES); 75 if (value == MAX_NODES) { 76 return; 77 } 78 79 *node = g_malloc0(sizeof(**node)); 80 (*node)->value = value; 81 node = &(*node)->next; 82 83 do { 84 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1); 85 if (value == MAX_NODES) { 86 break; 87 } 88 89 *node = g_malloc0(sizeof(**node)); 90 (*node)->value = value; 91 node = &(*node)->next; 92 } while (true); 93 94 visit_type_uint16List(v, &host_nodes, name, errp); 95 } 96 97 static void 98 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque, 99 const char *name, Error **errp) 100 { 101 #ifdef CONFIG_NUMA 102 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 103 uint16List *l = NULL; 104 105 visit_type_uint16List(v, &l, name, errp); 106 107 while (l) { 108 bitmap_set(backend->host_nodes, l->value, 1); 109 l = l->next; 110 } 111 #else 112 error_setg(errp, "NUMA node binding are not supported by this QEMU"); 113 #endif 114 } 115 116 static int 117 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED) 118 { 119 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 120 return backend->policy; 121 } 122 123 static void 124 host_memory_backend_set_policy(Object *obj, int policy, Error **errp) 125 { 126 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 127 backend->policy = policy; 128 129 #ifndef CONFIG_NUMA 130 if (policy != HOST_MEM_POLICY_DEFAULT) { 131 error_setg(errp, "NUMA policies are not supported by this QEMU"); 132 } 133 #endif 134 } 135 136 static bool host_memory_backend_get_merge(Object *obj, Error **errp) 137 { 138 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 139 140 return backend->merge; 141 } 142 143 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp) 144 { 145 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 146 147 if (!memory_region_size(&backend->mr)) { 148 backend->merge = value; 149 return; 150 } 151 152 if (value != backend->merge) { 153 void *ptr = memory_region_get_ram_ptr(&backend->mr); 154 uint64_t sz = memory_region_size(&backend->mr); 155 156 qemu_madvise(ptr, sz, 157 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE); 158 backend->merge = value; 159 } 160 } 161 162 static bool host_memory_backend_get_dump(Object *obj, Error **errp) 163 { 164 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 165 166 return backend->dump; 167 } 168 169 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp) 170 { 171 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 172 173 if (!memory_region_size(&backend->mr)) { 174 backend->dump = value; 175 return; 176 } 177 178 if (value != backend->dump) { 179 void *ptr = memory_region_get_ram_ptr(&backend->mr); 180 uint64_t sz = memory_region_size(&backend->mr); 181 182 qemu_madvise(ptr, sz, 183 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP); 184 backend->dump = value; 185 } 186 } 187 188 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp) 189 { 190 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 191 192 return backend->prealloc || backend->force_prealloc; 193 } 194 195 static void host_memory_backend_set_prealloc(Object *obj, bool value, 196 Error **errp) 197 { 198 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 199 200 if (backend->force_prealloc) { 201 if (value) { 202 error_setg(errp, 203 "remove -mem-prealloc to use the prealloc property"); 204 return; 205 } 206 } 207 208 if (!memory_region_size(&backend->mr)) { 209 backend->prealloc = value; 210 return; 211 } 212 213 if (value && !backend->prealloc) { 214 int fd = memory_region_get_fd(&backend->mr); 215 void *ptr = memory_region_get_ram_ptr(&backend->mr); 216 uint64_t sz = memory_region_size(&backend->mr); 217 218 os_mem_prealloc(fd, ptr, sz); 219 backend->prealloc = true; 220 } 221 } 222 223 static void host_memory_backend_init(Object *obj) 224 { 225 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 226 MachineState *machine = MACHINE(qdev_get_machine()); 227 228 backend->merge = machine_mem_merge(machine); 229 backend->dump = machine_dump_guest_core(machine); 230 backend->prealloc = mem_prealloc; 231 232 object_property_add_bool(obj, "merge", 233 host_memory_backend_get_merge, 234 host_memory_backend_set_merge, NULL); 235 object_property_add_bool(obj, "dump", 236 host_memory_backend_get_dump, 237 host_memory_backend_set_dump, NULL); 238 object_property_add_bool(obj, "prealloc", 239 host_memory_backend_get_prealloc, 240 host_memory_backend_set_prealloc, NULL); 241 object_property_add(obj, "size", "int", 242 host_memory_backend_get_size, 243 host_memory_backend_set_size, NULL, NULL, NULL); 244 object_property_add(obj, "host-nodes", "int", 245 host_memory_backend_get_host_nodes, 246 host_memory_backend_set_host_nodes, NULL, NULL, NULL); 247 object_property_add_enum(obj, "policy", "HostMemPolicy", 248 HostMemPolicy_lookup, 249 host_memory_backend_get_policy, 250 host_memory_backend_set_policy, NULL); 251 } 252 253 MemoryRegion * 254 host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp) 255 { 256 return memory_region_size(&backend->mr) ? &backend->mr : NULL; 257 } 258 259 static void 260 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) 261 { 262 HostMemoryBackend *backend = MEMORY_BACKEND(uc); 263 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc); 264 Error *local_err = NULL; 265 void *ptr; 266 uint64_t sz; 267 268 if (bc->alloc) { 269 bc->alloc(backend, &local_err); 270 if (local_err) { 271 error_propagate(errp, local_err); 272 return; 273 } 274 275 ptr = memory_region_get_ram_ptr(&backend->mr); 276 sz = memory_region_size(&backend->mr); 277 278 if (backend->merge) { 279 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE); 280 } 281 if (!backend->dump) { 282 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP); 283 } 284 #ifdef CONFIG_NUMA 285 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES); 286 /* lastbit == MAX_NODES means maxnode = 0 */ 287 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1); 288 /* ensure policy won't be ignored in case memory is preallocated 289 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so 290 * this doesn't catch hugepage case. */ 291 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE; 292 293 /* check for invalid host-nodes and policies and give more verbose 294 * error messages than mbind(). */ 295 if (maxnode && backend->policy == MPOL_DEFAULT) { 296 error_setg(errp, "host-nodes must be empty for policy default," 297 " or you should explicitly specify a policy other" 298 " than default"); 299 return; 300 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) { 301 error_setg(errp, "host-nodes must be set for policy %s", 302 HostMemPolicy_lookup[backend->policy]); 303 return; 304 } 305 306 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1 307 * as argument to mbind() due to an old Linux bug (feature?) which 308 * cuts off the last specified node. This means backend->host_nodes 309 * must have MAX_NODES+1 bits available. 310 */ 311 assert(sizeof(backend->host_nodes) >= 312 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long)); 313 assert(maxnode <= MAX_NODES); 314 if (mbind(ptr, sz, backend->policy, 315 maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) { 316 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) { 317 error_setg_errno(errp, errno, 318 "cannot bind memory to host NUMA nodes"); 319 return; 320 } 321 } 322 #endif 323 /* Preallocate memory after the NUMA policy has been instantiated. 324 * This is necessary to guarantee memory is allocated with 325 * specified NUMA policy in place. 326 */ 327 if (backend->prealloc) { 328 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz); 329 } 330 } 331 } 332 333 static bool 334 host_memory_backend_can_be_deleted(UserCreatable *uc, Error **errp) 335 { 336 MemoryRegion *mr; 337 338 mr = host_memory_backend_get_memory(MEMORY_BACKEND(uc), errp); 339 if (memory_region_is_mapped(mr)) { 340 return false; 341 } else { 342 return true; 343 } 344 } 345 346 static void 347 host_memory_backend_class_init(ObjectClass *oc, void *data) 348 { 349 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 350 351 ucc->complete = host_memory_backend_memory_complete; 352 ucc->can_be_deleted = host_memory_backend_can_be_deleted; 353 } 354 355 static const TypeInfo host_memory_backend_info = { 356 .name = TYPE_MEMORY_BACKEND, 357 .parent = TYPE_OBJECT, 358 .abstract = true, 359 .class_size = sizeof(HostMemoryBackendClass), 360 .class_init = host_memory_backend_class_init, 361 .instance_size = sizeof(HostMemoryBackend), 362 .instance_init = host_memory_backend_init, 363 .interfaces = (InterfaceInfo[]) { 364 { TYPE_USER_CREATABLE }, 365 { } 366 } 367 }; 368 369 static void register_types(void) 370 { 371 type_register_static(&host_memory_backend_info); 372 } 373 374 type_init(register_types); 375