qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Paolo Bonzini <pbonzini@redhat.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>,
	"Akihiko Odaki" <akihiko.odaki@daynix.com>
Subject: [PATCH v5 11/13] qemu-thread: Remove qatomic_read() in qemu_event_set()
Date: Thu, 29 May 2025 14:46:00 +0900	[thread overview]
Message-ID: <20250529-event-v5-11-53b285203794@daynix.com> (raw)
In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com>

The pair of smp_mb() and qatomic_read() sometimes allows skipping the
following qatomic_xchg() call, but it is unclear if it improves
performance so remove it.

Commit 374293ca6fb0 ("qemu-thread: use acquire/release to clarify
semantics of QemuEvent") replaced atomic_mb_read() in qemu_event_set()
with a pair of smp_mb() and atomic_read(). atomic_mb_read() was actually
cheaper than atomic_xchg(). 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.
 *
 * As long as they are used as paired operations they are safe to
 * use. See docs/atomic.txt for more discussion.
 */

However, smp_mb() enforces full sequential consistency, so we cannot
use the same reasoning to claim that the pair of it and qatomic_read()
is cheaper than qatomic_xchg(). Therefore remove the pair and simplify
the code instead.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 util/event.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/util/event.c b/util/event.c
index e937804a92a8..df6d60836041 100644
--- a/util/event.c
+++ b/util/event.c
@@ -55,18 +55,9 @@ void qemu_event_set(QemuEvent *ev)
     assert(ev->initialized);
 
 #ifdef HAVE_FUTEX
-    /*
-     * Pairs with both qemu_event_reset() and qemu_event_wait().
-     *
-     * 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) {
-            /* There were waiters, wake them up.  */
-            qemu_futex_wake_all(ev);
-        }
+    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);
@@ -88,7 +79,7 @@ void qemu_event_reset(QemuEvent *ev)
 
     /*
      * Order reset before checking the condition in the caller.
-     * Pairs with the first memory barrier in qemu_event_set().
+     * Pairs with the store-release in qemu_event_set().
      */
     smp_mb__after_rmw();
 }
@@ -102,7 +93,7 @@ void qemu_event_wait(QemuEvent *ev)
         /*
          * qemu_event_wait must synchronize with qemu_event_set even if it does
          * not go down the slow path, so this load-acquire is needed that
-         * synchronizes with the first memory barrier in qemu_event_set().
+         * synchronizes with the store-release in qemu_event_set().
          */
         unsigned value = qatomic_load_acquire(&ev->value);
         if (value == EV_SET) {

-- 
2.49.0



  parent reply	other threads:[~2025-05-29  5:47 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-29  5:45 [PATCH v5 00/13] Improve futex usage Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 01/13] futex: Check value after qemu_futex_wait() Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 02/13] futex: Support Windows Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 03/13] qemu-thread: Replace __linux__ with CONFIG_LINUX Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 04/13] qemu-thread: Avoid futex abstraction for non-Linux Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 05/13] qemu-thread: Use futex for QemuEvent on Windows Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 06/13] qemu-thread: Use futex if available for QemuLockCnt Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 07/13] migration: Replace QemuSemaphore with QemuEvent Akihiko Odaki
2025-05-29 14:28   ` Peter Xu
2025-05-29  5:45 ` [PATCH v5 08/13] migration/colo: " Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 09/13] migration/postcopy: " Akihiko Odaki
2025-05-29  5:45 ` [PATCH v5 10/13] hw/display/apple-gfx: " Akihiko Odaki
2025-05-29  5:46 ` Akihiko Odaki [this message]
2025-05-29  5:46 ` [PATCH v5 12/13] qemu-thread: Document QemuEvent Akihiko Odaki
2025-05-29  5:46 ` [PATCH v5 13/13] qemu-thread: Document QemuEvent memory ordering Akihiko Odaki
2025-06-05 13:24 ` [PATCH v5 00/13] Improve futex usage Paolo Bonzini
2025-06-06  9:46   ` Akihiko Odaki
2025-06-06 20:38     ` Paolo Bonzini
2025-06-07  4:35       ` 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=20250529-event-v5-11-53b285203794@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).