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