Linux I2C development
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org,
	"Pierre-Yves MORDRET" <pierre-yves.mordret@foss.st.com>,
	"Alain Volmat" <alain.volmat@foss.st.com>,
	"Andi Shyti" <andi.shyti@kernel.org>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Cedric Madianga" <cedric.madianga@gmail.com>,
	"Clément Le Goffic" <clement.legoffic@foss.st.com>,
	linux-i2c@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback()
Date: Thu,  3 Dec 2026 18:31:09 +0000	[thread overview]
Message-ID: <20261203183214.1939736-6-lee@kernel.org> (raw)
In-Reply-To: <20261203183214.1939736-1-lee@kernel.org>

stm32f7_i2c_dma_callback() can be invoked both by the DMA engine upon
transfer completion and directly by the threaded IRQ handler on NACK,
error, or timeout conditions.  When these race, dma_unmap_single() and
i2c_put_dma_safe_msg_buf() execute twice on the same buffer, causing
double DMA unmap and double free.

Guard stm32f7_i2c_dma_callback() with an atomic dma_cleanup_pending
flag so cleanup runs at most once per prepared DMA transfer, and route
threaded IRQ error/timeout paths through a helper that calls
dmaengine_terminate_sync() first so any in-flight DMA callback has
completed before proceeding or advancing to the next message.

Fixes: 6aae87fe7f18 ("i2c: stm32f7: unmap DMA mapped buffer")
Closes: https://sashiko.dev/#/bug/linux-ec58601d-83aa-4888-8bad-aa0356a7ae85
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/i2c/busses/i2c-stm32f7.c | 35 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
index 2e1cc6a92b81..1ebb2e0bc1b0 100644
--- a/drivers/i2c/busses/i2c-stm32f7.c
+++ b/drivers/i2c/busses/i2c-stm32f7.c
@@ -14,6 +14,7 @@
  * This driver is based on i2c-stm32f4.c
  *
  */
+#include <linux/atomic.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/err.h>
@@ -320,6 +321,7 @@ struct stm32f7_i2c_alert {
  * slave)
  * @dma: dma data
  * @use_dma: boolean to know if dma is used in the current transfer
+ * @dma_cleanup_pending: tracks whether a DMA transfer awaits cleanup
  * @regmap: holds SYSCFG phandle for Fast Mode Plus bits
  * @fmp_sreg: register address for setting Fast Mode Plus bits
  * @fmp_creg: register address for clearing Fast Mode Plus bits
@@ -353,6 +355,7 @@ struct stm32f7_i2c_dev {
 	bool master_mode;
 	struct stm32_i2c_dma *dma;
 	bool use_dma;
+	atomic_t dma_cleanup_pending;
 	struct regmap *regmap;
 	u32 fmp_sreg;
 	u32 fmp_creg;
@@ -735,6 +738,9 @@ static void stm32f7_i2c_dma_callback(void *arg)
 	struct stm32_i2c_dma *dma = i2c_dev->dma;
 	struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
 
+	if (!atomic_xchg(&i2c_dev->dma_cleanup_pending, 0))
+		return;
+
 	stm32f7_i2c_disable_dma_req(i2c_dev);
 	dmaengine_terminate_async(dma->chan_using);
 	dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
@@ -744,6 +750,12 @@ static void stm32f7_i2c_dma_callback(void *arg)
 	complete(&dma->dma_complete);
 }
 
+static void stm32f7_i2c_dma_abort(struct stm32f7_i2c_dev *i2c_dev)
+{
+	dmaengine_terminate_sync(i2c_dev->dma->chan_using);
+	stm32f7_i2c_dma_callback(i2c_dev);
+}
+
 static void stm32f7_i2c_hw_config(struct stm32f7_i2c_dev *i2c_dev)
 {
 	struct stm32f7_i2c_timings *t = &i2c_dev->timing;
@@ -928,12 +940,14 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
 		dma_buf = i2c_get_dma_safe_msg_buf(msg, STM32F7_I2C_DMA_LEN_MIN);
 		if (dma_buf) {
 			f7_msg->buf = dma_buf;
+			atomic_set(&i2c_dev->dma_cleanup_pending, 1);
 			ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
 						      msg->flags & I2C_M_RD,
 						      f7_msg->count, f7_msg->buf,
 						      stm32f7_i2c_dma_callback,
 						      i2c_dev);
 			if (ret) {
+				atomic_set(&i2c_dev->dma_cleanup_pending, 0);
 				dev_warn(i2c_dev->dev, "can't use DMA\n");
 				i2c_put_dma_safe_msg_buf(f7_msg->buf, msg, false);
 				f7_msg->buf = msg->buf;
@@ -1102,15 +1116,18 @@ static int stm32f7_i2c_smbus_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
 	/* Configure DMA or enable RX/TX interrupt */
 	i2c_dev->use_dma = false;
 	if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN) {
+		atomic_set(&i2c_dev->dma_cleanup_pending, 1);
 		ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
 					      cr2 & STM32F7_I2C_CR2_RD_WRN,
 					      f7_msg->count, f7_msg->buf,
 					      stm32f7_i2c_dma_callback,
 					      i2c_dev);
-		if (!ret)
+		if (!ret) {
 			i2c_dev->use_dma = true;
-		else
+		} else {
+			atomic_set(&i2c_dev->dma_cleanup_pending, 0);
 			dev_warn(i2c_dev->dev, "can't use DMA\n");
+		}
 	}
 
 	if (!i2c_dev->use_dma) {
@@ -1196,16 +1213,19 @@ static void stm32f7_i2c_smbus_rep_start(struct stm32f7_i2c_dev *i2c_dev)
 	if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN &&
 	    f7_msg->size != I2C_SMBUS_BLOCK_DATA &&
 	    f7_msg->size != I2C_SMBUS_BLOCK_PROC_CALL) {
+		atomic_set(&i2c_dev->dma_cleanup_pending, 1);
 		ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
 					      cr2 & STM32F7_I2C_CR2_RD_WRN,
 					      f7_msg->count, f7_msg->buf,
 					      stm32f7_i2c_dma_callback,
 					      i2c_dev);
 
-		if (!ret)
+		if (!ret) {
 			i2c_dev->use_dma = true;
-		else
+		} else {
+			atomic_set(&i2c_dev->dma_cleanup_pending, 0);
 			dev_warn(i2c_dev->dev, "can't use DMA\n");
+		}
 	}
 
 	if (!i2c_dev->use_dma)
@@ -1552,7 +1572,7 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,
 
 	/* Disable dma */
 	if (i2c_dev->use_dma)
-		stm32f7_i2c_dma_callback(i2c_dev);
+		stm32f7_i2c_dma_abort(i2c_dev);
 
 	i2c_dev->master_mode = false;
 	complete(&i2c_dev->complete);
@@ -1617,7 +1637,7 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 			__func__, f7_msg->addr);
 		writel_relaxed(STM32F7_I2C_ICR_NACKCF, base + STM32F7_I2C_ICR);
 		if (i2c_dev->use_dma)
-			stm32f7_i2c_dma_callback(i2c_dev);
+			stm32f7_i2c_dma_abort(i2c_dev);
 		f7_msg->result = -ENXIO;
 	}
 
@@ -1635,8 +1655,9 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 			ret = wait_for_completion_timeout(&i2c_dev->dma->dma_complete, HZ);
 			if (!ret) {
 				dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__);
-				stm32f7_i2c_dma_callback(i2c_dev);
+				stm32f7_i2c_dma_abort(i2c_dev);
 				f7_msg->result = -ETIMEDOUT;
+				f7_msg->stop = true;
 			}
 		}
 		if (f7_msg->stop) {
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03 18:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:31 [PATCH 00/10] i2c: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03 18:31 ` [PATCH 01/10] i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer() Lee Jones
2026-12-03 18:31 ` [PATCH 02/10] i2c: qcom-geni: Clear dma_buf pointer after releasing DMA buffer Lee Jones
2026-12-03 18:31 ` [PATCH 03/10] i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer() Lee Jones
2026-12-03 18:31 ` [PATCH 04/10] i2c: omap: Check buf_len before buffer accesses in data transfer helpers Lee Jones
2026-12-03 18:31 ` Lee Jones [this message]
2026-12-03 18:31 ` [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety Lee Jones
2026-12-03 18:31 ` [PATCH 07/10] i2c: usbio: Validate full reply length in split-read loop Lee Jones
2026-12-03 18:31 ` [PATCH 08/10] i2c: amd-mp2: Synchronise IRQ completion with command timeout Lee Jones
2026-12-03 18:31 ` [PATCH 09/10] i2c: mlxbf: Reject zero-length SMBus reads to prevent stack overflow Lee Jones
2026-12-03 18:31 ` [PATCH 10/10] i2c: octeon: Guard to_pci_dev() with dev_is_pci() in octeon_i2c_set_clock() Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203183214.1939736-6-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=alain.volmat@foss.st.com \
    --cc=alexandre.torgue@foss.st.com \
    --cc=andi.shyti@kernel.org \
    --cc=cedric.madianga@gmail.com \
    --cc=clement.legoffic@foss.st.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=pierre-yves.mordret@foss.st.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox