Re: [PATCH] Fix TSC calibration issues

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Linus Torvalds
Date: Tuesday, September 2, 2008 - 7:14 pm

On Wed, 3 Sep 2008, Thomas Gleixner wrote:

This is "wrongish".

You really should do the

	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);   
	...
	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);

around the whole loop, because they get more exact with more time inside, 
and they don't improve from looping around.

Also, that code is already _too_ unreadable. How about starting by just 
moving the PIT calibration into a function of its own, like the appended 
patch. And then as a separate patch, improving the heuristics for just the 
PIT calibration.

The others are independent of this issue..

		Linus

---
 arch/x86/kernel/tsc.c |   37 ++++++++++++++++++++-----------------
 1 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 8e786b0..bf7ff5a 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -122,19 +122,9 @@ static u64 tsc_read_refs(u64 *pm, u64 *hpet)
 	return ULLONG_MAX;
 }
 
-/**
- * native_calibrate_tsc - calibrate the tsc on boot
- */
-unsigned long native_calibrate_tsc(void)
+static unsigned long PIT_calibrate_tsc(void)
 {
-	unsigned long flags;
-	u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
-	int hpet = is_hpet_enabled();
-	unsigned int tsc_khz_val = 0;
-
-	local_irq_save(flags);
-
-	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
+	u64 tr1, tr2, delta;
 
 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
 
@@ -145,17 +135,30 @@ unsigned long native_calibrate_tsc(void)
 	while ((inb(0x61) & 0x20) == 0);
 	tr2 = get_cycles();
 
-	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
-
-	local_irq_restore(flags);
-
 	/*
 	 * Preset the result with the raw and inaccurate PIT
 	 * calibration value
 	 */
 	delta = (tr2 - tr1);
 	do_div(delta, 50);
-	tsc_khz_val = delta;
+	return delta;
+}
+
+/**
+ * native_calibrate_tsc - calibrate the tsc on boot
+ */
+unsigned long native_calibrate_tsc(void)
+{
+	unsigned long flags;
+	u64 tsc1, tsc2, pm1, pm2, hpet1, hpet2;
+	int hpet = is_hpet_enabled();
+	unsigned int tsc_khz_val;
+
+	local_irq_save(flags);
+	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
+	tsc_khz_val = PIT_calibrate_tsc();
+	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
+	local_irq_restore(flags);
 
 	/* hpet or pmtimer available ? */
 	if (!hpet && !pm1 && !pm2) {
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Sun Aug 31, 3:54 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 4:14 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Mon Sep 1, 8:37 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Mon Sep 1, 10:44 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 10:49 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 11:31 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 11:42 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 12:08 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 12:10 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Mon Sep 1, 12:36 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 1:07 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 1:09 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Mon Sep 1, 1:23 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 1:45 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 2:30 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 3:02 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 3:16 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 3:33 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 3:56 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 4:16 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Mon Sep 1, 4:24 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 8:18 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Mon Sep 1, 8:35 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Larry Finger, (Mon Sep 1, 9:54 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Andi Kleen, (Mon Sep 1, 11:37 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 5:15 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 5:21 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Tue Sep 2, 8:09 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Bill Davidsen, (Tue Sep 2, 10:17 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 11:14 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Alok Kataria, (Tue Sep 2, 11:41 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Tue Sep 2, 11:42 am)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 2:13 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 2:16 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Tue Sep 2, 3:21 pm)
[PATCH] Fix TSC calibration issues, Thomas Gleixner, (Tue Sep 2, 3:54 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Thomas Gleixner, (Tue Sep 2, 4:10 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Linus Torvalds, (Tue Sep 2, 6:49 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Tue Sep 2, 7:14 pm)
Re: [PATCH] Fix TSC calibration issues, Larry Finger, (Tue Sep 2, 7:51 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Tue Sep 2, 9:00 pm)
Re: [PATCH] Fix TSC calibration issues, Larry Finger, (Tue Sep 2, 9:34 pm)
Re: [PATCH] Fix TSC calibration issues, Thomas Gleixner, (Wed Sep 3, 2:11 am)
Re: [PATCH] Fix TSC calibration issues, Alok Kataria, (Wed Sep 3, 6:14 pm)
[PATCH] Change warning message in TSC calibration., Alok Kataria, (Wed Sep 3, 6:18 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Wed Sep 3, 7:56 pm)
Re: [PATCH] Fix TSC calibration issues, Arjan van de Ven, (Wed Sep 3, 8:16 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Wed Sep 3, 8:59 pm)
Re: [PATCH] Fix TSC calibration issues, Arjan van de Ven, (Wed Sep 3, 9:10 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Wed Sep 3, 9:20 pm)
Re: [PATCH] Fix TSC calibration issues, Willy Tarreau, (Wed Sep 3, 9:25 pm)
Re: [PATCH] Fix TSC calibration issues, Arjan van de Ven, (Wed Sep 3, 9:27 pm)
Re: [PATCH] Fix TSC calibration issues, Linus Torvalds, (Wed Sep 3, 9:53 pm)
Re: [PATCH] Fix TSC calibration issues, Willy Tarreau, (Wed Sep 3, 10:09 pm)
Re: Regression in 2.6.27 caused by commit bfc0f59, Mark Lord, (Fri Sep 5, 6:45 am)