AioContext: fix broken placement of event_notifier_test_and_clear
PATCH v2 1/1 AioContext: fix broken placement of event_notifier_test_and_clear
event_notifier_test_and_clear must be called before processing events. Otherwise, an aio_poll could "eat" the notification before the main I/O thread invokes ppoll(). The main I/O thread then never wakes up. This is an example of what could happen:
i/o thread vcpu thread worker thread
---------------------------------------------------------------------
lock_iothread
notify_me = 1
...
unlock_iothread
lock_iothread
notify_me = 3
ppoll
notify_me = 1
bh->scheduled = 1 event_notifier_set
event_notifier_test_and_clear
ppoll
*** hang ***
In contrast with the previous bug, there wasn't really much debugging to do here. "Tracing" with qemu_clock_get_ns shows pretty much the same behavior as in the previous patch, so the only wait to find the bug is staring at the code.
One could also use a formal model, of course. The included one shows this with three processes: notifier corresponds to a QEMU thread pool worker, temporary_waiter to a VCPU thread that invokes aio_poll(), waiter to the main I/O thread. I would be happy to say that the formal model found the bug for me, but actually I wrote it after the fact.
This patch is a bit of a big hammer. Fam is looking into optimizations.
---
v1->v2: only do event_notifier_test_and_clear once for Win32
aio-posix.c | 2 + aio-win32.c | 7 ++- async.c | 8 ++- docs/aio_notify_bug.promela | 140 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 docs/aio_notify_bug.promela
aio-posix.c +2 -0
@@ -276,6 +276,8 @@ bool aio_poll(AioContext *ctx, bool blocking)aio_context_acquire (ctx); } +event_notifier_test_and_clear (&ctx->notifier); +/* if we have any readable fds, dispatch event */ if (ret >0 ) {for (i =0 ; i < npfd; i++) {
aio-win32.c +4 -3
@@ -337,10 +337,11 @@ bool aio_poll(AioContext *ctx, bool blocking)aio_context_acquire (ctx); } -if (first &&aio_bh_poll (ctx)) { - progress = true; +if (first) { +event_notifier_test_and_clear (&ctx->notifier); + progress |=aio_bh_poll (ctx); + first = false; } - first = false;/* if we have any signaled events, dispatch event */ event =NULL ;
async.c +7 -1
@@ -203,6 +203,8 @@ aio_ctx_check(GSource *source)QEMUBH *bh;atomic_and (&ctx->notify_me, ~1 ); +event_notifier_test_and_clear (&ctx->notifier); +for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {return true;
@@ -279,6 +281,10 @@ static void aio_rfifolock_cb(void *opaque)aio_notify (opaque); } +static void event_notifier_dummy_cb (EventNotifier *e) +{ +} +AioContext *aio_context_new (Error **errp) {int ret;
@@ -293,7 +299,7 @@ AioContext *aio_context_new(Error **errp)g_source_set_can_recurse (&ctx->source, true);aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) - event_notifier_test_and_clear); + event_notifier_dummy_cb); ctx->thread_pool =NULL ;qemu_mutex_init (&ctx->bh_lock);rfifolock_init (&ctx->lock, aio_rfifolock_cb, ctx);
docs/aio_notify_bug.promela new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.4.3
aio-win32.c bool aio_poll(AioContext *ctx, bool blocking) · line 341
Quoted 12 lines · Paolo Bonzini
aio-win32.c +2
-2@@ -337,10 +337,11 @@ bool aio_poll(AioContext *ctx, bool blocking)aio_context_acquire (ctx); } -if (first &&aio_bh_poll (ctx)) { - progress = true; +if (first) { +event_notifier_test_and_clear (&ctx->notifier);
I'm looking at optimizing it but I don't fully understand the relationship between aio_prepare and WaitForMultipleObjects. Do they get the same set of events? What if a new event comes in between, for example, thread worker calls aio_notify()?
Fam
Quoted 7 lines
aio-win32.c +2
-1+ progress |=aio_bh_poll (ctx); + first = false; } - first = false;/* if we have any signaled events, dispatch event */ event =NULL ;
Quoted 9 lines · Fam Zheng
Quoted 8 lines · Paolo Bonzini
aio-win32.c +0
-0@@ -337,10 +337,11 @@ bool aio_poll(AioContext *ctx, bool blocking)aio_context_acquire (ctx); }
Quoted 10 lines
aio-win32.c bool aio_poll(AioContext *ctx, bool blocking) · line 341
Quoted 4 lines
aio-win32.c +2
-2-if (first &&aio_bh_poll (ctx)) { - progress = true; +if (first) { +event_notifier_test_and_clear (&ctx->notifier);I'm looking at optimizing it but I don't fully understand the relationship between
aio_prepareand WaitForMultipleObjects. Do they get the same set of events? What if a new event comes in between, for example, thread worker callsaio_notify()?
After some reading I think WaitForMultipleObjects is for event notifiers and aio_prepare is for select() on fd events.
It's a bit trickier than aio-posix, in the first iteration there could be another event masking ctx->notifier so we don't know if we need to clear it. But since MSDN says:
"""
... the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects.
"""
Maybe we can reverse events[] so that ctx->notifier will be the 0th one. And I think we can always remove it after first iteration, am I right?
Fam
[Qemu-devel] R: Re: [PATCH v2] AioContext: fix broken placement of event_notifier_test_and_clear
Quoted 11 lines
Quoted 4 lines
I'm looking at optimizing it but I don't fully understand the relationship between
aio_prepareand WaitForMultipleObjects. Do they get the same set of events?After some reading I think WaitForMultipleObjects is for event notifiers and
aio_prepareis forselect()on fd events.
It's a bit trickier than aio-posix, in the first iteration there could be another event maskingctx->notifierso we don't know if we need to clear it. Maybe we can reverse events[] so thatctx->notifierwill be the 0th one. And I think we can always remove it after first iteration, am I right?
Yes, that would work. I am not sure how complex it would be. You would also need a solution for the GSource and one (probably similar to aio-posix) for your epoll implementation. With ctx->notified at least you can encapsulate it in aio_notify_accept...
Stefan, any preferences?
Paolo
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines
Quoted 4 lines
I'm looking at optimizing it but I don't fully understand the relationship between
aio_prepareand WaitForMultipleObjects. Do they get the same set of events?After some reading I think WaitForMultipleObjects is for event notifiers and
aio_prepareis forselect()on fd events.
It's a bit trickier than aio-posix, in the first iteration there could be another event maskingctx->notifierso we don't know if we need to clear it. Maybe we can reverse events[] so thatctx->notifierwill be the 0th one. And I think we can always remove it after first iteration, am I right?
Quoted 4 lines
Yes, that would work. I am not sure how complex it would be. You would also need a solution for the GSource and one (probably similar to aio-posix) for your epoll implementation. With
ctx->notifiedat least you can encapsulate it inaio_notify_accept...
Yes, I'll take this into consideration when I refactor for epoll implementation. For now I think aio_notify_accept is cleaner.
Fam
Quoted 7 lines · Fam Zheng
Quoted 4 lines
Yes, that would work. I am not sure how complex it would be. You would also need a solution for the GSource and one (probably similar to aio-posix) for your epoll implementation. With
ctx->notifiedat least you can encapsulate it inaio_notify_accept...Yes, I'll take this into consideration when I refactor for epoll implementation. For now I think
aio_notify_acceptis cleaner.
Ok, I'll wait for Stefan to pick up the unoptimized version, and then send v2 of aio_notify_accept.
Paolo
Quoted 5 lines · Paolo Bonzini
event_notifier_test_and_clearmust be called before processing events. Otherwise, anaio_pollcould "eat" the notification before the main I/O thread invokesppoll(). The main I/O thread then never wakes up. This is an example of what could happen:
Quoted 15 lines
i/o thread vcpu thread worker thread
---------------------------------------------------------------------
lock_iothread
notify_me= 1
...
unlock_iothread
lock_iothread
notify_me= 3
ppoll
notify_me= 1
bh->scheduled= 1event_notifier_set
event_notifier_test_and_clear
ppoll
*** hang ***
I don't understand this diagram. Why is event_notifier_test_and_clear() called by the vcpu thread? event_notifier_set() was called *after* vcpu thread's ppoll() returned so I wouldn't expect the vcpu thread to see the notify.
Was there a previous notify pending?
Quoted 16 lines · Stefan Hajnoczi
Quoted 16 lines
Quoted 16 lines
i/o thread vcpu thread worker thread
---------------------------------------------------------------------
lock_iothread
notify_me= 1
...
unlock_iothread
lock_iothread
notify_me= 3
ppoll
notify_me= 1
bh->scheduled= 1event_notifier_set
event_notifier_test_and_clear
ppoll
*** hang ***
Quoted 6 lines
I don't understand this diagram. Why is
event_notifier_test_and_clear()called by the vcpu thread?event_notifier_set()was called *after* vcpu thread'sppoll()returned so I wouldn't expect the vcpu thread to see the notify.Was there a previous notify pending?
Yes, for example a bottom half could have rescheduled itself.
Paolo
Stefan Hajnoczi
AppliedQuoted 38 lines · Paolo Bonzini
event_notifier_test_and_clearmust be called before processing events. Otherwise, anaio_pollcould "eat" the notification before the main I/O thread invokesppoll(). The main I/O thread then never wakes up. This is an example of what could happen:i/o thread vcpu thread worker thread
---------------------------------------------------------------------
lock_iothread
notify_me= 1
...
unlock_iothread
lock_iothread
notify_me= 3
ppoll
notify_me= 1
bh->scheduled= 1event_notifier_set
event_notifier_test_and_clear
ppoll
*** hang ***In contrast with the previous bug, there wasn't really much debugging to do here. "Tracing" with
qemu_clock_get_nsshows pretty much the same behavior as in the previous patch, so the only wait to find the bug is staring at the code.One could also use a formal model, of course. The included one shows this with three processes: notifier corresponds to a QEMU thread pool worker,
temporary_waiterto a VCPU thread that invokesaio_poll(), waiter to the main I/O thread. I would be happy to say that the formal model found the bug for me, but actually I wrote it after the fact.This patch is a bit of a big hammer. Fam is looking into optimizations.
Reported-by Richard W. M. Jones <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
Quoted 8 lines
v1->v2: only do event_notifier_test_and_clear once for Win32aio-posix.c | 2 + aio-win32.c | 7 ++- async.c | 8 ++- docs/aio_notify_bug.promela | 140 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 docs/aio_notify_bug.promela
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan