block: keep AioContext pointer in BlockBackend
v2 cover block: keep AioContext pointer in BlockBackend
The first patches clean up usage of BlockBackend and changing of its (root's) aio contexts; the last patch is an update of Stefan's previous version rebasing on top of current master. The biggest change from the RFC is that blk_insert_bs callers are responsible to put the BB and BDS on the same context before calling it.
This fixes the crash triggered by "change" a scsi-cd on a virtio-scsi dataplane device.
The new assertions in block-backend.c ensures we won't have a conflict pair of BlockBackend users from different contextes.
Fam Zheng (4):
blockdev-mirror: Sanity check before moving target_bs AioContext
blockdev: Move BDS AioContext before inserting to BB
block: Introduce and make use of blk_new_with_root
migration: Set correct AioContext to BlockBackend
Stefan Hajnoczi (1):
block: keep AioContext pointer in BlockBackend
block/backup.c | 3 +-- block/block-backend.c | 48 +++++++++++++++++++++++++++++----------- block/commit.c | 12 ++++------ block/mirror.c | 3 +-- blockdev.c | 42 ++++++++++++++++++++++++++--------- blockjob.c | 3 +-- hmp.c | 3 +-- hw/core/qdev-properties-system.c | 3 +-- include/sysemu/block-backend.h | 1 + migration/block.c | 1 + nbd/server.c | 3 +-- tests/test-blockjob.c | 3 +-- 12 files changed, 79 insertions(+), 46 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/5 blockdev-mirror: Sanity check before moving target_bs AioContext
Similar to blockdev-backup, if the target was already moved to a different AioContext, bad things can happen. This happens when the target belongs to a data plane device. It's a very unlikely case, but let's check it anyway.
--- blockdev.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)
blockdev.c +24 -11
@@ -3281,6 +3281,21 @@ BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)return bdrv_named_nodes_list (errp); } +static void blockdev_set_aio_context (BlockDriverState *bs,AioContext *ctx, +Error **errp) +{ +if (bdrv_get_aio_context (bs) != ctx) { +if (!bdrv_has_blk (bs)) { +/* The target BDS is not attached, we can safely move it to another +* AioContext. */ +bdrv_set_aio_context (bs, ctx); + }else { +error_setg (errp,"Target is attached to a different thread from " +"source." ); + } + } +} +void do_blockdev_backup (BlockdevBackup *backup,BlockJobTxn *txn,Error **errp) {BlockDriverState *bs;
@@ -3317,16 +3332,10 @@ void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, Error **errp) goto out; } -if (bdrv_get_aio_context (target_bs) != aio_context) { -if (!bdrv_has_blk (target_bs)) { -/* The target BDS is not attached, we can safely move it to another -* AioContext. */ -bdrv_set_aio_context (target_bs, aio_context); - }else { -error_setg (errp,"Target is attached to a different thread from " -"source." ); - goto out; - } +blockdev_set_aio_context (target_bs, aio_context, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto out; } backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync, NULL, backup->compress, backup->on_source_error,
@@ -3538,7 +3547,11 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) goto out; } -bdrv_set_aio_context (target_bs, aio_context); +blockdev_set_aio_context (target_bs, aio_context, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto out; + } blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, arg->has_replaces, arg->replaces, arg->sync,
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 1/5] blockdev-mirror: Sanity check before moving target_bs AioContext
Quoted 4 lines · Fam Zheng
Similar to blockdev-backup, if the target was already moved to a different AioContext, bad things can happen. This happens when the target belongs to a data plane device. It's a very unlikely case, but let's check it anyway.
You didn't implement it for blockdev-mirror, though, but for drive-mirror (which I don't think needs this check).
Max
Quoted 67 lines
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)blockdev.c +24
-11@@ -3281,6 +3281,21 @@ BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)return bdrv_named_nodes_list (errp); } +static void blockdev_set_aio_context (BlockDriverState *bs,AioContext *ctx, +Error **errp) +{ +if (bdrv_get_aio_context (bs) != ctx) { +if (!bdrv_has_blk (bs)) { +/* The target BDS is not attached, we can safely move it to another +* AioContext. */ +bdrv_set_aio_context (bs, ctx); + }else { +error_setg (errp,"Target is attached to a different thread from " +"source." ); + } + } +} +void do_blockdev_backup (BlockdevBackup *backup,BlockJobTxn *txn,Error **errp) {BlockDriverState *bs;@@ -3317,16 +3332,10 @@ void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, Error **errp) goto out; } -if (bdrv_get_aio_context (target_bs) != aio_context) { -if (!bdrv_has_blk (target_bs)) { -/* The target BDS is not attached, we can safely move it to another -* AioContext. */ -bdrv_set_aio_context (target_bs, aio_context); - }else { -error_setg (errp,"Target is attached to a different thread from " -"source." ); - goto out; - } +blockdev_set_aio_context (target_bs, aio_context, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto out; } backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync, NULL, backup->compress, backup->on_source_error,@@ -3538,7 +3547,11 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) goto out; } -bdrv_set_aio_context (target_bs, aio_context); +blockdev_set_aio_context (target_bs, aio_context, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto out; + } blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, arg->has_replaces, arg->replaces, arg->sync,
Re: [Qemu-devel] [PATCH v2 1/5] blockdev-mirror: Sanity check before moving target_bs AioContext
Quoted 8 lines · Max Reitz
Quoted 4 lines · Fam Zheng
Similar to blockdev-backup, if the target was already moved to a different AioContext, bad things can happen. This happens when the target belongs to a data plane device. It's a very unlikely case, but let's check it anyway.
You didn't implement it for blockdev-mirror, though, but for drive-mirror (which I don't think needs this check).
Ugh, will fix.
PATCH v2 2/5 blockdev: Move BDS AioContext before inserting to BB
In reply to Fam ZhengThe callers made sure the BDS has no BB attached before, so blk is becoming the sole owner. It is necessary to move the BDS to the right AioContext before inserting it, to keep them in sync.
--- blockdev.c | 7 +++++++ 1 file changed, 7 insertions(+)
blockdev.c +7 -0
@@ -2478,6 +2478,7 @@ out:static void qmp_blockdev_insert_anon_medium (BlockBackend *blk,BlockDriverState *bs,Error **errp) { +AioContext *ctx;bool has_device;/* For BBs without a device, we can exchange the BDS tree at will */
@@ -2498,6 +2499,12 @@ static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,return ; } + ctx =blk_get_aio_context (blk); +if (ctx !=bdrv_get_aio_context (bs)) { +aio_context_acquire (ctx); +bdrv_set_aio_context (bs, ctx); +aio_context_release (ctx); + }blk_insert_bs (blk, bs);if (!blk_dev_has_tray (blk)) {
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 2/5] blockdev: Move BDS AioContext before inserting to BB
Quoted 3 lines · Fam Zheng
The callers made sure the BDS has no BB attached before, so blk is becoming the sole owner. It is necessary to move the BDS to the right AioContext before inserting it, to keep them in sync.
Well, I'm not sure whether it's so simple, though, because you can always have some BB attached down the chain. Imagine a qcow2 BDS named "top" with a backing BDS named "base". Now let's say we already have a BB at "base" (e.g. read-only for an NBD server). Just because "top" does not have a BB does not mean that we can just move the whole tree into another AioContext, because the BB at "base" may depend on the current one.
(Actually, NBD should be able to deal with that case, but other frontends might not.)
Of course, it's possible the other way around, too. You can have a BB at "top" and now want to attach a BB to "base". It's the same issue there, basically. (Actually, it's worse here, because changes in the AioContext only get propagated down the node tree, not up.)
The most important thing however is that unfortunately everything's probably already broken today, so I won't be too pedantic about all of this.
Quoted 18 lines
Signed-off-by Fam Zheng <[email protected]>--- blockdev.c | 7 +++++++ 1 file changed, 7 insertions(+)blockdev.c +1
-0@@ -2478,6 +2478,7 @@ out:static void qmp_blockdev_insert_anon_medium (BlockBackend *blk,BlockDriverState *bs,Error **errp) { +AioContext *ctx;bool has_device;/* For BBs without a device, we can exchange the BDS tree at will */
blockdev.c static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, · line 2503
Quoted 5 lines
blockdev.c +2
-0@@ -2498,6 +2499,12 @@ static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,return ; } + ctx =blk_get_aio_context (blk); +if (ctx !=bdrv_get_aio_context (bs)) {
A bit weird before patch 5 (I assume?) because ctx will always be the main loop's context (and the BB actually doesn't have an inherent AioContext yet).
While I see how this should be done before patch 5, it's actually not quite correct before that patch. But I think it's fine to accept this for the duration of three commits.
blockdev.c static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, · line 2510
Quoted 8 lines
blockdev.c +4
-0+aio_context_acquire (ctx); +bdrv_set_aio_context (bs, ctx); +aio_context_release (ctx); + }blk_insert_bs (blk, bs);if (!blk_dev_has_tray (blk)) {
The reason I'd feel bad about giving an R-b is because of the lack of propagation of the AioContext up the tree. We have to make sure that the BDS really does not have any parents in another AioContext, and that not only includes BBs but also other BDSs.
But the propagation down the tree is not quite flawless either: We do have notifiers which can tell e.g. frontends when the AioContext is changing, but only NBD and block jobs make use of this right now. Also, these are only notifiers, so the frontend cannot prevent the BDS from changing the AioContext, which is probably something that dataplane would require.
So I think we first need some infrastructure which can test whether moving a BDS tree to another AioContext is fine with all of the nodes in the tree (and that includes BBs/frontends). For instance, NBD is always fine with its BDS moving around, but dataplane probably is not.
Only if we know that moving the BDS tree to another context is fine, we should actually do so, and in this case, we need to make sure not just to propagate the new context down, but also up the graph (or we could simply not allow changing the AioContext up the graph).
Max
PATCH v2 3/5 block: Introduce and make use of blk_new_with_root
In reply to Fam ZhengThe idiom of "blk_new() + blk_insert_bs()" is common. Use a new BB interface that does both things to make the coming transition that adds AioContext to BB easier.
The added blk_new_with_ctx doesn't handle the passed ctx and is purely here to avoid a small code churn.
--- block/backup.c | 3 +-- block/block-backend.c | 24 ++++++++++++++++++------ block/commit.c | 12 ++++-------- block/mirror.c | 3 +-- blockjob.c | 3 +-- hmp.c | 3 +-- hw/core/qdev-properties-system.c | 3 +-- include/sysemu/block-backend.h | 1 + nbd/server.c | 3 +-- tests/test-blockjob.c | 3 +-- 10 files changed, 30 insertions(+), 28 deletions(-)
block/backup.c +1 -2
@@ -601,8 +601,7 @@ void backup_start(const char *job_id, BlockDriverState *bs, goto error; } - job->target =blk_new (); -blk_insert_bs (job->target, target); + job->target =blk_new_with_root (target); job->on_source_error = on_source_error; job->on_target_error = on_target_error;
block/block-backend.c +18 -6
@@ -115,12 +115,7 @@ static const BdrvChildRole child_root = { .drained_end = blk_root_drained_end, }; -/* -* Create a new BlockBackend with a reference count of one. -* Store an error through @errp on failure, unless it's null. -* Return the new BlockBackend on success, null on failure. -*/ -BlockBackend *blk_new (void ) +static BlockBackend *blk_new_with_ctx (AioContext *ctx) {BlockBackend *blk;
@@ -139,6 +134,23 @@ BlockBackend *blk_new(void) }/* +* Create a new BlockBackend with a reference count of one. +* Store an error through @errp on failure, unless it's null. +* Return the new BlockBackend on success, null on failure. +*/ +BlockBackend *blk_new (void ) +{ +return blk_new_with_ctx (qemu_get_aio_context ()); +} + +BlockBackend *blk_new_with_root (BlockDriverState *root) +{ +BlockBackend *blk =blk_new_with_ctx (bdrv_get_aio_context (root)); +blk_insert_bs (blk, root); +return blk; +} + +/* * Creates a new BlockBackend, opens a new BlockDriverState, and connects both. * * Just as with bdrv_open(), after having called this function the reference to
block/commit.c +4 -8
@@ -260,11 +260,9 @@ void commit_start(const char *job_id, BlockDriverState *bs, } - s->base =blk_new (); -blk_insert_bs (s->base, base); + s->base =blk_new_with_root (base); - s->top =blk_new (); -blk_insert_bs (s->top, top); + s->top =blk_new_with_root (top); s->active = bs;
@@ -314,11 +312,9 @@ int bdrv_commit(BlockDriverState *bs) } } - src =blk_new (); -blk_insert_bs (src, bs); + src =blk_new_with_root (bs); - backing =blk_new (); -blk_insert_bs (backing, bs->backing->bs); + backing =blk_new_with_root (bs->backing->bs); length =blk_getlength (src);if (length <0 ) {
block/mirror.c +1 -2
@@ -941,8 +941,7 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,return ; } - s->target =blk_new (); -blk_insert_bs (s->target, target); + s->target =blk_new_with_root (target); s->replaces =g_strdup (replaces); s->on_source_error = on_source_error;
blockjob.c +1 -2
@@ -148,8 +148,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,return NULL ; } - blk =blk_new (); -blk_insert_bs (blk, bs); + blk =blk_new_with_root (bs); job =g_malloc0 (driver->instance_size); error_setg(&job->blocker, "block device is in use by block job: %s",
hmp.c +1 -2
@@ -1934,8 +1934,7 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict)if (!blk) {BlockDriverState *bs =bdrv_lookup_bs (NULL , device, &err);if (bs) { - blk = local_blk =blk_new (); -blk_insert_bs (blk, bs); + blk = local_blk =blk_new_with_root (bs); }else { goto fail; }
hw/core/qdev-properties-system.c +1 -2
@@ -78,8 +78,7 @@ static void parse_drive(DeviceState *dev, const char *str, void **ptr,if (!blk) {BlockDriverState *bs =bdrv_lookup_bs (NULL , str,NULL );if (bs) { - blk =blk_new (); -blk_insert_bs (blk, bs); + blk =blk_new_with_root (bs); blk_created = true; } }
include/sysemu/block-backend.h +1 -0
@@ -79,6 +79,7 @@ typedef struct BlockBackendPublic { } BlockBackendPublic;BlockBackend *blk_new (void ); +BlockBackend *blk_new_with_root (BlockDriverState *root);BlockBackend *blk_new_open (const char *filename,const char *reference,QDict *options,int flags,Error **errp);int blk_get_refcnt (BlockBackend *blk);
nbd/server.c +1 -2
@@ -816,8 +816,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,BlockBackend *blk;NBDExport *exp =g_malloc0 (sizeof (NBDExport)); - blk =blk_new (); -blk_insert_bs (blk, bs); + blk =blk_new_with_root (bs);blk_set_enable_write_cache (blk, !writethrough); exp->refcount =1 ;
tests/test-blockjob.c +1 -2
@@ -53,10 +53,9 @@ static BlockJob *do_test_id(BlockBackend *blk, const char *id,* BlockDriverState inserted. */ static BlockBackend *create_blk (const char *name) { -BlockBackend *blk =blk_new ();BlockDriverState *bs =bdrv_new (); +BlockBackend *blk =blk_new_with_root (bs); -blk_insert_bs (blk, bs);bdrv_unref (bs);if (name) {
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 3/5] block: Introduce and make use of blk_new_with_root
Quoted 3 lines · Fam Zheng
The idiom of "
blk_new()+blk_insert_bs()" is common. Use a new BB interface that does both things to make the coming transition that adds AioContext to BB easier.
Indeed. It was called blk_new_with_bs() before and I dropped it. :-)
(The difference and the reason why I dropped it was that blk_new_with_bs() created the BDS, too.)
((And as a side note, this pattern does not come from me dropping blk_new_with_bs(). As far as I remember, I replaced most places where it was used by blk_new_open().))
Quoted 2 lines
The added
blk_new_with_ctxdoesn't handle the passed ctx and is purely here to avoid a small code churn.
Maybe add a "yet" in there, i.e. "does not handle the passed ctx yet"?
Quoted 14 lines
Signed-off-by Fam Zheng <[email protected]>--- block/backup.c | 3 +-- block/block-backend.c | 24 ++++++++++++++++++------ block/commit.c | 12 ++++-------- block/mirror.c | 3 +-- blockjob.c | 3 +-- hmp.c | 3 +-- hw/core/qdev-properties-system.c | 3 +-- include/sysemu/block-backend.h | 1 + nbd/server.c | 3 +-- tests/test-blockjob.c | 3 +-- 10 files changed, 30 insertions(+), 28 deletions(-)
PATCH v2 4/5 migration: Set correct AioContext to BlockBackend
In reply to Fam ZhengThe BB is newly created and it should follow the BDS's current context.
--- migration/block.c | 1 + 1 file changed, 1 insertion(+)
migration/block.c +1 -0
@@ -445,6 +445,7 @@ static void init_blk_migration(QEMUFile *f)BlockDriverState *bs = bmds_bs[i].bs;if (bmds) { +blk_set_aio_context (bmds->blk,bdrv_get_aio_context (bs));blk_insert_bs (bmds->blk, bs);alloc_aio_bitmap (bmds);
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 4/5] migration: Set correct AioContext to BlockBackend
Quoted 6 lines · Fam Zheng
The BB is newly created and it should follow the BDS's current context.
Signed-off-by Fam Zheng <[email protected]>--- migration/block.c | 1 + 1 file changed, 1 insertion(+)
PATCH v2 5/5 block: keep AioContext pointer in BlockBackend
In reply to Fam ZhengFrom: Stefan Hajnoczi <[email protected]>
blk_get/set_aio_context() delegate to BlockDriverState without storing the AioContext pointer in BlockBackend.
There are two flaws:
1. BlockBackend falls back to the QEMU main loop AioContext when there is no root BlockDriverState. This means the drive loses its AioContext during media change and would break dataplane.
2. BlockBackend state used from multiple threads has no lock. Race conditions will creep in as functionality is moved from BlockDriverState to BlockBackend due to the absense of a lock. The monitor cannot access BlockBackend state safely while an IOThread is also accessing the state.
Issue #1 can be triggered by "change" on virtio-scsi dataplane, causing a assertion failure (virtio-blk is fine because medium change is not possible). #2 may be possible with block accounting statistics in BlockBackend but I'm not aware of a crash that can be triggered.
This patch stores the AioContext pointer in BlockBackend and puts newly inserted BlockDriverStates into the AioContext.
--- block/block-backend.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
block/block-backend.c +17 -7
@@ -31,6 +31,7 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);struct BlockBackend {char *name;int refcnt; +AioContext *aio_context;BdrvChild *root;DriveInfo *legacy_dinfo;/* null unless created by drive_new() */ QTAILQ_ENTRY (BlockBackend) link;/* for block_backends */
@@ -121,6 +122,7 @@ static BlockBackend *blk_new_with_ctx(AioContext *ctx) blk =g_new0 (BlockBackend,1 ); blk->refcnt =1 ; + blk->aio_context = ctx;blk_set_enable_write_cache (blk, true);qemu_co_queue_init (&blk->public.throttled_reqs[0 ]);
@@ -510,6 +512,8 @@ void blk_remove_bs(BlockBackend *blk)void blk_insert_bs (BlockBackend *blk,BlockDriverState *bs) {bdrv_ref (bs); + +assert (blk->aio_context ==bdrv_get_aio_context (bs)); blk->root =bdrv_root_attach_child (bs,"root" , &child_root, blk);notifier_list_notify (&blk->insert_bs_notifiers, blk);
@@ -1413,13 +1417,7 @@ void blk_op_unblock_all(BlockBackend *blk, Error *reason)AioContext *blk_get_aio_context (BlockBackend *blk) { -BlockDriverState *bs =blk_bs (blk); - -if (bs) { -return bdrv_get_aio_context (bs); - }else { -return qemu_get_aio_context (); - } +return blk->aio_context; }static AioContext *blk_aiocb_get_aio_context (BlockAIOCB *acb)
@@ -1432,7 +1430,19 @@ void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) {BlockDriverState *bs =blk_bs (blk); + blk->aio_context = new_context; +if (bs) { +AioContext *ctx =bdrv_get_aio_context (bs); + +if (ctx == new_context) { +return ; + } +/* Moving context around happens when a block device is +* enabling/disabling data plane, in which case we own the root BDS and +* it cannot be associated with another AioContext. */ +assert (ctx ==qemu_get_aio_context () || + new_context ==qemu_get_aio_context ());if (blk->public.throttle_state) {throttle_timers_detach_aio_context (&blk->public.throttle_timers); }
Signature · 1 line
2.7.4
Quoted 82 lines · Fam Zheng
From: Stefan Hajnoczi <[email protected]>
blk_get/set_aio_context() delegate to BlockDriverState without storing the AioContext pointer in BlockBackend.
There are two flaws:
1. BlockBackend falls back to the QEMU main loop AioContext when there is no root BlockDriverState. This means the drive loses its AioContext during media change and would break dataplane.
2. BlockBackend state used from multiple threads has no lock. Race conditions will creep in as functionality is moved from BlockDriverState to BlockBackend due to the absense of a lock. The monitor cannot access BlockBackend state safely while an IOThread is also accessing the state.
Issue #1 can be triggered by "change" on virtio-scsi dataplane, causing a assertion failure (virtio-blk is fine because medium change is not possible). #2 may be possible with block accounting statistics in BlockBackend but I'm not aware of a crash that can be triggered.
This patch stores the AioContext pointer in BlockBackend and puts newly inserted BlockDriverStates into the AioContext.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/block-backend.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)block/block-backend.c +7
-7@@ -31,6 +31,7 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);struct BlockBackend {char *name;int refcnt; +AioContext *aio_context;BdrvChild *root;DriveInfo *legacy_dinfo;/* null unless created by drive_new() */ QTAILQ_ENTRY (BlockBackend) link;/* for block_backends */ @@ -121,6 +122,7 @@ static BlockBackend *blk_new_with_ctx(AioContext *ctx) blk =g_new0 (BlockBackend,1 ); blk->refcnt =1 ; + blk->aio_context = ctx;blk_set_enable_write_cache (blk, true);qemu_co_queue_init (&blk->public.throttled_reqs[0 ]);@@ -510,6 +512,8 @@ void blk_remove_bs(BlockBackend *blk)void blk_insert_bs (BlockBackend *blk,BlockDriverState *bs) {bdrv_ref (bs); + +assert (blk->aio_context ==bdrv_get_aio_context (bs)); blk->root =bdrv_root_attach_child (bs,"root" , &child_root, blk);notifier_list_notify (&blk->insert_bs_notifiers, blk);@@ -1413,13 +1417,7 @@ void blk_op_unblock_all(BlockBackend *blk, Error *reason)AioContext *blk_get_aio_context (BlockBackend *blk) { -BlockDriverState *bs =blk_bs (blk); - -if (bs) { -return bdrv_get_aio_context (bs); - }else { -return qemu_get_aio_context (); - } +return blk->aio_context; }static AioContext *blk_aiocb_get_aio_context (BlockAIOCB *acb)@@ -1432,7 +1430,19 @@ void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) {BlockDriverState *bs =blk_bs (blk); + blk->aio_context = new_context; +if (bs) {
block/block-backend.c void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) · line 1445
Quoted 10 lines
block/block-backend.c +10
-0+AioContext *ctx =bdrv_get_aio_context (bs); + +if (ctx == new_context) { +return ; + } +/* Moving context around happens when a block device is +* enabling/disabling data plane, in which case we own the root BDS and +* it cannot be associated with another AioContext. */ +assert (ctx ==qemu_get_aio_context () || + new_context ==qemu_get_aio_context ());
I don't really see the point behind this assertion. I know it's not currently possible, but you are basically asserting that we do not move a BDS tree directly from some non-main-loop context to another non-main-loop context, which in theory sounds completely fine to me.
Based on the "Write code for now and not for the future" rule, I'm fine with this assertion if you can tell me what good it does us now.
The only thing I can personally imagine is that it's a safeguard that we don't try to place a BDS tree into some other AioContext while having ignored that there are still some other BBs attached to it which don't want to agree on that new AioContext. But I think that should rather be fixed before patch 2, i.e. as I said we need an infrastructure which can tell us beforehand (and without failing assertions) whether we can move a certain BDS tree to some other context.
So whether we can move a certain BB from some context to another depends on what the frontend supports, I don't think there is a generic answer we can implement here in the generic BB code. NBD for instance allows any movement; but devices probably only allow movements they have initiated themselves (e.g. dataplane will allow exactly what you describe here with that assertion, and any other device will probably not allow anything but the main loop).
Max
Quoted 4 lines
block/block-backend.c +0
-0if (blk->public.throttle_state) {throttle_timers_detach_aio_context (&blk->public.throttle_timers); }
Quoted 110 lines · Max Reitz
Quoted 82 lines · Fam Zheng
From: Stefan Hajnoczi <[email protected]>
blk_get/set_aio_context() delegate to BlockDriverState without storing the AioContext pointer in BlockBackend.
There are two flaws:
1. BlockBackend falls back to the QEMU main loop AioContext when there is no root BlockDriverState. This means the drive loses its AioContext during media change and would break dataplane.
2. BlockBackend state used from multiple threads has no lock. Race conditions will creep in as functionality is moved from BlockDriverState to BlockBackend due to the absense of a lock. The monitor cannot access BlockBackend state safely while an IOThread is also accessing the state.
Issue #1 can be triggered by "change" on virtio-scsi dataplane, causing a assertion failure (virtio-blk is fine because medium change is not possible). #2 may be possible with block accounting statistics in BlockBackend but I'm not aware of a crash that can be triggered.
This patch stores the AioContext pointer in BlockBackend and puts newly inserted BlockDriverStates into the AioContext.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/block-backend.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)block/block-backend.c +7
-7@@ -31,6 +31,7 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);struct BlockBackend {char *name;int refcnt; +AioContext *aio_context;BdrvChild *root;DriveInfo *legacy_dinfo;/* null unless created by drive_new() */ QTAILQ_ENTRY (BlockBackend) link;/* for block_backends */ @@ -121,6 +122,7 @@ static BlockBackend *blk_new_with_ctx(AioContext *ctx) blk =g_new0 (BlockBackend,1 ); blk->refcnt =1 ; + blk->aio_context = ctx;blk_set_enable_write_cache (blk, true);qemu_co_queue_init (&blk->public.throttled_reqs[0 ]);@@ -510,6 +512,8 @@ void blk_remove_bs(BlockBackend *blk)void blk_insert_bs (BlockBackend *blk,BlockDriverState *bs) {bdrv_ref (bs); + +assert (blk->aio_context ==bdrv_get_aio_context (bs)); blk->root =bdrv_root_attach_child (bs,"root" , &child_root, blk);notifier_list_notify (&blk->insert_bs_notifiers, blk);@@ -1413,13 +1417,7 @@ void blk_op_unblock_all(BlockBackend *blk, Error *reason)AioContext *blk_get_aio_context (BlockBackend *blk) { -BlockDriverState *bs =blk_bs (blk); - -if (bs) { -return bdrv_get_aio_context (bs); - }else { -return qemu_get_aio_context (); - } +return blk->aio_context; }static AioContext *blk_aiocb_get_aio_context (BlockAIOCB *acb)@@ -1432,7 +1430,19 @@ void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) {BlockDriverState *bs =blk_bs (blk); + blk->aio_context = new_context; +if (bs) {block/block-backend.c void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) · line 1445
Quoted 10 lines
block/block-backend.c +10
-0+AioContext *ctx =bdrv_get_aio_context (bs); + +if (ctx == new_context) { +return ; + } +/* Moving context around happens when a block device is +* enabling/disabling data plane, in which case we own the root BDS and +* it cannot be associated with another AioContext. */ +assert (ctx ==qemu_get_aio_context () || + new_context ==qemu_get_aio_context ());I don't really see the point behind this assertion. I know it's not currently possible, but you are basically asserting that we do not move a BDS tree directly from some non-main-loop context to another non-main-loop context, which in theory sounds completely fine to me.
Based on the "Write code for now and not for the future" rule, I'm fine with this assertion if you can tell me what good it does us now.
The only thing I can personally imagine is that it's a safeguard that we don't try to place a BDS tree into some other AioContext while having ignored that there are still some other BBs attached to it which don't want to agree on that new AioContext. But I think that should rather be fixed before patch 2, i.e. as I said we need an infrastructure which can tell us beforehand (and without failing assertions) whether we can move a certain BDS tree to some other context.
Quoted 7 lines
So whether we can move a certain BB from some context to another depends on what the frontend supports, I don't think there is a generic answer we can implement here in the generic BB code. NBD for instance allows any movement; but devices probably only allow movements they have initiated themselves (e.g. dataplane will allow exactly what you describe here with that assertion, and any other device will probably not allow anything but the main loop).
Indeed, you make me think this should be an op blocker (that applies on whole graph).
Quoted 9 lines
Max
Quoted 4 lines
block/block-backend.c +0
-0if (blk->public.throttle_state) {throttle_timers_detach_aio_context (&blk->public.throttle_timers); }
Quoted 10 lines · Fam Zheng
Quoted 7 lines
So whether we can move a certain BB from some context to another depends on what the frontend supports, I don't think there is a generic answer we can implement here in the generic BB code. NBD for instance allows any movement; but devices probably only allow movements they have initiated themselves (e.g. dataplane will allow exactly what you describe here with that assertion, and any other device will probably not allow anything but the main loop).
Indeed, you make me think this should be an op blocker (that applies on whole graph).
The concept of a BDS or BB's AioContext is going to disappear sooner or later, take this into account to decide how much effort to put into fixing this.
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 8 lines · Fam Zheng
Quoted 7 lines
So whether we can move a certain BB from some context to another depends on what the frontend supports, I don't think there is a generic answer we can implement here in the generic BB code. NBD for instance allows any movement; but devices probably only allow movements they have initiated themselves (e.g. dataplane will allow exactly what you describe here with that assertion, and any other device will probably not allow anything but the main loop).
Quoted 6 lines
Quoted 2 lines
Indeed, you make me think this should be an op blocker (that applies on whole graph).
The concept of a BDS or BB's AioContext is going to disappear sooner or later, take this into account to decide how much effort to put into fixing this.
Taking one step back, the whole thing is so broken considering the node reference. Currently, even this is possible:
-drive file=null-co://,if=virtio,id=d0 \
-drive file.file=d0,driver=raw,file.driver=raw,if=virtioAioContext is actually a factor to make it worse. Maybe we should instead think about some generic protection mechanism? Is this in the scope of new op blocker?
Fam