qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com, famz@redhat.com,
	pbonzini@redhat.com, slp@redhat.com, jsnow@redhat.com,
	qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH v2 08/17] block: Add missing locking in bdrv_co_drain_bh_cb()
Date: Thu, 13 Sep 2018 14:52:08 +0200	[thread overview]
Message-ID: <20180913125217.23173-9-kwolf@redhat.com> (raw)
In-Reply-To: <20180913125217.23173-1-kwolf@redhat.com>

bdrv_do_drained_begin/end() assume that they are called with the
AioContext lock of bs held. If we call drain functions from a coroutine
with the AioContext lock held, we yield and schedule a BH to move out of
coroutine context. This means that the lock for the home context of the
coroutine is released and must be re-acquired in the bottom half.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/qemu/coroutine.h |  5 +++++
 block/io.c               | 15 +++++++++++++++
 util/qemu-coroutine.c    |  5 +++++
 3 files changed, 25 insertions(+)

diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 6f8a487041..9801e7f5a4 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -90,6 +90,11 @@ void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co);
 void coroutine_fn qemu_coroutine_yield(void);
 
 /**
+ * Get the AioContext of the given coroutine
+ */
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co);
+
+/**
  * Get the currently executing coroutine
  */
 Coroutine *coroutine_fn qemu_coroutine_self(void);
diff --git a/block/io.c b/block/io.c
index 7100344c7b..914ba78f1a 100644
--- a/block/io.c
+++ b/block/io.c
@@ -288,6 +288,18 @@ static void bdrv_co_drain_bh_cb(void *opaque)
     BlockDriverState *bs = data->bs;
 
     if (bs) {
+        AioContext *ctx = bdrv_get_aio_context(bs);
+        AioContext *co_ctx = qemu_coroutine_get_aio_context(co);
+
+        /*
+         * When the coroutine yielded, the lock for its home context was
+         * released, so we need to re-acquire it here. If it explicitly
+         * acquired a different context, the lock is still held and we don't
+         * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
+         */
+        if (ctx == co_ctx) {
+            aio_context_acquire(ctx);
+        }
         bdrv_dec_in_flight(bs);
         if (data->begin) {
             bdrv_do_drained_begin(bs, data->recursive, data->parent,
@@ -296,6 +308,9 @@ static void bdrv_co_drain_bh_cb(void *opaque)
             bdrv_do_drained_end(bs, data->recursive, data->parent,
                                 data->ignore_bds_parents);
         }
+        if (ctx == co_ctx) {
+            aio_context_release(ctx);
+        }
     } else {
         assert(data->begin);
         bdrv_drain_all_begin();
diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
index 1ba4191b84..2295928d33 100644
--- a/util/qemu-coroutine.c
+++ b/util/qemu-coroutine.c
@@ -198,3 +198,8 @@ bool qemu_coroutine_entered(Coroutine *co)
 {
     return co->caller;
 }
+
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co)
+{
+    return co->ctx;
+}
-- 
2.13.6

  parent reply	other threads:[~2018-09-13 12:53 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-13 12:52 [Qemu-devel] [PATCH v2 00/17] Fix some jobs/drain/aio_poll related hangs Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 01/17] job: Fix missing locking due to mismerge Kevin Wolf
2018-09-13 13:56   ` Max Reitz
2018-09-13 17:38   ` John Snow
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 02/17] blockjob: Wake up BDS when job becomes idle Kevin Wolf
2018-09-13 14:31   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 03/17] aio-wait: Increase num_waiters even in home thread Kevin Wolf
2018-09-13 15:11   ` Paolo Bonzini
2018-09-13 17:21     ` Kevin Wolf
2018-09-14 15:14       ` Paolo Bonzini
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 04/17] test-bdrv-drain: Drain with block jobs in an I/O thread Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 05/17] test-blockjob: Acquire AioContext around job_cancel_sync() Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 06/17] job: Use AIO_WAIT_WHILE() in job_finish_sync() Kevin Wolf
2018-09-13 14:45   ` Max Reitz
2018-09-13 15:15   ` Paolo Bonzini
2018-09-13 17:39     ` Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 07/17] test-bdrv-drain: Test AIO_WAIT_WHILE() in completion callback Kevin Wolf
2018-09-13 12:52 ` Kevin Wolf [this message]
2018-09-13 14:58   ` [Qemu-devel] [PATCH v2 08/17] block: Add missing locking in bdrv_co_drain_bh_cb() Max Reitz
2018-09-13 15:17   ` Paolo Bonzini
2018-09-13 17:36     ` Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 09/17] block-backend: Add .drained_poll callback Kevin Wolf
2018-09-13 15:01   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 10/17] block-backend: Fix potential double blk_delete() Kevin Wolf
2018-09-13 15:19   ` Paolo Bonzini
2018-09-13 19:50   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 11/17] block-backend: Decrease in_flight only after callback Kevin Wolf
2018-09-13 15:10   ` Paolo Bonzini
2018-09-13 16:59     ` Kevin Wolf
2018-09-14  7:47       ` Fam Zheng
2018-09-14 15:12       ` Paolo Bonzini
2018-09-14 17:14         ` Kevin Wolf
2018-09-14 17:38           ` Paolo Bonzini
2018-09-13 20:50   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 12/17] mirror: Fix potential use-after-free in active commit Kevin Wolf
2018-09-13 20:55   ` Max Reitz
2018-09-13 21:43     ` Max Reitz
2018-09-14 16:25     ` Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 13/17] blockjob: Lie better in child_job_drained_poll() Kevin Wolf
2018-09-13 21:52   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 14/17] block: Remove aio_poll() in bdrv_drain_poll variants Kevin Wolf
2018-09-13 21:55   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 15/17] test-bdrv-drain: Test nested poll in bdrv_drain_poll_top_level() Kevin Wolf
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 16/17] job: Avoid deadlocks in job_completed_txn_abort() Kevin Wolf
2018-09-13 22:01   ` Max Reitz
2018-09-13 12:52 ` [Qemu-devel] [PATCH v2 17/17] test-bdrv-drain: AIO_WAIT_WHILE() in job .commit/.abort Kevin Wolf
2018-09-13 22:05   ` Max Reitz

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=20180913125217.23173-9-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=famz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=slp@redhat.com \
    /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).