From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89512606A322 for ; Thu, 3 Dec 2026 13:13:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=AQQMgn4d; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.a=rsa-sha256 header.s=mail20170921 header.b=FOPuJOA2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:References:Content-Type: Content-Transfer-Encoding:In-Reply-To:From:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=JaYo9ESGT9lUrYa+nuqn/scUpo1lr9JxmKtDNueSu0Q=; b=AQQMgn4dPuymhs SZfa0N7vbAxfN9CRt0cLptodTva/ZcUOmSIhtW7tUPBrTp7D97ONdzL95hKrarJLacAUxu2R0jTgH MGOLTikw3ExbCWV5FgobJz0hF39KtVp+5aIm1HWAAkhTsgG1iVUZsEGNhcK1JhCSo+uzwZGgZ67VV aZV1mZu9BtjZw0e1a2imb7DNzeofQckk33nk4z/w2Em1t++W5VhxEmBMjornmerD8pzbFflIvABGG ioY244aLgcWZ7sH5KOlqdRiK8LPa/oJ8Y0sEtSWhwO7aT5ei3qamR4iKANap9gN95iZT4P8To0gaj XDEZ9v6rnXACLYzQxj1g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZ6cy-0000000Eh4q-2zNR; Thu, 03 Dec 2026 13:13:36 +0000 Received: from mailout2.w1.samsung.com ([210.118.77.12]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZ6cu-0000000Eh3s-2p5v for linux-arm-kernel@lists.infradead.org; Thu, 03 Dec 2026 13:13:35 +0000 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20261203131327euoutp0269651030c3c48ccf1420ffb435e2df06~twJ0isGSr1917619176euoutp02G; Thu, 3 Dec 2026 13:13:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20261203131327euoutp0269651030c3c48ccf1420ffb435e2df06~twJ0isGSr1917619176euoutp02G DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1796303607; bh=JaYo9ESGT9lUrYa+nuqn/scUpo1lr9JxmKtDNueSu0Q=; h=Date:Subject:To:Cc:From:In-Reply-To:References:From; b=FOPuJOA2ubCcICSb8mzurJdXV5UhWhtIpI/luSIA5bQ0AAY/4jPxfGa3H37FxIbRd lq068i8dtS6cZmEHRfffLeiZplyONIaw8vHXEdj0lRJ+yInMKOn0Hp6fgYC6AtjAFK 1r6WgT4UTMzC1KjprmZ9kZmOJLyQKlcf9rSHJznc= Received: from eusmtip2.samsung.com (unknown [203.254.199.222]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20261203131327eucas1p1c2a7bc05113edd5464dbb44162b75322~twJ0S7rWL1909219092eucas1p1P; Thu, 3 Dec 2026 13:13:27 +0000 (GMT) Received: from [106.210.134.192] (unknown [106.210.134.192]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20261203131322eusmtip289d153dc65ddb108a15136dfaa8a2cab~twJvagisF2363823638eusmtip2T; Thu, 3 Dec 2026 13:13:21 +0000 (GMT) Message-ID: Date: Thu, 3 Dec 2026 14:13:21 +0100 X-Archived-By: MIME-Version: 1.0 User-Agent: Betterbird (Windows) Subject: Re: [PATCH v11 15/23] dma-contiguous: Derive shared alignment from DMA attributes To: "Aneesh Kumar K.V (Arm)" , linux-coco@lists.linux.dev Cc: Andrew Morton , Catalin Marinas , =?UTF-8?Q?Christian_K=C3=B6nig?= , Jason Gunthorpe , Joerg Roedel , Marc Zyngier , Robin Murphy , Steven Price , Sumit Semwal , Suzuki K Poulose , Will Deacon , "T.J. Mercier" , Madhavan Srinivasan , Heiko Carstens , Christian Borntraeger , Sven Schnelle , Dave Hansen , Kiryl Shutsemau , Rick Edgecombe , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Haiyang Zhang , Wei Liu , Dexuan Cui , Andy Lutomirski , Peter Zijlstra , Alexander Gordeev , Gerald Schaefer , Vasily Gorbik , x86@kernel.org, dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, linaro-mm-sig@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-media@vger.kernel.org, linux-mm@kvack.org, kvmarm@lists.linux.dev, patches@lists.linux.dev Content-Language: en-US From: Marek Szyprowski In-Reply-To: <20261202095446.2029567-16-aneesh.kumar@kernel.org> Content-Transfer-Encoding: 8bit X-CMS-MailID: 20261203131327eucas1p1c2a7bc05113edd5464dbb44162b75322 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20261202095833eucas1p2ce446fc9ff49bb38124133a1604e4e2d X-EPHeader: CA X-CMS-RootMailID: 20261202095833eucas1p2ce446fc9ff49bb38124133a1604e4e2d References: <20261202095446.2029567-1-aneesh.kumar@kernel.org> <20261202095446.2029567-16-aneesh.kumar@kernel.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261203_051333_340615_7D96D218 X-CRM114-Status: GOOD ( 34.79 ) X-Spam-Score: -3.0 (---) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 02.12.2026 10:54, Aneesh Kumar K.V (Arm) wrote: > Confidential-computing shared DMA allocations require CMA backing memory > to be aligned to the architecture shared-memory granule. Passing explicit > alignment orders through the DMA allocation stack exposes this derived > constraint to callers and requires separate preferred and mandatory values. > > The DMA core already records whether an allocation needs shared backing in > __DMA_ATTR_ALLOC_CC_SHARED. Pass allocation attributes to the DMA-contiguous > entry points and derive the mandatory alignment there before calling CMA. > Preserve the existing alignment clamping for ordinary allocations, while > rejecting shared allocations whose granule exceeds CONFIG_CMA_ALIGNMENT so > callers can fall back to the page allocator. Do we really need to pass the information about required alignment to every low-level allocator? I think that all low-level allocator obey the common rule that the buffer is aligned to the nearest power-of-2 larger/equal to the requested size. IMHO it would be much easier to ensure that the caller simply aligns the requested buffer size to the shared-memory granule size? > Cc: Robin Murphy > Cc: Will Deacon > Cc: Marek Szyprowski > Signed-off-by: Aneesh Kumar K.V (Arm) > --- > arch/arm/mm/dma-mapping.c | 5 ++-- > drivers/iommu/dma-iommu.c | 2 +- > include/linux/dma-map-ops.h | 9 +++---- > kernel/dma/contiguous.c | 47 ++++++++++++++++++++++++++++--------- > kernel/dma/direct.c | 2 +- > kernel/dma/ops_helpers.c | 2 +- > kernel/dma/pool.c | 2 +- > kernel/kexec_file.c | 3 ++- > 8 files changed, 50 insertions(+), 22 deletions(-) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index fb2eb0ed7bb8..22888880b832 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -398,7 +398,8 @@ static void *__alloc_from_contiguous(struct device *dev, size_t size, > struct page *page; > void *ptr = NULL; > > - page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN); > + page = dma_alloc_from_contiguous(dev, count, order, 0, > + gfp & __GFP_NOWARN); > if (!page) > return NULL; > > @@ -866,7 +867,7 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, > unsigned long order = get_order(size); > struct page *page; > > - page = dma_alloc_from_contiguous(dev, count, order, > + page = dma_alloc_from_contiguous(dev, count, order, 0, > gfp & __GFP_NOWARN); > if (!page) > goto error; > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index 78742aeba932..2d2781910740 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -1646,7 +1646,7 @@ static void *iommu_dma_alloc_pages(struct device *dev, size_t size, > struct page *page = NULL; > void *cpu_addr; > > - page = dma_alloc_contiguous(dev, alloc_size, gfp); > + page = dma_alloc_contiguous(dev, alloc_size, gfp, 0); > if (!page) { > cpu_addr = alloc_pages_exact_nid(node, alloc_size, gfp); > if (!cpu_addr) > diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h > index afaf2c12189b..7a83942a10f3 100644 > --- a/include/linux/dma-map-ops.h > +++ b/include/linux/dma-map-ops.h > @@ -99,10 +99,11 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, > phys_addr_t limit, struct cma **res_cma, bool fixed); > > struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, > - unsigned int order, bool no_warn); > + unsigned int order, unsigned long attrs, bool no_warn); > bool dma_release_from_contiguous(struct device *dev, struct page *pages, > int count); > -struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp); > +struct page *dma_alloc_contiguous(struct device *dev, size_t size, > + gfp_t gfp, unsigned long attrs); > void dma_free_contiguous(struct device *dev, struct page *page, size_t size); > > void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size); > @@ -125,7 +126,7 @@ static inline int dma_contiguous_reserve_area(phys_addr_t size, > return -ENOSYS; > } > static inline struct page *dma_alloc_from_contiguous(struct device *dev, > - size_t count, unsigned int order, bool no_warn) > + size_t count, unsigned int order, unsigned long attrs, bool no_warn) > { > return NULL; > } > @@ -136,7 +137,7 @@ static inline bool dma_release_from_contiguous(struct device *dev, > } > /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */ > static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size, > - gfp_t gfp) > + gfp_t gfp, unsigned long attrs) > { > return NULL; > } > diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c > index 90937dc7c627..cb777c71ee38 100644 > --- a/kernel/dma/contiguous.c > +++ b/kernel/dma/contiguous.c > @@ -39,6 +39,7 @@ > > #include > > +#include > #include > #include > #include > @@ -357,19 +358,31 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, > * dma_alloc_from_contiguous() - allocate pages from contiguous area > * @dev: Pointer to device for which the allocation is performed. > * @count: Requested number of pages. > - * @align: Requested alignment of pages (in PAGE_SIZE order). > + * @align: Preferred alignment of pages (in PAGE_SIZE order). > + * @attrs: DMA allocation attributes. > * @no_warn: Avoid printing message about failed allocation. > * > * This function allocates memory buffer for specified device. It uses > * device specific contiguous memory area if available or the default > * global one. Requires architecture specific dev_get_cma_area() helper > * function. > + * > + * The preferred alignment is capped at CONFIG_CMA_ALIGNMENT. The internal > + * shared-allocation attribute requires at least the architecture shared > + * granule alignment and fails if that exceeds the CMA alignment limit. > */ > struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, > - unsigned int align, bool no_warn) > + unsigned int align, unsigned long attrs, bool no_warn) > { > - if (align > CONFIG_CMA_ALIGNMENT) > - align = CONFIG_CMA_ALIGNMENT; > + unsigned int required_align = 0; > + > + if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) > + required_align = get_order(cc_shared_granule_size()); > + > + if (required_align > CONFIG_CMA_ALIGNMENT) > + return NULL; > + > + align = min(max(align, required_align), CONFIG_CMA_ALIGNMENT); > > return cma_alloc(dev_get_cma_area(dev), count, align, no_warn); > } > @@ -390,10 +403,9 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages, > return cma_release(dev_get_cma_area(dev), pages, count); > } > > -static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp) > +static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp, > + unsigned int align) > { > - unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT); > - > return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN); > } > > @@ -402,6 +414,7 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp) > * @dev: Pointer to device for which the allocation is performed. > * @size: Requested allocation size. > * @gfp: Allocation flags. > + * @attrs: DMA allocation attributes. > * > * tries to use device specific contiguous memory area if available, or it > * tries to use per-numa cma, if the allocation fails, it will fallback to > @@ -412,8 +425,11 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp) > * there is no need to waste CMA pages for that kind; it also helps reduce > * fragmentations. > */ > -struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) > +struct page *dma_alloc_contiguous(struct device *dev, size_t size, > + gfp_t gfp, unsigned long attrs) > { > + unsigned int required_align = 0; > + unsigned int align = get_order(size); > #ifdef CONFIG_DMA_NUMA_CMA > int nid = dev_to_node(dev); > #endif > @@ -421,8 +437,17 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) > /* CMA can be used only in the context which permits sleeping */ > if (!gfpflags_allow_blocking(gfp)) > return NULL; > + > + if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) > + required_align = get_order(cc_shared_granule_size()); > + > + if (required_align > CONFIG_CMA_ALIGNMENT) > + return NULL; > + > + align = min(max(align, required_align), CONFIG_CMA_ALIGNMENT); > if (dev->cma_area) > - return cma_alloc_aligned(dev->cma_area, size, gfp); > + return cma_alloc_aligned(dev->cma_area, size, gfp, align); > + > if (size <= PAGE_SIZE) > return NULL; > > @@ -431,7 +456,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) > struct cma *cma = dma_contiguous_numa_area[nid]; > struct page *page; > if (cma) { > - page = cma_alloc_aligned(cma, size, gfp); > + page = cma_alloc_aligned(cma, size, gfp, align); > if (page) > return page; > } > @@ -440,7 +465,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) > if (!dma_contiguous_default_area) > return NULL; > > - return cma_alloc_aligned(dma_contiguous_default_area, size, gfp); > + return cma_alloc_aligned(dma_contiguous_default_area, size, gfp, align); > } > > /** > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > index 4d05817d6245..77ada1f92ca6 100644 > --- a/kernel/dma/direct.c > +++ b/kernel/dma/direct.c > @@ -125,7 +125,7 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, > WARN_ON_ONCE(!PAGE_ALIGNED(size)); > > gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit); > - page = dma_alloc_contiguous(dev, size, gfp); > + page = dma_alloc_contiguous(dev, size, gfp, 0); > if (page) { > if (dma_coherent_ok(dev, page_to_phys(page), size) && > (allow_highmem || !PageHighMem(page))) > diff --git a/kernel/dma/ops_helpers.c b/kernel/dma/ops_helpers.c > index 5cdf3f60cf47..11453e5ee0d3 100644 > --- a/kernel/dma/ops_helpers.c > +++ b/kernel/dma/ops_helpers.c > @@ -66,7 +66,7 @@ struct page *dma_common_alloc_pages(struct device *dev, size_t size, > struct page *page; > phys_addr_t phys; > > - page = dma_alloc_contiguous(dev, size, gfp); > + page = dma_alloc_contiguous(dev, size, gfp, 0); > if (!page) { > void *va = alloc_pages_exact_nid(dev_to_node(dev), size, gfp); > > diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c > index 5e8cf12e8222..088149b99103 100644 > --- a/kernel/dma/pool.c > +++ b/kernel/dma/pool.c > @@ -99,7 +99,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size, > pool_size = 1 << (PAGE_SHIFT + order); > if (cma_in_zone(gfp)) > page = dma_alloc_from_contiguous(NULL, 1 << order, > - order, false); > + order, 0, false); > if (!page) > page = alloc_pages(gfp | __GFP_NOWARN, order); > } while (!page && order-- > 0); > diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c > index b668ce8d68ee..5bcba818f046 100644 > --- a/kernel/kexec_file.c > +++ b/kernel/kexec_file.c > @@ -702,7 +702,8 @@ static int kexec_alloc_contig(struct kexec_buf *kbuf) > if (kbuf->image->type == KEXEC_TYPE_CRASH) > return -EPERM; > > - p = dma_alloc_from_contiguous(NULL, nr_pages, get_order(kbuf->buf_align), true); > + p = dma_alloc_from_contiguous(NULL, nr_pages, > + get_order(kbuf->buf_align), 0, true); > if (!p) > return -ENOMEM; > Best regards -- Marek Szyprowski, PhD Samsung R&D Institute Poland