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
**node
= &host_nodes
;
86 value
= find_first_bit(backend
->host_nodes
, MAX_NODES
);
87 if (value
== MAX_NODES
) {
91 *node
= g_malloc0(sizeof(**node
));
92 (*node
)->value
= value
;
93 node
= &(*node
)->next
;
96 value
= find_next_bit(backend
->host_nodes
, MAX_NODES
, value
+ 1);
97 if (value
== MAX_NODES
) {
101 *node
= g_malloc0(sizeof(**node
));
102 (*node
)->value
= value
;
103 node
= &(*node
)->next
;
107 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
108 qapi_free_uint16List(host_nodes
);
112 host_memory_backend_set_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
113 void *opaque
, Error
**errp
)
116 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
117 uint16List
*l
, *host_nodes
= NULL
;
119 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
121 for (l
= host_nodes
; l
; l
= l
->next
) {
122 if (l
->value
>= MAX_NODES
) {
123 error_setg(errp
, "Invalid host-nodes value: %d", l
->value
);
128 for (l
= host_nodes
; l
; l
= l
->next
) {
129 bitmap_set(backend
->host_nodes
, l
->value
, 1);
133 qapi_free_uint16List(host_nodes
);
135 error_setg(errp
, "NUMA node binding are not supported by this QEMU");
140 host_memory_backend_get_policy(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
142 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
143 return backend
->policy
;
147 host_memory_backend_set_policy(Object
*obj
, int policy
, Error
**errp
)
149 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
150 backend
->policy
= policy
;
153 if (policy
!= HOST_MEM_POLICY_DEFAULT
) {
154 error_setg(errp
, "NUMA policies are not supported by this QEMU");
159 static bool host_memory_backend_get_merge(Object
*obj
, Error
**errp
)
161 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
163 return backend
->merge
;
166 static void host_memory_backend_set_merge(Object
*obj
, bool value
, Error
**errp
)
168 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
170 if (!host_memory_backend_mr_inited(backend
)) {
171 backend
->merge
= value
;
175 if (value
!= backend
->merge
) {
176 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
177 uint64_t sz
= memory_region_size(&backend
->mr
);
179 qemu_madvise(ptr
, sz
,
180 value
? QEMU_MADV_MERGEABLE
: QEMU_MADV_UNMERGEABLE
);
181 backend
->merge
= value
;
185 static bool host_memory_backend_get_dump(Object
*obj
, Error
**errp
)
187 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
189 return backend
->dump
;
192 static void host_memory_backend_set_dump(Object
*obj
, bool value
, Error
**errp
)
194 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
196 if (!host_memory_backend_mr_inited(backend
)) {
197 backend
->dump
= value
;
201 if (value
!= backend
->dump
) {
202 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
203 uint64_t sz
= memory_region_size(&backend
->mr
);
205 qemu_madvise(ptr
, sz
,
206 value
? QEMU_MADV_DODUMP
: QEMU_MADV_DONTDUMP
);
207 backend
->dump
= value
;
211 static bool host_memory_backend_get_prealloc(Object
*obj
, Error
**errp
)
213 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
215 return backend
->prealloc
;
218 static void host_memory_backend_set_prealloc(Object
*obj
, bool value
,
221 Error
*local_err
= NULL
;
222 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
224 if (!host_memory_backend_mr_inited(backend
)) {
225 backend
->prealloc
= value
;
229 if (value
&& !backend
->prealloc
) {
230 int fd
= memory_region_get_fd(&backend
->mr
);
231 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
232 uint64_t sz
= memory_region_size(&backend
->mr
);
234 os_mem_prealloc(fd
, ptr
, sz
, backend
->prealloc_threads
, &local_err
);
236 error_propagate(errp
, local_err
);
239 backend
->prealloc
= true;
243 static void host_memory_backend_get_prealloc_threads(Object
*obj
, Visitor
*v
,
244 const char *name
, void *opaque
, Error
**errp
)
246 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
247 visit_type_uint32(v
, name
, &backend
->prealloc_threads
, errp
);
250 static void host_memory_backend_set_prealloc_threads(Object
*obj
, Visitor
*v
,
251 const char *name
, void *opaque
, Error
**errp
)
253 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
256 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
260 error_setg(errp
, "property '%s' of %s doesn't take value '%d'", name
,
261 object_get_typename(obj
), value
);
264 backend
->prealloc_threads
= value
;
267 static void host_memory_backend_init(Object
*obj
)
269 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
270 MachineState
*machine
= MACHINE(qdev_get_machine());
272 /* TODO: convert access to globals to compat properties */
273 backend
->merge
= machine_mem_merge(machine
);
274 backend
->dump
= machine_dump_guest_core(machine
);
275 backend
->prealloc_threads
= 1;
278 static void host_memory_backend_post_init(Object
*obj
)
280 object_apply_compat_props(obj
);
283 bool host_memory_backend_mr_inited(HostMemoryBackend
*backend
)
286 * NOTE: We forbid zero-length memory backend, so here zero means
287 * "we haven't inited the backend memory region yet".
289 return memory_region_size(&backend
->mr
) != 0;
292 MemoryRegion
*host_memory_backend_get_memory(HostMemoryBackend
*backend
)
294 return host_memory_backend_mr_inited(backend
) ? &backend
->mr
: NULL
;
297 void host_memory_backend_set_mapped(HostMemoryBackend
*backend
, bool mapped
)
299 backend
->is_mapped
= mapped
;
302 bool host_memory_backend_is_mapped(HostMemoryBackend
*backend
)
304 return backend
->is_mapped
;
308 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
310 Object
*obj
= OBJECT(memdev
);
311 char *path
= object_property_get_str(obj
, "mem-path", NULL
);
312 size_t pagesize
= qemu_mempath_getpagesize(path
);
318 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
320 return qemu_real_host_page_size
;
325 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
327 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
328 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
329 Error
*local_err
= NULL
;
334 bc
->alloc(backend
, &local_err
);
339 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
340 sz
= memory_region_size(&backend
->mr
);
342 if (backend
->merge
) {
343 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
345 if (!backend
->dump
) {
346 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
349 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
350 /* lastbit == MAX_NODES means maxnode = 0 */
351 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
352 /* ensure policy won't be ignored in case memory is preallocated
353 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
354 * this doesn't catch hugepage case. */
355 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
357 /* check for invalid host-nodes and policies and give more verbose
358 * error messages than mbind(). */
359 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
360 error_setg(errp
, "host-nodes must be empty for policy default,"
361 " or you should explicitly specify a policy other"
364 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
365 error_setg(errp
, "host-nodes must be set for policy %s",
366 HostMemPolicy_str(backend
->policy
));
370 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
371 * as argument to mbind() due to an old Linux bug (feature?) which
372 * cuts off the last specified node. This means backend->host_nodes
373 * must have MAX_NODES+1 bits available.
375 assert(sizeof(backend
->host_nodes
) >=
376 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
377 assert(maxnode
<= MAX_NODES
);
380 mbind(ptr
, sz
, backend
->policy
, backend
->host_nodes
, maxnode
+ 1,
382 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
383 error_setg_errno(errp
, errno
,
384 "cannot bind memory to host NUMA nodes");
389 /* Preallocate memory after the NUMA policy has been instantiated.
390 * This is necessary to guarantee memory is allocated with
391 * specified NUMA policy in place.
393 if (backend
->prealloc
) {
394 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
,
395 backend
->prealloc_threads
, &local_err
);
402 error_propagate(errp
, local_err
);
406 host_memory_backend_can_be_deleted(UserCreatable
*uc
)
408 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc
))) {
415 static bool host_memory_backend_get_share(Object
*o
, Error
**errp
)
417 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
419 return backend
->share
;
422 static void host_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
424 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
426 if (host_memory_backend_mr_inited(backend
)) {
427 error_setg(errp
, "cannot change property value");
430 backend
->share
= value
;
434 host_memory_backend_get_use_canonical_path(Object
*obj
, Error
**errp
)
436 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
438 return backend
->use_canonical_path
;
442 host_memory_backend_set_use_canonical_path(Object
*obj
, bool value
,
445 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
447 backend
->use_canonical_path
= value
;
451 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
453 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
455 ucc
->complete
= host_memory_backend_memory_complete
;
456 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
458 object_class_property_add_bool(oc
, "merge",
459 host_memory_backend_get_merge
,
460 host_memory_backend_set_merge
);
461 object_class_property_set_description(oc
, "merge",
462 "Mark memory as mergeable");
463 object_class_property_add_bool(oc
, "dump",
464 host_memory_backend_get_dump
,
465 host_memory_backend_set_dump
);
466 object_class_property_set_description(oc
, "dump",
467 "Set to 'off' to exclude from core dump");
468 object_class_property_add_bool(oc
, "prealloc",
469 host_memory_backend_get_prealloc
,
470 host_memory_backend_set_prealloc
);
471 object_class_property_set_description(oc
, "prealloc",
472 "Preallocate memory");
473 object_class_property_add(oc
, "prealloc-threads", "int",
474 host_memory_backend_get_prealloc_threads
,
475 host_memory_backend_set_prealloc_threads
,
477 object_class_property_set_description(oc
, "prealloc-threads",
478 "Number of CPU threads to use for prealloc");
479 object_class_property_add(oc
, "size", "int",
480 host_memory_backend_get_size
,
481 host_memory_backend_set_size
,
483 object_class_property_set_description(oc
, "size",
484 "Size of the memory region (ex: 500M)");
485 object_class_property_add(oc
, "host-nodes", "int",
486 host_memory_backend_get_host_nodes
,
487 host_memory_backend_set_host_nodes
,
489 object_class_property_set_description(oc
, "host-nodes",
490 "Binds memory to the list of NUMA host nodes");
491 object_class_property_add_enum(oc
, "policy", "HostMemPolicy",
492 &HostMemPolicy_lookup
,
493 host_memory_backend_get_policy
,
494 host_memory_backend_set_policy
);
495 object_class_property_set_description(oc
, "policy",
496 "Set the NUMA policy");
497 object_class_property_add_bool(oc
, "share",
498 host_memory_backend_get_share
, host_memory_backend_set_share
);
499 object_class_property_set_description(oc
, "share",
500 "Mark the memory as private to QEMU or shared");
501 object_class_property_add_bool(oc
, "x-use-canonical-path-for-ramblock-id",
502 host_memory_backend_get_use_canonical_path
,
503 host_memory_backend_set_use_canonical_path
);
506 static const TypeInfo host_memory_backend_info
= {
507 .name
= TYPE_MEMORY_BACKEND
,
508 .parent
= TYPE_OBJECT
,
510 .class_size
= sizeof(HostMemoryBackendClass
),
511 .class_init
= host_memory_backend_class_init
,
512 .instance_size
= sizeof(HostMemoryBackend
),
513 .instance_init
= host_memory_backend_init
,
514 .instance_post_init
= host_memory_backend_post_init
,
515 .interfaces
= (InterfaceInfo
[]) {
516 { TYPE_USER_CREATABLE
},
521 static void register_types(void)
523 type_register_static(&host_memory_backend_info
);
526 type_init(register_types
);