[PATCH 1/3] dma: add dma_xalloc() and dma_xzalloc()

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Oct 16 03:31:30 PDT 2026



On 10/16/26 9:36 AM, Sascha Hauer wrote:
> 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 at pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum at 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)
> 

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list