[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 45/66] libvhost-user: replace qemu/memfd.h usage
From: |
Michael S. Tsirkin |
Subject: |
[PULL 45/66] libvhost-user: replace qemu/memfd.h usage |
Date: |
Tue, 8 Dec 2020 14:36:21 -0500 |
From: Marc-André Lureau <marcandre.lureau@redhat.com>
Undo the damage from commit 5f9ff1eff3 ("libvhost-user: Support tracking
inflight I/O in shared memory") which introduced glib dependency through
osdep.h inclusion.
libvhost-user.c tries to stay free from glib usage.
Use glibc memfd_create directly when available (assumed so when
MFD_ALLOW_SEALING is defined). A following commit will make the project
standalone and check for memfd API at configure time, instead of a
panic at runtime.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20201125100640.366523-3-marcandre.lureau@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
contrib/libvhost-user/libvhost-user.c | 50 +++++++++++++++++++++++----
1 file changed, 43 insertions(+), 7 deletions(-)
diff --git a/contrib/libvhost-user/libvhost-user.c
b/contrib/libvhost-user/libvhost-user.c
index 1c1cfbf1e7..54aabd1878 100644
--- a/contrib/libvhost-user/libvhost-user.c
+++ b/contrib/libvhost-user/libvhost-user.c
@@ -42,8 +42,6 @@
#endif
#include "qemu/atomic.h"
-#include "qemu/osdep.h"
-#include "qemu/memfd.h"
#include "libvhost-user.h"
@@ -1615,11 +1613,45 @@ vu_inflight_queue_size(uint16_t queue_size)
sizeof(uint16_t), INFLIGHT_ALIGNMENT);
}
+#ifdef MFD_ALLOW_SEALING
+static void *
+memfd_alloc(const char *name, size_t size, unsigned int flags, int *fd)
+{
+ void *ptr;
+ int ret;
+
+ *fd = memfd_create(name, MFD_ALLOW_SEALING);
+ if (*fd < 0) {
+ return NULL;
+ }
+
+ ret = ftruncate(*fd, size);
+ if (ret < 0) {
+ close(*fd);
+ return NULL;
+ }
+
+ ret = fcntl(*fd, F_ADD_SEALS, flags);
+ if (ret < 0) {
+ close(*fd);
+ return NULL;
+ }
+
+ ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
+ if (ptr == MAP_FAILED) {
+ close(*fd);
+ return NULL;
+ }
+
+ return ptr;
+}
+#endif
+
static bool
vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
{
- int fd;
- void *addr;
+ int fd = -1;
+ void *addr = NULL;
uint64_t mmap_size;
uint16_t num_queues, queue_size;
@@ -1637,9 +1669,13 @@ vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
mmap_size = vu_inflight_queue_size(queue_size) * num_queues;
- addr = qemu_memfd_alloc("vhost-inflight", mmap_size,
- F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
- &fd, NULL);
+#ifdef MFD_ALLOW_SEALING
+ addr = memfd_alloc("vhost-inflight", mmap_size,
+ F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
+ &fd);
+#else
+ vu_panic(dev, "Not implemented: memfd support is missing");
+#endif
if (!addr) {
vu_panic(dev, "Failed to alloc vhost inflight area");
--
MST
- [PULL 37/66] failover: remove failover_find_primary_device() error parameter, (continued)
- [PULL 37/66] failover: remove failover_find_primary_device() error parameter, Michael S. Tsirkin, 2020/12/08
- [PULL 36/66] failover: make sure that id always exist, Michael S. Tsirkin, 2020/12/08
- [PULL 35/66] failover: simplify qdev_device_add(), Michael S. Tsirkin, 2020/12/08
- [PULL 38/66] failover: split failover_find_primary_device_id(), Michael S. Tsirkin, 2020/12/08
- [PULL 39/66] failover: We don't need to cache primary_device_id anymore, Michael S. Tsirkin, 2020/12/08
- [PULL 40/66] failover: Caller of this two functions already have primary_dev, Michael S. Tsirkin, 2020/12/08
- [PULL 42/66] failover: Remove primary_dev member, Michael S. Tsirkin, 2020/12/08
- [PULL 43/66] hw: add compat machines for 6.0, Michael S. Tsirkin, 2020/12/08
- [PULL 44/66] libvhost-user: replace qemu/bswap.h with glibc endian.h, Michael S. Tsirkin, 2020/12/08
- [PULL 47/66] libvhost-user: drop qemu/osdep.h dependency, Michael S. Tsirkin, 2020/12/08
- [PULL 45/66] libvhost-user: replace qemu/memfd.h usage,
Michael S. Tsirkin <=
- [PULL 46/66] libvhost-user: remove qemu/compiler.h usage, Michael S. Tsirkin, 2020/12/08
- [PULL 48/66] libvhost-user: make it a meson subproject, Michael S. Tsirkin, 2020/12/08
- [PULL 50/66] libvhost-user: add a simple link test without glib, Michael S. Tsirkin, 2020/12/08
- [PULL 51/66] .gitlab-ci: add build-libvhost-user, Michael S. Tsirkin, 2020/12/08
- [PULL 52/66] contrib/vhost-user-blk: avoid g_return_val_if() input validation, Michael S. Tsirkin, 2020/12/08
- [PULL 49/66] libvhost-user: check memfd API, Michael S. Tsirkin, 2020/12/08
- [PULL 53/66] contrib/vhost-user-gpu: avoid g_return_val_if() input validation, Michael S. Tsirkin, 2020/12/08
- [PULL 54/66] contrib/vhost-user-input: avoid g_return_val_if() input validation, Michael S. Tsirkin, 2020/12/08
- [PULL 55/66] block/export: avoid g_return_val_if() input validation, Michael S. Tsirkin, 2020/12/08
- [PULL 58/66] x86: acpi: introduce AcpiPmInfo::smi_on_cpu_unplug, Michael S. Tsirkin, 2020/12/08