[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 04/32] block-backend: Fix blk_new_open() for iothreads
From: |
Kevin Wolf |
Subject: |
[PULL 04/32] block-backend: Fix blk_new_open() for iothreads |
Date: |
Tue, 30 May 2023 18:32:11 +0200 |
This fixes blk_new_open() to not assume that bs is in the main context.
In particular, the BlockBackend must be created with the right
AioContext because it will refuse to move to a different context
afterwards. (blk->allow_aio_context_change is false.)
Use this opportunity to use blk_insert_bs() instead of duplicating the
bdrv_root_attach_child() call. This is consistent with what
blk_new_with_bs() does. Add comments to document the locking rules.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20230525124713.401149-5-kwolf@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/block-backend.c | 27 +++++++++++++++++++++------
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/block/block-backend.c b/block/block-backend.c
index 1d89fabd35..dde60e0f71 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -389,6 +389,8 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm,
uint64_t shared_perm)
* Both sets of permissions can be changed later using blk_set_perm().
*
* Return the new BlockBackend on success, null on failure.
+ *
+ * Callers must hold the AioContext lock of @bs.
*/
BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm,
uint64_t shared_perm, Error **errp)
@@ -406,11 +408,15 @@ BlockBackend *blk_new_with_bs(BlockDriverState *bs,
uint64_t perm,
/*
* Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
- * The new BlockBackend is in the main AioContext.
+ * By default, the new BlockBackend is in the main AioContext, but if the
+ * parameters connect it with any existing node in a different AioContext, it
+ * may end up there instead.
*
* Just as with bdrv_open(), after having called this function the reference to
* @options belongs to the block layer (even on failure).
*
+ * Called without holding an AioContext lock.
+ *
* TODO: Remove @filename and @flags; it should be possible to specify a whole
* BDS tree just by specifying the @options QDict (or @reference,
* alternatively). At the time of adding this function, this is not possible,
@@ -422,6 +428,7 @@ BlockBackend *blk_new_open(const char *filename, const char
*reference,
{
BlockBackend *blk;
BlockDriverState *bs;
+ AioContext *ctx;
uint64_t perm = 0;
uint64_t shared = BLK_PERM_ALL;
@@ -451,18 +458,24 @@ BlockBackend *blk_new_open(const char *filename, const
char *reference,
shared = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
}
- blk = blk_new(qemu_get_aio_context(), perm, shared);
aio_context_acquire(qemu_get_aio_context());
bs = bdrv_open(filename, reference, options, flags, errp);
aio_context_release(qemu_get_aio_context());
if (!bs) {
- blk_unref(blk);
return NULL;
}
- blk->root = bdrv_root_attach_child(bs, "root", &child_root,
- BDRV_CHILD_FILTERED |
BDRV_CHILD_PRIMARY,
- perm, shared, blk, errp);
+ /* bdrv_open() could have moved bs to a different AioContext */
+ ctx = bdrv_get_aio_context(bs);
+ blk = blk_new(bdrv_get_aio_context(bs), perm, shared);
+ blk->perm = perm;
+ blk->shared_perm = shared;
+
+ aio_context_acquire(ctx);
+ blk_insert_bs(blk, bs, errp);
+ bdrv_unref(bs);
+ aio_context_release(ctx);
+
if (!blk->root) {
blk_unref(blk);
return NULL;
@@ -903,6 +916,8 @@ void blk_remove_bs(BlockBackend *blk)
/*
* Associates a new BlockDriverState with @blk.
+ *
+ * Callers must hold the AioContext lock of @bs.
*/
int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
{
--
2.40.1
- [PULL 00/32] Block layer patches, Kevin Wolf, 2023/05/30
- [PULL 06/32] qcow2: Fix open with 'file' in iothread, Kevin Wolf, 2023/05/30
- [PULL 05/32] mirror: Hold main AioContext lock for calling bdrv_open_backing_file(), Kevin Wolf, 2023/05/30
- [PULL 08/32] copy-before-write: Fix open with child in iothread, Kevin Wolf, 2023/05/30
- [PULL 03/32] block: Take main AioContext lock when calling bdrv_open(), Kevin Wolf, 2023/05/30
- [PULL 02/32] block: Clarify locking rules for bdrv_open(_inherit)(), Kevin Wolf, 2023/05/30
- [PULL 01/32] block-coroutine-wrapper: Take AioContext lock in no_co_wrappers, Kevin Wolf, 2023/05/30
- [PULL 04/32] block-backend: Fix blk_new_open() for iothreads,
Kevin Wolf <=
- [PULL 19/32] block/export: stop using is_external in vhost-user-blk server, Kevin Wolf, 2023/05/30
- [PULL 16/32] virtio-scsi: stop using aio_disable_external() during unplug, Kevin Wolf, 2023/05/30
- [PULL 15/32] virtio-scsi: avoid race between unplug and transport event, Kevin Wolf, 2023/05/30
- [PULL 17/32] util/vhost-user-server: rename refcount to in_flight counter, Kevin Wolf, 2023/05/30
- [PULL 18/32] block/export: wait for vhost-user-blk requests when draining, Kevin Wolf, 2023/05/30
- [PULL 24/32] hw/xen: do not set is_external=true on evtchn fds, Kevin Wolf, 2023/05/30
- [PULL 26/32] block/export: don't require AioContext lock around blk_exp_ref/unref(), Kevin Wolf, 2023/05/30
- [PULL 22/32] block: drain from main loop thread in bdrv_co_yield_to_drain(), Kevin Wolf, 2023/05/30
- [PULL 30/32] virtio-scsi: implement BlockDevOps->drained_begin(), Kevin Wolf, 2023/05/30
- [PULL 07/32] raw-format: Fix open with 'file' in iothread, Kevin Wolf, 2023/05/30