vhost-user: support any POSIX system (tested on macOS, FreeBSD, OpenBSD)
Subsystems (2)
v2 cover vhost-user: support any POSIX system (tested on macOS, FreeBSD, OpenBSD)
v1: https://patchew.org/QEMU/[email protected]/
v2:
- fixed Author email and little typos in some patches
- added memory-backend-shm (patches 9, 10, 11) [Daniel, David, Markus]
- removed changes to memory-backend-file (ex patch 9)
- used memory-backend-shm in tests/qtest/vhost-user-blk-test (patch 10)
- added test case in tests/qtest/vhost-user-test (patch 11)
The vhost-user protocol is not really Linux-specific, so let's try support QEMU's frontends and backends (including libvhost-user) in any POSIX system with this series. The main use case is to be able to use virtio devices that we don't have built-in in QEMU (e.g. virtiofsd, vhost-user-vsock, etc.) even in non-Linux systems.
The first 5 patches are more like fixes discovered at runtime on macOS or FreeBSD that could go even independently of this series.
Patches 6, 7, and 8 enable building of frontends and backends (including libvhost-user) with associated code changes to succeed in compilation.
Patch 9 adds memory-backend-shm that uses the POSIX shm_open() API to create shared memory which is identified by an fd that can be shared with vhost-user backends. This is useful on those systems (like macOS) where we don't have memfd_create() or special filesystems like "/dev/shm".
Patches 10 and 11 use memory-backend-shm in some vhost-user tests.
Maybe the first 5 patches can go separately, but I only discovered those problems after testing patches 6 - 9, so I have included them in this series for now. Please let me know if you prefer that I send them separately.
I tested this series using vhost-user-blk and QSD on macOS Sonoma 14.4 (aarch64), FreeBSD 14 (x86_64), OpenBSD 7.4 (x86_64), and Fedora 39 (x86_64) in this way:
- Start vhost-user-blk or QSD (same commands for all systems)
vhost-user-blk -s /tmp/vhost.socket \
-b Fedora-Cloud-Base-39-1.5.x86_64.raw
qemu-storage-daemon \
--blockdev file,filename=Fedora-Cloud-Base-39-1.5.x86_64.qcow2,node-name=file \
--blockdev qcow2,file=file,node-name=qcow2 \
--export vhost-user-blk,addr.type=unix,addr.path=/tmp/vhost.socket,id=vub,num-queues=1,node-name=qcow2,writable=on- macOS (aarch64): start QEMU (using hvf accelerator)
qemu-system-aarch64 -smp 2 -cpu host -M virt,accel=hvf,memory-backend=mem \
-drive file=./build/pc-bios/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on \
-device virtio-net-device,netdev=net0 -netdev user,id=net0 \
-device ramfb -device usb-ehci -device usb-kbd \
-object memory-backend-shm,id=mem,size=512M \
-device vhost-user-blk-pci,num-queues=1,disable-legacy=on,chardev=char0 \
-chardev socket,id=char0,path=/tmp/vhost.socket- FreeBSD/OpenBSD (x86_64): start QEMU (no accelerators available)
qemu-system-x86_64 -smp 2 -M q35,memory-backend=mem \
-object memory-backend-shm,id=mem,size="512M" \
-device vhost-user-blk-pci,num-queues=1,chardev=char0 \
-chardev socket,id=char0,path=/tmp/vhost.socket- Fedora (x86_64): start QEMU (using kvm accelerator)
qemu-system-x86_64 -smp 2 -M q35,accel=kvm,memory-backend=mem \
-object memory-backend-shm,size="512M" \
-device vhost-user-blk-pci,num-queues=1,chardev=char0 \
-chardev socket,id=char0,path=/tmp/vhost.socketBranch pushed (and CI started) at https://gitlab.com/sgarzarella/qemu/-/tree/macos-vhost-user?ref_type=heads
Thanks,
Stefano
Stefano Garzarella (11):
libvhost-user: set msg.msg_control to NULL when it is empty
libvhost-user: fail vu_message_write() if sendmsg() is failing
libvhost-user: mask F_INFLIGHT_SHMFD if memfd is not supported
vhost-user-server: don't abort if we can't set fd non-blocking contrib/vhost-user-blk: fix bind() using the right size of the address
vhost-user: enable frontends on any POSIX system
libvhost-user: enable it on any POSIX system contrib/vhost-user-blk: enable it on any POSIX system
hostmem: add a new memory backend based on POSIX shm_open() tests/qtest/vhost-user-blk-test: use memory-backend-shm tests/qtest/vhost-user-test: add a test case for memory-backend-shm
docs/system/devices/vhost-user.rst | 5 +- meson.build | 5 +- qapi/qom.json | 17 ++++ subprojects/libvhost-user/libvhost-user.h | 2 +- backends/hostmem-shm.c | 118 ++++++++++++++++++++++ contrib/vhost-user-blk/vhost-user-blk.c | 23 ++++- hw/net/vhost_net.c | 5 + subprojects/libvhost-user/libvhost-user.c | 76 +++++++++++++- tests/qtest/vhost-user-blk-test.c | 2 +- tests/qtest/vhost-user-test.c | 23 +++++ util/vhost-user-server.c | 6 +- backends/meson.build | 1 + hw/block/Kconfig | 2 +- qemu-options.hx | 10 ++ util/meson.build | 4 +- 15 files changed, 280 insertions(+), 19 deletions(-) create mode 100644 backends/hostmem-shm.c
Signature · 1 line
2.44.0
PATCH v2 1/11 libvhost-user: set msg.msg_control to NULL when it is empty
On some OS (e.g. macOS) sendmsg() returns -1 (errno EINVAL) if the struct msghdr has the field msg_controllen set to 0, but msg_control is not NULL.
--- subprojects/libvhost-user/libvhost-user.c | 1 + 1 file changed, 1 insertion(+)
subprojects/libvhost-user/libvhost-user.c +1 -0
@@ -632,6 +632,7 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)memcpy (CMSG_DATA (cmsg), vmsg->fds, fdsize); }else { msg.msg_controllen =0 ; + msg.msg_control =NULL ; } do {
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v2 01/11] libvhost-user: set msg.msg_control to NULL when it is empty
Quoted 8 lines · Stefano Garzarella
On some OS (e.g. macOS)
sendmsg()returns -1 (errno EINVAL) if thestruct msghdrhas the fieldmsg_controllenset to 0, butmsg_controlis not NULL.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 1 + 1 file changed, 1 insertion(+)
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c +1
-0@@ -632,6 +632,7 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)memcpy (CMSG_DATA (cmsg), vmsg->fds, fdsize); }else { msg.msg_controllen =0 ; + msg.msg_control =NULL ; } do {Signature · 1 line
2.44.0
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH for-9.1 v2 01/11] libvhost-user: set msg.msg_control to NULL when it is empty
In reply to Stefano GarzarellaQuoted 14 lines · Stefano Garzarella
On some OS (e.g. macOS)
sendmsg()returns -1 (errno EINVAL) if thestruct msghdrhas the fieldmsg_controllenset to 0, butmsg_controlis not NULL.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 1 + 1 file changed, 1 insertion(+)subprojects/libvhost-user/libvhost-user.c +0
-0
subprojects/libvhost-user/libvhost-user.c vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) · line 638
Quoted 7 lines
subprojects/libvhost-user/libvhost-user.c +1
-0@@ -632,6 +632,7 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)memcpy (CMSG_DATA (cmsg), vmsg->fds, fdsize); }else { msg.msg_controllen =0 ; + msg.msg_control =NULL ; } do {
Signature · 3 lines
Cheers, David / dhildenb
PATCH v2 2/11 libvhost-user: fail vu_message_write() if sendmsg() is failing
In reply to Stefano GarzarellaIn vu_message_write() we use sendmsg() to send the message header, then a write() to send the payload.
If sendmsg() fails we should avoid sending the payload, since we were unable to send the header.
Discovered before fixing the issue with the previous patch, where sendmsg() failed on macOS due to wrong parameters, but the frontend still sent the payload which the backend incorrectly interpreted as a wrong header.
--- subprojects/libvhost-user/libvhost-user.c | 5 +++++ 1 file changed, 5 insertions(+)
subprojects/libvhost-user/libvhost-user.c +5 -0
@@ -639,6 +639,11 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) rc =sendmsg (conn_fd, &msg,0 ); }while (rc <0 && (errno ==EINTR || errno ==EAGAIN )); +if (rc <=0 ) { +vu_panic (dev,"Error while writing: %s" ,strerror (errno)); +return false; + } +if (vmsg->size) { do { if (vmsg->data) {
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v2 02/11] libvhost-user: fail vu_message_write() if sendmsg() is failing
Quoted 21 lines · Stefano Garzarella
In
vu_message_write()we usesendmsg()to send the message header, then awrite()to send the payload.If
sendmsg()fails we should avoid sending the payload, since we were unable to send the header.Discovered before fixing the issue with the previous patch, where
sendmsg()failed on macOS due to wrong parameters, but the frontend still sent the payload which the backend incorrectly interpreted as a wrong header.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 5 +++++ 1 file changed, 5 insertions(+)subprojects/libvhost-user/libvhost-user.c +0
-0
subprojects/libvhost-user/libvhost-user.c vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) · line 642
Quoted 4 lines
subprojects/libvhost-user/libvhost-user.c +1
-0@@ -639,6 +639,11 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) rc =sendmsg (conn_fd, &msg,0 ); }while (rc <0 && (errno ==EINTR || errno ==EAGAIN )); +if (rc <=0 ) {
Is rejecting a 0 return value correct? Technically, a 0 return means a successful write of 0 bytes - but then again, it is unwise to attempt to send an fd or other auxilliary ddata without at least one regular byte, so we should not be attempting a write of 0 bytes. So I guess this one is okay, although I might have used < instead of <=.
subprojects/libvhost-user/libvhost-user.c vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) · line 645
Quoted 3 lines
subprojects/libvhost-user/libvhost-user.c +3
-0+vu_panic (dev,"Error while writing: %s" ,strerror (errno)); +return false; + }
At any rate, noticing the error is the correct thing to do.
Quoted 7 lines
subprojects/libvhost-user/libvhost-user.c +1
-0+if (vmsg->size) { do { if (vmsg->data) {Signature · 1 line
2.44.0
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH for-9.1 v2 02/11] libvhost-user: fail vu_message_write() if sendmsg() is failing
Quoted 25 lines · Eric Blake
Quoted 24 lines · Stefano Garzarella
In
vu_message_write()we usesendmsg()to send the message header, then awrite()to send the payload.If
sendmsg()fails we should avoid sending the payload, since we were unable to send the header.Discovered before fixing the issue with the previous patch, where
sendmsg()failed on macOS due to wrong parameters, but the frontend still sent the payload which the backend incorrectly interpreted as a wrong header.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 5 +++++ 1 file changed, 5 insertions(+)subprojects/libvhost-user/libvhost-user.c +0
-0@@ -639,6 +639,11 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) rc =sendmsg (conn_fd, &msg,0 ); }while (rc <0 && (errno ==EINTR || errno ==EAGAIN ));
Quoted 7 lines
subprojects/libvhost-user/libvhost-user.c vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) · line 642
Quoted 1 line
subprojects/libvhost-user/libvhost-user.c +1
-0+if (rc <=0 ) {Is rejecting a 0 return value correct? Technically, a 0 return means a successful write of 0 bytes - but then again, it is unwise to attempt to send an fd or other auxilliary ddata without at least one regular byte, so we should not be attempting a write of 0 bytes. So I guess this one is okay, although I might have used < instead of <=.
I was wondering if we could see some partial sendmsg()/write succeeding. Meaning, we transferred some bytes but not all, and we'd actually need to loop ...
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH for-9.1 v2 02/11] libvhost-user: fail vu_message_write() if sendmsg() is failing
Quoted 18 lines · David Hildenbrand
Quoted 17 lines · Eric Blake
Quoted 16 lines · Stefano Garzarella
In
vu_message_write()we usesendmsg()to send the message header, then awrite()to send the payload.If
sendmsg()fails we should avoid sending the payload, since we were unable to send the header.Discovered before fixing the issue with the previous patch, where
sendmsg()failed on macOS due to wrong parameters, but the frontend still sent the payload which the backend incorrectly interpreted as a wrong header.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 5 +++++ 1 file changed, 5 insertions(+)
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +0
-0@@ -639,6 +639,11 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) rc =sendmsg (conn_fd, &msg,0 );Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg) · line 642
Quoted 2 lines
subprojects/libvhost-user/libvhost-user.c +1
-0}while (rc <0 && (errno ==EINTR || errno ==EAGAIN )); +if (rc <=0 ) {Is rejecting a 0 return value correct? Technically, a 0 return means a successful write of 0 bytes - but then again, it is unwise to attempt to send an fd or other auxilliary ddata without at least one regular byte, so we should not be attempting a write of 0 bytes. So I guess this one is okay, although I might have used < instead of <=.
I blindly copied what they already do at the end of the same function. However, your observation is correct. That said they have a sendmsg() to send the header. After this we have a write() loop to send the payload.
Now if sendmsg() returns 0, but we have not sent all the header, what should we do? Try sendmsg() again? For how many times?
Quoted 2 lines
I was wondering if we could see some partial sendmsg()/write succeeding. Meaning, we transferred some bytes but not all, and we'd actually need to loop ...
Yep, true, but I would fix it in another patch/series if you agree.
Thanks,
Stefano
Re: [PATCH for-9.1 v2 02/11] libvhost-user: fail vu_message_write() if sendmsg() is failing
Quoted 6 lines
Quoted 4 lines
I was wondering if we could see some partial sendmsg()/write succeeding. Meaning, we transferred some bytes but not all, and we'd actually need to loop ...
Yep, true, but I would fix it in another patch/series if you agree.
Absolutely.
Signature · 3 lines
Cheers, David / dhildenb
PATCH v2 3/11 libvhost-user: mask F_INFLIGHT_SHMFD if memfd is not supported
In reply to Stefano Garzarellalibvhost-user will panic when receiving VHOST_USER_GET_INFLIGHT_FD message if MFD_ALLOW_SEALING is not defined, since it's not able to create a memfd.
VHOST_USER_GET_INFLIGHT_FD is used only if VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD is negotiated. So, let's mask that feature if the backend is not able to properly handle these messages.
--- subprojects/libvhost-user/libvhost-user.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
subprojects/libvhost-user/libvhost-user.c +10 -0
@@ -1674,6 +1674,16 @@ vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) features |= dev->iface->get_protocol_features (dev); } +/* +* If MFD_ALLOW_SEALING is not defined, we are not able to handle +* VHOST_USER_GET_INFLIGHT_FD messages, since we can't create a memfd. +* Those messages are used only if VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD +* is negotiated. A device implementation can enable it, so let's mask +* it to avoid a runtime panic. +*/ +#ifndef MFD_ALLOW_SEALING + features &= ~(1ULL <<VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD ); +#endif vmsg_set_reply_u64 (vmsg, features);return true; }
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v2 03/11] libvhost-user: mask F_INFLIGHT_SHMFD if memfd is not supported
Quoted 22 lines · Stefano Garzarella
libvhost-user will panic when receiving
VHOST_USER_GET_INFLIGHT_FDmessage ifMFD_ALLOW_SEALINGis not defined, since it's not able to create a memfd.
VHOST_USER_GET_INFLIGHT_FDis used only ifVHOST_USER_PROTOCOL_F_INFLIGHT_SHMFDis negotiated. So, let's mask that feature if the backend is not able to properly handle these messages.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 10 ++++++++++ 1 file changed, 10 insertions(+)subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1674,6 +1674,16 @@ vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) features |= dev->iface->get_protocol_features (dev); }
subprojects/libvhost-user/libvhost-user.c vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) · line 1686
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+/* +* If MFD_ALLOW_SEALING is not defined, we are not able to handle +* VHOST_USER_GET_INFLIGHT_FD messages, since we can't create a memfd. +* Those messages are used only if VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD +* is negotiated. A device implementation can enable it, so let's mask +* it to avoid a runtime panic. +*/ +#ifndef MFD_ALLOW_SEALING + features &= ~(1ULL <<VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD ); +#endif
Masking the feature out of advertisement is obviously correct. But should we also fix the code for handling VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD to return an error to any client that requests it in error when the feature was not advertised, instead of panicking?
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +0
-0vmsg_set_reply_u64 (vmsg, features);return true; }Signature · 1 line
2.44.0
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH for-9.1 v2 03/11] libvhost-user: mask F_INFLIGHT_SHMFD if memfd is not supported
Quoted 23 lines · Eric Blake
Quoted 22 lines · Stefano Garzarella
libvhost-user will panic when receiving
VHOST_USER_GET_INFLIGHT_FDmessage ifMFD_ALLOW_SEALINGis not defined, since it's not able to create a memfd.
VHOST_USER_GET_INFLIGHT_FDis used only ifVHOST_USER_PROTOCOL_F_INFLIGHT_SHMFDis negotiated. So, let's mask that feature if the backend is not able to properly handle these messages.Signed-off-by Stefano Garzarella <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 10 ++++++++++ 1 file changed, 10 insertions(+)subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1674,6 +1674,16 @@ vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) features |= dev->iface->get_protocol_features (dev); }
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) · line 1686
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+/* +* If MFD_ALLOW_SEALING is not defined, we are not able to handle +* VHOST_USER_GET_INFLIGHT_FD messages, since we can't create a memfd. +* Those messages are used only if VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD +* is negotiated. A device implementation can enable it, so let's mask +* it to avoid a runtime panic. +*/ +#ifndef MFD_ALLOW_SEALING + features &= ~(1ULL <<VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD ); +#endif Masking the feature out of advertisement is obviously correct. But should we also fix the code for handling
VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFDto return an error to any client that requests it in error when the feature was not advertised, instead of panicking?
Totally agree!
Do I send a separate patch from this series or include it in this series?
I would do the former because this one is already long enough.
Thanks,
Stefano
PATCH v2 4/11 vhost-user-server: don't abort if we can't set fd non-blocking
In reply to Stefano GarzarellaIn vhost-user-server we set all fd received from the other peer in non-blocking mode. For some of them (e.g. memfd, shm_open, etc.) if we fail, it's not really a problem, because we don't use these fd with blocking operations, but only to map memory.
In these cases a failure is not bad, so let's just report a warning instead of panicking if we fail to set some fd in non-blocking mode.
This for example occurs in macOS where setting shm_open() fd non-blocking is failing (errno: 25).
--- util/vhost-user-server.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
util/vhost-user-server.c +5 -1
@@ -66,7 +66,11 @@ static void vmsg_unblock_fds(VhostUserMsg *vmsg) {int i;for (i =0 ; i < vmsg->fd_num; i++) { -qemu_socket_set_nonblock (vmsg->fds[i]); +int ret =qemu_socket_try_set_nonblock (vmsg->fds[i]); +if (ret) { +warn_report ("Failed to set fd %d nonblock for request %d: %s" , + vmsg->fds[i], vmsg->request,strerror (-ret)); + } } }
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v2 04/11] vhost-user-server: don't abort if we can't set fd non-blocking
Quoted 10 lines · Stefano Garzarella
In vhost-user-server we set all fd received from the other peer in non-blocking mode. For some of them (e.g. memfd,
shm_open, etc.) if we fail, it's not really a problem, because we don't use these fd with blocking operations, but only to map memory.In these cases a failure is not bad, so let's just report a warning instead of panicking if we fail to set some fd in non-blocking mode.
This for example occurs in macOS where setting
shm_open()fd non-blocking is failing (errno: 25).
What is errno 25 on MacOS?
Quoted 11 lines
Signed-off-by Stefano Garzarella <[email protected]>--- util/vhost-user-server.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)util/vhost-user-server.c +0
-0
util/vhost-user-server.c static void vmsg_unblock_fds(VhostUserMsg *vmsg) · line 70
Quoted 6 lines
util/vhost-user-server.c +2
-1@@ -66,7 +66,11 @@ static void vmsg_unblock_fds(VhostUserMsg *vmsg) {int i;for (i =0 ; i < vmsg->fd_num; i++) { -qemu_socket_set_nonblock (vmsg->fds[i]); +int ret =qemu_socket_try_set_nonblock (vmsg->fds[i]); +if (ret) {
Should this be 'if (ret < 0)'?
util/vhost-user-server.c static void vmsg_unblock_fds(VhostUserMsg *vmsg) · line 73
Quoted 3 lines
util/vhost-user-server.c +3
-0+warn_report ("Failed to set fd %d nonblock for request %d: %s" , + vmsg->fds[i], vmsg->request,strerror (-ret)); + }
This now ignores all errors even on pre-existing fds where we NEED non-blocking, rather than just the specific (expected) error we are seeing on MacOS. Should this code be a bit more precise about checking that -ret == EXXX for the expected errno value we are ignoring for the specific fds where non-blocking is not essential?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH for-9.1 v2 04/11] vhost-user-server: don't abort if we can't set fd non-blocking
Quoted 6 lines · Eric Blake
Quoted 5 lines · Stefano Garzarella
In vhost-user-server we set all fd received from the other peer in non-blocking mode. For some of them (e.g. memfd,
shm_open, etc.) if we fail, it's not really a problem, because we don't use these fd with blocking operations, but only to map memory.
Quoted 7 lines
Quoted 5 lines
In these cases a failure is not bad, so let's just report a warning instead of panicking if we fail to set some fd in non-blocking mode.
This for example occurs in macOS where setting
shm_open()fd non-blocking is failing (errno: 25).What is errno 25 on MacOS?
It should be ENOTTY.
I'll add in the commit description.
Quoted 7 lines
Quoted 7 lines
Signed-off-by Stefano Garzarella <[email protected]>--- util/vhost-user-server.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 13 lines
util/vhost-user-server.c static void vmsg_unblock_fds(VhostUserMsg *vmsg) · line 70
Quoted 11 lines
util/vhost-user-server.c +2
-1@@ -66,7 +66,11 @@ static void vmsg_unblock_fds(VhostUserMsg *vmsg) {int i;for (i =0 ; i < vmsg->fd_num; i++) { -qemu_socket_set_nonblock (vmsg->fds[i]); +int ret =qemu_socket_try_set_nonblock (vmsg->fds[i]); +if (ret) {Should this be 'if (ret < 0)'?
I was confused by the assert() in qemu_socket_set_nonblock():
void qemu_socket_set_nonblock(int fd)
{
int f;
f = qemu_socket_try_set_nonblock(fd);
assert(f == 0);
}BTW, I see most of the code checks ret < 0, so I'll fix it.
Quoted 10 lines
util/vhost-user-server.c static void vmsg_unblock_fds(VhostUserMsg *vmsg) · line 73
Quoted 4 lines
util/vhost-user-server.c +3
-0+warn_report ("Failed to set fd %d nonblock for request %d: %s" , + vmsg->fds[i], vmsg->request,strerror (-ret)); + }This now ignores all errors even on pre-existing fds where we NEED non-blocking, rather than just the specific (expected) error we are seeing on MacOS. Should this code be a bit more precise about checking that -ret == EXXX for the expected errno value we are ignoring for the specific fds where non-blocking is not essential?
Good point, maybe I'll just avoid calling vmsg_unblock_fds() when the message is VHOST_USER_ADD_MEM_REG or VHOST_USER_SET_MEM_TABLE.
These should be the cases where carried fds are used for mmap() and so there is no need to mark them non-blocking.
WDYT?
Stefano
PATCH v2 5/11 contrib/vhost-user-blk: fix bind() using the right size of the address
In reply to Stefano GarzarellaOn macOS passing -s /tmp/vhost.socket parameter to the vhost-user-blk application, the bind was done on /tmp/vhost.socke pathname, missing the last character.
This sounds like one of the portability problems described in the unix(7) manpage:
Pathname sockets
When binding a socket to a pathname, a few rules should be observed for maximum portability and ease of coding:
• The pathname in sun_path should be null-terminated.
• The length of the pathname, including the terminating null byte, should not exceed the size of sun_path.
• The addrlen argument that describes the enclosing sockaddr_un structure should have a value of at least:
offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)+1
or, more simply, addrlen can be specified as
sizeof(struct sockaddr_un).
So let's follow the last advice and simplify the code as well.
--- contrib/vhost-user-blk/vhost-user-blk.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
contrib/vhost-user-blk/vhost-user-blk.c +1 -3
@@ -469,7 +469,6 @@ static int unix_sock_new(char *unix_fn) {int sock;struct sockaddr_un un; -size_t len;assert (unix_fn);
@@ -481,10 +480,9 @@ static int unix_sock_new(char *unix_fn) un.sun_family =AF_UNIX ; (void )snprintf (un.sun_path,sizeof (un.sun_path),"%s" , unix_fn); - len =sizeof (un.sun_family) +strlen (un.sun_path); (void )unlink (unix_fn); -if (bind (sock, (struct sockaddr *)&un, len) <0 ) { +if (bind (sock, (struct sockaddr *)&un,sizeof (un)) <0 ) {perror ("bind" ); goto fail; }
Signature · 1 line
2.44.0
PATCH v2 6/11 vhost-user: enable frontends on any POSIX system
In reply to Stefano GarzarellaThe vhost-user protocol is not really Linux-specific so let's enable vhost-user frontends for any POSIX system.
In vhost_net.c we use VHOST_FILE_UNBIND which is defined in a Linux specific header, let's define it for other systems as well.
--- meson.build | 1 - hw/net/vhost_net.c | 5 +++++ hw/block/Kconfig | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-)
meson.build +0 -1
@@ -151,7 +151,6 @@ have_tpm = get_option('tpm') \ # vhost have_vhost_user = get_option('vhost_user') \ - .disable_auto_if(host_os != 'linux') \ .require(host_os != 'windows', error_message: 'vhost-user is not available on Windows').allowed() have_vhost_vdpa = get_option('vhost_vdpa') \
hw/net/vhost_net.c +5 -0
@@ -34,8 +34,13 @@#include "standard-headers/linux/virtio_ring.h" #include "hw/virtio/vhost.h" #include "hw/virtio/virtio-bus.h" +#if defined(__linux__)#include "linux-headers/linux/vhost.h" +#endif +#ifndef VHOST_FILE_UNBIND +#define VHOST_FILE_UNBIND -1 +#endif /* Features supported by host kernel. */ static const int kernel_feature_bits[] = {
hw/block/Kconfig +1 -1
@@ -40,7 +40,7 @@ config VHOST_USER_BLK bool # Only PCI devices are provided for now default y if VIRTIO_PCI - depends on VIRTIO && VHOST_USER && LINUX + depends on VIRTIO && VHOST_USER config SWIM bool
Signature · 1 line
2.44.0
PATCH v2 7/11 libvhost-user: enable it on any POSIX system
In reply to Stefano GarzarellaThe vhost-user protocol is not really Linux-specific so let's enable libvhost-user for any POSIX system.
Compiling it on macOS and FreeBSD some problems came up:
- avoid to include linux/vhost.h which is avaibale only on Linux (vhost_types.h contains many of the things we need)
- macOS doesn't provide sys/endian.h, so let's define them (note: libvhost-user doesn't include qemu's headers, so we can't use use "qemu/bswap.h")
- define eventfd_[write|read] as write/read wrapper when system doesn't provide those (e.g. macOS)
- copy SEAL defines from include/qemu/memfd.h to make the code works on FreeBSD where MFD_ALLOW_SEALING is defined
- define MAP_NORESERVE if it's not defined (e.g. on FreeBSD)
--- meson.build | 2 +- subprojects/libvhost-user/libvhost-user.h | 2 +- subprojects/libvhost-user/libvhost-user.c | 60 +++++++++++++++++++++-- 3 files changed, 59 insertions(+), 5 deletions(-)
meson.build +1 -1
@@ -3194,7 +3194,7 @@ endif config_host_data.set('CONFIG_FDT', fdt.found()) vhost_user = not_found -if host_os == 'linux' and have_vhost_user +if have_vhost_user libvhost_user = subproject('libvhost-user') vhost_user = libvhost_user.get_variable('vhost_user_dep') endif
subprojects/libvhost-user/libvhost-user.h +1 -1
@@ -18,9 +18,9 @@#include <stdbool.h> #include <stddef.h> #include <poll.h> -#include <linux/vhost.h> #include <pthread.h> #include "standard-headers/linux/virtio_ring.h" +#include "standard-headers/linux/vhost_types.h" /* Based on qemu/hw/virtio/vhost-user.c */ #define VHOST_USER_F_PROTOCOL_FEATURES 30
subprojects/libvhost-user/libvhost-user.c +57 -3
80 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v2 8/11 contrib/vhost-user-blk: enable it on any POSIX system
In reply to Stefano GarzarellaLet's make the code more portable by using the "qemu/bswap.h" API and adding defines from block/file-posix.c to support O_DIRECT in other systems (e.g. macOS).
vhost-user-server.c is a dependency, let's enable it for any POSIX system.
--- meson.build | 2 -- contrib/vhost-user-blk/vhost-user-blk.c | 19 +++++++++++++++++-- util/meson.build | 4 +++- 3 files changed, 20 insertions(+), 5 deletions(-)
meson.build +0 -2
@@ -1956,8 +1956,6 @@ has_statx = cc.has_header_symbol('sys/stat.h', 'STATX_BASIC_STATS', prefix: gnu_ has_statx_mnt_id = cc.has_header_symbol('sys/stat.h', 'STATX_MNT_ID', prefix: gnu_source_prefix) have_vhost_user_blk_server = get_option('vhost_user_blk_server') \ - .require(host_os == 'linux', - error_message: 'vhost_user_blk_server requires linux') \ .require(have_vhost_user, error_message: 'vhost_user_blk_server requires vhost-user support') \ .disable_auto_if(not have_tools and not have_system) \
contrib/vhost-user-blk/vhost-user-blk.c +17 -2
@@ -16,6 +16,7 @@*/ #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "standard-headers/linux/virtio_blk.h" #include "libvhost-user-glib.h"
@@ -24,6 +25,20 @@#include <sys/ioctl.h> #endif +/* OS X does not have O_DSYNC */ +#ifndef O_DSYNC +#ifdef O_SYNC +#define O_DSYNC O_SYNC +#elif defined(O_FSYNC ) +#define O_DSYNC O_FSYNC +#endif +#endif + +/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ +#ifndef O_DIRECT +#define O_DIRECT O_DSYNC +#endif +enum {VHOST_USER_BLK_MAX_QUEUES =8 , };
@@ -267,13 +282,13 @@ static int vub_virtio_process_req(VubDev *vdev_blk, req->in = (struct virtio_blk_inhdr *)elem->in_sg[in_num -1 ].iov_base; in_num--; - type =le32toh (req->out->type); + type =le32_to_cpu (req->out->type);switch (type & ~VIRTIO_BLK_T_BARRIER ) {case VIRTIO_BLK_T_IN :case VIRTIO_BLK_T_OUT : {ssize_t ret =0 ;bool is_write = type &VIRTIO_BLK_T_OUT ; - req->sector_num =le64toh (req->out->sector); + req->sector_num =le64_to_cpu (req->out->sector);if (is_write) { ret =vub_writev (req, &elem->out_sg[1 ], out_num); }else {
util/meson.build +3 -1
@@ -113,10 +113,12 @@ if have_block util_ss.add(files('filemonitor-stub.c')) endif if host_os == 'linux' - util_ss.add(files('vhost-user-server.c'), vhost_user) util_ss.add(files('vfio-helpers.c')) util_ss.add(files('chardev_open.c')) endif + if host_os != 'windows' + util_ss.add(files('vhost-user-server.c'), vhost_user) + endif endif if cpu == 'aarch64'
Signature · 1 line
2.44.0
PATCH v2 9/11 hostmem: add a new memory backend based on POSIX shm_open()
In reply to Stefano Garzarellashm_open() creates and opens a new POSIX shared memory object. A POSIX shared memory object allows creating memory backend with an associated file descriptor that can be shared with external processes (e.g. vhost-user).
The new memory-backend-shm can be used as an alternative when memory-backend-memfd is not available (Linux only), since shm_open() should be provided by any POSIX-compliant operating system.
--- docs/system/devices/vhost-user.rst | 5 +- qapi/qom.json | 17 +++++ backends/hostmem-shm.c | 118 +++++++++++++++++++++++++++++ backends/meson.build | 1 + qemu-options.hx | 10 +++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 backends/hostmem-shm.c
docs/system/devices/vhost-user.rst +3 -2
@@ -98,8 +98,9 @@ Shared memory object In order for the daemon to access the VirtIO queues to process the requests it needs access to the guest's address space. This is -achieved via the ``memory-backend-file`` or ``memory-backend-memfd`` -objects. A reference to a file-descriptor which can access this object +achieved via the ``memory-backend-file``, ``memory-backend-memfd``, or +``memory-backend-shm`` objects. +A reference to a file-descriptor which can access this object will be passed via the socket as part of the protocol negotiation. Currently the shared memory object needs to match the size of the main
qapi/qom.json +17 -0
@@ -720,6 +720,19 @@ '*hugetlbsize': 'size', '*seal': 'bool' } } +## +# @MemoryBackendShmProperties: +# +# Properties for memory-backend-shm objects. +# +# The @share boolean option is true by default with shm. +# +# Since: 9.1 +## +{ 'struct': 'MemoryBackendShmProperties', + 'base': 'MemoryBackendProperties', + 'data': { } } + ## # @MemoryBackendEpcProperties: #
@@ -976,6 +989,8 @@ { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', + { 'name': 'memory-backend-shm', + 'if': 'CONFIG_POSIX' }, 'pef-guest', { 'name': 'pr-manager-helper', 'if': 'CONFIG_LINUX' },
@@ -1047,6 +1062,8 @@ 'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, 'memory-backend-ram': 'MemoryBackendProperties', + 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', + 'if': 'CONFIG_POSIX' }, 'pr-manager-helper': { 'type': 'PrManagerHelperProperties', 'if': 'CONFIG_LINUX' }, 'qtest': 'QtestProperties',
backends/hostmem-shm.c new file +118 -0
118 lines not on this page · raw message
backends/meson.build +1 -0
@@ -13,6 +13,7 @@ system_ss.add([files( if host_os != 'windows' system_ss.add(files('rng-random.c')) system_ss.add(files('hostmem-file.c')) + system_ss.add([files('hostmem-shm.c'), rt]) endif if host_os == 'linux' system_ss.add(files('hostmem-memfd.c'))
qemu-options.hx +10 -0
@@ -5230,6 +5230,16 @@ SRST The ``share`` boolean option is on by default with memfd. + ``-object memory-backend-shm,id=id,merge=on|off,dump=on|off,share=on|off,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates a POSIX shared memory backend object, which allows + QEMU to share the memory with an external process (e.g. when + using vhost-user). + + Please refer to ``memory-backend-file`` for a description of the + options. + + The ``share`` boolean option is on by default with shm. + ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v2 09/11] hostmem: add a new memory backend based on POSIX shm_open()
Quoted 5 lines
backends/hostmem-shm.c new file +5
-0+ mode =0 ; + oflag =O_RDWR |O_CREAT |O_EXCL ; + backend_name =host_memory_backend_get_name (backend); + +/*
backends/hostmem-shm.c line 57
Quoted 10 lines
backends/hostmem-shm.c new file +10
-0+* Some operating systems allow creating anonymous POSIX shared memory +* objects (e.g. FreeBSD provides the SHM_ANON constant), but this is not +* defined by POSIX, so let's create a unique name. +* +* From Linux's shm_open(3) man-page: +* For portable use, a shared memory object should be identified +* by a name of the form /somename;" +*/ +g_string_printf (shm_name,"/qemu-" FMT_pid"-shm-%s" ,getpid (), + backend_name);
Hm, shouldn't we just let the user specify a name, and if no name was specified, generate one ourselves?
I'm also not quite sure if "host_memory_backend_get_name()" should be used for the purpose here.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH for-9.1 v2 09/11] hostmem: add a new memory backend based on POSIX shm_open()
Quoted 16 lines · David Hildenbrand
Quoted 5 lines
backends/hostmem-shm.c new file +5
-0+ mode =0 ; + oflag =O_RDWR |O_CREAT |O_EXCL ; + backend_name =host_memory_backend_get_name (backend); + +/* backends/hostmem-shm.c line 57
Quoted 10 lines
backends/hostmem-shm.c new file +10
-0+* Some operating systems allow creating anonymous POSIX shared memory +* objects (e.g. FreeBSD provides the SHM_ANON constant), but this is not +* defined by POSIX, so let's create a unique name. +* +* From Linux's shm_open(3) man-page: +* For portable use, a shared memory object should be identified +* by a name of the form /somename;" +*/ +g_string_printf (shm_name,"/qemu-" FMT_pid"-shm-%s" ,getpid (), + backend_name);
Quoted 1 line
Hm, shouldn't we just let the user specify a name, and if no name was specified, generate one ourselves?
I thought about it and initially did it that way, but then some problems came up so I tried to keep it as simple as possible for the user and for our use case (having an fd associated with memory and sharing it with other processes).
The problems I had were:
- what mode_t to use if the object does not exist and needs to be created?
- exclude O_EXCL if the user passes the name since they may have already created it?
- call shm_unlink() only at object deallocation?
So I thought that for now we only support the "anonymous" mode, and if in the future we have a use case where the user wants to specify the name, we can add it later.
That said, if you think it's already useful from the beginning, I can add the name as an optional parameter.
Quoted 2 lines
I'm also not quite sure if "
host_memory_backend_get_name()" should be used for the purpose here.
What problem do you see? As an alternative I thought of a static counter.
Thanks,
Stefano
Re: [PATCH for-9.1 v2 09/11] hostmem: add a new memory backend based on POSIX shm_open()
Quoted 25 lines · Stefano Garzarella
Quoted 16 lines · David Hildenbrand
Quoted 5 lines
backends/hostmem-shm.c new file +5
-0+ mode =0 ; + oflag =O_RDWR |O_CREAT |O_EXCL ; + backend_name =host_memory_backend_get_name (backend); + +/* backends/hostmem-shm.c line 57
Quoted 10 lines
backends/hostmem-shm.c new file +10
-0+* Some operating systems allow creating anonymous POSIX shared memory +* objects (e.g. FreeBSD provides the SHM_ANON constant), but this is not +* defined by POSIX, so let's create a unique name. +* +* From Linux's shm_open(3) man-page: +* For portable use, a shared memory object should be identified +* by a name of the form /somename;" +*/ +g_string_printf (shm_name,"/qemu-" FMT_pid"-shm-%s" ,getpid (), + backend_name);Quoted 2 lines
Hm, shouldn't we just let the user specify a name, and if no name was specified, generate one ourselves?
I thought about it and initially did it that way, but then some problems came up so I tried to keep it as simple as possible for the user and for our use case (having an fd associated with memory and sharing it with other processes).
Quoted 6 lines
The problems I had were:
- what
mode_tto use if the object does not exist and needs to be created? >
- excludeO_EXCLif the user passes the name since they may have already created it?
I'd handle both like we handle files. But I understand now that you really just want to "simulate" memfd.
Quoted 2 lines
- call
shm_unlink()only at object deallocation?
Right, we don't really do that for ordinary files, they keep existing. We only have the "discard-data" property to free up memory.
For memfd, it just happens "automatically". They cannot be looked up by name, and once the last user closes the fd, it gets destroyed.
Quoted 4 lines
So I thought that for now we only support the "anonymous" mode, and if in the future we have a use case where the user wants to specify the name, we can add it later.
Okay, so for now you really only want an anonymous fd that behaves like a memfd, got it.
Likely we should somehow fail if the "POSIX shared memory object" already exists. Hmm.
Quoted 10 lines
That said, if you think it's already useful from the beginning, I can add the name as an optional parameter.
Quoted 3 lines
I'm also not quite sure if "
host_memory_backend_get_name()" should be used for the purpose here.What problem do you see? As an alternative I thought of a static counter.
If it's really just an "internal UUID", as we'll remove the file using shm_unlink(), then the name in fact shouldn't matter and this would be fine. Correct?
So I assume if we ever want to have non-anonymous fds here, we'd pass in a new property "name", and change the logic how we open/unlink. Makes sense.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH for-9.1 v2 09/11] hostmem: add a new memory backend based on POSIX shm_open()
Quoted 26 lines · David Hildenbrand
Quoted 25 lines · Stefano Garzarella
Quoted 16 lines · David Hildenbrand
Quoted 5 lines
backends/hostmem-shm.c new file +5
-0+ mode =0 ; + oflag =O_RDWR |O_CREAT |O_EXCL ; + backend_name =host_memory_backend_get_name (backend); + +/* backends/hostmem-shm.c line 57
Quoted 10 lines
backends/hostmem-shm.c new file +10
-0+* Some operating systems allow creating anonymous POSIX shared memory +* objects (e.g. FreeBSD provides the SHM_ANON constant), but this is not +* defined by POSIX, so let's create a unique name. +* +* From Linux's shm_open(3) man-page: +* For portable use, a shared memory object should be identified +* by a name of the form /somename;" +*/ +g_string_printf (shm_name,"/qemu-" FMT_pid"-shm-%s" ,getpid (), + backend_name);Quoted 2 lines
Hm, shouldn't we just let the user specify a name, and if no name was specified, generate one ourselves?
I thought about it and initially did it that way, but then some problems came up so I tried to keep it as simple as possible for the user and for our use case (having an fd associated with memory and sharing it with other processes).
Quoted 8 lines
Quoted 6 lines
The problems I had were:
- what
mode_tto use if the object does not exist and needs to be created? >
- excludeO_EXCLif the user passes the name since they may have already created it?I'd handle both like we handle files. But I understand now that you really just want to "simulate" memfd.
Right, maybe I should write that in the commit message and documentation.
Quoted 7 lines
Quoted 3 lines
- call
shm_unlink()only at object deallocation?Right, we don't really do that for ordinary files, they keep existing. We only have the "discard-data" property to free up memory.
For memfd, it just happens "automatically". They cannot be looked up by name, and once the last user closes the fd, it gets destroyed.
Yep, I see.
Quoted 9 lines
Quoted 5 lines
So I thought that for now we only support the "anonymous" mode, and if in the future we have a use case where the user wants to specify the name, we can add it later.
Okay, so for now you really only want an anonymous fd that behaves like a memfd, got it.
Likely we should somehow fail if the "POSIX shared memory object" already exists. Hmm.
O_CREAT | O_EXCL should provide just this behavior. From shm_open(3) manpage:
O_EXCL If O_CREAT was also specified, and a shared memory object
with the given name already exists, return an error. The check for the existence of the object, and its creation if it does not exist, are performed atomically.
Quoted 6 lines
Quoted 6 lines
That said, if you think it's already useful from the beginning, I can add the name as an optional parameter.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
I'm also not quite sure if "
host_memory_backend_get_name()" should be used for the purpose here.What problem do you see? As an alternative I thought of a static counter.
If it's really just an "internal UUID", as we'll remove the file using
shm_unlink(), then the name in fact shouldn't matter and this would be fine. Correct?
Right. It's a name that will only "appear" in the system for a very short time since we call shm_unlink() right after shm_open(). So I just need the unique name to prevent several QEMUs launched at the same time from colliding with the name.
Quoted 2 lines
So I assume if we ever want to have non-anonymous fds here, we'd pass in a new property "name", and change the logic how we open/unlink. Makes sense.
Exactly! I can clarify this in the commit description as well.
Thanks for the helpful comments!
If there is anything I need to change for v3, please tell me ;-)
Stefano
Re: [PATCH for-9.1 v2 09/11] hostmem: add a new memory backend based on POSIX shm_open()
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
So I thought that for now we only support the "anonymous" mode, and if in the future we have a use case where the user wants to specify the name, we can add it later.
Okay, so for now you really only want an anonymous fd that behaves like a memfd, got it.
Likely we should somehow fail if the "POSIX shared memory object" already exists. Hmm.
Quoted 8 lines
O_CREAT | O_EXCLshould provide just this behavior.
Fromshm_open(3)manpage:
O_EXCLIfO_CREATwas also specified, and a shared memory object
with the given name already exists, return an error. The check for the existence of the object, and its creation if it does not exist, are performed atomically.
Cool!
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
That said, if you think it's already useful from the beginning, I can add the name as an optional parameter.
Quoted 3 lines
I'm also not quite sure if "
host_memory_backend_get_name()" should be used for the purpose here.What problem do you see? As an alternative I thought of a static counter.
Quoted 8 lines
Quoted 3 lines
If it's really just an "internal UUID", as we'll remove the file using
shm_unlink(), then the name in fact shouldn't matter and this would be fine. Correct?Right. It's a name that will only "appear" in the system for a very short time since we call
shm_unlink()right aftershm_open(). So I just need the unique name to prevent several QEMUs launched at the same time from colliding with the name.
Okay, essentially emulating tmpfile(), but for weird shmem_open() :)
Quoted 10 lines
Quoted 5 lines
So I assume if we ever want to have non-anonymous fds here, we'd pass in a new property "name", and change the logic how we open/unlink. Makes sense.
Exactly! I can clarify this in the commit description as well.
Thanks for the helpful comments!
If there is anything I need to change for v3, please tell me ;-)
Besides some patch description extension, makes sense to me :)
Signature · 3 lines
Cheers, David / dhildenb
PATCH v2 10/11 tests/qtest/vhost-user-blk-test: use memory-backend-shm
In reply to Stefano Garzarellamemory-backend-memfd is available only on Linux while the new memory-backend-shm can be used on any POSIX-compliant operating system. Let's use it so we can run the test in multiple environments.
--- tests/qtest/vhost-user-blk-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/vhost-user-blk-test.c +1 -1
@@ -906,7 +906,7 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances, vhost_user_blk_bin);g_string_append_printf (cmd_line, -" -object memory-backend-memfd,id=mem,size=256M,share=on " +" -object memory-backend-shm,id=mem,size=256M,share=on " " -M memory-backend=mem -m 256M " );for (i =0 ; i < vus_instances; i++) {
Signature · 1 line
2.44.0
PATCH v2 11/11 tests/qtest/vhost-user-test: add a test case for memory-backend-shm
In reply to Stefano Garzarellamemory-backend-shm can be used with vhost-user devices, so let's add a new test case for it.
--- tests/qtest/vhost-user-test.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tests/qtest/vhost-user-test.c +23 -0
@@ -44,6 +44,8 @@ "mem-path=%s,share=on -numa node,memdev=mem"#define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \ " -numa node,memdev=mem" +#define QEMU_CMD_SHM " -m %d -object memory-backend-shm,id=mem,size=%dM," \ + " -numa node,memdev=mem"#define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"#define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce=on"
@@ -195,6 +197,7 @@ enum test_memfd {TEST_MEMFD_AUTO ,TEST_MEMFD_YES ,TEST_MEMFD_NO , + TEST_MEMFD_SHM, }; static void append_vhost_net_opts(TestServer *s, GString *cmd_line,
@@ -228,6 +231,8 @@ static void append_mem_opts(TestServer *server, GString *cmd_line,if (memfd ==TEST_MEMFD_YES ) {g_string_append_printf (cmd_line,QEMU_CMD_MEMFD , size, size); + }else if (memfd ==TEST_MEMFD_SHM ) { +g_string_append_printf (cmd_line,QEMU_CMD_SHM , size, size); }else {const char *root =init_hugepagefs () ? : server->tmpfs;
@@ -788,6 +793,19 @@ static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg)return server; } +static void *vhost_user_test_setup_shm (GString *cmd_line,void *arg) +{ +TestServer *server =test_server_new ("vhost-user-test" , arg); +test_server_listen (server); + +append_mem_opts (server, cmd_line,256 ,TEST_MEMFD_SHM ); + server->vu_ops->append_opts (server, cmd_line,"" ); + +g_test_queue_destroy (vhost_user_test_cleanup, server); + +return server; +} +static void test_read_guest_mem (void *obj,void *arg,QGuestAllocator *alloc) {TestServer *server = arg;
@@ -1081,6 +1099,11 @@ static void register_vhost_user_test(void)"virtio-net" , test_read_guest_mem, &opts); + opts.before = vhost_user_test_setup_shm; +qos_add_test ("vhost-user/read-guest-mem/shm" , +"virtio-net" , + test_read_guest_mem, &opts); +if (qemu_memfd_check (MFD_ALLOW_SEALING )) { opts.before = vhost_user_test_setup_memfd; qos_add_test("vhost-user/read-guest-mem/memfd",
Signature · 1 line
2.44.0