From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, stefanha@redhat.com, hreitz@redhat.com,
eblake@redhat.com, qemu-devel@nongnu.org
Subject: [PATCH v2 10/11] graph-lock: Unlock the AioContext while polling
Date: Mon, 5 Jun 2023 10:57:10 +0200 [thread overview]
Message-ID: <20230605085711.21261-11-kwolf@redhat.com> (raw)
In-Reply-To: <20230605085711.21261-1-kwolf@redhat.com>
If the caller keeps the AioContext lock for a block node in an iothread,
polling in bdrv_graph_wrlock() deadlocks if the condition isn't
fulfilled immediately.
Now that all callers make sure to actually have the AioContext locked
when they call bdrv_replace_child_noperm() like they should, we can
change bdrv_graph_wrlock() to take a BlockDriverState whose AioContext
lock the caller holds (NULL if it doesn't) and unlock it temporarily
while polling.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
include/block/graph-lock.h | 6 ++++--
block.c | 4 ++--
block/graph-lock.c | 23 ++++++++++++++++++++++-
3 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h
index 7574a2de5b..7e04f98ff0 100644
--- a/include/block/graph-lock.h
+++ b/include/block/graph-lock.h
@@ -111,10 +111,12 @@ void unregister_aiocontext(AioContext *ctx);
* The wrlock can only be taken from the main loop, with BQL held, as only the
* main loop is allowed to modify the graph.
*
+ * If @bs is non-NULL, its AioContext is temporarily released.
+ *
* This function polls. Callers must not hold the lock of any AioContext other
- * than the current one.
+ * than the current one and the one of @bs.
*/
-void bdrv_graph_wrlock(void) TSA_ACQUIRE(graph_lock) TSA_NO_TSA;
+void bdrv_graph_wrlock(BlockDriverState *bs) TSA_ACQUIRE(graph_lock) TSA_NO_TSA;
/*
* bdrv_graph_wrunlock:
diff --git a/block.c b/block.c
index d5f8231f31..f94cee8930 100644
--- a/block.c
+++ b/block.c
@@ -2854,7 +2854,7 @@ uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm)
* Replaces the node that a BdrvChild points to without updating permissions.
*
* If @new_bs is non-NULL, the parent of @child must already be drained through
- * @child.
+ * @child and the caller must hold the AioContext lock for @new_bs.
*/
static void bdrv_replace_child_noperm(BdrvChild *child,
BlockDriverState *new_bs)
@@ -2893,7 +2893,7 @@ static void bdrv_replace_child_noperm(BdrvChild *child,
}
/* TODO Pull this up into the callers to avoid polling here */
- bdrv_graph_wrlock();
+ bdrv_graph_wrlock(new_bs);
if (old_bs) {
if (child->klass->detach) {
child->klass->detach(child);
diff --git a/block/graph-lock.c b/block/graph-lock.c
index a92c6ae219..3bf2591dc4 100644
--- a/block/graph-lock.c
+++ b/block/graph-lock.c
@@ -110,8 +110,10 @@ static uint32_t reader_count(void)
}
#endif
-void bdrv_graph_wrlock(void)
+void bdrv_graph_wrlock(BlockDriverState *bs)
{
+ AioContext *ctx = NULL;
+
GLOBAL_STATE_CODE();
/*
* TODO Some callers hold an AioContext lock when this is called, which
@@ -120,7 +122,22 @@ void bdrv_graph_wrlock(void)
*/
#if 0
assert(!qatomic_read(&has_writer));
+#endif
+
+ /*
+ * Release only non-mainloop AioContext. The mainloop often relies on the
+ * BQL and doesn't lock the main AioContext before doing things.
+ */
+ if (bs) {
+ ctx = bdrv_get_aio_context(bs);
+ if (ctx != qemu_get_aio_context()) {
+ aio_context_release(ctx);
+ } else {
+ ctx = NULL;
+ }
+ }
+#if 0
/* Make sure that constantly arriving new I/O doesn't cause starvation */
bdrv_drain_all_begin_nopoll();
@@ -150,6 +167,10 @@ void bdrv_graph_wrlock(void)
bdrv_drain_all_end();
#endif
+
+ if (ctx) {
+ aio_context_acquire(bdrv_get_aio_context(bs));
+ }
}
void bdrv_graph_wrunlock(void)
--
2.40.1
next prev parent reply other threads:[~2023-06-05 9:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-05 8:57 [PATCH v2 00/11] block: Re-enable the graph lock Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 01/11] iotests: Test active commit with iothread and background I/O Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 02/11] qdev-properties-system: Lock AioContext for blk_insert_bs() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 03/11] test-block-iothread: " Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 04/11] block: Fix AioContext locking in bdrv_open_child() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 05/11] block: Fix AioContext locking in bdrv_attach_child_common() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 06/11] block: Fix AioContext locking in bdrv_reopen_parse_file_or_backing() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 07/11] block: Fix AioContext locking in bdrv_open_inherit() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 08/11] block: Fix AioContext locking in bdrv_open_backing_file() Kevin Wolf
2023-06-05 8:57 ` [PATCH v2 09/11] blockjob: Fix AioContext locking in block_job_add_bdrv() Kevin Wolf
2023-06-05 8:57 ` Kevin Wolf [this message]
2023-06-05 8:57 ` [PATCH v2 11/11] Revert "graph-lock: Disable locking for now" Kevin Wolf
2023-06-05 9:06 ` Paolo Bonzini
2023-06-05 10:03 ` Kevin Wolf
2023-06-07 18:19 ` [PATCH v2 00/11] block: Re-enable the graph lock Stefan Hajnoczi
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=20230605085711.21261-11-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=eblake@redhat.com \
--cc=hreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.