All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@elte.hu>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Thomas Gleixner <tglx@linutronix.de>,
	LKML <linux-kernel@vger.kernel.org>,
	Alok Kataria <akataria@vmware.com>,
	Arjan van de Veen <arjan@infradead.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: Re: [RFC patch 0/4] TSC calibration improvements
Date: Thu, 4 Sep 2008 22:52:36 +0200	[thread overview]
Message-ID: <20080904205236.GA3864@elte.hu> (raw)
In-Reply-To: <20080904204305.GA29065@elte.hu>


> +static unsigned long quick_pit_calibrate(void)
> +{
[...]
> +	if (pit_expect_msb(0xff)) {
> +		int i;
> +		u64 t1, t2, delta;
> +		unsigned char expect = 0xfe;
> +
> +		t1 = get_cycles();
> +		for (i = 0; i < QUICK_PIT_ITERATIONS; i++, expect--) {
> +			if (!pit_expect_msb(expect))
> +				goto failed;
> +		}
> +		t2 = get_cycles();

hm, unless i'm missing something i think here we still have a small 
window for an SMI or some virtualization delay to slip in and cause 
massive inaccuracy: if the delay happens _after_ the last 
pit_expect_msb() and _before_ the external get_cycles() call. Right?

i fixed that by adding one more pit_expect_msb() call.

plus i think QUICK_PIT_ITERATIONS is quite close to overflowing 255 
which is built into the u32 'expect' variable (the MSB will only 
overflow to 10 bits or so) - so i've added a BUILD_BUG_ON() to make sure 
anyone tuning QUICK_PIT_MS above 60msec or so would get a build error 
instead of some hard(er) to track down calibration error.

but it's getting late here so please double-check me ... The commit is 
below.

	Ingo

------------>
>From 40d2650256289d3ba59c4fd146b86b972db6ec40 Mon Sep 17 00:00:00 2001
From: Ingo Molnar <mingo@elte.hu>
Date: Thu, 4 Sep 2008 22:47:47 +0200
Subject: [PATCH] x86: quick TSC calibration, improve

- make sure the final TSC timestamp is reliable too

- make sure nobody increases QUICK_PIT_MS so that
  QUICK_PIT_ITERATIONS can get larger than 0xff, breaking the iteration.
  (It would take about 60 msecs to reach that limit.)

Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
 arch/x86/kernel/tsc.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 839070b..4832a40 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -304,6 +304,11 @@ static unsigned long quick_pit_calibrate(void)
 	outb(0xff, 0x42);
 	outb(0xff, 0x42);
 
+	/*
+	 * The iteration assumes that expect never goes below zero:
+	 */
+	BUILD_BUG_ON(QUICK_PIT_ITERATIONS >= 0xff);
+
 	if (pit_expect_msb(0xff)) {
 		int i;
 		u64 t1, t2, delta;
@@ -317,6 +322,12 @@ static unsigned long quick_pit_calibrate(void)
 		t2 = get_cycles();
 
 		/*
+		 * Make sure we can rely on the second TSC timestamp:
+		 */
+		if (!pit_expect_msb(--expect))
+			goto failed;
+
+		/*
 		 * Ok, if we get here, then we've seen the
 		 * MSB of the PIT decrement QUICK_PIT_ITERATIONS
 		 * times, and each MSB had many hits, so we never

  reply	other threads:[~2008-09-04 20:53 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-04 15:18 [RFC patch 0/4] TSC calibration improvements Thomas Gleixner
2008-09-04 15:18 ` [RFC patch 1/4] x86: TSC: define the PIT latch value separate Thomas Gleixner
2008-09-04 15:18 ` [RFC patch 2/4] x86: TSC: separate hpet/pmtimer calculation out Thomas Gleixner
2008-09-04 15:18 ` [RFC patch 3/4] x86: TSC: use one set of reference variables Thomas Gleixner
2008-09-04 15:18 ` [RFC patch 4/4] x86: TSC make the calibration loop smarter Thomas Gleixner
2008-09-04 15:36 ` [RFC patch 0/4] TSC calibration improvements Ingo Molnar
2008-09-04 15:45   ` Linus Torvalds
2008-09-04 16:00     ` Ingo Molnar
2008-09-04 16:21       ` Linus Torvalds
2008-09-04 16:36         ` Ingo Molnar
2008-09-04 17:41         ` Linus Torvalds
2008-09-04 18:07           ` Alan Cox
2008-09-04 18:26             ` Linus Torvalds
2008-09-04 18:30               ` H. Peter Anvin
2008-09-04 20:09               ` Linus Torvalds
2008-09-04 20:43                 ` Ingo Molnar
2008-09-04 20:52                   ` Ingo Molnar [this message]
2008-09-04 21:09                     ` Linus Torvalds
2008-09-04 21:21                       ` Ingo Molnar
2008-09-04 21:30                         ` Linus Torvalds
2008-09-04 21:34                           ` Linus Torvalds
2008-09-04 21:39                             ` Ingo Molnar
2008-09-04 21:33                       ` Ingo Molnar
2008-09-05 22:18                         ` Alok Kataria
2008-09-05 22:34                           ` Linus Torvalds
2008-09-06 20:03                             ` Thomas Gleixner
2008-09-06 20:29                               ` Linus Torvalds
2008-09-06 20:37                                 ` Thomas Gleixner
2008-09-06 20:50                                   ` Linus Torvalds
2008-09-06 20:55                                     ` Linus Torvalds
2008-09-06 21:15                                       ` Thomas Gleixner
2008-09-06 21:22                                         ` Linus Torvalds
2008-09-06 21:30                                           ` Thomas Gleixner
2008-09-06 22:40                                       ` Ingo Molnar
2008-09-06 20:58                                     ` Thomas Gleixner
2008-09-06 21:10                                       ` Linus Torvalds
2008-09-07  6:01                                         ` Willy Tarreau
2008-09-06 20:52                                   ` Thomas Gleixner
2008-09-06 20:59                                     ` Linus Torvalds
2008-09-06 21:07                                       ` Thomas Gleixner
2008-09-06 21:15                                         ` Linus Torvalds
2008-09-06 21:26                                           ` Thomas Gleixner
2008-09-06 21:32                                             ` Linus Torvalds
2008-09-04 20:53                   ` Linus Torvalds
2008-09-04 21:38                 ` Alok Kataria
2008-09-04 21:52                   ` Linus Torvalds
2008-09-04 22:09                     ` Alok Kataria
2008-09-04 17:39     ` Alok Kataria
2008-09-04 17:53       ` Linus Torvalds
2008-09-04 18:31         ` Alok Kataria
2008-09-04 18:34           ` H. Peter Anvin
2008-09-04 21:00   ` Valdis.Kletnieks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080904205236.GA3864@elte.hu \
    --to=mingo@elte.hu \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akataria@vmware.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=arjan@infradead.org \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.