Skip to contentSkip to conversation

coco: guest: Enforce host page-size alignment for shared buffers

PATCH In review Aneesh Kumar K.V (Arm) 5 participants 37 messages Sep 4 – Sep 23

Subsystems (9)

v6 of 8 In review Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover coco: guest: Enforce host page-size alignment for shared buffers

Hi,

This series tightens the alignment requirements for buffers that are shared between confidential-computing guests and the host.

When a guest runs with private memory, buffers shared with the hypervisor are not only accessed by the guest. They are also accessed by the host kernel, and the host may manage the corresponding shared/private state at a granularity larger than the guest page size.

This matters for CCA systems where the Realm stage-2 mappings managed by the RMM can still operate at 4K granularity, while the non-secure host may manage the IPA state change at a larger page size, for example 64K. In that case, allowing a guest to convert and share only a 4K subrange of a host-managed granule is unsafe.

Architectures such as Arm can detect incorrect accesses to Realm physical address space PFNs through GPC faults. However, relying on that as the only line of defence is fragile and can still lead to kernel crashes. The risk is especially visible for shared buffers that are later mmapped into userspace, such as guest_memfd or dma-buf backed allocations. Once userspace can access the mapping, the kernel cannot guarantee that applications will only touch the intended 4K region rather than the whole host page mapped into their address space. Those userspace addresses may also be passed back into the kernel and accessed through the linear map, resulting in a GPC fault.

To avoid this, shared buffers must satisfy two constraints:

- the address must be aligned to the CoCo shared-granule size
- the size must be a multiple of that granule size

The series adds generic helpers for this:

- mem_cc_shared_granule_size()
- mem_cc_align_to_shared_granule()

The generic implementation defaults to PAGE_SIZE. arm64 CCA overrides this by querying the host IPA state change granule size through RHI and exposing that value through the arm64 memory-encryption operations.

The patche series update the main shared-buffer allocation paths that can be used by private-memory guests:

- arm64 set_memory_encrypted()/set_memory_decrypted() now reject unaligned addresses or sizes.
- GIC ITS shared allocations are rounded to the shared granule size.
- dma-direct and atomic DMA pool allocations use aligned allocation and conversion sizes.
- SWIOTLB pools, including dynamic pools, are allocated and converted at the shared granule size.
- restricted-dma-pool regions are checked and rejected if firmware did not provide a base and size aligned to the shared granule size.
- dma-buf system heap cc-shared allocations require aligned sizes and use at least the required allocation order.

Hyper-V users of set_memory_encrypted() and set_memory_decrypted() are not changed by this series. Those paths are not currently used by the arm64 CCA code path, and therefore are not part of the arm64 CCA IPA state change alignment problem addressed here.

The series is based on:
- https://lore.kernel.org/all/[email protected]

Changes from v5:
https://lore.kernel.org/all/[email protected]
* Rebased to latest kernel
* Drop patch arm64: realm: Move Realm memory encryption ops to RSI code

Changes from v4:
https://lore.kernel.org/all/[email protected]
* Rename the helpers to use CoCo terminology (mem_cc_shared_granule_size() / mem_cc_align_to_shared_granule() instead of mem_decrypt_granule_size() / mem_decrypt_align()).
* Use __DMA_ATTR_ALLOC_CC_SHARED to pass CoCo shared allocation requirements down to CMA-based allocation helpers.
* Add validation for restricted DMA pools to reject pools that are not aligned to the shared granule size.
* Add dma-buf system heap handling for cc-shared buffers.
* Split the previous combined DMA/SWIOTLB/ITS change into smaller subsystem patches covering ITS, DMA direct, SWIOTLB, restricted DMA pools, dma-buf system heap, and arm64 Realm support.
* Rework arm64 Realm support by moving Realm memory encryption ops into RSI code and exposing the CCA shared granule size through arm64_mem_crypt_ops.

Changes from v3:
https://lore.kernel.org/all/[email protected]
* Fix build error reported by kernel test robot <[email protected]>

Changes from v2:
https://lore.kernel.org/all/[email protected]
* Rebase to latest kernel
* Consider swiotlb always decrypted and don't align when allocating from swiotlb.

Changes from v1:
* Rename the helper to mem_encrypt_align
* Improve the commit message
* Handle DMA allocations from contiguous memory
* Handle DMA allocations from the pool
* swiotlb is still considered unencrypted. Support for an encrypted swiotlb pool is left as TODO and is independent of this series.

Cc Catalin Marinas <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Steven Price <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>

Aneesh Kumar K.V (Arm) (9):
mm/mem_encrypt: Add helpers for shared-buffer alignment irqchip/gic-v3-its: Align shared ITS allocations to the CoCo shared
granule size
dma-mapping: Pass allocation attrs to contiguous allocation helpers
dma-direct: Align CoCo shared DMA allocations to the shared granule
size
swiotlb: Align shared IO TLB pools to the shared granule size
swiotlb: Reject misaligned restricted DMA pools for CoCo guests
dma-buf: system_heap: Enforce shared-granule alignment for cc-shared
buffers
arm64: realm: Add RHI helper to query IPA state change alignment
arm64: realm: Expose the CCA shared granule size through mem_encrypt
ops

 arch/arm64/include/asm/mem_encrypt.h |  4 ++
 arch/arm64/mm/mem_encrypt.c          | 32 ++++++++++++++--
 drivers/dma-buf/heaps/system_heap.c  | 50 +++++++++++++++++++-----
 drivers/firmware/arm_rmm/rsi.c       | 57 ++++++++++++++++++++++++++++
 drivers/iommu/dma-iommu.c            |  2 +-
 drivers/irqchip/irq-gic-v3-its.c     |  6 ++-
 include/linux/arm-rsi-cmds.h         | 10 +++++
 include/linux/arm-smccc-rhi.h        | 25 ++++++++++++
 include/linux/arm-smccc-rsi.h        |  7 ++++
 include/linux/dma-map-ops.h          |  5 ++-
 include/linux/mem_encrypt.h          | 14 +++++++
 kernel/dma/contiguous.c              | 13 ++++++-
 kernel/dma/direct.c                  | 27 +++++++++----
 kernel/dma/ops_helpers.c             |  3 +-
 kernel/dma/pool.c                    |  4 +-
 kernel/dma/swiotlb.c                 | 36 ++++++++++++++----
 16 files changed, 260 insertions(+), 35 deletions(-)
 create mode 100644 include/linux/arm-smccc-rhi.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH v6 1/9 mm/mem_encrypt: Add helpers for shared-buffer alignment

confidential-computing guests may require shared buffers with alignment larger than the guest page size. As these buffers are shared with the host, which may manage shared/private state at a different page-size granularity, the required alignment must account for the host's page size as well.

Add helpers for querying the shared-buffer granule size and for rounding sizes up to that granule. The generic implementation defaults to PAGE_SIZE so that existing architectures keep their current behaviour unless they override the granule size.

---
 include/linux/mem_encrypt.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/mem_encrypt.h +14 -0
index 07584c5e36fb..5d2a868f8d3d 100644
@@ -11,6 +11,8 @@
 #define __MEM_ENCRYPT_H__
 
 #ifndef __ASSEMBLY__
+#include <linux/align.h>
+#include <vdso/page.h>
 
 #ifdef CONFIG_ARCH_HAS_MEM_ENCRYPT
 
@@ -54,6 +56,18 @@
 #define dma_addr_canonical(x)		(x)
 #endif
 
+#ifndef mem_cc_shared_granule_size
+static inline size_t mem_cc_shared_granule_size(void)
+{
+	return PAGE_SIZE;
+}
+#endif
+
+static inline size_t mem_cc_align_to_shared_granule(size_t size)
+{
+	return ALIGN(size, mem_cc_shared_granule_size());
+}
+
 #endif	/* __ASSEMBLY__ */
 
 #endif	/* __MEM_ENCRYPT_H__ */
Signature · 1 line
2.43.0
#2 lore raw

PATCH v6 2/9 irqchip/gic-v3-its: Align shared ITS allocations to the CoCo shared granule size

In reply to Aneesh Kumar K.V (Arm)

ITS tables allocated by the coco guest are shared with the hypervisor. These allocations must satisfy the host shared-buffer granule size so that the full converted range is safe for host access.

Allocate ITS pages using a size rounded up to the shared granule size and use the same allocation order when encrypting, decrypting and freeing the memory. Also grow the ITT cache in shared-granule sized chunks instead of assuming PAGE_SIZE is sufficient.

---
 drivers/irqchip/irq-gic-v3-its.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3-its.c +5 -1
index e9807af23537..98d8fccbf0f2 100644
@@ -216,6 +216,7 @@ static struct page *its_alloc_pages_node(int node, gfp_t gfp,
 	struct page *page;
 	int ret = 0;
 
+	order = get_order(mem_cc_align_to_shared_granule(PAGE_SIZE << order));
 	page = alloc_pages_node(node, gfp | gfp_flags_quirk, order);
 
 	if (!page)
@@ -241,6 +242,8 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
 
 static void its_free_pages(void *addr, unsigned int order)
 {
+
+	order = get_order(mem_cc_align_to_shared_granule(PAGE_SIZE << order));
 	/*
 	 * If the memory cannot be encrypted again then we must leak the pages.
 	 * set_memory_encrypted() will already have WARNed.
@@ -272,7 +275,8 @@ static void *itt_alloc_pool(int node, int size)
 		if (!page)
 			break;
 
-		gen_pool_add(itt_pool, (unsigned long)page_address(page), PAGE_SIZE, node);
+		gen_pool_add(itt_pool, (unsigned long)page_address(page),
+			     mem_cc_align_to_shared_granule(PAGE_SIZE), node);
 	} while (!addr);
 
 	return (void *)addr;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v6 3/9 dma-mapping: Pass allocation attrs to contiguous allocation helpers

In reply to Aneesh Kumar K.V (Arm)

Prepare for handling CoCo shared allocation requirements in the common contiguous allocation path by passing DMA allocation attributes down to the helpers that may allocate from CMA.

The next patch uses this to apply shared-granule alignment only to allocations that are actually creating CoCo shared backing pages.

---
 drivers/iommu/dma-iommu.c   |  2 +-
 include/linux/dma-map-ops.h |  5 +++--
 kernel/dma/contiguous.c     |  4 +++-
 kernel/dma/direct.c         | 11 ++++++-----
 kernel/dma/ops_helpers.c    |  3 ++-
 5 files changed, 15 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +1 -1
index 58c624513cd4..32ed56aff12a 100644
@@ -1627,7 +1627,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, attrs);
 	if (!page)
 		page = alloc_pages_node(node, gfp, get_order(alloc_size));
 	if (!page)
include/linux/dma-map-ops.h +3 -2
index 8fae2b7deb20..1849f352fb88 100644
@@ -102,7 +102,8 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 				       unsigned int order, 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);
@@ -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;
 }
kernel/dma/contiguous.c +3 -1
index 66093460584e..18cd423fbc67 100644
@@ -402,6 +402,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 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,7 +413,8 @@ 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)
 {
 #ifdef CONFIG_DMA_NUMA_CMA
 	int nid = dev_to_node(dev);
kernel/dma/direct.c +6 -5
index da665ca22d5c..fe02e8a3c0bb 100644
@@ -115,7 +115,7 @@ static struct page *dma_direct_alloc_swiotlb(struct device *dev, size_t size,
 }
 
 static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
-		gfp_t gfp, bool allow_highmem)
+		gfp_t gfp, bool allow_highmem, unsigned long attrs)
 {
 	int node = dev_to_node(dev);
 	struct page *page;
@@ -124,7 +124,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, attrs);
 	if (page) {
 		if (dma_coherent_ok(dev, page_to_phys(page), size) &&
 		    (allow_highmem || !PageHighMem(page)))
@@ -184,7 +184,7 @@ static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
 {
 	struct page *page;
 
-	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
+	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true, 0);
 	if (!page)
 		return NULL;
 
@@ -286,7 +286,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,
 	}
 
 	/* we always manually zero the memory once we are done */
-	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, allow_highmem);
+	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO,
+					allow_highmem, attrs);
 	if (!page)
 		return NULL;
 
@@ -452,7 +453,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
 		goto setup_page;
 	}
 
-	page = __dma_direct_alloc_pages(dev, size, gfp, false);
+	page = __dma_direct_alloc_pages(dev, size, gfp, false, attrs);
 	if (!page)
 		return NULL;
 
kernel/dma/ops_helpers.c +2 -1
index 6b5f9208d31c..43e5f8008a9c 100644
@@ -66,7 +66,8 @@ 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);
+	/* __DMA_ATTR_ALLOC_CC_SHARED is not yet supported here, attrs = 0 */
+	page = dma_alloc_contiguous(dev, size, gfp, 0);
 	if (!page)
 		page = alloc_pages_node(dev_to_node(dev), gfp, get_order(size));
 	if (!page)
Signature · 1 line
2.43.0
#4 lore raw

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

In reply to Aneesh Kumar K.V (Arm)

DMA allocations that create shared backing pages for confidential-computing guests are converted between private and shared memory before being used for DMA. On some architecture, the conversion granule may be larger than PAGE_SIZE, so converting only the requested size can leave the rest of the host-managed granule private.

Use the internal __DMA_ATTR_ALLOC_CC_SHARED allocation attribute to identify those allocations in the DMA allocation paths. Round the allocated and converted size up to mem_cc_shared_granule_size(), and use the same aligned size when restoring encryption on free.

Also reject CMA allocations for CoCo shared backing pages when CMA cannot provide alignment at the required shared granule size, and keep atomic DMA pool expansion from falling below the order needed for shared-buffer conversions.

---
 kernel/dma/contiguous.c |  9 +++++++++
 kernel/dma/direct.c     | 16 ++++++++++++++--
 kernel/dma/pool.c       |  4 +++-
 3 files changed, 26 insertions(+), 3 deletions(-)
kernel/dma/contiguous.c +9 -0
index 18cd423fbc67..6bdd4f264733 100644
@@ -45,6 +45,7 @@
 #include <linux/dma-map-ops.h>
 #include <linux/cma.h>
 #include <linux/nospec.h>
+#include <linux/mem_encrypt.h>
 
 #ifdef CONFIG_CMA_SIZE_MBYTES
 #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
@@ -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;
+	}
 
 	/* CMA can be used only in the context which permits sleeping */
 	if (!gfpflags_allow_blocking(gfp))
kernel/dma/direct.c +14 -2
index fe02e8a3c0bb..82d3ce39db0a 100644
@@ -285,6 +285,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
 		return NULL;
 	}
 
+	if (mark_mem_decrypt)
+		size = mem_cc_align_to_shared_granule(size);
+
 	/* we always manually zero the memory once we are done */
 	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO,
 					allow_highmem, attrs);
@@ -407,6 +410,9 @@ void dma_direct_free(struct device *dev, size_t size,
 		/* Swiotlb doesn't need a page attribute update on free */
 		mark_mem_encrypted = false;
 
+	if (mark_mem_encrypted && force_dma_unencrypted(dev))
+		size = mem_cc_align_to_shared_granule(size);
+
 	if (is_vmalloc_addr(cpu_addr)) {
 		vunmap(cpu_addr);
 	} else {
@@ -453,6 +459,9 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
 		goto setup_page;
 	}
 
+	if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
+		size = mem_cc_align_to_shared_granule(size);
+
 	page = __dma_direct_alloc_pages(dev, size, gfp, false, attrs);
 	if (!page)
 		return NULL;
@@ -493,8 +502,11 @@ void dma_direct_free_pages(struct device *dev, size_t size,
 	if (swiotlb_pool)
 		mark_mem_encrypted = false;
 
-	if (mark_mem_encrypted && dma_set_encrypted(dev, vaddr, size))
-		return;
+	if (mark_mem_encrypted) {
+		size = mem_cc_align_to_shared_granule(size);
+		if (dma_set_encrypted(dev, vaddr, size))
+			return;
+	}
 
 	if (swiotlb_pool)
 		swiotlb_free_from_pool(dev, phys, swiotlb_pool);
kernel/dma/pool.c +3 -1
index 00f422a1e896..fc4a834aaa14 100644
@@ -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);
 	if (!page)
 		goto out;
 
Signature · 1 line
2.43.0
maintainer
#5 lore raw

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

Quoted 12 lines · Aneesh Kumar K.V (Arm)
kernel/dma/contiguous.c +8 -0
@@ -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;
+	}

[...]

Quoted 15 lines
kernel/dma/pool.c +2 -0
index 00f422a1e896..fc4a834aaa14 100644
@@ -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);
 

kernel/dma/pool.c static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size, · line 107

Quoted 5 lines
kernel/dma/pool.c +1 -1
@@ -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().

Signature · 1 line
Catalin
#6 lore raw

PATCH v6 5/9 swiotlb: Align shared IO TLB pools to the shared granule size

In reply to Aneesh Kumar K.V (Arm)

swiotlb pools used by confidential-computing guests are shared with the host and therefore must be converted at the host shared-granule size. A PAGE_SIZE-sized or PAGE_SIZE-aligned pool is not sufficient when the host tracks shared state at a larger granularity.

Round swiotlb pool sizes to mem_cc_shared_granule_size(), allocate the pools at that alignment, and use the same aligned size when decrypting, encrypting and freeing pool memory. Apply the same rule to dynamically allocated swiotlb pools.

---
 kernel/dma/swiotlb.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
kernel/dma/swiotlb.c +14 -8
index ded7016a46a7..f8936b03d942 100644
@@ -369,7 +369,8 @@ void __init swiotlb_update_mem_attributes(void)
 
 	if (!mem->nslabs || mem->late_alloc)
 		return;
-	bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
+
+	bytes = mem_cc_align_to_shared_granule(mem->nslabs << IO_TLB_SHIFT);
 
 	if (io_tlb_default_mem.cc_shared) {
 		int ret;
@@ -436,8 +437,8 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
 		unsigned int flags,
 		int (*remap)(void *tlb, unsigned long nslabs))
 {
-	size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
 	void *tlb;
+	size_t bytes = mem_cc_align_to_shared_granule(nslabs << IO_TLB_SHIFT);
 
 	/*
 	 * By default allocate the bounce buffer memory from low memory, but
@@ -445,9 +446,9 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
 	 * memory encryption.
 	 */
 	if (flags & SWIOTLB_ANY)
-		tlb = memblock_alloc(bytes, PAGE_SIZE);
+		tlb = memblock_alloc(bytes, mem_cc_shared_granule_size());
 	else
-		tlb = memblock_alloc_low(bytes, PAGE_SIZE);
+		tlb = memblock_alloc_low(bytes, mem_cc_shared_granule_size());
 
 	if (!tlb) {
 		pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
@@ -456,7 +457,7 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
 	}
 
 	if (remap && remap(tlb, nslabs) < 0) {
-		memblock_free(tlb, PAGE_ALIGN(bytes));
+		memblock_free(tlb, bytes);
 		pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes);
 		return NULL;
 	}
@@ -578,7 +579,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
 		swiotlb_adjust_nareas(num_possible_cpus());
 
 retry:
-	order = get_order(nslabs << IO_TLB_SHIFT);
+	order = get_order(mem_cc_align_to_shared_granule(nslabs << IO_TLB_SHIFT));
 	nslabs = SLABS_PER_PAGE << order;
 
 	while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
@@ -587,6 +588,8 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
 		if (vstart)
 			break;
 		order--;
+		if (order < get_order(mem_cc_shared_granule_size()))
+			break;
 		nslabs = SLABS_PER_PAGE << order;
 		retried = true;
 	}
@@ -667,7 +670,7 @@ void __init swiotlb_exit(void)
 
 	pr_info("tearing down default memory pool\n");
 	tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
-	tbl_size = PAGE_ALIGN(mem->end - mem->start);
+	tbl_size = mem_cc_align_to_shared_granule(mem->end - mem->start);
 	slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
 
 	if (io_tlb_default_mem.cc_shared) {
@@ -711,12 +714,14 @@ void __init swiotlb_exit(void)
 static struct page *alloc_dma_pages(gfp_t gfp, size_t bytes,
 		u64 phys_limit, unsigned long attrs)
 {
-	unsigned int order = get_order(bytes);
 	bool cc_shared = attrs & __DMA_ATTR_ALLOC_CC_SHARED;
+	unsigned int order;
 	struct page *page;
 	phys_addr_t paddr;
 	void *vaddr;
 
+	bytes = mem_cc_align_to_shared_granule(bytes);
+	order = get_order(bytes);
 	page = alloc_pages(gfp, order);
 	if (!page)
 		return NULL;
@@ -807,6 +812,7 @@ static void swiotlb_free_tlb(void *vaddr, size_t bytes, bool cc_shared)
 	    dma_free_from_pool(NULL, vaddr, bytes))
 		return;
 
+	bytes = mem_cc_align_to_shared_granule(bytes);
 	/* Intentional leak if pages cannot be encrypted again. */
 	if (!cc_shared ||
 	    !set_memory_encrypted((unsigned long)vaddr, PFN_UP(bytes)))
Signature · 1 line
2.43.0
#7 lore raw

PATCH v6 6/9 swiotlb: Reject misaligned restricted DMA pools for CoCo guests

In reply to Aneesh Kumar K.V (Arm)

Restricted DMA pools are described by firmware reserved-memory nodes and are not resized or realigned by the kernel. For confidential-computing guests, such pools can only be shared safely when both the base address and size are aligned to the shared-granule size.

Reject restricted DMA pools that are not aligned to mem_cc_shared_granule_size() when guest memory encryption is active.

---
 kernel/dma/swiotlb.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
kernel/dma/swiotlb.c +14 -0
index f8936b03d942..d1bd06be628d 100644
@@ -2037,6 +2037,20 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
 		 * if platform supports memory encryption,
 		 * restricted mem pool is shared by default
 		 */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
+			size_t cc_shared_granule_size = mem_cc_shared_granule_size();
+
+			if (!IS_ALIGNED(rmem->base, cc_shared_granule_size) ||
+			    !IS_ALIGNED(rmem->size, cc_shared_granule_size)) {
+				dev_err(dev, "Restricted DMA pool must be aligned to %#zx bytes for memory encryption\n",
+					cc_shared_granule_size);
+				kfree(pool->areas);
+				kfree(pool->slots);
+				kfree(mem);
+				return -EINVAL;
+			}
+		}
+
 		if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
 			int ret;
 
Signature · 1 line
2.43.0
#8 lore raw

PATCH v6 7/9 dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

In reply to Aneesh Kumar K.V (Arm)

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

---
 drivers/dma-buf/heaps/system_heap.c | 50 +++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/dma-buf/heaps/system_heap.c +41 -9
index c8959eadc71d..9cbfcebe2088 100644
@@ -55,7 +55,6 @@ struct dma_heap_attachment {
 #define HIGH_ORDER_GFP  (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
 				| __GFP_NORETRY) & ~__GFP_RECLAIM) \
 				| __GFP_COMP)
-static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
 /*
  * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
  * to match with the sizes often found in IOMMUs. Using order 4 pages instead
@@ -375,26 +374,44 @@ static const struct dma_buf_ops system_heap_buf_ops = {
 	.release = system_heap_dma_buf_release,
 };
 
+static struct page *system_heap_alloc_order(unsigned int order)
+{
+	gfp_t flags = order ? HIGH_ORDER_GFP : LOW_ORDER_GFP;
+
+	if (mem_accounting)
+		flags |= __GFP_ACCOUNT;
+
+	return alloc_pages(flags, order);
+}
+
 static struct page *alloc_largest_available(unsigned long size,
-					    unsigned int max_order)
+					    unsigned int max_order,
+					    unsigned int min_order)
 {
 	struct page *page;
 	int i;
-	gfp_t flags;
 
 	for (i = 0; i < NUM_ORDERS; i++) {
 		if (size <  (PAGE_SIZE << orders[i]))
 			continue;
-		if (max_order < orders[i])
+
+		if (max_order < orders[i] || orders[i] < min_order)
 			continue;
-		flags = order_flags[i];
-		if (mem_accounting)
-			flags |= __GFP_ACCOUNT;
-		page = alloc_pages(flags, orders[i]);
+
+		page = system_heap_alloc_order(orders[i]);
 		if (!page)
 			continue;
 		return page;
 	}
+	/*
+	 * The required minimum order might not be one of the preferred heap
+	 * orders. Allocate exactly min_order when it does not exceed the
+	 * remaining size.
+	 */
+	if (min_order && min_order <= max_order &&
+	    size >= (PAGE_SIZE << min_order))
+		return system_heap_alloc_order(min_order);
+
 	return NULL;
 }
 
@@ -409,6 +426,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	unsigned int max_order = orders[0];
 	struct system_heap_priv *priv = dma_heap_get_drvdata(heap);
 	bool cc_shared = priv->cc_shared;
+	unsigned int min_order = 0;
+	size_t cc_granule_size;
 	struct dma_buf *dmabuf;
 	struct sg_table *table;
 	struct scatterlist *sg;
@@ -425,6 +444,18 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	buffer->heap = heap;
 	buffer->len = len;
 	buffer->cc_shared = cc_shared;
+	if (cc_shared_buffer(buffer)) {
+		cc_granule_size = mem_cc_shared_granule_size();
+		if (!IS_ALIGNED(len, cc_granule_size)) {
+			ret = -EINVAL;
+			goto free_buffer;
+		}
+		min_order = get_order(cc_granule_size);
+		if (min_order > max_order) {
+			ret = -EINVAL;
+			goto free_buffer;
+		}
+	}
 
 	INIT_LIST_HEAD(&pages);
 	i = 0;
@@ -438,7 +469,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 			goto free_buffer;
 		}
 
-		page = alloc_largest_available(size_remaining, max_order);
+		page = alloc_largest_available(size_remaining, max_order,
+					       min_order);
 		if (!page)
 			goto free_buffer;
 
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Quoted 98 lines · Aneesh Kumar K.V (Arm)

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

---
 drivers/dma-buf/heaps/system_heap.c | 50 +++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/dma-buf/heaps/system_heap.c +31 -8
index c8959eadc71d..9cbfcebe2088 100644
@@ -55,7 +55,6 @@ struct dma_heap_attachment {
 #define HIGH_ORDER_GFP  (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
 				| __GFP_NORETRY) & ~__GFP_RECLAIM) \
 				| __GFP_COMP)
-static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
 /*
  * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
  * to match with the sizes often found in IOMMUs. Using order 4 pages instead
@@ -375,26 +374,44 @@ static const struct dma_buf_ops system_heap_buf_ops = {
 	.release = system_heap_dma_buf_release,
 };
 
+static struct page *system_heap_alloc_order(unsigned int order)
+{
+	gfp_t flags = order ? HIGH_ORDER_GFP : LOW_ORDER_GFP;
+
+	if (mem_accounting)
+		flags |= __GFP_ACCOUNT;
+
+	return alloc_pages(flags, order);
+}
+
 static struct page *alloc_largest_available(unsigned long size,
-					    unsigned int max_order)
+					    unsigned int max_order,
+					    unsigned int min_order)
 {
 	struct page *page;
 	int i;
-	gfp_t flags;
 
 	for (i = 0; i < NUM_ORDERS; i++) {
 		if (size <  (PAGE_SIZE << orders[i]))
 			continue;
-		if (max_order < orders[i])
+
+		if (max_order < orders[i] || orders[i] < min_order)
 			continue;
-		flags = order_flags[i];
-		if (mem_accounting)
-			flags |= __GFP_ACCOUNT;
-		page = alloc_pages(flags, orders[i]);
+
+		page = system_heap_alloc_order(orders[i]);
 		if (!page)
 			continue;
 		return page;
 	}
+	/*
+	 * The required minimum order might not be one of the preferred heap
+	 * orders. Allocate exactly min_order when it does not exceed the
+	 * remaining size.
+	 */
+	if (min_order && min_order <= max_order &&
+	    size >= (PAGE_SIZE << min_order))
+		return system_heap_alloc_order(min_order);
+
 	return NULL;
 }
 
@@ -409,6 +426,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	unsigned int max_order = orders[0];
 	struct system_heap_priv *priv = dma_heap_get_drvdata(heap);
 	bool cc_shared = priv->cc_shared;
+	unsigned int min_order = 0;
+	size_t cc_granule_size;
 	struct dma_buf *dmabuf;
 	struct sg_table *table;
 	struct scatterlist *sg;
@@ -425,6 +444,18 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	buffer->heap = heap;
 	buffer->len = len;
 	buffer->cc_shared = cc_shared;
+	if (cc_shared_buffer(buffer)) {
+		cc_granule_size = mem_cc_shared_granule_size();
+		if (!IS_ALIGNED(len, cc_granule_size)) {
+			ret = -EINVAL;

drivers/dma-buf/heaps/system_heap.c static struct dma_buf *system_heap_allocate(struct dma_heap *heap, · line 460

Quoted 10 lines
drivers/dma-buf/heaps/system_heap.c +8 -0
+			goto free_buffer;
+		}
+		min_order = get_order(cc_granule_size);
+		if (min_order > max_order) {
+			ret = -EINVAL;
+			goto free_buffer;
+		}
+	}
 
 	INIT_LIST_HEAD(&pages);

You need to get a proper base so that Sashiko can review this series. I ran it through Claude and the above is wrong to go to free_buffer which walks over the pages list that has not been initialised yet.

Another issue, I'm just copying it verbatim:

drivers/dma-buf/heaps/system_heap.c:379 (patch 7/9) system_heap_alloc_order() picks "order ? HIGH_ORDER_GFP :
LOW_ORDER_GFP". HIGH_ORDER_GFP clears __GFP_RECLAIM and sets __GFP_NORETRY, which was fine while orders 8/4 always had order 0 as a fallback. The new min_order fallback at line 411 has no smaller order to fall back to - it is the mandatory minimum. On a 16K-page host with a 4K-page guest, min_order = get_order(SZ_16K) = 2, which is not in orders[] = {8, 4, 0}, and order 0 is filtered out by "orders[i] < min_order". Every tail allocation below 64K therefore goes through the non-reclaiming fallback and returns -ENOMEM under pressure that ordinary reclaim would have cleared. Note any fix must keep __GFP_COMP:
compound_order()/page_size() are used on the result (system_heap_set_page_decrypted(), the free paths and sg_set_page()).

Signature · 1 line
Catalin
maintainer
#10 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 2 lines · Catalin Marinas

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Yeah, thanks for doing this.

Quoted 7 lines · Aneesh Kumar K.V (Arm)
Quoted 6 lines

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Quoted 5 lines
Quoted 5 lines

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

Uff, I don't think we can do that.

That is massively platform specific behavior in a non platform specific code.

When the architecture can only expose unencrypted memory in larger chunks than the only real alternative is to use bounce buffers and those are incompatible with DMA-buf.

Additional to that you can trivially circumvent this with a memfd and udmabuf, so you most likely don't win anything with that.

Regards,
Christian.

Quoted 118 lines
Quoted 87 lines
---
 drivers/dma-buf/heaps/system_heap.c | 50 +++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/dma-buf/heaps/system_heap.c +31 -8
index c8959eadc71d..9cbfcebe2088 100644
@@ -55,7 +55,6 @@ struct dma_heap_attachment {
 #define HIGH_ORDER_GFP  (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
 				| __GFP_NORETRY) & ~__GFP_RECLAIM) \
 				| __GFP_COMP)
-static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
 /*
  * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
  * to match with the sizes often found in IOMMUs. Using order 4 pages instead
@@ -375,26 +374,44 @@ static const struct dma_buf_ops system_heap_buf_ops = {
 	.release = system_heap_dma_buf_release,
 };
 
+static struct page *system_heap_alloc_order(unsigned int order)
+{
+	gfp_t flags = order ? HIGH_ORDER_GFP : LOW_ORDER_GFP;
+
+	if (mem_accounting)
+		flags |= __GFP_ACCOUNT;
+
+	return alloc_pages(flags, order);
+}
+
 static struct page *alloc_largest_available(unsigned long size,
-					    unsigned int max_order)
+					    unsigned int max_order,
+					    unsigned int min_order)
 {
 	struct page *page;
 	int i;
-	gfp_t flags;
 
 	for (i = 0; i < NUM_ORDERS; i++) {
 		if (size <  (PAGE_SIZE << orders[i]))
 			continue;
-		if (max_order < orders[i])
+
+		if (max_order < orders[i] || orders[i] < min_order)
 			continue;
-		flags = order_flags[i];
-		if (mem_accounting)
-			flags |= __GFP_ACCOUNT;
-		page = alloc_pages(flags, orders[i]);
+
+		page = system_heap_alloc_order(orders[i]);
 		if (!page)
 			continue;
 		return page;
 	}
+	/*
+	 * The required minimum order might not be one of the preferred heap
+	 * orders. Allocate exactly min_order when it does not exceed the
+	 * remaining size.
+	 */
+	if (min_order && min_order <= max_order &&
+	    size >= (PAGE_SIZE << min_order))
+		return system_heap_alloc_order(min_order);
+
 	return NULL;
 }
 
@@ -409,6 +426,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	unsigned int max_order = orders[0];
 	struct system_heap_priv *priv = dma_heap_get_drvdata(heap);
 	bool cc_shared = priv->cc_shared;
+	unsigned int min_order = 0;
+	size_t cc_granule_size;
 	struct dma_buf *dmabuf;
 	struct sg_table *table;
 	struct scatterlist *sg;
@@ -425,6 +444,18 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	buffer->heap = heap;
 	buffer->len = len;
 	buffer->cc_shared = cc_shared;
+	if (cc_shared_buffer(buffer)) {
+		cc_granule_size = mem_cc_shared_granule_size();
+		if (!IS_ALIGNED(len, cc_granule_size)) {
+			ret = -EINVAL;

drivers/dma-buf/heaps/system_heap.c static struct dma_buf *system_heap_allocate(struct dma_heap *heap, · line 460

Quoted 10 lines
drivers/dma-buf/heaps/system_heap.c +8 -0
+			goto free_buffer;
+		}
+		min_order = get_order(cc_granule_size);
+		if (min_order > max_order) {
+			ret = -EINVAL;
+			goto free_buffer;
+		}
+	}
 
 	INIT_LIST_HEAD(&pages);

You need to get a proper base so that Sashiko can review this series. I ran it through Claude and the above is wrong to go to free_buffer which walks over the pages list that has not been initialised yet.

Another issue, I'm just copying it verbatim:

drivers/dma-buf/heaps/system_heap.c:379 (patch 7/9) system_heap_alloc_order() picks "order ? HIGH_ORDER_GFP :
LOW_ORDER_GFP". HIGH_ORDER_GFP clears __GFP_RECLAIM and sets __GFP_NORETRY, which was fine while orders 8/4 always had order 0 as a fallback. The new min_order fallback at line 411 has no smaller order to fall back to - it is the mandatory minimum. On a 16K-page host with a 4K-page guest, min_order = get_order(SZ_16K) = 2, which is not in orders[] = {8, 4, 0}, and order 0 is filtered out by "orders[i] < min_order". Every tail allocation below 64K therefore goes through the non-reclaiming fallback and returns -ENOMEM under pressure that ordinary reclaim would have cleared. Note any fix must keep __GFP_COMP:
compound_order()/page_size() are used on the result (system_heap_set_page_decrypted(), the free paths and sg_set_page()).

#11 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 18 lines · Christian König
Quoted 2 lines · Catalin Marinas

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Yeah, thanks for doing this.

Quoted 7 lines · Aneesh Kumar K.V (Arm)
Quoted 6 lines

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Quoted 5 lines
Quoted 5 lines

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

Quoted 4 lines

Uff, I don't think we can do that.

That is massively platform specific behavior in a non platform specific code.

What we really need is an allocator API that does all this for the caller.

It has been pointed out a few times, Aneesh maybe you need to try to tackle that?

dmabuf heap just wants

'allocate me an array of shared folios totaling XX bytes'

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

And solve the double/triple zeroing problem.

Jason

maintainer
#12 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 24 lines · Jason Gunthorpe
Quoted 18 lines · Christian König
Quoted 2 lines · Catalin Marinas

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Yeah, thanks for doing this.

Quoted 7 lines · Aneesh Kumar K.V (Arm)
Quoted 6 lines

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Quoted 5 lines
Quoted 5 lines

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

Quoted 4 lines

Uff, I don't think we can do that.

That is massively platform specific behavior in a non platform specific code.

Quoted 8 lines

What we really need is an allocator API that does all this for the caller.

It has been pointed out a few times, Aneesh maybe you need to try to tackle that?

dmabuf heap just wants

'allocate me an array of shared folios totaling XX bytes'

Yeah, I know at least two other use cases for that as well.

Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

Christian.

Quoted 4 lines

And solve the double/triple zeroing problem.

Jason

#13 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 6 lines · Christian König
Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

This isn't a dma allocation, this is a memory allocation.

We made things so the CC shared folios are DMA mappable through the DMA API. From the heap perspective the only difference is passing an additional DMA_ATTR_CC_SHARED when DMA mapping the folio.

Jason

maintainer
#14 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines · Christian König
Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

This isn't a dma allocation, this is a memory allocation.

No, I mean this is a DMA-buf heaps allocation. It is a DMA allocation, we just don't know for which device.

So ideally we should use resources which work for most devices in the system. If a device has special allocation requirements (CMA, restricted addressing etc...) we need a specialized DMA-buf heaps for it.

That userspace provides this cc_shared flag is a NO-GO to begin with.

Regards,
Christian.

Quoted 5 lines

We made things so the CC shared folios are DMA mappable through the DMA API. From the heap perspective the only difference is passing an additional DMA_ATTR_CC_SHARED when DMA mapping the folio.

Jason

#15 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 6 lines · Christian König
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Christian König
Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

This isn't a dma allocation, this is a memory allocation.

No, I mean this is a DMA-buf heaps allocation. It is a DMA allocation, we just don't know for which device.

So? How is it any different from the existing alloc pages?

Quoted 2 lines

So ideally we should use resources which work for most devices in the system.

Which this does.

Quoted 3 lines

If a device has special allocation requirements (CMA, restricted addressing etc...) we need a specialized DMA-buf heaps for it.

Those things don't really intersect with CC, but if they did their are already heap names to request those, someone can add some shared restricted CMA option if they need someday ?

Quoted 2 lines

That userspace provides this cc_shared flag is a NO-GO to begin with.

What do you mean? We discussed this with the heap maintainers and we all agreed this was a kind of heap just like any of the other kinds of heaps that userspace can request.

It is *exactly* the "special allocation requirements" you are talking about above.

Jason

maintainer
#16 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 11 lines · Jason Gunthorpe
Quoted 10 lines · Christian König
Quoted 9 lines · Jason Gunthorpe
Quoted 6 lines · Christian König
Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

This isn't a dma allocation, this is a memory allocation.

No, I mean this is a DMA-buf heaps allocation. It is a DMA allocation, we just don't know for which device.

So? How is it any different from the existing alloc pages?

That you need to specify that this is for DMA without encryption.

Quoted 16 lines
Quoted 2 lines

So ideally we should use resources which work for most devices in the system.

Which this does.

Quoted 3 lines

If a device has special allocation requirements (CMA, restricted addressing etc...) we need a specialized DMA-buf heaps for it.

Those things don't really intersect with CC, but if they did their are already heap names to request those, someone can add some shared restricted CMA option if they need someday ?

Quoted 2 lines

That userspace provides this cc_shared flag is a NO-GO to begin with.

Quoted 6 lines

What do you mean? We discussed this with the heap maintainers and we all agreed this was a kind of heap just like any of the other kinds of heaps that userspace can request.

It is *exactly* the "special allocation requirements" you are talking about above.

I only see a fraction of the patch set (because no DMA-buf maintainer was CCed) and to me it looked like the cc_shared flag was added as an additional parameter to the allocation and not a separate heap created.

If that is not the case then it is actually the correct approach.

Christian.

Quoted 2 lines

Jason

#17 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 12 lines · Christian König
Quoted 11 lines · Jason Gunthorpe
Quoted 10 lines · Christian König
Quoted 9 lines · Jason Gunthorpe
Quoted 6 lines · Christian König
Quoted 2 lines

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

The fundamental problem is that DMA allocations are highly architecture and device specific while Linux memory allocation APIs are generic.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

This isn't a dma allocation, this is a memory allocation.

No, I mean this is a DMA-buf heaps allocation. It is a DMA allocation, we just don't know for which device.

So? How is it any different from the existing alloc pages?

That you need to specify that this is for DMA without encryption.

It is not for "DMA without encryption". It creates shared pages in a CC guest which *every* guest device can DMA from.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

That userspace provides this cc_shared flag is a NO-GO to begin with.

What do you mean? We discussed this with the heap maintainers and we all agreed this was a kind of heap just like any of the other kinds of heaps that userspace can request.

Quoted 7 lines
Quoted 2 lines

It is *exactly* the "special allocation requirements" you are talking about above.

I only see a fraction of the patch set (because no DMA-buf maintainer was CCed) and to me it looked like the cc_shared flag was added as an additional parameter to the allocation and not a separate heap created.

?

It was merged months ago, and you were CC'd:

https://lore.kernel.org/all/[email protected]/

It follows the design the dma heaps maintiners guided to create a new named heap:

+ exp_info.name = "system_cc_shared";
+ exp_info.priv = &system_heap_cc_shared_priv;

Along side the normal heap:

exp_info.name = "system";

It is identical to "system" except the pages are "CC shared" meaning the hypervisor and every DMA device can access them.

This series is just adjusting the rules around how set_memory_decrypted() works. It isn't changing any uAPI or adding anything new to dmabuf heaps.

Yes, it ends up as a flag in the internal code flow, that is just how it got coded..

Jason

maintainer
#18 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

On 9/21/26 14:17, Jason Gunthorpe wrote:
...

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

That userspace provides this cc_shared flag is a NO-GO to begin with.

What do you mean? We discussed this with the heap maintainers and we all agreed this was a kind of heap just like any of the other kinds of heaps that userspace can request.

Quoted 7 lines
Quoted 2 lines

It is *exactly* the "special allocation requirements" you are talking about above.

I only see a fraction of the patch set (because no DMA-buf maintainer was CCed) and to me it looked like the cc_shared flag was added as an additional parameter to the allocation and not a separate heap created.

Quoted 5 lines

?

It was merged months ago, and you were CC'd:

https://lore.kernel.org/all/[email protected]/

Ah! I though the flag was added in this patch set.

Quoted 9 lines

It follows the design the dma heaps maintiners guided to create a new named heap:

(quoted diff) +2 -0
+	exp_info.name = "system_cc_shared";
+	exp_info.priv = &system_heap_cc_shared_priv;

Along side the normal heap:

Quoted 8 lines

exp_info.name = "system";

It is identical to "system" except the pages are "CC shared" meaning the hypervisor and every DMA device can access them.

This series is just adjusting the rules around how set_memory_decrypted() works. It isn't changing any uAPI or adding anything new to dmabuf heaps.

Yeah that was absolutely not clear to me since I only got added to this single patch.

I also don't see where mem_cc_shared_granule_size is added?

Quoted 2 lines

Yes, it ends up as a flag in the internal code flow, that is just how it got coded..

In that case this is probably ok as it is.

Christian.

Quoted 2 lines

Jason

#19 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 1 line · Christian König

I also don't see where mem_cc_shared_granule_size is added?

Patch 1:

https://lore.kernel.org/all/[email protected]/

Aneesh, please be sure to use get_maintainers on series like this so everyone gets cc'd.

Jason

#20 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 11 lines · Jason Gunthorpe
Quoted 1 line · Christian König

I also don't see where mem_cc_shared_granule_size is added?

Patch 1:

https://lore.kernel.org/all/[email protected]/

Aneesh, please be sure to use get_maintainers on series like this so everyone gets cc'd.

For v7, I included only the maintainer entries because the recipient list was getting too long, but I added the mailing lists.

-aneesh

maintainer
#21 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 8 lines · Aneesh Kumar K.V
Quoted 6 lines · Jason Gunthorpe
Quoted 1 line · Christian König

I also don't see where mem_cc_shared_granule_size is added?

Patch 1:

Quoted 7 lines
Quoted 4 lines

https://lore.kernel.org/all/[email protected]/

Aneesh, please be sure to use get_maintainers on series like this so everyone gets cc'd.

For v7, I included only the maintainer entries because the recipient list was getting too long, but I added the mailing lists.

I could not see Christian in the recipients of v7. Sometimes the list cc is enough but my inbox for example is already around 4000 new email, so easy to miss.

Recommendation: add Cc: lines to each patch with the people that must ack your changes (the maintainers for those specific patches). You can then collate all the cc's from your patches, add them to the cover letter and use --cc-cover with git send-email.

Of course, the list can get large. You can then make a call with who to
drop: colleagues you talk to regularly can be skipped for example, maybe reviewers if there are too many for a subsystem. I would not drop people that gave you feedback already on a previous version and not the maintainers you need acks from (you can drop them if they already acked your patches).

Signature · 1 line
Catalin
#22 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Aneesh Kumar K.V
Quoted 10 lines · Jason Gunthorpe
Quoted 1 line · Christian Knig

I also don't see where mem_cc_shared_granule_size is added?

Patch 1:

https://lore.kernel.org/all/[email protected]/

Aneesh, please be sure to use get_maintainers on series like this so everyone gets cc'd.

Quoted 7 lines
Quoted 2 lines

For v7, I included only the maintainer entries because the recipient list was getting too long, but I added the mailing lists.

I could not see Christian in the recipients of v7. Sometimes the list cc is enough but my inbox for example is already around 4000 new email, so easy to miss.

He is already on the Cc list for v7.

Quoted 6 lines

Recommendation: add Cc: lines to each patch with the people that must ack your changes (the maintainers for those specific patches). You can then collate all the cc's from your patches, add them to the cover letter and use --cc-cover with git send-email.

I'll do this

Quoted 8 lines

Of course, the list can get large. You can then make a call with who to
drop: colleagues you talk to regularly can be skipped for example, maybe reviewers if there are too many for a subsystem. I would not drop people that gave you feedback already on a previous version and not the maintainers you need acks from (you can drop them if they already acked your patches).

sure.

-aneesh

#23 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

In reply to Jason Gunthorpe
Quoted 29 lines · Jason Gunthorpe
Quoted 18 lines · Christian König
Quoted 2 lines · Catalin Marinas

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Yeah, thanks for doing this.

Quoted 7 lines · Aneesh Kumar K.V (Arm)
Quoted 6 lines

The system heap can allocate buffers that are decrypted and shared with the host. For confidential-computing guests, those shared buffers must cover whole shared-buffer granule; otherwise a userspace mmap of the dma-buf may expose only part of a host-managed granule and allow unintended access to adjacent private memory.

Quoted 5 lines
Quoted 5 lines

Require cc-shared system-heap allocations to have a size aligned to mem_cc_shared_granule_size(), and allocate pages at least as large as the required granule. Keep the allocation bounded by the existing heap orders, but fall back to an exact minimum-order allocation when the required granule is not one of the preferred heap orders.

Quoted 4 lines

Uff, I don't think we can do that.

That is massively platform specific behavior in a non platform specific code.

What we really need is an allocator API that does all this for the caller.

It has been pointed out a few times, Aneesh maybe you need to try to tackle that?

Quoted 9 lines

dmabuf heap just wants

'allocate me an array of shared folios totaling XX bytes'

Arch code can figure out how to do it. If some ARM configs only give order 4 folios or whatever then dmabuf heap doesn't care.

And solve the double/triple zeroing problem.

Agreed. Moving all the CoCo shared-allocation rules into one place makes this easier to follow.

+static struct page *system_heap_alloc_order(unsigned int order, gfp_t flags,
+ bool cc_shared)
+{
+ struct cc_shared_pages mem;
+
+ if (!cc_shared)
+ return alloc_pages(flags, order);
+
+ /* The shared granule can raise the actual allocation order. */
+ flags |= __GFP_COMP;
+ if (alloc_cc_shared_pages(flags, PAGE_SIZE << order, &mem))
+ return NULL;
+
+ return mem.page;
+}
+
static struct page *alloc_largest_available(unsigned long size,

-					    unsigned int max_order)
+					    unsigned int max_order,
+					    bool cc_shared)
 {
 	struct page *page;
	int i;

gfp_t flags;

for (i = 0; i < NUM_ORDERS; i++) {
if (size < (PAGE_SIZE << orders[i]))
continue;
if (max_order < orders[i])
continue;
flags = order_flags[i];
if (mem_accounting)
flags |= __GFP_ACCOUNT;
- page = alloc_pages(flags, orders[i]);
+ page = system_heap_alloc_order(orders[i], flags, cc_shared);
if (!page)
continue;
return page;

with mm/cc_shared.c having

int alloc_cc_shared_pages_node(int nid, gfp_t gfp, size_t requested,
struct cc_shared_pages *mem)

{
	struct cc_shared_layout layout;
	struct page *page;
	unsigned int order;
	bool zero = gfp & __GFP_ZERO;
	int ret;

	if (!mem)
		return -EINVAL;

	ret = cc_shared_calc_layout(requested, &layout);
	if (ret)
		return ret;

	order = get_order(layout.shared_size);
	if (order > MAX_PAGE_ORDER)
		return -EINVAL;

	/*
	 * State transitions require a linear-map address and may modify memory.
	 * Allocate from low memory and defer requested zeroing until afterwards.
	 */
	gfp &= ~(__GFP_HIGHMEM | __GFP_ZERO);
	if (nid == NUMA_NO_NODE)
		page = alloc_pages(gfp, order);
	else
		page = alloc_pages_node(nid, gfp, order);
	if (!page)
		return -ENOMEM;

	ret = cc_make_shared(page_address(page), layout.shared_size);
	if (ret) {
		if (!cc_make_private(page_address(page), layout.shared_size))
			__free_pages(page, order);
		else
			pr_warn_ratelimited("leaking %zu bytes with uncertain shared state\n",
					    layout.shared_size);
		return ret;
	}

	if (zero)
		memset(page_address(page), 0, layout.shared_size);

	mem->page = page;
	mem->shared_size = layout.shared_size;
	return 0;
}

-aneesh

#24 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Review

Quoted 12 lines · Aneesh Kumar K.V
(quoted diff) +12 -0
+static struct page *system_heap_alloc_order(unsigned int order, gfp_t flags,
+					    bool cc_shared)
+{
+	struct cc_shared_pages mem;
+
+	if (!cc_shared)
+		return alloc_pages(flags, order);
+
+	/* The shared granule can raise the actual allocation order. */
+	flags |= __GFP_COMP;
+	if (alloc_cc_shared_pages(flags, PAGE_SIZE << order, &mem))
+		return NULL;

Oh yeah that's surprisingly easy because of how this loop works with order

Makes sense to me

Quoted 2 lines

int alloc_cc_shared_pages_node(int nid, gfp_t gfp, size_t requested,
struct cc_shared_pages *mem)

This argument order is a little weird from mm norms?

Jason

#25 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

Quoted 18 lines · Jason Gunthorpe

Review

Quoted 12 lines · Aneesh Kumar K.V
(quoted diff) +12 -0
+static struct page *system_heap_alloc_order(unsigned int order, gfp_t flags,
+					    bool cc_shared)
+{
+	struct cc_shared_pages mem;
+
+	if (!cc_shared)
+		return alloc_pages(flags, order);
+
+	/* The shared granule can raise the actual allocation order. */
+	flags |= __GFP_COMP;
+	if (alloc_cc_shared_pages(flags, PAGE_SIZE << order, &mem))
+		return NULL;

Oh yeah that's surprisingly easy because of how this loop works with order

Quoted 7 lines

Makes sense to me

Quoted 2 lines

int alloc_cc_shared_pages_node(int nid, gfp_t gfp, size_t requested,
struct cc_shared_pages *mem)

This argument order is a little weird from mm norms?

That is similar to

static inline struct page *alloc_pages_node_noprof(int nid, gfp_t gfp_mask,
unsigned int order)

-aneesh

#26 lore raw

Re: [PATCH v6 7/9] dma-buf: system_heap: Enforce shared-granule alignment for cc-shared buffers

In reply to Catalin Marinas
Quoted 5 lines · Catalin Marinas

First, none of the DMA-BUF maintainers have been cc'ed. It might be fine for an RFC but this series got to version 6. We need their feedback.

Quoted 1 line · Aneesh Kumar K.V (Arm)

The system heap can allocate buffers that are decrypted and shared with the

[ ... 107 lines skipped ... ]

Quoted 5 lines

You need to get a proper base so that Sashiko can review this series. I ran it through Claude and the above is wrong to go to free_buffer which walks over the pages list that has not been initialised yet.

There is a dependency on the file-movement changes in the arm-smccc bus patch series. This was done because we agreed to move the RSI changes out of arch/arm64 before considering further CCA-related changes for merging.

Do you have any suggestions on how to handle this? I could include the dependent patches when posting the new series so that Sashiko has a base on which to apply it, and clearly mark those patches as not for merging.

-aneesh

#27 lore raw

PATCH v6 8/9 arm64: realm: Add RHI helper to query IPA state change alignment

In reply to Aneesh Kumar K.V (Arm)

Arm CCA guests need to know the granularity at which the host expects IPA state changes to be performed. This can be larger than the guest page size and is needed when deciding the alignment for memory shared with the host.

Add the Realm Host Interface host configuration definitions and an get_ipa_state_change_alignment() helper. The helper uses RSI_HOST_CALL to query the supported HostConf version and features, reads the IPA change alignment when available, and falls back to PAGE_SIZE if the interface is unavailable or returns an invalid value.

---
 drivers/firmware/arm_rmm/rsi.c | 46 ++++++++++++++++++++++++++++++++++
 include/linux/arm-rsi-cmds.h   | 10 ++++++++
 include/linux/arm-smccc-rhi.h  | 25 ++++++++++++++++++
 include/linux/arm-smccc-rsi.h  |  7 ++++++
 4 files changed, 88 insertions(+)
 create mode 100644 include/linux/arm-smccc-rhi.h
drivers/firmware/arm_rmm/rsi.c +46 -0
index 52f40256bd78..2cd53f82432f 100644
@@ -9,6 +9,7 @@
 #include <linux/swiotlb.h>
 #include <linux/platform_device.h>
 #include <linux/arm-rsi-cmds.h>
+#include <linux/arm-smccc-rhi.h>
 #include <linux/kobject.h>
 #include <linux/sysfs.h>
 
@@ -164,6 +165,51 @@ static int realm_register_memory_enc_ops(void)
 	return arm64_mem_crypt_ops_register(&realm_crypt_ops);
 }
 
+/* we need an aligned struct for rsi_host_call. slab is not yet ready */
+static struct rsi_host_call hostconf_call __initdata;
+static unsigned long __maybe_unused __init get_ipa_state_change_alignment(void)
+{
+	long ret;
+	unsigned long shared_granule_size;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_VERSION;
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	if (hostconf_call.gprs[0] != RHI_HOSTCONF_VER_1_0)
+		goto err_out;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_FEATURES;
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	if (!(hostconf_call.gprs[0] & __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT))
+		goto err_out;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT;
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	shared_granule_size = hostconf_call.gprs[0];
+	if (shared_granule_size & (SZ_4K - 1) ||
+	    !is_power_of_2(shared_granule_size))
+		goto err_out;
+
+	return max(PAGE_SIZE, shared_granule_size);
+err_out:
+	/*
+	 * For failure condition assume host is built with 4K page size
+	 * and hence IPA state change alignment can be guest PAGE_SIZE.
+	 */
+	return PAGE_SIZE;
+}
+
 void __init arm64_rsi_init(void)
 {
 	if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_SMC)
include/linux/arm-rsi-cmds.h +10 -0
index 3f7a6a833993..996f1621b996 100644
@@ -236,4 +236,14 @@ static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule,
 	return res.a0;
 }
 
+static inline unsigned long rsi_host_call(struct rsi_host_call *rhi_call)
+{
+	phys_addr_t addr = virt_to_phys(rhi_call);
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RSI_HOST_CALL, addr, &res);
+
+	return res.a0;
+}
+
 #endif /* __LINUX_ARM_RSI_CMDS_H_ */
include/linux/arm-smccc-rhi.h new file +25 -0
new file mode 100644
index 000000000000..91a29996d72d
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#ifndef __LINUX_ARM_SMCCC_RHI_H_
+#define __LINUX_ARM_SMCCC_RHI_H_
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RHI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD_HYP,\
+			   (func))
+
+#define RHI_HOSTCONF_VER_1_0		0x10000
+#define RHI_HOSTCONF_VERSION		SMC_RHI_CALL(0x004E)
+
+#define __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT BIT(0)
+#define RHI_HOSTCONF_FEATURES		SMC_RHI_CALL(0x004F)
+
+#define RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT	SMC_RHI_CALL(0x0050)
+
+#endif /* __LINUX_ARM_SMCCC_RHI_H_ */
include/linux/arm-smccc-rsi.h +7 -0
index fddb77986f70..3532a3f08f4e 100644
@@ -182,6 +182,13 @@ struct realm_config {
  */
 #define SMC_RSI_IPA_STATE_GET			SMC_RSI_FID(0x198)
 
+struct rsi_host_call {
+	union {
+		u16 imm;
+		u64 padding0;
+	};
+	u64 gprs[31];
+} __aligned(0x100);
 /*
  * Make a Host call.
  *
Signature · 1 line
2.43.0
#28 lore raw

Re: [PATCH v6 8/9] arm64: realm: Add RHI helper to query IPA state change alignment

Quoted 11 lines · Aneesh Kumar K.V (Arm)

Arm CCA guests need to know the granularity at which the host expects IPA state changes to be performed. This can be larger than the guest page size and is needed when deciding the alignment for memory shared with the host.

Add the Realm Host Interface host configuration definitions and an get_ipa_state_change_alignment() helper. The helper uses RSI_HOST_CALL to query the supported HostConf version and features, reads the IPA change alignment when available, and falls back to PAGE_SIZE if the interface is unavailable or returns an invalid value.

It may be worth mentioning the document number and may be the link for RHI spec.

e.g., The Realm Host Interface specification Arm DEN00148 defines ...

Quoted 23 lines
---
  drivers/firmware/arm_rmm/rsi.c | 46 ++++++++++++++++++++++++++++++++++
  include/linux/arm-rsi-cmds.h   | 10 ++++++++
  include/linux/arm-smccc-rhi.h  | 25 ++++++++++++++++++
  include/linux/arm-smccc-rsi.h  |  7 ++++++
  4 files changed, 88 insertions(+)

create mode 100644 include/linux/arm-smccc-rhi.h

drivers/firmware/arm_rmm/rsi.c +1 -0
index 52f40256bd78..2cd53f82432f 100644
@@ -9,6 +9,7 @@
  #include <linux/swiotlb.h>
  #include <linux/platform_device.h>
  #include <linux/arm-rsi-cmds.h>
+#include <linux/arm-smccc-rhi.h>
  #include <linux/kobject.h>
  #include <linux/sysfs.h>
 
@@ -164,6 +165,51 @@ static int realm_register_memory_enc_ops(void)
  	return arm64_mem_crypt_ops_register(&realm_crypt_ops);
  }

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 176

Quoted 10 lines
drivers/firmware/arm_rmm/rsi.c +9 -0
 
+/* we need an aligned struct for rsi_host_call. slab is not yet ready */
+static struct rsi_host_call hostconf_call __initdata;
+static unsigned long __maybe_unused __init get_ipa_state_change_alignment(void)
+{
+	long ret;
+	unsigned long shared_granule_size;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_VERSION;

Do we need to 0 out the other GPRs for RES0 arguments ?

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 185

Quoted 9 lines
drivers/firmware/arm_rmm/rsi.c +9 -0
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	if (hostconf_call.gprs[0] != RHI_HOSTCONF_VER_1_0)
+		goto err_out;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_FEATURES;

Same here, especially gprs[] could be overwritten by the results from the Host for the previous call.

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 195

Quoted 10 lines
drivers/firmware/arm_rmm/rsi.c +10 -0
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	if (!(hostconf_call.gprs[0] & __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT))
+		goto err_out;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT;
+	ret = rsi_host_call(lm_alias(&hostconf_call));

minor nit: This patterns keep repeating. May be we could define a macro or something to make it cleaner ?

Quoted 55 lines
drivers/firmware/arm_rmm/rsi.c +17 -0
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	shared_granule_size = hostconf_call.gprs[0];
+	if (shared_granule_size & (SZ_4K - 1) ||
+	    !is_power_of_2(shared_granule_size))
+		goto err_out;
+
+	return max(PAGE_SIZE, shared_granule_size);
+err_out:
+	/*
+	 * For failure condition assume host is built with 4K page size
+	 * and hence IPA state change alignment can be guest PAGE_SIZE.
+	 */
+	return PAGE_SIZE;
+}
+
  void __init arm64_rsi_init(void)
  {
  	if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_SMC)
include/linux/arm-rsi-cmds.h +10 -0
index 3f7a6a833993..996f1621b996 100644
@@ -236,4 +236,14 @@ static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule,
  	return res.a0;
  }
 
+static inline unsigned long rsi_host_call(struct rsi_host_call *rhi_call)
+{
+	phys_addr_t addr = virt_to_phys(rhi_call);
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RSI_HOST_CALL, addr, &res);
+
+	return res.a0;
+}
+
  #endif /* __LINUX_ARM_RSI_CMDS_H_ */
include/linux/arm-smccc-rhi.h new file +10 -0
new file mode 100644
index 000000000000..91a29996d72d
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#ifndef __LINUX_ARM_SMCCC_RHI_H_
+#define __LINUX_ARM_SMCCC_RHI_H_
+
+#include <linux/arm-smccc.h>
+

include/linux/arm-smccc-rhi.h line 20

Quoted 10 lines
include/linux/arm-smccc-rhi.h new file +10 -0
+#define SMC_RHI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD_HYP,\
+			   (func))
+
+#define RHI_HOSTCONF_VER_1_0		0x10000
+#define RHI_HOSTCONF_VERSION		SMC_RHI_CALL(0x004E)
+
+#define __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT BIT(0)

minor nit: Could this be:

RHI_HOSTCONF_FEATURE_GET_IPA_CHANGE_ALIGNMENT BIT(0)

to indicate this applies to the RHI_HOSTCONF_FEATURES output ?

include/linux/arm-smccc-rhi.h line 23

Quoted 3 lines
include/linux/arm-smccc-rhi.h new file +3 -0
+#define RHI_HOSTCONF_FEATURES		SMC_RHI_CALL(0x004F)
+
+#define RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT	SMC_RHI_CALL(0x0050)

Cheers
Suzuki

Quoted 20 lines
include/linux/arm-smccc-rhi.h new file +2 -0
+
+#endif /* __LINUX_ARM_SMCCC_RHI_H_ */
include/linux/arm-smccc-rsi.h +7 -0
index fddb77986f70..3532a3f08f4e 100644
@@ -182,6 +182,13 @@ struct realm_config {
   */
  #define SMC_RSI_IPA_STATE_GET			SMC_RSI_FID(0x198)
 
+struct rsi_host_call {
+	union {
+		u16 imm;
+		u64 padding0;
+	};
+	u64 gprs[31];
+} __aligned(0x100);
  /*
   * Make a Host call.
   *
#29 lore raw

Re: [PATCH v6 8/9] arm64: realm: Add RHI helper to query IPA state change alignment

Quoted 56 lines · Suzuki K Poulose
Quoted 11 lines · Aneesh Kumar K.V (Arm)

Arm CCA guests need to know the granularity at which the host expects IPA state changes to be performed. This can be larger than the guest page size and is needed when deciding the alignment for memory shared with the host.

Add the Realm Host Interface host configuration definitions and an get_ipa_state_change_alignment() helper. The helper uses RSI_HOST_CALL to query the supported HostConf version and features, reads the IPA change alignment when available, and falls back to PAGE_SIZE if the interface is unavailable or returns an invalid value.

It may be worth mentioning the document number and may be the link for RHI spec.

e.g., The Realm Host Interface specification Arm DEN00148 defines ...

Quoted 23 lines
---
  drivers/firmware/arm_rmm/rsi.c | 46 ++++++++++++++++++++++++++++++++++
  include/linux/arm-rsi-cmds.h   | 10 ++++++++
  include/linux/arm-smccc-rhi.h  | 25 ++++++++++++++++++
  include/linux/arm-smccc-rsi.h  |  7 ++++++
  4 files changed, 88 insertions(+)

create mode 100644 include/linux/arm-smccc-rhi.h

drivers/firmware/arm_rmm/rsi.c +1 -0
index 52f40256bd78..2cd53f82432f 100644
@@ -9,6 +9,7 @@
  #include <linux/swiotlb.h>
  #include <linux/platform_device.h>
  #include <linux/arm-rsi-cmds.h>
+#include <linux/arm-smccc-rhi.h>
  #include <linux/kobject.h>
  #include <linux/sysfs.h>
 
@@ -164,6 +165,51 @@ static int realm_register_memory_enc_ops(void)
  	return arm64_mem_crypt_ops_register(&realm_crypt_ops);
  }

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 176

Quoted 10 lines
drivers/firmware/arm_rmm/rsi.c +9 -0
 
+/* we need an aligned struct for rsi_host_call. slab is not yet ready */
+static struct rsi_host_call hostconf_call __initdata;
+static unsigned long __maybe_unused __init get_ipa_state_change_alignment(void)
+{
+	long ret;
+	unsigned long shared_granule_size;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_VERSION;

Do we need to 0 out the other GPRs for RES0 arguments ?

Quoted 13 lines

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 185

Quoted 9 lines
drivers/firmware/arm_rmm/rsi.c +9 -0
+	ret = rsi_host_call(lm_alias(&hostconf_call));
+	if (ret != RSI_SUCCESS)
+		goto err_out;
+
+	if (hostconf_call.gprs[0] != RHI_HOSTCONF_VER_1_0)
+		goto err_out;
+
+	hostconf_call.imm = 0;
+	hostconf_call.gprs[0] = RHI_HOSTCONF_FEATURES;

Same here, especially gprs[] could be overwritten by the results from the Host for the previous call.

[ ... 75 lines skipped ... ]

Quoted 8 lines

drivers/firmware/arm_rmm/rsi.c static int realm_register_memory_enc_ops(void) · line 190

Quoted 5 lines
drivers/firmware/arm_rmm/rsi.c +5 -0
+
+#define RHI_HOSTCONF_VER_1_0		0x10000
+#define RHI_HOSTCONF_VERSION		SMC_RHI_CALL(0x004E)
+
+#define __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT BIT(0)

minor nit: Could this be:

Quoted 4 lines

RHI_HOSTCONF_FEATURE_GET_IPA_CHANGE_ALIGNMENT BIT(0)

to indicate this applies to the RHI_HOSTCONF_FEATURES output ?

Will make this change.

- hostconf_call.imm = 0;
+ memset(&hostconf_call, 0, sizeof(hostconf_call));
hostconf_call.gprs[0] = RHI_HOSTCONF_FEATURES; ret = rsi_host_call(lm_alias(&hostconf_call)); if (ret != RSI_SUCCESS)
goto err_out;

- if (!(hostconf_call.gprs[0] & __RHI_HOSTCONF_GET_IPA_CHANGE_ALIGNMENT))
+ if (!(hostconf_call.gprs[0] & RHI_HOSTCONF_FEATURE_GET_IPA_CHANGE_ALIGNMENT))
goto err_out;

-aneesh

#30 lore raw

PATCH v6 9/9 arm64: realm: Expose the CCA shared granule size through mem_encrypt ops

In reply to Aneesh Kumar K.V (Arm)

CCA guests must align shared/private memory transitions to the size reported by the host for IPA state changes. This size can be larger than PAGE_SIZE, so allowing a guest to convert only a PAGE_SIZE-sized subrange can leave the remaining part of that host-sized range in the wrong state.

Cache the RHI-reported IPA sate change alignment during Realm initialization and expose it through a new arm64 mem_encrypt callback. Use PAGE_SIZE as the default shared granule size when no backend callback is registered.

Validate both the address and byte size passed to set_memory_encrypted() and set_memory_decrypted() against mem_cc_shared_granule_size() before calling into the backend. This prevents callers from converting only part of a host-managed page.

---
 arch/arm64/include/asm/mem_encrypt.h |  4 ++++
 arch/arm64/mm/mem_encrypt.c          | 32 ++++++++++++++++++++++++----
 drivers/firmware/arm_rmm/rsi.c       | 13 ++++++++++-
 3 files changed, 44 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mem_encrypt.h +4 -0
index ef8b8463e52b..890faeed0eb3 100644
@@ -10,6 +10,7 @@ struct device;
 struct arm64_mem_crypt_ops {
 	int (*encrypt)(unsigned long addr, int numpages);
 	int (*decrypt)(unsigned long addr, int numpages);
+	size_t (*cc_shared_granule_size)(void);
 };
 
 int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops);
@@ -18,6 +19,9 @@ int set_memory_encrypted(unsigned long addr, int numpages);
 int set_memory_decrypted(unsigned long addr, int numpages);
 int __set_memory_enc_dec(unsigned long addr, int numpages, bool encrypt);
 
+#define mem_cc_shared_granule_size mem_cc_shared_granule_size
+size_t mem_cc_shared_granule_size(void);
+
 static inline bool force_dma_unencrypted(struct device *dev)
 {
 	return is_realm_world() || is_protected_kvm_guest();
arch/arm64/mm/mem_encrypt.c +28 -4
index ee3c0ab04384..69783c6a3c08 100644
@@ -17,8 +17,7 @@
 #include <linux/compiler.h>
 #include <linux/err.h>
 #include <linux/mm.h>
-
-#include <asm/mem_encrypt.h>
+#include <linux/mem_encrypt.h>
 
 static const struct arm64_mem_crypt_ops *crypt_ops;
 
@@ -33,18 +32,43 @@ int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops)
 
 int set_memory_encrypted(unsigned long addr, int numpages)
 {
-	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
+	unsigned long size = (unsigned long)numpages << PAGE_SHIFT;
+
+	if (likely(!crypt_ops))
 		return 0;
 
+	if (WARN_ON(!IS_ALIGNED(addr, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(size, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
 	return crypt_ops->encrypt(addr, numpages);
 }
 EXPORT_SYMBOL_GPL(set_memory_encrypted);
 
 int set_memory_decrypted(unsigned long addr, int numpages)
 {
-	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
+	unsigned long size = (unsigned long)numpages << PAGE_SHIFT;
+
+	if (likely(!crypt_ops))
 		return 0;
 
+	if (WARN_ON(!IS_ALIGNED(addr, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(size, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
 	return crypt_ops->decrypt(addr, numpages);
 }
 EXPORT_SYMBOL_GPL(set_memory_decrypted);
+
+size_t mem_cc_shared_granule_size(void)
+{
+	if (likely(!crypt_ops) || !crypt_ops->cc_shared_granule_size)
+		return PAGE_SIZE;
+
+	return crypt_ops->cc_shared_granule_size();
+}
+EXPORT_SYMBOL_GPL(mem_cc_shared_granule_size);
drivers/firmware/arm_rmm/rsi.c +12 -1
index 2cd53f82432f..eeb797c6c2c7 100644
@@ -19,6 +19,7 @@
 
 static struct realm_config config;
 static struct kobject *cca_kobj;
+static unsigned long ipa_state_change_granule_size;
 
 unsigned long prot_ns_shared;
 EXPORT_SYMBOL(prot_ns_shared);
@@ -155,9 +156,17 @@ static int realm_set_memory_decrypted(unsigned long addr, int numpages)
 	return ret;
 }
 
+static size_t realm_cc_shared_granule_size(void)
+{
+	if (is_realm_world())
+		return ipa_state_change_granule_size;
+	return PAGE_SIZE;
+}
+
 static const struct arm64_mem_crypt_ops realm_crypt_ops = {
 	.encrypt = realm_set_memory_encrypted,
 	.decrypt = realm_set_memory_decrypted,
+	.cc_shared_granule_size = realm_cc_shared_granule_size,
 };
 
 static int realm_register_memory_enc_ops(void)
@@ -167,7 +176,7 @@ static int realm_register_memory_enc_ops(void)
 
 /* we need an aligned struct for rsi_host_call. slab is not yet ready */
 static struct rsi_host_call hostconf_call __initdata;
-static unsigned long __maybe_unused __init get_ipa_state_change_alignment(void)
+static unsigned long __init get_ipa_state_change_alignment(void)
 {
 	long ret;
 	unsigned long shared_granule_size;
@@ -218,6 +227,8 @@ void __init arm64_rsi_init(void)
 		return;
 	if (WARN_ON(rsi_get_realm_config(lm_alias(&config))))
 		return;
+
+	ipa_state_change_granule_size = get_ipa_state_change_alignment();
 	prot_ns_shared = __phys_to_pte_val(BIT(config.ipa_bits - 1));
 
 	if (arm64_ioremap_prot_hook_register(realm_ioremap_hook))
Signature · 1 line
2.43.0
reviewer Reviewed-by on v6 9/9
#31 lore raw

Re: [PATCH v6 9/9] arm64: realm: Expose the CCA shared granule size through mem_encrypt ops

Quoted 10 lines · Aneesh Kumar K.V (Arm)

CCA guests must align shared/private memory transitions to the size reported by the host for IPA state changes. This size can be larger than PAGE_SIZE, so allowing a guest to convert only a PAGE_SIZE-sized subrange can leave the remaining part of that host-sized range in the wrong state.

Cache the RHI-reported IPA sate change alignment during Realm initialization and expose it through a new arm64 mem_encrypt callback. Use PAGE_SIZE as the default shared granule size when no backend callback is registered.

Quoted 6 lines

Validate both the address and byte size passed to set_memory_encrypted() and set_memory_decrypted() against mem_cc_shared_granule_size() before calling into the backend. This prevents callers from converting only part of a host-managed page.

Quoted 136 lines
---
  arch/arm64/include/asm/mem_encrypt.h |  4 ++++
  arch/arm64/mm/mem_encrypt.c          | 32 ++++++++++++++++++++++++----
  drivers/firmware/arm_rmm/rsi.c       | 13 ++++++++++-
  3 files changed, 44 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mem_encrypt.h +4 -0
index ef8b8463e52b..890faeed0eb3 100644
@@ -10,6 +10,7 @@ struct device;
  struct arm64_mem_crypt_ops {
  	int (*encrypt)(unsigned long addr, int numpages);
  	int (*decrypt)(unsigned long addr, int numpages);
+	size_t (*cc_shared_granule_size)(void);
  };
 
  int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops);
@@ -18,6 +19,9 @@ int set_memory_encrypted(unsigned long addr, int numpages);
  int set_memory_decrypted(unsigned long addr, int numpages);
  int __set_memory_enc_dec(unsigned long addr, int numpages, bool encrypt);
 
+#define mem_cc_shared_granule_size mem_cc_shared_granule_size
+size_t mem_cc_shared_granule_size(void);
+
  static inline bool force_dma_unencrypted(struct device *dev)
  {
  	return is_realm_world() || is_protected_kvm_guest();
arch/arm64/mm/mem_encrypt.c +28 -4
index ee3c0ab04384..69783c6a3c08 100644
@@ -17,8 +17,7 @@
  #include <linux/compiler.h>
  #include <linux/err.h>
  #include <linux/mm.h>
-
-#include <asm/mem_encrypt.h>
+#include <linux/mem_encrypt.h>
 
  static const struct arm64_mem_crypt_ops *crypt_ops;
 
@@ -33,18 +32,43 @@ int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops)
 
  int set_memory_encrypted(unsigned long addr, int numpages)
  {
-	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
+	unsigned long size = (unsigned long)numpages << PAGE_SHIFT;
+
+	if (likely(!crypt_ops))
  		return 0;
 
+	if (WARN_ON(!IS_ALIGNED(addr, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(size, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
  	return crypt_ops->encrypt(addr, numpages);
  }
  EXPORT_SYMBOL_GPL(set_memory_encrypted);
 
  int set_memory_decrypted(unsigned long addr, int numpages)
  {
-	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
+	unsigned long size = (unsigned long)numpages << PAGE_SHIFT;
+
+	if (likely(!crypt_ops))
  		return 0;
 
+	if (WARN_ON(!IS_ALIGNED(addr, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(size, mem_cc_shared_granule_size())))
+		return -EINVAL;
+
  	return crypt_ops->decrypt(addr, numpages);
  }
  EXPORT_SYMBOL_GPL(set_memory_decrypted);
+
+size_t mem_cc_shared_granule_size(void)
+{
+	if (likely(!crypt_ops) || !crypt_ops->cc_shared_granule_size)
+		return PAGE_SIZE;
+
+	return crypt_ops->cc_shared_granule_size();
+}
+EXPORT_SYMBOL_GPL(mem_cc_shared_granule_size);
drivers/firmware/arm_rmm/rsi.c +12 -1
index 2cd53f82432f..eeb797c6c2c7 100644
@@ -19,6 +19,7 @@
 
  static struct realm_config config;
  static struct kobject *cca_kobj;
+static unsigned long ipa_state_change_granule_size;
 
  unsigned long prot_ns_shared;
  EXPORT_SYMBOL(prot_ns_shared);
@@ -155,9 +156,17 @@ static int realm_set_memory_decrypted(unsigned long addr, int numpages)
  	return ret;
  }
 
+static size_t realm_cc_shared_granule_size(void)
+{
+	if (is_realm_world())
+		return ipa_state_change_granule_size;
+	return PAGE_SIZE;
+}
+
  static const struct arm64_mem_crypt_ops realm_crypt_ops = {
  	.encrypt = realm_set_memory_encrypted,
  	.decrypt = realm_set_memory_decrypted,
+	.cc_shared_granule_size = realm_cc_shared_granule_size,
  };
 
  static int realm_register_memory_enc_ops(void)
@@ -167,7 +176,7 @@ static int realm_register_memory_enc_ops(void)
 
  /* we need an aligned struct for rsi_host_call. slab is not yet ready */
  static struct rsi_host_call hostconf_call __initdata;
-static unsigned long __maybe_unused __init get_ipa_state_change_alignment(void)
+static unsigned long __init get_ipa_state_change_alignment(void)
  {
  	long ret;
  	unsigned long shared_granule_size;
@@ -218,6 +227,8 @@ void __init arm64_rsi_init(void)
  		return;
  	if (WARN_ON(rsi_get_realm_config(lm_alias(&config))))
  		return;
+
+	ipa_state_change_granule_size = get_ipa_state_change_alignment();
  	prot_ns_shared = __phys_to_pte_val(BIT(config.ipa_bits - 1));
 
  	if (arm64_ioremap_prot_hook_register(realm_ioremap_hook))
maintainer
#32 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 42 lines · Aneesh Kumar K.V (Arm)

This series tightens the alignment requirements for buffers that are shared between confidential-computing guests and the host.

When a guest runs with private memory, buffers shared with the hypervisor are not only accessed by the guest. They are also accessed by the host kernel, and the host may manage the corresponding shared/private state at a granularity larger than the guest page size.

This matters for CCA systems where the Realm stage-2 mappings managed by the RMM can still operate at 4K granularity, while the non-secure host may manage the IPA state change at a larger page size, for example 64K. In that case, allowing a guest to convert and share only a 4K subrange of a host-managed granule is unsafe.

Architectures such as Arm can detect incorrect accesses to Realm physical address space PFNs through GPC faults. However, relying on that as the only line of defence is fragile and can still lead to kernel crashes. The risk is especially visible for shared buffers that are later mmapped into userspace, such as guest_memfd or dma-buf backed allocations. Once userspace can access the mapping, the kernel cannot guarantee that applications will only touch the intended 4K region rather than the whole host page mapped into their address space. Those userspace addresses may also be passed back into the kernel and accessed through the linear map, resulting in a GPC fault.

To avoid this, shared buffers must satisfy two constraints:

- the address must be aligned to the CoCo shared-granule size
- the size must be a multiple of that granule size

The series adds generic helpers for this:

- mem_cc_shared_granule_size()
- mem_cc_align_to_shared_granule()

The generic implementation defaults to PAGE_SIZE. arm64 CCA overrides this by querying the host IPA state change granule size through RHI and exposing that value through the arm64 memory-encryption operations.

The patche series update the main shared-buffer allocation paths that can be used by private-memory guests:

Quoted 11 lines

- arm64 set_memory_encrypted()/set_memory_decrypted() now reject unaligned addresses or sizes.
- GIC ITS shared allocations are rounded to the shared granule size.
- dma-direct and atomic DMA pool allocations use aligned allocation and conversion sizes.
- SWIOTLB pools, including dynamic pools, are allocated and converted at the shared granule size.
- restricted-dma-pool regions are checked and rejected if firmware did not provide a base and size aligned to the shared granule size.
- dma-buf system heap cc-shared allocations require aligned sizes and use at least the required allocation order.

While I understand why you want it, I think that's maintenance burden longer term. It's already touching about seven allocators (ITS, dma-direct, CMA, atomic pool, swiotlb, restricted DMA pools, dma-buf). Although most are in the DMA code, we still need to keep track of their changes and what else may be coming. Can we even test all these combinations in a CCA realm?

I assume for trusted devices we can skip this forced alignment.

TBH, I'm tempted not to support this configuration at all, just fail gracefully if the realm page size doesn't match the host's. However, if that's a real world configuration, I wonder whether could we leverage the swiotlb mechanism but with a separate io_tlb_mem as a shared coco allocator with the right alignment and use this pool when force_dma_unencrypted() instead of specific alloc_pages() with adjusted order.

Signature · 1 line
Catalin
#33 lore raw
Quoted 3 lines · Catalin Marinas

TBH, I'm tempted not to support this configuration at all, just fail gracefully if the realm page size doesn't match the host's. However, if that's a real world configuration,

realm != host is going to be fairly common.

host > realm is probably not that common, I would guess.

Quoted 4 lines

I wonder whether could we leverage the swiotlb mechanism but with a separate io_tlb_mem as a shared coco allocator with the right alignment and use this pool when force_dma_unencrypted() instead of specific alloc_pages() with adjusted order.

I propose we fix the far too low level set memory decrypted apis into proper allocators, with APIs that can handle a round up. Then see where we end up after that.

If we can hide 90% of it in an allocator then lets see if we can open code the rest.

Jason

maintainer
#34 lore raw
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Catalin Marinas

TBH, I'm tempted not to support this configuration at all, just fail gracefully if the realm page size doesn't match the host's. However, if that's a real world configuration,

realm != host is going to be fairly common.

host > realm is probably not that common, I would guess.

It's the latter that's problematic. If we don't think it's common, maybe not worth solving.

Quoted 8 lines
Quoted 4 lines

I wonder whether could we leverage the swiotlb mechanism but with a separate io_tlb_mem as a shared coco allocator with the right alignment and use this pool when force_dma_unencrypted() instead of specific alloc_pages() with adjusted order.

I propose we fix the far too low level set memory decrypted apis into proper allocators, with APIs that can handle a round up. Then see where we end up after that.

I fully agree. As you said, it also solves the double zeroing problem and we can wire up the granule difference handling.

Signature · 1 line
Catalin
#35 lore raw
In reply to Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Catalin Marinas

TBH, I'm tempted not to support this configuration at all, just fail gracefully if the realm page size doesn't match the host's. However, if that's a real world configuration,

realm != host is going to be fairly common.

host > realm is probably not that common, I would guess.

Wouldn't it be more common to run a distro with a 4K page size as the guest on a custom hypervisor with a 4K or larger page size?

-aneesh

#36 lore raw
Quoted 8 lines · Aneesh Kumar K.V
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Catalin Marinas

TBH, I'm tempted not to support this configuration at all, just fail gracefully if the realm page size doesn't match the host's. However, if that's a real world configuration,

Quoted 6 lines
Quoted 3 lines

realm != host is going to be fairly common.

host > realm is probably not that common, I would guess.

Wouldn't it be more common to run a distro with a 4K page size as the guest on a custom hypervisor with a 4K or larger page size?

Maybe someday, but right now I think the 4k hyp is going to be common and people who really reach for 16/64k are going to need to run !4k guests too.

Jason