Blame SOURCES/kvm-block-backup-fix-fleecing-scheme-use-serialized-writ.patch

357786
From 5627c9fb0b86809d42914f1beef9b68226141d4b Mon Sep 17 00:00:00 2001
357786
From: John Snow <jsnow@redhat.com>
357786
Date: Wed, 18 Jul 2018 22:54:58 +0200
357786
Subject: [PATCH 73/89] block/backup: fix fleecing scheme: use serialized
357786
 writes
357786
357786
RH-Author: John Snow <jsnow@redhat.com>
357786
Message-id: <20180718225511.14878-23-jsnow@redhat.com>
357786
Patchwork-id: 81396
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 22/35] block/backup: fix fleecing scheme: use serialized writes
357786
Bugzilla: 1207657
357786
RH-Acked-by: Eric Blake <eblake@redhat.com>
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
357786
357786
Fleecing scheme works as follows: we want a kind of temporary snapshot
357786
of active drive A. We create temporary image B, with B->backing = A.
357786
Then we start backup(sync=none) from A to B. From this point, B reads
357786
as point-in-time snapshot of A (A continues to be active drive,
357786
accepting guest IO).
357786
357786
This scheme needs some additional synchronization between reads from B
357786
and backup COW operations, otherwise, the following situation is
357786
theoretically possible:
357786
357786
(assume B is qcow2, client is NBD client, reading from B)
357786
357786
1. client starts reading and take qcow2 mutex in qcow2_co_preadv, and
357786
   goes up to l2 table loading (assume cache miss)
357786
357786
2) guest write => backup COW => qcow2 write =>
357786
   try to take qcow2 mutex => waiting
357786
357786
3. l2 table loaded, we see that cluster is UNALLOCATED, go to
357786
   "case QCOW2_CLUSTER_UNALLOCATED" and unlock mutex before
357786
   bdrv_co_preadv(bs->backing, ...)
357786
357786
4) aha, mutex unlocked, backup COW continues, and we finally finish
357786
   guest write and change cluster in our active disk A
357786
357786
5. actually, do bdrv_co_preadv(bs->backing, ...) and read
357786
   _new updated_ data.
357786
357786
To avoid this, let's make backup writes serializing, to not intersect
357786
with reads from B.
357786
357786
Note: we expand range of handled cases from (sync=none and
357786
B->backing = A) to just (A in backing chain of B), to finally allow
357786
safe reading from B during backup for all cases when A in backing chain
357786
of B, i.e. B formally looks like point-in-time snapshot of A.
357786
357786
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
357786
Reviewed-by: Fam Zheng <famz@redhat.com>
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
(cherry picked from commit f8d59dfb40bbc6f5aeea57c8aac1e68c1d2454ee)
357786
Signed-off-by: John Snow <jsnow@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/backup.c | 20 ++++++++++++++------
357786
 1 file changed, 14 insertions(+), 6 deletions(-)
357786
357786
diff --git a/block/backup.c b/block/backup.c
357786
index 369155a..4ba1a6a 100644
357786
--- a/block/backup.c
357786
+++ b/block/backup.c
357786
@@ -47,6 +47,8 @@ typedef struct BackupBlockJob {
357786
     HBitmap *copy_bitmap;
357786
     bool use_copy_range;
357786
     int64_t copy_range_size;
357786
+
357786
+    bool serialize_target_writes;
357786
 } BackupBlockJob;
357786
 
357786
 static const BlockJobDriver backup_job_driver;
357786
@@ -102,6 +104,8 @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
357786
     QEMUIOVector qiov;
357786
     BlockBackend *blk = job->common.blk;
357786
     int nbytes;
357786
+    int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
357786
+    int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
357786
 
357786
     hbitmap_reset(job->copy_bitmap, start / job->cluster_size, 1);
357786
     nbytes = MIN(job->cluster_size, job->len - start);
357786
@@ -112,8 +116,7 @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
357786
     iov.iov_len = nbytes;
357786
     qemu_iovec_init_external(&qiov, &iov, 1);
357786
 
357786
-    ret = blk_co_preadv(blk, start, qiov.size, &qiov,
357786
-                        is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
357786
+    ret = blk_co_preadv(blk, start, qiov.size, &qiov, read_flags);
357786
     if (ret < 0) {
357786
         trace_backup_do_cow_read_fail(job, start, ret);
357786
         if (error_is_read) {
357786
@@ -124,11 +127,11 @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
357786
 
357786
     if (qemu_iovec_is_zero(&qiov)) {
357786
         ret = blk_co_pwrite_zeroes(job->target, start,
357786
-                                   qiov.size, BDRV_REQ_MAY_UNMAP);
357786
+                                   qiov.size, write_flags | BDRV_REQ_MAY_UNMAP);
357786
     } else {
357786
         ret = blk_co_pwritev(job->target, start,
357786
-                             qiov.size, &qiov,
357786
-                             job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
357786
+                             qiov.size, &qiov, write_flags |
357786
+                             (job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0));
357786
     }
357786
     if (ret < 0) {
357786
         trace_backup_do_cow_write_fail(job, start, ret);
357786
@@ -156,6 +159,8 @@ static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
357786
     int nr_clusters;
357786
     BlockBackend *blk = job->common.blk;
357786
     int nbytes;
357786
+    int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
357786
+    int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
357786
 
357786
     assert(QEMU_IS_ALIGNED(job->copy_range_size, job->cluster_size));
357786
     nbytes = MIN(job->copy_range_size, end - start);
357786
@@ -163,7 +168,7 @@ static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
357786
     hbitmap_reset(job->copy_bitmap, start / job->cluster_size,
357786
                   nr_clusters);
357786
     ret = blk_co_copy_range(blk, start, job->target, start, nbytes,
357786
-                            is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0, 0);
357786
+                            read_flags, write_flags);
357786
     if (ret < 0) {
357786
         trace_backup_do_cow_copy_range_fail(job, start, ret);
357786
         hbitmap_set(job->copy_bitmap, start / job->cluster_size,
357786
@@ -701,6 +706,9 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
357786
                        sync_bitmap : NULL;
357786
     job->compress = compress;
357786
 
357786
+    /* Detect image-fleecing (and similar) schemes */
357786
+    job->serialize_target_writes = bdrv_chain_contains(target, bs);
357786
+
357786
     /* If there is no backing file on the target, we cannot rely on COW if our
357786
      * backup cluster size is smaller than the target cluster size. Even for
357786
      * targets with a backing file, try to avoid COW if possible. */
357786
-- 
357786
1.8.3.1
357786