[PATCH v6 4/9] dma-direct: Align CoCo shared DMA allocations to the shared granule size

Catalin Marinas catalin.marinas at arm.com
Fri Sep 18 07:16:21 PDT 2026


On Fri, Sep 04, 2026 at 04:04:47PM +0530, Aneesh Kumar K.V (Arm) wrote:
> @@ -419,6 +420,14 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
>  #ifdef CONFIG_DMA_NUMA_CMA
>  	int nid = dev_to_node(dev);
>  #endif
> +	/*
> +	 * CoCo shared allocations require CMA alignment large enough for the
> +	 * architecture's shared-buffer granule.
> +	 */
> +	if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) {
> +		if (get_order(mem_cc_shared_granule_size()) > CONFIG_CMA_ALIGNMENT)
> +			return NULL;
> +	}

[...]

> diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
> index 00f422a1e896..fc4a834aaa14 100644
> --- a/kernel/dma/pool.c
> +++ b/kernel/dma/pool.c
> @@ -91,7 +91,9 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
>  	void *addr;
>  	int ret = -ENOMEM;
>  	pgprot_t prot __maybe_unused;
> +	unsigned int min_encrypt_order = get_order(mem_cc_shared_granule_size());
>  
> +	pool_size = mem_cc_align_to_shared_granule(pool_size);
>  	/* Cannot allocate larger than MAX_PAGE_ORDER */
>  	order = min(get_order(pool_size), MAX_PAGE_ORDER);
>  
> @@ -102,7 +104,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
>  							 order, false);
>  		if (!page)
>  			page = alloc_pages(gfp | __GFP_NOWARN, order);
> -	} while (!page && order-- > 0);
> +	} while (!page && order-- > min_encrypt_order);

I can see dma_alloc_from_contiguous() called by atomic_pool_expand()
clamps the alignment to CONFIG_CMA_ALIGNMENT and this one has a minimum
value of 2. Allocation will silently succeed. Not sure of the best fix,
maybe do a check here before calling the dma alloc function or just
return NULL as in dma_alloc_contiguous().

-- 
Catalin



More information about the linux-arm-kernel mailing list