Skip to contentSkip to conversation

qemu-coroutine: remove qatomic_mb_read()

PATCH In review Paolo Bonzini 3 participants 4 messages Apr 6, 2023 – Apr 7, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-coroutine: remove qatomic_mb_read()

Replace with an explicit barrier and a comment.

---
 util/qemu-coroutine.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
util/qemu-coroutine.c +7 -3
index 849452369201..17a88f65053e 100644
@@ -127,9 +127,13 @@ void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)
         Coroutine *to = QSIMPLEQ_FIRST(&pending);
         CoroutineAction ret;
 
-        /* Cannot rely on the read barrier for to in aio_co_wake(), as there are
-         * callers outside of aio_co_wake() */
-        const char *scheduled = qatomic_mb_read(&to->scheduled);
+        /*
+         * Read to before to->scheduled; pairs with qatomic_cmpxchg in
+         * qemu_co_sleep(), aio_co_schedule() etc.
+         */
+        smp_read_barrier_depends();
+
+        const char *scheduled = qatomic_read(&to->scheduled);
 
         QSIMPLEQ_REMOVE_HEAD(&pending, co_queue_next);
 
Signature · 1 line
2.39.2
#1 lore raw
Quoted 14 lines · Paolo Bonzini

Replace with an explicit barrier and a comment.

---
 util/qemu-coroutine.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
util/qemu-coroutine.c +0 -0
index 849452369201..17a88f65053e 100644
@@ -127,9 +127,13 @@ void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)
         Coroutine *to = QSIMPLEQ_FIRST(&pending);

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co) · line 134

Quoted 10 lines
util/qemu-coroutine.c +5 -3
         CoroutineAction ret;
 
-        /* Cannot rely on the read barrier for to in aio_co_wake(), as there are
-         * callers outside of aio_co_wake() */
-        const char *scheduled = qatomic_mb_read(&to->scheduled);
+        /*
+         * Read to before to->scheduled; pairs with qatomic_cmpxchg in
+         * qemu_co_sleep(), aio_co_schedule() etc.
+         */
+        smp_read_barrier_depends();

I'm not a fan of nuanced memory ordering primitives. I don't understand or remember all the primitives available in docs/devel/atomics.rst and especially not how they interact with each other.

Does smp_read_barrier_depends() make sense for QEMU? Does QEMU support Alpha host CPUs?

Stefan

#2 lore raw

Il gio 6 apr 2023, 12:55 Stefan Hajnoczi <[email protected]> ha scritto:

Quoted 28 lines · Paolo Bonzini

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx,

Quoted 13 lines

Replace with an explicit barrier and a comment.

---
 util/qemu-coroutine.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
util/qemu-coroutine.c +0 -0
index 849452369201..17a88f65053e 100644
@@ -127,9 +127,13 @@ void qemu_aio_coroutine_enter(AioContext *ctx,

Coroutine *co)

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)

Quoted 4 lines
util/qemu-coroutine.c +0 -1
         Coroutine *to = QSIMPLEQ_FIRST(&pending);
         CoroutineAction ret;
 
-        /* Cannot rely on the read barrier for to in aio_co_wake(), as

there are

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co) · line 134

Quoted 7 lines
util/qemu-coroutine.c +5 -2
-         * callers outside of aio_co_wake() */
-        const char *scheduled = qatomic_mb_read(&to->scheduled);
+        /*
+         * Read to before to->scheduled; pairs with qatomic_cmpxchg in
+         * qemu_co_sleep(), aio_co_schedule() etc.
+         */
+        smp_read_barrier_depends();
Quoted 5 lines

I'm not a fan of nuanced memory ordering primitives. I don't understand or remember all the primitives available in docs/devel/atomics.rst and especially not how they interact with each other.

Understood, that's why I want to remove qatomic_mb_read().

Does smp_read_barrier_depends() make sense for QEMU? Does QEMU support

Quoted 2 lines

Alpha host CPUs?

It makes sense in that it's cheaper than qatomic_load_acquire() or smp_rmb() on ARM and PPC (32-bit ARM is especially bad). Here I can use smp_rmb() if you prefer; I thought that the comment, explicitly referring to "to->scheduled" which depends on "to", would be enough.

I could also use QSIMPLEQ_FIRST_RCU(&pending) to hide the barrier, but it seems to be a bad idea because there's no RCU involvement here.

Paolo

Quoted 3 lines

Stefan

Reviewed-by
#3 lore raw
Quoted 42 lines · Paolo Bonzini

Il gio 6 apr 2023, 12:55 Stefan Hajnoczi <[email protected]> ha scritto:

Quoted 28 lines · Paolo Bonzini

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx,

Quoted 13 lines

Replace with an explicit barrier and a comment.

---
 util/qemu-coroutine.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
util/qemu-coroutine.c +0 -0
index 849452369201..17a88f65053e 100644
@@ -127,9 +127,13 @@ void qemu_aio_coroutine_enter(AioContext *ctx,

Coroutine *co)

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)

Quoted 4 lines
util/qemu-coroutine.c +0 -1
         Coroutine *to = QSIMPLEQ_FIRST(&pending);
         CoroutineAction ret;
 
-        /* Cannot rely on the read barrier for to in aio_co_wake(), as

there are

util/qemu-coroutine.c void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co) · line 134

Quoted 7 lines
util/qemu-coroutine.c +5 -2
-         * callers outside of aio_co_wake() */
-        const char *scheduled = qatomic_mb_read(&to->scheduled);
+        /*
+         * Read to before to->scheduled; pairs with qatomic_cmpxchg in
+         * qemu_co_sleep(), aio_co_schedule() etc.
+         */
+        smp_read_barrier_depends();
Quoted 5 lines

I'm not a fan of nuanced memory ordering primitives. I don't understand or remember all the primitives available in docs/devel/atomics.rst and especially not how they interact with each other.

Understood, that's why I want to remove qatomic_mb_read().

Does smp_read_barrier_depends() make sense for QEMU? Does QEMU support

Quoted 2 lines

Alpha host CPUs?

Quoted 7 lines

It makes sense in that it's cheaper than qatomic_load_acquire() or smp_rmb() on ARM and PPC (32-bit ARM is especially bad). Here I can use smp_rmb() if you prefer; I thought that the comment, explicitly referring to "to->scheduled" which depends on "to", would be enough.

I could also use QSIMPLEQ_FIRST_RCU(&pending) to hide the barrier, but it seems to be a bad idea because there's no RCU involvement here.

If smp_read_barrier_depends() is cheaper on ARM and PPC than qatomic_load_acquire() or smp_rmb(), then this seems like a good use of it:

I didn't know that smp_read_barrier_depends() is relevant on any architecture other than Alpha. It would be nice if atomics.rst mentioned ARM and PPC rather than Alpha.

Thanks,
Stefan