block layer: split block APIs in global state and I/O
Subsystems (2)
PATCH v6 3/33 assertions for block global state API
In reply to Emanuele Giuseppe EspositoAll the global state (GS) API functions will check that qemu_in_main_thread() returns true. If not, it means that the safety of BQL cannot be guaranteed, and they need to be moved to I/O.
--- block.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++- block/commit.c | 2 + block/io.c | 14 ++++++ blockdev.c | 1 + 4 files changed, 148 insertions(+), 2 deletions(-)
block.c +131 -2
@@ -278,6 +278,8 @@ bool bdrv_is_read_only(BlockDriverState *bs)int bdrv_can_set_read_only (BlockDriverState *bs,bool read_only,bool ignore_allow_rdw,Error **errp) { +assert (qemu_in_main_thread ()); +/* Do not set read_only if copy_on_read is enabled */ if (bs->copy_on_read && read_only) { error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled",
@@ -311,6 +313,7 @@ int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg, Error **errp) {int ret =0 ; +assert (qemu_in_main_thread ());if (!(bs->open_flags &BDRV_O_RDWR )) {return 0 ;
@@ -393,6 +396,7 @@ char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp)void bdrv_register (BlockDriver *bdrv) {assert (bdrv->format_name); +assert (qemu_in_main_thread ());QLIST_INSERT_HEAD (&bdrv_drivers, bdrv, list); }
@@ -401,6 +405,8 @@ BlockDriverState *bdrv_new(void)BlockDriverState *bs;int i; +assert (qemu_in_main_thread ()); + bs =g_new0 (BlockDriverState,1 );QLIST_INIT (&bs->dirty_bitmaps);for (i =0 ; i <BLOCK_OP_TYPE_MAX ; i++) {
@@ -443,6 +449,8 @@ BlockDriver *bdrv_find_format(const char *format_name)BlockDriver *drv1;int i; +assert (qemu_in_main_thread ()); + drv1 =bdrv_do_find_format (format_name);if (drv1) {return drv1;
@@ -492,11 +500,13 @@ static int bdrv_format_is_whitelisted(const char *format_name, bool read_only)int bdrv_is_whitelisted (BlockDriver *drv,bool read_only) { +assert (qemu_in_main_thread ());return bdrv_format_is_whitelisted (drv->format_name, read_only); }bool bdrv_uses_whitelist (void ) { +assert (qemu_in_main_thread ());return use_bdrv_whitelist; }
@@ -527,6 +537,8 @@ int bdrv_create(BlockDriver *drv, const char* filename, {int ret; +assert (qemu_in_main_thread ()); +Coroutine *co;CreateCo cco = { .drv = drv,
@@ -702,6 +714,8 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)QDict *qdict;int ret; +assert (qemu_in_main_thread ()); + drv =bdrv_find_protocol (filename, true, errp);if (drv ==NULL ) {return -ENOENT ;
@@ -799,6 +813,7 @@ int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) {BlockDriver *drv = bs->drv;BlockDriverState *filtered =bdrv_filter_bs (bs); +assert (qemu_in_main_thread ());if (drv && drv->bdrv_probe_blocksizes) {return drv->bdrv_probe_blocksizes (bs, bsz);
@@ -819,6 +834,7 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) {BlockDriver *drv = bs->drv;BlockDriverState *filtered =bdrv_filter_bs (bs); +assert (qemu_in_main_thread ());if (drv && drv->bdrv_probe_geometry) {return drv->bdrv_probe_geometry (bs, geo);
@@ -910,6 +926,7 @@ BlockDriver *bdrv_find_protocol(const char *filename,const char *p;int i; +assert (qemu_in_main_thread ());/* TODO Drivers without bdrv_file_open must be specified explicitly */ /*
@@ -1122,6 +1139,7 @@ int bdrv_parse_aio(const char *mode, int *flags)*/ int bdrv_parse_discard_flags (const char *mode,int *flags) { +assert (qemu_in_main_thread ()); *flags &= ~BDRV_O_UNMAP ;if (!strcmp (mode,"off" ) || !strcmp (mode,"ignore" )) {
@@ -1142,6 +1160,7 @@ int bdrv_parse_discard_flags(const char *mode, int *flags)*/ int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough) { +assert (qemu_in_main_thread ()); *flags &= ~BDRV_O_CACHE_MASK ;if (!strcmp (mode,"off" ) || !strcmp (mode,"none" )) {
@@ -1634,6 +1653,8 @@ BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv,BlockDriverState *bs;int ret; +assert (qemu_in_main_thread ()); + bs =bdrv_new (); bs->open_flags = flags; bs->options = options ?:qdict_new ();
@@ -1659,6 +1680,7 @@ BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv,BlockDriverState *bdrv_new_open_driver (BlockDriver *drv,const char *node_name,int flags,Error **errp) { +assert (qemu_in_main_thread ());return bdrv_new_open_driver_opts (drv, node_name,NULL , flags, errp); }
@@ -2480,6 +2502,8 @@ void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,char *bdrv_perm_names (uint64_t perm) { +assert (qemu_in_main_thread ()); +struct perm_name {uint64_t perm;const char *name;
@@ -2716,6 +2740,8 @@ void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c,uint64_t bdrv_qapi_perm_to_blk_perm (BlockPermission qapi_perm) { +assert (qemu_in_main_thread ()); +static const uint64_t permissions[] = { [BLOCK_PERMISSION_CONSISTENT_READ ] =BLK_PERM_CONSISTENT_READ , [BLOCK_PERMISSION_WRITE ] =BLK_PERM_WRITE ,
@@ -3094,6 +3120,8 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,BdrvChild *child =NULL ;Transaction *tran =tran_new (); +assert (qemu_in_main_thread ()); + ret =bdrv_attach_child_noperm (parent_bs, child_bs, child_name, child_class, child_role, &child, tran, errp);if (ret <0 ) {
@@ -3120,6 +3148,8 @@ void bdrv_root_unref_child(BdrvChild *child) {BlockDriverState *child_bs; +assert (qemu_in_main_thread ()); + child_bs = child->bs;bdrv_detach_child (&child);bdrv_unref (child_bs);
@@ -3194,6 +3224,7 @@ static void bdrv_unset_inherits_from(BlockDriverState *root, BdrvChild *child,/* Callers must ensure that child->frozen is false. */ void bdrv_unref_child (BlockDriverState *parent,BdrvChild *child) { +assert (qemu_in_main_thread ());if (child ==NULL ) {return ; }
@@ -3344,6 +3375,8 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,int ret;Transaction *tran =tran_new (); +assert (qemu_in_main_thread ()); + ret =bdrv_set_backing_noperm (bs, backing_hd, tran, errp);if (ret <0 ) { goto out;
@@ -3379,6 +3412,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,QDict *tmp_parent_options =NULL ;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); +if (bs->backing !=NULL ) { goto free_exit; }
@@ -3538,6 +3573,8 @@ BdrvChild *bdrv_open_child(const char *filename, {BlockDriverState *bs; +assert (qemu_in_main_thread ()); + bs =bdrv_open_child_bs (filename, options, bdref_key, parent, child_class, child_role, allow_none, errp);if (bs ==NULL ) {
@@ -3560,6 +3597,8 @@ BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp)const char *reference =NULL ;Visitor *v =NULL ; +assert (qemu_in_main_thread ()); +if (ref->type ==QTYPE_QSTRING ) { reference = ref->u.reference; }else {
@@ -3957,6 +3996,8 @@ close_and_fail:BlockDriverState *bdrv_open (const char *filename,const char *reference,QDict *options,int flags,Error **errp) { +assert (qemu_in_main_thread ()); +return bdrv_open_inherit (filename, reference, options, flags,NULL ,NULL ,0 , errp); }
@@ -4211,12 +4252,15 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, BlockDriverState *bs, QDict *options, bool keep_old_opts) { +assert (qemu_in_main_thread ()); +return bdrv_reopen_queue_child (bs_queue, bs, options,NULL ,0 , false,NULL ,0 , keep_old_opts); }void bdrv_reopen_queue_free (BlockReopenQueue *bs_queue) { +assert (qemu_in_main_thread ());if (bs_queue) {BlockReopenQueueEntry *bs_entry, *next; QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
@@ -4364,6 +4408,8 @@ int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,BlockReopenQueue *queue;int ret; +assert (qemu_in_main_thread ()); +bdrv_subtree_drained_begin (bs);if (ctx !=qemu_get_aio_context ()) {aio_context_release (ctx);
@@ -4385,6 +4431,8 @@ int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, {QDict *opts =qdict_new (); +assert (qemu_in_main_thread ()); +qdict_put_bool (opts,BDRV_OPT_READ_ONLY , read_only);return bdrv_reopen (bs, opts, true, errp);
@@ -4839,6 +4887,7 @@ static void bdrv_close(BlockDriverState *bs)void bdrv_close_all (void ) {assert (job_next (NULL ) ==NULL ); +assert (qemu_in_main_thread ());/* Drop references from requests still in flight, such as canceled block * jobs whose AIO context has not been polled yet */
@@ -5153,11 +5202,15 @@ out:int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp) { +assert (qemu_in_main_thread ()); +return bdrv_replace_node_common (from, to, true, false, errp); }int bdrv_drop_filter (BlockDriverState *bs,Error **errp) { +assert (qemu_in_main_thread ()); +return bdrv_replace_node_common (bs,bdrv_filter_or_cow_bs (bs), true, true, errp); }
@@ -5180,6 +5233,8 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,int ret;Transaction *tran =tran_new (); +assert (qemu_in_main_thread ()); +assert (!bs_new->backing); ret = bdrv_attach_child_noperm(bs_new, bs_top, "backing",
@@ -5213,6 +5268,8 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,g_autoptr (GSList) refresh_list =NULL ;BlockDriverState *old_bs = child->bs; +assert (qemu_in_main_thread ()); +bdrv_ref (old_bs);bdrv_drained_begin (old_bs);bdrv_drained_begin (new_bs);
@@ -5284,6 +5341,8 @@ BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *options, node_name =qdict_get_try_str (options,"node-name" ); +assert (qemu_in_main_thread ()); + new_node_bs =bdrv_new_open_driver_opts (drv, node_name, options, flags, errp); options =NULL ;/* bdrv_new_open_driver() eats options */
@@ -5344,6 +5403,8 @@ int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file,BlockDriver *drv = bs->drv;int ret; +assert (qemu_in_main_thread ()); +if (!drv) {return -ENOMEDIUM ; }
@@ -5385,6 +5446,9 @@ int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file,BlockDriverState *bdrv_find_overlay (BlockDriverState *active,BlockDriverState *bs) { + +assert (qemu_in_main_thread ()); + bs =bdrv_skip_filters (bs); active =bdrv_skip_filters (active);
@@ -5402,6 +5466,8 @@ BlockDriverState *bdrv_find_overlay(BlockDriverState *active,/* Given a BDS, searches for the base layer. */ BlockDriverState *bdrv_find_base (BlockDriverState *bs) { +assert (qemu_in_main_thread ()); +return bdrv_find_overlay (bs,NULL ); }
@@ -5416,6 +5482,8 @@ bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base,BlockDriverState *i;BdrvChild *child; +assert (qemu_in_main_thread ()); +for (i = bs; i != base; i =child_bs (child)) { child =bdrv_filter_or_cow_child (i);
@@ -5442,6 +5510,8 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base,BlockDriverState *i;BdrvChild *child; +assert (qemu_in_main_thread ()); +if (bdrv_is_backing_chain_frozen (bs, base, errp)) {return -EPERM ; }
@@ -5476,6 +5546,8 @@ void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base)BlockDriverState *i;BdrvChild *child; +assert (qemu_in_main_thread ()); +for (i = bs; i != base; i =child_bs (child)) { child =bdrv_filter_or_cow_child (i);if (child) {
@@ -5525,6 +5597,8 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,g_autoptr (GSList) updated_children =NULL ;GSList *p; +assert (qemu_in_main_thread ()); +bdrv_ref (top);bdrv_subtree_drained_begin (top);
@@ -5784,6 +5858,8 @@ void bdrv_iterate_format(void (*it)(void *opaque, const char *name),int i;const char **formats =NULL ; +assert (qemu_in_main_thread ()); + QLIST_FOREACH(drv, &bdrv_drivers, list) {if (drv->format_name) {bool found = false;
@@ -5842,6 +5918,7 @@ BlockDriverState *bdrv_find_node(const char *node_name)BlockDriverState *bs;assert (node_name); +assert (qemu_in_main_thread ()); QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {if (!strcmp (node_name, bs->node_name)) {
@@ -5858,6 +5935,8 @@ BlockDeviceInfoList *bdrv_named_nodes_list(bool flat,BlockDeviceInfoList *list;BlockDriverState *bs; +assert (qemu_in_main_thread ()); + list =NULL ; QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {BlockDeviceInfo *info =bdrv_block_device_info (NULL , bs, flat, errp);
@@ -5963,6 +6042,8 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp)BdrvChild *child;XDbgBlockGraphConstructor *gr =xdbg_graph_new (); +assert (qemu_in_main_thread ()); +for (blk =blk_all_next (NULL ); blk; blk =blk_all_next (blk)) {char *allocated_name =NULL ;const char *name =blk_name (blk);
@@ -6006,6 +6087,8 @@ BlockDriverState *bdrv_lookup_bs(const char *device,BlockBackend *blk;BlockDriverState *bs; +assert (qemu_in_main_thread ()); +if (device) { blk =blk_by_name (device);
@@ -6037,6 +6120,9 @@ BlockDriverState *bdrv_lookup_bs(const char *device,* return false. If either argument is NULL, return false. */ bool bdrv_chain_contains (BlockDriverState *top,BlockDriverState *base) { + +assert (qemu_in_main_thread ()); +while (top && top != base) { top =bdrv_filter_or_cow_bs (top); }
@@ -6046,6 +6132,7 @@ bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)BlockDriverState *bdrv_next_node (BlockDriverState *bs) { +assert (qemu_in_main_thread ());if (!bs) {return QTAILQ_FIRST (&graph_bdrv_states); }
@@ -6054,6 +6141,7 @@ BlockDriverState *bdrv_next_node(BlockDriverState *bs)BlockDriverState *bdrv_next_all_states (BlockDriverState *bs) { +assert (qemu_in_main_thread ());if (!bs) {return QTAILQ_FIRST (&all_bdrv_states); }
@@ -6100,17 +6188,20 @@ const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)int bdrv_get_flags (BlockDriverState *bs) { +assert (qemu_in_main_thread ());return bs->open_flags; }int bdrv_has_zero_init_1 (BlockDriverState *bs) { +assert (qemu_in_main_thread ());return 1 ; }int bdrv_has_zero_init (BlockDriverState *bs) {BlockDriverState *filtered; +assert (qemu_in_main_thread ());if (!bs->drv) {return 0 ;
@@ -6222,6 +6313,7 @@ static BlockDriverState *bdrv_find_debug_node(BlockDriverState *bs)int bdrv_debug_breakpoint (BlockDriverState *bs,const char *event,const char *tag) { +assert (qemu_in_main_thread ()); bs =bdrv_find_debug_node (bs);if (bs) {return bs->drv->bdrv_debug_breakpoint (bs, event, tag);
@@ -6232,6 +6324,7 @@ int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,int bdrv_debug_remove_breakpoint (BlockDriverState *bs,const char *tag) { +assert (qemu_in_main_thread ()); bs =bdrv_find_debug_node (bs);if (bs) {return bs->drv->bdrv_debug_remove_breakpoint (bs, tag);
@@ -6242,6 +6335,7 @@ int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)int bdrv_debug_resume (BlockDriverState *bs,const char *tag) { +assert (qemu_in_main_thread ());while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { bs =bdrv_primary_bs (bs); }
@@ -6255,6 +6349,7 @@ int bdrv_debug_resume(BlockDriverState *bs, const char *tag)bool bdrv_debug_is_suspended (BlockDriverState *bs,const char *tag) { +assert (qemu_in_main_thread ());while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { bs =bdrv_primary_bs (bs); }
@@ -6282,6 +6377,8 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,BlockDriverState *retval =NULL ;BlockDriverState *bs_below; +assert (qemu_in_main_thread ()); +if (!bs || !bs->drv || !backing_file) {return NULL ; }
@@ -6380,6 +6477,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,void bdrv_init (void ) { +assert (qemu_in_main_thread ());#ifdef CONFIG_BDRV_WHITELIST_TOOLS use_bdrv_whitelist =1 ;#endif
@@ -6388,6 +6486,7 @@ void bdrv_init(void)void bdrv_init_with_whitelist (void ) { +assert (qemu_in_main_thread ()); use_bdrv_whitelist =1 ;bdrv_init (); }
@@ -6472,6 +6571,8 @@ void bdrv_invalidate_cache_all(Error **errp)BlockDriverState *bs;BdrvNextIterator it; +assert (qemu_in_main_thread ()); +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);int ret;
@@ -6571,6 +6672,8 @@ int bdrv_inactivate_all(void)int ret =0 ;GSList *aio_ctxs =NULL , *ctx; +assert (qemu_in_main_thread ()); +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);
@@ -6659,6 +6762,7 @@ void bdrv_lock_medium(BlockDriverState *bs, bool locked)/* Get a reference to bs */ void bdrv_ref (BlockDriverState *bs) { +assert (qemu_in_main_thread ()); bs->refcnt++; }
@@ -6667,6 +6771,7 @@ void bdrv_ref(BlockDriverState *bs)* deleted. */ void bdrv_unref (BlockDriverState *bs) { +assert (qemu_in_main_thread ());if (!bs) {return ; }
@@ -6684,6 +6789,7 @@ struct BdrvOpBlocker {bool bdrv_op_is_blocked (BlockDriverState *bs,BlockOpType op,Error **errp) {BdrvOpBlocker *blocker; +assert (qemu_in_main_thread ());assert ((int ) op >=0 && op <BLOCK_OP_TYPE_MAX );if (!QLIST_EMPTY (&bs->op_blockers[op])) { blocker =QLIST_FIRST (&bs->op_blockers[op]);
@@ -6698,6 +6804,7 @@ bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)void bdrv_op_block (BlockDriverState *bs,BlockOpType op,Error *reason) {BdrvOpBlocker *blocker; +assert (qemu_in_main_thread ());assert ((int ) op >=0 && op <BLOCK_OP_TYPE_MAX ); blocker =g_new0 (BdrvOpBlocker,1 );
@@ -6708,6 +6815,7 @@ void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)void bdrv_op_unblock (BlockDriverState *bs,BlockOpType op,Error *reason) {BdrvOpBlocker *blocker, *next; +assert (qemu_in_main_thread ());assert ((int ) op >=0 && op <BLOCK_OP_TYPE_MAX ); QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {if (blocker->reason == reason) {
@@ -6720,6 +6828,7 @@ void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)void bdrv_op_block_all (BlockDriverState *bs,Error *reason) {int i; +assert (qemu_in_main_thread ());for (i =0 ; i <BLOCK_OP_TYPE_MAX ; i++) {bdrv_op_block (bs, i, reason); }
@@ -6728,6 +6837,7 @@ void bdrv_op_block_all(BlockDriverState *bs, Error *reason)void bdrv_op_unblock_all (BlockDriverState *bs,Error *reason) {int i; +assert (qemu_in_main_thread ());for (i =0 ; i <BLOCK_OP_TYPE_MAX ; i++) {bdrv_op_unblock (bs, i, reason); }
@@ -6736,7 +6846,7 @@ void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)bool bdrv_op_blocker_is_empty (BlockDriverState *bs) {int i; - +assert (qemu_in_main_thread ());for (i =0 ; i <BLOCK_OP_TYPE_MAX ; i++) {if (!QLIST_EMPTY (&bs->op_blockers[i])) {return false;
@@ -6758,6 +6868,8 @@ void bdrv_img_create(const char *filename, const char *fmt,Error *local_err =NULL ;int ret =0 ; +assert (qemu_in_main_thread ()); +/* Find driver and parse its options */ drv =bdrv_find_format (fmt);if (!drv) {
@@ -7174,6 +7286,7 @@ static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx,bool bdrv_child_can_set_aio_context (BdrvChild *c,AioContext *ctx,GSList **ignore,Error **errp) { +assert (qemu_in_main_thread ());if (g_slist_find (*ignore, c)) {return true; }
@@ -7192,6 +7305,8 @@ bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx,return true; } +assert (qemu_in_main_thread ()); + QLIST_FOREACH(c, &bs->parents, next_parent) {if (!bdrv_parent_can_set_aio_context (c, ctx, ignore, errp)) {return false;
@@ -7212,6 +7327,8 @@ int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,GSList *ignore;bool ret; +assert (qemu_in_main_thread ()); + ignore = ignore_child ?g_slist_prepend (NULL , ignore_child) :NULL ; ret =bdrv_can_set_aio_context (bs, ctx, &ignore, errp);g_slist_free (ignore);
@@ -7230,6 +7347,7 @@ int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,int bdrv_try_set_aio_context (BlockDriverState *bs,AioContext *ctx,Error **errp) { +assert (qemu_in_main_thread ());return bdrv_child_try_set_aio_context (bs, ctx,NULL , errp); }
@@ -7243,6 +7361,7 @@ void bdrv_add_aio_context_notifier(BlockDriverState *bs, .detach_aio_context = detach_aio_context, .opaque = opaque }; +assert (qemu_in_main_thread ());QLIST_INSERT_HEAD (&bs->aio_notifiers, ban, list); }
@@ -7254,6 +7373,7 @@ void bdrv_remove_aio_context_notifier(BlockDriverState *bs, void *opaque) {BdrvAioNotifier *ban, *ban_next; +assert (qemu_in_main_thread ()); QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { if (ban->attached_aio_context == attached_aio_context &&
@@ -7278,6 +7398,7 @@ int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,bool force, Error **errp) { +assert (qemu_in_main_thread ());if (!bs->drv) {error_setg (errp,"Node is ejected" );return -ENOMEDIUM ;
@@ -7348,6 +7469,8 @@ BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,BlockDriverState *to_replace_bs =bdrv_find_node (node_name);AioContext *aio_context; +assert (qemu_in_main_thread ()); +if (!to_replace_bs) {error_setg (errp,"Failed to find node with node-name='%s'" , node_name);return NULL ;
@@ -7509,6 +7632,8 @@ void bdrv_refresh_filename(BlockDriverState *bs)bool generate_json_filename;/* Whether our default implementation should fill exact_filename (false) or not (true) */ +assert (qemu_in_main_thread ()); +if (!drv) {return ; }
@@ -7631,6 +7756,8 @@ char *bdrv_dirname(BlockDriverState *bs, Error **errp)BlockDriver *drv = bs->drv;BlockDriverState *child_bs; +assert (qemu_in_main_thread ()); +if (!drv) {error_setg (errp,"Node '%s' is ejected" , bs->node_name);return NULL ;
@@ -7662,7 +7789,7 @@ char *bdrv_dirname(BlockDriverState *bs, Error **errp)void bdrv_add_child (BlockDriverState *parent_bs,BlockDriverState *child_bs,Error **errp) { - +assert (qemu_in_main_thread ());if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {error_setg (errp,"The node %s does not support adding a child" ,bdrv_get_device_or_node_name (parent_bs));
@@ -7682,6 +7809,7 @@ void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp) {BdrvChild *tmp; +assert (qemu_in_main_thread ());if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {error_setg (errp,"The node %s does not support removing a child" ,bdrv_get_device_or_node_name (parent_bs));
@@ -7709,6 +7837,7 @@ int bdrv_make_empty(BdrvChild *c, Error **errp)BlockDriver *drv = c->bs->drv;int ret; +assert (qemu_in_main_thread ());assert (c->perm & (BLK_PERM_WRITE |BLK_PERM_WRITE_UNCHANGED ));if (!drv->bdrv_make_empty) {
block/commit.c +2 -0
@@ -432,6 +432,8 @@ int bdrv_commit(BlockDriverState *bs) QEMU_AUTO_VFREE uint8_t *buf = NULL;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); +if (!drv)return -ENOMEDIUM ;
block/io.c +14 -0
@@ -164,6 +164,8 @@ void bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp)BdrvChild *c;bool have_limits; +assert (qemu_in_main_thread ()); +if (tran) {BdrvRefreshLimitsState *s =g_new (BdrvRefreshLimitsState,1 ); *s = (BdrvRefreshLimitsState ) {
@@ -612,6 +614,7 @@ static bool bdrv_drain_all_poll(void) {BlockDriverState *bs =NULL ;bool result = false; +assert (qemu_in_main_thread ());/* bdrv_drain_poll() can't make changes to the graph and we are holding the * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
@@ -640,6 +643,7 @@ static bool bdrv_drain_all_poll(void)void bdrv_drain_all_begin (void ) {BlockDriverState *bs =NULL ; +assert (qemu_in_main_thread ());if (qemu_in_coroutine ()) {bdrv_co_yield_to_drain (NULL , true, false,NULL , true, true,NULL );
@@ -696,6 +700,7 @@ void bdrv_drain_all_end(void) {BlockDriverState *bs =NULL ;int drained_end_counter =0 ; +assert (qemu_in_main_thread ());/* * bdrv queue is managed by record/replay,
@@ -723,6 +728,7 @@ void bdrv_drain_all_end(void)void bdrv_drain_all (void ) { +assert (qemu_in_main_thread ());bdrv_drain_all_begin ();bdrv_drain_all_end (); }
@@ -1059,6 +1065,8 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)int64_t target_size, bytes, offset =0 ;BlockDriverState *bs = child->bs; +assert (qemu_in_main_thread ()); + target_size =bdrv_getlength (bs);if (target_size <0 ) {return target_size;
@@ -2345,6 +2353,8 @@ int bdrv_flush_all(void)BlockDriverState *bs =NULL ;int result =0 ; +assert (qemu_in_main_thread ()); +/* * bdrv queue is managed by record/replay, * creating new flush request for stopping
@@ -2898,6 +2908,7 @@ int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,void bdrv_aio_cancel (BlockAIOCB *acb) { +assert (qemu_in_main_thread ());qemu_aio_ref (acb);bdrv_aio_cancel_async (acb);while (acb->refcnt >1 ) {
@@ -3292,6 +3303,7 @@ void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size) {BdrvChild *child; +assert (qemu_in_main_thread ());if (bs->drv && bs->drv->bdrv_register_buf) { bs->drv->bdrv_register_buf (bs, host, size); }
@@ -3304,6 +3316,7 @@ void bdrv_unregister_buf(BlockDriverState *bs, void *host) {BdrvChild *child; +assert (qemu_in_main_thread ());if (bs->drv && bs->drv->bdrv_unregister_buf) { bs->drv->bdrv_unregister_buf (bs, host); }
@@ -3575,6 +3588,7 @@ out:void bdrv_cancel_in_flight (BlockDriverState *bs) { +assert (qemu_in_main_thread ());if (!bs || !bs->drv) {return ; }
blockdev.c +1 -0
@@ -658,6 +658,7 @@ void blockdev_close_all_bdrv_states(void)/* Iterates over the list of monitor-owned BlockDriverStates */ BlockDriverState *bdrv_next_monitor_owned (BlockDriverState *bs) { +assert (qemu_in_main_thread ());return bs ?QTAILQ_NEXT (bs, monitor_list) :QTAILQ_FIRST (&monitor_bdrv_states); }
Signature · 1 line
2.31.1
v6 cover block layer: split block APIs in global state and I/O
Currently, block layer APIs like block.h contain a mix of functions that are either running in the main loop and under the BQL, or are thread-safe functions and run in iothreads performing I/O. The functions running under BQL also take care of modifying the block graph, by using drain and/or aio_context_acquire/release. This makes it very confusing to understand where each function runs, and what assumptions it provided with regards to thread safety.
We call the functions running under BQL "global state (GS) API", and distinguish them from the thread-safe "I/O API".
The aim of this series is to split the relevant block headers in global state and I/O sub-headers. The division will be done in this way:header.h will be split in header-global-state.h, header-io.h and header-common.h. The latter will just contain the data structures needed by header-global-state and header-io, and common helpers that are neither in GS nor in I/O. header.h will remain for legacy and to avoid changing all includes in all QEMU c files, but will only include the two new headers. No function shall be added in header.c .
Once we split all relevant headers, it will be much easier to see what uses the AioContext lock and remove it, which is the overall main goal of this and other series that I posted/will post.
In addition to splitting the relevant headers shown in this series, it is also very helpful splitting the function pointers in some block structures, to understand what runs under AioContext lock and what doesn't. This is what patches 21-27 do.
Each function in the GS API will have an assertion, checking that it is always running under BQL.
I/O functions are instead thread safe (or so should be), meaning that they *can* run under BQL, but also in an iothread in another AioContext. Therefore they do not provide any assertion, and need to be audited manually to verify the correctness.
Adding assetions has helped finding 2 bugs already, as shown in my series "Migration: fix missing iothread locking".
Tested this series by running unit tests, qemu-iotests and qtests (x86_64).
Some functions in the GS API are used everywhere but not properly tested. Therefore their assertion is never actually run in the tests, so despite my very careful auditing, it is not impossible to exclude that some will trigger while actually using QEMU.
Patch 1 introduces qemu_in_main_thread(), the function used in all assertions. This had to be introduced otherwise all unit tests would fail, since they run in the main loop but use the code in stubs/iothread.c
Patches 2-27 (with the exception of patch 9-10, that are an additional assert) are all structured in the same way: first we split the header and in the next (or same, if small) patch we add assertions. Patch 28-31 take care instead of the block layer permission API, fixing some bugs where they are used in I/O functions.
Next steps once this get reviewed:
1) audit the GS API and replace the AioContext lock with drains, or remove them when not necessary (requires further discussion).
2) [optional as it should be already the case] audit the I/O API and check that thread safety is guaranteed
In order to keep this series a little bit smaller, move some refactoring patches in another series already merged, "block: minor refactoring in preparation to the block layer API split".
---
v6:
* Additional assertions in "block.c: add assertions to static functions"
* bdrv_co_invalidate_cache: create a new GS function bdrv_activate, and move all GS logic of bdrv_co_invalidate_cache there, so that the coroutine only runs I/O code. Move the resulting 3 patches before "block/coroutines: I/O API"
* crypto (patch 30): introduce bdrv_amend_pre_run and bdrv_clean, along with job_pre_run and job_clean to be sure of setting the permissions in GS
* remove TODO in blk_{get/set}_perm, and handle the RESIZE permission in patch 6
* in I/O:
blk_ioctl
blk_get_attached_dev_id
blk_enable_write_cache
blk_set_guest_block_size
blk_lock_medium
bdrv_lock_medium
(*bdrv_lock_medium)
blk_eject
bdrv_eject
(*bdrv_eject)
bdrv_probe_all* patch 13 just move an assertion before the variable assignment, and not after.
* remove patch 10 (block.c: modify .attach and .detach callbacks of child_of_bds), as it is not necessary.
v5:
* In short, apply all Hanna's comments. More in details, the following functions in the following headers have been moved:
block-backend:
blk_replace_bs (to gs)
blk_nb_sectors (to io)
blk_name (to io)
blk_set_perm (to io)
blk_get_perm (to io)
blk_drain (to io)
blk_abort_aio_request (to io)
blk_make_empty (to gs)
blk_invalidate_cache (was in io, but had GS assertion) blk_aio_cancel (to gs)
block:
bdrv_replace_child_bs (to gs)
bdrv_get_device_name (to io) bdrv_get_device_or_node_name (to io) bdrv_drained_end_no_poll (to io)
bdrv_block_status (was in io, but had GS assertion) bdrv_drain (to io)
bdrv_co_drain (to io)
bdrv_make_zero (was in GS, but did not have the assertion) bdrv_save_vmstate (to io)
bdrv_load_vmstate (to io)
bdrv_aio_cancel_async (to io)
block_int:
bdrv_get_parent_name (to io) bdrv_apply_subtree_drain (to io) bdrv_unapply_subtree_drain (to io) bdrv_co_copy_range_from (was in io, but had GS assertion) bdrv_co_copy_range_to (was in io, but had GS assertion) ->bdrv_save_vmstate (to io)
->bdrv_load_vmstate (to io)
coding style (assertion after definitions):
bdrv_save_vmstate
bdrv_load_vmstate
block_job_next
block_job_get
new patches:
block.c: modify .attach and .detach callbacks of child_of_bds introduce pre_run as JobDriver callback to handle
bdrv_co_amend usage of permission function
leave blk_set/get_perm as a TODO in fuse.c
make sure bdrv_co_invalidate_cache does not use permissions
if BQL is not held
minor changes:
put back TODO for include block/block.h in block-backend-common.h rebase on kwolf/block branch
modify where are used assert_bdrv_graph_writable, due to rebase
v4:
* Moved the following functions from block-io to block-global-state (+ assertion):
- bdrv_apply_auto_read_only
- bdrv_can_set_read_only
* Moved the following functions from block-backend-io to block-backend-global-state (+ assertion):
- blk_ioctl
* added patch 4 to distinguish assertions added to static functions in block.c
* block/coroutines: it seems that blk_co_do_ioctl and blk_do_ioctl are global state too
v3:
* blockdev.h (patch 14): blockdev_mark_auto_del, blockdev_auto_del and blk_legacy_dinfo as GS API.
* add copyright header to block.h, block-io.h and block-global-state.h
* rebase on current master (c5b2f55981)
v2:
* rename "graph API" into "global state API"
* change order of patches, block.h comes before block-backend.h
* change GS and I/O comment headers to avoid redundancy, all other headers refer to block-global-state.h and block-io.h
* fix typo on GS and I/O headers
* use assert instead of g_assert
* move bdrv_pwrite_sync, bdrv_block_status and bdrv_co_copy_range_{from/to} to the I/O API
* change assert_bdrv_graph_writable implementation, since we need to introduce additional drains
* remove transactions API split
* add preparation patch for blockdev.h (patch 13)
* backup-top -> copy-on-write
* change I/O comment in job.h into a better meaningful explanation
* fix all warnings given by checkpatch, mostly due to /* */ to be split in separate lines
* rebase on current master (c09124dcb8), and split the following new functions:
blk_replace_bs (I/O) bdrv_bsc_is_data (I/O) bdrv_bsc_invalidate_range (I/O) bdrv_bsc_fill (I/O) bdrv_new_open_driver_opts (GS) blk_get_max_hw_iov (I/O)
they are all added in patches 4 and 6.
v1:
* remove the iothread locking bug fix, and send it as separate patch
* rename graph API -> global state API
* better documented patch 1 (qemu_in_main_thread)
* add and split all other block layer headers
* fix warnings given by checkpatch on multiline comments
Emanuele Giuseppe Esposito (33):
main-loop.h: introduce qemu_in_main_thread() include/block/block: split header into I/O and global state API assertions for block global state API
block/export/fuse.c: allow writable exports to take RESIZE permission include/sysemu/block-backend: split header into I/O and global state
(GS) API
block/block-backend.c: assertions for block-backend include/block/block_int: split header into I/O and global state API assertions for block_int global state API
block: introduce assert_bdrv_graph_writable include/block/blockjob_int.h: split header into I/O and GS API assertions for blockjob_int.h
block.c: add assertions to static functions include/block/blockjob.h: global state API
assertions for blockjob.h global state API include/sysemu/blockdev.h: global state API
assertions for blockdev.h global state API include/block/snapshot: global state API + assertions block/copy-before-write.h: global state API + assertions
block: introduce bdrv_activate
block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and
test_sync_op_invalidate_cache
block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate block/coroutines: I/O API
block_int-common.h: split function pointers in BlockDriver block_int-common.h: assertions in the callers of BlockDriver function
pointers
block_int-common.h: split function pointers in BdrvChildClass block_int-common.h: assertions in the callers of BdrvChildClass
function pointers
block-backend-common.h: split function pointers in BlockDevOps job.h: split function pointers in JobDriver
job.h: assertions in the callers of JobDriver funcion pointers include/block/block_int-common.h: introduce bdrv_amend_pre_run and
bdrv_amend_clean
include/qemu/job.h: introduce job->pre_run() and use it in amend
crypto: delegate permission functions to JobDriver .pre_run block.c: assertions to the block layer permissions API
block.c | 278 +++- block/amend.c | 33 + block/backup.c | 1 + block/block-backend.c | 101 +- block/commit.c | 4 + block/copy-before-write.c | 2 + block/copy-before-write.h | 7 + block/coroutines.h | 6 + block/create.c | 2 + block/crypto.c | 73 +- block/dirty-bitmap.c | 1 + block/export/export.c | 2 +- block/export/fuse.c | 25 +- block/io.c | 26 + block/meson.build | 7 +- block/mirror.c | 4 + block/monitor/bitmap-qmp-cmds.c | 6 + block/parallels.c | 2 +- block/snapshot.c | 28 + block/stream.c | 2 + blockdev.c | 29 + blockjob.c | 14 + hw/block/pflash_cfi01.c | 2 +- hw/nvram/spapr_nvram.c | 2 +- include/block/block-common.h | 381 +++++ include/block/block-global-state.h | 269 ++++ include/block/block-io.h | 324 ++++ include/block/block.h | 879 +---------- include/block/block_int-common.h | 1215 +++++++++++++++ include/block/block_int-global-state.h | 320 ++++ include/block/block_int-io.h | 170 +++ include/block/block_int.h | 1475 +------------------ include/block/blockjob.h | 9 + include/block/blockjob_int.h | 28 + include/block/snapshot.h | 13 +- include/qemu/job.h | 31 + include/qemu/main-loop.h | 13 + include/sysemu/block-backend-common.h | 102 ++ include/sysemu/block-backend-global-state.h | 115 ++ include/sysemu/block-backend-io.h | 145 ++ include/sysemu/block-backend.h | 269 +--- include/sysemu/blockdev.h | 13 +- job.c | 22 + migration/block.c | 2 +- migration/migration.c | 10 +- migration/savevm.c | 6 +- monitor/qmp-cmds.c | 2 +- softmmu/cpus.c | 5 + softmmu/qdev-monitor.c | 2 + stubs/iothread-lock.c | 5 + tests/unit/test-block-iothread.c | 8 +- 51 files changed, 3824 insertions(+), 2666 deletions(-) create mode 100644 include/block/block-common.h create mode 100644 include/block/block-global-state.h create mode 100644 include/block/block-io.h create mode 100644 include/block/block_int-common.h create mode 100644 include/block/block_int-global-state.h create mode 100644 include/block/block_int-io.h create mode 100644 include/sysemu/block-backend-common.h create mode 100644 include/sysemu/block-backend-global-state.h create mode 100644 include/sysemu/block-backend-io.h
Signature · 1 line
2.31.1
PATCH v6 1/33 main-loop.h: introduce qemu_in_main_thread()
When invoked from the main loop, this function is the same as qemu_mutex_iothread_locked, and returns true if the BQL is held. When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop.
This essentially just extends qemu_mutex_iothread_locked to work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation in stubs/iothread-lock.c.
Using qemu_mutex_iothread_locked in unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop.
--- include/qemu/main-loop.h | 13 +++++++++++++ softmmu/cpus.c | 5 +++++ stubs/iothread-lock.c | 5 +++++ 3 files changed, 23 insertions(+)
include/qemu/main-loop.h +13 -0
@@ -245,6 +245,19 @@ AioContext *iohandler_get_aio_context(void);*/ bool qemu_mutex_iothread_locked (void ); +/** +* qemu_in_main_thread: same as qemu_mutex_iothread_locked when +* softmmu/cpus.c implementation is linked. Otherwise this function +* checks that the current AioContext is the global AioContext +* (main loop). +* +* This is useful when checking that the BQL is held, to avoid +* returning false when invoked by unit tests or other users like +* storage-daemon that end up using stubs/iothread-lock.c +* implementation. +*/ +bool qemu_in_main_thread (void ); +/** * qemu_mutex_lock_iothread: Lock the main loop mutex. *
softmmu/cpus.c +5 -0
@@ -485,6 +485,11 @@ bool qemu_mutex_iothread_locked(void)return iothread_locked; } +bool qemu_in_main_thread (void ) +{ +return qemu_mutex_iothread_locked (); +} +/* * The BQL is taken from so many places that it is worth profiling the * callers directly, instead of funneling them all through a single function.
stubs/iothread-lock.c +5 -0
@@ -6,6 +6,11 @@ bool qemu_mutex_iothread_locked(void)return false; } +bool qemu_in_main_thread (void ) +{ +return qemu_get_current_aio_context () ==qemu_get_aio_context (); +} +void qemu_mutex_lock_iothread_impl (const char *file,int line) { }
Signature · 1 line
2.31.1
PATCH v6 6/33 block/block-backend.c: assertions for block-backend
In reply to Emanuele Giuseppe EspositoAll the global state (GS) API functions will check that qemu_in_main_thread() returns true. If not, it means that the safety of BQL cannot be guaranteed, and they need to be moved to I/O.
--- block/block-backend.c | 79 ++++++++++++++++++++++++++++++++++++++++++ softmmu/qdev-monitor.c | 2 ++ 2 files changed, 81 insertions(+)
block/block-backend.c +79 -0
@@ -228,6 +228,7 @@ static void blk_root_activate(BdrvChild *child, Error **errp)void blk_set_force_allow_inactivate (BlockBackend *blk) { +assert (qemu_in_main_thread ()); blk->force_allow_inactivate = true; }
@@ -346,6 +347,8 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm) {BlockBackend *blk; +assert (qemu_in_main_thread ()); + blk =g_new0 (BlockBackend,1 ); blk->refcnt =1 ; blk->ctx = ctx;
@@ -383,6 +386,8 @@ BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm, {BlockBackend *blk =blk_new (bdrv_get_aio_context (bs), perm, shared_perm); +assert (qemu_in_main_thread ()); +if (blk_insert_bs (blk, bs, errp) <0 ) {blk_unref (blk);return NULL ;
@@ -411,6 +416,8 @@ BlockBackend *blk_new_open(const char *filename, const char *reference,uint64_t perm =0 ;uint64_t shared =BLK_PERM_ALL ; +assert (qemu_in_main_thread ()); +/* * blk_new_open() is mainly used in .bdrv_create implementations and the * tools where sharing isn't a major concern because the BDS stays private
@@ -488,6 +495,7 @@ static void drive_info_del(DriveInfo *dinfo)int blk_get_refcnt (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk ? blk->refcnt :0 ; }
@@ -498,6 +506,7 @@ int blk_get_refcnt(BlockBackend *blk)void blk_ref (BlockBackend *blk) {assert (blk->refcnt >0 ); +assert (qemu_in_main_thread ()); blk->refcnt++; }
@@ -508,6 +517,7 @@ void blk_ref(BlockBackend *blk)*/ void blk_unref (BlockBackend *blk) { +assert (qemu_in_main_thread ());if (blk) {assert (blk->refcnt >0 );if (blk->refcnt >1 ) {
@@ -528,6 +538,7 @@ void blk_unref(BlockBackend *blk)*/ BlockBackend *blk_all_next (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk ?QTAILQ_NEXT (blk, link) :QTAILQ_FIRST (&block_backends); }
@@ -536,6 +547,8 @@ void blk_remove_all_bs(void) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ()); +while ((blk =blk_all_next (blk)) !=NULL ) {AioContext *ctx =blk_get_aio_context (blk);
@@ -559,6 +572,7 @@ void blk_remove_all_bs(void)*/ BlockBackend *blk_next (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk ?QTAILQ_NEXT (blk, monitor_link) :QTAILQ_FIRST (&monitor_block_backends); }
@@ -625,6 +639,7 @@ static void bdrv_next_reset(BdrvNextIterator *it)BlockDriverState *bdrv_first (BdrvNextIterator *it) { +assert (qemu_in_main_thread ());bdrv_next_reset (it);return bdrv_next (it); }
@@ -662,6 +677,7 @@ bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) {assert (!blk->name);assert (name && name[0 ]); +assert (qemu_in_main_thread ());if (!id_wellformed (name)) {error_setg (errp,"Invalid device name" );
@@ -689,6 +705,8 @@ bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)*/ void monitor_remove_blk (BlockBackend *blk) { +assert (qemu_in_main_thread ()); +if (!blk->name) {return ; }
@@ -715,6 +733,7 @@ BlockBackend *blk_by_name(const char *name) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ());assert (name);while ((blk =blk_next (blk)) !=NULL ) {if (!strcmp (name, blk->name)) {
@@ -749,6 +768,7 @@ static BlockBackend *bdrv_first_blk(BlockDriverState *bs)*/ bool bdrv_has_blk (BlockDriverState *bs) { +assert (qemu_in_main_thread ());return bdrv_first_blk (bs) !=NULL ; }
@@ -759,6 +779,7 @@ bool bdrv_is_root_node(BlockDriverState *bs) {BdrvChild *c; +assert (qemu_in_main_thread ()); QLIST_FOREACH(c, &bs->parents, next_parent) {if (c->klass != &child_root) {return false;
@@ -808,6 +829,7 @@ BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)*/ BlockBackendPublic *blk_get_public (BlockBackend *blk) { +assert (qemu_in_main_thread ());return &blk->public; }
@@ -816,6 +838,7 @@ BlockBackendPublic *blk_get_public(BlockBackend *blk)*/ BlockBackend *blk_by_public (BlockBackendPublic *public) { +assert (qemu_in_main_thread ());return container_of (public, BlockBackend, public); }
@@ -827,6 +850,8 @@ void blk_remove_bs(BlockBackend *blk)ThrottleGroupMember *tgm = &blk->public.throttle_group_member;BdrvChild *root; +assert (qemu_in_main_thread ()); +notifier_list_notify (&blk->remove_bs_notifiers, blk);if (tgm->throttle_state) {BlockDriverState *bs =blk_bs (blk);
@@ -861,6 +886,7 @@ void blk_remove_bs(BlockBackend *blk)int blk_insert_bs (BlockBackend *blk,BlockDriverState *bs,Error **errp) {ThrottleGroupMember *tgm = &blk->public.throttle_group_member; +assert (qemu_in_main_thread ());bdrv_ref (bs); blk->root = bdrv_root_attach_child(bs, "root", &child_root, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@@ -884,6 +910,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)*/ int blk_replace_bs (BlockBackend *blk,BlockDriverState *new_bs,Error **errp) { +assert (qemu_in_main_thread ());return bdrv_replace_child_bs (blk->root, new_bs, errp); }
@@ -894,6 +921,7 @@ int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm, Error **errp) {int ret; +assert (qemu_in_main_thread ());if (blk->root && !blk->disable_perm) { ret =bdrv_child_try_set_perm (blk->root, perm, shared_perm, errp);
@@ -910,6 +938,7 @@ int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,void blk_get_perm (BlockBackend *blk,uint64_t *perm,uint64_t *shared_perm) { +assert (qemu_in_main_thread ()); *perm = blk->perm; *shared_perm = blk->shared_perm; }
@@ -920,6 +949,7 @@ void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)*/ int blk_attach_dev (BlockBackend *blk,DeviceState *dev) { +assert (qemu_in_main_thread ());if (blk->dev) {return -EBUSY ; }
@@ -945,6 +975,7 @@ int blk_attach_dev(BlockBackend *blk, DeviceState *dev)void blk_detach_dev (BlockBackend *blk,DeviceState *dev) {assert (blk->dev == dev); +assert (qemu_in_main_thread ()); blk->dev =NULL ; blk->dev_ops =NULL ; blk->dev_opaque =NULL ;
@@ -958,6 +989,7 @@ void blk_detach_dev(BlockBackend *blk, DeviceState *dev)*/ DeviceState *blk_get_attached_dev (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->dev; }
@@ -986,6 +1018,8 @@ BlockBackend *blk_by_dev(void *dev) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ()); +assert (dev !=NULL );while ((blk =blk_all_next (blk)) !=NULL ) {if (blk->dev == dev) {
@@ -1003,6 +1037,7 @@ BlockBackend *blk_by_dev(void *dev)void blk_set_dev_ops (BlockBackend *blk,const BlockDevOps *ops,void *opaque) { +assert (qemu_in_main_thread ()); blk->dev_ops = ops; blk->dev_opaque = opaque;
@@ -1024,6 +1059,7 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,*/ void blk_dev_change_media_cb (BlockBackend *blk,bool load,Error **errp) { +assert (qemu_in_main_thread ());if (blk->dev_ops && blk->dev_ops->change_media_cb) {bool tray_was_open, tray_is_open;Error *local_err =NULL ;
@@ -1115,6 +1151,7 @@ static void blk_root_resize(BdrvChild *child)void blk_iostatus_enable (BlockBackend *blk) { +assert (qemu_in_main_thread ()); blk->iostatus_enabled = true; blk->iostatus =BLOCK_DEVICE_IO_STATUS_OK ; }
@@ -1123,6 +1160,7 @@ void blk_iostatus_enable(BlockBackend *blk)* enables it _and_ the VM is configured to stop on errors */ bool blk_iostatus_is_enabled (const BlockBackend *blk) { +assert (qemu_in_main_thread ()); return (blk->iostatus_enabled && (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
@@ -1131,16 +1169,19 @@ bool blk_iostatus_is_enabled(const BlockBackend *blk)BlockDeviceIoStatus blk_iostatus (const BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->iostatus; }void blk_iostatus_disable (BlockBackend *blk) { +assert (qemu_in_main_thread ()); blk->iostatus_enabled = false; }void blk_iostatus_reset (BlockBackend *blk) { +assert (qemu_in_main_thread ());if (blk_iostatus_is_enabled (blk)) { blk->iostatus =BLOCK_DEVICE_IO_STATUS_OK ; }
@@ -1148,6 +1189,7 @@ void blk_iostatus_reset(BlockBackend *blk)void blk_iostatus_set_err (BlockBackend *blk,int error) { +assert (qemu_in_main_thread ());assert (blk_iostatus_is_enabled (blk));if (blk->iostatus ==BLOCK_DEVICE_IO_STATUS_OK ) { blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
@@ -1347,6 +1389,7 @@ int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,int blk_make_zero (BlockBackend *blk,BdrvRequestFlags flags) { +assert (qemu_in_main_thread ());return bdrv_make_zero (blk->root, flags); }
@@ -1556,6 +1599,7 @@ BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,void blk_aio_cancel (BlockAIOCB *acb) { +assert (qemu_in_main_thread ());bdrv_aio_cancel (acb); }
@@ -1732,6 +1776,8 @@ void blk_drain_all(void) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ()); +bdrv_drain_all_begin ();while ((blk =blk_all_next (blk)) !=NULL ) {
@@ -1751,6 +1797,7 @@ void blk_drain_all(void)void blk_set_on_error (BlockBackend *blk,BlockdevOnError on_read_error,BlockdevOnError on_write_error) { +assert (qemu_in_main_thread ()); blk->on_read_error = on_read_error; blk->on_write_error = on_write_error; }
@@ -1834,6 +1881,7 @@ void blk_error_action(BlockBackend *blk, BlockErrorAction action,bool blk_supports_write_perm (BlockBackend *blk) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (bs) {return !bdrv_is_read_only (bs);
@@ -1854,6 +1902,7 @@ bool blk_is_writable(BlockBackend *blk)bool blk_is_sg (BlockBackend *blk) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (!bs) {return false;
@@ -1869,6 +1918,7 @@ bool blk_enable_write_cache(BlockBackend *blk)void blk_set_enable_write_cache (BlockBackend *blk,bool wce) { +assert (qemu_in_main_thread ()); blk->enable_write_cache = wce; }
@@ -1908,6 +1958,7 @@ void blk_lock_medium(BlockBackend *blk, bool locked)void blk_eject (BlockBackend *blk,bool eject_flag) {BlockDriverState *bs =blk_bs (blk); +char *id;if (bs) {
@@ -1925,6 +1976,7 @@ void blk_eject(BlockBackend *blk, bool eject_flag)int blk_get_flags (BlockBackend *blk) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (bs) {return bdrv_get_flags (bs);
@@ -1994,6 +2046,7 @@ void *blk_blockalign(BlockBackend *blk, size_t size)bool blk_op_is_blocked (BlockBackend *blk,BlockOpType op,Error **errp) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (!bs) {return false;
@@ -2005,6 +2058,7 @@ bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)void blk_op_unblock (BlockBackend *blk,BlockOpType op,Error *reason) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (bs) {bdrv_op_unblock (bs, op, reason);
@@ -2014,6 +2068,7 @@ void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)void blk_op_block_all (BlockBackend *blk,Error *reason) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (bs) {bdrv_op_block_all (bs, reason);
@@ -2023,6 +2078,7 @@ void blk_op_block_all(BlockBackend *blk, Error *reason)void blk_op_unblock_all (BlockBackend *blk,Error *reason) {BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ());if (bs) {bdrv_op_unblock_all (bs, reason);
@@ -2082,6 +2138,7 @@ static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,int blk_set_aio_context (BlockBackend *blk,AioContext *new_context,Error **errp) { +assert (qemu_in_main_thread ());return blk_do_set_aio_context (blk, new_context, true, errp); }
@@ -2118,6 +2175,7 @@ void blk_add_aio_context_notifier(BlockBackend *blk, {BlockBackendAioNotifier *notifier;BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ()); notifier =g_new (BlockBackendAioNotifier,1 ); notifier->attached_aio_context = attached_aio_context;
@@ -2140,6 +2198,8 @@ void blk_remove_aio_context_notifier(BlockBackend *blk,BlockBackendAioNotifier *notifier;BlockDriverState *bs =blk_bs (blk); +assert (qemu_in_main_thread ()); +if (bs) {bdrv_remove_aio_context_notifier (bs, attached_aio_context, detach_aio_context, opaque);
@@ -2160,11 +2220,13 @@ void blk_remove_aio_context_notifier(BlockBackend *blk,void blk_add_remove_bs_notifier (BlockBackend *blk,Notifier *notify) { +assert (qemu_in_main_thread ());notifier_list_add (&blk->remove_bs_notifiers, notify); }void blk_add_insert_bs_notifier (BlockBackend *blk,Notifier *notify) { +assert (qemu_in_main_thread ());notifier_list_add (&blk->insert_bs_notifiers, notify); }
@@ -2227,6 +2289,7 @@ int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, int64_t pos, int size) { int ret; +assert (qemu_in_main_thread ());if (!blk_is_available (blk)) {return -ENOMEDIUM ;
@@ -2246,6 +2309,7 @@ int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,int blk_load_vmstate (BlockBackend *blk,uint8_t *buf,int64_t pos,int size) { +assert (qemu_in_main_thread ());if (!blk_is_available (blk)) {return -ENOMEDIUM ; }
@@ -2255,6 +2319,7 @@ int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)int blk_probe_blocksizes (BlockBackend *blk,BlockSizes *bsz) { +assert (qemu_in_main_thread ());if (!blk_is_available (blk)) {return -ENOMEDIUM ; }
@@ -2264,6 +2329,7 @@ int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)int blk_probe_geometry (BlockBackend *blk,HDGeometry *geo) { +assert (qemu_in_main_thread ());if (!blk_is_available (blk)) {return -ENOMEDIUM ; }
@@ -2277,6 +2343,7 @@ int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)*/ void blk_update_root_state (BlockBackend *blk) { +assert (qemu_in_main_thread ());assert (blk->root); blk->root_state.open_flags = blk->root->bs->open_flags;
@@ -2289,6 +2356,7 @@ void blk_update_root_state(BlockBackend *blk)*/ bool blk_get_detect_zeroes_from_root_state (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->root_state.detect_zeroes; }
@@ -2298,17 +2366,20 @@ bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)*/ int blk_get_open_flags_from_root_state (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->root_state.open_flags; }BlockBackendRootState *blk_get_root_state (BlockBackend *blk) { +assert (qemu_in_main_thread ());return &blk->root_state; }int blk_commit_all (void ) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ());while ((blk =blk_all_next (blk)) !=NULL ) {AioContext *aio_context =blk_get_aio_context (blk);
@@ -2333,6 +2404,7 @@ int blk_commit_all(void)/* throttling disk I/O limits */ void blk_set_io_limits (BlockBackend *blk,ThrottleConfig *cfg) { +assert (qemu_in_main_thread ());throttle_group_config (&blk->public.throttle_group_member, cfg); }
@@ -2341,6 +2413,7 @@ void blk_io_limits_disable(BlockBackend *blk)BlockDriverState *bs =blk_bs (blk);ThrottleGroupMember *tgm = &blk->public.throttle_group_member;assert (tgm->throttle_state); +assert (qemu_in_main_thread ());if (bs) {bdrv_ref (bs);bdrv_drained_begin (bs);
@@ -2356,12 +2429,14 @@ void blk_io_limits_disable(BlockBackend *blk)void blk_io_limits_enable (BlockBackend *blk,const char *group) {assert (!blk->public.throttle_group_member.throttle_state); +assert (qemu_in_main_thread ());throttle_group_register_tgm (&blk->public.throttle_group_member, group,blk_get_aio_context (blk)); }void blk_io_limits_update_group (BlockBackend *blk,const char *group) { +assert (qemu_in_main_thread ());/* this BB is not part of any group */ if (!blk->public.throttle_group_member.throttle_state) {return ;
@@ -2429,11 +2504,13 @@ static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter)void blk_register_buf (BlockBackend *blk,void *host,size_t size) { +assert (qemu_in_main_thread ());bdrv_register_buf (blk_bs (blk), host, size); }void blk_unregister_buf (BlockBackend *blk,void *host) { +assert (qemu_in_main_thread ());bdrv_unregister_buf (blk_bs (blk), host); }
@@ -2458,11 +2535,13 @@ int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,const BdrvChild *blk_root (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->root; }int blk_make_empty (BlockBackend *blk,Error **errp) { +assert (qemu_in_main_thread ());if (!blk_is_available (blk)) {error_setg (errp,"No medium inserted" );return -ENOMEDIUM ;
softmmu/qdev-monitor.c +2 -0
@@ -971,6 +971,8 @@ BlockBackend *blk_by_qdev_id(const char *id, Error **errp)DeviceState *dev;BlockBackend *blk; +assert (qemu_in_main_thread ()); + dev =find_device_state (id, errp);if (dev ==NULL ) {return NULL ;
Signature · 1 line
2.31.1
PATCH v6 13/33 include/block/blockjob.h: global state API
In reply to Emanuele Giuseppe Espositoblockjob functions run always under the BQL lock.
--- include/block/blockjob.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/block/blockjob.h +9 -0
@@ -74,6 +74,13 @@ typedef struct BlockJob {GSList *nodes; } BlockJob; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/** * block_job_next: * @job: A block job, or %NULL.
@@ -155,6 +162,8 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp);*/ void block_job_iostatus_reset (BlockJob *job); +/* Common functions that are neither I/O nor Global State */ +/** * block_job_is_internal: * @job: The job to determine if it is user-visible or not.
Signature · 1 line
2.31.1
PATCH v6 2/33 include/block/block: split header into I/O and global state API
In reply to Emanuele Giuseppe Espositoblock.h currently contains a mix of functions:
some of them run under the BQL and modify the block layer graph, others are instead thread-safe and perform I/O in iothreads. It is not easy to understand which function is part of which group (I/O vs GS), and this patch aims to clarify it.
The "GS" functions need the BQL, and often use aio_context_acquire/release and/or drain to be sure they can modify the graph safely.
The I/O function are instead thread safe, and can run in any AioContext.
By splitting the header in two files, block-io.h and block-global-state.h we have a clearer view on what needs what kind of protection. block-common.h contains common structures shared by both headers.
block.h is left there for legacy and to avoid changing all includes in all c files that use the block APIs.
Assertions are added in the next patch.
--- block.c | 3 + block/meson.build | 7 +- include/block/block-common.h | 381 +++++++++++++ include/block/block-global-state.h | 269 +++++++++ include/block/block-io.h | 324 +++++++++++ include/block/block.h | 879 +---------------------------- 6 files changed, 1005 insertions(+), 858 deletions(-) create mode 100644 include/block/block-common.h create mode 100644 include/block/block-global-state.h create mode 100644 include/block/block-io.h
block.c +3 -0
@@ -67,12 +67,15 @@#define NOT_DONE 0x7fffffff/* used while emulated sync operation in progress */ +/* Protected by BQL */ static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =QTAILQ_HEAD_INITIALIZER (graph_bdrv_states); +/* Protected by BQL */ static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =QTAILQ_HEAD_INITIALIZER (all_bdrv_states); +/* Protected by BQL */ static QLIST_HEAD(, BlockDriver) bdrv_drivers =QLIST_HEAD_INITIALIZER (bdrv_drivers);
block/meson.build +5 -2
@@ -113,8 +113,11 @@ block_ss.add(module_block_h) wrapper_py = find_program('../scripts/block-coroutine-wrapper.py') block_gen_c = custom_target('block-gen.c', output: 'block-gen.c', - input: files('../include/block/block.h', - 'coroutines.h'), + input: files( + '../include/block/block-io.h', + '../include/block/block-global-state.h', + 'coroutines.h' + ), command: [wrapper_py, '@OUTPUT@', '@INPUT@']) block_ss.add(block_gen_c)
include/block/block-common.h new file +381 -0
381 lines not on this page · raw message
include/block/block-global-state.h new file +269 -0
269 lines not on this page · raw message
include/block/block-io.h new file +324 -0
324 lines not on this page · raw message
include/block/block.h +23 -856
888 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v6 4/33 block/export/fuse.c: allow writable exports to take RESIZE permission
In reply to Emanuele Giuseppe EspositoAllow writable exports to get BLK_PERM_RESIZE permission from creation, in fuse_export_create().
In this way, there is no need to give the permission in fuse_do_truncate(), which might be run in an iothread.
Permissions should be set only in the main thread, so in any case if an iothread tries to set RESIZE, it will be blocked.
Also assert in fuse_do_truncate that if we give the RESIZE permission we can then restore the original ones, since we don't check the return value of blk_set_perm.
--- block/export/fuse.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
block/export/fuse.c +18 -7
@@ -86,8 +86,8 @@ static int fuse_export_create(BlockExport *blk_exp,assert (blk_exp_args->type ==BLOCK_EXPORT_TYPE_FUSE ); -/* For growable exports, take the RESIZE permission */ -if (args->growable) { +/* For growable and writable exports, take the RESIZE permission */ +if (args->growable || blk_exp_args->writable) {uint64_t blk_perm, blk_shared_perm;blk_get_perm (exp->common.blk, &blk_perm, &blk_shared_perm);
@@ -392,14 +392,23 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, {uint64_t blk_perm, blk_shared_perm;BdrvRequestFlags truncate_flags =0 ; -int ret; +bool add_resize_perm; +int ret, ret_check; + +/* Growable and writable exports have a permanent RESIZE permission */ + add_resize_perm = !exp->growable && !exp->writable;if (req_zero_write) { truncate_flags |=BDRV_REQ_ZERO_WRITE ; } -/* Growable exports have a permanent RESIZE permission */ -if (!exp->growable) { +if (add_resize_perm) { + +if (!qemu_in_main_thread ()) { +/* Changing permissions like below only works in the main thread */ +return -EPERM ; + } +blk_get_perm (exp->common.blk, &blk_perm, &blk_shared_perm); ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
@@ -412,9 +421,11 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, ret =blk_truncate (exp->common.blk, size, true, prealloc, truncate_flags,NULL ); -if (!exp->growable) { +if (add_resize_perm) {/* Must succeed, because we are only giving up the RESIZE permission */ -blk_set_perm (exp->common.blk, blk_perm, blk_shared_perm, &error_abort); + ret_check =blk_set_perm (exp->common.blk, blk_perm, + blk_shared_perm, &error_abort); +assert (ret_check ==0 ); }return ret;
Signature · 1 line
2.31.1
PATCH v6 19/33 block: introduce bdrv_activate
In reply to Emanuele Giuseppe EspositoThis function is currently just a wrapper for bdrv_invalidate_cache(), but in future will contain the code of bdrv_co_invalidate_cache() that has to always be protected by BQL, and leave the rest in the I/O coroutine.
Replace all bdrv_invalidate_cache() invokations with bdrv_activate().
--- block.c | 7 ++++++- block/block-backend.c | 2 +- block/export/export.c | 2 +- block/parallels.c | 2 +- include/block/block-global-state.h | 2 +- tests/unit/test-block-iothread.c | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-)
block.c +6 -1
@@ -6549,6 +6549,11 @@ void bdrv_init_with_whitelist(void)bdrv_init (); } +int bdrv_activate (BlockDriverState *bs,Error **errp) +{ +return bdrv_invalidate_cache (bs, errp); +} + int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) {BdrvChild *child, *parent;
@@ -6636,7 +6641,7 @@ void bdrv_invalidate_cache_all(Error **errp)int ret;aio_context_acquire (aio_context); - ret =bdrv_invalidate_cache (bs, errp); + ret =bdrv_activate (bs, errp);aio_context_release (aio_context);if (ret <0 ) {bdrv_next_cleanup (&it);
block/block-backend.c +1 -1
@@ -1940,7 +1940,7 @@ void blk_invalidate_cache(BlockBackend *blk, Error **errp)return ; } -bdrv_invalidate_cache (bs, errp); +bdrv_activate (bs, errp); }bool blk_is_inserted (BlockBackend *blk)
block/export/export.c +1 -1
@@ -139,7 +139,7 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)* access since the export could be available before migration handover. * ctx was acquired in the caller. */ -bdrv_invalidate_cache (bs,NULL ); +bdrv_activate (bs,NULL ); perm =BLK_PERM_CONSISTENT_READ ;if (export->writable) {
block/parallels.c +1 -1
@@ -873,7 +873,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, s->bat_dirty_bmap =bitmap_new (DIV_ROUND_UP (s->header_size, s->bat_dirty_block)); -/* Disable migration until bdrv_invalidate_cache method is added */ +/* Disable migration until bdrv_activate method is added */ error_setg (&s->migration_blocker,"The Parallels format used by node '%s' " "does not support live migration" ,bdrv_get_device_or_node_name (bs));
include/block/block-global-state.h +1 -1
@@ -143,8 +143,8 @@ BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,/* async block I/O */ void bdrv_aio_cancel (BlockAIOCB *acb); +int bdrv_activate (BlockDriverState *bs,Error **errp);void bdrv_invalidate_cache_all (Error **errp); -int bdrv_inactivate_all (void );int bdrv_flush_all (void );
tests/unit/test-block-iothread.c +1 -1
@@ -282,7 +282,7 @@ static void test_sync_op_check(BdrvChild *c)static void test_sync_op_invalidate_cache (BdrvChild *c) {/* Early success: Image is not inactive */ -bdrv_invalidate_cache (c->bs,NULL ); +bdrv_activate (c->bs,NULL ); }
Signature · 1 line
2.31.1
PATCH v6 5/33 include/sysemu/block-backend: split header into I/O and global state (GS) API
In reply to Emanuele Giuseppe EspositoSimilarly to the previous patches, split block-backend.h in block-backend-io.h and block-backend-global-state.h
In addition, remove "block/block.h" include as it seems it is not necessary anymore, together with "qemu/iov.h"
block-backend-common.h contains the structures shared between the two headers, and the functions that can't be categorized as I/O or global state.
Assertions are added in the next patch.
--- block/block-backend.c | 9 +- include/sysemu/block-backend-common.h | 84 ++++++ include/sysemu/block-backend-global-state.h | 115 +++++++++ include/sysemu/block-backend-io.h | 145 +++++++++++ include/sysemu/block-backend.h | 269 +------------------- 5 files changed, 353 insertions(+), 269 deletions(-) create mode 100644 include/sysemu/block-backend-common.h create mode 100644 include/sysemu/block-backend-global-state.h create mode 100644 include/sysemu/block-backend-io.h
block/block-backend.c +6 -3
@@ -79,6 +79,7 @@ struct BlockBackend {bool allow_aio_context_change;bool allow_write_beyond_eof; +/* Protected by BQL lock */ NotifierList remove_bs_notifiers, insert_bs_notifiers; QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers;
@@ -111,12 +112,14 @@ static const AIOCBInfo block_backend_aiocb_info = {static void drive_info_del (DriveInfo *dinfo);static BlockBackend *bdrv_first_blk (BlockDriverState *bs); -/* All BlockBackends */ +/* All BlockBackends. Protected by BQL lock. */ static QTAILQ_HEAD(, BlockBackend) block_backends =QTAILQ_HEAD_INITIALIZER (block_backends); -/* All BlockBackends referenced by the monitor and which are iterated through by -* blk_next() */ +/* +* All BlockBackends referenced by the monitor and which are iterated through by +* blk_next(). Protected by BQL lock. +*/ static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =QTAILQ_HEAD_INITIALIZER (monitor_block_backends);
include/sysemu/block-backend-common.h new file +84 -0
84 lines not on this page · raw message
include/sysemu/block-backend-global-state.h new file +115 -0
115 lines not on this page · raw message
include/sysemu/block-backend-io.h new file +145 -0
145 lines not on this page · raw message
include/sysemu/block-backend.h +3 -266
275 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v6 18/33 block/copy-before-write.h: global state API + assertions
In reply to Emanuele Giuseppe Espositocopy-before-write functions always run under BQL lock.
--- block/copy-before-write.c | 2 ++ block/copy-before-write.h | 7 +++++++ 2 files changed, 9 insertions(+)
block/copy-before-write.c +2 -0
@@ -223,6 +223,7 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,QDict *opts;assert (source->total_sectors == target->total_sectors); +assert (qemu_in_main_thread ()); opts =qdict_new ();qdict_put_str (opts,"driver" ,"copy-before-write" );
@@ -245,6 +246,7 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,void bdrv_cbw_drop (BlockDriverState *bs) { +assert (qemu_in_main_thread ());bdrv_drop_filter (bs, &error_abort);bdrv_unref (bs); }
block/copy-before-write.h +7 -0
@@ -29,6 +29,13 @@#include "block/block_int.h" #include "block/block-copy.h" +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ + BlockDriverState *bdrv_cbw_append(BlockDriverState *source, BlockDriverState *target, const char *filter_node_name,
Signature · 1 line
2.31.1
PATCH v6 21/33 block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate
In reply to Emanuele Giuseppe EspositoSplit bdrv_co_invalidate cache in two: the GS code that takes care of permissions and running GS callbacks, and leave only the I/O code (->bdrv_co_invalidate_cache) running in the I/O coroutine.
The only side effect is that bdrv_co_invalidate_cache is not recursive anymore, and so is every direct call to bdrv_invalidate_cache().
--- block.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
block.c +21 -15
@@ -6550,23 +6550,20 @@ void bdrv_init_with_whitelist(void) }int bdrv_activate (BlockDriverState *bs,Error **errp) -{ -return bdrv_invalidate_cache (bs, errp); -} - -int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) {BdrvChild *child, *parent;Error *local_err =NULL ;int ret;BdrvDirtyBitmap *bm; +assert (qemu_in_main_thread ()); +if (!bs->drv) {return -ENOMEDIUM ; } QLIST_FOREACH(child, &bs->children, next) { -bdrv_co_invalidate_cache (child->bs, &local_err); +bdrv_activate (child->bs, &local_err);if (local_err) {error_propagate (errp, local_err);return -EINVAL ;
@@ -6579,7 +6576,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)* Note that the required permissions of inactive images are always a * subset of the permissions required after activating the image. This * allows us to just get the permissions upfront without restricting -* drv->bdrv_invalidate_cache(). +* drv->bdrv_co_invalidate_cache(). * * It also means that in error cases, we don't have to try and revert to * the old permissions (which is an operation that could fail, too). We can
@@ -6594,14 +6591,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return ret; } -if (bs->drv->bdrv_co_invalidate_cache) { - bs->drv->bdrv_co_invalidate_cache (bs, &local_err); -if (local_err) { - bs->open_flags |=BDRV_O_INACTIVE ; -error_propagate (errp, local_err); -return -EINVAL ; - } - } +bdrv_invalidate_cache (bs, errp); FOR_EACH_DIRTY_BITMAP(bs, bm) {bdrv_dirty_bitmap_skip_store (bm, false);
@@ -6629,6 +6619,22 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return 0 ; } +int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) +{ +Error *local_err =NULL ; + +if (bs->drv->bdrv_co_invalidate_cache) { + bs->drv->bdrv_co_invalidate_cache (bs, &local_err); +if (local_err) { + bs->open_flags |=BDRV_O_INACTIVE ; +error_propagate (errp, local_err); +return -EINVAL ; + } + } + +return 0 ; +} +void bdrv_activate_all (Error **errp) {BlockDriverState *bs;
Signature · 1 line
2.31.1
PATCH v6 8/33 assertions for block_int global state API
In reply to Emanuele Giuseppe Esposito--- block.c | 15 +++++++++++++++ block/backup.c | 1 + block/block-backend.c | 3 +++ block/commit.c | 2 ++ block/dirty-bitmap.c | 1 + block/io.c | 1 + block/mirror.c | 4 ++++ block/monitor/bitmap-qmp-cmds.c | 6 ++++++ block/stream.c | 2 ++ blockdev.c | 7 +++++++ 10 files changed, 42 insertions(+)
block.c +15 -0
@@ -662,6 +662,8 @@ int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv,Error *local_err =NULL ;int ret; +assert (qemu_in_main_thread ()); + size =qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ); buf =qemu_opt_get_del (opts,BLOCK_OPT_PREALLOC ); prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
@@ -2491,6 +2493,8 @@ void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,uint64_t cumulative_perms =0 ;uint64_t cumulative_shared_perms =BLK_PERM_ALL ; +assert (qemu_in_main_thread ()); + QLIST_FOREACH(c, &bs->parents, next_parent) { cumulative_perms |= c->perm; cumulative_shared_perms &= c->shared_perm;
@@ -2550,6 +2554,8 @@ int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,Transaction *tran =tran_new ();int ret; +assert (qemu_in_main_thread ()); +bdrv_child_set_perm (c, perm, shared, tran); ret =bdrv_refresh_perms (c->bs, &local_err);
@@ -2580,6 +2586,8 @@ int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp)uint64_t parent_perms, parent_shared;uint64_t perms, shared; +assert (qemu_in_main_thread ()); +bdrv_get_cumulative_perm (bs, &parent_perms, &parent_shared);bdrv_child_perm (bs, c->bs, c, c->role,NULL , parent_perms, parent_shared, &perms, &shared);
@@ -2721,6 +2729,7 @@ void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { +assert (qemu_in_main_thread ());if (role &BDRV_CHILD_FILTERED ) {assert (!(role & (BDRV_CHILD_DATA |BDRV_CHILD_METADATA |BDRV_CHILD_COW )));
@@ -3080,6 +3089,8 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,BdrvChild *child =NULL ;Transaction *tran =tran_new (); +assert (qemu_in_main_thread ()); + ret =bdrv_attach_child_common (child_bs, child_name, child_class, child_role, perm, shared_perm, opaque, &child, tran, errp);
@@ -7429,6 +7440,8 @@ bool bdrv_recurse_can_replace(BlockDriverState *bs, {BlockDriverState *filtered; +assert (qemu_in_main_thread ()); +if (!bs || !bs->drv) {return false; }
@@ -7600,6 +7613,7 @@ static bool append_strong_runtime_options(QDict *d, BlockDriverState *bs)* would result in exactly bs->backing. */ static bool bdrv_backing_overridden (BlockDriverState *bs) { +assert (qemu_in_main_thread ());if (bs->backing) {return strcmp (bs->auto_backing_file, bs->backing->bs->filename);
@@ -7988,6 +8002,7 @@ static BlockDriverState *bdrv_do_skip_filters(BlockDriverState *bs,*/ BlockDriverState *bdrv_skip_implicit_filters (BlockDriverState *bs) { +assert (qemu_in_main_thread ());return bdrv_do_skip_filters (bs, true); }
block/backup.c +1 -0
@@ -372,6 +372,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,assert (bs);assert (target); +assert (qemu_in_main_thread ());/* QMP interface protects us from these cases */ assert (sync_mode !=MIRROR_SYNC_MODE_INCREMENTAL );
block/block-backend.c +3 -0
@@ -1092,6 +1092,7 @@ static void blk_root_change_media(BdrvChild *child, bool load)*/ bool blk_dev_has_removable_media (BlockBackend *blk) { +assert (qemu_in_main_thread ());return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); }
@@ -1109,6 +1110,7 @@ bool blk_dev_has_tray(BlockBackend *blk)*/ void blk_dev_eject_request (BlockBackend *blk,bool force) { +assert (qemu_in_main_thread ());if (blk->dev_ops && blk->dev_ops->eject_request_cb) { blk->dev_ops->eject_request_cb (blk->dev_opaque, force); }
@@ -1131,6 +1133,7 @@ bool blk_dev_is_tray_open(BlockBackend *blk)*/ bool blk_dev_is_medium_locked (BlockBackend *blk) { +assert (qemu_in_main_thread ());if (blk->dev_ops && blk->dev_ops->is_medium_locked) {return blk->dev_ops->is_medium_locked (blk->dev_opaque); }
block/commit.c +2 -0
@@ -253,6 +253,8 @@ void commit_start(const char *job_id, BlockDriverState *bs,uint64_t base_perms, iter_shared_perms;int ret; +assert (qemu_in_main_thread ()); +assert (top != bs);if (bdrv_skip_filters (top) ==bdrv_skip_filters (base)) {error_setg (errp,"Invalid files for merge: top and base are the same" );
block/dirty-bitmap.c +1 -0
@@ -673,6 +673,7 @@ void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup) {HBitmap *tmp = bitmap->bitmap;assert (!bdrv_dirty_bitmap_readonly (bitmap)); +assert (qemu_in_main_thread ()); bitmap->bitmap = backup;hbitmap_free (tmp); }
block/io.c +1 -0
@@ -687,6 +687,7 @@ void bdrv_drain_all_end_quiesce(BlockDriverState *bs) {int drained_end_counter =0 ; +assert (qemu_in_main_thread ());g_assert (bs->quiesce_counter >0 );g_assert (!bs->refcnt);
block/mirror.c +4 -0
@@ -1864,6 +1864,8 @@ void mirror_start(const char *job_id, BlockDriverState *bs,bool is_none_mode;BlockDriverState *base; +assert (qemu_in_main_thread ()); +if ((mode ==MIRROR_SYNC_MODE_INCREMENTAL ) || (mode ==MIRROR_SYNC_MODE_BITMAP )) { error_setg(errp, "Sync mode '%s' not supported",
@@ -1889,6 +1891,8 @@ BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs,bool base_read_only;BlockJob *job; +assert (qemu_in_main_thread ()); + base_read_only =bdrv_is_read_only (base);if (base_read_only) {
block/monitor/bitmap-qmp-cmds.c +6 -0
@@ -56,6 +56,8 @@ BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,BlockDriverState *bs;BdrvDirtyBitmap *bitmap; +assert (qemu_in_main_thread ()); +if (!node) {error_setg (errp,"Node cannot be NULL" );return NULL ;
@@ -155,6 +157,8 @@ BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,BdrvDirtyBitmap *bitmap;AioContext *aio_context; +assert (qemu_in_main_thread ()); + bitmap =block_dirty_bitmap_lookup (node, name, &bs, errp);if (!bitmap || !bs) {return NULL ;
@@ -261,6 +265,8 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,BlockDirtyBitmapMergeSourceList *lst;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); + dst =block_dirty_bitmap_lookup (node, target, &bs, errp);if (!dst) {return NULL ;
block/stream.c +2 -0
@@ -220,6 +220,8 @@ void stream_start(const char *job_id, BlockDriverState *bs,QDict *opts;int ret; +assert (qemu_in_main_thread ()); +assert (!(base && bottom));assert (!(backing_file_str && bottom));
blockdev.c +7 -0
@@ -69,6 +69,7 @@ QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =void bdrv_set_monitor_owned (BlockDriverState *bs) { +assert (qemu_in_main_thread ());QTAILQ_INSERT_TAIL (&monitor_bdrv_states, bs, monitor_list); }
@@ -629,6 +630,7 @@ BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) {int bdrv_flags =0 ; +assert (qemu_in_main_thread ());/* bdrv_open() defaults to the values in bdrv_flags (for compatibility * with other callers) rather than what we want as the real defaults. * Apply the defaults here instead. */
@@ -647,6 +649,7 @@ void blockdev_close_all_bdrv_states(void) {BlockDriverState *bs, *next_bs; +assert (qemu_in_main_thread ()); QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {AioContext *ctx =bdrv_get_aio_context (bs);
@@ -2300,6 +2303,8 @@ void qmp_transaction(TransactionActionList *dev_list,BlkActionState *state, *next;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); + QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;QTAILQ_INIT (&snap_bdrv_states);
@@ -3593,6 +3598,8 @@ void qmp_blockdev_del(const char *node_name, Error **errp)AioContext *aio_context;BlockDriverState *bs; +assert (qemu_in_main_thread ()); + bs =bdrv_find_node (node_name);if (!bs) {error_setg (errp,"Failed to find node with node-name='%s'" , node_name);
Signature · 1 line
2.31.1
PATCH v6 27/33 block-backend-common.h: split function pointers in BlockDevOps
In reply to Emanuele Giuseppe EspositoAssertions in the callers of the function pointrs are already added by previous patches.
--- include/sysemu/block-backend-common.h | 28 ++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
include/sysemu/block-backend-common.h +23 -5
@@ -27,6 +27,14 @@/* Callbacks for block device models */ typedef struct BlockDevOps { + + /* + * Global state (GS) API. These functions run under the BQL lock. + * + * See include/block/block-global-state.h for more information about + * the GS API. + */ + /* * Runs when virtual media changed (monitor commands eject, change) * Argument load is true on load and false on eject.
@@ -44,16 +52,26 @@ typedef struct BlockDevOps {* true, even if they do not support eject requests. */ void (*eject_request_cb)(void *opaque,bool force); -/* -* Is the virtual tray open? -* Device models implement this only when the device has a tray. -*/ -bool (*is_tray_open)(void *opaque); +/* * Is the virtual medium locked into the device? * Device models implement this only when device has such a lock. */ bool (*is_medium_locked)(void *opaque); + +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ + +/* +* Is the virtual tray open? +* Device models implement this only when the device has a tray. +*/ +bool (*is_tray_open)(void *opaque); +/* * Runs when the size changed (e.g. monitor command block_resize) */
Signature · 1 line
2.31.1
PATCH v6 10/33 include/block/blockjob_int.h: split header into I/O and GS API
In reply to Emanuele Giuseppe EspositoSince the I/O functions are not many, keep a single file. Also split the function pointers in BlockJobDriver.
--- include/block/blockjob_int.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
include/block/blockjob_int.h +28 -0
@@ -38,6 +38,13 @@ struct BlockJobDriver {/** Generic JobDriver callbacks and settings */ JobDriver job_driver; +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ +/* * Returns whether the job has pending requests for the child or will * submit new requests before the next pause point. This callback is polled
@@ -46,6 +53,13 @@ struct BlockJobDriver {*/ bool (*drained_poll)(BlockJob *job); +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/* * If the callback is not NULL, it will be invoked before the job is * resumed in a new AioContext. This is the place to move any resources
@@ -56,6 +70,13 @@ struct BlockJobDriver {void (*set_speed)(BlockJob *job,int64_t speed); }; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/** * block_job_create: * @job_id: The id of the newly-created job, or %NULL to have one
@@ -98,6 +119,13 @@ void block_job_free(Job *job);*/ void block_job_user_resume (Job *job); +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ +/** * block_job_ratelimit_get_delay: *
Signature · 1 line
2.31.1
PATCH v6 25/33 block_int-common.h: split function pointers in BdrvChildClass
In reply to Emanuele Giuseppe Esposito--- include/block/block_int-common.h | 67 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 27 deletions(-)
include/block/block_int-common.h +40 -27
@@ -815,12 +815,16 @@ struct BdrvChildClass {*/ bool parent_is_bds; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ void (*inherit_options)(BdrvChildRole role,bool parent_is_format,int *child_flags,QDict *child_options,int parent_flags,QDict *parent_options); -void (*change_media)(BdrvChild *child,bool load); -void (*resize)(BdrvChild *child);/* * Returns a name that is supposedly more useful for human users than the
@@ -837,6 +841,40 @@ struct BdrvChildClass {*/ char *(*get_parent_desc)(BdrvChild *child); +/* +* Notifies the parent that the child has been activated/inactivated (e.g. +* when migration is completing) and it can start/stop requesting +* permissions and doing I/O on it. +*/ +void (*activate)(BdrvChild *child,Error **errp); +int (*inactivate)(BdrvChild *child); + +void (*attach)(BdrvChild *child); +void (*detach)(BdrvChild *child); + +/* +* Notifies the parent that the filename of its child has changed (e.g. +* because the direct child was removed from the backing chain), so that it +* can update its reference. +*/ +int (*update_filename)(BdrvChild *child,BlockDriverState *new_base, +const char *filename,Error **errp); + +bool (*can_set_aio_ctx)(BdrvChild *child,AioContext *ctx, +GSList **ignore,Error **errp); +void (*set_aio_ctx)(BdrvChild *child,AioContext *ctx,GSList **ignore); + + AioContext *(*get_parent_aio_context)(BdrvChild *child); + +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ + +void (*resize)(BdrvChild *child); +/* * If this pair of functions is implemented, the parent doesn't issue new * requests after returning from .drained_begin() until .drained_end() is
@@ -861,31 +899,6 @@ struct BdrvChildClass {* activity on the child has stopped. */ bool (*drained_poll)(BdrvChild *child); - -/* -* Notifies the parent that the child has been activated/inactivated (e.g. -* when migration is completing) and it can start/stop requesting -* permissions and doing I/O on it. -*/ -void (*activate)(BdrvChild *child,Error **errp); -int (*inactivate)(BdrvChild *child); - -void (*attach)(BdrvChild *child); -void (*detach)(BdrvChild *child); - -/* -* Notifies the parent that the filename of its child has changed (e.g. -* because the direct child was removed from the backing chain), so that it -* can update its reference. -*/ -int (*update_filename)(BdrvChild *child,BlockDriverState *new_base, -const char *filename,Error **errp); - -bool (*can_set_aio_ctx)(BdrvChild *child,AioContext *ctx, -GSList **ignore,Error **errp); -void (*set_aio_ctx)(BdrvChild *child,AioContext *ctx,GSList **ignore); - - AioContext *(*get_parent_aio_context)(BdrvChild *child); };extern const BdrvChildClass child_of_bds;
Signature · 1 line
2.31.1
PATCH v6 14/33 assertions for blockjob.h global state API
In reply to Emanuele Giuseppe Esposito--- blockjob.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
blockjob.c +10 -0
@@ -62,6 +62,7 @@ static bool is_block_job(Job *job)BlockJob *block_job_next (BlockJob *bjob) {Job *job = bjob ? &bjob->job :NULL ; +assert (qemu_in_main_thread ()); do { job = job_next(job);
@@ -73,6 +74,7 @@ BlockJob *block_job_next(BlockJob *bjob)BlockJob *block_job_get (const char *id) {Job *job =job_get (id); +assert (qemu_in_main_thread ());if (job &&is_block_job (job)) {return container_of (job, BlockJob, job);
@@ -184,6 +186,7 @@ static const BdrvChildClass child_job = {void block_job_remove_all_bdrv (BlockJob *job) { +assert (qemu_in_main_thread ());/* * bdrv_root_unref_child() may reach child_job_[can_]set_aio_ctx(), * which will also traverse job->nodes, so consume the list one by
@@ -206,6 +209,7 @@ void block_job_remove_all_bdrv(BlockJob *job)bool block_job_has_bdrv (BlockJob *job,BlockDriverState *bs) {GSList *el; +assert (qemu_in_main_thread ());for (el = job->nodes; el; el = el->next) {BdrvChild *c = el->data;
@@ -222,6 +226,7 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, {BdrvChild *c;bool need_context_ops; +assert (qemu_in_main_thread ());bdrv_ref (bs);
@@ -271,6 +276,8 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)const BlockJobDriver *drv =block_job_driver (job);int64_t old_speed = job->speed; +assert (qemu_in_main_thread ()); +if (job_apply_verb (&job->job,JOB_VERB_SET_SPEED , errp) <0 ) {return false; }
@@ -308,6 +315,8 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)BlockJobInfo *info;uint64_t progress_current, progress_total; +assert (qemu_in_main_thread ()); +if (block_job_is_internal (job)) {error_setg (errp,"Cannot query QEMU internal jobs" );return NULL ;
@@ -491,6 +500,7 @@ fail:void block_job_iostatus_reset (BlockJob *job) { +assert (qemu_in_main_thread ());if (job->iostatus ==BLOCK_DEVICE_IO_STATUS_OK ) {return ; }
Signature · 1 line
2.31.1
PATCH v6 9/33 block: introduce assert_bdrv_graph_writable
In reply to Emanuele Giuseppe EspositoWe want to be sure that the functions that write the child and parent list of a bs are under BQL and drain.
BQL prevents from concurrent writings from the GS API, while drains protect from I/O.
TODO: drains are missing in some functions using this assert. Therefore a proper assertion will fail. Because adding drains requires additional discussions, they will be added in future series.
--- block.c | 4 ++++ block/io.c | 11 +++++++++++ include/block/block_int-global-state.h | 8 ++++++++ 3 files changed, 23 insertions(+)
block.c +4 -0
@@ -1415,6 +1415,7 @@ static void bdrv_child_cb_attach(BdrvChild *child) {BlockDriverState *bs = child->opaque; +assert_bdrv_graph_writable (bs);QLIST_INSERT_HEAD (&bs->children, child, next);if (child->role &BDRV_CHILD_COW ) {
@@ -1434,6 +1435,7 @@ static void bdrv_child_cb_detach(BdrvChild *child)bdrv_unapply_subtree_drain (child, bs); +assert_bdrv_graph_writable (bs);QLIST_REMOVE (child, next); }
@@ -2814,6 +2816,7 @@ static void bdrv_replace_child_noperm(BdrvChild **childp,if (child->klass->detach) { child->klass->detach (child); } +assert_bdrv_graph_writable (old_bs);QLIST_REMOVE (child, next_parent); }
@@ -2823,6 +2826,7 @@ static void bdrv_replace_child_noperm(BdrvChild **childp, }if (new_bs) { +assert_bdrv_graph_writable (new_bs);QLIST_INSERT_HEAD (&new_bs->parents, child, next_parent);/*
block/io.c +11 -0
@@ -734,6 +734,17 @@ void bdrv_drain_all(void)bdrv_drain_all_end (); } +void assert_bdrv_graph_writable (BlockDriverState *bs) +{ +/* +* TODO: this function is incomplete. Because the users of this +* assert lack the necessary drains, check only for BQL. +* Once the necessary drains are added, +* assert also for qatomic_read(&bs->quiesce_counter) > 0 +*/ +assert (qemu_in_main_thread ()); +} +/** * Remove an active request from the tracked requests list *
include/block/block_int-global-state.h +8 -0
@@ -309,4 +309,12 @@ void bdrv_remove_aio_context_notifier(BlockDriverState *bs,*/ void bdrv_drain_all_end_quiesce (BlockDriverState *bs); +/** +* Make sure that the function is running under both drain and BQL. +* The latter protects from concurrent writings +* from the GS API, while the former prevents concurrent reads +* from I/O. +*/ +void assert_bdrv_graph_writable (BlockDriverState *bs); +#endif /* BLOCK_INT_GLOBAL_STATE */
Signature · 1 line
2.31.1
PATCH v6 11/33 assertions for blockjob_int.h
In reply to Emanuele Giuseppe Esposito--- blockjob.c | 4 ++++ 1 file changed, 4 insertions(+)
blockjob.c +4 -0
@@ -84,6 +84,7 @@ BlockJob *block_job_get(const char *id)void block_job_free (Job *job) {BlockJob *bjob =container_of (job, BlockJob, job); +assert (qemu_in_main_thread ());block_job_remove_all_bdrv (bjob);ratelimit_destroy (&bjob->limit);
@@ -435,6 +436,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,BlockJob *job;int ret; +assert (qemu_in_main_thread ()); +if (job_id ==NULL && !(flags &JOB_INTERNAL )) { job_id =bdrv_get_device_name (bs); }
@@ -498,6 +501,7 @@ void block_job_iostatus_reset(BlockJob *job)void block_job_user_resume (Job *job) {BlockJob *bjob =container_of (job, BlockJob, job); +assert (qemu_in_main_thread ());block_job_iostatus_reset (bjob); }
Signature · 1 line
2.31.1
PATCH v6 7/33 include/block/block_int: split header into I/O and global state API
In reply to Emanuele Giuseppe EspositoSimilarly to the previous patch, split block_int.h in block_int-io.h and block_int-global-state.h
block_int-common.h contains the structures shared between the two headers, and the functions that can't be categorized as I/O or global state.
Assertions are added in the next patch.
--- blockdev.c | 5 + include/block/block_int-common.h | 1161 +++++++++++++++++++ include/block/block_int-global-state.h | 312 +++++ include/block/block_int-io.h | 170 +++ include/block/block_int.h | 1475 +----------------------- 5 files changed, 1651 insertions(+), 1472 deletions(-) create mode 100644 include/block/block_int-common.h create mode 100644 include/block/block_int-global-state.h create mode 100644 include/block/block_int-io.h
blockdev.c +5 -0
@@ -63,6 +63,7 @@#include "qemu/main-loop.h" #include "qemu/throttle-options.h" +/* Protected by BQL lock */ QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =QTAILQ_HEAD_INITIALIZER (monitor_bdrv_states);
@@ -1175,6 +1176,8 @@ typedef struct BlkActionState BlkActionState;* * Only prepare() may fail. In a single transaction, only one of commit() or * abort() will be called. clean() will always be called if it is present. +* +* Always run under BQL. */ typedef struct BlkActionOps { size_t instance_size;
@@ -2284,6 +2287,8 @@ static TransactionProperties *get_transaction_properties(/* * 'Atomic' group operations. The operations are performed as a set, and if * any fail then we roll back all operations in the group. +* +* Always run under BQL. */ void qmp_transaction(TransactionActionList *dev_list, bool has_props,
include/block/block_int-common.h new file +1161 -0
1,161 lines not on this page · raw message
include/block/block_int-global-state.h new file +312 -0
312 lines not on this page · raw message
include/block/block_int-io.h new file +170 -0
170 lines not on this page · raw message
include/block/block_int.h +3 -1472
1,334 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.31.1
Part of the diff is not shown here. View the raw message.
PATCH v6 22/33 block/coroutines: I/O API
In reply to Emanuele Giuseppe Espositoblock coroutines functions run in different aiocontext, and are not protected by the BQL. Therefore are I/O.
--- block/coroutines.h | 6 ++++++ 1 file changed, 6 insertions(+)
block/coroutines.h +6 -0
@@ -29,6 +29,12 @@/* For blk_bs() in generated block/block-gen.c */ #include "sysemu/block-backend.h" +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ int coroutine_fn bdrv_co_check(BlockDriverState *bs,BdrvCheckResult *res,BdrvCheckMode fix);
Signature · 1 line
2.31.1
PATCH v6 15/33 include/sysemu/blockdev.h: global state API
In reply to Emanuele Giuseppe Espositoblockdev functions run always under the BQL lock.
--- include/sysemu/blockdev.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
include/sysemu/blockdev.h +10 -3
@@ -13,9 +13,6 @@#include "block/block.h" #include "qemu/queue.h" -void blockdev_mark_auto_del (BlockBackend *blk); -void blockdev_auto_del (BlockBackend *blk); - typedef enum { IF_DEFAULT = -1, /* for use with drive_add() only */ /*
@@ -38,6 +35,16 @@ struct DriveInfo {QTAILQ_ENTRY (DriveInfo) next; }; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ + +void blockdev_mark_auto_del (BlockBackend *blk); +void blockdev_auto_del (BlockBackend *blk); +DriveInfo *blk_legacy_dinfo (BlockBackend *blk);DriveInfo *blk_set_legacy_dinfo (BlockBackend *blk,DriveInfo *dinfo);BlockBackend *blk_by_legacy_dinfo (DriveInfo *dinfo);
Signature · 1 line
2.31.1
PATCH v6 23/33 block_int-common.h: split function pointers in BlockDriver
In reply to Emanuele Giuseppe EspositoSimilar to the header split, also the function pointers in BlockDriver can be split in I/O and global state.
--- include/block/block_int-common.h | 434 ++++++++++++++++--------------- 1 file changed, 231 insertions(+), 203 deletions(-)
include/block/block_int-common.h +231 -203
@@ -96,6 +96,11 @@ typedef struct BdrvTrackedRequest {struct BlockDriver { +/* +* These fields are initialized when this object is created, +* and are never changed afterwards. +*/ +const char *format_name;int instance_size;
@@ -121,23 +126,7 @@ struct BlockDriver {* on those children. */ bool is_format; -/* -* Return true if @to_replace can be replaced by a BDS with the -* same data as @bs without it affecting @bs's behavior (that is, -* without it being visible to @bs's parents). -*/ -bool (*bdrv_recurse_can_replace)(BlockDriverState *bs, -BlockDriverState *to_replace); - -int (*bdrv_probe)(const uint8_t *buf,int buf_size,const char *filename); -int (*bdrv_probe_device)(const char *filename); -/* -* Any driver implementing this callback is expected to be able to handle -* NULL file names in its .bdrv_open() implementation. -*/ -void (*bdrv_parse_filename)(const char *filename,QDict *options, -Error **errp);/* * Drivers not implementing bdrv_parse_filename nor bdrv_open should have * this field set to true, except ones that are defined only by their
@@ -159,7 +148,66 @@ struct BlockDriver {*/ bool supports_backing; -/* For handling image reopen for split or non-split files */ +bool has_variable_length; + +/* +* Drivers setting this field must be able to work with just a plain +* filename with '<protocol_name>:' as a prefix, and no other options. +* Options may be extracted from the filename by implementing +* bdrv_parse_filename. +*/ +const char *protocol_name; + +/* List of options for creating images, terminated by name == NULL */ +QemuOptsList *create_opts; + +/* List of options for image amend */ +QemuOptsList *amend_opts; + +/* +* If this driver supports reopening images this contains a +* NULL-terminated list of the runtime options that can be +* modified. If an option in this list is unspecified during +* reopen then it _must_ be reset to its default value or return +* an error. +*/ +const char *const *mutable_opts; + +/* +* Pointer to a NULL-terminated array of names of strong options +* that can be specified for bdrv_open(). A strong option is one +* that changes the data of a BDS. +* If this pointer is NULL, the array is considered empty. +* "filename" and "driver" are always considered strong. +*/ +const char *const *strong_runtime_opts; + +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ + +/* +* Return true if @to_replace can be replaced by a BDS with the +* same data as @bs without it affecting @bs's behavior (that is, +* without it being visible to @bs's parents). +*/ +bool (*bdrv_recurse_can_replace)(BlockDriverState *bs, +BlockDriverState *to_replace); + +int (*bdrv_probe)(const uint8_t *buf,int buf_size,const char *filename); +int (*bdrv_probe_device)(const char *filename); + +/* +* Any driver implementing this callback is expected to be able to handle +* NULL file names in its .bdrv_open() implementation. +*/ +void (*bdrv_parse_filename)(const char *filename,QDict *options, +Error **errp); + +/* For handling image reopen for split or non-split files. */ int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state);
@@ -175,7 +223,6 @@ struct BlockDriver { Error **errp);void (*bdrv_close)(BlockDriverState *bs); - int coroutine_fn (*bdrv_co_create)(BlockdevCreateOptions *opts,Error **errp); int coroutine_fn (*bdrv_co_create_opts)(BlockDriver *drv,
@@ -183,11 +230,6 @@ struct BlockDriver { QemuOpts *opts, Error **errp); - int coroutine_fn (*bdrv_co_amend)(BlockDriverState *bs, -BlockdevAmendOptions *opts, -bool force, -Error **errp); - int (*bdrv_amend_options)(BlockDriverState *bs, QemuOpts *opts, BlockDriverAmendStatusCB *status_cb,
@@ -234,6 +276,172 @@ struct BlockDriver {*/ char *(*bdrv_dirname)(BlockDriverState *bs,Error **errp); +/* +* This informs the driver that we are no longer interested in the result +* of in-flight requests, so don't waste the time if possible. +* +* One example usage is to avoid waiting for an nbd target node reconnect +* timeout during job-cancel with force=true. +*/ +void (*bdrv_cancel_in_flight)(BlockDriverState *bs); + +int (*bdrv_inactivate)(BlockDriverState *bs); + +int (*bdrv_snapshot_create)(BlockDriverState *bs, +QEMUSnapshotInfo *sn_info); +int (*bdrv_snapshot_goto)(BlockDriverState *bs, +const char *snapshot_id); +int (*bdrv_snapshot_delete)(BlockDriverState *bs, +const char *snapshot_id, +const char *name, +Error **errp); +int (*bdrv_snapshot_list)(BlockDriverState *bs, +QEMUSnapshotInfo **psn_info); +int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, +const char *snapshot_id, +const char *name, +Error **errp); + +int (*bdrv_change_backing_file)(BlockDriverState *bs, +const char *backing_file,const char *backing_fmt); + +/* TODO Better pass a option string/QDict/QemuOpts to add any rule? */ +int (*bdrv_debug_breakpoint)(BlockDriverState *bs,const char *event, +const char *tag); +int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs, +const char *tag); +int (*bdrv_debug_resume)(BlockDriverState *bs,const char *tag); +bool (*bdrv_debug_is_suspended)(BlockDriverState *bs,const char *tag); + +void (*bdrv_refresh_limits)(BlockDriverState *bs,Error **errp); + +/* +* Returns 1 if newly created images are guaranteed to contain only +* zeros, 0 otherwise. +*/ +int (*bdrv_has_zero_init)(BlockDriverState *bs); + +/* +* Remove fd handlers, timers, and other event loop callbacks so the event +* loop is no longer in use. Called with no in-flight requests and in +* depth-first traversal order with parents before child nodes. +*/ +void (*bdrv_detach_aio_context)(BlockDriverState *bs); + +/* +* Add fd handlers, timers, and other event loop callbacks so I/O requests +* can be processed again. Called with no in-flight requests and in +* depth-first traversal order with child nodes before parent nodes. +*/ +void (*bdrv_attach_aio_context)(BlockDriverState *bs, +AioContext *new_context); + +/** +* Try to get @bs's logical and physical block size. +* On success, store them in @bsz and return zero. +* On failure, return negative errno. +*/ +int (*bdrv_probe_blocksizes)(BlockDriverState *bs,BlockSizes *bsz); +/** +* Try to get @bs's geometry (cyls, heads, sectors) +* On success, store them in @geo and return 0. +* On failure return -errno. +* Only drivers that want to override guest geometry implement this +* callback; see hd_geometry_guess(). +*/ +int (*bdrv_probe_geometry)(BlockDriverState *bs,HDGeometry *geo); + +void (*bdrv_add_child)(BlockDriverState *parent,BlockDriverState *child, +Error **errp); +void (*bdrv_del_child)(BlockDriverState *parent,BdrvChild *child, +Error **errp); + +/** +* Informs the block driver that a permission change is intended. The +* driver checks whether the change is permissible and may take other +* preparations for the change (e.g. get file system locks). This operation +* is always followed either by a call to either .bdrv_set_perm or +* .bdrv_abort_perm_update. +* +* Checks whether the requested set of cumulative permissions in @perm +* can be granted for accessing @bs and whether no other users are using +* permissions other than those given in @shared (both arguments take +* BLK_PERM_* bitmasks). +* +* If both conditions are met, 0 is returned. Otherwise, -errno is returned +* and errp is set to an error describing the conflict. +*/ +int (*bdrv_check_perm)(BlockDriverState *bs,uint64_t perm, +uint64_t shared,Error **errp); + +/** +* Called to inform the driver that the set of cumulative set of used +* permissions for @bs has changed to @perm, and the set of sharable +* permission to @shared. The driver can use this to propagate changes to +* its children (i.e. request permissions only if a parent actually needs +* them). +* +* This function is only invoked after bdrv_check_perm(), so block drivers +* may rely on preparations made in their .bdrv_check_perm implementation. +*/ +void (*bdrv_set_perm)(BlockDriverState *bs,uint64_t perm,uint64_t shared); + +/* +* Called to inform the driver that after a previous bdrv_check_perm() +* call, the permission update is not performed and any preparations made +* for it (e.g. taken file locks) need to be undone. +* +* This function can be called even for nodes that never saw a +* bdrv_check_perm() call. It is a no-op then. +*/ +void (*bdrv_abort_perm_update)(BlockDriverState *bs); + +/** +* Returns in @nperm and @nshared the permissions that the driver for @bs +* needs on its child @c, based on the cumulative permissions requested by +* the parents in @parent_perm and @parent_shared. +* +* If @c is NULL, return the permissions for attaching a new child for the +* given @child_class and @role. +* +* If @reopen_queue is non-NULL, don't return the currently needed +* permissions, but those that will be needed after applying the +* @reopen_queue. +*/ +void (*bdrv_child_perm)(BlockDriverState *bs,BdrvChild *c, +BdrvChildRole role, +BlockReopenQueue *reopen_queue, +uint64_t parent_perm,uint64_t parent_shared, +uint64_t *nperm,uint64_t *nshared); + +/** +* Register/unregister a buffer for I/O. For example, when the driver is +* interested to know the memory areas that will later be used in iovs, so +* that it can do IOMMU mapping with VFIO etc., in order to get better +* performance. In the case of VFIO drivers, this callback is used to do +* DMA mapping for hot buffers. +*/ +void (*bdrv_register_buf)(BlockDriverState *bs,void *host,size_t size); +void (*bdrv_unregister_buf)(BlockDriverState *bs,void *host); + +/* +* This field is modified only under the BQL, and is part of +* the global state. +*/ +QLIST_ENTRY (BlockDriver) list; + +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/ + + int coroutine_fn (*bdrv_co_amend)(BlockDriverState *bs, +BlockdevAmendOptions *opts, +bool force, +Error **errp); +/* aio */ BlockAIOCB *(*bdrv_aio_preadv)(BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
@@ -374,21 +582,11 @@ struct BlockDriver { bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum, int64_t *map, BlockDriverState **file); -/* -* This informs the driver that we are no longer interested in the result -* of in-flight requests, so don't waste the time if possible. -* -* One example usage is to avoid waiting for an nbd target node reconnect -* timeout during job-cancel with force=true. -*/ -void (*bdrv_cancel_in_flight)(BlockDriverState *bs); -/* * Invalidate any cached meta-data. */ void coroutine_fn (*bdrv_co_invalidate_cache)(BlockDriverState *bs,Error **errp); -int (*bdrv_inactivate)(BlockDriverState *bs);/* * Flushes all data for all layers by calling bdrv_co_flush for underlying
@@ -414,14 +612,6 @@ struct BlockDriver {*/ int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs); -/* -* Drivers setting this field must be able to work with just a plain -* filename with '<protocol_name>:' as a prefix, and no other options. -* Options may be extracted from the filename by implementing -* bdrv_parse_filename. -*/ -const char *protocol_name; -/* * Truncate @bs to @offset bytes using the given @prealloc mode * when growing. Modes other than PREALLOC_MODE_OFF should be
@@ -439,7 +629,6 @@ struct BlockDriver { bool exact, PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);int64_t (*bdrv_getlength)(BlockDriverState *bs); -bool has_variable_length;int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs); BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs, Error **errp);
@@ -450,20 +639,6 @@ struct BlockDriver { int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset); -int (*bdrv_snapshot_create)(BlockDriverState *bs, -QEMUSnapshotInfo *sn_info); -int (*bdrv_snapshot_goto)(BlockDriverState *bs, -const char *snapshot_id); -int (*bdrv_snapshot_delete)(BlockDriverState *bs, -const char *snapshot_id, -const char *name, -Error **errp); -int (*bdrv_snapshot_list)(BlockDriverState *bs, -QEMUSnapshotInfo **psn_info); -int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, -const char *snapshot_id, -const char *name, -Error **errp);int (*bdrv_get_info)(BlockDriverState *bs,BlockDriverInfo *bdi); ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs,
@@ -477,9 +652,6 @@ struct BlockDriver {QEMUIOVector *qiov, int64_t pos); -int (*bdrv_change_backing_file)(BlockDriverState *bs, -const char *backing_file,const char *backing_fmt); -/* removable device specific */ bool (*bdrv_is_inserted)(BlockDriverState *bs);void (*bdrv_eject)(BlockDriverState *bs,bool eject_flag);
@@ -492,21 +664,6 @@ struct BlockDriver { int coroutine_fn (*bdrv_co_ioctl)(BlockDriverState *bs,unsigned long int req,void *buf); -/* List of options for creating images, terminated by name == NULL */ -QemuOptsList *create_opts; - -/* List of options for image amend */ -QemuOptsList *amend_opts; - -/* -* If this driver supports reopening images this contains a -* NULL-terminated list of the runtime options that can be -* modified. If an option in this list is unspecified during -* reopen then it _must_ be reset to its default value or return -* an error. -*/ -const char *const *mutable_opts; -/* * Returns 0 for completed check, -errno for internal errors. * The check results are stored in result.
@@ -517,56 +674,10 @@ struct BlockDriver {void (*bdrv_debug_event)(BlockDriverState *bs,BlkdebugEvent event); -/* TODO Better pass a option string/QDict/QemuOpts to add any rule? */ -int (*bdrv_debug_breakpoint)(BlockDriverState *bs,const char *event, -const char *tag); -int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs, -const char *tag); -int (*bdrv_debug_resume)(BlockDriverState *bs,const char *tag); -bool (*bdrv_debug_is_suspended)(BlockDriverState *bs,const char *tag); - -void (*bdrv_refresh_limits)(BlockDriverState *bs,Error **errp); - -/* -* Returns 1 if newly created images are guaranteed to contain only -* zeros, 0 otherwise. -*/ -int (*bdrv_has_zero_init)(BlockDriverState *bs); - -/* -* Remove fd handlers, timers, and other event loop callbacks so the event -* loop is no longer in use. Called with no in-flight requests and in -* depth-first traversal order with parents before child nodes. -*/ -void (*bdrv_detach_aio_context)(BlockDriverState *bs); - -/* -* Add fd handlers, timers, and other event loop callbacks so I/O requests -* can be processed again. Called with no in-flight requests and in -* depth-first traversal order with child nodes before parent nodes. -*/ -void (*bdrv_attach_aio_context)(BlockDriverState *bs, -AioContext *new_context); -/* io queue for linux-aio */ void (*bdrv_io_plug)(BlockDriverState *bs);void (*bdrv_io_unplug)(BlockDriverState *bs); -/** -* Try to get @bs's logical and physical block size. -* On success, store them in @bsz and return zero. -* On failure, return negative errno. -*/ -int (*bdrv_probe_blocksizes)(BlockDriverState *bs,BlockSizes *bsz); -/** -* Try to get @bs's geometry (cyls, heads, sectors) -* On success, store them in @geo and return 0. -* On failure return -errno. -* Only drivers that want to override guest geometry implement this -* callback; see hd_geometry_guess(). -*/ -int (*bdrv_probe_geometry)(BlockDriverState *bs,HDGeometry *geo); -/** * bdrv_co_drain_begin is called if implemented in the beginning of a * drain operation to drain and stop any internal sources of requests in
@@ -580,69 +691,6 @@ struct BlockDriver { void coroutine_fn (*bdrv_co_drain_begin)(BlockDriverState *bs); void coroutine_fn (*bdrv_co_drain_end)(BlockDriverState *bs); -void (*bdrv_add_child)(BlockDriverState *parent,BlockDriverState *child, -Error **errp); -void (*bdrv_del_child)(BlockDriverState *parent,BdrvChild *child, -Error **errp); - -/** -* Informs the block driver that a permission change is intended. The -* driver checks whether the change is permissible and may take other -* preparations for the change (e.g. get file system locks). This operation -* is always followed either by a call to either .bdrv_set_perm or -* .bdrv_abort_perm_update. -* -* Checks whether the requested set of cumulative permissions in @perm -* can be granted for accessing @bs and whether no other users are using -* permissions other than those given in @shared (both arguments take -* BLK_PERM_* bitmasks). -* -* If both conditions are met, 0 is returned. Otherwise, -errno is returned -* and errp is set to an error describing the conflict. -*/ -int (*bdrv_check_perm)(BlockDriverState *bs,uint64_t perm, -uint64_t shared,Error **errp); - -/** -* Called to inform the driver that the set of cumulative set of used -* permissions for @bs has changed to @perm, and the set of sharable -* permission to @shared. The driver can use this to propagate changes to -* its children (i.e. request permissions only if a parent actually needs -* them). -* -* This function is only invoked after bdrv_check_perm(), so block drivers -* may rely on preparations made in their .bdrv_check_perm implementation. -*/ -void (*bdrv_set_perm)(BlockDriverState *bs,uint64_t perm,uint64_t shared); - -/* -* Called to inform the driver that after a previous bdrv_check_perm() -* call, the permission update is not performed and any preparations made -* for it (e.g. taken file locks) need to be undone. -* -* This function can be called even for nodes that never saw a -* bdrv_check_perm() call. It is a no-op then. -*/ -void (*bdrv_abort_perm_update)(BlockDriverState *bs); - -/** -* Returns in @nperm and @nshared the permissions that the driver for @bs -* needs on its child @c, based on the cumulative permissions requested by -* the parents in @parent_perm and @parent_shared. -* -* If @c is NULL, return the permissions for attaching a new child for the -* given @child_class and @role. -* -* If @reopen_queue is non-NULL, don't return the currently needed -* permissions, but those that will be needed after applying the -* @reopen_queue. -*/ -void (*bdrv_child_perm)(BlockDriverState *bs,BdrvChild *c, -BdrvChildRole role, -BlockReopenQueue *reopen_queue, -uint64_t parent_perm,uint64_t parent_shared, -uint64_t *nperm,uint64_t *nshared); -bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs); bool (*bdrv_co_can_store_new_dirty_bitmap)(BlockDriverState *bs, const char *name,
@@ -651,26 +699,6 @@ struct BlockDriver {int (*bdrv_co_remove_persistent_dirty_bitmap)(BlockDriverState *bs,const char *name,Error **errp); - -/** -* Register/unregister a buffer for I/O. For example, when the driver is -* interested to know the memory areas that will later be used in iovs, so -* that it can do IOMMU mapping with VFIO etc., in order to get better -* performance. In the case of VFIO drivers, this callback is used to do -* DMA mapping for hot buffers. -*/ -void (*bdrv_register_buf)(BlockDriverState *bs,void *host,size_t size); -void (*bdrv_unregister_buf)(BlockDriverState *bs,void *host); -QLIST_ENTRY (BlockDriver) list; - -/* -* Pointer to a NULL-terminated array of names of strong options -* that can be specified for bdrv_open(). A strong option is one -* that changes the data of a BDS. -* If this pointer is NULL, the array is considered empty. -* "filename" and "driver" are always considered strong. -*/ -const char *const *strong_runtime_opts; };static inline bool block_driver_can_compress (BlockDriver *drv)
Signature · 1 line
2.31.1
PATCH v6 12/33 block.c: add assertions to static functions
In reply to Emanuele Giuseppe EspositoFollowing the assertion derived from the API split, propagate the assertion also in the static functions.
--- block.c | 47 ++++++++++++++++++++++++++++++++++++++++++- block/block-backend.c | 3 +++ 2 files changed, 49 insertions(+), 1 deletion(-)
block.c +46 -1
@@ -434,6 +434,7 @@ BlockDriverState *bdrv_new(void)static BlockDriver *bdrv_do_find_format (const char *format_name) {BlockDriver *drv1; +assert (qemu_in_main_thread ()); QLIST_FOREACH(drv1, &bdrv_drivers, list) {if (!strcmp (drv1->format_name, format_name)) {
@@ -593,6 +594,8 @@ static int64_t create_file_fallback_truncate(BlockBackend *blk,int64_t size;int ret; +assert (qemu_in_main_thread ()); + ret =blk_truncate (blk, minimum_size, false,PREALLOC_MODE_OFF ,0 , &local_err);if (ret <0 && ret != -ENOTSUP ) {
@@ -631,6 +634,8 @@ static int create_file_fallback_zero_first_sector(BlockBackend *blk,int64_t bytes_to_clear;int ret; +assert (qemu_in_main_thread ()); + bytes_to_clear =MIN (current_size,BDRV_SECTOR_SIZE );if (bytes_to_clear) { ret =blk_pwrite_zeroes (blk,0 , bytes_to_clear,BDRV_REQ_MAY_UNMAP );
@@ -891,6 +896,7 @@ static BlockDriver *find_hdev_driver(const char *filename) {int score_max =0 , score;BlockDriver *drv =NULL , *d; +assert (qemu_in_main_thread ()); QLIST_FOREACH(d, &bdrv_drivers, list) {if (d->bdrv_probe_device) {
@@ -908,6 +914,7 @@ static BlockDriver *find_hdev_driver(const char *filename)static BlockDriver *bdrv_do_find_protocol (const char *protocol) {BlockDriver *drv1; +assert (qemu_in_main_thread ()); QLIST_FOREACH(drv1, &bdrv_drivers, list) {if (drv1->protocol_name && !strcmp (drv1->protocol_name, protocol)) {
@@ -1015,6 +1022,8 @@ static int find_image_format(BlockBackend *file, const char *filename,uint8_t buf[BLOCK_PROBE_BUF_SIZE ];int ret =0 ; +assert (qemu_in_main_thread ()); +/* Return the raw BlockDriver * to scsi-generic devices or empty drives */ if (blk_is_sg (file) || !blk_is_inserted (file) ||blk_getlength (file) ==0 ) { *pdrv = &bdrv_raw;
@@ -1096,6 +1105,7 @@ static BlockdevDetectZeroesOptions bdrv_parse_detect_zeroes(QemuOpts *opts,BlockdevDetectZeroesOptions detect_zeroes =qapi_enum_parse (&BlockdevDetectZeroesOptions_lookup, value,BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF , &local_err); +assert (qemu_in_main_thread ());g_free (value);if (local_err) {error_propagate (errp, local_err);
@@ -1213,6 +1223,7 @@ static void bdrv_child_cb_drained_end(BdrvChild *child,static int bdrv_child_cb_inactivate (BdrvChild *child) {BlockDriverState *bs = child->opaque; +assert (qemu_in_main_thread ());assert (bs->open_flags &BDRV_O_INACTIVE );return 0 ; }
@@ -1239,6 +1250,7 @@ static void bdrv_child_cb_set_aio_ctx(BdrvChild *child, AioContext *ctx,static void bdrv_temp_snapshot_options (int *child_flags,QDict *child_options,int parent_flags,QDict *parent_options) { +assert (qemu_in_main_thread ()); *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT ) |BDRV_O_TEMPORARY ;/* For temporary files, unconditional cache=unsafe is fine */
@@ -1259,6 +1271,7 @@ static void bdrv_backing_attach(BdrvChild *c)BlockDriverState *parent = c->opaque;BlockDriverState *backing_hd = c->bs; +assert (qemu_in_main_thread ());assert (!parent->backing_blocker); error_setg(&parent->backing_blocker, "node is used as backing hd of '%s'",
@@ -1297,6 +1310,7 @@ static void bdrv_backing_detach(BdrvChild *c) {BlockDriverState *parent = c->opaque; +assert (qemu_in_main_thread ());assert (parent->backing_blocker);bdrv_op_unblock_all (c->bs, parent->backing_blocker);error_free (parent->backing_blocker);
@@ -1309,6 +1323,7 @@ static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base,BlockDriverState *parent = c->opaque;bool read_only =bdrv_is_read_only (parent);int ret; +assert (qemu_in_main_thread ());if (read_only) { ret =bdrv_reopen_set_read_only (parent, false, errp);
@@ -1340,6 +1355,7 @@ static void bdrv_inherited_options(BdrvChildRole role, bool parent_is_format, int parent_flags, QDict *parent_options) { int flags = parent_flags; +assert (qemu_in_main_thread ());/* * First, decide whether to set, clear, or leave BDRV_O_PROTOCOL.
@@ -1479,6 +1495,7 @@ AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c)static int bdrv_open_flags (BlockDriverState *bs,int flags) {int open_flags = flags; +assert (qemu_in_main_thread ());/* * Clear flags that are internal to the block layer before opening the
@@ -1491,6 +1508,8 @@ static int bdrv_open_flags(BlockDriverState *bs, int flags)static void update_flags_from_options (int *flags,QemuOpts *opts) { +assert (qemu_in_main_thread ()); + *flags &= ~(BDRV_O_CACHE_MASK |BDRV_O_RDWR |BDRV_O_AUTO_RDONLY );if (qemu_opt_get_bool_del (opts,BDRV_OPT_CACHE_NO_FLUSH , false)) {
@@ -1512,6 +1531,7 @@ static void update_flags_from_options(int *flags, QemuOpts *opts)static void update_options_from_flags (QDict *options,int flags) { +assert (qemu_in_main_thread ());if (!qdict_haskey (options,BDRV_OPT_CACHE_DIRECT )) {qdict_put_bool (options,BDRV_OPT_CACHE_DIRECT , flags &BDRV_O_NOCACHE ); }
@@ -1533,6 +1553,7 @@ static void bdrv_assign_node_name(BlockDriverState *bs, Error **errp) {char *gen_node_name =NULL ; +assert (qemu_in_main_thread ());if (!node_name) { node_name = gen_node_name =id_generate (ID_BLOCK );
@@ -1779,6 +1800,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,assert (bs->file ==NULL );assert (options !=NULL && bs->options != options); +assert (qemu_in_main_thread ()); opts =qemu_opts_create (&bdrv_runtime_opts,NULL ,0 , &error_abort);if (!qemu_opts_absorb_qdict (opts, options, errp)) {
@@ -1904,6 +1926,7 @@ static QDict *parse_json_filename(const char *filename, Error **errp)QObject *options_obj;QDict *options;int ret; +assert (qemu_in_main_thread ()); ret =strstart (filename,"json:" , &filename);assert (ret);
@@ -1931,6 +1954,7 @@ static void parse_json_protocol(QDict *options, const char **pfilename, {QDict *json_options;Error *local_err =NULL ; +assert (qemu_in_main_thread ());/* Parse json: pseudo-protocol */ if (!*pfilename || !g_str_has_prefix (*pfilename,"json:" )) {
@@ -2184,6 +2208,8 @@ static GSList *bdrv_topological_dfs(GSList *list, GHashTable *found,BdrvChild *child;g_autoptr (GHashTable) local_found =NULL ; +assert (qemu_in_main_thread ()); +if (!found) {assert (!list); found = local_found =g_hash_table_new (NULL ,NULL );
@@ -2295,6 +2321,7 @@ typedef struct BdrvReplaceChildState {static void bdrv_replace_child_commit (void *opaque) {BdrvReplaceChildState *s = opaque; +assert (qemu_in_main_thread ());if (s->free_empty_child && !s->child->bs) {bdrv_child_free (s->child);
@@ -2307,6 +2334,7 @@ static void bdrv_replace_child_abort(void *opaque)BdrvReplaceChildState *s = opaque;BlockDriverState *new_bs = s->child->bs; +assert (qemu_in_main_thread ());/* * old_bs reference is transparently moved from @s to s->child. *
@@ -2869,6 +2897,7 @@ static void bdrv_replace_child_noperm(BdrvChild **childp,static void bdrv_child_free (BdrvChild *child) {assert (!child->bs); +assert (qemu_in_main_thread ());assert (!child->next.le_prev);/* not in children list */ g_free (child->name);
@@ -2949,6 +2978,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs,assert (child);assert (*child ==NULL );assert (child_class->get_parent_desc); +assert (qemu_in_main_thread ()); new_child =g_new (BdrvChild,1 ); *new_child = (BdrvChild ) {
@@ -3029,6 +3059,7 @@ static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,uint64_t perm, shared_perm;assert (parent_bs->drv); +assert (qemu_in_main_thread ());if (bdrv_recurse_has_child (child_bs, parent_bs)) { error_setg(errp, "Making '%s' a %s child of '%s' would create a cycle",
@@ -3054,6 +3085,7 @@ static void bdrv_detach_child(BdrvChild **childp) {BlockDriverState *old_bs = (*childp)->bs; +assert (qemu_in_main_thread ());bdrv_replace_child_noperm (childp,NULL , true);if (old_bs) {
@@ -3302,6 +3334,8 @@ static int bdrv_set_file_or_backing_noperm(BlockDriverState *parent_bs,BdrvChild *child = is_backing ? parent_bs->backing : parent_bs->file;BdrvChildRole role; +assert (qemu_in_main_thread ()); +if (!parent_bs->drv) {/* * Node without drv is an object without a class :/. TODO: finally fix
@@ -3381,6 +3415,7 @@ static int bdrv_set_backing_noperm(BlockDriverState *bs, BlockDriverState *backing_hd, Transaction *tran, Error **errp) { +assert (qemu_in_main_thread ());return bdrv_set_file_or_backing_noperm (bs, backing_hd, true, tran, errp); }
@@ -3656,6 +3691,8 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,BlockDriverState *bs_snapshot =NULL ;int ret; +assert (qemu_in_main_thread ()); +/* if snapshot, we create a temporary backing file and open it instead of opening 'filename' directly */
@@ -4482,6 +4519,8 @@ static int bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,QObject *value;const char *str; +assert (qemu_in_main_thread ()); + value =qdict_get (reopen_state->options, child_name);if (value ==NULL ) {return 0 ;
@@ -5021,7 +5060,7 @@ static void bdrv_remove_filter_or_cow_child_abort(void *opaque)static void bdrv_remove_filter_or_cow_child_commit (void *opaque) {BdrvRemoveFilterOrCowChild *s = opaque; - +assert (qemu_in_main_thread ());bdrv_child_free (s->child); }
@@ -5104,6 +5143,7 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,BdrvChild *c, *next;assert (to !=NULL ); +assert (qemu_in_main_thread ()); QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {assert (c->bs == from);
@@ -5154,6 +5194,7 @@ static int bdrv_replace_node_common(BlockDriverState *from,BlockDriverState *to_cow_parent =NULL ;int ret; +assert (qemu_in_main_thread ());assert (to !=NULL );if (detach_subchain) {
@@ -5312,6 +5353,7 @@ static void bdrv_delete(BlockDriverState *bs) {assert (bdrv_op_blocker_is_empty (bs));assert (!bs->refcnt); +assert (qemu_in_main_thread ());/* remove from list, if necessary */ if (bs->node_name[0 ] !='\0' ) {
@@ -6313,6 +6355,7 @@ void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)static BlockDriverState *bdrv_find_debug_node (BlockDriverState *bs) { +assert (qemu_in_main_thread ());while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { bs =bdrv_primary_bs (bs); }
@@ -6605,6 +6648,7 @@ void bdrv_invalidate_cache_all(Error **errp)static bool bdrv_has_bds_parent (BlockDriverState *bs,bool only_active) {BdrvChild *parent; +assert (qemu_in_main_thread ()); QLIST_FOREACH(parent, &bs->parents, next_parent) {if (parent->klass->parent_is_bds) {
@@ -7122,6 +7166,7 @@ void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co)static void bdrv_do_remove_aio_context_notifier (BdrvAioNotifier *ban) { +assert (qemu_in_main_thread ());QLIST_REMOVE (ban, list);g_free (ban); }
block/block-backend.c +3 -0
@@ -754,6 +754,9 @@ BlockDriverState *blk_bs(BlockBackend *blk)static BlockBackend *bdrv_first_blk (BlockDriverState *bs) {BdrvChild *child; + +assert (qemu_in_main_thread ()); + QLIST_FOREACH(child, &bs->parents, next_parent) {if (child->klass == &child_root) {return child->opaque;
Signature · 1 line
2.31.1
PATCH v6 28/33 job.h: split function pointers in JobDriver
In reply to Emanuele Giuseppe EspositoThe job API will be handled separately in another serie.
--- include/qemu/job.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/qemu/job.h +22 -0
@@ -169,6 +169,12 @@ typedef struct Job {* Callbacks and other information about a Job driver. */ struct JobDriver { + +/* +* These fields are initialized when this object is created, +* and are never changed afterwards +*/ +/** Derived Job struct size */ size_t instance_size;
@@ -184,9 +190,18 @@ struct JobDriver {* aborted. If it returns zero, the job moves into the WAITING state. If it * is the last job to complete in its transaction, all jobs in the * transaction move from WAITING to PENDING. +* +* This callback must be run in the job's context. */ int coroutine_fn (*run)(Job *job, Error **errp); +/* +* Functions run without regard to the BQL that may run in any +* arbitrary thread. These functions do not need to be thread-safe +* because the caller ensures that they are invoked from one +* thread at time. +*/ +/** * If the callback is not NULL, it will be invoked when the job transitions * into the paused state. Paused jobs must not perform any asynchronous
@@ -201,6 +216,13 @@ struct JobDriver {*/ void coroutine_fn (*resume)(Job *job); +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/** * Called when the job is resumed by the user (i.e. user_paused becomes * false). .user_resume is called before .resume.
Signature · 1 line
2.31.1
PATCH v6 16/33 assertions for blockdev.h global state API
In reply to Emanuele Giuseppe Esposito--- block/block-backend.c | 3 +++ blockdev.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+)
block/block-backend.c +3 -0
@@ -797,6 +797,7 @@ bool bdrv_is_root_node(BlockDriverState *bs)*/ DriveInfo *blk_legacy_dinfo (BlockBackend *blk) { +assert (qemu_in_main_thread ());return blk->legacy_dinfo; }
@@ -808,6 +809,7 @@ DriveInfo *blk_legacy_dinfo(BlockBackend *blk)DriveInfo *blk_set_legacy_dinfo (BlockBackend *blk,DriveInfo *dinfo) {assert (!blk->legacy_dinfo); +assert (qemu_in_main_thread ());return blk->legacy_dinfo = dinfo; }
@@ -818,6 +820,7 @@ DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)BlockBackend *blk_by_legacy_dinfo (DriveInfo *dinfo) {BlockBackend *blk =NULL ; +assert (qemu_in_main_thread ());while ((blk =blk_next (blk)) !=NULL ) {if (blk->legacy_dinfo == dinfo) {
blockdev.c +16 -0
@@ -113,6 +113,8 @@ void override_max_devs(BlockInterfaceType type, int max_devs)BlockBackend *blk;DriveInfo *dinfo; +assert (qemu_in_main_thread ()); +if (max_devs <=0 ) {return ; }
@@ -142,6 +144,8 @@ void blockdev_mark_auto_del(BlockBackend *blk)DriveInfo *dinfo =blk_legacy_dinfo (blk);BlockJob *job; +assert (qemu_in_main_thread ()); +if (!dinfo) {return ; }
@@ -163,6 +167,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)void blockdev_auto_del (BlockBackend *blk) {DriveInfo *dinfo =blk_legacy_dinfo (blk); +assert (qemu_in_main_thread ());if (dinfo && dinfo->auto_del) {monitor_remove_blk (blk);
@@ -187,6 +192,8 @@ QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, {QemuOpts *opts; +assert (qemu_in_main_thread ()); + opts =qemu_opts_parse_noisily (qemu_find_opts ("drive" ), optstr, false);if (!opts) {return NULL ;
@@ -207,6 +214,8 @@ DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)BlockBackend *blk;DriveInfo *dinfo; +assert (qemu_in_main_thread ()); +for (blk =blk_next (NULL ); blk; blk =blk_next (blk)) { dinfo =blk_legacy_dinfo (blk); if (dinfo && dinfo->type == type
@@ -229,6 +238,8 @@ void drive_check_orphaned(void)Location loc;bool orphans = false; +assert (qemu_in_main_thread ()); +for (blk =blk_next (NULL ); blk; blk =blk_next (blk)) { dinfo =blk_legacy_dinfo (blk);/*
@@ -262,6 +273,7 @@ void drive_check_orphaned(void)DriveInfo *drive_get_by_index (BlockInterfaceType type,int index) { +assert (qemu_in_main_thread ());return drive_get (type,drive_index_to_bus_id (type, index),drive_index_to_unit_id (type, index));
@@ -273,6 +285,8 @@ int drive_get_max_bus(BlockInterfaceType type)BlockBackend *blk;DriveInfo *dinfo; +assert (qemu_in_main_thread ()); + max_bus =-1 ;for (blk =blk_next (NULL ); blk; blk =blk_next (blk)) { dinfo =blk_legacy_dinfo (blk);
@@ -759,6 +773,8 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,const char *filename;int i; +assert (qemu_in_main_thread ()); +/* Change legacy command line options into QMP ones */ static const struct { const char *from;
Signature · 1 line
2.31.1
PATCH v6 30/33 include/block/block_int-common.h: introduce bdrv_amend_pre_run and bdrv_amend_clean
In reply to Emanuele Giuseppe EspositoThese two callbacks will be invoked by job callbacks to execute driver-specific code while still being in BQL.
In this example, we want the amend JobDriver to execute the permission check (bdrv_child_refresh_perms) currently only done in block/crypto.c block_crypto_amend_options_generic_luks() to all its bdrv.
This is achieved by introducing callbacks in the JobDriver, but we also need to make sure that crypto->updating_keys is true before refreshing the permissions the first time, so that WRITE perm is temporarly given to qcrypto_block_amend_options(), and set it to false when permissions are restored.
Therefore bdrv_amend_pre_run() and bdrv_amend_clean() will take care of just temporarly setting the crypto-specific updating_keys flag.
Note that at this stage, they are not yet invoked.
--- block/crypto.c | 16 ++++++++++++++++ include/block/block_int-common.h | 13 +++++++++++++ 2 files changed, 29 insertions(+)
block/crypto.c +16 -0
@@ -777,6 +777,20 @@ block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp)return spec_info; } +static void +block_crypto_amend_pre_run (BlockDriverState *bs) +{ +BlockCrypto *crypto = bs->opaque; + crypto->updating_keys = true; +} + +static void +block_crypto_amend_cleanup (BlockDriverState *bs) +{ +BlockCrypto *crypto = bs->opaque; + crypto->updating_keys = false; +} + static int block_crypto_amend_options_generic_luks(BlockDriverState *bs, QCryptoBlockAmendOptions *amend_options,
@@ -931,6 +945,8 @@ static BlockDriver bdrv_crypto_luks = { .bdrv_get_specific_info = block_crypto_get_specific_info_luks, .bdrv_amend_options = block_crypto_amend_options_luks, .bdrv_co_amend = block_crypto_co_amend_luks, + .bdrv_amend_pre_run = block_crypto_amend_pre_run, + .bdrv_amend_clean = block_crypto_amend_cleanup, .is_format = true,
include/block/block_int-common.h +13 -0
@@ -189,6 +189,19 @@ struct BlockDriver {* the GS API. */ +/* +* Called inside job->pre_run() callback, it is useful +* to perform driver-specific initialization code under +* BQL, like setting up specific permission flags. +*/ +void (*bdrv_amend_pre_run)(BlockDriverState *bs); +/* +* Called inside job->clean() callback, it undoes +* the driver-specific initialization code done in amend_pre_run. +* Also this function is under BQL. +*/ +void (*bdrv_amend_clean)(BlockDriverState *bs); +/* * Return true if @to_replace can be replaced by a BDS with the * same data as @bs without it affecting @bs's behavior (that is,
Signature · 1 line
2.31.1
PATCH v6 17/33 include/block/snapshot: global state API + assertions
In reply to Emanuele Giuseppe EspositoSnapshots run also under the BQL lock, so they all are in the global state API. The aiocontext lock that they hold is currently an overkill and in future could be removed.
--- block/snapshot.c | 28 ++++++++++++++++++++++++++++ include/block/snapshot.h | 13 +++++++++++-- migration/savevm.c | 2 ++ 3 files changed, 41 insertions(+), 2 deletions(-)
block/snapshot.c +28 -0
@@ -57,6 +57,8 @@ int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,QEMUSnapshotInfo *sn_tab, *sn;int nb_sns, i, ret; +assert (qemu_in_main_thread ()); + ret = -ENOENT ; nb_sns =bdrv_snapshot_list (bs, &sn_tab);if (nb_sns <0 ) {
@@ -105,6 +107,7 @@ bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,bool ret = false;assert (id || name); +assert (qemu_in_main_thread ()); nb_sns =bdrv_snapshot_list (bs, &sn_tab);if (nb_sns <0 ) {
@@ -200,6 +203,7 @@ static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs)int bdrv_can_snapshot (BlockDriverState *bs) {BlockDriver *drv = bs->drv; +assert (qemu_in_main_thread ());if (!drv || !bdrv_is_inserted (bs) ||bdrv_is_read_only (bs)) {return 0 ; }
@@ -220,6 +224,9 @@ int bdrv_snapshot_create(BlockDriverState *bs, {BlockDriver *drv = bs->drv;BlockDriverState *fallback_bs =bdrv_snapshot_fallback (bs); + +assert (qemu_in_main_thread ()); +if (!drv) {return -ENOMEDIUM ; }
@@ -240,6 +247,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,BdrvChild **fallback_ptr;int ret, open_ret; +assert (qemu_in_main_thread ()); +if (!drv) {error_setg (errp,"Block driver is closed" );return -ENOMEDIUM ;
@@ -348,6 +357,8 @@ int bdrv_snapshot_delete(BlockDriverState *bs,BlockDriverState *fallback_bs =bdrv_snapshot_fallback (bs);int ret; +assert (qemu_in_main_thread ()); +if (!drv) {error_setg (errp,QERR_DEVICE_HAS_NO_MEDIUM ,bdrv_get_device_name (bs));return -ENOMEDIUM ;
@@ -380,6 +391,8 @@ int bdrv_snapshot_list(BlockDriverState *bs, {BlockDriver *drv = bs->drv;BlockDriverState *fallback_bs =bdrv_snapshot_fallback (bs); + +assert (qemu_in_main_thread ());if (!drv) {return -ENOMEDIUM ; }
@@ -419,6 +432,8 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs, {BlockDriver *drv = bs->drv; +assert (qemu_in_main_thread ()); +if (!drv) {error_setg (errp,QERR_DEVICE_HAS_NO_MEDIUM ,bdrv_get_device_name (bs));return -ENOMEDIUM ;
@@ -447,6 +462,8 @@ int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,int ret;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); + ret =bdrv_snapshot_load_tmp (bs, id_or_name,NULL , &local_err);if (ret == -ENOENT || ret == -EINVAL ) {error_free (local_err);
@@ -515,6 +532,8 @@ bool bdrv_all_can_snapshot(bool has_devices, strList *devices,g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ()); +if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return false; }
@@ -549,6 +568,8 @@ int bdrv_all_delete_snapshot(const char *name,g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ()); +if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return -1 ; }
@@ -588,6 +609,8 @@ int bdrv_all_goto_snapshot(const char *name,g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ()); +if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return -1 ; }
@@ -622,6 +645,8 @@ int bdrv_all_has_snapshot(const char *name,g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ()); +if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return -1 ; }
@@ -663,6 +688,7 @@ int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, {g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ());if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return -1 ;
@@ -703,6 +729,8 @@ BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,g_autoptr (GList) bdrvs =NULL ;GList *iterbdrvs; +assert (qemu_in_main_thread ()); +if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) {return NULL ; }
include/block/snapshot.h +11 -2
@@ -45,6 +45,13 @@ typedef struct QEMUSnapshotInfo {uint64_t icount;/* record/replay step */ } QEMUSnapshotInfo; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +int bdrv_snapshot_find (BlockDriverState *bs,QEMUSnapshotInfo *sn_info,const char *name); bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
@@ -73,9 +80,11 @@ int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs, Error **errp); -/* Group operations. All block drivers are involved. +/* +* Group operations. All block drivers are involved. * These functions will properly handle dataplane (take aio_context_acquire -* when appropriate for appropriate block drivers */ +* when appropriate for appropriate block drivers +*/ bool bdrv_all_can_snapshot (bool has_devices,strList *devices,Error **errp);
migration/savevm.c +2 -0
@@ -2787,6 +2787,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,g_autoptr (GDateTime) now =g_date_time_new_now_local ();AioContext *aio_context; +assert (qemu_in_main_thread ()); +if (migration_is_blocked (errp)) {return false; }
Signature · 1 line
2.31.1
PATCH v6 31/33 include/qemu/job.h: introduce job->pre_run() and use it in amend
In reply to Emanuele Giuseppe EspositoIntroduce .pre_run() job callback. This cb will run in job_start, before the coroutine is created and runs run() in the job aiocontext.
Therefore, .pre_run() always runs in the main loop.
We can use this function together with clean() cb to replace bdrv_child_refresh_perms in block_crypto_amend_options_generic_luks(), since that function can also be called from an iothread via .bdrv_co_amend().
In addition, doing so we check for permissions in all bdrv in amend, not only crypto.
.pre_run() and .clean() take care of calling bdrv_amend_pre_run() and bdrv_amend_clean() respectively, to set up driver-specific flags and allow the crypto driver to temporarly provide the WRITE perm to qcrypto_block_amend_options().
.pre_run() is not yet invoked by job_start, but .clean() is. This is not a problem, since it will just be a redundant check and crypto will have the update->keys flag == false anyways.
--- block/amend.c | 33 +++++++++++++++++++++++++++++++++ include/qemu/job.h | 9 +++++++++ 2 files changed, 42 insertions(+)
block/amend.c +33 -0
@@ -53,10 +53,43 @@ static int coroutine_fn blockdev_amend_run(Job *job, Error **errp)return ret; } +static int blockdev_amend_refresh_perms (Job *job,Error **errp) +{ +BlockdevAmendJob *s =container_of (job, BlockdevAmendJob, common); +return bdrv_child_refresh_perms (s->bs, s->bs->file, errp); +} + +static int blockdev_amend_pre_run (Job *job,Error **errp) +{ +BlockdevAmendJob *s =container_of (job, BlockdevAmendJob, common); + +if (s->bs->drv->bdrv_amend_pre_run) { + s->bs->drv->bdrv_amend_pre_run (s->bs); + } +return blockdev_amend_refresh_perms (job, errp); +} + +static void blockdev_amend_clean (Job *job) +{ +BlockdevAmendJob *s =container_of (job, BlockdevAmendJob, common); +Error *errp =NULL ; + +if (s->bs->drv->bdrv_amend_clean) { + s->bs->drv->bdrv_amend_clean (s->bs); + } + +blockdev_amend_refresh_perms (job, &errp); +if (errp) { +error_report_err (errp); + } +} +static const JobDriver blockdev_amend_job_driver = { .instance_size =sizeof (BlockdevAmendJob), .job_type =JOB_TYPE_AMEND , .run = blockdev_amend_run, + .pre_run = blockdev_amend_pre_run, + .clean = blockdev_amend_clean, }; void qmp_x_blockdev_amend(const char *job_id,
include/qemu/job.h +9 -0
@@ -223,6 +223,15 @@ struct JobDriver {* the GS API. */ +/** +* Called in the Main Loop context, before the job coroutine +* is started in the job aiocontext. Useful to perform operations +* that needs to be done under BQL (like permissions setup). +* On success, it returns 0. Otherwise the job failed to initialize +* and won't start. +*/ +int (*pre_run)(Job *job,Error **errp); +/** * Called when the job is resumed by the user (i.e. user_paused becomes * false). .user_resume is called before .resume.
Signature · 1 line
2.31.1
PATCH v6 20/33 block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and test_sync_op_invalidate_cache
In reply to Emanuele Giuseppe EspositoFollowing the bdrv_activate renaming, change also the name of the respective callers.
bdrv_invalidate_cache_all -> bdrv_activate_all blk_invalidate_cache -> blk_activate test_sync_op_invalidate_cache -> test_sync_op_activate
No functional change intended.
--- block.c | 2 +- block/block-backend.c | 2 +- hw/block/pflash_cfi01.c | 2 +- hw/nvram/spapr_nvram.c | 2 +- include/block/block-global-state.h | 2 +- include/sysemu/block-backend-io.h | 2 +- migration/block.c | 2 +- migration/migration.c | 10 +++++----- migration/savevm.c | 4 ++-- monitor/qmp-cmds.c | 2 +- tests/unit/test-block-iothread.c | 6 +++--- 11 files changed, 18 insertions(+), 18 deletions(-)
block.c +1 -1
@@ -6629,7 +6629,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return 0 ; } -void bdrv_invalidate_cache_all (Error **errp) +void bdrv_activate_all (Error **errp) {BlockDriverState *bs;BdrvNextIterator it;
block/block-backend.c +1 -1
@@ -1931,7 +1931,7 @@ void blk_set_enable_write_cache(BlockBackend *blk, bool wce) blk->enable_write_cache = wce; } -void blk_invalidate_cache (BlockBackend *blk,Error **errp) +void blk_activate (BlockBackend *blk,Error **errp) {BlockDriverState *bs =blk_bs (blk);
hw/block/pflash_cfi01.c +1 -1
@@ -1023,7 +1023,7 @@ static void postload_update_cb(void *opaque, bool running, RunState state) {PFlashCFI01 *pfl = opaque; -/* This is called after bdrv_invalidate_cache_all. */ +/* This is called after bdrv_activate_all. */ qemu_del_vm_change_state_handler (pfl->vmstate); pfl->vmstate =NULL ;
hw/nvram/spapr_nvram.c +1 -1
@@ -219,7 +219,7 @@ static void postload_update_cb(void *opaque, bool running, RunState state) {SpaprNvram *nvram = opaque; -/* This is called after bdrv_invalidate_cache_all. */ +/* This is called after bdrv_activate_all. */ qemu_del_vm_change_state_handler (nvram->vmstate); nvram->vmstate =NULL ;
include/block/block-global-state.h +1 -1
@@ -144,7 +144,7 @@ BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,/* async block I/O */ void bdrv_aio_cancel (BlockAIOCB *acb);int bdrv_activate (BlockDriverState *bs,Error **errp); -void bdrv_invalidate_cache_all (Error **errp); +void bdrv_activate_all (Error **errp);int bdrv_inactivate_all (void );int bdrv_flush_all (void );
include/sysemu/block-backend-io.h +1 -1
@@ -112,7 +112,7 @@ int blk_get_max_iov(BlockBackend *blk);int blk_get_max_hw_iov (BlockBackend *blk);void blk_set_guest_block_size (BlockBackend *blk,int align); -void blk_invalidate_cache (BlockBackend *blk,Error **errp); +void blk_activate (BlockBackend *blk,Error **errp);void blk_io_plug (BlockBackend *blk);void blk_io_unplug (BlockBackend *blk);
migration/block.c +1 -1
@@ -932,7 +932,7 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)return -EINVAL ; } -blk_invalidate_cache (blk, &local_err); +blk_activate (blk, &local_err);if (local_err) {error_report_err (local_err);return -EINVAL ;
migration/migration.c +5 -5
@@ -499,7 +499,7 @@ static void process_incoming_migration_bh(void *opaque) global_state_get_runstate() == RUN_STATE_RUNNING))) {/* Make sure all file formats flush their mutable metadata. * If we get an error here, just don't restart the VM yet. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); local_err =NULL ;
@@ -586,7 +586,7 @@ static void process_incoming_migration_co(void *opaque)/* we get COLO info, and know if we are in COLO mode */ if (!ret &&migration_incoming_colo_enabled ()) {/* Make sure all file formats flush their mutable metadata */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); goto fail;
@@ -1923,7 +1923,7 @@ static void migrate_fd_cancel(MigrationState *s)if (s->state ==MIGRATION_STATUS_CANCELLING && s->block_inactive) {Error *local_err =NULL ; -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); }else {
@@ -3105,7 +3105,7 @@ fail:*/ Error *local_err =NULL ; -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); }
@@ -3246,7 +3246,7 @@ fail_invalidate:Error *local_err =NULL ;qemu_mutex_lock_iothread (); -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); }else {
migration/savevm.c +2 -2
@@ -1437,7 +1437,7 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,if (inactivate_disks) {/* Inactivate before sending QEMU_VM_EOF so that the -* bdrv_invalidate_cache_all() on the other end won't fail. */ +* bdrv_activate_all() on the other end won't fail. */ ret =bdrv_inactivate_all ();if (ret) { error_report("%s: bdrv_inactivate_all() failed (%d)",
@@ -2007,7 +2007,7 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)/* Make sure all file formats flush their mutable metadata. * If we get an error here, just don't restart the VM yet. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); local_err =NULL ;
monitor/qmp-cmds.c +1 -1
@@ -144,7 +144,7 @@ void qmp_cont(Error **errp)* If there are no inactive block nodes (e.g. because the VM was just * paused rather than completing a migration), bdrv_inactivate_all() simply * doesn't do anything. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_propagate (errp, local_err);return ;
tests/unit/test-block-iothread.c +3 -3
@@ -279,7 +279,7 @@ static void test_sync_op_check(BdrvChild *c) g_assert_cmpint(ret, ==, -ENOTSUP); } -static void test_sync_op_invalidate_cache (BdrvChild *c) +static void test_sync_op_activate (BdrvChild *c) {/* Early success: Image is not inactive */ bdrv_activate (c->bs,NULL );
@@ -325,8 +325,8 @@ const SyncOpTest sync_op_tests[] = { .name = "/sync-op/check", .fn = test_sync_op_check, }, { - .name = "/sync-op/invalidate_cache", - .fn = test_sync_op_invalidate_cache, + .name = "/sync-op/activate", + .fn = test_sync_op_activate, }, };
Signature · 1 line
2.31.1
PATCH v6 32/33 crypto: delegate permission functions to JobDriver .pre_run
In reply to Emanuele Giuseppe Espositoblock_crypto_amend_options_generic_luks uses the block layer permission API, therefore it should be called with the BQL held.
However, the same function is being called by two BlockDriver
callbacks: bdrv_amend_options (under BQL) and bdrv_co_amend (I/O).
The latter is I/O because it is invoked by block/amend.c's blockdev_amend_run(), a .run callback of the amend JobDriver
Therefore we want to 1) change block_crypto driver
to use the permission API only when the BQL is held, and
2) use the .pre_run JobDriver callback to check for permissions before switching to the job aiocontext. This has also the benefit of applying the same permission operation to all amend implementations, not only luks.
Remove the permission check in block_crypto_amend_options_generic_luks() and:
- Add helper functions block_crypto_amend_options_{prepare/cleanup} that take care of checking permissions in block_crypto_amend_options_luks(), so when it is under BQL, and
- Use job->pre_run() and job->clean() to do the same thing when we are in an iothread, by performing these checks before the job runs in its aiocontext. So far job->pre_run() is only defined but not called in job_start(), now it is the moment to use it.
--- block/crypto.c | 57 ++++++++++++++++++++++++++++++++------------------ job.c | 13 ++++++++++++ 2 files changed, 50 insertions(+), 20 deletions(-)
block/crypto.c +37 -20
@@ -791,6 +791,28 @@ block_crypto_amend_cleanup(BlockDriverState *bs) crypto->updating_keys = false; } +static int +block_crypto_amend_options_prepare (BlockDriverState *bs, +Error **errp) +{ +BlockCrypto *crypto = bs->opaque; + +/* apply for exclusive read/write permissions to the underlying file*/ + crypto->updating_keys = true; +return bdrv_child_refresh_perms (bs, bs->file, errp); +} + +static int +block_crypto_amend_options_cleanup (BlockDriverState *bs, +Error **errp) +{ +BlockCrypto *crypto = bs->opaque; + +/* release exclusive read/write permissions to the underlying file*/ + crypto->updating_keys = false; +return bdrv_child_refresh_perms (bs, bs->file, errp); +} + static int block_crypto_amend_options_generic_luks(BlockDriverState *bs, QCryptoBlockAmendOptions *amend_options,
@@ -798,30 +820,17 @@ block_crypto_amend_options_generic_luks(BlockDriverState *bs, Error **errp) {BlockCrypto *crypto = bs->opaque; -int ret;assert (crypto);assert (crypto->block); -/* apply for exclusive read/write permissions to the underlying file*/ - crypto->updating_keys = true; - ret =bdrv_child_refresh_perms (bs, bs->file, errp); -if (ret) { - goto cleanup; - } - - ret =qcrypto_block_amend_options (crypto->block, - block_crypto_read_func, - block_crypto_write_func, - bs, - amend_options, - force, - errp); -cleanup: -/* release exclusive read/write permissions to the underlying file*/ - crypto->updating_keys = false; -bdrv_child_refresh_perms (bs, bs->file, errp); -return ret; +return qcrypto_block_amend_options (crypto->block, + block_crypto_read_func, + block_crypto_write_func, + bs, + amend_options, + force, + errp); } static int
@@ -847,8 +856,16 @@ block_crypto_amend_options_luks(BlockDriverState *bs,if (!amend_options) { goto cleanup; } + + ret =block_crypto_amend_options_prepare (bs, errp); +if (ret) { + goto perm_cleanup; + } ret =block_crypto_amend_options_generic_luks (bs, amend_options, force, errp); + +perm_cleanup: +block_crypto_amend_options_cleanup (bs, errp); cleanup:qapi_free_QCryptoBlockAmendOptions (amend_options);return ret;
job.c +13 -0
@@ -967,11 +967,24 @@ static void coroutine_fn job_co_entry(void *opaque)aio_bh_schedule_oneshot (qemu_get_aio_context (), job_exit, job); } +static int job_pre_run (Job *job) +{ +assert (qemu_in_main_thread ()); +if (job->driver->pre_run) { +return job->driver->pre_run (job, &job->err); + } + +return 0 ; +} +void job_start (Job *job) {assert (job && !job_started (job) && job->paused && job->driver && job->driver->run); job->co =qemu_coroutine_create (job_co_entry, job); +if (job_pre_run (job)) { +return ; + } job->pause_count--; job->busy = true; job->paused = false;
Signature · 1 line
2.31.1
PATCH v6 26/33 block_int-common.h: assertions in the callers of BdrvChildClass function pointers
In reply to Emanuele Giuseppe Esposito--- block.c | 9 +++++++++ 1 file changed, 9 insertions(+)
block.c +9 -0
@@ -1491,6 +1491,7 @@ const BdrvChildClass child_of_bds = {AioContext *bdrv_child_get_parent_aio_context (BdrvChild *c) { +assert (qemu_in_main_thread ());return c->klass->get_parent_aio_context (c); }
@@ -2120,6 +2121,7 @@ bool bdrv_is_writable(BlockDriverState *bs)static char *bdrv_child_user_desc (BdrvChild *c) { +assert (qemu_in_main_thread ());return c->klass->get_parent_desc (c); }
@@ -2829,6 +2831,7 @@ static void bdrv_replace_child_noperm(BdrvChild **childp,assert (!child->frozen);assert (old_bs != new_bs); +assert (qemu_in_main_thread ());if (old_bs && new_bs) {assert (bdrv_get_aio_context (old_bs) ==bdrv_get_aio_context (new_bs));
@@ -2925,6 +2928,7 @@ static void bdrv_attach_child_common_abort(void *opaque)BdrvChild *child = *s->child;BlockDriverState *bs = child->bs; +assert (qemu_in_main_thread ());/* * Pass free_empty_child=false, because we still need the child * for the AioContext operations on the parent below; those
@@ -3293,6 +3297,7 @@ void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)static void bdrv_parent_cb_change_media (BlockDriverState *bs,bool load) {BdrvChild *c; +assert (qemu_in_main_thread ()); QLIST_FOREACH(c, &bs->parents, next_parent) {if (c->klass->change_media) { c->klass->change_media (c, load);
@@ -3789,6 +3794,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,assert (!child_class || !flags);assert (!child_class == !parent); +assert (qemu_in_main_thread ());if (reference) {bool options_non_empty = options ?qdict_size (options) : false;
@@ -4175,6 +4181,7 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,* important to avoid graph changes between the recursive queuing here and * bdrv_reopen_multiple(). */ assert (bs->quiesce_counter >0 ); +assert (qemu_in_main_thread ());if (bs_queue ==NULL ) { bs_queue =g_new0 (BlockReopenQueue,1 );
@@ -7278,6 +7285,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,BdrvChild *child, *parent;g_assert (qemu_get_current_aio_context () ==qemu_get_aio_context ()); +assert (qemu_in_main_thread ());if (old_context == new_context) {return ;
@@ -7350,6 +7358,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,static bool bdrv_parent_can_set_aio_context (BdrvChild *c,AioContext *ctx,GSList **ignore,Error **errp) { +assert (qemu_in_main_thread ());if (g_slist_find (*ignore, c)) {return true; }
Signature · 1 line
2.31.1
PATCH v6 33/33 block.c: assertions to the block layer permissions API
In reply to Emanuele Giuseppe EspositoNow that we "covered" the three main cases where the permission API was being used under BQL (fuse, amend and invalidate_cache), we can safely assert for the permission functions implemented in block.c
--- block.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block.c +12 -0
@@ -2138,6 +2138,7 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)assert (a->bs);assert (a->bs == b->bs); +assert (qemu_in_main_thread ());if ((b->perm & a->shared_perm) == b->perm) {return true;
@@ -2161,6 +2162,7 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)static bool bdrv_parent_perms_conflict (BlockDriverState *bs,Error **errp) {BdrvChild *a, *b; +assert (qemu_in_main_thread ());/* * During the loop we'll look at each pair twice. That's correct because
@@ -2245,6 +2247,8 @@ static void bdrv_child_set_perm_abort(void *opaque) {BdrvChildSetPermState *s = opaque; +assert (qemu_in_main_thread ()); + s->child->perm = s->old_perm; s->child->shared_perm = s->old_shared_perm; }
@@ -2258,6 +2262,7 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, Transaction *tran) { BdrvChildSetPermState *s = g_new(BdrvChildSetPermState, 1); +assert (qemu_in_main_thread ()); *s = (BdrvChildSetPermState) { .child = c,
@@ -2442,6 +2447,7 @@ static int bdrv_node_refresh_perm(BlockDriverState *bs, BlockReopenQueue *q,BdrvChild *c;int ret;uint64_t cumulative_perms, cumulative_shared_perms; +assert (qemu_in_main_thread ());bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms);
@@ -2510,6 +2516,7 @@ static int bdrv_list_refresh_perms(GSList *list, BlockReopenQueue *q, {int ret;BlockDriverState *bs; +assert (qemu_in_main_thread ());for ( ; list; list = list->next) { bs = list->data;
@@ -2581,6 +2588,7 @@ static int bdrv_refresh_perms(BlockDriverState *bs, Error **errp)int ret;Transaction *tran =tran_new ();g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); +assert (qemu_in_main_thread ()); ret =bdrv_list_refresh_perms (list,NULL , tran, errp);tran_finalize (tran, ret);
@@ -2647,6 +2655,7 @@ static void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { +assert (qemu_in_main_thread ()); *nperm = perm &DEFAULT_PERM_PASSTHROUGH ; *nshared = (shared &DEFAULT_PERM_PASSTHROUGH ) |DEFAULT_PERM_UNCHANGED ; }
@@ -2658,6 +2667,7 @@ static void bdrv_default_perms_for_cow(BlockDriverState *bs, BdrvChild *c, uint64_t *nperm, uint64_t *nshared) { assert(role & BDRV_CHILD_COW); +assert (qemu_in_main_thread ());/* * We want consistent read from backing files if the parent needs it.
@@ -2694,6 +2704,7 @@ static void bdrv_default_perms_for_storage(BlockDriverState *bs, BdrvChild *c, {int flags; +assert (qemu_in_main_thread ());assert (role & (BDRV_CHILD_METADATA |BDRV_CHILD_DATA )); flags =bdrv_reopen_get_flags (reopen_queue, bs);
@@ -6090,6 +6101,7 @@ static void xdbg_graph_add_edge(XDbgBlockGraphConstructor *gr, void *parent, {BlockPermission qapi_perm;XDbgBlockGraphEdge *edge; +assert (qemu_in_main_thread ()); edge =g_new0 (XDbgBlockGraphEdge,1 );
Signature · 1 line
2.31.1
PATCH v6 24/33 block_int-common.h: assertions in the callers of BlockDriver function pointers
In reply to Emanuele Giuseppe Esposito--- block.c | 18 ++++++++++++++++++ block/create.c | 2 ++ 2 files changed, 20 insertions(+)
block.c +18 -0
@@ -526,6 +526,7 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque)CreateCo *cco = opaque;assert (cco->drv); +assert (qemu_in_main_thread ()); ret = cco->drv->bdrv_co_create_opts (cco->drv, cco->filename, cco->opts, &local_err);
@@ -1089,6 +1090,7 @@ int refresh_total_sectors(BlockDriverState *bs, int64_t hint)static void bdrv_join_options (BlockDriverState *bs,QDict *options,QDict *old_options) { +assert (qemu_in_main_thread ());if (bs->drv && bs->drv->bdrv_join_options) { bs->drv->bdrv_join_options (options, old_options); }else {
@@ -1598,6 +1600,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, {Error *local_err =NULL ;int i, ret; +assert (qemu_in_main_thread ());bdrv_assign_node_name (bs, node_name, &local_err);if (local_err) {
@@ -1989,6 +1992,8 @@ static int bdrv_fill_options(QDict **options, const char *filename,BlockDriver *drv =NULL ;Error *local_err =NULL ; +assert (qemu_in_main_thread ()); +/* * Caution: while qdict_get_try_str() is fine, getting non-string * types would require more care. When @options come from
@@ -2182,6 +2187,7 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, uint64_t *nperm, uint64_t *nshared) { assert(bs->drv && bs->drv->bdrv_child_perm); +assert (qemu_in_main_thread ()); bs->drv->bdrv_child_perm (bs, c, role, reopen_queue, parent_perm, parent_shared, nperm, nshared);
@@ -2267,6 +2273,7 @@ static void bdrv_drv_set_perm_commit(void *opaque) {BlockDriverState *bs = opaque;uint64_t cumulative_perms, cumulative_shared_perms; +assert (qemu_in_main_thread ());if (bs->drv->bdrv_set_perm) { bdrv_get_cumulative_perm(bs, &cumulative_perms,
@@ -2278,6 +2285,7 @@ static void bdrv_drv_set_perm_commit(void *opaque)static void bdrv_drv_set_perm_abort (void *opaque) {BlockDriverState *bs = opaque; +assert (qemu_in_main_thread ());if (bs->drv->bdrv_abort_perm_update) { bs->drv->bdrv_abort_perm_update (bs);
@@ -2293,6 +2301,7 @@ static int bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared_perm, Transaction *tran, Error **errp) { +assert (qemu_in_main_thread ());if (!bs->drv) {return 0 ; }
@@ -4354,6 +4363,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)assert (qemu_get_current_aio_context () ==qemu_get_aio_context ());assert (bs_queue !=NULL ); +assert (qemu_in_main_thread ()); QTAILQ_FOREACH(bs_entry, bs_queue, entry) { ctx =bdrv_get_aio_context (bs_entry->state.bs);
@@ -4619,6 +4629,7 @@ static int bdrv_reopen_prepare(BDRVReopenState *reopen_state,assert (reopen_state !=NULL );assert (reopen_state->bs->drv !=NULL ); +assert (qemu_in_main_thread ()); drv = reopen_state->bs->drv;/* This function and each driver's bdrv_reopen_prepare() remove
@@ -4829,6 +4840,7 @@ static void bdrv_reopen_commit(BDRVReopenState *reopen_state) bs = reopen_state->bs; drv = bs->drv;assert (drv !=NULL ); +assert (qemu_in_main_thread ());/* If there are any driver level actions to take */ if (drv->bdrv_reopen_commit) {
@@ -4870,6 +4882,7 @@ static void bdrv_reopen_abort(BDRVReopenState *reopen_state)assert (reopen_state !=NULL ); drv = reopen_state->bs->drv;assert (drv !=NULL ); +assert (qemu_in_main_thread ());if (drv->bdrv_reopen_abort) { drv->bdrv_reopen_abort (reopen_state);
@@ -4883,6 +4896,7 @@ static void bdrv_close(BlockDriverState *bs)BdrvChild *child, *next;assert (!bs->refcnt); +assert (qemu_in_main_thread ());bdrv_drained_begin (bs);/* complete I/O */ bdrv_flush (bs);
@@ -6679,6 +6693,8 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs)int ret;uint64_t cumulative_perms, cumulative_shared_perms; +assert (qemu_in_main_thread ()); +if (!bs->drv) {return -ENOMEDIUM ; }
@@ -7187,6 +7203,7 @@ static void bdrv_detach_aio_context(BlockDriverState *bs)BdrvAioNotifier *baf, *baf_tmp;assert (!bs->walking_aio_notifiers); +assert (qemu_in_main_thread ()); bs->walking_aio_notifiers = true; QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {if (baf->deleted) {
@@ -7214,6 +7231,7 @@ static void bdrv_attach_aio_context(BlockDriverState *bs, AioContext *new_context) {BdrvAioNotifier *ban, *ban_tmp; +assert (qemu_in_main_thread ());if (bs->quiesce_counter) {aio_disable_external (new_context);
block/create.c +2 -0
@@ -42,6 +42,8 @@ static int coroutine_fn blockdev_create_run(Job *job, Error **errp)BlockdevCreateJob *s =container_of (job, BlockdevCreateJob, common);int ret; +assert (qemu_in_main_thread ()); +job_progress_set_remaining (&s->common,1 ); ret = s->drv->bdrv_co_create (s->opts, errp);job_progress_update (&s->common,1 );
Signature · 1 line
2.31.1
PATCH v6 29/33 job.h: assertions in the callers of JobDriver funcion pointers
In reply to Emanuele Giuseppe Esposito--- job.c | 9 +++++++++ 1 file changed, 9 insertions(+)
job.c +9 -0
@@ -381,6 +381,8 @@ void job_ref(Job *job)void job_unref (Job *job) { +assert (qemu_in_main_thread ()); +if (--job->refcnt ==0 ) {assert (job->status ==JOB_STATUS_NULL );assert (!timer_pending (&job->sleep_timer));
@@ -602,6 +604,7 @@ bool job_user_paused(Job *job)void job_user_resume (Job *job,Error **errp) {assert (job); +assert (qemu_in_main_thread ());if (!job->user_paused || job->pause_count <=0 ) {error_setg (errp,"Can't resume a job that was not paused" );return ;
@@ -672,6 +675,7 @@ static void job_update_rc(Job *job)static void job_commit (Job *job) {assert (!job->ret); +assert (qemu_in_main_thread ());if (job->driver->commit) { job->driver->commit (job); }
@@ -680,6 +684,7 @@ static void job_commit(Job *job)static void job_abort (Job *job) {assert (job->ret); +assert (qemu_in_main_thread ());if (job->driver->abort) { job->driver->abort (job); }
@@ -687,6 +692,7 @@ static void job_abort(Job *job)static void job_clean (Job *job) { +assert (qemu_in_main_thread ());if (job->driver->clean) { job->driver->clean (job); }
@@ -726,6 +732,7 @@ static int job_finalize_single(Job *job)static void job_cancel_async (Job *job,bool force) { +assert (qemu_in_main_thread ());if (job->driver->cancel) { force = job->driver->cancel (job, force); }else {
@@ -825,6 +832,7 @@ static void job_completed_txn_abort(Job *job)static int job_prepare (Job *job) { +assert (qemu_in_main_thread ());if (job->ret ==0 && job->driver->prepare) { job->ret = job->driver->prepare (job);job_update_rc (job);
@@ -1054,6 +1062,7 @@ void job_complete(Job *job, Error **errp) {/* Should not be reachable via external interface for internal jobs */ assert (job->id); +assert (qemu_in_main_thread ());if (job_apply_verb (job,JOB_VERB_COMPLETE , errp)) {return ; }
Signature · 1 line
2.31.1
Re: [PATCH v6 20/33] block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and test_sync_op_invalidate_cache
In reply to Emanuele Giuseppe EspositoQuoted 8 lines · Emanuele Giuseppe Esposito
Following the
bdrv_activaterenaming, change also the name of the respective callers.
bdrv_invalidate_cache_all->bdrv_activate_allblk_invalidate_cache->blk_activatetest_sync_op_invalidate_cache->test_sync_op_activateNo functional change intended.
In the sense that makes sense for this series.
But if you have to respin.
migration/migration.c static void process_incoming_migration_bh(void *opaque) · line 502
Quoted 10 lines
migration/migration.c +1
-1@@ -499,7 +499,7 @@ static void process_incoming_migration_bh(void *opaque) global_state_get_runstate() == RUN_STATE_RUNNING))) {/* Make sure all file formats flush their mutable metadata. * If we get an error here, just don't restart the VM yet. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);
I guess that we can change the comment here, it just looks weird the comment saying flush() and the function nawed _activate()
migration/migration.c static void process_incoming_migration_co(void *opaque) · line 592
Quoted 9 lines
migration/migration.c +1
-1@@ -586,7 +586,7 @@ static void process_incoming_migration_co(void *opaque)/* we get COLO info, and know if we are in COLO mode */ if (!ret &&migration_incoming_colo_enabled ()) {/* Make sure all file formats flush their mutable metadata */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); goto fail;
same here.
migration/savevm.c int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, · line 1443
Quoted 13 lines
migration/savevm.c +1
-1@@ -1437,7 +1437,7 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,if (inactivate_disks) {/* Inactivate before sending QEMU_VM_EOF so that the -* bdrv_invalidate_cache_all() on the other end won't fail. */ +* bdrv_activate_all() on the other end won't fail. */ ret =bdrv_inactivate_all ();if (ret) { error_report("%s: bdrv_inactivate_all() failed (%d)",
Here the comment makes sense.
migration/savevm.c static void loadvm_postcopy_handle_run_bh(void *opaque) · line 2013
Quoted 9 lines
migration/savevm.c +1
-1@@ -2007,7 +2007,7 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)/* Make sure all file formats flush their mutable metadata. * If we get an error here, just don't restart the VM yet. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_report_err (local_err); local_err =NULL ;
Comment here is bad.
monitor/qmp-cmds.c void qmp_cont(Error **errp) · line 150
Quoted 13 lines
monitor/qmp-cmds.c +1
-1@@ -144,7 +144,7 @@ void qmp_cont(Error **errp)* If there are no inactive block nodes (e.g. because the VM was just * paused rather than completing a migration), bdrv_inactivate_all() simply * doesn't do anything. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);if (local_err) {error_propagate (errp, local_err);return ;
I don't understand the comment here. And yes, I know, this was here before your change.
Thanks, Juan.
Re: [PATCH v6 32/33] crypto: delegate permission functions to JobDriver .pre_run
In reply to Emanuele Giuseppe EspositoQuoted 20 lines · Emanuele Giuseppe Esposito
job.c +10
-0@@ -967,11 +967,24 @@ static void coroutine_fn job_co_entry(void *opaque)aio_bh_schedule_oneshot (qemu_get_aio_context (), job_exit, job); } +static int job_pre_run (Job *job) +{ +assert (qemu_in_main_thread ()); +if (job->driver->pre_run) { +return job->driver->pre_run (job, &job->err); + } + +return 0 ; +} +void job_start (Job *job) {
job.c static void coroutine_fn job_co_entry(void *opaque)
Quoted 10 lines
job.c +3
-1assert (job && !job_started (job) && job->paused && job->driver && job->driver->run); job->co =qemu_coroutine_create (job_co_entry, job); +if (job_pre_run (job)) { +return ; + } job->pause_count--; job->busy = true; job->paused = false; -- 2.31.1
This should have been in patch 31. Might not need a respin, unless someone wants to have these three patches reorganized(*), still worth pointing out.
Paolo
(*) for example I would have done:
- first job->pre_run()
- then blockdev_amend_pre_run/blockdev_amend_clean and block_crypto_amend_pre_run/clean together
Re: [PATCH v6 04/33] block/export/fuse.c: allow writable exports to take RESIZE permission
In reply to Emanuele Giuseppe EspositoQuoted 5 lines · Emanuele Giuseppe Esposito
Allow writable exports to get
BLK_PERM_RESIZEpermission from creation, infuse_export_create().
In this way, there is no need to give the permission infuse_do_truncate(), which might be run in an iothread.
Quoted 7 lines
Permissions should be set only in the main thread, so in any case if an iothread tries to set RESIZE, it will be blocked.
Also assert in
fuse_do_truncatethat if we give the RESIZE permission we can then restore the original ones, since we don't check the return value ofblk_set_perm.
We do, because we pass &error_abort for errp, so if an error were to occur, qemu would abort.
Not that I mind adding an assertion on the return value, just noting that we omitted that kind of intentionally.
Quoted 61 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/export/fuse.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)block/export/fuse.c +18
-7@@ -86,8 +86,8 @@ static int fuse_export_create(BlockExport *blk_exp,assert (blk_exp_args->type ==BLOCK_EXPORT_TYPE_FUSE ); -/* For growable exports, take the RESIZE permission */ -if (args->growable) { +/* For growable and writable exports, take the RESIZE permission */ +if (args->growable || blk_exp_args->writable) {uint64_t blk_perm, blk_shared_perm;blk_get_perm (exp->common.blk, &blk_perm, &blk_shared_perm);@@ -392,14 +392,23 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, {uint64_t blk_perm, blk_shared_perm;BdrvRequestFlags truncate_flags =0 ; -int ret; +bool add_resize_perm; +int ret, ret_check; + +/* Growable and writable exports have a permanent RESIZE permission */ + add_resize_perm = !exp->growable && !exp->writable;if (req_zero_write) { truncate_flags |=BDRV_REQ_ZERO_WRITE ; } -/* Growable exports have a permanent RESIZE permission */ -if (!exp->growable) { +if (add_resize_perm) { + +if (!qemu_in_main_thread ()) { +/* Changing permissions like below only works in the main thread */ +return -EPERM ; + } +blk_get_perm (exp->common.blk, &blk_perm, &blk_shared_perm); ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,@@ -412,9 +421,11 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, ret =blk_truncate (exp->common.blk, size, true, prealloc, truncate_flags,NULL ); -if (!exp->growable) { +if (add_resize_perm) {/* Must succeed, because we are only giving up the RESIZE permission */ -blk_set_perm (exp->common.blk, blk_perm, blk_shared_perm, &error_abort); + ret_check =blk_set_perm (exp->common.blk, blk_perm, + blk_shared_perm, &error_abort); +assert (ret_check ==0 ); }return ret;
Re: [PATCH v6 06/33] block/block-backend.c: assertions for block-backend
In reply to Emanuele Giuseppe EspositoQuoted 15 lines · Emanuele Giuseppe Esposito
All the global state (GS) API functions will check that
qemu_in_main_thread()returns true. If not, it means that the safety of BQL cannot be guaranteed, and they need to be moved to I/O.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/block-backend.c | 79 ++++++++++++++++++++++++++++++++++++++++++ softmmu/qdev-monitor.c | 2 ++ 2 files changed, 81 insertions(+)block/block-backend.c +0
-0
[...]
block/block-backend.c void blk_lock_medium(BlockBackend *blk, bool locked) · line 1964
Quoted 8 lines
block/block-backend.c +1
-0@@ -1908,6 +1958,7 @@ void blk_lock_medium(BlockBackend *blk, bool locked)void blk_eject (BlockBackend *blk,bool eject_flag) {BlockDriverState *bs =blk_bs (blk); +char *id;if (bs) {
Left over hunk from when when there was an assertion added here, should be dropped, I believe.
Hanna
Re: [PATCH v6 19/33] block: introduce bdrv_activate
In reply to Emanuele Giuseppe EspositoQuoted 8 lines · Emanuele Giuseppe Esposito
This function is currently just a wrapper for
bdrv_invalidate_cache(), but in future will contain the code ofbdrv_co_invalidate_cache()that has to always be protected by BQL, and leave the rest in the I/O coroutine.Replace all
bdrv_invalidate_cache()invokations withbdrv_activate().Signed-off-by Emanuele Giuseppe Esposito <[email protected]>
Quoted 8 lines
--- block.c | 7 ++++++- block/block-backend.c | 2 +- block/export/export.c | 2 +- block/parallels.c | 2 +- include/block/block-global-state.h | 2 +- tests/unit/test-block-iothread.c | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-)
Re: [PATCH v6 20/33] block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and test_sync_op_invalidate_cache
In reply to Emanuele Giuseppe EspositoQuoted 10 lines · Emanuele Giuseppe Esposito
Following the
bdrv_activaterenaming, change also the name of the respective callers.
bdrv_invalidate_cache_all->bdrv_activate_allblk_invalidate_cache->blk_activatetest_sync_op_invalidate_cache->test_sync_op_activateNo functional change intended.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>
Quoted 13 lines
--- block.c | 2 +- block/block-backend.c | 2 +- hw/block/pflash_cfi01.c | 2 +- hw/nvram/spapr_nvram.c | 2 +- include/block/block-global-state.h | 2 +- include/sysemu/block-backend-io.h | 2 +- migration/block.c | 2 +- migration/migration.c | 10 +++++----- migration/savevm.c | 4 ++-- monitor/qmp-cmds.c | 2 +- tests/unit/test-block-iothread.c | 6 +++--- 11 files changed, 18 insertions(+), 18 deletions(-)
Re: [PATCH v6 21/33] block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate
In reply to Emanuele Giuseppe EspositoQuoted 17 lines · Emanuele Giuseppe Esposito
Split
bdrv_co_invalidatecache in two: the GS code that takes care of permissions and running GS callbacks, and leave only the I/O code (->bdrv_co_invalidate_cache) running in the I/O coroutine.The only side effect is that
bdrv_co_invalidate_cacheis not recursive anymore, and so is every direct call tobdrv_invalidate_cache().Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)block.c +0
-0
[...]
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6579
Quoted 6 lines
block.c +1
-1@@ -6579,7 +6576,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)* Note that the required permissions of inactive images are always a * subset of the permissions required after activating the image. This * allows us to just get the permissions upfront without restricting -* drv->bdrv_invalidate_cache(). +* drv->bdrv_co_invalidate_cache().
Perhaps just bdrv_invalidate_cache(), without the drv->?
Quoted 6 lines
block.c +0
-0* * It also means that in error cases, we don't have to try and revert to * the old permissions (which is an operation that could fail, too). We can @@ -6594,14 +6591,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return ret; }
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6594
Quoted 10 lines
block.c +1
-8-if (bs->drv->bdrv_co_invalidate_cache) { - bs->drv->bdrv_co_invalidate_cache (bs, &local_err); -if (local_err) { - bs->open_flags |=BDRV_O_INACTIVE ; -error_propagate (errp, local_err); -return -EINVAL ; - } - } +bdrv_invalidate_cache (bs, errp);
We should check the returned value here, and return on error.
Other than that, looks good and makes sense.
Hanna
Quoted 2 lines
block.c +0
-0FOR_EACH_DIRTY_BITMAP(bs, bm) {bdrv_dirty_bitmap_skip_store (bm, false);
Re: [PATCH v6 23/33] block_int-common.h: split function pointers in BlockDriver
In reply to Emanuele Giuseppe EspositoQuoted 12 lines · Emanuele Giuseppe Esposito
Similar to the header split, also the function pointers in BlockDriver can be split in I/O and global state.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- include/block/block_int-common.h | 434 ++++++++++++++++--------------- 1 file changed, 231 insertions(+), 203 deletions(-)include/block/block_int-common.h +0
-0
[...]
Quoted 1 line
include/block/block_int-common.h +0
-0@@ -159,7 +148,66 @@ struct BlockDriver {
[...]
Quoted 6 lines
include/block/block_int-common.h +6
-0+/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/
include/block/block_int-common.h struct BlockDriver { · line 200
Quoted 10 lines
include/block/block_int-common.h +10
-0+ +/* +* Return true if @to_replace can be replaced by a BDS with the +* same data as @bs without it affecting @bs's behavior (that is, +* without it being visible to @bs's parents). +*/ +bool (*bdrv_recurse_can_replace)(BlockDriverState *bs, +BlockDriverState *to_replace); + +int (*bdrv_probe)(const uint8_t *buf,int buf_size,const char *filename);
Considering that its only caller (bdrv_probe_all()) is now an I/O function, shouldn’t this be, too?
Hanna
Re: [PATCH v6 25/33] block_int-common.h: split function pointers in BdrvChildClass
In reply to Emanuele Giuseppe EspositoQuoted 18 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- include/block/block_int-common.h | 67 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 27 deletions(-)include/block/block_int-common.h +5
-0@@ -815,12 +815,16 @@ struct BdrvChildClass {*/ bool parent_is_bds; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API.
include/block/block_int-common.h struct BdrvChildClass { · line 830
Quoted 10 lines
include/block/block_int-common.h +1
-2+*/ void (*inherit_options)(BdrvChildRole role,bool parent_is_format,int *child_flags,QDict *child_options,int parent_flags,QDict *parent_options); -void (*change_media)(BdrvChild *child,bool load); -void (*resize)(BdrvChild *child);/* * Returns a name that is supposedly more useful for human users than the
The method this comment belongs to is .get_name(). It’s exposed through bdrv_get_parent_name(), which is called by bdrv_get_device_name() and bdrv_get_device_or_node_name() – so I think it should be classified as I/O.
include/block/block_int-common.h struct BdrvChildClass { · line 842
Quoted 3 lines
include/block/block_int-common.h +0
-0@@ -837,6 +841,40 @@ struct BdrvChildClass {*/ char *(*get_parent_desc)(BdrvChild *child);
This function is very similar, so we might also want to reconsider classifying it as I/O. There’s no need, because all of its callers do run in the main thread, but at the same time I don’t believe there’s anything stopping us (and it starts to sound to me like all functions of the “get name” kind perhaps should ideally be I/O, in that they shouldn’t require the GS context).
Up to you. O:)
(Rest of this patch looks good!)
Hanna
Re: [PATCH v6 26/33] block_int-common.h: assertions in the callers of BdrvChildClass function pointers
In reply to Emanuele Giuseppe EspositoQuoted 9 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block.c | 9 +++++++++ 1 file changed, 9 insertions(+)block.c +0
-0
[...]
block.c bool bdrv_is_writable(BlockDriverState *bs) · line 2126
Quoted 7 lines
block.c +1
-0@@ -2120,6 +2121,7 @@ bool bdrv_is_writable(BlockDriverState *bs)static char *bdrv_child_user_desc (BdrvChild *c) { +assert (qemu_in_main_thread ());return c->klass->get_parent_desc (c); }
Quick note: Whether we really want this depends on whether we find that .get_parent_desc() really should be a GS-only function.
Since I leave that up to you, though (and this patch interestingly (and correctly) doesn’t add an assertion to bdrv_get_parent_name(), even though that calls .get_name(), which the previous patch did classify as GS):
Re: [PATCH v6 29/33] job.h: assertions in the callers of JobDriver funcion pointers
In reply to Emanuele Giuseppe EspositoQuoted 4 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- job.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Just curious, why did you remove the assertion in job_co_entry()? (Looking at it again, it might have been nicer to swap it with the assertion below it, so that job != NULL is asserted first, but other than that...)
(And since I’m already replying to this patch, might as well point out s/funcion/function/ in the subject)
Hanna
Re: [PATCH v6 30/33] include/block/block_int-common.h: introduce bdrv_amend_pre_run and bdrv_amend_clean
In reply to Emanuele Giuseppe EspositoQuoted 28 lines · Emanuele Giuseppe Esposito
These two callbacks will be invoked by job callbacks to execute driver-specific code while still being in BQL.
In this example, we want the amend JobDriver to execute the permission check (bdrv_child_refresh_perms) currently only done inblock/crypto.cblock_crypto_amend_options_generic_luks()to all its bdrv.
This is achieved by introducing callbacks in the JobDriver, but we also need to make sure thatcrypto->updating_keysis true before refreshing the permissions the first time, so that WRITE perm is temporarly given toqcrypto_block_amend_options(), and set it to false when permissions are restored.Therefore
bdrv_amend_pre_run()andbdrv_amend_clean()will take care of just temporarly setting the crypto-specificupdating_keysflag.Note that at this stage, they are not yet invoked.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/crypto.c | 16 ++++++++++++++++ include/block/block_int-common.h | 13 +++++++++++++ 2 files changed, 29 insertions(+)block/crypto.c +0
-0
block/crypto.c block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp) · line 785
Quoted 9 lines
block/crypto.c +6
-0@@ -777,6 +777,20 @@ block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp)return spec_info; } +static void +block_crypto_amend_pre_run (BlockDriverState *bs) +{ +BlockCrypto *crypto = bs->opaque; + crypto->updating_keys = true; +}
So I see you decided to leave actually updating the permissions to the caller, i.e. blockdev_amend_pre_run(). Why?
I’m asking because:
(1) If the .bdrv_amend_pre_run() isn’t implemented, I don’t think refreshing the permissions in blockdev_amend_pre_run() is necessary. So if it is implemented, the implementation might as well do it itself.
(2) The way you implemented it, it’s actually kind of hard to see why this is even necessary. Both of these functions (block_crypto_amend_{pre_run,cleanup}()) don’t require the BQL, so the explanation for .bdrv_amend_pre_run() (“useful to perform driver-specific initialization code under BQL”) doesn’t really apply. If you want to explain (and we should) why this is necessary, then the .bdrv_amend_pre_run() documentation needs to state that we will refresh the permissions after this function has run and before .bdrv_co_amend() will run, and so it’s also useful to put code here that will change the permissions on permission update, but that just really gets complicated to explain. Naively, I find it simpler to just say “Put BQL code here, this will run before .bdrv_co_amend()” and have every implementation that needs it refresh the permissions itself.
(3) In patch 32, you add block_crypto_amend_options_{prepare,cleanup}(). If the functions added here (block_crypto_amend_{pre_run,cleanup}()) would refresh the permissions by themselves, they’d be exactly the same functions, so you could reuse the ones from here in patch 32.
Quoted 25 lines
block/crypto.c +10
-0+ +static void +block_crypto_amend_cleanup (BlockDriverState *bs) +{ +BlockCrypto *crypto = bs->opaque; + crypto->updating_keys = false; +} + static int block_crypto_amend_options_generic_luks(BlockDriverState *bs, QCryptoBlockAmendOptions *amend_options,@@ -931,6 +945,8 @@ static BlockDriver bdrv_crypto_luks = { .bdrv_get_specific_info = block_crypto_get_specific_info_luks, .bdrv_amend_options = block_crypto_amend_options_luks, .bdrv_co_amend = block_crypto_co_amend_luks, + .bdrv_amend_pre_run = block_crypto_amend_pre_run, + .bdrv_amend_clean = block_crypto_amend_cleanup, .is_format = true,include/block/block_int-common.h +0
-0
include/block/block_int-common.h struct BlockDriver { · line 195
Quoted 7 lines
include/block/block_int-common.h +4
-0@@ -189,6 +189,19 @@ struct BlockDriver {* the GS API. */ +/* +* Called inside job->pre_run() callback, it is useful +* to perform driver-specific initialization code under +* BQL, like setting up specific permission flags.
I wouldn’t necessarily state that this function is called by job->pre_run(), but rather paint the picture of how it’s used together with .bdrv_co_amend(), e.g. something like:
“This function is invoked under the BQL before .bdrv_co_amend() (which in contrast does not necessarily run under the BQL) to allow driver-specific initialization code that requires the BQL, like setting up specific permission flags.”
(Though this comment should be much more specific if we keep updating the permissions in the caller, because then the comment also needs to explain that we do that.)
include/block/block_int-common.h struct BlockDriver { · line 201
Quoted 6 lines
include/block/block_int-common.h +6
-0+*/ +void (*bdrv_amend_pre_run)(BlockDriverState *bs); +/* +* Called inside job->clean() callback, it undoes +* the driver-specific initialization code done in amend_pre_run. +* Also this function is under BQL.
Here too I’d omit the job reference and just say that (e.g.)
“This function is invoked under the BQL after .bdrv_co_amend() to allow cleaning up what was done in .bdrv_amend_pre_run().”
Hanna
Quoted 6 lines
include/block/block_int-common.h +3
-0+*/ +void (*bdrv_amend_clean)(BlockDriverState *bs); +/* * Return true if @to_replace can be replaced by a BDS with the * same data as @bs without it affecting @bs's behavior (that is,
Re: [PATCH v6 31/33] include/qemu/job.h: introduce job->pre_run() and use it in amend
In reply to Emanuele Giuseppe EspositoQuoted 2 lines · Emanuele Giuseppe Esposito
Introduce .pre_run() job callback. This cb will run in
job_start, before the coroutine is created and runsrun()in the job aiocontext.
I presume this means “before the coroutine is created that will run run() in the job aiocontext”?
(The way it’s written here sounds a bit like .pre_run() will run run())
Quoted 8 lines
Therefore, .pre_run() always runs in the main loop.
We can use this function together withclean()cb to replacebdrv_child_refresh_permsinblock_crypto_amend_options_generic_luks(), since that function can also be called from an iothread via .bdrv_co_amend().In addition, doing so we check for permissions in all bdrv in amend, not only crypto.
As I’ve written in my reply to patch 30, I’m not quite sold on this part, but disregarding that part (and one typo below), this patch looks good to me.
Quoted 14 lines
.pre_run() and .clean() take care of calling
bdrv_amend_pre_run()andbdrv_amend_clean()respectively, to set up driver-specific flags and allow the crypto driver to temporarly provide the WRITE perm toqcrypto_block_amend_options()..pre_run() is not yet invoked by
job_start, but .clean() is. This is not a problem, since it will just be a redundant check and crypto will have theupdate->keysflag == false anyways.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/amend.c | 33 +++++++++++++++++++++++++++++++++ include/qemu/job.h | 9 +++++++++ 2 files changed, 42 insertions(+)
[...]
include/qemu/job.h struct JobDriver { · line 229
Quoted 12 lines
include/qemu/job.h +4
-0@@ -223,6 +223,15 @@ struct JobDriver {* the GS API. */ +/** +* Called in the Main Loop context, before the job coroutine +* is started in the job aiocontext. Useful to perform operations +* that needs to be done under BQL (like permissions setup).
s/needs/need/
Quoted 8 lines
include/qemu/job.h +5
-0+* On success, it returns 0. Otherwise the job failed to initialize +* and won't start. +*/ +int (*pre_run)(Job *job,Error **errp); +/** * Called when the job is resumed by the user (i.e. user_paused becomes * false). .user_resume is called before .resume.
Re: [PATCH v6 32/33] crypto: delegate permission functions to JobDriver .pre_run
In reply to Emanuele Giuseppe EspositoQuoted 53 lines · Emanuele Giuseppe Esposito
block_crypto_amend_options_generic_luksuses the block layer permission API, therefore it should be called with the BQL held.However, the same function is being called by two BlockDriver
callbacks:bdrv_amend_options(under BQL) andbdrv_co_amend(I/O).The latter is I/O because it is invoked by block/amend.c's
blockdev_amend_run(), a .run callback of the amend JobDriverTherefore we want to 1) change
block_cryptodriver
to use the permission API only when the BQL is held, and
2) use the .pre_run JobDriver callback to check for permissions before switching to the job aiocontext. This has also the benefit of applying the same permission operation to all amend implementations, not only luks.Remove the permission check in
block_crypto_amend_options_generic_luks()and:
- Add helper functions block_crypto_amend_options_{prepare/cleanup} that take care of checking permissions inblock_crypto_amend_options_luks(), so when it is under BQL, and- Use
job->pre_run()andjob->clean()to do the same thing when we are in an iothread, by performing these checks before the job runs in its aiocontext. So farjob->pre_run()is only defined but not called injob_start(), now it is the moment to use it.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/crypto.c | 57 ++++++++++++++++++++++++++++++++------------------ job.c | 13 ++++++++++++ 2 files changed, 50 insertions(+), 20 deletions(-)block/crypto.c +12
-0@@ -791,6 +791,28 @@ block_crypto_amend_cleanup(BlockDriverState *bs) crypto->updating_keys = false; } +static int +block_crypto_amend_options_prepare (BlockDriverState *bs, +Error **errp) +{ +BlockCrypto *crypto = bs->opaque; + +/* apply for exclusive read/write permissions to the underlying file*/ + crypto->updating_keys = true; +return bdrv_child_refresh_perms (bs, bs->file, errp); +} + +static int
block/crypto.c block_crypto_amend_cleanup(BlockDriverState *bs) · line 815
Quoted 10 lines
block/crypto.c +10
-0+block_crypto_amend_options_cleanup (BlockDriverState *bs, + Error **errp) +{ +BlockCrypto *crypto = bs->opaque; + +/* release exclusive read/write permissions to the underlying file*/ + crypto->updating_keys = false; +return bdrv_child_refresh_perms (bs, bs->file, errp); +} +
Now that I have this patch applied, it does look like it would be nicer if we could skip adding these functions and just reuse block_crypto_amend_{pre_run,cleanup}() (which would require them to call bdrv_child_refresh_perms()).
Quoted 45 lines
block/crypto.c +7
-20static int block_crypto_amend_options_generic_luks(BlockDriverState *bs, QCryptoBlockAmendOptions *amend_options,@@ -798,30 +820,17 @@ block_crypto_amend_options_generic_luks(BlockDriverState *bs, Error **errp) {BlockCrypto *crypto = bs->opaque; -int ret;assert (crypto);assert (crypto->block); -/* apply for exclusive read/write permissions to the underlying file*/ - crypto->updating_keys = true; - ret =bdrv_child_refresh_perms (bs, bs->file, errp); -if (ret) { - goto cleanup; - } - - ret =qcrypto_block_amend_options (crypto->block, - block_crypto_read_func, - block_crypto_write_func, - bs, - amend_options, - force, - errp); -cleanup: -/* release exclusive read/write permissions to the underlying file*/ - crypto->updating_keys = false; -bdrv_child_refresh_perms (bs, bs->file, errp); -return ret; +return qcrypto_block_amend_options (crypto->block, + block_crypto_read_func, + block_crypto_write_func, + bs, + amend_options, + force, + errp); } static int@@ -847,8 +856,16 @@ block_crypto_amend_options_luks(BlockDriverState *bs,if (!amend_options) { goto cleanup; }
block/crypto.c block_crypto_amend_options_luks(BlockDriverState *bs, · line 868
Quoted 10 lines
block/crypto.c +8
-0+ + ret =block_crypto_amend_options_prepare (bs, errp); +if (ret) { + goto perm_cleanup; + } ret =block_crypto_amend_options_generic_luks (bs, amend_options, force, errp); + +perm_cleanup: +block_crypto_amend_options_cleanup (bs, errp);
Uh, pre-existing but still dangerous. We must not pass @errp here, because it may (and if we come from ..._prepare() failing, s/may/will/) already contain some error, and then, if this fails (which it very likely will not), we will get an assertion failure in error_setv().
We could decide that this must not fail and pass &error_abort (but then block_crypto_amend_options_cleanup() should do that), or we pass some new guaranteed-empty pointer and report it.
In any case, we should probably have block_crypto_amend_options_cleanup() (or block_crypto_amend_cleanup()) handle this and have that function return void and no error, so we don’t have to worry about that here at all.
Quoted 18 lines
block/crypto.c +0
-0cleanup:qapi_free_QCryptoBlockAmendOptions (amend_options);return ret;job.c +7
-0@@ -967,11 +967,24 @@ static void coroutine_fn job_co_entry(void *opaque)aio_bh_schedule_oneshot (qemu_get_aio_context (), job_exit, job); } +static int job_pre_run (Job *job) +{ +assert (qemu_in_main_thread ()); +if (job->driver->pre_run) { +return job->driver->pre_run (job, &job->err); + } +
job.c static void coroutine_fn job_co_entry(void *opaque) · line 986
Quoted 10 lines
job.c +5
-0+return 0 ; +} +void job_start (Job *job) {assert (job && !job_started (job) && job->paused && job->driver && job->driver->run); job->co =qemu_coroutine_create (job_co_entry, job); +if (job_pre_run (job)) { +return ;
Do we not need to announce the error somehow? Like, perhaps job_pre_run() should set job->ret to the value returned by .pre_run() (like job_co_entry() does it for .run()), and then call job_completed() on error (or even job_exit()? I’m not sure :/).
The way it is, it looks like the job will just basically leak on error, and never complete.
Hanna
Quoted 4 lines
job.c +1
-0+ } job->pause_count--; job->busy = true; job->paused = false;
Re: [PATCH v6 20/33] block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and test_sync_op_invalidate_cache
In reply to Juan QuintelaQuoted 4 lines · Juan Quintela
Quoted 4 lines
migration/migration.c +0
-0
Quoted 9 lines
migration/migration.c static void process_incoming_migration_bh(void *opaque) · line 502
Quoted 6 lines
migration/migration.c +1
-1@@ -499,7 +499,7 @@ static void process_incoming_migration_bh(void *opaque) global_state_get_runstate() == RUN_STATE_RUNNING))) {/* Make sure all file formats flush their mutable metadata. * If we get an error here, just don't restart the VM yet. */ -bdrv_invalidate_cache_all (&local_err); +bdrv_activate_all (&local_err);I guess that we can change the comment here, it just looks weird the comment saying
flush()and the function nawed_activate()
Do you think it's enough to replace "flush" with "activate"? I am not sure whether "activate their mutable metadata" is meaningful.
Thank you,
Emanuele
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 17 lines
block.hcurrently contains a mix of functions:
some of them run under the BQL and modify the block layer graph, others are instead thread-safe and perform I/O in iothreads. It is not easy to understand which function is part of which group (I/O vs GS), and this patch aims to clarify it.The "GS" functions need the BQL, and often use aio_context_acquire/release and/or drain to be sure they can modify the graph safely.
The I/O function are instead thread safe, and can run in any AioContext.By splitting the header in two files,
block-io.handblock-global-state.hwe have a clearer view on what needs what kind of protection.block-common.hcontains common structures shared by both headers.
Quoted 6 lines
block.his left there for legacy and to avoid changing all includes in all c files that use the block APIs.Assertions are added in the next patch.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>
I didn't check previous versions of the patch series, so you may already have answered some of the questions below. Anyway, here goes:
Quoted 55 lines
block.c | 3 + block/meson.build | 7 +- include/block/block-common.h | 381 +++++++++++++ include/block/block-global-state.h | 269 +++++++++ include/block/block-io.h | 324 +++++++++++ include/block/block.h | 879 +---------------------------- 6 files changed, 1005 insertions(+), 858 deletions(-) create mode 100644 include/block/block-common.h create mode 100644 include/block/block-global-state.h create mode 100644 include/block/block-io.hblock.c +3
-0@@ -67,12 +67,15 @@#define NOT_DONE 0x7fffffff/* used while emulated sync operation in progress */ +/* Protected by BQL */ static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =QTAILQ_HEAD_INITIALIZER (graph_bdrv_states); +/* Protected by BQL */ static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =QTAILQ_HEAD_INITIALIZER (all_bdrv_states); +/* Protected by BQL */ static QLIST_HEAD(, BlockDriver) bdrv_drivers =QLIST_HEAD_INITIALIZER (bdrv_drivers);block/meson.build +5
-2@@ -113,8 +113,11 @@ block_ss.add(module_block_h) wrapper_py = find_program('../scripts/block-coroutine-wrapper.py') block_gen_c = custom_target('block-gen.c', output: 'block-gen.c', - input: files('../include/block/block.h', - 'coroutines.h'), + input: files( + '../include/block/block-io.h', + '../include/block/block-global-state.h', + 'coroutines.h' + ), command: [wrapper_py, '@OUTPUT@', '@INPUT@']) block_ss.add(block_gen_c)include/block/block-common.h new file +0
-0
include/block/block-common.h line 2
Quoted 2 lines
include/block/block-common.h new file +2
-0@@ -0,0 +1,381 @@ +#ifndef BLOCK_COMMON_H +#define BLOCK_COMMON_H
You need to preserve the license header of the file where you moved this from.
Quoted 357 lines
include/block/block-common.h new file +357
-0+#include "block/aio.h" +#include "block/aio-wait.h" +#include "qemu/iov.h" +#include "qemu/coroutine.h" +#include "block/accounting.h" +#include "block/dirty-bitmap.h" +#include "block/blockjob.h" +#include "qemu/hbitmap.h" +#include "qemu/transactions.h" + +/* +* generated_co_wrapper +* +* Function specifier, which does nothing but mark functions to be +* generated by scripts/block-coroutine-wrapper.py +* +* Read more in docs/devel/block-coroutine-wrapper.rst +*/ +#define generated_co_wrapper + +/* block.c */ +typedef struct BlockDriver BlockDriver ; +typedef struct BdrvChild BdrvChild ; +typedef struct BdrvChildClass BdrvChildClass ; + +typedef struct BlockDriverInfo { +/* in bytes, 0 if irrelevant */ +int cluster_size; +/* offset at which the VM state can be saved (0 if not possible) */ +int64_t vm_state_offset; +bool is_dirty; +/* +* True if this block driver only supports compressed writes +*/ +bool needs_compressed_writes; +}BlockDriverInfo ; + +typedef struct BlockFragInfo { +uint64_t allocated_clusters; +uint64_t total_clusters; +uint64_t fragmented_clusters; +uint64_t compressed_clusters; +}BlockFragInfo ; + +typedef enum { +BDRV_REQ_COPY_ON_READ =0x1 , +BDRV_REQ_ZERO_WRITE =0x2 , + +/* +* The BDRV_REQ_MAY_UNMAP flag is used in write_zeroes requests to indicate +* that the block driver should unmap (discard) blocks if it is guaranteed +* that the result will read back as zeroes. The flag is only passed to the +* driver if the block device is opened with BDRV_O_UNMAP. +*/ +BDRV_REQ_MAY_UNMAP =0x4 , + +BDRV_REQ_FUA =0x10 , +BDRV_REQ_WRITE_COMPRESSED =0x20 , + +/* +* Signifies that this write request will not change the visible disk +* content. +*/ +BDRV_REQ_WRITE_UNCHANGED =0x40 , + +/* +* Forces request serialisation. Use only with write requests. +*/ +BDRV_REQ_SERIALISING =0x80 , + +/* +* Execute the request only if the operation can be offloaded or otherwise +* be executed efficiently, but return an error instead of using a slow +* fallback. +*/ +BDRV_REQ_NO_FALLBACK =0x100 , + +/* +* BDRV_REQ_PREFETCH makes sense only in the context of copy-on-read +* (i.e., together with the BDRV_REQ_COPY_ON_READ flag or when a COR +* filter is involved), in which case it signals that the COR operation +* need not read the data into memory (qiov) but only ensure they are +* copied to the top layer (i.e., that COR operation is done). +*/ +BDRV_REQ_PREFETCH =0x200 , + +/* +* If we need to wait for other requests, just fail immediately. Used +* only together with BDRV_REQ_SERIALISING. +*/ +BDRV_REQ_NO_WAIT =0x400 , + +/* Mask of valid flags */ +BDRV_REQ_MASK =0x7ff , +}BdrvRequestFlags ; + +#define BDRV_O_NO_SHARE 0x0001/* don't share permissions */ +#define BDRV_O_RDWR 0x0002 +#define BDRV_O_RESIZE 0x0004/* request permission for resizing the node */ +#define BDRV_O_SNAPSHOT 0x0008/* open the file read only and save +writes in a snapshot */ +#define BDRV_O_TEMPORARY 0x0010/* delete the file after use */ +#define BDRV_O_NOCACHE 0x0020/* do not use the host page cache */ +#define BDRV_O_NATIVE_AIO 0x0080/* use native AIO instead of the +thread pool */ +#define BDRV_O_NO_BACKING 0x0100/* don't open the backing file */ +#define BDRV_O_NO_FLUSH 0x0200/* disable flushing on this disk */ +#define BDRV_O_COPY_ON_READ 0x0400/* copy read backing sectors into image */ +#define BDRV_O_INACTIVE 0x0800/* consistency hint for migration handoff */ +#define BDRV_O_CHECK 0x1000/* open solely for consistency check */ +#define BDRV_O_ALLOW_RDWR 0x2000/* allow reopen to change from r/o to r/w */ +#define BDRV_O_UNMAP 0x4000/* execute guest UNMAP/TRIM operations */ +#define BDRV_O_PROTOCOL 0x8000/* if no block driver is explicitly given: +select an appropriate protocol driver, +ignoring the format layer */ +#define BDRV_O_NO_IO 0x10000/* don't initialize for I/O */ +#define BDRV_O_AUTO_RDONLY 0x20000/* degrade to read-only if opening +read-write fails */ +#define BDRV_O_IO_URING 0x40000/* use io_uring instead of the thread pool */ + +#define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_NO_FLUSH) + + +/* Option names of options parsed by the block layer */ + +#define BDRV_OPT_CACHE_WB "cache.writeback" +#define BDRV_OPT_CACHE_DIRECT "cache.direct" +#define BDRV_OPT_CACHE_NO_FLUSH "cache.no-flush" +#define BDRV_OPT_READ_ONLY "read-only" +#define BDRV_OPT_AUTO_READ_ONLY "auto-read-only" +#define BDRV_OPT_DISCARD "discard" +#define BDRV_OPT_FORCE_SHARE "force-share" + + +#define BDRV_SECTOR_BITS 9 +#define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS) + +#define BDRV_REQUEST_MAX_SECTORS MIN_CONST(SIZE_MAX >> BDRV_SECTOR_BITS, \ + INT_MAX >> BDRV_SECTOR_BITS) +#define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) + +/* +* We want allow aligning requests and disk length up to any 32bit alignment +* and don't afraid of overflow. +* To achieve it, and in the same time use some pretty number as maximum disk +* size, let's define maximum "length" (a limit for any offset/bytes request and +* for disk size) to be the greatest power of 2 less than INT64_MAX. +*/ +#define BDRV_MAX_ALIGNMENT (1L << 30) +#define BDRV_MAX_LENGTH (QEMU_ALIGN_DOWN(INT64_MAX, BDRV_MAX_ALIGNMENT)) + +/* +* Allocation status flags for bdrv_block_status() and friends. +* +* Public flags: +* BDRV_BLOCK_DATA: allocation for data at offset is tied to this layer +* BDRV_BLOCK_ZERO: offset reads as zero +* BDRV_BLOCK_OFFSET_VALID: an associated offset exists for accessing raw data +* BDRV_BLOCK_ALLOCATED: the content of the block is determined by this +* layer rather than any backing, set by block layer +* BDRV_BLOCK_EOF: the returned pnum covers through end of file for this +* layer, set by block layer +* +* Internal flags: +* BDRV_BLOCK_RAW: for use by passthrough drivers, such as raw, to request +* that the block layer recompute the answer from the returned +* BDS; must be accompanied by just BDRV_BLOCK_OFFSET_VALID. +* BDRV_BLOCK_RECURSE: request that the block layer will recursively search for +* zeroes in file child of current block node inside +* returned region. Only valid together with both +* BDRV_BLOCK_DATA and BDRV_BLOCK_OFFSET_VALID. Should not +* appear with BDRV_BLOCK_ZERO. +* +* If BDRV_BLOCK_OFFSET_VALID is set, the map parameter represents the +* host offset within the returned BDS that is allocated for the +* corresponding raw guest data. However, whether that offset +* actually contains data also depends on BDRV_BLOCK_DATA, as follows: +* +* DATA ZERO OFFSET_VALID +* t t t sectors read as zero, returned file is zero at offset +* t f t sectors read as valid from file at offset +* f t t sectors preallocated, read as zero, returned file not +* necessarily zero at offset +* f f t sectors preallocated but read from backing_hd, +* returned file contains garbage at offset +* t t f sectors preallocated, read as zero, unknown offset +* t f f sectors read from unknown file or offset +* f t f not allocated or unknown offset, read as zero +* f f f not allocated or unknown offset, read from backing_hd +*/ +#define BDRV_BLOCK_DATA 0x01 +#define BDRV_BLOCK_ZERO 0x02 +#define BDRV_BLOCK_OFFSET_VALID 0x04 +#define BDRV_BLOCK_RAW 0x08 +#define BDRV_BLOCK_ALLOCATED 0x10 +#define BDRV_BLOCK_EOF 0x20 +#define BDRV_BLOCK_RECURSE 0x40 + +typedef QTAILQ_HEAD(BlockReopenQueue, BlockReopenQueueEntry) BlockReopenQueue; + +typedef struct BDRVReopenState { +BlockDriverState *bs; +int flags; +BlockdevDetectZeroesOptions detect_zeroes; +bool backing_missing; +BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ +BlockDriverState *old_file_bs;/* keep pointer for permissions update */ +QDict *options; +QDict *explicit_options; +void *opaque; +}BDRVReopenState ; + +/* +* Block operation types +*/ +typedef enum BlockOpType { +BLOCK_OP_TYPE_BACKUP_SOURCE , +BLOCK_OP_TYPE_BACKUP_TARGET , +BLOCK_OP_TYPE_CHANGE , +BLOCK_OP_TYPE_COMMIT_SOURCE , +BLOCK_OP_TYPE_COMMIT_TARGET , +BLOCK_OP_TYPE_DATAPLANE , +BLOCK_OP_TYPE_DRIVE_DEL , +BLOCK_OP_TYPE_EJECT , +BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT , +BLOCK_OP_TYPE_INTERNAL_SNAPSHOT , +BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE , +BLOCK_OP_TYPE_MIRROR_SOURCE , +BLOCK_OP_TYPE_MIRROR_TARGET , +BLOCK_OP_TYPE_RESIZE , +BLOCK_OP_TYPE_STREAM , +BLOCK_OP_TYPE_REPLACE , +BLOCK_OP_TYPE_MAX , +}BlockOpType ; + +/* Block node permission constants */ +enum { +/** +* A user that has the "permission" of consistent reads is guaranteed that +* their view of the contents of the block device is complete and +* self-consistent, representing the contents of a disk at a specific +* point. +* +* For most block devices (including their backing files) this is true, but +* the property cannot be maintained in a few situations like for +* intermediate nodes of a commit block job. +*/ +BLK_PERM_CONSISTENT_READ =0x01 , + +/** This permission is required to change the visible disk contents. */ +BLK_PERM_WRITE =0x02 , + +/** +* This permission (which is weaker than BLK_PERM_WRITE) is both enough and +* required for writes to the block node when the caller promises that +* the visible disk content doesn't change. +* +* As the BLK_PERM_WRITE permission is strictly stronger, either is +* sufficient to perform an unchanging write. +*/ +BLK_PERM_WRITE_UNCHANGED =0x04 , + +/** This permission is required to change the size of a block node. */ +BLK_PERM_RESIZE =0x08 , + +/** +* There was a now-removed bit BLK_PERM_GRAPH_MOD, with value of 0x10. QEMU +* 6.1 and earlier may still lock the corresponding byte in block/file-posix +* locking. So, implementing some new permission should be very careful to +* not interfere with this old unused thing. +*/ + +BLK_PERM_ALL =0x0f , + +DEFAULT_PERM_PASSTHROUGH =BLK_PERM_CONSISTENT_READ + |BLK_PERM_WRITE + |BLK_PERM_WRITE_UNCHANGED + |BLK_PERM_RESIZE , + +DEFAULT_PERM_UNCHANGED =BLK_PERM_ALL & ~DEFAULT_PERM_PASSTHROUGH , +}; + +/* +* Flags that parent nodes assign to child nodes to specify what kind of +* role(s) they take. +* +* At least one of DATA, METADATA, FILTERED, or COW must be set for +* every child. +*/ +enum BdrvChildRoleBits { +/* +* This child stores data. +* Any node may have an arbitrary number of such children. +*/ +BDRV_CHILD_DATA = (1 <<0 ), + +/* +* This child stores metadata. +* Any node may have an arbitrary number of metadata-storing +* children. +*/ +BDRV_CHILD_METADATA = (1 <<1 ), + +/* +* A child that always presents exactly the same visible data as +* the parent, e.g. by virtue of the parent forwarding all reads +* and writes. +* This flag is mutually exclusive with DATA, METADATA, and COW. +* Any node may have at most one filtered child at a time. +*/ +BDRV_CHILD_FILTERED = (1 <<2 ), + +/* +* Child from which to read all data that isn't allocated in the +* parent (i.e., the backing child); such data is copied to the +* parent through COW (and optionally COR). +* This field is mutually exclusive with DATA, METADATA, and +* FILTERED. +* Any node may have at most one such backing child at a time. +*/ +BDRV_CHILD_COW = (1 <<3 ), + +/* +* The primary child. For most drivers, this is the child whose +* filename applies best to the parent node. +* Any node may have at most one primary child at a time. +*/ +BDRV_CHILD_PRIMARY = (1 <<4 ), + +/* Useful combination of flags */ +BDRV_CHILD_IMAGE =BDRV_CHILD_DATA + |BDRV_CHILD_METADATA + |BDRV_CHILD_PRIMARY , +}; + +/* Mask of BdrvChildRoleBits values */ +typedef unsigned int BdrvChildRole ; + +typedef struct BdrvCheckResult { +int corruptions; +int leaks; +int check_errors; +int corruptions_fixed; +int leaks_fixed; +int64_t image_end_offset; +BlockFragInfo bfi; +}BdrvCheckResult ; + +typedef enum { +BDRV_FIX_LEAKS =1 , +BDRV_FIX_ERRORS =2 , +}BdrvCheckMode ; + +typedef struct BlockSizes { +uint32_t phys; +uint32_t log; +}BlockSizes ;
include/block/block-common.h line 370
Quoted 10 lines
include/block/block-common.h new file +10
-0+ +typedef struct HDGeometry { +uint32_t heads; +uint32_t sectors; +uint32_t cylinders; +}HDGeometry ; + +/* Common functions that are neither I/O nor Global State */ + +int bdrv_parse_aio (const char *mode,int *flags);
Makes sense to me to have this here, it is just a helper function that parses stuff and doesn't touch any state. However, what is different about the following?
int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough); int bdrv_parse_discard_flags(const char *mode, int *flags);
Despite being simple helpers, you kept them as global state.
include/block/block-common.h line 379
Quoted 8 lines
include/block/block-common.h new file +8
-0+int path_has_protocol (const char *path); +int path_is_absolute (const char *path); +char *path_combine (const char *base_path,const char *filename); + +char *bdrv_get_full_backing_filename_from_filename (const char *backed, +const char *backing, +Error **errp); +char *bdrv_get_full_backing_filename (BlockDriverState *bs,Error **errp);
On the other hand, this one takes a BlockDriverState and returns a value derived from one of its fields, so it's not just a helper function that is independent of state.
Why isn't this grouped as global state? (Or possibly I/O since it only reads bs.)
Quoted 48 lines
include/block/block-common.h new file +1
-0+#endif /* BLOCK_COMMON_H */ include/block/block-global-state.h new file +41
-0@@ -0,0 +1,269 @@ +/* +* QEMU System Emulator block driver +* +* Copyright (c) 2003 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +#ifndef BLOCK_GLOBAL_STATE_H +#define BLOCK_GLOBAL_STATE_H + +#include "block-common.h" + +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* If a function modifies the graph, it also uses drain and/or +* aio_context_acquire/release to be sure it has unique access. +* aio_context locking is needed together with BQL because of +* the thread-safe I/O API that concurrently runs and accesses +* the graph without the BQL. +* +* It is important to note that not all of these functions are +* necessarily limited to running under the BQL, but they would +* require additional auditing and many small thread-safety changes +* to move them into the I/O API. Often it's not worth doing that
include/block/block-global-state.h line 51
Quoted 10 lines
include/block/block-global-state.h new file +10
-0+* work since the APIs are only used with the BQL held at the +* moment, so they have been placed in the GS API (for now). +* +* All functions in this header must use this assertion: +* assert(qemu_in_main_thread()); +* to catch when they are accidentally called without the BQL. +*/ + +char *bdrv_perm_names (uint64_t perm); +uint64_t bdrv_qapi_perm_to_blk_perm (BlockPermission qapi_perm);
These two functions are again helpers that don't touch the state at all.
include/block/block-global-state.h line 53
Quoted 1 line
include/block/block-global-state.h new file +1
-0+/* disk I/O throttling */
Let's remove this comment (maybe in a separate preparational patch), it doesn't make any sense at all any more. It was added in commit 0563e191516 to describe bdrv_io_limits_enable()/disable(), which were removed in commit 97148076, but apparently I forgot to remove the comment.
include/block/block-global-state.h line 57
Quoted 4 lines
include/block/block-global-state.h new file +4
-0+void bdrv_init (void ); +void bdrv_init_with_whitelist (void ); +bool bdrv_uses_whitelist (void ); +int bdrv_is_whitelisted (BlockDriver *drv,bool read_only);
The whitelist is static and doesn't touch runtime state.
include/block/block-global-state.h line 61
Quoted 4 lines
include/block/block-global-state.h new file +4
-0+BlockDriver *bdrv_find_protocol (const char *filename, +bool allow_protocol_prefix, +Error **errp); +BlockDriver *bdrv_find_format (const char *format_name);
These don't touch the graph, but they can load modules. I guess requiring the BQL for this can't hurt, so agree.
Quoted 7 lines
include/block/block-global-state.h new file +7
-0+int bdrv_create (BlockDriver *drv,const char * filename, +QemuOpts *opts,Error **errp); +int bdrv_create_file (const char *filename,QemuOpts *opts,Error **errp); + +BlockDriverState *bdrv_new (void ); +int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top, +Error **errp);
include/block/block-global-state.h line 78
Quoted 10 lines
include/block/block-global-state.h new file +10
-0+int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, +Error **errp); +int bdrv_replace_child_bs (BdrvChild *child,BlockDriverState *new_bs, +Error **errp); +BlockDriverState *bdrv_insert_node (BlockDriverState *bs,QDict *node_options, +int flags,Error **errp); +int bdrv_drop_filter (BlockDriverState *bs,Error **errp); + +int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough); +int bdrv_parse_discard_flags (const char *mode,int *flags);
I already mentioned these above.
Quoted 19 lines
include/block/block-global-state.h new file +19
-0+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); +BlockDriverState *bdrv_open_blockdev_ref (BlockdevRef *ref,Error **errp); +int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, +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 *bdrv_new_open_driver_opts (BlockDriver *drv, +const char *node_name, +QDict *options,int flags, +Error **errp); +BlockDriverState *bdrv_new_open_driver (BlockDriver *drv,const char *node_name, +int flags,Error **errp);
include/block/block-global-state.h line 107
Quoted 10 lines
include/block/block-global-state.h new file +10
-0+BlockReopenQueue *bdrv_reopen_queue (BlockReopenQueue *bs_queue, +BlockDriverState *bs,QDict *options, +bool keep_old_opts); +void bdrv_reopen_queue_free (BlockReopenQueue *bs_queue); +int bdrv_reopen_multiple (BlockReopenQueue *bs_queue,Error **errp); +int bdrv_reopen (BlockDriverState *bs,QDict *opts,bool keep_old_opts, +Error **errp); +int bdrv_reopen_set_read_only (BlockDriverState *bs,bool read_only, +Error **errp); +int bdrv_make_zero (BdrvChild *child,BdrvRequestFlags flags);
This is surprising. Why isn't it an I/O function? It only calls other I/O functions.
include/block/block-global-state.h line 113
Quoted 6 lines
include/block/block-global-state.h new file +6
-0+BlockDriverState *bdrv_find_backing_image (BlockDriverState *bs, +const char *backing_file); +void bdrv_refresh_filename (BlockDriverState *bs); +void bdrv_refresh_limits (BlockDriverState *bs,Transaction *tran,Error **errp); +int bdrv_commit (BlockDriverState *bs); +int bdrv_make_empty (BdrvChild *c,Error **errp);
I guess this could be an I/O function in theory. Depends on the implementations of drv->bdrv_make_empty(). They don't look safe, so having it as global state for now makes sense. Though holding the BQL doesn't make it safe either, draining would probably work.
Quoted 25 lines
include/block/block-global-state.h new file +25
-0+int bdrv_change_backing_file (BlockDriverState *bs,const char *backing_file, +const char *backing_fmt,bool warn); +void bdrv_register (BlockDriver *bdrv); +int bdrv_drop_intermediate (BlockDriverState *top,BlockDriverState *base, +const char *backing_file_str); +BlockDriverState *bdrv_find_overlay (BlockDriverState *active, +BlockDriverState *bs); +BlockDriverState *bdrv_find_base (BlockDriverState *bs); +bool bdrv_is_backing_chain_frozen (BlockDriverState *bs,BlockDriverState *base, +Error **errp); +int bdrv_freeze_backing_chain (BlockDriverState *bs,BlockDriverState *base, +Error **errp); +void bdrv_unfreeze_backing_chain (BlockDriverState *bs,BlockDriverState *base); + +/* +* The units of offset and total_work_size may be chosen arbitrarily by the +* block driver; total_work_size may change during the course of the amendment +* operation +*/ +typedef void BlockDriverAmendStatusCB (BlockDriverState *bs,int64_t offset, +int64_t total_work_size,void *opaque); +int bdrv_amend_options (BlockDriverState *bs_new,QemuOpts *opts, +BlockDriverAmendStatusCB *status_cb,void *cb_opaque, +bool force, +Error **errp);
include/block/block-global-state.h line 148
Quoted 10 lines
include/block/block-global-state.h new file +10
-0+ +/* check if a named node can be replaced when doing drive-mirror */ +BlockDriverState *check_to_replace_node (BlockDriverState *parent_bs, +const char *node_name,Error **errp); + +/* async block I/O */ +void bdrv_aio_cancel (BlockAIOCB *acb); +void bdrv_invalidate_cache_all (Error **errp); + +int bdrv_inactivate_all (void );
The grouping is odd here. The comment refers only to bdrv_aio_cancel(), while bdrv_invalidate_cache_all()/bdrv_inactivate_all() are a logically related function pair.
Also odd: Why keep bdrv_aio_cancel() as global state, but make bdrv_aio_cancel_async() an I/O function? (The latter is required, of course, because virtio-scsi can call it in I/O threads.)
bdrv_aio_cancel() calls aio_poll(). I think this means that it actually must run in the AioContext that is polled, which may or may not be the main thread. Before this series, we assert running in the main thread only in one if branch.
include/block/block-global-state.h line 159
Quoted 10 lines
include/block/block-global-state.h new file +10
-0+int bdrv_flush_all (void ); +void bdrv_close_all (void ); +void bdrv_drain_all_begin (void ); +void bdrv_drain_all_end (void ); +void bdrv_drain_all (void ); + +#define BDRV_POLL_WHILE (bs, cond) ({ \ + BlockDriverState *bs_ = (bs); \ + AIO_WAIT_WHILE(bdrv_get_aio_context(bs_), \ + cond); })
Hmm... AIO_WAIT_WHILE() is explicitly documented to be called from I/O threads. But it has to be a specific I/O thread, the home thread of the AioContext of bs.
So it really fits neither the description for global state (it works outside the main thread) nor for I/O functions (it doesn't work in any arbitrary thread).
I have a vague feeling that it might not be the only function of this kind.
Quoted 273 lines
include/block/block-global-state.h new file +109
-0+int bdrv_has_zero_init_1 (BlockDriverState *bs); +int bdrv_has_zero_init (BlockDriverState *bs); +int bdrv_can_set_read_only (BlockDriverState *bs,bool read_only, +bool ignore_allow_rdw,Error **errp); +int bdrv_apply_auto_read_only (BlockDriverState *bs,const char *errmsg, +Error **errp); +BlockDriverState *bdrv_find_node (const char *node_name); +BlockDeviceInfoList *bdrv_named_nodes_list (bool flat,Error **errp); +XDbgBlockGraph *bdrv_get_xdbg_block_graph (Error **errp); +BlockDriverState *bdrv_lookup_bs (const char *device, +const char *node_name, +Error **errp); +bool bdrv_chain_contains (BlockDriverState *top,BlockDriverState *base); +BlockDriverState *bdrv_next_node (BlockDriverState *bs); +BlockDriverState *bdrv_next_all_states (BlockDriverState *bs); + +typedef struct BdrvNextIterator { +enum { +BDRV_NEXT_BACKEND_ROOTS , +BDRV_NEXT_MONITOR_OWNED , + } phase; +BlockBackend *blk; +BlockDriverState *bs; +}BdrvNextIterator ; + +BlockDriverState *bdrv_first (BdrvNextIterator *it); +BlockDriverState *bdrv_next (BdrvNextIterator *it); +void bdrv_next_cleanup (BdrvNextIterator *it); + +BlockDriverState *bdrv_next_monitor_owned (BlockDriverState *bs); +void bdrv_iterate_format (void (*it)(void *opaque,const char *name), +void *opaque,bool read_only); +int bdrv_get_flags (BlockDriverState *bs); +char *bdrv_dirname (BlockDriverState *bs,Error **errp); + +void bdrv_img_create (const char *filename,const char *fmt, +const char *base_filename,const char *base_fmt, +char *options,uint64_t img_size,int flags, +bool quiet,Error **errp); + +void bdrv_ref (BlockDriverState *bs); +void bdrv_unref (BlockDriverState *bs); +void bdrv_unref_child (BlockDriverState *parent,BdrvChild *child); +BdrvChild *bdrv_attach_child (BlockDriverState *parent_bs, +BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +Error **errp); + +bool bdrv_op_is_blocked (BlockDriverState *bs,BlockOpType op,Error **errp); +void bdrv_op_block (BlockDriverState *bs,BlockOpType op,Error *reason); +void bdrv_op_unblock (BlockDriverState *bs,BlockOpType op,Error *reason); +void bdrv_op_block_all (BlockDriverState *bs,Error *reason); +void bdrv_op_unblock_all (BlockDriverState *bs,Error *reason); +bool bdrv_op_blocker_is_empty (BlockDriverState *bs); + +int bdrv_debug_breakpoint (BlockDriverState *bs,const char *event, +const char *tag); +int bdrv_debug_remove_breakpoint (BlockDriverState *bs,const char *tag); +int bdrv_debug_resume (BlockDriverState *bs,const char *tag); +bool bdrv_debug_is_suspended (BlockDriverState *bs,const char *tag); + +/** +* Locks the AioContext of @bs if it's not the current AioContext. This avoids +* double locking which could lead to deadlocks: This is a coroutine_fn, so we +* know we already own the lock of the current AioContext. +* +* May only be called in the main thread. +*/ +void coroutine_fn bdrv_co_lock(BlockDriverState *bs); + +/** +* Unlocks the AioContext of @bs if it's not the current AioContext. +*/ +void coroutine_fn bdrv_co_unlock(BlockDriverState *bs); + +void bdrv_set_aio_context_ignore (BlockDriverState *bs, +AioContext *new_context,GSList **ignore); +int bdrv_try_set_aio_context (BlockDriverState *bs,AioContext *ctx, +Error **errp); +int bdrv_child_try_set_aio_context (BlockDriverState *bs,AioContext *ctx, +BdrvChild *ignore_child,Error **errp); +bool bdrv_child_can_set_aio_context (BdrvChild *c,AioContext *ctx, +GSList **ignore,Error **errp); +bool bdrv_can_set_aio_context (BlockDriverState *bs,AioContext *ctx, +GSList **ignore,Error **errp); + +int bdrv_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz); +int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo); + +void bdrv_add_child (BlockDriverState *parent,BlockDriverState *child, +Error **errp); +void bdrv_del_child (BlockDriverState *parent,BdrvChild *child,Error **errp); + +/** +* +* bdrv_register_buf/bdrv_unregister_buf: +* +* Register/unregister a buffer for I/O. For example, VFIO drivers are +* interested to know the memory areas that would later be used for I/O, so +* that they can prepare IOMMU mapping etc., to get better performance. +*/ +void bdrv_register_buf (BlockDriverState *bs,void *host,size_t size); +void bdrv_unregister_buf (BlockDriverState *bs,void *host); + +void bdrv_cancel_in_flight (BlockDriverState *bs); + +#endif /* BLOCK_GLOBAL_STATE_H */ include/block/block-io.h new file +158
-0@@ -0,0 +1,324 @@ +/* +* QEMU System Emulator block driver +* +* Copyright (c) 2003 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +#ifndef BLOCK_IO_H +#define BLOCK_IO_H + +#include "block-common.h" + +/* +* I/O API functions. These functions are thread-safe, and therefore +* can run in any thread as long as the thread has called +* aio_context_acquire/release(). +*/ + +int bdrv_pwrite_zeroes (BdrvChild *child,int64_t offset, +int64_t bytes,BdrvRequestFlags flags); +int bdrv_pread (BdrvChild *child,int64_t offset,void *buf,int64_t bytes); +int bdrv_pwrite (BdrvChild *child,int64_t offset,const void *buf, +int64_t bytes); +int bdrv_pwrite_sync (BdrvChild *child,int64_t offset, +const void *buf,int64_t bytes); +/* +* Efficiently zero a region of the disk image. Note that this is a regular +* I/O request like read or write and should have a reasonable size. This +* function is not suitable for zeroing the entire image in a single request +* because it may allocate memory for the entire region. +*/ +int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, +int64_t bytes,BdrvRequestFlags flags); + +int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact, +PreallocMode prealloc,BdrvRequestFlags flags, +Error **errp); +int generated_co_wrapper +bdrv_truncate (BdrvChild *child,int64_t offset,bool exact, +PreallocMode prealloc,BdrvRequestFlags flags,Error **errp); + +int64_t bdrv_nb_sectors (BlockDriverState *bs); +int64_t bdrv_getlength (BlockDriverState *bs); +int64_t bdrv_get_allocated_file_size (BlockDriverState *bs); +BlockMeasureInfo *bdrv_measure (BlockDriver *drv,QemuOpts *opts, +BlockDriverState *in_bs,Error **errp); +void bdrv_get_geometry (BlockDriverState *bs,uint64_t *nb_sectors_ptr); +int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp); +void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs); + + +int generated_co_wrapper bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, +BdrvCheckMode fix); + +void bdrv_aio_cancel_async (BlockAIOCB *acb); + +/* sg packet commands */ +int bdrv_co_ioctl (BlockDriverState *bs,int req,void *buf); + +/* Invalidate any cached metadata used by image formats */ +int generated_co_wrapper bdrv_invalidate_cache(BlockDriverState *bs, + Error **errp); + +/* Ensure contents are flushed to disk. */ +int generated_co_wrapper bdrv_flush(BlockDriverState *bs); +int coroutine_fn bdrv_co_flush(BlockDriverState *bs); +void bdrv_drain (BlockDriverState *bs); +void coroutine_fn bdrv_co_drain(BlockDriverState *bs); + +int generated_co_wrapper bdrv_pdiscard(BdrvChild *child, int64_t offset, +int64_t bytes); +int bdrv_co_pdiscard (BdrvChild *child,int64_t offset,int64_t bytes); +bool bdrv_can_write_zeroes_with_unmap (BlockDriverState *bs); +int bdrv_block_status (BlockDriverState *bs,int64_t offset, +int64_t bytes,int64_t *pnum,int64_t *map, +BlockDriverState **file); +int bdrv_block_status_above (BlockDriverState *bs,BlockDriverState *base, +int64_t offset,int64_t bytes,int64_t *pnum, +int64_t *map,BlockDriverState **file); +int bdrv_is_allocated (BlockDriverState *bs,int64_t offset,int64_t bytes, +int64_t *pnum); +int bdrv_is_allocated_above (BlockDriverState *top,BlockDriverState *base, +bool include_base,int64_t offset,int64_t bytes, +int64_t *pnum); +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, +int64_t bytes); + +bool bdrv_is_read_only (BlockDriverState *bs); +bool bdrv_is_writable (BlockDriverState *bs); +bool bdrv_is_sg (BlockDriverState *bs); +bool bdrv_is_inserted (BlockDriverState *bs); +void bdrv_lock_medium (BlockDriverState *bs,bool locked); +void bdrv_eject (BlockDriverState *bs,bool eject_flag); +const char *bdrv_get_format_name (BlockDriverState *bs); + +bool bdrv_supports_compressed_writes (BlockDriverState *bs); +const char *bdrv_get_node_name (const BlockDriverState *bs); +const char *bdrv_get_device_name (const BlockDriverState *bs); +const char *bdrv_get_device_or_node_name (const BlockDriverState *bs); +int bdrv_get_info (BlockDriverState *bs,BlockDriverInfo *bdi); +ImageInfoSpecific *bdrv_get_specific_info (BlockDriverState *bs, +Error **errp); +BlockStatsSpecific *bdrv_get_specific_stats (BlockDriverState *bs); +void bdrv_round_to_clusters (BlockDriverState *bs, +int64_t offset,int64_t bytes, +int64_t *cluster_offset, +int64_t *cluster_bytes); + +void bdrv_get_backing_filename (BlockDriverState *bs, +char *filename,int filename_size); + +int generated_co_wrapper +bdrv_readv_vmstate (BlockDriverState *bs,QEMUIOVector *qiov,int64_t pos); +int generated_co_wrapper +bdrv_writev_vmstate (BlockDriverState *bs,QEMUIOVector *qiov,int64_t pos); +int bdrv_save_vmstate (BlockDriverState *bs,const uint8_t *buf, +int64_t pos,int size); + +int bdrv_load_vmstate (BlockDriverState *bs,uint8_t *buf, +int64_t pos,int size); + +/* +* Returns the alignment in bytes that is required so that no bounce buffer +* is required throughout the stack +*/ +size_t bdrv_min_mem_align (BlockDriverState *bs); +/* Returns optimal alignment in bytes for bounce buffer */ +size_t bdrv_opt_mem_align (BlockDriverState *bs); +void *qemu_blockalign (BlockDriverState *bs,size_t size); +void *qemu_blockalign0 (BlockDriverState *bs,size_t size); +void *qemu_try_blockalign (BlockDriverState *bs,size_t size); +void *qemu_try_blockalign0 (BlockDriverState *bs,size_t size); +bool bdrv_qiov_is_aligned (BlockDriverState *bs,QEMUIOVector *qiov); + +void bdrv_enable_copy_on_read (BlockDriverState *bs); +void bdrv_disable_copy_on_read (BlockDriverState *bs); + +void bdrv_debug_event (BlockDriverState *bs,BlkdebugEvent event); + +#define BLKDBG_EVENT (child, evt) \ + do { \ + if (child) { \
include/block/block-io.h line 168
Quoted 10 lines
include/block/block-io.h new file +10
-0+bdrv_debug_event (child->bs, evt); \ + } \ + }while (0 ) + +/** +* bdrv_get_aio_context: +* +* Returns: the currently bound #AioContext +*/ +AioContext *bdrv_get_aio_context (BlockDriverState *bs);
Could use an empty line here.
(That's about the kind of comment I can make for I/O functions... I still need to see how I can actually verify that the status given to them is correct and they are indeed thread-safe.)
Quoted 60 lines
include/block/block-io.h new file +60
-0+/** +* Move the current coroutine to the AioContext of @bs and return the old +* AioContext of the coroutine. Increase bs->in_flight so that draining @bs +* will wait for the operation to proceed until the corresponding +* bdrv_co_leave(). +* +* Consequently, you can't call drain inside a bdrv_co_enter/leave() section as +* this will deadlock. +*/ +AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs); + +/** +* Ends a section started by bdrv_co_enter(). Move the current coroutine back +* to old_ctx and decrease bs->in_flight again. +*/ +void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx); + +/** +* Transfer control to @co in the aio context of @bs +*/ +void bdrv_coroutine_enter (BlockDriverState *bs,Coroutine *co); + +AioContext *bdrv_child_get_parent_aio_context (BdrvChild *c); +AioContext *child_of_bds_get_parent_aio_context (BdrvChild *c); + +void bdrv_io_plug (BlockDriverState *bs); +void bdrv_io_unplug (BlockDriverState *bs); + +/** +* bdrv_parent_drained_begin_single: +* +* Begin a quiesced section for the parent of @c. If @poll is true, wait for +* any pending activity to cease. +*/ +void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll); + +/** +* bdrv_parent_drained_end_single: +* +* End a quiesced section for the parent of @c. +* +* This polls @bs's AioContext until all scheduled sub-drained_ends +* have settled, which may result in graph changes. +*/ +void bdrv_parent_drained_end_single (BdrvChild *c); + +/** +* bdrv_drain_poll: +* +* Poll for pending requests in @bs, its parents (except for @ignore_parent), +* and if @recursive is true its children as well (used for subtree drain). +* +* If @ignore_bds_parents is true, parents that are BlockDriverStates must +* ignore the drain request because they will be drained separately (used for +* drain_all). +* +* This is part of bdrv_drained_begin. +*/ +bool bdrv_drain_poll (BlockDriverState *bs,bool recursive, +BdrvChild *ignore_parent,bool ignore_bds_parents);
include/block/block-io.h line 238
Quoted 10 lines
include/block/block-io.h new file +10
-0+ +/** +* bdrv_drained_begin: +* +* Begin a quiesced section for exclusive access to the BDS, by disabling +* external request sources including NBD server, block jobs, and device model. +* +* This function can be recursive. +*/ +void bdrv_drained_begin (BlockDriverState *bs);
I don't see how the whole family of drain functions can work in any arbitrary thread. They call BDRV_POLL_WHILE(), the rules for which are that it has to be either in the main thread or in the AioContext of bs (which currently ensures that all block nodes and their users are in the same AioContext).
Cross-AioContext BDRV_POLL_WHILE() can cause deadlocks, as documented in AIO_WAIT_WHILE().
Actually, the same is true for all other BDRV_POLL_WHILE() callers, too. For example, every function generated by block-coroutine-wrapper.py can only be thread-safe when called in coroutine context, otherwise it has to be called from the node's AioContext.
Quoted 75 lines
include/block/block-io.h new file +75
-0+/** +* bdrv_do_drained_begin_quiesce: +* +* Quiesces a BDS like bdrv_drained_begin(), but does not wait for already +* running requests to complete. +*/ +void bdrv_do_drained_begin_quiesce (BlockDriverState *bs, +BdrvChild *parent,bool ignore_bds_parents); + +/** +* Like bdrv_drained_begin, but recursively begins a quiesced section for +* exclusive access to all child nodes as well. +*/ +void bdrv_subtree_drained_begin (BlockDriverState *bs); + +/** +* bdrv_drained_end: +* +* End a quiescent section started by bdrv_drained_begin(). +* +* This polls @bs's AioContext until all scheduled sub-drained_ends +* have settled. On one hand, that may result in graph changes. On +* the other, this requires that the caller either runs in the main +* loop; or that all involved nodes (@bs and all of its parents) are +* in the caller's AioContext. +*/ +void bdrv_drained_end (BlockDriverState *bs); + +/** +* bdrv_drained_end_no_poll: +* +* Same as bdrv_drained_end(), but do not poll for the subgraph to +* actually become unquiesced. Therefore, no graph changes will occur +* with this function. +* +* *drained_end_counter is incremented for every background operation +* that is scheduled, and will be decremented for every operation once +* it settles. The caller must poll until it reaches 0. The counter +* should be accessed using atomic operations only. +*/ +void bdrv_drained_end_no_poll (BlockDriverState *bs,int *drained_end_counter); + +/** +* End a quiescent section started by bdrv_subtree_drained_begin(). +*/ +void bdrv_subtree_drained_end (BlockDriverState *bs); + +bool bdrv_can_store_new_dirty_bitmap (BlockDriverState *bs,const char *name, +uint32_t granularity,Error **errp); + +/** +* +* bdrv_co_copy_range: +* +* Do offloaded copy between two children. If the operation is not implemented +* by the driver, or if the backend storage doesn't support it, a negative +* error code will be returned. +* +* Note: block layer doesn't emulate or fallback to a bounce buffer approach +* because usually the caller shouldn't attempt offloaded copy any more (e.g. +* calling copy_file_range(2)) after the first error, thus it should fall back +* to a read+write path in the caller level. +* +* @src: Source child to copy data from +* @src_offset: offset in @src image to read data +* @dst: Destination child to copy data to +* @dst_offset: offset in @dst image to write data +* @bytes: number of bytes to copy +* @flags: request flags. Supported flags: +* BDRV_REQ_ZERO_WRITE - treat the @src range as zero data and do zero +* write on @dst as if bdrv_co_pwrite_zeroes is +* called. Used to simplify caller code, or +* during BlockDriver.bdrv_co_copy_range_from() +* recursion. +* BDRV_REQ_NO_SERIALISING - do not serialize with other overlapping
include/block/block-io.h line 324
Quoted 10 lines
include/block/block-io.h new file +10
-0+* requests currently in flight. +* +* Returns: 0 if succeeded; negative error code if failed. +**/ +int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset, +BdrvChild *dst,int64_t dst_offset, +int64_t bytes,BdrvRequestFlags read_flags, +BdrvRequestFlags write_flags); + +#endif /* BLOCK_IO_H */
Kevin
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 2 lines
When invoked from the main loop, this function is the same as
qemu_mutex_iothread_locked, and returns true if the BQL is held.
So its name is misleading because it doesn't answer the question whether we're in the main thread, but whethere we're holding the BQL (which is mostly equivalent to holding an AioContext lock, not being in the home thread of that AioContext).
Quoted 8 lines
When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop.
This essentially just extends
qemu_mutex_iothread_lockedto work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation instubs/iothread-lock.c.
Quoted 7 lines
Using
qemu_mutex_iothread_lockedin unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>
This adds a new function that is almost the same as an existing function, but the documentation is unclear when I should use one or the other.
What are the use cases for the existing qemu_mutex_iothread_locked() stub where we rely on false being returned?
If there aren't any, then maybe we should change the stub for that one instead of adding a new function that behaves the same in the system emulator and different only when it's stubbed out?
Kevin
Re: [PATCH v6 21/33] block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 71 lines
Split
bdrv_co_invalidatecache in two: the GS code that takes care of permissions and running GS callbacks, and leave only the I/O code (->bdrv_co_invalidate_cache) running in the I/O coroutine.The only side effect is that
bdrv_co_invalidate_cacheis not recursive anymore, and so is every direct call tobdrv_invalidate_cache().Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)block.c +5
-15@@ -6550,23 +6550,20 @@ void bdrv_init_with_whitelist(void) }int bdrv_activate (BlockDriverState *bs,Error **errp) -{ -return bdrv_invalidate_cache (bs, errp); -} - -int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) {BdrvChild *child, *parent;Error *local_err =NULL ;int ret;BdrvDirtyBitmap *bm; +assert (qemu_in_main_thread ()); +if (!bs->drv) {return -ENOMEDIUM ; } QLIST_FOREACH(child, &bs->children, next) { -bdrv_co_invalidate_cache (child->bs, &local_err); +bdrv_activate (child->bs, &local_err);if (local_err) {error_propagate (errp, local_err);return -EINVAL ;@@ -6579,7 +6576,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)* Note that the required permissions of inactive images are always a * subset of the permissions required after activating the image. This * allows us to just get the permissions upfront without restricting -* drv->bdrv_invalidate_cache(). +* drv->bdrv_co_invalidate_cache(). * * It also means that in error cases, we don't have to try and revert to * the old permissions (which is an operation that could fail, too). We can @@ -6594,14 +6591,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return ret; } -if (bs->drv->bdrv_co_invalidate_cache) { - bs->drv->bdrv_co_invalidate_cache (bs, &local_err); -if (local_err) { - bs->open_flags |=BDRV_O_INACTIVE ; -error_propagate (errp, local_err); -return -EINVAL ; - } - } +bdrv_invalidate_cache (bs, errp); FOR_EACH_DIRTY_BITMAP(bs, bm) {bdrv_dirty_bitmap_skip_store (bm, false);@@ -6629,6 +6619,22 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)return 0 ;
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6629
Quoted 10 lines
block.c +8
-0} +int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) +{ +Error *local_err =NULL ; + +if (bs->drv->bdrv_co_invalidate_cache) { + bs->drv->bdrv_co_invalidate_cache (bs, &local_err); +if (local_err) { + bs->open_flags |=BDRV_O_INACTIVE ;
This doesn't feel like the right place. The flag is cleared by the caller, so it should also be set again on failure by the caller and not by this function.
What bdrv_co_invalidate_cache() could do is assert that BDRV_O_INACTIVE is cleared when it's called.
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6636
Quoted 7 lines
block.c +7
-0+error_propagate (errp, local_err); +return -EINVAL ; + } + } + +return 0 ; +}
Kevin
Re: [PATCH v6 04/33] block/export/fuse.c: allow writable exports to take RESIZE permission
In reply to Hanna ReitzQuoted 6 lines · Hanna Reitz
Quoted 5 lines · Emanuele Giuseppe Esposito
Allow writable exports to get
BLK_PERM_RESIZEpermission from creation, infuse_export_create().
In this way, there is no need to give the permission infuse_do_truncate(), which might be run in an iothread.
Quoted 8 lines
Quoted 7 lines
Permissions should be set only in the main thread, so in any case if an iothread tries to set RESIZE, it will be blocked.
Also assert in
fuse_do_truncatethat if we give the RESIZE permission we can then restore the original ones, since we don't check the return value ofblk_set_perm.
I will then just remove the last line in the commit message ("since .. blk_set_perm.").
Thank you,
Emanuele
Quoted 69 lines
We do, because we pass
&error_abortfor errp, so if an error were to occur, qemu would abort.Not that I mind adding an assertion on the return value, just noting that we omitted that kind of intentionally.
Reviewed-by Hanna Reitz <[email protected]>Quoted 60 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>---
block/export/fuse.c| 25 ++++++++++++++++++-------1 file changed, 18 insertions(+), 7 deletions(-)block/export/fuse.c +0
-0@@ -86,8 +86,8 @@ static int fuse_export_create(BlockExport *blk_exp, assert(blk_exp_args->type == BLOCK_EXPORT_TYPE_FUSE);- /* For growable exports, take the RESIZE permission */ - if (
args->growable) {
+ /* For growable and writable exports, take the RESIZE permission */ + if (args->growable||blk_exp_args->writable) {uint64_tblk_perm,blk_shared_perm;blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);block/export/fuse.c +4
-1@@ -392,14 +392,23 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, { uint64_t blk_perm, blk_shared_perm; BdrvRequestFlags truncate_flags = 0; - int ret; + bool add_resize_perm; + int ret, ret_check; + + /* Growable and writable exports have a permanent RESIZEpermission */
+add_resize_perm= !exp->growable && !exp->writable;
if (req_zero_write) {
truncate_flags|=BDRV_REQ_ZERO_WRITE;
}
- /* Growable exports have a permanent RESIZE permission */ - if (!exp->growable) {
+ if (add_resize_perm) {
+
+ if (!qemu_in_main_thread()) {
+ /* Changing permissions like below only works in the main thread */
+ return-EPERM;
+ }
+
blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm); ret = blk_set_perm(exp->common.blk,blk_perm|BLK_PERM_RESIZE,block/export/fuse.c +1
-0@@ -412,9 +421,11 @@ static int fuse_do_truncate(const FuseExport *exp, int64_t size, ret = blk_truncate(exp->common.blk, size, true, prealloc, truncate_flags, NULL); - if (!exp->growable) { + if (add_resize_perm) { /* Must succeed, because we are only giving up the RESIZEpermission */
-blk_set_perm(exp->common.blk, blk_perm, blk_shared_perm, &error_abort);
+ret_check= blk_set_perm(exp->common.blk,blk_perm, +blk_shared_perm,&error_abort); +assert(ret_check == 0);
}
return ret;
Re: [PATCH v6 29/33] job.h: assertions in the callers of JobDriver funcion pointers
In reply to Hanna ReitzQuoted 11 lines · Hanna Reitz
Quoted 4 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>---
job.c| 9 +++++++++1 file changed, 9 insertions(+)Just curious, why did you remove the assertion in
job_co_entry()? (Looking at it again, it might have been nicer to swap it with the assertion below it, so thatjob != NULLis asserted first, but other than that...)
I think it's useless, job_co_entry runs in a coroutine in job->aio_context created and entered in job_start (its only caller), so there is no way that we are in a different aiocontext.
Same as assert(job), I don't think the opaque pointer can ever be NULL.
Thank you,
Emanuele
Quoted 5 lines
(And since I’m already replying to this patch, might as well point out s/funcion/function/ in the subject)
Hanna
Re: [PATCH v6 25/33] block_int-common.h: split function pointers in BdrvChildClass
In reply to Hanna ReitzQuoted 26 lines · Hanna Reitz
Quoted 25 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>---
include/block/block_int-common.h| 67 +++++++++++++++++++-------------1 file changed, 40 insertions(+), 27 deletions(-)include/block/block_int-common.h +0
-0@@ -815,12 +815,16 @@ struct BdrvChildClass {*/ bool parent_is_bds;+ /*
+ * Global state (GS) API. These functions run under the BQL lock. + *
+ * Seeinclude/block/block-global-state.hfor more information about + * the GS API.
+ */
void (*inherit_options)(BdrvChildRole role, boolparent_is_format, int*child_flags, QDict*child_options, intparent_flags, QDict*parent_options); -
void (*change_media)(BdrvChild *child, bool load);
- void (*resize)(BdrvChild *child);
Quoted 8 lines
Quoted 3 lines
/*
* Returns a name that is supposedly more useful for human users than theThe method this comment belongs to is
.get_name(). It’s exposed throughbdrv_get_parent_name(), which is called bybdrv_get_device_name()andbdrv_get_device_or_node_name()– so I think it should be classified as I/O.
Agree on this, and also on comment on patch 23 about bdrv_probe.
Quoted 12 lines
include/block/block_int-common.h struct BdrvChildClass { · line 842
Quoted 4 lines
include/block/block_int-common.h +0
-0@@ -837,6 +841,40 @@ struct BdrvChildClass {*/ char *(*get_parent_desc)(BdrvChild *child);This function is very similar, so we might also want to reconsider classifying it as I/O. There’s no need, because all of its callers do run in the main thread, but at the same time I don’t believe there’s anything stopping us (and it starts to sound to me like all functions of the “get name” kind perhaps should ideally be I/O, in that they shouldn’t require the GS context).
Quoted 1 line
Up to you. O:)
I understand what you mean, but I would like to leave it in global state. Seeing the assertion/definition as GS tells me that it is already under BQL, and I should not worry about I/O in that function. If no such assert was present, it would lead me to think that there might be I/O concurrently using it, and complicate the logic of whatever I want to do there.
Thank you,
Emanuele
Quoted 5 lines
(Rest of this patch looks good!)
Hanna
Re: [PATCH v6 32/33] crypto: delegate permission functions to JobDriver .pre_run
In reply to Hanna ReitzQuoted 132 lines · Hanna Reitz
Quoted 53 lines · Emanuele Giuseppe Esposito
block_crypto_amend_options_generic_luksuses the block layer permission API, therefore it should be called with the BQL held.However, the same function is being called by two BlockDriver
callbacks:bdrv_amend_options(under BQL) andbdrv_co_amend(I/O).The latter is I/O because it is invoked by block/amend.c's
blockdev_amend_run(), a .run callback of the amend JobDriverTherefore we want to 1) change
block_cryptodriver
to use the permission API only when the BQL is held, and
2) use the .pre_run JobDriver callback to check for permissions before switching to the job aiocontext. This has also the benefit of applying the same permission operation to all amend implementations, not only luks.Remove the permission check in
block_crypto_amend_options_generic_luks()and:
- Add helper functions block_crypto_amend_options_{prepare/cleanup} that take care of checking permissions inblock_crypto_amend_options_luks(), so when it is under BQL, and- Use
job->pre_run()andjob->clean()to do the same thing when we are in an iothread, by performing these checks before the job runs in its aiocontext. So farjob->pre_run()is only defined but not called injob_start(), now it is the moment to use it.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>---
block/crypto.c| 57 ++++++++++++++++++++++++++++++++------------------ job.c | 13 ++++++++++++2 files changed, 50 insertions(+), 20 deletions(-)block/crypto.c +0
-0@@ -791,6 +791,28 @@ block_crypto_amend_cleanup(BlockDriverState *bs) crypto->updating_keys = false; }+static int
block/crypto.c +6
-0+block_crypto_amend_options_prepare (BlockDriverState *bs, + Error **errp) +{ + BlockCrypto *crypto = bs->opaque; + + /* apply for exclusive read/write permissions to the underlyingfile*/
+crypto->updating_keys= true;
+ returnbdrv_child_refresh_perms(bs, bs->file, errp);block/crypto.c +3
-0+} + +static intblock/crypto.c block_crypto_amend_cleanup(BlockDriverState *bs) · line 815
Quoted 10 lines
block/crypto.c +10
-0+block_crypto_amend_options_cleanup (BlockDriverState *bs, + Error **errp) +{ + BlockCrypto *crypto = bs->opaque; + + /* release exclusive read/write permissions to the underlying file*/ + crypto->updating_keys = false; + return bdrv_child_refresh_perms(bs, bs->file, errp); +} +Now that I have this patch applied, it does look like it would be nicer if we could skip adding these functions and just reuse block_crypto_amend_{pre_run,cleanup}() (which would require them to call
bdrv_child_refresh_perms()).Quoted 46 lines
static int block_crypto_amend_options_generic_luks(BlockDriverState *bs, QCryptoBlockAmendOptions
*amend_options,block/crypto.c +0
-1@@ -798,30 +820,17 @@block_crypto_amend_options_generic_luks (BlockDriverState *bs, Error **errp) { BlockCrypto *crypto = bs->opaque; - int ret; assert(crypto); assert(crypto->block);- /* apply for exclusive read/write permissions to the underlying file*/
-crypto->updating_keys= true;
- ret =bdrv_child_refresh_perms(bs, bs->file, errp);
- if (ret) {
- goto cleanup;
- }block/crypto.c +7
-13- - ret = qcrypto_block_amend_options(crypto->block, - block_crypto_read_func, - block_crypto_write_func, - bs, - amend_options, - force, - errp); -cleanup: - /* release exclusive read/write permissions to the underlying file*/ - crypto->updating_keys = false; - bdrv_child_refresh_perms(bs, bs->file, errp); - return ret; + return qcrypto_block_amend_options(crypto->block, + block_crypto_read_func, + block_crypto_write_func, + bs, + amend_options, + force, + errp);}
static intblock/crypto.c +0
-0@@ -847,8 +856,16 @@ block_crypto_amend_options_luks(BlockDriverState *bs, if (!amend_options) { goto cleanup; }block/crypto.c block_crypto_amend_options_luks(BlockDriverState · line 869
Quoted 10 lines
block/crypto.c +8
-0+ + ret = block_crypto_amend_options_prepare(bs, errp); + if (ret) { + goto perm_cleanup; + } ret = block_crypto_amend_options_generic_luks(bs, amend_options, force, errp); + +perm_cleanup: + block_crypto_amend_options_cleanup(bs, errp);Uh, pre-existing but still dangerous. We must not pass @errp here, because it may (and if we come from ..._prepare() failing, s/may/will/) already contain some error, and then, if this fails (which it very likely will not), we will get an assertion failure in
error_setv().
Quoted 8 lines
We could decide that this must not fail and pass
&error_abort(but thenblock_crypto_amend_options_cleanup()should do that), or we pass some new guaranteed-empty pointer and report it.In any case, we should probably have
block_crypto_amend_options_cleanup()(orblock_crypto_amend_cleanup()) handle this and have that function return void and no error, so we don’t have to worry about that here at all.
Applied all feedback on crypto and amend (patches 30, 31, 32). All what you said makes sense.
Quoted 34 lines
Quoted 23 lines
cleanup:
qapi_free_QCryptoBlockAmendOptions(amend_options); return ret;job.c +9
-0@@ -967,11 +967,24 @@ static void coroutine_fn job_co_entry(void *opaque) aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job); } +static int job_pre_run(Job *job) +{ + assert(qemu_in_main_thread()); + if (job->driver->pre_run) { + return job->driver->pre_run(job, &job->err); + } + + return 0; +} + void job_start(Job *job) {Quoted 5 lines
assert(job && !job_started(job) && job->paused && job->driver && job->driver->run);job->co=qemu_coroutine_create(job_co_entry, job); + if (job_pre_run(job)) {
+ return;Do we not need to announce the error somehow? Like, perhaps
job_pre_run()should setjob->retto the value returned by .pre_run() (likejob_co_entry()does it for .run()), and then calljob_completed()on error (or evenjob_exit()? I’m not sure :/).
Quoted 3 lines
The way it is, it looks like the job will just basically leak on error, and never complete.
I will do something like this:
1. move job_pre_run to run just before aio_co_enter() in job_start(), because it must be in JOB_STATUS_RUNNING otherwise the transition status in job_exit functions won't probably work.
Basically simulate as .run() just finished and failed.
2. change in this
+ ret = job->driver->pre_run(job, &job->err);
+ if (ret) {
+ job->ret = ret; + job_exit(job); + return ret;
+ }
basically cache the reply of pre_run, and if it's non-zero, set job->ret and exit, just as job_co_entry does.
Thank you,
Emanuele
Quoted 7 lines
Hanna
Quoted 4 lines
+ }
job->pause_count--;
job->busy= true;
job->paused= false;
Re: [PATCH v6 29/33] job.h: assertions in the callers of JobDriver funcion pointers
In reply to Emanuele Giuseppe EspositoQuoted 7 lines · Emanuele Giuseppe Esposito
Quoted 5 lines · Hanna Reitz
Quoted 4 lines · Emanuele Giuseppe Esposito
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- job.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Quoted 8 lines
Quoted 5 lines
Just curious, why did you remove the assertion in
job_co_entry()? (Looking at it again, it might have been nicer to swap it with the assertion below it, so thatjob != NULLis asserted first, but other than that...)I think it's useless,
job_co_entryruns in a coroutine injob->aio_contextcreated and entered injob_start(its only caller), so there is no way that we are in a different aiocontext.
Well, that is what assertions are for. You don’t put them there to catch errors, you put them there to show that there’s some sort of contract the caller definitely fulfills, so that after the assertion you know this contract is fulfilled without having to read and verify what the caller does.
And perhaps also to prevent regressions when the caller code changes.
Quoted 1 line
Same as assert(job), I don't think the opaque pointer can ever be NULL.
Good, because if it could, that shouldn’t be an assertion. O:)
Quoted 8 lines
Thank you,
EmanueleQuoted 5 lines
(And since I’m already replying to this patch, might as well point out s/funcion/function/ in the subject)
Hanna
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Kevin WolfQuoted 22 lines · Kevin Wolf
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 2 lines
When invoked from the main loop, this function is the same as
qemu_mutex_iothread_locked, and returns true if the BQL is held.So its name is misleading because it doesn't answer the question whether we're in the main thread, but whethere we're holding the BQL (which is mostly equivalent to holding an AioContext lock, not being in the home thread of that AioContext).
Quoted 13 lines
When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop.
This essentially just extends
qemu_mutex_iothread_lockedto work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation instubs/iothread-lock.c.Using
qemu_mutex_iothread_lockedin unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>This adds a new function that is almost the same as an existing function, but the documentation is unclear when I should use one or the other.
Why do you think it is unclear? as explained in commit and documentation, unit tests for example use stubs/iothread-lock, which returns always false. So we can (and should!) use this function in APIs invoked by
unit tests, because qemu_mutex_iothread_locked will just return false, making all tests crash. On the other side, I am pretty sure it won't cause any failure when running QEMU or qemu-iotests, because there most of the API use the cpu implementation.
Quoted 3 lines
What are the use cases for the existing
qemu_mutex_iothread_locked()stub where we rely on false being returned?
I don't think we ever rely on stub being false. There are only 2 places where I see
!qemu_mutex_iothread_locked(), and are in helper_regs.c and cpus.c
So being cpu related functions they use the cpu implementation.
However, commit 5f50be9b5810293141bb53cfd0cb46e765367d56 changed the stub to return false for a specific reason.
Quoted 4 lines
If there aren't any, then maybe we should change the stub for that one instead of adding a new function that behaves the same in the system emulator and different only when it's stubbed out?
I don't think we can replace it, bcause stubs/qemu_in_main_thread() calls qemu_get_current_aio_context, that in turn calls qemu_mutex_iothread_locked. So we implicitly rely on that "false".
Thank you,
Emanuele
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
include/block/block-common.h line 370
Quoted 3 lines
include/block/block-common.h new file +3
-0+/* Common functions that are neither I/O nor Global State */ + +int bdrv_parse_aio (const char *mode,int *flags);Makes sense to me to have this here, it is just a helper function that parses stuff and doesn't touch any state. However, what is different about the following?
Quoted 5 lines
int
bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough); intbdrv_parse_discard_flags(const char *mode, int *flags);Despite being simple helpers, you kept them as global state.
I guess those two are only called by GS functions, while tihs one is not. Anyways, you are right, I will move them to block-common.h
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
+/* disk I/O throttling */
Let's remove this comment (maybe in a separate preparational patch), it doesn't make any sense at all any more. It was added in commit
0563e191516to describe bdrv_io_limits_enable()/disable(), which were removed in commit 97148076, but apparently I forgot to remove the comment.
Done, sent as a separate patch "block.h: remove outdated comment"
[...]
Quoted 4 lines
Quoted 4 lines
include/block/block-global-state.h line 57
Quoted 3 lines
include/block/block-global-state.h new file +3
-0+void bdrv_init_with_whitelist (void ); +bool bdrv_uses_whitelist (void ); +int bdrv_is_whitelisted (BlockDriver *drv,bool read_only);The whitelist is static and doesn't touch runtime state.
Ok I am moving all the three above in block-common.h
[...]
Quoted 9 lines
Quoted 9 lines
include/block/block-global-state.h line 148
Quoted 5 lines
include/block/block-global-state.h new file +5
-0+/* async block I/O */ +void bdrv_aio_cancel (BlockAIOCB *acb); +void bdrv_invalidate_cache_all (Error **errp); + +int bdrv_inactivate_all (void );The grouping is odd here. The comment refers only to
bdrv_aio_cancel(), while bdrv_invalidate_cache_all()/bdrv_inactivate_all() are a logically related function pair.
Quoted 8 lines
Quoted 8 lines
Also odd: Why keep
bdrv_aio_cancel()as global state, but makebdrv_aio_cancel_async()an I/O function? (The latter is required, of course, because virtio-scsi can call it in I/O threads.)
bdrv_aio_cancel()callsaio_poll(). I think this means that it actually must run in the AioContext that is polled, which may or may not be the main thread. Before this series, we assert running in the main thread only in one if branch.
ok moving the comment + bdrv_aio_cancel in block-io.h
[...]
Quoted 10 lines
Quoted 10 lines
include/block/block-global-state.h line 159
Quoted 3 lines
include/block/block-global-state.h new file +3
-0+BlockDriverState *bs_ = (bs); \ +AIO_WAIT_WHILE (bdrv_get_aio_context (bs_), \ + cond); })Hmm...
AIO_WAIT_WHILE()is explicitly documented to be called from I/O threads. But it has to be a specific I/O thread, the home thread of the AioContext of bs.So it really fits neither the description for global state (it works outside the main thread) nor for I/O functions (it doesn't work in any arbitrary thread).
So I would say BDRV_POLL_WHILE can go in block-common? what you wrote above is the exact definition of common functions.
[...]
Quoted 16 lines
Quoted 16 lines
include/block/block-io.h line 238
Quoted 10 lines
include/block/block-io.h new file +10
-0+ +/** +* bdrv_drained_begin: +* +* Begin a quiesced section for exclusive access to the BDS, by disabling +* external request sources including NBD server, block jobs, and device model. +* +* This function can be recursive. +*/ +void bdrv_drained_begin (BlockDriverState *bs);I don't see how the whole family of drain functions can work in any arbitrary thread. They call
BDRV_POLL_WHILE(), the rules for which are that it has to be either in the main thread or in the AioContext of bs (which currently ensures that all block nodes and their users are in the same AioContext).
Quoted 8 lines
Quoted 8 lines
Cross-AioContext
BDRV_POLL_WHILE()can cause deadlocks, as documented inAIO_WAIT_WHILE().Actually, the same is true for all other
BDRV_POLL_WHILE()callers, too. For example, every function generated byblock-coroutine-wrapper.pycan only be thread-safe when called in coroutine context, otherwise it has to be called from the node's AioContext.
Maybe not in any arbitrary thread, but an iothread can call these function in any case. Therefore it is not a GS function for sure, so I would leave it as I/O.
I guess you can also see I/O more as "all the ones that are not GS".
Rest of comments that I did not answer: I agree.
Emanuele
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
Am 31.01.2022 um 14:40 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 7 lines
Quoted 7 lines
include/block/block-global-state.h line 159
Quoted 3 lines
include/block/block-global-state.h new file +3
-0+BlockDriverState *bs_ = (bs); \ +AIO_WAIT_WHILE (bdrv_get_aio_context (bs_), \ + cond); })Hmm...
AIO_WAIT_WHILE()is explicitly documented to be called from I/O threads. But it has to be a specific I/O thread, the home thread of the AioContext of bs.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
So it really fits neither the description for global state (it works outside the main thread) nor for I/O functions (it doesn't work in any arbitrary thread).
So I would say
BDRV_POLL_WHILEcan go in block-common? what you wrote above is the exact definition of common functions.
Maybe, but either way I feel it should be categorised the same as its users, like bdrv_drained_begin() below.
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
include/block/block-io.h line 238
Quoted 10 lines
include/block/block-io.h new file +10
-0+ +/** +* bdrv_drained_begin: +* +* Begin a quiesced section for exclusive access to the BDS, by disabling +* external request sources including NBD server, block jobs, and device model. +* +* This function can be recursive. +*/ +void bdrv_drained_begin (BlockDriverState *bs);I don't see how the whole family of drain functions can work in any arbitrary thread. They call
BDRV_POLL_WHILE(), the rules for which are that it has to be either in the main thread or in the AioContext of bs (which currently ensures that all block nodes and their users are in the same AioContext).Quoted 8 lines
Quoted 8 lines
Cross-AioContext
BDRV_POLL_WHILE()can cause deadlocks, as documented inAIO_WAIT_WHILE().Actually, the same is true for all other
BDRV_POLL_WHILE()callers, too. For example, every function generated byblock-coroutine-wrapper.pycan only be thread-safe when called in coroutine context, otherwise it has to be called from the node's AioContext.
Quoted 5 lines
Maybe not in any arbitrary thread, but an iothread can call these function in any case. Therefore it is not a GS function for sure, so I would leave it as I/O.
I guess you can also see I/O more as "all the ones that are not GS".
Your current definition is:
/*
* I/O API functions. These functions are thread-safe, and therefore
* can run in any thread as long as the thread has called
* aio_context_acquire/release().
*/
If you want it to mean "everything that isn't GS", then you need to change this definition. (This in turn would mean that nothing would fit the definiton for common functions any more, which is "neither GS nor I/O".)
I thought that the definition that you currently have is actually meaningful in the context of multiqueue, which I think is ultimately the goal of your work. If at some point you're going to argue "this uses only I/O functions, so it's threadsafe", the definition of I/O functions must actually guarantee this.
If you categorise bdrv_drained_begin() as an I/O function, then this property is violated because you have to call it from a specific thread.
So I guess the decision depends on what you're going to use the categories in the future. I get the feeling that we have one more category than this series introduces:
* Global State (must run from the main thread/hold the BQL)
* I/O (can be called in any thread under the AioContext lock, doesn't modify global state, drain waits for it to complete)
* Common (doesn't even touch global state)
* iothread dependent (can run without the BQL, but only in one specific iothread while holding its AioContext lock; this would cover at least AIO_WAIT_WHILE() and all of its indirect callers)
Kevin
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Emanuele Giuseppe EspositoAm 31.01.2022 um 12:25 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 32 lines · Kevin Wolf
Quoted 22 lines
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 2 lines
When invoked from the main loop, this function is the same as
qemu_mutex_iothread_locked, and returns true if the BQL is held.So its name is misleading because it doesn't answer the question whether we're in the main thread, but whethere we're holding the BQL (which is mostly equivalent to holding an AioContext lock, not being in the home thread of that AioContext).
Quoted 13 lines
When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop.
This essentially just extends
qemu_mutex_iothread_lockedto work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation instubs/iothread-lock.c.Using
qemu_mutex_iothread_lockedin unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop.Quoted 6 lines
Quoted 2 lines
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>This adds a new function that is almost the same as an existing function, but the documentation is unclear when I should use one or the other.
Quoted 8 lines
Why do you think it is unclear? as explained in commit and documentation, unit tests for example use stubs/iothread-lock, which returns always false. So we can (and should!) use this function in APIs invoked by unit tests, because
qemu_mutex_iothread_lockedwill just return false, making all tests crash. On the other side, I am pretty sure it won't cause any failure when running QEMU or qemu-iotests, because there most of the API use the cpu implementation.
I feel "use this function if your code is going to be used by unit tests, but use qemu_mutex_iothread_locked() otherwise" is a very strange interface. I'm relatively sure that qemu_mutex_iothread_locked() isn't primarily used to make unit tests crash.
Documentation and the definition of the interface of a function shouldn't be about the caller, but about the semantics of the function itself. So, which question does qemu_mutex_iothread_locked() answer, and which question does qemu_in_main_thread() answer?
The meaning of their result must be different, otherwise there wouldn't be two different functions that must be used in different contexts.
Quoted 11 lines
Quoted 2 lines
What are the use cases for the existing
qemu_mutex_iothread_locked()stub where we rely on false being returned?I don't think we ever rely on stub being false. There are only 2 places where I see !qemu_mutex_iothread_locked(), and are in
helper_regs.candcpus.cSo being cpu related functions they use the cpu implementation.
However, commit
5f50be9b5810293141bb53cfd0cb46e765367d56changed the stub to return false for a specific reason.
I must admit I don't really understand the reasoning there:
With this change, the stub qemu_mutex_iothread_locked() must be changed from true to false. The previous value of true was needed because the main thread did not have an AioContext in the thread-local variable, but now it does have one.
This explains why it _can_ be changed to false for this caller, but not why it _must_ be changed.
Quoted 7 lines
Quoted 3 lines
If there aren't any, then maybe we should change the stub for that one instead of adding a new function that behaves the same in the system emulator and different only when it's stubbed out?
I don't think we can replace it, bcause stubs/qemu_in_main_thread() calls
qemu_get_current_aio_context, that in turn callsqemu_mutex_iothread_locked. So we implicitly rely on that "false".
qemu_get_current_aio_context() will just return my_aiocontext, which is qemu_aio_context because all tools call qemu_init_main_loop(). I don't think it will ever call qemu_mutex_iothread_locked() in tools, except for worker threads.
Ooh, and that's why we return false. Because the only kind of non-main threads accessing global state in tools are worker threads which never hold the BQL in the system emulator.
So is the problem with the unit tests really just that they never call qemu_init_main_loop() and therefore never set the AioContext for the main thread?
Kevin
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Kevin WolfQuoted 11 lines · Kevin Wolf
So I guess the decision depends on what you're going to use the categories in the future. I get the feeling that we have one more category than this series introduces:
* Global State (must run from the main thread/hold the BQL)
* I/O (can be called in any thread under the AioContext lock, doesn't modify global state, drain waits for it to complete)
* Common (doesn't even touch global state)
* iothread dependent (can run without the BQL, but only in one specific iothread while holding its AioContext lock; this would cover at leastAIO_WAIT_WHILE()and all of its indirect callers)
Yes, I agree.
bdrv_drained_begin and friends are somewhat like a coroutine-level lock/unlock primitive, so they need to be available in both the main thread and the iothread. They could be called iothread dependent, AioContext dependent, or perhaps "global or I/O".
That said, even if they are a different category, I think it makes sense to leave them in the same header file as I/O functions, because I/O functions are locked out between drained_begin and drained_end.
Paolo
Re: [PATCH v6 20/33] block: rename bdrv_invalidate_cache_all, blk_invalidate_cache and test_sync_op_invalidate_cache
In reply to Emanuele Giuseppe EspositoQuoted 9 lines · Emanuele Giuseppe Esposito
Quoted 7 lines
Review
Quoted 4 lines
/* Make sure all file formats flush their mutable metadata.
* If we get an error here, just don't restart the VM yet. */(quoted diff) +1
-1- bdrv_invalidate_cache_all(&local_err); + bdrv_activate_all(&local_err);I guess that we can change the comment here, it just looks weird the comment saying
flush()and the function nawed_activate()Do you think it's enough to replace "flush" with "activate"? I am not sure whether "activate their mutable metadata" is meaningful.
"Activation" consists of throwing away mutable metadata.
That's what "flush" means in this case, but it is often interpreted as "persisting" the metadata instead. So perhaps s/flush/throw away/.
Paolo
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Kevin WolfQuoted 4 lines · Kevin Wolf
I feel "use this function if your code is going to be used by unit tests, but use
qemu_mutex_iothread_locked()otherwise" is a very strange interface. I'm relatively sure thatqemu_mutex_iothread_locked()isn't primarily used to make unit tests crash.
qemu_mutex_iothread_locked() should never be used in the block layer, because programs that use the block layer might not call an iothread lock, and indeed only the emulator have an iothread lock.
Making it always true would be okay when those programs were single-threaded, but really they all had worker threads so it was changed to false by commit 5f50be9b58 ("async: the main AioContext is only "current" if under the BQL", 2021-06-18).
Quoted 4 lines
Documentation and the definition of the interface of a function shouldn't be about the caller, but about the semantics of the function itself. So, which question does
qemu_mutex_iothread_locked()answer, and which question doesqemu_in_main_thread()answer?
qemu_mutex_iothread_locked() -> Does the program have exclusive access to the emulator's globals.
qemu_in_main_thread() -> Does the program have access to the block layer's globals. In emulators this is governed by the iothread lock, elsewhere they are accessible only from the home thread of the initial AioContext.
So, in emulators it is the same question, but in the block layer one of them is actually meaningless.
Really the "bug" is that qemu_mutex_iothread_locked() should really not be used outside emulators. There are just two uses, but it's hard to remove them.
So why are two functions needed? Two reasons:
- stubs/iothread-lock.c cannot define qemu_mutex_iothread_locked() as "return qemu_get_current_aio_context() == qemu_get_aio_context();" because it would cause infinite recursion with qemu_get_current_aio_context()
- even if it could, frankly qemu_mutex_iothread_locked() is a horrible name, and in the context of the block layer it conflicts especially badly with the "iothread" concept.
Maybe we should embrace the BQL name and rename the functions that refer to the "iothread lock". But then I don't want to have code in the block layer that refers to a "big lock".
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
What are the use cases for the existing
qemu_mutex_iothread_locked()stub where we rely on false being returned?I don't think we ever rely on stub being false. There are only 2 places where I see !qemu_mutex_iothread_locked(), and are in
helper_regs.candcpus.c
We rely on it indirectly, here:
AioContext *qemu_get_current_aio_context(void)
{
if (my_aiocontext) {
return my_aiocontext;
}
if (qemu_mutex_iothread_locked()) {
/* Possibly in a vCPU thread. */
return qemu_get_aio_context();
}
return NULL;
}which affects the behavior of aio_co_enter. Before that patch, worker threads computed qemu_get_aio_context(), while afterwards they compute NULL.
This is not just for unit tests but also for qemu-storage-daemon and other block layer programs.
Quoted 5 lines
Quoted 2 lines
However, commit
5f50be9b5810293141bb53cfd0cb46e765367d56changed the stub to return false for a specific reason.I must admit I don't really understand the reasoning there:
Quoted 7 lines
With this change, the stub
qemu_mutex_iothread_locked()must be changed from true to false. The previous value of true was needed because the main thread did not have an AioContext in the thread-local variable, but now it does have one.This explains why it _can_ be changed to false for this caller, but not why it _must_ be changed.
See above: because it returns the wrong value for all threads except one, and there are better ways to do a meaningful check in that one thread: using qemu_get_current_aio_context(), which is what aio_co_enter did at the time and qemu_in_main_thread() does with Emanuele's change.
Quoted 3 lines
So is the problem with the unit tests really just that they never call
qemu_init_main_loop()and therefore never set the AioContext for the main thread?
No, most of them do (and if some are missing it we can add it).
Paolo
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
Quoted 42 lines · Paolo Bonzini
Quoted 4 lines · Kevin Wolf
I feel "use this function if your code is going to be used by unit tests, but use
qemu_mutex_iothread_locked()otherwise" is a very strange interface. I'm relatively sure thatqemu_mutex_iothread_locked()isn't primarily used to make unit tests crash.
qemu_mutex_iothread_locked()should never be used in the block layer, because programs that use the block layer might not call an iothread lock, and indeed only the emulator have an iothread lock.Making it always true would be okay when those programs were single-threaded, but really they all had worker threads so it was changed to false by commit
5f50be9b58("async: the main AioContext is only "current" if under the BQL", 2021-06-18).Quoted 4 lines
Documentation and the definition of the interface of a function shouldn't be about the caller, but about the semantics of the function itself. So, which question does
qemu_mutex_iothread_locked()answer, and which question doesqemu_in_main_thread()answer?
qemu_mutex_iothread_locked()-> Does the program have exclusive access to the emulator's globals.
qemu_in_main_thread()-> Does the program have access to the block layer's globals. In emulators this is governed by the iothread lock, elsewhere they are accessible only from the home thread of the initial AioContext.So, in emulators it is the same question, but in the block layer one of them is actually meaningless.
Really the "bug" is that
qemu_mutex_iothread_locked()should really not be used outside emulators. There are just two uses, but it's hard to remove them.So why are two functions needed? Two reasons:
-
stubs/iothread-lock.ccannot defineqemu_mutex_iothread_locked()as "returnqemu_get_current_aio_context()==qemu_get_aio_context();" because it would cause infinite recursion withqemu_get_current_aio_context()
Quoted 7 lines
- even if it could, frankly
qemu_mutex_iothread_locked()is a horrible name, and in the context of the block layer it conflicts especially badly with the "iothread" concept.Maybe we should embrace the BQL name and rename the functions that refer to the "iothread lock". But then I don't want to have code in the block layer that refers to a "big lock".
So based on Paolo's reply, I would modify the function comment in this way:
@@ -242,6 +242,9 @@ AioContext *iohandler_get_aio_context(void);
* must always be taken outside other locks. This function helps
* functions take different paths depending on whether the current
* thread is running within the main loop mutex.
+ *
+ * This function should never be used in the block layer, please
+ * instead refer to qemu_in_main_thread().
*/
bool qemu_mutex_iothread_locked(void);
+
+/**
+ * qemu_in_main_thread: same as qemu_mutex_iothread_locked when
+ * softmmu/cpus.c implementation is linked. Otherwise this function
+ * checks that the current AioContext is the global AioContext
+ * (main loop).
+ *
+ * This is useful when checking that the BQL is held as a
+ * replacement of qemu_mutex_iothread_locked() usege in the
+ * block layer, since the former returns false when invoked by
+ * unit tests or other users like qemu-storage-daemon that end
+ * up using the stubs/iothread-lock.c implementation.
+ *
+ * This function should only be used in the block layer.
+ * Use this function to determine whether it is possible to safely
+ * access the block layer's globals.
+ */
+bool qemu_in_main_thread(void);
What do you think?
Emanuele
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Paolo BonziniQuoted 20 lines · Paolo Bonzini
Quoted 11 lines · Kevin Wolf
So I guess the decision depends on what you're going to use the categories in the future. I get the feeling that we have one more category than this series introduces:
* Global State (must run from the main thread/hold the BQL)
* I/O (can be called in any thread under the AioContext lock, doesn't modify global state, drain waits for it to complete)
* Common (doesn't even touch global state)
* iothread dependent (can run without the BQL, but only in one specific iothread while holding its AioContext lock; this would cover at leastAIO_WAIT_WHILE()and all of its indirect callers)Yes, I agree.
bdrv_drained_beginand friends are somewhat like a coroutine-level lock/unlock primitive, so they need to be available in both the main thread and the iothread. They could be called iothread dependent, AioContext dependent, or perhaps "global or I/O".
Quoted 6 lines
That said, even if they are a different category, I think it makes sense to leave them in the same header file as I/O functions, because I/O functions are locked out between
drained_beginanddrained_end.Paolo
Proposed category description:
/*
* "Global OR I/O" API functions. These functions can run without
* the BQL, but only in one specific iothread/main loop. *
* More specifically, these functions use BDRV_POLL_WHILE(bs), which
* requires the caller to be either in the main thread and hold
* the BlockdriverState (bs) AioContext lock, or directly in the
* home thread that runs the bs AioContext. Calling them from
* another thread in another AioContext would cause deadlocks. *
* Therefore, these functions are not proper I/O, because they
* can't run in *any* iothreads, but only in a specific one. */
Functions that will surely go under this category:
BDRV_POLL_WHILEbdrv_parent_drained_begin_singlebdrv_parent_drained_end_singlebdrv_drain_pollbdrv_drained_beginbdrv_do_drained_begin_quiescebdrv_subtree_drained_beginbdrv_drained_endbdrv_drained_end_no_pollbdrv_subtree_drained_end
(all generated_co_wrapper)bdrv_truncatebdrv_checkbdrv_invalidate_cachebdrv_flushbdrv_pdiscardbdrv_readv_vmstatebdrv_writev_vmstate
What I am not sure:
* bdrv_drain_all_begin - bdrv_drain_all_end - bdrv_drain_all: these were classified as GS, because thay are always called from the main loop. Should they go in this new category?
* how should I interpret "all the callers of BDRV_POLL_WHILE"? Meaning, if I consider also the callers of the callers, we end up covering much much more functions. Should I only consider the direct callers (ie the above)?
Emanuele
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
Quoted 42 lines · Emanuele Giuseppe Esposito
Quoted 3 lines
That said, even if they are a different category, I think it makes sense to leave them in the same header file as I/O functions, because I/O functions are locked out between
drained_beginanddrained_end.Proposed category description:
/*
* "Global OR I/O" API functions. These functions can run without
* the BQL, but only in one specific iothread/main loop. *
* More specifically, these functions useBDRV_POLL_WHILE(bs), which
* requires the caller to be either in the main thread and hold
* the BlockdriverState (bs) AioContext lock, or directly in the
* home thread that runs the bs AioContext. Calling them from
* another thread in another AioContext would cause deadlocks. *
* Therefore, these functions are not proper I/O, because they
* can't run in *any* iothreads, but only in a specific one. */Functions that will surely go under this category:
BDRV_POLL_WHILEbdrv_parent_drained_begin_singlebdrv_parent_drained_end_singlebdrv_drain_pollbdrv_drained_beginbdrv_do_drained_begin_quiescebdrv_subtree_drained_beginbdrv_drained_endbdrv_drained_end_no_pollbdrv_subtree_drained_end(all
generated_co_wrapper)bdrv_truncatebdrv_checkbdrv_invalidate_cachebdrv_flushbdrv_pdiscardbdrv_readv_vmstatebdrv_writev_vmstate
Quoted 5 lines
What I am not sure:
*
bdrv_drain_all_begin-bdrv_drain_all_end-bdrv_drain_all: these were classified as GS, because thay are always called from the main loop. Should they go in this new category?
1) They look at the list of BDS's, and 2) you can't in general be sure that all BDS's are in *your* AioContext if you call them from a specific AioContext.
So they should be GS.
Quoted 4 lines
* how should I interpret "all the callers of
BDRV_POLL_WHILE"? Meaning, if I consider also the callers of the callers, we end up covering much much more functions. Should I only consider the direct callers (ie the above)?
In general it is safe to make a function GS even if it is potentially "GS or I/O", because that _reduces_ the number of places you can call it from. It's likewise safe to make it I/O-only, but probably it makes less sense.
Paolo
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Emanuele Giuseppe EspositoQuoted 4 lines · Emanuele Giuseppe Esposito
+ *
+ * This function should never be used in the block layer, please
+ * instead refer toqemu_in_main_thread().
This function should never be used in the block layer, because unit tests, block layer tools and qemu-storage-daemon do not have a BQL. Please instead refer to qemu_in_main_thread().
Quoted 8 lines
(quoted diff) +8
-0+ +/** + * qemu_in_main_thread: same as qemu_mutex_iothread_locked when + * softmmu/cpus.c implementation is linked. Otherwise this function + * checks that the current AioContext is the global AioContext + * (main loop). + * + * This is useful when checking that the BQL is held as a
Review
Quoted 10 lines
(quoted diff) +10
-0+ * replacement of qemu_mutex_iothread_locked() usege in the + * block layer, since the former returns false when invoked by + * unit tests or other users like qemu-storage-daemon that end + * up using the stubs/iothread-lock.c implementation. + * + * This function should only be used in the block layer. + * Use this function to determine whether it is possible to safely + * access the block layer's globals. + */ +bool qemu_in_main_thread(void);
I think the reference to qemu_mutex_iothread_locked() complicates things. It's enough to explain the different policies in my opinion:
/**
* qemu_in_main_thread: return whether it's possible to safely access
* the global state of the block layer.
*
* Global state of the block layer is not accessible from I/O threads
* or worker threads; only from threads that "own" the default
* AioContext that qemu_get_aio_context() returns. For tests, block
* layer tools and qemu-storage-daemon there is a designated thread that
* runs the event loop for qemu_get_aio_context(), and that is the
* main thread.
*
* For emulators, however, any thread that holds the BQL can act
* as the block layer main thread; this will be any of the actual
* main thread, the vCPU threads or the RCU thread.
*
* For clarity, do not use this function outside the block layer. */
Paolo
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
In reply to Paolo BonziniAm 31.01.2022 um 16:49 hat Paolo Bonzini geschrieben:
Quoted 18 lines
Quoted 5 lines
Quoted 2 lines
However, commit
5f50be9b5810293141bb53cfd0cb46e765367d56changed the stub to return false for a specific reason.I must admit I don't really understand the reasoning there:
Quoted 7 lines
With this change, the stub
qemu_mutex_iothread_locked()must be changed from true to false. The previous value of true was needed because the main thread did not have an AioContext in the thread-local variable, but now it does have one.This explains why it _can_ be changed to false for this caller, but not why it _must_ be changed.
See above: because it returns the wrong value for all threads except one, and there are better ways to do a meaningful check in that one thread: using
qemu_get_current_aio_context(), which is whataio_co_enterdid at the time andqemu_in_main_thread()does with Emanuele's change.
Quoted 5 lines
Quoted 3 lines
So is the problem with the unit tests really just that they never call
qemu_init_main_loop()and therefore never set the AioContext for the main thread?No, most of them do (and if some are missing it we can add it).
But if they do, why doesn't qemu_get_current_aio_context() already return the right result? In this case, my_aiocontext is set and it should never even call qemu_mutex_iothread_locked() - at least not in any case where qemu_in_main_thread() would return true.
So provided that qemu_init_main_loop() is called, both functions should be equivalent and we wouldn't need a second one.
Kevin
Re: [PATCH v6 01/33] main-loop.h: introduce qemu_in_main_thread()
Am 01.02.2022 um 12:55 hat Kevin Wolf geschrieben:
Quoted 25 lines
Am 31.01.2022 um 16:49 hat Paolo Bonzini geschrieben:
Quoted 18 lines
Quoted 5 lines
Quoted 2 lines
However, commit
5f50be9b5810293141bb53cfd0cb46e765367d56changed the stub to return false for a specific reason.I must admit I don't really understand the reasoning there:
Quoted 7 lines
With this change, the stub
qemu_mutex_iothread_locked()must be changed from true to false. The previous value of true was needed because the main thread did not have an AioContext in the thread-local variable, but now it does have one.This explains why it _can_ be changed to false for this caller, but not why it _must_ be changed.
See above: because it returns the wrong value for all threads except one, and there are better ways to do a meaningful check in that one thread: using
qemu_get_current_aio_context(), which is whataio_co_enterdid at the time andqemu_in_main_thread()does with Emanuele's change.Quoted 5 lines
Quoted 3 lines
So is the problem with the unit tests really just that they never call
qemu_init_main_loop()and therefore never set the AioContext for the main thread?No, most of them do (and if some are missing it we can add it).
Quoted 7 lines
But if they do, why doesn't
qemu_get_current_aio_context()already return the right result? In this case,my_aiocontextis set and it should never even callqemu_mutex_iothread_locked()- at least not in any case whereqemu_in_main_thread()would return true.So provided that
qemu_init_main_loop()is called, both functions should be equivalent and we wouldn't need a second one.
Sorry, I was confused and comparing the wrong two functions. qemu_get_current_aio_context() does return the right result and it's exactly what qemu_in_main_thread() uses. So yes, it's right and it's different from qemu_mutex_iothread_locked().
It would be less confusing if qemu_get_current_aio_context() used qemu_in_main_thread() (with two different implementations of qemu_in_main_thread() for the system emulator and tools) instead of the other way around, but I guess that's harder to implement because we would need a different way to figure out whether we're in the main thread, at least as long as my_aiocontext is static and can't be accessed in stubs. We could probably make it public, though.
Kevin
Re: [PATCH v6 21/33] block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate
In reply to Kevin WolfQuoted 13 lines · Kevin Wolf
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6629
Quoted 8 lines
block.c +8
-0+int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) +{ +Error *local_err =NULL ; + +if (bs->drv->bdrv_co_invalidate_cache) { + bs->drv->bdrv_co_invalidate_cache (bs, &local_err); +if (local_err) { + bs->open_flags |=BDRV_O_INACTIVE ;This doesn't feel like the right place. The flag is cleared by the caller, so it should also be set again on failure by the caller and not by this function.
Quoted 2 lines
What
bdrv_co_invalidate_cache()could do is assert thatBDRV_O_INACTIVEis cleared when it's called.
Do you think this would be handled more easily into its own series?
In general, the work in this series is more incremental than its size suggests. Perhaps it should be flushed out in smaller pieces.
Paolo
Re: [PATCH v6 21/33] block: move BQL logic of bdrv_co_invalidate_cache in bdrv_activate
Am 02.02.2022 um 18:27 hat Paolo Bonzini geschrieben:
Quoted 14 lines · Kevin Wolf
Quoted 13 lines
block.c int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) · line 6629
Quoted 8 lines
block.c +8
-0+int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) +{ +Error *local_err =NULL ; + +if (bs->drv->bdrv_co_invalidate_cache) { + bs->drv->bdrv_co_invalidate_cache (bs, &local_err); +if (local_err) { + bs->open_flags |=BDRV_O_INACTIVE ;This doesn't feel like the right place. The flag is cleared by the caller, so it should also be set again on failure by the caller and not by this function.
Quoted 7 lines
Quoted 2 lines
What
bdrv_co_invalidate_cache()could do is assert thatBDRV_O_INACTIVEis cleared when it's called.Do you think this would be handled more easily into its own series?
In general, the work in this series is more incremental than its size suggests. Perhaps it should be flushed out in smaller pieces.
Smaller pieces are always easier to handle, so if things make sense independently, splitting them off is usually a good idea.
Kevin
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Paolo BonziniAm 01.02.2022 um 11:30 hat Paolo Bonzini geschrieben:
Quoted 49 lines · Emanuele Giuseppe Esposito
Quoted 42 lines
Quoted 3 lines
That said, even if they are a different category, I think it makes sense to leave them in the same header file as I/O functions, because I/O functions are locked out between
drained_beginanddrained_end.Proposed category description:
/*
* "Global OR I/O" API functions. These functions can run without
* the BQL, but only in one specific iothread/main loop. *
* More specifically, these functions useBDRV_POLL_WHILE(bs), which
* requires the caller to be either in the main thread and hold
* the BlockdriverState (bs) AioContext lock, or directly in the
* home thread that runs the bs AioContext. Calling them from
* another thread in another AioContext would cause deadlocks. *
* Therefore, these functions are not proper I/O, because they
* can't run in *any* iothreads, but only in a specific one. */Functions that will surely go under this category:
BDRV_POLL_WHILEbdrv_parent_drained_begin_singlebdrv_parent_drained_end_singlebdrv_drain_pollbdrv_drained_beginbdrv_do_drained_begin_quiescebdrv_subtree_drained_beginbdrv_drained_endbdrv_drained_end_no_pollbdrv_subtree_drained_end(all
generated_co_wrapper)bdrv_truncatebdrv_checkbdrv_invalidate_cachebdrv_flushbdrv_pdiscardbdrv_readv_vmstatebdrv_writev_vmstateQuoted 5 lines
What I am not sure:
*
bdrv_drain_all_begin-bdrv_drain_all_end-bdrv_drain_all: these were classified as GS, because thay are always called from the main loop. Should they go in this new category?
Quoted 5 lines
1) They look at the list of BDS's, and 2) you can't in general be sure that all BDS's are in *your* AioContext if you call them from a specific AioContext.
So they should be GS.
I agree, calling drain_all functions can only work from the main thread, so they are GS.
Quoted 8 lines
Quoted 4 lines
* how should I interpret "all the callers of
BDRV_POLL_WHILE"? Meaning, if I consider also the callers of the callers, we end up covering much much more functions. Should I only consider the direct callers (ie the above)?In general it is safe to make a function GS even if it is potentially "GS or I/O", because that _reduces_ the number of places you can call it from. It's likewise safe to make it I/O-only, but probably it makes less sense.
Basically, we have a hierarchy of categories where you can always call functions in other categories with less restrictions, but never the opposite direction.
1. Common functions
2. I/O functions
3. I/O or GS functions
4. GS functions
So common functions must never call any of the other categories. Global state functions can call functions in any category. And "I/O or GS" functions like BDRV_POLL_WHILE() can be called by other "I/O or GS" or just GS functions, but if it's ever (directly or indirectly) called by an I/O or common function, that would be a bug.
Kevin
Re: [PATCH v6 13/33] include/block/blockjob.h: global state API
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 27 lines
blockjob functions run always under the BQL lock.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>--- include/block/blockjob.h | 9 +++++++++ 1 file changed, 9 insertions(+)include/block/blockjob.h +7
-0@@ -74,6 +74,13 @@ typedef struct BlockJob {GSList *nodes; } BlockJob; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/** * block_job_next: * @job: A block job, or %NULL.
include/block/blockjob.h BlockJobInfo *block_job_query(BlockJob *job, Error **errp); · line 169
Quoted 8 lines
include/block/blockjob.h +2
-0@@ -155,6 +162,8 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp);*/ void block_job_iostatus_reset (BlockJob *job); +/* Common functions that are neither I/O nor Global State */ +/** * block_job_is_internal: * @job: The job to determine if it is user-visible or not.
It's a bit random to comment on it for this patch specifically, but I feel that the comments that separate different categories of interfaces in a single file are not very easy to visually register.
I don't think we're doing this anywhere yet, but I wonder if it wouldn't be helpful to use a comment style like this which gives more visibility to the start and end of sections:
/***********************************************************************
* Common functions that are neither I/O nor Global State
*/
Not sure what checkpatch thinks about it either... ;-)
Kevin
Re: [PATCH v6 05/33] include/sysemu/block-backend: split header into I/O and global state (GS) API
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 6 lines
Similarly to the previous patches, split
block-backend.hinblock-backend-io.handblock-backend-global-state.hIn addition, remove "
block/block.h" include as it seems it is not necessary anymore, together with "qemu/iov.h"
Quoted 7 lines
block-backend-common.hcontains the structures shared between the two headers, and the functions that can't be categorized as I/O or global state.Assertions are added in the next patch.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>
The same "GS or I/O" category is needed for drain and other polling functions here (mainly the synchronous I/O functions like blk_pread() etc.).
Most of these functions just wrap the bdrv_*() counterpart, so I'm not reviewing them in detail now. I don't expect surprises as long as both interfaces are consistent.
block/block-backend.c struct BlockBackend { · line 84
Quoted 11 lines
block/block-backend.c +1
-0@@ -79,6 +79,7 @@ struct BlockBackend {bool allow_aio_context_change;bool allow_write_beyond_eof; +/* Protected by BQL lock */ NotifierList remove_bs_notifiers, insert_bs_notifiers; QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers;
This should be just "BQL" (like in patch 2) because the L in "BQL" already means "lock", so "BQL lock" would be the "Big QEMU lock lock". (More instances in this patch.)
Kevin
Re: [PATCH v6 09/33] block: introduce assert_bdrv_graph_writable
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 21 lines
We want to be sure that the functions that write the child and parent list of a bs are under BQL and drain.
BQL prevents from concurrent writings from the GS API, while drains protect from I/O.
TODO: drains are missing in some functions using this assert. Therefore a proper assertion will fail. Because adding drains requires additional discussions, they will be added in future series.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>block/io.c +0
-0@@ -734,6 +734,17 @@ void bdrv_drain_all(void)bdrv_drain_all_end (); }
block/io.c void bdrv_drain_all(void) · line 746
Quoted 10 lines
block/io.c +10
-0+void assert_bdrv_graph_writable (BlockDriverState *bs) +{ +/* +* TODO: this function is incomplete. Because the users of this +* assert lack the necessary drains, check only for BQL. +* Once the necessary drains are added, +* assert also for qatomic_read(&bs->quiesce_counter) > 0 +*/ +assert (qemu_in_main_thread ()); +}
This looks like a trivial function that could easily be static inline.
Kevin
Re: [PATCH v6 22/33] block/coroutines: I/O API
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 15 lines
block coroutines functions run in different aiocontext, and are not protected by the BQL. Therefore are I/O.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>--- block/coroutines.h | 6 ++++++ 1 file changed, 6 insertions(+)block/coroutines.h +0
-0
block/coroutines.h line 37
Quoted 9 lines
block/coroutines.h +6
-0@@ -29,6 +29,12 @@/* For blk_bs() in generated block/block-gen.c */ #include "sysemu/block-backend.h" +/* +* I/O API functions. These functions are thread-safe. +* +* See include/block/block-io.h for more information about +* the I/O API. +*/
Please keep an empty line between #include and the rest.
As discussed in patch 2, this file will need some rearrangement. Generally, the coroutine_fn is indeed I/O, but the generated_co_wrapper is mixed "I/O or GS" and requires the BQL or a specific iothread.
Kevin
Re: [PATCH v6 31/33] include/qemu/job.h: introduce job->pre_run() and use it in amend
In reply to Emanuele Giuseppe EspositoAm 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 8 lines
Introduce .pre_run() job callback. This cb will run in
job_start, before the coroutine is created and runsrun()in the job aiocontext.Therefore, .pre_run() always runs in the main loop.
We can use this function together withclean()cb to replacebdrv_child_refresh_permsinblock_crypto_amend_options_generic_luks(), since that function can also be called from an iothread via .bdrv_co_amend().
How is this different from having the same code in the function that creates the job, i.e. qmp_x_blockdev_amend()?
Almost all block jobs have some setup code in the function that creates the job instead of doing everything in .run(), precisely because they know this code runs in the main thread.
Is amend really so different from the other block jobs in this respect that it needs a different solution?
Quoted 8 lines
In addition, doing so we check for permissions in all bdrv in amend, not only crypto.
.pre_run() and .clean() take care of calling
bdrv_amend_pre_run()andbdrv_amend_clean()respectively, to set up driver-specific flags and allow the crypto driver to temporarly provide the WRITE perm toqcrypto_block_amend_options().
Quoted 5 lines
.pre_run() is not yet invoked by
job_start, but .clean() is. This is not a problem, since it will just be a redundant check and crypto will have theupdate->keysflag == false anyways.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>
I find the way how you split the patches a bit confusing because the patches aren't self-contained, but always refer to what the code will do in the future, because after the patch it's dead code that isn't even theoretically called until the final patch comes in.
Can we restructure this a bit? First a patch that adds a new JobDriver callback (if really needed) along with the actual calls for it and everything else that needs to be touched in the generic job infrastructure. Second, new BlockDriver callbacks with all of the plumbing code. Third, the amend job changes with a patch that doesn't touch anything but block/amend.c and potentially block/crypto.c (the latter could also be another separate patch).
This change with three or four patches could also be a candidate to be split out into a separate smaller series.
Kevin
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 6 lines
Each function in the GS API will have an assertion, checking that it is always running under BQL.
I/O functions are instead thread safe (or so should be), meaning that they *can* run under BQL, but also in an iothread in another AioContext. Therefore they do not provide any assertion, and need to be audited manually to verify the correctness.
I wonder if we could actually do something to catch at least some kinds of bugs. The first conclusion from thinking about it is that we probably shouldn't open-code assert(qemu_in_main_thread()) everywhere, but have a macro or inline function for each category to be called in each function.
So an IO_CODE() macro could increase a counter in the coroutine object (that is decreased again at the end of the function with g_auto), and then GLOBAL_STATE_CODE() could not only assert that we're holding the BQL, but also that the counter is still 0, i.e. it is not (indirectly) called by an I/O function.
We may want to enable this only in debug builds, but maybe still worth a thought anyway?
Kevin
Re: [PATCH v6 02/33] include/block/block: split header into I/O and global state API
In reply to Kevin WolfQuoted 64 lines · Kevin Wolf
Am 01.02.2022 um 11:30 hat Paolo Bonzini geschrieben:
Quoted 49 lines · Emanuele Giuseppe Esposito
Quoted 42 lines
Quoted 3 lines
That said, even if they are a different category, I think it makes sense to leave them in the same header file as I/O functions, because I/O functions are locked out between
drained_beginanddrained_end.Proposed category description:
/*
* "Global OR I/O" API functions. These functions can run without
* the BQL, but only in one specific iothread/main loop. *
* More specifically, these functions useBDRV_POLL_WHILE(bs), which
* requires the caller to be either in the main thread and hold
* the BlockdriverState (bs) AioContext lock, or directly in the
* home thread that runs the bs AioContext. Calling them from
* another thread in another AioContext would cause deadlocks. *
* Therefore, these functions are not proper I/O, because they
* can't run in *any* iothreads, but only in a specific one. */Functions that will surely go under this category:
BDRV_POLL_WHILEbdrv_parent_drained_begin_singlebdrv_parent_drained_end_singlebdrv_drain_pollbdrv_drained_beginbdrv_do_drained_begin_quiescebdrv_subtree_drained_beginbdrv_drained_endbdrv_drained_end_no_pollbdrv_subtree_drained_end(all
generated_co_wrapper)bdrv_truncatebdrv_checkbdrv_invalidate_cachebdrv_flushbdrv_pdiscardbdrv_readv_vmstatebdrv_writev_vmstateQuoted 5 lines
What I am not sure:
*
bdrv_drain_all_begin-bdrv_drain_all_end-bdrv_drain_all: these were classified as GS, because thay are always called from the main loop. Should they go in this new category?Quoted 5 lines
1) They look at the list of BDS's, and 2) you can't in general be sure that all BDS's are in *your* AioContext if you call them from a specific AioContext.
So they should be GS.
I agree, calling
drain_allfunctions can only work from the main thread, so they are GS.Quoted 5 lines
Quoted 4 lines
* how should I interpret "all the callers of
BDRV_POLL_WHILE"? Meaning, if I consider also the callers of the callers, we end up covering much much more functions. Should I only consider the direct callers (ie the above)?
Quoted 7 lines
Quoted 3 lines
In general it is safe to make a function GS even if it is potentially "GS or I/O", because that _reduces_ the number of places you can call it from. It's likewise safe to make it I/O-only, but probably it makes less sense.
Basically, we have a hierarchy of categories where you can always call functions in other categories with less restrictions, but never the opposite direction.
Added in the respective category documentation:
Quoted 1 line
1. Common functions
* These functions must never call any function from other categories
* (I/O, "I/O or GS", Global State) except this one, but can be invoked by
* all of them.
Quoted 1 line
2. I/O functions
* These functions can only call functions from I/O and common categories,
* but can be invoked by GS, "I/O or GS" and I/O APIs.
Quoted 1 line
3. I/O or GS functions
* These functions can call any function from I/O, common and this
* categories, but must be invoked only by other "I/O or GS" and GS APIs.
Quoted 1 line
4. GS functions
* These functions can call any function from this and other categories
* (I/O, "I/O or GS", common), but must be invoked only by other GS APIs.
Emanuele
Quoted 9 lines
So common functions must never call any of the other categories. Global state functions can call functions in any category. And "I/O or GS" functions like
BDRV_POLL_WHILE()can be called by other "I/O or GS" or just GS functions, but if it's ever (directly or indirectly) called by an I/O or common function, that would be a bug.Kevin
Re: [PATCH v6 13/33] include/block/blockjob.h: global state API
In reply to Kevin WolfQuoted 45 lines · Kevin Wolf
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 27 lines
blockjob functions run always under the BQL lock.
Signed-off-by Emanuele Giuseppe Esposito <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>--- include/block/blockjob.h | 9 +++++++++ 1 file changed, 9 insertions(+)include/block/blockjob.h +7
-0@@ -74,6 +74,13 @@ typedef struct BlockJob {GSList *nodes; } BlockJob; +/* +* Global state (GS) API. These functions run under the BQL lock. +* +* See include/block/block-global-state.h for more information about +* the GS API. +*/ +/** * block_job_next: * @job: A block job, or %NULL. include/block/blockjob.h BlockJobInfo *block_job_query(BlockJob *job, Error **errp); · line 169
Quoted 8 lines
include/block/blockjob.h +2
-0@@ -155,6 +162,8 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp);*/ void block_job_iostatus_reset (BlockJob *job); +/* Common functions that are neither I/O nor Global State */ +/** * block_job_is_internal: * @job: The job to determine if it is user-visible or not. It's a bit random to comment on it for this patch specifically, but I feel that the comments that separate different categories of interfaces in a single file are not very easy to visually register.
I don't think we're doing this anywhere yet, but I wonder if it wouldn't be helpful to use a comment style like this which gives more visibility to the start and end of sections:
Quoted 5 lines
/***********************************************************************
* Common functions that are neither I/O nor Global State
*/Not sure what checkpatch thinks about it either... ;-)
Checkpatch does not like that:
WARNING: Block comments use a leading /* on a separate line
+/**************************************************************
But adding it like that would be ok
+/*
+ **************************************************************
I honestly don't find it very beautiful, but let me know if I should add it.
Emanuele
Quoted 3 lines
Kevin
Re: [PATCH v6 31/33] include/qemu/job.h: introduce job->pre_run() and use it in amend
In reply to Kevin WolfQuoted 13 lines · Kevin Wolf
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 8 lines
Introduce .pre_run() job callback. This cb will run in
job_start, before the coroutine is created and runsrun()in the job aiocontext.Therefore, .pre_run() always runs in the main loop.
We can use this function together withclean()cb to replacebdrv_child_refresh_permsinblock_crypto_amend_options_generic_luks(), since that function can also be called from an iothread via .bdrv_co_amend().How is this different from having the same code in the function that creates the job, i.e.
qmp_x_blockdev_amend()?
Quoted 7 lines
Almost all block jobs have some setup code in the function that creates the job instead of doing everything in .run(), precisely because they know this code runs in the main thread.
Is amend really so different from the other block jobs in this respect that it needs a different solution?
Are you suggesting to simply call .bdrv_amend_pre_run before job_start() and just leave JobDriver .clean() to call .bdrv_amend_clean?
Yes, that will work too. I will delete .pre_run().
Quoted 19 lines
Quoted 8 lines
In addition, doing so we check for permissions in all bdrv in amend, not only crypto.
.pre_run() and .clean() take care of calling
bdrv_amend_pre_run()andbdrv_amend_clean()respectively, to set up driver-specific flags and allow the crypto driver to temporarly provide the WRITE perm toqcrypto_block_amend_options().Quoted 5 lines
.pre_run() is not yet invoked by
job_start, but .clean() is. This is not a problem, since it will just be a redundant check and crypto will have theupdate->keysflag == false anyways.Signed-off-by Emanuele Giuseppe Esposito <[email protected]>I find the way how you split the patches a bit confusing because the patches aren't self-contained, but always refer to what the code will do in the future, because after the patch it's dead code that isn't even theoretically called until the final patch comes in.
Quoted 7 lines
Can we restructure this a bit? First a patch that adds a new JobDriver callback (if really needed) along with the actual calls for it and everything else that needs to be touched in the generic job infrastructure. Second, new BlockDriver callbacks with all of the plumbing code. Third, the amend job changes with a patch that doesn't touch anything but
block/amend.cand potentiallyblock/crypto.c(the latter could also be another separate patch).
It is more or less what also Hanna suggested, I have it for the next version.
Quoted 3 lines
This change with three or four patches could also be a candidate to be split out into a separate smaller series.
Makes sense.
Emanuele
Quoted 3 lines
Kevin
Quoted 13 lines · Kevin Wolf
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 6 lines
Each function in the GS API will have an assertion, checking that it is always running under BQL.
I/O functions are instead thread safe (or so should be), meaning that they *can* run under BQL, but also in an iothread in another AioContext. Therefore they do not provide any assertion, and need to be audited manually to verify the correctness.I wonder if we could actually do something to catch at least some kinds of bugs. The first conclusion from thinking about it is that we probably shouldn't open-code assert(qemu_in_main_thread()) everywhere, but have a macro or inline function for each category to be called in each function.
Quoted 8 lines
So an
IO_CODE()macro could increase a counter in the coroutine object (that is decreased again at the end of the function withg_auto), and thenGLOBAL_STATE_CODE()could not only assert that we're holding the BQL, but also that the counter is still 0, i.e. it is not (indirectly) called by an I/O function.We may want to enable this only in debug builds, but maybe still worth a thought anyway?
I don't understand what is the point of the counter, do you want to use it as a boolean flag? Would a single counter work in a multi-threaded context? Shouldn't we have it per-thread? And why you increase it only in coroutines?
Emanuele
Am 08.02.2022 um 12:42 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 19 lines
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 6 lines
Each function in the GS API will have an assertion, checking that it is always running under BQL.
I/O functions are instead thread safe (or so should be), meaning that they *can* run under BQL, but also in an iothread in another AioContext. Therefore they do not provide any assertion, and need to be audited manually to verify the correctness.I wonder if we could actually do something to catch at least some kinds of bugs. The first conclusion from thinking about it is that we probably shouldn't open-code assert(qemu_in_main_thread()) everywhere, but have a macro or inline function for each category to be called in each function.
So an
IO_CODE()macro could increase a counter in the coroutine object (that is decreased again at the end of the function withg_auto), and thenGLOBAL_STATE_CODE()could not only assert that we're holding the BQL, but also that the counter is still 0, i.e. it is not (indirectly) called by an I/O function.
Quoted 5 lines
Quoted 2 lines
We may want to enable this only in debug builds, but maybe still worth a thought anyway?
I don't understand what is the point of the counter, do you want to use it as a boolean flag?
It would only be checked as a boolean flag, but it needs to be a counter because of nesting where e.g. one I/O function calls another I/O function.
Quoted 2 lines
Would a single counter work in a multi-threaded context? Shouldn't we have it per-thread? And why you increase it only in coroutines?
I don't mean increasing it only in coroutine context, but having a per-coroutine counter, including the leader coroutine which exists for non-coroutine context in every thread.
Kevin
Quoted 33 lines · Kevin Wolf
Am 08.02.2022 um 12:42 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 19 lines
Am 21.01.2022 um 18:05 hat Emanuele Giuseppe Esposito geschrieben:
Quoted 6 lines
Each function in the GS API will have an assertion, checking that it is always running under BQL.
I/O functions are instead thread safe (or so should be), meaning that they *can* run under BQL, but also in an iothread in another AioContext. Therefore they do not provide any assertion, and need to be audited manually to verify the correctness.I wonder if we could actually do something to catch at least some kinds of bugs. The first conclusion from thinking about it is that we probably shouldn't open-code assert(qemu_in_main_thread()) everywhere, but have a macro or inline function for each category to be called in each function.
So an
IO_CODE()macro could increase a counter in the coroutine object (that is decreased again at the end of the function withg_auto), and thenGLOBAL_STATE_CODE()could not only assert that we're holding the BQL, but also that the counter is still 0, i.e. it is not (indirectly) called by an I/O function.Quoted 5 lines
Quoted 2 lines
We may want to enable this only in debug builds, but maybe still worth a thought anyway?
I don't understand what is the point of the counter, do you want to use it as a boolean flag?
It would only be checked as a boolean flag, but it needs to be a counter because of nesting where e.g. one I/O function calls another I/O function.
Quoted 7 lines
Quoted 2 lines
Would a single counter work in a multi-threaded context? Shouldn't we have it per-thread? And why you increase it only in coroutines?
I don't mean increasing it only in coroutine context, but having a per-coroutine counter, including the leader coroutine which exists for non-coroutine context in every thread.
As agreed also on IRC, while we wait also additional feedback from others on the counter logic, I am going to add GLOBAL_STATE_CODE, IO_CODE and IO_OR_GS_CODE macros in the respective functions.
GLOBAL_STATE_CODE will just replace the assert(qemu_in_main_thread()), while IO_CODE and IO_OR_GS_CODE will be nop.
This will also visually help understanding in which category each function is, without looking at the header.
In the future we can extend the macro to support also additional logic, like counters.
Emanuele