Skip to contentSkip to conversation

io/channel-socket: move cmsg memset into fd-passing branches

PATCH In review Bin Guo 2 participants 3 messages May 29 – Jun 4

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 io/channel-socket: move cmsg memset into fd-passing branches

In qio_channel_socket_readv() and qio_channel_socket_writev(), the control message buffer is unconditionally zeroed on every call, even when no file descriptors are being transferred. The memset touches CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS) bytes -- typically 80 bytes on x86-64 -- on every I/O operation.

Move the memset into the conditional branches that actually set up msg_control, so the common fast path (no fd passing) avoids the unnecessary zeroing. Also narrow the scope of fdsize and cmsg in writev() to the branch that uses them.

No functional change for paths that transfer file descriptors.

---
 io/channel-socket.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
io/channel-socket.c +5 -6
index 3053b35ad8..d19c73cacc 100644
@@ -562,11 +562,10 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
     int sflags = 0;
 
-    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
-
     msg.msg_iov = (struct iovec *)iov;
     msg.msg_iovlen = niov;
     if (fds && nfds) {
+        memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
         msg.msg_control = control;
         msg.msg_controllen = sizeof(control);
 #ifdef MSG_CMSG_CLOEXEC
@@ -622,20 +621,19 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
     ssize_t ret;
     struct msghdr msg = { NULL, };
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
-    size_t fdsize = sizeof(int) * nfds;
-    struct cmsghdr *cmsg;
     int sflags = 0;
 #ifdef QEMU_MSG_ZEROCOPY
     bool blocking = sioc->blocking;
     bool zerocopy_flushed_once = false;
 #endif
 
-    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
-
     msg.msg_iov = (struct iovec *)iov;
     msg.msg_iovlen = niov;
 
     if (nfds) {
+        size_t fdsize = sizeof(int) * nfds;
+        struct cmsghdr *cmsg;
+
         if (nfds > SOCKET_MAX_FDS) {
             error_setg_errno(errp, EINVAL,
                              "Only %d FDs can be sent, got %zu",
@@ -643,6 +641,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
             return -1;
         }
 
+        memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
         msg.msg_control = control;
         msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 5 lines · Bin Guo

In qio_channel_socket_readv() and qio_channel_socket_writev(), the control message buffer is unconditionally zeroed on every call, even when no file descriptors are being transferred. The memset touches CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS) bytes -- typically 80 bytes on x86-64 -- on every I/O operation.

Did you have a real world scenario where this creates an I/O performance problem, or is this just a optimization to address a theoretical concern ?

Quoted 17 lines

Move the memset into the conditional branches that actually set up msg_control, so the common fast path (no fd passing) avoids the unnecessary zeroing. Also narrow the scope of fdsize and cmsg in writev() to the branch that uses them.

No functional change for paths that transfer file descriptors.

---
 io/channel-socket.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
io/channel-socket.c +0 -0
index 3053b35ad8..d19c73cacc 100644

io/channel-socket.c static ssize_t qio_channel_socket_readv(QIOChannel *ioc, · line 562

Quoted 1 line
io/channel-socket.c +0 -0
@@ -562,11 +562,10 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];

QEMU builds with -ftrivial-auto-var-init=zero always set so this control variable will always be zero-initialized by the compiler making the following memset() pointless/redundant.

AFAICT, the compiler optimization will identify this and remove the memset() call entirely. IOW, I don't believe the change proposed in your patch will have any effect.

If we want to be explicit about this then we should just do

  char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = {};

and remove the memset() from our code entirely.

Quoted 47 lines
io/channel-socket.c +5 -6
     int sflags = 0;
 
-    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
-
     msg.msg_iov = (struct iovec *)iov;
     msg.msg_iovlen = niov;
     if (fds && nfds) {
+        memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
         msg.msg_control = control;
         msg.msg_controllen = sizeof(control);
 #ifdef MSG_CMSG_CLOEXEC
@@ -622,20 +621,19 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
     ssize_t ret;
     struct msghdr msg = { NULL, };
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
-    size_t fdsize = sizeof(int) * nfds;
-    struct cmsghdr *cmsg;
     int sflags = 0;
 #ifdef QEMU_MSG_ZEROCOPY
     bool blocking = sioc->blocking;
     bool zerocopy_flushed_once = false;
 #endif
 
-    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
-
     msg.msg_iov = (struct iovec *)iov;
     msg.msg_iovlen = niov;
 
     if (nfds) {
+        size_t fdsize = sizeof(int) * nfds;
+        struct cmsghdr *cmsg;
+
         if (nfds > SOCKET_MAX_FDS) {
             error_setg_errno(errp, EINVAL,
                              "Only %d FDs can be sent, got %zu",
@@ -643,6 +641,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
             return -1;
         }
 
+        memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
         msg.msg_control = control;
         msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
 
Signature · 1 line
2.50.1 (Apple Git-155)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#2 lore raw
Quoted 3 lines · Daniel P. Berrangé

QEMU builds with -ftrivial-auto-var-init=zero always set so this control variable will always be zero-initialized by the compiler making the following memset() pointless/redundant.

You're right, I missed the -ftrivial-auto-var-init=zero flag in meson.build. The memset() is indeed redundant and the performance rationale in the patch description doesn't hold.

I'll drop this patch.

Thanks for the review.

Bin Guo