From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9398B606A333 for ; Thu, 3 Dec 2026 18:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GPudxakQ Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E2E7A10EB0F; Thu, 3 Dec 2026 18:32:54 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2A2E810EB0F for ; Thu, 3 Dec 2026 18:32:49 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 13A1744431; Thu, 3 Dec 2026 18:32:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B9401F0089B; Thu, 3 Dec 2026 18:32:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796322769; bh=mEm9P6s4g+XsCQTSAP730sDh2l6vCK3tmuWBBGdSh+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GPudxakQXmHVyMEmvtdXZ4HJhu2z45OtQM+q2uPa0WZS6KKWdDE6gTRmkONfW8luo gRV0iuyK7vFFhZKs5J4Y5jXTOjhZFdI56q45mDf4/iWF3ag2FZFxtuMnd8sxdbBcDT 4OM2mxjWIfLdozokEhxFSHrzxsFuKLRGlixnofrFy8GHSx4V8wuJ+UhSKODrDDB/SW zbs1aTqYxTdqQt0zrzEyYssClVG7jyhdF9phBB5W9bUowzv9wprN0JP+PFGXgEIQVl Q/M74c/sJfGb79pkiZETTEGrMuvj1EXRh2wUJVHVh51N6qp14pRSNYNbNiyL/rTeA/ uV7AUdvU8XguA== From: Lee Jones To: lee@kernel.org, Wolfram Sang , Andi Shyti , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Carlos Munoz , =?UTF-8?q?Niklas=20S=C3=B6derlund?= , linux-renesas-soc@vger.kernel.org, linux-i2c@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 03/10] i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer() Date: Thu, 3 Dec 2026 18:31:07 +0000 Message-ID: <20261203183214.1939736-4-lee@kernel.org> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog In-Reply-To: <20261203183214.1939736-1-lee@kernel.org> References: <20261203183214.1939736-1-lee@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 In sh_mobile_xfer(), i2c_put_dma_safe_msg_buf() is called immediately after wait_event_timeout(), before checking for a timeout or an active DMA transfer and calling sh_mobile_i2c_cleanup_dma(). On a timeout or early NACK wakeup while DMA is still running, the bounce buffer is freed while still mapped for DMA. Select the DMA channel from the immutable message direction flag rather than mutable pd->dma_direction, always synchronise and terminate the channel in sh_mobile_xfer() before calling i2c_put_dma_safe_msg_buf(), re-check pd->dma_direction after dmaengine_terminate_sync() to avoid double unmapping, and update pd->pos, pd->stop_after_dma, and ICIC before clearing pd->dma_direction in sh_mobile_i2c_dma_callback(). Fixes: cebc07d84ad7 ("i2c: sh_mobile: fix leak when using DMA bounce buffer") Closes: https://sashiko.dev/#/bug/linux-af86edab-c752-40c1-9d09-2abb42f140f3 Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- drivers/i2c/busses/i2c-sh_mobile.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c index 0d10742a8ec0..d7aa710767c7 100644 --- a/drivers/i2c/busses/i2c-sh_mobile.c +++ b/drivers/i2c/busses/i2c-sh_mobile.c @@ -445,12 +445,17 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) static void sh_mobile_i2c_cleanup_dma(struct sh_mobile_i2c_data *pd, bool terminate) { - struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE + struct dma_chan *chan = (pd->msg->flags & I2C_M_RD) ? pd->dma_rx : pd->dma_tx; /* only allowed from thread context! */ - if (terminate) + if (terminate) { + if (IS_ERR_OR_NULL(chan)) + return; dmaengine_terminate_sync(chan); + if (pd->dma_direction == DMA_NONE) + return; + } dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), pd->msg->len, pd->dma_direction); @@ -462,11 +467,10 @@ static void sh_mobile_i2c_dma_callback(void *data) { struct sh_mobile_i2c_data *pd = data; - sh_mobile_i2c_cleanup_dma(pd, false); pd->pos = pd->msg->len; pd->stop_after_dma = true; - iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE); + sh_mobile_i2c_cleanup_dma(pd, false); } static struct dma_chan *sh_mobile_i2c_request_dma_chan(struct device *dev, @@ -682,15 +686,15 @@ static int sh_mobile_xfer(struct sh_mobile_i2c_data *pd, pd->sr & (ICSR_TACK | SW_DONE), pd->adap.timeout); + if (pd->dma_buf) + sh_mobile_i2c_cleanup_dma(pd, true); + /* 'stop_after_dma' tells if DMA xfer was complete */ i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, pd->stop_after_dma); } if (!time_left) { - if (pd->dma_direction != DMA_NONE) - sh_mobile_i2c_cleanup_dma(pd, true); - err = -ETIMEDOUT; break; } -- 2.56.1.911.g883d8bcb20-goog