From patchwork Mon Feb 13 15:25:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 13138590 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1793DC636D4 for ; Mon, 13 Feb 2023 15:27:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=xd9MPBlN5PbOd/Tk8ufwxf93uU7V6A1djLd9/g9n3hA=; b=cuAlz0vDwiNu1C 2zQgi1p6sF6WaFo25zoIXL7j4yAC6uuE/4iyyk5WGYtTfF81ZF3O5OwirTcfacP+LMXu/GcS8Gd2q aKIRZZR5Ca17VzujBzppOEEzwI3Ta9arIm/pqSOBfMiLtfoe7gS1ekGLCBcrSeLgkiKOY167PBi4D tuLvNhDvOKgrkkMGVp5o+VgdZMeVFsu+nzn0AC6fRzOd9SWFBnZk7b/82nNX4et2mygdrJ6CM4S0O MSuNwODSUx3B2XeAFCh/I1aIQLxMuCSZCZzB9ChvOBY4p61Y/auNNr3ZoIopYkl2u6F4GKggwJEQp sON0oImStpoSxWnd9kWA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pRaj1-00FByJ-RF; Mon, 13 Feb 2023 15:26:39 +0000 Received: from mx1.tq-group.com ([93.104.207.81]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pRaix-00FBw1-KK for linux-arm-kernel@lists.infradead.org; Mon, 13 Feb 2023 15:26:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1676301995; x=1707837995; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Myql0AbsOX9YiON7G1lqeSB9ZHXoG2jmKJVBF9XTC/o=; b=YuC7H4WZehrr2O5ECxd9BCrKfAP0/JnGPj4ChgM5sHB8foX4O6lfyY8x ZvBnD/Z2pHBMDUcfJMBfwDd5zhPaCmFUvr2hvHpKrjDcQy6jvgR0zbAoy RqyEjbUsfsxHfBTcQCinjqHxkd0aijQE7baY7KAHXUpyVNlJ+3M/Qrttk DR9H5SyZrAlGCP1vH/uibx4UjTI4y/ZdReAko32XJCgrUG6lLo4VrKq4j otZd5a+wyvZ+JMK/n2l8gyKkTIXvYx3XK8D+bebwSqNqcvgdKwpJC2aFr uXqPGRmZT/VruPbhF/hrlwyFADS5fXpvY/3ZOH8W8WibKnhQ1ZJqbSuPG A==; X-IronPort-AV: E=Sophos;i="5.97,294,1669071600"; d="scan'208";a="29048553" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 13 Feb 2023 16:26:29 +0100 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Mon, 13 Feb 2023 16:26:29 +0100 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Mon, 13 Feb 2023 16:26:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1676301989; x=1707837989; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Myql0AbsOX9YiON7G1lqeSB9ZHXoG2jmKJVBF9XTC/o=; b=IhCEx5kjYziJa9SK7o38BdNzhTgGaeUqUld2ytvHLW+dVfCtpEQZ784z 59kHJ0HXtOOfsTGoncggL+Xe751ZeVOxS3OLRQCg/XUehFYDeyIlHtDKr odwhxyaA9sNOevmhlkoWcf3wHlwye/U20YMy3FottDtj8PPaOq3WHuhEO 5eMCetMMvxSxGPEyB8Yaxm/Chemk6z+yGJ0+9yd0fovCdCwiAtdwN+CwE CILZQet9cb5sSgrLi3KF+sqphPij6fxuw0yKlnZ3NPRuCeuN03blIDZQy A9/HCHZGtVaPUWJr04i+YopeDUOO+DVsVRTmaGyfgvnCT0pZVCZJ8PenU g==; X-IronPort-AV: E=Sophos;i="5.97,294,1669071600"; d="scan'208";a="29048552" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 13 Feb 2023 16:26:29 +0100 Received: from localhost.localdomain (SCHIFFERM-M2.tq-net.de [10.121.49.14]) by vtuxmail01.tq-net.de (Postfix) with ESMTPA id BF54F280056; Mon, 13 Feb 2023 16:26:28 +0100 (CET) From: Matthias Schiffer To: Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team Cc: Nicolas Pitre , Ard Biesheuvel , Linus Walleij , linux-i2c@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux@ew.tq-group.com, Matthias Schiffer Subject: [PATCH] i2c: mxs: ensure that DMA buffers are safe for DMA Date: Mon, 13 Feb 2023 16:25:50 +0100 Message-Id: <20230213152550.1776352-1-matthias.schiffer@ew.tq-group.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230213_072636_090767_B579188E X-CRM114-Status: GOOD ( 15.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We found that after commit 9c46929e7989 ("ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems"), the PCF85063 RTC driver stopped working on i.MX28 due to regmap_bulk_read() reading bogus data into a stack buffer. This is caused by the i2c-mxs driver using DMA transfers even for messages without the I2C_M_DMA_SAFE flag, and the aforementioned commit enabling vmapped stacks. As the MXS I2C controller requires DMA for reads of >4 bytes, DMA can't be disabled, so the issue is fixed by using i2c_get_dma_safe_msg_buf() to create a bounce buffer when needed. Fixes: 9c46929e7989 ("ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems") Signed-off-by: Matthias Schiffer --- drivers/i2c/busses/i2c-mxs.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c index d113bed795452..e0f3b3545cfe4 100644 --- a/drivers/i2c/busses/i2c-mxs.c +++ b/drivers/i2c/busses/i2c-mxs.c @@ -171,7 +171,7 @@ static void mxs_i2c_dma_irq_callback(void *param) } static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap, - struct i2c_msg *msg, uint32_t flags) + struct i2c_msg *msg, u8 *buf, uint32_t flags) { struct dma_async_tx_descriptor *desc; struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); @@ -226,7 +226,7 @@ static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap, } /* Queue the DMA data transfer. */ - sg_init_one(&i2c->sg_io[1], msg->buf, msg->len); + sg_init_one(&i2c->sg_io[1], buf, msg->len); dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE); desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1, DMA_DEV_TO_MEM, @@ -259,7 +259,7 @@ static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap, /* Queue the DMA data transfer. */ sg_init_table(i2c->sg_io, 2); sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1); - sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len); + sg_set_buf(&i2c->sg_io[1], buf, msg->len); dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE); desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2, DMA_MEM_TO_DEV, @@ -563,6 +563,7 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); int ret; int flags; + u8 *dma_buf; int use_pio = 0; unsigned long time_left; @@ -588,13 +589,20 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, if (ret && (ret != -ENXIO)) mxs_i2c_reset(i2c); } else { + dma_buf = i2c_get_dma_safe_msg_buf(msg, 1); + if (!dma_buf) + return -ENOMEM; + reinit_completion(&i2c->cmd_complete); - ret = mxs_i2c_dma_setup_xfer(adap, msg, flags); - if (ret) + ret = mxs_i2c_dma_setup_xfer(adap, msg, dma_buf, flags); + if (ret) { + i2c_put_dma_safe_msg_buf(dma_buf, msg, false); return ret; + } time_left = wait_for_completion_timeout(&i2c->cmd_complete, msecs_to_jiffies(1000)); + i2c_put_dma_safe_msg_buf(dma_buf, msg, true); if (!time_left) goto timeout;