Improve futex usage
v4 cover Improve futex usage
In a recent discussion, Phil Dennis-Jordan pointed out a quirk in QemuEvent destruction due to futex-like abstraction, which prevented the usage of QemuEvent in new and existing code[1]. With some more thoughts after this discussion, I also found other problem and room of improvement in futex usage. Here is a stack of patches to resolve them.
Patch "futex: Check value after qemu_futex_wait()" ensures qemu_futex_wait() is used in loops as suggested in the man page.
Patch "futex: Support Windows" implements futex functions for Windows.
Patch "qemu-thread: Avoid futex abstraction for non-Linux" and "qemu-thread: Use futex for QemuEvent on Windows" enable destroying QemuEvent immediately after qemu_event_wait().
Patch "qemu-thread: Use futex for QemuEvent on Windows" and "qemu-thread: Use futex if available for QemuLockCnt" make the use of futex functions added for Windows.
Patches "migration: Replace QemuSemaphore with QemuEvent", "migration/colo: Replace QemuSemaphore with QemuEvent", "migration/postcopy: Replace QemuSemaphore with QemuEvent", and "hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent" replace some QemuSemaphores with QemuEvents, which can utilize futex. Some of them rely on that QemuEvent can be destroyed immediately after qemu_event_wait().
[1]: https://lore.kernel.org/r/CAAibmn3HZeDeK8FrYhHa1GGwc+N8rBuB2VvMRm7LCt0mUGmsYQ@mail.gmail.com
---
Changes in v4:
- Added patch "qemu-thread: Remove qatomic_read() in qemu_event_set()".
- Renamed patch "futex: Replace __linux__ with CONFIG_LINUX" to
"qemu-thread: Replace __linux__ with CONFIG_LINUX".
- Reverted changes to convert rp_pong_acks to QemuEvent.
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v3:
- Fixed race between qemu_event_reset() and qemu_event_set().
- Prepared for spurious pthread_cond_wait() wakeups.
- Added patch "futex: Replace __linux__ with CONFIG_LINUX".
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- Rebased.
- Added patch
"hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent".
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Akihiko Odaki (11):
futex: Check value after qemu_futex_wait()
futex: Support Windows
qemu-thread: Remove qatomic_read() in qemu_event_set()
qemu-thread: Replace __linux__ with CONFIG_LINUX
qemu-thread: Avoid futex abstraction for non-Linux
qemu-thread: Use futex for QemuEvent on Windows
qemu-thread: Use futex if available for QemuLockCnt
migration: Replace QemuSemaphore with QemuEvent migration/colo: Replace QemuSemaphore with QemuEvent migration/postcopy: Replace QemuSemaphore with QemuEvent hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
meson.build | 2 + include/qemu/futex.h | 44 +++++++++++- include/qemu/lockcnt.h | 2 +- include/qemu/thread-posix.h | 9 --- include/qemu/thread-win32.h | 6 -- include/qemu/thread.h | 11 ++- migration/migration.h | 12 ++-- migration/colo.c | 20 +++--- migration/migration.c | 21 +++--- migration/postcopy-ram.c | 10 +-- migration/savevm.c | 2 +- tests/unit/test-aio-multithread.c | 6 +- util/event.c | 122 +++++++++++++++++++++++++++++++ util/lockcnt.c | 9 +-- util/qemu-thread-posix.c | 148 -------------------------------------- util/qemu-thread-win32.c | 129 --------------------------------- hw/display/apple-gfx.m | 10 +-- util/meson.build | 3 +- 18 files changed, 223 insertions(+), 343 deletions(-)
---
base-commit: f0737158b483e7ec2b2512145aeab888b85cc1f7
change-id: 20241031-event-785a2f0dda4a
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
PATCH v4 1/11 futex: Check value after qemu_futex_wait()
futex(2) - Linux manual page
https://man7.org/linux/man-pages/man2/futex.2.html
Quoted 8 lines
Note that a wake-up can also be caused by common futex usage patterns in unrelated code that happened to have previously used the futex word's memory location (e.g., typical futex-based implementations of Pthreads mutexes can cause this under some conditions). Therefore, callers should always conservatively assume that a return value of 0 can mean a spurious wake-up, and use the futex word's value (i.e., the user-space synchronization scheme) to decide whether to continue to block or not.
--- include/qemu/futex.h | 9 +++++++++ tests/unit/test-aio-multithread.c | 4 +++- util/qemu-thread-posix.c | 35 +++++++++++++---------------------- 3 files changed, 25 insertions(+), 23 deletions(-)
include/qemu/futex.h +9 -0
@@ -24,6 +24,15 @@ static inline void qemu_futex_wake(void *f, int n)qemu_futex (f,FUTEX_WAKE , n,NULL ,NULL ,0 ); } +/* +* Note that a wake-up can also be caused by common futex usage patterns in +* unrelated code that happened to have previously used the futex word's +* memory location (e.g., typical futex-based implementations of Pthreads +* mutexes can cause this under some conditions). Therefore, callers should +* always conservatively assume that it is a spurious wake-up, and use the futex +* word's value (i.e., the user-space synchronization scheme) to decide whether +* to continue to block or not. +*/ static inline void qemu_futex_wait (void *f,unsigned val) {while (qemu_futex (f,FUTEX_WAIT , (int ) val,NULL ,NULL ,0 )) {
tests/unit/test-aio-multithread.c +3 -1
@@ -305,7 +305,9 @@ static void mcs_mutex_lock(void) prev =qatomic_xchg (&mutex_head, id);if (prev !=-1 ) {qatomic_set (&nodes[prev].next, id); -qemu_futex_wait (&nodes[id].locked,1 ); +while (qatomic_read (&nodes[id].locked) ==1 ) { +qemu_futex_wait (&nodes[id].locked,1 ); + } } }
util/qemu-thread-posix.c +13 -22
@@ -394,11 +394,7 @@ void qemu_event_set(QemuEvent *ev)*/ smp_mb ();if (qatomic_read (&ev->value) !=EV_SET ) { -int old =qatomic_xchg (&ev->value,EV_SET ); - -/* Pairs with memory barrier in kernel futex_wait system call. */ -smp_mb__after_rmw (); -if (old ==EV_BUSY ) { +if (qatomic_xchg (&ev->value,EV_SET ) ==EV_BUSY ) {/* There were waiters, wake them up. */ qemu_futex_wake (ev,INT_MAX ); }
@@ -428,17 +424,17 @@ void qemu_event_wait(QemuEvent *ev)assert (ev->initialized); -/* -* 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(). -* -* If we do go down the slow path, there is no requirement at all: we -* might miss a qemu_event_set() here but ultimately the memory barrier in -* qemu_futex_wait() will ensure the check is done correctly. -*/ - value =qatomic_load_acquire (&ev->value); -if (value !=EV_SET ) { +while (true) { +/* +* 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(). +*/ + value =qatomic_load_acquire (&ev->value); +if (value ==EV_SET ) { +break ; + } +if (value ==EV_FREE ) {/* * Leave the event reset and tell qemu_event_set that there are
@@ -452,15 +448,10 @@ void qemu_event_wait(QemuEvent *ev)* like the load above. */ if (qatomic_cmpxchg (&ev->value,EV_FREE ,EV_BUSY ) ==EV_SET ) { -return ; +break ; } } -/* -* This is the final check for a concurrent set, so it does need -* a smp_mb() pairing with the second barrier of qemu_event_set(). -* The barrier is inside the FUTEX_WAIT system call. -*/ qemu_futex_wait (ev,EV_BUSY ); } }
Signature · 1 line
2.49.0
PATCH v4 2/11 futex: Support Windows
In reply to Akihiko OdakiWindows supports futex-like APIs since Windows 8 and Windows Server 2012.
--- meson.build | 2 ++ include/qemu/futex.h | 53 ++++++++++++++++++++++++++++++--------- tests/unit/test-aio-multithread.c | 2 +- util/lockcnt.c | 2 +- util/qemu-thread-posix.c | 4 +-- util/meson.build | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-)
meson.build +2 -0
@@ -837,11 +837,13 @@ emulator_link_args = [] midl = not_found widl = not_found pathcch = not_found +synchronization = not_found host_dsosuf = '.so' if host_os == 'windows' midl = find_program('midl', required: false) widl = find_program('widl', required: false) pathcch = cc.find_library('pathcch') + synchronization = cc.find_library('Synchronization') socket = cc.find_library('ws2_32') winmm = cc.find_library('winmm')
include/qemu/futex.h +41 -12
79 lines not on this page · raw message
tests/unit/test-aio-multithread.c +1 -1
@@ -330,7 +330,7 @@ static void mcs_mutex_unlock(void)/* Wake up the next in line. */ next =qatomic_read (&nodes[id].next); nodes[next].locked =0 ; -qemu_futex_wake (&nodes[next].locked,1 ); +qemu_futex_wake_single (&nodes[next].locked); }static void test_multi_fair_mutex_entry (void *opaque)
util/lockcnt.c +1 -1
@@ -106,7 +106,7 @@ static bool qemu_lockcnt_cmpxchg_or_wait(QemuLockCnt *lockcnt, int *val,static void lockcnt_wake (QemuLockCnt *lockcnt) {trace_lockcnt_futex_wake (lockcnt); -qemu_futex_wake (&lockcnt->count,1 ); +qemu_futex_wake_single (&lockcnt->count); }void qemu_lockcnt_inc (QemuLockCnt *lockcnt)
util/qemu-thread-posix.c +2 -2
@@ -345,7 +345,7 @@ static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)/* Valid transitions: * - free->set, when setting the event -* - busy->set, when setting the event, followed by qemu_futex_wake +* - busy->set, when setting the event, followed by qemu_futex_wake_all * - set->free, when resetting the event * - free->busy, when waiting *
@@ -396,7 +396,7 @@ void qemu_event_set(QemuEvent *ev)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 (ev,INT_MAX ); +qemu_futex_wake_all (ev); } } }
util/meson.build +1 -1
@@ -27,7 +27,7 @@ else util_ss.add(files('event_notifier-win32.c')) util_ss.add(files('oslib-win32.c')) util_ss.add(files('qemu-thread-win32.c')) - util_ss.add(winmm, pathcch) + util_ss.add(winmm, pathcch, synchronization) endif util_ss.add(when: linux_io_uring, if_true: files('fdmon-io_uring.c')) if glib_has_gslice
Signature · 1 line
2.49.0
PATCH v4 3/11 qemu-thread: Remove qatomic_read() in qemu_event_set()
In reply to Akihiko OdakiThe 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.
--- util/qemu-thread-posix.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-)
util/qemu-thread-posix.c +5 -14
@@ -386,18 +386,9 @@ void qemu_event_set(QemuEvent *ev) {assert (ev->initialized); -/* -* 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); } }
@@ -413,7 +404,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 (); }
@@ -428,7 +419,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(). */ value =qatomic_load_acquire (&ev->value);if (value ==EV_SET ) {
Signature · 1 line
2.49.0
PATCH v4 4/11 qemu-thread: Replace __linux__ with CONFIG_LINUX
In reply to Akihiko Odakiscripts/checkpatch.pl warns for __linux__ saying "architecture specific defines should be avoided".
--- include/qemu/thread-posix.h | 2 +- util/qemu-thread-posix.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/qemu/thread-posix.h +1 -1
@@ -33,7 +33,7 @@ struct QemuSemaphore { };struct QemuEvent { -#ifndef __linux__ +#ifndef CONFIG_LINUX pthread_mutex_t lock;pthread_cond_t cond;#endif
util/qemu-thread-posix.c +3 -3
@@ -317,7 +317,7 @@ void qemu_sem_wait(QemuSemaphore *sem)qemu_mutex_unlock (&sem->mutex); } -#ifdef __linux__ +#ifdef CONFIG_LINUX #include "qemu/futex.h" #else static inline void qemu_futex_wake (QemuEvent *ev,int n)
@@ -363,7 +363,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 +376,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
Signature · 1 line
2.49.0
Re: [PATCH v4 04/11] qemu-thread: Replace __linux__ with CONFIG_LINUX
Quoted 8 lines · Akihiko Odaki
scripts/checkpatch.plwarns for__linux__saying "architecture specific defines should be avoided".Signed-off-by Akihiko Odaki <[email protected]>--- include/qemu/thread-posix.h | 2 +- util/qemu-thread-posix.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
PATCH v4 5/11 qemu-thread: Avoid futex abstraction for non-Linux
In reply to Akihiko Odakiqemu-thread used to abstract pthread primitives into futex for the QemuEvent implementation of POSIX systems other than Linux. However, this abstraction has one key difference: unlike futex, pthread primitives require an explicit destruction, and it must be ordered after wait and wake operations.
It would be easier to perform destruction if a wait operation ensures the corresponding wake operation finishes as POSIX semaphore does, but that requires to protect state accesses in qemu_event_set() and qemu_event_wait() with a mutex. On the other hand, real futex does not need such a protection but needs complex barrier and atomic operations to ensure ordering between the two functions.
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 to ensure ordering between them.
--- util/qemu-thread-posix.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-)
util/qemu-thread-posix.c +16 -25
@@ -319,28 +319,6 @@ void qemu_sem_wait(QemuSemaphore *sem)#ifdef CONFIG_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:
@@ -386,10 +364,17 @@ void qemu_event_set(QemuEvent *ev) {assert (ev->initialized); +#ifdef CONFIG_LINUX 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 }void qemu_event_reset (QemuEvent *ev)
@@ -411,17 +396,16 @@ void qemu_event_reset(QemuEvent *ev)void qemu_event_wait (QemuEvent *ev) { -unsigned value; -assert (ev->initialized); +#ifdef CONFIG_LINUX while (true) {/* * 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 store-release in qemu_event_set(). */ - value =qatomic_load_acquire (&ev->value); +unsigned value =qatomic_load_acquire (&ev->value);if (value ==EV_SET ) {break ; }
@@ -445,6 +429,13 @@ void qemu_event_wait(QemuEvent *ev) qemu_futex_wait(ev, EV_BUSY); } +#else +pthread_mutex_lock (&ev->lock); +while (qatomic_read (&ev->value) !=EV_SET ) { +pthread_cond_wait (&ev->cond, &ev->lock); + } +pthread_mutex_unlock (&ev->lock); +#endif }static __threadNotifierList thread_exit;
Signature · 1 line
2.49.0
PATCH v4 6/11 qemu-thread: Use futex for QemuEvent on Windows
In reply to Akihiko OdakiUse the futex-based implementation of QemuEvent on Windows to remove code duplication and remove the overhead of event object construction and destruction.
--- include/qemu/thread-posix.h | 9 ---- include/qemu/thread-win32.h | 6 --- include/qemu/thread.h | 11 +++- util/event.c | 122 +++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-posix.c | 121 ----------------------------------------- util/qemu-thread-win32.c | 129 -------------------------------------------- util/meson.build | 1 + 7 files changed, 133 insertions(+), 266 deletions(-)
include/qemu/thread-posix.h +0 -9
@@ -32,15 +32,6 @@ struct QemuSemaphore {unsigned int count; }; -struct QemuEvent { -#ifndef CONFIG_LINUX -pthread_mutex_t lock; -pthread_cond_t cond; -#endif -unsigned value; -bool initialized; -}; -struct QemuThread {pthread_t thread; };
include/qemu/thread-win32.h +0 -6
@@ -28,12 +28,6 @@ struct QemuSemaphore {bool initialized; }; -struct QemuEvent { -int value; -HANDLE event; -bool initialized; -}; -typedef struct QemuThreadData QemuThreadData ;struct QemuThread {QemuThreadData *data;
include/qemu/thread.h +10 -1
@@ -3,13 +3,22 @@#include "qemu/processor.h" #include "qemu/atomic.h" +#include "qemu/futex.h" typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ; -typedef struct QemuEvent QemuEvent ;typedef struct QemuLockCnt QemuLockCnt ;typedef struct QemuThread QemuThread ; +typedef struct QemuEvent { +#ifndef HAVE_FUTEX +pthread_mutex_t lock; +pthread_cond_t cond; +#endif +unsigned value; +bool initialized; +}QemuEvent ; +#ifdef _WIN32#include "qemu/thread-win32.h" #else
util/event.c new file +122 -0
122 lines not on this page · raw message
util/qemu-thread-posix.c +0 -121
127 lines not on this page · raw message
util/qemu-thread-win32.c +0 -129
135 lines not on this page · raw message
util/meson.build +1 -0
@@ -35,6 +35,7 @@ if glib_has_gslice endif util_ss.add(files('defer-call.c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) +util_ss.add(files('event.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c')) util_ss.add(files('fifo8.c'))
Signature · 1 line
2.49.0
Re: [PATCH v4 06/11] qemu-thread: Use futex for QemuEvent on Windows
Quoted 5 lines · Akihiko Odaki
Use the futex-based implementation of QemuEvent on Windows to remove code duplication and remove the overhead of event object construction and destruction.
Signed-off-by Akihiko Odaki <[email protected]>
Quoted 9 lines
--- include/qemu/thread-posix.h | 9 ---- include/qemu/thread-win32.h | 6 --- include/qemu/thread.h | 11 +++- util/event.c | 122 +++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-posix.c | 121 ----------------------------------------- util/qemu-thread-win32.c | 129 -------------------------------------------- util/meson.build | 1 + 7 files changed, 133 insertions(+), 266 deletions(-)
Great.
PATCH v4 7/11 qemu-thread: Use futex if available for QemuLockCnt
In reply to Akihiko OdakiThis unlocks the futex-based implementation of QemuLockCnt to Windows.
--- include/qemu/lockcnt.h | 2 +- util/lockcnt.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-)
include/qemu/lockcnt.h +1 -1
@@ -17,7 +17,7 @@typedef struct QemuLockCnt QemuLockCnt ;struct QemuLockCnt { -#ifndef CONFIG_LINUX +#ifndef HAVE_FUTEX QemuMutex mutex;#endif unsigned count;
util/lockcnt.c +4 -3
@@ -12,10 +12,11 @@#include "qemu/atomic.h" #include "trace.h" -#ifdef CONFIG_LINUX -#include "qemu/futex.h" +#ifdef HAVE_FUTEX -/* On Linux, bits 0-1 are a futex-based lock, bits 2-31 are the counter. +/* +* When futex is available, bits 0-1 are a futex-based lock, bits 2-31 are the +* counter. * For the mutex algorithm see Ulrich Drepper's "Futexes Are Tricky" (ok, * this is not the most relaxing citation I could make...). It is similar * to mutex2 in the paper.
Signature · 1 line
2.49.0
PATCH v4 8/11 migration: Replace QemuSemaphore with QemuEvent
In reply to Akihiko Odakipause_event can utilize qemu_event_reset() to discard events.
--- migration/migration.h | 2 +- migration/migration.c | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-)
migration/migration.h +1 -1
@@ -379,7 +379,7 @@ struct MigrationState {QemuSemaphore wait_unplug_sem;/* Migration is paused due to pause-before-switchover */ -QemuSemaphore pause_sem; +QemuEvent pause_event;/* The semaphore is used to notify COLO thread that failover is finished */ QemuSemaphore colo_exit_sem;
migration/migration.c +9 -12
@@ -1630,7 +1630,7 @@ void migration_cancel(void) }/* If the migration is paused, kick it out of the pause */ if (old_state ==MIGRATION_STATUS_PRE_SWITCHOVER ) { -qemu_sem_post (&s->pause_sem); +qemu_event_set (&s->pause_event); }migrate_set_state (&s->state, old_state,MIGRATION_STATUS_CANCELLING ); }while (s->state !=MIGRATION_STATUS_CANCELLING );
@@ -2342,7 +2342,7 @@ void qmp_migrate_continue(MigrationStatus state, Error **errp) MigrationStatus_str(s->state));return ; } -qemu_sem_post (&s->pause_sem); +qemu_event_set (&s->pause_event); }int migration_rp_wait (MigrationState *s)
@@ -2911,21 +2911,18 @@ static bool migration_switchover_prepare(MigrationState *s)return true; } -/* Since leaving this state is not atomic with posting the semaphore +/* +* Since leaving this state is not atomic with setting the event * it's possible that someone could have issued multiple migrate_continue -* and the semaphore is incorrectly positive at this point; -* the docs say it's undefined to reinit a semaphore that's already -* init'd, so use timedwait to eat up any existing posts. +* and the event is incorrectly set at this point so reset it. */ -while (qemu_sem_timedwait (&s->pause_sem,1 ) ==0 ) { -/* This block intentionally left blank */ - } +qemu_event_reset (&s->pause_event);/* Update [POSTCOPY_]ACTIVE to PRE_SWITCHOVER */ migrate_set_state (&s->state, s->state,MIGRATION_STATUS_PRE_SWITCHOVER );bql_unlock (); -qemu_sem_wait (&s->pause_sem); +qemu_event_wait (&s->pause_event);bql_lock ();/*
@@ -4057,7 +4054,7 @@ static void migration_instance_finalize(Object *obj)qemu_mutex_destroy (&ms->qemu_file_lock);qemu_sem_destroy (&ms->wait_unplug_sem);qemu_sem_destroy (&ms->rate_limit_sem); -qemu_sem_destroy (&ms->pause_sem); +qemu_event_destroy (&ms->pause_event);qemu_sem_destroy (&ms->postcopy_pause_sem);qemu_sem_destroy (&ms->rp_state.rp_sem);qemu_sem_destroy (&ms->rp_state.rp_pong_acks);
@@ -4072,7 +4069,7 @@ static void migration_instance_init(Object *obj) ms->state =MIGRATION_STATUS_NONE ; ms->mbps =-1 ; ms->pages_per_second =-1 ; -qemu_sem_init (&ms->pause_sem,0 ); +qemu_event_init (&ms->pause_event, false);qemu_mutex_init (&ms->error_mutex);migrate_params_init (&ms->parameters);
Signature · 1 line
2.49.0
Re: [PATCH v4 08/11] migration: Replace QemuSemaphore with QemuEvent
Quoted 7 lines · Akihiko Odaki
pause_eventcan utilizeqemu_event_reset()to discard events.Signed-off-by Akihiko Odaki <[email protected]>--- migration/migration.h | 2 +- migration/migration.c | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-)
PATCH v4 9/11 migration/colo: Replace QemuSemaphore with QemuEvent
In reply to Akihiko Odakicolo_exit_sem and colo_incoming_sem represent one-shot events so they can be converted into QemuEvent, which is more lightweight.
--- migration/migration.h | 6 +++--- migration/colo.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-)
migration/migration.h +3 -3
@@ -186,7 +186,7 @@ struct MigrationIncomingState {/* The coroutine we should enter (back) after failover */ Coroutine *colo_incoming_co; -QemuSemaphore colo_incoming_sem; +QemuEvent colo_incoming_event;/* Optional load threads pool and its thread exit request flag */ ThreadPool *load_threads;
@@ -381,8 +381,8 @@ struct MigrationState {/* Migration is paused due to pause-before-switchover */ QemuEvent pause_event; -/* The semaphore is used to notify COLO thread that failover is finished */ -QemuSemaphore colo_exit_sem; +/* The event is used to notify COLO thread that failover is finished */ +QemuEvent colo_exit_event;/* The event is used to notify COLO thread to do checkpoint */ QemuEvent colo_checkpoint_event;
migration/colo.c +10 -10
@@ -146,7 +146,7 @@ static void secondary_vm_do_failover(void)return ; }/* Notify COLO incoming thread that failover work is finished */ -qemu_sem_post (&mis->colo_incoming_sem); +qemu_event_set (&mis->colo_incoming_event);/* For Secondary VM, jump to incoming co */ if (mis->colo_incoming_co) {
@@ -195,7 +195,7 @@ static void primary_vm_do_failover(void) }/* Notify COLO thread that failover work is finished */ -qemu_sem_post (&s->colo_exit_sem); +qemu_event_set (&s->colo_exit_event); }COLOMode get_colo_mode (void )
@@ -620,8 +620,8 @@ out: }/* Hope this not to be too long to wait here */ -qemu_sem_wait (&s->colo_exit_sem); -qemu_sem_destroy (&s->colo_exit_sem); +qemu_event_wait (&s->colo_exit_event); +qemu_event_destroy (&s->colo_exit_event);/* * It is safe to unregister notifier after failover finished.
@@ -651,7 +651,7 @@ void migrate_start_colo_process(MigrationState *s) s->colo_delay_timer =timer_new_ms (QEMU_CLOCK_HOST , colo_checkpoint_notify_timer,NULL ); -qemu_sem_init (&s->colo_exit_sem,0 ); +qemu_event_init (&s->colo_exit_event, false);colo_process_checkpoint (s);bql_lock (); }
@@ -808,11 +808,11 @@ void colo_shutdown(void)case COLO_MODE_PRIMARY : s =migrate_get_current ();qemu_event_set (&s->colo_checkpoint_event); -qemu_sem_post (&s->colo_exit_sem); +qemu_event_set (&s->colo_exit_event);break ;case COLO_MODE_SECONDARY : mis =migration_incoming_get_current (); -qemu_sem_post (&mis->colo_incoming_sem); +qemu_event_set (&mis->colo_incoming_event);break ;default :break ;
@@ -827,7 +827,7 @@ static void *colo_process_incoming_thread(void *opaque)Error *local_err =NULL ;rcu_register_thread (); -qemu_sem_init (&mis->colo_incoming_sem,0 ); +qemu_event_init (&mis->colo_incoming_event, false);migrate_set_state (&mis->state,MIGRATION_STATUS_ACTIVE ,MIGRATION_STATUS_COLO );
@@ -923,8 +923,8 @@ out: }/* Hope this not to be too long to loop here */ -qemu_sem_wait (&mis->colo_incoming_sem); -qemu_sem_destroy (&mis->colo_incoming_sem); +qemu_event_wait (&mis->colo_incoming_event); +qemu_event_destroy (&mis->colo_incoming_event);rcu_unregister_thread ();return NULL ;
Signature · 1 line
2.49.0
Re: [PATCH v4 09/11] migration/colo: Replace QemuSemaphore with QemuEvent
Quoted 9 lines · Akihiko Odaki
colo_exit_semandcolo_incoming_semrepresent one-shot events so they can be converted into QemuEvent, which is more lightweight.Signed-off-by Akihiko Odaki <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>--- migration/migration.h | 6 +++--- migration/colo.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-)
PATCH v4 10/11 migration/postcopy: Replace QemuSemaphore with QemuEvent
In reply to Akihiko Odakithread_sync_sem is an one-shot event so it can be converted into QemuEvent, which is more lightweight.
--- migration/migration.h | 4 ++-- migration/postcopy-ram.c | 10 +++++----- migration/savevm.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-)
migration/migration.h +2 -2
@@ -98,9 +98,9 @@ struct MigrationIncomingState {void (*transport_cleanup)(void *data);/* * Used to sync thread creations. Note that we can't create threads in -* parallel with this sem. +* parallel with this event. */ -QemuSemaphore thread_sync_sem; +QemuEvent thread_sync_event;/* * Free at the start of the main state load, set as the main thread finishes * loading state.
migration/postcopy-ram.c +5 -5
@@ -90,10 +90,10 @@ void postcopy_thread_create(MigrationIncomingState *mis, QemuThread *thread, const char *name, void *(*fn)(void *), int joinable) { - qemu_sem_init(&mis->thread_sync_sem, 0); + qemu_event_init(&mis->thread_sync_event, false);qemu_thread_create (thread, name, fn, mis, joinable); -qemu_sem_wait (&mis->thread_sync_sem); -qemu_sem_destroy (&mis->thread_sync_sem); +qemu_event_wait (&mis->thread_sync_event); +qemu_event_destroy (&mis->thread_sync_event); }/* Postcopy needs to detect accesses to pages that haven't yet been copied
@@ -964,7 +964,7 @@ static void *postcopy_ram_fault_thread(void *opaque)trace_postcopy_ram_fault_thread_entry ();rcu_register_thread (); mis->last_rb =NULL ;/* last RAMBlock we sent part of */ -qemu_sem_post (&mis->thread_sync_sem); +qemu_event_set (&mis->thread_sync_event);struct pollfd *pfd;size_t pfd_len =2 + mis->postcopy_remote_fds->len;
@@ -1716,7 +1716,7 @@ void *postcopy_preempt_thread(void *opaque)rcu_register_thread (); -qemu_sem_post (&mis->thread_sync_sem); +qemu_event_set (&mis->thread_sync_event);/* * The preempt channel is established in asynchronous way. Wait
migration/savevm.c +1 -1
@@ -2078,7 +2078,7 @@ static void *postcopy_ram_listen_thread(void *opaque)migrate_set_state (&mis->state,MIGRATION_STATUS_ACTIVE ,MIGRATION_STATUS_POSTCOPY_ACTIVE ); -qemu_sem_post (&mis->thread_sync_sem); +qemu_event_set (&mis->thread_sync_event);trace_postcopy_ram_listen_thread_start ();rcu_register_thread ();
Signature · 1 line
2.49.0
PATCH v4 11/11 hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
In reply to Akihiko Odakisem in AppleGFXReadMemoryJob is an one-shot event so it can be converted into QemuEvent, which is more specialized for such a use case.
--- hw/display/apple-gfx.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
hw/display/apple-gfx.m +5 -5
@@ -454,7 +454,7 @@ static void set_cursor_glyph(void *opaque) /* ------ DMA (device reading system memory) ------ */ typedef struct AppleGFXReadMemoryJob { - QemuSemaphore sem; + QemuEvent event; hwaddr physical_address; uint64_t length; void *dst;
@@ -470,7 +470,7 @@ static void apple_gfx_do_read_memory(void *opaque) job->dst, job->length, MEMTXATTRS_UNSPECIFIED); job->success = (r == MEMTX_OK); - qemu_sem_post(&job->sem); + qemu_event_set(&job->event); } static bool apple_gfx_read_memory(AppleGFXState *s, hwaddr physical_address,
@@ -483,11 +483,11 @@ static bool apple_gfx_read_memory(AppleGFXState *s, hwaddr physical_address, trace_apple_gfx_read_memory(physical_address, length, dst); /* Performing DMA requires BQL, so do it in a BH. */ - qemu_sem_init(&job.sem, 0); + qemu_event_init(&job.event, 0); aio_bh_schedule_oneshot(qemu_get_aio_context(), apple_gfx_do_read_memory, &job); - qemu_sem_wait(&job.sem); - qemu_sem_destroy(&job.sem); + qemu_event_wait(&job.event); + qemu_event_destroy(&job.event); return job.success; }
Signature · 1 line
2.49.0
Re: [PATCH v4 11/11] hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
Quoted 7 lines · Akihiko Odaki
sem in AppleGFXReadMemoryJob is an one-shot event so it can be converted into QemuEvent, which is more specialized for such a use case.
Signed-off-by Akihiko Odaki <[email protected]>--- hw/display/apple-gfx.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Re: [PATCH v4 11/11] hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
Quoted 3 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Akihiko Odaki
sem in AppleGFXReadMemoryJob is an one-shot event so it can be converted into QemuEvent, which is more specialized for such a use case.
BTW it would be nice to document that in "qemu/thread.h" API.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Akihiko Odaki <[email protected]>---
hw/display/apple-gfx.m | 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Re: [PATCH v4 11/11] hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Akihiko Odaki
sem in AppleGFXReadMemoryJob is an one-shot event so it can be converted into QemuEvent, which is more specialized for such a use case.
BTW it would be nice to document that in "
qemu/thread.h" API.
I will add a documentation with the next version.
Quoted 11 lines · Akihiko Odaki
Akihiko Odaki (11):
futex: Check value afterqemu_futex_wait()
futex: Support Windows
qemu-thread: Removeqatomic_read()inqemu_event_set()
qemu-thread: Replace__linux__withCONFIG_LINUX
qemu-thread: Avoid futex abstraction for non-Linux
qemu-thread: Use futex for QemuEvent on Windows
qemu-thread: Use futex if available for QemuLockCnt
migration: Replace QemuSemaphore with QemuEvent migration/colo: Replace QemuSemaphore with QemuEvent migration/postcopy: Replace QemuSemaphore with QemuEvent
In case it makes things easier.. I queued the three migration patches; AFAIU they look like standalone to go even without prior patches, meanwhile it shouldn't be an issue if they're queued in two pulls.
I am still not sure whether patch 1 is needed at all, but I'll leave that to others to decide.
Thanks!
Quoted 1 line
hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent
Signature · 1 line
Peter Xu
Quoted 13 lines · Peter Xu
Quoted 11 lines · Akihiko Odaki
Akihiko Odaki (11):
futex: Check value afterqemu_futex_wait()
futex: Support Windows
qemu-thread: Removeqatomic_read()inqemu_event_set()
qemu-thread: Replace__linux__withCONFIG_LINUX
qemu-thread: Avoid futex abstraction for non-Linux
qemu-thread: Use futex for QemuEvent on Windows
qemu-thread: Use futex if available for QemuLockCnt
migration: Replace QemuSemaphore with QemuEvent migration/colo: Replace QemuSemaphore with QemuEvent migration/postcopy: Replace QemuSemaphore with QemuEvent
Quoted 6 lines
In case it makes things easier.. I queued the three migration patches; AFAIU they look like standalone to go even without prior patches, meanwhile it shouldn't be an issue if they're queued in two pulls.
I am still not sure whether patch 1 is needed at all, but I'll leave that to others to decide.
The migration patches shouldn't be applied before patches "futex: Check value after qemu_futex_wait()" and "qemu-thread: Avoid futex abstraction for non-Linux" as they fix QemuEvent. Merging migration patches earlier can trigger bugs just like one we faced with hw/display/apple-gfx*
Regarding patch 1 ("futex: Check value after qemu_futex_wait()"), I can argue that we should have it to comply the generic "upstream-first" principle; the upstream (man page) says to make a loop, so the downstream (QEMU) should follow that until the upstream says otherwise. But I think it's a good idea to spend efforts to understand the reasoning behind the man page since it's so important that it affects not only QEMU but also any userspace program that uses libpthread (i.e., practically everything).
Regards,
Akihiko Odaki
Quoted 18 lines · Akihiko Odaki
Quoted 17 lines · Peter Xu
Quoted 11 lines · Akihiko Odaki
Akihiko Odaki (11):
futex: Check value afterqemu_futex_wait()
futex: Support Windows
qemu-thread: Removeqatomic_read()inqemu_event_set()
qemu-thread: Replace__linux__withCONFIG_LINUX
qemu-thread: Avoid futex abstraction for non-Linux
qemu-thread: Use futex for QemuEvent on Windows
qemu-thread: Use futex if available for QemuLockCnt
migration: Replace QemuSemaphore with QemuEvent migration/colo: Replace QemuSemaphore with QemuEvent migration/postcopy: Replace QemuSemaphore with QemuEventIn case it makes things easier.. I queued the three migration patches; AFAIU they look like standalone to go even without prior patches, meanwhile it shouldn't be an issue if they're queued in two pulls.
Quoted 7 lines
Quoted 2 lines
I am still not sure whether patch 1 is needed at all, but I'll leave that to others to decide.
The migration patches shouldn't be applied before patches "futex: Check value after
qemu_futex_wait()" and "qemu-thread: Avoid futex abstraction for non-Linux" as they fix QemuEvent. Merging migration patches earlier can trigger bugs just like one we faced with hw/display/apple-gfx*
I didn't see anything like it mentioned in either cover letter or the apple-gfx patch. Could you provide a pointer?
Quoted 8 lines
Regarding patch 1 ("futex: Check value after
qemu_futex_wait()"), I can argue that we should have it to comply the generic "upstream-first" principle; the upstream (man page) says to make a loop, so the downstream (QEMU) should follow that until the upstream says otherwise. But I think it's a good idea to spend efforts to understand the reasoning behind the man page since it's so important that it affects not only QEMU but also any userspace program that uses libpthread (i.e., practically everything).
IMHO it's not how we define upstream/downstream, but maybe it's me who missed something.
It's fine - as long as someone agrees with you (Paolo? :), it could likely be that I was wrong and I just didn't realize that.
Thanks for the update anyway, I will drop the migration patches regardless.
Signature · 1 line
Peter Xu
Quoted 22 lines · Peter Xu
Quoted 21 lines · Akihiko Odaki
Quoted 19 lines · Peter Xu
Quoted 11 lines · Akihiko Odaki
Akihiko Odaki (11):
futex: Check value afterqemu_futex_wait()
futex: Support Windows
qemu-thread: Removeqatomic_read()inqemu_event_set()
qemu-thread: Replace__linux__withCONFIG_LINUX
qemu-thread: Avoid futex abstraction for non-Linux
qemu-thread: Use futex for QemuEvent on Windows
qemu-thread: Use futex if available for QemuLockCnt
migration: Replace QemuSemaphore with QemuEvent migration/colo: Replace QemuSemaphore with QemuEvent migration/postcopy: Replace QemuSemaphore with QemuEventIn case it makes things easier.. I queued the three migration patches; AFAIU they look like standalone to go even without prior patches, meanwhile it shouldn't be an issue if they're queued in two pulls.
I am still not sure whether patch 1 is needed at all, but I'll leave that to others to decide.
Quoted 7 lines
Quoted 4 lines
The migration patches shouldn't be applied before patches "futex: Check value after
qemu_futex_wait()" and "qemu-thread: Avoid futex abstraction for non-Linux" as they fix QemuEvent. Merging migration patches earlier can trigger bugs just like one we faced with hw/display/apple-gfx*I didn't see anything like it mentioned in either cover letter or the apple-gfx patch. Could you provide a pointer?
The previous email had a URL at the bottom:
https://lore.kernel.org/qemu-devel/[email protected]/
This thread discussed a bug of QemuEvent and reached to a conclusion to use QemuSemaphore instead to avoid it. This patch series intends to fix it.
Regards,
Akihiko Odaki
Quoted 2 lines · Akihiko Odaki
Changes in v4:
- Added patch "qemu-thread: Removeqatomic_read()inqemu_event_set()".
Hi Akihiko,
I'm not so confident about putting this patch before the other changes; I'm referring basically to this hunk:
util/event.c +3 -16
@@ -48,22 +48,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 ) { -int old =qatomic_xchg (&ev->value,EV_SET ); - -/* Pairs with memory barrier in kernel futex_wait system call. */ -smp_mb__after_rmw (); -if (old ==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);
... feel free to resubmit that separately, also because it's missing a smp_mb__before_rmw().
Also, I'm not sure what was your opinion of the more optimized version of qemu_event_reset:
util/event.c +23 -0
@@ -78,6 +78,7 @@ void qemu_event_reset(QemuEvent *ev) {assert (ev->initialized); +#ifdef HAVE_FUTEX /* * If there was a concurrent reset (or even reset+wait), * do nothing. Otherwise change EV_SET->EV_FREE.
@@ -86,6 +87,28 @@ void qemu_event_reset(QemuEvent *ev)*/ smp_mb__after_rmw (); +#else +/* +* If futexes are not available, there are no EV_FREE->EV_BUSY +* transitions because wakeups are done entirely through the +* condition variable. Since qatomic_set() only writes EV_FREE, +* the load seems useless but in reality, the acquire synchronizes +* with qemu_event_set()'s store release: if qemu_event_reset() +* sees EV_SET here, then the caller will certainly see a +* successful condition and skip qemu_event_wait(): +* +* done = 1; if (done == 0) +* qemu_event_set() { qemu_event_reset() { +* lock(); +* ev->value = EV_SET -----> load ev->value +* ev->value = old value | EV_FREE +* cond_broadcast() +* unlock(); } +* } if (done == 0) +* // qemu_event_wait() not called +*/ +qatomic_set (&ev->value,qatomic_load_acquire (&ev->value) |EV_FREE ); +#endif }void qemu_event_wait (QemuEvent *ev)
Do you think it's incorrect? I'll wait for your answer before sending out the actual pull request.
Paolo
Quoted 41 lines · Paolo Bonzini
Quoted 2 lines · Akihiko Odaki
Changes in v4:
- Added patch "qemu-thread: Removeqatomic_read()inqemu_event_set()".Hi Akihiko,
I'm not so confident about putting this patch before the other changes; I'm referring basically to this hunk:
util/event.c +3
-16@@ -48,22 +48,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) { - int old = qatomic_xchg(&ev->value, EV_SET); - - /* Pairs with memory barrier in kernel futex_wait system call. */ - smp_mb__after_rmw(); - if (old == 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);
Quoted 2 lines
... feel free to resubmit that separately, also because it's missing a
smp_mb__before_rmw().
I'd like to submit it with "[PATCH v4 05/11] qemu-thread: Avoid futex abstraction for non-Linux" because it aligns the implementations of Linux and non-Linux versions to rely on a store-release of EV_SET in qemu_event_set().
smp_mb__before_rmw() is removed with "[PATCH v4 01/11] futex: Check value after qemu_futex_wait()" because the fact mentioned with the comment "Pairs with memory barrier in kernel futex_wait system call" is no longer relevant; the patch makes QEMU always rely on qatomic_load_acquire() or qatomic_cmpxchg() to perform a load-acquire of EV_SET for ordering.
In either case, I can simply that say the ordering between qemu_event_set() and qemu_event_wait() are ensured by load-acquire and store-release operations of EV_SET, but to make the correctness absolutely sure, I recommend to look at my past explanation:
https://lore.kernel.org/qemu-devel/[email protected]
The explanation is long but so comprehensive that it lists all memory accesses, ordering requirements, and features of atomic primitives and futex employed to satisfy the requirements.
Quoted 47 lines
Also, I'm not sure what was your opinion of the more optimized version of
qemu_event_reset:util/event.c +23
-0@@ -78,6 +78,7 @@ void qemu_event_reset(QemuEvent *ev) { assert(ev->initialized); +#ifdef HAVE_FUTEX /** If there was a concurrent reset (or even reset+wait), * do nothing. Otherwise change EV_SET->EV_FREE. @@ -86,6 +87,28 @@ void qemu_event_reset(QemuEvent *ev)*/ smp_mb__after_rmw(); +#else + /* + * If futexes are not available, there are no EV_FREE->EV_BUSY + * transitions because wakeups are done entirely through the + * condition variable. Since qatomic_set() only writes EV_FREE, + * the load seems useless but in reality, the acquire synchronizes + * with qemu_event_set()'s store release: if qemu_event_reset() + * sees EV_SET here, then the caller will certainly see a + * successful condition and skip qemu_event_wait(): + * + * done = 1; if (done == 0) + * qemu_event_set() { qemu_event_reset() { + * lock(); + * ev->value = EV_SET -----> load ev->value + * ev->value = old value | EV_FREE + * cond_broadcast() + * unlock(); } + * } if (done == 0) + * // qemu_event_wait() not called + */ + qatomic_set(&ev->value, qatomic_load_acquire(&ev->value) | EV_FREE); +#endif } void qemu_event_wait(QemuEvent *ev)
Quoted 2 lines
Do you think it's incorrect? I'll wait for your answer before sending out the actual pull request.
It's correct, but I don't think it's worthwhile.
This code path is only used by platforms without a futex wrapper. Currently we only have one for Linux and this series adds one for Windows, but FreeBSD[1] and OpenBSD[2] have their own futex. macOS also gained one with version 14.4.[3] We can add wrappers for them too if their performance really matters.
So the only platforms listed in docs/about/build-platforms.rst that require the non-futex version are macOS older than 14.4 and NetBSD. macOS older than 14.4 will not be supported after June 5 since macOS 14 was released June 5, 2023 and docs/about/build-platforms.rst says:
Quoted 6 lines
Support for the previous major version will be dropped 2 years after the new major version is released or when the vendor itself drops support, whichever comes first.
Within each major release, only the most recent minor release is considered.
There are too few relevant platforms to justify the effort potentially needed for quality assurance.
Moreover, qemu_event_reset() is often followed by qemu_event_wait() or other barriers so probably relaxing ordering here does not affect the overall ordering constraint (and performance) much.
Regards,
Akihiko Odaki
[1] https://man.freebsd.org/cgi/man.cgi?query=_umtx_op&apropos=0&sektion=2&manpath=FreeBSD+14.2-RELEASE&arch=default&format=html
[2] https://man.openbsd.org/futex
[3] https://developer.apple.com/documentation/os/synchronization?language=objc#Futex-Conditional-Wait-Primitives
Quoted 4 lines · Akihiko Odaki
I'd like to submit it with "[PATCH v4 05/11] qemu-thread: Avoid futex abstraction for non-Linux" because it aligns the implementations of Linux and non-Linux versions to rely on a store-release of
EV_SETinqemu_event_set().
Ok, I see what you mean - you would like the xchg to be an xchg_release essentially.
There is actually one case in which skipping the xchg has an effect. If you have the following:
- one side does
s.foo = 1; qemu_event_set(&s.ev);
- the other side never reaches the qemu_event_reset(&s.ev)
then skipping the xchg might allow the cacheline for ev to remain shared. This is unlikely to *make* a difference, though it does *exist* as a difference, so I will review the patch, but I really prefer to place it last. It's safer to take a known-working algorithm, apply it to all OSes (or at least Linux and Windows), and only then you refine it. It also makes my queue shorter.
Quoted 15 lines
Quoted 2 lines
Do you think it's incorrect? I'll wait for your answer before sending out the actual pull request.
It's correct, but I don't think it's worthwhile.
This code path is only used by platforms without a futex wrapper. Currently we only have one for Linux and this series adds one for Windows, but FreeBSD[1] and OpenBSD[2] have their own futex. macOS also gained one with version 14.4.[3] We can add wrappers for them too if their performance really matters.
So the only platforms listed indocs/about/build-platforms.rstthat require the non-futex version are macOS older than 14.4 and NetBSD. macOS older than 14.4 will not be supported after June 5 since macOS 14 was released June 5, 2023 anddocs/about/build-platforms.rstsays:
Quoted 2 lines
There are too few relevant platforms to justify the effort potentially needed for quality assurance.
Ok, nice. So it's really just NetBSD in the end.
Quoted 3 lines
Moreover,
qemu_event_reset()is often followed byqemu_event_wait()or other barriers so probably relaxing ordering here does not affect the overall ordering constraint (and performance) much.
Understood. For me it wasn't really about performance, but more about understanding exactly which reorderings can happen and what synchronizes with what. Load-acquire/store-release are simpler to understand in that respect, especially since this use of condvar, without the mutex in reset, is different from everything else that I've ever seen.
Paolo
Quoted 17 lines · Paolo Bonzini
Quoted 4 lines · Akihiko Odaki
I'd like to submit it with "[PATCH v4 05/11] qemu-thread: Avoid futex abstraction for non-Linux" because it aligns the implementations of Linux and non-Linux versions to rely on a store-release of
EV_SETinqemu_event_set().Ok, I see what you mean - you would like the xchg to be an
xchg_releaseessentially.There is actually one case in which skipping the xchg has an effect. If you have the following:
- one side does
s.foo = 1; qemu_event_set(&s.ev);
Quoted 8 lines
- the other side never reaches the
qemu_event_reset(&s.ev)then skipping the xchg might allow the cacheline for ev to remain shared. This is unlikely to *make* a difference, though it does *exist* as a difference, so I will review the patch, but I really prefer to place it last. It's safer to take a known-working algorithm, apply it to all OSes (or at least Linux and Windows), and only then you refine it. It also makes my queue shorter.
Compilers and processors are free to make such an optimization so the contract between us and them does not change in that sense.
On the other hand, the removal of smb_mb() does change the contract; now compilers and processors are free to reorder loads and stores specified before qemu_event_set() after the load of ev->value, which was not possible in the past.
I'm confident that the change of the contract is safe, but it makes sense to pay extra caution. I'll reorder the patches with the next version.
Regards,
Akihiko Odaki