qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Stefan Weil <sw@weilnetz.de>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH] block/vdi: Fix locking for parallel requests
Date: Tue, 17 Feb 2015 16:33:39 -0500	[thread overview]
Message-ID: <1424208819-10306-1-git-send-email-mreitz@redhat.com> (raw)

Concurrently modifying the bmap is not a good idea; this patch adds a
lock for it. See https://bugs.launchpad.net/qemu/+bug/1422307 for what
can go wrong without.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/vdi.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/block/vdi.c b/block/vdi.c
index 74030c6..c5ff428 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -51,6 +51,7 @@
 
 #include "qemu-common.h"
 #include "block/block_int.h"
+#include "block/coroutine.h"
 #include "qemu/module.h"
 #include "migration/migration.h"
 
@@ -196,6 +197,8 @@ typedef struct {
     /* VDI header (converted to host endianness). */
     VdiHeader header;
 
+    CoMutex bmap_lock;
+
     Error *migration_blocker;
 } BDRVVdiState;
 
@@ -498,6 +501,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail_free_bmap;
     }
 
+    qemu_co_mutex_init(&s->bmap_lock);
+
     /* Disable migration when vdi images are used */
     error_set(&s->migration_blocker,
               QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
@@ -619,6 +624,9 @@ static int vdi_co_write(BlockDriverState *bs,
                n_sectors, sector_num);
 
         /* prepare next AIO request */
+        if (!block) {
+            qemu_co_mutex_lock(&s->bmap_lock);
+        }
         bmap_entry = le32_to_cpu(s->bmap[block_index]);
         if (!VDI_IS_ALLOCATED(bmap_entry)) {
             /* Allocate new block and write to it. */
@@ -641,9 +649,13 @@ static int vdi_co_write(BlockDriverState *bs,
                    (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
             ret = bdrv_write(bs->file, offset, block, s->block_sectors);
         } else {
-            uint64_t offset = s->header.offset_data / SECTOR_SIZE +
-                              (uint64_t)bmap_entry * s->block_sectors +
-                              sector_in_block;
+            uint64_t offset;
+
+            qemu_co_mutex_unlock(&s->bmap_lock);
+
+            offset = s->header.offset_data / SECTOR_SIZE +
+                     (uint64_t)bmap_entry * s->block_sectors +
+                     sector_in_block;
             ret = bdrv_write(bs->file, offset, buf, n_sectors);
         }
 
@@ -656,6 +668,9 @@ static int vdi_co_write(BlockDriverState *bs,
 
     logout("finished data write\n");
     if (ret < 0) {
+        if (block) {
+            qemu_co_mutex_unlock(&s->bmap_lock);
+        }
         return ret;
     }
 
@@ -688,6 +703,8 @@ static int vdi_co_write(BlockDriverState *bs,
         logout("will write %u block map sectors starting from entry %u\n",
                n_sectors, bmap_first);
         ret = bdrv_write(bs->file, offset, base, n_sectors);
+
+        qemu_co_mutex_unlock(&s->bmap_lock);
     }
 
     return ret;
-- 
2.1.0

             reply	other threads:[~2015-02-17 21:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-17 21:33 Max Reitz [this message]
2015-02-17 21:49 ` [Qemu-devel] [PATCH] block/vdi: Fix locking for parallel requests Paolo Bonzini
2015-02-17 21:49   ` Max Reitz
2015-02-18  7:52 ` Paolo Bonzini
2015-02-18 15:10   ` Max Reitz
2015-02-18 12:39 ` Kevin Wolf
2015-02-18 15:09   ` Max Reitz
2015-02-27 11:25 ` Stefan Hajnoczi
2015-02-27 11:43   ` Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1424208819-10306-1-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=sw@weilnetz.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).