Skip to contentSkip to conversation

i2c: tegra: Fix failure during probe deferral cleanup

PATCH Superseded Thierry Reding 2 participants 3 messages Jun 8, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: tegra: Fix failure during probe deferral cleanup

From: Thierry Reding <[email protected]>

If the driver fails to obtain a DMA channel, it will initiate cleanup and try to release the DMA channel that couldn't be retrieved. This will cause a crash because the cleanup will try to dereference an ERR_PTR()- encoded error code.

However, there's nothing to clean up at this point yet, so we can avoid this by simply propagating the error code.

Fixes fcc8a89a1c83 ("i2c: tegra: Share same DMA channel for RX and TX")
---
 drivers/i2c/busses/i2c-tegra.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-tegra.c +2 -4
index f155e9028f94..0eab199900ae 100644
@@ -458,10 +458,8 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
 	 * with existing devicetrees.
 	 */
 	i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
-	if (IS_ERR(i2c_dev->dma_chan)) {
-		err = PTR_ERR(i2c_dev->dma_chan);
-		goto err_out;
-	}
+	if (IS_ERR(i2c_dev->dma_chan))
+		return PTR_ERR(i2c_dev->dma_chan);
 
 	i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev;
 	i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
Signature · 1 line
2.40.1
maintainer
#1 lore raw

Hi Thierry,

Quoted 21 lines · Thierry Reding

From: Thierry Reding <[email protected]>

If the driver fails to obtain a DMA channel, it will initiate cleanup and try to release the DMA channel that couldn't be retrieved. This will cause a crash because the cleanup will try to dereference an ERR_PTR()- encoded error code.

However, there's nothing to clean up at this point yet, so we can avoid this by simply propagating the error code.

Fixes fcc8a89a1c83 ("i2c: tegra: Share same DMA channel for RX and TX")
---
 drivers/i2c/busses/i2c-tegra.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-tegra.c +0 -0
index f155e9028f94..0eab199900ae 100644

drivers/i2c/busses/i2c-tegra.c static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) · line 462

Quoted 9 lines
drivers/i2c/busses/i2c-tegra.c +2 -4
@@ -458,10 +458,8 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
 	 * with existing devicetrees.
 	 */
 	i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
-	if (IS_ERR(i2c_dev->dma_chan)) {
-		err = PTR_ERR(i2c_dev->dma_chan);
-		goto err_out;
-	}
+	if (IS_ERR(i2c_dev->dma_chan))
+		return PTR_ERR(i2c_dev->dma_chan);

Actually you are ignoring the case when the driver would use programmed i/o only as a backup plan.

I think a possible fix could be:

drivers/i2c/busses/i2c-tegra.c +2 -1
@@ -460,7 +460,7 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
        i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
        if (IS_ERR(i2c_dev->dma_chan)) {
                err = PTR_ERR(i2c_dev->dma_chan);
-               goto err_out;
+               goto err_probe_defer;
        }
 
        i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev;
@@ -482,6 +482,7 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
 
 err_out:
        tegra_i2c_release_dma(i2c_dev);
+err_probe_defer:
        if (err != -EPROBE_DEFER) {
                dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
                dev_err(i2c_dev->dev, "falling back to PIO\n");

Thank you,
Andi

(BTW, those are not dev_err's but they should be either dev_info or dev_warn())

Quoted 6 lines
drivers/i2c/busses/i2c-tegra.c +0 -0
 	i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev;
 	i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
Signature · 1 line
2.40.1
#2 lore raw
Quoted 28 lines · Andi Shyti

Hi Thierry,

Quoted 25 lines · Thierry Reding

From: Thierry Reding <[email protected]>

If the driver fails to obtain a DMA channel, it will initiate cleanup and try to release the DMA channel that couldn't be retrieved. This will cause a crash because the cleanup will try to dereference an ERR_PTR()- encoded error code.

However, there's nothing to clean up at this point yet, so we can avoid this by simply propagating the error code.

Fixes fcc8a89a1c83 ("i2c: tegra: Share same DMA channel for RX and TX")
---
 drivers/i2c/busses/i2c-tegra.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-tegra.c +0 -1
index f155e9028f94..0eab199900ae 100644
@@ -458,10 +458,8 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
 	 * with existing devicetrees.
 	 */
 	i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
-	if (IS_ERR(i2c_dev->dma_chan)) {
Quoted 8 lines

drivers/i2c/busses/i2c-tegra.c static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) · line 462

Quoted 5 lines
drivers/i2c/busses/i2c-tegra.c +2 -3
-		err = PTR_ERR(i2c_dev->dma_chan);
-		goto err_out;
-	}
+	if (IS_ERR(i2c_dev->dma_chan))
+		return PTR_ERR(i2c_dev->dma_chan);

Actually you are ignoring the case when the driver would use programmed i/o only as a backup plan.

Good point.

Quoted 14 lines

I think a possible fix could be:

drivers/i2c/busses/i2c-tegra.c +1 -1
@@ -460,7 +460,7 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
        i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
        if (IS_ERR(i2c_dev->dma_chan)) {
                err = PTR_ERR(i2c_dev->dma_chan);
-               goto err_out;
+               goto err_probe_defer;
        }
 
        i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev;

drivers/i2c/busses/i2c-tegra.c static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) · line 488

Quoted 7 lines
drivers/i2c/busses/i2c-tegra.c +1 -0
@@ -482,6 +482,7 @@ static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
 
 err_out:
        tegra_i2c_release_dma(i2c_dev);
+err_probe_defer:
        if (err != -EPROBE_DEFER) {
                dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
                dev_err(i2c_dev->dev, "falling back to PIO\n");

I think it's even simpler. I'll send out a v2 that simply resets i2c_dev->dma_chan to NULL when an error occurs. That way any subsequent code will already know to ignore it. Another case where this could happen is during regular cleanup (i.e. driver unbind), and resetting to NULL should fix that as well.

Quoted 6 lines

Thank you,
Andi

(BTW, those are not dev_err's but they should be either dev_info or dev_warn())

Yeah. I can send up a follow-up patch to change these to dev_warn(). I think dev_info() is perhaps a bit too weak, since we really do want to use DMA if at all possible. Any case where it can't be used would be a configuration problem. I think I recall that we only have this in the driver for backwards-compatibility with old device trees that may not have had the DMA channels specified, so this serves as a reminder that the DT should be updated.

Thierry