From: Stefan Hajnoczi <stefanha@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Stefan Hajnoczi <stefanha@gmail.com>, Fam Zheng <famz@redhat.com>,
qemu-devel <qemu-devel@nongnu.org>, Kevin Wolf <kwolf@redhat.com>,
qemu block <qemu-block@nongnu.org>, Max Reitz <mreitz@redhat.com>
Subject: Re: [Qemu-devel] [Qemu-block] [PATCH for-2.9-rc5 v2] block: Drain BH in bdrv_drained_begin
Date: Sun, 16 Apr 2017 10:37:43 +0100 [thread overview]
Message-ID: <20170416093743.GA24137@stefanha-x1.localdomain> (raw)
In-Reply-To: <80449391-88a6-d517-0682-de555fe4c169@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 2844 bytes --]
On Sat, Apr 15, 2017 at 01:10:02AM +0800, Paolo Bonzini wrote:
>
>
> On 14/04/2017 16:51, Stefan Hajnoczi wrote:
> > On Fri, Apr 14, 2017 at 9:02 AM, Fam Zheng <famz@redhat.com> wrote:
> >> @@ -398,11 +399,15 @@ void bdrv_drain_all(void);
> >> */ \
> >> assert(!bs_->wakeup); \
> >> bs_->wakeup = true; \
> >> - while ((cond)) { \
> >> - aio_context_release(ctx_); \
> >> - aio_poll(qemu_get_aio_context(), true); \
> >> - aio_context_acquire(ctx_); \
> >> - waited_ = true; \
> >> + while (busy_) { \
> >> + if ((cond)) { \
> >> + waited_ = busy_ = true; \
> >> + aio_context_release(ctx_); \
> >> + aio_poll(qemu_get_aio_context(), true); \
> >> + aio_context_acquire(ctx_); \
> >> + } else { \
> >> + busy_ = aio_poll(ctx_, false); \
> >> + } \
> >
> > Wait, I'm confused. The current thread is not in the BDS AioContext.
> > We're not allowed to call aio_poll(ctx_, false).
>
> It's pretty ugly indeed. Strictly from a thread-safety point of view,
> everything that aio_poll calls will acquire the AioContext, so that is
> safe and in fact the release/acquire pair can beeven hoisted outside
> the "if".
>
> If we did that for blocking=true in both I/O and main thread, then that
> would be racy. This is the scenario mentioned in the commit message for
> c9d1a56, "block: only call aio_poll on the current thread's AioContext",
> 2016-10-28).
>
> If only one thread has blocking=true, it's subject to races too. In
> this case, the I/O thread may fail to be woken by iothread_stop's
> aio_notify. However, by the time iothread_stop is called there should
> be no BlockDriverStates (and thus no BDRV_POLL_WHILE running the above
> code) for the I/O thread's AioContext.
The scenario I have in mind is:
BDRV_POLL_WHILE() is called from the IOThread and the main loop also
invokes BDRV_POLL_WHILE().
The IOThread is blocked in aio_poll(ctx, true).
The main loop calls aio_poll(ctx, false) and can therefore steal the
event/completion condition.
I *think* ppoll() will return in both threads and they will race to
invoke handlers, but I'm not 100% sure that two BDRV_POLL_WHILE() calls
in parallel are safe.
What do you think?
Stefan
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
next prev parent reply other threads:[~2017-04-16 17:13 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-14 8:02 [Qemu-devel] [PATCH for-2.9-rc5 v2] block: Drain BH in bdrv_drained_begin Fam Zheng
2017-04-14 8:10 ` Fam Zheng
2017-04-14 8:45 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2017-04-14 8:51 ` Stefan Hajnoczi
2017-04-14 17:10 ` Paolo Bonzini
2017-04-16 9:37 ` Stefan Hajnoczi [this message]
2017-04-17 3:33 ` Fam Zheng
2017-04-18 8:16 ` [Qemu-devel] " Paolo Bonzini
2017-04-17 8:27 ` [Qemu-devel] [Qemu-block] " Fam Zheng
2017-04-17 11:21 ` Jeff Cody
2017-04-18 8:18 ` [Qemu-devel] " Paolo Bonzini
2017-04-18 8:36 ` Fam Zheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170416093743.GA24137@stefanha-x1.localdomain \
--to=stefanha@redhat.com \
--cc=famz@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).