0a122b
From d8c60f4fc2bb0fb0e5b363cd61230e103fbff3ab Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Wed, 4 Dec 2013 17:08:50 +0100
0a122b
Subject: [PATCH 21/37] block: Make overlap range for serialisation dynamic
0a122b
0a122b
Message-id: <1392117622-28812-22-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: 57186
0a122b
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 21/37] block: Make overlap range for serialisation dynamic
0a122b
Bugzilla: 748906
0a122b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
0a122b
0a122b
Copy on Read wants to serialise with all requests touching the same
0a122b
cluster, so wait_serialising_requests() rounded to cluster boundaries.
0a122b
Other users like alignment RMW will have different requirements, though
0a122b
(requests touching the same sector), so make it dynamic.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
Reviewed-by: Max Reitz <mreitz@redhat.com>
0a122b
Reviewed-by: Benoit Canet <benoit@irqsave.net>
0a122b
(cherry picked from commit 7327145f63a224c9ba9c16d0c29781feffef8dc6)
0a122b
0a122b
Conflicts:
0a122b
	include/block/block_int.h
0a122b
0a122b
Conflicts because in RHEL 7 BdrvTrackedRequest is in block.c
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block.c | 57 +++++++++++++++++++++++++++++++--------------------------
0a122b
 1 file changed, 31 insertions(+), 26 deletions(-)
0a122b
---
0a122b
 block.c |   57 +++++++++++++++++++++++++++++++--------------------------
0a122b
 1 files changed, 31 insertions(+), 26 deletions(-)
0a122b
0a122b
diff --git a/block.c b/block.c
0a122b
index 7b30bb3..24e94e6 100644
0a122b
--- a/block.c
0a122b
+++ b/block.c
0a122b
@@ -2039,7 +2039,11 @@ struct BdrvTrackedRequest {
0a122b
     int64_t offset;
0a122b
     unsigned int bytes;
0a122b
     bool is_write;
0a122b
+
0a122b
     bool serialising;
0a122b
+    int64_t overlap_offset;
0a122b
+    unsigned int overlap_bytes;
0a122b
+
0a122b
     QLIST_ENTRY(BdrvTrackedRequest) list;
0a122b
     Coroutine *co; /* owner, used for deadlock detection */
0a122b
     CoQueue wait_queue; /* coroutines blocked on this request */
0a122b
@@ -2075,6 +2079,8 @@ static void tracked_request_begin(BdrvTrackedRequest *req,
0a122b
         .is_write       = is_write,
0a122b
         .co             = qemu_coroutine_self(),
0a122b
         .serialising    = false,
0a122b
+        .overlap_offset = offset,
0a122b
+        .overlap_bytes  = bytes,
0a122b
     };
0a122b
 
0a122b
     qemu_co_queue_init(&req->wait_queue);
0a122b
@@ -2082,12 +2088,19 @@ static void tracked_request_begin(BdrvTrackedRequest *req,
0a122b
     QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
0a122b
 }
0a122b
 
0a122b
-static void mark_request_serialising(BdrvTrackedRequest *req)
0a122b
+static void mark_request_serialising(BdrvTrackedRequest *req, size_t align)
0a122b
 {
0a122b
+    int64_t overlap_offset = req->offset & ~(align - 1);
0a122b
+    int overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
0a122b
+                      - overlap_offset;
0a122b
+
0a122b
     if (!req->serialising) {
0a122b
         req->bs->serialising_in_flight++;
0a122b
         req->serialising = true;
0a122b
     }
0a122b
+
0a122b
+    req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
0a122b
+    req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
0a122b
 }
0a122b
 
0a122b
 /**
0a122b
@@ -2111,20 +2124,16 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
0a122b
     }
0a122b
 }
0a122b
 
0a122b
-static void round_bytes_to_clusters(BlockDriverState *bs,
0a122b
-                                    int64_t offset, unsigned int bytes,
0a122b
-                                    int64_t *cluster_offset,
0a122b
-                                    unsigned int *cluster_bytes)
0a122b
+static int bdrv_get_cluster_size(BlockDriverState *bs)
0a122b
 {
0a122b
     BlockDriverInfo bdi;
0a122b
+    int ret;
0a122b
 
0a122b
-    if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
0a122b
-        *cluster_offset = offset;
0a122b
-        *cluster_bytes = bytes;
0a122b
+    ret = bdrv_get_info(bs, &bdi);
0a122b
+    if (ret < 0 || bdi.cluster_size == 0) {
0a122b
+        return bs->request_alignment;
0a122b
     } else {
0a122b
-        *cluster_offset = QEMU_ALIGN_DOWN(offset, bdi.cluster_size);
0a122b
-        *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes,
0a122b
-                                       bdi.cluster_size);
0a122b
+        return bdi.cluster_size;
0a122b
     }
0a122b
 }
0a122b
 
0a122b
@@ -2132,11 +2141,11 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
0a122b
                                      int64_t offset, unsigned int bytes)
0a122b
 {
0a122b
     /*        aaaa   bbbb */
0a122b
-    if (offset >= req->offset + req->bytes) {
0a122b
+    if (offset >= req->overlap_offset + req->overlap_bytes) {
0a122b
         return false;
0a122b
     }
0a122b
     /* bbbb   aaaa        */
0a122b
-    if (req->offset >= offset + bytes) {
0a122b
+    if (req->overlap_offset >= offset + bytes) {
0a122b
         return false;
0a122b
     }
0a122b
     return true;
0a122b
@@ -2146,30 +2155,21 @@ static void coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
0a122b
 {
0a122b
     BlockDriverState *bs = self->bs;
0a122b
     BdrvTrackedRequest *req;
0a122b
-    int64_t cluster_offset;
0a122b
-    unsigned int cluster_bytes;
0a122b
     bool retry;
0a122b
 
0a122b
     if (!bs->serialising_in_flight) {
0a122b
         return;
0a122b
     }
0a122b
 
0a122b
-    /* If we touch the same cluster it counts as an overlap.  This guarantees
0a122b
-     * that allocating writes will be serialized and not race with each other
0a122b
-     * for the same cluster.  For example, in copy-on-read it ensures that the
0a122b
-     * CoR read and write operations are atomic and guest writes cannot
0a122b
-     * interleave between them.
0a122b
-     */
0a122b
-    round_bytes_to_clusters(bs, self->offset, self->bytes,
0a122b
-                            &cluster_offset, &cluster_bytes);
0a122b
-
0a122b
     do {
0a122b
         retry = false;
0a122b
         QLIST_FOREACH(req, &bs->tracked_requests, list) {
0a122b
             if (req == self || (!req->serialising && !self->serialising)) {
0a122b
                 continue;
0a122b
             }
0a122b
-            if (tracked_request_overlaps(req, cluster_offset, cluster_bytes)) {
0a122b
+            if (tracked_request_overlaps(req, self->overlap_offset,
0a122b
+                                         self->overlap_bytes))
0a122b
+            {
0a122b
                 /* Hitting this means there was a reentrant request, for
0a122b
                  * example, a block driver issuing nested requests.  This must
0a122b
                  * never happen since it means deadlock.
0a122b
@@ -2780,7 +2780,12 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
0a122b
 
0a122b
     /* Handle Copy on Read and associated serialisation */
0a122b
     if (flags & BDRV_REQ_COPY_ON_READ) {
0a122b
-        mark_request_serialising(req);
0a122b
+        /* If we touch the same cluster it counts as an overlap.  This
0a122b
+         * guarantees that allocating writes will be serialized and not race
0a122b
+         * with each other for the same cluster.  For example, in copy-on-read
0a122b
+         * it ensures that the CoR read and write operations are atomic and
0a122b
+         * guest writes cannot interleave between them. */
0a122b
+        mark_request_serialising(req, bdrv_get_cluster_size(bs));
0a122b
     }
0a122b
 
0a122b
     wait_serialising_requests(req);
0a122b
-- 
0a122b
1.7.1
0a122b