thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-QIOChannelSocket-Implement-io_writev-zero-copy-flag-.patch

4841a6
From 5fd7af93a06adaddbae719aabbaf912159f4fb28 Mon Sep 17 00:00:00 2001
719b13
From: Leonardo Bras <leobras@redhat.com>
719b13
Date: Wed, 18 May 2022 02:52:25 -0300
4841a6
Subject: [PATCH 18/37] QIOChannelSocket: Implement io_writev zero copy flag &
719b13
 io_flush for CONFIG_LINUX
719b13
MIME-Version: 1.0
719b13
Content-Type: text/plain; charset=UTF-8
719b13
Content-Transfer-Encoding: 8bit
719b13
719b13
RH-Author: Leonardo Brás <leobras@redhat.com>
4841a6
RH-MergeRequest: 191: MSG_ZEROCOPY + Multifd @ rhel8.7
4841a6
RH-Commit: [18/26] 6f65c8c879a5df57213b541d58285b65178f8547
4841a6
RH-Bugzilla: 2072049
719b13
RH-Acked-by: Peter Xu <peterx@redhat.com>
4841a6
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
4841a6
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
719b13
719b13
For CONFIG_LINUX, implement the new zero copy flag and the optional callback
719b13
io_flush on QIOChannelSocket, but enables it only when MSG_ZEROCOPY
719b13
feature is available in the host kernel, which is checked on
719b13
qio_channel_socket_connect_sync()
719b13
719b13
qio_channel_socket_flush() was implemented by counting how many times
719b13
sendmsg(...,MSG_ZEROCOPY) was successfully called, and then reading the
719b13
socket's error queue, in order to find how many of them finished sending.
719b13
Flush will loop until those counters are the same, or until some error occurs.
719b13
719b13
Notes on using writev() with QIO_CHANNEL_WRITE_FLAG_ZERO_COPY:
719b13
1: Buffer
719b13
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying,
719b13
some caution is necessary to avoid overwriting any buffer before it's sent.
719b13
If something like this happen, a newer version of the buffer may be sent instead.
719b13
- If this is a problem, it's recommended to call qio_channel_flush() before freeing
719b13
or re-using the buffer.
719b13
719b13
2: Locked memory
719b13
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and
719b13
unlocked after it's sent.
719b13
- Depending on the size of each buffer, and how often it's sent, it may require
719b13
a larger amount of locked memory than usually available to non-root user.
719b13
- If the required amount of locked memory is not available, writev_zero_copy
719b13
will return an error, which can abort an operation like migration,
719b13
- Because of this, when an user code wants to add zero copy as a feature, it
719b13
requires a mechanism to disable it, so it can still be accessible to less
719b13
privileged users.
719b13
719b13
Signed-off-by: Leonardo Bras <leobras@redhat.com>
719b13
Reviewed-by: Peter Xu <peterx@redhat.com>
719b13
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
719b13
Reviewed-by: Juan Quintela <quintela@redhat.com>
719b13
Message-Id: <20220513062836.965425-4-leobras@redhat.com>
719b13
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
719b13
(cherry picked from commit 2bc58ffc2926a4efdd03edfb5909861fefc68c3d)
719b13
Signed-off-by: Leonardo Bras <leobras@redhat.com>
719b13
---
719b13
 include/io/channel-socket.h |   2 +
719b13
 io/channel-socket.c         | 116 ++++++++++++++++++++++++++++++++++--
719b13
 2 files changed, 114 insertions(+), 4 deletions(-)
719b13
719b13
diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
719b13
index e747e63514..513c428fe4 100644
719b13
--- a/include/io/channel-socket.h
719b13
+++ b/include/io/channel-socket.h
719b13
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
719b13
     socklen_t localAddrLen;
719b13
     struct sockaddr_storage remoteAddr;
719b13
     socklen_t remoteAddrLen;
719b13
+    ssize_t zero_copy_queued;
719b13
+    ssize_t zero_copy_sent;
719b13
 };
719b13
 
719b13
 
719b13
diff --git a/io/channel-socket.c b/io/channel-socket.c
719b13
index bfbd64787e..38a46ba213 100644
719b13
--- a/io/channel-socket.c
719b13
+++ b/io/channel-socket.c
719b13
@@ -26,6 +26,14 @@
719b13
 #include "io/channel-watch.h"
719b13
 #include "trace.h"
719b13
 #include "qapi/clone-visitor.h"
719b13
+#ifdef CONFIG_LINUX
719b13
+#include <linux/errqueue.h>
719b13
+#include <sys/socket.h>
719b13
+
719b13
+#if (defined(MSG_ZEROCOPY) && defined(SO_ZEROCOPY))
719b13
+#define QEMU_MSG_ZEROCOPY
719b13
+#endif
719b13
+#endif
719b13
 
719b13
 #define SOCKET_MAX_FDS 16
719b13
 
719b13
@@ -55,6 +63,8 @@ qio_channel_socket_new(void)
719b13
 
719b13
     sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
719b13
     sioc->fd = -1;
719b13
+    sioc->zero_copy_queued = 0;
719b13
+    sioc->zero_copy_sent = 0;
719b13
 
719b13
     ioc = QIO_CHANNEL(sioc);
719b13
     qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
719b13
@@ -154,6 +164,16 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
719b13
         return -1;
719b13
     }
719b13
 
719b13
+#ifdef QEMU_MSG_ZEROCOPY
719b13
+    int ret, v = 1;
719b13
+    ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
719b13
+    if (ret == 0) {
719b13
+        /* Zero copy available on host */
719b13
+        qio_channel_set_feature(QIO_CHANNEL(ioc),
719b13
+                                QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY);
719b13
+    }
719b13
+#endif
719b13
+
719b13
     return 0;
719b13
 }
719b13
 
719b13
@@ -534,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
719b13
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
719b13
     size_t fdsize = sizeof(int) * nfds;
719b13
     struct cmsghdr *cmsg;
719b13
+    int sflags = 0;
719b13
 
719b13
     memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
719b13
 
719b13
@@ -558,15 +579,31 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
719b13
         memcpy(CMSG_DATA(cmsg), fds, fdsize);
719b13
     }
719b13
 
719b13
+#ifdef QEMU_MSG_ZEROCOPY
719b13
+    if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
719b13
+        sflags = MSG_ZEROCOPY;
719b13
+    }
719b13
+#endif
719b13
+
719b13
  retry:
719b13
-    ret = sendmsg(sioc->fd, &msg, 0);
719b13
+    ret = sendmsg(sioc->fd, &msg, sflags);
719b13
     if (ret <= 0) {
719b13
-        if (errno == EAGAIN) {
719b13
+        switch (errno) {
719b13
+        case EAGAIN:
719b13
             return QIO_CHANNEL_ERR_BLOCK;
719b13
-        }
719b13
-        if (errno == EINTR) {
719b13
+        case EINTR:
719b13
             goto retry;
719b13
+#ifdef QEMU_MSG_ZEROCOPY
719b13
+        case ENOBUFS:
719b13
+            if (sflags & MSG_ZEROCOPY) {
719b13
+                error_setg_errno(errp, errno,
719b13
+                                 "Process can't lock enough memory for using MSG_ZEROCOPY");
719b13
+                return -1;
719b13
+            }
719b13
+            break;
719b13
+#endif
719b13
         }
719b13
+
719b13
         error_setg_errno(errp, errno,
719b13
                          "Unable to write to socket");
719b13
         return -1;
719b13
@@ -660,6 +697,74 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
719b13
 }
719b13
 #endif /* WIN32 */
719b13
 
719b13
+
719b13
+#ifdef QEMU_MSG_ZEROCOPY
719b13
+static int qio_channel_socket_flush(QIOChannel *ioc,
719b13
+                                    Error **errp)
719b13
+{
719b13
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
719b13
+    struct msghdr msg = {};
719b13
+    struct sock_extended_err *serr;
719b13
+    struct cmsghdr *cm;
719b13
+    char control[CMSG_SPACE(sizeof(*serr))];
719b13
+    int received;
719b13
+    int ret = 1;
719b13
+
719b13
+    msg.msg_control = control;
719b13
+    msg.msg_controllen = sizeof(control);
719b13
+    memset(control, 0, sizeof(control));
719b13
+
719b13
+    while (sioc->zero_copy_sent < sioc->zero_copy_queued) {
719b13
+        received = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
719b13
+        if (received < 0) {
719b13
+            switch (errno) {
719b13
+            case EAGAIN:
719b13
+                /* Nothing on errqueue, wait until something is available */
719b13
+                qio_channel_wait(ioc, G_IO_ERR);
719b13
+                continue;
719b13
+            case EINTR:
719b13
+                continue;
719b13
+            default:
719b13
+                error_setg_errno(errp, errno,
719b13
+                                 "Unable to read errqueue");
719b13
+                return -1;
719b13
+            }
719b13
+        }
719b13
+
719b13
+        cm = CMSG_FIRSTHDR(&msg;;
719b13
+        if (cm->cmsg_level != SOL_IP &&
719b13
+            cm->cmsg_type != IP_RECVERR) {
719b13
+            error_setg_errno(errp, EPROTOTYPE,
719b13
+                             "Wrong cmsg in errqueue");
719b13
+            return -1;
719b13
+        }
719b13
+
719b13
+        serr = (void *) CMSG_DATA(cm);
719b13
+        if (serr->ee_errno != SO_EE_ORIGIN_NONE) {
719b13
+            error_setg_errno(errp, serr->ee_errno,
719b13
+                             "Error on socket");
719b13
+            return -1;
719b13
+        }
719b13
+        if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
719b13
+            error_setg_errno(errp, serr->ee_origin,
719b13
+                             "Error not from zero copy");
719b13
+            return -1;
719b13
+        }
719b13
+
719b13
+        /* No errors, count successfully finished sendmsg()*/
719b13
+        sioc->zero_copy_sent += serr->ee_data - serr->ee_info + 1;
719b13
+
719b13
+        /* If any sendmsg() succeeded using zero copy, return 0 at the end */
719b13
+        if (serr->ee_code != SO_EE_CODE_ZEROCOPY_COPIED) {
719b13
+            ret = 0;
719b13
+        }
719b13
+    }
719b13
+
719b13
+    return ret;
719b13
+}
719b13
+
719b13
+#endif /* QEMU_MSG_ZEROCOPY */
719b13
+
719b13
 static int
719b13
 qio_channel_socket_set_blocking(QIOChannel *ioc,
719b13
                                 bool enabled,
719b13
@@ -789,6 +894,9 @@ static void qio_channel_socket_class_init(ObjectClass *klass,
719b13
     ioc_klass->io_set_delay = qio_channel_socket_set_delay;
719b13
     ioc_klass->io_create_watch = qio_channel_socket_create_watch;
719b13
     ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
719b13
+#ifdef QEMU_MSG_ZEROCOPY
719b13
+    ioc_klass->io_flush = qio_channel_socket_flush;
719b13
+#endif
719b13
 }
719b13
 
719b13
 static const TypeInfo qio_channel_socket_info = {
719b13
-- 
719b13
2.35.3
719b13