block: switch to AIO_WAIT_WHILE_UNLOCKED() where possible
PATCH v2 2/6 block: convert blk_exp_close_all_type() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziThere is no change in behavior. Switch to AIO_WAIT_WHILE_UNLOCKED() instead of AIO_WAIT_WHILE() to document that this code has already been audited and converted. The AioContext argument is already NULL so aio_context_release() is never called anyway.
--- block/export/export.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/export/export.c +1 -1
@@ -306,7 +306,7 @@ void blk_exp_close_all_type(BlockExportType type)blk_exp_request_shutdown (exp); } -AIO_WAIT_WHILE (NULL ,blk_exp_has_type (type)); +AIO_WAIT_WHILE_UNLOCKED (NULL ,blk_exp_has_type (type)); }void blk_exp_close_all (void )
Signature · 1 line
2.39.2
PATCH v2 6/6 monitor: convert monitor_cleanup() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan Hajnoczimonitor_cleanup() is called from the main loop thread. Calling AIO_WAIT_WHILE(qemu_get_aio_context(), ...) from the main loop thread is equivalent to AIO_WAIT_WHILE_UNLOCKED(NULL, ...) because neither unlocks the AioContext and the latter's assertion that we're in the main loop succeeds.
--- monitor/monitor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
monitor/monitor.c +2 -2
@@ -666,7 +666,7 @@ void monitor_cleanup(void)* We need to poll both qemu_aio_context and iohandler_ctx to make * sure that the dispatcher coroutine keeps making progress and * eventually terminates. qemu_aio_context is automatically -* polled by calling AIO_WAIT_WHILE on it, but we must poll +* polled by calling AIO_WAIT_WHILE_UNLOCKED on it, but we must poll * iohandler_ctx manually. * * Letting the iothread continue while shutting down the dispatcher
@@ -679,7 +679,7 @@ void monitor_cleanup(void)aio_co_wake (qmp_dispatcher_co); } -AIO_WAIT_WHILE (qemu_get_aio_context (), +AIO_WAIT_WHILE_UNLOCKED (NULL , (aio_poll (iohandler_get_aio_context (), false),qatomic_mb_read (&qmp_dispatcher_co_busy)));
Signature · 1 line
2.39.2
v2 cover block: switch to AIO_WAIT_WHILE_UNLOCKED() where possible
v2:
- Clarify NULL ctx argument in Patch 1 commit description [Kevin]
AIO_WAIT_WHILE_UNLOCKED() is the future replacement for AIO_WAIT_WHILE(). Most callers haven't been converted yet because they rely on the AioContext lock. I looked through the code and found the easy cases that can be converted today.
Stefan Hajnoczi (6):
block: don't acquire AioContext lock in bdrv_drain_all()
block: convert blk_exp_close_all_type() to AIO_WAIT_WHILE_UNLOCKED()
block: convert bdrv_graph_wrlock() to AIO_WAIT_WHILE_UNLOCKED()
block: convert bdrv_drain_all_begin() to AIO_WAIT_WHILE_UNLOCKED()
hmp: convert handle_hmp_command() to AIO_WAIT_WHILE_UNLOCKED()
monitor: convert monitor_cleanup() to AIO_WAIT_WHILE_UNLOCKED()
block/block-backend.c | 8 +------- block/export/export.c | 2 +- block/graph-lock.c | 2 +- block/io.c | 2 +- monitor/hmp.c | 2 +- monitor/monitor.c | 4 ++-- 6 files changed, 7 insertions(+), 13 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/6 block: don't acquire AioContext lock in bdrv_drain_all()
There is no need for the AioContext lock in bdrv_drain_all() because nothing in AIO_WAIT_WHILE() needs the lock and the condition is atomic.
AIO_WAIT_WHILE_UNLOCKED() has no use for the AioContext parameter other than performing a check that is nowadays already done by the GLOBAL_STATE_CODE()/IO_CODE() macros. Set the ctx argument to NULL here to help us keep track of all converted callers. Eventually all callers will have been converted and then the argument can be dropped entirely.
--- block/block-backend.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
block/block-backend.c +1 -7
@@ -1835,14 +1835,8 @@ void blk_drain_all(void)bdrv_drain_all_begin ();while ((blk =blk_all_next (blk)) !=NULL ) { -AioContext *ctx =blk_get_aio_context (blk); - -aio_context_acquire (ctx); -/* We may have -ENOMEDIUM completions in flight */ -AIO_WAIT_WHILE (ctx,qatomic_mb_read (&blk->in_flight) >0 ); - -aio_context_release (ctx); +AIO_WAIT_WHILE_UNLOCKED (NULL ,qatomic_mb_read (&blk->in_flight) >0 ); }bdrv_drain_all_end ();
Signature · 1 line
2.39.2
PATCH v2 3/6 block: convert bdrv_graph_wrlock() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziThe following conversion is safe and does not change behavior:
GLOBAL_STATE_CODE();
... - AIO_WAIT_WHILE(qemu_get_aio_context(), ...);
+ AIO_WAIT_WHILE_UNLOCKED(NULL, ...);
Since we're in GLOBAL_STATE_CODE(), qemu_get_aio_context() is our home thread's AioContext. Thus AIO_WAIT_WHILE() does not unlock the AioContext:
if (ctx_ && in_aio_context_home_thread(ctx_)) { \
while ((cond)) { \
aio_poll(ctx_, true); \
waited_ = true; \
} \And that means AIO_WAIT_WHILE_UNLOCKED(NULL, ...) can be substituted.
--- block/graph-lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/graph-lock.c +1 -1
@@ -127,7 +127,7 @@ void bdrv_graph_wrlock(void)* reader lock. */ qatomic_set (&has_writer,0 ); -AIO_WAIT_WHILE (qemu_get_aio_context (),reader_count () >=1 ); +AIO_WAIT_WHILE_UNLOCKED (NULL ,reader_count () >=1 );qatomic_set (&has_writer,1 );/*
Signature · 1 line
2.39.2
PATCH v2 4/6 block: convert bdrv_drain_all_begin() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziSince the AioContext argument was already NULL, AIO_WAIT_WHILE() was never going to unlock the AioContext. Therefore it is possible to replace AIO_WAIT_WHILE() with AIO_WAIT_WHILE_UNLOCKED().
--- block/io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/io.c +1 -1
@@ -520,7 +520,7 @@ void bdrv_drain_all_begin(void)bdrv_drain_all_begin_nopoll ();/* Now poll the in-flight requests */ -AIO_WAIT_WHILE (NULL ,bdrv_drain_all_poll ()); +AIO_WAIT_WHILE_UNLOCKED (NULL ,bdrv_drain_all_poll ());while ((bs =bdrv_next_all_states (bs))) {bdrv_drain_assert_idle (bs);
Signature · 1 line
2.39.2
PATCH v2 5/6 hmp: convert handle_hmp_command() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziThe HMP monitor runs in the main loop thread. Calling AIO_WAIT_WHILE(qemu_get_aio_context(), ...) from the main loop thread is equivalent to AIO_WAIT_WHILE_UNLOCKED(NULL, ...) because neither unlocks the AioContext and the latter's assertion that we're in the main loop succeeds.
--- monitor/hmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/hmp.c +1 -1
@@ -1167,7 +1167,7 @@ void handle_hmp_command(MonitorHMP *mon, const char *cmdline)Coroutine *co =qemu_coroutine_create (handle_hmp_command_co, &data);monitor_set_cur (co, &mon->common);aio_co_enter (qemu_get_aio_context (), co); -AIO_WAIT_WHILE (qemu_get_aio_context (), !data.done); +AIO_WAIT_WHILE_UNLOCKED (NULL , !data.done); }qobject_unref (qdict);
Signature · 1 line
2.39.2
Re: [PATCH v2 1/6] block: don't acquire AioContext lock in bdrv_drain_all()
In reply to Stefan HajnocziQuoted 14 lines · Stefan Hajnoczi
There is no need for the AioContext lock in
bdrv_drain_all()because nothing inAIO_WAIT_WHILE()needs the lock and the condition is atomic.
AIO_WAIT_WHILE_UNLOCKED()has no use for the AioContext parameter other
than performing a check that is nowadays already done by the GLOBAL_STATE_CODE()/IO_CODE() macros. Set the ctx argument to NULL here
to help us keep track of all converted callers. Eventually all callers
will have been converted and then the argument can be dropped entirely.
Quoted 5 lines
Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
block/block-backend.c | 8 +-------1 file changed, 1 insertion(+), 7 deletions(-)
Quoted 22 lines
block/block-backend.c +0
-0@@ -1835,14 +1835,8 @@ void blk_drain_all(void) bdrv_drain_all_begin();
while ((blk =blk_all_next(blk)) != NULL) { - AioContext *ctx =blk_get_aio_context(blk);block/block-backend.c +0
-3- - aio_context_acquire(ctx); -/* We may have
-ENOMEDIUMcompletions in flight */ -AIO_WAIT_WHILE(ctx, qatomic_mb_read(&blk->in_flight) > 0); -
-aio_context_release(ctx);
+ AIO_WAIT_WHILE_UNLOCKED(NULL, qatomic_mb_read(&blk-Quoted 1 line
in_flight) > 0);}
bdrv_drain_all_end();
Re: [PATCH v2 2/6] block: convert blk_exp_close_all_type() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziQuoted 6 lines · Stefan Hajnoczi
There is no change in behavior. Switch to
AIO_WAIT_WHILE_UNLOCKED()instead ofAIO_WAIT_WHILE()to document that this code has already been
audited and converted. The AioContext argument is already NULL soaio_context_release()is never called anyway.
Quoted 7 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
block/export/export.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
block/export/export.c +0
-0@@ -306,7 +306,7 @@ void blk_exp_close_all_type(BlockExportType type) blk_exp_request_shutdown(exp); }
-AIO_WAIT_WHILE(NULL, blk_exp_has_type(type)); +AIO_WAIT_WHILE_UNLOCKED(NULL, blk_exp_has_type(type)); }
voidblk_exp_close_all(void)
Re: [PATCH v2 3/6] block: convert bdrv_graph_wrlock() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziQuoted 20 lines · Stefan Hajnoczi
The following conversion is safe and does not change behavior:
GLOBAL_STATE_CODE();
...
-AIO_WAIT_WHILE(qemu_get_aio_context(), ...); +AIO_WAIT_WHILE_UNLOCKED(NULL, ...);Since we're in
GLOBAL_STATE_CODE(),qemu_get_aio_context()is our home
thread's AioContext. ThusAIO_WAIT_WHILE()does not unlock the AioContext:if (
ctx_&&in_aio_context_home_thread(ctx_)) { \
while ((cond)) { \
aio_poll(ctx_, true); \
waited_= true; \
} \And that means
AIO_WAIT_WHILE_UNLOCKED(NULL, ...)can be substituted.
Quoted 7 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
block/graph-lock.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
block/graph-lock.c +1
-1@@ -127,7 +127,7 @@ void bdrv_graph_wrlock(void)* reader lock. */ qatomic_set(&has_writer, 0); - AIO_WAIT_WHILE(qemu_get_aio_context(), reader_count() >= 1); + AIO_WAIT_WHILE_UNLOCKED(NULL, reader_count() >= 1); qatomic_set(&has_writer, 1);
/*
Re: [PATCH v2 4/6] block: convert bdrv_drain_all_begin() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziQuoted 11 lines · Stefan Hajnoczi
Since the AioContext argument was already NULL,
AIO_WAIT_WHILE()was never going to unlock the AioContext. Therefore it is possible to replaceAIO_WAIT_WHILE()withAIO_WAIT_WHILE_UNLOCKED().Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
block/io.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
block/io.c +0
-0@@ -520,7 +520,7 @@ void bdrv_drain_all_begin(void) bdrv_drain_all_begin_nopoll();
/* Now poll the in-flight requests */
-AIO_WAIT_WHILE(NULL, bdrv_drain_all_poll()); +AIO_WAIT_WHILE_UNLOCKED(NULL, bdrv_drain_all_poll());
while ((bs =bdrv_next_all_states(bs))) {bdrv_drain_assert_idle(bs);
Re: [PATCH v2 5/6] hmp: convert handle_hmp_command() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziQuoted 8 lines · Stefan Hajnoczi
The HMP monitor runs in the main loop thread. Calling
AIO_WAIT_WHILE(qemu_get_aio_context(), ...)from the main loop thread is
equivalent toAIO_WAIT_WHILE_UNLOCKED(NULL, ...)because neither unlocks
the AioContext and the latter's assertion that we're in the main loop succeeds.
Quoted 8 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Markus Armbruster <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
monitor/hmp.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
monitor/hmp.c +1
-1@@ -1167,7 +1167,7 @@ void handle_hmp_command(MonitorHMP *mon, const char *cmdline) Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data); monitor_set_cur(co, &mon->common); aio_co_enter(qemu_get_aio_context(), co); - AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done); + AIO_WAIT_WHILE_UNLOCKED(NULL, !data.done); }
qobject_unref(qdict);
Re: [PATCH v2 6/6] monitor: convert monitor_cleanup() to AIO_WAIT_WHILE_UNLOCKED()
In reply to Stefan HajnocziQuoted 8 lines · Stefan Hajnoczi
monitor_cleanup()is called from the main loop thread. CallingAIO_WAIT_WHILE(qemu_get_aio_context(), ...)from the main loop thread is
equivalent toAIO_WAIT_WHILE_UNLOCKED(NULL, ...)because neither unlocks
the AioContext and the latter's assertion that we're in the main loop succeeds.
Quoted 8 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Markus Armbruster <[email protected]>Reviewed-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
monitor/monitor.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 26 lines
monitor/monitor.c +0
-0@@ -666,7 +666,7 @@ void monitor_cleanup(void) * We need to poll both qemu_aio_context and iohandler_ctx tomake
* sure that the dispatcher coroutine keeps making progress and
* eventually terminates.qemu_aio_contextis automatically - * polled by callingAIO_WAIT_WHILEon it, but we must poll + * polled by callingAIO_WAIT_WHILE_UNLOCKEDon it, but we must poll
*iohandler_ctxmanually.
*
* Letting the iothread continue while shutting down the dispatchermonitor/monitor.c +0
-0@@ -679,7 +679,7 @@ void monitor_cleanup(void) aio_co_wake(qmp_dispatcher_co); }
- AIO_WAIT_WHILE(qemu_get_aio_context(),
+AIO_WAIT_WHILE_UNLOCKED(NULL, (aio_poll(iohandler_get_aio_context(), false), qatomic_mb_read(&qmp_dispatcher_co_busy)));
Kevin Wolf
AppliedAm 09.03.2023 um 20:08 hat Stefan Hajnoczi geschrieben:
Quoted 6 lines
v2:
- Clarify NULL ctx argument in Patch 1 commit description [Kevin]
AIO_WAIT_WHILE_UNLOCKED()is the future replacement forAIO_WAIT_WHILE(). Most callers haven't been converted yet because they rely on the AioContext lock. I looked through the code and found the easy cases that can be converted today.
Thanks, applied to block-next.
Kevin