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

Frank Li posted 2 patches 2 months, 2 weeks ago
[PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Frank Li 2 months, 2 weeks 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>
---
change v2
 need goto unmap buffer.

desc_prepare_err_exit:
        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)

auto cleanup by __free, so whole error path submit_err_exit can be removed
and use desc_prepare_err_exit label.
---
 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..e31c2d81f59f18a09d34f79896a7fa4bb83c0a12 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;
+		goto desc_prepare_err_exit;
 	}
 
 	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 v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Dave Jiang 1 week, 6 days ago

On 10/3/25 9:26 AM, Frank Li wrote:
> Use the cleanup helper to simplify dma_async_tx_descriptor error handling.
> 
> No functional change.
> 
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
> change v2
>  need goto unmap buffer.
> 
> desc_prepare_err_exit:
>         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)
> 
> auto cleanup by __free, so whole error path submit_err_exit can be removed
> and use desc_prepare_err_exit label.
> ---
>  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..e31c2d81f59f18a09d34f79896a7fa4bb83c0a12 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;
> +		goto desc_prepare_err_exit;

Typically if you are introducing cleanup usage, you will want to remove all the goto usages. Otherwise it gets kinda messy having both styles. Although not sure how to do a cleanup macro with dma_unmap_*. Unless a local one where lpi2c_imx_dma also contains a ptr to the device. Then that may be doable. 

DJ>  	}
>  
>  	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)
>
Re: [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Frank Li 1 week, 6 days ago
On Wed, Dec 03, 2025 at 03:52:09PM -0700, Dave Jiang wrote:
>
>
> On 10/3/25 9:26 AM, Frank Li wrote:
> > Use the cleanup helper to simplify dma_async_tx_descriptor error handling.
> >
> > No functional change.
> >
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > ---
> > change v2
> >  need goto unmap buffer.
> >
> > desc_prepare_err_exit:
> >         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)
> >
> > auto cleanup by __free, so whole error path submit_err_exit can be removed
> > and use desc_prepare_err_exit label.
> > ---
> >  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..e31c2d81f59f18a09d34f79896a7fa4bb83c0a12 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;
> > +		goto desc_prepare_err_exit;
>
> Typically if you are introducing cleanup usage, you will want to remove all the goto usages. Otherwise it gets kinda messy having both styles. Although not sure how to do a cleanup macro with dma_unmap_*. Unless a local one where lpi2c_imx_dma also contains a ptr to the device. Then that may be doable.

dma_unmap() patch at
https://lore.kernel.org/imx/aPZbuY5bLpkoO2J%2F@lizhi-Precision-Tower-5810/

The first patch need be pickup and dma umap firstly.  Then I can remove all
goto.

Frank

>
> DJ>  	}
> >
> >  	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)
> >
>
Re: [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
Posted by Dave Jiang 1 week, 6 days ago

On 12/3/25 6:27 PM, Frank Li wrote:
> On Wed, Dec 03, 2025 at 03:52:09PM -0700, Dave Jiang wrote:
>>
>>
>> On 10/3/25 9:26 AM, Frank Li wrote:
>>> Use the cleanup helper to simplify dma_async_tx_descriptor error handling.
>>>
>>> No functional change.
>>>
>>> Signed-off-by: Frank Li <Frank.Li@nxp.com>
>>> ---
>>> change v2
>>>  need goto unmap buffer.
>>>
>>> desc_prepare_err_exit:
>>>         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)
>>>
>>> auto cleanup by __free, so whole error path submit_err_exit can be removed
>>> and use desc_prepare_err_exit label.
>>> ---
>>>  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..e31c2d81f59f18a09d34f79896a7fa4bb83c0a12 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;
>>> +		goto desc_prepare_err_exit;
>>
>> Typically if you are introducing cleanup usage, you will want to remove all the goto usages. Otherwise it gets kinda messy having both styles. Although not sure how to do a cleanup macro with dma_unmap_*. Unless a local one where lpi2c_imx_dma also contains a ptr to the device. Then that may be doable.
> 
> dma_unmap() patch at
> https://lore.kernel.org/imx/aPZbuY5bLpkoO2J%2F@lizhi-Precision-Tower-5810/
> 
> The first patch need be pickup and dma umap firstly.  Then I can remove all
> goto.

perfect


> 
> Frank
> 
>>
>> DJ>  	}
>>>
>>>  	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)
>>>
>>
>