* [PATCH 0/2] dmaengine: Add cleanup to simple code
@ 2025-10-02 19:49 Frank Li
2025-10-02 19:49 ` [PATCH 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
2025-10-02 19:49 ` [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
0 siblings, 2 replies; 5+ messages in thread
From: Frank Li @ 2025-10-02 19:49 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>
---
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 | 4 +++-
2 files changed, 6 insertions(+), 9 deletions(-)
---
base-commit: 019ffdd87705bcef1ca5cf9bfdd2b358ce190ab5
change-id: 20251001-dma_chan_free-0392f4004d9e
Best regards,
--
Frank Li <Frank.Li@nxp.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor
2025-10-02 19:49 [PATCH 0/2] dmaengine: Add cleanup to simple code Frank Li
@ 2025-10-02 19:49 ` Frank Li
2025-10-02 19:49 ` [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
1 sibling, 0 replies; 5+ messages in thread
From: Frank Li @ 2025-10-02 19:49 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
---
include/linux/dmaengine.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
index 99efe2b9b4ea9844ca6161208362ef18ef111d96..7826b40727ab889c9167343bf15628b83b85647c 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>
@@ -1525,7 +1526,6 @@ struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
struct dma_chan *dma_request_chan(struct device *dev, const char *name);
struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name);
-
void dma_release_channel(struct dma_chan *chan);
int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
#else
@@ -1612,6 +1612,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] 5+ messages in thread
* [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-10-02 19:49 [PATCH 0/2] dmaengine: Add cleanup to simple code Frank Li
2025-10-02 19:49 ` [PATCH 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
@ 2025-10-02 19:49 ` Frank Li
2025-10-02 20:10 ` Christophe JAILLET
1 sibling, 1 reply; 5+ messages in thread
From: Frank Li @ 2025-10-02 19:49 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>
---
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-10-02 19:49 ` [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
@ 2025-10-02 20:10 ` Christophe JAILLET
2025-10-02 20:51 ` Frank Li
0 siblings, 1 reply; 5+ messages in thread
From: Christophe JAILLET @ 2025-10-02 20:10 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
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)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling
2025-10-02 20:10 ` Christophe JAILLET
@ 2025-10-02 20:51 ` Frank Li
0 siblings, 0 replies; 5+ messages in thread
From: Frank Li @ 2025-10-02 20:51 UTC (permalink / raw)
To: Christophe JAILLET
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 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)
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-10-02 20:51 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-02 19:49 [PATCH 0/2] dmaengine: Add cleanup to simple code Frank Li
2025-10-02 19:49 ` [PATCH 1/2] dmaengine: Add cleanup FREE defines for dma_async_tx_descriptor Frank Li
2025-10-02 19:49 ` [PATCH 2/2] i2c: lpi2c: Use cleanup helper for dma_async_tx_descriptor error handling Frank Li
2025-10-02 20:10 ` Christophe JAILLET
2025-10-02 20:51 ` Frank Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®