Skip to contentSkip to conversation

x86/vtd: fix EPT page table sharing check

PATCH In review Roger Pau Monne 2 participants 4 messages Apr 17, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vtd: fix EPT page table sharing check

The EPT page tables can be shared with the IOMMU as long as the page sizes supported by EPT are also supported by the IOMMU.

Current code checks that both the IOMMU and EPT support the same page sizes, but this is not strictly required, the IOMMU supporting more page sizes than EPT is fine and shouldn't block page sharing.

This is likely not a common case (IOMMU supporting more page sizes than EPT), but should still be fixed for correctness.

---
 xen/drivers/passthrough/vtd/iommu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +4 -2
index 07d40b37fe..63298d3a99 100644
@@ -1914,8 +1914,10 @@ static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu)
     if ( rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, ept_cap) != 0 ) 
         return 0;
 
-    return (ept_has_2mb(ept_cap) && opt_hap_2mb) == cap_sps_2mb(vtd_cap) &&
-           (ept_has_1gb(ept_cap) && opt_hap_1gb) == cap_sps_1gb(vtd_cap);
+    return ((ept_has_2mb(ept_cap) && opt_hap_2mb) ? cap_sps_2mb(vtd_cap)
+                                                  : true) &&
+           ((ept_has_1gb(ept_cap) && opt_hap_1gb) ? cap_sps_1gb(vtd_cap)
+                                                  : true);
 }
 
 /*
Signature · 1 line
2.26.0
#1 lore raw
Quoted 6 lines · Roger Pau Monne

The EPT page tables can be shared with the IOMMU as long as the page sizes supported by EPT are also supported by the IOMMU.

Current code checks that both the IOMMU and EPT support the same page sizes, but this is not strictly required, the IOMMU supporting more page sizes than EPT is fine and shouldn't block page sharing.

Meaning the check isn't wrong, just too strict. Hence maybe "relax" instead of "fix" in the subject?

Also "... page table sharing."

xen/drivers/passthrough/vtd/iommu.c static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu) · line 1920

Quoted 12 lines
xen/drivers/passthrough/vtd/iommu.c +4 -2
@@ -1914,8 +1914,10 @@ static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu)
     if ( rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, ept_cap) != 0 ) 
         return 0;
 
-    return (ept_has_2mb(ept_cap) && opt_hap_2mb) == cap_sps_2mb(vtd_cap) &&
-           (ept_has_1gb(ept_cap) && opt_hap_1gb) == cap_sps_1gb(vtd_cap);
+    return ((ept_has_2mb(ept_cap) && opt_hap_2mb) ? cap_sps_2mb(vtd_cap)
+                                                  : true) &&
+           ((ept_has_1gb(ept_cap) && opt_hap_1gb) ? cap_sps_1gb(vtd_cap)
+                                                  : true);

I have to admit that I always find it odd to have "true" or "false" as the 2nd or 3rd operand of a ternary. How about simply changing == to <= in the original expression?

Jan

#2 lore raw
Quoted 4 lines · Jan Beulich
Quoted 3 lines · Roger Pau Monne

The EPT page tables can be shared with the IOMMU as long as the page sizes supported by EPT are also supported by the IOMMU.

Quoted 8 lines
Quoted 3 lines

Current code checks that both the IOMMU and EPT support the same page sizes, but this is not strictly required, the IOMMU supporting more page sizes than EPT is fine and shouldn't block page sharing.

Meaning the check isn't wrong, just too strict. Hence maybe "relax" instead of "fix" in the subject?

Also "... page table sharing."

Sure.

Quoted 6 lines
Quoted 6 lines
xen/drivers/passthrough/vtd/iommu.c +0 -0
@@ -1914,8 +1914,10 @@ static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu)
     if ( rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, ept_cap) != 0 ) 
         return 0;
 
Quoted 10 lines

xen/drivers/passthrough/vtd/iommu.c static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu) · line 1920

Quoted 6 lines
xen/drivers/passthrough/vtd/iommu.c +4 -2
-    return (ept_has_2mb(ept_cap) && opt_hap_2mb) == cap_sps_2mb(vtd_cap) &&
-           (ept_has_1gb(ept_cap) && opt_hap_1gb) == cap_sps_1gb(vtd_cap);
+    return ((ept_has_2mb(ept_cap) && opt_hap_2mb) ? cap_sps_2mb(vtd_cap)
+                                                  : true) &&
+           ((ept_has_1gb(ept_cap) && opt_hap_1gb) ? cap_sps_1gb(vtd_cap)
+                                                  : true);

I have to admit that I always find it odd to have "true" or "false" as the 2nd or 3rd operand of a ternary. How about simply changing == to <= in the original expression?

I find it weird to use <= to compare booleans, but I can change it. Let me post a new version with the adjustments to the commit message and this requested change.

Thanks, Roger.

#3 lore raw
Quoted 19 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monne
xen/drivers/passthrough/vtd/iommu.c +0 -0
@@ -1914,8 +1914,10 @@ static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu)
     if ( rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, ept_cap) != 0 ) 
         return 0;
 
Quoted 10 lines

xen/drivers/passthrough/vtd/iommu.c static int __init vtd_ept_page_compatible(struct vtd_iommu *iommu) · line 1920

Quoted 6 lines
xen/drivers/passthrough/vtd/iommu.c +4 -2
-    return (ept_has_2mb(ept_cap) && opt_hap_2mb) == cap_sps_2mb(vtd_cap) &&
-           (ept_has_1gb(ept_cap) && opt_hap_1gb) == cap_sps_1gb(vtd_cap);
+    return ((ept_has_2mb(ept_cap) && opt_hap_2mb) ? cap_sps_2mb(vtd_cap)
+                                                  : true) &&
+           ((ept_has_1gb(ept_cap) && opt_hap_1gb) ? cap_sps_1gb(vtd_cap)
+                                                  : true);

I have to admit that I always find it odd to have "true" or "false" as the 2nd or 3rd operand of a ternary. How about simply changing == to <= in the original expression?

Quoted 1 line

I find it weird to use <= to compare booleans, but I can change it.

In the end Kevin will have to judge what variant to use.

Quoted 2 lines

Let me post a new version with the adjustments to the commit message and this requested change.

Thanks.

Jan