Skip to contentSkip to conversation

KVM: x86: nSVM: disallow userspace setting of MSR_AMD64_TSC_RATIO to non default value when tsc scaling disabled

PATCH Applied Maxim Levitsky 2 participants 2 messages Feb 23, 2022 – Feb 24, 2022

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

PATCH 1/1 KVM: x86: nSVM: disallow userspace setting of MSR_AMD64_TSC_RATIO to non default value when tsc scaling disabled

If nested tsc scaling is disabled, MSR_AMD64_TSC_RATIO should never have non default value.

Due to way nested tsc scaling support was implmented in qemu, it would set this msr to 0 when nested tsc scaling was disabled. Ignore that value for now, as it causes no harm.

Fixes 5228eb96a487 ("KVM: x86: nSVM: implement nested TSC scaling")
---
 arch/x86/kvm/svm/svm.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +17 -2
index 7038c76fa841..b80ad471776f 100644
@@ -2705,8 +2705,23 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 	u64 data = msr->data;
 	switch (ecx) {
 	case MSR_AMD64_TSC_RATIO:
-		if (!msr->host_initiated && !svm->tsc_scaling_enabled)
-			return 1;
+
+		if (!svm->tsc_scaling_enabled) {
+
+			if (!msr->host_initiated)
+				return 1;
+			/*
+			 * In case TSC scaling is not enabled, always
+			 * leave this MSR at the default value.
+			 *
+			 * Due to bug in qemu 6.2.0, it would try to set
+			 * this msr to 0 if tsc scaling is not enabled.
+			 * Ignore this value as well.
+			 */
+			if (data != 0 && data != svm->tsc_ratio_msr)
+				return 1;
+			break;
+		}
 
 		if (data & TSC_RATIO_RSVD)
 			return 1;
Signature · 1 line
2.26.3
maintainer Applied
#1 lore raw
Quoted 8 lines · Maxim Levitsky

If nested tsc scaling is disabled, MSR_AMD64_TSC_RATIO should never have non default value.

Due to way nested tsc scaling support was implmented in qemu, it would set this msr to 0 when nested tsc scaling was disabled. Ignore that value for now, as it causes no harm.

Quoted 7 lines
Fixes 5228eb96a487 ("KVM: x86: nSVM: implement nested TSC scaling")
---
  arch/x86/kvm/svm/svm.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)

Queued, thanks.

Paolo

Quoted 30 lines
arch/x86/kvm/svm/svm.c +17 -2
index 7038c76fa841..b80ad471776f 100644
@@ -2705,8 +2705,23 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  	u64 data = msr->data;
  	switch (ecx) {
  	case MSR_AMD64_TSC_RATIO:
-		if (!msr->host_initiated && !svm->tsc_scaling_enabled)
-			return 1;
+
+		if (!svm->tsc_scaling_enabled) {
+
+			if (!msr->host_initiated)
+				return 1;
+			/*
+			 * In case TSC scaling is not enabled, always
+			 * leave this MSR at the default value.
+			 *
+			 * Due to bug in qemu 6.2.0, it would try to set
+			 * this msr to 0 if tsc scaling is not enabled.
+			 * Ignore this value as well.
+			 */
+			if (data != 0 && data != svm->tsc_ratio_msr)
+				return 1;
+			break;
+		}
 
  		if (data & TSC_RATIO_RSVD)
  			return 1;