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
);
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
);
111 host_memory_backend_set_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
112 void *opaque
, Error
**errp
)
115 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
116 uint16List
*l
, *host_nodes
= NULL
;
118 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
120 for (l
= host_nodes
; l
; l
= l
->next
) {
121 if (l
->value
>= MAX_NODES
) {
122 error_setg(errp
, "Invalid host-nodes value: %d", l
->value
);
127 for (l
= host_nodes
; l
; l
= l
->next
) {
128 bitmap_set(backend
->host_nodes
, l
->value
, 1);
132 qapi_free_uint16List(host_nodes
);
134 error_setg(errp
, "NUMA node binding are not supported by this QEMU");
139 host_memory_backend_get_policy(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
141 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
142 return backend
->policy
;
146 host_memory_backend_set_policy(Object
*obj
, int policy
, Error
**errp
)
148 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
149 backend
->policy
= policy
;
152 if (policy
!= HOST_MEM_POLICY_DEFAULT
) {
153 error_setg(errp
, "NUMA policies are not supported by this QEMU");
158 static bool host_memory_backend_get_merge(Object
*obj
, Error
**errp
)
160 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
162 return backend
->merge
;
165 static void host_memory_backend_set_merge(Object
*obj
, bool value
, Error
**errp
)
167 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
169 if (!host_memory_backend_mr_inited(backend
)) {
170 backend
->merge
= value
;
174 if (value
!= backend
->merge
) {
175 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
176 uint64_t sz
= memory_region_size(&backend
->mr
);
178 qemu_madvise(ptr
, sz
,
179 value
? QEMU_MADV_MERGEABLE
: QEMU_MADV_UNMERGEABLE
);
180 backend
->merge
= value
;
184 static bool host_memory_backend_get_dump(Object
*obj
, Error
**errp
)
186 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
188 return backend
->dump
;
191 static void host_memory_backend_set_dump(Object
*obj
, bool value
, Error
**errp
)
193 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
195 if (!host_memory_backend_mr_inited(backend
)) {
196 backend
->dump
= value
;
200 if (value
!= backend
->dump
) {
201 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
202 uint64_t sz
= memory_region_size(&backend
->mr
);
204 qemu_madvise(ptr
, sz
,
205 value
? QEMU_MADV_DODUMP
: QEMU_MADV_DONTDUMP
);
206 backend
->dump
= value
;
210 static bool host_memory_backend_get_prealloc(Object
*obj
, Error
**errp
)
212 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
214 return backend
->prealloc
;
217 static void host_memory_backend_set_prealloc(Object
*obj
, bool value
,
220 Error
*local_err
= NULL
;
221 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
223 if (!host_memory_backend_mr_inited(backend
)) {
224 backend
->prealloc
= value
;
228 if (value
&& !backend
->prealloc
) {
229 int fd
= memory_region_get_fd(&backend
->mr
);
230 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
231 uint64_t sz
= memory_region_size(&backend
->mr
);
233 os_mem_prealloc(fd
, ptr
, sz
, backend
->prealloc_threads
, &local_err
);
235 error_propagate(errp
, local_err
);
238 backend
->prealloc
= true;
242 static void host_memory_backend_get_prealloc_threads(Object
*obj
, Visitor
*v
,
243 const char *name
, void *opaque
, Error
**errp
)
245 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
246 visit_type_uint32(v
, name
, &backend
->prealloc_threads
, errp
);
249 static void host_memory_backend_set_prealloc_threads(Object
*obj
, Visitor
*v
,
250 const char *name
, void *opaque
, Error
**errp
)
252 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
255 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
259 error_setg(errp
, "property '%s' of %s doesn't take value '%d'", name
,
260 object_get_typename(obj
), value
);
263 backend
->prealloc_threads
= value
;
266 static void host_memory_backend_init(Object
*obj
)
268 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
269 MachineState
*machine
= MACHINE(qdev_get_machine());
271 /* TODO: convert access to globals to compat properties */
272 backend
->merge
= machine_mem_merge(machine
);
273 backend
->dump
= machine_dump_guest_core(machine
);
274 backend
->prealloc_threads
= 1;
277 static void host_memory_backend_post_init(Object
*obj
)
279 object_apply_compat_props(obj
);
282 bool host_memory_backend_mr_inited(HostMemoryBackend
*backend
)
285 * NOTE: We forbid zero-length memory backend, so here zero means
286 * "we haven't inited the backend memory region yet".
288 return memory_region_size(&backend
->mr
) != 0;
291 MemoryRegion
*host_memory_backend_get_memory(HostMemoryBackend
*backend
)
293 return host_memory_backend_mr_inited(backend
) ? &backend
->mr
: NULL
;
296 void host_memory_backend_set_mapped(HostMemoryBackend
*backend
, bool mapped
)
298 backend
->is_mapped
= mapped
;
301 bool host_memory_backend_is_mapped(HostMemoryBackend
*backend
)
303 return backend
->is_mapped
;
307 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
309 Object
*obj
= OBJECT(memdev
);
310 char *path
= object_property_get_str(obj
, "mem-path", NULL
);
311 size_t pagesize
= qemu_mempath_getpagesize(path
);
317 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
319 return qemu_real_host_page_size
;
324 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
326 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
327 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
328 Error
*local_err
= NULL
;
333 bc
->alloc(backend
, &local_err
);
338 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
339 sz
= memory_region_size(&backend
->mr
);
341 if (backend
->merge
) {
342 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
344 if (!backend
->dump
) {
345 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
348 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
349 /* lastbit == MAX_NODES means maxnode = 0 */
350 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
351 /* ensure policy won't be ignored in case memory is preallocated
352 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
353 * this doesn't catch hugepage case. */
354 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
356 /* check for invalid host-nodes and policies and give more verbose
357 * error messages than mbind(). */
358 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
359 error_setg(errp
, "host-nodes must be empty for policy default,"
360 " or you should explicitly specify a policy other"
363 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
364 error_setg(errp
, "host-nodes must be set for policy %s",
365 HostMemPolicy_str(backend
->policy
));
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.
374 assert(sizeof(backend
->host_nodes
) >=
375 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
376 assert(maxnode
<= MAX_NODES
);
379 mbind(ptr
, sz
, backend
->policy
, backend
->host_nodes
, maxnode
+ 1,
381 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
382 error_setg_errno(errp
, errno
,
383 "cannot bind memory to host NUMA nodes");
388 /* Preallocate memory after the NUMA policy has been instantiated.
389 * This is necessary to guarantee memory is allocated with
390 * specified NUMA policy in place.
392 if (backend
->prealloc
) {
393 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
,
394 backend
->prealloc_threads
, &local_err
);
401 error_propagate(errp
, local_err
);
405 host_memory_backend_can_be_deleted(UserCreatable
*uc
)
407 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc
))) {
414 static bool host_memory_backend_get_share(Object
*o
, Error
**errp
)
416 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
418 return backend
->share
;
421 static void host_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
423 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
425 if (host_memory_backend_mr_inited(backend
)) {
426 error_setg(errp
, "cannot change property value");
429 backend
->share
= value
;
433 host_memory_backend_get_use_canonical_path(Object
*obj
, Error
**errp
)
435 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
437 return backend
->use_canonical_path
;
441 host_memory_backend_set_use_canonical_path(Object
*obj
, bool value
,
444 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
446 backend
->use_canonical_path
= value
;
450 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
452 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
454 ucc
->complete
= host_memory_backend_memory_complete
;
455 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
457 object_class_property_add_bool(oc
, "merge",
458 host_memory_backend_get_merge
,
459 host_memory_backend_set_merge
);
460 object_class_property_set_description(oc
, "merge",
461 "Mark memory as mergeable");
462 object_class_property_add_bool(oc
, "dump",
463 host_memory_backend_get_dump
,
464 host_memory_backend_set_dump
);
465 object_class_property_set_description(oc
, "dump",
466 "Set to 'off' to exclude from core dump");
467 object_class_property_add_bool(oc
, "prealloc",
468 host_memory_backend_get_prealloc
,
469 host_memory_backend_set_prealloc
);
470 object_class_property_set_description(oc
, "prealloc",
471 "Preallocate memory");
472 object_class_property_add(oc
, "prealloc-threads", "int",
473 host_memory_backend_get_prealloc_threads
,
474 host_memory_backend_set_prealloc_threads
,
476 object_class_property_set_description(oc
, "prealloc-threads",
477 "Number of CPU threads to use for prealloc");
478 object_class_property_add(oc
, "size", "int",
479 host_memory_backend_get_size
,
480 host_memory_backend_set_size
,
482 object_class_property_set_description(oc
, "size",
483 "Size of the memory region (ex: 500M)");
484 object_class_property_add(oc
, "host-nodes", "int",
485 host_memory_backend_get_host_nodes
,
486 host_memory_backend_set_host_nodes
,
488 object_class_property_set_description(oc
, "host-nodes",
489 "Binds memory to the list of NUMA host nodes");
490 object_class_property_add_enum(oc
, "policy", "HostMemPolicy",
491 &HostMemPolicy_lookup
,
492 host_memory_backend_get_policy
,
493 host_memory_backend_set_policy
);
494 object_class_property_set_description(oc
, "policy",
495 "Set the NUMA policy");
496 object_class_property_add_bool(oc
, "share",
497 host_memory_backend_get_share
, host_memory_backend_set_share
);
498 object_class_property_set_description(oc
, "share",
499 "Mark the memory as private to QEMU or shared");
500 object_class_property_add_bool(oc
, "x-use-canonical-path-for-ramblock-id",
501 host_memory_backend_get_use_canonical_path
,
502 host_memory_backend_set_use_canonical_path
);
505 static const TypeInfo host_memory_backend_info
= {
506 .name
= TYPE_MEMORY_BACKEND
,
507 .parent
= TYPE_OBJECT
,
509 .class_size
= sizeof(HostMemoryBackendClass
),
510 .class_init
= host_memory_backend_class_init
,
511 .instance_size
= sizeof(HostMemoryBackend
),
512 .instance_init
= host_memory_backend_init
,
513 .instance_post_init
= host_memory_backend_post_init
,
514 .interfaces
= (InterfaceInfo
[]) {
515 { TYPE_USER_CREATABLE
},
520 static void register_types(void)
522 type_register_static(&host_memory_backend_info
);
525 type_init(register_types
);