From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com,
stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com
Subject: [PATCH 3/8] qemu-thread-win32: cleanup, fix, document QemuEvent
Date: Fri, 3 Mar 2023 18:19:34 +0100 [thread overview]
Message-ID: <20230303171939.237819-4-pbonzini@redhat.com> (raw)
In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com>
QemuEvent is currently broken on ARM due to missing memory barriers
after qatomic_*(). Apart from adding the memory barrier, a closer look
reveals some unpaired memory barriers that are not really needed and
complicated the functions unnecessarily, as well as some optimizations
that I couldn't quite prove to be correct.
Finally, the code is relying on a memory barrier in ResetEvent(); the
barrier _ought_ to be there but there is really no documentation about
it; it only affects the slow path, so make it explicit.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
util/qemu-thread-win32.c | 78 +++++++++++++++++++++++++++-------------
1 file changed, 53 insertions(+), 25 deletions(-)
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index 69db254ac7c1..eff664ae6b31 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -272,12 +272,20 @@ void qemu_event_destroy(QemuEvent *ev)
void qemu_event_set(QemuEvent *ev)
{
assert(ev->initialized);
- /* qemu_event_set has release semantics, but because it *loads*
+
+ /*
+ * Pairs with memory barrier in qemu_event_reset.
+ *
+ * qemu_event_set has release semantics, but because it *loads*
* ev->value we need a full memory barrier here.
*/
smp_mb();
if (qatomic_read(&ev->value) != EV_SET) {
- if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
+ int old = qatomic_xchg(&ev->value, EV_SET);
+
+ /* Pairs with memory barrier after ResetEvent. */
+ smp_mb__after_rmw();
+ if (old == EV_BUSY) {
/* There were waiters, wake them up. */
SetEvent(ev->event);
}
@@ -286,17 +294,19 @@ void qemu_event_set(QemuEvent *ev)
void qemu_event_reset(QemuEvent *ev)
{
- unsigned value;
-
assert(ev->initialized);
- value = qatomic_read(&ev->value);
- smp_mb_acquire();
- if (value == EV_SET) {
- /* If there was a concurrent reset (or even reset+wait),
- * do nothing. Otherwise change EV_SET->EV_FREE.
- */
- qatomic_or(&ev->value, EV_FREE);
- }
+
+ /*
+ * If there was a concurrent reset (or even reset+wait),
+ * do nothing. Otherwise change EV_SET->EV_FREE.
+ */
+ qatomic_or(&ev->value, EV_FREE);
+
+ /*
+ * Order reset before checking the condition in the caller.
+ * Pairs with the first memory barrier in qemu_event_set().
+ */
+ smp_mb__after_rmw();
}
void qemu_event_wait(QemuEvent *ev)
@@ -304,29 +314,47 @@ void qemu_event_wait(QemuEvent *ev)
unsigned value;
assert(ev->initialized);
+
+ /*
+ * This read does not have any particular ordering requirements;
+ * if it moves earlier, we might miss qemu_event_set() and go down the
+ * slow path unnecessarily, but ultimately the memory barrier below,
+ * plus the internal synchronization of the kernel event, will ensure
+ * the check is done correctly.
+ */
value = qatomic_read(&ev->value);
- smp_mb_acquire();
if (value != EV_SET) {
if (value == EV_FREE) {
- /* qemu_event_set is not yet going to call SetEvent, but we are
- * going to do another check for EV_SET below when setting EV_BUSY.
- * At that point it is safe to call WaitForSingleObject.
+ /*
+ * Here the underlying kernel event is reset, but qemu_event_set is
+ * not yet going to call SetEvent. However, there will be another
+ * check for EV_SET below when setting EV_BUSY. At that point it
+ * is safe to call WaitForSingleObject.
*/
ResetEvent(ev->event);
- /* Tell qemu_event_set that there are waiters. No need to retry
- * because there cannot be a concurrent busy->free transition.
- * After the CAS, the event will be either set or busy.
+ /*
+ * It is not clear whether ResetEvent provides this barrier; kernel
+ * APIs (KeResetEvent/KeClearEvent) do not. Better safe than sorry!
+ */
+ smp_mb();
+
+ /*
+ * Leave the event reset and tell qemu_event_set that there are
+ * waiters. No need to retry, because there cannot be a concurrent
+ * busy->free transition. After the CAS, the event will be either
+ * set or busy.
*/
if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
- value = EV_SET;
- } else {
- value = EV_BUSY;
+ return;
}
}
- if (value == EV_BUSY) {
- WaitForSingleObject(ev->event, INFINITE);
- }
+
+ /*
+ * ev->value is now EV_BUSY. Since we didn't observe EV_SET,
+ * qemu_event_set() must observe EV_BUSY and call SetEvent().
+ */
+ WaitForSingleObject(ev->event, INFINITE);
}
}
--
2.39.1
next prev parent reply other threads:[~2023-03-03 17:20 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-03 17:19 [PATCH 0/8] Fix missing memory barriers on ARM Paolo Bonzini
2023-03-03 17:19 ` [PATCH 1/8] qatomic: add smp_mb__before/after_rmw() Paolo Bonzini
2023-03-05 18:57 ` Richard Henderson
2023-03-05 21:00 ` Paolo Bonzini
2023-03-06 13:21 ` David Hildenbrand
2023-03-06 13:22 ` David Hildenbrand
2023-03-03 17:19 ` [PATCH 2/8] qemu-thread-posix: cleanup, fix, document QemuEvent Paolo Bonzini
2023-03-05 19:11 ` Richard Henderson
2023-03-06 13:28 ` David Hildenbrand
2023-03-03 17:19 ` Paolo Bonzini [this message]
2023-03-05 19:14 ` [PATCH 3/8] qemu-thread-win32: " Richard Henderson
2023-03-06 13:31 ` David Hildenbrand
2023-03-06 14:20 ` Paolo Bonzini
2023-03-06 14:32 ` David Hildenbrand
2023-03-06 15:17 ` Paolo Bonzini
2023-03-03 17:19 ` [PATCH 4/8] edu: add smp_mb__after_rmw() Paolo Bonzini
2023-03-05 19:14 ` Richard Henderson
2023-03-06 13:31 ` David Hildenbrand
2023-03-06 13:38 ` Peter Maydell
2023-03-06 14:10 ` Paolo Bonzini
2023-03-06 14:24 ` Peter Maydell
2023-03-06 15:06 ` Paolo Bonzini
2023-03-06 15:36 ` Peter Maydell
2023-03-03 17:19 ` [PATCH 5/8] util/async: add smp_mb__after_rmw() around BH enqueue/dequeue Paolo Bonzini
2023-03-05 19:32 ` Richard Henderson
2023-03-06 9:55 ` Paolo Bonzini
2023-03-03 17:19 ` [PATCH 6/8] aio-wait: switch to smp_mb__after_rmw() Paolo Bonzini
2023-03-05 19:32 ` Richard Henderson
2023-03-06 13:32 ` David Hildenbrand
2023-03-06 14:38 ` Stefan Hajnoczi
2023-03-03 17:19 ` [PATCH 7/8] qemu-coroutine-lock: add smp_mb__after_rmw() Paolo Bonzini
2023-03-05 19:36 ` Richard Henderson
2023-03-06 13:33 ` David Hildenbrand
2023-03-03 17:19 ` [PATCH 8/8] physmem: add missing memory barrier Paolo Bonzini
2023-03-05 19:40 ` Richard Henderson
2023-03-06 13:34 ` David Hildenbrand
2023-03-06 13:35 ` [PATCH 0/8] Fix missing memory barriers on ARM David Hildenbrand
2023-03-06 14:14 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230303171939.237819-4-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=cohuck@redhat.com \
--cc=david@redhat.com \
--cc=eauger@redhat.com \
--cc=eesposit@redhat.com \
--cc=gshan@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).