|
|
ed5979 |
From 02347869410fe53d814487501fb586f7dc614375 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 06/12] qemu-thread-win32: cleanup, fix, document QemuEvent
|
|
|
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: [3/9] d228e9d6a4a75dd1f0a23a6dceaf4fea23d69192 (eesposit/qemu-kvm)
|
|
|
ed5979 |
|
|
|
ed5979 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2175660
|
|
|
ed5979 |
|
|
|
ed5979 |
commit 6c5df4b48f0c52a61342ecb307a43f4c2a3565c4
|
|
|
ed5979 |
Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
ed5979 |
Date: Thu Mar 2 11:22:50 2023 +0100
|
|
|
ed5979 |
|
|
|
ed5979 |
qemu-thread-win32: cleanup, fix, document QemuEvent
|
|
|
ed5979 |
|
|
|
ed5979 |
QemuEvent is currently broken on ARM due to missing memory barriers
|
|
|
ed5979 |
after qatomic_*(). Apart from adding the memory barrier, a closer look
|
|
|
ed5979 |
reveals some unpaired memory barriers that are not really needed and
|
|
|
ed5979 |
complicated the functions unnecessarily. Also, it is relying on
|
|
|
ed5979 |
a memory barrier in ResetEvent(); the barrier _ought_ to be there
|
|
|
ed5979 |
but there is really no documentation about it, so make it explicit.
|
|
|
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-thread-win32.c | 82 +++++++++++++++++++++++++++-------------
|
|
|
ed5979 |
1 file changed, 56 insertions(+), 26 deletions(-)
|
|
|
ed5979 |
|
|
|
ed5979 |
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
|
|
|
ed5979 |
index 69db254ac7..a7fe3cc345 100644
|
|
|
ed5979 |
--- a/util/qemu-thread-win32.c
|
|
|
ed5979 |
+++ b/util/qemu-thread-win32.c
|
|
|
ed5979 |
@@ -272,12 +272,20 @@ void qemu_event_destroy(QemuEvent *ev)
|
|
|
ed5979 |
void qemu_event_set(QemuEvent *ev)
|
|
|
ed5979 |
{
|
|
|
ed5979 |
assert(ev->initialized);
|
|
|
ed5979 |
- /* qemu_event_set has release semantics, but because it *loads*
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * Pairs with both qemu_event_reset() and qemu_event_wait().
|
|
|
ed5979 |
+ *
|
|
|
ed5979 |
+ * qemu_event_set has release semantics, but because it *loads*
|
|
|
ed5979 |
* ev->value we need a full memory barrier here.
|
|
|
ed5979 |
*/
|
|
|
ed5979 |
smp_mb();
|
|
|
ed5979 |
if (qatomic_read(&ev->value) != EV_SET) {
|
|
|
ed5979 |
- if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
|
|
|
ed5979 |
+ int old = qatomic_xchg(&ev->value, EV_SET);
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /* Pairs with memory barrier after ResetEvent. */
|
|
|
ed5979 |
+ smp_mb__after_rmw();
|
|
|
ed5979 |
+ if (old == EV_BUSY) {
|
|
|
ed5979 |
/* There were waiters, wake them up. */
|
|
|
ed5979 |
SetEvent(ev->event);
|
|
|
ed5979 |
}
|
|
|
ed5979 |
@@ -286,17 +294,19 @@ void qemu_event_set(QemuEvent *ev)
|
|
|
ed5979 |
|
|
|
ed5979 |
void qemu_event_reset(QemuEvent *ev)
|
|
|
ed5979 |
{
|
|
|
ed5979 |
- unsigned value;
|
|
|
ed5979 |
-
|
|
|
ed5979 |
assert(ev->initialized);
|
|
|
ed5979 |
- value = qatomic_read(&ev->value);
|
|
|
ed5979 |
- smp_mb_acquire();
|
|
|
ed5979 |
- if (value == EV_SET) {
|
|
|
ed5979 |
- /* If there was a concurrent reset (or even reset+wait),
|
|
|
ed5979 |
- * do nothing. Otherwise change EV_SET->EV_FREE.
|
|
|
ed5979 |
- */
|
|
|
ed5979 |
- qatomic_or(&ev->value, EV_FREE);
|
|
|
ed5979 |
- }
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * If there was a concurrent reset (or even reset+wait),
|
|
|
ed5979 |
+ * do nothing. Otherwise change EV_SET->EV_FREE.
|
|
|
ed5979 |
+ */
|
|
|
ed5979 |
+ qatomic_or(&ev->value, EV_FREE);
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * Order reset before checking the condition in the caller.
|
|
|
ed5979 |
+ * Pairs with the first memory barrier in qemu_event_set().
|
|
|
ed5979 |
+ */
|
|
|
ed5979 |
+ smp_mb__after_rmw();
|
|
|
ed5979 |
}
|
|
|
ed5979 |
|
|
|
ed5979 |
void qemu_event_wait(QemuEvent *ev)
|
|
|
ed5979 |
@@ -304,29 +314,49 @@ void qemu_event_wait(QemuEvent *ev)
|
|
|
ed5979 |
unsigned value;
|
|
|
ed5979 |
|
|
|
ed5979 |
assert(ev->initialized);
|
|
|
ed5979 |
- value = qatomic_read(&ev->value);
|
|
|
ed5979 |
- smp_mb_acquire();
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * qemu_event_wait must synchronize with qemu_event_set even if it does
|
|
|
ed5979 |
+ * not go down the slow path, so this load-acquire is needed that
|
|
|
ed5979 |
+ * synchronizes with the first memory barrier in qemu_event_set().
|
|
|
ed5979 |
+ *
|
|
|
ed5979 |
+ * If we do go down the slow path, there is no requirement at all: we
|
|
|
ed5979 |
+ * might miss a qemu_event_set() here but ultimately the memory barrier in
|
|
|
ed5979 |
+ * qemu_futex_wait() will ensure the check is done correctly.
|
|
|
ed5979 |
+ */
|
|
|
ed5979 |
+ value = qatomic_load_acquire(&ev->value);
|
|
|
ed5979 |
if (value != EV_SET) {
|
|
|
ed5979 |
if (value == EV_FREE) {
|
|
|
ed5979 |
- /* qemu_event_set is not yet going to call SetEvent, but we are
|
|
|
ed5979 |
- * going to do another check for EV_SET below when setting EV_BUSY.
|
|
|
ed5979 |
- * At that point it is safe to call WaitForSingleObject.
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * Here the underlying kernel event is reset, but qemu_event_set is
|
|
|
ed5979 |
+ * not yet going to call SetEvent. However, there will be another
|
|
|
ed5979 |
+ * check for EV_SET below when setting EV_BUSY. At that point it
|
|
|
ed5979 |
+ * is safe to call WaitForSingleObject.
|
|
|
ed5979 |
*/
|
|
|
ed5979 |
ResetEvent(ev->event);
|
|
|
ed5979 |
|
|
|
ed5979 |
- /* Tell qemu_event_set that there are waiters. No need to retry
|
|
|
ed5979 |
- * because there cannot be a concurrent busy->free transition.
|
|
|
ed5979 |
- * After the CAS, the event will be either set or busy.
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * It is not clear whether ResetEvent provides this barrier; kernel
|
|
|
ed5979 |
+ * APIs (KeResetEvent/KeClearEvent) do not. Better safe than sorry!
|
|
|
ed5979 |
+ */
|
|
|
ed5979 |
+ smp_mb();
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * Leave the event reset and tell qemu_event_set that there are
|
|
|
ed5979 |
+ * waiters. No need to retry, because there cannot be a concurrent
|
|
|
ed5979 |
+ * busy->free transition. After the CAS, the event will be either
|
|
|
ed5979 |
+ * set or busy.
|
|
|
ed5979 |
*/
|
|
|
ed5979 |
if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
|
|
|
ed5979 |
- value = EV_SET;
|
|
|
ed5979 |
- } else {
|
|
|
ed5979 |
- value = EV_BUSY;
|
|
|
ed5979 |
+ return;
|
|
|
ed5979 |
}
|
|
|
ed5979 |
}
|
|
|
ed5979 |
- if (value == EV_BUSY) {
|
|
|
ed5979 |
- WaitForSingleObject(ev->event, INFINITE);
|
|
|
ed5979 |
- }
|
|
|
ed5979 |
+
|
|
|
ed5979 |
+ /*
|
|
|
ed5979 |
+ * ev->value is now EV_BUSY. Since we didn't observe EV_SET,
|
|
|
ed5979 |
+ * qemu_event_set() must observe EV_BUSY and call SetEvent().
|
|
|
ed5979 |
+ */
|
|
|
ed5979 |
+ WaitForSingleObject(ev->event, INFINITE);
|
|
|
ed5979 |
}
|
|
|
ed5979 |
}
|
|
|
ed5979 |
|
|
|
ed5979 |
--
|
|
|
ed5979 |
2.39.1
|
|
|
ed5979 |
|