block: Fix bdrv_open*() calls from coroutine context
Subsystems (2)
cover block: Fix bdrv_open*() calls from coroutine context
bdrv_open*() must not be called from coroutine context, amongst others because it modifies the block graph. However, some functions - in particular all .bdrv_co_create* implementations of image formats - do call it from coroutine context. This is already wrong today, but when we add locking, it actually becomes visible.
This series adds no_co_wrapper functions, which are automatically generated wrappers that run in coroutine context and use a BH to call the wrapped function outside of coroutine context. It then uses these wrappers to fix the problematic bdrv_open*() calls.
Kevin Wolf (13):
block-coroutine-wrapper: Introduce no_co_wrapper
block: Create no_co_wrappers for open functions
luks: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
parallels: Fix .bdrv_co_create(_opts) to open images with
no_co_wrapper
qcow: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
qcow2: Fix open/create to open images with no_co_wrapper
qed: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
vdi: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
vhdx: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
vmdk: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
vpc: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
block: Fix bdrv_co_create_opts_simple() to open images with
no_co_wrapper
block: Assert non-coroutine context for bdrv_open_inherit()
include/block/block-common.h | 14 ++++ include/block/block-global-state.h | 35 ++++++--- include/sysemu/block-backend-global-state.h | 21 +++++- block.c | 17 ++--- block/crypto.c | 10 +-- block/parallels.c | 10 +-- block/qcow.c | 10 +-- block/qcow2.c | 43 +++++------ block/qed.c | 10 +-- block/vdi.c | 10 +-- block/vhdx.c | 10 +-- block/vmdk.c | 22 +++--- block/vpc.c | 10 +-- scripts/block-coroutine-wrapper.py | 83 ++++++++++++++++++--- block/meson.build | 1 + 15 files changed, 207 insertions(+), 99 deletions(-)
Signature · 1 line
2.38.1
PATCH 1/13 block-coroutine-wrapper: Introduce no_co_wrapper
Some functions must not be called from coroutine context. The common pattern to use them anyway from a coroutine is running them in a BH and letting the calling coroutine yield to be woken up when the BH is completed.
Instead of manually writing such wrappers, add support for generating them to block-coroutine-wrapper.
--- include/block/block-common.h | 14 +++++ scripts/block-coroutine-wrapper.py | 83 ++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 11 deletions(-)
include/block/block-common.h +14 -0
@@ -54,6 +54,20 @@#define co_wrapper_bdrv_rdlock no_coroutine_fn#define co_wrapper_mixed_bdrv_rdlock no_coroutine_fn coroutine_mixed_fn +/* +* no_co_wrapper: Function specifier used by block-coroutine-wrapper.py +* +* Function specifier which does nothing but mark functions to be generated by +* scripts/block-coroutine-wrapper.py. +* +* A no_co_wrapper function declaration creates a coroutine_fn wrapper around +* functions that must not be called in coroutine context. It achieves this by +* scheduling a BH in the bottom half that runs the respective non-coroutine +* function. The coroutine yields after scheduling the BH and is reentered when +* the wrapped function returns. +*/ +#define no_co_wrapper +#include "block/blockjob.h" /* block.c */
scripts/block-coroutine-wrapper.py +72 -11
137 lines not on this page · raw message
Signature · 1 line
2.38.1
PATCH 2/13 block: Create no_co_wrappers for open functions
In reply to Kevin WolfImages can't be opened in coroutine context because opening needs to change the block graph. Add no_co_wrappers so that coroutines have a simple way of opening images in a BH instead.
At the same time, mark the wrapped functions as no_coroutine_fn.
--- include/block/block-global-state.h | 35 +++++++++++++++------ include/sysemu/block-backend-global-state.h | 21 ++++++++++--- block/meson.build | 1 + 3 files changed, 44 insertions(+), 13 deletions(-)
include/block/block-global-state.h +26 -9
@@ -77,16 +77,26 @@ BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options, int flags, Error **errp);int bdrv_drop_filter (BlockDriverState *bs,Error **errp); -BdrvChild *bdrv_open_child (const char *filename, -QDict *options,const char *bdref_key, -BlockDriverState *parent, -const BdrvChildClass *child_class, -BdrvChildRole child_role, -bool allow_none,Error **errp); +BdrvChild * no_coroutine_fn +bdrv_open_child(const char *filename, QDict *options, const char *bdref_key, + BlockDriverState *parent, const BdrvChildClass *child_class, + BdrvChildRole child_role, bool allow_none, Error **errp); + +BdrvChild * coroutine_fn no_co_wrapper +bdrv_co_open_child (const char *filename,QDict *options,const char *bdref_key, +BlockDriverState *parent,const BdrvChildClass *child_class, +BdrvChildRole child_role,bool allow_none,Error **errp); +int bdrv_open_file_child (const char *filename,QDict *options,const char *bdref_key,BlockDriverState *parent,Error **errp); -BlockDriverState *bdrv_open_blockdev_ref (BlockdevRef *ref,Error **errp); + +BlockDriverState * no_coroutine_fn +bdrv_open_blockdev_ref (BlockdevRef *ref, Error **errp); + +BlockDriverState * coroutine_fn no_co_wrapper +bdrv_co_open_blockdev_ref (BlockdevRef *ref,Error **errp); +int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd,Error **errp); int bdrv_set_backing_hd_drained(BlockDriverState *bs,
@@ -94,8 +104,15 @@ int bdrv_set_backing_hd_drained(BlockDriverState *bs, Error **errp);int bdrv_open_backing_file (BlockDriverState *bs,QDict *parent_options,const char *bdref_key,Error **errp); -BlockDriverState *bdrv_open (const char *filename,const char *reference, -QDict *options,int flags,Error **errp); + +BlockDriverState * no_coroutine_fn +bdrv_open(const char *filename, const char *reference, QDict *options, + int flags, Error **errp); + +BlockDriverState * coroutine_fn no_co_wrapper +bdrv_co_open (const char *filename,const char *reference, +QDict *options,int flags,Error **errp); + BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv, const char *node_name, QDict *options, int flags,
include/sysemu/block-backend-global-state.h +17 -4
@@ -23,10 +23,23 @@*/ BlockBackend *blk_new (AioContext *ctx,uint64_t perm,uint64_t shared_perm); -BlockBackend *blk_new_with_bs (BlockDriverState *bs,uint64_t perm, -uint64_t shared_perm,Error **errp); -BlockBackend *blk_new_open (const char *filename,const char *reference, -QDict *options,int flags,Error **errp); + +BlockBackend * no_coroutine_fn +blk_new_with_bs(BlockDriverState *bs, uint64_t perm, uint64_t shared_perm, + Error **errp); + +BlockBackend * coroutine_fn no_co_wrapper +blk_co_new_with_bs (BlockDriverState *bs,uint64_t perm,uint64_t shared_perm, +Error **errp); + +BlockBackend * no_coroutine_fn +blk_new_open(const char *filename, const char *reference, QDict *options, + int flags, Error **errp); + +BlockBackend * coroutine_fn no_co_wrapper +blk_co_new_open (const char *filename,const char *reference,QDict *options, +int flags,Error **errp); +int blk_get_refcnt (BlockBackend *blk);void blk_ref (BlockBackend *blk);void blk_unref (BlockBackend *blk);
block/meson.build +1 -0
@@ -141,6 +141,7 @@ block_gen_c = custom_target('block-gen.c', '../include/block/dirty-bitmap.h', '../include/block/block_int-io.h', '../include/block/block-global-state.h', + '../include/sysemu/block-backend-global-state.h', '../include/sysemu/block-backend-io.h', 'coroutines.h' ),
Signature · 1 line
2.38.1
PATCH 3/13 luks: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/crypto.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/crypto.c +5 -5
@@ -325,8 +325,8 @@ static int block_crypto_co_create_generic(BlockDriverState *bs,QCryptoBlock *crypto =NULL ;struct BlockCryptoCreateData data; - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto cleanup;
@@ -639,7 +639,7 @@ block_crypto_co_create_luks(BlockdevCreateOptions *create_options, Error **errp)assert (create_options->driver ==BLOCKDEV_DRIVER_LUKS ); luks_opts = &create_options->u.luks; - bs =bdrv_open_blockdev_ref (luks_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (luks_opts->file, errp);if (bs ==NULL ) {return -EIO ; }
@@ -708,8 +708,8 @@ static int coroutine_fn block_crypto_co_create_opts_luks(BlockDriver *drv, goto fail; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (!bs) { ret = -EINVAL ; goto fail;
Signature · 1 line
2.38.1
PATCH 4/13 parallels: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/parallels.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/parallels.c +5 -5
@@ -565,13 +565,13 @@ static int coroutine_fn parallels_co_create(BlockdevCreateOptions* opts, }/* Create BlockBackend to write to the image */ - bs =bdrv_open_blockdev_ref (parallels_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (parallels_opts->file, errp);if (bs ==NULL ) {return -EIO ; } - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto out;
@@ -651,8 +651,8 @@ static int coroutine_fn parallels_co_create_opts(BlockDriver *drv, goto done; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto done;
Signature · 1 line
2.38.1
PATCH 5/13 qcow: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/qcow.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/qcow.c +5 -5
@@ -833,13 +833,13 @@ static int coroutine_fn qcow_co_create(BlockdevCreateOptions *opts, }/* Create BlockBackend to write to the image */ - bs =bdrv_open_blockdev_ref (qcow_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (qcow_opts->file, errp);if (bs ==NULL ) {return -EIO ; } - qcow_blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE , -BLK_PERM_ALL , errp); + qcow_blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE , +BLK_PERM_ALL , errp);if (!qcow_blk) { ret = -EPERM ; goto exit;
@@ -978,8 +978,8 @@ static int coroutine_fn qcow_co_create_opts(BlockDriver *drv, goto fail; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto fail;
Signature · 1 line
2.38.1
PATCH 6/13 qcow2: Fix open/create to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine, as does qcow2_do_open(). Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/qcow2.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)
block/qcow2.c +22 -21
@@ -1617,9 +1617,9 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,if (open_data_file) {/* Open external data file */ - s->data_file =bdrv_open_child (NULL , options,"data-file" , bs, - &child_of_bds,BDRV_CHILD_DATA , - true, errp); + s->data_file =bdrv_co_open_child (NULL , options,"data-file" , bs, + &child_of_bds,BDRV_CHILD_DATA , + true, errp);if (*errp) { ret = -EINVAL ; goto fail;
@@ -1627,9 +1627,10 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,if (s->incompatible_features &QCOW2_INCOMPAT_DATA_FILE ) {if (!s->data_file && s->image_data_file) { - s->data_file =bdrv_open_child (s->image_data_file, options, -"data-file" , bs, &child_of_bds, -BDRV_CHILD_DATA , false, errp); + s->data_file =bdrv_co_open_child (s->image_data_file, options, +"data-file" , bs, + &child_of_bds, +BDRV_CHILD_DATA , false, errp);if (!s->data_file) { ret = -EINVAL ; goto fail;
@@ -3454,7 +3455,7 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)assert (create_options->driver ==BLOCKDEV_DRIVER_QCOW2 ); qcow2_opts = &create_options->u.qcow2; - bs =bdrv_open_blockdev_ref (qcow2_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (qcow2_opts->file, errp);if (bs ==NULL ) {return -EIO ; }
@@ -3596,7 +3597,7 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) ret = -EINVAL ; goto out; } - data_bs =bdrv_open_blockdev_ref (qcow2_opts->data_file, errp); + data_bs =bdrv_co_open_blockdev_ref (qcow2_opts->data_file, errp);if (data_bs ==NULL ) { ret = -EIO ; goto out;
@@ -3629,8 +3630,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) }/* Create BlockBackend to write to the image */ - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto out;
@@ -3712,9 +3713,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)if (data_bs) {qdict_put_str (options,"data-file" , data_bs->node_name); } - blk =blk_new_open (NULL ,NULL , options, -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_NO_FLUSH , - errp); + blk =blk_co_new_open (NULL ,NULL , options, +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_NO_FLUSH , + errp);if (blk ==NULL ) { ret = -EIO ; goto out;
@@ -3793,9 +3794,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)if (data_bs) {qdict_put_str (options,"data-file" , data_bs->node_name); } - blk =blk_new_open (NULL ,NULL , options, -BDRV_O_RDWR |BDRV_O_NO_BACKING |BDRV_O_NO_IO , - errp); + blk =blk_co_new_open (NULL ,NULL , options, +BDRV_O_RDWR |BDRV_O_NO_BACKING |BDRV_O_NO_IO , + errp);if (blk ==NULL ) { ret = -EIO ; goto out;
@@ -3877,8 +3878,8 @@ static int coroutine_fn qcow2_co_create_opts(BlockDriver *drv, goto finish; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto finish;
@@ -3892,9 +3893,9 @@ static int coroutine_fn qcow2_co_create_opts(BlockDriver *drv, goto finish; } - data_bs =bdrv_open (val,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , - errp); + data_bs =bdrv_co_open (val,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , + errp);if (data_bs ==NULL ) { ret = -EIO ; goto finish;
Signature · 1 line
2.38.1
PATCH 7/13 qed: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/qed.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/qed.c +5 -5
@@ -676,13 +676,13 @@ static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts, }/* Create BlockBackend to write to the image */ - bs =bdrv_open_blockdev_ref (qed_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (qed_opts->file, errp);if (bs ==NULL ) {return -EIO ; } - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto out;
@@ -783,8 +783,8 @@ static int coroutine_fn bdrv_qed_co_create_opts(BlockDriver *drv, goto fail; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto fail;
Signature · 1 line
2.38.1
PATCH 8/13 vdi: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/vdi.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/vdi.c +5 -5
@@ -800,14 +800,14 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options, }/* Create BlockBackend to write to the image */ - bs_file =bdrv_open_blockdev_ref (vdi_opts->file, errp); + bs_file =bdrv_co_open_blockdev_ref (vdi_opts->file, errp);if (!bs_file) { ret = -EIO ; goto exit; } - blk =blk_new_with_bs (bs_file,BLK_PERM_WRITE |BLK_PERM_RESIZE , -BLK_PERM_ALL , errp); + blk =blk_co_new_with_bs (bs_file,BLK_PERM_WRITE |BLK_PERM_RESIZE , +BLK_PERM_ALL , errp);if (!blk) { ret = -EPERM ; goto exit;
@@ -940,8 +940,8 @@ static int coroutine_fn vdi_co_create_opts(BlockDriver *drv, goto done; } - bs_file =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs_file =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (!bs_file) { ret = -EIO ; goto done;
Signature · 1 line
2.38.1
PATCH 9/13 vhdx: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/vhdx.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/vhdx.c +5 -5
@@ -1991,13 +1991,13 @@ static int coroutine_fn vhdx_co_create(BlockdevCreateOptions *opts, }/* Create BlockBackend to write to the image */ - bs =bdrv_open_blockdev_ref (vhdx_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (vhdx_opts->file, errp);if (bs ==NULL ) {return -EIO ; } - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto delete_and_exit;
@@ -2090,8 +2090,8 @@ static int coroutine_fn vhdx_co_create_opts(BlockDriver *drv, goto fail; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto fail;
Signature · 1 line
2.38.1
PATCH 10/13 vmdk: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/vmdk.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
block/vmdk.c +12 -10
@@ -2299,9 +2299,9 @@ static int coroutine_fn vmdk_create_extent(const char *filename, goto exit; } - blk =blk_new_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , - errp); + blk =blk_co_new_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , + errp);if (blk ==NULL ) { ret = -EIO ; goto exit;
@@ -2518,8 +2518,8 @@ static int coroutine_fn vmdk_co_do_create(int64_t size, }assert (full_backing); - backing =blk_new_open (full_backing,NULL ,NULL , -BDRV_O_NO_BACKING , errp); + backing =blk_co_new_open (full_backing,NULL ,NULL , +BDRV_O_NO_BACKING , errp);g_free (full_backing);if (backing ==NULL ) { ret = -EIO ;
@@ -2781,7 +2781,7 @@ static BlockBackend * coroutine_fn vmdk_co_create_cb(int64_t size, int idx,BlockdevCreateOptionsVmdk *opts = opaque;if (idx ==0 ) { - bs =bdrv_open_blockdev_ref (opts->file, errp); + bs =bdrv_co_open_blockdev_ref (opts->file, errp); }else {int i;BlockdevRefList *list = opts->extents;
@@ -2796,14 +2796,16 @@ static BlockBackend * coroutine_fn vmdk_co_create_cb(int64_t size, int idx,error_setg (errp,"Extent [%d] not specified" , idx -1 );return NULL ; } - bs =bdrv_open_blockdev_ref (list->value, errp); + bs =bdrv_co_open_blockdev_ref (list->value, errp); }if (!bs) {return NULL ; } - blk =blk_new_with_bs (bs, -BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE |BLK_PERM_RESIZE , -BLK_PERM_ALL , errp); + blk =blk_co_new_with_bs (bs, +BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE | +BLK_PERM_RESIZE , +BLK_PERM_ALL , + errp);if (!blk) {return NULL ; }
Signature · 1 line
2.38.1
PATCH 11/13 vpc: Fix .bdrv_co_create(_opts) to open images with no_co_wrapper
In reply to Kevin Wolf.bdrv_co_create implementations run in a coroutine. Therefore they are not allowed to open images directly. Fix the calls to use the corresponding no_co_wrappers instead.
--- block/vpc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/vpc.c +5 -5
@@ -1005,13 +1005,13 @@ static int coroutine_fn vpc_co_create(BlockdevCreateOptions *opts, }/* Create BlockBackend to write to the image */ - bs =bdrv_open_blockdev_ref (vpc_opts->file, errp); + bs =bdrv_co_open_blockdev_ref (vpc_opts->file, errp);if (bs ==NULL ) {return -EIO ; } - blk =blk_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , - errp); + blk =blk_co_new_with_bs (bs,BLK_PERM_WRITE |BLK_PERM_RESIZE ,BLK_PERM_ALL , + errp);if (!blk) { ret = -EPERM ; goto out;
@@ -1117,8 +1117,8 @@ static int coroutine_fn vpc_co_create_opts(BlockDriver *drv, goto fail; } - bs =bdrv_open (filename,NULL ,NULL , -BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp); + bs =bdrv_co_open (filename,NULL ,NULL , +BDRV_O_RDWR |BDRV_O_RESIZE |BDRV_O_PROTOCOL , errp);if (bs ==NULL ) { ret = -EIO ; goto fail;
Signature · 1 line
2.38.1
PATCH 12/13 block: Fix bdrv_co_create_opts_simple() to open images with no_co_wrapper
In reply to Kevin Wolfbdrv_co_create_opts_simpl() runs in a coroutine. Therefore it is not allowed to open images directly. Fix the call to use the corresponding no_co_wrapper instead.
--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block.c +2 -2
@@ -657,8 +657,8 @@ int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv, options =qdict_new ();qdict_put_str (options,"driver" , drv->format_name); - blk =blk_new_open (filename,NULL , options, -BDRV_O_RDWR |BDRV_O_RESIZE , errp); + blk =blk_co_new_open (filename,NULL , options, +BDRV_O_RDWR |BDRV_O_RESIZE , errp);if (!blk) { error_prepend(errp, "Protocol driver '%s' does not support image " "creation, and opening the image failed: ",
Signature · 1 line
2.38.1
Re: [PATCH 12/13] block: Fix bdrv_co_create_opts_simple() to open images with no_co_wrapper
Quoted 1 line · Kevin Wolf
bdrv_co_create_opts_simpl()runs in a coroutine. Therefore it is not
simple
Quoted 27 lines
allowed to open images directly. Fix the call to use the corresponding
no_co_wrapperinstead.Signed-off-by Kevin Wolf <[email protected]>--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)block.c +2
-2@@ -657,8 +657,8 @@ int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv, options =qdict_new ();qdict_put_str (options,"driver" , drv->format_name); - blk =blk_new_open (filename,NULL , options, -BDRV_O_RDWR |BDRV_O_RESIZE , errp); + blk =blk_co_new_open (filename,NULL , options, +BDRV_O_RDWR |BDRV_O_RESIZE , errp);if (!blk) { error_prepend(errp, "Protocol driver '%s' does not support image " "creation, and opening the image failed: ",Signature · 1 line
2.38.1
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 13/13 block: Assert non-coroutine context for bdrv_open_inherit()
In reply to Kevin Wolf--- block.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
block.c +6 -7
@@ -3807,13 +3807,11 @@ out:* function eventually calls bdrv_refresh_total_sectors() which polls * when called from non-coroutine context. */ -static BlockDriverState *bdrv_open_inherit (const char *filename, -const char *reference, -QDict *options,int flags, -BlockDriverState *parent, -const BdrvChildClass *child_class, -BdrvChildRole child_role, -Error **errp) +static BlockDriverState * no_coroutine_fn +bdrv_open_inherit (const char *filename,const char *reference,QDict *options, +int flags,BlockDriverState *parent, +const BdrvChildClass *child_class,BdrvChildRole child_role, +Error **errp) {int ret;BlockBackend *file =NULL ;
@@ -3829,6 +3827,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,assert (!child_class || !flags);assert (!child_class == !parent);GLOBAL_STATE_CODE (); +assert (!qemu_in_coroutine ());if (reference) {bool options_non_empty = options ?qdict_size (options) : false;
Signature · 1 line
2.38.1
Am 26/01/2023 um 18:24 schrieb Kevin Wolf:
Quoted 11 lines
bdrv_open*() must not be called from coroutine context, amongst others because it modifies the block graph. However, some functions - in particular all .bdrv_co_create* implementations of image formats - do call it from coroutine context. This is already wrong today, but when we add locking, it actually becomes visible.
This series adds
no_co_wrapperfunctions, which are automatically generated wrappers that run in coroutine context and use a BH to call the wrapped function outside of coroutine context. It then uses these wrappers to fix the problematic bdrv_open*() calls.
Quoted 17 lines
Kevin Wolf (13):
block-coroutine-wrapper: Introduceno_co_wrapper
block: Createno_co_wrappersfor open functions
luks: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
parallels: Fix .bdrv_co_create(_opts) to open images with
no_co_wrapper
qcow: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
qcow2: Fix open/create to open images withno_co_wrapper
qed: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
vdi: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
vhdx: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
vmdk: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
vpc: Fix .bdrv_co_create(_opts) to open images withno_co_wrapper
block: Fixbdrv_co_create_opts_simple()to open images with
no_co_wrapper
block: Assert non-coroutine context forbdrv_open_inherit()
Apart from a small nitpick in patch 3 where the functions are not marked as coroutine_fn (but I think this is because BDS callbacks usually don't have such annotations), looks good to me.
Quoted 17 lines
include/block/block-common.h | 14 ++++ include/block/block-global-state.h | 35 ++++++--- include/sysemu/block-backend-global-state.h | 21 +++++- block.c | 17 ++--- block/crypto.c | 10 +-- block/parallels.c | 10 +-- block/qcow.c | 10 +-- block/qcow2.c | 43 +++++------ block/qed.c | 10 +-- block/vdi.c | 10 +-- block/vhdx.c | 10 +-- block/vmdk.c | 22 +++--- block/vpc.c | 10 +-- scripts/block-coroutine-wrapper.py | 83 ++++++++++++++++++--- block/meson.build | 1 + 15 files changed, 207 insertions(+), 99 deletions(-)
Quoted 10 lines · Kevin Wolf
bdrv_open*() must not be called from coroutine context, amongst others because it modifies the block graph. However, some functions - in particular all .bdrv_co_create* implementations of image formats - do call it from coroutine context. This is already wrong today, but when we add locking, it actually becomes visible.
This series adds
no_co_wrapperfunctions, which are automatically generated wrappers that run in coroutine context and use a BH to call the wrapped function outside of coroutine context. It then uses these wrappers to fix the problematic bdrv_open*() calls.
Am 26.01.2023 um 18:24 hat Kevin Wolf geschrieben:
Quoted 10 lines
bdrv_open*() must not be called from coroutine context, amongst others because it modifies the block graph. However, some functions - in particular all .bdrv_co_create* implementations of image formats - do call it from coroutine context. This is already wrong today, but when we add locking, it actually becomes visible.
This series adds
no_co_wrapperfunctions, which are automatically generated wrappers that run in coroutine context and use a BH to call the wrapped function outside of coroutine context. It then uses these wrappers to fix the problematic bdrv_open*() calls.
Thanks for the review, fixed up the missing coroutine_fn in patch 3 (as pointed out by Emanuele) and applied to the block branch.
Kevin