* [PATCH 01/10] i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer()
[not found] <20261203183214.1939736-1-lee@kernel.org>
@ 2026-12-03 18:31 ` Lee Jones
2026-12-03 20:36 ` Frank Li
2026-12-03 18:31 ` [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback() Lee Jones
2026-12-03 18:31 ` [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety Lee Jones
2 siblings, 1 reply; 4+ messages in thread
From: Lee Jones @ 2026-12-03 18:31 UTC (permalink / raw)
To: lee, Dong Aisheng, Andi Shyti, Frank Li, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Carlos Song, Gao Pan,
linux-i2c, imx, linux-arm-kernel
Cc: linux-kernel, stable
lpi2c_imx->dma persists across transfers. On a DMA read, the
disable_dma path frees dma->rx_cmd_buf without clearing the pointer.
If a subsequent DMA read fails in lpi2c_dma_config() or
lpi2c_dma_submit() before lpi2c_imx_alloc_rx_cmd_buf() runs, the
cleanup path calls kfree(dma->rx_cmd_buf) a second time on the stale
pointer.
Reset dma->rx_cmd_buf to NULL immediately after freeing it.
Fixes: a09c8b3f9047 ("i2c: imx-lpi2c: add eDMA mode support for LPI2C")
Closes: https://sashiko.dev/#/bug/linux-17d925e1-8c99-4efb-a920-2c90ce2b53b5
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/i2c/busses/i2c-imx-lpi2c.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
index ecf2bed01b29..f911b3b7367e 100644
--- a/drivers/i2c/busses/i2c-imx-lpi2c.c
+++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
@@ -1097,8 +1097,10 @@ static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,
/* Disable I2C DMA function */
writel(0, lpi2c_imx->base + LPI2C_MDER);
- if (dma->dma_msg_flag & I2C_M_RD)
+ if (dma->dma_msg_flag & I2C_M_RD) {
kfree(dma->rx_cmd_buf);
+ dma->rx_cmd_buf = NULL;
+ }
if (ret)
i2c_put_dma_safe_msg_buf(dma->dma_buf, msg, false);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback()
[not found] <20261203183214.1939736-1-lee@kernel.org>
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 ` Lee Jones
2026-12-03 18:31 ` [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety Lee Jones
2 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-12-03 18:31 UTC (permalink / raw)
To: lee, Pierre-Yves MORDRET, Alain Volmat, Andi Shyti,
Maxime Coquelin, Alexandre Torgue, Cedric Madianga,
Clément Le Goffic, linux-i2c, linux-stm32, linux-arm-kernel
Cc: linux-kernel, stable
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety
[not found] <20261203183214.1939736-1-lee@kernel.org>
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 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback() Lee Jones
@ 2026-12-03 18:31 ` Lee Jones
2 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-12-03 18:31 UTC (permalink / raw)
To: lee, Pierre-Yves MORDRET, Alain Volmat, Andi Shyti,
Maxime Coquelin, Alexandre Torgue, Cedric Madianga, Wolfram Sang,
linux-i2c, linux-stm32, linux-arm-kernel
Cc: linux-kernel, stable
smbus_buf is embedded directly inside struct stm32f7_i2c_msg (and thus
struct stm32f7_i2c_dev), sharing a CPU cacheline with adjacent driver
state. Passing it to dma_map_single() on non-coherent ARM/ARM64 SoCs
can corrupt adjacent fields during cache invalidation or corrupt DMA
data via concurrent CPU writes.
Allocate smbus_buf separately with devm_kzalloc() during probe so it is
isolated in its own DMA-safe allocation.
Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
Closes: https://sashiko.dev/#/bug/linux-0de0fdd3-8e61-4777-a456-ad4761a2207c
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/i2c/busses/i2c-stm32f7.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
index 1ebb2e0bc1b0..720e1d46da09 100644
--- a/drivers/i2c/busses/i2c-stm32f7.c
+++ b/drivers/i2c/busses/i2c-stm32f7.c
@@ -285,7 +285,7 @@ struct stm32f7_i2c_msg {
bool smbus;
int size;
char read_write;
- u8 smbus_buf[I2C_SMBUS_BLOCK_MAX + 3] __aligned(4);
+ u8 *smbus_buf;
};
/**
@@ -2186,6 +2186,12 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)
if (!i2c_dev)
return -ENOMEM;
+ i2c_dev->f7_msg.smbus_buf = devm_kzalloc(&pdev->dev,
+ I2C_SMBUS_BLOCK_MAX + 3,
+ GFP_KERNEL);
+ if (!i2c_dev->f7_msg.smbus_buf)
+ return -ENOMEM;
+
setup = of_device_get_match_data(&pdev->dev);
if (!setup)
return dev_err_probe(&pdev->dev, -ENODEV, "Can't get device data\n");
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 01/10] i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer()
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 20:36 ` Frank Li
0 siblings, 0 replies; 4+ messages in thread
From: Frank Li @ 2026-12-03 20:36 UTC (permalink / raw)
To: Lee Jones
Cc: Dong Aisheng, Andi Shyti, Frank Li, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Carlos Song, Gao Pan,
linux-i2c, imx, linux-arm-kernel, linux-kernel, stable
On Thu, Dec 03, 2026 at 06:31:05PM +0000, Lee Jones wrote:
> lpi2c_imx->dma persists across transfers. On a DMA read, the
> disable_dma path frees dma->rx_cmd_buf without clearing the pointer.
> If a subsequent DMA read fails in lpi2c_dma_config() or
> lpi2c_dma_submit() before lpi2c_imx_alloc_rx_cmd_buf() runs, the
> cleanup path calls kfree(dma->rx_cmd_buf) a second time on the stale
> pointer.
>
> Reset dma->rx_cmd_buf to NULL immediately after freeing it.
>
> Fixes: a09c8b3f9047 ("i2c: imx-lpi2c: add eDMA mode support for LPI2C")
> Closes: https://sashiko.dev/#/bug/linux-17d925e1-8c99-4efb-a920-2c90ce2b53b5
> Cc: stable@vger.kernel.org
> Signed-off-by: Lee Jones <lee@kernel.org>
> ---
Reviewed-by: Frank Li <Frank.Li@nxp.com>
> drivers/i2c/busses/i2c-imx-lpi2c.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
> index ecf2bed01b29..f911b3b7367e 100644
> --- a/drivers/i2c/busses/i2c-imx-lpi2c.c
> +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
> @@ -1097,8 +1097,10 @@ static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,
> /* Disable I2C DMA function */
> writel(0, lpi2c_imx->base + LPI2C_MDER);
>
> - if (dma->dma_msg_flag & I2C_M_RD)
> + if (dma->dma_msg_flag & I2C_M_RD) {
> kfree(dma->rx_cmd_buf);
> + dma->rx_cmd_buf = NULL;
> + }
>
> if (ret)
> i2c_put_dma_safe_msg_buf(dma->dma_buf, msg, false);
> --
> 2.56.1.911.g883d8bcb20-goog
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 20:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261203183214.1939736-1-lee@kernel.org>
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 20:36 ` Frank Li
2026-12-03 18:31 ` [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback() Lee Jones
2026-12-03 18:31 ` [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety Lee Jones
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox