From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 1/3] dma: add dma_xalloc() and dma_xzalloc()
Date: Fri, 16 Oct 2026 09:36:53 +0200 [thread overview]
Message-ID: <20261016-dma-alloc-v1-1-31fc7afe3954@pengutronix.de> (raw)
In-Reply-To: <20261016-dma-alloc-v1-0-31fc7afe3954@pengutronix.de>
dma_alloc() and dma_zalloc() are implemented with xmemalign() and
can't fail, yet many of their callers check for NULL, while others
rely on them never returning NULL.
As a first step towards making dma_alloc() and dma_zalloc() fallible
like malloc() and calloc(), add dma_xalloc() and dma_xzalloc() as
counterparts to xmalloc() and xzalloc() for the callers that rely on
the allocation to always succeed.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/dma/map.c | 17 +++++++++++++++++
include/dma.h | 12 ++++++++++++
2 files changed, 29 insertions(+)
diff --git a/drivers/dma/map.c b/drivers/dma/map.c
index 11671f9f66..806439a05d 100644
--- a/drivers/dma/map.c
+++ b/drivers/dma/map.c
@@ -9,6 +9,12 @@ void *dma_alloc(size_t size)
}
EXPORT_SYMBOL(dma_alloc);
+void *dma_xalloc(size_t size)
+{
+ return xmemalign(DMA_ALIGNMENT, ALIGN(size, DMA_ALIGNMENT));
+}
+EXPORT_SYMBOL(dma_xalloc);
+
void *dma_zalloc(size_t size)
{
void *buf;
@@ -21,6 +27,17 @@ void *dma_zalloc(size_t size)
}
EXPORT_SYMBOL(dma_zalloc);
+void *dma_xzalloc(size_t size)
+{
+ void *buf;
+
+ buf = dma_xalloc(size);
+ memset(buf, 0x00, size);
+
+ return buf;
+}
+EXPORT_SYMBOL(dma_xzalloc);
+
void dma_sync_single_for_cpu(struct device *dev, dma_addr_t address,
size_t size, enum dma_data_direction dir)
{
diff --git a/include/dma.h b/include/dma.h
index c285b2095a..600b91c485 100644
--- a/include/dma.h
+++ b/include/dma.h
@@ -28,17 +28,29 @@
#ifdef CONFIG_HAS_DMA
void *dma_alloc(size_t size);
+void *dma_xalloc(size_t size);
void *dma_zalloc(size_t size);
+void *dma_xzalloc(size_t size);
#else
static inline void *dma_alloc(size_t size)
{
return malloc(size);
}
+static inline void *dma_xalloc(size_t size)
+{
+ return xmalloc(size);
+}
+
static inline void *dma_zalloc(size_t size)
{
return calloc(size, 1);
}
+
+static inline void *dma_xzalloc(size_t size)
+{
+ return xzalloc(size);
+}
#endif
static inline void dma_free(void *mem)
--
2.47.3
next prev parent reply other threads:[~2026-10-16 7:37 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 7:36 [PATCH 0/3] dma: make dma_alloc() and dma_zalloc() fallible Sascha Hauer
2026-10-16 7:36 ` Sascha Hauer [this message]
2026-10-16 10:31 ` [PATCH 1/3] dma: add dma_xalloc() and dma_xzalloc() Ahmad Fatoum
2026-10-16 7:36 ` [PATCH 2/3] treewide: use dma_xalloc()/dma_xzalloc() where the result isn't checked Sascha Hauer
2026-10-16 10:35 ` Ahmad Fatoum
2026-10-16 7:36 ` [PATCH 3/3] dma: let dma_alloc() and dma_zalloc() fail Sascha Hauer
2026-10-16 10:35 ` Ahmad Fatoum
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=20261016-dma-alloc-v1-1-31fc7afe3954@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox