Skip to contentSkip to conversation

aio_wait_kick: add missing memory barrier

PATCH Applied Emanuele Giuseppe Esposito 6 participants 6 messages May 24, 2022 – Jun 5, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio_wait_kick: add missing memory barrier

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.

---
 include/block/aio-wait.h |  2 ++
 util/aio-wait.c          | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
include/block/aio-wait.h +2 -0
index b39eefb38d..54840f8622 100644
@@ -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);                                  \
util/aio-wait.c +15 -1
index bdb3d3af22..98c5accd29 100644
@@ -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);
     }
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · 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.

Thanks!

Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v1 1/1
#2 lore raw
In reply to Emanuele Giuseppe Esposito
Quoted 8 lines · 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.

Quoted 6 lines
---
 include/block/aio-wait.h |  2 ++
 util/aio-wait.c          | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
Applied
#3 lore raw
In reply to Emanuele Giuseppe Esposito

Am 24.05.2022 um 19:30 hat Emanuele Giuseppe Esposito geschrieben:

Quoted 10 lines

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.

Thanks, applied to the block branch.

Kevin

#4 lore raw
In reply to Emanuele Giuseppe Esposito
Quoted 20 lines · 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.

---
 include/block/aio-wait.h |  2 ++
 util/aio-wait.c          | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
include/block/aio-wait.h +0 -0
index b39eefb38d..54840f8622 100644

include/block/aio-wait.h extern AioWait global_aio_wait; · line 85

Quoted 5 lines
include/block/aio-wait.h +2 -0
@@ -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?

include/block/aio-wait.h extern AioWait global_aio_wait; · line 88

Quoted 3 lines
include/block/aio-wait.h +0 -0
     if (ctx_ && in_aio_context_home_thread(ctx_)) {                \
         while ((cond)) {                                           \
             aio_poll(ctx_, true);                                  \

Roman.

#5 lore raw
Quoted 16 lines · Roman Kagan
Quoted 15 lines · 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.

---
  include/block/aio-wait.h |  2 ++
  util/aio-wait.c          | 16 +++++++++++++++-
  2 files changed, 17 insertions(+), 1 deletion(-)
Quoted 12 lines

include/block/aio-wait.h extern AioWait global_aio_wait; · line 85

Quoted 10 lines
include/block/aio-wait.h +2 -0
index b39eefb38d..54840f8622 100644
@@ -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