2 * QEMU Host Memory Backend
4 * Copyright (C) 2013-2014 Red Hat Inc
7 * Igor Mammedov <imammedo@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 "sysemu/hostmem.h"
15 #include "sysemu/sysemu.h"
16 #include "hw/boards.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-builtin-visit.h"
19 #include "qapi/visitor.h"
20 #include "qemu/config-file.h"
21 #include "qom/object_interfaces.h"
22 #include "qemu/mmap-alloc.h"
26 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
28 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
29 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
33 host_memory_backend_get_name(HostMemoryBackend *backend)
35 if (!backend->use_canonical_path) {
36 return object_get_canonical_path_component(OBJECT(backend));
39 return object_get_canonical_path(OBJECT(backend));
43 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
44 void *opaque, Error **errp)
46 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
47 uint64_t value = backend->size;
49 visit_type_size(v, name, &value, errp);
53 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
54 void *opaque, Error **errp)
56 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
57 Error *local_err = NULL;
60 if (host_memory_backend_mr_inited(backend)) {
61 error_setg(errp, "cannot change property %s of %s ", name,
62 object_get_typename(obj));
66 if (!visit_type_size(v, name, &value, &local_err)) {
67 error_propagate(errp, local_err);
72 "property '%s' of %s doesn't take value '%" PRIu64 "'",
73 name, object_get_typename(obj), value);
76 backend->size = value;
80 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
81 void *opaque, Error **errp)
83 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
84 uint16List *host_nodes = NULL;
85 uint16List **node = &host_nodes;
88 value = find_first_bit(backend->host_nodes, MAX_NODES);
89 if (value == MAX_NODES) {
93 *node = g_malloc0(sizeof(**node));
94 (*node)->value = value;
95 node = &(*node)->next;
98 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
99 if (value == MAX_NODES) {
103 *node = g_malloc0(sizeof(**node));
104 (*node)->value = value;
105 node = &(*node)->next;
109 visit_type_uint16List(v, name, &host_nodes, errp);
113 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
114 void *opaque, Error **errp)
117 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
118 uint16List *l, *host_nodes = NULL;
120 visit_type_uint16List(v, name, &host_nodes, errp);
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);
129 for (l = host_nodes; l; l = l->next) {
130 bitmap_set(backend->host_nodes, l->value, 1);
134 qapi_free_uint16List(host_nodes);
136 error_setg(errp, "NUMA node binding are not supported by this QEMU");
141 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
143 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
144 return backend->policy;
148 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
150 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
151 backend->policy = policy;
154 if (policy != HOST_MEM_POLICY_DEFAULT) {
155 error_setg(errp, "NUMA policies are not supported by this QEMU");
160 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
162 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
164 return backend->merge;
167 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
169 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
171 if (!host_memory_backend_mr_inited(backend)) {
172 backend->merge = value;
176 if (value != backend->merge) {
177 void *ptr = memory_region_get_ram_ptr(&backend->mr);
178 uint64_t sz = memory_region_size(&backend->mr);
180 qemu_madvise(ptr, sz,
181 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
182 backend->merge = value;
186 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
188 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
190 return backend->dump;
193 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
195 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
197 if (!host_memory_backend_mr_inited(backend)) {
198 backend->dump = value;
202 if (value != backend->dump) {
203 void *ptr = memory_region_get_ram_ptr(&backend->mr);
204 uint64_t sz = memory_region_size(&backend->mr);
206 qemu_madvise(ptr, sz,
207 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
208 backend->dump = value;
212 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
214 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
216 return backend->prealloc;
219 static void host_memory_backend_set_prealloc(Object *obj, bool value,
222 Error *local_err = NULL;
223 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
225 if (!host_memory_backend_mr_inited(backend)) {
226 backend->prealloc = value;
230 if (value && !backend->prealloc) {
231 int fd = memory_region_get_fd(&backend->mr);
232 void *ptr = memory_region_get_ram_ptr(&backend->mr);
233 uint64_t sz = memory_region_size(&backend->mr);
235 os_mem_prealloc(fd, ptr, sz, backend->prealloc_threads, &local_err);
237 error_propagate(errp, local_err);
240 backend->prealloc = true;
244 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v,
245 const char *name, void *opaque, Error **errp)
247 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
248 visit_type_uint32(v, name, &backend->prealloc_threads, errp);
251 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
252 const char *name, void *opaque, Error **errp)
254 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
255 Error *local_err = NULL;
258 if (!visit_type_uint32(v, name, &value, &local_err)) {
259 error_propagate(errp, local_err);
263 error_setg(errp, "property '%s' of %s doesn't take value '%d'", name,
264 object_get_typename(obj), value);
267 backend->prealloc_threads = value;
270 static void host_memory_backend_init(Object *obj)
272 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
273 MachineState *machine = MACHINE(qdev_get_machine());
275 /* TODO: convert access to globals to compat properties */
276 backend->merge = machine_mem_merge(machine);
277 backend->dump = machine_dump_guest_core(machine);
278 backend->prealloc_threads = 1;
281 static void host_memory_backend_post_init(Object *obj)
283 object_apply_compat_props(obj);
286 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
289 * NOTE: We forbid zero-length memory backend, so here zero means
290 * "we haven't inited the backend memory region yet".
292 return memory_region_size(&backend->mr) != 0;
295 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
297 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
300 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
302 backend->is_mapped = mapped;
305 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
307 return backend->is_mapped;
311 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
313 Object *obj = OBJECT(memdev);
314 char *path = object_property_get_str(obj, "mem-path", NULL);
315 size_t pagesize = qemu_mempath_getpagesize(path);
321 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
323 return qemu_real_host_page_size;
328 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
330 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
331 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
332 Error *local_err = NULL;
337 bc->alloc(backend, &local_err);
342 ptr = memory_region_get_ram_ptr(&backend->mr);
343 sz = memory_region_size(&backend->mr);
345 if (backend->merge) {
346 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
348 if (!backend->dump) {
349 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
352 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
353 /* lastbit == MAX_NODES means maxnode = 0 */
354 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
355 /* ensure policy won't be ignored in case memory is preallocated
356 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
357 * this doesn't catch hugepage case. */
358 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
360 /* check for invalid host-nodes and policies and give more verbose
361 * error messages than mbind(). */
362 if (maxnode && backend->policy == MPOL_DEFAULT) {
363 error_setg(errp, "host-nodes must be empty for policy default,"
364 " or you should explicitly specify a policy other"
367 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
368 error_setg(errp, "host-nodes must be set for policy %s",
369 HostMemPolicy_str(backend->policy));
373 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
374 * as argument to mbind() due to an old Linux bug (feature?) which
375 * cuts off the last specified node. This means backend->host_nodes
376 * must have MAX_NODES+1 bits available.
378 assert(sizeof(backend->host_nodes) >=
379 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
380 assert(maxnode <= MAX_NODES);
383 mbind(ptr, sz, backend->policy, backend->host_nodes, maxnode + 1,
385 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
386 error_setg_errno(errp, errno,
387 "cannot bind memory to host NUMA nodes");
392 /* Preallocate memory after the NUMA policy has been instantiated.
393 * This is necessary to guarantee memory is allocated with
394 * specified NUMA policy in place.
396 if (backend->prealloc) {
397 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
398 backend->prealloc_threads, &local_err);
405 error_propagate(errp, local_err);
409 host_memory_backend_can_be_deleted(UserCreatable *uc)
411 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
418 static bool host_memory_backend_get_share(Object *o, Error **errp)
420 HostMemoryBackend *backend = MEMORY_BACKEND(o);
422 return backend->share;
425 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
427 HostMemoryBackend *backend = MEMORY_BACKEND(o);
429 if (host_memory_backend_mr_inited(backend)) {
430 error_setg(errp, "cannot change property value");
433 backend->share = value;
437 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
439 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
441 return backend->use_canonical_path;
445 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
448 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
450 backend->use_canonical_path = value;
454 host_memory_backend_class_init(ObjectClass *oc, void *data)
456 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
458 ucc->complete = host_memory_backend_memory_complete;
459 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
461 object_class_property_add_bool(oc, "merge",
462 host_memory_backend_get_merge,
463 host_memory_backend_set_merge);
464 object_class_property_set_description(oc, "merge",
465 "Mark memory as mergeable");
466 object_class_property_add_bool(oc, "dump",
467 host_memory_backend_get_dump,
468 host_memory_backend_set_dump);
469 object_class_property_set_description(oc, "dump",
470 "Set to 'off' to exclude from core dump");
471 object_class_property_add_bool(oc, "prealloc",
472 host_memory_backend_get_prealloc,
473 host_memory_backend_set_prealloc);
474 object_class_property_set_description(oc, "prealloc",
475 "Preallocate memory");
476 object_class_property_add(oc, "prealloc-threads", "int",
477 host_memory_backend_get_prealloc_threads,
478 host_memory_backend_set_prealloc_threads,
480 object_class_property_set_description(oc, "prealloc-threads",
481 "Number of CPU threads to use for prealloc");
482 object_class_property_add(oc, "size", "int",
483 host_memory_backend_get_size,
484 host_memory_backend_set_size,
486 object_class_property_set_description(oc, "size",
487 "Size of the memory region (ex: 500M)");
488 object_class_property_add(oc, "host-nodes", "int",
489 host_memory_backend_get_host_nodes,
490 host_memory_backend_set_host_nodes,
492 object_class_property_set_description(oc, "host-nodes",
493 "Binds memory to the list of NUMA host nodes");
494 object_class_property_add_enum(oc, "policy", "HostMemPolicy",
495 &HostMemPolicy_lookup,
496 host_memory_backend_get_policy,
497 host_memory_backend_set_policy);
498 object_class_property_set_description(oc, "policy",
499 "Set the NUMA policy");
500 object_class_property_add_bool(oc, "share",
501 host_memory_backend_get_share, host_memory_backend_set_share);
502 object_class_property_set_description(oc, "share",
503 "Mark the memory as private to QEMU or shared");
504 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
505 host_memory_backend_get_use_canonical_path,
506 host_memory_backend_set_use_canonical_path);
509 static const TypeInfo host_memory_backend_info = {
510 .name = TYPE_MEMORY_BACKEND,
511 .parent = TYPE_OBJECT,
513 .class_size = sizeof(HostMemoryBackendClass),
514 .class_init = host_memory_backend_class_init,
515 .instance_size = sizeof(HostMemoryBackend),
516 .instance_init = host_memory_backend_init,
517 .instance_post_init = host_memory_backend_post_init,
518 .interfaces = (InterfaceInfo[]) {
519 { TYPE_USER_CREATABLE },
524 static void register_types(void)
526 type_register_static(&host_memory_backend_info);
529 type_init(register_types);