Skip to contentSkip to conversation

Fix up calibration refinement conditionals to avoid divide by zero

PATCH In review John Stultz 1 participant 2 messages Jan 14, 2011 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix up calibration refinement conditionals to avoid divide by zero

The conditional (!hpet && !ref_start && !ref_stop) doesn't really make sense. If the refs are null, but hpet is on, we still want to break out.

Additionally, we've seen cases where invalid non-null values are returned from emulated hardware, and this conditional misses those, resulting in a div by zero.

The div by zero would be possible to trigger by chance if both reads from the hardware provided the exact same value (due to hardware wrapping).

So checking if both the ref values are the same should handle if we don't have hardware (both null) or if they are the same value (either by invalid hardware, or by chance), avoiding the div by zero issue.

---
 arch/x86/kernel/tsc.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/tsc.c +1 -1
index c43e182..362ca9a 100644
@@ -935,7 +935,7 @@ static void tsc_refine_calibration_work(struct work_struct *work)
 	tsc_stop = tsc_read_refs(&ref_stop, hpet);
 
 	/* hpet or pmtimer available ? */
-	if (!hpet && !ref_start && !ref_stop)
+	if (ref_start == ref_stop)
 		goto out;
 
 	/* Check, whether the sampling was disturbed by an SMI */
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw

[tip:x86/urgent] x86: tsc: Fix calibration refinement conditionals to avoid divide by zero

Commit-ID:  62627bec8a601c5679bf3d20a2096a1206d61b71
Gitweb:     http://git.kernel.org/tip/62627bec8a601c5679bf3d20a2096a1206d61b71
Author:     John Stultz <[email protected]>
AuthorDate: Fri, 14 Jan 2011 09:06:28 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 14 Jan 2011 18:28:01 +0100

x86: tsc: Fix calibration refinement conditionals to avoid divide by zero

Konrad Wilk reported that the new delayed calibration crashes with a divide by zero on Xen. The reason is that Xen sets the pmtimer address, but reading from it returns 0xffffff. That results in the ref_start and ref_stop value being the same, so the delta is zero which causes the divide by zero later in the calculation.

The conditional (!hpet && !ref_start && !ref_stop) which sanity checks the calibration reference values doesn't really make sense. If the refs are null, but hpet is on, we still want to break out.

The div by zero would be possible to trigger by chance if both reads from the hardware provided the exact same value (due to hardware wrapping).

So checking if both the ref values are the same should handle if we don't have hardware (both null) or if they are the same value (either by invalid hardware, or by chance), avoiding the div by zero issue.

[ tglx: Applied the same fix to native_calibrate_tsc() where this
check was copied from ]

LKML-Reference: <[email protected]>

---
 arch/x86/kernel/tsc.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/tsc.c +2 -2
index 463901e..ae09f97 100644
@@ -464,7 +464,7 @@ unsigned long native_calibrate_tsc(void)
 		tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
 
 		/* hpet or pmtimer available ? */
-		if (!hpet && !ref1 && !ref2)
+		if (ref1 == ref2)
 			continue;
 
 		/* Check, whether the sampling was disturbed by an SMI */
@@ -935,7 +935,7 @@ static void tsc_refine_calibration_work(struct work_struct *work)
 	tsc_stop = tsc_read_refs(&ref_stop, hpet);
 
 	/* hpet or pmtimer available ? */
-	if (!hpet && !ref_start && !ref_stop)
+	if (ref_start == ref_stop)
 		goto out;
 
 	/* Check, whether the sampling was disturbed by an SMI */