block-backend: process I/O in the current AioContext
v3 cover block-backend: process I/O in the current AioContext
v3
- Add Patch 2 to fix a race condition in test-bdrv-drain. This was the CI failure that bumped this patch series from Kevin's pull request.
- Add missing 051.pc.out file. I tried qemu-system-aarch64 to see of 051.out also needs to be updated, but no changes were necessary. [Kevin]
v2
- Add patch to remove AIOCBInfo->get_aio_context() [Kevin]
- Add patch to use qemu_get_current_aio_context() in block-coroutine-wrapper so that the wrappers use the current AioContext instead of bdrv_get_aio_context().
Switch blk_aio_*() APIs over to multi-queue by using qemu_get_current_aio_context() instead of blk_get_aio_context(). This change will allow devices to process I/O in multiple IOThreads in the future.
The final patch requires my QIOChannel AioContext series to pass tests/qemu-iotests/check -qcow2 281 because the nbd block driver is now accessed from the main loop thread in addition to the IOThread:
https://lore.kernel.org/qemu-devel/[email protected]/T/#t
Based-on: [email protected]
Stefan Hajnoczi (5):
block: remove AIOCBInfo->get_aio_context()
test-bdrv-drain: avoid race with BH in IOThread drain test
block-backend: process I/O in the current AioContext
block-backend: process zoned requests in the current AioContext
block-coroutine-wrapper: use qemu_get_current_aio_context()
include/block/aio.h | 1 - include/block/block-global-state.h | 2 ++ include/block/block-io.h | 1 - block/block-backend.c | 35 ++++++++---------------------- block/io.c | 23 +++++++------------- hw/nvme/ctrl.c | 7 ------ softmmu/dma-helpers.c | 8 ------- tests/unit/test-bdrv-drain.c | 8 +++++++ util/thread-pool.c | 8 ------- scripts/block-coroutine-wrapper.py | 6 ++--- tests/qemu-iotests/051.pc.out | 4 ++-- 11 files changed, 31 insertions(+), 72 deletions(-)
Signature · 1 line
2.41.0
PATCH v3 1/5 block: remove AIOCBInfo->get_aio_context()
The synchronous bdrv_aio_cancel() function needs the acb's AioContext so it can call aio_poll() to wait for cancellation.
It turns out that all users run under the BQL in the main AioContext, so this callback is not needed.
Remove the callback, mark bdrv_aio_cancel() GLOBAL_STATE_CODE just like its blk_aio_cancel() caller, and poll the main loop AioContext.
The purpose of this cleanup is to identify bdrv_aio_cancel() as an API that does not work with the multi-queue block layer.
--- include/block/aio.h | 1 - include/block/block-global-state.h | 2 ++ include/block/block-io.h | 1 - block/block-backend.c | 17 ----------------- block/io.c | 23 ++++++++--------------- hw/nvme/ctrl.c | 7 ------- softmmu/dma-helpers.c | 8 -------- util/thread-pool.c | 8 -------- 8 files changed, 10 insertions(+), 57 deletions(-)
include/block/aio.h +0 -1
@@ -31,7 +31,6 @@ typedef void BlockCompletionFunc(void *opaque, int ret);typedef struct AIOCBInfo {void (*cancel_async)(BlockAIOCB *acb); -AioContext *(*get_aio_context)(BlockAIOCB *acb);size_t aiocb_size; }AIOCBInfo ;
include/block/block-global-state.h +2 -0
@@ -185,6 +185,8 @@ void bdrv_drain_all_begin_nopoll(void);void bdrv_drain_all_end (void );void bdrv_drain_all (void ); +void bdrv_aio_cancel (BlockAIOCB *acb); +int bdrv_has_zero_init_1 (BlockDriverState *bs);int bdrv_has_zero_init (BlockDriverState *bs);BlockDriverState *bdrv_find_node (const char *node_name);
include/block/block-io.h +0 -1
@@ -101,7 +101,6 @@ bdrv_co_delete_file_noerr(BlockDriverState *bs);/* async block I/O */ -void bdrv_aio_cancel (BlockAIOCB *acb);void bdrv_aio_cancel_async (BlockAIOCB *acb);/* sg packet commands */
block/block-backend.c +0 -17
@@ -33,8 +33,6 @@#define NOT_DONE 0x7fffffff/* used while emulated sync operation in progress */ -static AioContext *blk_aiocb_get_aio_context (BlockAIOCB *acb); - typedef struct BlockBackendAioNotifier { void (*attached_aio_context)(AioContext *new_context, void *opaque); void (*detach_aio_context)(void *opaque);
@@ -103,7 +101,6 @@ typedef struct BlockBackendAIOCB { } BlockBackendAIOCB;static const AIOCBInfo block_backend_aiocb_info = { - .get_aio_context = blk_aiocb_get_aio_context, .aiocb_size =sizeof (BlockBackendAIOCB), };
@@ -1545,16 +1542,8 @@ typedef struct BlkAioEmAIOCB {bool has_returned; } BlkAioEmAIOCB; -static AioContext *blk_aio_em_aiocb_get_aio_context (BlockAIOCB *acb_) -{ -BlkAioEmAIOCB *acb =container_of (acb_, BlkAioEmAIOCB, common); - -return blk_get_aio_context (acb->rwco.blk); -} -static const AIOCBInfo blk_aio_em_aiocb_info = { .aiocb_size =sizeof (BlkAioEmAIOCB), - .get_aio_context = blk_aio_em_aiocb_get_aio_context, };static void blk_aio_complete (BlkAioEmAIOCB *acb)
@@ -2434,12 +2423,6 @@ AioContext *blk_get_aio_context(BlockBackend *blk)return blk->ctx; } -static AioContext *blk_aiocb_get_aio_context (BlockAIOCB *acb) -{ -BlockBackendAIOCB *blk_acb =DO_UPCAST (BlockBackendAIOCB, common, acb); -return blk_get_aio_context (blk_acb->blk); -} -int blk_set_aio_context (BlockBackend *blk,AioContext *new_context,Error **errp) {
block/io.c +8 -15
@@ -2950,25 +2950,18 @@ int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,/**************************************************************/ /* async I/Os */ +/** +* Synchronously cancels an acb. Must be called with the BQL held and the acb +* must be processed with the BQL held too (IOThreads are not allowed). +* +* Use bdrv_aio_cancel_async() instead when possible. +*/ void bdrv_aio_cancel (BlockAIOCB *acb) { -IO_CODE (); +GLOBAL_STATE_CODE ();qemu_aio_ref (acb);bdrv_aio_cancel_async (acb); -while (acb->refcnt >1 ) { -if (acb->aiocb_info->get_aio_context) { -aio_poll (acb->aiocb_info->get_aio_context (acb), true); - }else if (acb->bs) { -/* qemu_aio_ref and qemu_aio_unref are not thread-safe, so -* assert that we're not using an I/O thread. Thread-safe -* code should use bdrv_aio_cancel_async exclusively. -*/ -assert (bdrv_get_aio_context (acb->bs) ==qemu_get_aio_context ()); -aio_poll (bdrv_get_aio_context (acb->bs), true); - }else { -abort (); - } - } +AIO_WAIT_WHILE_UNLOCKED (NULL , acb->refcnt >1 );qemu_aio_unref (acb); }
hw/nvme/ctrl.c +0 -7
@@ -2130,11 +2130,6 @@ static inline bool nvme_is_write(NvmeRequest *req) rw->opcode ==NVME_CMD_WRITE_ZEROES ; } -static AioContext *nvme_get_aio_context (BlockAIOCB *acb) -{ -return qemu_get_aio_context (); -} -static void nvme_misc_cb (void *opaque,int ret) {NvmeRequest *req = opaque;
@@ -3302,7 +3297,6 @@ static void nvme_flush_cancel(BlockAIOCB *acb)static const AIOCBInfo nvme_flush_aiocb_info = { .aiocb_size =sizeof (NvmeFlushAIOCB), .cancel_async = nvme_flush_cancel, - .get_aio_context = nvme_get_aio_context, };static void nvme_do_flush (NvmeFlushAIOCB *iocb);
@@ -6478,7 +6472,6 @@ static void nvme_format_cancel(BlockAIOCB *aiocb)static const AIOCBInfo nvme_format_aiocb_info = { .aiocb_size =sizeof (NvmeFormatAIOCB), .cancel_async = nvme_format_cancel, - .get_aio_context = nvme_get_aio_context, }; static void nvme_format_set(NvmeNamespace *ns, uint8_t lbaf, uint8_t mset,
softmmu/dma-helpers.c +0 -8
@@ -206,17 +206,9 @@ static void dma_aio_cancel(BlockAIOCB *acb) } } -static AioContext *dma_get_aio_context (BlockAIOCB *acb) -{ -DMAAIOCB *dbs =container_of (acb,DMAAIOCB , common); - -return dbs->ctx; -} -static const AIOCBInfo dma_aiocb_info = { .aiocb_size =sizeof (DMAAIOCB ), .cancel_async = dma_aio_cancel, - .get_aio_context = dma_get_aio_context, }; BlockAIOCB *dma_blk_io(AioContext *ctx,
util/thread-pool.c +0 -8
@@ -228,17 +228,9 @@ static void thread_pool_cancel(BlockAIOCB *acb) } -static AioContext *thread_pool_get_aio_context (BlockAIOCB *acb) -{ -ThreadPoolElement *elem = (ThreadPoolElement *)acb; -ThreadPool *pool = elem->pool; -return pool->ctx; -} -static const AIOCBInfo thread_pool_aiocb_info = { .aiocb_size =sizeof (ThreadPoolElement), .cancel_async = thread_pool_cancel, - .get_aio_context = thread_pool_get_aio_context, }; BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
Signature · 1 line
2.41.0
Re: [PATCH v3 1/5] block: remove AIOCBInfo->get_aio_context()
Quoted 6 lines · Stefan Hajnoczi
The synchronous
bdrv_aio_cancel()function needs the acb's AioContext so it can callaio_poll()to wait for cancellation.It turns out that all users run under the BQL in the main AioContext, so this callback is not needed.
Quoted 8 lines
Remove the callback, mark
bdrv_aio_cancel()GLOBAL_STATE_CODEjust like itsblk_aio_cancel()caller, and poll the main loop AioContext.The purpose of this cleanup is to identify
bdrv_aio_cancel()as an API that does not work with the multi-queue block layer.Signed-off-by Stefan Hajnoczi <[email protected]>---
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH v3 1/5] block: remove AIOCBInfo->get_aio_context()
In reply to Stefan HajnocziQuoted 49 lines · Stefan Hajnoczi
The synchronous
bdrv_aio_cancel()function needs the acb's AioContext so it can callaio_poll()to wait for cancellation.It turns out that all users run under the BQL in the main AioContext, so this callback is not needed.
Remove the callback, mark
bdrv_aio_cancel()GLOBAL_STATE_CODEjust like itsblk_aio_cancel()caller, and poll the main loop AioContext.The purpose of this cleanup is to identify
bdrv_aio_cancel()as an API that does not work with the multi-queue block layer.Signed-off-by Stefan Hajnoczi <[email protected]>--- include/block/aio.h | 1 - include/block/block-global-state.h | 2 ++ include/block/block-io.h | 1 - block/block-backend.c | 17 ----------------- block/io.c | 23 ++++++++--------------- hw/nvme/ctrl.c | 7 ------- softmmu/dma-helpers.c | 8 -------- util/thread-pool.c | 8 -------- 8 files changed, 10 insertions(+), 57 deletions(-)hw/nvme/ctrl.c +0
-6@@ -2130,11 +2130,6 @@ static inline bool nvme_is_write(NvmeRequest *req) rw->opcode ==NVME_CMD_WRITE_ZEROES ; } -static AioContext *nvme_get_aio_context (BlockAIOCB *acb) -{ -return qemu_get_aio_context (); -} -static void nvme_misc_cb (void *opaque,int ret) {NvmeRequest *req = opaque;@@ -3302,7 +3297,6 @@ static void nvme_flush_cancel(BlockAIOCB *acb)static const AIOCBInfo nvme_flush_aiocb_info = { .aiocb_size =sizeof (NvmeFlushAIOCB), .cancel_async = nvme_flush_cancel, - .get_aio_context = nvme_get_aio_context, };static void nvme_do_flush (NvmeFlushAIOCB *iocb);
hw/nvme/ctrl.c static void nvme_format_cancel(BlockAIOCB *aiocb) · line 6477
Quoted 7 lines
hw/nvme/ctrl.c +0
-1@@ -6478,7 +6472,6 @@ static void nvme_format_cancel(BlockAIOCB *aiocb)static const AIOCBInfo nvme_format_aiocb_info = { .aiocb_size =sizeof (NvmeFormatAIOCB), .cancel_async = nvme_format_cancel, - .get_aio_context = nvme_get_aio_context, }; static void nvme_format_set(NvmeNamespace *ns, uint8_t lbaf, uint8_t mset,
PATCH v3 2/5 test-bdrv-drain: avoid race with BH in IOThread drain test
In reply to Stefan HajnocziThis patch fixes a race condition in test-bdrv-drain that is difficult to reproduce. test-bdrv-drain sometimes fails without an error message on the block pull request sent by Kevin Wolf on Sep 4, 2023. I was able to reproduce it locally and found that "block-backend: process I/O in the current AioContext" (in this patch series) is the first commit where it reproduces.
I do not know why "block-backend: process I/O in the current AioContext" exposes this bug. It might be related to the fact that the test's preadv request runs in the main thread instead of IOThread a after my commit. That might simply change the timing of the test.
Now on to the race condition in test-bdrv-drain. The main thread schedules a BH in IOThread a and then drains the BDS:
aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
/* The request is running on the IOThread a. Draining its block device
* will make sure that it has completed as far as the BDS is concerned,
* but the drain in this thread can continue immediately after
* bdrv_dec_in_flight() and aio_ret might be assigned only slightly
* later. */
do_drain_begin(drain_type, bs);
If the BH completes before do_drain_begin() then there is nothing to worry about.
If the BH invokes bdrv_flush() before do_drain_begin(), then do_drain_begin() waits for it to complete.
The problematic case is when do_drain_begin() runs before the BH enters bdrv_flush(). Then do_drain_begin() misses the BH and the drain mechanism has failed in quiescing I/O.
Fix this by incrementing the in_flight counter so that do_drain_begin() waits for test_iothread_main_thread_bh().
--- tests/unit/test-bdrv-drain.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tests/unit/test-bdrv-drain.c +8 -0
@@ -512,6 +512,7 @@ static void test_iothread_main_thread_bh(void *opaque)* executed during drain, otherwise this would deadlock. */ aio_context_acquire (bdrv_get_aio_context (data->bs));bdrv_flush (data->bs); +bdrv_dec_in_flight (data->bs);/* incremented by test_iothread_common() */ aio_context_release (bdrv_get_aio_context (data->bs)); }
@@ -583,6 +584,13 @@ static void test_iothread_common(enum drain_type drain_type, int drain_thread)aio_context_acquire (ctx_a); } +/* +* Increment in_flight so that do_drain_begin() waits for +* test_iothread_main_thread_bh(). This prevents the race between +* test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in +* this thread. test_iothread_main_thread_bh() decrements in_flight. +*/ +bdrv_inc_in_flight (bs);aio_bh_schedule_oneshot (ctx_a, test_iothread_main_thread_bh, &data);/* The request is running on the IOThread a. Draining its block device
Signature · 1 line
2.41.0
Re: [PATCH v3 2/5] test-bdrv-drain: avoid race with BH in IOThread drain test
Quoted 10 lines · Stefan Hajnoczi
This patch fixes a race condition in test-bdrv-drain that is difficult to reproduce. test-bdrv-drain sometimes fails without an error message on the block pull request sent by Kevin Wolf on Sep 4, 2023. I was able to reproduce it locally and found that "block-backend: process I/O in the current AioContext" (in this patch series) is the first commit where it reproduces.
I do not know why "block-backend: process I/O in the current AioContext" exposes this bug. It might be related to the fact that the test's preadv request runs in the main thread instead of IOThread a after my commit.
In reading the commit message before the impacted code, my first thought was that you had a typo of an extra word (that is, something to fix by s/a //), but reading further, a better fix would be calling attention to the fact that you are referencing a specific named thread, as in s/IOThread a/IOThread A/...
Quoted 4 lines
That might simply change the timing of the test.
Now on to the race condition in test-bdrv-drain. The main thread schedules a BH in IOThread a and then drains the BDS:
...and another spot with the same parse issue...
Quoted 4 lines
aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);/* The request is running on the IOThread a. Draining its block device
...but here you were quoting from the existing code base, which is where I finally realized it was more than just your commit message.
Quoted 40 lines
* will make sure that it has completed as far as the BDS is concerned,
* but the drain in this thread can continue immediately after
*bdrv_dec_in_flight()andaio_retmight be assigned only slightly
* later. */
do_drain_begin(drain_type, bs);If the BH completes before
do_drain_begin()then there is nothing to worry about.If the BH invokes
bdrv_flush()beforedo_drain_begin(), thendo_drain_begin()waits for it to complete.The problematic case is when
do_drain_begin()runs before the BH entersbdrv_flush(). Thendo_drain_begin()misses the BH and the drain mechanism has failed in quiescing I/O.Fix this by incrementing the
in_flightcounter so thatdo_drain_begin()waits fortest_iothread_main_thread_bh().Signed-off-by Stefan Hajnoczi <[email protected]>--- tests/unit/test-bdrv-drain.c | 8 ++++++++ 1 file changed, 8 insertions(+)tests/unit/test-bdrv-drain.c +1
-0@@ -512,6 +512,7 @@ static void test_iothread_main_thread_bh(void *opaque)* executed during drain, otherwise this would deadlock. */ aio_context_acquire (bdrv_get_aio_context (data->bs));bdrv_flush (data->bs); +bdrv_dec_in_flight (data->bs);/* incremented by test_iothread_common() */ aio_context_release (bdrv_get_aio_context (data->bs)); }@@ -583,6 +584,13 @@ static void test_iothread_common(enum drain_type drain_type, int drain_thread)aio_context_acquire (ctx_a); }
tests/unit/test-bdrv-drain.c static void test_iothread_common(enum drain_type drain_type, int drain_thread) · line 596
Quoted 10 lines
tests/unit/test-bdrv-drain.c +7
-0+/* +* Increment in_flight so that do_drain_begin() waits for +* test_iothread_main_thread_bh(). This prevents the race between +* test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in +* this thread. test_iothread_main_thread_bh() decrements in_flight. +*/ +bdrv_inc_in_flight (bs);aio_bh_schedule_oneshot (ctx_a, test_iothread_main_thread_bh, &data);/* The request is running on the IOThread a. Draining its block device
and indeed, your commit message is consistent with the current code's naming convention. If you have reason to respin, a pre-req patch to change the case before adding more references might be nice, but I won't insist.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH v3 2/5] test-bdrv-drain: avoid race with BH in IOThread drain test
Quoted 82 lines · Eric Blake
Quoted 10 lines · Stefan Hajnoczi
This patch fixes a race condition in test-bdrv-drain that is difficult to reproduce. test-bdrv-drain sometimes fails without an error message on the block pull request sent by Kevin Wolf on Sep 4, 2023. I was able to reproduce it locally and found that "block-backend: process I/O in the current AioContext" (in this patch series) is the first commit where it reproduces.
I do not know why "block-backend: process I/O in the current AioContext" exposes this bug. It might be related to the fact that the test's preadv request runs in the main thread instead of IOThread a after my commit.
In reading the commit message before the impacted code, my first thought was that you had a typo of an extra word (that is, something to fix by s/a //), but reading further, a better fix would be calling attention to the fact that you are referencing a specific named thread, as in s/IOThread a/IOThread A/...
Quoted 4 lines
That might simply change the timing of the test.
Now on to the race condition in test-bdrv-drain. The main thread schedules a BH in IOThread a and then drains the BDS:
...and another spot with the same parse issue...
Quoted 4 lines
aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);/* The request is running on the IOThread a. Draining its block device
...but here you were quoting from the existing code base, which is where I finally realized it was more than just your commit message.
Quoted 49 lines
* will make sure that it has completed as far as the BDS is concerned,
* but the drain in this thread can continue immediately after
*bdrv_dec_in_flight()andaio_retmight be assigned only slightly
* later. */
do_drain_begin(drain_type, bs);If the BH completes before
do_drain_begin()then there is nothing to worry about.If the BH invokes
bdrv_flush()beforedo_drain_begin(), thendo_drain_begin()waits for it to complete.The problematic case is when
do_drain_begin()runs before the BH entersbdrv_flush(). Thendo_drain_begin()misses the BH and the drain mechanism has failed in quiescing I/O.Fix this by incrementing the
in_flightcounter so thatdo_drain_begin()waits fortest_iothread_main_thread_bh().Signed-off-by Stefan Hajnoczi <[email protected]>--- tests/unit/test-bdrv-drain.c | 8 ++++++++ 1 file changed, 8 insertions(+)tests/unit/test-bdrv-drain.c +8
-0@@ -512,6 +512,7 @@ static void test_iothread_main_thread_bh(void *opaque)* executed during drain, otherwise this would deadlock. */ aio_context_acquire (bdrv_get_aio_context (data->bs));bdrv_flush (data->bs); +bdrv_dec_in_flight (data->bs);/* incremented by test_iothread_common() */ aio_context_release (bdrv_get_aio_context (data->bs)); }@@ -583,6 +584,13 @@ static void test_iothread_common(enum drain_type drain_type, int drain_thread)aio_context_acquire (ctx_a); } +/* +* Increment in_flight so that do_drain_begin() waits for +* test_iothread_main_thread_bh(). This prevents the race between +* test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in +* this thread. test_iothread_main_thread_bh() decrements in_flight. +*/ +bdrv_inc_in_flight (bs);aio_bh_schedule_oneshot (ctx_a, test_iothread_main_thread_bh, &data);
Quoted 8 lines
tests/unit/test-bdrv-drain.c static void test_iothread_common(enum drain_type drain_type, int drain_thread) · line 596
Quoted 1 line
tests/unit/test-bdrv-drain.c +0
-0/* The request is running on the IOThread a. Draining its block device and indeed, your commit message is consistent with the current code's naming convention. If you have reason to respin, a pre-req patch to change the case before adding more references might be nice, but I won't insist.
Reviewed-by Eric Blake <[email protected]>
Sorry about that. It is confusing.
Stefan
PATCH v3 3/5 block-backend: process I/O in the current AioContext
In reply to Stefan HajnocziSwitch blk_aio_*() APIs over to multi-queue by using qemu_get_current_aio_context() instead of blk_get_aio_context(). This change will allow devices to process I/O in multiple IOThreads in the future.
I audited existing blk_aio_*() callers:
- migration/block.c: blk_mig_lock() protects the data accessed by the completion callback.
- The remaining emulated devices and exports run with qemu_get_aio_context() == blk_get_aio_context().
--- block/block-backend.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
block/block-backend.c +3 -3
@@ -1530,7 +1530,7 @@ BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, acb->blk = blk; acb->ret = ret; -replay_bh_schedule_oneshot_event (blk_get_aio_context (blk), +replay_bh_schedule_oneshot_event (qemu_get_current_aio_context (), error_callback_bh, acb);return &acb->common; }
@@ -1584,11 +1584,11 @@ static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, acb->has_returned = false; co =qemu_coroutine_create (co_entry, acb); -aio_co_enter (blk_get_aio_context (blk), co); +aio_co_enter (qemu_get_current_aio_context (), co); acb->has_returned = true;if (acb->rwco.ret !=NOT_DONE ) { -replay_bh_schedule_oneshot_event (blk_get_aio_context (blk), +replay_bh_schedule_oneshot_event (qemu_get_current_aio_context (), blk_aio_complete_bh, acb); }
Signature · 1 line
2.41.0
Quoted 11 lines · Stefan Hajnoczi
Switch blk_aio_*() APIs over to multi-queue by using
qemu_get_current_aio_context()instead ofblk_get_aio_context(). This change will allow devices to process I/O in multiple IOThreads in the future.I audited existing blk_aio_*() callers:
-migration/block.c:blk_mig_lock()protects the data accessed by the completion callback.
- The remaining emulated devices and exports run withqemu_get_aio_context()==blk_get_aio_context().
Quoted 5 lines
Signed-off-by Stefan Hajnoczi <[email protected]>--- block/block-backend.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH v3 4/5 block-backend: process zoned requests in the current AioContext
In reply to Stefan HajnocziProcess zoned requests in the current thread's AioContext instead of in the BlockBackend's AioContext.
There is no need to use the BlockBackend's AioContext thanks to CoMutex bs->wps->colock, which protects zone metadata.
--- block/block-backend.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
block/block-backend.c +6 -6
@@ -1890,11 +1890,11 @@ BlockAIOCB *blk_aio_zone_report(BlockBackend *blk, int64_t offset, acb->has_returned = false; co =qemu_coroutine_create (blk_aio_zone_report_entry, acb); -aio_co_enter (blk_get_aio_context (blk), co); +aio_co_enter (qemu_get_current_aio_context (), co); acb->has_returned = true;if (acb->rwco.ret !=NOT_DONE ) { -replay_bh_schedule_oneshot_event (blk_get_aio_context (blk), +replay_bh_schedule_oneshot_event (qemu_get_current_aio_context (), blk_aio_complete_bh, acb); }
@@ -1931,11 +1931,11 @@ BlockAIOCB *blk_aio_zone_mgmt(BlockBackend *blk, BlockZoneOp op, acb->has_returned = false; co =qemu_coroutine_create (blk_aio_zone_mgmt_entry, acb); -aio_co_enter (blk_get_aio_context (blk), co); +aio_co_enter (qemu_get_current_aio_context (), co); acb->has_returned = true;if (acb->rwco.ret !=NOT_DONE ) { -replay_bh_schedule_oneshot_event (blk_get_aio_context (blk), +replay_bh_schedule_oneshot_event (qemu_get_current_aio_context (), blk_aio_complete_bh, acb); }
@@ -1971,10 +1971,10 @@ BlockAIOCB *blk_aio_zone_append(BlockBackend *blk, int64_t *offset, acb->has_returned = false; co =qemu_coroutine_create (blk_aio_zone_append_entry, acb); -aio_co_enter (blk_get_aio_context (blk), co); +aio_co_enter (qemu_get_current_aio_context (), co); acb->has_returned = true;if (acb->rwco.ret !=NOT_DONE ) { -replay_bh_schedule_oneshot_event (blk_get_aio_context (blk), +replay_bh_schedule_oneshot_event (qemu_get_current_aio_context (), blk_aio_complete_bh, acb); }
Signature · 1 line
2.41.0
Re: [PATCH v3 4/5] block-backend: process zoned requests in the current AioContext
Quoted 11 lines · Stefan Hajnoczi
Process zoned requests in the current thread's AioContext instead of in the BlockBackend's AioContext.
There is no need to use the BlockBackend's AioContext thanks to CoMutex
bs->wps->colock, which protects zone metadata.Signed-off-by Stefan Hajnoczi <[email protected]>--- block/block-backend.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH v3 5/5 block-coroutine-wrapper: use qemu_get_current_aio_context()
In reply to Stefan HajnocziUse qemu_get_current_aio_context() in mixed wrappers and coroutine wrappers so that code runs in the caller's AioContext instead of moving to the BlockDriverState's AioContext. This change is necessary for the multi-queue block layer where any thread can call into the block layer.
Most wrappers are IO_CODE where it's safe to use the current AioContext nowadays. BlockDrivers and the core block layer use their own locks and no longer depend on the AioContext lock for thread-safety.
The bdrv_create() wrapper invokes GLOBAL_STATE code. Using the current AioContext is safe because this code is only called with the BQL held from the main loop thread.
The output of qemu-iotests 051 is sensitive to event loop activity. Update the output because the monitor BH runs at a different time, causing prompts to be printed differently in the output.
--- scripts/block-coroutine-wrapper.py | 6 ++---- tests/qemu-iotests/051.pc.out | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-)
scripts/block-coroutine-wrapper.py +2 -4
@@ -88,8 +88,6 @@ def __init__(self, wrapper_type: str, return_type: str, name: str, raise ValueError(f"no_co function can't be rdlock: {self.name}") self.target_name = f'{subsystem}_{subname}' - self.ctx = self.gen_ctx() - self.get_result = 's->ret = ' self.ret = 'return s.ret;' self.co_ret = 'return '
@@ -162,7 +160,7 @@ def create_mixed_wrapper(func: FuncDecl) -> str: {func.co_ret}{name}({ func.gen_list('{name}') }); }} else {{ {struct_name} s = {{ - .poll_state.ctx = {func.ctx}, + .poll_state.ctx = qemu_get_current_aio_context(), .poll_state.in_progress = true, { func.gen_block(' .{name} = {name},') }
@@ -186,7 +184,7 @@ def create_co_wrapper(func: FuncDecl) -> str: {func.return_type} {func.name}({ func.gen_list('{decl}') }) {{ {struct_name} s = {{ - .poll_state.ctx = {func.ctx}, + .poll_state.ctx = qemu_get_current_aio_context(), .poll_state.in_progress = true, { func.gen_block(' .{name} = {name},') }
tests/qemu-iotests/051.pc.out +2 -2
@@ -177,11 +177,11 @@ QEMU_PROG: -device virtio-blk-pci,drive=disk,share-rw=on: Cannot change iothread Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device lsi53c895a,id=lsi0 -device scsi-hd,bus=lsi0.0,drive=disk,share-rw=on QEMU X.Y.Z monitor - type 'help' for more information -(qemu) QEMU_PROG: -device scsi-hd,bus=lsi0.0,drive=disk,share-rw=on: HBA does not support iothreads +QEMU_PROG: -device scsi-hd,bus=lsi0.0,drive=disk,share-rw=on: HBA does not support iothreads Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device virtio-scsi,id=virtio-scsi1 -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on QEMU X.Y.Z monitor - type 'help' for more information -(qemu) QEMU_PROG: -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on: Cannot change iothread of active block backend +QEMU_PROG: -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on: Cannot change iothread of active block backend Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device virtio-blk-pci,drive=disk,iothread=thread0,share-rw=on QEMU X.Y.Z monitor - type 'help' for more information
Signature · 1 line
2.41.0
Re: [PATCH v3 5/5] block-coroutine-wrapper: use qemu_get_current_aio_context()
Quoted 17 lines · Stefan Hajnoczi
Use
qemu_get_current_aio_context()in mixed wrappers and coroutine wrappers so that code runs in the caller's AioContext instead of moving to the BlockDriverState's AioContext. This change is necessary for the multi-queue block layer where any thread can call into the block layer.Most wrappers are
IO_CODEwhere it's safe to use the current AioContext nowadays. BlockDrivers and the core block layer use their own locks and no longer depend on the AioContext lock for thread-safety.The
bdrv_create()wrapper invokesGLOBAL_STATEcode. Using the current AioContext is safe because this code is only called with the BQL held from the main loop thread.The output of qemu-iotests 051 is sensitive to event loop activity. Update the output because the monitor BH runs at a different time, causing prompts to be printed differently in the output.
Quoted 6 lines
Signed-off-by Stefan Hajnoczi <[email protected]>--- scripts/block-coroutine-wrapper.py | 6 ++---- tests/qemu-iotests/051.pc.out | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Kevin Wolf
AppliedAm 13.09.2023 um 01:10 hat Stefan Hajnoczi geschrieben:
Quoted 15 lines
v3
- Add Patch 2 to fix a race condition in test-bdrv-drain. This was the CI failure that bumped this patch series from Kevin's pull request.
- Add missing 051.pc.out file. I tried qemu-system-aarch64 to see of 051.out also needs to be updated, but no changes were necessary. [Kevin]
v2
- Add patch to removeAIOCBInfo->get_aio_context()[Kevin]
- Add patch to useqemu_get_current_aio_context()in block-coroutine-wrapper so that the wrappers use the current AioContext instead ofbdrv_get_aio_context().Switch blk_aio_*() APIs over to multi-queue by using
qemu_get_current_aio_context()instead ofblk_get_aio_context(). This change will allow devices to process I/O in multiple IOThreads in the future.
Quoted 6 lines
The final patch requires my QIOChannel AioContext series to pass tests/qemu-iotests/check -qcow2 281 because the nbd block driver is now accessed from the main loop thread in addition to the IOThread:
https://lore.kernel.org/qemu-devel/[email protected]/T/#tBased-on: [email protected]
Thanks, applied to the block branch.
Kevin