* [PATCH v2 0/2] dmaengine: Add cleanup to simple code
@ 2025-10-03 16:26 Frank Li
2025-10-03 16:26 ` [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
2025-10-03 16:26 ` [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
0 siblings, 2 replies; 11+ messages in thread
From: Frank Li @ 2025-10-03 16:26 UTC (permalink / raw)
To: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song, Frank Li
start_dma()
{
struct dma_async_tx_descriptor *rx_cmd_desc __free(dma_async_tx_descriptor) = NULL;
...
cookie = dmaengine_submit(rx_cmd_desc);
if (dma_submit_error(cookie))
return dma_submit_error(cookie);
...
retain_and_null_ptr(rx_cmd_desc);
return 0;
}
To: Vinod Koul <vkoul@kernel.org>
To: Dong Aisheng <aisheng.dong@nxp.com>
To: Andi Shyti <andi.shyti@kernel.org>
To: Shawn Guo <shawnguo@kernel.org>
To: Sascha Hauer <s.hauer@pengutronix.de>
To: Pengutronix Kernel Team <kernel@pengutronix.de>
To: Fabio Estevam <festevam@gmail.com>
Cc: dmaengine@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-i2c@vger.kernel.org
Cc: imx@lists.linux.dev
Cc: linux-arm-kernel@lists.infradead.org
Cc: carlos.song@nxp.com
Signed-off-by: Frank Li <Frank.Li@nxp.com>
---
Changes in v2:
- fix reduntant empty line remove
- fix goto branch in i2c
- Link to v1: https://lore.kernel.org/r/20251002-dma_chan_free-v1-0-4dbf116c2b19@nxp.com
---
Frank Li (2):
dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
drivers/i2c/busses/i2c-imx-lpi2c.c | 11 +++--------
include/linux/dmaengine.h | 3 +++
2 files changed, 6 insertions(+), 8 deletions(-)
---
base-commit: 019ffdd87705bcef1ca5cf9bfdd2b358ce190ab5
change-id: 20251001-dma_chan_free-0392f4004d9e
Best regards,
--
Frank Li <Frank.Li@nxp.com>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-10-03 16:26 [PATCH v2 0/2] dmaengine: Add cleanup to simple code Frank Li
@ 2025-10-03 16:26 ` Frank Li
2025-12-03 22:19 ` Frank Li
2025-12-03 22:48 ` Dave Jiang
2025-10-03 16:26 ` [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
1 sibling, 2 replies; 11+ messages in thread
From: Frank Li @ 2025-10-03 16:26 UTC (permalink / raw)
To: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song, Frank Li
Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
cleanup and simplify error handling.
Signed-off-by: Frank Li <Frank.Li@nxp.com>
---
Check patch report exceed 100 chars, but it's still better put into one
line to keep consistent with other DEFINE_FREE and better readablity
change in v2
- remove surpoiouse remove empty line
---
include/linux/dmaengine.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
--- a/include/linux/dmaengine.h
+++ b/include/linux/dmaengine.h
@@ -5,6 +5,7 @@
#ifndef LINUX_DMAENGINE_H
#define LINUX_DMAENGINE_H
+#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/uio.h>
@@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
return desc->desc_free(desc);
}
+DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
+
/* --- DMA device --- */
int dma_async_device_register(struct dma_device *device);
--
2.34.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-10-03 16:26 [PATCH v2 0/2] dmaengine: Add cleanup to simple code Frank Li
2025-10-03 16:26 ` [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
@ 2025-10-03 16:26 ` Frank Li
2025-12-03 22:52 ` Dave Jiang
1 sibling, 1 reply; 11+ messages in thread
From: Frank Li @ 2025-10-03 16:26 UTC (permalink / raw)
To: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song, Frank Li
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
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-10-03 16:26 ` [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
@ 2025-12-03 22:19 ` Frank Li
2025-12-03 22:48 ` Dave Jiang
1 sibling, 0 replies; 11+ messages in thread
From: Frank Li @ 2025-12-03 22:19 UTC (permalink / raw)
To: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song
On Fri, Oct 03, 2025 at 12:26:49PM -0400, Frank Li wrote:
> Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
> cleanup and simplify error handling.
>
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
Vinod Koul:
Do you have chance to check this one, 3 month passed. Is it missed
at your review queue?
Frank
> Check patch report exceed 100 chars, but it's still better put into one
> line to keep consistent with other DEFINE_FREE and better readablity
>
> change in v2
> - remove surpoiouse remove empty line
> ---
> include/linux/dmaengine.h | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
> index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
> --- a/include/linux/dmaengine.h
> +++ b/include/linux/dmaengine.h
> @@ -5,6 +5,7 @@
> #ifndef LINUX_DMAENGINE_H
> #define LINUX_DMAENGINE_H
>
> +#include <linux/cleanup.h>
> #include <linux/device.h>
> #include <linux/err.h>
> #include <linux/uio.h>
> @@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
> return desc->desc_free(desc);
> }
>
> +DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
> +
> /* --- DMA device --- */
>
> int dma_async_device_register(struct dma_device *device);
>
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-10-03 16:26 ` [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
2025-12-03 22:19 ` Frank Li
@ 2025-12-03 22:48 ` Dave Jiang
2025-12-04 1:30 ` Frank Li
1 sibling, 1 reply; 11+ messages in thread
From: Dave Jiang @ 2025-12-03 22:48 UTC (permalink / raw)
To: Frank Li, Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo,
Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song
On 10/3/25 9:26 AM, Frank Li wrote:
> Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
> cleanup and simplify error handling.
>
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
> Check patch report exceed 100 chars, but it's still better put into one
> line to keep consistent with other DEFINE_FREE and better readablity
>
> change in v2
> - remove surpoiouse remove empty line
> ---
> include/linux/dmaengine.h | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
> index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
> --- a/include/linux/dmaengine.h
> +++ b/include/linux/dmaengine.h
> @@ -5,6 +5,7 @@
> #ifndef LINUX_DMAENGINE_H
> #define LINUX_DMAENGINE_H
>
> +#include <linux/cleanup.h>
> #include <linux/device.h>
> #include <linux/err.h>
> #include <linux/uio.h>
> @@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
> return desc->desc_free(desc);
> }
>
> +DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
maybe free_dma_async_tx may be clearer as the name vs dma_async_tx_descriptor.
DJ
> +
> /* --- DMA device --- */
>
> int dma_async_device_register(struct dma_device *device);
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-10-03 16:26 ` [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
@ 2025-12-03 22:52 ` Dave Jiang
2025-12-04 1:27 ` Frank Li
0 siblings, 1 reply; 11+ messages in thread
From: Dave Jiang @ 2025-12-03 22:52 UTC (permalink / raw)
To: Frank Li, Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo,
Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam
Cc: dmaengine, linux-kernel, linux-i2c, imx, linux-arm-kernel,
carlos.song
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)
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-12-03 22:52 ` Dave Jiang
@ 2025-12-04 1:27 ` Frank Li
2025-12-04 15:09 ` Dave Jiang
0 siblings, 1 reply; 11+ messages in thread
From: Frank Li @ 2025-12-04 1:27 UTC (permalink / raw)
To: Dave Jiang
Cc: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, dmaengine, linux-kernel,
linux-i2c, imx, linux-arm-kernel, carlos.song
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)
> >
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-12-03 22:48 ` Dave Jiang
@ 2025-12-04 1:30 ` Frank Li
2025-12-04 15:07 ` Dave Jiang
0 siblings, 1 reply; 11+ messages in thread
From: Frank Li @ 2025-12-04 1:30 UTC (permalink / raw)
To: Dave Jiang
Cc: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, dmaengine, linux-kernel,
linux-i2c, imx, linux-arm-kernel, carlos.song
On Wed, Dec 03, 2025 at 03:48:41PM -0700, Dave Jiang wrote:
>
>
> On 10/3/25 9:26 AM, Frank Li wrote:
> > Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
> > cleanup and simplify error handling.
> >
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > ---
> > Check patch report exceed 100 chars, but it's still better put into one
> > line to keep consistent with other DEFINE_FREE and better readablity
> >
> > change in v2
> > - remove surpoiouse remove empty line
> > ---
> > include/linux/dmaengine.h | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
> > index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
> > --- a/include/linux/dmaengine.h
> > +++ b/include/linux/dmaengine.h
> > @@ -5,6 +5,7 @@
> > #ifndef LINUX_DMAENGINE_H
> > #define LINUX_DMAENGINE_H
> >
> > +#include <linux/cleanup.h>
> > #include <linux/device.h>
> > #include <linux/err.h>
> > #include <linux/uio.h>
> > @@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
> > return desc->desc_free(desc);
> > }
> >
> > +DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
>
> maybe free_dma_async_tx may be clearer as the name vs dma_async_tx_descriptor.
If that, 'dmaengine_desc_free' is better because avoid create new name for
it.
Frank
>
> DJ
>
> > +
> > /* --- DMA device --- */
> >
> > int dma_async_device_register(struct dma_device *device);
> >
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-12-04 1:30 ` Frank Li
@ 2025-12-04 15:07 ` Dave Jiang
2025-12-16 15:01 ` Vinod Koul
0 siblings, 1 reply; 11+ messages in thread
From: Dave Jiang @ 2025-12-04 15:07 UTC (permalink / raw)
To: Frank Li
Cc: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, dmaengine, linux-kernel,
linux-i2c, imx, linux-arm-kernel, carlos.song
On 12/3/25 6:30 PM, Frank Li wrote:
> On Wed, Dec 03, 2025 at 03:48:41PM -0700, Dave Jiang wrote:
>>
>>
>> On 10/3/25 9:26 AM, Frank Li wrote:
>>> Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
>>> cleanup and simplify error handling.
>>>
>>> Signed-off-by: Frank Li <Frank.Li@nxp.com>
>>> ---
>>> Check patch report exceed 100 chars, but it's still better put into one
>>> line to keep consistent with other DEFINE_FREE and better readablity
>>>
>>> change in v2
>>> - remove surpoiouse remove empty line
>>> ---
>>> include/linux/dmaengine.h | 3 +++
>>> 1 file changed, 3 insertions(+)
>>>
>>> diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
>>> index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
>>> --- a/include/linux/dmaengine.h
>>> +++ b/include/linux/dmaengine.h
>>> @@ -5,6 +5,7 @@
>>> #ifndef LINUX_DMAENGINE_H
>>> #define LINUX_DMAENGINE_H
>>>
>>> +#include <linux/cleanup.h>
>>> #include <linux/device.h>
>>> #include <linux/err.h>
>>> #include <linux/uio.h>
>>> @@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
>>> return desc->desc_free(desc);
>>> }
>>>
>>> +DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
>>
>> maybe free_dma_async_tx may be clearer as the name vs dma_async_tx_descriptor.
>
> If that, 'dmaengine_desc_free' is better because avoid create new name for
> it.
That works too.
>
> Frank
>>
>> DJ
>>
>>> +
>>> /* --- DMA device --- */
>>>
>>> int dma_async_device_register(struct dma_device *device);
>>>
>>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-12-04 1:27 ` Frank Li
@ 2025-12-04 15:09 ` Dave Jiang
0 siblings, 0 replies; 11+ messages in thread
From: Dave Jiang @ 2025-12-04 15:09 UTC (permalink / raw)
To: Frank Li
Cc: Vinod Koul, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, dmaengine, linux-kernel,
linux-i2c, imx, linux-arm-kernel, carlos.song
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)
>>>
>>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-12-04 15:07 ` Dave Jiang
@ 2025-12-16 15:01 ` Vinod Koul
0 siblings, 0 replies; 11+ messages in thread
From: Vinod Koul @ 2025-12-16 15:01 UTC (permalink / raw)
To: Dave Jiang
Cc: Frank Li, Dong Aisheng, Andi Shyti, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, dmaengine, linux-kernel,
linux-i2c, imx, linux-arm-kernel, carlos.song
On 04-12-25, 08:07, Dave Jiang wrote:
>
>
> On 12/3/25 6:30 PM, Frank Li wrote:
> > On Wed, Dec 03, 2025 at 03:48:41PM -0700, Dave Jiang wrote:
> >>
> >>
> >> On 10/3/25 9:26 AM, Frank Li wrote:
> >>> Add cleanup FREE defines for dma_async_tx_descriptor to support automatic
> >>> cleanup and simplify error handling.
> >>>
> >>> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> >>> ---
> >>> Check patch report exceed 100 chars, but it's still better put into one
> >>> line to keep consistent with other DEFINE_FREE and better readablity
> >>>
> >>> change in v2
> >>> - remove surpoiouse remove empty line
> >>> ---
> >>> include/linux/dmaengine.h | 3 +++
> >>> 1 file changed, 3 insertions(+)
> >>>
> >>> diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
> >>> index 99efe2b9b4ea9844ca6161208362ef18ef111d96..27fa1646a807c49c781e1bce9e3e7d9a3c66f41d 100644
> >>> --- a/include/linux/dmaengine.h
> >>> +++ b/include/linux/dmaengine.h
> >>> @@ -5,6 +5,7 @@
> >>> #ifndef LINUX_DMAENGINE_H
> >>> #define LINUX_DMAENGINE_H
> >>>
> >>> +#include <linux/cleanup.h>
> >>> #include <linux/device.h>
> >>> #include <linux/err.h>
> >>> #include <linux/uio.h>
> >>> @@ -1612,6 +1613,8 @@ static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
> >>> return desc->desc_free(desc);
> >>> }
> >>>
> >>> +DEFINE_FREE(dma_async_tx_descriptor, struct dma_async_tx_descriptor *, if (_T) dmaengine_desc_free(_T))
> >>
> >> maybe free_dma_async_tx may be clearer as the name vs dma_async_tx_descriptor.
> >
> > If that, 'dmaengine_desc_free' is better because avoid create new name for
> > it.
>
> That works too.
Agree, it is better name
--
~Vinod
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2025-12-16 15:01 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-03 16:26 [PATCH v2 0/2] dmaengine: Add cleanup to simple code Frank Li
2025-10-03 16:26 ` [PATCH v2 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
2025-12-03 22:19 ` Frank Li
2025-12-03 22:48 ` Dave Jiang
2025-12-04 1:30 ` Frank Li
2025-12-04 15:07 ` Dave Jiang
2025-12-16 15:01 ` Vinod Koul
2025-10-03 16:26 ` [PATCH v2 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
2025-12-03 22:52 ` Dave Jiang
2025-12-04 1:27 ` Frank Li
2025-12-04 15:09 ` Dave Jiang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).