From: Paolo Bonzini <pbonzini@redhat.com>
To: Akihiko Odaki <akihiko.odaki@daynix.com>,
Stefan Weil <sw@weilnetz.de>, Peter Xu <peterx@redhat.com>,
Fabiano Rosas <farosas@suse.de>,
Hailiang Zhang <zhanghailiang@xfusion.com>
Cc: "Phil Dennis-Jordan" <phil@philjordan.eu>,
qemu-devel@nongnu.org, devel@daynix.com,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: Re: [PATCH v2 3/9] qemu-thread: Avoid futex abstraction for non-Linux
Date: Sat, 10 May 2025 17:02:42 +0200 [thread overview]
Message-ID: <54bb98a7-110d-41d2-a683-1428cb7d94a2@redhat.com> (raw)
In-Reply-To: <20250510-event-v2-3-7953177ce1b8@daynix.com>
On 5/10/25 10:51, Akihiko Odaki wrote:
> Add special implementations of qemu_event_set() and qemu_event_wait()
> using pthread primitives. qemu_event_wait() will ensure qemu_event_set()
> finishes, and these functions will avoid complex barrier and atomic
> operations.
Unfortunately not...
> Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
> Tested-by: Phil Dennis-Jordan <phil@philjordan.eu>
> Reviewed-by: Phil Dennis-Jordan <phil@philjordan.eu>
> ---
> util/qemu-thread-posix.c | 45 ++++++++++++++++++---------------------------
> 1 file changed, 18 insertions(+), 27 deletions(-)
>
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index 13459e44c768..805cac444f15 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -319,28 +319,6 @@ void qemu_sem_wait(QemuSemaphore *sem)
>
> #ifdef __linux__
> #include "qemu/futex.h"
> -#else
> -static inline void qemu_futex_wake(QemuEvent *ev, int n)
> -{
> - assert(ev->initialized);
> - pthread_mutex_lock(&ev->lock);
> - if (n == 1) {
> - pthread_cond_signal(&ev->cond);
> - } else {
> - pthread_cond_broadcast(&ev->cond);
> - }
> - pthread_mutex_unlock(&ev->lock);
> -}
> -
> -static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
> -{
> - assert(ev->initialized);
> - pthread_mutex_lock(&ev->lock);
> - if (ev->value == val) {
> - pthread_cond_wait(&ev->cond, &ev->lock);
> - }
> - pthread_mutex_unlock(&ev->lock);
> -}
> #endif
>
> /* Valid transitions:
> @@ -363,7 +341,7 @@ static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
>
> void qemu_event_init(QemuEvent *ev, bool init)
> {
> -#ifndef __linux__
> +#ifndef CONFIG_LINUX
> pthread_mutex_init(&ev->lock, NULL);
> pthread_cond_init(&ev->cond, NULL);
> #endif
> @@ -376,7 +354,7 @@ void qemu_event_destroy(QemuEvent *ev)
> {
> assert(ev->initialized);
> ev->initialized = false;
> -#ifndef __linux__
> +#ifndef CONFIG_LINUX
> pthread_mutex_destroy(&ev->lock);
> pthread_cond_destroy(&ev->cond);
> #endif
> @@ -386,6 +364,7 @@ void qemu_event_set(QemuEvent *ev)
> {
> assert(ev->initialized);
>
> +#ifdef CONFIG_LINUX
> /*
> * Pairs with both qemu_event_reset() and qemu_event_wait().
> *
The user of this code will not have mutexes so some care is needed. You
have:
if (!qatomic_load_acquire(&done)) {
qemu_event_reset(&ev);
if (!qatomic_load_acquire(&done))
qemu_event_wait(&ev);
}
and on the other side
qatomic_store_release(&done, 1);
qemu_event_set(&ev);
--> qatomic_set(&ev.value, EV_SET);
I don't think this is correct without the memory barrier in
qemu_event_set(), though I am not actually sure how you'd
add it.
The problem is, I don't see anything that prevents this:
set done
qemu_event_set()
pthread_mutex_lock()
ev.value = SET
qemu_event_reset()
ev.value |= FREE
smp_mb__after_rmw()
// store buffer not flushed yet,
// so other thread doesn't see "done"
read done
pthread_mutex_lock()
pthread_cond_broadcast()
pthread_mutex_unlock()
while ev.value != SET
// hang
pthread_cond_wait()
The barrier in qemu_event_reset() is not enough, you need one on each side.
> +#else
> + pthread_mutex_lock(&ev->lock);
> + if (qatomic_read(&ev->value) != EV_SET) {
Apart from the above this needs to be a while(), because condition
variables also have spurious wakeups.
Paolo
> + pthread_cond_wait(&ev->cond, &ev->lock);
> + }
> + pthread_mutex_unlock(&ev->lock);
> +#endif
> }
>
> static __thread NotifierList thread_exit;
>
next prev parent reply other threads:[~2025-05-10 15:03 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-10 8:51 [PATCH v2 0/9] Improve futex usage Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 1/9] futex: Check value after qemu_futex_wait() Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 2/9] futex: Support Windows Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 3/9] qemu-thread: Avoid futex abstraction for non-Linux Akihiko Odaki
2025-05-10 15:02 ` Paolo Bonzini [this message]
2025-05-11 4:41 ` Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 4/9] qemu-thread: Use futex for QemuEvent on Windows Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 5/9] qemu-thread: Use futex if available for QemuLockCnt Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 6/9] migration: Replace QemuSemaphore with QemuEvent Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 7/9] migration/colo: " Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 8/9] migration/postcopy: " Akihiko Odaki
2025-05-10 8:51 ` [PATCH v2 9/9] hw/display/apple-gfx: " Akihiko Odaki
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=54bb98a7-110d-41d2-a683-1428cb7d94a2@redhat.com \
--to=pbonzini@redhat.com \
--cc=akihiko.odaki@daynix.com \
--cc=berrange@redhat.com \
--cc=devel@daynix.com \
--cc=farosas@suse.de \
--cc=marcandre.lureau@redhat.com \
--cc=peterx@redhat.com \
--cc=phil@philjordan.eu \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=zhanghailiang@xfusion.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).