Skip to contentSkip to conversation

kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode

PATCH In review Anton Romanov 2 participants 2 messages Feb 16, 2022

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

PATCH v3 1/1 kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode

If vcpu has tsc_always_catchup set each request updates pvclock data. KVM_HC_CLOCK_PAIRING consumers such as ptp_kvm_x86 rely on tsc read on host's side and do hypercall inside pvclock_read_retry loop leading to infinite loop in such situation.

v3:
Removed warn
Changed return code to KVM_EFAULT
v2:
Added warn

---
 arch/x86/kvm/x86.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/x86.c +7 -0
index 7131d735b1ef..d0b31b115922 100644
@@ -8945,6 +8945,13 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
 	if (!kvm_get_walltime_and_clockread(&ts, &cycle))
 		return -KVM_EOPNOTSUPP;
 
+	/*
+	 * When tsc is in permanent catchup mode guests won't be able to use
+	 * pvclock_read_retry loop to get consistent view of pvclock
+	 */
+	if (vcpu->arch.tsc_always_catchup)
+		return -KVM_EFAULT;
+
 	clock_pairing.sec = ts.tv_sec;
 	clock_pairing.nsec = ts.tv_nsec;
 	clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
maintainer
#1 lore raw
Quoted 21 lines · Anton Romanov

If vcpu has tsc_always_catchup set each request updates pvclock data. KVM_HC_CLOCK_PAIRING consumers such as ptp_kvm_x86 rely on tsc read on host's side and do hypercall inside pvclock_read_retry loop leading to infinite loop in such situation.

v3:
Removed warn
Changed return code to KVM_EFAULT
v2:
Added warn

---
  arch/x86/kvm/x86.c | 7 +++++++
  1 file changed, 7 insertions(+)
arch/x86/kvm/x86.c +0 -0
index 7131d735b1ef..d0b31b115922 100644

arch/x86/kvm/x86.c static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, · line 8954

Quoted 10 lines
arch/x86/kvm/x86.c +7 -0
@@ -8945,6 +8945,13 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
  	if (!kvm_get_walltime_and_clockread(&ts, &cycle))
  		return -KVM_EOPNOTSUPP;
 
+	/*
+	 * When tsc is in permanent catchup mode guests won't be able to use
+	 * pvclock_read_retry loop to get consistent view of pvclock
+	 */
+	if (vcpu->arch.tsc_always_catchup)
+		return -KVM_EFAULT;
+

KVM_EFAULT is for invalid addresses. I changed it back to EOPNOTSUPP and queued the patch.

Paolo