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.
12 #include "qemu/osdep.h"
13 #include "sysemu/hostmem.h"
14 #include "hw/boards.h"
15 #include "qapi/error.h"
16 #include "qapi/visitor.h"
17 #include "qapi-types.h"
18 #include "qapi-visit.h"
19 #include "qemu/config-file.h"
20 #include "qom/object_interfaces.h"
24 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT
!= MPOL_DEFAULT
);
25 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED
!= MPOL_PREFERRED
);
26 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND
!= MPOL_BIND
);
27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE
!= MPOL_INTERLEAVE
);
31 host_memory_backend_get_size(Object
*obj
, Visitor
*v
, const char *name
,
32 void *opaque
, Error
**errp
)
34 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
35 uint64_t value
= backend
->size
;
37 visit_type_size(v
, name
, &value
, errp
);
41 host_memory_backend_set_size(Object
*obj
, Visitor
*v
, const char *name
,
42 void *opaque
, Error
**errp
)
44 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
45 Error
*local_err
= NULL
;
48 if (memory_region_size(&backend
->mr
)) {
49 error_setg(&local_err
, "cannot change property value");
53 visit_type_size(v
, name
, &value
, &local_err
);
58 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%"
59 PRIu64
"'", object_get_typename(obj
), name
, value
);
62 backend
->size
= value
;
64 error_propagate(errp
, local_err
);
68 host_memory_backend_get_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
69 void *opaque
, Error
**errp
)
71 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
72 uint16List
*host_nodes
= NULL
;
73 uint16List
**node
= &host_nodes
;
76 value
= find_first_bit(backend
->host_nodes
, MAX_NODES
);
77 if (value
== MAX_NODES
) {
81 *node
= g_malloc0(sizeof(**node
));
82 (*node
)->value
= value
;
83 node
= &(*node
)->next
;
86 value
= find_next_bit(backend
->host_nodes
, MAX_NODES
, value
+ 1);
87 if (value
== MAX_NODES
) {
91 *node
= g_malloc0(sizeof(**node
));
92 (*node
)->value
= value
;
93 node
= &(*node
)->next
;
96 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
100 host_memory_backend_set_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
101 void *opaque
, Error
**errp
)
104 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
105 uint16List
*l
= NULL
;
107 visit_type_uint16List(v
, name
, &l
, errp
);
110 bitmap_set(backend
->host_nodes
, l
->value
, 1);
114 error_setg(errp
, "NUMA node binding are not supported by this QEMU");
119 host_memory_backend_get_policy(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
121 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
122 return backend
->policy
;
126 host_memory_backend_set_policy(Object
*obj
, int policy
, Error
**errp
)
128 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
129 backend
->policy
= policy
;
132 if (policy
!= HOST_MEM_POLICY_DEFAULT
) {
133 error_setg(errp
, "NUMA policies are not supported by this QEMU");
138 static bool host_memory_backend_get_merge(Object
*obj
, Error
**errp
)
140 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
142 return backend
->merge
;
145 static void host_memory_backend_set_merge(Object
*obj
, bool value
, Error
**errp
)
147 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
149 if (!memory_region_size(&backend
->mr
)) {
150 backend
->merge
= value
;
154 if (value
!= backend
->merge
) {
155 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
156 uint64_t sz
= memory_region_size(&backend
->mr
);
158 qemu_madvise(ptr
, sz
,
159 value
? QEMU_MADV_MERGEABLE
: QEMU_MADV_UNMERGEABLE
);
160 backend
->merge
= value
;
164 static bool host_memory_backend_get_dump(Object
*obj
, Error
**errp
)
166 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
168 return backend
->dump
;
171 static void host_memory_backend_set_dump(Object
*obj
, bool value
, Error
**errp
)
173 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
175 if (!memory_region_size(&backend
->mr
)) {
176 backend
->dump
= value
;
180 if (value
!= backend
->dump
) {
181 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
182 uint64_t sz
= memory_region_size(&backend
->mr
);
184 qemu_madvise(ptr
, sz
,
185 value
? QEMU_MADV_DODUMP
: QEMU_MADV_DONTDUMP
);
186 backend
->dump
= value
;
190 static bool host_memory_backend_get_prealloc(Object
*obj
, Error
**errp
)
192 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
194 return backend
->prealloc
|| backend
->force_prealloc
;
197 static void host_memory_backend_set_prealloc(Object
*obj
, bool value
,
200 Error
*local_err
= NULL
;
201 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
203 if (backend
->force_prealloc
) {
206 "remove -mem-prealloc to use the prealloc property");
211 if (!memory_region_size(&backend
->mr
)) {
212 backend
->prealloc
= value
;
216 if (value
&& !backend
->prealloc
) {
217 int fd
= memory_region_get_fd(&backend
->mr
);
218 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
219 uint64_t sz
= memory_region_size(&backend
->mr
);
221 os_mem_prealloc(fd
, ptr
, sz
, smp_cpus
, &local_err
);
223 error_propagate(errp
, local_err
);
226 backend
->prealloc
= true;
230 static void host_memory_backend_init(Object
*obj
)
232 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
233 MachineState
*machine
= MACHINE(qdev_get_machine());
235 backend
->merge
= machine_mem_merge(machine
);
236 backend
->dump
= machine_dump_guest_core(machine
);
237 backend
->prealloc
= mem_prealloc
;
241 host_memory_backend_get_memory(HostMemoryBackend
*backend
, Error
**errp
)
243 return memory_region_size(&backend
->mr
) ? &backend
->mr
: NULL
;
246 void host_memory_backend_set_mapped(HostMemoryBackend
*backend
, bool mapped
)
248 backend
->is_mapped
= mapped
;
251 bool host_memory_backend_is_mapped(HostMemoryBackend
*backend
)
253 return backend
->is_mapped
;
257 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
259 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
260 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
261 Error
*local_err
= NULL
;
266 bc
->alloc(backend
, &local_err
);
271 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
272 sz
= memory_region_size(&backend
->mr
);
274 if (backend
->merge
) {
275 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
277 if (!backend
->dump
) {
278 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
281 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
282 /* lastbit == MAX_NODES means maxnode = 0 */
283 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
284 /* ensure policy won't be ignored in case memory is preallocated
285 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
286 * this doesn't catch hugepage case. */
287 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
289 /* check for invalid host-nodes and policies and give more verbose
290 * error messages than mbind(). */
291 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
292 error_setg(errp
, "host-nodes must be empty for policy default,"
293 " or you should explicitly specify a policy other"
296 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
297 error_setg(errp
, "host-nodes must be set for policy %s",
298 HostMemPolicy_lookup
[backend
->policy
]);
302 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
303 * as argument to mbind() due to an old Linux bug (feature?) which
304 * cuts off the last specified node. This means backend->host_nodes
305 * must have MAX_NODES+1 bits available.
307 assert(sizeof(backend
->host_nodes
) >=
308 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
309 assert(maxnode
<= MAX_NODES
);
310 if (mbind(ptr
, sz
, backend
->policy
,
311 maxnode
? backend
->host_nodes
: NULL
, maxnode
+ 1, flags
)) {
312 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
313 error_setg_errno(errp
, errno
,
314 "cannot bind memory to host NUMA nodes");
319 /* Preallocate memory after the NUMA policy has been instantiated.
320 * This is necessary to guarantee memory is allocated with
321 * specified NUMA policy in place.
323 if (backend
->prealloc
) {
324 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
,
325 smp_cpus
, &local_err
);
332 error_propagate(errp
, local_err
);
336 host_memory_backend_can_be_deleted(UserCreatable
*uc
, Error
**errp
)
338 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc
))) {
345 static char *get_id(Object
*o
, Error
**errp
)
347 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
349 return g_strdup(backend
->id
);
352 static void set_id(Object
*o
, const char *str
, Error
**errp
)
354 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
357 error_setg(errp
, "cannot change property value");
360 backend
->id
= g_strdup(str
);
364 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
366 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
368 ucc
->complete
= host_memory_backend_memory_complete
;
369 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
371 object_class_property_add_bool(oc
, "merge",
372 host_memory_backend_get_merge
,
373 host_memory_backend_set_merge
, &error_abort
);
374 object_class_property_add_bool(oc
, "dump",
375 host_memory_backend_get_dump
,
376 host_memory_backend_set_dump
, &error_abort
);
377 object_class_property_add_bool(oc
, "prealloc",
378 host_memory_backend_get_prealloc
,
379 host_memory_backend_set_prealloc
, &error_abort
);
380 object_class_property_add(oc
, "size", "int",
381 host_memory_backend_get_size
,
382 host_memory_backend_set_size
,
383 NULL
, NULL
, &error_abort
);
384 object_class_property_add(oc
, "host-nodes", "int",
385 host_memory_backend_get_host_nodes
,
386 host_memory_backend_set_host_nodes
,
387 NULL
, NULL
, &error_abort
);
388 object_class_property_add_enum(oc
, "policy", "HostMemPolicy",
389 HostMemPolicy_lookup
,
390 host_memory_backend_get_policy
,
391 host_memory_backend_set_policy
, &error_abort
);
392 object_class_property_add_str(oc
, "id", get_id
, set_id
, &error_abort
);
395 static void host_memory_backend_finalize(Object
*o
)
397 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
401 static const TypeInfo host_memory_backend_info
= {
402 .name
= TYPE_MEMORY_BACKEND
,
403 .parent
= TYPE_OBJECT
,
405 .class_size
= sizeof(HostMemoryBackendClass
),
406 .class_init
= host_memory_backend_class_init
,
407 .instance_size
= sizeof(HostMemoryBackend
),
408 .instance_init
= host_memory_backend_init
,
409 .instance_finalize
= host_memory_backend_finalize
,
410 .interfaces
= (InterfaceInfo
[]) {
411 { TYPE_USER_CREATABLE
},
416 static void register_types(void)
418 type_register_static(&host_memory_backend_info
);
421 type_init(register_types
);