qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	Alberto Garcia <berto@igalia.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PULL 14/17] throttle: acquire the ThrottleGroup lock in bdrv_swap()
Date: Fri,  5 Jun 2015 12:57:29 +0100	[thread overview]
Message-ID: <1433505452-11692-15-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1433505452-11692-1-git-send-email-stefanha@redhat.com>

From: Alberto Garcia <berto@igalia.com>

bdrv_swap() touches the fields of a BlockDriverState that are
protected by the ThrottleGroup lock. Although those fields end up in
their original place, they are temporarily swapped in the process,
so there's a chance that an operation on a member of the same group
happening on a different thread can try to use them.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 59ff27e9afb45a9f694bb0fa6a86b80494fa0bc8.1432037840.git.berto@igalia.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block.c                         | 16 ++++++++++++++++
 block/throttle-groups.c         | 31 ++++++++++++++++++++++++++++++-
 include/block/throttle-groups.h |  3 +++
 3 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/block.c b/block.c
index fb993f2..53918cf 100644
--- a/block.c
+++ b/block.c
@@ -36,6 +36,7 @@
 #include "qmp-commands.h"
 #include "qemu/timer.h"
 #include "qapi-event.h"
+#include "block/throttle-groups.h"
 
 #ifdef CONFIG_BSD
 #include <sys/types.h>
@@ -1887,11 +1888,20 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
         QTAILQ_REMOVE(&graph_bdrv_states, bs_old, node_list);
     }
 
+    /* If the BlockDriverState is part of a throttling group acquire
+     * its lock since we're going to mess with the protected fields.
+     * Otherwise there's no need to worry since no one else can touch
+     * them. */
+    if (bs_old->throttle_state) {
+        throttle_group_lock(bs_old);
+    }
+
     /* bs_new must be unattached and shouldn't have anything fancy enabled */
     assert(!bs_new->blk);
     assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
     assert(bs_new->job == NULL);
     assert(bs_new->io_limits_enabled == false);
+    assert(bs_new->throttle_state == NULL);
     assert(!throttle_timers_are_initialized(&bs_new->throttle_timers));
 
     tmp = *bs_new;
@@ -1909,8 +1919,14 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     /* Check a few fields that should remain attached to the device */
     assert(bs_new->job == NULL);
     assert(bs_new->io_limits_enabled == false);
+    assert(bs_new->throttle_state == NULL);
     assert(!throttle_timers_are_initialized(&bs_new->throttle_timers));
 
+    /* Release the ThrottleGroup lock */
+    if (bs_old->throttle_state) {
+        throttle_group_unlock(bs_old);
+    }
+
     /* insert the nodes back into the graph node list if needed */
     if (bs_new->node_name[0] != '\0') {
         QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_new, node_list);
diff --git a/block/throttle-groups.c b/block/throttle-groups.c
index da8c70c..efc462f 100644
--- a/block/throttle-groups.c
+++ b/block/throttle-groups.c
@@ -33,7 +33,8 @@
  * its own locking.
  *
  * This locking is however handled internally in this file, so it's
- * transparent to outside users.
+ * mostly transparent to outside users (but see the documentation in
+ * throttle_groups_lock()).
  *
  * The whole ThrottleGroup structure is private and invisible to
  * outside users, that only use it through its ThrottleState.
@@ -459,6 +460,34 @@ void throttle_group_unregister_bs(BlockDriverState *bs)
     bs->throttle_state = NULL;
 }
 
+/* Acquire the lock of this throttling group.
+ *
+ * You won't normally need to use this. None of the functions from the
+ * ThrottleGroup API require you to acquire the lock since all of them
+ * deal with it internally.
+ *
+ * This should only be used in exceptional cases when you want to
+ * access the protected fields of a BlockDriverState directly
+ * (e.g. bdrv_swap()).
+ *
+ * @bs: a BlockDriverState that is member of the group
+ */
+void throttle_group_lock(BlockDriverState *bs)
+{
+    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
+    qemu_mutex_lock(&tg->lock);
+}
+
+/* Release the lock of this throttling group.
+ *
+ * See the comments in throttle_group_lock().
+ */
+void throttle_group_unlock(BlockDriverState *bs)
+{
+    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
+    qemu_mutex_unlock(&tg->lock);
+}
+
 static void throttle_groups_init(void)
 {
     qemu_mutex_init(&throttle_groups_lock);
diff --git a/include/block/throttle-groups.h b/include/block/throttle-groups.h
index 322139a..fab113f 100644
--- a/include/block/throttle-groups.h
+++ b/include/block/throttle-groups.h
@@ -40,4 +40,7 @@ void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
                                                         unsigned int bytes,
                                                         bool is_write);
 
+void throttle_group_lock(BlockDriverState *bs);
+void throttle_group_unlock(BlockDriverState *bs);
+
 #endif
-- 
2.4.2

  parent reply	other threads:[~2015-06-05 11:58 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-05 11:57 [Qemu-devel] [PULL 00/17] Block patches Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 01/17] block: Add bdrv_get_block_status_above Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 02/17] qmp: Add optional bool "unmap" to drive-mirror Stefan Hajnoczi
2015-06-08  5:41   ` Fam Zheng
2015-06-05 11:57 ` [Qemu-devel] [PULL 03/17] mirror: Do zero write on target if sectors not allocated Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 04/17] block: Fix dirty bitmap in bdrv_co_discard Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 05/17] block: Remove bdrv_reset_dirty Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 06/17] qemu-iotests: Make block job methods common Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 07/17] qemu-iotests: Add test case for mirror with unmap Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 08/17] iotests: Use event_wait in wait_ready Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 09/17] Revert "iothread: release iothread around aio_poll" Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 10/17] throttle: Extract timers from ThrottleState into a separate structure Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 11/17] throttle: Add throttle group infrastructure Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 12/17] throttle: Add throttle group infrastructure tests Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 13/17] throttle: Add throttle group support Stefan Hajnoczi
2015-06-05 11:57 ` Stefan Hajnoczi [this message]
2015-06-05 11:57 ` [Qemu-devel] [PULL 15/17] throttle: add the name of the ThrottleGroup to BlockDeviceInfo Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 16/17] throttle: Update throttle infrastructure copyright Stefan Hajnoczi
2015-06-05 11:57 ` [Qemu-devel] [PULL 17/17] qemu-iotests: expand test 093 to support group throttling Stefan Hajnoczi
2015-06-05 13:53 ` [Qemu-devel] [PULL 00/17] Block patches Peter Maydell

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=1433505452-11692-15-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=berto@igalia.com \
    --cc=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /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).