Skip to contentSkip to conversation

KVM: x86: reinstate vendor-agnostic check on SPEC_CTRL cpuid bits

PATCH In review Paolo Bonzini 3 participants 4 messages Dec 3, 2020 – Dec 8, 2020

Subsystems (4)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: reinstate vendor-agnostic check on SPEC_CTRL cpuid bits

Until commit e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP", 2018-05-17), KVM was testing both Intel and AMD CPUID bits before allowing the guest to write MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD. Testing only Intel bits on VMX processors, or only AMD bits on SVM processors, fails if the guests are created with the "opposite" vendor as the host.

While at it, also tweak the host CPU check to use the vendor-agnostic feature bit X86_FEATURE_IBPB, since we only care about the availability of the MSR on the host here and not about specific CPUID bits.

Fixes e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP")
---
 arch/x86/kvm/svm/svm.c |  3 ++-
 arch/x86/kvm/vmx/vmx.c | 10 +++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +2 -1
index 62390fbc9233..0b4aa60b2754 100644
@@ -2686,12 +2686,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		break;
 	case MSR_IA32_PRED_CMD:
 		if (!msr->host_initiated &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_AMD_IBPB))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;
arch/x86/kvm/vmx/vmx.c +7 -3
index c3441e7e5a87..b74d2105ced7 100644
@@ -2028,7 +2028,10 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+                    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
 			return 1;
 
 		if (kvm_spec_ctrl_test_value(data))
@@ -2063,12 +2066,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		goto find_uret_msr;
 	case MSR_IA32_PRED_CMD:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;
Signature · 1 line
2.26.2
#1 lore raw

arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

Quoted 43 lines · Paolo Bonzini

Until commit e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP", 2018-05-17), KVM was testing both Intel and AMD CPUID bits before allowing the guest to write MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD. Testing only Intel bits on VMX processors, or only AMD bits on SVM processors, fails if the guests are created with the "opposite" vendor as the host.

While at it, also tweak the host CPU check to use the vendor-agnostic feature bit X86_FEATURE_IBPB, since we only care about the availability of the MSR on the host here and not about specific CPUID bits.

Fixes e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP")
---
 arch/x86/kvm/svm/svm.c |  3 ++-
 arch/x86/kvm/vmx/vmx.c | 10 +++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +2 -1
index 62390fbc9233..0b4aa60b2754 100644
@@ -2686,12 +2686,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		break;
 	case MSR_IA32_PRED_CMD:
 		if (!msr->host_initiated &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_AMD_IBPB))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;
arch/x86/kvm/vmx/vmx.c +0 -0
index c3441e7e5a87..b74d2105ced7 100644
@@ -2028,7 +2028,10 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

Doesn't vmx_get_msr() also need to be changed for SPEC_CTRL? Assuming that's the case, adding helpers in cpuid.h to detect guest support for SPEC_CTRL (and maybe for PRED_CMD?) would be helpful. It'd reduce duplicate code and document that KVM allows cross-vendor emulation. The condition for SPEC_CTRL support is especially long.

arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2034

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +4 -1
 		break;
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+                    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))

Whitespace is messed up.

ERROR: code indent should use tabs where possible
#54: FILE: arch/x86/kvm/vmx/vmx.c:2031:
+ !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&$

WARNING: please, no spaces at the start of a line
#54: FILE: arch/x86/kvm/vmx/vmx.c:2031:
+ !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&$

ERROR: code indent should use tabs where possible
#55: FILE: arch/x86/kvm/vmx/vmx.c:2032:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&$

WARNING: please, no spaces at the start of a line
#55: FILE: arch/x86/kvm/vmx/vmx.c:2032:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&$

ERROR: code indent should use tabs where possible
#56: FILE: arch/x86/kvm/vmx/vmx.c:2033:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&$

WARNING: please, no spaces at the start of a line
#56: FILE: arch/x86/kvm/vmx/vmx.c:2033:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&$

ERROR: code indent should use tabs where possible
#57: FILE: arch/x86/kvm/vmx/vmx.c:2034:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))$

WARNING: please, no spaces at the start of a line
#57: FILE: arch/x86/kvm/vmx/vmx.c:2034:
+ !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))$

Quoted 22 lines
arch/x86/kvm/vmx/vmx.c +3 -2
 			return 1;
 
 		if (kvm_spec_ctrl_test_value(data))
@@ -2063,12 +2066,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		goto find_uret_msr;
 	case MSR_IA32_PRED_CMD:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;
Signature · 1 line
2.26.2
author maintainer
#2 lore raw
Quoted 5 lines · Sean Christopherson

Assuming that's
the case, adding helpers in cpuid.h to detect guest support for SPEC_CTRL (and maybe for PRED_CMD?) would be helpful. It'd reduce duplicate code and document that KVM allows cross-vendor emulation. The condition for SPEC_CTRL support is especially long.

That's a great idea nevertheless.

Paolo

Reviewed-by on v1 1/1
#3 lore raw
In reply to Paolo Bonzini
Quoted 60 lines · Paolo Bonzini

Until commit e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP", 2018-05-17), KVM was testing both Intel and AMD CPUID bits before allowing the guest to write MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD. Testing only Intel bits on VMX processors, or only AMD bits on SVM processors, fails if the guests are created with the "opposite" vendor as the host.

While at it, also tweak the host CPU check to use the vendor-agnostic feature bit X86_FEATURE_IBPB, since we only care about the availability of the MSR on the host here and not about specific CPUID bits.

Fixes e7c587da1252 ("x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP")
---
 arch/x86/kvm/svm/svm.c |  3 ++-
 arch/x86/kvm/vmx/vmx.c | 10 +++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +2 -1
index 62390fbc9233..0b4aa60b2754 100644
@@ -2686,12 +2686,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		break;
 	case MSR_IA32_PRED_CMD:
 		if (!msr->host_initiated &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_AMD_IBPB))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;
arch/x86/kvm/vmx/vmx.c +5 -2
index c3441e7e5a87..b74d2105ced7 100644
@@ -2028,7 +2028,10 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+                    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
+                    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
 			return 1;
 
 		if (kvm_spec_ctrl_test_value(data))
@@ -2063,12 +2066,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		goto find_uret_msr;
 	case MSR_IA32_PRED_CMD:
 		if (!msr_info->host_initiated &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&

arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2078

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2 -1
+		    !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
 			return 1;
 
 		if (data & ~PRED_CMD_IBPB)
 			return 1;
-		if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
+		if (!boot_cpu_has(X86_FEATURE_IBPB))
 			return 1;
 		if (!data)
 			break;