Skip to content

Commit 810033b

Browse files
elmarcomstsirkin
authored andcommitted
libvhost-user: replace qemu/memfd.h usage
Undo the damage from commit 5f9ff1e ("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 <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Message-Id: <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 0ca2931 commit 810033b

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

contrib/libvhost-user/libvhost-user.c

+43-7
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
#endif
4343

4444
#include "qemu/atomic.h"
45-
#include "qemu/osdep.h"
46-
#include "qemu/memfd.h"
4745

4846
#include "libvhost-user.h"
4947

@@ -1615,11 +1613,45 @@ vu_inflight_queue_size(uint16_t queue_size)
16151613
sizeof(uint16_t), INFLIGHT_ALIGNMENT);
16161614
}
16171615

1616+
#ifdef MFD_ALLOW_SEALING
1617+
static void *
1618+
memfd_alloc(const char *name, size_t size, unsigned int flags, int *fd)
1619+
{
1620+
void *ptr;
1621+
int ret;
1622+
1623+
*fd = memfd_create(name, MFD_ALLOW_SEALING);
1624+
if (*fd < 0) {
1625+
return NULL;
1626+
}
1627+
1628+
ret = ftruncate(*fd, size);
1629+
if (ret < 0) {
1630+
close(*fd);
1631+
return NULL;
1632+
}
1633+
1634+
ret = fcntl(*fd, F_ADD_SEALS, flags);
1635+
if (ret < 0) {
1636+
close(*fd);
1637+
return NULL;
1638+
}
1639+
1640+
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
1641+
if (ptr == MAP_FAILED) {
1642+
close(*fd);
1643+
return NULL;
1644+
}
1645+
1646+
return ptr;
1647+
}
1648+
#endif
1649+
16181650
static bool
16191651
vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
16201652
{
1621-
int fd;
1622-
void *addr;
1653+
int fd = -1;
1654+
void *addr = NULL;
16231655
uint64_t mmap_size;
16241656
uint16_t num_queues, queue_size;
16251657

@@ -1637,9 +1669,13 @@ vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
16371669

16381670
mmap_size = vu_inflight_queue_size(queue_size) * num_queues;
16391671

1640-
addr = qemu_memfd_alloc("vhost-inflight", mmap_size,
1641-
F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
1642-
&fd, NULL);
1672+
#ifdef MFD_ALLOW_SEALING
1673+
addr = memfd_alloc("vhost-inflight", mmap_size,
1674+
F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
1675+
&fd);
1676+
#else
1677+
vu_panic(dev, "Not implemented: memfd support is missing");
1678+
#endif
16431679

16441680
if (!addr) {
16451681
vu_panic(dev, "Failed to alloc vhost inflight area");

0 commit comments

Comments
 (0)