qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Stefan Weil" <sw@weilnetz.de>, "Peter Xu" <peterx@redhat.com>,
	"Fabiano Rosas" <farosas@suse.de>,
	"Hailiang Zhang" <zhanghailiang@xfusion.com>,
	"Phil Dennis-Jordan" <phil@philjordan.eu>,
	qemu-devel <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 v3 04/10] qemu-thread: Avoid futex abstraction for non-Linux
Date: Fri, 16 May 2025 21:58:27 +0900	[thread overview]
Message-ID: <12b1dba8-ecb5-4167-841f-0a32256285d5@daynix.com> (raw)
In-Reply-To: <CABgObfa+sBbA3OURGm=6WGzs1TQKyaHjRj+QS3n9dUvSjEPkZw@mail.gmail.com>

On 2025/05/16 20:09, 'Paolo Bonzini' via devel wrote:
> 
> 
> Il mer 14 mag 2025, 08:57 Akihiko Odaki <akihiko.odaki@daynix.com 
> <mailto:akihiko.odaki@daynix.com>> ha scritto:
> 
>     Honestly, I do not understand why smp_mb() is placed here even in the
>     futex case. The comment says:
>       > qemu_event_set has release semantics, but because it *loads*
>       > ev->value we need a full memory barrier here.
> 
>     The barrier is indeed followed by a load: qatomic_read(&ev->value) !
>     = EV_SET
>     However, the caller of qemu_event_set() should not care whether the
>     event is already set or not
> 
>     so I'm not sure if smp_mb() is needed in
>     the first place.
> 
> 
> The barrier is needed to ensure correct ordering in all cases. You have 
> on one side
> 
> done=true
> Set
>    Read ev->value
>    If not EV_SET, set the event+ wake up waiters
> 
> 
> And on the other
> 
> Write EV_FREE
>    Write
> If not done
>    Wait
> 
> If one that calls qemu_event_set and the other calls qemu_event_reset, 
> you need to avoid that set reads a previous EV_SET for the value *and* 
> the other side reads done equal to false. The only way to avoid this is 
> for both sides to use a memory barrier before the read.
> 
>     qemu_event_set(): release *if the event is not already set*; otherwise
>     it has no effect on synchronization so we don't need a barrier either.
> 
> 
> It needs to be release always. This ensures that, whenever the setter 
> declares the event to be set, the other side sees whatever the setter 
> did before the call.

That is what I misunderstood, and now I can also imagine why it should 
always release. Thinking of the scenario with the "done" flag we have 
discussed, if we have two setters, the resetter should acquire the state 
from both of the two setters.

If the event is already set, we need to ensure all stores specified 
before qatomic_read(&ev->value) != EV_SET should happen before 
qatomic_read(&ev->value), which requires us to put a full barrier.

But I have a new question: do we really need "if 
(qatomic_read(&ev->value) != EV_SET)"?

With git blame, I found it didn't have the full barrier until commit 
374293ca6fb0 ("qemu-thread: use acquire/release to clarify semantics of 
QemuEvent"), which added it.

atomic_mb_read() was used until the commit instead. 
include/qemu/atomic.h at that time had the following comment:
 > /* atomic_mb_read/set semantics map Java volatile variables. They are
 >  * less expensive on some platforms (notably POWER & ARMv7) than fully
 >  * sequentially consistent operations.

We place a full barrier anyway, so we no longer have a reason to have 
qatomic_read() before performing qatomic_xchg().

I also found smp_mb__after_rmw() before qemu_futex_wake_all() is no 
longer necessary. Summing up, I think the code should look like as follows:

#ifdef HAVE_FUTEX
     if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
         /* There were waiters, wake them up.  */
         qemu_futex_wake_all(ev);
     }
#else
     pthread_mutex_lock(&ev->lock);
     qatomic_store_release(&ev->value, EV_SET);
     pthread_cond_broadcast(&ev->cond);
     pthread_mutex_unlock(&ev->lock);
#endif

Regards,
Akihiko Odaki


  reply	other threads:[~2025-05-16 12:59 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-11  6:08 [PATCH v3 00/10] Improve futex usage Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 01/10] futex: Check value after qemu_futex_wait() Akihiko Odaki
2025-05-13 14:39   ` Peter Xu
2025-05-14  7:34     ` Akihiko Odaki
2025-05-14 17:06       ` Peter Xu
2025-05-16  5:34         ` Akihiko Odaki
2025-05-16 14:53           ` Peter Xu
2025-05-17  5:01             ` Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 02/10] futex: Support Windows Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 03/10] futex: Replace __linux__ with CONFIG_LINUX Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 04/10] qemu-thread: Avoid futex abstraction for non-Linux Akihiko Odaki
2025-05-14  0:51   ` Paolo Bonzini
2025-05-14 12:57     ` Akihiko Odaki
2025-05-16 11:09       ` Paolo Bonzini
2025-05-16 12:58         ` Akihiko Odaki [this message]
2025-05-16 14:25           ` Paolo Bonzini
2025-05-17  5:40             ` Akihiko Odaki
2025-05-17 10:24               ` Paolo Bonzini
2025-05-17 16:30                 ` Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 05/10] qemu-thread: Use futex for QemuEvent on Windows Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 06/10] qemu-thread: Use futex if available for QemuLockCnt Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 07/10] migration: Replace QemuSemaphore with QemuEvent Akihiko Odaki
2025-05-13 19:13   ` Peter Xu
2025-05-14 12:36     ` Akihiko Odaki
2025-05-11  6:08 ` [PATCH v3 08/10] migration/colo: " Akihiko Odaki
2025-05-12 15:12   ` Fabiano Rosas
2025-05-11  6:08 ` [PATCH v3 09/10] migration/postcopy: " Akihiko Odaki
2025-05-12 15:12   ` Fabiano Rosas
2025-05-11  6:08 ` [PATCH v3 10/10] 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=12b1dba8-ecb5-4167-841f-0a32256285d5@daynix.com \
    --to=akihiko.odaki@daynix.com \
    --cc=berrange@redhat.com \
    --cc=devel@daynix.com \
    --cc=farosas@suse.de \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@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).