thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

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