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