From nobody Fri Nov 26 14:23:57 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 8C15418A26C1; Fri, 26 Nov 2021 14:23:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4J0xnT6Dw7z4cRr; Fri, 26 Nov 2021 14:23:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ADAEF2054; Fri, 26 Nov 2021 14:23:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1AQENvv3002369; Fri, 26 Nov 2021 14:23:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1AQENvF8002368; Fri, 26 Nov 2021 14:23:57 GMT (envelope-from git) Date: Fri, 26 Nov 2021 14:23:57 GMT Message-Id: <202111261423.1AQENvF8002368@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Andriy Gapon Subject: git: 69cfa60ddd4d - main - twsi: protect interaction between twsi_transfer and twsi_intr List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: avg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 69cfa60ddd4df814b507699f9bb7253ece054f40 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1637936638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HF7mwsCorUaU0/hCB3khvUSNPtMtcouiSpkDWDyFEv0=; b=htm43gJjHVxJ2VXfyWceKhRZoQvvElJ+DnV14FZ2NPGylh0MKy4y84J3YjBWQyFKkOcXwj E5pDssNFH176gaw94HetL+iuYl8FtG9RtBD79AAyAzvRcIj5WM35VSsZhQmEwvIdmNXH8K GGxEt7GQi6jzQPNAo8Bml+rf2iUtGvVVTpJc08GT1kWoEURC3XB2bfmHIO+aroTzduq3b3 sTYldgG0ZWAMeUqy7akfdOzSbR/mHP4vqx10TEQjcedNWlCyhVeq+LPiYpxmkrSUvuwbMO /Va514xVkJc4Sh2ILNfESfChMVJ/Vt4qnx74m5GcHq9QVA8VdIB6tiqKHI5TBg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1637936638; a=rsa-sha256; cv=none; b=bkifp5RPdFeGW9S13Tfbv2D0erNiWNTlrpwpuBMOIzyYjwXeaWm3h3/PBmYUR7W67qDTwj Ilt/EItVUmqiCnJcrgmesSXlzy9/A9OC0FnPFSym5Lgqmd5QOj+gCHVSVJOOZvQob0i8Mk lhIkUQbZJi2rzRb0y9dZLXABiC6sBPSbLcTnem8m1FKtqD/3jJyBBU1UTK+aaWCCVEda1G iYk6tsWOCfy+5lM3DQQqzQl2oI0N1V8aZc46FLy3iO39Ne6vwWoE5YO5LeSQdvd/cI2LHf /+s2sa5AQu7jAbQ8XIObMLhJo1I9w6h9ZbHzZO/uh9CHanBkFnsh+6rgx6708A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=69cfa60ddd4df814b507699f9bb7253ece054f40 commit 69cfa60ddd4df814b507699f9bb7253ece054f40 Author: Andriy Gapon AuthorDate: 2021-11-26 07:25:01 +0000 Commit: Andriy Gapon CommitDate: 2021-11-26 14:14:28 +0000 twsi: protect interaction between twsi_transfer and twsi_intr All accesses to softc are now done under a mutex to prevent data races between the open context and the interrupt handler. Additionally, the wait time in twsi_transfer is bounded now. Previously we could get stuck there forever if an interrupt got lost. MFC after: 2 weeks --- sys/dev/iicbus/twsi/twsi.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/sys/dev/iicbus/twsi/twsi.c b/sys/dev/iicbus/twsi/twsi.c index 49fe608473b2..552787874833 100644 --- a/sys/dev/iicbus/twsi/twsi.c +++ b/sys/dev/iicbus/twsi/twsi.c @@ -484,13 +484,16 @@ static int twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) { struct twsi_softc *sc; + int error; sc = device_get_softc(dev); if (!sc->have_intr) return (iicbus_transfer_gen(dev, msgs, nmsgs)); - sc->error = 0; + mtx_lock(&sc->mutex); + KASSERT(sc->transfer == 0, + ("starting a transfer while another is active")); sc->control_val = TWSI_CONTROL_TWSIEN | TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK; @@ -501,6 +504,7 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) sc->msgs = msgs; sc->msg_idx = 0; sc->transfer = 1; + sc->error = 0; #ifdef TWSI_DEBUG for (int i = 0; i < nmsgs; i++) @@ -512,22 +516,25 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) if (sc->msgs[0].len == 1) sc->control_val &= ~TWSI_CONTROL_ACK; TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_START); - while (sc->error == 0 && sc->transfer != 0) { - tsleep_sbt(sc, 0, "twsi", SBT_1MS * 30, SBT_1MS, 0); - } + msleep_sbt(sc, &sc->mutex, 0, "twsi", 3000 * SBT_1MS, SBT_1MS, 0); debugf(sc, "pause finish\n"); - - if (sc->error) { - debugf(sc, "Error, aborting (%d)\n", sc->error); - TWSI_WRITE(sc, sc->reg_control, 0); + if (sc->error == 0 && sc->transfer != 0) { + device_printf(sc->dev, "transfer timeout\n"); + sc->error = IIC_ETIMEOUT; + sc->transfer = 0; } + if (sc->error != 0) + debugf(sc, "Error: %d\n", sc->error); + /* Disable module and interrupts */ debugf(sc, "status=%x\n", TWSI_READ(sc, sc->reg_status)); TWSI_WRITE(sc, sc->reg_control, 0); debugf(sc, "status=%x\n", TWSI_READ(sc, sc->reg_status)); + error = sc->error; + mtx_unlock(&sc->mutex); - return (sc->error); + return (error); } static void @@ -539,11 +546,20 @@ twsi_intr(void *arg) sc = arg; + mtx_lock(&sc->mutex); debugf(sc, "Got interrupt Current msg=%x\n", sc->msg_idx); status = TWSI_READ(sc, sc->reg_status); debugf(sc, "reg control=%x\n", TWSI_READ(sc, sc->reg_control)); + if (sc->transfer == 0) { + device_printf(sc->dev, "interrupt without active transfer, " + "status = 0x%x\n", status); + TWSI_WRITE(sc, sc->reg_control, sc->control_val | + TWSI_CONTROL_STOP); + goto end; + } + switch (status) { case TWSI_STATUS_START: case TWSI_STATUS_RPTD_START: @@ -670,6 +686,7 @@ twsi_intr(void *arg) } debugf(sc, "Refresh reg_control\n"); +end: /* * Newer Allwinner chips clear IFLG after writing 1 to it. */ @@ -681,6 +698,7 @@ twsi_intr(void *arg) sc->transfer = 0; wakeup(sc); } + mtx_unlock(&sc->mutex); } static void