All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] aio_wait_kick: add missing memory barrier
@ 2022-05-24 17:30 Emanuele Giuseppe Esposito
  2022-05-24 18:30 ` Vladimir Sementsov-Ogievskiy
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Emanuele Giuseppe Esposito @ 2022-05-24 17:30 UTC (permalink / raw)
  To: qemu-block
  Cc: Stefan Hajnoczi, Fam Zheng, Kevin Wolf, Hanna Reitz,
	Paolo Bonzini, Vladimir Sementsov-Ogievskiy, qemu-devel,
	Emanuele Giuseppe Esposito

It seems that aio_wait_kick always required a memory barrier
or atomic operation in the caller, but nobody actually
took care of doing it.

Let's put the barrier in the function instead, and pair it
with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
comment for further explanation.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/block/aio-wait.h |  2 ++
 util/aio-wait.c          | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h
index b39eefb38d..54840f8622 100644
--- a/include/block/aio-wait.h
+++ b/include/block/aio-wait.h
@@ -81,6 +81,8 @@ extern AioWait global_aio_wait;
     AioContext *ctx_ = (ctx);                                      \
     /* Increment wait_->num_waiters before evaluating cond. */     \
     qatomic_inc(&wait_->num_waiters);                              \
+    /* Paired with smp_mb in aio_wait_kick(). */                   \
+    smp_mb();                                                      \
     if (ctx_ && in_aio_context_home_thread(ctx_)) {                \
         while ((cond)) {                                           \
             aio_poll(ctx_, true);                                  \
diff --git a/util/aio-wait.c b/util/aio-wait.c
index bdb3d3af22..98c5accd29 100644
--- a/util/aio-wait.c
+++ b/util/aio-wait.c
@@ -35,7 +35,21 @@ static void dummy_bh_cb(void *opaque)
 
 void aio_wait_kick(void)
 {
-    /* The barrier (or an atomic op) is in the caller.  */
+    /*
+     * Paired with smp_mb in AIO_WAIT_WHILE. Here we have:
+     * write(condition);
+     * aio_wait_kick() {
+     *      smp_mb();
+     *      read(num_waiters);
+     * }
+     *
+     * And in AIO_WAIT_WHILE:
+     * write(num_waiters);
+     * smp_mb();
+     * read(condition);
+     */
+    smp_mb();
+
     if (qatomic_read(&global_aio_wait.num_waiters)) {
         aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
     }
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] aio_wait_kick: add missing memory barrier
  2022-05-24 17:30 [PATCH] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
@ 2022-05-24 18:30 ` Vladimir Sementsov-Ogievskiy
  2022-05-25 14:42 ` Stefan Hajnoczi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2022-05-24 18:30 UTC (permalink / raw)
  To: Emanuele Giuseppe Esposito, qemu-block
  Cc: Stefan Hajnoczi, Fam Zheng, Kevin Wolf, Hanna Reitz,
	Paolo Bonzini, qemu-devel

On 5/24/22 20:30, Emanuele Giuseppe Esposito wrote:
> It seems that aio_wait_kick always required a memory barrier
> or atomic operation in the caller, but nobody actually
> took care of doing it.
> 
> Let's put the barrier in the function instead, and pair it
> with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
> comment for further explanation.
> 
> Suggested-by: Paolo Bonzini<pbonzini@redhat.com>
> Signed-off-by: Emanuele Giuseppe Esposito<eesposit@redhat.com>

Thanks!

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

-- 
Best regards,
Vladimir


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] aio_wait_kick: add missing memory barrier
  2022-05-24 17:30 [PATCH] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
  2022-05-24 18:30 ` Vladimir Sementsov-Ogievskiy
@ 2022-05-25 14:42 ` Stefan Hajnoczi
  2022-05-30 13:16 ` Kevin Wolf
  2022-06-04 12:51 ` Roman Kagan
  3 siblings, 0 replies; 6+ messages in thread
From: Stefan Hajnoczi @ 2022-05-25 14:42 UTC (permalink / raw)
  To: Emanuele Giuseppe Esposito
  Cc: qemu-block, Fam Zheng, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Vladimir Sementsov-Ogievskiy, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 713 bytes --]

On Tue, May 24, 2022 at 01:30:54PM -0400, Emanuele Giuseppe Esposito wrote:
> It seems that aio_wait_kick always required a memory barrier
> or atomic operation in the caller, but nobody actually
> took care of doing it.
> 
> Let's put the barrier in the function instead, and pair it
> with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
> comment for further explanation.
> 
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> ---
>  include/block/aio-wait.h |  2 ++
>  util/aio-wait.c          | 16 +++++++++++++++-
>  2 files changed, 17 insertions(+), 1 deletion(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] aio_wait_kick: add missing memory barrier
  2022-05-24 17:30 [PATCH] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
  2022-05-24 18:30 ` Vladimir Sementsov-Ogievskiy
  2022-05-25 14:42 ` Stefan Hajnoczi
@ 2022-05-30 13:16 ` Kevin Wolf
  2022-06-04 12:51 ` Roman Kagan
  3 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2022-05-30 13:16 UTC (permalink / raw)
  To: Emanuele Giuseppe Esposito
  Cc: qemu-block, Stefan Hajnoczi, Fam Zheng, Hanna Reitz,
	Paolo Bonzini, Vladimir Sementsov-Ogievskiy, qemu-devel

Am 24.05.2022 um 19:30 hat Emanuele Giuseppe Esposito geschrieben:
> It seems that aio_wait_kick always required a memory barrier
> or atomic operation in the caller, but nobody actually
> took care of doing it.
> 
> Let's put the barrier in the function instead, and pair it
> with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
> comment for further explanation.
> 
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>

Thanks, applied to the block branch.

Kevin



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] aio_wait_kick: add missing memory barrier
  2022-05-24 17:30 [PATCH] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
                   ` (2 preceding siblings ...)
  2022-05-30 13:16 ` Kevin Wolf
@ 2022-06-04 12:51 ` Roman Kagan
  2022-06-05  6:19   ` Paolo Bonzini
  3 siblings, 1 reply; 6+ messages in thread
From: Roman Kagan @ 2022-06-04 12:51 UTC (permalink / raw)
  To: Emanuele Giuseppe Esposito
  Cc: qemu-block, Stefan Hajnoczi, Fam Zheng, Kevin Wolf, Hanna Reitz,
	Paolo Bonzini, Vladimir Sementsov-Ogievskiy, qemu-devel

On Tue, May 24, 2022 at 01:30:54PM -0400, Emanuele Giuseppe Esposito wrote:
> It seems that aio_wait_kick always required a memory barrier
> or atomic operation in the caller, but nobody actually
> took care of doing it.
> 
> Let's put the barrier in the function instead, and pair it
> with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
> comment for further explanation.
> 
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> ---
>  include/block/aio-wait.h |  2 ++
>  util/aio-wait.c          | 16 +++++++++++++++-
>  2 files changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h
> index b39eefb38d..54840f8622 100644
> --- a/include/block/aio-wait.h
> +++ b/include/block/aio-wait.h
> @@ -81,6 +81,8 @@ extern AioWait global_aio_wait;
>      AioContext *ctx_ = (ctx);                                      \
>      /* Increment wait_->num_waiters before evaluating cond. */     \
>      qatomic_inc(&wait_->num_waiters);                              \
> +    /* Paired with smp_mb in aio_wait_kick(). */                   \
> +    smp_mb();                                                      \

IIRC qatomic_inc() ensures sequential consistency, isn't it enough here?

>      if (ctx_ && in_aio_context_home_thread(ctx_)) {                \
>          while ((cond)) {                                           \
>              aio_poll(ctx_, true);                                  \

Roman.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] aio_wait_kick: add missing memory barrier
  2022-06-04 12:51 ` Roman Kagan
@ 2022-06-05  6:19   ` Paolo Bonzini
  0 siblings, 0 replies; 6+ messages in thread
From: Paolo Bonzini @ 2022-06-05  6:19 UTC (permalink / raw)
  To: Roman Kagan, Emanuele Giuseppe Esposito, qemu-block,
	Stefan Hajnoczi, Fam Zheng, Kevin Wolf, Hanna Reitz,
	Vladimir Sementsov-Ogievskiy, qemu-devel

On 6/4/22 14:51, Roman Kagan wrote:
> On Tue, May 24, 2022 at 01:30:54PM -0400, Emanuele Giuseppe Esposito wrote:
>> It seems that aio_wait_kick always required a memory barrier
>> or atomic operation in the caller, but nobody actually
>> took care of doing it.
>>
>> Let's put the barrier in the function instead, and pair it
>> with another one in AIO_WAIT_WHILE. Read aio_wait_kick()
>> comment for further explanation.
>>
>> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
>> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
>> ---
>>   include/block/aio-wait.h |  2 ++
>>   util/aio-wait.c          | 16 +++++++++++++++-
>>   2 files changed, 17 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h
>> index b39eefb38d..54840f8622 100644
>> --- a/include/block/aio-wait.h
>> +++ b/include/block/aio-wait.h
>> @@ -81,6 +81,8 @@ extern AioWait global_aio_wait;
>>       AioContext *ctx_ = (ctx);                                      \
>>       /* Increment wait_->num_waiters before evaluating cond. */     \
>>       qatomic_inc(&wait_->num_waiters);                              \
>> +    /* Paired with smp_mb in aio_wait_kick(). */                   \
>> +    smp_mb();                                                      \
> 
> IIRC qatomic_inc() ensures sequential consistency, isn't it enough here?

Nope, it only ensures sequential consistency with other SEQ_CST 
operations, i.e. not with qatomic_read or qatomic_set. :(

The smp_mb() is needed on ARM, in particular.


Paolo



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-05  6:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-24 17:30 [PATCH] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
2022-05-24 18:30 ` Vladimir Sementsov-Ogievskiy
2022-05-25 14:42 ` Stefan Hajnoczi
2022-05-30 13:16 ` Kevin Wolf
2022-06-04 12:51 ` Roman Kagan
2022-06-05  6:19   ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.