ed5979
From 7a9907c65e3e2bbb0c119acdbbeb4381e7f1d902 Mon Sep 17 00:00:00 2001
ed5979
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
Date: Thu, 9 Mar 2023 08:24:36 -0500
ed5979
Subject: [PATCH 09/12] qemu-coroutine-lock: add smp_mb__after_rmw()
ed5979
ed5979
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
RH-MergeRequest: 158: qatomic: add smp_mb__before/after_rmw()
ed5979
RH-Bugzilla: 2175660
ed5979
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ed5979
RH-Acked-by: David Hildenbrand <david@redhat.com>
ed5979
RH-Acked-by: Eric Auger <eric.auger@redhat.com>
ed5979
RH-Commit: [6/9] 4b1723b1ad670ec4c85240390b4fc15ff361154f (eesposit/qemu-kvm)
ed5979
ed5979
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2175660
ed5979
ed5979
commit e3a3b6ec8169eab2feb241b4982585001512cd55
ed5979
Author: Paolo Bonzini <pbonzini@redhat.com>
ed5979
Date:   Fri Mar 3 10:52:59 2023 +0100
ed5979
ed5979
    qemu-coroutine-lock: add smp_mb__after_rmw()
ed5979
ed5979
    mutex->from_push and mutex->handoff in qemu-coroutine-lock implement
ed5979
    the familiar pattern:
ed5979
ed5979
       write a                                  write b
ed5979
       smp_mb()                                 smp_mb()
ed5979
       read b                                   read a
ed5979
ed5979
    The memory barrier is required by the C memory model even after a
ed5979
    SEQ_CST read-modify-write operation such as QSLIST_INSERT_HEAD_ATOMIC.
ed5979
    Add it and avoid the unclear qatomic_mb_read() operation.
ed5979
ed5979
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
ed5979
    Reviewed-by: David Hildenbrand <david@redhat.com>
ed5979
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
ed5979
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
---
ed5979
 util/qemu-coroutine-lock.c | 9 ++++++++-
ed5979
 1 file changed, 8 insertions(+), 1 deletion(-)
ed5979
ed5979
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
ed5979
index 45c6b57374..c5897bd963 100644
ed5979
--- a/util/qemu-coroutine-lock.c
ed5979
+++ b/util/qemu-coroutine-lock.c
ed5979
@@ -202,10 +202,16 @@ static void coroutine_fn qemu_co_mutex_lock_slowpath(AioContext *ctx,
ed5979
     trace_qemu_co_mutex_lock_entry(mutex, self);
ed5979
     push_waiter(mutex, &w);
ed5979
 
ed5979
+    /*
ed5979
+     * Add waiter before reading mutex->handoff.  Pairs with qatomic_mb_set
ed5979
+     * in qemu_co_mutex_unlock.
ed5979
+     */
ed5979
+    smp_mb__after_rmw();
ed5979
+
ed5979
     /* This is the "Responsibility Hand-Off" protocol; a lock() picks from
ed5979
      * a concurrent unlock() the responsibility of waking somebody up.
ed5979
      */
ed5979
-    old_handoff = qatomic_mb_read(&mutex->handoff);
ed5979
+    old_handoff = qatomic_read(&mutex->handoff);
ed5979
     if (old_handoff &&
ed5979
         has_waiters(mutex) &&
ed5979
         qatomic_cmpxchg(&mutex->handoff, old_handoff, 0) == old_handoff) {
ed5979
@@ -304,6 +310,7 @@ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
ed5979
         }
ed5979
 
ed5979
         our_handoff = mutex->sequence;
ed5979
+        /* Set handoff before checking for waiters.  */
ed5979
         qatomic_mb_set(&mutex->handoff, our_handoff);
ed5979
         if (!has_waiters(mutex)) {
ed5979
             /* The concurrent lock has not added itself yet, so it
ed5979
-- 
ed5979
2.39.1
ed5979