|
|
97168e |
From d46ca52c3f42add549bd3790a41d06594821334e Mon Sep 17 00:00:00 2001
|
|
|
97168e |
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
|
|
|
97168e |
Date: Thu, 9 Mar 2023 08:10:57 -0500
|
|
|
97168e |
Subject: [PATCH 03/13] qemu-thread-posix: 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: [3/10] 746070c4d78c7f0a9ac4456d9aee69475acb8964
|
|
|
97168e |
|
|
|
97168e |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168472
|
|
|
97168e |
|
|
|
97168e |
commit 9586a1329f5dce6c1d7f4de53cf0536644d7e593
|
|
|
97168e |
Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
97168e |
Date: Thu Mar 2 11:19:52 2023 +0100
|
|
|
97168e |
|
|
|
97168e |
qemu-thread-posix: 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 too. Document more clearly what
|
|
|
97168e |
is going on.
|
|
|
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-posix.c | 69 ++++++++++++++++++++++++++++------------
|
|
|
97168e |
1 file changed, 49 insertions(+), 20 deletions(-)
|
|
|
97168e |
|
|
|
97168e |
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
|
|
|
97168e |
index e1225b63bd..dd3b6d4670 100644
|
|
|
97168e |
--- a/util/qemu-thread-posix.c
|
|
|
97168e |
+++ b/util/qemu-thread-posix.c
|
|
|
97168e |
@@ -430,13 +430,21 @@ void qemu_event_destroy(QemuEvent *ev)
|
|
|
97168e |
|
|
|
97168e |
void qemu_event_set(QemuEvent *ev)
|
|
|
97168e |
{
|
|
|
97168e |
- /* qemu_event_set has release semantics, but because it *loads*
|
|
|
97168e |
+ assert(ev->initialized);
|
|
|
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 |
- assert(ev->initialized);
|
|
|
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 in kernel futex_wait system call. */
|
|
|
97168e |
+ smp_mb__after_rmw();
|
|
|
97168e |
+ if (old == EV_BUSY) {
|
|
|
97168e |
/* There were waiters, wake them up. */
|
|
|
97168e |
qemu_futex_wake(ev, INT_MAX);
|
|
|
97168e |
}
|
|
|
97168e |
@@ -445,18 +453,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 |
- /*
|
|
|
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 |
@@ -464,20 +473,40 @@ 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 |
/*
|
|
|
97168e |
- * Leave the event reset and tell qemu_event_set that there
|
|
|
97168e |
- * are waiters. No need to retry, because there cannot be
|
|
|
97168e |
- * a concurrent busy->free transition. After the CAS, the
|
|
|
97168e |
- * event will be either set or busy.
|
|
|
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 |
+ * This cmpxchg doesn't have particular ordering requirements if it
|
|
|
97168e |
+ * succeeds (moving the store earlier can only cause qemu_event_set()
|
|
|
97168e |
+ * to issue _more_ wakeups), the failing case needs acquire semantics
|
|
|
97168e |
+ * like the load above.
|
|
|
97168e |
*/
|
|
|
97168e |
if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
|
|
|
97168e |
return;
|
|
|
97168e |
}
|
|
|
97168e |
}
|
|
|
97168e |
+
|
|
|
97168e |
+ /*
|
|
|
97168e |
+ * This is the final check for a concurrent set, so it does need
|
|
|
97168e |
+ * a smp_mb() pairing with the second barrier of qemu_event_set().
|
|
|
97168e |
+ * The barrier is inside the FUTEX_WAIT system call.
|
|
|
97168e |
+ */
|
|
|
97168e |
qemu_futex_wait(ev, EV_BUSY);
|
|
|
97168e |
}
|
|
|
97168e |
}
|
|
|
97168e |
--
|
|
|
97168e |
2.37.3
|
|
|
97168e |
|