From: Frank Li <Frank.li@nxp.com>
To: Leon Romanovsky <leon@kernel.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>,
Robin Murphy <robin.murphy@arm.com>,
Dong Aisheng <aisheng.dong@nxp.com>,
Andi Shyti <andi.shyti@kernel.org>,
Shawn Guo <shawnguo@kernel.org>,
Sascha Hauer <s.hauer@pengutronix.de>,
Pengutronix Kernel Team <kernel@pengutronix.de>,
Fabio Estevam <festevam@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
linux-kernel@vger.kernel.org, iommu@lists.linux.dev,
linux-i2c@vger.kernel.org, imx@lists.linux.dev,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH 0/3] dma_mapping: Add auto cleanup support
Date: Mon, 20 Oct 2025 11:56:41 -0400 [thread overview]
Message-ID: <aPZbuY5bLpkoO2J/@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20251019120304.GG6199@unreal>
On Sun, Oct 19, 2025 at 03:03:04PM +0300, Leon Romanovsky wrote:
> On Tue, Oct 14, 2025 at 07:54:46AM +0200, Marek Szyprowski wrote:
> > Hi
> >
> > On 10.10.2025 20:50, Frank Li wrote:
> > > There are many below pattern
> > >
> > > fun()
> > > {
> > > ...
> > > dma_map_single();
> > > if (dma_mapping_error)
> > > goto err1;
> > >
> > > dmaengine_prep_slave_single()
> > > if (...)
> > > goto err2
> > >
> > > dmaengine_submit()
> > > if (...)
> > > goto err3
> > >
> > > wait_for_completion_timeout()
> > > if (...)
> > > goto err4
> > >
> > > err4:
> > > err3:
> > > err2:
> > > dma_umap_single();
> > > err1:
> > > }
> > >
> > > Use cleanup can simple error handle like guard(), such as guard(mutex).
> > > or __free(kfree) = kmalloc.
> > >
> > > But dma_umap_single() need more argurements. So situation below complex.
> > >
> > > It need pack argurments list into structure.
> > >
> > > #define __DEFINE_GUARD_CLASS(_name, _return_type, _list_class_fields) \
> > > typedef struct { \
> > > _return_type ret; \
> > > bool okay; \
> > > struct { \
> > > __REMOVE(_list_class_fields); \
> > > } args; \
> > > } class_##_name##_t;
> > >
> > > So save all arugments to it.
> > >
> > > __DEFINE_GUARD_CLASS(dma_map_single, dma_addr_t, (struct device *dev; void *ptr; size_t size; enum dma_data_direction dir)
> > > will expand to
> > >
> > > struct {
> > > dma_addr_t ret;
> > > bool okay;
> > > struct {
> > > struct device *dev;
> > > void *ptr;
> > > size_t size;
> > > enum dma_data_direction dir;
> > > }
> > > }
> > >
> > > So cleanup function can use saved argurement.
> > >
> > > The above fun will be
> > >
> > > fun()
> > > {
> > > CLASS(dma_map_single, dma)(dev, ...);
> > >
> > > ...
> > > if (...)
> > > return err;
> > > }
> > >
> > > if funtion return, which need keep map,
> > >
> > > submit()
> > > {
> > > dma_map_single();
> > > ...
> > > dmaengine_submit();
> > > if (...)
> > > goto err1
> > > return;
> > >
> > > goto err1:
> > > dma_umap_single();
> > > }
> > >
> > > Macro retain_and_empty() will clean varible to avoid unmap.
> > >
> > > ({ \
> > > __auto_type __ptr = &(t); typeof(t) empty= {}; \
> > > __auto_type __val = *__ptr; \
> > > __ptr->okay = 0; \
> > > __val.ret; \
> > > })
> > >
> > > So
> > >
> > > submit()
> > > {
> > > CLASS(dma_map_single, dma)(dev,...;
> > >
> > > ...
> > > dmaengine_submit();
> > > if (...)
> > > return err;
> > >
> > > //before return;
> > >
> > > retain_and_empty(dma)
> > > }
> > >
> > > This series just show how to hanndle many agurement at resource alloc/free
> > > functions. Only show dma_map_single. If the over all method is acceptable.
> > > I will more define for dma mapping functions.
> > >
> > > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> >
> > This looks fine from the DMA-mapping API perspective. I think we should
> > also get some feedback from Peter, who authored most of the __cleanup()
> > based infrastructure, so I've added him to the recipients list.
>
> I may represent minority here, but patch "i2c: lpi2c: Use auto cleanup
> for dma_map_single()" looks completely unreadable after this change.
>
> It is perfectly valid to use __cleanup() for simple and scoped things
> like kmalloc, but DMA API is much complicated than that.
Yes, DMA API is much complicated. Actually macro CLASS() is using
__cleanup().
#define CLASS(_name, var) \
class_##_name##_t var __cleanup(class_##_name##_destructor) = \
class_##_name##_constructor
Frank
>
> Thanks
>
> >
> >
> > > ---
> > > Frank Li (3):
> > > cleanup: Add DEFINE_GUARD_ARGS_CLASS macro for resource alloc/free functions with multiple arguments
> > > dma-mapping: Add auto cleanup support dma_map_single()
> > > i2c: lpi2c: Use auto cleanup for dma_map_single()
> > >
> > > drivers/i2c/busses/i2c-imx-lpi2c.c | 13 ++++---
> > > include/linux/cleanup.h | 73 ++++++++++++++++++++++++++++++++++++++
> > > include/linux/dma-mapping.h | 8 +++++
> > > 3 files changed, 87 insertions(+), 7 deletions(-)
> > > ---
> > > base-commit: 7c3ba4249a3604477ea9c077e10089ba7ddcaa03
> > > change-id: 20251008-dmamap_cleanup-d0a7f0525a3d
> > >
> > > Best regards,
> > > --
> > > Frank Li <Frank.Li@nxp.com>
> > >
> > >
> > Best regards
> > --
> > Marek Szyprowski, PhD
> > Samsung R&D Institute Poland
> >
> >
next prev parent reply other threads:[~2025-10-20 15:56 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20251010185046eucas1p26868b540b74a96e36943066216525bed@eucas1p2.samsung.com>
2025-10-10 18:50 ` [PATCH 0/3] dma_mapping: Add auto cleanup support Frank Li
2025-10-10 18:50 ` [PATCH 1/3] cleanup: Add DEFINE_GUARD_ARGS_CLASS macro for resource alloc/free functions with multiple arguments Frank Li
2025-10-10 18:50 ` [PATCH 2/3] dma-mapping: Add auto cleanup support dma_map_single() Frank Li
2025-10-10 18:50 ` [PATCH 3/3] i2c: lpi2c: Use auto cleanup for dma_map_single() Frank Li
2025-10-14 5:54 ` [PATCH 0/3] dma_mapping: Add auto cleanup support Marek Szyprowski
2025-10-19 12:03 ` Leon Romanovsky
2025-10-20 15:56 ` Frank Li [this message]
2025-11-06 17:27 ` Frank Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aPZbuY5bLpkoO2J/@lizhi-Precision-Tower-5810 \
--to=frank.li@nxp.com \
--cc=aisheng.dong@nxp.com \
--cc=andi.shyti@kernel.org \
--cc=festevam@gmail.com \
--cc=imx@lists.linux.dev \
--cc=iommu@lists.linux.dev \
--cc=kernel@pengutronix.de \
--cc=leon@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=peterz@infradead.org \
--cc=robin.murphy@arm.com \
--cc=s.hauer@pengutronix.de \
--cc=shawnguo@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.