All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dma-mapping: move pgprot_decrypted out of dma_pgprot
@ 2022-03-31  6:06 Christoph Hellwig
  2022-03-31 16:13 ` Alex Xu (Hello71) via iommu
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2022-03-31  6:06 UTC (permalink / raw)
  To: iommu; +Cc: thomas.lendacky, robin.murphy, Alex Xu

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: 5ff79fddf0ef ("dma-mapping: remove CONFIG_DMA_REMAP")
Reported-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 kernel/dma/direct.c  | 10 ++++++++--
 kernel/dma/mapping.c |  2 --
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 35a1d29d6a2e9..9743c6ccce1a9 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -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;
diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c
index 559461a826bab..db7244291b745 100644
--- a/kernel/dma/mapping.c
+++ b/kernel/dma/mapping.c
@@ -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
-- 
2.30.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] dma-mapping: move pgprot_decrypted out of dma_pgprot
  2022-03-31  6:06 [PATCH] dma-mapping: move pgprot_decrypted out of dma_pgprot Christoph Hellwig
@ 2022-03-31 16:13 ` Alex Xu (Hello71) via iommu
  2022-03-31 16:26   ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Xu (Hello71) via iommu @ 2022-03-31 16:13 UTC (permalink / raw)
  To: Christoph Hellwig, iommu; +Cc: thomas.lendacky, robin.murphy

Excerpts from Christoph Hellwig's message of March 31, 2022 2:06 am:
> 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: 5ff79fddf0ef ("dma-mapping: remove CONFIG_DMA_REMAP")
> Reported-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  kernel/dma/direct.c  | 10 ++++++++--
>  kernel/dma/mapping.c |  2 --
>  2 files changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> index 35a1d29d6a2e9..9743c6ccce1a9 100644
> --- a/kernel/dma/direct.c
> +++ b/kernel/dma/direct.c
> @@ -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;
> diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c
> index 559461a826bab..db7244291b745 100644
> --- a/kernel/dma/mapping.c
> +++ b/kernel/dma/mapping.c
> @@ -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
> -- 
> 2.30.2
> 
> 

Tested-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>

Thanks,
Alex.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] dma-mapping: move pgprot_decrypted out of dma_pgprot
  2022-03-31 16:13 ` Alex Xu (Hello71) via iommu
@ 2022-03-31 16:26   ` Christoph Hellwig
  0 siblings, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2022-03-31 16:26 UTC (permalink / raw)
  To: Alex Xu (Hello71); +Cc: thomas.lendacky, iommu, robin.murphy, Christoph Hellwig

On Thu, Mar 31, 2022 at 12:13:19PM -0400, Alex Xu (Hello71) wrote:
> Tested-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>

Thanks!
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-31 16:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31  6:06 [PATCH] dma-mapping: move pgprot_decrypted out of dma_pgprot Christoph Hellwig
2022-03-31 16:13 ` Alex Xu (Hello71) via iommu
2022-03-31 16:26   ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.