change some odd-looking atomic uses
cover change some odd-looking atomic uses
I spotted the weird-looking pattern of:
atomic_set(atomic_load() <some operation> N)
in a few palces and one variable in the graph-lock code which was used with atomics except for a single case, which also seemed suspicious.
I'm not sure if there are any known compiler-optimizations or ordering semantics already ensuring that these operations are indeed working correctly atomically, so I thought I'd point them out and ask about it by sending patches.
In patch 2 the ordering is changed (see the note in its mail)
Wolfgang Bumiller (2):
graph-lock: make sure reader_count access is atomic
atomics: replace fetch-use-store with direct atomic operations
block/graph-lock.c | 8 +++----- util/aio-posix.c | 3 +-- util/aio-win32.c | 3 +-- util/async.c | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/2 graph-lock: make sure reader_count access is atomic
There's one case where reader_count is accessed non-atomically. This was likely seen as being "guarded by a mutex" held in that block, but other access to this does not actually depend on the mutex and already uses atomics.
Additionally this replaces the pattern of atomic_set(atomic_read() +
1) with qatomic_inc() (and -1 with _dec)
--- block/graph-lock.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
block/graph-lock.c +3 -5
@@ -176,8 +176,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void) bdrv_graph =qemu_get_current_aio_context ()->bdrv_graph;for (;;) { -qatomic_set (&bdrv_graph->reader_count, - bdrv_graph->reader_count +1 ); +qatomic_inc (&bdrv_graph->reader_count);/* make sure writer sees reader_count before we check has_writer */ smp_mb ();
@@ -226,7 +225,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void) }/* slow path where reader sleeps */ - bdrv_graph->reader_count--; +qatomic_dec (&bdrv_graph->reader_count);aio_wait_kick ();qemu_co_queue_wait (&reader_queue, &aio_context_list_lock); }
@@ -238,8 +237,7 @@ void coroutine_fn bdrv_graph_co_rdunlock(void)BdrvGraphRWlock *bdrv_graph; bdrv_graph =qemu_get_current_aio_context ()->bdrv_graph; -qatomic_store_release (&bdrv_graph->reader_count, - bdrv_graph->reader_count -1 ); +qatomic_dec (&bdrv_graph->reader_count);/* make sure writer sees reader_count before we check has_writer */ smp_mb ();
Signature · 1 line
2.39.2
PATCH 2/2 atomics: replace fetch-use-store with direct atomic operations
In reply to Wolfgang BumillerReplaces the pattern atomic_store(atomic_load() <op> something) pattern with its direct atomic function.
---
Note: these previously used RELEASE ordering for the store and relaxed ordering for the reads, while the replacement uses SEQ_CST, as there are no other wrappers around yet. Should we add qatomic_fetch_{sub,and}_release variants?
util/aio-posix.c | 3 +-- util/aio-win32.c | 3 +-- util/async.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-)
util/aio-posix.c +1 -2
@@ -672,8 +672,7 @@ bool aio_poll(AioContext *ctx, bool blocking)if (use_notify_me) {/* Finish the poll before clearing the flag. */ -qatomic_store_release (&ctx->notify_me, -qatomic_read (&ctx->notify_me) -2 ); +qatomic_fetch_sub (&ctx->notify_me,2 ); }aio_notify_accept (ctx);
util/aio-win32.c +1 -2
@@ -387,8 +387,7 @@ bool aio_poll(AioContext *ctx, bool blocking) ret =WaitForMultipleObjects (count, events, FALSE, timeout);if (blocking) {assert (first); -qatomic_store_release (&ctx->notify_me, -qatomic_read (&ctx->notify_me) -2 ); +qatomic_fetch_sub (&ctx->notify_me,2 );aio_notify_accept (ctx); }
util/async.c +1 -1
@@ -330,7 +330,7 @@ aio_ctx_check(GSource *source)BHListSlice *s;/* Finish computing the timeout before clearing the flag. */ -qatomic_store_release (&ctx->notify_me,qatomic_read (&ctx->notify_me) & ~1 ); +qatomic_fetch_and (&ctx->notify_me, ~1 );aio_notify_accept (ctx); QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
Signature · 1 line
2.39.2
Quoted 10 lines · Wolfgang Bumiller
I spotted the weird-looking pattern of:
atomic_set(atomic_load() <some operation> N)
in a few palces and one variable in the graph-lock code which was used with atomics except for a single case, which also seemed suspicious.I'm not sure if there are any known compiler-optimizations or ordering semantics already ensuring that these operations are indeed working correctly atomically, so I thought I'd point them out and ask about it by sending patches.
Hi Wolfgang,
indeed the usage is intended - but thanks a lot for the patches, it's certainly a good way to proceed!
Here's a quick explanation:
- bdrv_graph_co_rdlock()/bdrv_graph_co_rdunlock(): only one coroutine can run in each AioContext at a time, so the writes to bdrv_graph->reader_count are effectively thread-local. It's just reads that need to be synchronized.
- aio_poll(): similar, as the writer is the thread for the AioContext, while the readers are the other threads that call aio_notify() for that AioContext.
- aio_ctx_prepare(): same idea as aio_poll(), in this case the writer is the thread running the GLib event loop (almost always the main thread, except with the Cocoa UI because macOS wants the main thread for itself).
So why is this written in full instead of using fetch-add-store? From most to least important:
- to make you think about who reads and who writes, i.e. to show that no atomicity is needed
- because it's faster
The disadvantage of course is the verbosity.
Thanks,
Paolo
Quoted 16 lines
In patch 2 the ordering is changed (see the note in its mail)
Wolfgang Bumiller (2):
graph-lock: make surereader_countaccess is atomic
atomics: replace fetch-use-store with direct atomic operationsblock/graph-lock.c | 8 +++----- util/aio-posix.c | 3 +-- util/aio-win32.c | 3 +-- util/async.c | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-)--
2.39.2Hi