Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7317817imu; Thu, 27 Dec 2018 17:39:32 -0800 (PST) X-Google-Smtp-Source: ALg8bN5FU4q2OymC/nkE9Vr6NhFX9+45NR4EhS98nFxUjMWr8AjSPJ42J+HMt8j1L6u2BQ9cl8wf X-Received: by 2002:a17:902:145:: with SMTP id 63mr25706306plb.256.1545961172560; Thu, 27 Dec 2018 17:39:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545961172; cv=none; d=google.com; s=arc-20160816; b=vnjc+w3Ha6iZLFMP+IYmykGBqZuay9iH9GiJHna1qclu0ZhtbxT7zXWeKaGe/fzm/8 1/lryIg2j6fqOKRERwxtk8Pf9xpGMXGaf+ErY3UKg/GYT6pPqRDGVwsP+ST1UfVt6HUt AhTsRrW5n+Vk2ZvBoqcMRSVGWyhvlEKAd7PLIbreT8T9dhECWdQGE4Z17sUjMX9yd51g TVoniJBf7a3sUJHP124/IyORKHVhIsgj8pq+mxM6F0RhLWkLbE2b6Y1C1zf/hJhiylBb amcshrK1dw29t5GeNNUYnS5LFSfMzthWeOzYj+ypnrxtp8eAmi069aZpQYfAmSa1QIq0 P74Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=6FT9e6TfbOxzMhjl+q9yVcOmjKzIoZ8JITbS0f8UMQc=; b=zLi39Q/7fCmuzXIbXRv1YUlhPHevnZ4qXKVWUmcmCwCR5YQGfD3/JdnoOul3kMI6ib Bj2qK9daUeok7xnqC9Ek9w25AeHJqtV6VlkfakxztOw0xMLa1hKIdks7AHuwQKw0psNj ZlN/B0By9NzIqvFhjt8M8ukeXhMB7T1Q6X1ChANxZJONnxnLD/8AVZBrIGbXrbE9MuGX 4eagbsux61VBZdWmqGJsPGBhW8c6gm/V+M1yjQx2E7v8evu/oY3IkjSWASTq8vEm0SUD 50i2TG3siRRxPhDHUM4kqbdvBTdcFn0PuIGiyfX1NBvfjIvNzAxXLwXq7l3QcWJyuWS3 TRDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c8si22459257pgc.65.2018.12.27.17.39.16; Thu, 27 Dec 2018 17:39:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728451AbeL0Pmv (ORCPT + 99 others); Thu, 27 Dec 2018 10:42:51 -0500 Received: from mail.bootlin.com ([62.4.15.54]:35028 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727119AbeL0Pmv (ORCPT ); Thu, 27 Dec 2018 10:42:51 -0500 Received: by mail.bootlin.com (Postfix, from userid 110) id 0C8952072F; Thu, 27 Dec 2018 16:42:49 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=0.5 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED,URIBL_RHS_DOB shortcircuit=ham autolearn=disabled version=3.4.2 Received: from localhost.localdomain (aaubervilliers-681-1-80-230.w90-88.abo.wanadoo.fr [90.88.22.230]) by mail.bootlin.com (Postfix) with ESMTPSA id 603C12070E; Thu, 27 Dec 2018 16:42:48 +0100 (CET) From: Paul Kocialkowski To: bcm-kernel-feedback-list@broadcom.com, linux-i2c@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Florian Fainelli , Ray Jui , Scott Branden , Eric Anholt , Stefan Wahren , Maxime Ripard , Eben Upton , Thomas Petazzoni , Paul Kocialkowski Subject: [PATCH v2] i2c: bcm2835: Clear current buffer pointers and counts after a transfer Date: Thu, 27 Dec 2018 16:42:25 +0100 Message-Id: <20181227154225.5492-1-paul.kocialkowski@bootlin.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The driver's interrupt handler checks whether a message is currently being handled with the curr_msg pointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, the i2c_start_transfer routine checks for the remaining number of messages to handle in num_msgs. However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed). When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the curr_msg structure, leading to a fatal page fault. The msg_buf and msg_buf_remaining fields are also never cleared at the end of the transfer, which can lead to similar pitfalls. Fix these issues by introducing a cleanup function and always calling it after a transfer is finished. Fixes: e2474541032d ("i2c: bcm2835: Fix hang for writing messages larger than 16 bytes") Signed-off-by: Paul Kocialkowski --- Changes since v1: * Added cleanup of msg_buf and msg_buf_remaining; * Moved cleanups to a dedicated function; * Performed cleanup on timeout as well; * Added Fixes tag. drivers/i2c/busses/i2c-bcm2835.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/i2c/busses/i2c-bcm2835.c b/drivers/i2c/busses/i2c-bcm2835.c index 44deae78913e..4d19254f78c8 100644 --- a/drivers/i2c/busses/i2c-bcm2835.c +++ b/drivers/i2c/busses/i2c-bcm2835.c @@ -191,6 +191,15 @@ static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev) bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c); } +static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev) +{ + i2c_dev->curr_msg = NULL; + i2c_dev->num_msgs = 0; + + i2c_dev->msg_buf = NULL; + i2c_dev->msg_buf_remaining = 0; +} + /* * Note about I2C_C_CLEAR on error: * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in @@ -291,6 +300,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], time_left = wait_for_completion_timeout(&i2c_dev->completion, adap->timeout); + + bcm2835_i2c_finish_transfer(i2c_dev); + if (!time_left) { bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); -- 2.20.1