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(&local_err, "cannot change property %s of %s ",
62 name, object_get_typename(obj));
66 visit_type_size(v, name, &value, &local_err);
71 error_setg(&local_err,
72 "property '%s' of %s doesn't take value '%" PRIu64 "'",
73 name, object_get_typename(obj), value);
76 backend->size = value;
78 error_propagate(errp, local_err);
82 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
83 void *opaque, Error **errp)
85 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
86 uint16List *host_nodes = NULL;
87 uint16List **node = &host_nodes;
90 value = find_first_bit(backend->host_nodes, MAX_NODES);
91 if (value == MAX_NODES) {
95 *node = g_malloc0(sizeof(**node));
96 (*node)->value = value;
97 node = &(*node)->next;
100 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
101 if (value == MAX_NODES) {
105 *node = g_malloc0(sizeof(**node));
106 (*node)->value = value;
107 node = &(*node)->next;
111 visit_type_uint16List(v, name, &host_nodes, errp);
115 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
116 void *opaque, Error **errp)
119 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
120 uint16List *l, *host_nodes = NULL;
122 visit_type_uint16List(v, name, &host_nodes, errp);
124 for (l = host_nodes; l; l = l->next) {
125 if (l->value >= MAX_NODES) {
126 error_setg(errp, "Invalid host-nodes value: %d", l->value);
131 for (l = host_nodes; l; l = l->next) {
132 bitmap_set(backend->host_nodes, l->value, 1);
136 qapi_free_uint16List(host_nodes);
138 error_setg(errp, "NUMA node binding are not supported by this QEMU");
143 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
145 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
146 return backend->policy;
150 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
152 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
153 backend->policy = policy;
156 if (policy != HOST_MEM_POLICY_DEFAULT) {
157 error_setg(errp, "NUMA policies are not supported by this QEMU");
162 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
164 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
166 return backend->merge;
169 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
171 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
173 if (!host_memory_backend_mr_inited(backend)) {
174 backend->merge = value;
178 if (value != backend->merge) {
179 void *ptr = memory_region_get_ram_ptr(&backend->mr);
180 uint64_t sz = memory_region_size(&backend->mr);
182 qemu_madvise(ptr, sz,
183 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
184 backend->merge = value;
188 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
190 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
192 return backend->dump;
195 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
197 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
199 if (!host_memory_backend_mr_inited(backend)) {
200 backend->dump = value;
204 if (value != backend->dump) {
205 void *ptr = memory_region_get_ram_ptr(&backend->mr);
206 uint64_t sz = memory_region_size(&backend->mr);
208 qemu_madvise(ptr, sz,
209 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
210 backend->dump = value;
214 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
216 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
218 return backend->prealloc;
221 static void host_memory_backend_set_prealloc(Object *obj, bool value,
224 Error *local_err = NULL;
225 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
227 if (!host_memory_backend_mr_inited(backend)) {
228 backend->prealloc = value;
232 if (value && !backend->prealloc) {
233 int fd = memory_region_get_fd(&backend->mr);
234 void *ptr = memory_region_get_ram_ptr(&backend->mr);
235 uint64_t sz = memory_region_size(&backend->mr);
237 os_mem_prealloc(fd, ptr, sz, backend->prealloc_threads, &local_err);
239 error_propagate(errp, local_err);
242 backend->prealloc = true;
246 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v,
247 const char *name, void *opaque, Error **errp)
249 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
250 visit_type_uint32(v, name, &backend->prealloc_threads, errp);
253 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
254 const char *name, void *opaque, Error **errp)
256 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
257 Error *local_err = NULL;
260 visit_type_uint32(v, name, &value, &local_err);
265 error_setg(&local_err,
266 "property '%s' of %s doesn't take value '%d'",
267 name, object_get_typename(obj), value);
270 backend->prealloc_threads = value;
272 error_propagate(errp, local_err);
275 static void host_memory_backend_init(Object *obj)
277 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
278 MachineState *machine = MACHINE(qdev_get_machine());
280 /* TODO: convert access to globals to compat properties */
281 backend->merge = machine_mem_merge(machine);
282 backend->dump = machine_dump_guest_core(machine);
285 static void host_memory_backend_post_init(Object *obj)
287 object_apply_compat_props(obj);
290 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
293 * NOTE: We forbid zero-length memory backend, so here zero means
294 * "we haven't inited the backend memory region yet".
296 return memory_region_size(&backend->mr) != 0;
299 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
301 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
304 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
306 backend->is_mapped = mapped;
309 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
311 return backend->is_mapped;
315 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
317 Object *obj = OBJECT(memdev);
318 char *path = object_property_get_str(obj, "mem-path", NULL);
319 size_t pagesize = qemu_mempath_getpagesize(path);
325 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
327 return qemu_real_host_page_size;
332 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
334 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
335 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
336 Error *local_err = NULL;
341 bc->alloc(backend, &local_err);
346 ptr = memory_region_get_ram_ptr(&backend->mr);
347 sz = memory_region_size(&backend->mr);
349 if (backend->merge) {
350 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
352 if (!backend->dump) {
353 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
356 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
357 /* lastbit == MAX_NODES means maxnode = 0 */
358 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
359 /* ensure policy won't be ignored in case memory is preallocated
360 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
361 * this doesn't catch hugepage case. */
362 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
364 /* check for invalid host-nodes and policies and give more verbose
365 * error messages than mbind(). */
366 if (maxnode && backend->policy == MPOL_DEFAULT) {
367 error_setg(errp, "host-nodes must be empty for policy default,"
368 " or you should explicitly specify a policy other"
371 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
372 error_setg(errp, "host-nodes must be set for policy %s",
373 HostMemPolicy_str(backend->policy));
377 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
378 * as argument to mbind() due to an old Linux bug (feature?) which
379 * cuts off the last specified node. This means backend->host_nodes
380 * must have MAX_NODES+1 bits available.
382 assert(sizeof(backend->host_nodes) >=
383 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
384 assert(maxnode <= MAX_NODES);
385 if (mbind(ptr, sz, backend->policy,
386 maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
387 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
388 error_setg_errno(errp, errno,
389 "cannot bind memory to host NUMA nodes");
394 /* Preallocate memory after the NUMA policy has been instantiated.
395 * This is necessary to guarantee memory is allocated with
396 * specified NUMA policy in place.
398 if (backend->prealloc) {
399 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
400 backend->prealloc_threads, &local_err);
407 error_propagate(errp, local_err);
411 host_memory_backend_can_be_deleted(UserCreatable *uc)
413 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
420 static bool host_memory_backend_get_share(Object *o, Error **errp)
422 HostMemoryBackend *backend = MEMORY_BACKEND(o);
424 return backend->share;
427 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
429 HostMemoryBackend *backend = MEMORY_BACKEND(o);
431 if (host_memory_backend_mr_inited(backend)) {
432 error_setg(errp, "cannot change property value");
435 backend->share = value;
439 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
441 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
443 return backend->use_canonical_path;
447 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
450 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
452 backend->use_canonical_path = value;
456 host_memory_backend_class_init(ObjectClass *oc, void *data)
458 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
460 ucc->complete = host_memory_backend_memory_complete;
461 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
463 object_class_property_add_bool(oc, "merge",
464 host_memory_backend_get_merge,
465 host_memory_backend_set_merge, &error_abort);
466 object_class_property_set_description(oc, "merge",
467 "Mark memory as mergeable", &error_abort);
468 object_class_property_add_bool(oc, "dump",
469 host_memory_backend_get_dump,
470 host_memory_backend_set_dump, &error_abort);
471 object_class_property_set_description(oc, "dump",
472 "Set to 'off' to exclude from core dump", &error_abort);
473 object_class_property_add_bool(oc, "prealloc",
474 host_memory_backend_get_prealloc,
475 host_memory_backend_set_prealloc, &error_abort);
476 object_class_property_set_description(oc, "prealloc",
477 "Preallocate memory", &error_abort);
478 object_class_property_add(oc, "prealloc-threads", "int",
479 host_memory_backend_get_prealloc_threads,
480 host_memory_backend_set_prealloc_threads,
481 NULL, NULL, &error_abort);
482 object_class_property_set_description(oc, "prealloc-threads",
483 "Number of CPU threads to use for prealloc", &error_abort);
484 object_class_property_add(oc, "size", "int",
485 host_memory_backend_get_size,
486 host_memory_backend_set_size,
487 NULL, NULL, &error_abort);
488 object_class_property_set_description(oc, "size",
489 "Size of the memory region (ex: 500M)", &error_abort);
490 object_class_property_add(oc, "host-nodes", "int",
491 host_memory_backend_get_host_nodes,
492 host_memory_backend_set_host_nodes,
493 NULL, NULL, &error_abort);
494 object_class_property_set_description(oc, "host-nodes",
495 "Binds memory to the list of NUMA host nodes", &error_abort);
496 object_class_property_add_enum(oc, "policy", "HostMemPolicy",
497 &HostMemPolicy_lookup,
498 host_memory_backend_get_policy,
499 host_memory_backend_set_policy, &error_abort);
500 object_class_property_set_description(oc, "policy",
501 "Set the NUMA policy", &error_abort);
502 object_class_property_add_bool(oc, "share",
503 host_memory_backend_get_share, host_memory_backend_set_share,
505 object_class_property_set_description(oc, "share",
506 "Mark the memory as private to QEMU or shared", &error_abort);
507 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
508 host_memory_backend_get_use_canonical_path,
509 host_memory_backend_set_use_canonical_path, &error_abort);
512 static const TypeInfo host_memory_backend_info = {
513 .name = TYPE_MEMORY_BACKEND,
514 .parent = TYPE_OBJECT,
516 .class_size = sizeof(HostMemoryBackendClass),
517 .class_init = host_memory_backend_class_init,
518 .instance_size = sizeof(HostMemoryBackend),
519 .instance_init = host_memory_backend_init,
520 .instance_post_init = host_memory_backend_post_init,
521 .interfaces = (InterfaceInfo[]) {
522 { TYPE_USER_CREATABLE },
527 static void register_types(void)
529 type_register_static(&host_memory_backend_info);
532 type_init(register_types);