[PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling

Frank Li posted 2 patches 4 months, 1 week ago
There is a newer version of this series
[PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Frank Li 4 months, 1 week ago
Use the cleanup helper to simplify dma_async_tx_descriptor error handling.

No functional change.

Signed-off-by: Frank Li <Frank.Li@nxp.com>
---
 drivers/i2c/busses/i2c-imx-lpi2c.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
index 03b5a7e8c361abe1d75fb4d31f9614bbc6387d93..b1d035ca8002e9648b67bfe4d674578373394710 100644
--- a/drivers/i2c/busses/i2c-imx-lpi2c.c
+++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
@@ -722,7 +722,7 @@ static void lpi2c_dma_callback(void *data)
 
 static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
 {
-	struct dma_async_tx_descriptor *rx_cmd_desc;
+	struct dma_async_tx_descriptor *rx_cmd_desc __free(dma_async_tx_descriptor) = NULL;
 	struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
 	struct dma_chan *txchan = dma->chan_tx;
 	dma_cookie_t cookie;
@@ -746,10 +746,11 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
 	cookie = dmaengine_submit(rx_cmd_desc);
 	if (dma_submit_error(cookie)) {
 		dev_err(&lpi2c_imx->adapter.dev, "submitting DMA failed, use pio\n");
-		goto submit_err_exit;
+		return dma_submit_error(cookie);
 	}
 
 	dma_async_issue_pending(txchan);
+	retain_and_null_ptr(rx_cmd_desc);
 
 	return 0;
 
@@ -757,12 +758,6 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
 	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
 			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
 	return -EINVAL;
-
-submit_err_exit:
-	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
-			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
-	dmaengine_desc_free(rx_cmd_desc);
-	return -EINVAL;
 }
 
 static int lpi2c_dma_submit(struct lpi2c_imx_struct *lpi2c_imx)

-- 
2.34.1
Re: [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Christophe JAILLET 4 months, 1 week ago
Le 02/10/2025 à 21:49, Frank Li a écrit :
> Use the cleanup helper to simplify dma_async_tx_descriptor error handling.
> 
> No functional change.
> 
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
>   drivers/i2c/busses/i2c-imx-lpi2c.c | 11 +++--------
>   1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
> index 03b5a7e8c361abe1d75fb4d31f9614bbc6387d93..b1d035ca8002e9648b67bfe4d674578373394710 100644
> --- a/drivers/i2c/busses/i2c-imx-lpi2c.c
> +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
> @@ -722,7 +722,7 @@ static void lpi2c_dma_callback(void *data)
>   
>   static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
>   {
> -	struct dma_async_tx_descriptor *rx_cmd_desc;
> +	struct dma_async_tx_descriptor *rx_cmd_desc __free(dma_async_tx_descriptor) = NULL;
>   	struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
>   	struct dma_chan *txchan = dma->chan_tx;
>   	dma_cookie_t cookie;
> @@ -746,10 +746,11 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
>   	cookie = dmaengine_submit(rx_cmd_desc);
>   	if (dma_submit_error(cookie)) {
>   		dev_err(&lpi2c_imx->adapter.dev, "submitting DMA failed, use pio\n");
> -		goto submit_err_exit;
> +		return dma_submit_error(cookie);

I don't know if it matters or not, but this may change the returned 
value in this error handling path.

>   	}
>   
>   	dma_async_issue_pending(txchan);
> +	retain_and_null_ptr(rx_cmd_desc);
>   
>   	return 0;
>   
> @@ -757,12 +758,6 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
>   	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
>   			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
>   	return -EINVAL;
> -
> -submit_err_exit:
> -	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
> -			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);

dma_unmap_single() is not needed anymore?

> -	dmaengine_desc_free(rx_cmd_desc);
> -	return -EINVAL;
>   }
>   
>   static int lpi2c_dma_submit(struct lpi2c_imx_struct *lpi2c_imx)
> 

Re: [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Frank Li 4 months, 1 week ago
On Thu, Oct 02, 2025 at 10:10:16PM +0200, Christophe JAILLET wrote:
> Le 02/10/2025 à 21:49, Frank Li a écrit :
> > Use the cleanup helper to simplify dma_async_tx_descriptor error handling.
> >
> > No functional change.
> >
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > ---
> >   drivers/i2c/busses/i2c-imx-lpi2c.c | 11 +++--------
> >   1 file changed, 3 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
> > index 03b5a7e8c361abe1d75fb4d31f9614bbc6387d93..b1d035ca8002e9648b67bfe4d674578373394710 100644
> > --- a/drivers/i2c/busses/i2c-imx-lpi2c.c
> > +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
> > @@ -722,7 +722,7 @@ static void lpi2c_dma_callback(void *data)
> >   static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
> >   {
> > -	struct dma_async_tx_descriptor *rx_cmd_desc;
> > +	struct dma_async_tx_descriptor *rx_cmd_desc __free(dma_async_tx_descriptor) = NULL;
> >   	struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
> >   	struct dma_chan *txchan = dma->chan_tx;
> >   	dma_cookie_t cookie;
> > @@ -746,10 +746,11 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
> >   	cookie = dmaengine_submit(rx_cmd_desc);
> >   	if (dma_submit_error(cookie)) {
> >   		dev_err(&lpi2c_imx->adapter.dev, "submitting DMA failed, use pio\n");
> > -		goto submit_err_exit;
> > +		return dma_submit_error(cookie);
>
> I don't know if it matters or not, but this may change the returned value in
> this error handling path.
>
> >   	}
> >   	dma_async_issue_pending(txchan);
> > +	retain_and_null_ptr(rx_cmd_desc);
> >   	return 0;
> > @@ -757,12 +758,6 @@ static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
> >   	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
> >   			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
> >   	return -EINVAL;
> > -
> > -submit_err_exit:
> > -	dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
> > -			 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
>
> dma_unmap_single() is not needed anymore?

Previous label position should be before submit_err_exit to avoid
duplicate dma_unmap_single() twice at error path. After remove
submit_err_exit, needn't dma_unmap_single() here.

But previous return dma_submit_error(cookie) is wrong, should be
goto desc_prepare_err_exit;

Frank
>
> > -	dmaengine_desc_free(rx_cmd_desc);
> > -	return -EINVAL;
> >   }
> >   static int lpi2c_dma_submit(struct lpi2c_imx_struct *lpi2c_imx)
> >
>