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/visitor.h"
16 #include "qapi-types.h"
17 #include "qapi-visit.h"
18 #include "qemu/config-file.h"
19 #include "qom/object_interfaces.h"
23 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT
!= MPOL_DEFAULT
);
24 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED
!= MPOL_PREFERRED
);
25 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND
!= MPOL_BIND
);
26 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE
!= MPOL_INTERLEAVE
);
30 host_memory_backend_get_size(Object
*obj
, Visitor
*v
, const char *name
,
31 void *opaque
, Error
**errp
)
33 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
34 uint64_t value
= backend
->size
;
36 visit_type_size(v
, name
, &value
, errp
);
40 host_memory_backend_set_size(Object
*obj
, Visitor
*v
, const char *name
,
41 void *opaque
, Error
**errp
)
43 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
44 Error
*local_err
= NULL
;
47 if (memory_region_size(&backend
->mr
)) {
48 error_setg(&local_err
, "cannot change property value");
52 visit_type_size(v
, name
, &value
, &local_err
);
57 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%"
58 PRIu64
"'", object_get_typename(obj
), name
, value
);
61 backend
->size
= value
;
63 error_propagate(errp
, local_err
);
67 host_memory_backend_get_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
68 void *opaque
, Error
**errp
)
70 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
71 uint16List
*host_nodes
= NULL
;
72 uint16List
**node
= &host_nodes
;
75 value
= find_first_bit(backend
->host_nodes
, MAX_NODES
);
76 if (value
== MAX_NODES
) {
80 *node
= g_malloc0(sizeof(**node
));
81 (*node
)->value
= value
;
82 node
= &(*node
)->next
;
85 value
= find_next_bit(backend
->host_nodes
, MAX_NODES
, value
+ 1);
86 if (value
== MAX_NODES
) {
90 *node
= g_malloc0(sizeof(**node
));
91 (*node
)->value
= value
;
92 node
= &(*node
)->next
;
95 visit_type_uint16List(v
, name
, &host_nodes
, errp
);
99 host_memory_backend_set_host_nodes(Object
*obj
, Visitor
*v
, const char *name
,
100 void *opaque
, Error
**errp
)
103 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
104 uint16List
*l
= NULL
;
106 visit_type_uint16List(v
, name
, &l
, errp
);
109 bitmap_set(backend
->host_nodes
, l
->value
, 1);
113 error_setg(errp
, "NUMA node binding are not supported by this QEMU");
118 host_memory_backend_get_policy(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
120 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
121 return backend
->policy
;
125 host_memory_backend_set_policy(Object
*obj
, int policy
, Error
**errp
)
127 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
128 backend
->policy
= policy
;
131 if (policy
!= HOST_MEM_POLICY_DEFAULT
) {
132 error_setg(errp
, "NUMA policies are not supported by this QEMU");
137 static bool host_memory_backend_get_merge(Object
*obj
, Error
**errp
)
139 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
141 return backend
->merge
;
144 static void host_memory_backend_set_merge(Object
*obj
, bool value
, Error
**errp
)
146 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
148 if (!memory_region_size(&backend
->mr
)) {
149 backend
->merge
= value
;
153 if (value
!= backend
->merge
) {
154 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
155 uint64_t sz
= memory_region_size(&backend
->mr
);
157 qemu_madvise(ptr
, sz
,
158 value
? QEMU_MADV_MERGEABLE
: QEMU_MADV_UNMERGEABLE
);
159 backend
->merge
= value
;
163 static bool host_memory_backend_get_dump(Object
*obj
, Error
**errp
)
165 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
167 return backend
->dump
;
170 static void host_memory_backend_set_dump(Object
*obj
, bool value
, Error
**errp
)
172 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
174 if (!memory_region_size(&backend
->mr
)) {
175 backend
->dump
= value
;
179 if (value
!= backend
->dump
) {
180 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
181 uint64_t sz
= memory_region_size(&backend
->mr
);
183 qemu_madvise(ptr
, sz
,
184 value
? QEMU_MADV_DODUMP
: QEMU_MADV_DONTDUMP
);
185 backend
->dump
= value
;
189 static bool host_memory_backend_get_prealloc(Object
*obj
, Error
**errp
)
191 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
193 return backend
->prealloc
|| backend
->force_prealloc
;
196 static void host_memory_backend_set_prealloc(Object
*obj
, bool value
,
199 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
201 if (backend
->force_prealloc
) {
204 "remove -mem-prealloc to use the prealloc property");
209 if (!memory_region_size(&backend
->mr
)) {
210 backend
->prealloc
= value
;
214 if (value
&& !backend
->prealloc
) {
215 int fd
= memory_region_get_fd(&backend
->mr
);
216 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
217 uint64_t sz
= memory_region_size(&backend
->mr
);
219 os_mem_prealloc(fd
, ptr
, sz
);
220 backend
->prealloc
= true;
224 static void host_memory_backend_init(Object
*obj
)
226 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
227 MachineState
*machine
= MACHINE(qdev_get_machine());
229 backend
->merge
= machine_mem_merge(machine
);
230 backend
->dump
= machine_dump_guest_core(machine
);
231 backend
->prealloc
= mem_prealloc
;
233 object_property_add_bool(obj
, "merge",
234 host_memory_backend_get_merge
,
235 host_memory_backend_set_merge
, NULL
);
236 object_property_add_bool(obj
, "dump",
237 host_memory_backend_get_dump
,
238 host_memory_backend_set_dump
, NULL
);
239 object_property_add_bool(obj
, "prealloc",
240 host_memory_backend_get_prealloc
,
241 host_memory_backend_set_prealloc
, NULL
);
242 object_property_add(obj
, "size", "int",
243 host_memory_backend_get_size
,
244 host_memory_backend_set_size
, NULL
, NULL
, NULL
);
245 object_property_add(obj
, "host-nodes", "int",
246 host_memory_backend_get_host_nodes
,
247 host_memory_backend_set_host_nodes
, NULL
, NULL
, NULL
);
248 object_property_add_enum(obj
, "policy", "HostMemPolicy",
249 HostMemPolicy_lookup
,
250 host_memory_backend_get_policy
,
251 host_memory_backend_set_policy
, NULL
);
255 host_memory_backend_get_memory(HostMemoryBackend
*backend
, Error
**errp
)
257 return memory_region_size(&backend
->mr
) ? &backend
->mr
: NULL
;
261 host_memory_backend_memory_complete(UserCreatable
*uc
, Error
**errp
)
263 HostMemoryBackend
*backend
= MEMORY_BACKEND(uc
);
264 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_GET_CLASS(uc
);
265 Error
*local_err
= NULL
;
270 bc
->alloc(backend
, &local_err
);
272 error_propagate(errp
, local_err
);
276 ptr
= memory_region_get_ram_ptr(&backend
->mr
);
277 sz
= memory_region_size(&backend
->mr
);
279 if (backend
->merge
) {
280 qemu_madvise(ptr
, sz
, QEMU_MADV_MERGEABLE
);
282 if (!backend
->dump
) {
283 qemu_madvise(ptr
, sz
, QEMU_MADV_DONTDUMP
);
286 unsigned long lastbit
= find_last_bit(backend
->host_nodes
, MAX_NODES
);
287 /* lastbit == MAX_NODES means maxnode = 0 */
288 unsigned long maxnode
= (lastbit
+ 1) % (MAX_NODES
+ 1);
289 /* ensure policy won't be ignored in case memory is preallocated
290 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
291 * this doesn't catch hugepage case. */
292 unsigned flags
= MPOL_MF_STRICT
| MPOL_MF_MOVE
;
294 /* check for invalid host-nodes and policies and give more verbose
295 * error messages than mbind(). */
296 if (maxnode
&& backend
->policy
== MPOL_DEFAULT
) {
297 error_setg(errp
, "host-nodes must be empty for policy default,"
298 " or you should explicitly specify a policy other"
301 } else if (maxnode
== 0 && backend
->policy
!= MPOL_DEFAULT
) {
302 error_setg(errp
, "host-nodes must be set for policy %s",
303 HostMemPolicy_lookup
[backend
->policy
]);
307 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
308 * as argument to mbind() due to an old Linux bug (feature?) which
309 * cuts off the last specified node. This means backend->host_nodes
310 * must have MAX_NODES+1 bits available.
312 assert(sizeof(backend
->host_nodes
) >=
313 BITS_TO_LONGS(MAX_NODES
+ 1) * sizeof(unsigned long));
314 assert(maxnode
<= MAX_NODES
);
315 if (mbind(ptr
, sz
, backend
->policy
,
316 maxnode
? backend
->host_nodes
: NULL
, maxnode
+ 1, flags
)) {
317 if (backend
->policy
!= MPOL_DEFAULT
|| errno
!= ENOSYS
) {
318 error_setg_errno(errp
, errno
,
319 "cannot bind memory to host NUMA nodes");
324 /* Preallocate memory after the NUMA policy has been instantiated.
325 * This is necessary to guarantee memory is allocated with
326 * specified NUMA policy in place.
328 if (backend
->prealloc
) {
329 os_mem_prealloc(memory_region_get_fd(&backend
->mr
), ptr
, sz
);
335 host_memory_backend_can_be_deleted(UserCreatable
*uc
, Error
**errp
)
339 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(uc
), errp
);
340 if (memory_region_is_mapped(mr
)) {
348 host_memory_backend_class_init(ObjectClass
*oc
, void *data
)
350 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
352 ucc
->complete
= host_memory_backend_memory_complete
;
353 ucc
->can_be_deleted
= host_memory_backend_can_be_deleted
;
356 static const TypeInfo host_memory_backend_info
= {
357 .name
= TYPE_MEMORY_BACKEND
,
358 .parent
= TYPE_OBJECT
,
360 .class_size
= sizeof(HostMemoryBackendClass
),
361 .class_init
= host_memory_backend_class_init
,
362 .instance_size
= sizeof(HostMemoryBackend
),
363 .instance_init
= host_memory_backend_init
,
364 .interfaces
= (InterfaceInfo
[]) {
365 { TYPE_USER_CREATABLE
},
370 static void register_types(void)
372 type_register_static(&host_memory_backend_info
);
375 type_init(register_types
);