9ae3a8
From ccff61f6316a815aa4a538799e089dec7ce754c5 Mon Sep 17 00:00:00 2001
9ae3a8
From: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
9ae3a8
Date: Thu, 27 Feb 2014 14:53:37 +0100
9ae3a8
Subject: [PATCH 3/6] Fix two XBZRLE corruption issues
9ae3a8
9ae3a8
RH-Author: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
9ae3a8
Message-id: <1393512817-21040-1-git-send-email-dgilbert@redhat.com>
9ae3a8
Patchwork-id: 57921
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 1/1] Fix two XBZRLE corruption issues
9ae3a8
Bugzilla: 1063417
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Juan Quintela <quintela@redhat.com>
9ae3a8
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
9ae3a8
9ae3a8
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1063417
9ae3a8
Brew: https://brewweb.devel.redhat.com/taskinfo?taskID=7115209
9ae3a8
Upstream: 6d3cb1f970ee85361618f7ff02869180394e012d
9ae3a8
9ae3a8
Push zero'd pages into the XBZRLE cache
9ae3a8
    A page that was cached by XBZRLE, zero'd and then XBZRLE'd again
9ae3a8
    was being compared against a stale cache value
9ae3a8
9ae3a8
Don't use 'qemu_put_buffer_async' to put pages from the XBZRLE cache
9ae3a8
    Since the cache might change before the data hits the wire
9ae3a8
9ae3a8
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
9ae3a8
Signed-off-by: Juan Quintela <quintela@redhat.com>
9ae3a8
(cherry picked from commit 6d3cb1f970ee85361618f7ff02869180394e012d)
9ae3a8
---
9ae3a8
 arch_init.c                    | 64 ++++++++++++++++++++++++++++++++----------
9ae3a8
 include/migration/page_cache.h |  2 +-
9ae3a8
 page_cache.c                   |  2 +-
9ae3a8
 3 files changed, 51 insertions(+), 17 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 arch_init.c                    |   64 ++++++++++++++++++++++++++++++---------
9ae3a8
 include/migration/page_cache.h |    2 +-
9ae3a8
 page_cache.c                   |    2 +-
9ae3a8
 3 files changed, 51 insertions(+), 17 deletions(-)
9ae3a8
9ae3a8
diff --git a/arch_init.c b/arch_init.c
9ae3a8
index 31bf690..f5d521a 100644
9ae3a8
--- a/arch_init.c
9ae3a8
+++ b/arch_init.c
9ae3a8
@@ -122,7 +122,6 @@ static void check_guest_throttling(void);
9ae3a8
 #define RAM_SAVE_FLAG_XBZRLE   0x40
9ae3a8
 /* 0x80 is reserved in migration.h start with 0x100 next */
9ae3a8
 
9ae3a8
-
9ae3a8
 static struct defconfig_file {
9ae3a8
     const char *filename;
9ae3a8
     /* Indicates it is an user config file (disabled by -no-user-config) */
9ae3a8
@@ -133,6 +132,7 @@ static struct defconfig_file {
9ae3a8
     { NULL }, /* end of list */
9ae3a8
 };
9ae3a8
 
9ae3a8
+static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
9ae3a8
 
9ae3a8
 int qemu_read_default_config_files(bool userconfig)
9ae3a8
 {
9ae3a8
@@ -273,6 +273,34 @@ static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
9ae3a8
     return size;
9ae3a8
 }
9ae3a8
 
9ae3a8
+/* This is the last block that we have visited serching for dirty pages
9ae3a8
+ */
9ae3a8
+static RAMBlock *last_seen_block;
9ae3a8
+/* This is the last block from where we have sent data */
9ae3a8
+static RAMBlock *last_sent_block;
9ae3a8
+static ram_addr_t last_offset;
9ae3a8
+static unsigned long *migration_bitmap;
9ae3a8
+static uint64_t migration_dirty_pages;
9ae3a8
+static uint32_t last_version;
9ae3a8
+static bool ram_bulk_stage;
9ae3a8
+
9ae3a8
+/* Update the xbzrle cache to reflect a page that's been sent as all 0.
9ae3a8
+ * The important thing is that a stale (not-yet-0'd) page be replaced
9ae3a8
+ * by the new data.
9ae3a8
+ * As a bonus, if the page wasn't in the cache it gets added so that
9ae3a8
+ * when a small write is made into the 0'd page it gets XBZRLE sent
9ae3a8
+ */
9ae3a8
+static void xbzrle_cache_zero_page(ram_addr_t current_addr)
9ae3a8
+{
9ae3a8
+    if (ram_bulk_stage || !migrate_use_xbzrle()) {
9ae3a8
+        return;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* We don't care if this fails to allocate a new cache page
9ae3a8
+     * as long as it updated an old one */
9ae3a8
+    cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE);
9ae3a8
+}
9ae3a8
+
9ae3a8
 #define ENCODING_FLAG_XBZRLE 0x1
9ae3a8
 
9ae3a8
 static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
9ae3a8
@@ -329,18 +357,6 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
9ae3a8
     return bytes_sent;
9ae3a8
 }
9ae3a8
 
9ae3a8
-
9ae3a8
-/* This is the last block that we have visited serching for dirty pages
9ae3a8
- */
9ae3a8
-static RAMBlock *last_seen_block;
9ae3a8
-/* This is the last block from where we have sent data */
9ae3a8
-static RAMBlock *last_sent_block;
9ae3a8
-static ram_addr_t last_offset;
9ae3a8
-static unsigned long *migration_bitmap;
9ae3a8
-static uint64_t migration_dirty_pages;
9ae3a8
-static uint32_t last_version;
9ae3a8
-static bool ram_bulk_stage;
9ae3a8
-
9ae3a8
 static inline
9ae3a8
 ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
9ae3a8
                                                  ram_addr_t start)
9ae3a8
@@ -512,6 +528,7 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
9ae3a8
         } else {
9ae3a8
             int ret;
9ae3a8
             uint8_t *p;
9ae3a8
+            bool send_async = true;
9ae3a8
             int cont = (block == last_sent_block) ?
9ae3a8
                 RAM_SAVE_FLAG_CONTINUE : 0;
9ae3a8
 
9ae3a8
@@ -522,6 +539,7 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
9ae3a8
             ret = ram_control_save_page(f, block->offset,
9ae3a8
                                offset, TARGET_PAGE_SIZE, &bytes_sent);
9ae3a8
 
9ae3a8
+            current_addr = block->offset + offset;
9ae3a8
             if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
9ae3a8
                 if (ret != RAM_SAVE_CONTROL_DELAYED) {
9ae3a8
                     if (bytes_sent > 0) {
9ae3a8
@@ -536,19 +554,35 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
9ae3a8
                                             RAM_SAVE_FLAG_COMPRESS);
9ae3a8
                 qemu_put_byte(f, 0);
9ae3a8
                 bytes_sent++;
9ae3a8
+                /* Must let xbzrle know, otherwise a previous (now 0'd) cached
9ae3a8
+                 * page would be stale
9ae3a8
+                 */
9ae3a8
+                xbzrle_cache_zero_page(current_addr);
9ae3a8
             } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
9ae3a8
-                current_addr = block->offset + offset;
9ae3a8
                 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
9ae3a8
                                               offset, cont, last_stage);
9ae3a8
                 if (!last_stage) {
9ae3a8
+                    /* We must send exactly what's in the xbzrle cache
9ae3a8
+                     * even if the page wasn't xbzrle compressed, so that
9ae3a8
+                     * it's right next time.
9ae3a8
+                     */
9ae3a8
                     p = get_cached_data(XBZRLE.cache, current_addr);
9ae3a8
+
9ae3a8
+                    /* Can't send this cached data async, since the cache page
9ae3a8
+                     * might get updated before it gets to the wire
9ae3a8
+                     */
9ae3a8
+                    send_async = false;
9ae3a8
                 }
9ae3a8
             }
9ae3a8
 
9ae3a8
             /* XBZRLE overflow or normal page */
9ae3a8
             if (bytes_sent == -1) {
9ae3a8
                 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
9ae3a8
-                qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
9ae3a8
+                if (send_async) {
9ae3a8
+                    qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
9ae3a8
+                } else {
9ae3a8
+                    qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
9ae3a8
+                }
9ae3a8
                 bytes_sent += TARGET_PAGE_SIZE;
9ae3a8
                 acct_info.norm_pages++;
9ae3a8
             }
9ae3a8
diff --git a/include/migration/page_cache.h b/include/migration/page_cache.h
9ae3a8
index d156f0d..2d5ce2d 100644
9ae3a8
--- a/include/migration/page_cache.h
9ae3a8
+++ b/include/migration/page_cache.h
9ae3a8
@@ -66,7 +66,7 @@ uint8_t *get_cached_data(const PageCache *cache, uint64_t addr);
9ae3a8
  * @addr: page address
9ae3a8
  * @pdata: pointer to the page
9ae3a8
  */
9ae3a8
-int cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata);
9ae3a8
+int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata);
9ae3a8
 
9ae3a8
 /**
9ae3a8
  * cache_resize: resize the page cache. In case of size reduction the extra
9ae3a8
diff --git a/page_cache.c b/page_cache.c
9ae3a8
index 250772d..5a763f9 100644
9ae3a8
--- a/page_cache.c
9ae3a8
+++ b/page_cache.c
9ae3a8
@@ -151,7 +151,7 @@ uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
9ae3a8
     return cache_get_by_addr(cache, addr)->it_data;
9ae3a8
 }
9ae3a8
 
9ae3a8
-int cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
9ae3a8
+int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata)
9ae3a8
 {
9ae3a8
 
9ae3a8
     CacheItem *it = NULL;
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8