97168e
From fa730378c42567e77eaf3e70983108f31f9001b9 Mon Sep 17 00:00:00 2001
97168e
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
97168e
Date: Thu, 9 Mar 2023 08:11:05 -0500
97168e
Subject: [PATCH 04/13] qemu-thread-win32: cleanup, fix, document QemuEvent
97168e
97168e
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
97168e
RH-MergeRequest: 263: qatomic: add smp_mb__before/after_rmw()
97168e
RH-Bugzilla: 2168472
97168e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
97168e
RH-Acked-by: Eric Auger <eric.auger@redhat.com>
97168e
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
97168e
RH-Acked-by: David Hildenbrand <david@redhat.com>
97168e
RH-Commit: [4/10] 43d5bd903b460d4c3c5793a456820e8c5c8521d9
97168e
97168e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168472
97168e
97168e
commit 6c5df4b48f0c52a61342ecb307a43f4c2a3565c4
97168e
Author: Paolo Bonzini <pbonzini@redhat.com>
97168e
Date:   Thu Mar 2 11:22:50 2023 +0100
97168e
97168e
    qemu-thread-win32: cleanup, fix, document QemuEvent
97168e
97168e
    QemuEvent is currently broken on ARM due to missing memory barriers
97168e
    after qatomic_*().  Apart from adding the memory barrier, a closer look
97168e
    reveals some unpaired memory barriers that are not really needed and
97168e
    complicated the functions unnecessarily.  Also, it is relying on
97168e
    a memory barrier in ResetEvent(); the barrier _ought_ to be there
97168e
    but there is really no documentation about it, so make it explicit.
97168e
97168e
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
97168e
    Reviewed-by: David Hildenbrand <david@redhat.com>
97168e
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
97168e
97168e
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
97168e
---
97168e
 util/qemu-thread-win32.c | 82 +++++++++++++++++++++++++++-------------
97168e
 1 file changed, 56 insertions(+), 26 deletions(-)
97168e
97168e
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
97168e
index 52eb19f351..c10249bc2e 100644
97168e
--- a/util/qemu-thread-win32.c
97168e
+++ b/util/qemu-thread-win32.c
97168e
@@ -246,12 +246,20 @@ void qemu_event_destroy(QemuEvent *ev)
97168e
 void qemu_event_set(QemuEvent *ev)
97168e
 {
97168e
     assert(ev->initialized);
97168e
-    /* qemu_event_set has release semantics, but because it *loads*
97168e
+
97168e
+    /*
97168e
+     * Pairs with both qemu_event_reset() and qemu_event_wait().
97168e
+     *
97168e
+     * qemu_event_set has release semantics, but because it *loads*
97168e
      * ev->value we need a full memory barrier here.
97168e
      */
97168e
     smp_mb();
97168e
     if (qatomic_read(&ev->value) != EV_SET) {
97168e
-        if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
97168e
+        int old = qatomic_xchg(&ev->value, EV_SET);
97168e
+
97168e
+        /* Pairs with memory barrier after ResetEvent.  */
97168e
+        smp_mb__after_rmw();
97168e
+        if (old == EV_BUSY) {
97168e
             /* There were waiters, wake them up.  */
97168e
             SetEvent(ev->event);
97168e
         }
97168e
@@ -260,17 +268,19 @@ void qemu_event_set(QemuEvent *ev)
97168e
 
97168e
 void qemu_event_reset(QemuEvent *ev)
97168e
 {
97168e
-    unsigned value;
97168e
-
97168e
     assert(ev->initialized);
97168e
-    value = qatomic_read(&ev->value);
97168e
-    smp_mb_acquire();
97168e
-    if (value == EV_SET) {
97168e
-        /* If there was a concurrent reset (or even reset+wait),
97168e
-         * do nothing.  Otherwise change EV_SET->EV_FREE.
97168e
-         */
97168e
-        qatomic_or(&ev->value, EV_FREE);
97168e
-    }
97168e
+
97168e
+    /*
97168e
+     * If there was a concurrent reset (or even reset+wait),
97168e
+     * do nothing.  Otherwise change EV_SET->EV_FREE.
97168e
+     */
97168e
+    qatomic_or(&ev->value, EV_FREE);
97168e
+
97168e
+    /*
97168e
+     * Order reset before checking the condition in the caller.
97168e
+     * Pairs with the first memory barrier in qemu_event_set().
97168e
+     */
97168e
+    smp_mb__after_rmw();
97168e
 }
97168e
 
97168e
 void qemu_event_wait(QemuEvent *ev)
97168e
@@ -278,29 +288,49 @@ void qemu_event_wait(QemuEvent *ev)
97168e
     unsigned value;
97168e
 
97168e
     assert(ev->initialized);
97168e
-    value = qatomic_read(&ev->value);
97168e
-    smp_mb_acquire();
97168e
+
97168e
+    /*
97168e
+     * qemu_event_wait must synchronize with qemu_event_set even if it does
97168e
+     * not go down the slow path, so this load-acquire is needed that
97168e
+     * synchronizes with the first memory barrier in qemu_event_set().
97168e
+     *
97168e
+     * If we do go down the slow path, there is no requirement at all: we
97168e
+     * might miss a qemu_event_set() here but ultimately the memory barrier in
97168e
+     * qemu_futex_wait() will ensure the check is done correctly.
97168e
+     */
97168e
+    value = qatomic_load_acquire(&ev->value);
97168e
     if (value != EV_SET) {
97168e
         if (value == EV_FREE) {
97168e
-            /* qemu_event_set is not yet going to call SetEvent, but we are
97168e
-             * going to do another check for EV_SET below when setting EV_BUSY.
97168e
-             * At that point it is safe to call WaitForSingleObject.
97168e
+            /*
97168e
+             * Here the underlying kernel event is reset, but qemu_event_set is
97168e
+             * not yet going to call SetEvent.  However, there will be another
97168e
+             * check for EV_SET below when setting EV_BUSY.  At that point it
97168e
+             * is safe to call WaitForSingleObject.
97168e
              */
97168e
             ResetEvent(ev->event);
97168e
 
97168e
-            /* Tell qemu_event_set that there are waiters.  No need to retry
97168e
-             * because there cannot be a concurrent busy->free transition.
97168e
-             * After the CAS, the event will be either set or busy.
97168e
+            /*
97168e
+             * It is not clear whether ResetEvent provides this barrier; kernel
97168e
+             * APIs (KeResetEvent/KeClearEvent) do not.  Better safe than sorry!
97168e
+             */
97168e
+            smp_mb();
97168e
+
97168e
+            /*
97168e
+             * Leave the event reset and tell qemu_event_set that there are
97168e
+             * waiters.  No need to retry, because there cannot be a concurrent
97168e
+             * busy->free transition.  After the CAS, the event will be either
97168e
+             * set or busy.
97168e
              */
97168e
             if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
97168e
-                value = EV_SET;
97168e
-            } else {
97168e
-                value = EV_BUSY;
97168e
+                return;
97168e
             }
97168e
         }
97168e
-        if (value == EV_BUSY) {
97168e
-            WaitForSingleObject(ev->event, INFINITE);
97168e
-        }
97168e
+
97168e
+        /*
97168e
+         * ev->value is now EV_BUSY.  Since we didn't observe EV_SET,
97168e
+         * qemu_event_set() must observe EV_BUSY and call SetEvent().
97168e
+         */
97168e
+        WaitForSingleObject(ev->event, INFINITE);
97168e
     }
97168e
 }
97168e
 
97168e
-- 
97168e
2.37.3
97168e