1*4e647fa0SStefano Garzarella /* 2*4e647fa0SStefano Garzarella * QEMU host POSIX shared memory object backend 3*4e647fa0SStefano Garzarella * 4*4e647fa0SStefano Garzarella * Copyright (C) 2024 Red Hat Inc 5*4e647fa0SStefano Garzarella * 6*4e647fa0SStefano Garzarella * Authors: 7*4e647fa0SStefano Garzarella * Stefano Garzarella <sgarzare@redhat.com> 8*4e647fa0SStefano Garzarella * 9*4e647fa0SStefano Garzarella * This work is licensed under the terms of the GNU GPL, version 2 or later. 10*4e647fa0SStefano Garzarella * See the COPYING file in the top-level directory. 11*4e647fa0SStefano Garzarella */ 12*4e647fa0SStefano Garzarella 13*4e647fa0SStefano Garzarella #include "qemu/osdep.h" 14*4e647fa0SStefano Garzarella #include "sysemu/hostmem.h" 15*4e647fa0SStefano Garzarella #include "qapi/error.h" 16*4e647fa0SStefano Garzarella 17*4e647fa0SStefano Garzarella #define TYPE_MEMORY_BACKEND_SHM "memory-backend-shm" 18*4e647fa0SStefano Garzarella 19*4e647fa0SStefano Garzarella OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendShm, MEMORY_BACKEND_SHM) 20*4e647fa0SStefano Garzarella 21*4e647fa0SStefano Garzarella struct HostMemoryBackendShm { 22*4e647fa0SStefano Garzarella HostMemoryBackend parent_obj; 23*4e647fa0SStefano Garzarella }; 24*4e647fa0SStefano Garzarella 25*4e647fa0SStefano Garzarella static bool 26*4e647fa0SStefano Garzarella shm_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 27*4e647fa0SStefano Garzarella { 28*4e647fa0SStefano Garzarella g_autoptr(GString) shm_name = g_string_new(NULL); 29*4e647fa0SStefano Garzarella g_autofree char *backend_name = NULL; 30*4e647fa0SStefano Garzarella uint32_t ram_flags; 31*4e647fa0SStefano Garzarella int fd, oflag; 32*4e647fa0SStefano Garzarella mode_t mode; 33*4e647fa0SStefano Garzarella 34*4e647fa0SStefano Garzarella if (!backend->size) { 35*4e647fa0SStefano Garzarella error_setg(errp, "can't create shm backend with size 0"); 36*4e647fa0SStefano Garzarella return false; 37*4e647fa0SStefano Garzarella } 38*4e647fa0SStefano Garzarella 39*4e647fa0SStefano Garzarella if (!backend->share) { 40*4e647fa0SStefano Garzarella error_setg(errp, "can't create shm backend with `share=off`"); 41*4e647fa0SStefano Garzarella return false; 42*4e647fa0SStefano Garzarella } 43*4e647fa0SStefano Garzarella 44*4e647fa0SStefano Garzarella /* 45*4e647fa0SStefano Garzarella * Let's use `mode = 0` because we don't want other processes to open our 46*4e647fa0SStefano Garzarella * memory unless we share the file descriptor with them. 47*4e647fa0SStefano Garzarella */ 48*4e647fa0SStefano Garzarella mode = 0; 49*4e647fa0SStefano Garzarella oflag = O_RDWR | O_CREAT | O_EXCL; 50*4e647fa0SStefano Garzarella backend_name = host_memory_backend_get_name(backend); 51*4e647fa0SStefano Garzarella 52*4e647fa0SStefano Garzarella /* 53*4e647fa0SStefano Garzarella * Some operating systems allow creating anonymous POSIX shared memory 54*4e647fa0SStefano Garzarella * objects (e.g. FreeBSD provides the SHM_ANON constant), but this is not 55*4e647fa0SStefano Garzarella * defined by POSIX, so let's create a unique name. 56*4e647fa0SStefano Garzarella * 57*4e647fa0SStefano Garzarella * From Linux's shm_open(3) man-page: 58*4e647fa0SStefano Garzarella * For portable use, a shared memory object should be identified 59*4e647fa0SStefano Garzarella * by a name of the form /somename;" 60*4e647fa0SStefano Garzarella */ 61*4e647fa0SStefano Garzarella g_string_printf(shm_name, "/qemu-" FMT_pid "-shm-%s", getpid(), 62*4e647fa0SStefano Garzarella backend_name); 63*4e647fa0SStefano Garzarella 64*4e647fa0SStefano Garzarella fd = shm_open(shm_name->str, oflag, mode); 65*4e647fa0SStefano Garzarella if (fd < 0) { 66*4e647fa0SStefano Garzarella error_setg_errno(errp, errno, 67*4e647fa0SStefano Garzarella "failed to create POSIX shared memory"); 68*4e647fa0SStefano Garzarella return false; 69*4e647fa0SStefano Garzarella } 70*4e647fa0SStefano Garzarella 71*4e647fa0SStefano Garzarella /* 72*4e647fa0SStefano Garzarella * We have the file descriptor, so we no longer need to expose the 73*4e647fa0SStefano Garzarella * POSIX shared memory object. However it will remain allocated as long as 74*4e647fa0SStefano Garzarella * there are file descriptors pointing to it. 75*4e647fa0SStefano Garzarella */ 76*4e647fa0SStefano Garzarella shm_unlink(shm_name->str); 77*4e647fa0SStefano Garzarella 78*4e647fa0SStefano Garzarella if (ftruncate(fd, backend->size) == -1) { 79*4e647fa0SStefano Garzarella error_setg_errno(errp, errno, 80*4e647fa0SStefano Garzarella "failed to resize POSIX shared memory to %" PRIu64, 81*4e647fa0SStefano Garzarella backend->size); 82*4e647fa0SStefano Garzarella close(fd); 83*4e647fa0SStefano Garzarella return false; 84*4e647fa0SStefano Garzarella } 85*4e647fa0SStefano Garzarella 86*4e647fa0SStefano Garzarella ram_flags = RAM_SHARED; 87*4e647fa0SStefano Garzarella ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; 88*4e647fa0SStefano Garzarella 89*4e647fa0SStefano Garzarella return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), 90*4e647fa0SStefano Garzarella backend_name, backend->size, 91*4e647fa0SStefano Garzarella ram_flags, fd, 0, errp); 92*4e647fa0SStefano Garzarella } 93*4e647fa0SStefano Garzarella 94*4e647fa0SStefano Garzarella static void 95*4e647fa0SStefano Garzarella shm_backend_instance_init(Object *obj) 96*4e647fa0SStefano Garzarella { 97*4e647fa0SStefano Garzarella HostMemoryBackendShm *m = MEMORY_BACKEND_SHM(obj); 98*4e647fa0SStefano Garzarella 99*4e647fa0SStefano Garzarella MEMORY_BACKEND(m)->share = true; 100*4e647fa0SStefano Garzarella } 101*4e647fa0SStefano Garzarella 102*4e647fa0SStefano Garzarella static void 103*4e647fa0SStefano Garzarella shm_backend_class_init(ObjectClass *oc, void *data) 104*4e647fa0SStefano Garzarella { 105*4e647fa0SStefano Garzarella HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); 106*4e647fa0SStefano Garzarella 107*4e647fa0SStefano Garzarella bc->alloc = shm_backend_memory_alloc; 108*4e647fa0SStefano Garzarella } 109*4e647fa0SStefano Garzarella 110*4e647fa0SStefano Garzarella static const TypeInfo shm_backend_info = { 111*4e647fa0SStefano Garzarella .name = TYPE_MEMORY_BACKEND_SHM, 112*4e647fa0SStefano Garzarella .parent = TYPE_MEMORY_BACKEND, 113*4e647fa0SStefano Garzarella .instance_init = shm_backend_instance_init, 114*4e647fa0SStefano Garzarella .class_init = shm_backend_class_init, 115*4e647fa0SStefano Garzarella .instance_size = sizeof(HostMemoryBackendShm), 116*4e647fa0SStefano Garzarella }; 117*4e647fa0SStefano Garzarella 118*4e647fa0SStefano Garzarella static void register_types(void) 119*4e647fa0SStefano Garzarella { 120*4e647fa0SStefano Garzarella type_register_static(&shm_backend_info); 121*4e647fa0SStefano Garzarella } 122*4e647fa0SStefano Garzarella 123*4e647fa0SStefano Garzarella type_init(register_types); 124