From nobody Fri Dec 10 12:30:18 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 56B5D18CFF14; Fri, 10 Dec 2021 12:30:19 +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 4J9Vbv05kHz3rZb; Fri, 10 Dec 2021 12:30:19 +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 D7A6C1A470; Fri, 10 Dec 2021 12:30:18 +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 1BACUIM0063438; Fri, 10 Dec 2021 12:30:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BACUIci063433; Fri, 10 Dec 2021 12:30:18 GMT (envelope-from git) Date: Fri, 10 Dec 2021 12:30:18 GMT Message-Id: <202112101230.1BACUIci063433@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: 109000c32ddc - stable/13 - 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/13 X-Git-Reftype: branch X-Git-Commit: 109000c32ddc99de0f7bfe6e8a07bcc67f5929ec Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639139419; 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=13PTtvRFZFQAingnQ+hFGweit0ZfD58GLB738EOdcS4=; b=LGoB2Auj3uL8iH7S8Dsd2CxpXrUaOoyVH5oOX+aJLXmAEWHw67EiiCxSM4j1jZujtrV4iH aAkheZkqX/hmG7Wi/GFPPPy3w4GprpSq62TCUOQegLKMvfxV8D8blzg5Z3RmAMQU0gKZQU 5GxDzqEA6QY4hY5LyEZWffMxg5/yaKygMbYBh/GDR5mMxYpXNFLXSu9vVWKh/HqCPP0ll3 WaZHbq7yZBrHrIFBarFM856mC0M1C0rIuzQrRY/braz0bJt2N9O0GqkAe0ds68t3qfd9j7 Wk1YoUQ2NYaMZHzRvScnm8MGyUNr9QBIPOotjDONg/IUndb+jEgXiEa5sSYlGQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639139419; a=rsa-sha256; cv=none; b=ITzFpnRgnvU30CZ++oIyH1d3MJbTUzjtc0d1vtT9xXvAHfN8yo+1A+XabV99YQEW6AmRCH eOD27Zf17wKpIRwo1e/iftOW+bi4jhGudtYAQr2DKUzNqoc1YZw0e1mOJiv/rZmQQLaaRi +YzgSE2jEjTI3r0+drDKf3naUwo40ug5vj4FDmRHTrhKIQneXhoHXG6wwiASLYm314nRTV 7UHsbJAGbaqevZ8QAJs06pfNztUhvIEDMB5Wu+ImcM05lbfbYHHkQ9d6IubQjuzACgP93P 9sP9aISYSJhqy+1DPwzUTFbBwC54/gChS8LPhXyhzrlV+T3ee3htw6nL0dkmXw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=109000c32ddc99de0f7bfe6e8a07bcc67f5929ec commit 109000c32ddc99de0f7bfe6e8a07bcc67f5929ec Author: Andriy Gapon AuthorDate: 2021-11-26 07:25:01 +0000 Commit: Andriy Gapon CommitDate: 2021-12-10 12:30:12 +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