Skip to contentSkip to conversation

x86/mm: Don't force unencrypted DMA for IOMMU-backed devices

PATCH Applied Aneesh Kumar K.V (Arm) 5 participants 9 messages Sep 8 – Sep 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: Don't force unencrypted DMA for IOMMU-backed devices

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")
---
 arch/x86/mm/mem_encrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +2 -1
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
 #include <linux/cc_platform.h>
 #include <linux/mem_encrypt.h>
 #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
 #include <asm/sev.h>
 
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
 	 * device does not support DMA to addresses that include the
 	 * encryption mask.
 	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {
 		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
 		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
 						dev->bus_dma_limit);
Signature · 1 line
2.43.0
#1 lore raw

From: Aneesh Kumar K.V (Arm) <[email protected]> Sent: Tuesday, September 8, 2026 4:33 AM

Quoted 17 lines

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Quoted 7 lines

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")

There seems to be something wrong with this link. Just viewing the email thread in lore.kernel.org shows a [not found] error in the thread.

Michael

Quoted 4 lines
---
 arch/x86/mm/mem_encrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
#2 lore raw
Quoted 21 lines · Michael Kelley

From: Aneesh Kumar K.V (Arm) <[email protected]> Sent: Tuesday, September 8, 2026 4:33 AM

Quoted 20 lines

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Quoted 5 lines
Quoted 3 lines
Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")

There seems to be something wrong with this link. Just viewing the email thread in lore.kernel.org shows a [not found] error in the thread.

Indeed, this one works better:

https://lore.kernel.org/all/CANB4YXS=Nf-co3t8eMHtqrW4sn=1BDcP6o=EoTrgZm0WYMYTyw@mail.gmail.com/

I will amend it while applying, but I want give everyone some time for comments.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#3 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 41 lines · Aneesh Kumar K.V (Arm)

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")
---
  arch/x86/mm/mem_encrypt.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +1 -0
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
  #include <linux/cc_platform.h>
  #include <linux/mem_encrypt.h>
  #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
  #include <asm/sev.h>
 

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 37

Quoted 8 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
  	 * device does not support DMA to addresses that include the
  	 * encryption mask.
  	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {
  		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
  		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
  						dev->bus_dma_limit);

if we drop this whole "if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))" chunk, won't dma_alloc_attrs()&co be able to figure out this from dev->bus_dma_limit and other DMA masks we have all over the place? I should probably just try it... Thanks,

Signature · 1 line
Alexey
#4 lore raw
Quoted 41 lines · Alexey Kardashevskiy
Quoted 40 lines · Aneesh Kumar K.V (Arm)

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")
---
  arch/x86/mm/mem_encrypt.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +1 -0
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
  #include <linux/cc_platform.h>
  #include <linux/mem_encrypt.h>
  #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
  #include <asm/sev.h>
 
Quoted 15 lines

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 37

Quoted 9 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
  	 * device does not support DMA to addresses that include the
  	 * encryption mask.
  	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {
  		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
  		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
  						dev->bus_dma_limit);

if we drop this whole "if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))" chunk, won't dma_alloc_attrs()&co be able to figure out this from dev->bus_dma_limit and other DMA masks we have all over the place? I should probably just try it... Thanks,

That would imply that, with host memory encryption, no device requires unencrypted DMA. IIUC, that is not true. There are three cases:

1. Host memory encryption with an IOMMU: unencrypted DMA is not required.
2. Host memory encryption without an IOMMU, where the device's DMA addressing constraints cannot cover the C-bit: unencrypted DMA is required.
3. Host memory encryption without an IOMMU, where the device can address the C-bit: unencrypted DMA is not required.

-aneesh

#6 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 16 lines · Aneesh Kumar K.V (Arm)

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Quoted 8 lines

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")

Applied to dma-mapping-fixes with adjusted url and changed 'link' tag to the 'closes' one, thanks!

Quoted 17 lines
---
 arch/x86/mm/mem_encrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +1 -0
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
 #include <linux/cc_platform.h>
 #include <linux/mem_encrypt.h>
 #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
 #include <asm/sev.h>
 

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 37

Quoted 8 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
 	 * device does not support DMA to addresses that include the
 	 * encryption mask.
 	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {
 		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
 		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
 						dev->bus_dma_limit);

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#5 lore raw
In reply to Aneesh Kumar K.V
Quoted 60 lines · Aneesh Kumar K.V
Quoted 41 lines · Alexey Kardashevskiy
Quoted 40 lines · Aneesh Kumar K.V (Arm)

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")
---
   arch/x86/mm/mem_encrypt.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +1 -0
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
   #include <linux/cc_platform.h>
   #include <linux/mem_encrypt.h>
   #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
   #include <asm/sev.h>
 
Quoted 15 lines

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 37

Quoted 9 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
   	 * device does not support DMA to addresses that include the
   	 * encryption mask.
   	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {
   		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
   		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
   						dev->bus_dma_limit);

if we drop this whole "if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))" chunk, won't dma_alloc_attrs()&co be able to figure out this from dev->bus_dma_limit and other DMA masks we have all over the place? I should probably just try it... Thanks,

Quoted 4 lines

That would imply that, with host memory encryption, no device requires unencrypted DMA. IIUC, that is not true. There are three cases:

1. Host memory encryption with an IOMMU: unencrypted DMA is not required.

Then this helper returns "false".

Quoted 1 line

2. Host memory encryption without an IOMMU, where the device's DMA addressing constraints cannot cover the C-bit: unencrypted DMA is required.

then it goes via swiotlb.

Quoted 1 line

3. Host memory encryption without an IOMMU, where the device can address the C-bit: unencrypted DMA is not required.

Then this helper returns "false".

So it should "just work", no? And it seems like it actually does.

Signature · 1 line
Alexey
#7 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 41 lines · Aneesh Kumar K.V (Arm)

Commit 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs") exposed an issue with force_dma_unencrypted() on systems using host memory encryption.

force_dma_unencrypted() checks whether the device DMA mask can address the encryption bit and, if not, requires DMA allocations to use unencrypted memory. However, this check is not applicable when the device is using the IOMMU. In that case, the device DMA mask constrains the IOVA seen by the device, not the backing physical address, so it does not need to cover the C-bit.

This currently causes dma_alloc_attrs() to set __DMA_ATTR_ALLOC_CC_SHARED for such devices. iommu_dma_alloc() does not support that attribute and rejects the allocation, causing DMA allocations to fail.

Do not force DMA allocations to be unencrypted when the device is using the IOMMU. This allows the IOMMU to map the encrypted physical pages as before and avoids incorrectly requesting CC_SHARED allocations.

Fixes 8277a12d0d60 ("dma-pool: track decrypted atomic pools and select them via attrs")
---
 arch/x86/mm/mem_encrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/mem_encrypt.c +1 -0
index 912f22ca838f..a349d8d21569 100644
@@ -13,6 +13,7 @@
 #include <linux/cc_platform.h>
 #include <linux/mem_encrypt.h>
 #include <linux/virtio_anchor.h>
+#include <linux/iommu-dma.h>
 
 #include <asm/sev.h>
 

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 34

Quoted 5 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
 	 * device does not support DMA to addresses that include the
 	 * encryption mask.
 	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {

When this support was originally added many years ago, this function was not called if an IOMMU was active and generating IOVAs. So if this function is now called even when an IOMMU is performing the DMA mapping, then this is appropriate. Although, it would seem that if an IOMMU is performing the mapping and this function is still being called, checking use_dma_iommu(dev) and exiting early from force_dma_unencrypted() at the very beginning is more appropriate, right?

@Alexey, would that impact your TIO/TDISP support at all?

Thanks,
Tom

Quoted 3 lines
arch/x86/mm/mem_encrypt.c +0 -0
 		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
 		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
 						dev->bus_dma_limit);
#8 lore raw
Quoted 1 line · Tom Lendacky

On 9/8/26 06:32, Aneesh Kumar K.V (Arm) wrote:

[ ... 39 lines skipped ... ]

Quoted 16 lines

arch/x86/mm/mem_encrypt.c bool force_dma_unencrypted(struct device *dev) · line 34

Quoted 7 lines
arch/x86/mm/mem_encrypt.c +1 -1
@@ -30,7 +31,7 @@ bool force_dma_unencrypted(struct device *dev)
 	 * device does not support DMA to addresses that include the
 	 * encryption mask.
 	 */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && !use_dma_iommu(dev)) {

When this support was originally added many years ago, this function was not called if an IOMMU was active and generating IOVAs. So if this function is now called even when an IOMMU is performing the DMA mapping, then this is appropriate. Although, it would seem that if an IOMMU is performing the mapping and this function is still being called, checking use_dma_iommu(dev) and exiting early from force_dma_unencrypted() at the very beginning is more appropriate, right?

I don't think that is correct for a confidential guest with a stage-1 IOMMU.

Quoted 2 lines

@Alexey, would that impact your TIO/TDISP support at all?

-aneesh