Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

dma-mapping: introduce DMA_ATTR_CC_SHARED for shared memory

Current CC designs don't place a vIOMMU in front of untrusted devices.
Instead, the DMA API forces all untrusted device DMA through swiotlb
bounce buffers (is_swiotlb_force_bounce()) which copies data into
shared memory on behalf of the device.

When a caller has already arranged for the memory to be shared
via set_memory_decrypted(), the DMA API needs to know so it can map
directly using the unencrypted physical address rather than bounce
buffering. Following the pattern of DMA_ATTR_MMIO, add
DMA_ATTR_CC_SHARED for this purpose. Like the MMIO case, only the
caller knows what kind of memory it has and must inform the DMA API
for it to work correctly.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Acked-by: Sumit Semwal <sumit.semwal@linaro.org>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Link: https://lore.kernel.org/r/20260325192352.437608-2-jiri@resnulli.us

authored by

Jiri Pirko and committed by
Marek Szyprowski
f0548044 27e2e9b9

+34 -6
+10
include/linux/dma-mapping.h
··· 92 92 * flushing. 93 93 */ 94 94 #define DMA_ATTR_REQUIRE_COHERENT (1UL << 12) 95 + /* 96 + * DMA_ATTR_CC_SHARED: Indicates the DMA mapping is shared (decrypted) for 97 + * confidential computing guests. For normal system memory the caller must have 98 + * called set_memory_decrypted(), and pgprot_decrypted must be used when 99 + * creating CPU PTEs for the mapping. The same shared semantic may be passed 100 + * to the vIOMMU when it sets up the IOPTE. For MMIO use together with 101 + * DMA_ATTR_MMIO to indicate shared MMIO. Unless DMA_ATTR_MMIO is provided 102 + * a struct page is required. 103 + */ 104 + #define DMA_ATTR_CC_SHARED (1UL << 13) 95 105 96 106 /* 97 107 * A dma_addr_t can hold any valid DMA or bus address for the platform. It can
+2 -1
include/trace/events/dma.h
··· 34 34 { DMA_ATTR_PRIVILEGED, "PRIVILEGED" }, \ 35 35 { DMA_ATTR_MMIO, "MMIO" }, \ 36 36 { DMA_ATTR_DEBUGGING_IGNORE_CACHELINES, "CACHELINES_OVERLAP" }, \ 37 - { DMA_ATTR_REQUIRE_COHERENT, "REQUIRE_COHERENT" }) 37 + { DMA_ATTR_REQUIRE_COHERENT, "REQUIRE_COHERENT" }, \ 38 + { DMA_ATTR_CC_SHARED, "CC_SHARED" }) 38 39 39 40 DECLARE_EVENT_CLASS(dma_map, 40 41 TP_PROTO(struct device *dev, phys_addr_t phys_addr, dma_addr_t dma_addr,
+11 -3
kernel/dma/direct.h
··· 89 89 dma_addr_t dma_addr; 90 90 91 91 if (is_swiotlb_force_bounce(dev)) { 92 - if (attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT)) 93 - return DMA_MAPPING_ERROR; 92 + if (!(attrs & DMA_ATTR_CC_SHARED)) { 93 + if (attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT)) 94 + return DMA_MAPPING_ERROR; 94 95 95 - return swiotlb_map(dev, phys, size, dir, attrs); 96 + return swiotlb_map(dev, phys, size, dir, attrs); 97 + } 98 + } else if (attrs & DMA_ATTR_CC_SHARED) { 99 + return DMA_MAPPING_ERROR; 96 100 } 97 101 98 102 if (attrs & DMA_ATTR_MMIO) { 99 103 dma_addr = phys; 104 + if (unlikely(!dma_capable(dev, dma_addr, size, false))) 105 + goto err_overflow; 106 + } else if (attrs & DMA_ATTR_CC_SHARED) { 107 + dma_addr = phys_to_dma_unencrypted(dev, phys); 100 108 if (unlikely(!dma_capable(dev, dma_addr, size, false))) 101 109 goto err_overflow; 102 110 } else {
+11 -2
kernel/dma/mapping.c
··· 157 157 { 158 158 const struct dma_map_ops *ops = get_dma_ops(dev); 159 159 bool is_mmio = attrs & DMA_ATTR_MMIO; 160 + bool is_cc_shared = attrs & DMA_ATTR_CC_SHARED; 160 161 dma_addr_t addr = DMA_MAPPING_ERROR; 161 162 162 163 BUG_ON(!valid_dma_direction(dir)); ··· 169 168 return DMA_MAPPING_ERROR; 170 169 171 170 if (dma_map_direct(dev, ops) || 172 - (!is_mmio && arch_dma_map_phys_direct(dev, phys + size))) 171 + (!is_mmio && !is_cc_shared && 172 + arch_dma_map_phys_direct(dev, phys + size))) 173 173 addr = dma_direct_map_phys(dev, phys, size, dir, attrs, true); 174 + else if (is_cc_shared) 175 + return DMA_MAPPING_ERROR; 174 176 else if (use_dma_iommu(dev)) 175 177 addr = iommu_dma_map_phys(dev, phys, size, dir, attrs); 176 178 else if (ops->map_phys) ··· 210 206 { 211 207 const struct dma_map_ops *ops = get_dma_ops(dev); 212 208 bool is_mmio = attrs & DMA_ATTR_MMIO; 209 + bool is_cc_shared = attrs & DMA_ATTR_CC_SHARED; 213 210 214 211 BUG_ON(!valid_dma_direction(dir)); 212 + 215 213 if (dma_map_direct(dev, ops) || 216 - (!is_mmio && arch_dma_unmap_phys_direct(dev, addr + size))) 214 + (!is_mmio && !is_cc_shared && 215 + arch_dma_unmap_phys_direct(dev, addr + size))) 217 216 dma_direct_unmap_phys(dev, addr, size, dir, attrs, true); 217 + else if (is_cc_shared) 218 + return; 218 219 else if (use_dma_iommu(dev)) 219 220 iommu_dma_unmap_phys(dev, addr, size, dir, attrs); 220 221 else if (ops->unmap_phys)