thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 7 months ago
Clone

Blame SOURCES/kvm-multifd-Copy-pages-before-compressing-them-with-zlib.patch

29b115
From 1d280070748b604c60a7be4d4c3c3a28e3964f37 Mon Sep 17 00:00:00 2001
29b115
From: Thomas Huth <thuth@redhat.com>
29b115
Date: Tue, 2 Aug 2022 10:11:21 +0200
29b115
Subject: [PATCH 31/32] multifd: Copy pages before compressing them with zlib
29b115
29b115
RH-Author: Thomas Huth <thuth@redhat.com>
29b115
RH-MergeRequest: 112: Fix postcopy migration on s390x
29b115
RH-Commit: [1/2] fd5a0221e22b4563bd1cb7f8a8b95f0bfe8f5fc9 (thuth/qemu-kvm-cs9)
29b115
RH-Bugzilla: 2099934
29b115
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
29b115
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
29b115
RH-Acked-by: David Hildenbrand <david@redhat.com>
29b115
RH-Acked-by: Peter Xu <peterx@redhat.com>
29b115
29b115
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2099934
29b115
29b115
zlib_send_prepare() compresses pages of a running VM. zlib does not
29b115
make any thread-safety guarantees with respect to changing deflate()
29b115
input concurrently with deflate() [1].
29b115
29b115
One can observe problems due to this with the IBM zEnterprise Data
29b115
Compression accelerator capable zlib [2]. When the hardware
29b115
acceleration is enabled, migration/multifd/tcp/plain/zlib test fails
29b115
intermittently [3] due to sliding window corruption. The accelerator's
29b115
architecture explicitly discourages concurrent accesses [4]:
29b115
29b115
    Page 26-57, "Other Conditions":
29b115
29b115
    As observed by this CPU, other CPUs, and channel
29b115
    programs, references to the parameter block, first,
29b115
    second, and third operands may be multiple-access
29b115
    references, accesses to these storage locations are
29b115
    not necessarily block-concurrent, and the sequence
29b115
    of these accesses or references is undefined.
29b115
29b115
Mark Adler pointed out that vanilla zlib performs double fetches under
29b115
certain circumstances as well [5], therefore we need to copy data
29b115
before passing it to deflate().
29b115
29b115
[1] https://zlib.net/manual.html
29b115
[2] https://github.com/madler/zlib/pull/410
29b115
[3] https://lists.nongnu.org/archive/html/qemu-devel/2022-03/msg03988.html
29b115
[4] http://publibfp.dhe.ibm.com/epubs/pdf/a227832c.pdf
29b115
[5] https://lists.gnu.org/archive/html/qemu-devel/2022-07/msg00889.html
29b115
29b115
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
29b115
Message-Id: <20220705203559.2960949-1-iii@linux.ibm.com>
29b115
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
29b115
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
29b115
(cherry picked from commit 007e179ef0e97eafda4c9ff2a9d665a1947c7c6d)
29b115
Signed-off-by: Thomas Huth <thuth@redhat.com>
29b115
---
29b115
 migration/multifd-zlib.c | 38 ++++++++++++++++++++++++++++++--------
29b115
 1 file changed, 30 insertions(+), 8 deletions(-)
29b115
29b115
diff --git a/migration/multifd-zlib.c b/migration/multifd-zlib.c
29b115
index 3a7ae44485..18213a9513 100644
29b115
--- a/migration/multifd-zlib.c
29b115
+++ b/migration/multifd-zlib.c
29b115
@@ -27,6 +27,8 @@ struct zlib_data {
29b115
     uint8_t *zbuff;
29b115
     /* size of compressed buffer */
29b115
     uint32_t zbuff_len;
29b115
+    /* uncompressed buffer of size qemu_target_page_size() */
29b115
+    uint8_t *buf;
29b115
 };
29b115
 
29b115
 /* Multifd zlib compression */
29b115
@@ -45,26 +47,38 @@ static int zlib_send_setup(MultiFDSendParams *p, Error **errp)
29b115
 {
29b115
     struct zlib_data *z = g_new0(struct zlib_data, 1);
29b115
     z_stream *zs = &z->zs;
29b115
+    const char *err_msg;
29b115
 
29b115
     zs->zalloc = Z_NULL;
29b115
     zs->zfree = Z_NULL;
29b115
     zs->opaque = Z_NULL;
29b115
     if (deflateInit(zs, migrate_multifd_zlib_level()) != Z_OK) {
29b115
-        g_free(z);
29b115
-        error_setg(errp, "multifd %u: deflate init failed", p->id);
29b115
-        return -1;
29b115
+        err_msg = "deflate init failed";
29b115
+        goto err_free_z;
29b115
     }
29b115
     /* This is the maxium size of the compressed buffer */
29b115
     z->zbuff_len = compressBound(MULTIFD_PACKET_SIZE);
29b115
     z->zbuff = g_try_malloc(z->zbuff_len);
29b115
     if (!z->zbuff) {
29b115
-        deflateEnd(&z->zs);
29b115
-        g_free(z);
29b115
-        error_setg(errp, "multifd %u: out of memory for zbuff", p->id);
29b115
-        return -1;
29b115
+        err_msg = "out of memory for zbuff";
29b115
+        goto err_deflate_end;
29b115
+    }
29b115
+    z->buf = g_try_malloc(qemu_target_page_size());
29b115
+    if (!z->buf) {
29b115
+        err_msg = "out of memory for buf";
29b115
+        goto err_free_zbuff;
29b115
     }
29b115
     p->data = z;
29b115
     return 0;
29b115
+
29b115
+err_free_zbuff:
29b115
+    g_free(z->zbuff);
29b115
+err_deflate_end:
29b115
+    deflateEnd(&z->zs);
29b115
+err_free_z:
29b115
+    g_free(z);
29b115
+    error_setg(errp, "multifd %u: %s", p->id, err_msg);
29b115
+    return -1;
29b115
 }
29b115
 
29b115
 /**
29b115
@@ -82,6 +96,8 @@ static void zlib_send_cleanup(MultiFDSendParams *p, Error **errp)
29b115
     deflateEnd(&z->zs);
29b115
     g_free(z->zbuff);
29b115
     z->zbuff = NULL;
29b115
+    g_free(z->buf);
29b115
+    z->buf = NULL;
29b115
     g_free(p->data);
29b115
     p->data = NULL;
29b115
 }
29b115
@@ -114,8 +130,14 @@ static int zlib_send_prepare(MultiFDSendParams *p, Error **errp)
29b115
             flush = Z_SYNC_FLUSH;
29b115
         }
29b115
 
29b115
+        /*
29b115
+         * Since the VM might be running, the page may be changing concurrently
29b115
+         * with compression. zlib does not guarantee that this is safe,
29b115
+         * therefore copy the page before calling deflate().
29b115
+         */
29b115
+        memcpy(z->buf, p->pages->block->host + p->normal[i], page_size);
29b115
         zs->avail_in = page_size;
29b115
-        zs->next_in = p->pages->block->host + p->normal[i];
29b115
+        zs->next_in = z->buf;
29b115
 
29b115
         zs->avail_out = available;
29b115
         zs->next_out = z->zbuff + out_size;
29b115
-- 
29b115
2.31.1
29b115