Skip to contentSkip to conversation

i2c: mxs: fix DMA channel leak on probe error

PATCH Applied Ruoyu Wang 4 participants 4 messages Aug 15 – Aug 19

Subsystems (2)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH v2 1/1 i2c: mxs: fix DMA channel leak on probe error

mxs_i2c_probe() requests an exclusive DMA channel before resetting the controller and registering the I2C adapter. If either later operation fails, probe returns without releasing the channel because the remove callback is not invoked after a failed probe.

Use devm_dma_request_chan() so the device core releases the channel on probe failure and driver detach. Remove the manual release from the remove callback because the channel is now device-managed.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 62885f59a261 ("MXS: Implement DMA support into mxs-i2c")
Assisted-by unnamed:claude-opus-4.8 typestate

---
Changes in v2:
- Use devm_dma_request_chan() instead of explicit error unwinding.
- Remove the now-redundant manual release in remove().
- Add the Assisted-by tag.

v1: https://lore.kernel.org/r/[email protected]/

---
 drivers/i2c/busses/i2c-mxs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/i2c/busses/i2c-mxs.c +1 -4
index 4e07babea9c3f4..eee4fdcd9df31a 100644
@@ -839,7 +839,7 @@ static int mxs_i2c_probe(struct platform_device *pdev)
 	}
 
 	/* Setup the DMA */
-	i2c->dmach = dma_request_chan(dev, "rx-tx");
+	i2c->dmach = devm_dma_request_chan(dev, "rx-tx");
 	if (IS_ERR(i2c->dmach)) {
 		return dev_err_probe(dev, PTR_ERR(i2c->dmach),
 				     "Failed to request dma\n");
@@ -877,9 +877,6 @@ static void mxs_i2c_remove(struct platform_device *pdev)
 
 	i2c_del_adapter(&i2c->adapter);
 
-	if (i2c->dmach)
-		dma_release_channel(i2c->dmach);
-
 	writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
 }
 
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] On a DMA transfer timeout, the active DMA bounce buffer is improperly freed before unmapping and terminating the DMA engine, leading to a use-after-free and potential memory corruption.
- [High] The DMA completion callback signals the waiting thread before unmapping the DMA buffer, causing a race condition where the CPU may read stale data and the buffer may be freed before unmapping completes. --

--- Patch [1]: [PATCH v2] i2c: mxs: fix DMA channel leak on probe error --- commit 3a30f1ffa0258833921bc6e721cb282f66cc45c7
Author: Ruoyu Wang <[email protected]>
i2c: mxs: fix DMA channel leak on probe error

This commit fixes a DMA channel leak in mxs_i2c_probe() by using devm_dma_request_chan(). This ensures the device core releases the channel on probe failure and driver detach, removing the need for manual release in the remove callback.

drivers/i2c/busses/i2c-mxs.c static int mxs_i2c_probe(struct platform_device *pdev) · line 842

Quoted 10 lines
drivers/i2c/busses/i2c-mxs.c +1 -1
index 4e07babea9c3f..eee4fdcd9df31 100644
@@ -839,7 +839,7 @@ static int mxs_i2c_probe(struct platform_device *pdev)
 	}
 
 	/* Setup the DMA */
-	i2c->dmach = dma_request_chan(dev, "rx-tx");
+	i2c->dmach = devm_dma_request_chan(dev, "rx-tx");

[Severity: High]
This is a pre-existing issue, but does the timeout path in mxs_i2c_xfer_msg() free the active DMA bounce buffer before terminating the DMA engine?

In mxs_i2c_xfer_msg(), if the completion times out:

		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;

The bounce buffer is freed by i2c_put_dma_safe_msg_buf(). The code then jumps to the timeout label, which calls mxs_i2c_dma_finish() to unmap the scatterlist but does not call dmaengine_terminate_sync().

Could this leave the DMA hardware active and able to write into the freed memory?

Additionally, a late DMA completion could cause mxs_i2c_dma_irq_callback() to execute, resulting in a second call to mxs_i2c_dma_finish() and a double-unmap of the scatterlist.

[Severity: High]
This is also a pre-existing issue, but does the DMA completion callback signal the waiting thread before unmapping the DMA buffer?

Looking at mxs_i2c_dma_irq_callback():

static void mxs_i2c_dma_irq_callback(void *param)

{
	struct mxs_i2c_dev *i2c = param;

	complete(&i2c->cmd_complete);
	mxs_i2c_dma_finish(i2c);
}

By calling complete() before mxs_i2c_dma_finish(), the waiting thread in mxs_i2c_xfer_msg() is awakened and proceeds to call i2c_put_dma_safe_msg_buf().

Since the unmap has not occurred yet, the CPU cache may not be invalidated. Could this result in the CPU reading stale cached data during the memcpy from the bounce buffer instead of the actual data received from the bus?

Quoted 3 lines
drivers/i2c/busses/i2c-mxs.c +0 -0
 	if (IS_ERR(i2c->dmach)) {
 		return dev_err_probe(dev, PTR_ERR(i2c->dmach),
 				     "Failed to request dma\n");

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Ruoyu Wang
Quoted 9 lines · Ruoyu Wang

mxs_i2c_probe() requests an exclusive DMA channel before resetting the controller and registering the I2C adapter. If either later operation fails, probe returns without releasing the channel because the remove callback is not invoked after a failed probe.

Use devm_dma_request_chan() so the device core releases the channel on probe failure and driver detach. Remove the manual release from the remove callback because the channel is now device-managed.

Quoted 8 lines

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 62885f59a261 ("MXS: Implement DMA support into mxs-i2c")
Assisted-by unnamed:claude-opus-4.8 typestate

---

Quoted 37 lines

Changes in v2:
- Use devm_dma_request_chan() instead of explicit error unwinding.
- Remove the now-redundant manual release in remove().
- Add the Assisted-by tag.

v1: https://lore.kernel.org/r/[email protected]/

---
 drivers/i2c/busses/i2c-mxs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/i2c/busses/i2c-mxs.c +1 -5
index 4e07babea9c3f4..eee4fdcd9df31a 100644
@@ -839,7 +839,7 @@ static int mxs_i2c_probe(struct platform_device *pdev)
 	}
 
 	/* Setup the DMA */
-	i2c->dmach = dma_request_chan(dev, "rx-tx");
+	i2c->dmach = devm_dma_request_chan(dev, "rx-tx");
 	if (IS_ERR(i2c->dmach)) {
 		return dev_err_probe(dev, PTR_ERR(i2c->dmach),
 				     "Failed to request dma\n");
@@ -877,9 +877,6 @@ static void mxs_i2c_remove(struct platform_device *pdev)
 
 	i2c_del_adapter(&i2c->adapter);
 
-	if (i2c->dmach)
-		dma_release_channel(i2c->dmach);
-
 	writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
 }
 
--

2.51.0

maintainer Applied
#3 lore raw
In reply to Ruoyu Wang

Hi Ruoyu,

Quoted 9 lines · Ruoyu Wang

mxs_i2c_probe() requests an exclusive DMA channel before resetting the controller and registering the I2C adapter. If either later operation fails, probe returns without releasing the channel because the remove callback is not invoked after a failed probe.

Use devm_dma_request_chan() so the device core releases the channel on probe failure and driver detach. Remove the manual release from the remove callback because the channel is now device-managed.

Quoted 6 lines

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 62885f59a261 ("MXS: Implement DMA support into mxs-i2c")
Assisted-by unnamed:claude-opus-4.8 typestate

merged to i2c/i2c-2.

Thanks,
Andi