dma-mapping: move pgprot_decrypted out of dma_pgprot
authorChristoph Hellwig <hch@lst.de>
Thu, 31 Mar 2022 06:01:21 +0000 (08:01 +0200)
committerChristoph Hellwig <hch@lst.de>
Fri, 1 Apr 2022 04:46:51 +0000 (06:46 +0200)
pgprot_decrypted is used by AMD SME systems to allow access to memory
that was set to not encrypted using set_memory_decrypted.  That only
happens for dma-direct memory as the IOMMU solves the addressing
challenges for the encryption bit using its own remapping.

Move the pgprot_decrypted call out of dma_pgprot which is also used
by the IOMMU mappings and into dma-direct so that it is only used with
memory that was set decrypted.

Fixes: f5ff79fddf0e ("dma-mapping: remove CONFIG_DMA_REMAP")
Reported-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Tested-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
kernel/dma/direct.c
kernel/dma/mapping.c

index 35a1d29d6a2e99aad04bb7434e62202838377816..9743c6ccce1a92c66b87af1530bd9396136f0ad3 100644 (file)
@@ -277,12 +277,16 @@ 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);
 
                /* create a coherent mapping */
-               ret = dma_common_contiguous_remap(page, size,
-                               dma_pgprot(dev, PAGE_KERNEL, attrs),
+               ret = dma_common_contiguous_remap(page, size, prot,
                                __builtin_return_address(0));
                if (!ret)
                        goto out_free_pages;
@@ -535,6 +539,8 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
        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);
 
        if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
                return ret;
index 559461a826babff64054b6a85119e5edabb9f99d..db7244291b745b8ff42ea6ed6b20c33ec8ec1e84 100644 (file)
@@ -407,8 +407,6 @@ EXPORT_SYMBOL(dma_get_sgtable_attrs);
  */
 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
 {
-       if (force_dma_unencrypted(dev))
-               prot = pgprot_decrypted(prot);
        if (dev_is_dma_coherent(dev))
                return prot;
 #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE