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_size(Object
*obj
, Visitor
*v
, const char *name
,
33 void *opaque
, Error
**errp
)
35 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
36 uint64_t value
= backend
->size
;
38 visit_type_size(v
, name
, &value
, errp
);
42 host_memory_backend_set_size(Object
*obj
, Visitor
*v
, const char *name
,
43 void *opaque
, Error
**errp
)
45 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
46 Error
*local_err
= NULL
;
49 if (host_memory_backend_mr_inited(backend
)) {
50 error_setg(&local_err
, "cannot change property value");
54 visit_type_size(v
, name
, &value
, &local_err
);
59 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%"
60 PRIu64
"'", object_get_typename(obj
), name
, value
);
63 backend
->size
= value
;
65 error_propagate(errp
, local_err
);
69 host_memory_backend_get_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
70 void *opaque
, Error
**errp
)
72 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
73 uint16List
*host_nodes
= NULL
;
74 uint16List
**node
= &host_nodes
;
77 value
= find_first_bit(backend
->host_nodes
, MAX_NODES
);
78 if (value
== MAX_NODES
) {
82 *node
= g_malloc0(sizeof(**node
));
83 (*node
)->value
= value
;
84 node
= &(*node
)->next
;
87 value
= find_next_bit(backend
->host_nodes
, MAX_NODES
, value
+ 1);
88 if (value
== MAX_NODES
) {
92 *node
= g_malloc0(sizeof(**node
));
93 (*node
)->value
= value
;
94 node
= &(*node
)->next
;
97 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
101 host_memory_backend_set_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
102 void *opaque
, Error
**errp
)
105 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
106 uint16List
*l
= NULL
;
108 visit_type_uint16List(v
, name
, &l
, errp
);
111 bitmap_set(backend
->host_nodes
, l
->value
, 1);
115 error_setg(errp
, "NUMA node binding are not supported by this QEMU");
120 host_memory_backend_get_policy(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
122 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
123 return backend
->policy
;
127 host_memory_backend_set_policy(Object
*obj
, int policy
, Error
**errp
)
129 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
130 backend
->policy
= policy
;
133 if (policy
!= HOST_MEM_POLICY_DEFAULT
) {
134 error_setg(errp
, "NUMA policies are not supported by this QEMU");
139 static bool host_memory_backend_get_merge(Object
*obj
, Error
**errp
)
141 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
143 return backend
->merge
;
146 static void host_memory_backend_set_merge(Object
*obj
, bool value
, Error
**errp
)
148 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
150 if (!host_memory_backend_mr_inited(backend
)) {
151 backend
->merge
= value
;
155 if (value
!= backend
->merge
) {
156 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
157 uint64_t sz
= memory_region_size(&backend
->mr
);
159 qemu_madvise(ptr
, sz
,
160 value
? QEMU_MADV_MERGEABLE
: QEMU_MADV_UNMERGEABLE
);
161 backend
->merge
= value
;
165 static bool host_memory_backend_get_dump(Object
*obj
, Error
**errp
)
167 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
169 return backend
->dump
;
172 static void host_memory_backend_set_dump(Object
*obj
, bool value
, Error
**errp
)
174 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
176 if (!host_memory_backend_mr_inited(backend
)) {
177 backend
->dump
= value
;
181 if (value
!= backend
->dump
) {
182 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
183 uint64_t sz
= memory_region_size(&backend
->mr
);
185 qemu_madvise(ptr
, sz
,
186 value
? QEMU_MADV_DODUMP
: QEMU_MADV_DONTDUMP
);
187 backend
->dump
= value
;
191 static bool host_memory_backend_get_prealloc(Object
*obj
, Error
**errp
)
193 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
195 return backend
->prealloc
|| backend
->force_prealloc
;
198 static void host_memory_backend_set_prealloc(Object
*obj
, bool value
,
201 Error
*local_err
= NULL
;
202 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
204 if (backend
->force_prealloc
) {
207 "remove -mem-prealloc to use the prealloc property");
212 if (!host_memory_backend_mr_inited(backend
)) {
213 backend
->prealloc
= value
;
217 if (value
&& !backend
->prealloc
) {
218 int fd
= memory_region_get_fd(&backend
->mr
);
219 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
220 uint64_t sz
= memory_region_size(&backend
->mr
);
222 os_mem_prealloc(fd
, ptr
, sz
, smp_cpus
, &local_err
);
224 error_propagate(errp
, local_err
);
227 backend
->prealloc
= true;
231 static void host_memory_backend_init(Object
*obj
)
233 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
234 MachineState
*machine
= MACHINE(qdev_get_machine());
236 backend
->merge
= machine_mem_merge(machine
);
237 backend
->dump
= machine_dump_guest_core(machine
);
238 backend
->prealloc
= mem_prealloc
;
241 bool host_memory_backend_mr_inited(HostMemoryBackend
*backend
)
244 * NOTE: We forbid zero-length memory backend, so here zero means
245 * "we haven't inited the backend memory region yet".
247 return memory_region_size(&backend
->mr
) != 0;
250 MemoryRegion
*host_memory_backend_get_memory(HostMemoryBackend
*backend
)
252 return host_memory_backend_mr_inited(backend
) ? &backend
->mr
: NULL
;
255 void host_memory_backend_set_mapped(HostMemoryBackend
*backend
, bool mapped
)
257 backend
->is_mapped
= mapped
;
260 bool host_memory_backend_is_mapped(HostMemoryBackend
*backend
)
262 return backend
->is_mapped
;
266 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
268 Object
*obj
= OBJECT(memdev
);
269 char *path
= object_property_get_str(obj
, "mem-path", NULL
);
270 size_t pagesize
= qemu_mempath_getpagesize(path
);
276 size_t host_memory_backend_pagesize(HostMemoryBackend
*memdev
)
278 return getpagesize();
283 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
285 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
286 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
287 Error
*local_err
= NULL
;
292 bc
->alloc(backend
, &local_err
);
297 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
298 sz
= memory_region_size(&backend
->mr
);
300 if (backend
->merge
) {
301 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
303 if (!backend
->dump
) {
304 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
307 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
308 /* lastbit == MAX_NODES means maxnode = 0 */
309 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
310 /* ensure policy won't be ignored in case memory is preallocated
311 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
312 * this doesn't catch hugepage case. */
313 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
315 /* check for invalid host-nodes and policies and give more verbose
316 * error messages than mbind(). */
317 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
318 error_setg(errp
, "host-nodes must be empty for policy default,"
319 " or you should explicitly specify a policy other"
322 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
323 error_setg(errp
, "host-nodes must be set for policy %s",
324 HostMemPolicy_str(backend
->policy
));
328 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
329 * as argument to mbind() due to an old Linux bug (feature?) which
330 * cuts off the last specified node. This means backend->host_nodes
331 * must have MAX_NODES+1 bits available.
333 assert(sizeof(backend
->host_nodes
) >=
334 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
335 assert(maxnode
<= MAX_NODES
);
336 if (mbind(ptr
, sz
, backend
->policy
,
337 maxnode
? backend
->host_nodes
: NULL
, maxnode
+ 1, flags
)) {
338 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
339 error_setg_errno(errp
, errno
,
340 "cannot bind memory to host NUMA nodes");
345 /* Preallocate memory after the NUMA policy has been instantiated.
346 * This is necessary to guarantee memory is allocated with
347 * specified NUMA policy in place.
349 if (backend
->prealloc
) {
350 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
,
351 smp_cpus
, &local_err
);
358 error_propagate(errp
, local_err
);
362 host_memory_backend_can_be_deleted(UserCreatable
*uc
)
364 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc
))) {
371 static bool host_memory_backend_get_share(Object
*o
, Error
**errp
)
373 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
375 return backend
->share
;
378 static void host_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
380 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
382 if (host_memory_backend_mr_inited(backend
)) {
383 error_setg(errp
, "cannot change property value");
386 backend
->share
= value
;
390 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
392 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
394 ucc
->complete
= host_memory_backend_memory_complete
;
395 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
397 object_class_property_add_bool(oc
, "merge",
398 host_memory_backend_get_merge
,
399 host_memory_backend_set_merge
, &error_abort
);
400 object_class_property_add_bool(oc
, "dump",
401 host_memory_backend_get_dump
,
402 host_memory_backend_set_dump
, &error_abort
);
403 object_class_property_add_bool(oc
, "prealloc",
404 host_memory_backend_get_prealloc
,
405 host_memory_backend_set_prealloc
, &error_abort
);
406 object_class_property_add(oc
, "size", "int",
407 host_memory_backend_get_size
,
408 host_memory_backend_set_size
,
409 NULL
, NULL
, &error_abort
);
410 object_class_property_add(oc
, "host-nodes", "int",
411 host_memory_backend_get_host_nodes
,
412 host_memory_backend_set_host_nodes
,
413 NULL
, NULL
, &error_abort
);
414 object_class_property_add_enum(oc
, "policy", "HostMemPolicy",
415 &HostMemPolicy_lookup
,
416 host_memory_backend_get_policy
,
417 host_memory_backend_set_policy
, &error_abort
);
418 object_class_property_add_bool(oc
, "share",
419 host_memory_backend_get_share
, host_memory_backend_set_share
,
423 static const TypeInfo host_memory_backend_info
= {
424 .name
= TYPE_MEMORY_BACKEND
,
425 .parent
= TYPE_OBJECT
,
427 .class_size
= sizeof(HostMemoryBackendClass
),
428 .class_init
= host_memory_backend_class_init
,
429 .instance_size
= sizeof(HostMemoryBackend
),
430 .instance_init
= host_memory_backend_init
,
431 .interfaces
= (InterfaceInfo
[]) {
432 { TYPE_USER_CREATABLE
},
437 static void register_types(void)
439 type_register_static(&host_memory_backend_info
);
442 type_init(register_types
);