Josh Boyer 8a56c6c
@@ -, +, @@ 
Josh Boyer 8a56c6c
    VMX: x86: handle host TSC calibration failure
Josh Boyer 8a56c6c
    
Josh Boyer 8a56c6c
    If the host TSC calibration fails, tsc_khz is zero (see tsc_init.c).
Josh Boyer 8a56c6c
    Handle such case properly in KVM (instead of dividing by zero).
Josh Boyer 8a56c6c
    
Josh Boyer 8a56c6c
    https://bugzilla.redhat.com/show_bug.cgi?id=859282
Josh Boyer 8a56c6c
    
Josh Boyer 8a56c6c
    Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Josh Boyer 8a56c6c
    Signed-off-by: Gleb Natapov <gleb@redhat.com>
Josh Boyer 8a56c6c
--- a/arch/x86/kvm/x86.c	
Josh Boyer 8a56c6c
+++ a/arch/x86/kvm/x86.c	
Josh Boyer 8a56c6c
@@ -1079,6 +1079,10 @@ static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
Josh Boyer 8a56c6c
 	u32 thresh_lo, thresh_hi;
Josh Boyer 8a56c6c
 	int use_scaling = 0;
Josh Boyer 8a56c6c
 
Josh Boyer 8a56c6c
+	/* tsc_khz can be zero if TSC calibration fails */
Josh Boyer 8a56c6c
+	if (this_tsc_khz == 0)
Josh Boyer 8a56c6c
+		return;
Josh Boyer 8a56c6c
+
Josh Boyer 8a56c6c
 	/* Compute a scale to convert nanoseconds in TSC cycles */
Josh Boyer 8a56c6c
 	kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Josh Boyer 8a56c6c
 			   &vcpu->arch.virtual_tsc_shift,
Josh Boyer 8a56c6c
@@ -1156,20 +1160,23 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Josh Boyer 8a56c6c
 	ns = get_kernel_ns();
Josh Boyer 8a56c6c
 	elapsed = ns - kvm->arch.last_tsc_nsec;
Josh Boyer 8a56c6c
 
Josh Boyer 8a56c6c
-	/* n.b - signed multiplication and division required */
Josh Boyer 8a56c6c
-	usdiff = data - kvm->arch.last_tsc_write;
Josh Boyer 8a56c6c
+	if (vcpu->arch.virtual_tsc_khz) {
Josh Boyer 8a56c6c
+		/* n.b - signed multiplication and division required */
Josh Boyer 8a56c6c
+		usdiff = data - kvm->arch.last_tsc_write;
Josh Boyer 8a56c6c
 #ifdef CONFIG_X86_64
Josh Boyer 8a56c6c
-	usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Josh Boyer 8a56c6c
+		usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Josh Boyer 8a56c6c
 #else
Josh Boyer 8a56c6c
-	/* do_div() only does unsigned */
Josh Boyer 8a56c6c
-	asm("idivl %2; xor %%edx, %%edx"
Josh Boyer 8a56c6c
-	    : "=A"(usdiff)
Josh Boyer 8a56c6c
-	    : "A"(usdiff * 1000), "rm"(vcpu->arch.virtual_tsc_khz));
Josh Boyer 8a56c6c
+		/* do_div() only does unsigned */
Josh Boyer 8a56c6c
+		asm("idivl %2; xor %%edx, %%edx"
Josh Boyer 8a56c6c
+		: "=A"(usdiff)
Josh Boyer 8a56c6c
+		: "A"(usdiff * 1000), "rm"(vcpu->arch.virtual_tsc_khz));
Josh Boyer 8a56c6c
 #endif
Josh Boyer 8a56c6c
-	do_div(elapsed, 1000);
Josh Boyer 8a56c6c
-	usdiff -= elapsed;
Josh Boyer 8a56c6c
-	if (usdiff < 0)
Josh Boyer 8a56c6c
-		usdiff = -usdiff;
Josh Boyer 8a56c6c
+		do_div(elapsed, 1000);
Josh Boyer 8a56c6c
+		usdiff -= elapsed;
Josh Boyer 8a56c6c
+		if (usdiff < 0)
Josh Boyer 8a56c6c
+			usdiff = -usdiff;
Josh Boyer 8a56c6c
+	} else
Josh Boyer 8a56c6c
+		usdiff = USEC_PER_SEC; /* disable TSC match window below */
Josh Boyer 8a56c6c
 
Josh Boyer 8a56c6c
 	/*
Josh Boyer 8a56c6c
 	 * Special case: TSC write with a small delta (1 second) of virtual