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 #include "qemu/madvise.h" 23 24 #ifdef CONFIG_NUMA 25 #include <numaif.h> 26 #include <numa.h> 27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT); 28 /* 29 * HOST_MEM_POLICY_PREFERRED may either translate to MPOL_PREFERRED or 30 * MPOL_PREFERRED_MANY, see comments further below. 31 */ 32 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED); 33 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND); 34 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE); 35 #endif 36 37 char * 38 host_memory_backend_get_name(HostMemoryBackend *backend) 39 { 40 if (!backend->use_canonical_path) { 41 return g_strdup(object_get_canonical_path_component(OBJECT(backend))); 42 } 43 44 return object_get_canonical_path(OBJECT(backend)); 45 } 46 47 static void 48 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name, 49 void *opaque, Error **errp) 50 { 51 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 52 uint64_t value = backend->size; 53 54 visit_type_size(v, name, &value, errp); 55 } 56 57 static void 58 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name, 59 void *opaque, Error **errp) 60 { 61 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 62 uint64_t value; 63 64 if (host_memory_backend_mr_inited(backend)) { 65 error_setg(errp, "cannot change property %s of %s ", name, 66 object_get_typename(obj)); 67 return; 68 } 69 70 if (!visit_type_size(v, name, &value, errp)) { 71 return; 72 } 73 if (!value) { 74 error_setg(errp, 75 "property '%s' of %s doesn't take value '%" PRIu64 "'", 76 name, object_get_typename(obj), value); 77 return; 78 } 79 backend->size = value; 80 } 81 82 static void 83 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name, 84 void *opaque, Error **errp) 85 { 86 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 87 uint16List *host_nodes = NULL; 88 uint16List **tail = &host_nodes; 89 unsigned long value; 90 91 value = find_first_bit(backend->host_nodes, MAX_NODES); 92 if (value == MAX_NODES) { 93 goto ret; 94 } 95 96 QAPI_LIST_APPEND(tail, value); 97 98 do { 99 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1); 100 if (value == MAX_NODES) { 101 break; 102 } 103 104 QAPI_LIST_APPEND(tail, value); 105 } while (true); 106 107 ret: 108 visit_type_uint16List(v, name, &host_nodes, errp); 109 qapi_free_uint16List(host_nodes); 110 } 111 112 static void 113 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name, 114 void *opaque, Error **errp) 115 { 116 #ifdef CONFIG_NUMA 117 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 118 uint16List *l, *host_nodes = NULL; 119 120 visit_type_uint16List(v, name, &host_nodes, errp); 121 122 for (l = host_nodes; l; l = l->next) { 123 if (l->value >= MAX_NODES) { 124 error_setg(errp, "Invalid host-nodes value: %d", l->value); 125 goto out; 126 } 127 } 128 129 for (l = host_nodes; l; l = l->next) { 130 bitmap_set(backend->host_nodes, l->value, 1); 131 } 132 133 out: 134 qapi_free_uint16List(host_nodes); 135 #else 136 error_setg(errp, "NUMA node binding are not supported by this QEMU"); 137 #endif 138 } 139 140 static int 141 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED) 142 { 143 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 144 return backend->policy; 145 } 146 147 static void 148 host_memory_backend_set_policy(Object *obj, int policy, Error **errp) 149 { 150 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 151 backend->policy = policy; 152 153 #ifndef CONFIG_NUMA 154 if (policy != HOST_MEM_POLICY_DEFAULT) { 155 error_setg(errp, "NUMA policies are not supported by this QEMU"); 156 } 157 #endif 158 } 159 160 static bool host_memory_backend_get_merge(Object *obj, Error **errp) 161 { 162 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 163 164 return backend->merge; 165 } 166 167 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp) 168 { 169 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 170 171 if (!host_memory_backend_mr_inited(backend)) { 172 backend->merge = value; 173 return; 174 } 175 176 if (value != backend->merge) { 177 void *ptr = memory_region_get_ram_ptr(&backend->mr); 178 uint64_t sz = memory_region_size(&backend->mr); 179 180 qemu_madvise(ptr, sz, 181 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE); 182 backend->merge = value; 183 } 184 } 185 186 static bool host_memory_backend_get_dump(Object *obj, Error **errp) 187 { 188 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 189 190 return backend->dump; 191 } 192 193 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp) 194 { 195 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 196 197 if (!host_memory_backend_mr_inited(backend)) { 198 backend->dump = value; 199 return; 200 } 201 202 if (value != backend->dump) { 203 void *ptr = memory_region_get_ram_ptr(&backend->mr); 204 uint64_t sz = memory_region_size(&backend->mr); 205 206 qemu_madvise(ptr, sz, 207 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP); 208 backend->dump = value; 209 } 210 } 211 212 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp) 213 { 214 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 215 216 return backend->prealloc; 217 } 218 219 static void host_memory_backend_set_prealloc(Object *obj, bool value, 220 Error **errp) 221 { 222 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 223 224 if (!backend->reserve && value) { 225 error_setg(errp, "'prealloc=on' and 'reserve=off' are incompatible"); 226 return; 227 } 228 229 if (!host_memory_backend_mr_inited(backend)) { 230 backend->prealloc = value; 231 return; 232 } 233 234 if (value && !backend->prealloc) { 235 int fd = memory_region_get_fd(&backend->mr); 236 void *ptr = memory_region_get_ram_ptr(&backend->mr); 237 uint64_t sz = memory_region_size(&backend->mr); 238 239 if (!qemu_prealloc_mem(fd, ptr, sz, backend->prealloc_threads, 240 backend->prealloc_context, errp)) { 241 return; 242 } 243 backend->prealloc = true; 244 } 245 } 246 247 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v, 248 const char *name, void *opaque, Error **errp) 249 { 250 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 251 visit_type_uint32(v, name, &backend->prealloc_threads, errp); 252 } 253 254 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v, 255 const char *name, void *opaque, Error **errp) 256 { 257 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 258 uint32_t value; 259 260 if (!visit_type_uint32(v, name, &value, errp)) { 261 return; 262 } 263 if (value <= 0) { 264 error_setg(errp, "property '%s' of %s doesn't take value '%d'", name, 265 object_get_typename(obj), value); 266 return; 267 } 268 backend->prealloc_threads = value; 269 } 270 271 static void host_memory_backend_init(Object *obj) 272 { 273 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 274 MachineState *machine = MACHINE(qdev_get_machine()); 275 276 /* TODO: convert access to globals to compat properties */ 277 backend->merge = machine_mem_merge(machine); 278 backend->dump = machine_dump_guest_core(machine); 279 backend->reserve = true; 280 backend->prealloc_threads = machine->smp.cpus; 281 } 282 283 static void host_memory_backend_post_init(Object *obj) 284 { 285 object_apply_compat_props(obj); 286 } 287 288 bool host_memory_backend_mr_inited(HostMemoryBackend *backend) 289 { 290 /* 291 * NOTE: We forbid zero-length memory backend, so here zero means 292 * "we haven't inited the backend memory region yet". 293 */ 294 return memory_region_size(&backend->mr) != 0; 295 } 296 297 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend) 298 { 299 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL; 300 } 301 302 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped) 303 { 304 backend->is_mapped = mapped; 305 } 306 307 bool host_memory_backend_is_mapped(HostMemoryBackend *backend) 308 { 309 return backend->is_mapped; 310 } 311 312 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev) 313 { 314 size_t pagesize = qemu_ram_pagesize(memdev->mr.ram_block); 315 g_assert(pagesize >= qemu_real_host_page_size()); 316 return pagesize; 317 } 318 319 static void 320 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) 321 { 322 HostMemoryBackend *backend = MEMORY_BACKEND(uc); 323 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc); 324 void *ptr; 325 uint64_t sz; 326 327 if (!bc->alloc) { 328 return; 329 } 330 if (!bc->alloc(backend, errp)) { 331 return; 332 } 333 334 ptr = memory_region_get_ram_ptr(&backend->mr); 335 sz = memory_region_size(&backend->mr); 336 337 if (backend->merge) { 338 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE); 339 } 340 if (!backend->dump) { 341 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP); 342 } 343 #ifdef CONFIG_NUMA 344 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES); 345 /* lastbit == MAX_NODES means maxnode = 0 */ 346 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1); 347 /* 348 * Ensure policy won't be ignored in case memory is preallocated 349 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so 350 * this doesn't catch hugepage case. 351 */ 352 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE; 353 int mode = backend->policy; 354 355 /* check for invalid host-nodes and policies and give more verbose 356 * error messages than mbind(). */ 357 if (maxnode && backend->policy == MPOL_DEFAULT) { 358 error_setg(errp, "host-nodes must be empty for policy default," 359 " or you should explicitly specify a policy other" 360 " than default"); 361 return; 362 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) { 363 error_setg(errp, "host-nodes must be set for policy %s", 364 HostMemPolicy_str(backend->policy)); 365 return; 366 } 367 368 /* 369 * We can have up to MAX_NODES nodes, but we need to pass maxnode+1 370 * as argument to mbind() due to an old Linux bug (feature?) which 371 * cuts off the last specified node. This means backend->host_nodes 372 * must have MAX_NODES+1 bits available. 373 */ 374 assert(sizeof(backend->host_nodes) >= 375 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long)); 376 assert(maxnode <= MAX_NODES); 377 378 #ifdef HAVE_NUMA_HAS_PREFERRED_MANY 379 if (mode == MPOL_PREFERRED && numa_has_preferred_many() > 0) { 380 /* 381 * Replace with MPOL_PREFERRED_MANY otherwise the mbind() below 382 * silently picks the first node. 383 */ 384 mode = MPOL_PREFERRED_MANY; 385 } 386 #endif 387 388 if (maxnode && 389 mbind(ptr, sz, mode, backend->host_nodes, maxnode + 1, flags)) { 390 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) { 391 error_setg_errno(errp, errno, 392 "cannot bind memory to host NUMA nodes"); 393 return; 394 } 395 } 396 #endif 397 /* 398 * Preallocate memory after the NUMA policy has been instantiated. 399 * This is necessary to guarantee memory is allocated with 400 * specified NUMA policy in place. 401 */ 402 if (backend->prealloc && !qemu_prealloc_mem(memory_region_get_fd(&backend->mr), 403 ptr, sz, 404 backend->prealloc_threads, 405 backend->prealloc_context, errp)) { 406 return; 407 } 408 } 409 410 static bool 411 host_memory_backend_can_be_deleted(UserCreatable *uc) 412 { 413 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) { 414 return false; 415 } else { 416 return true; 417 } 418 } 419 420 static bool host_memory_backend_get_share(Object *o, Error **errp) 421 { 422 HostMemoryBackend *backend = MEMORY_BACKEND(o); 423 424 return backend->share; 425 } 426 427 static void host_memory_backend_set_share(Object *o, bool value, Error **errp) 428 { 429 HostMemoryBackend *backend = MEMORY_BACKEND(o); 430 431 if (host_memory_backend_mr_inited(backend)) { 432 error_setg(errp, "cannot change property value"); 433 return; 434 } 435 backend->share = value; 436 } 437 438 #ifdef CONFIG_LINUX 439 static bool host_memory_backend_get_reserve(Object *o, Error **errp) 440 { 441 HostMemoryBackend *backend = MEMORY_BACKEND(o); 442 443 return backend->reserve; 444 } 445 446 static void host_memory_backend_set_reserve(Object *o, bool value, Error **errp) 447 { 448 HostMemoryBackend *backend = MEMORY_BACKEND(o); 449 450 if (host_memory_backend_mr_inited(backend)) { 451 error_setg(errp, "cannot change property value"); 452 return; 453 } 454 if (backend->prealloc && !value) { 455 error_setg(errp, "'prealloc=on' and 'reserve=off' are incompatible"); 456 return; 457 } 458 backend->reserve = value; 459 } 460 #endif /* CONFIG_LINUX */ 461 462 static bool 463 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp) 464 { 465 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 466 467 return backend->use_canonical_path; 468 } 469 470 static void 471 host_memory_backend_set_use_canonical_path(Object *obj, bool value, 472 Error **errp) 473 { 474 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 475 476 backend->use_canonical_path = value; 477 } 478 479 static void 480 host_memory_backend_class_init(ObjectClass *oc, void *data) 481 { 482 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 483 484 ucc->complete = host_memory_backend_memory_complete; 485 ucc->can_be_deleted = host_memory_backend_can_be_deleted; 486 487 object_class_property_add_bool(oc, "merge", 488 host_memory_backend_get_merge, 489 host_memory_backend_set_merge); 490 object_class_property_set_description(oc, "merge", 491 "Mark memory as mergeable"); 492 object_class_property_add_bool(oc, "dump", 493 host_memory_backend_get_dump, 494 host_memory_backend_set_dump); 495 object_class_property_set_description(oc, "dump", 496 "Set to 'off' to exclude from core dump"); 497 object_class_property_add_bool(oc, "prealloc", 498 host_memory_backend_get_prealloc, 499 host_memory_backend_set_prealloc); 500 object_class_property_set_description(oc, "prealloc", 501 "Preallocate memory"); 502 object_class_property_add(oc, "prealloc-threads", "int", 503 host_memory_backend_get_prealloc_threads, 504 host_memory_backend_set_prealloc_threads, 505 NULL, NULL); 506 object_class_property_set_description(oc, "prealloc-threads", 507 "Number of CPU threads to use for prealloc"); 508 object_class_property_add_link(oc, "prealloc-context", 509 TYPE_THREAD_CONTEXT, offsetof(HostMemoryBackend, prealloc_context), 510 object_property_allow_set_link, OBJ_PROP_LINK_STRONG); 511 object_class_property_set_description(oc, "prealloc-context", 512 "Context to use for creating CPU threads for preallocation"); 513 object_class_property_add(oc, "size", "int", 514 host_memory_backend_get_size, 515 host_memory_backend_set_size, 516 NULL, NULL); 517 object_class_property_set_description(oc, "size", 518 "Size of the memory region (ex: 500M)"); 519 object_class_property_add(oc, "host-nodes", "int", 520 host_memory_backend_get_host_nodes, 521 host_memory_backend_set_host_nodes, 522 NULL, NULL); 523 object_class_property_set_description(oc, "host-nodes", 524 "Binds memory to the list of NUMA host nodes"); 525 object_class_property_add_enum(oc, "policy", "HostMemPolicy", 526 &HostMemPolicy_lookup, 527 host_memory_backend_get_policy, 528 host_memory_backend_set_policy); 529 object_class_property_set_description(oc, "policy", 530 "Set the NUMA policy"); 531 object_class_property_add_bool(oc, "share", 532 host_memory_backend_get_share, host_memory_backend_set_share); 533 object_class_property_set_description(oc, "share", 534 "Mark the memory as private to QEMU or shared"); 535 #ifdef CONFIG_LINUX 536 object_class_property_add_bool(oc, "reserve", 537 host_memory_backend_get_reserve, host_memory_backend_set_reserve); 538 object_class_property_set_description(oc, "reserve", 539 "Reserve swap space (or huge pages) if applicable"); 540 #endif /* CONFIG_LINUX */ 541 /* 542 * Do not delete/rename option. This option must be considered stable 543 * (as if it didn't have the 'x-' prefix including deprecation period) as 544 * long as 4.0 and older machine types exists. 545 * Option will be used by upper layers to override (disable) canonical path 546 * for ramblock-id set by compat properties on old machine types ( <= 4.0), 547 * to keep migration working when backend is used for main RAM with 548 * -machine memory-backend= option (main RAM historically used prefix-less 549 * ramblock-id). 550 */ 551 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id", 552 host_memory_backend_get_use_canonical_path, 553 host_memory_backend_set_use_canonical_path); 554 } 555 556 static const TypeInfo host_memory_backend_info = { 557 .name = TYPE_MEMORY_BACKEND, 558 .parent = TYPE_OBJECT, 559 .abstract = true, 560 .class_size = sizeof(HostMemoryBackendClass), 561 .class_init = host_memory_backend_class_init, 562 .instance_size = sizeof(HostMemoryBackend), 563 .instance_init = host_memory_backend_init, 564 .instance_post_init = host_memory_backend_post_init, 565 .interfaces = (InterfaceInfo[]) { 566 { TYPE_USER_CREATABLE }, 567 { } 568 } 569 }; 570 571 static void register_types(void) 572 { 573 type_register_static(&host_memory_backend_info); 574 } 575 576 type_init(register_types); 577