diff mbox series

[5/5] monitor: use aio_co_reschedule_self()

Message ID 20240205172659.476970-6-stefanha@redhat.com (mailing list archive)
State New, archived
Headers show
Series virtio-blk: iothread-vq-mapping cleanups | expand

Commit Message

Stefan Hajnoczi Feb. 5, 2024, 5:26 p.m. UTC
The aio_co_reschedule_self() API is designed to avoid the race
condition between scheduling the coroutine in another AioContext and
yielding.

The QMP dispatch code uses the open-coded version that appears
susceptible to the race condition at first glance:

  aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
  qemu_coroutine_yield();

The code is actually safe because the iohandler and qemu_aio_context
AioContext run under the Big QEMU Lock. Nevertheless, set a good example
and use aio_co_reschedule_self() so it's obvious that there is no race.

Suggested-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 qapi/qmp-dispatch.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Comments

Manos Pitsidianakis Feb. 6, 2024, 7:28 a.m. UTC | #1
On Mon, 05 Feb 2024 19:26, Stefan Hajnoczi <stefanha@redhat.com> wrote:
>The aio_co_reschedule_self() API is designed to avoid the race
>condition between scheduling the coroutine in another AioContext and
>yielding.
>
>The QMP dispatch code uses the open-coded version that appears
>susceptible to the race condition at first glance:
>
>  aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
>  qemu_coroutine_yield();
>
>The code is actually safe because the iohandler and qemu_aio_context
>AioContext run under the Big QEMU Lock. Nevertheless, set a good example
>and use aio_co_reschedule_self() so it's obvious that there is no race.
>
>Suggested-by: Hanna Reitz <hreitz@redhat.com>
>Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
>---
> qapi/qmp-dispatch.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
>
>diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c
>index 176b549473..f3488afeef 100644
>--- a/qapi/qmp-dispatch.c
>+++ b/qapi/qmp-dispatch.c
>@@ -212,8 +212,7 @@ QDict *coroutine_mixed_fn qmp_dispatch(const QmpCommandList *cmds, QObject *requ
>              * executing the command handler so that it can make progress if it
>              * involves an AIO_WAIT_WHILE().
>              */
>-            aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
>-            qemu_coroutine_yield();
>+            aio_co_reschedule_self(qemu_get_aio_context());
>         }
> 
>         monitor_set_cur(qemu_coroutine_self(), cur_mon);
>@@ -227,9 +226,7 @@ QDict *coroutine_mixed_fn qmp_dispatch(const QmpCommandList *cmds, QObject *requ
>              * Move back to iohandler_ctx so that nested event loops for
>              * qemu_aio_context don't start new monitor commands.
>              */
>-            aio_co_schedule(iohandler_get_aio_context(),
>-                            qemu_coroutine_self());
>-            qemu_coroutine_yield();
>+            aio_co_reschedule_self(iohandler_get_aio_context());
>         }
>     } else {
>        /*
>-- 
>2.43.0
>
>

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Hanna Czenczek Feb. 6, 2024, 3:11 p.m. UTC | #2
On 05.02.24 18:26, Stefan Hajnoczi wrote:
> The aio_co_reschedule_self() API is designed to avoid the race
> condition between scheduling the coroutine in another AioContext and
> yielding.
>
> The QMP dispatch code uses the open-coded version that appears
> susceptible to the race condition at first glance:
>
>    aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
>    qemu_coroutine_yield();
>
> The code is actually safe because the iohandler and qemu_aio_context
> AioContext run under the Big QEMU Lock. Nevertheless, set a good example
> and use aio_co_reschedule_self() so it's obvious that there is no race.
>
> Suggested-by: Hanna Reitz <hreitz@redhat.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   qapi/qmp-dispatch.c | 7 ++-----
>   1 file changed, 2 insertions(+), 5 deletions(-)

Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
Markus Armbruster Feb. 7, 2024, 7:04 a.m. UTC | #3
Stefan Hajnoczi <stefanha@redhat.com> writes:

> The aio_co_reschedule_self() API is designed to avoid the race
> condition between scheduling the coroutine in another AioContext and
> yielding.
>
> The QMP dispatch code uses the open-coded version that appears
> susceptible to the race condition at first glance:
>
>   aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
>   qemu_coroutine_yield();
>
> The code is actually safe because the iohandler and qemu_aio_context
> AioContext run under the Big QEMU Lock. Nevertheless, set a good example
> and use aio_co_reschedule_self() so it's obvious that there is no race.
>
> Suggested-by: Hanna Reitz <hreitz@redhat.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

Acked-by: Markus Armbruster <armbru@redhat.com>

Feel free to merge this together with the remainder of the series.
diff mbox series

Patch

diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c
index 176b549473..f3488afeef 100644
--- a/qapi/qmp-dispatch.c
+++ b/qapi/qmp-dispatch.c
@@ -212,8 +212,7 @@  QDict *coroutine_mixed_fn qmp_dispatch(const QmpCommandList *cmds, QObject *requ
              * executing the command handler so that it can make progress if it
              * involves an AIO_WAIT_WHILE().
              */
-            aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
-            qemu_coroutine_yield();
+            aio_co_reschedule_self(qemu_get_aio_context());
         }
 
         monitor_set_cur(qemu_coroutine_self(), cur_mon);
@@ -227,9 +226,7 @@  QDict *coroutine_mixed_fn qmp_dispatch(const QmpCommandList *cmds, QObject *requ
              * Move back to iohandler_ctx so that nested event loops for
              * qemu_aio_context don't start new monitor commands.
              */
-            aio_co_schedule(iohandler_get_aio_context(),
-                            qemu_coroutine_self());
-            qemu_coroutine_yield();
+            aio_co_reschedule_self(iohandler_get_aio_context());
         }
     } else {
        /*