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 "hw/boards.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-builtin-visit.h"
18 #include "qapi/visitor.h"
19 #include "qemu/config-file.h"
20 #include "qom/object_interfaces.h"
21 #include "qemu/mmap-alloc.h"
25 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT
!= MPOL_DEFAULT
);
26 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED
!= MPOL_PREFERRED
);
27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND
!= MPOL_BIND
);
28 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE
!= MPOL_INTERLEAVE
);
32 host_memory_backend_get_name(HostMemoryBackend
*backend
)
34 if (!backend
->use_canonical_path
) {
35 return object_get_canonical_path_component(OBJECT(backend
));
38 return object_get_canonical_path(OBJECT(backend
));
42 host_memory_backend_get_size(Object
*obj
, Visitor
*v
, const char *name
,
43 void *opaque
, Error
**errp
)
45 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
46 uint64_t value
= backend
->size
;
48 visit_type_size(v
, name
, &value
, errp
);
52 host_memory_backend_set_size(Object
*obj
, Visitor
*v
, const char *name
,
53 void *opaque
, Error
**errp
)
55 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
56 Error
*local_err
= NULL
;
59 if (host_memory_backend_mr_inited(backend
)) {
60 error_setg(&local_err
, "cannot change property value");
64 visit_type_size(v
, name
, &value
, &local_err
);
69 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%"
70 PRIu64
"'", object_get_typename(obj
), name
, value
);
73 backend
->size
= value
;
75 error_propagate(errp
, local_err
);
79 host_memory_backend_get_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
80 void *opaque
, Error
**errp
)
82 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
83 uint16List
*host_nodes
= NULL
;
84 uint16List
**node
= &host_nodes
;
87 value
= find_first_bit(backend
->host_nodes
, MAX_NODES
);
88 if (value
== MAX_NODES
) {
92 *node
= g_malloc0(sizeof(**node
));
93 (*node
)->value
= value
;
94 node
= &(*node
)->next
;
97 value
= find_next_bit(backend
->host_nodes
, MAX_NODES
, value
+ 1);
98 if (value
== MAX_NODES
) {
102 *node
= g_malloc0(sizeof(**node
));
103 (*node
)->value
= value
;
104 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
|| backend
->force_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 (backend
->force_prealloc
) {
226 "remove -mem-prealloc to use the prealloc property");
231 if (!host_memory_backend_mr_inited(backend
)) {
232 backend
->prealloc
= value
;
236 if (value
&& !backend
->prealloc
) {
237 int fd
= memory_region_get_fd(&backend
->mr
);
238 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
239 uint64_t sz
= memory_region_size(&backend
->mr
);
241 os_mem_prealloc(fd
, ptr
, sz
, smp_cpus
, &local_err
);
243 error_propagate(errp
, local_err
);
246 backend
->prealloc
= true;
250 static void host_memory_backend_init(Object
*obj
)
252 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
253 MachineState
*machine
= MACHINE(qdev_get_machine());
255 backend
->merge
= machine_mem_merge(machine
);
256 backend
->dump
= machine_dump_guest_core(machine
);
257 backend
->prealloc
= mem_prealloc
;
260 static void host_memory_backend_post_init(Object
*obj
)
262 object_apply_compat_props(obj
);
265 bool host_memory_backend_mr_inited(HostMemoryBackend
*backend
)
268 * NOTE: We forbid zero-length memory backend, so here zero means
269 * "we haven't inited the backend memory region yet".
271 return memory_region_size(&backend
->mr
) != 0;
274 MemoryRegion
*host_memory_backend_get_memory(HostMemoryBackend
*backend
)
276 return host_memory_backend_mr_inited(backend
) ? &backend
->mr
: NULL
;
279 void host_memory_backend_set_mapped(HostMemoryBackend
*backend
, bool mapped
)
281 backend
->is_mapped
= mapped
;
284 bool host_memory_backend_is_mapped(HostMemoryBackend
*backend
)
286 return backend
->is_mapped
;
290 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
292 Object
*obj
= OBJECT(memdev
);
293 char *path
= object_property_get_str(obj
, "mem-path", NULL
);
294 size_t pagesize
= qemu_mempath_getpagesize(path
);
300 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
302 return getpagesize();
307 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
309 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
310 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
311 Error
*local_err
= NULL
;
316 bc
->alloc(backend
, &local_err
);
321 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
322 sz
= memory_region_size(&backend
->mr
);
324 if (backend
->merge
) {
325 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
327 if (!backend
->dump
) {
328 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
331 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
332 /* lastbit == MAX_NODES means maxnode = 0 */
333 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
334 /* ensure policy won't be ignored in case memory is preallocated
335 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
336 * this doesn't catch hugepage case. */
337 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
339 /* check for invalid host-nodes and policies and give more verbose
340 * error messages than mbind(). */
341 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
342 error_setg(errp
, "host-nodes must be empty for policy default,"
343 " or you should explicitly specify a policy other"
346 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
347 error_setg(errp
, "host-nodes must be set for policy %s",
348 HostMemPolicy_str(backend
->policy
));
352 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
353 * as argument to mbind() due to an old Linux bug (feature?) which
354 * cuts off the last specified node. This means backend->host_nodes
355 * must have MAX_NODES+1 bits available.
357 assert(sizeof(backend
->host_nodes
) >=
358 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
359 assert(maxnode
<= MAX_NODES
);
360 if (mbind(ptr
, sz
, backend
->policy
,
361 maxnode
? backend
->host_nodes
: NULL
, maxnode
+ 1, flags
)) {
362 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
363 error_setg_errno(errp
, errno
,
364 "cannot bind memory to host NUMA nodes");
369 /* Preallocate memory after the NUMA policy has been instantiated.
370 * This is necessary to guarantee memory is allocated with
371 * specified NUMA policy in place.
373 if (backend
->prealloc
) {
374 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
,
375 smp_cpus
, &local_err
);
382 error_propagate(errp
, local_err
);
386 host_memory_backend_can_be_deleted(UserCreatable
*uc
)
388 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc
))) {
395 static bool host_memory_backend_get_share(Object
*o
, Error
**errp
)
397 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
399 return backend
->share
;
402 static void host_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
404 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
406 if (host_memory_backend_mr_inited(backend
)) {
407 error_setg(errp
, "cannot change property value");
410 backend
->share
= value
;
414 host_memory_backend_get_use_canonical_path(Object
*obj
, Error
**errp
)
416 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
418 return backend
->use_canonical_path
;
422 host_memory_backend_set_use_canonical_path(Object
*obj
, bool value
,
425 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
427 backend
->use_canonical_path
= value
;
431 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
433 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
435 ucc
->complete
= host_memory_backend_memory_complete
;
436 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
438 object_class_property_add_bool(oc
, "merge",
439 host_memory_backend_get_merge
,
440 host_memory_backend_set_merge
, &error_abort
);
441 object_class_property_set_description(oc
, "merge",
442 "Mark memory as mergeable", &error_abort
);
443 object_class_property_add_bool(oc
, "dump",
444 host_memory_backend_get_dump
,
445 host_memory_backend_set_dump
, &error_abort
);
446 object_class_property_set_description(oc
, "dump",
447 "Set to 'off' to exclude from core dump", &error_abort
);
448 object_class_property_add_bool(oc
, "prealloc",
449 host_memory_backend_get_prealloc
,
450 host_memory_backend_set_prealloc
, &error_abort
);
451 object_class_property_set_description(oc
, "prealloc",
452 "Preallocate memory", &error_abort
);
453 object_class_property_add(oc
, "size", "int",
454 host_memory_backend_get_size
,
455 host_memory_backend_set_size
,
456 NULL
, NULL
, &error_abort
);
457 object_class_property_set_description(oc
, "size",
458 "Size of the memory region (ex: 500M)", &error_abort
);
459 object_class_property_add(oc
, "host-nodes", "int",
460 host_memory_backend_get_host_nodes
,
461 host_memory_backend_set_host_nodes
,
462 NULL
, NULL
, &error_abort
);
463 object_class_property_set_description(oc
, "host-nodes",
464 "Binds memory to the list of NUMA host nodes", &error_abort
);
465 object_class_property_add_enum(oc
, "policy", "HostMemPolicy",
466 &HostMemPolicy_lookup
,
467 host_memory_backend_get_policy
,
468 host_memory_backend_set_policy
, &error_abort
);
469 object_class_property_set_description(oc
, "policy",
470 "Set the NUMA policy", &error_abort
);
471 object_class_property_add_bool(oc
, "share",
472 host_memory_backend_get_share
, host_memory_backend_set_share
,
474 object_class_property_set_description(oc
, "share",
475 "Mark the memory as private to QEMU or shared", &error_abort
);
476 object_class_property_add_bool(oc
, "x-use-canonical-path-for-ramblock-id",
477 host_memory_backend_get_use_canonical_path
,
478 host_memory_backend_set_use_canonical_path
, &error_abort
);
481 static const TypeInfo host_memory_backend_info
= {
482 .name
= TYPE_MEMORY_BACKEND
,
483 .parent
= TYPE_OBJECT
,
485 .class_size
= sizeof(HostMemoryBackendClass
),
486 .class_init
= host_memory_backend_class_init
,
487 .instance_size
= sizeof(HostMemoryBackend
),
488 .instance_init
= host_memory_backend_init
,
489 .instance_post_init
= host_memory_backend_post_init
,
490 .interfaces
= (InterfaceInfo
[]) {
491 { TYPE_USER_CREATABLE
},
496 static void register_types(void)
498 type_register_static(&host_memory_backend_info
);
501 type_init(register_types
);