coco: guest: Enforce host page-size alignment for shared buffers
Subsystems (9)
v5 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]
- https://lore.kernel.org/all/[email protected]
Changes from v4:
https://lore.kernel.org/all/[email protected]
Changes since v4:
* 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.
Aneesh Kumar K.V (Arm) (10):
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: Move Realm memory encryption ops to RSI code
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 | 5 +- arch/arm64/include/asm/rsi_cmds.h | 10 ++++ arch/arm64/kernel/rsi.c | 90 ++++++++++++++++++++++++++++ arch/arm64/mm/mem_encrypt.c | 32 ++++++++-- arch/arm64/mm/pageattr.c | 38 +----------- drivers/dma-buf/heaps/system_heap.c | 50 +++++++++++++--- drivers/iommu/dma-iommu.c | 2 +- drivers/irqchip/irq-gic-v3-its.c | 17 ++++-- 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 ++++++++--- 17 files changed, 300 insertions(+), 78 deletions(-) create mode 100644 include/linux/arm-smccc-rhi.h
Signature · 1 line
2.43.0
PATCH v5 1/10 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
@@ -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
PATCH v5 2/10 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 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +11 -6
@@ -213,16 +213,17 @@ static gfp_t gfp_flags_quirk;static struct page *its_alloc_pages_node (int node,gfp_t gfp,unsigned int order) { +unsigned int alloc_order;struct page *page;int ret =0 ; - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - + alloc_order =get_order (mem_cc_align_to_shared_granule (PAGE_SIZE << order)); + page =alloc_pages_node (node, gfp | gfp_flags_quirk, alloc_order);if (!page)return NULL ; ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); +1 << alloc_order);/* * If set_memory_decrypted() fails then we don't know what state the * page is in, so we can't free it. Instead we leak it.
@@ -241,13 +242,16 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)static void its_free_pages (void *addr,unsigned int order) { +int alloc_order; + + alloc_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. */ -if (set_memory_encrypted ((unsigned long )addr,1 << order)) +if (set_memory_encrypted ((unsigned long )addr,1 << alloc_order))return ; -free_pages ((unsigned long )addr, order); +free_pages ((unsigned long )addr, alloc_order); }static struct gen_pool *itt_pool;
@@ -272,7 +276,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
Marc Zyngier
maintainerRe: [PATCH v5 02/10] irqchip/gic-v3-its: Align shared ITS allocations to the CoCo shared granule size
Quoted 46 lines · 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_SIZEis sufficient.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/irqchip/irq-gic-v3-its.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)drivers/irqchip/irq-gic-v3-its.c +6
-3@@ -213,16 +213,17 @@ static gfp_t gfp_flags_quirk;static struct page *its_alloc_pages_node (int node,gfp_t gfp,unsigned int order) { +unsigned int alloc_order;struct page *page;int ret =0 ; - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - + alloc_order =get_order (mem_cc_align_to_shared_granule (PAGE_SIZE << order)); + page =alloc_pages_node (node, gfp | gfp_flags_quirk, alloc_order);if (!page)return NULL ; ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); +1 << alloc_order);/* * If set_memory_decrypted() fails then we don't know what state the * page is in, so we can't free it. Instead we leak it. @@ -241,13 +242,16 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)static void its_free_pages (void *addr,unsigned int order) { +int alloc_order; +
drivers/irqchip/irq-gic-v3-its.c static struct page *its_alloc_pages(gfp_t gfp, unsigned int order) · line 254
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +3
-2+ alloc_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. */ -if (set_memory_encrypted ((unsigned long )addr,1 << order)) +if (set_memory_encrypted ((unsigned long )addr,1 << alloc_order))return ; -free_pages ((unsigned long )addr, order); +free_pages ((unsigned long )addr, alloc_order);
The comments I had in [1] are still applicable.
M.
[1] https://lore.kernel.org/all/[email protected]/
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v5 02/10] irqchip/gic-v3-its: Align shared ITS allocations to the CoCo shared granule size
Quoted 61 lines · Marc Zyngier
Quoted 46 lines · 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_SIZEis sufficient.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/irqchip/irq-gic-v3-its.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)drivers/irqchip/irq-gic-v3-its.c +6
-3@@ -213,16 +213,17 @@ static gfp_t gfp_flags_quirk;static struct page *its_alloc_pages_node (int node,gfp_t gfp,unsigned int order) { +unsigned int alloc_order;struct page *page;int ret =0 ; - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - + alloc_order =get_order (mem_cc_align_to_shared_granule (PAGE_SIZE << order)); + page =alloc_pages_node (node, gfp | gfp_flags_quirk, alloc_order);if (!page)return NULL ; ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); +1 << alloc_order);/* * If set_memory_decrypted() fails then we don't know what state the * page is in, so we can't free it. Instead we leak it. @@ -241,13 +242,16 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)static void its_free_pages (void *addr,unsigned int order) { +int alloc_order; +drivers/irqchip/irq-gic-v3-its.c static struct page *its_alloc_pages(gfp_t gfp, unsigned int order) · line 254
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +3
-2+ alloc_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. */ -if (set_memory_encrypted ((unsigned long )addr,1 << order)) +if (set_memory_encrypted ((unsigned long )addr,1 << alloc_order))return ; -free_pages ((unsigned long )addr, order); +free_pages ((unsigned long )addr, alloc_order);The comments I had in [1] are still applicable.
Quoted 4 lines
M.
I renamed it to alloc_order to make it clear that it refers to the allocation order rather than the requested order. If you feel strongly about not introducing another variable, I'll drop the new variable.
-aneesh
Marc Zyngier
maintainerRe: [PATCH v5 02/10] irqchip/gic-v3-its: Align shared ITS allocations to the CoCo shared granule size
Quoted 64 lines · Aneesh Kumar K.V
Quoted 61 lines · Marc Zyngier
Quoted 46 lines · 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_SIZEis sufficient.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/irqchip/irq-gic-v3-its.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)drivers/irqchip/irq-gic-v3-its.c +6
-3@@ -213,16 +213,17 @@ static gfp_t gfp_flags_quirk;static struct page *its_alloc_pages_node (int node,gfp_t gfp,unsigned int order) { +unsigned int alloc_order;struct page *page;int ret =0 ; - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - + alloc_order =get_order (mem_cc_align_to_shared_granule (PAGE_SIZE << order)); + page =alloc_pages_node (node, gfp | gfp_flags_quirk, alloc_order);if (!page)return NULL ; ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); +1 << alloc_order);/* * If set_memory_decrypted() fails then we don't know what state the * page is in, so we can't free it. Instead we leak it. @@ -241,13 +242,16 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)static void its_free_pages (void *addr,unsigned int order) { +int alloc_order; +drivers/irqchip/irq-gic-v3-its.c static struct page *its_alloc_pages(gfp_t gfp, unsigned int order) · line 254
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +3
-2+ alloc_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. */ -if (set_memory_encrypted ((unsigned long )addr,1 << order)) +if (set_memory_encrypted ((unsigned long )addr,1 << alloc_order))return ; -free_pages ((unsigned long )addr, order); +free_pages ((unsigned long )addr, alloc_order);The comments I had in [1] are still applicable.
Quoted 8 lines
Quoted 4 lines
M.I renamed it to
alloc_orderto make it clear that it refers to the allocation order rather than the requested order. If you feel strongly about not introducing another variable, I'll drop the new variable.
I do. At the very least, to avoid you changing the type of the order in its_free_pages(). These things do matter.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v5 3/10 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
@@ -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
@@ -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
@@ -400,6 +400,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
@@ -410,7 +411,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
@@ -113,7 +113,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;
@@ -122,7 +122,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)))
@@ -182,7 +182,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 ;
@@ -281,7 +281,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 ;
@@ -447,7 +448,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
@@ -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
PATCH v5 4/10 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
@@ -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
@@ -417,6 +418,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
@@ -280,6 +280,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);
@@ -402,6 +405,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 {
@@ -448,6 +454,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 ;
@@ -488,8 +497,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
@@ -90,7 +90,9 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,bool leak_pages = false;void *addr;int ret = -ENOMEM ; +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 );
@@ -101,7 +103,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
PATCH v5 5/10 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
@@ -287,7 +287,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;
@@ -354,8 +355,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
@@ -363,9 +364,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",
@@ -374,7 +375,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 ; }
@@ -496,7 +497,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 ) {
@@ -505,6 +506,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; }
@@ -585,7 +588,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) {
@@ -629,12 +632,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 ;
@@ -725,6 +730,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
PATCH v5 6/10 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
@@ -2002,6 +2002,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
PATCH v5 7/10 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
@@ -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
PATCH v5 8/10 arm64: realm: Move Realm memory encryption ops to RSI code
In reply to Aneesh Kumar K.V (Arm)Realm memory encryption callbacks are CCA-specific. Keep the Realm callback registration with the RSI initialization code instead of pageattr.c, which only needs to provide the low-level page-attribute transition helper.
Export __set_memory_enc_dec() within arm64 so the RSI code can wrap it with the Realm-specific encrypt/decrypt callbacks and warning policy.
No functional changes in this patch.
--- arch/arm64/include/asm/mem_encrypt.h | 3 +-- arch/arm64/kernel/rsi.c | 34 +++++++++++++++++++++++++ arch/arm64/mm/pageattr.c | 38 +--------------------------- 3 files changed, 36 insertions(+), 39 deletions(-)
arch/arm64/include/asm/mem_encrypt.h +1 -2
@@ -15,8 +15,7 @@ int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops);int set_memory_encrypted (unsigned long addr,int numpages);int set_memory_decrypted (unsigned long addr,int numpages); - -int realm_register_memory_enc_ops (void ); +int __set_memory_enc_dec (unsigned long addr,int numpages,bool encrypt);static inline bool force_dma_unencrypted (struct device *dev) {
arch/arm64/kernel/rsi.c +34 -0
@@ -143,6 +143,40 @@ static int realm_ioremap_hook(phys_addr_t phys, size_t size, pgprot_t *prot)return 0 ; } +static int realm_set_memory_encrypted (unsigned long addr,int numpages) +{ +int ret =__set_memory_enc_dec (addr, numpages, true); + +/* +* If the request to change state fails, then the only sensible cause +* of action for the caller is to leak the memory +*/ +WARN (ret,"Failed to encrypt memory, %d pages will be leaked" , + numpages); + +return ret; +} + +static int realm_set_memory_decrypted (unsigned long addr,int numpages) +{ +int ret =__set_memory_enc_dec (addr, numpages, false); + +WARN (ret,"Failed to decrypt memory, %d pages will be leaked" , + numpages); + +return ret; +} + +static const struct arm64_mem_crypt_ops realm_crypt_ops = { + .encrypt = realm_set_memory_encrypted, + .decrypt = realm_set_memory_decrypted, +}; + +static int realm_register_memory_enc_ops (void ) +{ +return arm64_mem_crypt_ops_register (&realm_crypt_ops); +} +void __init arm64_rsi_init (void ) {if (arm_smccc_1_1_get_conduit () !=SMCCC_CONDUIT_SMC )
arch/arm64/mm/pageattr.c +1 -37
@@ -275,9 +275,7 @@ int set_direct_map_default_noflush(struct page *page) PAGE_SIZE, set_mask, clear_mask); } -static int __set_memory_enc_dec (unsigned long addr, -int numpages, -bool encrypt) +int __set_memory_enc_dec (unsigned long addr,int numpages,bool encrypt) {unsigned long set_prot =0 , clear_prot =0 ;phys_addr_t start, end;
@@ -321,40 +319,6 @@ static int __set_memory_enc_dec(unsigned long addr, __pgprot(PTE_PRESENT_INVALID)); } -static int realm_set_memory_encrypted (unsigned long addr,int numpages) -{ -int ret =__set_memory_enc_dec (addr, numpages, true); - -/* -* If the request to change state fails, then the only sensible cause -* of action for the caller is to leak the memory -*/ -WARN (ret,"Failed to encrypt memory, %d pages will be leaked" , - numpages); - -return ret; -} - -static int realm_set_memory_decrypted (unsigned long addr,int numpages) -{ -int ret =__set_memory_enc_dec (addr, numpages, false); - -WARN (ret,"Failed to decrypt memory, %d pages will be leaked" , - numpages); - -return ret; -} - -static const struct arm64_mem_crypt_ops realm_crypt_ops = { - .encrypt = realm_set_memory_encrypted, - .decrypt = realm_set_memory_decrypted, -}; - -int realm_register_memory_enc_ops (void ) -{ -return arm64_mem_crypt_ops_register (&realm_crypt_ops); -} -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) {unsigned long addr = (unsigned long )page_address (page);
Signature · 1 line
2.43.0
Will Deacon
maintainerRe: [PATCH v5 08/10] arm64: realm: Move Realm memory encryption ops to RSI code
Quoted 61 lines · Aneesh Kumar K.V (Arm)
Realm memory encryption callbacks are CCA-specific. Keep the Realm callback registration with the RSI initialization code instead of
pageattr.c, which only needs to provide the low-level page-attribute transition helper.Export
__set_memory_enc_dec()within arm64 so the RSI code can wrap it with the Realm-specific encrypt/decrypt callbacks and warning policy.No functional changes in this patch.
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- arch/arm64/include/asm/mem_encrypt.h | 3 +-- arch/arm64/kernel/rsi.c | 34 +++++++++++++++++++++++++ arch/arm64/mm/pageattr.c | 38 +--------------------------- 3 files changed, 36 insertions(+), 39 deletions(-)arch/arm64/include/asm/mem_encrypt.h +1
-2@@ -15,8 +15,7 @@ int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops);int set_memory_encrypted (unsigned long addr,int numpages);int set_memory_decrypted (unsigned long addr,int numpages); - -int realm_register_memory_enc_ops (void ); +int __set_memory_enc_dec (unsigned long addr,int numpages,bool encrypt);static inline bool force_dma_unencrypted (struct device *dev) {arch/arm64/kernel/rsi.c +23
-0@@ -143,6 +143,40 @@ static int realm_ioremap_hook(phys_addr_t phys, size_t size, pgprot_t *prot)return 0 ; } +static int realm_set_memory_encrypted (unsigned long addr,int numpages) +{ +int ret =__set_memory_enc_dec (addr, numpages, true); + +/* +* If the request to change state fails, then the only sensible cause +* of action for the caller is to leak the memory +*/ +WARN (ret,"Failed to encrypt memory, %d pages will be leaked" , + numpages); + +return ret; +} + +static int realm_set_memory_decrypted (unsigned long addr,int numpages) +{ +int ret =__set_memory_enc_dec (addr, numpages, false); + +WARN (ret,"Failed to decrypt memory, %d pages will be leaked" , + numpages); + +return ret; +}
arch/arm64/kernel/rsi.c static int realm_ioremap_hook(phys_addr_t phys, size_t size, pgprot_t *prot) · line 178
Quoted 10 lines
arch/arm64/kernel/rsi.c +10
-0+ +static const struct arm64_mem_crypt_ops realm_crypt_ops = { + .encrypt = realm_set_memory_encrypted, + .decrypt = realm_set_memory_decrypted, +}; + +static int realm_register_memory_enc_ops (void ) +{ +return arm64_mem_crypt_ops_register (&realm_crypt_ops); +}
It's good to move these out of pageattr.c but I still don't think any of this stuff should be in arch/arm64. This is a firmware interface, just like PSCI, so it should live in drivers/firmware/ along with all the other Arm firmware interfaces (SDEI, FFA, SCMI, PSCI, SMCCC, ...).
Will
PATCH v5 9/10 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.
--- arch/arm64/include/asm/rsi_cmds.h | 10 +++++++ arch/arm64/kernel/rsi.c | 45 +++++++++++++++++++++++++++++++ include/linux/arm-smccc-rhi.h | 25 +++++++++++++++++ include/linux/arm-smccc-rsi.h | 7 +++++ 4 files changed, 87 insertions(+) create mode 100644 include/linux/arm-smccc-rhi.h
arch/arm64/include/asm/rsi_cmds.h +10 -0
@@ -86,4 +86,14 @@ static inline long rsi_set_addr_range_state(phys_addr_t start, 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 /* __ASM_RSI_CMDS_H */
arch/arm64/kernel/rsi.c +45 -0
57 lines not on this page · raw message
include/linux/arm-smccc-rhi.h new file +25 -0
@@ -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
@@ -184,6 +184,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
PATCH v5 10/10 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/kernel/rsi.c | 13 ++++++++++- arch/arm64/mm/mem_encrypt.c | 32 ++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mem_encrypt.h +4 -0
@@ -9,6 +9,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);
@@ -17,6 +18,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 ();
arch/arm64/kernel/rsi.c +12 -1
@@ -20,6 +20,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);
@@ -168,9 +169,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 )
@@ -180,7 +189,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;
@@ -230,6 +239,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))
arch/arm64/mm/mem_encrypt.c +28 -4
@@ -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);
Signature · 1 line
2.43.0