aio: first part of aio_context_acquire/release pushdown
v2 cover aio: first part of aio_context_acquire/release pushdown
This is the infrastructure part of the aio_context_acquire/release pushdown, which in turn is the first step towards a real multiqueue block layer in QEMU. The next step is to touch all the drivers and move calls to the aio_context_acquire/release functions from aio-*.c to the drivers. This will be done in a separate patch series, which I plan to post before soft freeze.
While the inserted lines are a lot, more than half of it are in documentation and formal models of the code, as well as in the implementation of the new "lockcnt" synchronization primitive. The code is also very heavily commented.
The first four patches are new, as the issue they fix was found after posting the previous patch. Everything else is more or less the same as before.
Paolo
v1->v2: Update documentation [Stefan]
Remove g_usleep from testcase [Stefan]
Paolo Bonzini (16):
aio: introduce aio_context_in_iothread
aio: do not really acquire/release the main AIO context
aio: introduce aio_poll_internal
aio: only call aio_poll_internal from iothread
iothread: release AioContext around aio_poll
qemu-thread: introduce QemuRecMutex
aio: convert from RFifoLock to QemuRecMutex
aio: rename bh_lock to list_lock
qemu-thread: introduce QemuLockCnt
aio: make ctx->list_lock a QemuLockCnt, subsuming ctx->walking_bh
qemu-thread: optimize QemuLockCnt with futexes on Linux
aio: tweak walking in dispatch phase
aio-posix: remove walking_handlers, protecting AioHandler list with list_lock
aio-win32: remove walking_handlers, protecting AioHandler list with list_lock
aio: document locking
aio: push aio_context_acquire/release down to dispatching
aio-posix.c | 86 +++++---- aio-win32.c | 106 ++++++----- async.c | 279 +++++++++++++++++++++++---- block/io.c | 14 +- docs/aio_poll_drain.promela | 210 +++++++++++++++++++++ docs/aio_poll_drain_bug.promela | 158 ++++++++++++++++ docs/aio_poll_sync_io.promela | 88 +++++++++ docs/lockcnt.txt | 342 ++++++++++++++++++++++++++++++++++ docs/multiple-iothreads.txt | 63 ++++--- include/block/aio.h | 69 ++++--- include/qemu/futex.h | 36 ++++ include/qemu/rfifolock.h | 54 ------ include/qemu/thread-posix.h | 6 + include/qemu/thread-win32.h | 10 + include/qemu/thread.h | 23 +++ iothread.c | 20 +- stubs/iothread-lock.c | 5 + tests/.gitignore | 1 - tests/Makefile | 2 - tests/test-aio.c | 22 ++- tests/test-rfifolock.c | 91 --------- trace-events | 10 + util/Makefile.objs | 2 +- util/lockcnt.c | 404 ++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-posix.c | 38 ++-- util/qemu-thread-win32.c | 25 +++ util/rfifolock.c | 78 -------- 27 files changed, 1792 insertions(+), 450 deletions(-) create mode 100644 docs/aio_poll_drain.promela create mode 100644 docs/aio_poll_drain_bug.promela create mode 100644 docs/aio_poll_sync_io.promela create mode 100644 docs/lockcnt.txt create mode 100644 include/qemu/futex.h delete mode 100644 include/qemu/rfifolock.h delete mode 100644 tests/test-rfifolock.c create mode 100644 util/lockcnt.c delete mode 100644 util/rfifolock.c
Signature · 1 line
2.5.0
PATCH v2 1/16 aio: introduce aio_context_in_iothread
This will be used by the synchronous I/O helpers, to choose between aio_poll or QemuEvent.
--- include/block/aio.h | 7 +++++++ iothread.c | 9 +++++++++ 2 files changed, 16 insertions(+)
include/block/aio.h +7 -0
@@ -435,6 +435,13 @@ static inline bool aio_node_check(AioContext *ctx, bool is_external) }/** +* @ctx: the aio context +* +* Return whether we are running in the I/O thread that manages @ctx. +*/ +bool aio_context_in_iothread (AioContext *ctx); + +/** * aio_context_setup: * @ctx: the aio context *
iothread.c +9 -0
@@ -20,6 +20,7 @@#include "qmp-commands.h" #include "qemu/error-report.h" #include "qemu/rcu.h" +#include "qemu/main-loop.h" typedef ObjectClass IOThreadClass ;
@@ -28,6 +29,13 @@ typedef ObjectClass IOThreadClass;#define IOTHREAD_CLASS (klass) \ OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD) +static __threadIOThread *my_iothread; + +bool aio_context_in_iothread (AioContext *ctx) +{ +return ctx == (my_iothread ? my_iothread->ctx :qemu_get_aio_context ()); +} +static void *iothread_run (void *opaque) {IOThread *iothread = opaque;
@@ -35,6 +43,7 @@ static void *iothread_run(void *opaque)rcu_register_thread (); + my_iothread = iothread;qemu_mutex_lock (&iothread->init_done_lock); iothread->thread_id =qemu_get_thread_id ();qemu_cond_signal (&iothread->init_done_cond);
Signature · 1 line
2.5.0
PATCH v2 2/16 aio: do not really acquire/release the main AIO context
In reply to Paolo BonziniThe main AIO context is used in many places that are not aware of AioContexts at all. bdrv_drain will soon do a release/acquire itself, which for the main AIO context would break because code calls bdrv_drain on it without acquiring anything.
Very soon, bdrv will be ready for removal of aio_context_acquire from non-block-layer code. The idea is that the AioContext will be acquired by bdrv_*, and no one will care of what's running in the main I/O thread or in the dataplane thread. Even if there are two concurrent instances of the I/O thread, locks protect the data structures; this evolves naturally to the multiqueue case where there are multiple I/O threads touching the same BlockDriverState.
When this happens, aio_context_acquire/release can go away, replaced by fine-grained locks, and this hack will also go away with it.
--- async.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
async.c +10 -2
@@ -369,10 +369,18 @@ void aio_context_unref(AioContext *ctx)void aio_context_acquire (AioContext *ctx) { -rfifolock_lock (&ctx->lock); +if (ctx ==qemu_get_aio_context ()) { +assert (qemu_mutex_iothread_locked ()); + }else { +rfifolock_lock (&ctx->lock); + } }void aio_context_release (AioContext *ctx) { -rfifolock_unlock (&ctx->lock); +if (ctx ==qemu_get_aio_context ()) { +assert (qemu_mutex_iothread_locked ()); + }else { +rfifolock_unlock (&ctx->lock); + } }
Signature · 1 line
2.5.0
PATCH v2 3/16 aio: introduce aio_poll_internal
In reply to Paolo BonziniMove the implemntation of aio_poll to aio_poll_internal, and introduce a wrapper for public use. For now it just asserts that aio_poll is being used correctly---either from the thread that manages the context, or with the QEMU global mutex held.
The next patch, however, will completely separate the two cases.
--- aio-posix.c | 2 +- aio-win32.c | 2 +- async.c | 8 ++++++++ include/block/aio.h | 6 ++++++ 4 files changed, 16 insertions(+), 2 deletions(-)
aio-posix.c +1 -1
@@ -401,7 +401,7 @@ static void add_pollfd(AioHandler *node) npfd++; } -bool aio_poll (AioContext *ctx,bool blocking) +bool aio_poll_internal (AioContext *ctx,bool blocking) {AioHandler *node;int i, ret;
aio-win32.c +1 -1
@@ -281,7 +281,7 @@ bool aio_dispatch(AioContext *ctx)return progress; } -bool aio_poll (AioContext *ctx,bool blocking) +bool aio_poll_internal (AioContext *ctx,bool blocking) {AioHandler *node;HANDLE events[MAXIMUM_WAIT_OBJECTS +1 ];
async.c +8 -0
@@ -300,6 +300,14 @@ void aio_notify_accept(AioContext *ctx) } } +bool aio_poll (AioContext *ctx,bool blocking) +{ +assert (qemu_mutex_iothread_locked () || +aio_context_in_iothread (ctx)); + +return aio_poll_internal (ctx, blocking); +} +static void aio_timerlist_notify (void *opaque) {aio_notify (opaque);
include/block/aio.h +6 -0
@@ -287,6 +287,12 @@ bool aio_pending(AioContext *ctx);*/ bool aio_dispatch (AioContext *ctx); +/* Same as aio_poll, but only meant for use in the I/O thread. +* +* This is used internally in the implementation of aio_poll. +*/ +bool aio_poll_internal (AioContext *ctx,bool blocking); +/* Progress in completing AIO work to occur. This can issue new pending * aio as a result of executing I/O completion or bh callbacks. *
Signature · 1 line
2.5.0
PATCH v2 4/16 aio: only call aio_poll_internal from iothread
In reply to Paolo Bonziniaio_poll is not thread safe; it can report progress incorrectly when called from the main thread. The bug remains latent as long as all of it is called within aio_context_acquire/aio_context_release, but this will change soon.
The details of the bug are pretty simple, but fixing it in an efficient way is thorny. There are plenty of comments and formal models in the patch, so I will refer to it.
--- async.c | 218 +++++++++++++++++++++++++++++++++++++++- block/io.c | 14 ++- docs/aio_poll_drain.promela | 210 ++++++++++++++++++++++++++++++++++++++ docs/aio_poll_drain_bug.promela | 158 +++++++++++++++++++++++++++++ docs/aio_poll_sync_io.promela | 88 ++++++++++++++++ docs/multiple-iothreads.txt | 20 ++-- include/block/aio.h | 12 ++- stubs/iothread-lock.c | 5 + 8 files changed, 703 insertions(+), 22 deletions(-) create mode 100644 docs/aio_poll_drain.promela create mode 100644 docs/aio_poll_drain_bug.promela create mode 100644 docs/aio_poll_sync_io.promela
async.c +215 -3
@@ -300,12 +300,224 @@ void aio_notify_accept(AioContext *ctx) } } +/* aio_poll_internal is not thread-safe; it only reports progress +* correctly when called from one thread, because it has no +* history of what happened in different threads. When called +* from two threads, there is a race: +* +* main thread I/O thread +* ----------------------- -------------------------- +* blk_drain +* bdrv_requests_pending -> true +* aio_poll_internal +* process last request +* aio_poll_internal +* +* Now aio_poll_internal will never exit, because there is no pending +* I/O on the AioContext. +* +* Therefore, aio_poll is a wrapper around aio_poll_internal that allows +* usage from _two_ threads: the I/O thread of course, and the main thread. +* When called from the main thread, aio_poll just asks the I/O thread +* for a nudge as soon as the next call to aio_poll is complete. +* Because we use QemuEvent, and QemuEvent supports a single consumer +* only, this only works when the calling thread holds the big QEMU lock. +* +* Because aio_poll is used in a loop, spurious wakeups are okay. +* Therefore, the I/O thread calls qemu_event_set very liberally +* (it helps that qemu_event_set is cheap on an already-set event). +* generally used in a loop, it's okay to have spurious wakeups. +* Similarly it is okay to return true when no progress was made +* (as long as this doesn't happen forever, or you get livelock). +* +* The important thing is that you need to report progress from +* aio_poll(ctx, false) correctly. This is complicated and the +* implementation builds on event_notifier_set plus +* aio_poll(ctx, true). +* +* The implementation consists of the following functions: +* - aio_poll_and_wake: runs in the iothread and takes care of +* waking up the main thread after aio_poll_internal returns +* +* - aio_wait_iteration: implementation of aio_poll(ctx, true) +* for the main thread +* +* - aio_force_iteration: implementation of aio_poll(ctx, false) +* for the main thread +*/ +static bool aio_poll_and_wake (AioContext *ctx,bool blocking) +{ +bool progress =aio_poll_internal (ctx, blocking); +smp_wmb (); +if (progress) { + ctx->progress = true; + } + +qemu_event_set (&ctx->sync_io_event); +return progress; +} + +static bool aio_wait_iteration (AioContext *ctx) +{ +/* Wait until at least one iteration has passed since the main thread +* last called aio_poll. +*/ +qemu_event_wait (&ctx->sync_io_event); + +/* Remember how aio_poll is used---in a loop, until a guard condition +* becomes true. By resetting the event here, we ensure that the guard +* condition will be checked before the next call to qemu_event_wait. +* The above race is resolved as follows. +* +* main thread I/O thread +* ----------------------- -------------------------- +* blk_drain +* bdrv_requests_pending -> true +* aio_poll_internal +* process last request +* qemu_event_set +* qemu_event_wait +*/ +qemu_event_reset (&ctx->sync_io_event); + +return atomic_xchg (&ctx->progress, false); +} + +/* The first idea is to simply implement non-blocking aio_poll as +* +* // cannot use aio_notify; because of the notify_me optimization, +* // aio_notify might do nothing and then poll will block +* event_notifier_set(&ctx->notifier); +* progress = aio_wait_iteration(ctx); +* +* which doesn't work because of a relatively simple race. Remember +* that spurious wakeups of aio_poll are common. What can happen then is: +* +* main thread I/O thread +* --------------------------- ------------------------- +* qemu_event_set(); +* ... +* qemu_bh_schedule() +* aio_poll(ctx, true) +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* ctx->progress = true; +* progress=xchg(ctx->progress, false) +* qemu_event_set(); +* ... +* aio_poll(ctx, false) +* event_notifier_set() +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* <<returns false>> +* +* aio_poll's contract ensures that *some* progress is made if possible; +* hence the bottom half should be executed before aio_poll(ctx, false) +* returns, and aio_poll should return true. The failure happened because +* aio_poll_internal has not run at all since the last qemu_event_wait; +* the execution threads interleaved so that aio_poll(ctx, false) +* immediately returns false. +* +* +* The next idea then is to add a reset of the event in the non-blocking +* aio_poll. This has the same problem, just a little harder to trigger: +* +* main thread I/O thread +* --------------------------- ----------------------------- +* qemu_event_set(); +* qemu_bh_schedule() +* aio_poll(ctx, true) +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* ctx->progress = true; +* progress=xchg(ctx->progress, false) +* ... +* aio_poll(ctx, false) +* qemu_event_reset(); +* qemu_event_set(); +* event_notifier_set() +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* progress=xchg(ctx->progress, false) +* <<returns false>> +* +* +* Let's change approach and try running aio_wait_iteration *twice*. +* This doesn't work either, but for a different reason. If the main +* thread is "slow", an entire execution of the loop happens in the I/O +* thread, and a preceding aio_poll(ctx, true) can leave ctx->progress +* equal to false. But unlike the previous example, some progress was +* made *after* qemu_event_wait returned: +* +* main thread I/O thread +* --------------------------- ----------------------------- +* qemu_event_set(); +* ... +* aio_poll(ctx, true) +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* ctx->progress = true; +* qemu_event_set(); +* ... +* qemu_bh_schedule() +* progress=xchg(ctx->progress, false) +* aio_poll(ctx, false) +* event_notifier_set() +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* progress=xchg(ctx->progress, false) +* qemu_event_set(); +* event_notifier_set() +* aio_wait_iteration +* qemu_event_wait(); +* qemu_event_reset(); +* progress|=xchg(ctx->progress, false) +* <<returns false>> +* +* However, applying both workarounds solves the problems. The initial +* qemu_event_reset removes leftovers of previously completed iterations +* (such as in the last example). Then, the double iteration ensures +* at least one execution of the dispatch phase to happen. The dispatch +* phase will then execute pending bottom halves and return progress +* correctly. +*/ +static bool aio_force_iteration (AioContext *ctx) +{ +bool progress; +qemu_event_reset (&ctx->sync_io_event); + +event_notifier_set (&ctx->notifier); + progress =aio_wait_iteration (ctx); + +event_notifier_set (&ctx->notifier); + progress |=aio_wait_iteration (ctx); + +return progress; +} +bool aio_poll (AioContext *ctx,bool blocking) { -assert (qemu_mutex_iothread_locked () || -aio_context_in_iothread (ctx)); +bool progress; + +if (aio_context_in_iothread (ctx)) { +return aio_poll_and_wake (ctx, blocking); + } -return aio_poll_internal (ctx, blocking); +assert (qemu_mutex_iothread_locked ()); +aio_context_release (ctx); +if (blocking) { + progress =aio_wait_iteration (ctx); + }else { + progress =aio_force_iteration (ctx); + } +aio_context_acquire (ctx); +return progress; }static void aio_timerlist_notify (void *opaque)
block/io.c +9 -5
@@ -270,15 +270,19 @@ static void bdrv_drain_recurse(BlockDriverState *bs)*/ void bdrv_drain (BlockDriverState *bs) { -bool busy = true; +bool busy;bdrv_drain_recurse (bs); -while (busy) { +do {/* Keep iterating */ bdrv_flush_io_queue (bs); - busy =bdrv_requests_pending (bs); - busy |=aio_poll (bdrv_get_aio_context (bs), busy); - } +if (bdrv_requests_pending (bs)) { +aio_poll (bdrv_get_aio_context (bs), true); + busy = true; + }else { + busy =aio_poll (bdrv_get_aio_context (bs), false); + } + }while (busy); }/*
docs/aio_poll_drain.promela new file +210 -0
210 lines not on this page · raw message
docs/aio_poll_drain_bug.promela new file +158 -0
158 lines not on this page · raw message
docs/aio_poll_sync_io.promela new file +88 -0
88 lines not on this page · raw message
docs/multiple-iothreads.txt +9 -11
@@ -88,17 +88,15 @@ descriptors, event notifiers, timers, or BHs across threads: notifier, timer, or BH callbacks invoked by the AioContext. No locking is necessary. -2. Other threads wishing to access the AioContext must use -aio_context_acquire()/aio_context_release() for mutual exclusion. Once the -context is acquired no other thread can access it or run event loop iterations -in this AioContext. - -aio_context_acquire()/aio_context_release() calls may be nested. This -means you can call them if you're not sure whether #1 applies. - -There is currently no lock ordering rule if a thread needs to acquire multiple -AioContexts simultaneously. Therefore, it is only safe for code holding the -QEMU global mutex to acquire other AioContexts. +2. Other threads wishing to access the AioContext must take the QEMU global +mutex *as well as* call aio_context_acquire()/aio_context_release() for +mutual exclusion. The QEMU global mutex must be taken outside the call +to aio_context_acquire(). + +No lock ordering rule is necessary if a thread needs to acquire multiple +AioContexts simultaneously. Because the IOThreads won't ever acquire +multiple AioContexts, it is always safe for the owner of the QEMU global +mutex to acquire any number of them. Side note: the best way to schedule a function call across threads is to create a BH in the target AioContext beforehand and then call qemu_bh_schedule(). No
include/block/aio.h +9 -3
@@ -129,6 +129,12 @@ struct AioContext {int epollfd;bool epoll_enabled;bool epoll_available; + +/* Returns whether there has been progress on the AioContext since +* the last time this field was set to false. +*/ +bool progress; +QemuEvent sync_io_event; };/**
@@ -296,9 +302,9 @@ bool aio_poll_internal(AioContext *ctx, bool blocking);/* Progress in completing AIO work to occur. This can issue new pending * aio as a result of executing I/O completion or bh callbacks. * -* Return whether any progress was made by executing AIO or bottom half -* handlers. If @blocking == true, this should always be true except -* if someone called aio_notify. +* Return whether any progress was made since the last call to aio_poll +* in the execution of AIO or bottom half handlers. If @blocking == +* true, this should always be true except if someone called aio_notify. * * If there are no pending bottom halves, but there are pending AIO * operations, it may not be possible to make any progress without
stubs/iothread-lock.c +5 -0
@@ -14,3 +14,8 @@ void qemu_mutex_lock_iothread(void)void qemu_mutex_unlock_iothread (void ) { } + +bool aio_context_in_iothread (AioContext *ctx) +{ +return true; +}
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 04/16] aio: only call aio_poll_internal from iothread
Quoted 34 lines · Paolo Bonzini
aio_pollis not thread safe; it can report progress incorrectly when called from the main thread. The bug remains latent as long as all of it is called within aio_context_acquire/aio_context_release, but this will change soon.The details of the bug are pretty simple, but fixing it in an efficient way is thorny. There are plenty of comments and formal models in the patch, so I will refer to it.
Signed-off-by Paolo Bonzini <[email protected]>---
+++
b/async.casync.c +17
-0@@ -300,12 +300,224 @@ void aio_notify_accept(AioContext *ctx) } } +/* aio_poll_internal is not thread-safe; it only reports progress +* correctly when called from one thread, because it has no +* history of what happened in different threads. When called +* from two threads, there is a race: +* +* main thread I/O thread +* ----------------------- -------------------------- +* blk_drain +* bdrv_requests_pending -> true +* aio_poll_internal +* process last request +* aio_poll_internal +* +* Now aio_poll_internal will never exit, because there is no pending +* I/O on the AioContext. +* +* Therefore, aio_poll is a wrapper around aio_poll_internal that allows
async.c void aio_notify_accept(AioContext *ctx) · line 329
Quoted 10 lines
async.c +10
-0+* usage from _two_ threads: the I/O thread of course, and the main thread. +* When called from the main thread, aio_poll just asks the I/O thread +* for a nudge as soon as the next call to aio_poll is complete. +* Because we use QemuEvent, and QemuEvent supports a single consumer +* only, this only works when the calling thread holds the big QEMU lock. +* +* Because aio_poll is used in a loop, spurious wakeups are okay. +* Therefore, the I/O thread calls qemu_event_set very liberally +* (it helps that qemu_event_set is cheap on an already-set event). +* generally used in a loop, it's okay to have spurious wakeups.
Incomplete sentence due to bad rebase leftovers?
Quoted 4 lines
async.c +4
-0+* Similarly it is okay to return true when no progress was made +* (as long as this doesn't happen forever, or you get livelock). +* +
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v2 5/16 iothread: release AioContext around aio_poll
In reply to Paolo BonziniThis is the first step towards having fine-grained critical sections in dataplane threads, which resolves lock ordering problems between address_space_* functions (which need the BQL when doing MMIO, even after we complete RCU-based dispatch) and the AioContext.
Because AioContext does not use contention callbacks anymore, the unit test has to be changed.
Previously applied as a0710f7995f914e3044e5899bd8ff6c43c62f916 and then reverted.
---
v1->v2: Update function names in documentation [Stefan]
Remove g_usleep from testcase [Stefan]
async.c | 22 ++++------------------ docs/multiple-iothreads.txt | 40 +++++++++++++++++++++++----------------- include/block/aio.h | 3 --- iothread.c | 11 ++--------- tests/test-aio.c | 22 ++++++++++++++-------- 5 files changed, 43 insertions(+), 55 deletions(-)
async.c +4 -18
@@ -85,8 +85,8 @@ int aio_bh_poll(AioContext *ctx)* aio_notify again if necessary. */ if (!bh->deleted &&atomic_xchg (&bh->scheduled,0 )) { -/* Idle BHs and the notify BH don't count as progress */ -if (!bh->idle && bh != ctx->notify_dummy_bh) { +/* Idle BHs don't count as progress */ +if (!bh->idle) { ret =1 ; } bh->idle =0 ;
@@ -238,7 +238,6 @@ aio_ctx_finalize(GSource *source) {AioContext *ctx = (AioContext *) source; -qemu_bh_delete (ctx->notify_dummy_bh);thread_pool_free (ctx->thread_pool);qemu_mutex_lock (&ctx->bh_lock);
@@ -525,19 +524,6 @@ static void aio_timerlist_notify(void *opaque)aio_notify (opaque); } -static void aio_rfifolock_cb (void *opaque) -{ -AioContext *ctx = opaque; - -/* Kick owner thread in case they are blocked in aio_poll() */ -qemu_bh_schedule (ctx->notify_dummy_bh); -} - -static void notify_dummy_bh (void *opaque) -{ -/* Do nothing, we were invoked just to force the event loop to iterate */ -} -static void event_notifier_dummy_cb (EventNotifier *e) { }
@@ -566,10 +552,10 @@ AioContext *aio_context_new(Error **errp) event_notifier_dummy_cb); ctx->thread_pool =NULL ;qemu_mutex_init (&ctx->bh_lock); -rfifolock_init (&ctx->lock, aio_rfifolock_cb, ctx); +rfifolock_init (&ctx->lock,NULL ,NULL );timerlistgroup_init (&ctx->tlg, aio_timerlist_notify, ctx); - ctx->notify_dummy_bh =aio_bh_new (ctx, notify_dummy_bh,NULL ); +qemu_event_init (&ctx->sync_io_event, true);return ctx; fail:
docs/multiple-iothreads.txt +23 -17
@@ -103,13 +103,10 @@ a BH in the target AioContext beforehand and then call qemu_bh_schedule(). No acquire/release or locking is needed for the qemu_bh_schedule() call. But be sure to acquire the AioContext for aio_bh_new() if necessary. -The relationship between AioContext and the block layer -------------------------------------------------------- -The AioContext originates from the QEMU block layer because it provides a -scoped way of running event loop iterations until all work is done. This -feature is used to complete all in-flight block I/O requests (see -bdrv_drain_all()). Nowadays AioContext is a generic event loop that can be -used by any QEMU subsystem. +AioContext and the block layer +------------------------------ +The AioContext originates from the QEMU block layer, even though nowadays +AioContext is a generic event loop that can be used by any QEMU subsystem. The block layer has support for AioContext integrated. Each BlockDriverState is associated with an AioContext using bdrv_set_aio_context() and
@@ -120,13 +117,22 @@ Block layer code must therefore expect to run in an IOThread and avoid using old APIs that implicitly use the main loop. See the "How to program for IOThreads" above for information on how to do that. -If main loop code such as a QMP function wishes to access a BlockDriverState it -must first call aio_context_acquire(bdrv_get_aio_context(bs)) to ensure the -IOThread does not run in parallel. - -Long-running jobs (usually in the form of coroutines) are best scheduled in the -BlockDriverState's AioContext to avoid the need to acquire/release around each -bdrv_*() call. Be aware that there is currently no mechanism to get notified -when bdrv_set_aio_context() moves this BlockDriverState to a different -AioContext (see bdrv_detach_aio_context()/bdrv_attach_aio_context()), so you -may need to add this if you want to support long-running jobs. +If main loop code such as a QMP function wishes to access a BlockDriverState +it must first call aio_context_acquire(bdrv_get_aio_context(bs)) to ensure +that callbacks in the IOThread do not run in parallel. + +Code running in the monitor typically needs to ensure that past +requests from the guest are completed. When a block device is running +in an IOThread, the IOThread can also process requests from the guest +(via ioeventfd). To achieve both objects, wrap the code between +bdrv_drained_begin() and bdrv_drained_end(), thus creating a "drained +section". The functions must be called between aio_context_acquire() +and aio_context_release(). You can freely release and re-acquire the +AioContext within a drained section. + +Long-running jobs (usually in the form of coroutines) are best scheduled in +the BlockDriverState's AioContext to avoid the need to acquire/release around +each bdrv_*() call. The functions bdrv_add/remove_aio_context_notifier, +or alternatively blk_add/remove_aio_context_notifier if you use BlockBackends, +can be used to get a notification whenever bdrv_set_aio_context() moves a +BlockDriverState to a different AioContext.
include/block/aio.h +0 -3
@@ -114,9 +114,6 @@ struct AioContext {bool notified;EventNotifier notifier; -/* Scheduling this BH forces the event loop it iterate */ -QEMUBH *notify_dummy_bh; -/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool;
iothread.c +2 -9
@@ -39,7 +39,6 @@ bool aio_context_in_iothread(AioContext *ctx)static void *iothread_run (void *opaque) {IOThread *iothread = opaque; -bool blocking;rcu_register_thread ();
@@ -49,14 +48,8 @@ static void *iothread_run(void *opaque)qemu_cond_signal (&iothread->init_done_cond);qemu_mutex_unlock (&iothread->init_done_lock); -while (!iothread->stopping) { -aio_context_acquire (iothread->ctx); - blocking = true; -while (!iothread->stopping &&aio_poll (iothread->ctx, blocking)) { -/* Progress was made, keep going */ - blocking = false; - } -aio_context_release (iothread->ctx); +while (!atomic_read (&iothread->stopping)) { +aio_poll (iothread->ctx, true); }rcu_unregister_thread ();
tests/test-aio.c +14 -8
@@ -99,6 +99,7 @@ static void event_ready_cb(EventNotifier *e)typedef struct {QemuMutex start_lock; +EventNotifier notifier;bool thread_acquired; }AcquireTestData ;
@@ -110,6 +111,11 @@ static void *test_acquire_thread(void *opaque)qemu_mutex_lock (&data->start_lock);qemu_mutex_unlock (&data->start_lock); +/* event_notifier_set might be called either before or after +* the main thread's call to poll(). The test case's outcome +* should be the same in either case. +*/ +event_notifier_set (&data->notifier);aio_context_acquire (ctx);aio_context_release (ctx);
@@ -124,20 +130,19 @@ static void set_event_notifier(AioContext *ctx, EventNotifier *notifier,aio_set_event_notifier (ctx, notifier, false, handler); } -static void dummy_notifier_read (EventNotifier *unused) +static void dummy_notifier_read (EventNotifier *n) { -g_assert (false);/* should never be invoked */ +event_notifier_test_and_clear (n); }static void test_acquire (void ) {QemuThread thread; -EventNotifier notifier;AcquireTestData data;/* Dummy event notifier ensures aio_poll() will block */ -event_notifier_init (¬ifier, false); -set_event_notifier (ctx, ¬ifier, dummy_notifier_read); +event_notifier_init (&data.notifier, false); +set_event_notifier (ctx, &data.notifier, dummy_notifier_read);g_assert (!aio_poll (ctx, false));/* consume aio_notify() */ qemu_mutex_init (&data.start_lock);
@@ -151,12 +156,13 @@ static void test_acquire(void)/* Block in aio_poll(), let other thread kick us and acquire context */ aio_context_acquire (ctx);qemu_mutex_unlock (&data.start_lock);/* let the thread run */ -g_assert (!aio_poll (ctx, true)); +g_assert (aio_poll (ctx, true)); +g_assert (!data.thread_acquired);aio_context_release (ctx);qemu_thread_join (&thread); -set_event_notifier (ctx, ¬ifier,NULL ); -event_notifier_cleanup (¬ifier); +set_event_notifier (ctx, &data.notifier,NULL ); +event_notifier_cleanup (&data.notifier);g_assert (data.thread_acquired); }
Signature · 1 line
2.5.0
PATCH v2 6/16 qemu-thread: introduce QemuRecMutex
In reply to Paolo BonziniGRecMutex is new in glib 2.32, so we cannot use it. Introduce a recursive mutex in qemu-thread instead, which will be used instead of RFifoLock.
--- include/qemu/thread-posix.h | 6 ++++++ include/qemu/thread-win32.h | 10 ++++++++++ include/qemu/thread.h | 3 +++ util/qemu-thread-posix.c | 13 +++++++++++++ util/qemu-thread-win32.c | 25 +++++++++++++++++++++++++ 5 files changed, 57 insertions(+)
include/qemu/thread-posix.h +6 -0
@@ -3,6 +3,12 @@#include "pthread.h" #include <semaphore.h> +typedef QemuMutex QemuRecMutex ; +#define qemu_rec_mutex_destroy qemu_mutex_destroy +#define qemu_rec_mutex_lock qemu_mutex_lock +#define qemu_rec_mutex_try_lock qemu_mutex_try_lock +#define qemu_rec_mutex_unlock qemu_mutex_unlock +struct QemuMutex {pthread_mutex_t lock; };
include/qemu/thread-win32.h +10 -0
@@ -7,6 +7,16 @@ struct QemuMutex {LONG owner; }; +typedef struct QemuRecMutex QemuRecMutex ; +struct QemuRecMutex { +CRITICAL_SECTION lock; +}; + +void qemu_rec_mutex_destroy (QemuMutex *mutex); +void qemu_rec_mutex_lock (QemuMutex *mutex); +int qemu_rec_mutex_trylock (QemuMutex *mutex); +void qemu_rec_mutex_unlock (QemuMutex *mutex); +struct QemuCond {LONG waiters, target;HANDLE sema;
include/qemu/thread.h +3 -0
@@ -25,6 +25,9 @@ void qemu_mutex_lock(QemuMutex *mutex);int qemu_mutex_trylock (QemuMutex *mutex);void qemu_mutex_unlock (QemuMutex *mutex); +/* Prototypes for other functions are in thread-posix.h/thread-win32.h. */ +void qemu_rec_mutex_init (QemuRecMutex *mutex); +void qemu_cond_init (QemuCond *cond);void qemu_cond_destroy (QemuCond *cond);
util/qemu-thread-posix.c +13 -0
@@ -80,6 +80,19 @@ void qemu_mutex_unlock(QemuMutex *mutex)error_exit (err, __func__); } +void qemu_rec_mutex_init (QemuRecMutex *mutex) +{ +int err; +pthread_mutexattr_t attr; + +pthread_mutexattr_init (&attr); +pthread_mutexattr_settype (&attr,PTHREAD_MUTEX_RECURSIVE ); + err =pthread_mutex_init (&mutex->lock, &attr); +if (err) { +error_exit (err, __func__); + } +} +void qemu_cond_init (QemuCond *cond) {int err;
util/qemu-thread-win32.c +25 -0
@@ -79,6 +79,31 @@ void qemu_mutex_unlock(QemuMutex *mutex)LeaveCriticalSection (&mutex->lock); } +void qemu_rec_mutex_init (QemuRecMutex *mutex) +{ +InitializeCriticalSection (&mutex->lock); +} + +void qemu_rec_mutex_destroy (QemuRecMutex *mutex) +{ +DeleteCriticalSection (&mutex->lock); +} + +void qemu_rec_mutex_lock (QemuRecMutex *mutex) +{ +EnterCriticalSection (&mutex->lock); +} + +int qemu_rec_mutex_trylock (QemuRecMutex *mutex) +{ +return !TryEnterCriticalSection (&mutex->lock); +} + +void qemu_rec_mutex_unlock (QemuRecMutex *mutex) +{ +LeaveCriticalSection (&mutex->lock); +} +void qemu_cond_init (QemuCond *cond) {memset (cond,0 ,sizeof (*cond));
Signature · 1 line
2.5.0
PATCH v2 7/16 aio: convert from RFifoLock to QemuRecMutex
In reply to Paolo BonziniIt is simpler and a bit faster, and QEMU does not need the contention callbacks (and thus the fairness) anymore.
--- async.c | 8 ++--- include/block/aio.h | 3 +- include/qemu/rfifolock.h | 54 ---------------------------- tests/.gitignore | 1 - tests/Makefile | 2 -- tests/test-rfifolock.c | 91 ------------------------------------------------ util/Makefile.objs | 1 - util/rfifolock.c | 78 ----------------------------------------- 8 files changed, 5 insertions(+), 233 deletions(-) delete mode 100644 include/qemu/rfifolock.h delete mode 100644 tests/test-rfifolock.c delete mode 100644 util/rfifolock.c
async.c +4 -4
@@ -254,7 +254,7 @@ aio_ctx_finalize(GSource *source)aio_set_event_notifier (ctx, &ctx->notifier, false,NULL );event_notifier_cleanup (&ctx->notifier); -rfifolock_destroy (&ctx->lock); +qemu_rec_mutex_destroy (&ctx->lock);qemu_mutex_destroy (&ctx->bh_lock);timerlistgroup_deinit (&ctx->tlg); }
@@ -552,7 +552,7 @@ AioContext *aio_context_new(Error **errp) event_notifier_dummy_cb); ctx->thread_pool =NULL ;qemu_mutex_init (&ctx->bh_lock); -rfifolock_init (&ctx->lock,NULL ,NULL ); +qemu_rec_mutex_init (&ctx->lock);timerlistgroup_init (&ctx->tlg, aio_timerlist_notify, ctx);qemu_event_init (&ctx->sync_io_event, true);
@@ -578,7 +578,7 @@ void aio_context_acquire(AioContext *ctx)if (ctx ==qemu_get_aio_context ()) {assert (qemu_mutex_iothread_locked ()); }else { -rfifolock_lock (&ctx->lock); +qemu_rec_mutex_lock (&ctx->lock); } }
@@ -587,6 +587,6 @@ void aio_context_release(AioContext *ctx)if (ctx ==qemu_get_aio_context ()) {assert (qemu_mutex_iothread_locked ()); }else { -rfifolock_unlock (&ctx->lock); +qemu_rec_mutex_unlock (&ctx->lock); } }
include/block/aio.h +1 -2
@@ -19,7 +19,6 @@#include "qemu/queue.h" #include "qemu/event_notifier.h" #include "qemu/thread.h" -#include "qemu/rfifolock.h" #include "qemu/timer.h" typedef struct BlockAIOCB BlockAIOCB ;
@@ -52,7 +51,7 @@ struct AioContext {GSource source;/* Protects all fields from multi-threaded access */ -RFifoLock lock; +QemuRecMutex lock;/* The list of registered AIO handlers */ QLIST_HEAD(, AioHandler) aio_handlers;
include/qemu/rfifolock.h deleted +0 -54
54 lines not on this page · raw message
tests/.gitignore +0 -1
@@ -51,7 +51,6 @@ test-qmp-introspect.[ch] test-qmp-marshal.c test-qmp-output-visitor test-rcu-list -test-rfifolock test-string-input-visitor test-string-output-visitor test-thread-pool
tests/Makefile +0 -2
@@ -39,7 +39,6 @@ check-unit-y += tests/test-visitor-serialization$(EXESUF) check-unit-y += tests/test-iov$(EXESUF) gcov-files-test-iov-y = util/iov.c check-unit-y += tests/test-aio$(EXESUF) -check-unit-$(CONFIG_POSIX) += tests/test-rfifolock$(EXESUF) check-unit-y += tests/test-throttle$(EXESUF) gcov-files-test-aio-$(CONFIG_WIN32) = aio-win32.c gcov-files-test-aio-$(CONFIG_POSIX) = aio-posix.c
@@ -403,7 +402,6 @@ tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y) tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) -tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o $(test-util-obj-y) tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y) tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y) tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y)
tests/test-rfifolock.c deleted +0 -91
91 lines not on this page · raw message
util/Makefile.objs +0 -1
@@ -23,7 +23,6 @@ util-obj-y += crc32c.o util-obj-y += throttle.o util-obj-y += getauxval.o util-obj-y += readline.o -util-obj-y += rfifolock.o util-obj-y += rcu.o util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y += qemu-coroutine-sleep.o
util/rfifolock.c deleted +0 -78
78 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v2 8/16 aio: rename bh_lock to list_lock
In reply to Paolo BonziniThis will be used for AioHandlers too. There is going to be little or no contention, so it is better to reuse the same lock.
--- async.c | 16 ++++++++-------- include/block/aio.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-)
async.c +8 -8
@@ -51,12 +51,12 @@ QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) .cb = cb, .opaque = opaque, }; -qemu_mutex_lock (&ctx->bh_lock); +qemu_mutex_lock (&ctx->list_lock); bh->next = ctx->first_bh;/* Make sure that the members are ready before putting bh into list */ smp_wmb (); ctx->first_bh = bh; -qemu_mutex_unlock (&ctx->bh_lock); +qemu_mutex_unlock (&ctx->list_lock);return bh; }
@@ -98,7 +98,7 @@ int aio_bh_poll(AioContext *ctx)/* remove deleted bhs */ if (!ctx->walking_bh) { -qemu_mutex_lock (&ctx->bh_lock); +qemu_mutex_lock (&ctx->list_lock); bhp = &ctx->first_bh;while (*bhp) { bh = *bhp;
@@ -109,7 +109,7 @@ int aio_bh_poll(AioContext *ctx) bhp = &bh->next; } } -qemu_mutex_unlock (&ctx->bh_lock); +qemu_mutex_unlock (&ctx->list_lock); }return ret;
@@ -240,7 +240,7 @@ aio_ctx_finalize(GSource *source)thread_pool_free (ctx->thread_pool); -qemu_mutex_lock (&ctx->bh_lock); +qemu_mutex_lock (&ctx->list_lock);while (ctx->first_bh) {QEMUBH *next = ctx->first_bh->next;
@@ -250,12 +250,12 @@ aio_ctx_finalize(GSource *source)g_free (ctx->first_bh); ctx->first_bh = next; } -qemu_mutex_unlock (&ctx->bh_lock); +qemu_mutex_unlock (&ctx->list_lock);aio_set_event_notifier (ctx, &ctx->notifier, false,NULL );event_notifier_cleanup (&ctx->notifier);qemu_rec_mutex_destroy (&ctx->lock); -qemu_mutex_destroy (&ctx->bh_lock); +qemu_mutex_destroy (&ctx->list_lock);timerlistgroup_deinit (&ctx->tlg); }
@@ -551,7 +551,7 @@ AioContext *aio_context_new(Error **errp) (EventNotifierHandler *) event_notifier_dummy_cb); ctx->thread_pool =NULL ; -qemu_mutex_init (&ctx->bh_lock); +qemu_mutex_init (&ctx->list_lock);qemu_rec_mutex_init (&ctx->lock);timerlistgroup_init (&ctx->tlg, aio_timerlist_notify, ctx);
include/block/aio.h +1 -1
@@ -88,7 +88,7 @@ struct AioContext {uint32_t notify_me;/* lock to protect between bh's adders and deleter */ -QemuMutex bh_lock; +QemuMutex list_lock;/* Anchor of the list of Bottom Halves belonging to the context */ struct QEMUBH *first_bh;
Signature · 1 line
2.5.0
PATCH v2 9/16 qemu-thread: introduce QemuLockCnt
In reply to Paolo BonziniA QemuLockCnt comprises a counter and a mutex, with primitives to increment and decrement the counter, and to take and release the mutex. It can be used to do lock-free visits to a data structure whenever mutexes would be too heavy-weight and the critical section is too long for RCU.
This could be implemented simply by protecting the counter with the mutex, but QemuLockCnt is harder to misuse and more efficient.
--- docs/lockcnt.txt | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 17 +++ util/Makefile.objs | 1 + util/lockcnt.c | 122 ++++++++++++++++++ 4 files changed, 483 insertions(+) create mode 100644 docs/lockcnt.txt create mode 100644 util/lockcnt.c
docs/lockcnt.txt new file +343 -0
@@ -0,0 +1,343 @@ +DOCUMENTATION FOR LOCKED COUNTERS (aka QemuLockCnt) +=================================================== + +QEMU often uses reference counts to track data structures that are being +accessed and should not be freed. For example, a loop that invoke +callbacks like this is not safe: + + QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { + if (ioh->revents & G_IO_OUT) { + ioh->fd_write(ioh->opaque); + } + } + +QLIST_FOREACH_SAFE protects against deletion of the current node (ioh) +by stashing away its "next" pointer. However, ioh->fd_write could +actually delete the next node from the list. The simplest way to +avoid this is to mark the node as deleted, and remove it from the +list in the above loop: + + QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { + if (ioh->deleted) { + QLIST_REMOVE(ioh, next); + g_free(ioh); + } else { + if (ioh->revents & G_IO_OUT) { + ioh->fd_write(ioh->opaque); + } + } + } + +If however this loop must also be reentrant, i.e. it is possible that +ioh->fd_write invokes the loop again, some kind of counting is needed: + + walking_handlers++; + QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { + if (ioh->deleted) { + if (walking_handlers == 1) { + QLIST_REMOVE(ioh, next); + g_free(ioh); + } + } else { + if (ioh->revents & G_IO_OUT) { + ioh->fd_write(ioh->opaque); + } + } + } + walking_handlers--; + +One may think of using the RCU primitives, rcu_read_lock() and +rcu_read_unlock(); effectively, the RCU nesting count would take +the place of the walking_handlers global variable. Indeed, +reference counting and RCU have similar purposes, but their usage in +general is complementary: + +- reference counting is fine-grained and limited to a single data + structure; RCU delays reclamation of *all* RCU-protected data + structures; + +- reference counting works even in the presence of code that keeps + a reference for a long time; RCU critical sections in principle + should be kept short; + +- reference counting is often applied to code that is not thread-safe + but is reentrant; in fact, usage of reference counting in QEMU predates + the introduction of threads by many years. RCU is generally used to + protect readers from other threads freeing memory after concurrent + modifications to a data structure. + +- reclaiming data can be done by a separate thread in the case of RCU; + this can improve performance, but also delay reclamation undesirably. + With reference counting, reclamation is deterministic. + +This file documents QemuLockCnt, an abstraction for using reference +counting in code that has to be both thread-safe and reentrant. + + +QemuLockCnt concepts +-------------------- + +A QemuLockCnt comprises both a counter and a mutex; it has primitives +to increment and decrement the counter, and to take and release the +mutex. The counter notes how many visits to the data structures are +taking place (the visits could be from different threads, or there could +be multiple reentrant visits from the same thread). The basic rules +governing the counter/mutex pair then are the following: + +- Data protected by the QemuLockCnt must not be freed unless the + counter is zero and the mutex is taken. + +- A new visit cannot be started while the counter is zero and the + mutex is taken. + +Most of the time, the mutex protects all writes to the data structure, +not just frees, though there could be cases where this is not necessary. + +Reads, instead, can be done without taking the mutex, as long as the +readers and writers use the same macros that are used for RCU, for +example atomic_rcu_read, atomic_rcu_set, QLIST_FOREACH_RCU, etc. This is +because the reads are done outside a lock and a set or QLIST_INSERT_HEAD +can happen concurrently with the read. The RCU API ensures that the +processor and the compiler see all required memory barriers. + +This could be implemented simply by protecting the counter with the +mutex, for example: + + // (1) + qemu_mutex_lock(&walking_handlers_mutex); + walking_handlers++; + qemu_mutex_unlock(&walking_handlers_mutex); + + ... + + // (2) + qemu_mutex_lock(&walking_handlers_mutex); + if (--walking_handlers == 0) { + QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { + if (ioh->deleted) { + QLIST_REMOVE(ioh, next); + g_free(ioh); + } + } + } + qemu_mutex_unlock(&walking_handlers_mutex); + +Here, no frees can happen in the code represented by the ellipsis. +If another thread is executing critical section (2), that part of +the code cannot be entered, because the thread will not be able +to increment the walking_handlers variable. And of course +during the visit any other thread will see a nonzero value for +walking_handlers, as in the single-threaded code. + +Note that it is possible for multiple concurrent accesses to delay +the cleanup arbitrarily; in other words, for the walking_handlers +counter to never become zero. For this reason, this technique is +more easily applicable if concurrent access to the structure is rare. + +However, critical sections are easy to forget since you have to do +them for each modification of the counter. QemuLockCnt ensures that +all modifications of the counter take the lock appropriately, and it +can also be more efficient in two ways: + +- it avoids taking the lock for many operations (for example + incrementing the counter while it is non-zero); + +- on some platforms, one could implement QemuLockCnt to hold the + lock and the mutex in a single word, making it no more expensive + than simply managing a counter using atomic operations (see + docs/atomics.txt). This is not implemented yet, but can be + very helpful if concurrent access to the data structure is + expected to be rare. + + +Using the same mutex for frees and writes can still incur some small +inefficiencies; for example, a visit can never start if the counter is +zero and the mutex is taken---even if the mutex is taken by a write, +which in principle need not block a visit of the data structure. +However, these are usually not a problem if any of the following +assumptions are valid: + +- concurrent access is possible but rare + +- writes are rare + +- writes are frequent, but this kind of write (e.g. appending to a + list) has a very small critical section. + +For example, QEMU uses QemuLockCnt to manage an AioContext's list of +bottom halves and file descriptor handlers. Modifications to the list +of file descriptor handlers are rare. Creation of a new bottom half is +frequent and can happen on a fast path; however: 1) it is almost never +concurrent with a visit to the list of bottom halves; 2) it only has +three instructions in the critical path, two assignments and a smp_wmb(). + + +QemuLockCnt API +--------------- + + void qemu_lockcnt_init(QemuLockCnt *lockcnt); + + Initialize lockcnt's counter to zero and prepare its mutex + for usage. + + void qemu_lockcnt_destroy(QemuLockCnt *lockcnt); + + Destroy lockcnt's mutex. + + void qemu_lockcnt_inc(QemuLockCnt *lockcnt); + + If the lockcnt's count is zero, wait for critical sections + to finish and increment lockcnt's count to 1. If the count + is not zero, just increment it. + + Because this function can wait on the mutex, it must not be + called while the lockcnt's mutex is held by the current thread. + For the same reason, qemu_lockcnt_inc can also contribute to + AB-BA deadlocks. This is a sample deadlock scenario: + + thread 1 thread 2 + ------------------------------------------------------- + qemu_lockcnt_lock(&lc1); + qemu_lockcnt_lock(&lc2); + qemu_lockcnt_inc(&lc2); + qemu_lockcnt_inc(&lc1); + + void qemu_lockcnt_dec(QemuLockCnt *lockcnt); + + Decrement lockcnt's count. + + bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt); + + Decrement the count. If the new count is zero, lock + the mutex and return true. Otherwise, return false. + + bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt); + + If the count is 1, decrement the count to zero, lock + the mutex and return true. Otherwise, return false. + + void qemu_lockcnt_lock(QemuLockCnt *lockcnt); + + Lock the lockcnt's mutex. Remember that concurrent visits + are not blocked unless the count is also zero. You can + use qemu_lockcnt_count to check for this inside a critical + section. + + void qemu_lockcnt_unlock(QemuLockCnt *lockcnt); + + Release the lockcnt's mutex. + + void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt); + + This is the same as + + qemu_lockcnt_unlock(lockcnt); + qemu_lockcnt_inc(lockcnt); + + but more efficient. + + int qemu_lockcnt_count(QemuLockCnt *lockcnt); + + Return the lockcnt's count. The count can change at any time + any time; still, while the lockcnt is locked, one can usefully + check whether the count is non-zero. + + +QemuLockCnt usage +----------------- + +This section explains the typical usage patterns for QemuLockCnt functions. + +Setting a variable to a non-NULL value can be done between +qemu_lockcnt_lock and qemu_lockcnt_unlock: + + qemu_lockcnt_lock(&xyz_lockcnt); + if (!xyz) { + new_xyz = g_new(XYZ, 1); + ... + atomic_rcu_set(&xyz, new_xyz); + } + qemu_lockcnt_unlock(&xyz_lockcnt); + +Accessing the value can be done between qemu_lockcnt_inc and +qemu_lockcnt_dec: + + qemu_lockcnt_inc(&xyz_lockcnt); + if (xyz) { + XYZ *p = atomic_rcu_read(&xyz); + ... + /* Accesses can now be done through "p". */ + } + qemu_lockcnt_dec(&xyz_lockcnt); + +Freeing the object can similarly use qemu_lockcnt_lock and +qemu_lockcnt_unlock, but you also need to ensure that the count +is zero (i.e. there is no concurrent visit). Because qemu_lockcnt_inc +takes the QemuLockCnt's lock, the count cannot become non-zero while +the object is being freed. Freeing an object looks like this: + + qemu_lockcnt_lock(&xyz_lockcnt); + if (!qemu_lockcnt_count(&xyz_lockcnt)) { + g_free(xyz); + xyz = NULL; + } + qemu_lockcnt_unlock(&xyz_lockcnt); + +If an object has to be freed right after a visit, you can combine +the decrement, the locking and the check on count as follows: + + qemu_lockcnt_inc(&xyz_lockcnt); + if (xyz) { + XYZ *p = atomic_rcu_read(&xyz); + ... + /* Accesses can now be done through "p". */ + } + if (qemu_lockcnt_dec_and_lock(&xyz_lockcnt)) { + g_free(xyz); + xyz = NULL; + qemu_lockcnt_unlock(&xyz_lockcnt); + } + +QemuLockCnt can also be used to access a list as follows: + + qemu_lockcnt_inc(&io_handlers_lockcnt); + QLIST_FOREACH_RCU(ioh, &io_handlers, pioh) { + if (ioh->revents & G_IO_OUT) { + ioh->fd_write(ioh->opaque); + } + } + + if (qemu_lockcnt_dec_and_lock(&io_handlers_lockcnt)) { + QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { + if (ioh->deleted) { + QLIST_REMOVE(ioh, next); + g_free(ioh); + } + } + qemu_lockcnt_unlock(&io_handlers_lockcnt); + } + +Again, the RCU primitives are used because new items can be added to the +list during the walk. QLIST_FOREACH_RCU ensures that the processor and +the compiler see the appropriate memory barriers. + +An alternative pattern uses qemu_lockcnt_dec_if_lock: + + qemu_lockcnt_inc(&io_handlers_lockcnt); + QLIST_FOREACH_SAFE_RCU(ioh, &io_handlers, next, pioh) { + if (ioh->deleted) { + if (qemu_lockcnt_dec_if_lock(&io_handlers_lockcnt)) { + QLIST_REMOVE(ioh, next); + g_free(ioh); + qemu_lockcnt_inc_and_unlock(&io_handlers_lockcnt); + } + } else { + if (ioh->revents & G_IO_OUT) { + ioh->fd_write(ioh->opaque); + } + } + } + qemu_lockcnt_dec(&io_handlers_lockcnt); + +Here you can use qemu_lockcnt_dec instead of qemu_lockcnt_dec_and_lock, +because there is no special task to do if the count goes from 1 to 0.
include/qemu/thread.h +17 -0
@@ -8,6 +8,7 @@ typedef struct QemuMutex QemuMutex;typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ;typedef struct QemuEvent QemuEvent ; +typedef struct QemuLockCnt QemuLockCnt ;typedef struct QemuThread QemuThread ;#ifdef _WIN32
@@ -65,4 +66,20 @@ struct Notifier;void qemu_thread_atexit_add (struct Notifier *notifier);void qemu_thread_atexit_remove (struct Notifier *notifier); +struct QemuLockCnt { +QemuMutex mutex; +unsigned count; +}; + +void qemu_lockcnt_init (QemuLockCnt *lockcnt); +void qemu_lockcnt_destroy (QemuLockCnt *lockcnt); +void qemu_lockcnt_inc (QemuLockCnt *lockcnt); +void qemu_lockcnt_dec (QemuLockCnt *lockcnt); +bool qemu_lockcnt_dec_and_lock (QemuLockCnt *lockcnt); +bool qemu_lockcnt_dec_if_lock (QemuLockCnt *lockcnt); +void qemu_lockcnt_lock (QemuLockCnt *lockcnt); +void qemu_lockcnt_unlock (QemuLockCnt *lockcnt); +void qemu_lockcnt_inc_and_unlock (QemuLockCnt *lockcnt); +unsigned qemu_lockcnt_count (QemuLockCnt *lockcnt); +#endif
util/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o +util-obj-y += lockcnt.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util/lockcnt.c new file +122 -0
122 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 09/16] qemu-thread: introduce QemuLockCnt
Quoted 25 lines · Paolo Bonzini
A QemuLockCnt comprises a counter and a mutex, with primitives to increment and decrement the counter, and to take and release the mutex. It can be used to do lock-free visits to a data structure whenever mutexes would be too heavy-weight and the critical section is too long for RCU.
This could be implemented simply by protecting the counter with the mutex, but QemuLockCnt is harder to misuse and more efficient.
Signed-off-by Paolo Bonzini <[email protected]>--- docs/lockcnt.txt | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 17 +++ util/Makefile.objs | 1 + util/lockcnt.c | 122 ++++++++++++++++++ 4 files changed, 483 insertions(+) create mode 100644 docs/lockcnt.txt create mode 100644 util/lockcnt.cdocs/lockcnt.txt new file +0
-0
docs/lockcnt.txt line 2
Quoted 2 lines
docs/lockcnt.txt new file +2
-0@@ -0,0 +1,343 @@ +DOCUMENTATION FOR LOCKED COUNTERS (aka QemuLockCnt) +===================================================
Worth an explicit mention that this document is GPLv2+ (or an explicit choice of a different license)?
docs/lockcnt.txt line 5
Quoted 3 lines
docs/lockcnt.txt new file +3
-0+ +QEMU often uses reference counts to track data structures that are being +accessed and should not be freed. For example, a loop that invoke
s/invoke/invokes/
docs/lockcnt.txt line 7
Quoted 2 lines
docs/lockcnt.txt new file +2
-0+callbacks like this is not safe: +
but overall a nice writeup.
I'll leave the code review to others, though.
Quoted 10 lines
docs/lockcnt.txt new file +9
-0+++ b/util/lockcnt.c@@ -0,0 +1,122 @@ +/* + * QemuLockCnt implementation + * + * Copyright Red Hat, Inc. 2015 + * + * Author: + * Paolo Bonzini <[email protected]> + */
docs/lockcnt.txt line 18
Quoted 10 lines
docs/lockcnt.txt new file +10
-0+#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <time.h> +#include <signal.h> +#include <stdint.h> +#include <string.h> +#include <limits.h> +#include <unistd.h> +#include <sys/time.h>
You'll want to use qemu/osdep.h in place of several of these headers.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v2 10/16 aio: make ctx->list_lock a QemuLockCnt, subsuming ctx->walking_bh
In reply to Paolo BonziniThis will make it possible to walk the list of bottom halves without holding the AioContext lock---and in turn to call bottom half handlers without holding the lock.
--- async.c | 31 ++++++++++++++----------------- include/block/aio.h | 12 +++++------- 2 files changed, 19 insertions(+), 24 deletions(-)
async.c +14 -17
@@ -51,12 +51,12 @@ QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) .cb = cb, .opaque = opaque, }; -qemu_mutex_lock (&ctx->list_lock); +qemu_lockcnt_lock (&ctx->list_lock); bh->next = ctx->first_bh;/* Make sure that the members are ready before putting bh into list */ smp_wmb (); ctx->first_bh = bh; -qemu_mutex_unlock (&ctx->list_lock); +qemu_lockcnt_unlock (&ctx->list_lock);return bh; }
@@ -71,13 +71,11 @@ int aio_bh_poll(AioContext *ctx)QEMUBH *bh, **bhp, *next;int ret; - ctx->walking_bh++; +qemu_lockcnt_inc (&ctx->list_lock); ret =0 ; -for (bh = ctx->first_bh; bh; bh = next) { -/* Make sure that fetching bh happens before accessing its members */ -smp_read_barrier_depends (); - next = bh->next; +for (bh =atomic_rcu_read (&ctx->first_bh); bh; bh = next) { + next =atomic_rcu_read (&bh->next);/* The atomic_xchg is paired with the one in qemu_bh_schedule. The * implicit memory barrier ensures that the callback sees all writes * done by the scheduling thread. It also ensures that the scheduling
@@ -94,11 +92,8 @@ int aio_bh_poll(AioContext *ctx) } } - ctx->walking_bh--; -/* remove deleted bhs */ -if (!ctx->walking_bh) { -qemu_mutex_lock (&ctx->list_lock); +if (qemu_lockcnt_dec_and_lock (&ctx->list_lock)) { bhp = &ctx->first_bh;while (*bhp) { bh = *bhp;
@@ -109,7 +104,7 @@ int aio_bh_poll(AioContext *ctx) bhp = &bh->next; } } -qemu_mutex_unlock (&ctx->list_lock); +qemu_lockcnt_unlock (&ctx->list_lock); }return ret;
@@ -165,7 +160,8 @@ aio_compute_timeout(AioContext *ctx)int timeout =-1 ;QEMUBH *bh; -for (bh = ctx->first_bh; bh; bh = bh->next) { +for (bh =atomic_rcu_read (&ctx->first_bh); bh; + bh =atomic_rcu_read (&bh->next)) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least
@@ -240,7 +236,8 @@ aio_ctx_finalize(GSource *source)thread_pool_free (ctx->thread_pool); -qemu_mutex_lock (&ctx->list_lock); +qemu_lockcnt_lock (&ctx->list_lock); +assert (!qemu_lockcnt_count (&ctx->list_lock));while (ctx->first_bh) {QEMUBH *next = ctx->first_bh->next;
@@ -250,12 +247,12 @@ aio_ctx_finalize(GSource *source)g_free (ctx->first_bh); ctx->first_bh = next; } -qemu_mutex_unlock (&ctx->list_lock); +qemu_lockcnt_unlock (&ctx->list_lock);aio_set_event_notifier (ctx, &ctx->notifier, false,NULL );event_notifier_cleanup (&ctx->notifier);qemu_rec_mutex_destroy (&ctx->lock); -qemu_mutex_destroy (&ctx->list_lock); +qemu_lockcnt_destroy (&ctx->list_lock);timerlistgroup_deinit (&ctx->tlg); }
@@ -551,7 +548,7 @@ AioContext *aio_context_new(Error **errp) (EventNotifierHandler *) event_notifier_dummy_cb); ctx->thread_pool =NULL ; -qemu_mutex_init (&ctx->list_lock); +qemu_lockcnt_init (&ctx->list_lock);qemu_rec_mutex_init (&ctx->lock);timerlistgroup_init (&ctx->tlg, aio_timerlist_notify, ctx);
include/block/aio.h +5 -7
@@ -87,17 +87,15 @@ struct AioContext {*/ uint32_t notify_me; -/* lock to protect between bh's adders and deleter */ -QemuMutex list_lock; +/* A lock to protect between bh's adders and deleter, and to ensure +* that no callbacks are removed while we're walking and dispatching +* them. +*/ +QemuLockCnt list_lock;/* Anchor of the list of Bottom Halves belonging to the context */ struct QEMUBH *first_bh; -/* A simple lock used to protect the first_bh list, and ensure that -* no callbacks are removed while we're walking and dispatching callbacks. -*/ -int walking_bh; -/* Used by aio_notify. * * "notified" is used to avoid expensive event_notifier_test_and_clear
Signature · 1 line
2.5.0
PATCH v2 11/16 qemu-thread: optimize QemuLockCnt with futexes on Linux
In reply to Paolo BonziniThis is complex, but I think it is reasonably documented in the source.
--- docs/lockcnt.txt | 9 +- include/qemu/futex.h | 36 ++++++ include/qemu/thread.h | 3 + trace-events | 10 ++ util/lockcnt.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-posix.c | 25 +---- 6 files changed, 336 insertions(+), 29 deletions(-) create mode 100644 include/qemu/futex.h
docs/lockcnt.txt +4 -5
@@ -142,12 +142,11 @@ can also be more efficient in two ways: - it avoids taking the lock for many operations (for example incrementing the counter while it is non-zero); -- on some platforms, one could implement QemuLockCnt to hold the - lock and the mutex in a single word, making it no more expensive +- on some platforms, one can implement QemuLockCnt to hold the lock + and the mutex in a single word, making the fast path no more expensive than simply managing a counter using atomic operations (see - docs/atomics.txt). This is not implemented yet, but can be - very helpful if concurrent access to the data structure is - expected to be rare. + docs/atomics.txt). This can be very helpful if concurrent access to + the data structure is expected to be rare. Using the same mutex for frees and writes can still incur some small
include/qemu/futex.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Wrappers around Linux futex syscall +* +* Copyright Red Hat, Inc. 2015 +* +* Author: +* Paolo Bonzini <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include <sys/syscall.h> +#include <linux/futex.h> + +#define futex (...) syscall(__NR_futex, __VA_ARGS__) + +static inline void futex_wake (void *f,int n) +{ +futex (f,FUTEX_WAKE , n,NULL ,NULL ,0 ); +} + +static inline void futex_wait (void *f,unsigned val) +{ +while (futex (f,FUTEX_WAIT , (int ) val,NULL ,NULL ,0 )) { +switch (errno) { +case EWOULDBLOCK : +return ; +case EINTR : +break ;/* get out of switch and retry */ +default : +abort (); + } + } +}
include/qemu/thread.h +3 -0
@@ -1,6 +1,7 @@#ifndef __QEMU_THREAD_H#define __QEMU_THREAD_H 1 +#include "config-host.h" #include <inttypes.h> #include <stdbool.h>
@@ -67,7 +68,9 @@ void qemu_thread_atexit_add(struct Notifier *notifier);void qemu_thread_atexit_remove (struct Notifier *notifier);struct QemuLockCnt { +#ifndef CONFIG_LINUX QemuMutex mutex; +#endif unsigned count; };
trace-events +10 -0
@@ -1434,6 +1434,16 @@ hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long c hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64 hbitmap_set(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64 +# util/lockcnt.c +lockcnt_fast_path_attempt(const void *lockcnt, int expected, int new) "lockcnt %p fast path %d->%d" +lockcnt_fast_path_success(const void *lockcnt, int expected, int new) "lockcnt %p fast path %d->%d succeeded" +lockcnt_unlock_attempt(const void *lockcnt, int expected, int new) "lockcnt %p unlock %d->%d" +lockcnt_unlock_success(const void *lockcnt, int expected, int new) "lockcnt %p unlock %d->%d succeeded" +lockcnt_futex_wait_prepare(const void *lockcnt, int expected, int new) "lockcnt %p preparing slow path %d->%d" +lockcnt_futex_wait(const void *lockcnt, int val) "lockcnt %p waiting on %d" +lockcnt_futex_wait_resume(const void *lockcnt, int new) "lockcnt %p after wait: %d" +lockcnt_futex_wake(const void *lockcnt) "lockcnt %p waking up one waiter" + # target-s390x/mmu_helper.c get_skeys_nonzero(int rc) "SKEY: Call to get_skeys unexpectedly returned %d" set_skeys_nonzero(int rc) "SKEY: Call to set_skeys unexpectedly returned %d"
util/lockcnt.c +282 -0
292 lines not on this page · raw message
util/qemu-thread-posix.c +1 -24
@@ -11,10 +11,6 @@* */ #include "qemu/osdep.h" -#ifdef __linux__ -#include <sys/syscall.h> -#include <linux/futex.h> -#endif #include "qemu/thread.h" #include "qemu/atomic.h" #include "qemu/notify.h"
@@ -293,26 +289,7 @@ void qemu_sem_wait(QemuSemaphore *sem) }#ifdef __linux__ -#define futex (...) syscall(__NR_futex, __VA_ARGS__) - -static inline void futex_wake (QemuEvent *ev,int n) -{ -futex (ev,FUTEX_WAKE , n,NULL ,NULL ,0 ); -} - -static inline void futex_wait (QemuEvent *ev,unsigned val) -{ -while (futex (ev,FUTEX_WAIT , (int ) val,NULL ,NULL ,0 )) { -switch (errno) { -case EWOULDBLOCK : -return ; -case EINTR : -break ;/* get out of switch and retry */ -default : -abort (); - } - } -} +#include "qemu/futex.h" #else static inline void futex_wake (QemuEvent *ev,int n) {
Signature · 1 line
2.5.0
PATCH v2 12/16 aio: tweak walking in dispatch phase
In reply to Paolo BonziniPreparing for the following patch, use QLIST_FOREACH_SAFE and modify the placement of walking_handlers increment/decrement.
--- aio-posix.c | 27 +++++++++++++-------------- aio-win32.c | 26 ++++++++++++-------------- 2 files changed, 25 insertions(+), 28 deletions(-)
aio-posix.c +13 -14
@@ -295,7 +295,7 @@ bool aio_pending(AioContext *ctx)bool aio_dispatch (AioContext *ctx) { -AioHandler *node; +AioHandler *node, *tmp;bool progress = false;/*
@@ -311,12 +311,10 @@ bool aio_dispatch(AioContext *ctx)* We have to walk very carefully in case aio_set_fd_handler is * called while we're walking. */ - node =QLIST_FIRST (&ctx->aio_handlers); -while (node) { -AioHandler *tmp; -int revents; + ctx->walking_handlers++; - ctx->walking_handlers++; + QLIST_FOREACH_SAFE(node, &ctx->aio_handlers, node, tmp) { +int revents; revents = node->pfd.revents & node->pfd.events; node->pfd.revents =0 ;
@@ -338,17 +336,18 @@ bool aio_dispatch(AioContext *ctx) progress = true; } - tmp = node; - node =QLIST_NEXT (node, node); - - ctx->walking_handlers--; - -if (!ctx->walking_handlers && tmp->deleted) { -QLIST_REMOVE (tmp, node); -g_free (tmp); +if (node->deleted) { + ctx->walking_handlers--; +if (!ctx->walking_handlers) { +QLIST_REMOVE (node, node); +g_free (node); + } + ctx->walking_handlers++; } } + ctx->walking_handlers--; +/* Run our timers */ progress |=timerlistgroup_run_timers (&ctx->tlg);
aio-win32.c +12 -14
@@ -209,20 +209,18 @@ bool aio_pending(AioContext *ctx)static bool aio_dispatch_handlers (AioContext *ctx,HANDLE event) { -AioHandler *node; +AioHandler *node, *tmp;bool progress = false; + ctx->walking_handlers++; +/* * We have to walk very carefully in case aio_set_fd_handler is * called while we're walking. */ - node =QLIST_FIRST (&ctx->aio_handlers); -while (node) { -AioHandler *tmp; + QLIST_FOREACH_SAFE(node, &ctx->aio_handlers, node, tmp) {int revents = node->pfd.revents; - ctx->walking_handlers++; -if (!node->deleted && (revents ||event_notifier_get_handle (node->e) == event) && node->io_notify) {
@@ -257,17 +255,17 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event) } } - tmp = node; - node =QLIST_NEXT (node, node); - - ctx->walking_handlers--; - -if (!ctx->walking_handlers && tmp->deleted) { -QLIST_REMOVE (tmp, node); -g_free (tmp); +if (node->deleted) { + ctx->walking_handlers--; +if (!ctx->walking_handlers) { +QLIST_REMOVE (node, node); +g_free (node); + } + ctx->walking_handlers++; } } + ctx->walking_handlers--;return progress; }
Signature · 1 line
2.5.0
PATCH v2 13/16 aio-posix: remove walking_handlers, protecting AioHandler list with list_lock
In reply to Paolo Bonzini--- aio-posix.c | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-)
aio-posix.c +32 -22
@@ -16,7 +16,7 @@#include "qemu/osdep.h" #include "qemu-common.h" #include "block/block.h" -#include "qemu/queue.h" +#include "qemu/rcu_queue.h" #include "qemu/sockets.h" #ifdef CONFIG_EPOLL #include <sys/epoll.h>
@@ -213,6 +213,8 @@ void aio_set_fd_handler(AioContext *ctx,bool is_new = false;bool deleted = false; +qemu_lockcnt_lock (&ctx->list_lock); + node =find_aio_handler (ctx, fd);/* Are we deleting the fd handler? */
@@ -220,14 +222,14 @@ void aio_set_fd_handler(AioContext *ctx,if (node) {g_source_remove_poll (&ctx->source, &node->pfd); -/* If the lock is held, just mark the node as deleted */ -if (ctx->walking_handlers) { +/* If aio_poll is in progress, just mark the node as deleted */ +if (qemu_lockcnt_count (&ctx->list_lock)) { node->deleted =1 ; node->pfd.revents =0 ; }else {/* Otherwise, delete it for real. We can't just mark it as * deleted because deleted nodes are only cleaned up after -* releasing the walking_handlers lock. +* releasing the list_lock. */ QLIST_REMOVE (node, node); deleted = true;
@@ -238,7 +240,7 @@ void aio_set_fd_handler(AioContext *ctx,/* Alloc and insert if it's not already there */ node =g_new0 (AioHandler,1 ); node->pfd.fd = fd; -QLIST_INSERT_HEAD (&ctx->aio_handlers, node, node); +QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, node, node);g_source_add_poll (&ctx->source, &node->pfd); is_new = true;
@@ -254,6 +256,7 @@ void aio_set_fd_handler(AioContext *ctx, }aio_epoll_update (ctx, node, is_new); +qemu_lockcnt_unlock (&ctx->list_lock);aio_notify (ctx);if (deleted) {g_free (node);
@@ -277,20 +280,30 @@ bool aio_prepare(AioContext *ctx)bool aio_pending (AioContext *ctx) {AioHandler *node; +bool result = false; - QLIST_FOREACH(node, &ctx->aio_handlers, node) { +/* +* We have to walk very carefully in case aio_set_fd_handler is +* called while we're walking. +*/ +qemu_lockcnt_inc (&ctx->list_lock); + + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {int revents; revents = node->pfd.revents & node->pfd.events;if (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR ) && node->io_read) { -return true; + result = true; +break ; }if (revents & (G_IO_OUT |G_IO_ERR ) && node->io_write) { -return true; + result = true; +break ; } } +qemu_lockcnt_dec (&ctx->list_lock); -return false; +return result; }bool aio_dispatch (AioContext *ctx)
@@ -311,13 +324,12 @@ bool aio_dispatch(AioContext *ctx)* We have to walk very carefully in case aio_set_fd_handler is * called while we're walking. */ - ctx->walking_handlers++; +qemu_lockcnt_inc (&ctx->list_lock); - QLIST_FOREACH_SAFE(node, &ctx->aio_handlers, node, tmp) { + QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {int revents; - revents = node->pfd.revents & node->pfd.events; - node->pfd.revents =0 ; + revents =atomic_xchg (&node->pfd.revents,0 ) & node->pfd.events; if (!node->deleted && (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
@@ -337,16 +349,15 @@ bool aio_dispatch(AioContext *ctx) }if (node->deleted) { - ctx->walking_handlers--; -if (!ctx->walking_handlers) { +if (qemu_lockcnt_dec_if_lock (&ctx->list_lock)) {QLIST_REMOVE (node, node);g_free (node); +qemu_lockcnt_inc_and_unlock (&ctx->list_lock); } - ctx->walking_handlers++; } } - ctx->walking_handlers--; +qemu_lockcnt_dec (&ctx->list_lock);/* Run our timers */ progress |=timerlistgroup_run_timers (&ctx->tlg);
@@ -421,12 +432,11 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)atomic_add (&ctx->notify_me,2 ); } - ctx->walking_handlers++; - +qemu_lockcnt_inc (&ctx->list_lock);assert (npfd ==0 );/* fill pollfds */ - QLIST_FOREACH(node, &ctx->aio_handlers, node) { + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {if (!node->deleted && node->pfd.events && !aio_epoll_enabled (ctx) &&aio_node_check (ctx, node->is_external)) {
@@ -463,12 +473,12 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)/* if we have any readable fds, dispatch event */ if (ret >0 ) {for (i =0 ; i < npfd; i++) { - nodes[i]->pfd.revents = pollfds[i].revents; +atomic_or (&nodes[i]->pfd.revents, pollfds[i].revents); } } npfd =0 ; - ctx->walking_handlers--; +qemu_lockcnt_dec (&ctx->list_lock);/* Run dispatch even if there were no readable fds to run timers */ if (aio_dispatch (ctx)) {
Signature · 1 line
2.5.0
PATCH v2 14/16 aio-win32: remove walking_handlers, protecting AioHandler list with list_lock
In reply to Paolo Bonzini--- aio-win32.c | 81 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 32 deletions(-)
aio-win32.c +49 -32
@@ -43,6 +43,7 @@ void aio_set_fd_handler(AioContext *ctx,/* fd is a SOCKET in our case */ AioHandler *node; +qemu_lockcnt_lock (&ctx->list_lock); QLIST_FOREACH(node, &ctx->aio_handlers, node) {if (node->pfd.fd == fd && !node->deleted) {break ;
@@ -52,14 +53,14 @@ void aio_set_fd_handler(AioContext *ctx,/* Are we deleting the fd handler? */ if (!io_read && !io_write) {if (node) { -/* If the lock is held, just mark the node as deleted */ -if (ctx->walking_handlers) { +/* If aio_poll is in progress, just mark the node as deleted */ +if (qemu_lockcnt_count (&ctx->list_lock)) { node->deleted =1 ; node->pfd.revents =0 ; }else {/* Otherwise, delete it for real. We can't just mark it as * deleted because deleted nodes are only cleaned up after -* releasing the walking_handlers lock. +* releasing the list_lock. */ QLIST_REMOVE (node, node);g_free (node);
@@ -72,7 +73,7 @@ void aio_set_fd_handler(AioContext *ctx,/* Alloc and insert if it's not already there */ node =g_new0 (AioHandler,1 ); node->pfd.fd = fd; -QLIST_INSERT_HEAD (&ctx->aio_handlers, node, node); +QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, node, node); } node->pfd.events =0 ;
@@ -97,6 +98,7 @@ void aio_set_fd_handler(AioContext *ctx, FD_CONNECT | FD_WRITE | FD_OOB); } +qemu_lockcnt_unlock (&ctx->list_lock);aio_notify (ctx); }
@@ -107,6 +109,7 @@ void aio_set_event_notifier(AioContext *ctx, {AioHandler *node; +qemu_lockcnt_lock (&ctx->list_lock); QLIST_FOREACH(node, &ctx->aio_handlers, node) {if (node->e == e && !node->deleted) {break ;
@@ -118,14 +121,14 @@ void aio_set_event_notifier(AioContext *ctx,if (node) {g_source_remove_poll (&ctx->source, &node->pfd); -/* If the lock is held, just mark the node as deleted */ -if (ctx->walking_handlers) { +/* aio_poll is in progress, just mark the node as deleted */ +if (qemu_lockcnt_count (&ctx->list_lock)) { node->deleted =1 ; node->pfd.revents =0 ; }else {/* Otherwise, delete it for real. We can't just mark it as * deleted because deleted nodes are only cleaned up after -* releasing the walking_handlers lock. +* releasing the list_lock. */ QLIST_REMOVE (node, node);g_free (node);
@@ -139,7 +142,7 @@ void aio_set_event_notifier(AioContext *ctx, node->pfd.fd = (uintptr_t )event_notifier_get_handle (e); node->pfd.events =G_IO_IN ; node->is_external = is_external; -QLIST_INSERT_HEAD (&ctx->aio_handlers, node, node); +QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, node, node);g_source_add_poll (&ctx->source, &node->pfd); }
@@ -147,6 +150,7 @@ void aio_set_event_notifier(AioContext *ctx, node->io_notify = io_notify; } +qemu_lockcnt_unlock (&ctx->list_lock);aio_notify (ctx); }
@@ -157,10 +161,16 @@ bool aio_prepare(AioContext *ctx)bool have_select_revents = false;fd_set rfds, wfds; +/* +* We have to walk very carefully in case aio_set_fd_handler is +* called while we're walking. +*/ +qemu_lockcnt_inc (&ctx->list_lock); +/* fill fd sets */ FD_ZERO (&rfds);FD_ZERO (&wfds); - QLIST_FOREACH(node, &ctx->aio_handlers, node) { + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {if (node->io_read) {FD_SET ((SOCKET )node->pfd.fd, &rfds); }
@@ -170,61 +180,71 @@ bool aio_prepare(AioContext *ctx) }if (select (0 , &rfds, &wfds,NULL , &tv0) >0 ) { - QLIST_FOREACH(node, &ctx->aio_handlers, node) { - node->pfd.revents =0 ; + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {if (FD_ISSET (node->pfd.fd, &rfds)) { - node->pfd.revents |=G_IO_IN ; +atomic_or (&node->pfd.revents,G_IO_IN ); have_select_revents = true; }if (FD_ISSET (node->pfd.fd, &wfds)) { - node->pfd.revents |=G_IO_OUT ; +atomic_or (&node->pfd.revents,G_IO_OUT ); have_select_revents = true; } } } +qemu_lockcnt_dec (&ctx->list_lock);return have_select_revents; }bool aio_pending (AioContext *ctx) {AioHandler *node; +bool result = false; - QLIST_FOREACH(node, &ctx->aio_handlers, node) { +/* +* We have to walk very carefully in case aio_set_fd_handler is +* called while we're walking. +*/ +qemu_lockcnt_inc (&ctx->list_lock); + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {if (node->pfd.revents && node->io_notify) { -return true; + result = true; +break ; }if ((node->pfd.revents &G_IO_IN ) && node->io_read) { -return true; + result = true; +break ; }if ((node->pfd.revents &G_IO_OUT ) && node->io_write) { -return true; + result = true; +break ; } } -return false; +qemu_lockcnt_dec (&ctx->list_lock); +return result; }static bool aio_dispatch_handlers (AioContext *ctx,HANDLE event) { -AioHandler *node, *tmp; +AioHandler *node;bool progress = false; - ctx->walking_handlers++; +qemu_lockcnt_inc (&ctx->list_lock);/* * We have to walk very carefully in case aio_set_fd_handler is * called while we're walking. */ - QLIST_FOREACH_SAFE(node, &ctx->aio_handlers, node, tmp) { -int revents = node->pfd.revents; + QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { +AioHandler *tmp; +int revents =atomic_xchg (&node->pfd.revents,0 );if (!node->deleted && (revents ||event_notifier_get_handle (node->e) == event) && node->io_notify) { - node->pfd.revents =0 ; node->io_notify (node->e);/* aio_notify() does not count as progress */
@@ -235,7 +255,6 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)if (!node->deleted && (node->io_read || node->io_write)) { - node->pfd.revents =0 ;if ((revents &G_IO_IN ) && node->io_read) { node->io_read (node->opaque); progress = true;
@@ -256,16 +275,15 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event) }if (node->deleted) { - ctx->walking_handlers--; -if (!ctx->walking_handlers) { +if (qemu_lockcnt_dec_if_lock (&ctx->list_lock)) {QLIST_REMOVE (node, node);g_free (node); +qemu_lockcnt_inc_and_unlock (&ctx->list_lock); } - ctx->walking_handlers++; } } - ctx->walking_handlers--; +qemu_lockcnt_dec (&ctx->list_lock);return progress; }
@@ -301,20 +319,19 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)atomic_add (&ctx->notify_me,2 ); } +qemu_lockcnt_inc (&ctx->list_lock); have_select_revents =aio_prepare (ctx); - ctx->walking_handlers++; -/* fill fd sets */ count =0 ; - QLIST_FOREACH(node, &ctx->aio_handlers, node) { + QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {if (!node->deleted && node->io_notify &&aio_node_check (ctx, node->is_external)) { events[count++] =event_notifier_get_handle (node->e); } } - ctx->walking_handlers--; +qemu_lockcnt_dec (&ctx->list_lock); first = true;/* ctx->notifier is always registered. */
Signature · 1 line
2.5.0
PATCH v2 15/16 aio: document locking
In reply to Paolo Bonzini--- docs/multiple-iothreads.txt | 5 ++--- include/block/aio.h | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 19 deletions(-)
docs/multiple-iothreads.txt +2 -3
@@ -84,9 +84,8 @@ How to synchronize with an IOThread AioContext is not thread-safe so some rules must be followed when using file descriptors, event notifiers, timers, or BHs across threads: -1. AioContext functions can be called safely from file descriptor, event -notifier, timer, or BH callbacks invoked by the AioContext. No locking is -necessary. +1. AioContext functions can always be called safely. They handle their +own locking internally. 2. Other threads wishing to access the AioContext must take the QEMU global mutex *as well as* call aio_context_acquire()/aio_context_release() for
include/block/aio.h +16 -16
@@ -50,18 +50,12 @@ typedef void IOHandler(void *opaque);struct AioContext {GSource source; -/* Protects all fields from multi-threaded access */ +/* Used by AioContext users to protect from multi-threaded access. */ QemuRecMutex lock; -/* The list of registered AIO handlers */ +/* The list of registered AIO handlers. Protected by ctx->list_lock. */ QLIST_HEAD(, AioHandler) aio_handlers; -/* This is a simple lock used to protect the aio_handlers list. -* Specifically, it's used to ensure that no callbacks are removed while -* we're walking and dispatching callbacks. -*/ -int walking_handlers; -/* Used to avoid unnecessary event_notifier_set calls in aio_notify; * accessed with atomic primitives. If this field is 0, everything * (file descriptors, bottom halves, timers) will be re-evaluated
@@ -87,9 +81,9 @@ struct AioContext {*/ uint32_t notify_me; -/* A lock to protect between bh's adders and deleter, and to ensure -* that no callbacks are removed while we're walking and dispatching -* them. +/* A lock to protect between QEMUBH and AioHandler adders and deleter, +* and to ensure that no callbacks are removed while we're walking and +* dispatching them. */ QemuLockCnt list_lock;
@@ -111,10 +105,14 @@ struct AioContext {bool notified;EventNotifier notifier; -/* Thread pool for performing work and receiving completion callbacks */ +/* Thread pool for performing work and receiving completion callbacks. +* Has its own locking. +*/ struct ThreadPool *thread_pool; -/* TimerLists for calling timers - one per clock type */ +/* TimerLists for calling timers - one per clock type. Has its own +* locking. +*/ QEMUTimerListGroup tlg;int external_disable_cnt;
@@ -162,9 +160,11 @@ void aio_context_unref(AioContext *ctx);* automatically takes care of calling aio_context_acquire and * aio_context_release. * -* Access to timers and BHs from a thread that has not acquired AioContext -* is possible. Access to callbacks for now must be done while the AioContext -* is owned by the thread (FIXME). +* Note that this is separate from bdrv_drained_begin/bdrv_drained_end. A +* thread still has to call those to avoid being interrupted by the guest. +* +* Bottom halves, timers and callbacks can be created or removed without +* acquiring the AioContext. */ void aio_context_acquire (AioContext *ctx);
Signature · 1 line
2.5.0
PATCH v2 16/16 aio: push aio_context_acquire/release down to dispatching
In reply to Paolo BonziniThe AioContext data structures are now protected by list_lock and/or they are walked with FOREACH_RCU primitives. There is no need anymore to acquire the AioContext for the entire duration of aio_dispatch. Instead, just acquire it before and after invoking the callbacks. The next step is then to push it further down.
--- aio-posix.c | 15 ++++++--------- aio-win32.c | 15 +++++++-------- async.c | 2 ++ 3 files changed, 15 insertions(+), 17 deletions(-)
aio-posix.c +6 -9
@@ -334,7 +334,9 @@ bool aio_dispatch(AioContext *ctx)if (!node->deleted && (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR )) && node->io_read) { +aio_context_acquire (ctx); node->io_read (node->opaque); +aio_context_release (ctx);/* aio_notify() does not count as progress */ if (node->opaque != &ctx->notifier) {
@@ -344,7 +346,9 @@ bool aio_dispatch(AioContext *ctx)if (!node->deleted && (revents & (G_IO_OUT |G_IO_ERR )) && node->io_write) { +aio_context_acquire (ctx); node->io_write (node->opaque); +aio_context_release (ctx); progress = true; }
@@ -360,7 +364,9 @@ bool aio_dispatch(AioContext *ctx)qemu_lockcnt_dec (&ctx->list_lock);/* Run our timers */ +aio_context_acquire (ctx); progress |=timerlistgroup_run_timers (&ctx->tlg); +aio_context_release (ctx);return progress; }
@@ -418,7 +424,6 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)bool progress;int64_t timeout; -aio_context_acquire (ctx); progress = false;/* aio_notify can avoid the expensive event_notifier_set if
@@ -447,9 +452,6 @@ bool aio_poll_internal(AioContext *ctx, bool blocking) timeout = blocking ?aio_compute_timeout (ctx) :0 ;/* wait until next event */ -if (timeout) { -aio_context_release (ctx); - }if (aio_epoll_check_poll (ctx, pollfds, npfd, timeout)) {AioHandler epoll_handler;
@@ -464,9 +466,6 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)if (blocking) {atomic_sub (&ctx->notify_me,2 ); } -if (timeout) { -aio_context_acquire (ctx); - }aio_notify_accept (ctx);
@@ -485,8 +484,6 @@ bool aio_poll_internal(AioContext *ctx, bool blocking) progress = true; } -aio_context_release (ctx); -return progress; }
aio-win32.c +7 -8
@@ -245,7 +245,9 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)if (!node->deleted && (revents ||event_notifier_get_handle (node->e) == event) && node->io_notify) { +aio_context_acquire (ctx); node->io_notify (node->e); +aio_context_release (ctx);/* aio_notify() does not count as progress */ if (node->e != &ctx->notifier) {
@@ -256,11 +258,15 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)if (!node->deleted && (node->io_read || node->io_write)) {if ((revents &G_IO_IN ) && node->io_read) { +aio_context_acquire (ctx); node->io_read (node->opaque); +aio_context_release (ctx); progress = true; }if ((revents &G_IO_OUT ) && node->io_write) { +aio_context_acquire (ctx); node->io_write (node->opaque); +aio_context_release (ctx); progress = true; }
@@ -305,7 +311,6 @@ bool aio_poll_internal(AioContext *ctx, bool blocking)int count;int timeout; -aio_context_acquire (ctx); progress = false;/* aio_notify can avoid the expensive event_notifier_set if
@@ -347,17 +352,11 @@ bool aio_poll_internal(AioContext *ctx, bool blocking) timeout = blocking && !have_select_revents ?qemu_timeout_ns_to_ms (aio_compute_timeout (ctx)) :0 ; -if (timeout) { -aio_context_release (ctx); - } ret =WaitForMultipleObjects (count, events, FALSE, timeout);if (blocking) {assert (first);atomic_sub (&ctx->notify_me,2 ); } -if (timeout) { -aio_context_acquire (ctx); - }if (first) {aio_notify_accept (ctx);
@@ -380,8 +379,8 @@ bool aio_poll_internal(AioContext *ctx, bool blocking) progress |=aio_dispatch_handlers (ctx, event); }while (count >0 ); +aio_context_acquire (ctx); progress |=timerlistgroup_run_timers (&ctx->tlg); -aio_context_release (ctx);return progress; }
async.c +2 -0
@@ -88,7 +88,9 @@ int aio_bh_poll(AioContext *ctx) ret =1 ; } bh->idle =0 ; +aio_context_acquire (ctx);aio_bh_call (bh); +aio_context_release (ctx); } }
Signature · 1 line
2.5.0