From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 08FA4C77B61 for ; Mon, 24 Apr 2023 13:01:19 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pqvoB-000234-6a; Mon, 24 Apr 2023 09:00:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pquzB-0004iT-Om for qemu-devel@nongnu.org; Mon, 24 Apr 2023 08:08:01 -0400 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pquz9-0007tB-QB for qemu-devel@nongnu.org; Mon, 24 Apr 2023 08:08:01 -0400 Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-2f8405b3dc1so2573489f8f.3 for ; Mon, 24 Apr 2023 05:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1682338075; x=1684930075; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=K/nk8/06PUIiEF4MTmCY2ffYz6LVNdA5FqcFP3h+JHE=; b=E/HqZX9LsREaDluM7cJjcYiaeEE/srWvsBzb31fs0CH00Nu768RWLMrUUQuDuYcGQJ yX7N1yUYRfWezOgunqHuu5v9jpf5UOheoeccCcIPEOM9rWw8t/q759zsvL3P7h1sBfaV AgnLwVzu/dqQWgIJJ1ECPPGufTa/GBT7ssQSI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682338075; x=1684930075; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=K/nk8/06PUIiEF4MTmCY2ffYz6LVNdA5FqcFP3h+JHE=; b=XnURfNBeRjmVoMPtI8cD6M6HnQZWuheeDkF3/mHLyiqi1a237mG4qzTUFsa3FwwFwp T2oWGQDOK58rXemw50sH8Uy7sW5h7eTxxKepEdBvPePkOkbkt8/6uv5DEaFjiqsabftV LxJphyjejbCm89B+RgSYjhr4lRxj4v0jycGZM+oHCs1Ta3ZGe/XKnRADr4EBpOG0jDj5 PU4KePYESiaxyzl7INWWdIKf8vcHFarPEBlGtRtRxEyd4NbLh7xbHiStSaWu566RQPvm eY2LonL6ZIaVsDV1mf90oPdCE8RT4cpI0pAvdIhg1PApDX0/4PLCbMURjm8Imy5E6IsT HUaQ== X-Gm-Message-State: AC+VfDwLjXEik983leAXbAthpP89ffnIQB7CT9Sclh7XowkLHG2Uk4MM 29pIZsDroQ4BSIHBzzzKeSnSMRtAN40PGDYZYEFLKw== X-Google-Smtp-Source: ACHHUZ6u7WWEPIINNleWsZq9Pu/ASD8qrU9YsusjfMMTBjNblxYwTHQsh4UhCIhr4kycgtXT9iCpnCYDngowCfq8+Eg= X-Received: by 2002:a5d:4b86:0:b0:304:8149:239b with SMTP id b6-20020a5d4b86000000b003048149239bmr1051205wrt.50.1682338074747; Mon, 24 Apr 2023 05:07:54 -0700 (PDT) MIME-Version: 1.0 References: <20230420110205.688689-1-mark.syms@citrix.com> <54a37172-cad5-3b27-36fc-3b7768e39df8@xen.org> In-Reply-To: <54a37172-cad5-3b27-36fc-3b7768e39df8@xen.org> From: Mark Syms Date: Mon, 24 Apr 2023 13:07:44 +0100 Message-ID: Subject: Re: [PATCH v2 RESEND] xen: Fix SEGV on domain disconnect To: paul@xen.org Cc: mark.syms@citrix.com, qemu-devel@nongnu.org, Stefano Stabellini , Anthony Perard , xen-devel@lists.xenproject.org, tim.smith@citrix.com Content-Type: multipart/alternative; boundary="000000000000567bf705fa13db30" Received-SPF: pass client-ip=2a00:1450:4864:20::432; envelope-from=msyms@tibco.com; helo=mail-wr1-x432.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.25, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 24 Apr 2023 09:00:36 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --000000000000567bf705fa13db30 Content-Type: text/plain; charset="UTF-8" Copying in Tim who did the final phase of the changes. On Mon, 24 Apr 2023 at 11:32, Paul Durrant wrote: > > On 20/04/2023 12:02, mark.syms@citrix.com wrote: > > From: Mark Syms > > > > Ensure the PV ring is drained on disconnect. Also ensure all pending > > AIO is complete, otherwise AIO tries to complete into a mapping of the > > ring which has been torn down. > > > > Signed-off-by: Mark Syms > > --- > > CC: Stefano Stabellini > > CC: Anthony Perard > > CC: Paul Durrant > > CC: xen-devel@lists.xenproject.org > > > > v2: > > * Ensure all inflight requests are completed before teardown > > * RESEND to fix formatting > > --- > > hw/block/dataplane/xen-block.c | 31 +++++++++++++++++++++++++------ > > 1 file changed, 25 insertions(+), 6 deletions(-) > > > > diff --git a/hw/block/dataplane/xen-block.c b/hw/block/dataplane/xen-block.c > > index 734da42ea7..d9da4090bf 100644 > > --- a/hw/block/dataplane/xen-block.c > > +++ b/hw/block/dataplane/xen-block.c > > @@ -523,6 +523,10 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane) > > > > dataplane->more_work = 0; > > > > + if (dataplane->sring == 0) { > > + return done_something; > > + } > > + > > I think you could just return false here... Nothing is ever going to be > done if there's no ring :-) > > > rc = dataplane->rings.common.req_cons; > > rp = dataplane->rings.common.sring->req_prod; > > xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ > > @@ -666,14 +670,35 @@ void xen_block_dataplane_destroy(XenBlockDataPlane *dataplane > void xen_block_dataplane_stop(XenBlockDataPlane *dataplane) > > { > > XenDevice *xendev; > > + XenBlockRequest *request, *next; > > > > if (!dataplane) { > > return; > > } > > > > + /* We're about to drain the ring. We can cancel the scheduling of any > > + * bottom half now */ > > + qemu_bh_cancel(dataplane->bh); > > + > > + /* Ensure we have drained the ring */ > > + aio_context_acquire(dataplane->ctx); > > + do { > > + xen_block_handle_requests(dataplane); > > + } while (dataplane->more_work); > > + aio_context_release(dataplane->ctx); > > + > > I don't think we want to be taking new requests, do we? > > > + /* Now ensure that all inflight requests are complete */ > > + while (!QLIST_EMPTY(&dataplane->inflight)) { > > + QLIST_FOREACH_SAFE(request, &dataplane->inflight, list, next) { > > + blk_aio_flush(request->dataplane->blk, xen_block_complete_aio, > > + request); > > + } > > + } > > + > > I think this could possibly be simplified by doing the drain after the > call to blk_set_aio_context(), as long as we set dataplane->ctx to > qemu_get_aio_context(). Alos, as long as more_work is not set then it > should still be safe to cancel the bh before the drain AFAICT. > > Paul > > > xendev = dataplane->xendev; > > > > aio_context_acquire(dataplane->ctx); > > + > > if (dataplane->event_channel) { > > /* Only reason for failure is a NULL channel */ > > xen_device_set_event_channel_context(xendev, dataplane->event_channel, > > @@ -684,12 +709,6 @@ void xen_block_dataplane_stop(XenBlockDataPlane *dataplane) > > blk_set_aio_context(dataplane->blk, qemu_get_aio_context(), &error_abort); > > aio_context_release(dataplane->ctx); > > > > - /* > > - * Now that the context has been moved onto the main thread, cancel > > - * further processing. > > - */ > > - qemu_bh_cancel(dataplane->bh); > > - > > if (dataplane->event_channel) { > > Error *local_err = NULL; > > > --000000000000567bf705fa13db30 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Copying in Tim who did the final phase of the changes= .

On Mon, 24 Apr 2023 at 11:32, Paul Durrant <xadimgnik@gmail.com> wrote:
>=
> On 20/04/2023 12:02, mark.= syms@citrix.com wrote:
> > From: Mark Syms <mark.syms@citrix.com>
> >
> = > Ensure the PV ring is drained on disconnect. Also ensure all pending> > AIO is complete, otherwise AIO tries to complete into a mapping= of the
> > ring which has been torn down.
> >
> &g= t; Signed-off-by: Mark Syms <mar= k.syms@citrix.com>
> > ---
> > CC: Stefano Stabell= ini <sstabellini@kernel.org>
> > CC: Anthony Perard <
anthony.perard@citrix.com>
> > CC: Paul Durrant= <paul@xen.org>
> > CC: = xen-devel@lists.xenprojec= t.org
> >
> > v2:
> > =C2=A0 * Ensure all in= flight requests are completed before teardown
> > =C2=A0 * RESEND = to fix formatting
> > ---
> > =C2=A0 hw/block/dataplane/x= en-block.c | 31 +++++++++++++++++++++++++------
> > =C2=A0 1 file = changed, 25 insertions(+), 6 deletions(-)
> >
> > diff --= git a/hw/block/dataplane/xen-block.c b/hw/block/dataplane/xen-block.c
&g= t; > index 734da42ea7..d9da4090bf 100644
> > --- a/hw/block/dat= aplane/xen-block.c
> > +++ b/hw/block/dataplane/xen-block.c
>= ; > @@ -523,6 +523,10 @@ static bool xen_block_handle_requests(XenBlockD= ataPlane *dataplane)
> > =C2=A0
> > =C2=A0 =C2=A0 =C2=A0= dataplane->more_work =3D 0;
> > =C2=A0
> > + =C2=A0 = =C2=A0if (dataplane->sring =3D=3D 0) {
> > + =C2=A0 =C2=A0 =C2= =A0 =C2=A0return done_something;
> > + =C2=A0 =C2=A0}
> >= +
>
> I think you could just return false here... Nothing is e= ver going to be
> done if there's no ring :-)
>
> >= ; =C2=A0 =C2=A0 =C2=A0 rc =3D dataplane->rings.common.req_cons;
> = > =C2=A0 =C2=A0 =C2=A0 rp =3D dataplane->rings.common.sring->req_p= rod;
> > =C2=A0 =C2=A0 =C2=A0 xen_rmb(); /* Ensure we see queued r= equests up to 'rp'. */
> > @@ -666,14 +670,35 @@ void xen_= block_dataplane_destroy(XenBlockDataPlane *dataplane > =C2=A0 void xen_b= lock_dataplane_stop(XenBlockDataPlane *dataplane)
> > =C2=A0 {
= > > =C2=A0 =C2=A0 =C2=A0 XenDevice *xendev;
> > + =C2=A0 =C2= =A0XenBlockRequest *request, *next;
> > =C2=A0
> > =C2= =A0 =C2=A0 =C2=A0 if (!dataplane) {
> > =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 return;
> > =C2=A0 =C2=A0 =C2=A0 }
> > =C2=A0 =
> > + =C2=A0 =C2=A0/* We're about to drain the ring. We can c= ancel the scheduling of any
> > + =C2=A0 =C2=A0 * bottom half now = */
> > + =C2=A0 =C2=A0qemu_bh_cancel(dataplane->bh);
> &g= t; +
> > + =C2=A0 =C2=A0/* Ensure we have drained the ring */
&= gt; > + =C2=A0 =C2=A0aio_context_acquire(dataplane->ctx);
> >= ; + =C2=A0 =C2=A0do {
> > + =C2=A0 =C2=A0 =C2=A0 =C2=A0xen_block_h= andle_requests(dataplane);
> > + =C2=A0 =C2=A0} while (dataplane-&= gt;more_work);
> > + =C2=A0 =C2=A0aio_context_release(dataplane-&g= t;ctx);
> > +
>
> I don't think we want to be taki= ng new requests, do we?
>
> > + =C2=A0 =C2=A0/* Now ensure t= hat all inflight requests are complete */
> > + =C2=A0 =C2=A0while= (!QLIST_EMPTY(&dataplane->inflight)) {
> > + =C2=A0 =C2=A0= =C2=A0 =C2=A0QLIST_FOREACH_SAFE(request, &dataplane->inflight, list= , next) {
> > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0blk_aio_f= lush(request->dataplane->blk, xen_block_complete_aio,
> > + = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0request);
> > + =C2=A0 =C2=A0 =C2=A0 =C2=A0}
> >= ; + =C2=A0 =C2=A0}
> > +
>
> I think this could possib= ly be simplified by doing the drain after the
> call to blk_set_aio_c= ontext(), as long as we set dataplane->ctx to
> qemu_get_aio_conte= xt(). Alos, as long as more_work is not set then it
> should still be= safe to cancel the bh before the drain AFAICT.
>
> =C2=A0 =C2= =A0Paul
>
> > =C2=A0 =C2=A0 =C2=A0 xendev =3D dataplane->= xendev;
> > =C2=A0
> > =C2=A0 =C2=A0 =C2=A0 aio_context_= acquire(dataplane->ctx);
> > +
> > =C2=A0 =C2=A0 =C2= =A0 if (dataplane->event_channel) {
> > =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 /* Only reason for failure is a NULL channel */
> > = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xen_device_set_event_channel_context(xen= dev, dataplane->event_channel,
> > @@ -684,12 +709,6 @@ void xe= n_block_dataplane_stop(XenBlockDataPlane *dataplane)
> > =C2=A0 = =C2=A0 =C2=A0 blk_set_aio_context(dataplane->blk, qemu_get_aio_context()= , &error_abort);
> > =C2=A0 =C2=A0 =C2=A0 aio_context_release(= dataplane->ctx);
> > =C2=A0
> > - =C2=A0 =C2=A0/*
= > > - =C2=A0 =C2=A0 * Now that the context has been moved onto the ma= in thread, cancel
> > - =C2=A0 =C2=A0 * further processing.
>= ; > - =C2=A0 =C2=A0 */
> > - =C2=A0 =C2=A0qemu_bh_cancel(datapl= ane->bh);
> > -
> > =C2=A0 =C2=A0 =C2=A0 if (dataplane= ->event_channel) {
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Error= *local_err =3D NULL;
> > =C2=A0
>
--000000000000567bf705fa13db30--