1 /* 2 * QEMU host memfd memory backend 3 * 4 * Copyright (C) 2018 Red Hat Inc 5 * 6 * Authors: 7 * Marc-André Lureau <marcandre.lureau@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 "qom/object_interfaces.h" 17 #include "qemu/memfd.h" 18 #include "qemu/module.h" 19 #include "qapi/error.h" 20 #include "qom/object.h" 21 22 #define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" 23 24 typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd; 25 DECLARE_INSTANCE_CHECKER(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD, 26 TYPE_MEMORY_BACKEND_MEMFD) 27 28 29 struct HostMemoryBackendMemfd { 30 HostMemoryBackend parent_obj; 31 32 bool hugetlb; 33 uint64_t hugetlbsize; 34 bool seal; 35 }; 36 37 static void 38 memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 39 { 40 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend); 41 char *name; 42 int fd; 43 44 if (!backend->size) { 45 error_setg(errp, "can't create backend with size 0"); 46 return; 47 } 48 49 fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size, 50 m->hugetlb, m->hugetlbsize, m->seal ? 51 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0, 52 errp); 53 if (fd == -1) { 54 return; 55 } 56 57 name = host_memory_backend_get_name(backend); 58 memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), 59 name, backend->size, 60 backend->share, fd, errp); 61 g_free(name); 62 } 63 64 static bool 65 memfd_backend_get_hugetlb(Object *o, Error **errp) 66 { 67 return MEMORY_BACKEND_MEMFD(o)->hugetlb; 68 } 69 70 static void 71 memfd_backend_set_hugetlb(Object *o, bool value, Error **errp) 72 { 73 MEMORY_BACKEND_MEMFD(o)->hugetlb = value; 74 } 75 76 static void 77 memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name, 78 void *opaque, Error **errp) 79 { 80 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj); 81 uint64_t value; 82 83 if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) { 84 error_setg(errp, "cannot change property value"); 85 return; 86 } 87 88 if (!visit_type_size(v, name, &value, errp)) { 89 return; 90 } 91 if (!value) { 92 error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu64 "'", 93 object_get_typename(obj), name, value); 94 return; 95 } 96 m->hugetlbsize = value; 97 } 98 99 static void 100 memfd_backend_get_hugetlbsize(Object *obj, Visitor *v, const char *name, 101 void *opaque, Error **errp) 102 { 103 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj); 104 uint64_t value = m->hugetlbsize; 105 106 visit_type_size(v, name, &value, errp); 107 } 108 109 static bool 110 memfd_backend_get_seal(Object *o, Error **errp) 111 { 112 return MEMORY_BACKEND_MEMFD(o)->seal; 113 } 114 115 static void 116 memfd_backend_set_seal(Object *o, bool value, Error **errp) 117 { 118 MEMORY_BACKEND_MEMFD(o)->seal = value; 119 } 120 121 static void 122 memfd_backend_instance_init(Object *obj) 123 { 124 HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj); 125 126 /* default to sealed file */ 127 m->seal = true; 128 MEMORY_BACKEND(m)->share = true; 129 } 130 131 static void 132 memfd_backend_class_init(ObjectClass *oc, void *data) 133 { 134 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); 135 136 bc->alloc = memfd_backend_memory_alloc; 137 138 if (qemu_memfd_check(MFD_HUGETLB)) { 139 object_class_property_add_bool(oc, "hugetlb", 140 memfd_backend_get_hugetlb, 141 memfd_backend_set_hugetlb); 142 object_class_property_set_description(oc, "hugetlb", 143 "Use huge pages"); 144 object_class_property_add(oc, "hugetlbsize", "int", 145 memfd_backend_get_hugetlbsize, 146 memfd_backend_set_hugetlbsize, 147 NULL, NULL); 148 object_class_property_set_description(oc, "hugetlbsize", 149 "Huge pages size (ex: 2M, 1G)"); 150 } 151 object_class_property_add_bool(oc, "seal", 152 memfd_backend_get_seal, 153 memfd_backend_set_seal); 154 object_class_property_set_description(oc, "seal", 155 "Seal growing & shrinking"); 156 } 157 158 static const TypeInfo memfd_backend_info = { 159 .name = TYPE_MEMORY_BACKEND_MEMFD, 160 .parent = TYPE_MEMORY_BACKEND, 161 .instance_init = memfd_backend_instance_init, 162 .class_init = memfd_backend_class_init, 163 .instance_size = sizeof(HostMemoryBackendMemfd), 164 }; 165 166 static void register_types(void) 167 { 168 if (qemu_memfd_check(MFD_ALLOW_SEALING)) { 169 type_register_static(&memfd_backend_info); 170 } 171 } 172 173 type_init(register_types); 174