thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 6 months ago
Clone

Blame SOURCES/kvm-multifd-Implement-zero-copy-write-in-multifd-migrati.patch

29b115
From c1a2866d158ac67179fa0d17f1710302eb9a3866 Mon Sep 17 00:00:00 2001
29b115
From: Leonardo Bras <leobras@redhat.com>
29b115
Date: Fri, 13 May 2022 03:28:37 -0300
29b115
Subject: [PATCH 14/18] multifd: Implement zero copy write in multifd migration
29b115
 (multifd-zero-copy)
29b115
MIME-Version: 1.0
29b115
Content-Type: text/plain; charset=UTF-8
29b115
Content-Transfer-Encoding: 8bit
29b115
29b115
RH-Author: Leonardo Brás <leobras@redhat.com>
29b115
RH-MergeRequest: 95: MSG_ZEROCOPY + Multifd
29b115
RH-Commit: [8/11] b93009cc94b2cc4b464b4f68ebfb37b870dd6f7d (LeoBras/centos-qemu-kvm)
29b115
RH-Bugzilla: 1968509
29b115
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
29b115
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
29b115
RH-Acked-by: Peter Xu <peterx@redhat.com>
29b115
29b115
Implement zero copy send on nocomp_send_write(), by making use of QIOChannel
29b115
writev + flags & flush interface.
29b115
29b115
Change multifd_send_sync_main() so flush_zero_copy() can be called
29b115
after each iteration in order to make sure all dirty pages are sent before
29b115
a new iteration is started. It will also flush at the beginning and at the
29b115
end of migration.
29b115
29b115
Also make it return -1 if flush_zero_copy() fails, in order to cancel
29b115
the migration process, and avoid resuming the guest in the target host
29b115
without receiving all current RAM.
29b115
29b115
This will work fine on RAM migration because the RAM pages are not usually freed,
29b115
and there is no problem on changing the pages content between writev_zero_copy() and
29b115
the actual sending of the buffer, because this change will dirty the page and
29b115
cause it to be re-sent on a next iteration anyway.
29b115
29b115
A lot of locked memory may be needed in order to use multifd migration
29b115
with zero-copy enabled, so disabling the feature should be necessary for
29b115
low-privileged users trying to perform multifd migrations.
29b115
29b115
Signed-off-by: Leonardo Bras <leobras@redhat.com>
29b115
Reviewed-by: Peter Xu <peterx@redhat.com>
29b115
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
29b115
Message-Id: <20220513062836.965425-9-leobras@redhat.com>
29b115
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
29b115
(cherry picked from commit 5b1d9bab2da4fca3a3caee97c430e5709cb32b7b)
29b115
Signed-off-by: Leonardo Bras <leobras@redhat.com>
29b115
---
29b115
 migration/migration.c | 11 ++++++++++-
29b115
 migration/multifd.c   | 37 +++++++++++++++++++++++++++++++++++--
29b115
 migration/multifd.h   |  2 ++
29b115
 migration/socket.c    |  5 +++--
29b115
 4 files changed, 50 insertions(+), 5 deletions(-)
29b115
29b115
diff --git a/migration/migration.c b/migration/migration.c
29b115
index d91efb66fe..102236fba0 100644
29b115
--- a/migration/migration.c
29b115
+++ b/migration/migration.c
29b115
@@ -1485,7 +1485,16 @@ static bool migrate_params_check(MigrationParameters *params, Error **errp)
29b115
         error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
29b115
         return false;
29b115
     }
29b115
-
29b115
+#ifdef CONFIG_LINUX
29b115
+    if (params->zero_copy_send &&
29b115
+        (!migrate_use_multifd() ||
29b115
+         params->multifd_compression != MULTIFD_COMPRESSION_NONE ||
29b115
+         (params->tls_creds && *params->tls_creds))) {
29b115
+        error_setg(errp,
29b115
+                   "Zero copy only available for non-compressed non-TLS multifd migration");
29b115
+        return false;
29b115
+    }
29b115
+#endif
29b115
     return true;
29b115
 }
29b115
 
29b115
diff --git a/migration/multifd.c b/migration/multifd.c
29b115
index 8fca6c970e..0b5b41c53f 100644
29b115
--- a/migration/multifd.c
29b115
+++ b/migration/multifd.c
29b115
@@ -571,6 +571,7 @@ void multifd_save_cleanup(void)
29b115
 int multifd_send_sync_main(QEMUFile *f)
29b115
 {
29b115
     int i;
29b115
+    bool flush_zero_copy;
29b115
 
29b115
     if (!migrate_use_multifd()) {
29b115
         return 0;
29b115
@@ -581,6 +582,20 @@ int multifd_send_sync_main(QEMUFile *f)
29b115
             return -1;
29b115
         }
29b115
     }
29b115
+
29b115
+    /*
29b115
+     * When using zero-copy, it's necessary to flush the pages before any of
29b115
+     * the pages can be sent again, so we'll make sure the new version of the
29b115
+     * pages will always arrive _later_ than the old pages.
29b115
+     *
29b115
+     * Currently we achieve this by flushing the zero-page requested writes
29b115
+     * per ram iteration, but in the future we could potentially optimize it
29b115
+     * to be less frequent, e.g. only after we finished one whole scanning of
29b115
+     * all the dirty bitmaps.
29b115
+     */
29b115
+
29b115
+    flush_zero_copy = migrate_use_zero_copy_send();
29b115
+
29b115
     for (i = 0; i < migrate_multifd_channels(); i++) {
29b115
         MultiFDSendParams *p = &multifd_send_state->params[i];
29b115
 
29b115
@@ -602,6 +617,17 @@ int multifd_send_sync_main(QEMUFile *f)
29b115
         ram_counters.transferred += p->packet_len;
29b115
         qemu_mutex_unlock(&p->mutex);
29b115
         qemu_sem_post(&p->sem);
29b115
+
29b115
+        if (flush_zero_copy && p->c) {
29b115
+            int ret;
29b115
+            Error *err = NULL;
29b115
+
29b115
+            ret = qio_channel_flush(p->c, &err;;
29b115
+            if (ret < 0) {
29b115
+                error_report_err(err);
29b115
+                return -1;
29b115
+            }
29b115
+        }
29b115
     }
29b115
     for (i = 0; i < migrate_multifd_channels(); i++) {
29b115
         MultiFDSendParams *p = &multifd_send_state->params[i];
29b115
@@ -686,8 +712,8 @@ static void *multifd_send_thread(void *opaque)
29b115
                 p->iov[0].iov_base = p->packet;
29b115
             }
29b115
 
29b115
-            ret = qio_channel_writev_all(p->c, p->iov, p->iovs_num,
29b115
-                                         &local_err);
29b115
+            ret = qio_channel_writev_full_all(p->c, p->iov, p->iovs_num, NULL,
29b115
+                                              0, p->write_flags, &local_err);
29b115
             if (ret != 0) {
29b115
                 break;
29b115
             }
29b115
@@ -928,6 +954,13 @@ int multifd_save_setup(Error **errp)
29b115
         /* We need one extra place for the packet header */
29b115
         p->iov = g_new0(struct iovec, page_count + 1);
29b115
         p->normal = g_new0(ram_addr_t, page_count);
29b115
+
29b115
+        if (migrate_use_zero_copy_send()) {
29b115
+            p->write_flags = QIO_CHANNEL_WRITE_FLAG_ZERO_COPY;
29b115
+        } else {
29b115
+            p->write_flags = 0;
29b115
+        }
29b115
+
29b115
         socket_send_channel_create(multifd_new_send_channel_async, p);
29b115
     }
29b115
 
29b115
diff --git a/migration/multifd.h b/migration/multifd.h
29b115
index cd495195ce..7ec688fb4f 100644
29b115
--- a/migration/multifd.h
29b115
+++ b/migration/multifd.h
29b115
@@ -96,6 +96,8 @@ typedef struct {
29b115
     uint32_t packet_len;
29b115
     /* pointer to the packet */
29b115
     MultiFDPacket_t *packet;
29b115
+    /* multifd flags for sending ram */
29b115
+    int write_flags;
29b115
     /* multifd flags for each packet */
29b115
     uint32_t flags;
29b115
     /* size of the next packet that contains pages */
29b115
diff --git a/migration/socket.c b/migration/socket.c
29b115
index 3754d8f72c..4fd5e85f50 100644
29b115
--- a/migration/socket.c
29b115
+++ b/migration/socket.c
29b115
@@ -79,8 +79,9 @@ static void socket_outgoing_migration(QIOTask *task,
29b115
 
29b115
     trace_migration_socket_outgoing_connected(data->hostname);
29b115
 
29b115
-    if (migrate_use_zero_copy_send()) {
29b115
-        error_setg(&err, "Zero copy send not available in migration");
29b115
+    if (migrate_use_zero_copy_send() &&
29b115
+        !qio_channel_has_feature(sioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
29b115
+        error_setg(&err, "Zero copy send feature not detected in host kernel");
29b115
     }
29b115
 
29b115
 out:
29b115
-- 
29b115
2.35.3
29b115