From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54399) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSuCv-0001hl-R9 for qemu-devel@nongnu.org; Mon, 08 Feb 2016 17:22:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSuCq-0008GA-SW for qemu-devel@nongnu.org; Mon, 08 Feb 2016 17:22:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43119) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSuCq-0008G5-KL for qemu-devel@nongnu.org; Mon, 08 Feb 2016 17:22:52 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 2035591FCD for ; Mon, 8 Feb 2016 22:22:52 +0000 (UTC) References: <1454948107-11844-1-git-send-email-pbonzini@redhat.com> <1454948107-11844-5-git-send-email-pbonzini@redhat.com> From: Eric Blake Message-ID: <56B9153A.8080700@redhat.com> Date: Mon, 8 Feb 2016 15:22:50 -0700 MIME-Version: 1.0 In-Reply-To: <1454948107-11844-5-git-send-email-pbonzini@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="5S5bCXklV5dIK6o5VpmK8QIMwxB6Os1k1" Subject: Re: [Qemu-devel] [PATCH 04/16] aio: only call aio_poll_internal from iothread List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini , qemu-devel@nongnu.org Cc: stefanha@redhat.com This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --5S5bCXklV5dIK6o5VpmK8QIMwxB6Os1k1 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 02/08/2016 09:14 AM, Paolo Bonzini wrote: > aio_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. >=20 > 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. >=20 > Signed-off-by: Paolo Bonzini > --- > +++ b/async.c > @@ -300,12 +300,224 @@ void aio_notify_accept(AioContext *ctx) > } > } > =20 > +/* 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 allo= ws > + * usage from _two_ threads: the I/O thread of course, and the main th= read. > + * 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 lo= ck. > + * > + * 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? > + * Similarly it is okay to return true when no progress was made > + * (as long as this doesn't happen forever, or you get livelock). > + * > + --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --5S5bCXklV5dIK6o5VpmK8QIMwxB6Os1k1 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWuRU6AAoJEKeha0olJ0NqVMAH/3z3UkmPcGIFETZ03XdSGGOY t9/m7jPpT2zRF4aNQO/6CsFffxNWJL+SPbqCEwoXebA7Gkg0WnQM1tQkUjDe5TWt Y27MXZ/2aX2IArMz3aUntyKd+Ova6GQzc/aa9TK5ac9M4jy5IfLUZQaORBwnmOk4 e422kelGgbd9BRq5gmhCS6wYIROBXSRHybokpcvVC8J1qVrnOvuvuVkP3Gs6M9Q9 /hFpF64dJMDifizIo5M32ImfJAbuOUZvaarKy55XqEcf9uZ1jZ9RS2WiysRmLSE+ 6ht8FX88mQml/f4YFbuyyji+uOqiQfFU4+LHcWlvyloY00Y4O6Hf7tnuveZ+Edw= =aygD -----END PGP SIGNATURE----- --5S5bCXklV5dIK6o5VpmK8QIMwxB6Os1k1--