Received: from eggs.gnu.org ([2001:4830:134:3::10]:41756) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brm0j-0007dW-Gx for qemu-devel@nongnu.org; Wed, 05 Oct 2016 09:13:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1brm0f-0006BZ-KM for qemu-devel@nongnu.org; Wed, 05 Oct 2016 09:13:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41960) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brm0f-0006B2-BV for qemu-devel@nongnu.org; Wed, 05 Oct 2016 09:13:21 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D40548123D for ; Wed, 5 Oct 2016 13:13:20 +0000 (UTC) Date: Wed, 5 Oct 2016 14:13:18 +0100 From: Stefan Hajnoczi Message-ID: <20161005131318.GF863@stefanha-x1.localdomain> References: <1475511256-20051-1-git-send-email-pbonzini@redhat.com> <1475511256-20051-2-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="RE3pQJLXZi4fr8Xo" Content-Disposition: inline In-Reply-To: <1475511256-20051-2-git-send-email-pbonzini@redhat.com> Subject: Re: [Qemu-devel] [PATCH 1/2] async: add aio_bh_schedule_oneshot List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-devel@nongnu.org, kwolf@redhat.com, famz@redhat.com --RE3pQJLXZi4fr8Xo Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Oct 03, 2016 at 06:14:15PM +0200, Paolo Bonzini wrote: > qemu_bh_delete is already clearing bh->scheduled at the same time > as it's setting bh->deleted. Since it's not using any memory > barriers, there is no synchronization going on for bh->deleted, > and this makes the bh->deleted checks superfluous in aio_compute_timeout, > aio_bh_poll and aio_ctx_check. Yikes. On one hand this sounds scary but in practice qemu_bh_delete() isn't called from another thread so the next aio_bh_poll() will indeed clean it up instead of dispatching a deleted BH. Due to the nature of this change I suggest making it in a separate patch. > Just remove them, and put the (bh->scheduled && bh->deleted) combo > to work in a new function aio_bh_schedule_oneshot. The new function > removes the need to save the QEMUBH pointer between the creation > and the execution of the bottom half. >=20 > Signed-off-by: Paolo Bonzini > --- > async.c | 27 +++++++++++++++++++++++---- > include/block/aio.h | 10 ++++++++++ > 2 files changed, 33 insertions(+), 4 deletions(-) >=20 > diff --git a/async.c b/async.c > index 3bca9b0..f30d011 100644 > --- a/async.c > +++ b/async.c > @@ -44,6 +44,25 @@ struct QEMUBH { > bool deleted; > }; > =20 > +void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaq= ue) > +{ > + QEMUBH *bh; > + bh =3D g_new(QEMUBH, 1); > + *bh =3D (QEMUBH){ > + .ctx =3D ctx, > + .cb =3D cb, > + .opaque =3D opaque, > + }; > + qemu_mutex_lock(&ctx->bh_lock); > + bh->next =3D ctx->first_bh; > + bh->scheduled =3D 1; > + bh->deleted =3D 1; > + /* Make sure that the members are ready before putting bh into list = */ > + smp_wmb(); > + ctx->first_bh =3D bh; > + qemu_mutex_unlock(&ctx->bh_lock); > +} > + > QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) > { > QEMUBH *bh; > @@ -86,7 +105,7 @@ int aio_bh_poll(AioContext *ctx) > * thread sees the zero before bh->cb has run, and thus will call > * aio_notify again if necessary. > */ > - if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) { > + if (atomic_xchg(&bh->scheduled, 0)) { > /* Idle BHs and the notify BH don't count as progress */ > if (!bh->idle && bh !=3D ctx->notify_dummy_bh) { > ret =3D 1; > @@ -104,7 +123,7 @@ int aio_bh_poll(AioContext *ctx) > bhp =3D &ctx->first_bh; > while (*bhp) { > bh =3D *bhp; > - if (bh->deleted) { > + if (bh->deleted && !bh->scheduled) { > *bhp =3D bh->next; > g_free(bh); > } else { > @@ -168,7 +187,7 @@ aio_compute_timeout(AioContext *ctx) > QEMUBH *bh; > =20 > for (bh =3D ctx->first_bh; bh; bh =3D bh->next) { > - if (!bh->deleted && bh->scheduled) { > + if (bh->scheduled) { > if (bh->idle) { > /* idle bottom halves will be polled at least > * every 10ms */ > @@ -216,7 +235,7 @@ aio_ctx_check(GSource *source) > aio_notify_accept(ctx); > =20 > for (bh =3D ctx->first_bh; bh; bh =3D bh->next) { > - if (!bh->deleted && bh->scheduled) { > + if (bh->scheduled) { > return true; > } > } > diff --git a/include/block/aio.h b/include/block/aio.h > index 173c1ed..be7dd3b 100644 > --- a/include/block/aio.h > +++ b/include/block/aio.h > @@ -181,6 +181,16 @@ void aio_context_acquire(AioContext *ctx); > void aio_context_release(AioContext *ctx); > =20 > /** > + * aio_bh_schedule_oneshot: Allocate a new bottom half structure that wi= ll run > + * only once and as soon as possible. > + * > + * Bottom halves are lightweight callbacks whose invocation is guaranteed > + * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure > + * is opaque and must be allocated prior to its use. I'm confused. There is no QEMUBH structure in this function prototype.=A0 Is this comment from an earlier version of this function? > + */ > +void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaq= ue); > + > +/** > * aio_bh_new: Allocate a new bottom half structure. > * > * Bottom halves are lightweight callbacks whose invocation is guaranteed > --=20 > 2.7.4 >=20 >=20 --RE3pQJLXZi4fr8Xo Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEcBAEBAgAGBQJX9PxuAAoJEJykq7OBq3PI0UsH+wXeMY8uZUiltlJafcF9DTZk PFwQIlv1qP/8N8We06JBIVKpvBq319A8mmlKCjccdr1EKrkWPpBlpoKjOTjs5hyv 29KvZVTQN0rVft8Nhx5Yc68ih6N233+eKDUKxnhdECo9eswhy81Tt5tluayGc8KY taGkS9vcSugt9wIsFQ4XTNwAtDFYn26vdxHJVlFK6cvUi2hua76ka+6LiTCMZoMn Kx6ssGMQvKxTyrMx8tRxpiYKpZpuglSW3ZzIduUtTlYz1ectpIRpDzhZBJAxjPgU IuLHgi0/88pVLnyBebg/claZJBU10hQdCgR0QePzO55FsAUc0JfwkxhbSVvVOjA= =tVXX -----END PGP SIGNATURE----- --RE3pQJLXZi4fr8Xo--