dma-mapping: Track shared DMA state through direct, pool and swiotlb paths
Subsystems (20)
v7 cover dma-mapping: Track shared DMA state through direct, pool and swiotlb paths
This series tracks confidential-computing shared DMA state through the dma-direct, dma-pool, and swiotlb paths so that encrypted and decrypted DMA buffers are handled consistently.
Today, the direct DMA path mostly relies on force_dma_unencrypted() for shared/decrypted buffer handling. This series consolidates the force_dma_unencrypted() checks in the top-level functions and ensures that the remaining DMA interfaces use DMA attributes to make the correct decisions.
The series separates mapping and allocation state:
- DMA_ATTR_CC_SHARED describes the DMA address attribute requested for a mapping. It tells the DMA mapping path that the DMA address must target shared/decrypted memory.
- __DMA_ATTR_ALLOC_CC_SHARED is an internal DMA-mapping attribute used only by allocation paths after the DMA core decides that the backing pages must be allocated as shared/decrypted memory.
The series:
- moves swiotlb-backed allocations out of __dma_direct_alloc_pages(),
- uses __DMA_ATTR_ALLOC_CC_SHARED through the dma-direct alloc/free paths
- teaches the atomic DMA pools to track encrypted versus decrypted state
- tracks swiotlb pool encryption state and enforces strict pool selection
- centralizes encrypted/decrypted pgprot handling in dma_pgprot() using DMA attributes
- passes DMA attributes down to dma_capable() so capability checks can validate whether the selected DMA address encoding matches DMA_ATTR_CC_SHARED
- makes dma_direct_map_phys() choose the DMA address encoding from DMA_ATTR_CC_SHARED and fall back to swiotlb when a shared DMA request cannot use the direct mapping, which lets arm64 and x86 CCA guests stop relying on SWIOTLB_FORCE for DMA mappings
- use the selected swiotlb pool state to derive the returned DMA address
- reports CC_ATTR_GUEST_MEM_ENCRYPT for arm64 Realms, powerpc secure guests, and s390 protected virtualization guests.
Dependency:
This series depends on the pKVM changes posted at:
https://lore.kernel.org/all/[email protected]
Please merge this series only after the pKVM changes above are merged. Otherwise pKVM will be broken.
Changes since v6:
* Rebase onto the latest kernel.
* Add __DMA_ATTR_ALLOC_CC_SHARED for allocation paths. DMA_ATTR_CC_SHARED is now used to describe the requested DMA mapping address attribute, while __DMA_ATTR_ALLOC_CC_SHARED is used internally when allocating shared/decrypted backing pages.
* Report CC_ATTR_GUEST_MEM_ENCRYPT for arm64 Realms, powerpc secure guests, and s390 protected virtualization guests.
* Add CC_ATTR_HOST_MEM_ENCRYPT and swiotlb=force fixes.
Changes since v5:
https://lore.kernel.org/all/[email protected]
* Add Tested-by
* Drop the pKVM patch, which has now been posted separately:
https://lore.kernel.org/all/[email protected]
* Remove the DO_NOT_MERGE tag from the s390 change.
* Add a patch to drop the SWIOTLB_FORCE flag.
* Rebase onto the latest kernel.
Changes since v4:
https://lore.kernel.org/all/[email protected]
* Add new patches based on Sashiko review:
swiotlb: Preserve allocation virtual address for dynamic pools
dma: free atomic pool pages by physical address
dma: swiotlb: handle set_memory_decrypted() failures
dma: swiotlb: free dynamic pools from process context
iommu/dma: Check atomic pool allocation result directly
* Include pKVM and s390 changes as dependent patches. These are not yet ready to merge and are waiting for subsystem testing feedback.
* Drop the AMD GART patch because it requires wider testing.
* Update swiotlb_tbl_map_single() to take attrs by reference.
* Switch swiotlb_free() to use rcu_work.
* Avoid calling swiotlb_find_pool() multiple times in the free path.
* Make DMA_ATTR_MMIO imply DMA_ATTR_CC_SHARED for devices requiring unencrypted DMA.
Changes from v3:
https://lore.kernel.org/all/[email protected]
* Handle DMA_ATTR_MMIO correctly in dma_direct_map_phys()
* Address most of sashiko review
* Rebase to latest kernel
* drop SWIOTLB_FORCE for s390 and powerpc secure guest.
Changes from v2:
https://lore.kernel.org/all/[email protected]
* pass attrs to dma_capable() and update direct, swiotlb, Xen swiotlb, and x86 GART paths so the capability checks see the DMA address attr value DMA_ATTR_CC_SHARED.
* rework dma_direct_map_phys() so DMA_ATTR_CC_SHARED selects phys_to_dma_unencrypted() while the default path uses phys_to_dma_encrypted(), with swiotlb fallback when the requested shared/private state cannot be satisfied by a direct DMA address.
* stop relying on SWIOTLB_FORCE for arm64 and x86 CC guest DMA mappings; swiotlb is still enabled there, but shared mappings is now selected through the generic dma_direct_map_phys()/dma_capable() decision instead of a global force-bounce flag.
Changes from v1:
https://lore.kernel.org/all/[email protected]
* rebased to latest kernel (change from DMA_ATTR_CC_DECRYPTED -> DMA_ATTR_CC_SHARED)
* update the alloc path so DMA_ATTR_CC_SHARED is not a caller-visible attribute.
Aneesh Kumar K.V (Arm) (22):
dma-direct: return struct page from dma_direct_alloc_from_pool()
dma-pool: fix page leak in atomic_pool_expand() cleanup iommu/dma: Check atomic pool allocation result directly
dma: free atomic pool pages by physical address
swiotlb: Preserve allocation virtual address for dynamic pools
s390: Expose protected virtualization through cc_platform_has()
dma-direct: swiotlb: handle swiotlb alloc/free outside
__dma_direct_alloc_pages
coco: arm64: s390: powerpc: Mark secure guests with
CC_ATTR_GUEST_MEM_ENCRYPT
dma-mapping: Add internal shared allocation attribute
dma-direct: use __DMA_ATTR_ALLOC_CC_SHARED in alloc/free paths
dma-pool: track decrypted atomic pools and select them via attrs
dma: swiotlb: pass mapping attributes by reference
dma: swiotlb: track pool encryption state and honor DMA_ATTR_CC_SHARED
dma-mapping: make dma_pgprot() honor __DMA_ATTR_ALLOC_CC_SHARED
dma-direct: pass attrs to dma_capable() for DMA_ATTR_CC_SHARED checks
dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
dma-direct: set decrypted flag for remapped DMA allocations
dma-direct: select DMA address encoding from
__DMA_ATTR_ALLOC_CC_SHARED
dma-direct: rename ret to cpu_addr in alloc helpers
dma: swiotlb: free dynamic pools from process context
dma: swiotlb: handle set_memory_decrypted() failures
swiotlb: remove unused SWIOTLB_FORCE flag
Documentation/core-api/dma-attributes.rst | 29 ++ arch/arm64/kernel/rsi.c | 1 + arch/arm64/mm/init.c | 4 +- arch/powerpc/platforms/pseries/cc_platform.c | 1 + arch/powerpc/platforms/pseries/svm.c | 2 +- arch/s390/Kconfig | 1 + arch/s390/mm/init.c | 17 +- arch/x86/kernel/amd_gart_64.c | 30 +- arch/x86/kernel/pci-dma.c | 4 +- drivers/iommu/dma-iommu.c | 20 +- drivers/xen/swiotlb-xen.c | 8 +- include/linux/dma-direct.h | 20 +- include/linux/dma-map-ops.h | 3 +- include/linux/dma-mapping.h | 8 + include/linux/swiotlb.h | 25 +- include/trace/events/dma.h | 3 +- kernel/dma/direct.c | 264 ++++++++++++----- kernel/dma/direct.h | 47 +-- kernel/dma/mapping.c | 25 +- kernel/dma/pool.c | 221 ++++++++++---- kernel/dma/swiotlb.c | 292 ++++++++++++++----- 21 files changed, 756 insertions(+), 269 deletions(-)
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
Signature · 1 line
2.43.0
PATCH v7 1/22 dma-direct: return struct page from dma_direct_alloc_from_pool()
Commit 5b138c534fda ("dma-direct: factor out a dma_direct_alloc_from_pool helper") changed dma_direct_alloc_from_pool() to return the CPU address from dma_alloc_from_pool(). That fits dma_direct_alloc(), but dma_direct_alloc_pages() also uses the helper and expects a struct page *.
Fix this by making dma_direct_alloc_from_pool() return the struct page * again, and pass the CPU address back through an out-parameter for the dma_direct_alloc() caller.
--- kernel/dma/direct.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/dma/direct.c +10 -8
@@ -164,22 +164,21 @@ static bool dma_direct_use_pool(struct device *dev, gfp_t gfp)return !gfpflags_allow_blocking (gfp) && !is_swiotlb_for_alloc (dev); } -static void *dma_direct_alloc_from_pool (struct device *dev,size_t size, -dma_addr_t *dma_handle,gfp_t gfp) +static struct page *dma_direct_alloc_from_pool (struct device *dev,size_t size, +dma_addr_t *dma_handle,void **cpu_addr,gfp_t gfp) {struct page *page;u64 phys_limit; -void *ret;if (WARN_ON_ONCE (!IS_ENABLED (CONFIG_DMA_COHERENT_POOL )))return NULL ; gfp |=dma_direct_optimal_gfp_mask (dev, &phys_limit); - page =dma_alloc_from_pool (dev, size, &ret, gfp, dma_coherent_ok); + page =dma_alloc_from_pool (dev, size, cpu_addr, gfp, dma_coherent_ok);if (!page)return NULL ; *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); -return ret; +return page; } static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
@@ -247,8 +246,11 @@ void *dma_direct_alloc(struct device *dev, size_t size,* the atomic pools instead if we aren't allowed block. */ if ((remap ||force_dma_unencrypted (dev)) && -dma_direct_use_pool (dev, gfp)) -return dma_direct_alloc_from_pool (dev, size, dma_handle, gfp); +dma_direct_use_pool (dev, gfp)) { + page =dma_direct_alloc_from_pool (dev, size, dma_handle, + &ret, gfp); +return page ? ret :NULL ; + }/* we always manually zero the memory once we are done */ page =__dma_direct_alloc_pages (dev, size, gfp & ~__GFP_ZERO, true);
@@ -357,7 +359,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,void *ret;if (force_dma_unencrypted (dev) &&dma_direct_use_pool (dev, gfp)) -return dma_direct_alloc_from_pool (dev, size, dma_handle, gfp); +return dma_direct_alloc_from_pool (dev, size, dma_handle, &ret, gfp); page =__dma_direct_alloc_pages (dev, size, gfp, false);if (!page)
Signature · 1 line
2.43.0
PATCH v7 2/22 dma-pool: fix page leak in atomic_pool_expand() cleanup
In reply to Aneesh Kumar K.V (Arm)atomic_pool_expand() frees the allocated pages from the remove_mapping error path only when CONFIG_DMA_DIRECT_REMAP is enabled.
When CONFIG_DMA_DIRECT_REMAP is disabled, failures after page allocation, such as gen_pool_add_virt(), jump to remove_mapping and return without freeing the pages.
Move __free_pages(page, order) out of the CONFIG_DMA_DIRECT_REMAP block so that cleanup paths always release the allocation.
--- kernel/dma/pool.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/dma/pool.c +7 -3
@@ -81,6 +81,7 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, {unsigned int order;struct page *page =NULL ; +bool leak_pages = false;void *addr;int ret = -ENOMEM ;
@@ -115,8 +116,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,*/ ret =set_memory_decrypted ((unsigned long )page_to_virt (page),1 << order); -if (ret) +if (ret) { + leak_pages = true; goto remove_mapping; + } ret =gen_pool_add_virt (pool, (unsigned long )addr,page_to_phys (page), pool_size,NUMA_NO_NODE );if (ret)
@@ -130,14 +133,15 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, 1 << order);if (WARN_ON_ONCE (ret)) {/* Decrypt succeeded but encrypt failed, purposely leak */ - goto out; + leak_pages = true; } remove_mapping: #ifdef CONFIG_DMA_DIRECT_REMAPdma_common_free_remap (addr ,pool_size ); free_page: -__free_pages (page, order); #endif +if (!leak_pages) +__free_pages (page, order); out:return ret; }
Signature · 1 line
2.43.0
PATCH v7 3/22 iommu/dma: Check atomic pool allocation result directly
In reply to Aneesh Kumar K.V (Arm)The non-blocking, non-coherent allocation path uses dma_alloc_from_pool(), which returns the allocated page and fills cpu_addr only on success.
Do not rely on cpu_addr to detect allocation failure in this path. Check the returned page directly before using it for the IOMMU mapping.
--- drivers/iommu/dma-iommu.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/iommu/dma-iommu.c +8 -5
@@ -1671,13 +1671,16 @@ void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, }if (IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ) && - !gfpflags_allow_blocking (gfp) && !coherent) + !gfpflags_allow_blocking (gfp) && !coherent) { page =dma_alloc_from_pool (dev,PAGE_ALIGN (size), &cpu_addr, - gfp,NULL ); -else + gfp,NULL ); +if (!page) +return NULL ; + }else { cpu_addr =iommu_dma_alloc_pages (dev, size, &page, gfp, attrs); -if (!cpu_addr) -return NULL ; +if (!cpu_addr) +return NULL ; + } *handle =__iommu_dma_map (dev,page_to_phys (page), size, ioprot, dev->coherent_dma_mask);
Signature · 1 line
2.43.0
PATCH v7 4/22 dma: free atomic pool pages by physical address
In reply to Aneesh Kumar K.V (Arm)dma_direct_alloc_pages() may satisfy atomic allocations from the coherent atomic pools. The pool allocation is keyed by the virtual address stored in the gen_pool, but the pages API returns only the backing struct page.
On architectures with CONFIG_DMA_DIRECT_REMAP, atomic pool chunks are added to the gen_pool using their remapped virtual address. dma_direct_free_pages() reconstructs a linear-map address with page_address(page) and passes that to dma_free_from_pool(). That address does not match the gen_pool virtual range, so the pool lookup can fail and the code can fall through to freeing a pool-owned page through the normal page allocator path.
Add a page-based pool free helper that looks up the owning pool chunk by physical address, translates it back to the gen_pool virtual address, and frees that address to the pool. Use it from dma_direct_free_pages() while keeping the existing virtual-address helper for coherent allocation frees.
--- include/linux/dma-map-ops.h | 1 + kernel/dma/direct.c | 4 +-- kernel/dma/pool.c | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)
include/linux/dma-map-ops.h +1 -0
@@ -215,6 +215,7 @@ struct page *dma_alloc_from_pool(struct device *dev, size_t size, void **cpu_addr, gfp_t flags, bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));bool dma_free_from_pool (struct device *dev,void *start,size_t size); +bool dma_free_from_pool_page (struct device *dev,struct page *page,size_t size);int dma_direct_set_offset (struct device *dev,phys_addr_t cpu_start,dma_addr_t dma_start,u64 size);
kernel/dma/direct.c +2 -2
@@ -381,9 +381,9 @@ void dma_direct_free_pages(struct device *dev, size_t size, {void *vaddr =page_address (page); -/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ +/* If page is not from an atomic pool, dma_free_from_pool_page() fails */ if (IS_ENABLED (CONFIG_DMA_COHERENT_POOL ) && -dma_free_from_pool (dev, vaddr, size)) +dma_free_from_pool_page (dev, page, size))return ;if (dma_set_encrypted (dev, vaddr, size))
kernel/dma/pool.c +54 -0
57 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 5/22 swiotlb: Preserve allocation virtual address for dynamic pools
In reply to Aneesh Kumar K.V (Arm)swiotlb_alloc_tlb() can allocate from the DMA atomic pool when a decrypted pool is needed from atomic context. With CONFIG_DMA_DIRECT_REMAP, the atomic pool is backed by remapped virtual addresses, which are not the same as the direct-map addresses returned by phys_to_virt().
swiotlb_init_io_tlb_pool() currently reconstructs the pool virtual address from the physical start address. For atomic-pool backed allocations this stores the wrong address in pool->vaddr. Later, swiotlb_free_tlb() passes that address to dma_free_from_pool(), which will fail to recognize the chunk
Pass the virtual address returned by the allocation path into swiotlb_init_io_tlb_pool(), and store that address in pool->vaddr. This keeps the pool free path using the same virtual address as the allocator.
--- kernel/dma/swiotlb.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
kernel/dma/swiotlb.c +19 -12
@@ -266,9 +266,9 @@ void __init swiotlb_update_mem_attributes(void) }static void swiotlb_init_io_tlb_pool (struct io_tlb_pool *mem,phys_addr_t start, -unsigned long nslabs,bool late_alloc,unsigned int nareas) +void *vaddr,unsigned long nslabs,bool late_alloc, +unsigned int nareas) { -void *vaddr =phys_to_virt (start);unsigned long bytes = nslabs <<IO_TLB_SHIFT , i; mem->nslabs = nslabs;
@@ -409,7 +409,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,return ; } -swiotlb_init_io_tlb_pool (mem,__pa (tlb), nslabs, false, nareas); +swiotlb_init_io_tlb_pool (mem,__pa (tlb), tlb, nslabs, false, nareas);add_mem_pool (&io_tlb_default_mem, mem);if (flags &SWIOTLB_VERBOSE )
@@ -507,7 +507,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,set_memory_decrypted ((unsigned long )vstart, (nslabs <<IO_TLB_SHIFT ) >>PAGE_SHIFT ); -swiotlb_init_io_tlb_pool (mem,virt_to_phys (vstart), nslabs, true, +swiotlb_init_io_tlb_pool (mem,virt_to_phys (vstart), vstart, nslabs, true, nareas);add_mem_pool (&io_tlb_default_mem, mem);
@@ -605,25 +605,26 @@ static struct page *alloc_dma_pages(gfp_t gfp, size_t bytes, u64 phys_limit)* @bytes: Size of the buffer. * @phys_limit: Maximum allowed physical address of the buffer. * @gfp: GFP flags for the allocation. +* @vaddr: Receives the virtual address for the allocated buffer. * * Return: Allocated pages, or %NULL on allocation failure. */ static struct page *swiotlb_alloc_tlb (struct device *dev,size_t bytes, -u64 phys_limit,gfp_t gfp) +u64 phys_limit,gfp_t gfp,void **vaddr) {struct page *page; + *vaddr =NULL ; +/* * Allocate from the atomic pools if memory is encrypted and * the allocation is atomic, because decrypting may block. */ if (!gfpflags_allow_blocking (gfp) && dev &&force_dma_unencrypted (dev)) { -void *vaddr; -if (!IS_ENABLED (CONFIG_DMA_COHERENT_POOL ))return NULL ; -return dma_alloc_from_pool (dev, bytes, &vaddr, gfp, +return dma_alloc_from_pool (dev, bytes, vaddr, gfp, dma_coherent_ok); }
@@ -645,6 +646,8 @@ static struct page *swiotlb_alloc_tlb(struct device *dev, size_t bytes,return NULL ; } +if (page) + *vaddr =phys_to_virt (page_to_phys (page));return page; }
@@ -685,6 +688,7 @@ static struct io_tlb_pool *swiotlb_alloc_pool(struct device *dev, {struct io_tlb_pool *pool;unsigned int slot_order; +void *tlb_vaddr;struct page *tlb;size_t pool_size;size_t tlb_size;
@@ -701,7 +705,8 @@ static struct io_tlb_pool *swiotlb_alloc_pool(struct device *dev, pool->areas = (void *)pool +sizeof (*pool); tlb_size = nslabs <<IO_TLB_SHIFT ; -while (!(tlb =swiotlb_alloc_tlb (dev, tlb_size, phys_limit, gfp))) { +while (!(tlb =swiotlb_alloc_tlb (dev, tlb_size, phys_limit, gfp, + &tlb_vaddr))) {if (nslabs <= minslabs) goto error_tlb; nslabs =ALIGN (nslabs >>1 ,IO_TLB_SEGSIZE );
@@ -715,11 +720,12 @@ static struct io_tlb_pool *swiotlb_alloc_pool(struct device *dev,if (!pool->slots) goto error_slots; -swiotlb_init_io_tlb_pool (pool,page_to_phys (tlb), nslabs, true, nareas); +swiotlb_init_io_tlb_pool (pool,page_to_phys (tlb), tlb_vaddr, nslabs, + true, nareas);return pool; error_slots: -swiotlb_free_tlb (page_address (tlb), tlb_size); +swiotlb_free_tlb (tlb_vaddr, tlb_size); error_tlb:kfree (pool); error:
@@ -1851,7 +1857,8 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,set_memory_decrypted ((unsigned long )phys_to_virt (rmem->base), rmem->size >>PAGE_SHIFT ); -swiotlb_init_io_tlb_pool (pool, rmem->base, nslabs, +swiotlb_init_io_tlb_pool (pool, rmem->base,phys_to_virt (rmem->base), + nslabs, false, nareas); mem->force_bounce = true; mem->for_alloc = true;
Signature · 1 line
2.43.0
PATCH v7 6/22 s390: Expose protected virtualization through cc_platform_has()
In reply to Aneesh Kumar K.V (Arm)Protected virtualization guests use memory encryption, so advertise that to the rest of the kernel through cc_platform_has(CC_ATTR_MEM_ENCRYPT).
s390 already forces DMA mappings to be unencrypted for protected virtualization guests through force_dma_unencrypted(). Add ARCH_HAS_CC_PLATFORM and provide the matching cc_platform_has() implementation
--- arch/s390/Kconfig | 1 + arch/s390/mm/init.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+)
arch/s390/Kconfig +1 -0
@@ -84,6 +84,7 @@ config S390 select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2 select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE select ARCH_HAS_CC_CAN_LINK + select ARCH_HAS_CC_PLATFORM select ARCH_HAS_CPU_FINALIZE_INIT select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL
arch/s390/mm/init.c +14 -0
@@ -50,6 +50,7 @@#include <linux/virtio_anchor.h> #include <linux/virtio_config.h> #include <linux/execmem.h> +#include <linux/cc_platform.h> pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".bss..swapper_pg_dir"); pgd_t invalid_pg_dir[PTRS_PER_PGD] __section(".bss..invalid_pg_dir");
@@ -142,6 +143,19 @@ bool force_dma_unencrypted(struct device *dev)return is_prot_virt_guest (); } + +bool cc_platform_has (enum cc_attr attr) +{ +switch (attr) { +case CC_ATTR_MEM_ENCRYPT : +return is_prot_virt_guest (); + +default : +return false; + } +} +EXPORT_SYMBOL_GPL (cc_platform_has); +/* protected virtualization */ static void __init pv_init (void ) {
Signature · 1 line
2.43.0
PATCH v7 7/22 dma-direct: swiotlb: handle swiotlb alloc/free outside __dma_direct_alloc_pages
In reply to Aneesh Kumar K.V (Arm)Move swiotlb allocation out of __dma_direct_alloc_pages() and handle it in dma_direct_alloc() / dma_direct_alloc_pages().
This is needed for follow-up changes that simplify the handling of memory encryption/decryption based on the DMA attribute flags.
swiotlb backing pages are already mapped decrypted by swiotlb_update_mem_attributes() and rmem_swiotlb_device_init(), so dma-direct should not call dma_set_decrypted() on allocation nor dma_set_encrypted() on free for swiotlb-backed memory.
Update alloc/free paths to detect swiotlb-backed pages and skip encrypt/decrypt transitions for those paths. Keep the existing highmem rejection in dma_direct_alloc_pages() for swiotlb allocations.
Only for "restricted-dma-pool", we currently set for_alloc = true, while rmem_swiotlb_device_init() decrypts the whole pool up front. This pool is typically used together with "shared-dma-pool", where the shared region is accessed after remap/ioremap and the returned address is suitable for decrypted memory access. So existing code paths remain valid.
--- include/linux/swiotlb.h | 6 ++++ kernel/dma/direct.c | 71 ++++++++++++++++++++++++++++++----------- kernel/dma/swiotlb.c | 6 ++++ 3 files changed, 65 insertions(+), 18 deletions(-)
include/linux/swiotlb.h +6 -0
@@ -284,6 +284,8 @@ extern void swiotlb_print_info(void);#ifdef CONFIG_DMA_RESTRICTED_POOL struct page *swiotlb_alloc (struct device *dev,size_t size);bool swiotlb_free (struct device *dev,struct page *page,size_t size); +void swiotlb_free_from_pool (struct device *dev, +phys_addr_t tlb_addr,struct io_tlb_pool *pool);static inline bool is_swiotlb_for_alloc (struct device *dev) {
@@ -299,6 +301,10 @@ static inline bool swiotlb_free(struct device *dev, struct page *page, {return false; } +static inline void swiotlb_free_from_pool (struct device *dev, +phys_addr_t tlb_addr,struct io_tlb_pool *pool) +{ +}static inline bool is_swiotlb_for_alloc (struct device *dev) {return false;
kernel/dma/direct.c +53 -18
160 lines not on this page · raw message
kernel/dma/swiotlb.c +6 -0
@@ -1815,6 +1815,12 @@ bool swiotlb_free(struct device *dev, struct page *page, size_t size)return true; } +void swiotlb_free_from_pool (struct device *dev, +phys_addr_t tlb_addr,struct io_tlb_pool *pool) +{ +swiotlb_release_slots (dev, tlb_addr, pool); +} +static int rmem_swiotlb_device_init (struct reserved_mem *rmem,struct device *dev) {
Signature · 1 line
2.43.0
PATCH v7 8/22 coco: arm64: s390: powerpc: Mark secure guests with CC_ATTR_GUEST_MEM_ENCRYPT
In reply to Aneesh Kumar K.V (Arm)arm64 Realms, powerpc secure guests and s390 protected virtualization guests currently report CC_ATTR_MEM_ENCRYPT, but not CC_ATTR_GUEST_MEM_ENCRYPT.
These environments are all secure guest configurations. Their force_dma_unencrypted() implementations also treat the same secure guest state as requiring unencrypted DMA. Report CC_ATTR_GUEST_MEM_ENCRYPT for the same condition so generic confidential-computing code can distinguish guest memory encryption from host memory encryption instead of relying only on CC_ATTR_MEM_ENCRYPT.
--- arch/arm64/kernel/rsi.c | 1 + arch/powerpc/platforms/pseries/cc_platform.c | 1 + arch/s390/mm/init.c | 1 + 3 files changed, 3 insertions(+)
arch/arm64/kernel/rsi.c +1 -0
@@ -27,6 +27,7 @@ bool cc_platform_has(enum cc_attr attr) {switch (attr) {case CC_ATTR_MEM_ENCRYPT : +case CC_ATTR_GUEST_MEM_ENCRYPT :return is_realm_world ();default :return false;
arch/powerpc/platforms/pseries/cc_platform.c +1 -0
@@ -17,6 +17,7 @@ bool cc_platform_has(enum cc_attr attr) {switch (attr) {case CC_ATTR_MEM_ENCRYPT : +case CC_ATTR_GUEST_MEM_ENCRYPT :return is_secure_guest ();default :
arch/s390/mm/init.c +1 -0
@@ -148,6 +148,7 @@ bool cc_platform_has(enum cc_attr attr) {switch (attr) {case CC_ATTR_MEM_ENCRYPT : +case CC_ATTR_GUEST_MEM_ENCRYPT :return is_prot_virt_guest ();default :
Signature · 1 line
2.43.0
PATCH v7 9/22 dma-mapping: Add internal shared allocation attribute
In reply to Aneesh Kumar K.V (Arm)DMA_ATTR_CC_SHARED describes an existing DMA mapping whose backing memory is already shared, or decrypted, for confidential computing. It is a mapping attribute: callers use it to request a shared DMA address encoding for memory that has already been prepared for shared DMA.
Allocation paths need a related but different state. Once the DMA core decides that an allocation must use shared backing pages, the lower-level allocation helpers need to select shared pools, decrypt newly allocated pages, derive the DMA address with the shared-memory translation and restore encryption on free. That state is internal to the DMA-mapping implementation and should not be passed by drivers to dma_alloc_attrs().
--- Documentation/core-api/dma-attributes.rst | 29 +++++++++++++++++++++++ include/linux/dma-mapping.h | 8 +++++++ include/trace/events/dma.h | 3 ++- 3 files changed, 39 insertions(+), 1 deletion(-)
Documentation/core-api/dma-attributes.rst +29 -0
@@ -179,3 +179,32 @@ interface when building their uAPIs, when possible. It must never be used in an in-kernel driver that only works with kernel memory. + +DMA_ATTR_CC_SHARED +------------------ + +This attribute indicates that a DMA mapping is shared, or decrypted, for +confidential computing guests. For normal system memory, the caller must +already have marked the memory decrypted with set_memory_decrypted(). CPU +PTEs for the mapping must use pgprot_decrypted(), and the same shared +semantic may be passed to a vIOMMU when it sets up the IOPTE. + +This attribute describes an existing mapping. It does not allocate shared +backing pages and must not be passed to dma_alloc_attrs(). For MMIO, use +this together with DMA_ATTR_MMIO to indicate shared MMIO. Unless +DMA_ATTR_MMIO is provided, the mapping requires a struct page. + +__DMA_ATTR_ALLOC_CC_SHARED +-------------------------- + +This is an internal DMA-mapping attribute for confidential computing guests. +It is used by allocation paths after the DMA core has determined that the +backing pages must be shared, or decrypted. For example, the direct DMA and +SWIOTLB allocation paths use it to select shared DMA pools, decrypt newly +allocated pages, derive DMA addresses using the shared-memory translation, and +restore encryption on free. + +__DMA_ATTR_ALLOC_CC_SHARED differs from DMA_ATTR_CC_SHARED in that it is not +a caller-visible DMA API attribute. DMA_ATTR_CC_SHARED describes an +already-shared mapping and requires the caller to have prepared normal +system memory before mapping it.
include/linux/dma-mapping.h +8 -0
@@ -103,6 +103,14 @@*/ #define DMA_ATTR_CC_SHARED (1UL << 13) +/* +* __DMA_ATTR_ALLOC_CC_SHARED: Internal DMA-mapping attribute used by +* allocation paths that create shared (decrypted) backing pages for +* confidential computing guests. Drivers must not pass this attribute to +* dma_alloc_attrs(). +*/ +#define __DMA_ATTR_ALLOC_CC_SHARED (1UL << 14) +/* * A dma_addr_t can hold any valid DMA or bus address for the platform. It can * be given to a device to use as a DMA source or target. It is specific to a
include/trace/events/dma.h +2 -1
@@ -35,7 +35,8 @@ TRACE_DEFINE_ENUM(DMA_NONE); { DMA_ATTR_MMIO, "MMIO" }, \ { DMA_ATTR_DEBUGGING_IGNORE_CACHELINES, "CACHELINES_OVERLAP" }, \ { DMA_ATTR_REQUIRE_COHERENT, "REQUIRE_COHERENT" }, \ - { DMA_ATTR_CC_SHARED, "CC_SHARED" }) + { DMA_ATTR_CC_SHARED, "CC_SHARED" }, \ + { __DMA_ATTR_ALLOC_CC_SHARED, "ALLOC_CC_SHARED" }) DECLARE_EVENT_CLASS(dma_map, TP_PROTO(struct device *dev, phys_addr_t phys_addr, dma_addr_t dma_addr,
Signature · 1 line
2.43.0
PATCH v7 10/22 dma-direct: use __DMA_ATTR_ALLOC_CC_SHARED in alloc/free paths
In reply to Aneesh Kumar K.V (Arm)Propagate force_dma_unencrypted() into __DMA_ATTR_ALLOC_CC_SHARED in the dma-direct allocation path and use the attribute to drive the related decisions.
This updates dma_direct_alloc(), dma_direct_free(), and dma_direct_alloc_pages() to fold the forced unencrypted case into attrs.
--- kernel/dma/direct.c | 42 +++++++++++++++++++++++++++++++++--------- kernel/dma/mapping.c | 9 +++++++++ 2 files changed, 42 insertions(+), 9 deletions(-)
kernel/dma/direct.c +33 -9
@@ -192,16 +192,22 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) { bool remap = false, set_uncached = false; -bool mark_mem_decrypt = true; +bool mark_mem_decrypt = false;struct page *page;void *ret; +if (force_dma_unencrypted (dev)) + attrs |= __DMA_ATTR_ALLOC_CC_SHARED; + +if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) + mark_mem_decrypt = true; + size =PAGE_ALIGN (size);if (attrs &DMA_ATTR_NO_WARN ) gfp |= __GFP_NOWARN; -if ((attrs &DMA_ATTR_NO_KERNEL_MAPPING ) && - !force_dma_unencrypted (dev) && !is_swiotlb_for_alloc (dev)) +if (((attrs & (DMA_ATTR_NO_KERNEL_MAPPING | __DMA_ATTR_ALLOC_CC_SHARED)) == +DMA_ATTR_NO_KERNEL_MAPPING ) && !is_swiotlb_for_alloc (dev))return dma_direct_alloc_no_mapping (dev, size, dma_handle, gfp);if (!dev_is_dma_coherent (dev)) {
@@ -235,7 +241,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,* Remapping or decrypting memory may block, allocate the memory from * the atomic pools instead if we aren't allowed block. */ -if ((remap ||force_dma_unencrypted (dev)) && +if ((remap || (attrs & __DMA_ATTR_ALLOC_CC_SHARED)) &&dma_direct_use_pool (dev, gfp)) { page =dma_direct_alloc_from_pool (dev, size, dma_handle, &ret, gfp);
@@ -314,12 +320,22 @@ void dma_direct_free(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) { phys_addr_t phys; -bool mark_mem_encrypted = true; +bool mark_mem_encrypted = false;struct io_tlb_pool *swiotlb_pool;unsigned int page_order =get_order (size); -if ((attrs &DMA_ATTR_NO_KERNEL_MAPPING ) && - !force_dma_unencrypted (dev) && !is_swiotlb_for_alloc (dev)) { +/* +* If the allocation used decrypted/shared backing pages, restore +* the encryption state on free. +*/ +if (force_dma_unencrypted (dev)) + attrs |= __DMA_ATTR_ALLOC_CC_SHARED; + +if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) + mark_mem_encrypted = true; + +if (((attrs & (DMA_ATTR_NO_KERNEL_MAPPING | __DMA_ATTR_ALLOC_CC_SHARED)) == +DMA_ATTR_NO_KERNEL_MAPPING ) && !is_swiotlb_for_alloc (dev)) {/* cpu_addr is a struct page cookie, not a kernel address */ dma_free_contiguous (dev, cpu_addr, size);return ;
@@ -368,10 +384,14 @@ void dma_direct_free(struct device *dev, size_t size,struct page *dma_direct_alloc_pages (struct device *dev,size_t size,dma_addr_t *dma_handle,enum dma_data_direction dir,gfp_t gfp) { +unsigned long attrs =0 ;struct page *page;void *ret; -if (force_dma_unencrypted (dev) &&dma_direct_use_pool (dev, gfp)) +if (force_dma_unencrypted (dev)) + attrs |= __DMA_ATTR_ALLOC_CC_SHARED; + +if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&dma_direct_use_pool (dev, gfp))return dma_direct_alloc_from_pool (dev, size, dma_handle, &ret, gfp);if (is_swiotlb_for_alloc (dev)) {
@@ -405,7 +425,11 @@ void dma_direct_free_pages(struct device *dev, size_t size,phys_addr_t phys;void *vaddr =page_address (page);struct io_tlb_pool *swiotlb_pool; -bool mark_mem_encrypted = true; +/* +* if the device had requested for an unencrypted buffer, +* convert it to encrypted on free +*/ +bool mark_mem_encrypted =force_dma_unencrypted (dev);/* If page is not from an atomic pool, dma_free_from_pool_page() fails */ if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
kernel/dma/mapping.c +9 -0
@@ -638,6 +638,15 @@ void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,if (WARN_ON_ONCE (flag & __GFP_COMP))return NULL ; +if (attrs & (DMA_ATTR_CC_SHARED | __DMA_ATTR_ALLOC_CC_SHARED)) { +trace_dma_alloc (dev,NULL ,0 , size,DMA_BIDIRECTIONAL , flag, + attrs); +return NULL ; + } + +if (force_dma_unencrypted (dev)) + attrs |= __DMA_ATTR_ALLOC_CC_SHARED; +if (dma_alloc_from_dev_coherent (dev, size, dma_handle, &cpu_addr)) {trace_dma_alloc (dev, cpu_addr, *dma_handle, size,DMA_BIDIRECTIONAL , flag, attrs);
Signature · 1 line
2.43.0
PATCH v7 11/22 dma-pool: track decrypted atomic pools and select them via attrs
In reply to Aneesh Kumar K.V (Arm)Teach the atomic DMA pool code to distinguish between encrypted and unencrypted pools, and make pool allocation select the matching pool based on DMA attributes.
Introduce a dma_gen_pool wrapper that records whether a pool is unencrypted, initialize that state when the atomic pools are created, and use it when expanding and resizing the pools. Update dma_alloc_from_pool() to take attrs and skip pools whose encrypted state does not match __DMA_ATTR_ALLOC_CC_SHARED. Update dma_free_from_pool() accordingly.
Also pass __DMA_ATTR_ALLOC_CC_SHARED from the swiotlb atomic allocation path so decrypted swiotlb allocations are taken from the correct atomic pool.
--- drivers/iommu/dma-iommu.c | 7 +- include/linux/dma-map-ops.h | 2 +- kernel/dma/direct.c | 11 ++- kernel/dma/pool.c | 173 +++++++++++++++++++++++------------- kernel/dma/swiotlb.c | 8 +- 5 files changed, 129 insertions(+), 72 deletions(-)
drivers/iommu/dma-iommu.c +6 -1
@@ -1660,9 +1660,14 @@ void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, {bool coherent =dev_is_dma_coherent (dev);int ioprot =dma_info_to_prot (DMA_BIDIRECTIONAL , coherent, attrs); +bool is_alloc_cc_shared = attrs & __DMA_ATTR_ALLOC_CC_SHARED;struct page *page =NULL ;void *cpu_addr; +/* Not yet supported */ +if (is_alloc_cc_shared) +return NULL ; + gfp |= __GFP_ZERO; if (gfpflags_allow_blocking(gfp) &&
@@ -1673,7 +1678,7 @@ void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,if (IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ) && !gfpflags_allow_blocking (gfp) && !coherent) { page =dma_alloc_from_pool (dev,PAGE_ALIGN (size), &cpu_addr, - gfp,NULL ); + gfp, attrs,NULL );if (!page)return NULL ; }else {
include/linux/dma-map-ops.h +1 -1
@@ -212,7 +212,7 @@ void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,void dma_common_free_remap (void *cpu_addr,size_t size);struct page *dma_alloc_from_pool (struct device *dev,size_t size, -void **cpu_addr,gfp_t flags, +void **cpu_addr,gfp_t flags,unsigned long attrs,bool (*phys_addr_ok)(struct device *,phys_addr_t ,size_t ));bool dma_free_from_pool (struct device *dev,void *start,size_t size);bool dma_free_from_pool_page (struct device *dev,struct page *page,size_t size);
kernel/dma/direct.c +7 -4
@@ -154,7 +154,8 @@ static bool dma_direct_use_pool(struct device *dev, gfp_t gfp) }static struct page *dma_direct_alloc_from_pool (struct device *dev,size_t size, -dma_addr_t *dma_handle,void **cpu_addr,gfp_t gfp) +dma_addr_t *dma_handle,void **cpu_addr,gfp_t gfp, +unsigned long attrs) {struct page *page;u64 phys_limit;
@@ -163,7 +164,8 @@ static struct page *dma_direct_alloc_from_pool(struct device *dev, size_t size,return NULL ; gfp |=dma_direct_optimal_gfp_mask (dev, &phys_limit); - page =dma_alloc_from_pool (dev, size, cpu_addr, gfp, dma_coherent_ok); + page =dma_alloc_from_pool (dev, size, cpu_addr, gfp, attrs, + dma_coherent_ok);if (!page)return NULL ; *dma_handle =phys_to_dma_direct (dev,page_to_phys (page));
@@ -244,7 +246,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,if ((remap || (attrs & __DMA_ATTR_ALLOC_CC_SHARED)) &&dma_direct_use_pool (dev, gfp)) { page =dma_direct_alloc_from_pool (dev, size, dma_handle, - &ret, gfp); + &ret, gfp, attrs);return page ? ret :NULL ; }
@@ -392,7 +394,8 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size, attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&dma_direct_use_pool (dev, gfp)) -return dma_direct_alloc_from_pool (dev, size, dma_handle, &ret, gfp); +return dma_direct_alloc_from_pool (dev, size, dma_handle, + &ret, gfp, attrs);if (is_swiotlb_for_alloc (dev)) { page =dma_direct_alloc_swiotlb (dev, size);
kernel/dma/pool.c +109 -64
296 lines not on this page · raw message
kernel/dma/swiotlb.c +6 -2
@@ -613,9 +613,9 @@ static struct page *swiotlb_alloc_tlb(struct device *dev, size_t bytes, u64 phys_limit, gfp_t gfp, void **vaddr) { struct page *page; +unsigned long attrs =0 ; *vaddr =NULL ; -/* * Allocate from the atomic pools if memory is encrypted and * the allocation is atomic, because decrypting may block.
@@ -624,8 +624,12 @@ static struct page *swiotlb_alloc_tlb(struct device *dev, size_t bytes,if (!IS_ENABLED (CONFIG_DMA_COHERENT_POOL ))return NULL ; +/* swiotlb considered decrypted by default */ +if (cc_platform_has (CC_ATTR_MEM_ENCRYPT )) + attrs = __DMA_ATTR_ALLOC_CC_SHARED; +return dma_alloc_from_pool (dev, bytes, vaddr, gfp, - dma_coherent_ok); + attrs, dma_coherent_ok); } gfp &= ~GFP_ZONEMASK ;
Signature · 1 line
2.43.0
PATCH v7 12/22 dma: swiotlb: pass mapping attributes by reference
In reply to Aneesh Kumar K.V (Arm)Change swiotlb_tbl_map_single() to take the DMA mapping attributes by reference and update the direct callers accordingly.
This is a preparatory change for a follow-up patch which updates the attributes based on the selected swiotlb pool. Keeping the signature change separate makes the follow-up patch easier to review.
No functional change in this patch.
--- drivers/iommu/dma-iommu.c | 2 +- drivers/xen/swiotlb-xen.c | 2 +- include/linux/swiotlb.h | 2 +- kernel/dma/swiotlb.c | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/dma-iommu.c +1 -1
@@ -1180,7 +1180,7 @@ static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,trace_swiotlb_bounced (dev, phys, size); phys =swiotlb_tbl_map_single (dev, phys, size,iova_mask (iovad), dir, - attrs); + &attrs);/* * Untrusted devices should not see padding areas with random leftover
drivers/xen/swiotlb-xen.c +1 -1
@@ -243,7 +243,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,*/ trace_swiotlb_bounced (dev, dev_addr, size); - map =swiotlb_tbl_map_single (dev, phys, size,0 , dir, attrs); + map =swiotlb_tbl_map_single (dev, phys, size,0 , dir, &attrs);if (map == (phys_addr_t )DMA_MAPPING_ERROR )return DMA_MAPPING_ERROR ;
include/linux/swiotlb.h +1 -1
@@ -238,7 +238,7 @@ static inline phys_addr_t default_swiotlb_limit(void)phys_addr_t swiotlb_tbl_map_single (struct device *hwdev,phys_addr_t phys,size_t mapping_size,unsigned int alloc_aligned_mask, -enum dma_data_direction dir,unsigned long attrs); +enum dma_data_direction dir,unsigned long *attrs);dma_addr_t swiotlb_map (struct device *dev,phys_addr_t phys,size_t size,enum dma_data_direction dir,unsigned long attrs);
kernel/dma/swiotlb.c +3 -3
@@ -1396,7 +1396,7 @@ static unsigned long mem_used(struct io_tlb_mem *mem)*/ phys_addr_t swiotlb_tbl_map_single (struct device *dev,phys_addr_t orig_addr,size_t mapping_size,unsigned int alloc_align_mask, -enum dma_data_direction dir,unsigned long attrs) +enum dma_data_direction dir,unsigned long *attrs) {struct io_tlb_mem *mem = dev->dma_io_tlb_mem;unsigned int offset;
@@ -1430,7 +1430,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, size =ALIGN (mapping_size + offset, alloc_align_mask +1 ); index =swiotlb_find_slots (dev, orig_addr, size, alloc_align_mask, &pool);if (index ==-1 ) { -if (!(attrs &DMA_ATTR_NO_WARN )) +if (!(*attrs &DMA_ATTR_NO_WARN ))dev_warn_ratelimited (dev,"swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n" , size, mem->nslabs,mem_used (mem));
@@ -1609,7 +1609,7 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,trace_swiotlb_bounced (dev,phys_to_dma (dev, paddr), size); - swiotlb_addr =swiotlb_tbl_map_single (dev, paddr, size,0 , dir, attrs); + swiotlb_addr =swiotlb_tbl_map_single (dev, paddr, size,0 , dir, &attrs);if (swiotlb_addr == (phys_addr_t )DMA_MAPPING_ERROR )return DMA_MAPPING_ERROR ;
Signature · 1 line
2.43.0
PATCH v7 13/22 dma: swiotlb: track pool encryption state and honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Teach swiotlb to distinguish between encrypted and decrypted bounce buffer pools, and make allocation and mapping paths select a pool whose state matches the requested DMA attributes.
Add a cc_shared flag to io_tlb_mem, initialize it for the default and restricted pools, and propagate __DMA_ATTR_ALLOC_CC_SHARED into swiotlb pool allocation. Reject swiotlb alloc/map requests when the selected pool does not match the required encrypted/decrypted state.
Also return DMA addresses with the matching phys_to_dma_{encrypted, unencrypted} helper so the DMA address encoding stays consistent with the chosen pool.
--- include/linux/dma-direct.h | 10 +++ include/linux/swiotlb.h | 10 ++- kernel/dma/direct.c | 13 ++- kernel/dma/swiotlb.c | 177 ++++++++++++++++++++++++++++--------- 4 files changed, 162 insertions(+), 48 deletions(-)
include/linux/dma-direct.h +10 -0
@@ -77,6 +77,10 @@ static inline dma_addr_t dma_range_map_max(const struct bus_dma_region *map)#ifndef phys_to_dma_unencrypted#define phys_to_dma_unencrypted phys_to_dma#endif + +#ifndef phys_to_dma_encrypted +#define phys_to_dma_encrypted phys_to_dma +#endif #else static inline dma_addr_t __phys_to_dma (struct device *dev,phys_addr_t paddr) {
@@ -90,6 +94,12 @@ static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, {return dma_addr_unencrypted (__phys_to_dma (dev, paddr)); } + +static inline dma_addr_t phys_to_dma_encrypted (struct device *dev, +phys_addr_t paddr) +{ +return dma_addr_encrypted (__phys_to_dma (dev, paddr)); +}/* * If memory encryption is supported, phys_to_dma will set the memory encryption * bit in the DMA address, and dma_to_phys will clear it.
include/linux/swiotlb.h +8 -2
@@ -66,6 +66,7 @@ extern void __init swiotlb_update_mem_attributes(void);* @node: Member of the IO TLB memory pool list. * @rcu: RCU head for swiotlb_dyn_free(). * @transient: %true if transient memory pool. +* @cc_shared: %true if the pool memory is shared for confidential computing. */ struct io_tlb_pool {phys_addr_t start;
@@ -81,6 +82,7 @@ struct io_tlb_pool {struct list_head node;struct rcu_head rcu;bool transient; +bool cc_shared;#endif };
@@ -92,6 +94,7 @@ struct io_tlb_pool {* @debugfs: The dentry to debugfs. * @force_bounce: %true if swiotlb bouncing is forced * @for_alloc: %true if the pool is used for memory allocation +* @cc_shared: %true if the pool memory is shared for confidential computing. * @can_grow: %true if more pools can be allocated dynamically. * @phys_limit: Maximum allowed physical address. * @lock: Lock to synchronize changes to the list.
@@ -111,6 +114,7 @@ struct io_tlb_mem {struct dentry *debugfs;bool force_bounce;bool for_alloc; +bool cc_shared;#ifdef CONFIG_SWIOTLB_DYNAMIC bool can_grow;u64 phys_limit;
@@ -282,7 +286,8 @@ static inline void swiotlb_sync_single_for_cpu(struct device *dev,extern void swiotlb_print_info (void );#ifdef CONFIG_DMA_RESTRICTED_POOL -struct page *swiotlb_alloc (struct device *dev,size_t size); +struct page *swiotlb_alloc (struct device *dev,size_t size, +unsigned long attrs);bool swiotlb_free (struct device *dev,struct page *page,size_t size);void swiotlb_free_from_pool (struct device *dev,phys_addr_t tlb_addr,struct io_tlb_pool *pool);
@@ -292,7 +297,8 @@ static inline bool is_swiotlb_for_alloc(struct device *dev) return dev->dma_io_tlb_mem->for_alloc; }#else -static inline struct page *swiotlb_alloc (struct device *dev,size_t size) +static inline struct page *swiotlb_alloc (struct device *dev,size_t size, +unsigned long attrs) {return NULL ; }
kernel/dma/direct.c +9 -4
@@ -96,9 +96,10 @@ static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)return ret; } -static struct page *dma_direct_alloc_swiotlb (struct device *dev,size_t size) +static struct page *dma_direct_alloc_swiotlb (struct device *dev,size_t size, +unsigned long attrs) { -struct page *page =swiotlb_alloc (dev, size); +struct page *page =swiotlb_alloc (dev, size, attrs);if (page && !dma_coherent_ok (dev,page_to_phys (page), size)) {swiotlb_free (dev, page, size);
@@ -251,8 +252,12 @@ void *dma_direct_alloc(struct device *dev, size_t size, }if (is_swiotlb_for_alloc (dev)) { - page =dma_direct_alloc_swiotlb (dev, size); + page =dma_direct_alloc_swiotlb (dev, size, attrs);if (page) { +/* +* swiotlb allocations comes from pool already marked +* decrypted +*/ mark_mem_decrypt = false; goto setup_page; }
@@ -398,7 +403,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size, &ret, gfp, attrs);if (is_swiotlb_for_alloc (dev)) { - page =dma_direct_alloc_swiotlb (dev, size); + page =dma_direct_alloc_swiotlb (dev, size, attrs);if (!page)return NULL ;
kernel/dma/swiotlb.c +135 -42
407 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 14/22 dma-mapping: make dma_pgprot() honor __DMA_ATTR_ALLOC_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Fold encrypted/decrypted pgprot selection into dma_pgprot() so callers do not need to adjust the page protection separately.
Update dma_pgprot() to apply pgprot_decrypted() when DMA_ATTR_CC_SHARED or __DMA_ATTR_ALLOC_CC_SHARED is set and pgprot_encrypted() otherwise Convert the dma-direct mmap paths to pass DMA_ATTR_CC_SHARED instead of open-coding force_dma_unencrypted() handling around dma_pgprot().
--- kernel/dma/direct.c | 8 +++----- kernel/dma/mapping.c | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-)
kernel/dma/direct.c +3 -5
@@ -283,9 +283,6 @@ void *dma_direct_alloc(struct device *dev, size_t size,if (remap) {pgprot_t prot =dma_pgprot (dev,PAGE_KERNEL , attrs); -if (force_dma_unencrypted (dev)) - prot =pgprot_decrypted (prot); -/* remove any dirty cache lines on the kernel alias */ arch_dma_prep_coherent (page, size);
@@ -605,9 +602,10 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,unsigned long pfn =PHYS_PFN (dma_to_phys (dev, dma_addr));int ret = -ENXIO ; - vma->vm_page_prot =dma_pgprot (dev, vma->vm_page_prot, attrs);if (force_dma_unencrypted (dev)) - vma->vm_page_prot =pgprot_decrypted (vma->vm_page_prot); + attrs |=DMA_ATTR_CC_SHARED ; + + vma->vm_page_prot =dma_pgprot (dev, vma->vm_page_prot, attrs);if (dma_mmap_from_dev_coherent (dev, vma, cpu_addr, size, &ret))return ret;
kernel/dma/mapping.c +12 -4
@@ -537,13 +537,21 @@ EXPORT_SYMBOL(dma_get_sgtable_attrs);*/ pgprot_t dma_pgprot (struct device *dev,pgprot_t prot,unsigned long attrs) { +pgprot_t dma_prot; +if (dev_is_dma_coherent (dev)) -return prot; + dma_prot = prot;#ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE -if (attrs &DMA_ATTR_WRITE_COMBINE ) -return pgprot_writecombine (prot); + else if (attrs & DMA_ATTR_WRITE_COMBINE) + dma_prot =pgprot_writecombine (prot);#endif -return pgprot_dmacoherent (prot); + else + dma_prot =pgprot_dmacoherent (prot); + +if (attrs & (DMA_ATTR_CC_SHARED | __DMA_ATTR_ALLOC_CC_SHARED)) +return pgprot_decrypted (dma_prot); +else +return pgprot_encrypted (dma_prot); }#endif /* CONFIG_MMU */
Signature · 1 line
2.43.0
PATCH v7 15/22 dma-direct: pass attrs to dma_capable() for DMA_ATTR_CC_SHARED checks
In reply to Aneesh Kumar K.V (Arm)Teach dma_capable() about DMA_ATTR_CC_SHARED so the capability check can reject encrypted DMA addresses for devices that require unencrypted/shared DMA.
Also propagate DMA_ATTR_CC_SHARED in swiotlb_map() when the selected SWIOTLB pool is decrypted so the capability check sees the correct DMA address attribute.
--- arch/x86/kernel/amd_gart_64.c | 30 ++++++++++++++++-------------- drivers/xen/swiotlb-xen.c | 6 +++--- include/linux/dma-direct.h | 10 +++++++++- kernel/dma/direct.h | 6 +++--- kernel/dma/swiotlb.c | 2 +- 5 files changed, 32 insertions(+), 22 deletions(-)
arch/x86/kernel/amd_gart_64.c +16 -14
@@ -180,22 +180,23 @@ static void iommu_full(struct device *dev, size_t size, int dir) }static inline int -need_iommu (struct device *dev,unsigned long addr,size_t size) +need_iommu (struct device *dev,unsigned long addr,size_t size,unsigned long attrs) { -return force_iommu || !dma_capable (dev, addr, size, true); +return force_iommu || !dma_capable (dev, addr, size, true, attrs); }static inline int -nonforced_iommu (struct device *dev,unsigned long addr,size_t size) +nonforced_iommu (struct device *dev,unsigned long addr,size_t size, +unsigned long attrs) { -return !dma_capable (dev, addr, size, true); +return !dma_capable (dev, addr, size, true, attrs); }/* Map a single continuous physical area into the IOMMU. * Caller needs to check if the iommu is needed and flush. */ static dma_addr_t dma_map_area (struct device *dev,dma_addr_t phys_mem, -size_t size,int dir,unsigned long align_mask) +size_t size,int dir,unsigned long align_mask,unsigned long attrs) {unsigned long npages =iommu_num_pages (phys_mem, size,PAGE_SIZE );unsigned long iommu_page;
@@ -206,7 +207,7 @@ static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem, iommu_page =alloc_iommu (dev, npages, align_mask);if (iommu_page ==-1 ) { -if (!nonforced_iommu (dev, phys_mem, size)) +if (!nonforced_iommu (dev, phys_mem, size, attrs))return phys_mem;if (panic_on_overflow)panic ("dma_map_area overflow %lu bytes\n" , size);
@@ -231,10 +232,10 @@ static dma_addr_t gart_map_phys(struct device *dev, phys_addr_t paddr,if (unlikely (attrs &DMA_ATTR_MMIO ))return DMA_MAPPING_ERROR ; -if (!need_iommu (dev, paddr, size)) +if (!need_iommu (dev, paddr, size, attrs))return paddr; - bus =dma_map_area (dev, paddr, size, dir,0 ); + bus =dma_map_area (dev, paddr, size, dir,0 , attrs);flush_gart ();return bus;
@@ -289,7 +290,7 @@ static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,/* Fallback for dma_map_sg in case of overflow */ static int dma_map_sg_nonforce (struct device *dev,struct scatterlist *sg, -int nents,int dir) +int nents,int dir,unsigned long attrs) {struct scatterlist *s;int i;
@@ -301,8 +302,8 @@ static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,for_each_sg (sg, s, nents, i) {unsigned long addr =sg_phys (s); -if (nonforced_iommu (dev, addr, s->length)) { - addr =dma_map_area (dev, addr, s->length, dir,0 ); +if (nonforced_iommu (dev, addr, s->length, attrs)) { + addr =dma_map_area (dev, addr, s->length, dir,0 , attrs);if (addr ==DMA_MAPPING_ERROR ) {if (i >0 )gart_unmap_sg (dev, sg, i, dir,0 );
@@ -401,7 +402,7 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, s->dma_address = addr;BUG_ON (s->length ==0 ); - nextneed =need_iommu (dev, addr, s->length); + nextneed =need_iommu (dev, addr, s->length, attrs);/* Handle the previous not yet processed entries */ if (i > start) {
@@ -449,7 +450,7 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,/* When it was forced or merged try again in a dumb way */ if (force_iommu || iommu_merge) { - out =dma_map_sg_nonforce (dev, sg, nents, dir); + out =dma_map_sg_nonforce (dev, sg, nents, dir, attrs);if (out >0 )return out; }
@@ -473,7 +474,8 @@ gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,return vaddr; *dma_addr =dma_map_area (dev,virt_to_phys (vaddr), size, -DMA_BIDIRECTIONAL , (1UL <<get_order (size)) -1 ); +DMA_BIDIRECTIONAL , + (1UL <<get_order (size)) -1 , attrs);flush_gart ();if (unlikely (*dma_addr ==DMA_MAPPING_ERROR )) goto out_free;
drivers/xen/swiotlb-xen.c +3 -3
@@ -212,7 +212,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,BUG_ON (dir ==DMA_NONE );if (attrs &DMA_ATTR_MMIO ) { -if (unlikely (!dma_capable (dev, phys, size, false))) { +if (unlikely (!dma_capable (dev, phys, size, false, attrs))) { dev_err_once( dev, "DMA addr %pa+%zu overflow (mask %llx, bus limit %llx).\n",
@@ -231,7 +231,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,* we can safely return the device addr and not worry about bounce * buffering it. */ - if (dma_capable(dev, dev_addr, size, true) && + if (dma_capable(dev, dev_addr, size, true, attrs) && !dma_kmalloc_needs_bounce(dev, size, dir) && !range_straddles_page_boundary(phys, size) && !xen_arch_need_swiotlb(dev, phys, dev_addr) &&
@@ -253,7 +253,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,/* * Ensure that the address returned is DMA'ble */ -if (unlikely (!dma_capable (dev, dev_addr, size, true))) { +if (unlikely (!dma_capable (dev, dev_addr, size, true, attrs))) {__swiotlb_tbl_unmap_single (dev, map, size, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC ,swiotlb_find_pool (dev, map));
include/linux/dma-direct.h +9 -1
@@ -135,12 +135,20 @@ static inline bool force_dma_unencrypted(struct device *dev)#endif /* CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED */ static inline bool dma_capable (struct device *dev,dma_addr_t addr,size_t size, -bool is_ram) +bool is_ram,unsigned long attrs) {dma_addr_t end = addr + size -1 ;if (addr ==DMA_MAPPING_ERROR )return false; +/* +* The DMA address was derived from encrypted RAM, but this device +* requires unencrypted DMA addresses. Treat it as not DMA-capable +* so the caller can fall back to a suitable SWIOTLB pool. +*/ +if (!(attrs &DMA_ATTR_CC_SHARED ) &&force_dma_unencrypted (dev)) +return false; +if (is_ram && !IS_ENABLED (CONFIG_ARCH_DMA_ADDR_T_64BIT ) &&min (addr, end) <phys_to_dma (dev,PFN_PHYS (min_low_pfn)))return false;
kernel/dma/direct.h +3 -3
@@ -101,15 +101,15 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev,if (attrs &DMA_ATTR_MMIO ) { dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false))) +if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) goto err_overflow; }else if (attrs &DMA_ATTR_CC_SHARED ) { dma_addr =phys_to_dma_unencrypted (dev, phys); -if (unlikely (!dma_capable (dev, dma_addr, size, false))) +if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) goto err_overflow; }else { dma_addr =phys_to_dma (dev, phys); -if (unlikely (!dma_capable (dev, dma_addr, size, true)) || +if (unlikely (!dma_capable (dev, dma_addr, size, true, attrs)) ||dma_kmalloc_needs_bounce (dev, size, dir)) {if (is_swiotlb_active (dev) && !(attrs &DMA_ATTR_REQUIRE_COHERENT ))
kernel/dma/swiotlb.c +1 -1
@@ -1697,7 +1697,7 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,else dma_addr =phys_to_dma_encrypted (dev, swiotlb_addr); -if (unlikely (!dma_capable (dev, dma_addr, size, true))) { +if (unlikely (!dma_capable (dev, dma_addr, size, true, attrs))) {__swiotlb_tbl_unmap_single (dev, swiotlb_addr, size, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC ,swiotlb_find_pool (dev, swiotlb_addr));
Signature · 1 line
2.43.0
PATCH v7 16/22 dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Teach dma_direct_map_phys() to select the DMA address encoding based on DMA_ATTR_CC_SHARED.
Use phys_to_dma_unencrypted() for decrypted mappings and phys_to_dma_encrypted() otherwise. If a device requires unencrypted DMA but the source physical address is still encrypted, force the mapping through swiotlb so the DMA address and backing memory attributes remain consistent.
Update the arm64, x86, s390 and powerpc secure-guest setup to not use swiotlb force option
---
Changes from v3:
* Handle DMA_ATTR_MMIO
--- arch/arm64/mm/init.c | 4 +-- arch/powerpc/platforms/pseries/svm.c | 2 +- arch/s390/mm/init.c | 2 +- arch/x86/kernel/pci-dma.c | 4 +-- kernel/dma/direct.c | 4 ++- kernel/dma/direct.h | 45 +++++++++++++++------------- 6 files changed, 31 insertions(+), 30 deletions(-)
arch/arm64/mm/init.c +1 -3
@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }if (IS_ENABLED (CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC ) && !swiotlb) {/*
arch/powerpc/platforms/pseries/svm.c +1 -1
@@ -29,7 +29,7 @@ static int __init init_svm(void)* need to use the SWIOTLB buffer for DMA even if dma_capable() says * otherwise. */ - ppc_swiotlb_flags |=SWIOTLB_ANY |SWIOTLB_FORCE ; + ppc_swiotlb_flags |=SWIOTLB_ANY ;/* Share the SWIOTLB buffer with the host. */ swiotlb_update_mem_attributes ();
arch/s390/mm/init.c +1 -1
@@ -166,7 +166,7 @@ static void __init pv_init(void)virtio_set_mem_acc_cb (virtio_require_restricted_mem_acc);/* make sure bounce buffers are shared */ -swiotlb_init (true,SWIOTLB_FORCE |SWIOTLB_VERBOSE ); +swiotlb_init (true,SWIOTLB_VERBOSE );swiotlb_update_mem_attributes (); }
arch/x86/kernel/pci-dma.c +1 -3
@@ -59,10 +59,8 @@ static void __init pci_swiotlb_detect(void)* bounce buffers as the hypervisor can't access arbitrary VM memory * that is not explicitly shared with it. */ -if (cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) { +if (cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) x86_swiotlb_enable = true; - x86_swiotlb_flags |=SWIOTLB_FORCE ; - } }#else static inline void __init pci_swiotlb_detect (void )
kernel/dma/direct.c +3 -1
@@ -693,8 +693,10 @@ size_t dma_direct_max_mapping_size(struct device *dev) {/* If SWIOTLB is active, use its maximum mapping size */ if (is_swiotlb_active (dev) && - (dma_addressing_limited (dev) ||is_swiotlb_force_bounce (dev))) + (dma_addressing_limited (dev) ||is_swiotlb_force_bounce (dev) || +force_dma_unencrypted (dev)))return swiotlb_max_mapping_size (dev); +return SIZE_MAX ; }
kernel/dma/direct.h +24 -21
61 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 17/22 dma-direct: set decrypted flag for remapped DMA allocations
In reply to Aneesh Kumar K.V (Arm)Devices that are DMA non-coherent and require a remap were skipping dma_set_decrypted(), leaving DMA buffers encrypted even when the device requires unencrypted access. Move the call after the if (remap) branch so that both the direct and remapped allocation paths correctly mark the allocation as decrypted (or fail cleanly) before use.
Fix dma_direct_alloc() and dma_direct_free() to apply set_memory_*() to the linear-map alias of the backing pages instead of the remapped CPU address. Also disallow highmem pages for __DMA_ATTR_ALLOC_CC_SHARED, because highmem buffers do not provide a usable linear-map address.
--- kernel/dma/direct.c | 56 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-)
kernel/dma/direct.c +44 -12
@@ -196,14 +196,23 @@ void *dma_direct_alloc(struct device *dev, size_t size, {bool remap = false, set_uncached = false;bool mark_mem_decrypt = false; +bool allow_highmem = true;struct page *page;void *ret;if (force_dma_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED; -if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) +if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) { +/* +* Unencrypted/shared DMA requires a linear-mapped buffer +* address to look up the PFN and set architecture-required PFN +* attributes. This is not possible with HighMem. Avoid HighMem +* allocation. +*/ + allow_highmem = false; mark_mem_decrypt = true; + } size =PAGE_ALIGN (size);if (attrs &DMA_ATTR_NO_WARN )
@@ -265,7 +274,7 @@ 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, true); + page =__dma_direct_alloc_pages (dev, size, gfp & ~__GFP_ZERO, allow_highmem);if (!page)return NULL ;
@@ -280,6 +289,14 @@ void *dma_direct_alloc(struct device *dev, size_t size, set_uncached = false; } +if (mark_mem_decrypt) { +void *lm_addr; + + lm_addr =page_address (page); +if (set_memory_decrypted ((unsigned long )lm_addr,PFN_UP (size))) + goto out_leak_pages; + } +if (remap) {pgprot_t prot =dma_pgprot (dev,PAGE_KERNEL , attrs);
@@ -290,29 +307,36 @@ void *dma_direct_alloc(struct device *dev, size_t size, ret =dma_common_contiguous_remap (page, size, prot,__builtin_return_address (0 ));if (!ret) - goto out_free_pages; + goto out_encrypt_pages; }else { ret =page_address (page); -if (mark_mem_decrypt &&dma_set_decrypted (dev, ret, size)) - goto out_leak_pages; }memset (ret,0 , size);if (set_uncached) { +void *uncached_cpu_addr; +arch_dma_prep_coherent (page, size); - ret =arch_dma_set_uncached (ret, size); -if (IS_ERR (ret)) - goto out_encrypt_pages; + uncached_cpu_addr =arch_dma_set_uncached (ret, size); +if (IS_ERR (uncached_cpu_addr)) + goto out_free_remap_pages; + ret = uncached_cpu_addr; } *dma_handle =phys_to_dma_direct (dev,page_to_phys (page));return ret; + +out_free_remap_pages: +if (remap) +dma_common_free_remap (ret, size); + out_encrypt_pages: -if (mark_mem_decrypt &&dma_set_encrypted (dev,page_address (page), size)) -return NULL ; -out_free_pages: +if (mark_mem_decrypt && +dma_set_encrypted (dev,page_address (page), size)) + goto out_leak_pages; +if (!swiotlb_free (dev, page, size))dma_free_contiguous (dev, page, size);return NULL ;
@@ -375,8 +399,16 @@ void dma_direct_free(struct device *dev, size_t size, }else {if (IS_ENABLED (CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED ))arch_dma_clear_uncached (cpu_addr, size); -if (mark_mem_encrypted &&dma_set_encrypted (dev, cpu_addr, size)) + } + +if (mark_mem_encrypted) { +void *lm_addr; + + lm_addr =phys_to_virt (phys); +if (set_memory_encrypted ((unsigned long )lm_addr,PFN_UP (size))) { +pr_warn_ratelimited ("leaking DMA memory that can't be re-encrypted\n" );return ; + } }if (swiotlb_pool)
Signature · 1 line
2.43.0
PATCH v7 18/22 dma-direct: select DMA address encoding from __DMA_ATTR_ALLOC_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Make the dma-direct helpers derive the DMA address encoding from __DMA_ATTR_ALLOC_CC_SHARED instead of implicitly relying on force_dma_unencrypted() inside phys_to_dma_direct()
Pass an explicit unencrypted/decrypted state into phys_to_dma_direct(), make the alloc paths return DMA addresses that match the requested buffer encryption state. Also only call dma_set_decrypted() when __DMA_ATTR_ALLOC_CC_SHARED is actually set.
--- kernel/dma/direct.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-)
kernel/dma/direct.c +26 -17
@@ -24,11 +24,11 @@u64 zone_dma_limit__ro_after_init =DMA_BIT_MASK (24 );static inline dma_addr_t phys_to_dma_direct (struct device *dev, -phys_addr_t phys) +phys_addr_t phys,bool unencrypted) { -if (force_dma_unencrypted (dev)) +if (unencrypted)return phys_to_dma_unencrypted (dev, phys); -return phys_to_dma (dev, phys); +return phys_to_dma_encrypted (dev, phys); } static inline struct page *dma_direct_to_page(struct device *dev,
@@ -39,8 +39,9 @@ static inline struct page *dma_direct_to_page(struct device *dev,u64 dma_direct_get_required_mask (struct device *dev) { +bool require_decrypted =force_dma_unencrypted (dev);phys_addr_t phys = ((phys_addr_t )max_pfn <<PAGE_SHIFT ) -1 ; -u64 max_dma =phys_to_dma_direct (dev, phys); +u64 max_dma =phys_to_dma_direct (dev, phys, require_decrypted);return (1ULL << (fls64 (max_dma) -1 )) *2 -1 ; }
@@ -69,7 +70,8 @@ static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 *phys_limit)bool dma_coherent_ok (struct device *dev,phys_addr_t phys,size_t size) { -dma_addr_t dma_addr =phys_to_dma_direct (dev, phys); +bool require_decrypted =force_dma_unencrypted (dev); +dma_addr_t dma_addr =phys_to_dma_direct (dev, phys, require_decrypted);if (dma_addr ==DMA_MAPPING_ERROR )return false;
@@ -79,17 +81,18 @@ bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)static int dma_set_decrypted (struct device *dev,void *vaddr,size_t size) { -if (!force_dma_unencrypted (dev)) -return 0 ; -return set_memory_decrypted ((unsigned long )vaddr,PFN_UP (size)); +int ret; + + ret =set_memory_decrypted ((unsigned long )vaddr,PFN_UP (size)); +if (ret) +pr_warn_ratelimited ("leaking DMA memory that can't be decrypted\n" ); +return ret; }static int dma_set_encrypted (struct device *dev,void *vaddr,size_t size) {int ret; -if (!force_dma_unencrypted (dev)) -return 0 ; ret =set_memory_encrypted ((unsigned long )vaddr,PFN_UP (size));if (ret)pr_warn_ratelimited ("leaking DMA memory that can't be re-encrypted\n" );
@@ -169,7 +172,8 @@ static struct page *dma_direct_alloc_from_pool(struct device *dev, size_t size, dma_coherent_ok);if (!page)return NULL ; - *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); + *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), + !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED));return page; }
@@ -185,9 +189,11 @@ static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,/* remove any dirty cache lines on the kernel alias */ if (!PageHighMem (page))arch_dma_prep_coherent (page, size); - -/* return the page pointer as the opaque cookie */ - *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); +/* +* return the page pointer as the opaque cookie. +* Never used for unencrypted allocation +*/ + *dma_handle =phys_to_dma_encrypted (dev,page_to_phys (page));return page; }
@@ -324,7 +330,8 @@ void *dma_direct_alloc(struct device *dev, size_t size, ret = uncached_cpu_addr; } - *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); + *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), + !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED));return ret;
@@ -445,11 +452,13 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,return NULL ; ret =page_address (page); -if (dma_set_decrypted (dev, ret, size)) +if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) && +dma_set_decrypted (dev, ret, size)) goto out_leak_pages; setup_page:memset (ret,0 , size); - *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); + *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), + !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED));return page; out_leak_pages:return NULL ;
Signature · 1 line
2.43.0
PATCH v7 19/22 dma-direct: rename ret to cpu_addr in alloc helpers
In reply to Aneesh Kumar K.V (Arm)ret in dma_direct_alloc() and dma_direct_alloc_pages() holds the returned CPU mapping, not a generic return value. Rename it to cpu_addr and update the remaining uses to match.
This makes the allocation paths easier to follow and keeps the local naming consistent with what the variable actually represents.
--- kernel/dma/direct.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
kernel/dma/direct.c +20 -20
@@ -204,7 +204,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,bool mark_mem_decrypt = false;bool allow_highmem = true;struct page *page; -void *ret; +void *cpu_addr;if (force_dma_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
@@ -261,9 +261,10 @@ void *dma_direct_alloc(struct device *dev, size_t size,*/ if ((remap || (attrs & __DMA_ATTR_ALLOC_CC_SHARED)) &&dma_direct_use_pool (dev, gfp)) { - page =dma_direct_alloc_from_pool (dev, size, dma_handle, - &ret, gfp, attrs); -return page ? ret :NULL ; + page =dma_direct_alloc_from_pool (dev, size, + dma_handle, &cpu_addr, + gfp, attrs); +return page ? cpu_addr :NULL ; }if (is_swiotlb_for_alloc (dev)) {
@@ -310,34 +311,33 @@ void *dma_direct_alloc(struct device *dev, size_t size,arch_dma_prep_coherent (page, size);/* create a coherent mapping */ - ret =dma_common_contiguous_remap (page, size, prot, -__builtin_return_address (0 )); -if (!ret) + cpu_addr =dma_common_contiguous_remap (page, size, prot, +__builtin_return_address (0 )); +if (!cpu_addr) goto out_encrypt_pages; }else { - ret =page_address (page); + cpu_addr =page_address (page); } -memset (ret,0 , size); +memset (cpu_addr,0 , size);if (set_uncached) {void *uncached_cpu_addr;arch_dma_prep_coherent (page, size); - uncached_cpu_addr =arch_dma_set_uncached (ret, size); + uncached_cpu_addr =arch_dma_set_uncached (cpu_addr, size);if (IS_ERR (uncached_cpu_addr)) goto out_free_remap_pages; - ret = uncached_cpu_addr; + cpu_addr = uncached_cpu_addr; } *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED)); -return ret; - +return cpu_addr; out_free_remap_pages:if (remap) -dma_common_free_remap (ret, size); +dma_common_free_remap (cpu_addr, size); out_encrypt_pages: if (mark_mem_decrypt &&
@@ -429,21 +429,21 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size, {unsigned long attrs =0 ;struct page *page; -void *ret; +void *cpu_addr;if (force_dma_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&dma_direct_use_pool (dev, gfp))return dma_direct_alloc_from_pool (dev, size, dma_handle, - &ret, gfp, attrs); + &cpu_addr, gfp, attrs);if (is_swiotlb_for_alloc (dev)) { page =dma_direct_alloc_swiotlb (dev, size, attrs);if (!page)return NULL ; - ret =page_address (page); + cpu_addr =page_address (page); goto setup_page; }
@@ -451,12 +451,12 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,if (!page)return NULL ; - ret =page_address (page); + cpu_addr =page_address (page);if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) && -dma_set_decrypted (dev, ret, size)) +dma_set_decrypted (dev, cpu_addr, size)) goto out_leak_pages; setup_page: -memset (ret,0 , size); +memset (cpu_addr,0 , size); *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED));return page;
Signature · 1 line
2.43.0
PATCH v7 20/22 dma: swiotlb: free dynamic pools from process context
In reply to Aneesh Kumar K.V (Arm)swiotlb_dyn_free() is used after removing a dynamic swiotlb pool from RCU-protected lists. It can call swiotlb_free_tlb(), which may need to restore the encryption state of an unencrypted pool with set_memory_encrypted() before freeing the pages.
RCU callbacks run in atomic context, but set_memory_encrypted() is not guaranteed to be atomic-safe on all architectures. For example, page attribute updates may allocate page tables or take sleeping locks.
Use queue_rcu_work() for dynamic pool freeing instead. This keeps the RCU grace period before freeing a published pool, while running the actual pool teardown from workqueue context. Use the same helper for the transient-pool error path, since that path may also be reached from atomic DMA mapping context.
--- include/linux/swiotlb.h | 4 ++-- kernel/dma/swiotlb.c | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-)
include/linux/swiotlb.h +2 -2
@@ -64,7 +64,7 @@ extern void __init swiotlb_update_mem_attributes(void);* @areas: Array of memory area descriptors. * @slots: Array of slot descriptors. * @node: Member of the IO TLB memory pool list. -* @rcu: RCU head for swiotlb_dyn_free(). +* @dyn_free: RCU work item used to free the pool from process context. * @transient: %true if transient memory pool. * @cc_shared: %true if the pool memory is shared for confidential computing. */
@@ -80,7 +80,7 @@ struct io_tlb_pool {struct io_tlb_slot *slots;#ifdef CONFIG_SWIOTLB_DYNAMIC struct list_head node; -struct rcu_head rcu; +struct rcu_work dyn_free;bool transient;bool cc_shared;#endif
kernel/dma/swiotlb.c +11 -8
@@ -779,13 +779,10 @@ static void swiotlb_dyn_alloc(struct work_struct *work)add_mem_pool (mem, pool); } -/** -* swiotlb_dyn_free() - RCU callback to free a memory pool -* @rcu: RCU head in the corresponding struct io_tlb_pool. -*/ -static void swiotlb_dyn_free (struct rcu_head *rcu) +static void swiotlb_dyn_free_work (struct work_struct *work) { - struct io_tlb_pool *pool = container_of(rcu, struct io_tlb_pool, rcu); +struct io_tlb_pool *pool = + container_of(to_rcu_work(work), struct io_tlb_pool, dyn_free);size_t slots_size =array_size (sizeof (*pool->slots), pool->nslabs);size_t tlb_size = pool->end - pool->start;
@@ -794,6 +791,12 @@ static void swiotlb_dyn_free(struct rcu_head *rcu)kfree (pool); } +static void swiotlb_schedule_dyn_free (struct io_tlb_pool *pool) +{ +INIT_RCU_WORK (&pool->dyn_free, swiotlb_dyn_free_work); +queue_rcu_work (system_wq, &pool->dyn_free); +} +/** * __swiotlb_find_pool() - find the IO TLB pool for a physical address * @dev: Device which has mapped the DMA buffer.
@@ -840,7 +843,7 @@ static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool)list_del_rcu (&pool->node);spin_unlock_irqrestore (&dev->dma_io_tlb_lock, flags); - call_rcu(&pool->rcu, swiotlb_dyn_free); + swiotlb_schedule_dyn_free(pool); }#endif /* CONFIG_SWIOTLB_DYNAMIC */
@@ -1281,7 +1284,7 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, index =swiotlb_search_pool_area (dev, pool,0 , orig_addr, tbl_dma_addr, alloc_size, alloc_align_mask);if (index <0 ) { -swiotlb_dyn_free (&pool->rcu); +swiotlb_schedule_dyn_free (pool);return -1 ; }
Signature · 1 line
2.43.0
PATCH v7 21/22 dma: swiotlb: handle set_memory_decrypted() failures
In reply to Aneesh Kumar K.V (Arm)Check the return value when converting swiotlb pools between encrypted and decrypted mappings. If the default pool cannot be decrypted after early initialization, mark the pool fully used so it cannot satisfy future bounce allocations.
For late initialization, return the set_memory_decrypted() failure. For restricted DMA pools, fail device initialization if the reserved pool cannot be decrypted.
This prevents swiotlb from using pools whose encryption attributes do not match their metadata, and avoids returning pages with uncertain encryption state back to the allocator.
--- kernel/dma/swiotlb.c | 80 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 15 deletions(-)
kernel/dma/swiotlb.c +65 -15
@@ -248,6 +248,23 @@ static inline unsigned long nr_slots(u64 val)return DIV_ROUND_UP (val,IO_TLB_SIZE ); } +static void swiotlb_mark_pool_used (struct io_tlb_pool *pool) +{ +unsigned long i; + +for (i =0 ; i < pool->nareas; i++) { + pool->areas[i].index =0 ; + pool->areas[i].used = pool->area_nslabs; + } + +for (i =0 ; i < pool->nslabs; i++) { + pool->slots[i].list =0 ; + pool->slots[i].orig_addr =INVALID_PHYS_ADDR ; + pool->slots[i].alloc_size =0 ; + pool->slots[i].pad_slots =0 ; + } +} +/* * Early SWIOTLB allocation may be too early to allow an architecture to * perform the desired operations. This function allows the architecture to
@@ -272,8 +289,16 @@ void __init swiotlb_update_mem_attributes(void)return ; bytes =PAGE_ALIGN (mem->nslabs <<IO_TLB_SHIFT ); -if (io_tlb_default_mem.cc_shared) -set_memory_decrypted ((unsigned long )mem->vaddr, bytes >>PAGE_SHIFT ); +if (io_tlb_default_mem.cc_shared) { +int ret; + + ret =set_memory_decrypted ((unsigned long )mem->vaddr, + bytes >>PAGE_SHIFT ); +if (ret) { +pr_warn ("Failed to decrypt default memory pool, disabling it\n" ); +swiotlb_mark_pool_used (mem); + } + } } static void swiotlb_init_io_tlb_pool(struct io_tlb_pool *mem, phys_addr_t start,
@@ -442,9 +467,10 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, {struct io_tlb_pool *mem = &io_tlb_default_mem.defpool;unsigned long nslabs =ALIGN (size >>IO_TLB_SHIFT ,IO_TLB_SEGSIZE ); +unsigned int order, area_order, slot_order; +bool leak_pages = false;unsigned int nareas;unsigned char *vstart =NULL ; -unsigned int order, area_order;bool retried = false;int rc =0 ;
@@ -504,6 +530,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, (PAGE_SIZE << order) >> 20); } + rc = -ENOMEM ; nareas =limit_nareas (default_nareas, nslabs); area_order =get_order (array_size (sizeof (*mem->areas), nareas)); mem->areas = (struct io_tlb_area *)
@@ -511,14 +538,20 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,if (!mem->areas) goto error_area; + slot_order =get_order (array_size (sizeof (*mem->slots), nslabs)); mem->slots = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, -get_order (array_size (sizeof (*mem->slots), nslabs))); + slot_order);if (!mem->slots) goto error_slots; -if (io_tlb_default_mem.cc_shared) -set_memory_decrypted ((unsigned long )vstart, - (nslabs <<IO_TLB_SHIFT ) >>PAGE_SHIFT ); +if (io_tlb_default_mem.cc_shared) { + rc =set_memory_decrypted ((unsigned long )vstart, + (nslabs <<IO_TLB_SHIFT ) >>PAGE_SHIFT ); +if (rc) { + leak_pages = true; + goto error_decrypt; + } + }swiotlb_init_io_tlb_pool (mem,virt_to_phys (vstart), vstart, nslabs, true, nareas);
@@ -527,16 +560,20 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,swiotlb_print_info ();return 0 ; +error_decrypt: +free_pages ((unsigned long )mem->slots, slot_order); error_slots:free_pages ((unsigned long )mem->areas, area_order); error_area: -free_pages ((unsigned long )vstart, order); -return -ENOMEM ; +if (!leak_pages) +free_pages ((unsigned long )vstart, order); +return rc; }void __init swiotlb_exit (void ) {struct io_tlb_pool *mem = &io_tlb_default_mem.defpool; +bool leak_pages = false;unsigned long tbl_vaddr;size_t tbl_size, slots_size;unsigned int area_order;
@@ -552,19 +589,23 @@ void __init swiotlb_exit(void) tbl_size =PAGE_ALIGN (mem->end - mem->start); slots_size =PAGE_ALIGN (array_size (sizeof (*mem->slots), mem->nslabs)); -if (io_tlb_default_mem.cc_shared) -set_memory_encrypted (tbl_vaddr, tbl_size >>PAGE_SHIFT ); +if (io_tlb_default_mem.cc_shared) { +if (set_memory_encrypted (tbl_vaddr, tbl_size >>PAGE_SHIFT )) + leak_pages = true; + }if (mem->late_alloc) { area_order =get_order (array_size (sizeof (*mem->areas), mem->nareas));free_pages ((unsigned long )mem->areas, area_order); -free_pages (tbl_vaddr,get_order (tbl_size)); +if (!leak_pages) +free_pages (tbl_vaddr,get_order (tbl_size));free_pages ((unsigned long )mem->slots,get_order (slots_size)); }else {memblock_free (mem->areas,array_size (sizeof (*mem->areas), mem->nareas)); -memblock_phys_free (mem->start, tbl_size); +if (!leak_pages) +memblock_phys_free (mem->start, tbl_size);memblock_free (mem->slots, slots_size); }
@@ -1957,9 +1998,18 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,* restricted mem pool is shared by default */ if (cc_platform_has (CC_ATTR_MEM_ENCRYPT )) { +int ret; + mem->cc_shared = true; -set_memory_decrypted ((unsigned long )phys_to_virt (rmem->base), - rmem->size >>PAGE_SHIFT ); + ret =set_memory_decrypted ((unsigned long )phys_to_virt (rmem->base), + rmem->size >>PAGE_SHIFT ); +if (ret) { +dev_err (dev,"Failed to decrypt restricted DMA pool\n" ); +kfree (pool->areas); +kfree (pool->slots); +kfree (mem); +return ret; + } }else { mem->cc_shared = false; }
Signature · 1 line
2.43.0
PATCH v7 22/22 swiotlb: remove unused SWIOTLB_FORCE flag
In reply to Aneesh Kumar K.V (Arm)SWIOTLB_FORCE has no remaining in-tree users. Forced bouncing is now controlled through the swiotlb=force command line option via swiotlb_force_bounce.
Remove the unused flag and simplify the force_bounce initialization.
--- include/linux/swiotlb.h | 3 +-- kernel/dma/swiotlb.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
include/linux/swiotlb.h +1 -2
@@ -15,8 +15,7 @@ struct page;struct scatterlist ;#define SWIOTLB_VERBOSE (1 << 0)/* verbose initialization */ -#define SWIOTLB_FORCE (1 << 1)/* force bounce buffering */ -#define SWIOTLB_ANY (1 << 2)/* allow any memory for the buffer */ +#define SWIOTLB_ANY (1 << 1)/* allow any memory for the buffer */ /* * Maximum allowable number of contiguous slabs to map,
kernel/dma/swiotlb.c +1 -2
@@ -400,8 +400,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,if (swiotlb_force_disable)return ; - io_tlb_default_mem.force_bounce = - swiotlb_force_bounce || (flags &SWIOTLB_FORCE ); + io_tlb_default_mem.force_bounce = swiotlb_force_bounce;#ifdef CONFIG_SWIOTLB_DYNAMIC if (!remap)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 02/22] dma-pool: fix page leak in atomic_pool_expand() cleanup
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] Pages allocated from CMA are incorrectly freed using __free_pages() instead of dma_release_from_contiguous(), permanently leaking CMA tracking capacity. --
commit ede353d82bd6f31cd894661b3fa06b014104f3cc
Author: Aneesh Kumar K.V (Arm) <[email protected]>
dma-pool: fix page leak in atomic_pool_expand() cleanup
atomic_pool_expand() frees the allocated pages from the remove_mapping error path only when CONFIG_DMA_DIRECT_REMAP is enabled. This patch ensures the pages are always freed on failure paths by moving the __free_pages() call outside the CONFIG_DMA_DIRECT_REMAP block.
Quoted 4 lines
kernel/dma/pool.c +0
-0
[ ... ]
Quoted 5 lines
kernel/dma/pool.c +0
-1@@ -130,14 +133,15 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, 1 << order);if (WARN_ON_ONCE (ret)) {/* Decrypt succeeded but encrypt failed, purposely leak */ - goto out;
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 144
Quoted 10 lines
kernel/dma/pool.c +3
-1+ leak_pages = true; } remove_mapping: #ifdef CONFIG_DMA_DIRECT_REMAPdma_common_free_remap (addr ,pool_size ); free_page: -__free_pages (page, order); #endif +if (!leak_pages) +__free_pages (page, order);
^^^^^^^^^^^^^^^^^^^^^^^^^
[Severity: Medium]
This is a pre-existing issue, but does this cleanup path correctly handle pages allocated from CMA?
Looking at the allocation logic earlier in atomic_pool_expand():
do {
pool_size = 1 << (PAGE_SHIFT + order);
if (cma_in_zone(gfp))
page = dma_alloc_from_contiguous(NULL, 1 << order,
order, false);
if (!page)
page = alloc_pages(gfp | __GFP_NOWARN, order);
} while (!page && order-- > 0);If the page was allocated via dma_alloc_from_contiguous(), freeing it directly with __free_pages() bypasses the CMA bitmap cleanup. This returns the page to the buddy allocator but permanently reduces the memory available for contiguous allocations.
Should this instead check if the page came from CMA and call dma_release_from_contiguous() accordingly?
Quoted 3 lines
kernel/dma/pool.c +0
-0out:return ret; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] dma_map_resource() fails for devices using a dma_range_map that does not cover MMIO regions, as…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] dma_map_resource() fails for devices using a dma_range_map that does not cover MMIO regions, as DMA_ATTR_MMIO mappings are now unconditionally subjected to phys_to_dma() translation.
- [High] Peer-to-Peer DMA (MMIO) breaks on AMD SME systems because the memory encryption mask (C-bit) is erroneously applied to MMIO addresses, potentially causing memory corruption. --
commit 5904e27516915147560df0a4dd16561a829eaf9e
Author: Aneesh Kumar K.V (Arm) <[email protected]>
dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
This commit modifies dma_direct_map_phys() to select the DMA address encoding based on DMA_ATTR_CC_SHARED. It uses phys_to_dma_unencrypted() for decrypted mappings and phys_to_dma_encrypted() otherwise, while preventing the swiotlb force option for several architectures' secure-guest setups.
Quoted 29 lines
kernel/dma/direct.h +9
-8@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; } -if (attrs &DMA_ATTR_MMIO ) {
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 109
Quoted 10 lines
kernel/dma/direct.h +5
-4- dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys); + +if (attrs &DMA_ATTR_MMIO ) {
[Severity: High]
Will this break dma_map_resource() for devices that use a dma_range_map?
Previously, DMA_ATTR_MMIO addresses bypassed translation and directly used dma_addr = phys. By routing MMIO addresses through phys_to_dma_encrypted() or phys_to_dma_unencrypted(), they now pass through __phys_to_dma().
Since MMIO addresses do not reside in system RAM, they will be absent from the device's dma_range_map. Doesn't this cause translate_phys_to_dma() to return DMA_MAPPING_ERROR and fail the mapping?
[Severity: High]
On AMD SME systems, does this inadvertently apply the memory encryption mask (C-bit) to MMIO addresses?
If a capable device performs Peer-to-Peer DMA, force_dma_unencrypted(dev) returns false, so DMA_ATTR_CC_SHARED is not set. The MMIO address then passes through phys_to_dma_encrypted(), which injects the C-bit via __sme_set().
If the PCIe Root Complex receives an MMIO address with the C-bit set, will it misroute the transaction to system RAM instead of the target PCI BAR, potentially causing memory corruption?
Quoted 4 lines
kernel/dma/direct.h +0
-2if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) goto err_overflow; - }else { - dma_addr =phys_to_dma (dev, phys);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
[email protected]
AI review Tested-by Jiri Pirko Tested-by Michael Kelley Tested-by Mostafa Saleh Reviewed-by Petr Tesarik on v7 15/22Re: [PATCH v7 15/22] dma-direct: pass attrs to dma_capable() for DMA_ATTR_CC_SHARED checks
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] xen_swiotlb_map_phys() unconditionally rejects valid MMIO DMA mappings for devices requiring unencrypted DMA, breaking peer-to-peer DMA.
- [High] xen_swiotlb_map_phys() fails to strip the encryption bit from bounce buffer DMA addresses.
Pre-existing issues:
- [Critical] gart_alloc_coherent() and gart_free_coherent() pass an invalid dma_addr to dma_direct_free(), causing severe page allocator corruption and physical memory leaks. --
commit d278e823d2c8033c6a6105a3f51b2763931c565f
Author: Aneesh Kumar K.V (Arm) <[email protected]>
dma-direct: pass attrs to dma_capable() for DMA_ATTR_CC_SHARED checks
Teach dma_capable() about DMA_ATTR_CC_SHARED so the capability check can reject encrypted DMA addresses for devices that require unencrypted/shared DMA.
Also propagate DMA_ATTR_CC_SHARED in swiotlb_map() when the selected SWIOTLB pool is decrypted so the capability check sees the correct DMA address attribute.
Quoted 4 lines
arch/x86/kernel/amd_gart_64.c +0
-0
[ ... ]
arch/x86/kernel/amd_gart_64.c gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr, · line 481
Quoted 10 lines
arch/x86/kernel/amd_gart_64.c +2
-1@@ -473,7 +474,8 @@ gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,return vaddr; *dma_addr =dma_map_area (dev,virt_to_phys (vaddr), size, -DMA_BIDIRECTIONAL , (1UL <<get_order (size)) -1 ); +DMA_BIDIRECTIONAL , + (1UL <<get_order (size)) -1 , attrs);flush_gart ();if (unlikely (*dma_addr ==DMA_MAPPING_ERROR )) goto out_free;
[Severity: Critical]
This is a pre-existing issue, but does the error path in gart_alloc_coherent() and gart_free_coherent() pass an invalid dma_addr to dma_direct_free()?
If dma_map_area() fails, *dma_addr becomes DMA_MAPPING_ERROR, and the error path at out_free passes it to dma_direct_free(). On success, gart_free_coherent() later passes the GART-mapped dma_addr to dma_direct_free().
Because dma_direct_free() uses the provided dma_addr to locate the backing struct page (when dma_free_from_pool() is false), passing a GART aperture address or DMA_MAPPING_ERROR instead of the physical DMA address causes it to operate on bogus page structures, leading to severe memory corruption and physical page leaks.
drivers/xen/swiotlb-xen.c static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys, · line 215
Quoted 10 lines
drivers/xen/swiotlb-xen.c +1
-1@@ -212,7 +212,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,BUG_ON (dir ==DMA_NONE );if (attrs &DMA_ATTR_MMIO ) { -if (unlikely (!dma_capable (dev, phys, size, false))) { +if (unlikely (!dma_capable (dev, phys, size, false, attrs))) {
[Severity: High]
Does this unconditionally reject valid MMIO DMA mappings for devices requiring unencrypted DMA, breaking peer-to-peer DMA?
When mapping MMIO memory, attrs typically lacks DMA_ATTR_CC_SHARED. For CoCo guests under Xen, force_dma_unencrypted(dev) is true, so dma_capable() will evaluate !(attrs & DMA_ATTR_CC_SHARED) && force_dma_unencrypted(dev) as true, thus rejecting the mapping.
While dma_direct_map_phys() was fixed elsewhere in this series to append DMA_ATTR_CC_SHARED for MMIO, it appears xen_swiotlb_map_phys() was missed.
Quoted 3 lines
drivers/xen/swiotlb-xen.c +0
-0dev_err_once( dev, "DMA addr %pa+%zu overflow (mask %llx, bus limit %llx).\n",
[ ... ]
drivers/xen/swiotlb-xen.c static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys, · line 256
Quoted 6 lines
drivers/xen/swiotlb-xen.c +1
-1@@ -253,7 +253,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,/* * Ensure that the address returned is DMA'ble */ -if (unlikely (!dma_capable (dev, dev_addr, size, true))) { +if (unlikely (!dma_capable (dev, dev_addr, size, true, attrs))) {
[Severity: High]
Does this fail to strip the encryption bit from bounce buffer DMA addresses?
When a DMA mapping requires a bounce buffer, swiotlb_tbl_map_single() allocates an unencrypted buffer and injects DMA_ATTR_CC_SHARED into attrs. However, dev_addr was set via xen_phys_to_dma(dev, map) right above this check, which relies on phys_to_dma() and unconditionally applies the physical encryption bit to the DMA address.
Because attrs now contains DMA_ATTR_CC_SHARED, dma_capable() will approve the address. As a result, the device erroneously receives an encrypted DMA address for decrypted memory.
Quoted 3 lines
drivers/xen/swiotlb-xen.c +0
-0__swiotlb_tbl_unmap_single (dev, map, size, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC ,swiotlb_find_pool (dev, map));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Quoted 39 lines · Aneesh Kumar K.V (Arm)
This series tracks confidential-computing shared DMA state through the dma-direct, dma-pool, and swiotlb paths so that encrypted and decrypted DMA buffers are handled consistently.
Today, the direct DMA path mostly relies on
force_dma_unencrypted()for shared/decrypted buffer handling. This series consolidates theforce_dma_unencrypted()checks in the top-level functions and ensures that the remaining DMA interfaces use DMA attributes to make the correct decisions.The series separates mapping and allocation state:
-DMA_ATTR_CC_SHAREDdescribes the DMA address attribute requested for a mapping. It tells the DMA mapping path that the DMA address must target shared/decrypted memory.
-__DMA_ATTR_ALLOC_CC_SHAREDis an internal DMA-mapping attribute used only by allocation paths after the DMA core decides that the backing pages must be allocated as shared/decrypted memory.The series:
- moves swiotlb-backed allocations out of__dma_direct_alloc_pages(),
- uses__DMA_ATTR_ALLOC_CC_SHAREDthrough the dma-direct alloc/free paths
- teaches the atomic DMA pools to track encrypted versus decrypted state
- tracks swiotlb pool encryption state and enforces strict pool selection
- centralizes encrypted/decrypted pgprot handling indma_pgprot()using DMA attributes
- passes DMA attributes down todma_capable()so capability checks can validate whether the selected DMA address encoding matchesDMA_ATTR_CC_SHARED
- makesdma_direct_map_phys()choose the DMA address encoding fromDMA_ATTR_CC_SHAREDand fall back to swiotlb when a shared DMA request cannot use the direct mapping, which lets arm64 and x86 CCA guests stop relying onSWIOTLB_FORCEfor DMA mappings
- use the selected swiotlb pool state to derive the returned DMA address
- reportsCC_ATTR_GUEST_MEM_ENCRYPTfor arm64 Realms, powerpc secure guests, and s390 protected virtualization guests.
Quoted 7 lines
Dependency:
This series depends on the pKVM changes posted at:
https://lore.kernel.org/all/[email protected]Please merge this series only after the pKVM changes above are merged. Otherwise pKVM will be broken.
A rebased tree on top of the dependent pKVM changes can be found at:
https://gitlab.arm.com/linux-arm/linux-cca/-/tree/scratch/pkvm/testing?ref_type=heads
The patches had minor conflicts. I am not sure how we want to get this merged.
Should we ask the pKVM maintainers for a topic branch, and then I can repost the updated series on top of that?
-aneesh
Marek Szyprowski
maintainerQuoted 48 lines · Aneesh Kumar K.V
Quoted 39 lines · Aneesh Kumar K.V (Arm)
This series tracks confidential-computing shared DMA state through the dma-direct, dma-pool, and swiotlb paths so that encrypted and decrypted DMA buffers are handled consistently.
Today, the direct DMA path mostly relies on
force_dma_unencrypted()for shared/decrypted buffer handling. This series consolidates theforce_dma_unencrypted()checks in the top-level functions and ensures that the remaining DMA interfaces use DMA attributes to make the correct decisions.The series separates mapping and allocation state:
-DMA_ATTR_CC_SHAREDdescribes the DMA address attribute requested for a mapping. It tells the DMA mapping path that the DMA address must target shared/decrypted memory.
-__DMA_ATTR_ALLOC_CC_SHAREDis an internal DMA-mapping attribute used only by allocation paths after the DMA core decides that the backing pages must be allocated as shared/decrypted memory.The series:
- moves swiotlb-backed allocations out of__dma_direct_alloc_pages(),
- uses__DMA_ATTR_ALLOC_CC_SHAREDthrough the dma-direct alloc/free paths
- teaches the atomic DMA pools to track encrypted versus decrypted state
- tracks swiotlb pool encryption state and enforces strict pool selection
- centralizes encrypted/decrypted pgprot handling indma_pgprot()using DMA attributes
- passes DMA attributes down todma_capable()so capability checks can validate whether the selected DMA address encoding matchesDMA_ATTR_CC_SHARED
- makesdma_direct_map_phys()choose the DMA address encoding fromDMA_ATTR_CC_SHAREDand fall back to swiotlb when a shared DMA request cannot use the direct mapping, which lets arm64 and x86 CCA guests stop relying onSWIOTLB_FORCEfor DMA mappings
- use the selected swiotlb pool state to derive the returned DMA address
- reportsCC_ATTR_GUEST_MEM_ENCRYPTfor arm64 Realms, powerpc secure guests, and s390 protected virtualization guests.Quoted 7 lines
Dependency:
This series depends on the pKVM changes posted at:
https://lore.kernel.org/all/[email protected]Please merge this series only after the pKVM changes above are merged. Otherwise pKVM will be broken.
Quoted 8 lines
A rebased tree on top of the dependent pKVM changes can be found at:
https://gitlab.arm.com/linux-arm/linux-cca/-/tree/scratch/pkvm/testing?ref_type=headsThe patches had minor conflicts. I am not sure how we want to get this merged.
Should we ask the pKVM maintainers for a topic branch, and then I can repost the updated series on top of that?
I'm fine with merging on top of the topic branch and I assume that this patchset is mature enough to give it a try in linux-next, but first I would like to get a review or at least acks from others with good CC knowledge or experience.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v7 08/22] coco: arm64: s390: powerpc: Mark secure guests with CC_ATTR_GUEST_MEM_ENCRYPT
In reply to Aneesh Kumar K.V (Arm)Quoted 4 lines · Aneesh Kumar K.V (Arm)
arm64 Realms, powerpc secure guests and s390 protected virtualization guests currently report
CC_ATTR_MEM_ENCRYPT, but notCC_ATTR_GUEST_MEM_ENCRYPT.
Quoted 8 lines
These environments are all secure guest configurations. Their
force_dma_unencrypted()implementations also treat the same secure guest state as requiring unencrypted DMA. ReportCC_ATTR_GUEST_MEM_ENCRYPTfor the same condition so generic confidential-computing code can distinguish guest memory encryption from host memory encryption instead of relying only onCC_ATTR_MEM_ENCRYPT.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
For arm64 bits:
Quoted 11 lines
--- arch/arm64/kernel/rsi.c | 1 + arch/powerpc/platforms/pseries/cc_platform.c | 1 + arch/s390/mm/init.c | 1 + 3 files changed, 3 insertions(+)arch/arm64/kernel/rsi.c +0
-0
arch/arm64/kernel/rsi.c bool cc_platform_has(enum cc_attr attr) · line 33
Quoted 7 lines
arch/arm64/kernel/rsi.c +1
-0@@ -27,6 +27,7 @@ bool cc_platform_has(enum cc_attr attr) {switch (attr) {case CC_ATTR_MEM_ENCRYPT : +case CC_ATTR_GUEST_MEM_ENCRYPT :return is_realm_world ();default :return false;
Suzuki
Re: [PATCH v7 08/22] coco: arm64: s390: powerpc: Mark secure guests with CC_ATTR_GUEST_MEM_ENCRYPT
In reply to Aneesh Kumar K.V (Arm)Quoted 4 lines · Aneesh Kumar K.V (Arm)
arm64 Realms, powerpc secure guests and s390 protected virtualization guests currently report
CC_ATTR_MEM_ENCRYPT, but notCC_ATTR_GUEST_MEM_ENCRYPT.
Quoted 8 lines
These environments are all secure guest configurations. Their
force_dma_unencrypted()implementations also treat the same secure guest state as requiring unencrypted DMA. ReportCC_ATTR_GUEST_MEM_ENCRYPTfor the same condition so generic confidential-computing code can distinguish guest memory encryption from host memory encryption instead of relying only onCC_ATTR_MEM_ENCRYPT.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
For arm64:
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 344
Quoted 13 lines · Aneesh Kumar K.V (Arm)
arch/arm64/mm/init.c +1
-3@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }
For this part:
Quoted 28 lines
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 10 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);
For AMD/SME, on host with memory encryption we now end up setting the C bit for DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like __phys_to_dma() for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the old dma_addr = phys if MMIO and ignore any unlikely DMA offsets.
In the other case, for an arm CCA guest, if the MMIO is shared we end up setting the shared attribute but that's fine, it's only an IPA address.
Signature · 1 line
Catalin
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Quoted 34 lines · Aneesh Kumar K.V (Arm)
Teach
dma_direct_map_phys()to select the DMA address encoding based onDMA_ATTR_CC_SHARED.Use
phys_to_dma_unencrypted()for decrypted mappings andphys_to_dma_encrypted()otherwise. If a device requires unencrypted DMA but the source physical address is still encrypted, force the mapping through swiotlb so the DMA address and backing memory attributes remain consistent.Update the arm64, x86, s390 and powerpc secure-guest setup to not use swiotlb force option
Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>---
Changes from v3:
* HandleDMA_ATTR_MMIO--- arch/arm64/mm/init.c | 4 +-- arch/powerpc/platforms/pseries/svm.c | 2 +- arch/s390/mm/init.c | 2 +- arch/x86/kernel/pci-dma.c | 4 +-- kernel/dma/direct.c | 4 ++- kernel/dma/direct.h | 45 +++++++++++++++------------- 6 files changed, 31 insertions(+), 30 deletions(-)arch/arm64/mm/init.c +0
-0@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;
arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 345
Quoted 10 lines
arch/arm64/mm/init.c +1
-3bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }if (IS_ENABLED (CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC ) && !swiotlb) {/*
For arm64 CCA bits:
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Catalin MarinasQuoted 65 lines · Catalin Marinas
arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 344
Quoted 13 lines · Aneesh Kumar K.V (Arm)
arch/arm64/mm/init.c +1
-3@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }For this part:
Reviewed-by Catalin Marinas <[email protected]>Quoted 28 lines
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 10 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.
Quoted 3 lines
In the other case, for an arm CCA guest, if the MMIO is shared we end up setting the shared attribute but that's fine, it's only an IPA address.
P2PDMA with CoCo is currently not supported, as discussed here:
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]
Since I do not have much experience with P2PDMA, I have prepared a patch that Leon Romanovsky <[email protected]> has kindly agreed to review to ensure it is correct. I have attached it below.
The conclusion is that P2P is not currently supported, so we get this series merged first and handle P2PDMA support as a follow-up outside this series.
commit f39bcb603c5cc4cfa738597715828ac45f01f142
Author: Aneesh Kumar K.V (Arm) <[email protected]> Date: Fri May 22 10:10:35 2026 +0530
PCI/P2PDMA: Carry host bridge DMA attrs through map and unmap
P2PDMA host bridge mappings currently pass DMA_ATTR_MMIO and rely on dma-direct to add DMA_ATTR_CC_SHARED for devices that require unencrypted DMA. That puts P2PDMA-specific confidential-computing policy in the generic dma-direct MMIO mapping path and does not preserve the selected attributes through all unmap paths.
Add DMA mapping flags to struct p2pdma_provider for host bridge mappings and use those flags in the block, dma-buf and HMM mapping paths. Store the attributes selected at map time where the unmap side no longer has provider context, so that the unmap path uses the same attributes that were used for mapping.
block/blk-mq-dma.c +7 -4
@@ -87,10 +87,11 @@ static bool blk_dma_map_bus(struct blk_dma_iter *iter, struct phys_vec *vec)static bool blk_dma_map_direct (struct request *req,struct device *dma_dev,struct blk_dma_iter *iter,struct phys_vec *vec) { -unsigned int attrs =0 ; +unsigned long attrs =0 ;if (iter->p2pdma.map ==PCI_P2PDMA_MAP_THRU_HOST_BRIDGE ) - attrs |=DMA_ATTR_MMIO ; + attrs |= iter->p2pdma.mem->dma_mapping_flags; + iter->attrs = attrs; iter->addr =dma_map_phys (dma_dev, vec->paddr, vec->len,rq_dma_dir (req), attrs);
@@ -107,7 +108,7 @@ static bool blk_rq_dma_map_iova(struct request *req, struct device *dma_dev, struct phys_vec *vec) {enum dma_data_direction dir =rq_dma_dir (req); -unsigned int attrs =0 ; +unsigned long attrs =0 ;size_t mapped =0 ;int error;
@@ -115,7 +116,8 @@ static bool blk_rq_dma_map_iova(struct request *req, struct device *dma_dev, iter->len =dma_iova_size (state);if (iter->p2pdma.map ==PCI_P2PDMA_MAP_THRU_HOST_BRIDGE ) - attrs |=DMA_ATTR_MMIO ; + attrs |= iter->p2pdma.mem->dma_mapping_flags; + iter->attrs = attrs; do { error = dma_iova_link(dma_dev, state, vec->paddr, mapped,
@@ -168,6 +170,7 @@ static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev,struct phys_vec vec;memset (&iter->p2pdma,0 ,sizeof (iter->p2pdma)); + iter->attrs =0 ; iter->status =BLK_STS_OK ; iter->p2pdma.map =PCI_P2PDMA_MAP_NONE ;
drivers/dma-buf/dma-buf-mapping.c +9 -6
@@ -59,11 +59,13 @@ static unsigned int calc_sg_nents(struct dma_iova_state *state,* @sgt: Scatter-gather table * @state: DMA IOVA state relevant in IOMMU-based DMA * @size: Total size of DMA transfer +* @attrs: DMA attributes used for host bridge mappings */ struct dma_buf_dma {struct sg_table sgt;struct dma_iova_state *state;size_t size; +unsigned long attrs; };/**
@@ -119,6 +121,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,*/ break ;case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE : + dma->attrs = provider->dma_mapping_flags; dma->state =kzalloc_obj (*dma->state);if (!dma->state) { ret = -ENOMEM ;
@@ -147,7 +150,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach, ret =dma_iova_link (attach->dev, dma->state, phys_vec[i].paddr,0 , phys_vec[i].len, dir, -DMA_ATTR_MMIO ); + dma->attrs);if (ret) goto err_unmap_dma;
@@ -155,7 +158,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach, }else { addr =dma_map_phys (attach->dev, phys_vec[i].paddr, phys_vec[i].len, dir, -DMA_ATTR_MMIO ); + dma->attrs); ret =dma_mapping_error (attach->dev, addr);if (ret) goto err_unmap_dma;
@@ -195,11 +198,11 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach, ;/* Do nothing */ }else if (dma_use_iova (dma->state)) {dma_iova_destroy (attach->dev, dma->state, mapped_len, dir, -DMA_ATTR_MMIO ); + dma->attrs); }else {for_each_sgtable_dma_sg (&dma->sgt, sgl, i)dma_unmap_phys (attach->dev,sg_dma_address (sgl), -sg_dma_len (sgl), dir,DMA_ATTR_MMIO ); +sg_dma_len (sgl), dir, dma->attrs); }sg_free_table (&dma->sgt); err_free_state:
@@ -231,13 +234,13 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt, ;/* Do nothing */ }else if (dma_use_iova (dma->state)) {dma_iova_destroy (attach->dev, dma->state, dma->size, dir, -DMA_ATTR_MMIO ); + dma->attrs); }else {struct scatterlist *sgl;for_each_sgtable_dma_sg (sgt, sgl, i)dma_unmap_phys (attach->dev,sg_dma_address (sgl), -sg_dma_len (sgl), dir,DMA_ATTR_MMIO ); +sg_dma_len (sgl), dir, dma->attrs); }sg_free_table (sgt);
drivers/nvme/host/pci.c +14 -8
@@ -436,6 +436,7 @@ struct nvme_iod {size_t total_len;struct dma_iova_state dma_state; +unsigned long dma_attrs;void *descriptors[NVME_MAX_NR_DESCRIPTORS ];struct nvme_dma_vec *dma_vecs;unsigned int nr_dma_vecs;
@@ -443,6 +444,7 @@ struct nvme_iod {dma_addr_t meta_dma;size_t meta_total_len;struct dma_iova_state meta_dma_state; +unsigned long meta_dma_attrs;struct nvme_sgl_desc *meta_descriptor; };
@@ -859,7 +861,7 @@ static void nvme_free_descriptors(struct request *req) } } -static void nvme_free_prps (struct request *req,unsigned int attrs) +static void nvme_free_prps (struct request *req,unsigned long attrs) {struct nvme_iod *iod =blk_mq_rq_to_pdu (req);struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
@@ -872,7 +874,7 @@ static void nvme_free_prps(struct request *req, unsigned int attrs) }static void nvme_free_sgls (struct request *req,struct nvme_sgl_desc *sge, -struct nvme_sgl_desc *sg_list,unsigned int attrs) +struct nvme_sgl_desc *sg_list,unsigned long attrs) {struct nvme_queue *nvmeq = req->mq_hctx->driver_data;enum dma_data_direction dir =rq_dma_dir (req);
@@ -899,7 +901,7 @@ static void nvme_unmap_metadata(struct request *req)struct nvme_iod *iod =blk_mq_rq_to_pdu (req);struct device *dma_dev = nvmeq->dev->dev;struct nvme_sgl_desc *sge = iod->meta_descriptor; -unsigned int attrs =0 ; +unsigned long attrs =0 ;if (iod->flags &IOD_SINGLE_META_SEGMENT ) { dma_unmap_page(dma_dev, iod->meta_dma,
@@ -912,11 +914,11 @@ static void nvme_unmap_metadata(struct request *req) map =PCI_P2PDMA_MAP_BUS_ADDR ; else if (iod->flags & IOD_META_MMIO) { map =PCI_P2PDMA_MAP_THRU_HOST_BRIDGE ; - attrs |=DMA_ATTR_MMIO ; + attrs = iod->meta_dma_attrs; }if (!blk_rq_dma_unmap (req, dma_dev, &iod->meta_dma_state, - iod->meta_total_len, map)) { + iod->meta_total_len, map, attrs)) {if (nvme_pci_cmd_use_meta_sgl (&iod->cmd))nvme_free_sgls (req, sge, &sge[1 ], attrs);else
@@ -935,7 +937,7 @@ static void nvme_unmap_data(struct request *req)struct nvme_iod *iod =blk_mq_rq_to_pdu (req);struct nvme_queue *nvmeq = req->mq_hctx->driver_data;struct device *dma_dev = nvmeq->dev->dev; -unsigned int attrs =0 ; +unsigned long attrs =0 ;if (iod->flags &IOD_SINGLE_SEGMENT ) { static_assert(offsetof(union nvme_data_ptr, prp1) ==
@@ -949,11 +951,11 @@ static void nvme_unmap_data(struct request *req) map =PCI_P2PDMA_MAP_BUS_ADDR ; else if (iod->flags & IOD_DATA_MMIO) { map =PCI_P2PDMA_MAP_THRU_HOST_BRIDGE ; - attrs |=DMA_ATTR_MMIO ; + attrs = iod->dma_attrs; }if (!blk_rq_dma_unmap (req, dma_dev, &iod->dma_state, iod->total_len, - map)) { + map, attrs)) {if (nvme_pci_cmd_use_sgl (&iod->cmd))nvme_free_sgls (req, &iod->cmd.common.dptr.sgl, iod->descriptors[0 ], attrs);
@@ -1270,6 +1272,7 @@ static blk_status_t nvme_map_data(struct request *req)break ;case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE : iod->flags |=IOD_DATA_MMIO ; + iod->dma_attrs = iter.attrs;break ;case PCI_P2PDMA_MAP_NONE :break ;
@@ -1305,6 +1308,7 @@ static blk_status_t nvme_pci_setup_meta_iter(struct request *req)break ;case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE : iod->flags |=IOD_META_MMIO ; + iod->meta_dma_attrs = iter.attrs;break ;case PCI_P2PDMA_MAP_NONE :break ;
@@ -1405,6 +1409,8 @@ static blk_status_t nvme_prep_rq(struct request *req) iod->nr_descriptors =0 ; iod->total_len =0 ; iod->meta_total_len =0 ; + iod->dma_attrs =0 ; + iod->meta_dma_attrs =0 ; iod->nr_dma_vecs =0 ; ret =nvme_setup_cmd (req->q->queuedata, req);
drivers/pci/p2pdma.c +5 -0
@@ -285,6 +285,11 @@ int pcim_p2pdma_init(struct pci_dev *pdev)continue ; p2p->mem[i].owner = &pdev->dev; + + p2p->mem[i].dma_mapping_flags =DMA_ATTR_MMIO ; +if (force_dma_unencrypted (dev)) + p2p->mem[i].dma_mapping_flags |=DMA_ATTR_CC_SHARED ; + p2p->mem[i].bus_offset =pci_bus_address (pdev, i) -pci_resource_start (pdev, i); }
include/linux/blk-mq-dma.h +3 -6
@@ -16,6 +16,7 @@ struct blk_dma_iter {/* Output address range for this iteration */ dma_addr_t addr;u32 len; +unsigned long attrs;struct pci_p2pdma_map_state p2pdma;/* Status code. Only valid when blk_rq_dma_map_iter_* returned false */
@@ -49,23 +50,19 @@ static inline bool blk_rq_dma_map_coalesce(struct dma_iova_state *state)* @state: DMA IOVA state * @mapped_len: number of bytes to unmap * @map: peer-to-peer mapping type +* @attrs: DMA attributes used for the mapping * * Returns %false if the callers need to manually unmap every DMA segment * mapped using @iter or %true if no work is left to be done. */ static inline bool blk_rq_dma_unmap (struct request *req,struct device *dma_dev,struct dma_iova_state *state,size_t mapped_len, -enum pci_p2pdma_map_type map) +enum pci_p2pdma_map_type map,unsigned long attrs) {if (map ==PCI_P2PDMA_MAP_BUS_ADDR )return true;if (dma_use_iova (state)) { -unsigned int attrs =0 ; - -if (map ==PCI_P2PDMA_MAP_THRU_HOST_BRIDGE ) - attrs |=DMA_ATTR_MMIO ; -dma_iova_destroy (dma_dev, state, mapped_len,rq_dma_dir (req), attrs);return true;
include/linux/hmm-dma.h +2 -0
@@ -14,12 +14,14 @@ struct pci_p2pdma_map_state;* @state: DMA IOVA state * @pfns: array of PFNs * @dma_list: array of DMA addresses +* @dma_attrs: array of DMA attributes * @dma_entry_size: size of each DMA entry in the array */ struct hmm_dma_map {struct dma_iova_state state;unsigned long *pfn_list;dma_addr_t *dma_list; +unsigned long *dma_attrs;size_t dma_entry_size; };
include/linux/pci-p2pdma.h +2 -0
@@ -21,10 +21,12 @@ struct scatterlist;* * A p2pdma provider is a range of MMIO address space available to the CPU. * @owner: Device to which this provider belongs. +* @dma_mapping_flags: DMA attributes to use for host bridge mappings. * @bus_offset: Bus offset for p2p communication. */ struct p2pdma_provider {struct device *owner; +unsigned long dma_mapping_flags;u64 bus_offset; };
mm/hmm.c +14 -5
@@ -720,6 +720,11 @@ int hmm_dma_map_alloc(struct device *dev, struct hmm_dma_map *map,if (!map->pfn_list)return -ENOMEM ; + map->dma_attrs =kvcalloc (nr_entries,sizeof (*map->dma_attrs), +GFP_KERNEL | __GFP_NOWARN); +if (!map->dma_attrs) + goto err_attrs; + use_iova =dma_iova_try_alloc (dev, &map->state,0 , nr_entries *PAGE_SIZE );if (!use_iova &&dma_need_unmap (dev)) {
@@ -731,6 +736,8 @@ int hmm_dma_map_alloc(struct device *dev, struct hmm_dma_map *map,return 0 ; err_dma: +kvfree (map->dma_attrs); +err_attrs:kvfree (map->pfn_list);return -ENOMEM ; }
@@ -749,6 +756,7 @@ void hmm_dma_map_free(struct device *dev, struct hmm_dma_map *map)dma_iova_free (dev, &map->state);kvfree (map->pfn_list);kvfree (map->dma_list); +kvfree (map->dma_attrs); }EXPORT_SYMBOL_GPL (hmm_dma_map_free);
@@ -811,11 +819,12 @@ dma_addr_t hmm_dma_map_pfn(struct device *dev, struct hmm_dma_map *map,case PCI_P2PDMA_MAP_NONE :break ;case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE : - attrs |=DMA_ATTR_MMIO ; + attrs |= p2pdma_state->mem->dma_mapping_flags; pfns[idx] |=HMM_PFN_P2PDMA ;break ;case PCI_P2PDMA_MAP_BUS_ADDR : pfns[idx] |=HMM_PFN_P2PDMA_BUS |HMM_PFN_DMA_MAPPED ; + map->dma_attrs[idx] =0 ;return pci_p2pdma_bus_addr_map (p2pdma_state->mem, paddr);default :return DMA_MAPPING_ERROR ;
@@ -848,10 +857,12 @@ dma_addr_t hmm_dma_map_pfn(struct device *dev, struct hmm_dma_map *map,if (dma_need_unmap (dev)) dma_addrs[idx] = dma_addr; } + map->dma_attrs[idx] = attrs; pfns[idx] |=HMM_PFN_DMA_MAPPED ;return dma_addr; error: pfns[idx] &= ~HMM_PFN_P2PDMA ; + map->dma_attrs[idx] =0 ;return DMA_MAPPING_ERROR ; }
@@ -871,14 +882,11 @@ bool hmm_dma_unmap_pfn(struct device *dev, struct hmm_dma_map *map, size_t idx)struct dma_iova_state *state = &map->state;dma_addr_t *dma_addrs = map->dma_list;unsigned long *pfns = map->pfn_list; -unsigned long attrs =DMA_ATTR_REQUIRE_COHERENT ; +unsigned long attrs =DMA_ATTR_REQUIRE_COHERENT | map->dma_attrs[idx];if ((pfns[idx] & valid_dma) != valid_dma)return false; -if (pfns[idx] &HMM_PFN_P2PDMA ) - attrs |=DMA_ATTR_MMIO ; -if (pfns[idx] &HMM_PFN_P2PDMA_BUS ) ;/* no need to unmap bus address P2P mappings */ else if (dma_use_iova (state))
@@ -890,6 +898,7 @@ bool hmm_dma_unmap_pfn(struct device *dev, struct hmm_dma_map *map, size_t idx) pfns[idx] &= ~(HMM_PFN_DMA_MAPPED |HMM_PFN_P2PDMA |HMM_PFN_P2PDMA_BUS ); + map->dma_attrs[idx] =0 ;return true; }EXPORT_SYMBOL_GPL (hmm_dma_unmap_pfn);
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Catalin MarinasQuoted 48 lines · Catalin Marinas
arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 344
Quoted 13 lines · Aneesh Kumar K.V (Arm)
arch/arm64/mm/init.c +1
-3@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }For this part:
Reviewed-by Catalin Marinas <[email protected]>Quoted 29 lines
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }
Quoted 17 lines
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 9 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.
Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
/*
* For host memory encryption and device requiring unencrypted DMA,
* MMIO memory is treated as shared by default.
*/
if (attrs & DMA_ATTR_MMIO) {
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev))
attrs |= DMA_ATTR_CC_SHARED;
}Quoted 6 lines
In the other case, for an arm CCA guest, if the MMIO is shared we end up setting the shared attribute but that's fine, it's only an IPA address.
Signature · 1 line
Catalin
-aneesh
Catalin Marinas
maintainerRe: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 71 lines · Aneesh Kumar K.V
Quoted 48 lines · Catalin Marinas
arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 344
Quoted 13 lines · Aneesh Kumar K.V (Arm)
arch/arm64/mm/init.c +1
-3@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }For this part:
Reviewed-by Catalin Marinas <[email protected]>Quoted 29 lines
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }Quoted 17 lines
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 9 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
Quoted 8 lines
/*
* For host memory encryption and device requiring unencrypted DMA,
* MMIO memory is treated as shared by default.
*/
if (attrs & DMA_ATTR_MMIO) {
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev))
attrs |= DMA_ATTR_CC_SHARED;
}Yes, I think it does the trick, preserves the current semantics for AMD. I guess you could use a single 'if' for all checks (up to you).
Signature · 1 line
Catalin
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 20 lines · Catalin Marinas
Quoted 11 lines
Quoted 7 lines
For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
Quoted 8 lines
/* * For host memory encryption and device requiring unencrypted DMA, * MMIO memory is treated as shared by default. */ if (attrs & DMA_ATTR_MMIO) { if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev)) attrs |= DMA_ATTR_CC_SHARED; }
Quoted 2 lines
Yes, I think it does the trick, preserves the current semantics for AMD. I guess you could use a single 'if' for all checks (up to you).
Please don't change it, MMIO P2P is broken on CC systems today and it should stay broken. Passing DMA_ATTR_MMIO with DMA_ATTR_CC_SHARED is an error that we need to correct in the drivers not make work in the core code.
Jason
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 21 lines · Jason Gunthorpe
Quoted 20 lines · Catalin Marinas
Quoted 19 lines
Quoted 7 lines
For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
/* * For host memory encryption and device requiring unencrypted DMA, * MMIO memory is treated as shared by default. */ if (attrs & DMA_ATTR_MMIO) { if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev)) attrs |= DMA_ATTR_CC_SHARED; }
Quoted 8 lines
Quoted 2 lines
Yes, I think it does the trick, preserves the current semantics for AMD. I guess you could use a single 'if' for all checks (up to you).
Please don't change it, MMIO P2P is broken on CC systems today and it should stay broken. Passing
DMA_ATTR_MMIOwithDMA_ATTR_CC_SHAREDis an error that we need to correct in the drivers not make work in the core code.
But the above changes are intended to handle HOST_MEM_ENCRYPT. In v7, we had the following diff:
@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev,
{
dma_addr_t dma_addr;
+ /*
+ * For a device requiring unencrypted DMA, MMIO memory is treated
+ * as shared by default.
+ */
+ if (force_dma_unencrypted(dev) && (attrs & DMA_ATTR_MMIO))
+ attrs |= DMA_ATTR_CC_SHARED;
+
This is now getting updated to
@@ -624,37 +626,44 @@ dma_addr_t dma_direct_map_phys(struct device *dev, phys_addr_t phys,
{
dma_addr_t dma_addr;
+ if (attrs & DMA_ATTR_MMIO) {
+ /*
+ * For host memory encryption and device requiring
+ * unencrypted DMA, MMIO memory is treated as shared by
+ * default.
+ */
+ if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
+ force_dma_unencrypted(dev))
+ attrs |= DMA_ATTR_CC_SHARED;
+ }
+
I agree that we need to move that DMA_ATTR_CC_SHARED setting to
modified drivers/pci/p2pdma.c
@@ -285,6 +285,11 @@ int pcim_p2pdma_init(struct pci_dev *pdev)
continue;
p2p->mem[i].owner = &pdev->dev;
+
+ p2p->mem[i].dma_mapping_flags = DMA_ATTR_MMIO;
+ if (force_dma_unencrypted(dev))
+ p2p->mem[i].dma_mapping_flags |= DMA_ATTR_CC_SHARED;
+
As we discussed [1], that can come in a later patch. In the meantime, adding the HOST_MEM_ENCRYPT check preserves the previous behavior for SME.
[1] https://lore.kernel.org/all/[email protected]/
-aneesh
Will Deacon
maintainerQuoted 42 lines · Marek Szyprowski
Quoted 41 lines · Aneesh Kumar K.V
Quoted 39 lines · Aneesh Kumar K.V (Arm)
This series tracks confidential-computing shared DMA state through the dma-direct, dma-pool, and swiotlb paths so that encrypted and decrypted DMA buffers are handled consistently.
Today, the direct DMA path mostly relies on
force_dma_unencrypted()for shared/decrypted buffer handling. This series consolidates theforce_dma_unencrypted()checks in the top-level functions and ensures that the remaining DMA interfaces use DMA attributes to make the correct decisions.The series separates mapping and allocation state:
-DMA_ATTR_CC_SHAREDdescribes the DMA address attribute requested for a mapping. It tells the DMA mapping path that the DMA address must target shared/decrypted memory.
-__DMA_ATTR_ALLOC_CC_SHAREDis an internal DMA-mapping attribute used only by allocation paths after the DMA core decides that the backing pages must be allocated as shared/decrypted memory.The series:
- moves swiotlb-backed allocations out of__dma_direct_alloc_pages(),
- uses__DMA_ATTR_ALLOC_CC_SHAREDthrough the dma-direct alloc/free paths
- teaches the atomic DMA pools to track encrypted versus decrypted state
- tracks swiotlb pool encryption state and enforces strict pool selection
- centralizes encrypted/decrypted pgprot handling indma_pgprot()using DMA attributes
- passes DMA attributes down todma_capable()so capability checks can validate whether the selected DMA address encoding matchesDMA_ATTR_CC_SHARED
- makesdma_direct_map_phys()choose the DMA address encoding fromDMA_ATTR_CC_SHAREDand fall back to swiotlb when a shared DMA request cannot use the direct mapping, which lets arm64 and x86 CCA guests stop relying onSWIOTLB_FORCEfor DMA mappings
- use the selected swiotlb pool state to derive the returned DMA address
- reportsCC_ATTR_GUEST_MEM_ENCRYPTfor arm64 Realms, powerpc secure guests, and s390 protected virtualization guests.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Dependency:
This series depends on the pKVM changes posted at:
https://lore.kernel.org/all/[email protected]
FYI, I'll probably take the second patch there as a fix, then I can put the other two on a topic branch in the arm64 tree.
Will
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.VQuoted 12 lines · Aneesh Kumar K.V
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
/* * For host memory encryption and device requiring unencrypted DMA, * MMIO memory is treated as shared by default. */ if (attrs & DMA_ATTR_MMIO) { if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev)) attrs |= DMA_ATTR_CC_SHARED; }Yes, I think it does the trick, preserves the current semantics for AMD. I guess you could use a single 'if' for all checks (up to you).
Quoted 8 lines
Quoted 5 lines
Please don't change it, MMIO P2P is broken on CC systems today and it should stay broken. Passing
DMA_ATTR_MMIOwithDMA_ATTR_CC_SHAREDis an error that we need to correct in the drivers not make work in the core code.But the above changes are intended to handle
HOST_MEM_ENCRYPT. In v7, we had the following diff:
To follow how the rest of the decrypted/encrypted stuff works the MMIO has to be flaged with CC_SHARED for HOST_MEM_ENCRYPT too, just like the PTEs.
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 96
Quoted 10 lines
kernel/dma/direct.h +6
-0@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ;
force_dma_unencrypted() says nothing about the properties of the address passed in, this was nonsense :\
Quoted 2 lines
As we discussed [1], that can come in a later patch. In the meantime, adding the
HOST_MEM_ENCRYPTcheck preserves the previous behavior for SME.
It never worked. When we added ATTR_MMIO it started to have a chance to work but prior to that it was always broken anyhow. I don't see there is much merit in preserving the narrow window when we inadvertantly had a half working ATTR_MMIO.
But if you really want to it should be cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) *only* and get rid of the wrong force_dma_unencrypted().
But IMHO, I'd rather this series treat ATTR_MMIO as private MMIO and ATTR_MMIO|CC_SHARED as shared MMIO and that's the right and correct thing for the DMA API.
Jason
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 43 lines · Jason Gunthorpe
Quoted 12 lines · Aneesh Kumar K.V
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
/* * For host memory encryption and device requiring unencrypted DMA, * MMIO memory is treated as shared by default. */ if (attrs & DMA_ATTR_MMIO) { if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev)) attrs |= DMA_ATTR_CC_SHARED; }Yes, I think it does the trick, preserves the current semantics for AMD. I guess you could use a single 'if' for all checks (up to you).
Quoted 8 lines
Quoted 5 lines
Please don't change it, MMIO P2P is broken on CC systems today and it should stay broken. Passing
DMA_ATTR_MMIOwithDMA_ATTR_CC_SHAREDis an error that we need to correct in the drivers not make work in the core code.But the above changes are intended to handle
HOST_MEM_ENCRYPT. In v7, we had the following diff:To follow how the rest of the decrypted/encrypted stuff works the MMIO has to be flaged with
CC_SHAREDforHOST_MEM_ENCRYPTtoo, just like the PTEs.Review static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 96
Quoted 10 lines
(quoted diff) +6
-0@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, { dma_addr_t dma_addr; + /* + * For a device requiring unencrypted DMA, MMIO memory is treated + * as shared by default. + */ + if (force_dma_unencrypted(dev) && (attrs & DMA_ATTR_MMIO)) + attrs |= DMA_ATTR_CC_SHARED;
force_dma_unencrypted()says nothing about the properties of the address passed in, this was nonsense :\Quoted 2 lines
As we discussed [1], that can come in a later patch. In the meantime, adding the
HOST_MEM_ENCRYPTcheck preserves the previous behavior for SME.
Quoted 9 lines
It never worked. When we added
ATTR_MMIOit started to have a chance to work but prior to that it was always broken anyhow. I don't see there is much merit in preserving the narrow window when we inadvertantly had a half workingATTR_MMIO.But if you really want to it should be
cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)*only* and get rid of the wrongforce_dma_unencrypted().
Ok, I will update the changes to so we preserve the previous behaviour for SME?
if (attrs & DMA_ATTR_MMIO) {
/*
* For host memory encryption treat MMIO memory as shared
*/
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
attrs |= DMA_ATTR_CC_SHARED;
}Do let me know if you feel strongly that the above change should not be included and that the SME P2P case should instead be handled in a future patch.
Quoted 5 lines
But IMHO, I'd rather this series treat
ATTR_MMIOas private MMIO and ATTR_MMIO|CC_SHARED as shared MMIO and that's the right and correct thing for the DMA API.
If you think the rest of the series is ready for upstream, could you please ack it so it can be picked up for the next merge window? I'll repost v8, rebased on top of the pKVM topic branch.
-aneesh
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 8 lines · Aneesh Kumar K.V
Quoted 4 lines
But if you really want to it should be
cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)*only* and get rid of the wrongforce_dma_unencrypted().Ok, I will update the changes to so we preserve the previous behaviour for SME?
Quoted 7 lines
if (attrs & DMA_ATTR_MMIO) {
/*
* For host memory encryption treat MMIO memory as shared
*/
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
attrs |= DMA_ATTR_CC_SHARED;
}I'm OK with this, with the expectation we should aim to remove it when fixing the callers.
Quoted 8 lines
Quoted 4 lines
But IMHO, I'd rather this series treat
ATTR_MMIOas private MMIO and ATTR_MMIO|CC_SHARED as shared MMIO and that's the right and correct thing for the DMA API.If you think the rest of the series is ready for upstream, could you please ack it so it can be picked up for the next merge window? I'll repost v8, rebased on top of the pKVM topic branch.
Yeah
Jason
Re: [PATCH v7 02/22] dma-pool: fix page leak in atomic_pool_expand() cleanup
In reply to Aneesh Kumar K.V (Arm)kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 122
Quoted 9 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +3
-1@@ -115,8 +116,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,*/ ret =set_memory_decrypted ((unsigned long )page_to_virt (page),1 << order); -if (ret) +if (ret) { + leak_pages = true; goto remove_mapping; + }
Truely these _set_memory_decrypted() things are an insane API. So a if it fails to decrypt it can be in any messy state?
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 136
Quoted 6 lines
kernel/dma/pool.c +1
-1@@ -130,14 +133,15 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, 1 << order);if (WARN_ON_ONCE (ret)) {/* Decrypt succeeded but encrypt failed, purposely leak */ - goto out; + leak_pages = true;
At least this one makes some sense..
Jason
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Aneesh Kumar K.V (Arm)Quoted 33 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +11
-8-static int atomic_pool_expand (struct gen_pool *pool,size_t pool_size, +static int atomic_pool_expand (struct dma_gen_pool *dma_pool,size_t pool_size,gfp_t gfp) {unsigned int order;@@ -114,14 +120,17 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,* Memory in the atomic DMA pools must be unencrypted, the pools do not * shrink so no re-encryption occurs in dma_direct_free(). */ - ret =set_memory_decrypted ((unsigned long )page_to_virt (page), -1 << order); -if (ret) { - leak_pages = true; - goto remove_mapping; +if (dma_pool->cc_shared) { + ret =set_memory_decrypted ((unsigned long )page_to_virt (page), +1 << order); +if (ret) { + leak_pages = true; + goto remove_mapping; + } } - ret =gen_pool_add_virt (pool, (unsigned long )addr,page_to_phys (page), - pool_size,NUMA_NO_NODE ); + + ret =gen_pool_add_virt (dma_pool->pool, (unsigned long )addr, +page_to_phys (page), pool_size,NUMA_NO_NODE );if (ret) goto encrypt_mapping;@@ -129,12 +138,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,return 0 ;
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 144
Quoted 10 lines
kernel/dma/pool.c +3
-5encrypt_mapping: - ret =set_memory_encrypted ((unsigned long )page_to_virt (page), -1 << order); -if (WARN_ON_ONCE (ret)) { -/* Decrypt succeeded but encrypt failed, purposely leak */ +if (dma_pool->cc_shared && +set_memory_encrypted ((unsigned long )page_to_virt (page),1 << order)) leak_pages = true; - } +
Was it intentional to remove the WARN_ON and comment ?
Jason
Re: [PATCH v7 03/22] iommu/dma: Check atomic pool allocation result directly
In reply to Aneesh Kumar K.V (Arm)Quoted 6 lines · Aneesh Kumar K.V (Arm)
The non-blocking, non-coherent allocation path uses
dma_alloc_from_pool(), which returns the allocated page and fillscpu_addronly on success.Do not rely on
cpu_addrto detect allocation failure in this path. Check the returned page directly before using it for the IOMMU mapping.
Quoted 8 lines
Fixes 9420139f516d ("dma-pool: fix coherent pool allocations for IOMMU mappings")Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/dma-iommu.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
Jason
Re: [PATCH v7 04/22] dma: free atomic pool pages by physical address
In reply to Aneesh Kumar K.V (Arm)Quoted 17 lines · Aneesh Kumar K.V (Arm)
dma_direct_alloc_pages()may satisfy atomic allocations from the coherent atomic pools. The pool allocation is keyed by the virtual address stored in thegen_pool, but the pages API returns only the backing struct page.On architectures with
CONFIG_DMA_DIRECT_REMAP, atomic pool chunks are added to thegen_poolusing their remapped virtual address.dma_direct_free_pages()reconstructs a linear-map address withpage_address(page)and passes that todma_free_from_pool(). That address does not match thegen_poolvirtual range, so the pool lookup can fail and the code can fall through to freeing a pool-owned page through the normal page allocator path.Add a page-based pool free helper that looks up the owning pool chunk by physical address, translates it back to the
gen_poolvirtual address, and frees that address to the pool. Use it fromdma_direct_free_pages()while keeping the existing virtual-address helper for coherent allocation frees.
Quoted 8 lines
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- include/linux/dma-map-ops.h | 1 + kernel/dma/direct.c | 4 +-- kernel/dma/pool.c | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)
This seems pretty suboptimal?
If !CONFIG_DMA_DIRECT_REMAP then page_to_virt() was used to compute the genpool's addr so dma_free_from_pool_page() can use the same logic, which is how things must be working at all today
The CONFIG_DMA_DIRECT_REMAP scenario does look broken, so I'm surprised there isn't a Fixes line on this commit? I don't have an opinion on the search, but since alloc_pages() is used there is 8 bytes in the struct page that could be used to store the remapped vaddr to avoid the search if someday someone wants to improve this. Maybe a small comment hinting that direction would be a nice addition.
Jason
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Aneesh Kumar K.V (Arm)Quoted 7 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +0
-3@@ -114,14 +120,17 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,* Memory in the atomic DMA pools must be unencrypted, the pools do not * shrink so no re-encryption occurs in dma_direct_free(). */ - ret =set_memory_decrypted ((unsigned long )page_to_virt (page), -1 << order); -if (ret) {
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 130
Quoted 10 lines
kernel/dma/pool.c +7
-2- leak_pages = true; - goto remove_mapping; +if (dma_pool->cc_shared) { + ret =set_memory_decrypted ((unsigned long )page_to_virt (page), +1 << order); +if (ret) { + leak_pages = true; + goto remove_mapping; + } }
This makes the memory_decrypted conditional, but it doesn't change the lines a few above:
addr = dma_common_contiguous_remap(page, pool_size,
pgprot_decrypted(pgprot_dmacoherent(PAGE_KERNEL)),
^^^^^^^^^^^^
__builtin_return_address(0));
if (!addr)
goto free_page;It is wrong to pass pgprot_decrypted() to the arch code if set_memory_decrypted() was not called.
Also it looks at some point the nature of the atomic pool has become confused. Originally it was just to allocate atomic memory that had been vmap'd outside the atomic context (to set the non-coherent pgprot), so every caller was expecting non-cached memory.
Then it was reused to also allocate CC shared memory outside the atomic context. That was fine for x86 that doesn't use DMA_REMAP but on ARM64 it now means all atomic pool CC memory is uncached? That doesn't seem to make any sense...
I suppose along the lines of this patch the solution is to add a noncoherent property to the pool so we can select the correct combination:
noncoherent !SHARED = vmap pgprot_noncached
!noncoherent SHARED= vmap pgprot_decrypted + set_memory_decrypted
noncoherent SHARED = (probably unrealistic in real systems)
!noncoherent !SHARED = normal __dma_direct_alloc_pages()
But I don't view this as that important, the CC hypervisor is probably going to use the S2 page table to force cachable on all system memory so the non-cached pgprot is a NOP, but the extra vmap is wasteful and it is confusing.. So maybe a little fixme is all that is needed here.
Jason
Re: [PATCH v7 05/22] swiotlb: Preserve allocation virtual address for dynamic pools
In reply to Aneesh Kumar K.V (Arm)Quoted 15 lines · Aneesh Kumar K.V (Arm)
swiotlb_alloc_tlb()can allocate from the DMA atomic pool when a decrypted pool is needed from atomic context. WithCONFIG_DMA_DIRECT_REMAP, the atomic pool is backed by remapped virtual addresses, which are not the same as the direct-map addresses returned byphys_to_virt().
swiotlb_init_io_tlb_pool()currently reconstructs the pool virtual address from the physical start address. For atomic-pool backed allocations this stores the wrong address inpool->vaddr. Later,swiotlb_free_tlb()passes that address todma_free_from_pool(), which will fail to recognize the chunkPass the virtual address returned by the allocation path into
swiotlb_init_io_tlb_pool(), and store that address inpool->vaddr. This keeps the pool free path using the same virtual address as the allocator.
Quoted 8 lines
Fixes 79636caad361 ("swiotlb: if swiotlb is full, fall back to a transient memory pool")Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/swiotlb.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
Jason
Re: [PATCH v7 07/22] dma-direct: swiotlb: handle swiotlb alloc/free outside __dma_direct_alloc_pages
In reply to Aneesh Kumar K.V (Arm)Quoted 25 lines · Aneesh Kumar K.V (Arm)
Move swiotlb allocation out of
__dma_direct_alloc_pages()and handle it indma_direct_alloc()/dma_direct_alloc_pages().This is needed for follow-up changes that simplify the handling of memory encryption/decryption based on the DMA attribute flags.
swiotlb backing pages are already mapped decrypted by
swiotlb_update_mem_attributes()andrmem_swiotlb_device_init(), so dma-direct should not calldma_set_decrypted()on allocation nordma_set_encrypted()on free for swiotlb-backed memory.Update alloc/free paths to detect swiotlb-backed pages and skip encrypt/decrypt transitions for those paths. Keep the existing highmem rejection in
dma_direct_alloc_pages()for swiotlb allocations.Only for "restricted-dma-pool", we currently set
for_alloc = true, whilermem_swiotlb_device_init()decrypts the whole pool up front. This pool is typically used together with "shared-dma-pool", where the shared region is accessed after remap/ioremap and the returned address is suitable for decrypted memory access. So existing code paths remain valid.Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
Quoted 5 lines
--- include/linux/swiotlb.h | 6 ++++ kernel/dma/direct.c | 71 ++++++++++++++++++++++++++++++----------- kernel/dma/swiotlb.c | 6 ++++ 3 files changed, 65 insertions(+), 18 deletions(-)
Jason
Re: [PATCH v7 09/22] dma-mapping: Add internal shared allocation attribute
In reply to Aneesh Kumar K.V (Arm)Quoted 12 lines · Aneesh Kumar K.V (Arm)
DMA_ATTR_CC_SHAREDdescribes an existing DMA mapping whose backing memory is already shared, or decrypted, for confidential computing. It is a mapping attribute: callers use it to request a shared DMA address encoding for memory that has already been prepared for shared DMA.Allocation paths need a related but different state. Once the DMA core decides that an allocation must use shared backing pages, the lower-level allocation helpers need to select shared pools, decrypt newly allocated pages, derive the DMA address with the shared-memory translation and restore encryption on free. That state is internal to the DMA-mapping implementation and should not be passed by drivers to
dma_alloc_attrs().
Quoted 6 lines
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- Documentation/core-api/dma-attributes.rst | 29 +++++++++++++++++++++++ include/linux/dma-mapping.h | 8 +++++++ include/trace/events/dma.h | 3 ++- 3 files changed, 39 insertions(+), 1 deletion(-)
Jason
Re: [PATCH v7 10/22] dma-direct: use __DMA_ATTR_ALLOC_CC_SHARED in alloc/free paths
In reply to Aneesh Kumar K.V (Arm)Quoted 22 lines · Aneesh Kumar K.V (Arm)
Propagate
force_dma_unencrypted()into__DMA_ATTR_ALLOC_CC_SHAREDin the dma-direct allocation path and use the attribute to drive the related decisions.This updates
dma_direct_alloc(),dma_direct_free(), anddma_direct_alloc_pages()to fold the forced unencrypted case into attrs.Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/direct.c | 42 +++++++++++++++++++++++++++++++++--------- kernel/dma/mapping.c | 9 +++++++++ 2 files changed, 42 insertions(+), 9 deletions(-)kernel/dma/direct.c +0
-0
kernel/dma/direct.c void *dma_direct_alloc(struct device *dev, size_t size, · line 200
Quoted 10 lines
kernel/dma/direct.c +3
-1@@ -192,16 +192,22 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) { bool remap = false, set_uncached = false; -bool mark_mem_decrypt = true; +bool mark_mem_decrypt = false;struct page *page;void *ret; +if (force_dma_unencrypted (dev)) + attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
After going through what was left of force_dma_unencrypted() once the series was fully applied you might consider something like
dma_update_attrs(dev, &attrs);
To remove the frequently duplicated code
It is fine like this too
Jason
Re: [PATCH v7 12/22] dma: swiotlb: pass mapping attributes by reference
In reply to Aneesh Kumar K.V (Arm)Quoted 13 lines · Aneesh Kumar K.V (Arm)
Change
swiotlb_tbl_map_single()to take the DMA mapping attributes by reference and update the direct callers accordingly.This is a preparatory change for a follow-up patch which updates the attributes based on the selected swiotlb pool. Keeping the signature change separate makes the follow-up patch easier to review.
No functional change in this patch.
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
Quoted 6 lines
--- drivers/iommu/dma-iommu.c | 2 +- drivers/xen/swiotlb-xen.c | 2 +- include/linux/swiotlb.h | 2 +- kernel/dma/swiotlb.c | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-)
Jason
Re: [PATCH v7 13/22] dma: swiotlb: track pool encryption state and honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Quoted 17 lines · Aneesh Kumar K.V (Arm)
Teach swiotlb to distinguish between encrypted and decrypted bounce buffer pools, and make allocation and mapping paths select a pool whose state matches the requested DMA attributes.
Add a
cc_sharedflag toio_tlb_mem, initialize it for the default and restricted pools, and propagate__DMA_ATTR_ALLOC_CC_SHAREDinto swiotlb pool allocation. Reject swiotlb alloc/map requests when the selected pool does not match the required encrypted/decrypted state.Also return DMA addresses with the matching phys_to_dma_{encrypted, unencrypted} helper so the DMA address encoding stays consistent with the chosen pool.
Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
Quoted 6 lines
--- include/linux/dma-direct.h | 10 +++ include/linux/swiotlb.h | 10 ++- kernel/dma/direct.c | 13 ++- kernel/dma/swiotlb.c | 177 ++++++++++++++++++++++++++++--------- 4 files changed, 162 insertions(+), 48 deletions(-)
Jason
Re: [PATCH v7 14/22] dma-mapping: make dma_pgprot() honor __DMA_ATTR_ALLOC_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Quoted 8 lines · Aneesh Kumar K.V (Arm)
Fold encrypted/decrypted pgprot selection into
dma_pgprot()so callers do not need to adjust the page protection separately.Update
dma_pgprot()to applypgprot_decrypted()whenDMA_ATTR_CC_SHAREDor__DMA_ATTR_ALLOC_CC_SHAREDis set andpgprot_encrypted()otherwise Convert the dma-direct mmap paths to passDMA_ATTR_CC_SHAREDinstead of open-codingforce_dma_unencrypted()handling arounddma_pgprot().
Quoted 8 lines
Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/direct.c | 8 +++----- kernel/dma/mapping.c | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-)
Jason
Re: [PATCH v7 15/22] dma-direct: pass attrs to dma_capable() for DMA_ATTR_CC_SHARED checks
In reply to Aneesh Kumar K.V (Arm)Quoted 13 lines · Aneesh Kumar K.V (Arm)
Teach
dma_capable()aboutDMA_ATTR_CC_SHAREDso the capability check can reject encrypted DMA addresses for devices that require unencrypted/shared DMA.Also propagate
DMA_ATTR_CC_SHAREDinswiotlb_map()when the selected SWIOTLB pool is decrypted so the capability check sees the correct DMA address attribute.Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
Quoted 7 lines
--- arch/x86/kernel/amd_gart_64.c | 30 ++++++++++++++++-------------- drivers/xen/swiotlb-xen.c | 6 +++--- include/linux/dma-direct.h | 10 +++++++++- kernel/dma/direct.h | 6 +++--- kernel/dma/swiotlb.c | 2 +- 5 files changed, 32 insertions(+), 22 deletions(-)
Jason
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)Quoted 24 lines · Aneesh Kumar K.V (Arm)
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 10 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);
Maybe things should be adjusted so this can be
phys_to_dma_direct(dev, phys, (attrs & DMA_ATTR_CC_SHARED)
?
It would also be nice if swiotlb.c could do the same
Stated another way, maybe we should get rid of phys_to_dma_unencrypted/encrypted() entirely and just have the bool variation since that seems to be what everyone wants.
Jason
Re: [PATCH v7 17/22] dma-direct: set decrypted flag for remapped DMA allocations
In reply to Aneesh Kumar K.V (Arm)Quoted 11 lines · Aneesh Kumar K.V (Arm)
Devices that are DMA non-coherent and require a remap were skipping
dma_set_decrypted(), leaving DMA buffers encrypted even when the device requires unencrypted access. Move the call after the if (remap) branch so that both the direct and remapped allocation paths correctly mark the allocation as decrypted (or fail cleanly) before use.Fix
dma_direct_alloc()anddma_direct_free()to apply set_memory_*() to the linear-map alias of the backing pages instead of the remapped CPU address. Also disallow highmem pages for__DMA_ATTR_ALLOC_CC_SHARED, because highmem buffers do not provide a usable linear-map address.
Quoted 7 lines
Tested-by Jiri Pirko <[email protected]>Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/direct.c | 56 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-)
Jason
Re: [PATCH v7 18/22] dma-direct: select DMA address encoding from __DMA_ATTR_ALLOC_CC_SHARED
In reply to Aneesh Kumar K.V (Arm)kernel/dma/direct.c static struct page *dma_direct_alloc_from_pool(struct device *dev, size_t size, · line 176
Quoted 3 lines
kernel/dma/direct.c +2
-1- *dma_handle =phys_to_dma_direct (dev,page_to_phys (page)); + *dma_handle =phys_to_dma_direct (dev,page_to_phys (page), + !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED));
The !! isn't needed when going through bool, the compiler does it automatically.
Jason
Re: [PATCH v7 19/22] dma-direct: rename ret to cpu_addr in alloc helpers
In reply to Aneesh Kumar K.V (Arm)Quoted 7 lines · Aneesh Kumar K.V (Arm)
ret in
dma_direct_alloc()anddma_direct_alloc_pages()holds the returned CPU mapping, not a generic return value. Rename it tocpu_addrand update the remaining uses to match.This makes the allocation paths easier to follow and keeps the local naming consistent with what the variable actually represents.
Quoted 7 lines
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/direct.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
Jason
Re: [PATCH v7 20/22] dma: swiotlb: free dynamic pools from process context
In reply to Aneesh Kumar K.V (Arm)kernel/dma/swiotlb.c static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool) · line 846
Quoted 6 lines · Aneesh Kumar K.V (Arm)
kernel/dma/swiotlb.c +1
-1@@ -840,7 +843,7 @@ static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool)list_del_rcu (&pool->node);spin_unlock_irqrestore (&dev->dma_io_tlb_lock, flags); -call_rcu (&pool->rcu, swiotlb_dyn_free); +swiotlb_schedule_dyn_free (pool);
This knows if the pool has cc_shared memory in it since there is now a flag tracking that? So should the work queue version be conditional on that flag?
Jason
Re: [PATCH v7 21/22] dma: swiotlb: handle set_memory_decrypted() failures
In reply to Aneesh Kumar K.V (Arm)Quoted 13 lines · Aneesh Kumar K.V (Arm)
Check the return value when converting swiotlb pools between encrypted and decrypted mappings. If the default pool cannot be decrypted after early initialization, mark the pool fully used so it cannot satisfy future bounce allocations.
For late initialization, return the
set_memory_decrypted()failure. For restricted DMA pools, fail device initialization if the reserved pool cannot be decrypted.This prevents swiotlb from using pools whose encryption attributes do not match their metadata, and avoids returning pages with uncertain encryption state back to the allocator.
Quoted 7 lines
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Reviewed-by Petr Tesarik <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- kernel/dma/swiotlb.c | 80 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 15 deletions(-)
Jason
Re: [PATCH v7 22/22] swiotlb: remove unused SWIOTLB_FORCE flag
In reply to Aneesh Kumar K.V (Arm)Quoted 11 lines · Aneesh Kumar K.V (Arm)
SWIOTLB_FORCEhas no remaining in-tree users. Forced bouncing is now controlled through the swiotlb=force command line option viaswiotlb_force_bounce.Remove the unused flag and simplify the
force_bounceinitialization.Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- include/linux/swiotlb.h | 3 +-- kernel/dma/swiotlb.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
Jason
Quoted 4 lines · Marek Szyprowski
I'm fine with merging on top of the topic branch and I assume that this patchset is mature enough to give it a try in linux-next, but first I would like to get a review or at least acks from others with good CC knowledge or experience.
I think it is good to go from a CC perspective, there are still some more items to fix up (like the MMIO) but I'd rather they be followups at this point.
Thanks,
Jason
Re: [PATCH v7 02/22] dma-pool: fix page leak in atomic_pool_expand() cleanup
In reply to Jason GunthorpeQuoted 14 lines · Jason Gunthorpe
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 122
Quoted 9 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +3
-1@@ -115,8 +116,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,*/ ret =set_memory_decrypted ((unsigned long )page_to_virt (page),1 << order); -if (ret) +if (ret) { + leak_pages = true; goto remove_mapping; + }Truely these
_set_memory_decrypted()things are an insane API. So a if it fails to decrypt it can be in any messy state?
Yes, we could possibly try to encrypt the page again and, if that succeeds, avoid leaking it. We might want to do that tree-wide in a separate patch.
Quoted 10 lines
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 136
Quoted 7 lines
kernel/dma/pool.c +1
-1@@ -130,14 +133,15 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, 1 << order);if (WARN_ON_ONCE (ret)) {/* Decrypt succeeded but encrypt failed, purposely leak */ - goto out; + leak_pages = true;At least this one makes some sense..
Quoted 3 lines
Reviewed-by Jason Gunthorpe <[email protected]>Jason
-aneesh
Re: [PATCH v7 04/22] dma: free atomic pool pages by physical address
In reply to Jason GunthorpeQuoted 41 lines · Jason Gunthorpe
Quoted 17 lines · Aneesh Kumar K.V (Arm)
dma_direct_alloc_pages()may satisfy atomic allocations from the coherent atomic pools. The pool allocation is keyed by the virtual address stored in thegen_pool, but the pages API returns only the backing struct page.On architectures with
CONFIG_DMA_DIRECT_REMAP, atomic pool chunks are added to thegen_poolusing their remapped virtual address.dma_direct_free_pages()reconstructs a linear-map address withpage_address(page)and passes that todma_free_from_pool(). That address does not match thegen_poolvirtual range, so the pool lookup can fail and the code can fall through to freeing a pool-owned page through the normal page allocator path.Add a page-based pool free helper that looks up the owning pool chunk by physical address, translates it back to the
gen_poolvirtual address, and frees that address to the pool. Use it fromdma_direct_free_pages()while keeping the existing virtual-address helper for coherent allocation frees.Quoted 8 lines
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- include/linux/dma-map-ops.h | 1 + kernel/dma/direct.c | 4 +-- kernel/dma/pool.c | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)This seems pretty suboptimal?
If !CONFIG_DMA_DIRECT_REMAP then
page_to_virt()was used to compute the genpool's addr sodma_free_from_pool_page()can use the same logic, which is how things must be working at all todayThe
CONFIG_DMA_DIRECT_REMAPscenario does look broken, so I'm surprised there isn't a Fixes line on this commit? I don't have an opinion on the search, but sincealloc_pages()is used there is 8 bytes in the struct page that could be used to store the remapped vaddr to avoid the search if someday someone wants to improve this. Maybe a small comment hinting that direction would be a nice addition.
Quoted 2 lines
Jason
Something like
+/*
+ * FIXME!! We could avoid this by storing the remapped virtual address in
+ * struct page and using that for lookup.
+ */
bool dma_free_from_pool_page(struct device *dev, struct page *page, size_t size)
-aneesh
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Jason GunthorpeQuoted 34 lines · Jason Gunthorpe
Quoted 33 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +11
-8-static int atomic_pool_expand (struct gen_pool *pool,size_t pool_size, +static int atomic_pool_expand (struct dma_gen_pool *dma_pool,size_t pool_size,gfp_t gfp) {unsigned int order;@@ -114,14 +120,17 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,* Memory in the atomic DMA pools must be unencrypted, the pools do not * shrink so no re-encryption occurs in dma_direct_free(). */ - ret =set_memory_decrypted ((unsigned long )page_to_virt (page), -1 << order); -if (ret) { - leak_pages = true; - goto remove_mapping; +if (dma_pool->cc_shared) { + ret =set_memory_decrypted ((unsigned long )page_to_virt (page), +1 << order); +if (ret) { + leak_pages = true; + goto remove_mapping; + } } - ret =gen_pool_add_virt (pool, (unsigned long )addr,page_to_phys (page), - pool_size,NUMA_NO_NODE ); + + ret =gen_pool_add_virt (dma_pool->pool, (unsigned long )addr, +page_to_phys (page), pool_size,NUMA_NO_NODE );if (ret) goto encrypt_mapping;@@ -129,12 +138,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,return 0 ;
Quoted 12 lines
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 144
Quoted 10 lines
kernel/dma/pool.c +3
-5encrypt_mapping: - ret =set_memory_encrypted ((unsigned long )page_to_virt (page), -1 << order); -if (WARN_ON_ONCE (ret)) { -/* Decrypt succeeded but encrypt failed, purposely leak */ +if (dma_pool->cc_shared && +set_memory_encrypted ((unsigned long )page_to_virt (page),1 << order)) leak_pages = true; - } +Was it intentional to remove the
WARN_ONand comment ?
That WARN_ON() was a bit inconsistent. Not all page leaks due to encrypt/decrypt failures triggered a warning, so I removed it.
If we want to warn about such failures, we should add the warning consistently across the code. We may also want to handle decrypt failures by encrypting the page again to avoid leaking it.
I will work on that as a tree-wide change in a separate patch.
-aneesh
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Jason GunthorpeQuoted 51 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +0
-3@@ -114,14 +120,17 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,* Memory in the atomic DMA pools must be unencrypted, the pools do not * shrink so no re-encryption occurs in dma_direct_free(). */ - ret =set_memory_decrypted ((unsigned long )page_to_virt (page), -1 << order); -if (ret) {kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 130
Quoted 10 lines
kernel/dma/pool.c +7
-2- leak_pages = true; - goto remove_mapping; +if (dma_pool->cc_shared) { + ret =set_memory_decrypted ((unsigned long )page_to_virt (page), +1 << order); +if (ret) { + leak_pages = true; + goto remove_mapping; + } }This makes the
memory_decryptedconditional, but it doesn't change the lines a few above:addr = dma_common_contiguous_remap(page, pool_size, pgprot_decrypted(pgprot_dmacoherent(PAGE_KERNEL)), ^^^^^^^^^^^^ __builtin_return_address(0)); if (!addr) goto free_page;It is wrong to pass
pgprot_decrypted()to the arch code ifset_memory_decrypted()was not called.Also it looks at some point the nature of the atomic pool has become confused. Originally it was just to allocate atomic memory that had been vmap'd outside the atomic context (to set the non-coherent pgprot), so every caller was expecting non-cached memory.
Then it was reused to also allocate CC shared memory outside the atomic context. That was fine for x86 that doesn't use
DMA_REMAPbut on ARM64 it now means all atomic pool CC memory is uncached? That doesn't seem to make any sense...I suppose along the lines of this patch the solution is to add a noncoherent property to the pool so we can select the correct combination:
noncoherent !SHARED = vmap
pgprot_noncached
!noncoherent SHARED= vmappgprot_decrypted+set_memory_decrypted
noncoherent SHARED = (probably unrealistic in real systems)
!noncoherent !SHARED = normal__dma_direct_alloc_pages()
Quoted 5 lines
But I don't view this as that important, the CC hypervisor is probably going to use the S2 page table to force cachable on all system memory so the non-cached pgprot is a NOP, but the extra vmap is wasteful and it is confusing.. So maybe a little fixme is all that is needed here.
Something like?
modified kernel/dma/direct.c
@@ -260,6 +260,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
/*
* Remapping or decrypting memory may block, allocate the memory from
* the atomic pools instead if we aren't allowed block.
+ * FIXME!! With CONFIG_DMA_DIRECT_REMAP, the pool is also mapped as
+ * DMA-coherent (non-cacheable). We may want to create a separate pool
+ * dedicated to CC_SHARED atomic allocations.
*/
if ((remap || (attrs & __DMA_ATTR_ALLOC_CC_SHARED)) &&
dma_direct_use_pool(dev, gfp)) {
modified kernel/dma/pool.c
@@ -88,6 +88,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
unsigned int order;
struct page *page = NULL;
bool leak_pages = false;
+ pgprot_t prot;
void *addr;
int ret = -ENOMEM;
unsigned int min_encrypt_order = get_order(mem_cc_shared_granule_size());
@@ -110,8 +111,12 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
arch_dma_prep_coherent(page, pool_size);
#ifdef CONFIG_DMA_DIRECT_REMAP
- addr = dma_common_contiguous_remap(page, pool_size,
- pgprot_decrypted(pgprot_dmacoherent(PAGE_KERNEL)),
+ if (dma_pool->cc_shared)
+ prot = pgprot_decrypted(pgprot_dmacoherent(PAGE_KERNEL));
+ else
+ prot = pgprot_dmacoherent(PAGE_KERNEL);
+
+ addr = dma_common_contiguous_remap(page, pool_size, prot,
__builtin_return_address(0));
if (!addr)
goto free_page;
Re: [PATCH v7 20/22] dma: swiotlb: free dynamic pools from process context
In reply to Jason GunthorpeQuoted 6 lines · Jason Gunthorpe
Quoted 4 lines · Aneesh Kumar K.V (Arm)
kernel/dma/swiotlb.c +0
-0@@ -840,7 +843,7 @@ static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool)list_del_rcu (&pool->node);spin_unlock_irqrestore (&dev->dma_io_tlb_lock, flags);
Quoted 7 lines
kernel/dma/swiotlb.c static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool) · line 846
Quoted 2 lines
kernel/dma/swiotlb.c +1
-1-call_rcu (&pool->rcu, swiotlb_dyn_free); +swiotlb_schedule_dyn_free (pool);This knows if the pool has
cc_sharedmemory in it since there is now a flag tracking that? So should the work queue version be conditional on that flag?
We could definitely do that. I was not sure whether it was worth it, considering that it would add two paths for SWIOTLB dynamic free.
-aneesh
Marek Szyprowski
maintainerQuoted 8 lines · Jason Gunthorpe
Quoted 4 lines · Marek Szyprowski
I'm fine with merging on top of the topic branch and I assume that this patchset is mature enough to give it a try in linux-next, but first I would like to get a review or at least acks from others with good CC knowledge or experience.
I think it is good to go from a CC perspective, there are still some more items to fix up (like the MMIO) but I'd rather they be followups at this point.
Great. Aneesh, any chance You could send v8 with the remaining items fixed till end of this week? I will be happy to push it to dma-mapping-for-next for testing in linux-next asap.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Quoted 6 lines · Marek Szyprowski
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Marek Szyprowski
I'm fine with merging on top of the topic branch and I assume that this patchset is mature enough to give it a try in linux-next, but first I would like to get a review or at least acks from others with good CC knowledge or experience.
Quoted 8 lines
Quoted 3 lines
I think it is good to go from a CC perspective, there are still some more items to fix up (like the MMIO) but I'd rather they be followups at this point.
Great. Aneesh, any chance You could send v8 with the remaining items fixed till end of this week? I will be happy to push it to dma-mapping-for-next for testing in linux-next asap.
Sure. You need this rebased on top of the pKVM changes, right? Otherwise, we'll end up with conflicts when you do the final pull request.
-aneesh
Marek Szyprowski
maintainerQuoted 10 lines · Aneesh Kumar K.V
Quoted 9 lines · Marek Szyprowski
Quoted 8 lines · Jason Gunthorpe
Quoted 4 lines · Marek Szyprowski
I'm fine with merging on top of the topic branch and I assume that this patchset is mature enough to give it a try in linux-next, but first I would like to get a review or at least acks from others with good CC knowledge or experience.
I think it is good to go from a CC perspective, there are still some more items to fix up (like the MMIO) but I'd rather they be followups at this point.
Quoted 7 lines
Quoted 4 lines
Great. Aneesh, any chance You could send v8 with the remaining items fixed till end of this week? I will be happy to push it to dma-mapping-for-next for testing in linux-next asap.
Sure. You need this rebased on top of the pKVM changes, right? Otherwise, we'll end up with conflicts when you do the final pull request.
Ah, indeed. I forgot about the pKVM changes. If there is a stable branch with those patches, please base v8 on top of it.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v7 02/22] dma-pool: fix page leak in atomic_pool_expand() cleanup
In reply to Aneesh Kumar K.VQuoted 17 lines · Aneesh Kumar K.V
Quoted 14 lines · Jason Gunthorpe
kernel/dma/pool.c static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, · line 122
Quoted 9 lines · Aneesh Kumar K.V (Arm)
kernel/dma/pool.c +3
-1@@ -115,8 +116,10 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,*/ ret =set_memory_decrypted ((unsigned long )page_to_virt (page),1 << order); -if (ret) +if (ret) { + leak_pages = true; goto remove_mapping; + }Truely these
_set_memory_decrypted()things are an insane API. So a if it fails to decrypt it can be in any messy state?
Quoted 3 lines
Yes, we could possibly try to encrypt the page again and, if that succeeds, avoid leaking it. We might want to do that tree-wide in a separate patch.
IMHO it is a horrid API if failure leaves thing in an indeterminate state. For something like this I don't see why the arch FW implementation would be unable to restore things back to as they were on failure.
But whatever, everything about set_memory_xx is really bad it could use a cleaning
Jason
Re: [PATCH v7 04/22] dma: free atomic pool pages by physical address
In reply to Aneesh Kumar K.VQuoted 43 lines · Aneesh Kumar K.V
Quoted 33 lines · Jason Gunthorpe
Quoted 17 lines · Aneesh Kumar K.V (Arm)
dma_direct_alloc_pages()may satisfy atomic allocations from the coherent atomic pools. The pool allocation is keyed by the virtual address stored in thegen_pool, but the pages API returns only the backing struct page.On architectures with
CONFIG_DMA_DIRECT_REMAP, atomic pool chunks are added to thegen_poolusing their remapped virtual address.dma_direct_free_pages()reconstructs a linear-map address withpage_address(page)and passes that todma_free_from_pool(). That address does not match thegen_poolvirtual range, so the pool lookup can fail and the code can fall through to freeing a pool-owned page through the normal page allocator path.Add a page-based pool free helper that looks up the owning pool chunk by physical address, translates it back to the
gen_poolvirtual address, and frees that address to the pool. Use it fromdma_direct_free_pages()while keeping the existing virtual-address helper for coherent allocation frees.Quoted 8 lines
Tested-by Michael Kelley <[email protected]>Tested-by Mostafa Saleh <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- include/linux/dma-map-ops.h | 1 + kernel/dma/direct.c | 4 +-- kernel/dma/pool.c | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)This seems pretty suboptimal?
If !CONFIG_DMA_DIRECT_REMAP then
page_to_virt()was used to compute the genpool's addr sodma_free_from_pool_page()can use the same logic, which is how things must be working at all todayQuoted 7 lines
The
CONFIG_DMA_DIRECT_REMAPscenario does look broken, so I'm surprised there isn't a Fixes line on this commit? I don't have an opinion on the search, but sincealloc_pages()is used there is 8 bytes in the struct page that could be used to store the remapped vaddr to avoid the search if someday someone wants to improve this. Maybe a small comment hinting that direction would be a nice addition.
Quoted 7 lines
Something like
+/*
+ * FIXME!! We could avoid this by storing the remapped virtual address in
+ * struct page and using that for lookup.
+ */
booldma_free_from_pool_page(struct device *dev, struct page *page, size_t size)
Plus some if (!IS_ENABLED()) that does the direct lookup
I would just use : not !! :)
Jason
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Aneesh Kumar K.VQuoted 5 lines · Aneesh Kumar K.V
If we want to warn about such failures, we should add the warning consistently across the code. We may also want to handle decrypt failures by encrypting the page again to avoid leaking it.
I will work on that as a tree-wide change in a separate patch.
IMHO the WARN should be inside set_memory_encrypted(), and maybe it should be a BUG_ON...
"free" functions shouldn't fail, and returning the memory back to normal is a "free" operation.
Jason
Re: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Aneesh Kumar K.VQuoted 8 lines · Aneesh Kumar K.V
Quoted 6 lines
But I don't view this as that important, the CC hypervisor is probably going to use the S2 page table to force cachable on all system memory so the non-cached pgprot is a NOP, but the extra vmap is wasteful and it is confusing.. So maybe a little fixme is all that is needed here.
Something like?
Yeah
Jason
RE: [PATCH v7 11/22] dma-pool: track decrypted atomic pools and select them via attrs
In reply to Jason GunthorpeFrom: Jason Gunthorpe <[email protected]> Sent: Tuesday, July 14, 2026 5:25 AM
Quoted 11 lines · Aneesh Kumar K.V
Quoted 6 lines
If we want to warn about such failures, we should add the warning consistently across the code. We may also want to handle decrypt failures by encrypting the page again to avoid leaking it.
I will work on that as a tree-wide change in a separate patch.
IMHO the WARN should be inside
set_memory_encrypted(), and maybe it should be aBUG_ON...
There was a discussion about this a while back. The decision was to embed the WARNs, and that callers should not output any error messages upon failure.
The x86 implementation of set_memory_decrypted() and set_memory_encrypted() has an embedded WARN_ON_ONCE() that triggers on a failure. The arm64 realm encrypt/decrypt ops also have an embedded WARN().
The pkvm encrypt/decrypt ops do not, so that's maybe where it should be added.
Michael
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
In reply to Aneesh Kumar K.VQuoted 68 lines · Aneesh Kumar K.V
Quoted 48 lines · Catalin Marinas
arch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 344
Quoted 13 lines · Aneesh Kumar K.V (Arm)
arch/arm64/mm/init.c +1
-3@@ -338,10 +338,8 @@ void __init arch_mm_preinit(void)unsigned int flags =SWIOTLB_VERBOSE ;bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); -if (is_realm_world ()) { +if (is_realm_world ()) swiotlb = true; - flags |=SWIOTLB_FORCE ; - }For this part:
Reviewed-by Catalin Marinas <[email protected]>Quoted 29 lines
kernel/dma/direct.h +10
-7@@ -88,37 +88,40 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, {dma_addr_t dma_addr; +/* +* For a device requiring unencrypted DMA, MMIO memory is treated +* as shared by default. +*/ +if (force_dma_unencrypted (dev) && (attrs &DMA_ATTR_MMIO )) + attrs |=DMA_ATTR_CC_SHARED ; +if (is_swiotlb_force_bounce (dev)) { -if (!(attrs &DMA_ATTR_CC_SHARED )) { -if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) -return DMA_MAPPING_ERROR ; +if (attrs & (DMA_ATTR_MMIO |DMA_ATTR_REQUIRE_COHERENT )) +return DMA_MAPPING_ERROR ; -return swiotlb_map (dev, phys, size, dir, attrs); - } - }else if (attrs &DMA_ATTR_CC_SHARED ) { -return DMA_MAPPING_ERROR ; +return swiotlb_map (dev, phys, size, dir, attrs); }Quoted 17 lines
kernel/dma/direct.h static inline dma_addr_t dma_direct_map_phys(struct device *dev, · line 108
Quoted 9 lines
kernel/dma/direct.h +3
-5-if (attrs &DMA_ATTR_MMIO ) { - dma_addr = phys; -if (unlikely (!dma_capable (dev, dma_addr, size, false, attrs))) - goto err_overflow; - }else if (attrs &DMA_ATTR_CC_SHARED ) { +if (attrs &DMA_ATTR_CC_SHARED ) dma_addr =phys_to_dma_unencrypted (dev, phys); +else + dma_addr =phys_to_dma_encrypted (dev, phys);For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.
Quoted 2 lines
Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
Normally both encrypted and unencrypted DMA addresses do not have Cbit, with the only exception of "iommu=pt" (which is not the default afaik). And in this case, having Cbit in DMA handles only makes sense if p2p trafic goes via the root port (== IOMMU in passthrough mode, and I am not sure even about if the root port will convert this Cbit to T=1 MMIO in this mode) but if it goes via some PCI bridge - then Cbit won't mean encryption for sure. But I do not know much about p2p (never touched). But in any case force_dma_unencrypted() seems to make no sense here.
Quoted 14 lines
/* * For host memory encryption and device requiring unencrypted DMA, * MMIO memory is treated as shared by default. */ if (attrs & DMA_ATTR_MMIO) { if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) || force_dma_unencrypted(dev)) attrs |= DMA_ATTR_CC_SHARED; }Quoted 3 lines
In the other case, for an arm CCA guest, if the MMIO is shared we end up setting the shared attribute but that's fine, it's only an IPA address.
IPA == guest physical address? Thanks,
Quoted 5 lines
Quoted 3 lines
Signature · 2 lines
-- Catalin-aneesh
Signature · 1 line
Alexey
Re: [PATCH v7 16/22] dma-direct: make dma_direct_map_phys() honor DMA_ATTR_CC_SHARED
Quoted 11 lines · Alexey Kardashevskiy
Quoted 10 lines
Quoted 7 lines
For AMD/SME, on host with memory encryption we now end up setting the C bit for
DMA_ATTR_MMIO. This is fine for RAM but not sure whether some other MMIO bus understands this attribute. Maybe we should stick to something like__phys_to_dma()for the !CC_SHARED && MMIO path. Or, since this is not universally defined, just use the olddma_addr= phys if MMIO and ignore any unlikely DMA offsets.Considering for AMD/SME system an unencrypted dma addr is one without C bit, will this be good?
Quoted 9 lines
Normally both encrypted and unencrypted DMA addresses do not have Cbit, with the only exception of "iommu=pt" (which is not the default afaik). And in this case, having Cbit in DMA handles only makes sense if p2p trafic goes via the root port (== IOMMU in passthrough mode, and I am not sure even about if the root port will convert this Cbit to T=1 MMIO in this mode) but if it goes via some PCI bridge - then Cbit won't mean encryption for sure. But I do not know much about p2p (never touched). But in any case
force_dma_unencrypted()seems to make no sense here.
True p2p through a switch does not call the DMA API at all, it directly stuffs the physical MMIO BAR address of the target into the dma_addr_t - which won't have a C bit.
The DMA API is only involved if the p2p will transit through the root port, so adding the C bit is correct.
AFAICT it is iommu=!pt that doesn't work at all since the AMD IOMMU driver always does __sme_set() even for MMIO when forming the IOPTE, which I guess is wrong for MMIO.
Jason