aio: remove AioContext lock
Subsystems (2)
v2 cover aio: remove AioContext lock
v2:
- Add Patch 2 "scsi: assert that callbacks run in the correct AioContext" [Kevin]
- Add Patch 7 "block: remove bdrv_co_lock()" [Eric and Kevin]
- Remove stray goto label in Patch 8 [Kevin]
- Fix "eeked" -> "eked" typo in Patch 10 [Eric]
This series removes the AioContext locking APIs from QEMU. aio_context_acquire() and aio_context_release() are currently only needed to support the locking discipline required by AIO_POLL_WHILE() (except for a stray user that I converted in Patch 1). AIO_POLL_WHILE() doesn't really need the AioContext lock anymore, so it's possible to remove the API. This is a nice simplification because the AioContext locking rules were sometimes tricky or underspecified, leading to many bugs of the years.
This patch series removes these APIs across the codebase and cleans up the documentation/comments that refers to them.
Patch 1 is a AioContext lock user I forgot to convert in my earlier SCSI conversion series.
Patch 2 adds an assertion to the SCSI code to ensure that callbacks are invoked in the correct AioContext.
Patch 3 removes tests for the AioContext lock because they will no longer be needed when the lock is gone.
Patches 4-10 remove the AioContext lock. These can be reviewed by categorizing the call sites into 1. places that take the lock because they call an API that requires the lock (ultimately AIO_POLL_WHILE()) and 2. places that take the lock to protect state. There should be no instances of case 2 left. If you see one, you've found a bug in this patch series!
Patches 11-14 remove comments.
Based-on: [email protected] ("[PATCH v2 0/4] scsi: eliminate AioContext lock") Since SCSI needs to stop relying on the AioContext lock before we can remove
the lock.
Stefan Hajnoczi (14):
virtio-scsi: replace AioContext lock with tmf_bh_lock
scsi: assert that callbacks run in the correct AioContext
tests: remove aio_context_acquire() tests
aio: make aio_context_acquire()/aio_context_release() a no-op
graph-lock: remove AioContext locking
block: remove AioContext locking
block: remove bdrv_co_lock()
scsi: remove AioContext locking
aio-wait: draw equivalence between AIO_WAIT_WHILE() and
AIO_WAIT_WHILE_UNLOCKED()
aio: remove aio_context_acquire()/aio_context_release() API
docs: remove AioContext lock from IOThread docs
scsi: remove outdated AioContext lock comment
job: remove outdated AioContext locking comments
block: remove outdated AioContext locking comments
docs/devel/multiple-iothreads.txt | 45 ++-- include/block/aio-wait.h | 16 +- include/block/aio.h | 17 -- include/block/block-common.h | 3 - include/block/block-global-state.h | 23 +- include/block/block-io.h | 12 +- include/block/block_int-common.h | 2 - include/block/graph-lock.h | 21 +- include/block/snapshot.h | 2 - include/hw/virtio/virtio-scsi.h | 17 +- include/qemu/job.h | 20 -- block.c | 363 ++++----------------------- block/backup.c | 4 +- block/blklogwrites.c | 8 +- block/blkverify.c | 4 +- block/block-backend.c | 33 +-- block/commit.c | 16 +- block/copy-before-write.c | 22 +- block/export/export.c | 22 +- block/export/vhost-user-blk-server.c | 4 - block/graph-lock.c | 44 +--- block/io.c | 45 +--- block/mirror.c | 41 +-- block/monitor/bitmap-qmp-cmds.c | 20 +- block/monitor/block-hmp-cmds.c | 29 --- block/qapi-sysemu.c | 27 +- block/qapi.c | 18 +- block/qcow2.c | 4 +- block/quorum.c | 8 +- block/raw-format.c | 5 - block/replication.c | 72 +----- block/snapshot.c | 26 +- block/stream.c | 12 +- block/vmdk.c | 20 +- block/write-threshold.c | 6 - blockdev.c | 319 +++++------------------ blockjob.c | 30 +-- hw/block/dataplane/virtio-blk.c | 10 - hw/block/dataplane/xen-block.c | 17 +- hw/block/virtio-blk.c | 45 +--- hw/core/qdev-properties-system.c | 9 - hw/scsi/scsi-bus.c | 2 - hw/scsi/scsi-disk.c | 46 ++-- hw/scsi/virtio-scsi.c | 80 +++--- job.c | 16 -- migration/block.c | 33 +-- migration/migration-hmp-cmds.c | 3 - migration/savevm.c | 22 -- net/colo-compare.c | 2 - qemu-img.c | 4 - qemu-io.c | 10 +- qemu-nbd.c | 2 - replay/replay-debugging.c | 4 - system/dma-helpers.c | 3 + tests/unit/test-aio.c | 67 +---- tests/unit/test-bdrv-drain.c | 91 ++----- tests/unit/test-bdrv-graph-mod.c | 26 +- tests/unit/test-block-iothread.c | 31 --- tests/unit/test-blockjob.c | 137 ---------- tests/unit/test-replication.c | 11 - util/async.c | 14 -- util/vhost-user-server.c | 3 - scripts/block-coroutine-wrapper.py | 7 +- tests/qemu-iotests/202 | 2 +- tests/qemu-iotests/203 | 3 +- tests/tsan/suppressions.tsan | 1 - 66 files changed, 340 insertions(+), 1741 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/14 virtio-scsi: replace AioContext lock with tmf_bh_lock
Protect the Task Management Function BH state with a lock. The TMF BH runs in the main loop thread. An IOThread might process a TMF at the same time as the TMF BH is running. Therefore tmf_bh_list and tmf_bh must be protected by a lock.
Run TMF request completion in the IOThread using aio_wait_bh_oneshot(). This avoids more locking to protect the virtqueue and SCSI layer state.
--- include/hw/virtio/virtio-scsi.h | 3 +- hw/scsi/virtio-scsi.c | 62 ++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 22 deletions(-)
include/hw/virtio/virtio-scsi.h +2 -1
@@ -85,8 +85,9 @@ struct VirtIOSCSI {/* * TMFs deferred to main loop BH. These fields are protected by -* virtio_scsi_acquire(). +* tmf_bh_lock. */ +QemuMutex tmf_bh_lock;QEMUBH *tmf_bh; QTAILQ_HEAD(, VirtIOSCSIReq) tmf_bh_list;
hw/scsi/virtio-scsi.c +41 -21
114 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 4/14 aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Stefan Hajnocziaio_context_acquire()/aio_context_release() has been replaced by fine-grained locking to protect state shared by multiple threads. The AioContext lock still plays the role of balancing locking in AIO_WAIT_WHILE() and many functions in QEMU either require that the AioContext lock is held or not held for this reason. In other words, the AioContext lock is purely there for consistency with itself and serves no real purpose anymore.
Stop actually acquiring/releasing the lock in aio_context_acquire()/aio_context_release() so that subsequent patches can remove callers across the codebase incrementally.
I have performed "make check" and qemu-iotests stress tests across x86-64, ppc64le, and aarch64 to confirm that there are no failures as a result of eliminating the lock.
--- util/async.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
util/async.c +2 -2
@@ -725,12 +725,12 @@ void aio_context_unref(AioContext *ctx)void aio_context_acquire (AioContext *ctx) { -qemu_rec_mutex_lock (&ctx->lock); +/* TODO remove this function */ }void aio_context_release (AioContext *ctx) { -qemu_rec_mutex_unlock (&ctx->lock); +/* TODO remove this function */ } QEMU_DEFINE_STATIC_CO_TLS(AioContext *, my_aiocontext)
Signature · 1 line
2.43.0
PATCH v2 2/14 scsi: assert that callbacks run in the correct AioContext
In reply to Stefan HajnocziSince the removal of AioContext locking, the correctness of the code relies on running requests from a single AioContext at any given time.
Add assertions that verify that callbacks are invoked in the correct AioContext.
--- hw/scsi/scsi-disk.c | 14 ++++++++++++++ system/dma-helpers.c | 3 +++ 2 files changed, 17 insertions(+)
hw/scsi/scsi-disk.c +14 -0
@@ -273,6 +273,10 @@ static void scsi_aio_complete(void *opaque, int ret)SCSIDiskReq *r = (SCSIDiskReq *)opaque;SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, r->req.dev); +/* The request must only run in the BlockBackend's AioContext */ +assert (blk_get_aio_context (s->qdev.conf.blk) == +qemu_get_current_aio_context ()); +assert (r->req.aiocb !=NULL ); r->req.aiocb =NULL ;
@@ -370,8 +374,13 @@ static void scsi_dma_complete(void *opaque, int ret)static void scsi_read_complete_noio (SCSIDiskReq *r,int ret) { +SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, r->req.dev);uint32_t n; +/* The request must only run in the BlockBackend's AioContext */ +assert (blk_get_aio_context (s->qdev.conf.blk) == +qemu_get_current_aio_context ()); +assert (r->req.aiocb ==NULL );if (scsi_disk_req_check_error (r, ret, false)) { goto done;
@@ -496,8 +505,13 @@ static void scsi_read_data(SCSIRequest *req)static void scsi_write_complete_noio (SCSIDiskReq *r,int ret) { +SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, r->req.dev);uint32_t n; +/* The request must only run in the BlockBackend's AioContext */ +assert (blk_get_aio_context (s->qdev.conf.blk) == +qemu_get_current_aio_context ()); +assert (r->req.aiocb ==NULL );if (scsi_disk_req_check_error (r, ret, false)) { goto done;
system/dma-helpers.c +3 -0
@@ -119,6 +119,9 @@ static void dma_blk_cb(void *opaque, int ret)trace_dma_blk_cb (dbs, ret); +/* DMAAIOCB is not thread-safe and must be accessed only from dbs->ctx */ +assert (ctx ==qemu_get_current_aio_context ()); + dbs->acb =NULL ; dbs->offset += dbs->iov.size;
Signature · 1 line
2.43.0
PATCH v2 6/14 block: remove AioContext locking
In reply to Stefan HajnocziThis is the big patch that removes aio_context_acquire()/aio_context_release() from the block layer and affected block layer users.
There isn't a clean way to split this patch and the reviewers are likely the same group of people, so I decided to do it in one patch.
--- include/block/block-global-state.h | 9 +- include/block/block-io.h | 3 +- include/block/snapshot.h | 2 - block.c | 234 +--------------------- block/block-backend.c | 14 -- block/copy-before-write.c | 22 +-- block/export/export.c | 22 +-- block/io.c | 45 +---- block/mirror.c | 19 -- block/monitor/bitmap-qmp-cmds.c | 20 +- block/monitor/block-hmp-cmds.c | 29 --- block/qapi-sysemu.c | 27 +-- block/qapi.c | 18 +- block/raw-format.c | 5 - block/replication.c | 58 +----- block/snapshot.c | 22 +-- block/write-threshold.c | 6 - blockdev.c | 307 +++++------------------------ blockjob.c | 18 -- hw/block/dataplane/virtio-blk.c | 10 - hw/block/dataplane/xen-block.c | 17 +- hw/block/virtio-blk.c | 45 +---- hw/core/qdev-properties-system.c | 9 - job.c | 16 -- migration/block.c | 33 +--- migration/migration-hmp-cmds.c | 3 - migration/savevm.c | 22 --- net/colo-compare.c | 2 - qemu-img.c | 4 - qemu-io.c | 10 +- qemu-nbd.c | 2 - replay/replay-debugging.c | 4 - tests/unit/test-bdrv-drain.c | 51 +---- tests/unit/test-bdrv-graph-mod.c | 6 - tests/unit/test-block-iothread.c | 31 --- tests/unit/test-blockjob.c | 137 ------------- tests/unit/test-replication.c | 11 -- util/async.c | 4 - util/vhost-user-server.c | 3 - scripts/block-coroutine-wrapper.py | 3 - tests/tsan/suppressions.tsan | 1 - 41 files changed, 102 insertions(+), 1202 deletions(-)
include/block/block-global-state.h +4 -5
@@ -31,11 +31,10 @@/* * Global state (GS) API. These functions run under the BQL. * -* 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. +* If a function modifies the graph, it also uses the graph lock to be sure it +* has unique access. The graph lock 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
include/block/block-io.h +1 -2
@@ -31,8 +31,7 @@/* * 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(). +* can run in any thread. * * 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.
include/block/snapshot.h +0 -2
@@ -86,8 +86,6 @@ int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,/* * Group operations. All block drivers are involved. -* These functions will properly handle dataplane (take aio_context_acquire -* when appropriate for appropriate block drivers */ bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
block.c +8 -226
580 lines not on this page · raw message
block/block-backend.c +0 -14
@@ -429,7 +429,6 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, {BlockBackend *blk;BlockDriverState *bs; -AioContext *ctx;uint64_t perm =0 ;uint64_t shared =BLK_PERM_ALL ;
@@ -459,23 +458,18 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, shared =BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE_UNCHANGED ; } -aio_context_acquire (qemu_get_aio_context ()); bs =bdrv_open (filename, reference, options, flags, errp); -aio_context_release (qemu_get_aio_context ());if (!bs) {return NULL ; }/* bdrv_open() could have moved bs to a different AioContext */ - ctx =bdrv_get_aio_context (bs); blk =blk_new (bdrv_get_aio_context (bs), perm, shared); blk->perm = perm; blk->shared_perm = shared; -aio_context_acquire (ctx);blk_insert_bs (blk, bs, errp);bdrv_unref (bs); -aio_context_release (ctx);if (!blk->root) {blk_unref (blk);
@@ -577,13 +571,9 @@ void blk_remove_all_bs(void)GLOBAL_STATE_CODE ();while ((blk =blk_all_next (blk)) !=NULL ) { -AioContext *ctx =blk_get_aio_context (blk); - -aio_context_acquire (ctx);if (blk->root) {blk_remove_bs (blk); } -aio_context_release (ctx); } }
@@ -2736,20 +2726,16 @@ int blk_commit_all(void)GRAPH_RDLOCK_GUARD_MAINLOOP ();while ((blk =blk_all_next (blk)) !=NULL ) { -AioContext *aio_context =blk_get_aio_context (blk);BlockDriverState *unfiltered_bs =bdrv_skip_filters (blk_bs (blk)); -aio_context_acquire (aio_context);if (blk_is_inserted (blk) &&bdrv_cow_child (unfiltered_bs)) {int ret; ret =bdrv_commit (unfiltered_bs);if (ret <0 ) { -aio_context_release (aio_context);return ret; } } -aio_context_release (aio_context); }return 0 ; }
block/copy-before-write.c +5 -17
@@ -412,7 +412,6 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,int64_t cluster_size;g_autoptr (BlockdevOptions) full_opts =NULL ;BlockdevOptionsCbw *opts; -AioContext *ctx;int ret; full_opts =cbw_parse_options (options, errp);
@@ -435,15 +434,11 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,GRAPH_RDLOCK_GUARD_MAINLOOP (); - ctx =bdrv_get_aio_context (bs); -aio_context_acquire (ctx); -if (opts->bitmap) { bitmap =block_dirty_bitmap_lookup (opts->bitmap->node, opts->bitmap->name,NULL , errp);if (!bitmap) { - ret = -EINVAL ; - goto out; +return -EINVAL ; } } s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error :
@@ -461,24 +456,21 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, s->bcs =block_copy_state_new (bs->file, s->target, bitmap, errp);if (!s->bcs) {error_prepend (errp,"Cannot create block-copy-state: " ); - ret = -EINVAL ; - goto out; +return -EINVAL ; } cluster_size =block_copy_cluster_size (s->bcs); s->done_bitmap =bdrv_create_dirty_bitmap (bs, cluster_size,NULL , errp);if (!s->done_bitmap) { - ret = -EINVAL ; - goto out; +return -EINVAL ; }bdrv_disable_dirty_bitmap (s->done_bitmap);/* s->access_bitmap starts equal to bcs bitmap */ s->access_bitmap =bdrv_create_dirty_bitmap (bs, cluster_size,NULL , errp);if (!s->access_bitmap) { - ret = -EINVAL ; - goto out; +return -EINVAL ; }bdrv_disable_dirty_bitmap (s->access_bitmap); bdrv_dirty_bitmap_merge_internal(s->access_bitmap,
@@ -487,11 +479,7 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,qemu_co_mutex_init (&s->lock);QLIST_INIT (&s->frozen_read_reqs); - - ret =0 ; -out: -aio_context_release (ctx); -return ret; +return 0 ; }static void cbw_close (BlockDriverState *bs)
block/export/export.c +1 -21
@@ -114,7 +114,6 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) } ctx =bdrv_get_aio_context (bs); -aio_context_acquire (ctx);if (export->iothread) {IOThread *iothread;
@@ -133,8 +132,6 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) set_context_errp = fixed_iothread ? errp :NULL ; ret =bdrv_try_change_aio_context (bs, new_ctx,NULL , set_context_errp);if (ret ==0 ) { -aio_context_release (ctx); -aio_context_acquire (new_ctx); ctx = new_ctx; }else if (fixed_iothread) { goto fail;
@@ -191,8 +188,6 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)assert (exp->blk !=NULL );QLIST_INSERT_HEAD (&block_exports, exp, next); - -aio_context_release (ctx);return exp; fail:
@@ -200,7 +195,6 @@ fail:blk_set_dev_ops (blk,NULL ,NULL );blk_unref (blk); } -aio_context_release (ctx);if (exp) {g_free (exp->id);g_free (exp);
@@ -218,9 +212,6 @@ void blk_exp_ref(BlockExport *exp)static void blk_exp_delete_bh (void *opaque) {BlockExport *exp = opaque; -AioContext *aio_context = exp->ctx; - -aio_context_acquire (aio_context);assert (exp->refcount ==0 );QLIST_REMOVE (exp, next);
@@ -230,8 +221,6 @@ static void blk_exp_delete_bh(void *opaque)qapi_event_send_block_export_deleted (exp->id);g_free (exp->id);g_free (exp); - -aio_context_release (aio_context); }void blk_exp_unref (BlockExport *exp)
@@ -249,22 +238,16 @@ void blk_exp_unref(BlockExport *exp)* connections and other internally held references start to shut down. When * the function returns, there may still be active references while the export * is in the process of shutting down. -* -* Acquires exp->ctx internally. Callers must *not* hold the lock. */ void blk_exp_request_shutdown (BlockExport *exp) { -AioContext *aio_context = exp->ctx; - -aio_context_acquire (aio_context); -/* * If the user doesn't own the export any more, it is already shutting * down. We must not call .request_shutdown and decrease the refcount a * second time. */ if (!exp->user_owned) { - goto out; +return ; } exp->drv->request_shutdown (exp);
@@ -272,9 +255,6 @@ void blk_exp_request_shutdown(BlockExport *exp)assert (exp->user_owned); exp->user_owned = false;blk_exp_unref (exp); - -out: -aio_context_release (aio_context); }/*
block/io.c +5 -40
106 lines not on this page · raw message
block/mirror.c +0 -19
@@ -662,7 +662,6 @@ static int mirror_exit_common(Job *job)MirrorBlockJob *s =container_of (job, MirrorBlockJob, common.job);BlockJob *bjob = &s->common;MirrorBDSOpaque *bs_opaque; -AioContext *replace_aio_context =NULL ;BlockDriverState *src;BlockDriverState *target_bs;BlockDriverState *mirror_top_bs;
@@ -677,7 +676,6 @@ static int mirror_exit_common(Job *job) } s->prepared = true; -aio_context_acquire (qemu_get_aio_context ());bdrv_graph_rdlock_main_loop (); mirror_top_bs = s->mirror_top_bs;
@@ -742,11 +740,6 @@ static int mirror_exit_common(Job *job) }bdrv_graph_rdunlock_main_loop (); -if (s->to_replace) { - replace_aio_context =bdrv_get_aio_context (s->to_replace); -aio_context_acquire (replace_aio_context); - } -if (s->should_complete && !abort) {BlockDriverState *to_replace = s->to_replace ?: src;bool ro =bdrv_is_read_only (to_replace);
@@ -785,9 +778,6 @@ static int mirror_exit_common(Job *job)error_free (s->replace_blocker);bdrv_unref (s->to_replace); } -if (replace_aio_context) { -aio_context_release (replace_aio_context); - }g_free (s->replaces);/*
@@ -811,8 +801,6 @@ static int mirror_exit_common(Job *job)bdrv_unref (mirror_top_bs);bdrv_unref (src); -aio_context_release (qemu_get_aio_context ()); -return ret; }
@@ -1191,24 +1179,17 @@ static void mirror_complete(Job *job, Error **errp)/* block all operations on to_replace bs */ if (s->replaces) { -AioContext *replace_aio_context; - s->to_replace =bdrv_find_node (s->replaces);if (!s->to_replace) {error_setg (errp,"Node name '%s' not found" , s->replaces);return ; } - replace_aio_context =bdrv_get_aio_context (s->to_replace); -aio_context_acquire (replace_aio_context); -/* TODO Translate this into child freeze system. */ error_setg (&s->replace_blocker,"block device is in use by block-job-complete" );bdrv_op_block_all (s->to_replace, s->replace_blocker);bdrv_ref (s->to_replace); - -aio_context_release (replace_aio_context); } s->should_complete = true;
block/monitor/bitmap-qmp-cmds.c +3 -17
@@ -95,7 +95,6 @@ void qmp_block_dirty_bitmap_add(const char *node, const char *name, {BlockDriverState *bs;BdrvDirtyBitmap *bitmap; -AioContext *aio_context;if (!name || name[0 ] =='\0' ) {error_setg (errp,"Bitmap name cannot be empty" );
@@ -107,14 +106,11 @@ void qmp_block_dirty_bitmap_add(const char *node, const char *name,return ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); -if (has_granularity) {if (granularity <512 || !is_power_of_2 (granularity)) {error_setg (errp,"Granularity must be power of 2 " "and at least 512" ); - goto out; +return ; } }else {/* Default to cluster size, if available: */
@@ -132,12 +128,12 @@ void qmp_block_dirty_bitmap_add(const char *node, const char *name,if (persistent && !bdrv_can_store_new_dirty_bitmap (bs, name, granularity, errp)) { - goto out; +return ; } bitmap =bdrv_create_dirty_bitmap (bs, granularity, name, errp);if (bitmap ==NULL ) { - goto out; +return ; }if (disabled) {
@@ -145,9 +141,6 @@ void qmp_block_dirty_bitmap_add(const char *node, const char *name, }bdrv_dirty_bitmap_set_persistence (bitmap, persistent); - -out: -aio_context_release (aio_context); } BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,
@@ -157,7 +150,6 @@ BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name, {BlockDriverState *bs;BdrvDirtyBitmap *bitmap; -AioContext *aio_context;GLOBAL_STATE_CODE ();
@@ -166,19 +158,14 @@ BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,return NULL ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); -if (bdrv_dirty_bitmap_check (bitmap,BDRV_BITMAP_BUSY |BDRV_BITMAP_RO , errp)) { -aio_context_release (aio_context);return NULL ; }if (bdrv_dirty_bitmap_get_persistence (bitmap) &&bdrv_remove_persistent_dirty_bitmap (bs, name, errp) <0 ) { -aio_context_release (aio_context);return NULL ; }
@@ -190,7 +177,6 @@ BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name, *bitmap_bs = bs; } -aio_context_release (aio_context);return release ?NULL : bitmap; }
block/monitor/block-hmp-cmds.c +0 -29
@@ -141,7 +141,6 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)const char *id =qdict_get_str (qdict,"id" );BlockBackend *blk;BlockDriverState *bs; -AioContext *aio_context;Error *local_err =NULL ;GLOBAL_STATE_CODE ();
@@ -168,14 +167,10 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)return ; } - aio_context =blk_get_aio_context (blk); -aio_context_acquire (aio_context); - bs =blk_bs (blk);if (bs) {if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_DRIVE_DEL , &local_err)) {error_report_err (local_err); -aio_context_release (aio_context);return ; }
@@ -196,8 +191,6 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict) }else {blk_unref (blk); } - -aio_context_release (aio_context); }void hmp_commit (Monitor *mon,const QDict *qdict)
@@ -213,7 +206,6 @@ void hmp_commit(Monitor *mon, const QDict *qdict) ret =blk_commit_all (); }else {BlockDriverState *bs; -AioContext *aio_context; blk =blk_by_name (device);if (!blk) {
@@ -222,18 +214,13 @@ void hmp_commit(Monitor *mon, const QDict *qdict) } bs =bdrv_skip_implicit_filters (blk_bs (blk)); - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context);if (!blk_is_available (blk)) {error_report ("Device '%s' has no medium" , device); -aio_context_release (aio_context);return ; } ret =bdrv_commit (bs); - -aio_context_release (aio_context); }if (ret <0 ) {error_report ("'commit' error for '%s': %s" , device,strerror (-ret));
@@ -560,7 +547,6 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict)BlockBackend *blk =NULL ;BlockDriverState *bs =NULL ;BlockBackend *local_blk =NULL ; -AioContext *ctx =NULL ;bool qdev =qdict_get_try_bool (qdict,"qdev" , false);const char *device =qdict_get_str (qdict,"device" );const char *command =qdict_get_str (qdict,"command" );
@@ -582,9 +568,6 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict) } } - ctx = blk ?blk_get_aio_context (blk) :bdrv_get_aio_context (bs); -aio_context_acquire (ctx); -if (bs) { blk = local_blk =blk_new (bdrv_get_aio_context (bs),0 ,BLK_PERM_ALL ); ret =blk_insert_bs (blk, bs, &err);
@@ -622,11 +605,6 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict) fail:blk_unref (local_blk); - -if (ctx) { -aio_context_release (ctx); - } -hmp_handle_error (mon, err); }
@@ -882,7 +860,6 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)int nb_sns, i;int total;int *global_snapshots; -AioContext *aio_context; typedef struct SnapshotEntry { QEMUSnapshotInfo sn;
@@ -909,11 +886,8 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)error_report_err (err);return ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); nb_sns =bdrv_snapshot_list (bs, &sn_tab); -aio_context_release (aio_context);if (nb_sns <0 ) {monitor_printf (mon,"bdrv_snapshot_list: error %d\n" , nb_sns);
@@ -924,9 +898,7 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)int bs1_nb_sns =0 ;ImageEntry *ie;SnapshotEntry *se; -AioContext *ctx =bdrv_get_aio_context (bs1); -aio_context_acquire (ctx);if (bdrv_can_snapshot (bs1)) { sn =NULL ; bs1_nb_sns =bdrv_snapshot_list (bs1, &sn);
@@ -944,7 +916,6 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict) }g_free (sn); } -aio_context_release (ctx); }if (no_snapshot) {
block/qapi-sysemu.c +3 -24
@@ -174,7 +174,6 @@ blockdev_remove_medium(const char *device, const char *id, Error **errp) {BlockBackend *blk;BlockDriverState *bs; -AioContext *aio_context;bool has_attached_device;GLOBAL_STATE_CODE ();
@@ -204,13 +203,10 @@ blockdev_remove_medium(const char *device, const char *id, Error **errp)return ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); -bdrv_graph_rdlock_main_loop ();if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_EJECT , errp)) {bdrv_graph_rdunlock_main_loop (); - goto out; +return ; }bdrv_graph_rdunlock_main_loop ();
@@ -223,9 +219,6 @@ blockdev_remove_medium(const char *device, const char *id, Error **errp)* value passed here (i.e. false). */ blk_dev_change_media_cb (blk, false, &error_abort); } - -out: -aio_context_release (aio_context); }void qmp_blockdev_remove_medium (const char *id,Error **errp)
@@ -237,7 +230,6 @@ static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, BlockDriverState *bs, Error **errp) {Error *local_err =NULL ; -AioContext *ctx;bool has_device;int ret;
@@ -259,11 +251,7 @@ static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,return ; } - ctx =bdrv_get_aio_context (bs); -aio_context_acquire (ctx); ret =blk_insert_bs (blk, bs, errp); -aio_context_release (ctx); -if (ret <0 ) {return ; }
@@ -374,9 +362,7 @@ void qmp_blockdev_change_medium(const char *device,qdict_put_str (options,"driver" , format); } -aio_context_acquire (qemu_get_aio_context ()); medium_bs =bdrv_open (filename,NULL , options, bdrv_flags, errp); -aio_context_release (qemu_get_aio_context ());if (!medium_bs) { goto fail;
@@ -437,20 +423,16 @@ void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)ThrottleConfig cfg;BlockDriverState *bs;BlockBackend *blk; -AioContext *aio_context; blk =qmp_get_blk (arg->device, arg->id, errp);if (!blk) {return ; } - aio_context =blk_get_aio_context (blk); -aio_context_acquire (aio_context); - bs =blk_bs (blk);if (!bs) {error_setg (errp,"Device has no medium" ); - goto out; +return ; }throttle_config_init (&cfg);
@@ -505,7 +487,7 @@ void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) }if (!throttle_is_valid (&cfg, errp)) { - goto out; +return ; }if (throttle_enabled (&cfg)) {
@@ -522,9 +504,6 @@ void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)/* If all throttling settings are set to 0, disable I/O limits */ blk_io_limits_disable (blk); } - -out: -aio_context_release (aio_context); } void qmp_block_latency_histogram_set(
block/qapi.c +3 -15
@@ -234,13 +234,11 @@ bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp)int ret;Error *err =NULL ; -aio_context_acquire (bdrv_get_aio_context (bs)); - size =bdrv_getlength (bs);if (size <0 ) {error_setg_errno (errp, -size,"Can't get image size '%s'" , bs->exact_filename); - goto out; +return ; }bdrv_refresh_filename (bs);
@@ -265,7 +263,7 @@ bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp) info->format_specific =bdrv_get_specific_info (bs, &err);if (err) {error_propagate (errp, err); - goto out; +return ; } backing_filename = bs->backing_file;if (backing_filename[0 ] !='\0' ) {
@@ -300,11 +298,8 @@ bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp)break ;default :error_propagate (errp, err); - goto out; +return ; } - -out: -aio_context_release (bdrv_get_aio_context (bs)); }/**
@@ -709,15 +704,10 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,/* Just to be safe if query_nodes is not always initialized */ if (has_query_nodes && query_nodes) {for (bs =bdrv_next_node (NULL ); bs; bs =bdrv_next_node (bs)) { -AioContext *ctx =bdrv_get_aio_context (bs); - -aio_context_acquire (ctx);QAPI_LIST_APPEND (tail,bdrv_query_bds_stats (bs, false)); -aio_context_release (ctx); } }else {for (blk =blk_all_next (NULL ); blk; blk =blk_all_next (blk)) { -AioContext *ctx =blk_get_aio_context (blk);BlockStats *s;char *qdev;
@@ -725,7 +715,6 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,continue ; } -aio_context_acquire (ctx); s =bdrv_query_bds_stats (blk_bs (blk), true); s->device =g_strdup (blk_name (blk));
@@ -737,7 +726,6 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes, }bdrv_query_blk_stats (s->stats, blk); -aio_context_release (ctx);QAPI_LIST_APPEND (tail, s); }
block/raw-format.c +0 -5
@@ -470,7 +470,6 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {BDRVRawState *s = bs->opaque; -AioContext *ctx;bool has_size;uint64_t offset, size;BdrvChildRole file_role;
@@ -522,11 +521,7 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags, bs->file->bs->filename); } - ctx =bdrv_get_aio_context (bs); -aio_context_acquire (ctx); ret =raw_apply_options (bs, s, offset, has_size, size, errp); -aio_context_release (ctx); -if (ret <0 ) {return ret; }
block/replication.c +3 -55
233 lines not on this page · raw message
block/snapshot.c +1 -21
@@ -525,9 +525,7 @@ static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs)return bdrv_has_blk (bs) ||QLIST_EMPTY (&bs->parents); } -/* Group operations. All block drivers are involved. -* These functions will properly handle dataplane (take aio_context_acquire -* when appropriate for appropriate block drivers) */ +/* Group operations. All block drivers are involved. */ bool bdrv_all_can_snapshot (bool has_devices,strList *devices,Error **errp)
@@ -545,14 +543,11 @@ bool bdrv_all_can_snapshot(bool has_devices, strList *devices, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);bool ok = true; -aio_context_acquire (ctx);if (devices ||bdrv_all_snapshots_includes_bs (bs)) { ok =bdrv_can_snapshot (bs); } -aio_context_release (ctx);if (!ok) {error_setg (errp,"Device '%s' is writable but does not support " "snapshots" ,bdrv_get_device_or_node_name (bs));
@@ -582,18 +577,15 @@ int bdrv_all_delete_snapshot(const char *name, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);QEMUSnapshotInfo sn1, *snapshot = &sn1;int ret =0 ; -aio_context_acquire (ctx);if ((devices ||bdrv_all_snapshots_includes_bs (bs)) &&bdrv_snapshot_find (bs, snapshot, name) >=0 ) { ret =bdrv_snapshot_delete (bs, snapshot->id_str, snapshot->name, errp); } -aio_context_release (ctx);if (ret <0 ) {error_prepend (errp,"Could not delete snapshot '%s' on '%s': " , name,bdrv_get_device_or_node_name (bs));
@@ -628,17 +620,14 @@ int bdrv_all_goto_snapshot(const char *name, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);bool all_snapshots_includes_bs; -aio_context_acquire (ctx);bdrv_graph_rdlock_main_loop (); all_snapshots_includes_bs =bdrv_all_snapshots_includes_bs (bs);bdrv_graph_rdunlock_main_loop (); ret = (devices || all_snapshots_includes_bs) ?bdrv_snapshot_goto (bs, name, errp) :0 ; -aio_context_release (ctx);if (ret <0 ) {bdrv_graph_rdlock_main_loop (); error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
@@ -670,15 +659,12 @@ int bdrv_all_has_snapshot(const char *name, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);QEMUSnapshotInfo sn;int ret =0 ; -aio_context_acquire (ctx);if (devices ||bdrv_all_snapshots_includes_bs (bs)) { ret =bdrv_snapshot_find (bs, &sn, name); } -aio_context_release (ctx);if (ret <0 ) {if (ret == -ENOENT ) {return 0 ;
@@ -715,10 +701,8 @@ int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);int ret =0 ; -aio_context_acquire (ctx);if (bs == vm_state_bs) { sn->vm_state_size = vm_state_size; ret =bdrv_snapshot_create (bs, sn);
@@ -726,7 +710,6 @@ int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, sn->vm_state_size =0 ; ret =bdrv_snapshot_create (bs, sn); } -aio_context_release (ctx);if (ret <0 ) {error_setg (errp,"Could not create snapshot '%s' on '%s'" , sn->name,bdrv_get_device_or_node_name (bs));
@@ -757,13 +740,10 @@ BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, iterbdrvs = bdrvs;while (iterbdrvs) {BlockDriverState *bs = iterbdrvs->data; -AioContext *ctx =bdrv_get_aio_context (bs);bool found = false; -aio_context_acquire (ctx); found = (devices ||bdrv_all_snapshots_includes_bs (bs)) &&bdrv_can_snapshot (bs); -aio_context_release (ctx);if (vmstate_bs) { if (g_str_equal(vmstate_bs,
block/write-threshold.c +0 -6
@@ -33,7 +33,6 @@ void qmp_block_set_write_threshold(const char *node_name, Error **errp) {BlockDriverState *bs; -AioContext *aio_context; bs =bdrv_find_node (node_name);if (!bs) {
@@ -41,12 +40,7 @@ void qmp_block_set_write_threshold(const char *node_name,return ; } - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); -bdrv_write_threshold_set (bs, threshold_bytes); - -aio_context_release (aio_context); } void bdrv_write_threshold_check_write(BlockDriverState *bs, int64_t offset,
blockdev.c +53 -254
1,003 lines not on this page · raw message
blockjob.c +0 -18
@@ -198,9 +198,7 @@ void block_job_remove_all_bdrv(BlockJob *job)* one to make sure that such a concurrent access does not attempt * to process an already freed BdrvChild. */ -aio_context_release (job->job.aio_context);bdrv_graph_wrlock (); -aio_context_acquire (job->job.aio_context);while (job->nodes) {GSList *l = job->nodes;BdrvChild *c = l->data;
@@ -234,28 +232,12 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, uint64_t perm, uint64_t shared_perm, Error **errp) { BdrvChild *c; -AioContext *ctx =bdrv_get_aio_context (bs); -bool need_context_ops;GLOBAL_STATE_CODE ();bdrv_ref (bs); - need_context_ops = ctx != job->job.aio_context; - -if (need_context_ops) { -if (job->job.aio_context !=qemu_get_aio_context ()) { -aio_context_release (job->job.aio_context); - } -aio_context_acquire (ctx); - } c =bdrv_root_attach_child (bs, name, &child_job,0 , perm, shared_perm, job, errp); -if (need_context_ops) { -aio_context_release (ctx); -if (job->job.aio_context !=qemu_get_aio_context ()) { -aio_context_acquire (job->job.aio_context); - } - }if (c ==NULL ) {return -EPERM ; }
hw/block/dataplane/virtio-blk.c +0 -10
@@ -124,7 +124,6 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)VirtIOBlockDataPlane *s = vblk->dataplane;BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (vblk)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus); -AioContext *old_context;unsigned i;unsigned nvqs = s->conf->num_queues;Error *local_err =NULL ;
@@ -178,10 +177,7 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)trace_virtio_blk_data_plane_start (s); - old_context =blk_get_aio_context (s->conf->conf.blk); -aio_context_acquire (old_context); r =blk_set_aio_context (s->conf->conf.blk, s->ctx, &local_err); -aio_context_release (old_context);if (r <0 ) {error_report_err (local_err); goto fail_aio_context;
@@ -208,13 +204,11 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)/* Get this show started by hooking up our callbacks */ if (!blk_in_drain (s->conf->conf.blk)) { -aio_context_acquire (s->ctx);for (i =0 ; i < nvqs; i++) {VirtQueue *vq =virtio_get_queue (s->vdev, i);virtio_queue_aio_attach_host_notifier (vq, s->ctx); } -aio_context_release (s->ctx); }return 0 ;
@@ -314,8 +308,6 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)*/ vblk->dataplane_started = false; -aio_context_acquire (s->ctx); -/* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */ blk_drain (s->conf->conf.blk);
@@ -325,8 +317,6 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)*/ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context (),NULL ); -aio_context_release (s->ctx); -/* Clean up guest notifier (irq) */ k->set_guest_notifiers (qbus->parent, nvqs, false);
hw/block/dataplane/xen-block.c +2 -15
@@ -260,8 +260,6 @@ static void xen_block_complete_aio(void *opaque, int ret)XenBlockRequest *request = opaque;XenBlockDataPlane *dataplane = request->dataplane; -aio_context_acquire (dataplane->ctx); -if (ret !=0 ) { error_report("%s I/O error", request->req.operation == BLKIF_OP_READ ?
@@ -273,10 +271,10 @@ static void xen_block_complete_aio(void *opaque, int ret)if (request->presync) { request->presync =0 ;xen_block_do_aio (request); - goto done; +return ; }if (request->aio_inflight >0 ) { - goto done; +return ; }switch (request->req.operation) {
@@ -318,9 +316,6 @@ static void xen_block_complete_aio(void *opaque, int ret)if (dataplane->more_work) {qemu_bh_schedule (dataplane->bh); } - -done: -aio_context_release (dataplane->ctx); } static bool xen_block_split_discard(XenBlockRequest *request,
@@ -601,9 +596,7 @@ static void xen_block_dataplane_bh(void *opaque) {XenBlockDataPlane *dataplane = opaque; -aio_context_acquire (dataplane->ctx);xen_block_handle_requests (dataplane); -aio_context_release (dataplane->ctx); }static bool xen_block_dataplane_event (void *opaque)
@@ -703,10 +696,8 @@ void xen_block_dataplane_stop(XenBlockDataPlane *dataplane)xen_block_dataplane_detach (dataplane); } -aio_context_acquire (dataplane->ctx);/* Xen doesn't have multiple users for nodes, so this can't fail */ blk_set_aio_context (dataplane->blk,qemu_get_aio_context (), &error_abort); -aio_context_release (dataplane->ctx);/* * Now that the context has been moved onto the main thread, cancel
@@ -752,7 +743,6 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane, {ERRP_GUARD ();XenDevice *xendev = dataplane->xendev; -AioContext *old_context;unsigned int ring_size;unsigned int i;
@@ -836,11 +826,8 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane, goto stop; } - old_context =blk_get_aio_context (dataplane->blk); -aio_context_acquire (old_context);/* If other users keep the BlockBackend in the iothread, that's ok */ blk_set_aio_context (dataplane->blk, dataplane->ctx,NULL ); -aio_context_release (old_context);if (!blk_in_drain (dataplane->blk)) {xen_block_dataplane_attach (dataplane);
hw/block/virtio-blk.c +2 -43
135 lines not on this page · raw message
tests/unit/test-bdrv-drain.c +2 -49
51 lines not on this page · raw message
tests/unit/test-blockjob.c +0 -137
137 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
PATCH v2 8/14 scsi: remove AioContext locking
In reply to Stefan HajnocziThe AioContext lock no longer has any effect. Remove it.
--- include/hw/virtio/virtio-scsi.h | 14 -------------- hw/scsi/scsi-bus.c | 2 -- hw/scsi/scsi-disk.c | 31 +++++-------------------------- hw/scsi/virtio-scsi.c | 18 ------------------ 4 files changed, 5 insertions(+), 60 deletions(-)
include/hw/virtio/virtio-scsi.h +0 -14
@@ -101,20 +101,6 @@ struct VirtIOSCSI {uint32_t host_features; }; -static inline void virtio_scsi_acquire (VirtIOSCSI *s) -{ -if (s->ctx) { -aio_context_acquire (s->ctx); - } -} - -static inline void virtio_scsi_release (VirtIOSCSI *s) -{ -if (s->ctx) { -aio_context_release (s->ctx); - } -} - void virtio_scsi_common_realize(DeviceState *dev, VirtIOHandleOutput ctrl, VirtIOHandleOutput evt,
hw/scsi/scsi-bus.c +0 -2
@@ -1731,9 +1731,7 @@ void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) {scsi_device_for_each_req_async (sdev, scsi_device_purge_one_req,NULL ); -aio_context_acquire (blk_get_aio_context (sdev->conf.blk));blk_drain (sdev->conf.blk); -aio_context_release (blk_get_aio_context (sdev->conf.blk));scsi_device_set_ua (sdev, sense); }
hw/scsi/scsi-disk.c +5 -26
@@ -2339,14 +2339,10 @@ static void scsi_disk_reset(DeviceState *dev) {SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev.qdev, dev);uint64_t nb_sectors; -AioContext *ctx;scsi_device_purge_requests (&s->qdev,SENSE_CODE (RESET )); - ctx =blk_get_aio_context (s->qdev.conf.blk); -aio_context_acquire (ctx);blk_get_geometry (s->qdev.conf.blk, &nb_sectors); -aio_context_release (ctx); nb_sectors /= s->qdev.blocksize /BDRV_SECTOR_SIZE ;if (nb_sectors) {
@@ -2545,15 +2541,13 @@ static void scsi_unrealize(SCSIDevice *dev)static void scsi_hd_realize (SCSIDevice *dev,Error **errp) {SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, dev); -AioContext *ctx =NULL ; +/* can happen for devices without drive. The error message for missing * backend will be issued in scsi_realize */ if (s->qdev.conf.blk) { - ctx =blk_get_aio_context (s->qdev.conf.blk); -aio_context_acquire (ctx);if (!blkconf_blocksizes (&s->qdev.conf, errp)) { - goto out; +return ; } } s->qdev.blocksize = s->qdev.conf.logical_block_size;
@@ -2562,16 +2556,11 @@ static void scsi_hd_realize(SCSIDevice *dev, Error **errp) s->product =g_strdup ("QEMU HARDDISK" ); }scsi_realize (&s->qdev, errp); -out: -if (ctx) { -aio_context_release (ctx); - } }static void scsi_cd_realize (SCSIDevice *dev,Error **errp) {SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, dev); -AioContext *ctx;int ret;uint32_t blocksize =2048 ;
@@ -2587,8 +2576,6 @@ static void scsi_cd_realize(SCSIDevice *dev, Error **errp) blocksize = dev->conf.physical_block_size; } - ctx =blk_get_aio_context (dev->conf.blk); -aio_context_acquire (ctx); s->qdev.blocksize = blocksize; s->qdev.type =TYPE_ROM ; s->features |=1 <<SCSI_DISK_F_REMOVABLE ;
@@ -2596,7 +2583,6 @@ static void scsi_cd_realize(SCSIDevice *dev, Error **errp) s->product =g_strdup ("QEMU CD-ROM" ); }scsi_realize (&s->qdev, errp); -aio_context_release (ctx); }
@@ -2727,7 +2713,6 @@ static int get_device_type(SCSIDiskState *s)static void scsi_block_realize (SCSIDevice *dev,Error **errp) {SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, dev); -AioContext *ctx;int sg_version;int rc;
@@ -2742,9 +2727,6 @@ static void scsi_block_realize(SCSIDevice *dev, Error **errp) "be removed in a future version"); } - ctx =blk_get_aio_context (s->qdev.conf.blk); -aio_context_acquire (ctx); -/* check we are using a driver managing SG_IO (version 3 and after) */ rc =blk_ioctl (s->qdev.conf.blk,SG_GET_VERSION_NUM , &sg_version);if (rc <0 ) {
@@ -2752,18 +2734,18 @@ static void scsi_block_realize(SCSIDevice *dev, Error **errp)if (rc != -EPERM ) {error_append_hint (errp,"Is this a SCSI device?\n" ); } - goto out; +return ; }if (sg_version <30000 ) {error_setg (errp,"scsi generic interface too old" ); - goto out; +return ; }/* get device type from INQUIRY data */ rc =get_device_type (s);if (rc <0 ) {error_setg (errp,"INQUIRY failed" ); - goto out; +return ; }/* Make a guess for the block size, we'll fix it when the guest sends.
@@ -2783,9 +2765,6 @@ static void scsi_block_realize(SCSIDevice *dev, Error **errp)scsi_realize (&s->qdev, errp);scsi_generic_read_device_inquiry (&s->qdev); - -out: -aio_context_release (ctx); } typedef struct SCSIBlockReq {
hw/scsi/virtio-scsi.c +0 -18
@@ -642,9 +642,7 @@ static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)return ; } -virtio_scsi_acquire (s);virtio_scsi_handle_ctrl_vq (s, vq); -virtio_scsi_release (s); }static void virtio_scsi_complete_cmd_req (VirtIOSCSIReq *req)
@@ -882,9 +880,7 @@ static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)return ; } -virtio_scsi_acquire (s);virtio_scsi_handle_cmd_vq (s, vq); -virtio_scsi_release (s); } static void virtio_scsi_get_config(VirtIODevice *vdev,
@@ -1031,9 +1027,7 @@ static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)return ; } -virtio_scsi_acquire (s);virtio_scsi_handle_event_vq (s, vq); -virtio_scsi_release (s); }static void virtio_scsi_change (SCSIBus *bus,SCSIDevice *dev,SCSISense sense)
@@ -1052,9 +1046,7 @@ static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) }, }; -virtio_scsi_acquire (s);virtio_scsi_push_event (s, &info); -virtio_scsi_release (s); } }
@@ -1071,17 +1063,13 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,VirtIODevice *vdev =VIRTIO_DEVICE (hotplug_dev);VirtIOSCSI *s =VIRTIO_SCSI (vdev);SCSIDevice *sd =SCSI_DEVICE (dev); -AioContext *old_context;int ret;if (s->ctx && !s->dataplane_fenced) {if (blk_op_is_blocked (sd->conf.blk,BLOCK_OP_TYPE_DATAPLANE , errp)) {return ; } - old_context =blk_get_aio_context (sd->conf.blk); -aio_context_acquire (old_context); ret =blk_set_aio_context (sd->conf.blk, s->ctx, errp); -aio_context_release (old_context);if (ret <0 ) {return ; }
@@ -1097,10 +1085,8 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, }, }; -virtio_scsi_acquire (s);virtio_scsi_push_event (s, &info);scsi_bus_set_ua (&s->bus,SENSE_CODE (REPORTED_LUNS_CHANGED )); -virtio_scsi_release (s); } }
@@ -1122,17 +1108,13 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,qdev_simple_device_unplug_cb (hotplug_dev, dev, errp);if (s->ctx) { -virtio_scsi_acquire (s);/* If other users keep the BlockBackend in the iothread, that's ok */ blk_set_aio_context (sd->conf.blk,qemu_get_aio_context (),NULL ); -virtio_scsi_release (s); }if (virtio_vdev_has_feature (vdev,VIRTIO_SCSI_F_HOTPLUG )) { -virtio_scsi_acquire (s);virtio_scsi_push_event (s, &info);scsi_bus_set_ua (&s->bus,SENSE_CODE (REPORTED_LUNS_CHANGED )); -virtio_scsi_release (s); } }
Signature · 1 line
2.43.0
PATCH v2 3/14 tests: remove aio_context_acquire() tests
In reply to Stefan HajnocziThe aio_context_acquire() API is being removed. Drop the test case that calls the API.
--- tests/unit/test-aio.c | 67 +------------------------------------------ 1 file changed, 1 insertion(+), 66 deletions(-)
tests/unit/test-aio.c +1 -66
@@ -100,76 +100,12 @@ static void event_ready_cb(EventNotifier *e)/* Tests using aio_*. */ -typedef struct { -QemuMutex start_lock; -EventNotifier notifier; -bool thread_acquired; -}AcquireTestData ; - -static void *test_acquire_thread (void *opaque) -{ -AcquireTestData *data = opaque; - -/* Wait for other thread to let us start */ -qemu_mutex_lock (&data->start_lock); -qemu_mutex_unlock (&data->start_lock); - -/* event_notifier_set might be called either before or after -* the main thread's call to poll(). The test case's outcome -* should be the same in either case. -*/ -event_notifier_set (&data->notifier); -aio_context_acquire (ctx); -aio_context_release (ctx); - - data->thread_acquired = true;/* success, we got here */ - -return NULL ; -} -static void set_event_notifier (AioContext *nctx,EventNotifier *notifier,EventNotifierHandler *handler) {aio_set_event_notifier (nctx, notifier, handler,NULL ,NULL ); } -static void dummy_notifier_read (EventNotifier *n) -{ -event_notifier_test_and_clear (n); -} - -static void test_acquire (void ) -{ -QemuThread thread; -AcquireTestData data; - -/* Dummy event notifier ensures aio_poll() will block */ -event_notifier_init (&data.notifier, false); -set_event_notifier (ctx, &data.notifier, dummy_notifier_read); -g_assert (!aio_poll (ctx, false));/* consume aio_notify() */ - -qemu_mutex_init (&data.start_lock); -qemu_mutex_lock (&data.start_lock); - data.thread_acquired = false; - -qemu_thread_create (&thread,"test_acquire_thread" , - test_acquire_thread, - &data,QEMU_THREAD_JOINABLE ); - -/* Block in aio_poll(), let other thread kick us and acquire context */ -aio_context_acquire (ctx); -qemu_mutex_unlock (&data.start_lock);/* let the thread run */ -g_assert (aio_poll (ctx, true)); -g_assert (!data.thread_acquired); -aio_context_release (ctx); - -qemu_thread_join (&thread); -set_event_notifier (ctx, &data.notifier,NULL ); -event_notifier_cleanup (&data.notifier); - -g_assert (data.thread_acquired); -} -static void test_bh_schedule (void ) {BHTestData data = { .n =0 };
@@ -879,7 +815,7 @@ static void test_worker_thread_co_enter(void)qemu_thread_get_self (&this_thread); co =qemu_coroutine_create (co_check_current_thread, &this_thread); -qemu_thread_create (&worker_thread,"test_acquire_thread" , +qemu_thread_create (&worker_thread,"test_aio_co_enter" , test_aio_co_enter, co,QEMU_THREAD_JOINABLE );
@@ -899,7 +835,6 @@ int main(int argc, char **argv)while (g_main_context_iteration (NULL , false));g_test_init (&argc, &argv,NULL ); -g_test_add_func ("/aio/acquire" , test_acquire);g_test_add_func ("/aio/bh/schedule" , test_bh_schedule);g_test_add_func ("/aio/bh/schedule10" , test_bh_schedule10);g_test_add_func ("/aio/bh/cancel" , test_bh_cancel);
Signature · 1 line
2.43.0
PATCH v2 9/14 aio-wait: draw equivalence between AIO_WAIT_WHILE() and AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziNow that the AioContext lock no longer exists, AIO_WAIT_WHILE() and AIO_WAIT_WHILE_UNLOCKED() are equivalent.
A future patch will get rid of AIO_WAIT_WHILE_UNLOCKED().
--- include/block/aio-wait.h | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
include/block/aio-wait.h +4 -12
@@ -63,9 +63,6 @@ extern AioWait global_aio_wait;* @ctx: the aio context, or NULL if multiple aio contexts (for which the * caller does not hold a lock) are involved in the polling condition. * @cond: wait while this conditional expression is true -* @unlock: whether to unlock and then lock again @ctx. This applies -* only when waiting for another AioContext from the main loop. -* Otherwise it's ignored. * * Wait while a condition is true. Use this to implement synchronous * operations that require event loop activity.
@@ -78,7 +75,7 @@ extern AioWait global_aio_wait;* wait on conditions between two IOThreads since that could lead to deadlock, * go via the main loop instead. */ -#define AIO_WAIT_WHILE_INTERNAL (ctx, cond, unlock) ({ \ +#define AIO_WAIT_WHILE_INTERNAL (ctx, cond) ({ \ bool waited_ = false; \ AioWait *wait_ = &global_aio_wait; \ AioContext *ctx_ = (ctx); \
@@ -95,13 +92,7 @@ extern AioWait global_aio_wait;assert (qemu_get_current_aio_context () == \qemu_get_aio_context ()); \while ((cond)) { \ -if (unlock && ctx_) { \ -aio_context_release (ctx_); \ - } \aio_poll (qemu_get_aio_context (), true); \ -if (unlock && ctx_) { \ -aio_context_acquire (ctx_); \ - } \ waited_ = true; \ } \ } \
@@ -109,10 +100,11 @@ extern AioWait global_aio_wait; waited_; })#define AIO_WAIT_WHILE (ctx, cond) \ - AIO_WAIT_WHILE_INTERNAL(ctx, cond, true) + AIO_WAIT_WHILE_INTERNAL(ctx, cond) +/* TODO replace this with AIO_WAIT_WHILE() in a future patch */ #define AIO_WAIT_WHILE_UNLOCKED (ctx, cond) \ - AIO_WAIT_WHILE_INTERNAL(ctx, cond, false) + AIO_WAIT_WHILE_INTERNAL(ctx, cond)/** * aio_wait_kick:
Signature · 1 line
2.43.0
PATCH v2 5/14 graph-lock: remove AioContext locking
In reply to Stefan HajnocziStop acquiring/releasing the AioContext lock in bdrv_graph_wrlock()/bdrv_graph_unlock() since the lock no longer has any effect.
The distinction between bdrv_graph_wrunlock() and bdrv_graph_wrunlock_ctx() becomes meaningless and they can be collapsed into one function.
--- include/block/graph-lock.h | 21 ++----------- block.c | 50 +++++++++++++++--------------- block/backup.c | 4 +-- block/blklogwrites.c | 8 ++--- block/blkverify.c | 4 +-- block/block-backend.c | 11 +++---- block/commit.c | 16 +++++----- block/graph-lock.c | 44 ++------------------------ block/mirror.c | 22 ++++++------- block/qcow2.c | 4 +-- block/quorum.c | 8 ++--- block/replication.c | 14 ++++----- block/snapshot.c | 4 +-- block/stream.c | 12 +++---- block/vmdk.c | 20 ++++++------ blockdev.c | 8 ++--- blockjob.c | 12 +++---- tests/unit/test-bdrv-drain.c | 40 ++++++++++++------------ tests/unit/test-bdrv-graph-mod.c | 20 ++++++------ scripts/block-coroutine-wrapper.py | 4 +-- 20 files changed, 133 insertions(+), 193 deletions(-)
include/block/graph-lock.h +2 -19
@@ -110,34 +110,17 @@ void unregister_aiocontext(AioContext *ctx);* * The wrlock can only be taken from the main loop, with BQL held, as only the * main loop is allowed to modify the graph. -* -* If @bs is non-NULL, its AioContext is temporarily released. -* -* This function polls. Callers must not hold the lock of any AioContext other -* than the current one and the one of @bs. */ void no_coroutine_fn TSA_ACQUIRE(graph_lock) TSA_NO_TSA -bdrv_graph_wrlock(BlockDriverState *bs); +bdrv_graph_wrlock(void); /* * bdrv_graph_wrunlock: * Write finished, reset global has_writer to 0 and restart * all readers that are waiting. - * - * If @bs is non-NULL, its AioContext is temporarily released. */ void no_coroutine_fn TSA_RELEASE(graph_lock) TSA_NO_TSA -bdrv_graph_wrunlock(BlockDriverState *bs); - -/* - * bdrv_graph_wrunlock_ctx: - * Write finished, reset global has_writer to 0 and restart - * all readers that are waiting. - * - * If @ctx is non-NULL, its lock is temporarily released. - */ -void no_coroutine_fn TSA_RELEASE(graph_lock) TSA_NO_TSA -bdrv_graph_wrunlock_ctx(AioContext *ctx); +bdrv_graph_wrunlock(void);/* * bdrv_graph_co_rdlock:
block.c +25 -25
170 lines not on this page · raw message
block/backup.c +2 -2
@@ -496,10 +496,10 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,block_copy_set_speed (bcs, speed);/* Required permissions are taken by copy-before-write filter target */ -bdrv_graph_wrlock (target); +bdrv_graph_wrlock ();block_job_add_bdrv (&job->common,"target" , target,0 ,BLK_PERM_ALL , &error_abort); -bdrv_graph_wrunlock (target); +bdrv_graph_wrunlock ();return &job->common;
block/blklogwrites.c +4 -4
@@ -251,9 +251,9 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags, ret =0 ; fail_log:if (ret <0 ) { -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, s->log_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); s->log_file =NULL ; } fail:
@@ -265,10 +265,10 @@ static void blk_log_writes_close(BlockDriverState *bs) {BDRVBlkLogWritesState *s = bs->opaque; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, s->log_file); s->log_file =NULL ; -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); } static int64_t coroutine_fn GRAPH_RDLOCK
block/blkverify.c +2 -2
@@ -151,10 +151,10 @@ static void blkverify_close(BlockDriverState *bs) {BDRVBlkverifyState *s = bs->opaque; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, s->test_file); s->test_file =NULL ; -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); } static int64_t coroutine_fn GRAPH_RDLOCK
block/block-backend.c +4 -7
@@ -889,7 +889,6 @@ void blk_remove_bs(BlockBackend *blk) {ThrottleGroupMember *tgm = &blk->public.throttle_group_member;BdrvChild *root; -AioContext *ctx;GLOBAL_STATE_CODE ();
@@ -919,10 +918,9 @@ void blk_remove_bs(BlockBackend *blk) root = blk->root; blk->root =NULL ; - ctx =bdrv_get_aio_context (root->bs); -bdrv_graph_wrlock (root->bs); +bdrv_graph_wrlock ();bdrv_root_unref_child (root); -bdrv_graph_wrunlock_ctx (ctx); +bdrv_graph_wrunlock (); }/*
@@ -933,16 +931,15 @@ void blk_remove_bs(BlockBackend *blk)int blk_insert_bs (BlockBackend *blk,BlockDriverState *bs,Error **errp) {ThrottleGroupMember *tgm = &blk->public.throttle_group_member; -AioContext *ctx =bdrv_get_aio_context (bs);GLOBAL_STATE_CODE ();bdrv_ref (bs); -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock (); blk->root =bdrv_root_attach_child (bs,"root" , &child_root,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , blk->perm, blk->shared_perm, blk, errp); -bdrv_graph_wrunlock_ctx (ctx); +bdrv_graph_wrunlock ();if (blk->root ==NULL ) {return -EPERM ; }
block/commit.c +8 -8
@@ -100,9 +100,9 @@ static void commit_abort(Job *job)bdrv_graph_rdunlock_main_loop ();bdrv_drained_begin (commit_top_backing_bs); -bdrv_graph_wrlock (commit_top_backing_bs); +bdrv_graph_wrlock ();bdrv_replace_node (s->commit_top_bs, commit_top_backing_bs, &error_abort); -bdrv_graph_wrunlock (commit_top_backing_bs); +bdrv_graph_wrunlock ();bdrv_drained_end (commit_top_backing_bs);bdrv_unref (s->commit_top_bs);
@@ -339,7 +339,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,* this is the responsibility of the interface (i.e. whoever calls * commit_start()). */ -bdrv_graph_wrlock (top); +bdrv_graph_wrlock (); s->base_overlay =bdrv_find_overlay (top, base);assert (s->base_overlay);
@@ -370,19 +370,19 @@ void commit_start(const char *job_id, BlockDriverState *bs, ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , iter_shared_perms, errp);if (ret <0 ) { -bdrv_graph_wrunlock (top); +bdrv_graph_wrunlock (); goto fail; } }if (bdrv_freeze_backing_chain (commit_top_bs, base, errp) <0 ) { -bdrv_graph_wrunlock (top); +bdrv_graph_wrunlock (); goto fail; } s->chain_frozen = true; ret =block_job_add_bdrv (&s->common,"base" , base,0 ,BLK_PERM_ALL , errp); -bdrv_graph_wrunlock (top); +bdrv_graph_wrunlock ();if (ret <0 ) { goto fail;
@@ -434,9 +434,9 @@ fail:* otherwise this would fail because of lack of permissions. */ if (commit_top_bs) {bdrv_drained_begin (top); -bdrv_graph_wrlock (top); +bdrv_graph_wrlock ();bdrv_replace_node (commit_top_bs, top, &error_abort); -bdrv_graph_wrunlock (top); +bdrv_graph_wrunlock ();bdrv_drained_end (top); } }
block/graph-lock.c +2 -42
73 lines not on this page · raw message
block/mirror.c +11 -11
@@ -764,7 +764,7 @@ static int mirror_exit_common(Job *job)* check for an op blocker on @to_replace, and we have our own * there. */ -bdrv_graph_wrlock (target_bs); +bdrv_graph_wrlock ();if (bdrv_recurse_can_replace (src, to_replace)) {bdrv_replace_node (to_replace, target_bs, &local_err); }else {
@@ -773,7 +773,7 @@ static int mirror_exit_common(Job *job)"would not lead to an abrupt change of visible data" , to_replace->node_name, target_bs->node_name); } -bdrv_graph_wrunlock (target_bs); +bdrv_graph_wrunlock ();bdrv_drained_end (to_replace);if (local_err) {error_report_err (local_err);
@@ -796,9 +796,9 @@ static int mirror_exit_common(Job *job)* valid. */ block_job_remove_all_bdrv (bjob); -bdrv_graph_wrlock (mirror_top_bs); +bdrv_graph_wrlock ();bdrv_replace_node (mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); -bdrv_graph_wrunlock (mirror_top_bs); +bdrv_graph_wrunlock ();bdrv_drained_end (target_bs);bdrv_unref (target_bs);
@@ -1914,13 +1914,13 @@ static BlockJob *mirror_start_job(*/ bdrv_disable_dirty_bitmap (s->dirty_bitmap); -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock (); ret =block_job_add_bdrv (&s->common,"source" , bs,0 ,BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE |BLK_PERM_CONSISTENT_READ , errp);if (ret <0 ) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); goto fail; }
@@ -1965,17 +1965,17 @@ static BlockJob *mirror_start_job( ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , iter_shared_perms, errp);if (ret <0 ) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); goto fail; } }if (bdrv_freeze_backing_chain (mirror_top_bs, target, errp) <0 ) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); goto fail; } } -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();QTAILQ_INIT (&s->ops_in_flight);
@@ -2001,12 +2001,12 @@ fail: bs_opaque->stop = true;bdrv_drained_begin (bs); -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock ();assert (mirror_top_bs->backing->bs == bs);bdrv_child_refresh_perms (mirror_top_bs, mirror_top_bs->backing, &error_abort);bdrv_replace_node (mirror_top_bs, bs, &error_abort); -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();bdrv_drained_end (bs);bdrv_unref (mirror_top_bs);
block/qcow2.c +2 -2
@@ -2807,9 +2807,9 @@ qcow2_do_close(BlockDriverState *bs, bool close_data_file)if (close_data_file &&has_data_file (bs)) {GLOBAL_STATE_CODE ();bdrv_graph_rdunlock_main_loop (); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, s->data_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); s->data_file =NULL ;bdrv_graph_rdlock_main_loop (); }
block/quorum.c +4 -4
@@ -1037,14 +1037,14 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags, close_exit:/* cleanup on error */ -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();for (i =0 ; i < s->num_children; i++) {if (!opened[i]) {continue ; }bdrv_unref_child (bs, s->children[i]); } -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();g_free (s->children);g_free (opened); exit:
@@ -1057,11 +1057,11 @@ static void quorum_close(BlockDriverState *bs)BDRVQuorumState *s = bs->opaque;int i; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();for (i =0 ; i < s->num_children; i++) {bdrv_unref_child (bs, s->children[i]); } -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();g_free (s->children); }
block/replication.c +7 -7
@@ -560,7 +560,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,return ; } -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock ();bdrv_ref (hidden_disk->bs); s->hidden_disk = bdrv_attach_child(bs, hidden_disk->bs, "hidden disk",
@@ -568,7 +568,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, &local_err);if (local_err) {error_propagate (errp, local_err); -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();aio_context_release (aio_context);return ; }
@@ -579,7 +579,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, BDRV_CHILD_DATA, &local_err);if (local_err) {error_propagate (errp, local_err); -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();aio_context_release (aio_context);return ; }
@@ -592,7 +592,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,if (!top_bs || !bdrv_is_root_node (top_bs) || !check_top_bs (top_bs, bs)) {error_setg (errp,"No top_bs or it is invalid" ); -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();reopen_backing_file (bs, false,NULL );aio_context_release (aio_context);return ;
@@ -600,7 +600,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,bdrv_op_block_all (top_bs, s->blocker);bdrv_op_unblock (top_bs,BLOCK_OP_TYPE_DATAPLANE , s->blocker); -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); s->backup_job = backup_job_create( NULL, s->secondary_disk->bs, s->hidden_disk->bs,
@@ -691,12 +691,12 @@ static void replication_done(void *opaque, int ret)if (ret ==0 ) { s->stage =BLOCK_REPLICATION_DONE ; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, s->secondary_disk); s->secondary_disk =NULL ;bdrv_unref_child (bs, s->hidden_disk); s->hidden_disk =NULL ; -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); s->error =0 ; }else {
block/snapshot.c +2 -2
@@ -290,9 +290,9 @@ int bdrv_snapshot_goto(BlockDriverState *bs, }/* .bdrv_open() will re-attach it */ -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, fallback); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); ret =bdrv_snapshot_goto (fallback_bs, snapshot_id, errp); open_ret = drv->bdrv_open (bs, options, bs->open_flags, &local_err);
block/stream.c +6 -6
@@ -99,9 +99,9 @@ static int stream_prepare(Job *job) } } -bdrv_graph_wrlock (s->target_bs); +bdrv_graph_wrlock ();bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); -bdrv_graph_wrunlock (s->target_bs); +bdrv_graph_wrunlock ();/* * This call will do I/O, so the graph can change again from here on.
@@ -366,10 +366,10 @@ void stream_start(const char *job_id, BlockDriverState *bs,* already have our own plans. Also don't allow resize as the image size is * queried only at the job start and then cached. */ -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock ();if (block_job_add_bdrv (&s->common,"active node" , bs,0 , basic_flags |BLK_PERM_WRITE , errp)) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); goto fail; }
@@ -389,11 +389,11 @@ void stream_start(const char *job_id, BlockDriverState *bs, ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , basic_flags, errp);if (ret <0 ) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); goto fail; } } -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock (); s->base_overlay = base_overlay; s->above_base = above_base;
block/vmdk.c +10 -10
@@ -272,7 +272,7 @@ static void vmdk_free_extents(BlockDriverState *bs)BDRVVmdkState *s = bs->opaque;VmdkExtent *e; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();for (i =0 ; i < s->num_extents; i++) { e = &s->extents[i];g_free (e->l1_table);
@@ -283,7 +283,7 @@ static void vmdk_free_extents(BlockDriverState *bs)bdrv_unref_child (bs, e->file); } } -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();g_free (s->extents); }
@@ -1247,9 +1247,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, 0, 0, 0, 0, 0, &extent, errp);if (ret <0 ) {bdrv_graph_rdunlock_main_loop (); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1266,9 +1266,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options,g_free (buf);if (ret) {bdrv_graph_rdunlock_main_loop (); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1277,9 +1277,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, ret =vmdk_open_se_sparse (bs, extent_file, bs->open_flags, errp);if (ret) {bdrv_graph_rdunlock_main_loop (); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1287,9 +1287,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, }else {error_setg (errp,"Unsupported extent type '%s'" , type);bdrv_graph_rdunlock_main_loop (); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_graph_rdlock_main_loop (); ret = -ENOTSUP ; goto out;
blockdev.c +4 -4
@@ -1611,9 +1611,9 @@ static void external_snapshot_abort(void *opaque) }bdrv_drained_begin (state->new_bs); -bdrv_graph_wrlock (state->old_bs); +bdrv_graph_wrlock ();bdrv_replace_node (state->new_bs, state->old_bs, &error_abort); -bdrv_graph_wrunlock (state->old_bs); +bdrv_graph_wrunlock ();bdrv_drained_end (state->new_bs);bdrv_unref (state->old_bs);/* bdrv_replace_node() ref'ed old_bs */
@@ -3656,7 +3656,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child,BlockDriverState *parent_bs, *new_bs =NULL ;BdrvChild *p_child; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock (); parent_bs =bdrv_lookup_bs (parent, parent, errp);if (!parent_bs) {
@@ -3692,7 +3692,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child, } out: -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); }BlockJobInfoList *qmp_query_block_jobs (Error **errp)
blockjob.c +6 -6
@@ -199,7 +199,7 @@ void block_job_remove_all_bdrv(BlockJob *job)* to process an already freed BdrvChild. */ aio_context_release (job->job.aio_context); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();aio_context_acquire (job->job.aio_context);while (job->nodes) {GSList *l = job->nodes;
@@ -212,7 +212,7 @@ void block_job_remove_all_bdrv(BlockJob *job)g_slist_free_1 (l); } -bdrv_graph_wrunlock_ctx (job->job.aio_context); +bdrv_graph_wrunlock (); }bool block_job_has_bdrv (BlockJob *job,BlockDriverState *bs)
@@ -514,7 +514,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,int ret;GLOBAL_STATE_CODE (); -bdrv_graph_wrlock (bs); +bdrv_graph_wrlock ();if (job_id ==NULL && !(flags &JOB_INTERNAL )) { job_id =bdrv_get_device_name (bs);
@@ -523,7 +523,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, job =job_create (job_id, &driver->job_driver, txn,bdrv_get_aio_context (bs), flags, cb, opaque, errp);if (job ==NULL ) { -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();return NULL ; }
@@ -563,11 +563,11 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, goto fail; } -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();return job; fail: -bdrv_graph_wrunlock (bs); +bdrv_graph_wrunlock ();job_early_fail (&job->job);return NULL ; }
tests/unit/test-bdrv-drain.c +20 -20
@@ -807,9 +807,9 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, tjob->bs = src; job = &tjob->common; -bdrv_graph_wrlock (target); +bdrv_graph_wrlock ();block_job_add_bdrv (job,"target" , target,0 ,BLK_PERM_ALL , &error_abort); -bdrv_graph_wrunlock (target); +bdrv_graph_wrunlock ();switch (result) {case TEST_JOB_SUCCESS :
@@ -991,11 +991,11 @@ static void bdrv_test_top_close(BlockDriverState *bs) {BdrvChild *c, *next_c; -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock (); QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {bdrv_unref_child (bs, c); } -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); } static int coroutine_fn GRAPH_RDLOCK
@@ -1085,10 +1085,10 @@ static void do_test_delete_by_drain(bool detach_instead_of_delete, null_bs =bdrv_open ("null-co://" ,NULL ,NULL ,BDRV_O_RDWR |BDRV_O_PROTOCOL , &error_abort); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (bs, null_bs,"null-child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();/* This child will be the one to pass to requests through to, and * it will stall until a drain occurs */
@@ -1096,21 +1096,21 @@ static void do_test_delete_by_drain(bool detach_instead_of_delete, &error_abort); child_bs->total_sectors =65536 >>BDRV_SECTOR_BITS ;/* Takes our reference to child_bs */ -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock (); tts->wait_child =bdrv_attach_child (bs, child_bs,"wait-child" , &child_of_bds,BDRV_CHILD_DATA |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();/* This child is just there to be deleted * (for detach_instead_of_delete == true) */ null_bs =bdrv_open ("null-co://" ,NULL ,NULL ,BDRV_O_RDWR |BDRV_O_PROTOCOL , &error_abort); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (bs, null_bs,"null-child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); blk =blk_new (qemu_get_aio_context (),BLK_PERM_ALL ,BLK_PERM_ALL );blk_insert_bs (blk, bs, &error_abort);
@@ -1193,14 +1193,14 @@ static void no_coroutine_fn detach_indirect_bh(void *opaque)bdrv_dec_in_flight (data->child_b->bs); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_unref_child (data->parent_b, data->child_b);bdrv_ref (data->c); data->child_c =bdrv_attach_child (data->parent_b, data->c,"PB-C" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); } static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret)
@@ -1298,7 +1298,7 @@ static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)/* Set child relationships */ bdrv_ref (b);bdrv_ref (a); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock (); child_b =bdrv_attach_child (parent_b, b,"PB-B" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
@@ -1308,7 +1308,7 @@ static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)bdrv_attach_child (parent_a, a,"PA-A" , by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); g_assert_cmpint(parent_a->refcnt, ==, 1); g_assert_cmpint(parent_b->refcnt, ==, 1);
@@ -1727,7 +1727,7 @@ static void test_drop_intermediate_poll(void)* Establish the chain last, so the chain links are the first * elements in the BDS.parents lists */ -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();for (i =0 ; i <3 ; i++) {if (i) {/* Takes the reference to chain[i - 1] */
@@ -1735,7 +1735,7 @@ static void test_drop_intermediate_poll(void) &chain_child_class, BDRV_CHILD_COW, &error_abort); } } -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); job =block_job_create ("job" , &test_simple_job_driver,NULL , job_node,0 ,BLK_PERM_ALL ,0 ,0 ,NULL ,NULL , &error_abort);
@@ -1982,10 +1982,10 @@ static void do_test_replace_child_mid_drain(int old_drain_count, new_child_bs->total_sectors =1 ;bdrv_ref (old_child_bs); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (parent_bs, old_child_bs,"child" , &child_of_bds,BDRV_CHILD_COW , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock (); parent_s->setup_completed = true;for (i =0 ; i < old_drain_count; i++) {
@@ -2016,9 +2016,9 @@ static void do_test_replace_child_mid_drain(int old_drain_count,g_assert (parent_bs->quiesce_counter == old_drain_count);bdrv_drained_begin (old_child_bs);bdrv_drained_begin (new_child_bs); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_replace_node (old_child_bs, new_child_bs, &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_drained_end (new_child_bs);bdrv_drained_end (old_child_bs);g_assert (parent_bs->quiesce_counter == new_drain_count);
tests/unit/test-bdrv-graph-mod.c +10 -10
@@ -137,10 +137,10 @@ static void test_update_perm_tree(void)blk_insert_bs (root, bs, &error_abort); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (filter, bs,"child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();aio_context_acquire (qemu_get_aio_context ()); ret =bdrv_append (filter, bs,NULL );
@@ -206,11 +206,11 @@ static void test_should_update_child(void)bdrv_set_backing_hd (target, bs, &error_abort); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();g_assert (target->backing->bs == bs);bdrv_attach_child (filter, target,"target" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();aio_context_acquire (qemu_get_aio_context ());bdrv_append (filter, bs, &error_abort);aio_context_release (qemu_get_aio_context ());
@@ -248,7 +248,7 @@ static void test_parallel_exclusive_write(void)bdrv_ref (base);bdrv_ref (fl1); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (top, fl1,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);
@@ -260,7 +260,7 @@ static void test_parallel_exclusive_write(void) &error_abort);bdrv_replace_node (fl1, fl2, &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();bdrv_drained_end (fl2);bdrv_drained_end (fl1);
@@ -367,7 +367,7 @@ static void test_parallel_perm_update(void)*/ bdrv_ref (base); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (top, ws,"file" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); c_fl1 = bdrv_attach_child(ws, fl1, "first", &child_of_bds,
@@ -380,7 +380,7 @@ static void test_parallel_perm_update(void)bdrv_attach_child (fl2, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();/* Select fl1 as first child to be active */ s->selected = c_fl1;
@@ -434,11 +434,11 @@ static void test_append_greedy_filter(void)BlockDriverState *base =no_perm_node ("base" );BlockDriverState *fl =exclusive_writer_node ("fl1" ); -bdrv_graph_wrlock (NULL ); +bdrv_graph_wrlock ();bdrv_attach_child (top, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_graph_wrunlock (NULL ); +bdrv_graph_wrunlock ();aio_context_acquire (qemu_get_aio_context ());bdrv_append (fl, base, &error_abort);
scripts/block-coroutine-wrapper.py +2 -2
@@ -261,8 +261,8 @@ def gen_no_co_wrapper(func: FuncDecl) -> str: graph_lock=' bdrv_graph_rdlock_main_loop();' graph_unlock=' bdrv_graph_rdunlock_main_loop();' elif func.graph_wrlock: - graph_lock=' bdrv_graph_wrlock(NULL);' - graph_unlock=' bdrv_graph_wrunlock(NULL);' + graph_lock=' bdrv_graph_wrlock();' + graph_unlock=' bdrv_graph_wrunlock();' return f"""\ /*
Signature · 1 line
2.43.0
PATCH v2 12/14 scsi: remove outdated AioContext lock comment
In reply to Stefan HajnocziThe SCSI subsystem no longer uses the AioContext lock. Request processing runs exclusively in the BlockBackend's AioContext since "scsi: only access SCSIDevice->requests from one thread" and hence the lock is unnecessary.
--- hw/scsi/scsi-disk.c | 1 - 1 file changed, 1 deletion(-)
hw/scsi/scsi-disk.c +0 -1
@@ -355,7 +355,6 @@ done:scsi_req_unref (&r->req); } -/* Called with AioContext lock held */ static void scsi_dma_complete (void *opaque,int ret) {SCSIDiskReq *r = (SCSIDiskReq *)opaque;
Signature · 1 line
2.43.0
PATCH v2 7/14 block: remove bdrv_co_lock()
In reply to Stefan HajnocziThe bdrv_co_lock() and bdrv_co_unlock() functions are already no-ops. Remove them.
--- include/block/block-global-state.h | 14 -------------- block.c | 10 ---------- blockdev.c | 4 ---- 3 files changed, 28 deletions(-)
include/block/block-global-state.h +0 -14
@@ -267,20 +267,6 @@ 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); -bool bdrv_child_change_aio_context (BdrvChild *c,AioContext *ctx,GHashTable *visited,Transaction *tran,Error **errp);
block.c +0 -10
@@ -7431,16 +7431,6 @@ void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx)bdrv_dec_in_flight (bs); } -void coroutine_fn bdrv_co_lock(BlockDriverState *bs) -{ -/* TODO removed in next patch */ -} - -void coroutine_fn bdrv_co_unlock(BlockDriverState *bs) -{ -/* TODO removed in next patch */ -} -static void bdrv_do_remove_aio_context_notifier (BdrvAioNotifier *ban) {GLOBAL_STATE_CODE ();
blockdev.c +0 -4
@@ -2264,18 +2264,14 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,return ; } -bdrv_co_lock (bs);bdrv_drained_begin (bs); -bdrv_co_unlock (bs); old_ctx =bdrv_co_enter (bs);blk_co_truncate (blk, size, false,PREALLOC_MODE_OFF ,0 , errp);bdrv_co_leave (bs, old_ctx); -bdrv_co_lock (bs);bdrv_drained_end (bs);blk_co_unref (blk); -bdrv_co_unlock (bs); } void qmp_block_stream(const char *job_id, const char *device,
Signature · 1 line
2.43.0
PATCH v2 10/14 aio: remove aio_context_acquire()/aio_context_release() API
In reply to Stefan HajnocziDelete these functions because nothing calls these functions anymore.
I introduced these APIs in commit 98563fc3ec44 ("aio: add aio_context_acquire() and aio_context_release()") in 2014. It's with a sigh of relief that I delete these APIs almost 10 years later.
Thanks to Paolo Bonzini's vision for multi-queue QEMU, we got an understanding of where the code needed to go in order to remove the limitations that the original dataplane and the IOThread/AioContext approach that followed it.
Emanuele Giuseppe Esposito had the splendid determination to convert large parts of the codebase so that they no longer needed the AioContext lock. This was a painstaking process, both in the actual code changes required and the iterations of code review that Emanuele eked out of Kevin and me over many months.
Kevin Wolf tackled multitudes of graph locking conversions to protect in-flight I/O from run-time changes to the block graph as well as the clang Thread Safety Analysis annotations that allow the compiler to check whether the graph lock is being used correctly.
And me, well, I'm just here to add some pizzazz to the QEMU multi-queue block layer :). Thank you to everyone who helped with this effort, including Eric Blake, code reviewer extraordinaire, and others who I've forgotten to mention.
--- include/block/aio.h | 17 ----------------- util/async.c | 10 ---------- 2 files changed, 27 deletions(-)
include/block/aio.h +0 -17
@@ -278,23 +278,6 @@ void aio_context_ref(AioContext *ctx);*/ void aio_context_unref (AioContext *ctx); -/* Take ownership of the AioContext. If the AioContext will be shared between -* threads, and a thread does not want to be interrupted, it will have to -* take ownership around calls to aio_poll(). Otherwise, aio_poll() -* automatically takes care of calling aio_context_acquire and -* aio_context_release. -* -* Note that this is separate from bdrv_drained_begin/bdrv_drained_end. A -* thread still has to call those to avoid being interrupted by the guest. -* -* Bottom halves, timers and callbacks can be created or removed without -* acquiring the AioContext. -*/ -void aio_context_acquire (AioContext *ctx); - -/* Relinquish ownership of the AioContext. */ -void aio_context_release (AioContext *ctx); -/** * aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will * run only once and as soon as possible.
util/async.c +0 -10
@@ -719,16 +719,6 @@ void aio_context_unref(AioContext *ctx)g_source_unref (&ctx->source); } -void aio_context_acquire (AioContext *ctx) -{ -/* TODO remove this function */ -} - -void aio_context_release (AioContext *ctx) -{ -/* TODO remove this function */ -} - QEMU_DEFINE_STATIC_CO_TLS(AioContext *, my_aiocontext) AioContext *qemu_get_current_aio_context(void)
Signature · 1 line
2.43.0
PATCH v2 11/14 docs: remove AioContext lock from IOThread docs
In reply to Stefan HajnocziEncourage the use of locking primitives and stop mentioning the AioContext lock since it is being removed.
--- docs/devel/multiple-iothreads.txt | 45 +++++++++++-------------------- 1 file changed, 15 insertions(+), 30 deletions(-)
docs/devel/multiple-iothreads.txt +15 -30
@@ -88,27 +88,18 @@ loop, depending on which AioContext instance the caller passes in. How to synchronize with an IOThread ----------------------------------- -AioContext is not thread-safe so some rules must be followed when using file -descriptors, event notifiers, timers, or BHs across threads: +Variables that can be accessed by multiple threads require some form of +synchronization such as qemu_mutex_lock(), rcu_read_lock(), etc. -1. AioContext functions can always be called safely. They handle their -own locking internally. - -2. Other threads wishing to access the AioContext must use -aio_context_acquire()/aio_context_release() for mutual exclusion. Once the -context is acquired no other thread can access it or run event loop iterations -in this AioContext. - -Legacy code sometimes nests aio_context_acquire()/aio_context_release() calls. -Do not use nesting anymore, it is incompatible with the BDRV_POLL_WHILE() macro -used in the block layer and can lead to hangs. - -There is currently no lock ordering rule if a thread needs to acquire multiple -AioContexts simultaneously. Therefore, it is only safe for code holding the -QEMU global mutex to acquire other AioContexts. +AioContext functions like aio_set_fd_handler(), aio_set_event_notifier(), +aio_bh_new(), and aio_timer_new() are thread-safe. They can be used to trigger +activity in an IOThread. Side note: the best way to schedule a function call across threads is to call -aio_bh_schedule_oneshot(). No acquire/release or locking is needed. +aio_bh_schedule_oneshot(). + +The main loop thread can wait synchronously for a condition using +AIO_WAIT_WHILE(). AioContext and the block layer ------------------------------
@@ -124,22 +115,16 @@ Block layer code must therefore expect to run in an IOThread and avoid using old APIs that implicitly use the main loop. See the "How to program for IOThreads" above for information on how to do that. -If main loop code such as a QMP function wishes to access a BlockDriverState -it must first call aio_context_acquire(bdrv_get_aio_context(bs)) to ensure -that callbacks in the IOThread do not run in parallel. - Code running in the monitor typically needs to ensure that past requests from the guest are completed. When a block device is running in an IOThread, the IOThread can also process requests from the guest (via ioeventfd). To achieve both objects, wrap the code between bdrv_drained_begin() and bdrv_drained_end(), thus creating a "drained -section". The functions must be called between aio_context_acquire() -and aio_context_release(). You can freely release and re-acquire the -AioContext within a drained section. +section". -Long-running jobs (usually in the form of coroutines) are best scheduled in -the BlockDriverState's AioContext to avoid the need to acquire/release around -each bdrv_*() call. The functions bdrv_add/remove_aio_context_notifier, -or alternatively blk_add/remove_aio_context_notifier if you use BlockBackends, -can be used to get a notification whenever bdrv_try_change_aio_context() moves a +Long-running jobs (usually in the form of coroutines) are often scheduled in +the BlockDriverState's AioContext. The functions +bdrv_add/remove_aio_context_notifier, or alternatively +blk_add/remove_aio_context_notifier if you use BlockBackends, can be used to +get a notification whenever bdrv_try_change_aio_context() moves a BlockDriverState to a different AioContext.
Signature · 1 line
2.43.0
PATCH v2 14/14 block: remove outdated AioContext locking comments
In reply to Stefan HajnocziThe AioContext lock no longer exists.
There is one noteworthy change:
- * 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.
+ * More specifically, these functions use BDRV_POLL_WHILE(bs), which requires
+ * the caller to be either in the main thread or directly in the home thread
+ * that runs the bs AioContext. Calling them from another thread in another
+ * AioContext would cause deadlocks.
I am not sure whether deadlocks are still possible. Maybe they have just moved to the fine-grained locks that have replaced the AioContext. Since I am not sure if the deadlocks are gone, I have kept the substance unchanged and just removed mention of the AioContext.
--- include/block/block-common.h | 3 -- include/block/block-io.h | 9 ++-- include/block/block_int-common.h | 2 - block.c | 73 ++++++---------------------- block/block-backend.c | 8 --- block/export/vhost-user-blk-server.c | 4 -- tests/qemu-iotests/202 | 2 +- tests/qemu-iotests/203 | 3 +- 8 files changed, 22 insertions(+), 82 deletions(-)
include/block/block-common.h +0 -3
@@ -70,9 +70,6 @@* automatically takes the graph rdlock when calling the wrapped function. In * the same way, no_co_wrapper_bdrv_wrlock functions automatically take the * graph wrlock. -* -* If the first parameter of the function is a BlockDriverState, BdrvChild or -* BlockBackend pointer, the AioContext lock for it is taken in the wrapper. */ #define no_co_wrapper#define no_co_wrapper_bdrv_rdlock
include/block/block-io.h +4 -5
@@ -332,11 +332,10 @@ bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,* "I/O or GS" 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. +* More specifically, these functions use BDRV_POLL_WHILE(bs), which requires +* the caller to be either in the main thread 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.
include/block/block_int-common.h +0 -2
@@ -1192,8 +1192,6 @@ struct BlockDriverState {/* The error object in use for blocking operations on backing_hd */ Error *backing_blocker; -/* Protected by AioContext lock */ -/* * If we are reading a disk image, give its size in sectors. * Generally read-only; it is written to by load_snapshot and
block.c +15 -58
194 lines not on this page · raw message
block/block-backend.c +0 -8
@@ -390,8 +390,6 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm)* Both sets of permissions can be changed later using blk_set_perm(). * * Return the new BlockBackend on success, null on failure. -* -* Callers must hold the AioContext lock of @bs. */ BlockBackend *blk_new_with_bs (BlockDriverState *bs,uint64_t perm,uint64_t shared_perm,Error **errp)
@@ -416,8 +414,6 @@ BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm,* Just as with bdrv_open(), after having called this function the reference to * @options belongs to the block layer (even on failure). * -* Called without holding an AioContext lock. -* * TODO: Remove @filename and @flags; it should be possible to specify a whole * BDS tree just by specifying the @options QDict (or @reference, * alternatively). At the time of adding this function, this is not possible,
@@ -872,8 +868,6 @@ BlockBackend *blk_by_public(BlockBackendPublic *public)/* * Disassociates the currently associated BlockDriverState from @blk. -* -* The caller must hold the AioContext lock for the BlockBackend. */ void blk_remove_bs (BlockBackend *blk) {
@@ -915,8 +909,6 @@ void blk_remove_bs(BlockBackend *blk)/* * Associates a new BlockDriverState with @blk. -* -* Callers must hold the AioContext lock of @bs. */ int blk_insert_bs (BlockBackend *blk,BlockDriverState *bs,Error **errp) {
block/export/vhost-user-blk-server.c +0 -4
@@ -278,7 +278,6 @@ static void vu_blk_exp_resize(void *opaque)vu_config_change_msg (&vexp->vu_server.vu_dev); } -/* Called with vexp->export.ctx acquired */ static void vu_blk_drained_begin (void *opaque) {VuBlkExport *vexp = opaque;
@@ -287,7 +286,6 @@ static void vu_blk_drained_begin(void *opaque)vhost_user_server_detach_aio_context (&vexp->vu_server); } -/* Called with vexp->export.blk AioContext acquired */ static void vu_blk_drained_end (void *opaque) {VuBlkExport *vexp = opaque;
@@ -300,8 +298,6 @@ static void vu_blk_drained_end(void *opaque)* Ensures that bdrv_drained_begin() waits until in-flight requests complete * and the server->co_trip coroutine has terminated. It will be restarted in * vhost_user_server_attach_aio_context(). -* -* Called with vexp->export.ctx acquired. */ static bool vu_blk_drained_poll (void *opaque) {
tests/qemu-iotests/202 +1 -1
@@ -21,7 +21,7 @@ # Check that QMP 'transaction' blockdev-snapshot-sync with multiple drives on a # single IOThread completes successfully. This particular command triggered a # hang due to recursive AioContext locking and BDRV_POLL_WHILE(). Protect -# against regressions. +# against regressions even though the AioContext lock no longer exists. import iotests
tests/qemu-iotests/203 +2 -1
@@ -21,7 +21,8 @@ # Check that QMP 'migrate' with multiple drives on a single IOThread completes # successfully. This particular command triggered a hang in the source QEMU # process due to recursive AioContext locking in bdrv_invalidate_all() and -# BDRV_POLL_WHILE(). +# BDRV_POLL_WHILE(). Protect against regressions even though the AioContext +# lock no longer exists. import iotests
Signature · 1 line
2.43.0
PATCH v2 13/14 job: remove outdated AioContext locking comments
In reply to Stefan HajnocziThe AioContext lock no longer exists.
--- include/qemu/job.h | 20 -------------------- 1 file changed, 20 deletions(-)
include/qemu/job.h +0 -20
@@ -67,8 +67,6 @@ typedef struct Job {/** * The completion function that will be called when the job completes. -* Called with AioContext lock held, since many callback implementations -* use bdrv_* functions that require to hold the lock. */ BlockCompletionFunc *cb;
@@ -264,9 +262,6 @@ struct JobDriver {* * This callback will not be invoked if the job has already failed. * If it fails, abort and then clean will be called. -* -* Called with AioContext lock held, since many callbacs implementations -* use bdrv_* functions that require to hold the lock. */ int (*prepare)(Job *job);
@@ -277,9 +272,6 @@ struct JobDriver {* * All jobs will complete with a call to either .commit() or .abort() but * never both. -* -* Called with AioContext lock held, since many callback implementations -* use bdrv_* functions that require to hold the lock. */ void (*commit)(Job *job);
@@ -290,9 +282,6 @@ struct JobDriver {* * All jobs will complete with a call to either .commit() or .abort() but * never both. -* -* Called with AioContext lock held, since many callback implementations -* use bdrv_* functions that require to hold the lock. */ void (*abort)(Job *job);
@@ -301,9 +290,6 @@ struct JobDriver {* .commit() or .abort(). Regardless of which callback is invoked after * completion, .clean() will always be called, even if the job does not * belong to a transaction group. -* -* Called with AioContext lock held, since many callbacs implementations -* use bdrv_* functions that require to hold the lock. */ void (*clean)(Job *job);
@@ -318,17 +304,12 @@ struct JobDriver {* READY). * (If the callback is NULL, the job is assumed to terminate * without I/O.) -* -* Called with AioContext lock held, since many callback implementations -* use bdrv_* functions that require to hold the lock. */ bool (*cancel)(Job *job,bool force);/** * Called when the job is freed. -* Called with AioContext lock held, since many callback implementations -* use bdrv_* functions that require to hold the lock. */ void (*free)(Job *job); };
@@ -424,7 +405,6 @@ void job_ref_locked(Job *job);* Release a reference that was previously acquired with job_ref_locked() or * job_create(). If it's the last reference to the object, it will be freed. * -* Takes AioContext lock internally to invoke a job->driver callback. * Called with job lock held. */ void job_unref_locked (Job *job);
Signature · 1 line
2.43.0
Re: [PATCH v2 02/14] scsi: assert that callbacks run in the correct AioContext
In reply to Stefan HajnocziAm 05.12.2023 um 19:19 hat Stefan Hajnoczi geschrieben:
Quoted 7 lines
Since the removal of AioContext locking, the correctness of the code relies on running requests from a single AioContext at any given time.
Add assertions that verify that callbacks are invoked in the correct AioContext.
Signed-off-by Stefan Hajnoczi <[email protected]>
Re: [PATCH v2 07/14] block: remove bdrv_co_lock()
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 4 lines
The
bdrv_co_lock()andbdrv_co_unlock()functions are already no-ops. Remove them.Signed-off-by Stefan Hajnoczi <[email protected]>
Re: [PATCH v2 08/14] scsi: remove AioContext locking
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 4 lines
The AioContext lock no longer has any effect. Remove it.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>
Re: [PATCH v2 09/14] aio-wait: draw equivalence between AIO_WAIT_WHILE() and AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 7 lines
Now that the AioContext lock no longer exists,
AIO_WAIT_WHILE()andAIO_WAIT_WHILE_UNLOCKED()are equivalent.A future patch will get rid of
AIO_WAIT_WHILE_UNLOCKED().Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>
Re: [PATCH v2 10/14] aio: remove aio_context_acquire()/aio_context_release() API
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 22 lines
Delete these functions because nothing calls these functions anymore.
I introduced these APIs in commit
98563fc3ec44("aio: addaio_context_acquire()andaio_context_release()") in 2014. It's with a sigh of relief that I delete these APIs almost 10 years later.Thanks to Paolo Bonzini's vision for multi-queue QEMU, we got an understanding of where the code needed to go in order to remove the limitations that the original dataplane and the IOThread/AioContext approach that followed it.
Emanuele Giuseppe Esposito had the splendid determination to convert large parts of the codebase so that they no longer needed the AioContext lock. This was a painstaking process, both in the actual code changes required and the iterations of code review that Emanuele eked out of Kevin and me over many months.
Kevin Wolf tackled multitudes of graph locking conversions to protect in-flight I/O from run-time changes to the block graph as well as the clang Thread Safety Analysis annotations that allow the compiler to check whether the graph lock is being used correctly.
Quoted 7 lines
And me, well, I'm just here to add some pizzazz to the QEMU multi-queue block layer :). Thank you to everyone who helped with this effort, including Eric Blake, code reviewer extraordinaire, and others who I've forgotten to mention.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>
Re: [PATCH v2 14/14] block: remove outdated AioContext locking comments
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 14 lines
The AioContext lock no longer exists.
There is one noteworthy change:
- * 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.
+ * More specifically, these functions useBDRV_POLL_WHILE(bs), which requires
+ * the caller to be either in the main thread or directly in the home thread
+ * that runs the bs AioContext. Calling them from another thread in another
+ * AioContext would cause deadlocks.
Quoted 7 lines
I am not sure whether deadlocks are still possible. Maybe they have just moved to the fine-grained locks that have replaced the AioContext. Since I am not sure if the deadlocks are gone, I have kept the substance unchanged and just removed mention of the AioContext.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>
I think the deadlock the comment refers to is that AIO_WAIT_WHILE() sits in a blocking aio_poll() and nobody wakes it up because aio_wait_kick() only wakes up the main thread. This deadlock still exists after removing the AioContext lock, so the comment after your change looks right to me.
Kevin Wolf
AppliedAm 05.12.2023 um 19:19 hat Stefan Hajnoczi geschrieben:
Quoted 26 lines
v2:
- Add Patch 2 "scsi: assert that callbacks run in the correct AioContext" [Kevin]
- Add Patch 7 "block: removebdrv_co_lock()" [Eric and Kevin]
- Remove stray goto label in Patch 8 [Kevin]
- Fix "eeked" -> "eked" typo in Patch 10 [Eric]This series removes the AioContext locking APIs from QEMU.
aio_context_acquire()andaio_context_release()are currently only needed to support the locking discipline required byAIO_POLL_WHILE()(except for a stray user that I converted in Patch 1).AIO_POLL_WHILE()doesn't really need the AioContext lock anymore, so it's possible to remove the API. This is a nice simplification because the AioContext locking rules were sometimes tricky or underspecified, leading to many bugs of the years.This patch series removes these APIs across the codebase and cleans up the documentation/comments that refers to them.
Patch 1 is a AioContext lock user I forgot to convert in my earlier SCSI conversion series.
Patch 2 adds an assertion to the SCSI code to ensure that callbacks are invoked in the correct AioContext.
Patch 3 removes tests for the AioContext lock because they will no longer be needed when the lock is gone.
Quoted 7 lines
Patches 4-10 remove the AioContext lock. These can be reviewed by categorizing the call sites into 1. places that take the lock because they call an API that requires the lock (ultimately
AIO_POLL_WHILE()) and 2. places that take the lock to protect state. There should be no instances of case 2 left. If you see one, you've found a bug in this patch series!Patches 11-14 remove comments.
Thanks, applied to the block branch.
Kevin
Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 12 lines
aio_context_acquire()/aio_context_release() has been replaced by fine-grained locking to protect state shared by multiple threads. The AioContext lock still plays the role of balancing locking in
AIO_WAIT_WHILE()and many functions in QEMU either require that the AioContext lock is held or not held for this reason. In other words, the AioContext lock is purely there for consistency with itself and serves no real purpose anymore.Stop actually acquiring/releasing the lock in aio_context_acquire()/aio_context_release() so that subsequent patches can remove callers across the codebase incrementally.
Quoted 7 lines
I have performed "make check" and qemu-iotests stress tests across x86-64, ppc64le, and aarch64 to confirm that there are no failures as a result of eliminating the lock.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Acked-by Kevin Wolf <[email protected]>
I knew why I wasn't confident enough to give a R-b... This crashes qemu-storage-daemon in the qemu-iotests case graph-changes-while-io.
qemu-storage-daemon: ../nbd/server.c:2542: nbd_co_receive_request: Assertion `client->recv_coroutine == qemu_coroutine_self()' failed.
(gdb) bt
#0 0x00007fdb00529884 in __pthread_kill_implementation () from /lib64/libc.so.6 #1 0x00007fdb004d8afe in raise () from /lib64/libc.so.6 #2 0x00007fdb004c187f in abort () from /lib64/libc.so.6 #3 0x00007fdb004c179b in __assert_fail_base.cold () from /lib64/libc.so.6 #4 0x00007fdb004d1187 in __assert_fail () from /lib64/libc.so.6 #5 0x0000557f9f9534eb in nbd_co_receive_request (errp=0x7fdafc25eec0, request=0x7fdafc25ef10, req=0x7fdaf00159c0) at ../nbd/server.c:2542 #6 nbd_trip (opaque=0x557fa0b33fa0) at ../nbd/server.c:2962 #7 0x0000557f9faa416b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #8 0x00007fdb004efe90 in ?? () from /lib64/libc.so.6 #9 0x00007fdafc35f680 in ?? ()
#10 0x0000000000000000 in ?? ()
(gdb) p *client
$2 = {refcount = 4, close_fn = 0x557f9f95dc40 <nbd_blockdev_client_closed>, exp = 0x557fa0b30590, tlscreds = 0x0, tlsauthz = 0x0, sioc = 0x557fa0b33d90, ioc = 0x557fa0b33d90,
recv_coroutine = 0x7fdaf0015eb0, send_lock = {locked = 0, ctx = 0x0, from_push = {slh_first = 0x0}, to_pop = {slh_first = 0x0}, handoff = 0, sequence = 0, holder = 0x0}, send_coroutine = 0x0, read_yielding = false, quiescing = false, next = {tqe_next = 0x0, tqe_circ = {tql_next = 0x0, tql_prev = 0x557fa0b305e8}}, nb_requests = 1, closing = false, check_align = 1, mode = NBD_MODE_EXTENDED, contexts = {exp = 0x557fa0b30590, count = 1, base_allocation = true, allocation_depth = false, bitmaps = 0x0}, opt = 7, optlen = 0}
(gdb) p co_tls_current
$3 = (Coroutine *) 0x7fdaf00061d0
Kevin
Re: [PATCH v2 06/14] block: remove AioContext locking
In reply to Stefan HajnocziAm 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 29 lines
This is the big patch that removes aio_context_acquire()/aio_context_release() from the block layer and affected block layer users.
There isn't a clean way to split this patch and the reviewers are likely the same group of people, so I decided to do it in one patch.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Reviewed-by Paul Durrant <[email protected]>migration/block.c +0
-9@@ -313,22 +311,10 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) block_mig_state.submitted++;blk_mig_unlock (); -/* We do not know if bs is under the main thread (and thus does -* not acquire the AioContext when doing AIO) or rather under -* dataplane. Thus acquire both the iothread mutex and the -* AioContext. -* -* This is ugly and will disappear when we make bdrv_* thread-safe, -* without the need to acquire the AioContext. -*/ -qemu_mutex_lock_iothread ();
migration/block.c static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) · line 320
Quoted 10 lines
migration/block.c +0
-3-aio_context_acquire (blk_get_aio_context (bmds->blk));bdrv_reset_dirty_bitmap (bmds->dirty_bitmap, cur_sector *BDRV_SECTOR_SIZE , nr_sectors *BDRV_SECTOR_SIZE ); blk->aiocb =blk_aio_preadv (bb, cur_sector *BDRV_SECTOR_SIZE , &blk->qiov,0 , blk_mig_read_cb, blk); -aio_context_release (blk_get_aio_context (bmds->blk)); -qemu_mutex_unlock_iothread (); bmds->cur_sector = cur_sector + nr_sectors;return (bmds->cur_sector >= total_sectors);
With this hunk applied, qemu-iotests 183 fails:
(gdb) bt
#0 0x000055aaa7d47c09 in bdrv_graph_co_rdlock () at ../block/graph-lock.c:176 #1 0x000055aaa7d3de2e in graph_lockable_auto_lock (x=<optimized out>) at /home/kwolf/source/qemu/include/block/graph-lock.h:215 #2 blk_co_do_preadv_part (blk=0x7f38a4000f30, offset=0, bytes=1048576, qiov=0x7f38a40250f0, qiov_offset=qiov_offset@entry=0, flags=0) at ../block/block-backend.c:1340 #3 0x000055aaa7d3e006 in blk_aio_read_entry (opaque=0x7f38a4025140) at ../block/block-backend.c:1620 #4 0x000055aaa7e7aa5b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #5 0x00007f38d14dbe90 in __start_context () at /lib64/libc.so.6 #6 0x00007f38b3dfa060 in () #7 0x0000000000000000 in ()
qemu_get_current_aio_context() returns NULL now. I don't completely understand why it depends on the BQL, but adding the BQL locking back fixes it.
Kevin
Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Kevin WolfAm 19.12.2023 um 16:28 hat Kevin Wolf geschrieben:
Quoted 37 lines
Am 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 12 lines
aio_context_acquire()/aio_context_release() has been replaced by fine-grained locking to protect state shared by multiple threads. The AioContext lock still plays the role of balancing locking in
AIO_WAIT_WHILE()and many functions in QEMU either require that the AioContext lock is held or not held for this reason. In other words, the AioContext lock is purely there for consistency with itself and serves no real purpose anymore.Stop actually acquiring/releasing the lock in aio_context_acquire()/aio_context_release() so that subsequent patches can remove callers across the codebase incrementally.
Quoted 7 lines
I have performed "make check" and qemu-iotests stress tests across x86-64, ppc64le, and aarch64 to confirm that there are no failures as a result of eliminating the lock.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Acked-by Kevin Wolf <[email protected]>I knew why I wasn't confident enough to give a R-b... This crashes qemu-storage-daemon in the qemu-iotests case graph-changes-while-io.
qemu-storage-daemon:
../nbd/server.c:2542:nbd_co_receive_request: Assertion `client->recv_coroutine ==qemu_coroutine_self()' failed.(gdb) bt
#0 0x00007fdb00529884 in __pthread_kill_implementation () from /lib64/libc.so.6 #1 0x00007fdb004d8afe in raise () from /lib64/libc.so.6 #2 0x00007fdb004c187f in abort () from /lib64/libc.so.6 #3 0x00007fdb004c179b in __assert_fail_base.cold () from /lib64/libc.so.6 #4 0x00007fdb004d1187 in __assert_fail () from /lib64/libc.so.6 #5 0x0000557f9f9534eb in nbd_co_receive_request (errp=0x7fdafc25eec0, request=0x7fdafc25ef10, req=0x7fdaf00159c0) at ../nbd/server.c:2542 #6 nbd_trip (opaque=0x557fa0b33fa0) at ../nbd/server.c:2962 #7 0x0000557f9faa416b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #8 0x00007fdb004efe90 in ?? () from /lib64/libc.so.6 #9 0x00007fdafc35f680 in ?? ()
Quoted 8 lines
#10
0x0000000000000000in ?? ()
(gdb) p *client
$2 = {refcount = 4,close_fn=0x557f9f95dc40<nbd_blockdev_client_closed>, exp =0x557fa0b30590, tlscreds =0x0, tlsauthz =0x0, sioc =0x557fa0b33d90, ioc =0x557fa0b33d90,
recv_coroutine=0x7fdaf0015eb0,send_lock= {locked = 0, ctx =0x0,from_push= {slh_first=0x0},to_pop= {slh_first=0x0}, handoff = 0, sequence = 0, holder =0x0},send_coroutine=0x0,read_yielding= false, quiescing = false, next = {tqe_next=0x0,tqe_circ= {tql_next=0x0,tql_prev=0x557fa0b305e8}},nb_requests= 1, closing = false,check_align= 1, mode =NBD_MODE_EXTENDED, contexts = {exp =0x557fa0b30590, count = 1,base_allocation= true,allocation_depth= false, bitmaps =0x0}, opt = 7, optlen = 0}
(gdb) pco_tls_current
$3 = (Coroutine *)0x7fdaf00061d0
This one isn't easy to debug...
The first problem here is that two nbd_trip() coroutines are scheduled in the same iothread, and creating the second one overwrites client->recv_coroutine, which triggers the assertion in the first one.
This can be fixed by introducing a new mutex in NBDClient and taking it in nbd_client_receive_next_request() so that there is no race between checking client->recv_coroutine != NULL and setting it to a new coroutine. (Not entirely sure why two different threads are doing this, maybe the main thread reentering in drained_end and the iothread waiting for the next request?)
However, I'm seeing new assertion failures when I do that:client->quiescing isn't set in the -EAGAIN case in nbd_trip(). I haven't really figured out yet where this comes from. Taking the new NBDClient lock in the drain functions and in nbd_trip() doesn't seem to be enough to fix it anyway. Or maybe I didn't quite find the right places to take it.
I'm not sure if the list of NBD clients needs a lock, too, or if it is only ever accessed in the main thread, but that should be unrelated to the assertion failures I'm seeing.
Kevin
Re: [PATCH v2 06/14] block: remove AioContext locking
In reply to Kevin WolfQuoted 54 lines · Kevin Wolf
Am 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 29 lines
This is the big patch that removes aio_context_acquire()/aio_context_release() from the block layer and affected block layer users.
There isn't a clean way to split this patch and the reviewers are likely the same group of people, so I decided to do it in one patch.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Reviewed-by Paul Durrant <[email protected]>migration/block.c +0
-9@@ -313,22 +311,10 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) block_mig_state.submitted++;blk_mig_unlock (); -/* We do not know if bs is under the main thread (and thus does -* not acquire the AioContext when doing AIO) or rather under -* dataplane. Thus acquire both the iothread mutex and the -* AioContext. -* -* This is ugly and will disappear when we make bdrv_* thread-safe, -* without the need to acquire the AioContext. -*/ -qemu_mutex_lock_iothread ();migration/block.c static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) · line 320
Quoted 10 lines
migration/block.c +0
-3-aio_context_acquire (blk_get_aio_context (bmds->blk));bdrv_reset_dirty_bitmap (bmds->dirty_bitmap, cur_sector *BDRV_SECTOR_SIZE , nr_sectors *BDRV_SECTOR_SIZE ); blk->aiocb =blk_aio_preadv (bb, cur_sector *BDRV_SECTOR_SIZE , &blk->qiov,0 , blk_mig_read_cb, blk); -aio_context_release (blk_get_aio_context (bmds->blk)); -qemu_mutex_unlock_iothread (); bmds->cur_sector = cur_sector + nr_sectors;return (bmds->cur_sector >= total_sectors);With this hunk applied, qemu-iotests 183 fails:
(gdb) bt
#0 0x000055aaa7d47c09 in bdrv_graph_co_rdlock () at ../block/graph-lock.c:176 #1 0x000055aaa7d3de2e in graph_lockable_auto_lock (x=<optimized out>) at /home/kwolf/source/qemu/include/block/graph-lock.h:215 #2 blk_co_do_preadv_part (blk=0x7f38a4000f30, offset=0, bytes=1048576, qiov=0x7f38a40250f0, qiov_offset=qiov_offset@entry=0, flags=0) at ../block/block-backend.c:1340 #3 0x000055aaa7d3e006 in blk_aio_read_entry (opaque=0x7f38a4025140) at ../block/block-backend.c:1620 #4 0x000055aaa7e7aa5b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #5 0x00007f38d14dbe90 in __start_context () at /lib64/libc.so.6 #6 0x00007f38b3dfa060 in () #7 0x0000000000000000 in ()
Quoted 3 lines
qemu_get_current_aio_context()returns NULL now. I don't completely understand why it depends on the BQL, but adding the BQL locking back fixes it.
Thanks for letting me know. I have reviewed migration/block.c and agree that taking the BQL is correct.
I have inlined the fix below and will resend this patch.
Stefan
---
migration/block.c +0 -0
@@ -311,10 +311,17 @@ static int mig_save_device_bulk(QEMUFile *f,
BlkMigDevState *bmds)
block_mig_state.submitted++;
blk_mig_unlock();+ /*
+ * The migration thread does not have an AioContext. Lock the BQL so that
+ * I/O runs in the main loop AioContext (see
+ * qemu_get_current_aio_context()).
+ */
+ qemu_mutex_lock_iothread();
bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector * BDRV_SECTOR_SIZE,
nr_sectors * BDRV_SECTOR_SIZE);
blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
0, blk_mig_read_cb, blk);+ qemu_mutex_unlock_iothread();
bmds->cur_sector = cur_sector + nr_sectors;
return (bmds->cur_sector >= total_sectors);Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Kevin WolfThe following hack makes the test pass but there are larger safety issues that I'll need to look at on Wednesday:
nbd/server.c +8 -1
@@ -1617,7 +1617,7 @@ static void nbd_drained_begin(void *opaque) } } -static void nbd_drained_end (void *opaque) +static void nbd_resume_clients (void *opaque) {NBDExport *exp = opaque;NBDClient *client;
@@ -1628,6 +1628,15 @@ static void nbd_drained_end(void *opaque) } } +static void nbd_drained_end (void *opaque) +{ +NBDExport *exp = opaque; + +/* TODO how to make sure exp doesn't go away? */ +/* TODO what if AioContext changes before this runs? */ + aio_bh_schedule_oneshot(nbd_export_aio_context(exp),
nbd_resume_clients, exp);
+}
+
static bool nbd_drained_poll(void *opaque)
{
NBDExport *exp = opaque;Re: [PATCH v2 06/14] block: remove AioContext locking
In reply to Stefan HajnocziAm 19.12.2023 um 21:04 hat Stefan Hajnoczi geschrieben:
Quoted 55 lines · Kevin Wolf
Quoted 54 lines
Am 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 29 lines
This is the big patch that removes aio_context_acquire()/aio_context_release() from the block layer and affected block layer users.
There isn't a clean way to split this patch and the reviewers are likely the same group of people, so I decided to do it in one patch.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Reviewed-by Paul Durrant <[email protected]>migration/block.c +0
-9@@ -313,22 +311,10 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) block_mig_state.submitted++;blk_mig_unlock (); -/* We do not know if bs is under the main thread (and thus does -* not acquire the AioContext when doing AIO) or rather under -* dataplane. Thus acquire both the iothread mutex and the -* AioContext. -* -* This is ugly and will disappear when we make bdrv_* thread-safe, -* without the need to acquire the AioContext. -*/ -qemu_mutex_lock_iothread ();migration/block.c static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) · line 320
Quoted 10 lines
migration/block.c +0
-3-aio_context_acquire (blk_get_aio_context (bmds->blk));bdrv_reset_dirty_bitmap (bmds->dirty_bitmap, cur_sector *BDRV_SECTOR_SIZE , nr_sectors *BDRV_SECTOR_SIZE ); blk->aiocb =blk_aio_preadv (bb, cur_sector *BDRV_SECTOR_SIZE , &blk->qiov,0 , blk_mig_read_cb, blk); -aio_context_release (blk_get_aio_context (bmds->blk)); -qemu_mutex_unlock_iothread (); bmds->cur_sector = cur_sector + nr_sectors;return (bmds->cur_sector >= total_sectors);With this hunk applied, qemu-iotests 183 fails:
(gdb) bt
#0 0x000055aaa7d47c09 in bdrv_graph_co_rdlock () at ../block/graph-lock.c:176 #1 0x000055aaa7d3de2e in graph_lockable_auto_lock (x=<optimized out>) at /home/kwolf/source/qemu/include/block/graph-lock.h:215 #2 blk_co_do_preadv_part (blk=0x7f38a4000f30, offset=0, bytes=1048576, qiov=0x7f38a40250f0, qiov_offset=qiov_offset@entry=0, flags=0) at ../block/block-backend.c:1340 #3 0x000055aaa7d3e006 in blk_aio_read_entry (opaque=0x7f38a4025140) at ../block/block-backend.c:1620 #4 0x000055aaa7e7aa5b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #5 0x00007f38d14dbe90 in __start_context () at /lib64/libc.so.6 #6 0x00007f38b3dfa060 in () #7 0x0000000000000000 in ()
Quoted 8 lines
Quoted 3 lines
qemu_get_current_aio_context()returns NULL now. I don't completely understand why it depends on the BQL, but adding the BQL locking back fixes it.Thanks for letting me know. I have reviewed
migration/block.cand agree that taking the BQL is correct.I have inlined the fix below and will resend this patch.
Thanks, I'll squash this into the queued patch. No need to resend, as far as I am concerned.
Kevin
Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Stefan HajnocziAm 19.12.2023 um 22:23 hat Stefan Hajnoczi geschrieben:
Quoted 2 lines
The following hack makes the test pass but there are larger safety issues that I'll need to look at on Wednesday:
I see, you're taking the same approach as in the SCSI layer: Don't make things thread-safe, but just always access them from the same thread.
In theory this should be okay, but I'm almost sure that at least nbd_drained_poll() must then run in the same AioContext, too.
Quoted 14 lines
nbd/server.c +1
-1@@ -1617,7 +1617,7 @@ static void nbd_drained_begin(void *opaque) } } -static void nbd_drained_end (void *opaque) +static void nbd_resume_clients (void *opaque) {NBDExport *exp = opaque;NBDClient *client;
nbd/server.c static void nbd_drained_end(void *opaque) · line 1635
Quoted 8 lines
nbd/server.c +5
-0@@ -1628,6 +1628,15 @@ static void nbd_drained_end(void *opaque) } } +static void nbd_drained_end (void *opaque) +{ +NBDExport *exp = opaque; + +/* TODO how to make sure exp doesn't go away? */
blk_exp_ref()?
nbd/server.c static void nbd_drained_end(void *opaque)
Quoted 3 lines
nbd/server.c +2
-0+/* TODO what if AioContext changes before this runs? */ +aio_bh_schedule_oneshot (nbd_export_aio_context (exp), nbd_resume_clients, exp);
We could increase client->nb_requests if we change it to be accessed atomically. Then nbd_drained_poll() will make any AioContext change wait for the BH.
Or maybe aio_wait_bh_oneshot() would already solve both problems?
nbd/server.c static void nbd_drained_end(void *opaque) · line 1640
Quoted 2 lines
nbd/server.c +2
-0+} +
Kevin
Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
Quoted 7 lines · Kevin Wolf
Am 19.12.2023 um 22:23 hat Stefan Hajnoczi geschrieben:
Quoted 2 lines
The following hack makes the test pass but there are larger safety issues that I'll need to look at on Wednesday:
I see, you're taking the same approach as in the SCSI layer: Don't make things thread-safe, but just always access them from the same thread.
Yes, but it feels like a hack to me. You pointed out that other parts also don't look thread-safe (e.g. the clients list) and I agree. I've started annotating the code and will try to come up with a full fix today.
Stefan
Re: [PATCH v2 04/14] aio: make aio_context_acquire()/aio_context_release() a no-op
In reply to Kevin WolfQuoted 61 lines · Kevin Wolf
Am 19.12.2023 um 16:28 hat Kevin Wolf geschrieben:
Quoted 37 lines
Am 05.12.2023 um 19:20 hat Stefan Hajnoczi geschrieben:
Quoted 12 lines
aio_context_acquire()/aio_context_release() has been replaced by fine-grained locking to protect state shared by multiple threads. The AioContext lock still plays the role of balancing locking in
AIO_WAIT_WHILE()and many functions in QEMU either require that the AioContext lock is held or not held for this reason. In other words, the AioContext lock is purely there for consistency with itself and serves no real purpose anymore.Stop actually acquiring/releasing the lock in aio_context_acquire()/aio_context_release() so that subsequent patches can remove callers across the codebase incrementally.
Quoted 7 lines
I have performed "make check" and qemu-iotests stress tests across x86-64, ppc64le, and aarch64 to confirm that there are no failures as a result of eliminating the lock.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Eric Blake <[email protected]>Acked-by Kevin Wolf <[email protected]>I knew why I wasn't confident enough to give a R-b... This crashes qemu-storage-daemon in the qemu-iotests case graph-changes-while-io.
qemu-storage-daemon:
../nbd/server.c:2542:nbd_co_receive_request: Assertion `client->recv_coroutine ==qemu_coroutine_self()' failed.(gdb) bt
#0 0x00007fdb00529884 in __pthread_kill_implementation () from /lib64/libc.so.6 #1 0x00007fdb004d8afe in raise () from /lib64/libc.so.6 #2 0x00007fdb004c187f in abort () from /lib64/libc.so.6 #3 0x00007fdb004c179b in __assert_fail_base.cold () from /lib64/libc.so.6 #4 0x00007fdb004d1187 in __assert_fail () from /lib64/libc.so.6 #5 0x0000557f9f9534eb in nbd_co_receive_request (errp=0x7fdafc25eec0, request=0x7fdafc25ef10, req=0x7fdaf00159c0) at ../nbd/server.c:2542 #6 nbd_trip (opaque=0x557fa0b33fa0) at ../nbd/server.c:2962 #7 0x0000557f9faa416b in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at ../util/coroutine-ucontext.c:177 #8 0x00007fdb004efe90 in ?? () from /lib64/libc.so.6 #9 0x00007fdafc35f680 in ?? ()Quoted 8 lines
#10
0x0000000000000000in ?? ()
(gdb) p *client
$2 = {refcount = 4,close_fn=0x557f9f95dc40<nbd_blockdev_client_closed>, exp =0x557fa0b30590, tlscreds =0x0, tlsauthz =0x0, sioc =0x557fa0b33d90, ioc =0x557fa0b33d90,
recv_coroutine=0x7fdaf0015eb0,send_lock= {locked = 0, ctx =0x0,from_push= {slh_first=0x0},to_pop= {slh_first=0x0}, handoff = 0, sequence = 0, holder =0x0},send_coroutine=0x0,read_yielding= false, quiescing = false, next = {tqe_next=0x0,tqe_circ= {tql_next=0x0,tql_prev=0x557fa0b305e8}},nb_requests= 1, closing = false,check_align= 1, mode =NBD_MODE_EXTENDED, contexts = {exp =0x557fa0b30590, count = 1,base_allocation= true,allocation_depth= false, bitmaps =0x0}, opt = 7, optlen = 0}
(gdb) pco_tls_current
$3 = (Coroutine *)0x7fdaf00061d0This one isn't easy to debug...
The first problem here is that two
nbd_trip()coroutines are scheduled in the same iothread, and creating the second one overwritesclient->recv_coroutine, which triggers the assertion in the first one.This can be fixed by introducing a new mutex in NBDClient and taking it in
nbd_client_receive_next_request()so that there is no race between checkingclient->recv_coroutine!= NULL and setting it to a new coroutine. (Not entirely sure why two different threads are doing this, maybe the main thread reentering indrained_endand the iothread waiting for the next request?)
Quoted 6 lines
However, I'm seeing new assertion failures when I do that:
client->quiescingisn't set in the-EAGAINcase innbd_trip(). I haven't really figured out yet where this comes from. Taking the new NBDClient lock in the drain functions and innbd_trip()doesn't seem to be enough to fix it anyway. Or maybe I didn't quite find the right places to take it.
bdrv_graph_wrlock() -> bdrv_drain_all_begin_nopoll() followed by bdrv_drain_all_end() causes this issue.
It's a race condition where nbd_trip() in the IOThread sees client->quiescing == true for a moment but then the drained region ends before nbd_trip() re-acquires the lock and reaches assert(client->quiescing).
The "nopoll" part of bdrv_drain_all_begin_nopoll() seems to be the issue. We cannot assume all requests have quiesced when .drained_end() is called.
I'm running more tests now to be sure I have a working solution. Will send patches soon.
Stefan