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 g_strdup(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);
59 if (host_memory_backend_mr_inited(backend)) {
60 error_setg(errp, "cannot change property %s of %s ", name,
61 object_get_typename(obj));
65 if (!visit_type_size(v, name, &value, errp)) {
70 "property '%s' of %s doesn't take value '%" PRIu64 "'",
71 name, object_get_typename(obj), value);
74 backend->size = value;
78 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
79 void *opaque, Error **errp)
81 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
82 uint16List *host_nodes = NULL;
83 uint16List **tail = &host_nodes;
86 value = find_first_bit(backend->host_nodes, MAX_NODES);
87 if (value == MAX_NODES) {
91 QAPI_LIST_APPEND(tail, value);
94 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
95 if (value == MAX_NODES) {
99 QAPI_LIST_APPEND(tail, value);
103 visit_type_uint16List(v, name, &host_nodes, errp);
104 qapi_free_uint16List(host_nodes);
108 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
109 void *opaque, Error **errp)
112 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
113 uint16List *l, *host_nodes = NULL;
115 visit_type_uint16List(v, name, &host_nodes, errp);
117 for (l = host_nodes; l; l = l->next) {
118 if (l->value >= MAX_NODES) {
119 error_setg(errp, "Invalid host-nodes value: %d", l->value);
124 for (l = host_nodes; l; l = l->next) {
125 bitmap_set(backend->host_nodes, l->value, 1);
129 qapi_free_uint16List(host_nodes);
131 error_setg(errp, "NUMA node binding are not supported by this QEMU");
136 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
138 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
139 return backend->policy;
143 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
145 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
146 backend->policy = policy;
149 if (policy != HOST_MEM_POLICY_DEFAULT) {
150 error_setg(errp, "NUMA policies are not supported by this QEMU");
155 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
157 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
159 return backend->merge;
162 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
164 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
166 if (!host_memory_backend_mr_inited(backend)) {
167 backend->merge = value;
171 if (value != backend->merge) {
172 void *ptr = memory_region_get_ram_ptr(&backend->mr);
173 uint64_t sz = memory_region_size(&backend->mr);
175 qemu_madvise(ptr, sz,
176 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
177 backend->merge = value;
181 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
183 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
185 return backend->dump;
188 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
190 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
192 if (!host_memory_backend_mr_inited(backend)) {
193 backend->dump = value;
197 if (value != backend->dump) {
198 void *ptr = memory_region_get_ram_ptr(&backend->mr);
199 uint64_t sz = memory_region_size(&backend->mr);
201 qemu_madvise(ptr, sz,
202 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
203 backend->dump = value;
207 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
209 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
211 return backend->prealloc;
214 static void host_memory_backend_set_prealloc(Object *obj, bool value,
217 Error *local_err = NULL;
218 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
220 if (!host_memory_backend_mr_inited(backend)) {
221 backend->prealloc = value;
225 if (value && !backend->prealloc) {
226 int fd = memory_region_get_fd(&backend->mr);
227 void *ptr = memory_region_get_ram_ptr(&backend->mr);
228 uint64_t sz = memory_region_size(&backend->mr);
230 os_mem_prealloc(fd, ptr, sz, backend->prealloc_threads, &local_err);
232 error_propagate(errp, local_err);
235 backend->prealloc = true;
239 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v,
240 const char *name, void *opaque, Error **errp)
242 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
243 visit_type_uint32(v, name, &backend->prealloc_threads, errp);
246 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
247 const char *name, void *opaque, Error **errp)
249 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
252 if (!visit_type_uint32(v, name, &value, errp)) {
256 error_setg(errp, "property '%s' of %s doesn't take value '%d'", name,
257 object_get_typename(obj), value);
260 backend->prealloc_threads = value;
263 static void host_memory_backend_init(Object *obj)
265 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
266 MachineState *machine = MACHINE(qdev_get_machine());
268 /* TODO: convert access to globals to compat properties */
269 backend->merge = machine_mem_merge(machine);
270 backend->dump = machine_dump_guest_core(machine);
271 backend->prealloc_threads = 1;
274 static void host_memory_backend_post_init(Object *obj)
276 object_apply_compat_props(obj);
279 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
282 * NOTE: We forbid zero-length memory backend, so here zero means
283 * "we haven't inited the backend memory region yet".
285 return memory_region_size(&backend->mr) != 0;
288 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
290 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
293 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
295 backend->is_mapped = mapped;
298 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
300 return backend->is_mapped;
304 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
306 Object *obj = OBJECT(memdev);
307 char *path = object_property_get_str(obj, "mem-path", NULL);
308 size_t pagesize = qemu_mempath_getpagesize(path);
314 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
316 return qemu_real_host_page_size;
321 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
323 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
324 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
325 Error *local_err = NULL;
330 bc->alloc(backend, &local_err);
335 ptr = memory_region_get_ram_ptr(&backend->mr);
336 sz = memory_region_size(&backend->mr);
338 if (backend->merge) {
339 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
341 if (!backend->dump) {
342 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
345 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
346 /* lastbit == MAX_NODES means maxnode = 0 */
347 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
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 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
353 /* check for invalid host-nodes and policies and give more verbose
354 * error messages than mbind(). */
355 if (maxnode && backend->policy == MPOL_DEFAULT) {
356 error_setg(errp, "host-nodes must be empty for policy default,"
357 " or you should explicitly specify a policy other"
360 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
361 error_setg(errp, "host-nodes must be set for policy %s",
362 HostMemPolicy_str(backend->policy));
366 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
367 * as argument to mbind() due to an old Linux bug (feature?) which
368 * cuts off the last specified node. This means backend->host_nodes
369 * must have MAX_NODES+1 bits available.
371 assert(sizeof(backend->host_nodes) >=
372 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
373 assert(maxnode <= MAX_NODES);
376 mbind(ptr, sz, backend->policy, backend->host_nodes, maxnode + 1,
378 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
379 error_setg_errno(errp, errno,
380 "cannot bind memory to host NUMA nodes");
385 /* Preallocate memory after the NUMA policy has been instantiated.
386 * This is necessary to guarantee memory is allocated with
387 * specified NUMA policy in place.
389 if (backend->prealloc) {
390 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
391 backend->prealloc_threads, &local_err);
398 error_propagate(errp, local_err);
402 host_memory_backend_can_be_deleted(UserCreatable *uc)
404 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
411 static bool host_memory_backend_get_share(Object *o, Error **errp)
413 HostMemoryBackend *backend = MEMORY_BACKEND(o);
415 return backend->share;
418 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
420 HostMemoryBackend *backend = MEMORY_BACKEND(o);
422 if (host_memory_backend_mr_inited(backend)) {
423 error_setg(errp, "cannot change property value");
426 backend->share = value;
430 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
432 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
434 return backend->use_canonical_path;
438 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
441 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
443 backend->use_canonical_path = value;
447 host_memory_backend_class_init(ObjectClass *oc, void *data)
449 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
451 ucc->complete = host_memory_backend_memory_complete;
452 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
454 object_class_property_add_bool(oc, "merge",
455 host_memory_backend_get_merge,
456 host_memory_backend_set_merge);
457 object_class_property_set_description(oc, "merge",
458 "Mark memory as mergeable");
459 object_class_property_add_bool(oc, "dump",
460 host_memory_backend_get_dump,
461 host_memory_backend_set_dump);
462 object_class_property_set_description(oc, "dump",
463 "Set to 'off' to exclude from core dump");
464 object_class_property_add_bool(oc, "prealloc",
465 host_memory_backend_get_prealloc,
466 host_memory_backend_set_prealloc);
467 object_class_property_set_description(oc, "prealloc",
468 "Preallocate memory");
469 object_class_property_add(oc, "prealloc-threads", "int",
470 host_memory_backend_get_prealloc_threads,
471 host_memory_backend_set_prealloc_threads,
473 object_class_property_set_description(oc, "prealloc-threads",
474 "Number of CPU threads to use for prealloc");
475 object_class_property_add(oc, "size", "int",
476 host_memory_backend_get_size,
477 host_memory_backend_set_size,
479 object_class_property_set_description(oc, "size",
480 "Size of the memory region (ex: 500M)");
481 object_class_property_add(oc, "host-nodes", "int",
482 host_memory_backend_get_host_nodes,
483 host_memory_backend_set_host_nodes,
485 object_class_property_set_description(oc, "host-nodes",
486 "Binds memory to the list of NUMA host nodes");
487 object_class_property_add_enum(oc, "policy", "HostMemPolicy",
488 &HostMemPolicy_lookup,
489 host_memory_backend_get_policy,
490 host_memory_backend_set_policy);
491 object_class_property_set_description(oc, "policy",
492 "Set the NUMA policy");
493 object_class_property_add_bool(oc, "share",
494 host_memory_backend_get_share, host_memory_backend_set_share);
495 object_class_property_set_description(oc, "share",
496 "Mark the memory as private to QEMU or shared");
497 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
498 host_memory_backend_get_use_canonical_path,
499 host_memory_backend_set_use_canonical_path);
502 static const TypeInfo host_memory_backend_info = {
503 .name = TYPE_MEMORY_BACKEND,
504 .parent = TYPE_OBJECT,
506 .class_size = sizeof(HostMemoryBackendClass),
507 .class_init = host_memory_backend_class_init,
508 .instance_size = sizeof(HostMemoryBackend),
509 .instance_init = host_memory_backend_init,
510 .instance_post_init = host_memory_backend_post_init,
511 .interfaces = (InterfaceInfo[]) {
512 { TYPE_USER_CREATABLE },
517 static void register_types(void)
519 type_register_static(&host_memory_backend_info);
522 type_init(register_types);