Skip to contentSkip to conversation

x86/iommu: Drop IOMMU support when cx16 isn't supported

PATCH In review Teddy Astie 3 participants 10 messages Apr 18, 2024 – Jan 21, 2025

lore
Share & tools
lore
#0 lore raw

v5 cover x86/iommu: Drop IOMMU support when cx16 isn't supported

All hardware that supports VT-d/AMD-Vi that exists also supports cx16 (aside specifically crafted virtual machines).

Some IOMMU code paths in Xen consider cases where VT-d/AMD-Vi is supported while cx16 isn't, those paths may be bugged and are barely tested, dead code in practice.

Disable IOMMU in case we have IOMMU hardware but no cx16, then cleanup no-cx16 handling logic from VT-d and AMD-Vi drivers. Also disable interrupt remapping that also relies on cx16.

Teddy

---

Changed in v2:

* Added cleanup no-cx16 code for x2APIC
* Fixed commit and code formatting
* Added missing Suggested-by note

Changed in v3:

* Use -ENODEV instead of -ENOSYS.

Changed in v4:

* Reworded "Disable IOMMU if cx16 isn't supported"
* Moved interrupt remapping cleanup in separate patches
* Check cx16 for interrupt remapping in driver's callbacks rather than
in x2apic_bsp_setup

Changed in v5:

* Folded VT-d/AMD-Vi cx16 cleanup patches

Teddy Astie (3):
x86/iommu: Disable IOMMU if cx16 isn't supported
VT-d: Cleanup MAP_SINGLE_DEVICE and related code x86/iommu: Disable intrerrupt remapping if cx16 is not supported

 xen/drivers/passthrough/amd/iommu_intr.c    |  6 ++
 xen/drivers/passthrough/amd/iommu_map.c     | 42 ++++------
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  6 ++
 xen/drivers/passthrough/vtd/intremap.c      | 70 +++++-----------
 xen/drivers/passthrough/vtd/iommu.c         | 92 +++++++--------------
 xen/drivers/passthrough/vtd/vtd.h           |  5 +-
 6 files changed, 77 insertions(+), 144 deletions(-)
Signature · 9 lines
2.44.0



Teddy Astie | Vates XCP-ng Intern

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

PATCH v5 1/3 x86/iommu: Disable IOMMU if cx16 isn't supported

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

---
 xen/drivers/passthrough/amd/iommu_map.c     | 42 ++++--------
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  6 ++
 xen/drivers/passthrough/vtd/iommu.c         | 73 +++++++--------------
 3 files changed, 45 insertions(+), 76 deletions(-)
xen/drivers/passthrough/amd/iommu_map.c +14 -28
index e0f4fe736a..f67975e700 100644
@@ -167,15 +167,14 @@ int amd_iommu_set_root_page_table(struct amd_iommu_dte *dte,
 {
     bool valid = flags & SET_ROOT_VALID;
 
-    if ( dte->v && dte->tv &&
-         (cpu_has_cx16 || (flags & SET_ROOT_WITH_UNITY_MAP)) )
+    if ( dte->v && dte->tv )
     {
         union {
             struct amd_iommu_dte dte;
             uint64_t raw64[4];
             __uint128_t raw128[2];
         } ldte = { .dte = *dte };
-        __uint128_t old = ldte.raw128[0];
+        __uint128_t res, old = ldte.raw128[0];
         int ret = 0;
 
         ldte.dte.domain_id = domain_id;
@@ -185,33 +184,20 @@ int amd_iommu_set_root_page_table(struct amd_iommu_dte *dte,
         ldte.dte.paging_mode = paging_mode;
         ldte.dte.v = valid;
 
-        if ( cpu_has_cx16 )
-        {
-            __uint128_t res = cmpxchg16b(dte, &old, &ldte.raw128[0]);
+        res = cmpxchg16b(dte, &old, &ldte.raw128[0]);
 
-            /*
-             * Hardware does not update the DTE behind our backs, so the
-             * return value should match "old".
-             */
-            if ( res != old )
-            {
-                printk(XENLOG_ERR
-                       "Dom%d: unexpected DTE %016lx_%016lx (expected %016lx_%016lx)\n",
-                       domain_id,
-                       (uint64_t)(res >> 64), (uint64_t)res,
-                       (uint64_t)(old >> 64), (uint64_t)old);
-                ret = -EILSEQ;
-            }
-        }
-        else /* Best effort, updating domain_id last. */
+        /*
+         * Hardware does not update the DTE behind our backs, so the
+         * return value should match "old".
+         */
+        if ( res != old )
         {
-            uint64_t *ptr = (void *)dte;
-
-            write_atomic(ptr + 0, ldte.raw64[0]);
-            /* No barrier should be needed between these two. */
-            write_atomic(ptr + 1, ldte.raw64[1]);
-
-            ret = 1;
+            printk(XENLOG_ERR
+                   "Dom%d: unexpected DTE %016lx_%016lx (expected %016lx_%016lx)\n",
+                   domain_id,
+                   (uint64_t)(res >> 64), (uint64_t)res,
+                   (uint64_t)(old >> 64), (uint64_t)old);
+            ret = -EILSEQ;
         }
 
         return ret;
xen/drivers/passthrough/amd/pci_amd_iommu.c +6 -0
index f6efd88e36..3a6a23f706 100644
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;
 
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }
+
     if ( (init_done ? amd_iommu_init_late()
                     : amd_iommu_init(false)) != 0 )
     {
xen/drivers/passthrough/vtd/iommu.c +25 -48

93 lines not on this page · raw message

Signature · 9 lines
2.44.0



Teddy Astie | Vates XCP-ng Intern

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#2 lore raw

Re: [XEN PATCH v5 1/3] x86/iommu: Disable IOMMU if cx16 isn't supported

Quoted 3 lines · Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

As before: What about Xen itself running virtualized, and the underlying hypervisor surfacing an IOMMU but not CX16? It may be okay to ignore the IOMMU in such an event, but by not mentioning the case you give the appearance of not having considered it at all.

Quoted 5 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +0 -0
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 316

Quoted 10 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +6 -0
 
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }
+
     if ( (init_done ? amd_iommu_init_late()
                     : amd_iommu_init(false)) != 0 )
     {

I did previously point out (and that's even visible in patch context here) that the per-vendor .setup() hooks aren't necessarily the first thing to run. Please can you make sure you address (verbally or by code) prior to submitting new versions?

Jan

#3 lore raw

Re: [XEN PATCH v5 1/3] x86/iommu: Disable IOMMU if cx16 isn't supported

Quoted 16 lines · Jan Beulich
Quoted 3 lines · Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

As before: What about Xen itself running virtualized, and the underlying hypervisor surfacing an IOMMU but not CX16? It may be okay to ignore the IOMMU in such an event, but by not mentioning the case you give the appearance of not having considered it at all.

Quoted 6 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +0 -0
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;
 
Quoted 14 lines

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 316

Quoted 9 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +6 -0
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }
+
     if ( (init_done ? amd_iommu_init_late()
                     : amd_iommu_init(false)) != 0 )
     {

I did previously point out (and that's even visible in patch context here) that the per-vendor .setup() hooks aren't necessarily the first thing to run. Please can you make sure you address (verbally or by code) prior to submitting new versions?

I've re-visiting this as part of my other IOMMU IRTE atomic update fix.

Would you prefer the check for CX16 be in the common x86 iommu_hardware_setup()? That would be kind of layering violation, as in principle a further IOMMU implementation on x86 might not require CX16. However I find it very unlikely, and hence I would be fine in placing the check in iommu_hardware_setup() if you prefer it there.

Thanks, Roger.

#4 lore raw

Re: [XEN PATCH v5 1/3] x86/iommu: Disable IOMMU if cx16 isn't supported

Quoted 32 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 3 lines · Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

As before: What about Xen itself running virtualized, and the underlying hypervisor surfacing an IOMMU but not CX16? It may be okay to ignore the IOMMU in such an event, but by not mentioning the case you give the appearance of not having considered it at all.

Quoted 6 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +0 -0
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;
 
Quoted 14 lines

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 316

Quoted 9 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +6 -0
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }
+
     if ( (init_done ? amd_iommu_init_late()
                     : amd_iommu_init(false)) != 0 )
     {

I did previously point out (and that's even visible in patch context here) that the per-vendor .setup() hooks aren't necessarily the first thing to run. Please can you make sure you address (verbally or by code) prior to submitting new versions?

Quoted 8 lines

I've re-visiting this as part of my other IOMMU IRTE atomic update fix.

Would you prefer the check for CX16 be in the common x86 iommu_hardware_setup()? That would be kind of layering violation, as in principle a further IOMMU implementation on x86 might not require CX16. However I find it very unlikely, and hence I would be fine in placing the check in iommu_hardware_setup() if you prefer it there.

No. The check needs replicating into the other hook that may run first, ->supports_x2apic(). And the ->enable_x2apic() hooks may want to gain respective assertions then.

Jan

#5 lore raw

Re: [XEN PATCH v5 1/3] x86/iommu: Disable IOMMU if cx16 isn't supported

Quoted 42 lines · Jan Beulich
Quoted 32 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 3 lines · Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

As before: What about Xen itself running virtualized, and the underlying hypervisor surfacing an IOMMU but not CX16? It may be okay to ignore the IOMMU in such an event, but by not mentioning the case you give the appearance of not having considered it at all.

Quoted 6 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +0 -0
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;
 
Quoted 14 lines

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 316

Quoted 9 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +6 -0
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }
+
     if ( (init_done ? amd_iommu_init_late()
                     : amd_iommu_init(false)) != 0 )
     {

I did previously point out (and that's even visible in patch context here) that the per-vendor .setup() hooks aren't necessarily the first thing to run. Please can you make sure you address (verbally or by code) prior to submitting new versions?

Quoted 8 lines

I've re-visiting this as part of my other IOMMU IRTE atomic update fix.

Would you prefer the check for CX16 be in the common x86 iommu_hardware_setup()? That would be kind of layering violation, as in principle a further IOMMU implementation on x86 might not require CX16. However I find it very unlikely, and hence I would be fine in placing the check in iommu_hardware_setup() if you prefer it there.

Quoted 3 lines

No. The check needs replicating into the other hook that may run first, ->supports_x2apic(). And the ->enable_x2apic() hooks may want to gain respective assertions then.

Oh, I see. So you either want patch 3 ahead of this, or both patches merged into a single one.

Thanks, Roger.

#6 lore raw

Re: [XEN PATCH v5 1/3] x86/iommu: Disable IOMMU if cx16 isn't supported

In reply to Teddy Astie

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 312

Quoted 11 lines · Teddy Astie
xen/drivers/passthrough/amd/pci_amd_iommu.c +5 -0
@@ -305,6 +305,12 @@ static int __init cf_check iov_detect(void)
     if ( !iommu_enable && !iommu_intremap )
         return 0;
 
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n");
+        return -ENODEV;
+    }

One additional nit: Here you neatly have just AMD-Vi: as a prefix for the log message. Why ...

xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check vtd_setup(void) · line 2608

Quoted 12 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -0
@@ -2630,6 +2598,15 @@ static int __init cf_check vtd_setup(void)
     int ret;
     bool reg_inval_supported = true;
 
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        printk(XENLOG_ERR VTDPREFIX
+               "IOMMU: CPU doesn't support CMPXCHG16B, disabling\n");
+
+        ret = -ENODEV;
+        goto error;
+    }

... both VTDPREFIX and "IOMMU:" here?

Jan

#7 lore raw

PATCH v5 2/3 VT-d: Cleanup MAP_SINGLE_DEVICE and related code

In reply to Teddy Astie

This flag was only used in case cx16 is not available, as those code paths no longer exist, this flag now does basically nothing.

---
 xen/drivers/passthrough/vtd/iommu.c | 12 +++---------
 xen/drivers/passthrough/vtd/vtd.h   |  5 ++---
 2 files changed, 5 insertions(+), 12 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +3 -9
index 9c787ba9eb..7c6bae0256 100644
@@ -1692,15 +1692,9 @@ static int domain_context_mapping(struct domain *domain, u8 devfn,
         break;
     }
 
-    if ( domain != pdev->domain && pdev->domain != dom_io )
-    {
-        if ( pdev->domain->is_dying )
-            mode |= MAP_OWNER_DYING;
-        else if ( drhd &&
-                  !any_pdev_behind_iommu(pdev->domain, pdev, drhd->iommu) &&
-                  !pdev->phantom_stride )
-            mode |= MAP_SINGLE_DEVICE;
-    }
+    if ( domain != pdev->domain && pdev->domain != dom_io &&
+         pdev->domain->is_dying )
+        mode |= MAP_OWNER_DYING;
 
     switch ( pdev->type )
     {
xen/drivers/passthrough/vtd/vtd.h +2 -3
index cb2df76eed..43f06a353d 100644
@@ -28,9 +28,8 @@
  */
 #define MAP_WITH_RMRR         (1u << 0)
 #define MAP_OWNER_DYING       (1u << 1)
-#define MAP_SINGLE_DEVICE     (1u << 2)
-#define MAP_ERROR_RECOVERY    (1u << 3)
-#define UNMAP_ME_PHANTOM_FUNC (1u << 4)
+#define MAP_ERROR_RECOVERY    (1u << 2)
+#define UNMAP_ME_PHANTOM_FUNC (1u << 3)
 
 /* Allow for both IOAPIC and IOSAPIC. */
 #define IO_xAPIC_route_entry IO_APIC_route_entry
Signature · 9 lines
2.44.0



Teddy Astie | Vates XCP-ng Intern

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#8 lore raw

PATCH v5 3/3 x86/iommu: Disable intrerrupt remapping if cx16 is not supported

In reply to Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

---
 xen/drivers/passthrough/amd/iommu_intr.c |  6 ++
 xen/drivers/passthrough/vtd/intremap.c   | 70 +++++++-----------------
 xen/drivers/passthrough/vtd/iommu.c      |  7 +--
 3 files changed, 27 insertions(+), 56 deletions(-)
xen/drivers/passthrough/amd/iommu_intr.c +6 -0
index 7fc796dec2..9ab7c68749 100644
@@ -649,6 +649,12 @@ bool __init cf_check iov_supports_xt(void)
     if ( !iommu_enable || !iommu_intremap )
         return false;
 
+    if ( unlikely(!cpu_has_cx16) )
+    {
+        AMD_IOMMU_WARN("CPU doesn't support CMPXCHG16B, disable interrupt remapping\n");
+        return false;
+    }
+
     if ( amd_iommu_prepare(true) )
         return false;
 
xen/drivers/passthrough/vtd/intremap.c +20 -50

101 lines not on this page · raw message

xen/drivers/passthrough/vtd/iommu.c +1 -6
index 7c6bae0256..a1bd3c5ff6 100644
@@ -2663,12 +2663,7 @@ static int __init cf_check vtd_setup(void)
             iommu_intremap = iommu_intremap_off;
 
 #ifndef iommu_intpost
-        /*
-         * We cannot use posted interrupt if X86_FEATURE_CX16 is
-         * not supported, since we count on this feature to
-         * atomically update 16-byte IRTE in posted format.
-         */
-        if ( !cap_intr_post(iommu->cap) || !iommu_intremap || !cpu_has_cx16 )
+        if ( !cap_intr_post(iommu->cap) || !iommu_intremap )
             iommu_intpost = false;
 #endif
 
Signature · 9 lines
2.44.0



Teddy Astie | Vates XCP-ng Intern

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#9 lore raw

Re: [XEN PATCH v5 3/3] x86/iommu: Disable intrerrupt remapping if cx16 is not supported

Quoted 6 lines · Teddy Astie

All hardware with VT-d/AMD-Vi has CMPXCHG16B support. Check this at initialisation time, and remove the effectively-dead logic for the non-cx16 case.

Hmm, so this looks to be the code that I commented on as missing in patch 1. I don't think this can be two separate patches. If you want to keep things in small chunks, move the removal of "the effectively-dead logic" to a separate, follow-on patch.

Jan