From: Kevin Wolf <kwolf@redhat.com>
To: Fam Zheng <famz@redhat.com>
Cc: qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>,
qemu-block@nongnu.org, Ed Swierk <eswierk@skyportsystems.com>,
Max Reitz <mreitz@redhat.com>, Eric Blake <eblake@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 5/6] coroutine: Explicitly specify AioContext when entering coroutine
Date: Fri, 7 Apr 2017 17:14:45 +0200 [thread overview]
Message-ID: <20170407151445.GF4716@noname.redhat.com> (raw)
In-Reply-To: <20170407065414.9143-6-famz@redhat.com>
Am 07.04.2017 um 08:54 hat Fam Zheng geschrieben:
> Coroutine in block layer should always be waken up in bs->aio_context
> rather than the "current" context where it is entered. They differ when
> the main loop is doing QMP tasks.
>
> Race conditions happen without this patch, because the wrong context is
> acquired in co_schedule_bh_cb, while the entered coroutine works on a
> different one:
>
> main loop iothread
> -----------------------------------------------------------------------
> blockdev_snapshot
> aio_context_acquire(bs->ctx)
> bdrv_flush(bs)
> bdrv_co_flush(bs)
> ...
> qemu_coroutine_yield(co)
> BDRV_POLL_WHILE()
> aio_context_release(bs->ctx)
> aio_context_acquire(bs->ctx)
> ...
> aio_co_wake(co)
> aio_poll(qemu_aio_context) ...
> co_schedule_bh_cb() ...
> qemu_coroutine_enter(co) ...
> /* (A) bdrv_co_flush(bs) /* (B) I/O on bs */
> continues... */
> aio_context_release(bs->ctx)
As I discussed with Fam on IRC this morning, what this patch tries to
fix (acquiring the wrong context) is not the real problem, but just a
symptom.
If you look at the example above (the same is true for the other example
that Fam posted in a reply), you see that the monitor called
aio_context_acquire() specifically in order to avoid that some other
user interferes with its activities. The real bug is that the iothread
even had a chance to run. One part of the reason is that
BDRV_POLL_WHILE() drops the lock since commit c9d1a561, so just calling
aio_context_acquire() doesn't protect you any more if there is any
chance that a nested function calls BDRV_POLL_WHILE().
I haven't checked what was done when merging said commit, but I kind of
expect that we didn't carefully audit all callers of
aio_context_acquire() whether they can cope with this. Specifically,
monitor commands tend to rely on the fact that they keep the lock and
therefore nobody else can interfere. When I scrolled through blockdev.c
this morning, I saw a few suspicious ones that could be broken now.
Now, of course, some callers additionally call bdrv_drained_begin(), and
the snapshot code is one of them. This should in theory be safe, but in
practice even both aio_context_acquire() and bdrv_drained_begin()
together don't give us the exclusive access that these callers want.
This is the real bug to address.
I don't know enough about this code to say whether aio_context_acquire()
alone should give the same guarantees again (I suspect it became
impractical?) or whether we need to fix only bdrv_drained_begin() and
add it to more places. So I'll join the others in this email thread:
Paolo, do you have an opinion on this?
Kevin
> Both (A) and (B) can access resources protected by bs->ctx, but (A) is
> not thread-safe.
>
> Make the block layer explicitly specify a desired context for the
> entered coroutine. For the rest callers, stick to the old behavior,
> qemu_get_aio_context() or qemu_get_current_aio_context().
>
> Signed-off-by: Fam Zheng <famz@redhat.com>
next prev parent reply other threads:[~2017-04-07 15:15 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-07 6:54 [Qemu-devel] [PATCH v2 0/6] block: Fixes regarding dataplane and management operations Fam Zheng
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 1/6] block: Fix unpaired aio_disable_external in external snapshot Fam Zheng
2017-04-07 13:23 ` Stefan Hajnoczi
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 2/6] block: Assert attached child node has right aio context Fam Zheng
2017-04-07 13:24 ` Stefan Hajnoczi
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 3/6] mirror: Fix aio context of mirror_top_bs Fam Zheng
2017-04-07 13:24 ` Stefan Hajnoczi
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 4/6] block: Quiesce old aio context during bdrv_set_aio_context Fam Zheng
2017-04-07 12:50 ` Stefan Hajnoczi
2017-04-08 3:43 ` Fam Zheng
2017-04-10 8:06 ` Kevin Wolf
2017-04-10 8:45 ` Fam Zheng
2017-04-10 9:11 ` Kevin Wolf
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 5/6] coroutine: Explicitly specify AioContext when entering coroutine Fam Zheng
2017-04-07 9:57 ` Fam Zheng
2017-04-07 13:26 ` Stefan Hajnoczi
2017-04-08 3:27 ` Fam Zheng
2017-04-07 14:07 ` Eric Blake
2017-04-07 15:14 ` Kevin Wolf [this message]
2017-04-10 0:56 ` Paolo Bonzini
2017-04-10 1:43 ` Fam Zheng
2017-04-07 6:54 ` [Qemu-devel] [PATCH v2 6/6] tests/block-job-txn: Don't start block job before adding to txn Fam Zheng
2017-04-07 13:28 ` Stefan Hajnoczi
2017-04-07 18:05 ` John Snow
2017-04-08 3:39 ` Fam Zheng
2017-04-07 12:45 ` [Qemu-devel] [PATCH v2 0/6] block: Fixes regarding dataplane and management operations Kevin Wolf
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=20170407151445.GF4716@noname.redhat.com \
--to=kwolf@redhat.com \
--cc=eblake@redhat.com \
--cc=eswierk@skyportsystems.com \
--cc=famz@redhat.com \
--cc=mreitz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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).