0727d3
From 1181a9cbcaf37a82aa7bf117ef209f554b8c4a71 Mon Sep 17 00:00:00 2001
0727d3
From: Juan Quintela <quintela@redhat.com>
0727d3
Date: Wed, 18 May 2022 02:52:24 -0300
0727d3
Subject: [PATCH 11/37] multifd: Move iov from pages to params
0727d3
MIME-Version: 1.0
0727d3
Content-Type: text/plain; charset=UTF-8
0727d3
Content-Transfer-Encoding: 8bit
0727d3
0727d3
RH-Author: Leonardo Brás <leobras@redhat.com>
0727d3
RH-MergeRequest: 191: MSG_ZEROCOPY + Multifd @ rhel8.7
0727d3
RH-Commit: [11/26] 24dff3ef68cf3327811242193502319ed3e3940a
0727d3
RH-Bugzilla: 2072049
0727d3
RH-Acked-by: Peter Xu <peterx@redhat.com>
0727d3
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
0727d3
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
0727d3
0727d3
This will allow us to reduce the number of system calls on the next patch.
0727d3
0727d3
Signed-off-by: Juan Quintela <quintela@redhat.com>
0727d3
(cherry picked from commit 226468ba3dea950ab4bb0b729878dde25812da1c)
0727d3
Signed-off-by: Leonardo Bras <leobras@redhat.com>
0727d3
---
0727d3
 migration/multifd.c | 34 ++++++++++++++++++++++++----------
0727d3
 migration/multifd.h |  8 ++++++--
0727d3
 2 files changed, 30 insertions(+), 12 deletions(-)
0727d3
0727d3
diff --git a/migration/multifd.c b/migration/multifd.c
0727d3
index d0d19470f9..5004f394aa 100644
0727d3
--- a/migration/multifd.c
0727d3
+++ b/migration/multifd.c
0727d3
@@ -86,7 +86,16 @@ static void nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)
0727d3
  */
0727d3
 static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)
0727d3
 {
0727d3
-    p->next_packet_size = p->pages->num * qemu_target_page_size();
0727d3
+    MultiFDPages_t *pages = p->pages;
0727d3
+    size_t page_size = qemu_target_page_size();
0727d3
+
0727d3
+    for (int i = 0; i < p->pages->num; i++) {
0727d3
+        p->iov[p->iovs_num].iov_base = pages->block->host + pages->offset[i];
0727d3
+        p->iov[p->iovs_num].iov_len = page_size;
0727d3
+        p->iovs_num++;
0727d3
+    }
0727d3
+
0727d3
+    p->next_packet_size = p->pages->num * page_size;
0727d3
     p->flags |= MULTIFD_FLAG_NOCOMP;
0727d3
     return 0;
0727d3
 }
0727d3
@@ -104,7 +113,7 @@ static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)
0727d3
  */
0727d3
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
0727d3
 {
0727d3
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
0727d3
+    return qio_channel_writev_all(p->c, p->iov, p->iovs_num, errp);
0727d3
 }
0727d3
 
0727d3
 /**
0727d3
@@ -146,13 +155,18 @@ static void nocomp_recv_cleanup(MultiFDRecvParams *p)
0727d3
 static int nocomp_recv_pages(MultiFDRecvParams *p, Error **errp)
0727d3
 {
0727d3
     uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
0727d3
+    size_t page_size = qemu_target_page_size();
0727d3
 
0727d3
     if (flags != MULTIFD_FLAG_NOCOMP) {
0727d3
         error_setg(errp, "multifd %u: flags received %x flags expected %x",
0727d3
                    p->id, flags, MULTIFD_FLAG_NOCOMP);
0727d3
         return -1;
0727d3
     }
0727d3
-    return qio_channel_readv_all(p->c, p->pages->iov, p->pages->num, errp);
0727d3
+    for (int i = 0; i < p->pages->num; i++) {
0727d3
+        p->iov[i].iov_base = p->pages->block->host + p->pages->offset[i];
0727d3
+        p->iov[i].iov_len = page_size;
0727d3
+    }
0727d3
+    return qio_channel_readv_all(p->c, p->iov, p->pages->num, errp);
0727d3
 }
0727d3
 
0727d3
 static MultiFDMethods multifd_nocomp_ops = {
0727d3
@@ -242,7 +256,6 @@ static MultiFDPages_t *multifd_pages_init(size_t size)
0727d3
     MultiFDPages_t *pages = g_new0(MultiFDPages_t, 1);
0727d3
 
0727d3
     pages->allocated = size;
0727d3
-    pages->iov = g_new0(struct iovec, size);
0727d3
     pages->offset = g_new0(ram_addr_t, size);
0727d3
 
0727d3
     return pages;
0727d3
@@ -254,8 +267,6 @@ static void multifd_pages_clear(MultiFDPages_t *pages)
0727d3
     pages->allocated = 0;
0727d3
     pages->packet_num = 0;
0727d3
     pages->block = NULL;
0727d3
-    g_free(pages->iov);
0727d3
-    pages->iov = NULL;
0727d3
     g_free(pages->offset);
0727d3
     pages->offset = NULL;
0727d3
     g_free(pages);
0727d3
@@ -365,8 +376,6 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
0727d3
             return -1;
0727d3
         }
0727d3
         p->pages->offset[i] = offset;
0727d3
-        p->pages->iov[i].iov_base = block->host + offset;
0727d3
-        p->pages->iov[i].iov_len = page_size;
0727d3
     }
0727d3
 
0727d3
     return 0;
0727d3
@@ -470,8 +479,6 @@ int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
0727d3
 
0727d3
     if (pages->block == block) {
0727d3
         pages->offset[pages->num] = offset;
0727d3
-        pages->iov[pages->num].iov_base = block->host + offset;
0727d3
-        pages->iov[pages->num].iov_len = qemu_target_page_size();
0727d3
         pages->num++;
0727d3
 
0727d3
         if (pages->num < pages->allocated) {
0727d3
@@ -564,6 +571,8 @@ void multifd_save_cleanup(void)
0727d3
         p->packet_len = 0;
0727d3
         g_free(p->packet);
0727d3
         p->packet = NULL;
0727d3
+        g_free(p->iov);
0727d3
+        p->iov = NULL;
0727d3
         multifd_send_state->ops->send_cleanup(p, &local_err);
0727d3
         if (local_err) {
0727d3
             migrate_set_error(migrate_get_current(), local_err);
0727d3
@@ -651,6 +660,7 @@ static void *multifd_send_thread(void *opaque)
0727d3
             uint32_t used = p->pages->num;
0727d3
             uint64_t packet_num = p->packet_num;
0727d3
             uint32_t flags = p->flags;
0727d3
+            p->iovs_num = 0;
0727d3
 
0727d3
             if (used) {
0727d3
                 ret = multifd_send_state->ops->send_prepare(p, &local_err);
0727d3
@@ -919,6 +929,7 @@ int multifd_save_setup(Error **errp)
0727d3
         p->packet->version = cpu_to_be32(MULTIFD_VERSION);
0727d3
         p->name = g_strdup_printf("multifdsend_%d", i);
0727d3
         p->tls_hostname = g_strdup(s->hostname);
0727d3
+        p->iov = g_new0(struct iovec, page_count);
0727d3
         socket_send_channel_create(multifd_new_send_channel_async, p);
0727d3
     }
0727d3
 
0727d3
@@ -1018,6 +1029,8 @@ int multifd_load_cleanup(Error **errp)
0727d3
         p->packet_len = 0;
0727d3
         g_free(p->packet);
0727d3
         p->packet = NULL;
0727d3
+        g_free(p->iov);
0727d3
+        p->iov = NULL;
0727d3
         multifd_recv_state->ops->recv_cleanup(p);
0727d3
     }
0727d3
     qemu_sem_destroy(&multifd_recv_state->sem_sync);
0727d3
@@ -1158,6 +1171,7 @@ int multifd_load_setup(Error **errp)
0727d3
                       + sizeof(uint64_t) * page_count;
0727d3
         p->packet = g_malloc0(p->packet_len);
0727d3
         p->name = g_strdup_printf("multifdrecv_%d", i);
0727d3
+        p->iov = g_new0(struct iovec, page_count);
0727d3
     }
0727d3
 
0727d3
     for (i = 0; i < thread_count; i++) {
0727d3
diff --git a/migration/multifd.h b/migration/multifd.h
0727d3
index e57adc783b..c3f18af364 100644
0727d3
--- a/migration/multifd.h
0727d3
+++ b/migration/multifd.h
0727d3
@@ -62,8 +62,6 @@ typedef struct {
0727d3
     uint64_t packet_num;
0727d3
     /* offset of each page */
0727d3
     ram_addr_t *offset;
0727d3
-    /* pointer to each page */
0727d3
-    struct iovec *iov;
0727d3
     RAMBlock *block;
0727d3
 } MultiFDPages_t;
0727d3
 
0727d3
@@ -110,6 +108,10 @@ typedef struct {
0727d3
     uint64_t num_pages;
0727d3
     /* syncs main thread and channels */
0727d3
     QemuSemaphore sem_sync;
0727d3
+    /* buffers to send */
0727d3
+    struct iovec *iov;
0727d3
+    /* number of iovs used */
0727d3
+    uint32_t iovs_num;
0727d3
     /* used for compression methods */
0727d3
     void *data;
0727d3
 }  MultiFDSendParams;
0727d3
@@ -149,6 +151,8 @@ typedef struct {
0727d3
     uint64_t num_pages;
0727d3
     /* syncs main thread and channels */
0727d3
     QemuSemaphore sem_sync;
0727d3
+    /* buffers to recv */
0727d3
+    struct iovec *iov;
0727d3
     /* used for de-compression methods */
0727d3
     void *data;
0727d3
 } MultiFDRecvParams;
0727d3
-- 
0727d3
2.35.3
0727d3