From nobody Fri Nov 7 23:52:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548854991623678.0221092320189; Wed, 30 Jan 2019 05:29:51 -0800 (PST) Received: from localhost ([127.0.0.1]:38144 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gopw5-0006Wx-On for importer@patchew.org; Wed, 30 Jan 2019 08:29:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36404) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1goppL-00016i-Eo for qemu-devel@nongnu.org; Wed, 30 Jan 2019 08:22:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1goppJ-00062W-Ka for qemu-devel@nongnu.org; Wed, 30 Jan 2019 08:22:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39214) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1goppJ-0005mi-9L; Wed, 30 Jan 2019 08:22:49 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7325E13AA9; Wed, 30 Jan 2019 13:22:25 +0000 (UTC) Received: from localhost (dhcp-192-213.str.redhat.com [10.33.192.213]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 23F9D5C224; Wed, 30 Jan 2019 13:22:18 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Wed, 30 Jan 2019 14:22:07 +0100 Message-Id: <20190130132212.7376-2-cohuck@redhat.com> In-Reply-To: <20190130132212.7376-1-cohuck@redhat.com> References: <20190130132212.7376-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 30 Jan 2019 13:22:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/6] vfio-ccw: make it safe to access channel programs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When we get a solicited interrupt, the start function may have been cleared by a csch, but we still have a channel program structure allocated. Make it safe to call the cp accessors in any case, so we can call them unconditionally. While at it, also make sure that functions called from other parts of the code return gracefully if the channel program structure has not been initialized (even though that is a bug in the caller). Signed-off-by: Cornelia Huck --- drivers/s390/cio/vfio_ccw_cp.c | 20 +++++++++++++++++++- drivers/s390/cio/vfio_ccw_cp.h | 2 ++ drivers/s390/cio/vfio_ccw_fsm.c | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index ba08fe137c2e..0bc0c38edda7 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -335,6 +335,7 @@ static void cp_unpin_free(struct channel_program *cp) struct ccwchain *chain, *temp; int i; =20 + cp->initialized =3D false; list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { for (i =3D 0; i < chain->ch_len; i++) { pfn_array_table_unpin_free(chain->ch_pat + i, @@ -701,6 +702,8 @@ int cp_init(struct channel_program *cp, struct device *= mdev, union orb *orb) */ cp->orb.cmd.c64 =3D 1; =20 + cp->initialized =3D true; + return ret; } =20 @@ -715,7 +718,8 @@ int cp_init(struct channel_program *cp, struct device *= mdev, union orb *orb) */ void cp_free(struct channel_program *cp) { - cp_unpin_free(cp); + if (cp->initialized) + cp_unpin_free(cp); } =20 /** @@ -760,6 +764,10 @@ int cp_prefetch(struct channel_program *cp) struct ccwchain *chain; int len, idx, ret; =20 + /* this is an error in the caller */ + if (!cp || !cp->initialized) + return -EINVAL; + list_for_each_entry(chain, &cp->ccwchain_list, next) { len =3D chain->ch_len; for (idx =3D 0; idx < len; idx++) { @@ -795,6 +803,10 @@ union orb *cp_get_orb(struct channel_program *cp, u32 = intparm, u8 lpm) struct ccwchain *chain; struct ccw1 *cpa; =20 + /* this is an error in the caller */ + if (!cp || !cp->initialized) + return NULL; + orb =3D &cp->orb; =20 orb->cmd.intparm =3D intparm; @@ -831,6 +843,9 @@ void cp_update_scsw(struct channel_program *cp, union s= csw *scsw) u32 cpa =3D scsw->cmd.cpa; u32 ccw_head, ccw_tail; =20 + if (!cp->initialized) + return; + /* * LATER: * For now, only update the cmd.cpa part. We may need to deal with @@ -869,6 +884,9 @@ bool cp_iova_pinned(struct channel_program *cp, u64 iov= a) struct ccwchain *chain; int i; =20 + if (!cp->initialized) + return false; + list_for_each_entry(chain, &cp->ccwchain_list, next) { for (i =3D 0; i < chain->ch_len; i++) if (pfn_array_table_iova_pinned(chain->ch_pat + i, diff --git a/drivers/s390/cio/vfio_ccw_cp.h b/drivers/s390/cio/vfio_ccw_cp.h index a4b74fb1aa57..3c20cd208da5 100644 --- a/drivers/s390/cio/vfio_ccw_cp.h +++ b/drivers/s390/cio/vfio_ccw_cp.h @@ -21,6 +21,7 @@ * @ccwchain_list: list head of ccwchains * @orb: orb for the currently processed ssch request * @mdev: the mediated device to perform page pinning/unpinning + * @initialized: whether this instance is actually initialized * * @ccwchain_list is the head of a ccwchain list, that contents the * translated result of the guest channel program that pointed out by @@ -30,6 +31,7 @@ struct channel_program { struct list_head ccwchain_list; union orb orb; struct device *mdev; + bool initialized; }; =20 extern int cp_init(struct channel_program *cp, struct device *mdev, diff --git a/drivers/s390/cio/vfio_ccw_fsm.c b/drivers/s390/cio/vfio_ccw_fs= m.c index cab17865aafe..e7c9877c9f1e 100644 --- a/drivers/s390/cio/vfio_ccw_fsm.c +++ b/drivers/s390/cio/vfio_ccw_fsm.c @@ -31,6 +31,10 @@ static int fsm_io_helper(struct vfio_ccw_private *privat= e) private->state =3D VFIO_CCW_STATE_BUSY; =20 orb =3D cp_get_orb(&private->cp, (u32)(addr_t)sch, sch->lpm); + if (!orb) { + ret =3D -EIO; + goto out; + } =20 /* Issue "Start Subchannel" */ ccode =3D ssch(sch->schid, orb); @@ -64,6 +68,7 @@ static int fsm_io_helper(struct vfio_ccw_private *private) default: ret =3D ccode; } +out: spin_unlock_irqrestore(sch->lock, flags); return ret; } --=20 2.17.2