xref: /openbmc/qemu/backends/hostmem.c (revision c00506aa)
1 /*
2  * QEMU Host Memory Backend
3  *
4  * Copyright (C) 2013-2014 Red Hat Inc
5  *
6  * Authors:
7  *   Igor Mammedov <imammedo@redhat.com>
8  *
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.
11  */
12 
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"
23 
24 #ifdef CONFIG_NUMA
25 #include <numaif.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);
30 #endif
31 
32 char *
33 host_memory_backend_get_name(HostMemoryBackend *backend)
34 {
35     if (!backend->use_canonical_path) {
36         return g_strdup(object_get_canonical_path_component(OBJECT(backend)));
37     }
38 
39     return object_get_canonical_path(OBJECT(backend));
40 }
41 
42 static void
43 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
44                              void *opaque, Error **errp)
45 {
46     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
47     uint64_t value = backend->size;
48 
49     visit_type_size(v, name, &value, errp);
50 }
51 
52 static void
53 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
54                              void *opaque, Error **errp)
55 {
56     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
57     uint64_t value;
58 
59     if (host_memory_backend_mr_inited(backend)) {
60         error_setg(errp, "cannot change property %s of %s ", name,
61                    object_get_typename(obj));
62         return;
63     }
64 
65     if (!visit_type_size(v, name, &value, errp)) {
66         return;
67     }
68     if (!value) {
69         error_setg(errp,
70                    "property '%s' of %s doesn't take value '%" PRIu64 "'",
71                    name, object_get_typename(obj), value);
72         return;
73     }
74     backend->size = value;
75 }
76 
77 static void
78 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
79                                    void *opaque, Error **errp)
80 {
81     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
82     uint16List *host_nodes = NULL;
83     uint16List **node = &host_nodes;
84     unsigned long value;
85 
86     value = find_first_bit(backend->host_nodes, MAX_NODES);
87     if (value == MAX_NODES) {
88         goto ret;
89     }
90 
91     *node = g_malloc0(sizeof(**node));
92     (*node)->value = value;
93     node = &(*node)->next;
94 
95     do {
96         value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
97         if (value == MAX_NODES) {
98             break;
99         }
100 
101         *node = g_malloc0(sizeof(**node));
102         (*node)->value = value;
103         node = &(*node)->next;
104     } while (true);
105 
106 ret:
107     visit_type_uint16List(v, name, &host_nodes, errp);
108     qapi_free_uint16List(host_nodes);
109 }
110 
111 static void
112 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
113                                    void *opaque, Error **errp)
114 {
115 #ifdef CONFIG_NUMA
116     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
117     uint16List *l, *host_nodes = NULL;
118 
119     visit_type_uint16List(v, name, &host_nodes, errp);
120 
121     for (l = host_nodes; l; l = l->next) {
122         if (l->value >= MAX_NODES) {
123             error_setg(errp, "Invalid host-nodes value: %d", l->value);
124             goto out;
125         }
126     }
127 
128     for (l = host_nodes; l; l = l->next) {
129         bitmap_set(backend->host_nodes, l->value, 1);
130     }
131 
132 out:
133     qapi_free_uint16List(host_nodes);
134 #else
135     error_setg(errp, "NUMA node binding are not supported by this QEMU");
136 #endif
137 }
138 
139 static int
140 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
141 {
142     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
143     return backend->policy;
144 }
145 
146 static void
147 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
148 {
149     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
150     backend->policy = policy;
151 
152 #ifndef CONFIG_NUMA
153     if (policy != HOST_MEM_POLICY_DEFAULT) {
154         error_setg(errp, "NUMA policies are not supported by this QEMU");
155     }
156 #endif
157 }
158 
159 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
160 {
161     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
162 
163     return backend->merge;
164 }
165 
166 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
167 {
168     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
169 
170     if (!host_memory_backend_mr_inited(backend)) {
171         backend->merge = value;
172         return;
173     }
174 
175     if (value != backend->merge) {
176         void *ptr = memory_region_get_ram_ptr(&backend->mr);
177         uint64_t sz = memory_region_size(&backend->mr);
178 
179         qemu_madvise(ptr, sz,
180                      value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
181         backend->merge = value;
182     }
183 }
184 
185 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
186 {
187     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
188 
189     return backend->dump;
190 }
191 
192 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
193 {
194     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
195 
196     if (!host_memory_backend_mr_inited(backend)) {
197         backend->dump = value;
198         return;
199     }
200 
201     if (value != backend->dump) {
202         void *ptr = memory_region_get_ram_ptr(&backend->mr);
203         uint64_t sz = memory_region_size(&backend->mr);
204 
205         qemu_madvise(ptr, sz,
206                      value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
207         backend->dump = value;
208     }
209 }
210 
211 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
212 {
213     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
214 
215     return backend->prealloc;
216 }
217 
218 static void host_memory_backend_set_prealloc(Object *obj, bool value,
219                                              Error **errp)
220 {
221     Error *local_err = NULL;
222     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
223 
224     if (!host_memory_backend_mr_inited(backend)) {
225         backend->prealloc = value;
226         return;
227     }
228 
229     if (value && !backend->prealloc) {
230         int fd = memory_region_get_fd(&backend->mr);
231         void *ptr = memory_region_get_ram_ptr(&backend->mr);
232         uint64_t sz = memory_region_size(&backend->mr);
233 
234         os_mem_prealloc(fd, ptr, sz, backend->prealloc_threads, &local_err);
235         if (local_err) {
236             error_propagate(errp, local_err);
237             return;
238         }
239         backend->prealloc = true;
240     }
241 }
242 
243 static void host_memory_backend_get_prealloc_threads(Object *obj, Visitor *v,
244     const char *name, void *opaque, Error **errp)
245 {
246     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
247     visit_type_uint32(v, name, &backend->prealloc_threads, errp);
248 }
249 
250 static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
251     const char *name, void *opaque, Error **errp)
252 {
253     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
254     uint32_t value;
255 
256     if (!visit_type_uint32(v, name, &value, errp)) {
257         return;
258     }
259     if (value <= 0) {
260         error_setg(errp, "property '%s' of %s doesn't take value '%d'", name,
261                    object_get_typename(obj), value);
262         return;
263     }
264     backend->prealloc_threads = value;
265 }
266 
267 static void host_memory_backend_init(Object *obj)
268 {
269     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
270     MachineState *machine = MACHINE(qdev_get_machine());
271 
272     /* TODO: convert access to globals to compat properties */
273     backend->merge = machine_mem_merge(machine);
274     backend->dump = machine_dump_guest_core(machine);
275     backend->prealloc_threads = 1;
276 }
277 
278 static void host_memory_backend_post_init(Object *obj)
279 {
280     object_apply_compat_props(obj);
281 }
282 
283 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
284 {
285     /*
286      * NOTE: We forbid zero-length memory backend, so here zero means
287      * "we haven't inited the backend memory region yet".
288      */
289     return memory_region_size(&backend->mr) != 0;
290 }
291 
292 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
293 {
294     return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
295 }
296 
297 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
298 {
299     backend->is_mapped = mapped;
300 }
301 
302 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
303 {
304     return backend->is_mapped;
305 }
306 
307 #ifdef __linux__
308 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
309 {
310     Object *obj = OBJECT(memdev);
311     char *path = object_property_get_str(obj, "mem-path", NULL);
312     size_t pagesize = qemu_mempath_getpagesize(path);
313 
314     g_free(path);
315     return pagesize;
316 }
317 #else
318 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
319 {
320     return qemu_real_host_page_size;
321 }
322 #endif
323 
324 static void
325 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
326 {
327     HostMemoryBackend *backend = MEMORY_BACKEND(uc);
328     HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
329     Error *local_err = NULL;
330     void *ptr;
331     uint64_t sz;
332 
333     if (bc->alloc) {
334         bc->alloc(backend, &local_err);
335         if (local_err) {
336             goto out;
337         }
338 
339         ptr = memory_region_get_ram_ptr(&backend->mr);
340         sz = memory_region_size(&backend->mr);
341 
342         if (backend->merge) {
343             qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
344         }
345         if (!backend->dump) {
346             qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
347         }
348 #ifdef CONFIG_NUMA
349         unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
350         /* lastbit == MAX_NODES means maxnode = 0 */
351         unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
352         /* ensure policy won't be ignored in case memory is preallocated
353          * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
354          * this doesn't catch hugepage case. */
355         unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
356 
357         /* check for invalid host-nodes and policies and give more verbose
358          * error messages than mbind(). */
359         if (maxnode && backend->policy == MPOL_DEFAULT) {
360             error_setg(errp, "host-nodes must be empty for policy default,"
361                        " or you should explicitly specify a policy other"
362                        " than default");
363             return;
364         } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
365             error_setg(errp, "host-nodes must be set for policy %s",
366                        HostMemPolicy_str(backend->policy));
367             return;
368         }
369 
370         /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
371          * as argument to mbind() due to an old Linux bug (feature?) which
372          * cuts off the last specified node. This means backend->host_nodes
373          * must have MAX_NODES+1 bits available.
374          */
375         assert(sizeof(backend->host_nodes) >=
376                BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
377         assert(maxnode <= MAX_NODES);
378 
379         if (maxnode &&
380             mbind(ptr, sz, backend->policy, backend->host_nodes, maxnode + 1,
381                   flags)) {
382             if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
383                 error_setg_errno(errp, errno,
384                                  "cannot bind memory to host NUMA nodes");
385                 return;
386             }
387         }
388 #endif
389         /* Preallocate memory after the NUMA policy has been instantiated.
390          * This is necessary to guarantee memory is allocated with
391          * specified NUMA policy in place.
392          */
393         if (backend->prealloc) {
394             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
395                             backend->prealloc_threads, &local_err);
396             if (local_err) {
397                 goto out;
398             }
399         }
400     }
401 out:
402     error_propagate(errp, local_err);
403 }
404 
405 static bool
406 host_memory_backend_can_be_deleted(UserCreatable *uc)
407 {
408     if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
409         return false;
410     } else {
411         return true;
412     }
413 }
414 
415 static bool host_memory_backend_get_share(Object *o, Error **errp)
416 {
417     HostMemoryBackend *backend = MEMORY_BACKEND(o);
418 
419     return backend->share;
420 }
421 
422 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
423 {
424     HostMemoryBackend *backend = MEMORY_BACKEND(o);
425 
426     if (host_memory_backend_mr_inited(backend)) {
427         error_setg(errp, "cannot change property value");
428         return;
429     }
430     backend->share = value;
431 }
432 
433 static bool
434 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
435 {
436     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
437 
438     return backend->use_canonical_path;
439 }
440 
441 static void
442 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
443                                            Error **errp)
444 {
445     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
446 
447     backend->use_canonical_path = value;
448 }
449 
450 static void
451 host_memory_backend_class_init(ObjectClass *oc, void *data)
452 {
453     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
454 
455     ucc->complete = host_memory_backend_memory_complete;
456     ucc->can_be_deleted = host_memory_backend_can_be_deleted;
457 
458     object_class_property_add_bool(oc, "merge",
459         host_memory_backend_get_merge,
460         host_memory_backend_set_merge);
461     object_class_property_set_description(oc, "merge",
462         "Mark memory as mergeable");
463     object_class_property_add_bool(oc, "dump",
464         host_memory_backend_get_dump,
465         host_memory_backend_set_dump);
466     object_class_property_set_description(oc, "dump",
467         "Set to 'off' to exclude from core dump");
468     object_class_property_add_bool(oc, "prealloc",
469         host_memory_backend_get_prealloc,
470         host_memory_backend_set_prealloc);
471     object_class_property_set_description(oc, "prealloc",
472         "Preallocate memory");
473     object_class_property_add(oc, "prealloc-threads", "int",
474         host_memory_backend_get_prealloc_threads,
475         host_memory_backend_set_prealloc_threads,
476         NULL, NULL);
477     object_class_property_set_description(oc, "prealloc-threads",
478         "Number of CPU threads to use for prealloc");
479     object_class_property_add(oc, "size", "int",
480         host_memory_backend_get_size,
481         host_memory_backend_set_size,
482         NULL, NULL);
483     object_class_property_set_description(oc, "size",
484         "Size of the memory region (ex: 500M)");
485     object_class_property_add(oc, "host-nodes", "int",
486         host_memory_backend_get_host_nodes,
487         host_memory_backend_set_host_nodes,
488         NULL, NULL);
489     object_class_property_set_description(oc, "host-nodes",
490         "Binds memory to the list of NUMA host nodes");
491     object_class_property_add_enum(oc, "policy", "HostMemPolicy",
492         &HostMemPolicy_lookup,
493         host_memory_backend_get_policy,
494         host_memory_backend_set_policy);
495     object_class_property_set_description(oc, "policy",
496         "Set the NUMA policy");
497     object_class_property_add_bool(oc, "share",
498         host_memory_backend_get_share, host_memory_backend_set_share);
499     object_class_property_set_description(oc, "share",
500         "Mark the memory as private to QEMU or shared");
501     object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
502         host_memory_backend_get_use_canonical_path,
503         host_memory_backend_set_use_canonical_path);
504 }
505 
506 static const TypeInfo host_memory_backend_info = {
507     .name = TYPE_MEMORY_BACKEND,
508     .parent = TYPE_OBJECT,
509     .abstract = true,
510     .class_size = sizeof(HostMemoryBackendClass),
511     .class_init = host_memory_backend_class_init,
512     .instance_size = sizeof(HostMemoryBackend),
513     .instance_init = host_memory_backend_init,
514     .instance_post_init = host_memory_backend_post_init,
515     .interfaces = (InterfaceInfo[]) {
516         { TYPE_USER_CREATABLE },
517         { }
518     }
519 };
520 
521 static void register_types(void)
522 {
523     type_register_static(&host_memory_backend_info);
524 }
525 
526 type_init(register_types);
527