Skip to contentSkip to conversation

drviers/iommu/amd: support P2P access through IOMMU when SME is enabled

PATCH In review Wencheng Yang 3 participants 11 messages Jan 17, 2025 – Jan 21, 2025

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

PATCH v2 1/1 drviers/iommu/amd: support P2P access through IOMMU when SME is enabled

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Clear memory encryption bit in io page table if the mapping is MMIO rather than memory.

---
 drivers/iommu/amd/amd_iommu_types.h | 7 ++++---
 drivers/iommu/amd/io_pgtable.c      | 2 ++
 drivers/iommu/amd/io_pgtable_v2.c   | 5 ++++-
 drivers/iommu/amd/iommu.c           | 2 ++
 drivers/vfio/vfio_iommu_type1.c     | 4 +++-
 include/uapi/linux/vfio.h           | 1 +
 6 files changed, 16 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +4 -3
index fdb0357e0bb9..b0f055200cf3 100644
@@ -434,9 +434,10 @@
 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IOMMU_PROT_MASK 0x07
+#define IOMMU_PROT_IR   0x01
+#define IOMMU_PROT_IW   0x02
+#define IOMMU_PROT_MMIO 0x04
 
 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE	(1 << 2)
 
drivers/iommu/amd/io_pgtable.c +2 -0
index f3399087859f..dff887958a56 100644
@@ -373,6 +373,8 @@ static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
 			__pte |= IOMMU_PTE_IR;
 		if (prot & IOMMU_PROT_IW)
 			__pte |= IOMMU_PTE_IW;
+		if (prot & IOMMU_PROT_MMIO)
+			__pte = __sme_clr(__pte);
 
 		for (i = 0; i < count; ++i)
 			pte[i] = __pte;
drivers/iommu/amd/io_pgtable_v2.c +4 -1
index c616de2c5926..55f969727dea 100644
@@ -65,7 +65,10 @@ static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot)
 {
 	u64 pte;
 
-	pte = __sme_set(paddr & PM_ADDR_MASK);
+	pte = paddr & PM_ADDR_MASK;
+	if (!(prot & IOMMU_PROT_MMIO))
+		pte = __sme_set(pte);
+
 	pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER;
 	pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY;
 
drivers/iommu/amd/iommu.c +2 -0
index 16f40b8000d7..9194ad681504 100644
@@ -2578,6 +2578,8 @@ static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
 		prot |= IOMMU_PROT_IR;
 	if (iommu_prot & IOMMU_WRITE)
 		prot |= IOMMU_PROT_IW;
+	if (iommu_prot & IOMMU_MMIO)
+		prot |= IOMMU_PROT_MMIO;
 
 	if (ops->map_pages) {
 		ret = ops->map_pages(ops, iova, paddr, pgsize,
drivers/vfio/vfio_iommu_type1.c +3 -1
index 50ebc9593c9d..08be1ef8514b 100644
@@ -1557,6 +1557,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		prot |= IOMMU_WRITE;
 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
 		prot |= IOMMU_READ;
+    if (map->flags & VFIO_DMA_MAP_FLAG_MMIO)
+        prot |= IOMMU_MMIO;
 
 	if ((prot && set_vaddr) || (!prot && !set_vaddr))
 		return -EINVAL;
@@ -2801,7 +2803,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
 	struct vfio_iommu_type1_dma_map map;
 	unsigned long minsz;
 	uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
-			VFIO_DMA_MAP_FLAG_VADDR;
+			VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_MMIO;
 
 	minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
include/uapi/linux/vfio.h +1 -0
index c8dbf8219c4f..68002c8f1157 100644
@@ -1560,6 +1560,7 @@ struct vfio_iommu_type1_dma_map {
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)		/* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)	/* writable from device */
 #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+#define VFIO_DMA_MAP_FLAG_MMIO (1 << 3)     /* map of mmio */
 	__u64	vaddr;				/* Process virtual address */
 	__u64	iova;				/* IO virtual address */
 	__u64	size;				/* Size of mapping (bytes) */
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · On Fri, 17 Jan 2025 15:14:18 +0800 Wencheng Yang <[email protected]>…

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Clear memory encryption bit in io page table if the mapping is MMIO rather than memory.

Quoted 8 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 7 ++++---
 drivers/iommu/amd/io_pgtable.c      | 2 ++
 drivers/iommu/amd/io_pgtable_v2.c   | 5 ++++-
 drivers/iommu/amd/iommu.c           | 2 ++
 drivers/vfio/vfio_iommu_type1.c     | 4 +++-
 include/uapi/linux/vfio.h           | 1 +
 6 files changed, 16 insertions(+), 5 deletions(-)

This needs to:

- Be split into separate IOMMU vs VFIO patches
- Consider and consolidate with other IOMMU implementations of the same
- Provide introspection to userspace relative to the availability of the resulting mapping option

It's also not clear to me that the user should be responsible for setting this flag versus something in the VFIO or IOMMU layer. For example what are the implications of the user setting this flag incorrectly (not just failing to set it for MMIO, but using it for RAM)? Thanks,

Alex

Quoted 95 lines
drivers/iommu/amd/amd_iommu_types.h +4 -3
index fdb0357e0bb9..b0f055200cf3 100644
@@ -434,9 +434,10 @@
 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IOMMU_PROT_MASK 0x07
+#define IOMMU_PROT_IR   0x01
+#define IOMMU_PROT_IW   0x02
+#define IOMMU_PROT_MMIO 0x04
 
 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE	(1 << 2)
 
drivers/iommu/amd/io_pgtable.c +2 -0
index f3399087859f..dff887958a56 100644
@@ -373,6 +373,8 @@ static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
 			__pte |= IOMMU_PTE_IR;
 		if (prot & IOMMU_PROT_IW)
 			__pte |= IOMMU_PTE_IW;
+		if (prot & IOMMU_PROT_MMIO)
+			__pte = __sme_clr(__pte);
 
 		for (i = 0; i < count; ++i)
 			pte[i] = __pte;
drivers/iommu/amd/io_pgtable_v2.c +4 -1
index c616de2c5926..55f969727dea 100644
@@ -65,7 +65,10 @@ static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot)
 {
 	u64 pte;
 
-	pte = __sme_set(paddr & PM_ADDR_MASK);
+	pte = paddr & PM_ADDR_MASK;
+	if (!(prot & IOMMU_PROT_MMIO))
+		pte = __sme_set(pte);
+
 	pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER;
 	pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY;
 
drivers/iommu/amd/iommu.c +2 -0
index 16f40b8000d7..9194ad681504 100644
@@ -2578,6 +2578,8 @@ static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
 		prot |= IOMMU_PROT_IR;
 	if (iommu_prot & IOMMU_WRITE)
 		prot |= IOMMU_PROT_IW;
+	if (iommu_prot & IOMMU_MMIO)
+		prot |= IOMMU_PROT_MMIO;
 
 	if (ops->map_pages) {
 		ret = ops->map_pages(ops, iova, paddr, pgsize,
drivers/vfio/vfio_iommu_type1.c +3 -1
index 50ebc9593c9d..08be1ef8514b 100644
@@ -1557,6 +1557,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		prot |= IOMMU_WRITE;
 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
 		prot |= IOMMU_READ;
+    if (map->flags & VFIO_DMA_MAP_FLAG_MMIO)
+        prot |= IOMMU_MMIO;
 
 	if ((prot && set_vaddr) || (!prot && !set_vaddr))
 		return -EINVAL;
@@ -2801,7 +2803,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
 	struct vfio_iommu_type1_dma_map map;
 	unsigned long minsz;
 	uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
-			VFIO_DMA_MAP_FLAG_VADDR;
+			VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_MMIO;
 
 	minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
include/uapi/linux/vfio.h +1 -0
index c8dbf8219c4f..68002c8f1157 100644
@@ -1560,6 +1560,7 @@ struct vfio_iommu_type1_dma_map {
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)		/* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)	/* writable from device */
 #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+#define VFIO_DMA_MAP_FLAG_MMIO (1 << 3)     /* map of mmio */
 	__u64	vaddr;				/* Process virtual address */
 	__u64	iova;				/* IO virtual address */
 	__u64	size;				/* Size of mapping (bytes) */
#2 lore raw
Quoted 4 lines

This needs to:

- Be split into separate IOMMU vs VFIO patches
- Consider and consolidate with other IOMMU implementations of the same

I will do that in the next patch.

Quoted 2 lines

- Provide introspection to userspace relative to the availability of the resulting mapping option

I don't get your meaning, can you expain in detail?

Quoted 5 lines

It's also not clear to me that the user should be responsible for setting this flag versus something in the VFIO or IOMMU layer. For example what are the implications of the user setting this flag incorrectly (not just failing to set it for MMIO, but using it for RAM)?

If user sets this flag to RAM region, it has no effect on the platform that memory
encrytion is disable. If memory encrytion is enabled, then device can't get correct
data from RAM, for example, CPU writes data to RAM that is encrypted by memory controller, but device read the data from RAM as plaintext, but will never leak confidential data.

Thanks,
Wencheng

Quoted 135 lines · Alex Williamson
Quoted 10 lines · On Fri, 17 Jan 2025 15:14:18 +0800 Wencheng Yang <[email protected]>…

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Clear memory encryption bit in io page table if the mapping is MMIO rather than memory.

Quoted 8 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 7 ++++---
 drivers/iommu/amd/io_pgtable.c      | 2 ++
 drivers/iommu/amd/io_pgtable_v2.c   | 5 ++++-
 drivers/iommu/amd/iommu.c           | 2 ++
 drivers/vfio/vfio_iommu_type1.c     | 4 +++-
 include/uapi/linux/vfio.h           | 1 +
 6 files changed, 16 insertions(+), 5 deletions(-)

This needs to:

- Be split into separate IOMMU vs VFIO patches
- Consider and consolidate with other IOMMU implementations of the same
- Provide introspection to userspace relative to the availability of the resulting mapping option

It's also not clear to me that the user should be responsible for setting this flag versus something in the VFIO or IOMMU layer. For example what are the implications of the user setting this flag incorrectly (not just failing to set it for MMIO, but using it for RAM)?

Thanks,

Alex

Quoted 95 lines
drivers/iommu/amd/amd_iommu_types.h +4 -3
index fdb0357e0bb9..b0f055200cf3 100644
@@ -434,9 +434,10 @@
 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IOMMU_PROT_MASK 0x07
+#define IOMMU_PROT_IR   0x01
+#define IOMMU_PROT_IW   0x02
+#define IOMMU_PROT_MMIO 0x04
 
 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE      (1 << 2)
 
drivers/iommu/amd/io_pgtable.c +2 -0
index f3399087859f..dff887958a56 100644
@@ -373,6 +373,8 @@ static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
                      __pte |= IOMMU_PTE_IR;
              if (prot & IOMMU_PROT_IW)
                      __pte |= IOMMU_PTE_IW;
+             if (prot & IOMMU_PROT_MMIO)
+                     __pte = __sme_clr(__pte);
 
              for (i = 0; i < count; ++i)
                      pte[i] = __pte;
drivers/iommu/amd/io_pgtable_v2.c +4 -1
index c616de2c5926..55f969727dea 100644
@@ -65,7 +65,10 @@ static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot)
 {
      u64 pte;
 
-     pte = __sme_set(paddr & PM_ADDR_MASK);
+     pte = paddr & PM_ADDR_MASK;
+     if (!(prot & IOMMU_PROT_MMIO))
+             pte = __sme_set(pte);
+
      pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER;
      pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY;
 
drivers/iommu/amd/iommu.c +2 -0
index 16f40b8000d7..9194ad681504 100644
@@ -2578,6 +2578,8 @@ static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
              prot |= IOMMU_PROT_IR;
      if (iommu_prot & IOMMU_WRITE)
              prot |= IOMMU_PROT_IW;
+     if (iommu_prot & IOMMU_MMIO)
+             prot |= IOMMU_PROT_MMIO;
 
      if (ops->map_pages) {
              ret = ops->map_pages(ops, iova, paddr, pgsize,
drivers/vfio/vfio_iommu_type1.c +3 -1
index 50ebc9593c9d..08be1ef8514b 100644
@@ -1557,6 +1557,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
              prot |= IOMMU_WRITE;
      if (map->flags & VFIO_DMA_MAP_FLAG_READ)
              prot |= IOMMU_READ;
+    if (map->flags & VFIO_DMA_MAP_FLAG_MMIO)
+        prot |= IOMMU_MMIO;
 
      if ((prot && set_vaddr) || (!prot && !set_vaddr))
              return -EINVAL;
@@ -2801,7 +2803,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
      struct vfio_iommu_type1_dma_map map;
      unsigned long minsz;
      uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
-                     VFIO_DMA_MAP_FLAG_VADDR;
+                     VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_MMIO;
 
      minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
include/uapi/linux/vfio.h +1 -0
index c8dbf8219c4f..68002c8f1157 100644
@@ -1560,6 +1560,7 @@ struct vfio_iommu_type1_dma_map {
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)              /* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)     /* writable from device */
 #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+#define VFIO_DMA_MAP_FLAG_MMIO (1 << 3)     /* map of mmio */
      __u64   vaddr;                          /* Process virtual address */
      __u64   iova;                           /* IO virtual address */
      __u64   size;                           /* Size of mapping (bytes) */
#3 lore raw

PATCH v3 1/3 uapi/linux/vfio:Add VFIO_DMA_MAP_FLAG_MMIO flag

The flag will be used by VFIO to map DMA for device MMIO on IOMMU page table.

---
 include/uapi/linux/vfio.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/vfio.h +1 -0
index c8dbf8219c4f..68002c8f1157 100644
@@ -1560,6 +1560,7 @@ struct vfio_iommu_type1_dma_map {
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)		/* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)	/* writable from device */
 #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+#define VFIO_DMA_MAP_FLAG_MMIO (1 << 3)     /* map of mmio */
 	__u64	vaddr;				/* Process virtual address */
 	__u64	iova;				/* IO virtual address */
 	__u64	size;				/* Size of mapping (bytes) */
Signature · 1 line
2.43.0
#4 lore raw

PATCH v3 2/3 vfio/vfio_iommu_type1:convert VFIO_DMA_MAP_FLAG_MMIO to IOMMU_MMIO flag

Refer to commit: 31e6850e0fdb iommu: Add MMIO mapping type (Robin Murphy), IOMMU needs to know the type of mapping to setup IOMMU page table, if the mapping is for device MMIO region, on some platforms, IOMMU page table entry attrs should be different versus regular memory.

---
 drivers/vfio/vfio_iommu_type1.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +3 -1
index 50ebc9593c9d..08be1ef8514b 100644
@@ -1557,6 +1557,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		prot |= IOMMU_WRITE;
 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
 		prot |= IOMMU_READ;
+    if (map->flags & VFIO_DMA_MAP_FLAG_MMIO)
+        prot |= IOMMU_MMIO;
 
 	if ((prot && set_vaddr) || (!prot && !set_vaddr))
 		return -EINVAL;
@@ -2801,7 +2803,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
 	struct vfio_iommu_type1_dma_map map;
 	unsigned long minsz;
 	uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
-			VFIO_DMA_MAP_FLAG_VADDR;
+			VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_MMIO;
 
 	minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH v3 3/3 iommu/amd:Clear encryption bit if the mapping is for device MMIO

In reply to Wencheng Yang

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Clear memory encryption bit in io page table if the mapping is MMIO rather than memory.

---
 drivers/iommu/amd/amd_iommu_types.h | 7 ++++---
 drivers/iommu/amd/io_pgtable.c      | 2 ++
 drivers/iommu/amd/io_pgtable_v2.c   | 5 ++++-
 drivers/iommu/amd/iommu.c           | 2 ++
 4 files changed, 12 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +4 -3
index fdb0357e0bb9..b0f055200cf3 100644
@@ -434,9 +434,10 @@
 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IOMMU_PROT_MASK 0x07
+#define IOMMU_PROT_IR   0x01
+#define IOMMU_PROT_IW   0x02
+#define IOMMU_PROT_MMIO 0x04
 
 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE	(1 << 2)
 
drivers/iommu/amd/io_pgtable.c +2 -0
index f3399087859f..dff887958a56 100644
@@ -373,6 +373,8 @@ static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
 			__pte |= IOMMU_PTE_IR;
 		if (prot & IOMMU_PROT_IW)
 			__pte |= IOMMU_PTE_IW;
+		if (prot & IOMMU_PROT_MMIO)
+			__pte = __sme_clr(__pte);
 
 		for (i = 0; i < count; ++i)
 			pte[i] = __pte;
drivers/iommu/amd/io_pgtable_v2.c +4 -1
index c616de2c5926..55f969727dea 100644
@@ -65,7 +65,10 @@ static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot)
 {
 	u64 pte;
 
-	pte = __sme_set(paddr & PM_ADDR_MASK);
+	pte = paddr & PM_ADDR_MASK;
+	if (!(prot & IOMMU_PROT_MMIO))
+		pte = __sme_set(pte);
+
 	pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER;
 	pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY;
 
drivers/iommu/amd/iommu.c +2 -0
index 16f40b8000d7..9194ad681504 100644
@@ -2578,6 +2578,8 @@ static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
 		prot |= IOMMU_PROT_IR;
 	if (iommu_prot & IOMMU_WRITE)
 		prot |= IOMMU_PROT_IW;
+	if (iommu_prot & IOMMU_MMIO)
+		prot |= IOMMU_PROT_MMIO;
 
 	if (ops->map_pages) {
 		ret = ops->map_pages(ops, iova, paddr, pgsize,
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v3 1/3] uapi/linux/vfio:Add VFIO_DMA_MAP_FLAG_MMIO flag

In reply to Wencheng Yang
Quoted 1 line · Wencheng Yang

The flag will be used by VFIO to map DMA for device MMIO on IOMMU page table.

Definitely not, the kernel needs to know and protect the memory type itself. Userspace cannot be allowed to override things

Jason

#7 lore raw
In reply to Wencheng Yang
Quoted 6 lines · On Tue, 21 Jan 2025 19:07:26 +0800 Wencheng Yang <[email protected]>…
Quoted 4 lines

This needs to:

- Be split into separate IOMMU vs VFIO patches
- Consider and consolidate with other IOMMU implementations of the same

I will do that in the next patch.

Clearly the latter bullet is not considered in the most recent posting.

Quoted 3 lines
Quoted 2 lines

- Provide introspection to userspace relative to the availability of the resulting mapping option

I don't get your meaning, can you expain in detail?

Generally it would be polite to get these sorts of clarifications before spamming the list with another version of the series. Userspace has no ability to determine whether the kernel supports this flag other than trial and error. The ability to determine the kernel support for a new feature is introspection. For example, if QEMU blindly adds the MMIO flag the mapping will fail on older kernels. How does QEMU know whether support for the flag is available on the underlying kernel?

Quoted 5 lines
Quoted 4 lines

It's also not clear to me that the user should be responsible for setting this flag versus something in the VFIO or IOMMU layer. For example what are the implications of the user setting this flag incorrectly (not just failing to set it for MMIO, but using it for RAM)?

Quoted 7 lines

If user sets this flag to RAM region, it has no effect on the platform that memory
encrytion is disable. If memory encrytion is enabled, then device can't get correct
data from RAM, for example, CPU writes data to RAM that is encrypted by memory controller, but device read the data from RAM as plaintext, but will never leak confidential data.

This description is unclear to me. As others have noted, we probably need to look at whether the flag should be automatically applied by the kernel. We certainly know in the vfio IOMMU layer whether we're mapping a page or a pfnmap. In any case, we're in the process of phasing out the vfio type1 IOMMU backend for iommufd, so whatever the implementation, and especially if there's a uapi component, it needs to be implemented in iommufd first. Thanks,

Alex

Quoted 138 lines
Quoted 135 lines · Alex Williamson
Quoted 10 lines · On Fri, 17 Jan 2025 15:14:18 +0800 Wencheng Yang <[email protected]>…

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Clear memory encryption bit in io page table if the mapping is MMIO rather than memory.

Quoted 8 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 7 ++++---
 drivers/iommu/amd/io_pgtable.c      | 2 ++
 drivers/iommu/amd/io_pgtable_v2.c   | 5 ++++-
 drivers/iommu/amd/iommu.c           | 2 ++
 drivers/vfio/vfio_iommu_type1.c     | 4 +++-
 include/uapi/linux/vfio.h           | 1 +
 6 files changed, 16 insertions(+), 5 deletions(-)  

This needs to:

- Be split into separate IOMMU vs VFIO patches
- Consider and consolidate with other IOMMU implementations of the same
- Provide introspection to userspace relative to the availability of the resulting mapping option

It's also not clear to me that the user should be responsible for setting this flag versus something in the VFIO or IOMMU layer. For example what are the implications of the user setting this flag incorrectly (not just failing to set it for MMIO, but using it for RAM)?

Thanks,

Alex

Quoted 95 lines
drivers/iommu/amd/amd_iommu_types.h +4 -3
index fdb0357e0bb9..b0f055200cf3 100644
@@ -434,9 +434,10 @@
 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IOMMU_PROT_MASK 0x07
+#define IOMMU_PROT_IR   0x01
+#define IOMMU_PROT_IW   0x02
+#define IOMMU_PROT_MMIO 0x04
 
 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE      (1 << 2)
 
drivers/iommu/amd/io_pgtable.c +2 -0
index f3399087859f..dff887958a56 100644
@@ -373,6 +373,8 @@ static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
                      __pte |= IOMMU_PTE_IR;
              if (prot & IOMMU_PROT_IW)
                      __pte |= IOMMU_PTE_IW;
+             if (prot & IOMMU_PROT_MMIO)
+                     __pte = __sme_clr(__pte);
 
              for (i = 0; i < count; ++i)
                      pte[i] = __pte;
drivers/iommu/amd/io_pgtable_v2.c +4 -1
index c616de2c5926..55f969727dea 100644
@@ -65,7 +65,10 @@ static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot)
 {
      u64 pte;
 
-     pte = __sme_set(paddr & PM_ADDR_MASK);
+     pte = paddr & PM_ADDR_MASK;
+     if (!(prot & IOMMU_PROT_MMIO))
+             pte = __sme_set(pte);
+
      pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER;
      pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY;
 
drivers/iommu/amd/iommu.c +2 -0
index 16f40b8000d7..9194ad681504 100644
@@ -2578,6 +2578,8 @@ static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
              prot |= IOMMU_PROT_IR;
      if (iommu_prot & IOMMU_WRITE)
              prot |= IOMMU_PROT_IW;
+     if (iommu_prot & IOMMU_MMIO)
+             prot |= IOMMU_PROT_MMIO;
 
      if (ops->map_pages) {
              ret = ops->map_pages(ops, iova, paddr, pgsize,
drivers/vfio/vfio_iommu_type1.c +3 -1
index 50ebc9593c9d..08be1ef8514b 100644
@@ -1557,6 +1557,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
              prot |= IOMMU_WRITE;
      if (map->flags & VFIO_DMA_MAP_FLAG_READ)
              prot |= IOMMU_READ;
+    if (map->flags & VFIO_DMA_MAP_FLAG_MMIO)
+        prot |= IOMMU_MMIO;
 
      if ((prot && set_vaddr) || (!prot && !set_vaddr))
              return -EINVAL;
@@ -2801,7 +2803,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
      struct vfio_iommu_type1_dma_map map;
      unsigned long minsz;
      uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
-                     VFIO_DMA_MAP_FLAG_VADDR;
+                     VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_MMIO;
 
      minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
include/uapi/linux/vfio.h +1 -0
index c8dbf8219c4f..68002c8f1157 100644
@@ -1560,6 +1560,7 @@ struct vfio_iommu_type1_dma_map {
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)              /* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)     /* writable from device */
 #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+#define VFIO_DMA_MAP_FLAG_MMIO (1 << 3)     /* map of mmio */
      __u64   vaddr;                          /* Process virtual address */
      __u64   iova;                           /* IO virtual address */
      __u64   size;                           /* Size of mapping (bytes) */  
#8 lore raw
Quoted 4 lines · Alex Williamson

This description is unclear to me. As others have noted, we probably need to look at whether the flag should be automatically applied by the kernel. We certainly know in the vfio IOMMU layer whether we're mapping a page or a pfnmap.

It is not page or pfnmap.. When vfio is using follow_pte() it should extract information from the PTE and then relay it to the IOMMU. The iommu page table and the CPU page table should have the same PTE flags.

So, a pte that is pgprot_cached() should be IOMMU_CACHE, otherwise IOMMU_MMIO.

The encrypted bit in the PTE should be mapped to some new IOMMU_ENCRYPTED.

I suspect AMD has created a troublesome issue that IOMMU_CACHE conditionally implies encrypted depending on their platform features (meaning cachable decrypted is impossible). Arguably a higher level should be deciding this and the iommu page table code should simply follow IOMMU_ENCRYPTED always.

That might be something for later, but I would note it :\

Quoted 4 lines

In any case, we're in the process of phasing out the vfio type1 IOMMU backend for iommufd, so whatever the implementation, and especially if there's a uapi component, it needs to be implemented in iommufd first.

Since iommufd won't be using follow_pte() it will have to get this meta information from the FD, eg through DMABUf, and there is a huge thread on how to go about doing that..

There should be no uapi component.

Jason

#9 lore raw
In reply to Wencheng Yang
Quoted 5 lines · Wencheng Yang

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

This doesn't seem entirely right to me, the encrypted bit should flow in from the entity doing the map and be based on more detailed knowledge about what is happening.

Not be guessed at inside the iommu.

We have non-encrpyted CPU memory, and (someday) encrypted MMIO.

Jason

#10 lore raw
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Wencheng Yang

When SME is enabled, memory encryption bit is set in IOMMU page table pte entry, it works fine if the pfn of the pte entry is memory. However, if the pfn is MMIO address, for example, map other device's mmio space to its io page table, in such situation, setting memory encryption bit in pte would cause P2P failure.

Quoted 7 lines

This doesn't seem entirely right to me, the encrypted bit should flow in from the entity doing the map and be based on more detailed knowledge about what is happening.

Not be guessed at inside the iommu.

We have non-encrpyted CPU memory, and (someday) encrypted MMIO.

hi Jason

IOMMU shouldn't and can't guess the type of the mapping, e.g. memory or device MMIO,
VFIO passes the info in a flag to IOMMU to setup IO page table entry. There is another Qemu patch which will set the flag.
Qemu path: https://lists.nongnu.org/archive/html/qemu-devel/2025-01/msg02837.html

Thanks,
Wencheng

Quoted 2 lines

Jason