From nobody Fri Dec 10 12:31:23 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 2FB0D18D17AD; Fri, 10 Dec 2021 12:31:25 +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 4J9Vd82kQNz3sTb; Fri, 10 Dec 2021 12:31:24 +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 004351A4E9; Fri, 10 Dec 2021 12:31:23 +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 1BACVNGO066083; Fri, 10 Dec 2021 12:31:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BACVNZG066082; Fri, 10 Dec 2021 12:31:23 GMT (envelope-from git) Date: Fri, 10 Dec 2021 12:31:23 GMT Message-Id: <202112101231.1BACVNZG066082@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andriy Gapon Subject: git: 4030ad30b2fe - stable/12 - 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/stable/12 X-Git-Reftype: branch X-Git-Commit: 4030ad30b2feb96bddbb2b6e6e94721bdf160f87 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639139484; 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=avKgQMH/TEGsZSCTbj7XzwoffOOMqW9qpfADwPoumFg=; b=bvVEDFGoBs9FTsnXzeOMyp7lbA9/yNocXDtVylfAeBLZUAgKsI5Zv6fU7vDNfuzMujHv46 ggTseWTg6CPyXgqu5iC7pPwyUzNPWWpNWMiFdDHZufssoYpNQ5YU6+vZw6Qmqh0Fb24Ehk D4gDg3cjnlBoE0RNSZ/nyXKcVH/0jzLTmA29LN6mvCRLYdpZ5J9vuiFkER9pCf6B4MqzKq 1l1EcBiw+igcRc9L2AjFgKWXENr+2DmEofBr5nsoWl24xWFgfKe7OqiaVDFTgd/hqblTj1 XIRCBINhzrduBZTIJn/9DiGDMFue5jwSByEsX8DwRlnMR6cBtGMe4g5qLOz9Pw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639139484; a=rsa-sha256; cv=none; b=k1XU5ntxFZ8eb+qrkGFf/uvY2WMjRy7AiMdBcmpwkC673yVcyNhdgY5Jva73GHBpKfqyTo tfjiAMaZzl+KcE2X99wewht+dD/+/K1Eet8zIpBJkTuowC730YKw8IQx3nNKHgs8Z9bZu8 K9aJspsfVKNO3OTJBlreDeH2GXq5i6ol9Fw+SgrNXdwpETnKEZAvwD+ShSvQy4oUY/5WEp tNWV4oqTBEp8+56HG+NfszZIAdraO9h6NuWukTxZBt32sjWA3aHvaQVfKXk8gscZRGkxLF uG3xzVqYeax5YD8WsiarA/v4Wbu+qpxeIHtJLjcHE3zhDwPKOwhlS/z5NgiW4g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=4030ad30b2feb96bddbb2b6e6e94721bdf160f87 commit 4030ad30b2feb96bddbb2b6e6e94721bdf160f87 Author: Andriy Gapon AuthorDate: 2021-11-26 07:25:01 +0000 Commit: Andriy Gapon CommitDate: 2021-12-10 12:30:42 +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. (cherry picked from commit 69cfa60ddd4df814b507699f9bb7253ece054f40) --- 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