x86/iommu: Drop IOMMU support when cpu doesn't support cx16.
cover x86/iommu: Drop IOMMU support when cpu doesn't support cx16.
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.
Consider disabling IOMMU in case we have IOMMU hardware but no cx16, then cleanup no-cx16 handling logic from VT-d and AMD-Vi drivers.
Teddy Astie (3):
VT-d: Disable IOMMU if cx16 isn't supported
AMD-Vi: Disable IOMMU if cx16 isn't supported
VT-d: Cleanup MAP_SINGLE_DEVICE and related dead code.
xen/drivers/passthrough/amd/iommu_map.c | 43 ++++------ xen/drivers/passthrough/amd/pci_amd_iommu.c | 6 ++ xen/drivers/passthrough/vtd/intremap.c | 67 ++++----------- xen/drivers/passthrough/vtd/iommu.c | 92 +++++++-------------- xen/drivers/passthrough/vtd/vtd.h | 5 +- 5 files changed, 67 insertions(+), 146 deletions(-)
Signature · 9 lines
2.44.0 Teddy Astie | Vates XCP-ng Intern XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 1/3 VT-d: Disable IOMMU if cx16 isn't supported
No hardware has VT-d support while not having cx16 support, consider disabling IOMMU in this case to avoid potentially buggy code.
Now that IOMMU is only enabled if cx16 is supported, drop dead code that handles cases where cx16 isn't supported.
Signed-off-by Teddy Astie <[email protected]>
--- xen/drivers/passthrough/vtd/intremap.c | 67 +++++---------------- xen/drivers/passthrough/vtd/iommu.c | 80 +++++++++----------------- 2 files changed, 41 insertions(+), 106 deletions(-)
xen/drivers/passthrough/vtd/intremap.c +15 -52
@@ -173,47 +173,26 @@ bool __init cf_check intel_iommu_supports_eim(void)* Assume iremap_lock has been acquired. It is to make sure software will not * change the same IRTE behind us. With this assumption, if only high qword or * low qword in IRTE is to be updated, this function's atomic variant can -* present an atomic update to VT-d hardware even when cmpxchg16b -* instruction is not supported. +* present an atomic update to VT-d hardware. */ static void update_irte (struct vtd_iommu *iommu,struct iremap_entry *entry,const struct iremap_entry *new_ire,bool atomic) { -ASSERT (spin_is_locked (&iommu->intremap.lock)); - -if ( cpu_has_cx16 ) - { -__uint128_t ret; -struct iremap_entry old_ire; +__uint128_t ret; +struct iremap_entry old_ire; - old_ire = *entry; - ret =cmpxchg16b (entry, &old_ire, new_ire); +ASSERT (spin_is_locked (&iommu->intremap.lock)); + + old_ire = *entry; + ret =cmpxchg16b (entry, &old_ire, new_ire); -/* -* In the above, we use cmpxchg16 to atomically update the 128-bit -* IRTE, and the hardware cannot update the IRTE behind us, so -* the return value of cmpxchg16 should be the same as old_ire. -* This ASSERT validate it. -*/ -ASSERT (ret == old_ire.val); - } -else - { -/* -* VT-d hardware doesn't update IRTEs behind us, nor the software -* since we hold iremap_lock. If the caller wants VT-d hardware to -* always see a consistent entry, but we can't meet it, a bug will -* be raised. -*/ -if ( entry->lo == new_ire->lo ) -write_atomic (&entry->hi, new_ire->hi); -else if ( entry->hi == new_ire->hi ) -write_atomic (&entry->lo, new_ire->lo); -else if ( !atomic ) - *entry = *new_ire; -else -BUG (); - } +/* +* In the above, we use cmpxchg16 to atomically update the 128-bit +* IRTE, and the hardware cannot update the IRTE behind us, so +* the return value of cmpxchg16 should be the same as old_ire. +* This ASSERT validate it. +*/ +ASSERT (ret == old_ire.val); }/* Mark specified intr remap entry as free */
@@ -394,8 +373,7 @@ static int ioapic_rte_to_remap_entry(struct vtd_iommu *iommu, remap_rte->reserved =0 ;/* Indicate remap format. */ remap_rte->format =1 ; - -/* If cmpxchg16b is not available the caller must mask the IO-APIC pin. */ +update_irte (iommu, iremap_entry, &new_ire, !init && !masked);iommu_sync_cache (iremap_entry,sizeof (*iremap_entry));iommu_flush_iec_index (iommu,0 , index);
@@ -437,21 +415,6 @@ void cf_check io_apic_write_remap_rte(bool masked = true;int rc; -if ( !cpu_has_cx16 ) - { -/* -* Cannot atomically update the IRTE entry: mask the IO-APIC pin to -* avoid interrupts seeing an inconsistent IRTE entry. -*/ - old_rte =__ioapic_read_entry (apic, pin, true); -if ( !old_rte.mask ) - { - masked = false; - old_rte.mask =1 ; -__ioapic_write_entry (apic, pin, true, old_rte); - } - } -/* Not the initializer, for old gcc to cope. */ new_rte.raw = rte;
xen/drivers/passthrough/vtd/iommu.c +26 -54
106 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
Re: [XEN PATCH 1/3] VT-d: Disable IOMMU if cx16 isn't supported
Quoted 1 line · Teddy Astie
No hardware has VT-d support while not having cx16 support, consider disabling IOMMU in this case to avoid potentially buggy code.
Like in patch 2 (which for whatever reason made it through quite a bit earlier), why "consider"? Your change does disable the IOMMU in that case, you don't merely consider doing so.
xen/drivers/passthrough/vtd/intremap.c static int ioapic_rte_to_remap_entry(struct vtd_iommu *iommu, · line 376
Quoted 7 lines
xen/drivers/passthrough/vtd/intremap.c +1
-2@@ -394,8 +373,7 @@ static int ioapic_rte_to_remap_entry(struct vtd_iommu *iommu, remap_rte->reserved =0 ;/* Indicate remap format. */ remap_rte->format =1 ; - -/* If cmpxchg16b is not available the caller must mask the IO-APIC pin. */ +
Please don't introduce lines with trailing blanks.
xen/drivers/passthrough/vtd/iommu.c static int __init cf_check vtd_setup(void) · line 2608
Quoted 12 lines
xen/drivers/passthrough/vtd/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 = -ENOSYS ; + goto error; + }
One thing I forgot to mention when replying to patch 2, applying here
equally: vtd_setup() isn't necessarily the first bit of IOMMU setup done. For x2APIC enabling earlier calls out of x2apic_bsp_setup() exist, which also need taking care of.
Jan
PATCH 2/3 AMD-Vi: Disable IOMMU if cx16 isn't supported
In reply to Teddy AstieNo hardware has VT-d support while not having cx16 support, consider disabling IOMMU in this case to avoid potentially buggy code.
Now that IOMMU is only enabled if cx16 is supported, drop dead code that handles cases where cx16 isn't supported.
Signed-off-by Teddy Astie <[email protected]>
--- xen/drivers/passthrough/amd/iommu_map.c | 43 +++++++-------------- xen/drivers/passthrough/amd/pci_amd_iommu.c | 6 +++ 2 files changed, 21 insertions(+), 28 deletions(-)
xen/drivers/passthrough/amd/iommu_map.c +15 -28
@@ -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,21 @@ 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
@@ -312,6 +312,12 @@ static int __init cf_check iov_detect(void)return -ENODEV ; } +if (unlikely (!cpu_has_cx16) ) + { +printk ("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n" ); +return -ENOSYS ; + } + init_done =1 ;if ( !amd_iommu_perdev_intremap )
Signature · 9 lines
2.44.0 Teddy Astie | Vates XCP-ng Intern XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [XEN PATCH 2/3] AMD-Vi: Disable IOMMU if cx16 isn't supported
Quoted 1 line · Teddy Astie
No hardware has VT-d support while not having cx16 support, consider disabling IOMMU in this case to avoid potentially buggy code.
VT-d? That's Intel, not AMD. Also alongside bare hardware you also want to not completely leave out Xen running virtualized itself.
As a nit, please limit description lines to 80 chars.
xen/drivers/passthrough/amd/pci_amd_iommu.c static int __init cf_check iov_detect(void) · line 318
Quoted 10 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +4
-0@@ -312,6 +312,12 @@ static int __init cf_check iov_detect(void)return -ENODEV ; } +if (unlikely (!cpu_has_cx16) ) + { +printk ("AMD-Vi: CPU doesn't support CMPXCHG16B, disabling\n" ); +return -ENOSYS ;
ENOSYS has a dedicated purpose and hence doesn't fit here. If no better suitable error code can be identified, ENODEV is perhaps what ought to be used.
I'd also like to note that while this says 2/3, I can't spot any other part of such a 3-patch series anywhere. However, in particular
https://lists.xen.org/archives/html/xen-devel/2024-03/threads.html looks to be lagging anyway.
Further please note that maintainership recently has changed some; please make sure you use up-to-date information for composing the Cc: list.
Jan
PATCH 3/3 VT-d: Cleanup MAP_SINGLE_DEVICE and related dead code.
In reply to Teddy AstieThis flag was only used in case cx16 is not available, as those code paths no longer exist, this flag now does basically nothing.
Signed-off-by Teddy Astie <[email protected]>
--- 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
@@ -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
@@ -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