public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, stable@kernel.org
Cc: Justin Forbes <jmforbes@linuxtx.org>,
	Zwane Mwaikambo <zwane@arm.linux.org.uk>,
	"Theodore Ts'o" <tytso@mit.edu>,
	Randy Dunlap <rdunlap@xenotime.net>,
	Dave Jones <davej@redhat.com>,
	Chuck Wolber <chuckw@quantumlinux.com>,
	Chris Wedgwood <reviews@ml.cw.f00f.org>,
	Michael Krufky <mkrufky@linuxtv.org>,
	Chuck Ebbert <cebbert@redhat.com>,
	Domenico Andreoli <cavokz@gmail.com>,
	torvalds@linux-foundation.org, akpm@linux-foundation.org,
	alan@lxorguk.ukuu.org.uk,
	Dave Johnson <djohnson@sw.starentnetworks.com>,
	Ingo Molnar <mingo@elte.hu>, Thomas Gleixner <tglx@linutronix.de>
Subject: [patch 18/19] x86: fix TSC clock source calibration error
Date: Wed, 14 Nov 2007 22:15:25 -0800	[thread overview]
Message-ID: <20071115061525.GS7980@kroah.com> (raw)
In-Reply-To: <20071115061415.GA7980@kroah.com>

[-- Attachment #1: x86-fix-tsc-clock-source-calibration-error.patch --]
[-- Type: text/plain, Size: 3436 bytes --]

-stable review patch.  If anyone has any objections, please let us know.

------------------
From: Dave Johnson <djohnson@sw.starentnetworks.com>

patch edaf420fdc122e7a42326fe39274c8b8c9b19d41 in mainline.

I ran into this problem on a system that was unable to obtain NTP sync
because the clock was running very slow (over 10000ppm slow). ntpd had
declared all of its peers 'reject' with 'peer_dist' reason.

On investigation, the tsc_khz variable was significantly incorrect
causing xtime to run slow.  After a reboot tsc_khz was correct so I
did a reboot test to see how often the problem occurred:

Test was done on a 2000 Mhz Xeon system.  Of 689 reboots, 8 of them
had unacceptable tsc_khz values (>500ppm):

 range of tsc_khz  # of boots  % of boots
 ----------------  ----------  ----------
        < 1999750           0      0.000%
1999750 - 1999800          21      3.048%
1999800 - 1999850         166     24.128%
1999850 - 1999900         241     35.029%
1999900 - 1999950         211     30.669%
1999950 - 2000000          42      6.105%
2000000 - 2000000           0      0.000%
2000050 - 2000100           0      0.000%
                   [...]
2000100 - 2015000           1      0.145%  << BAD
2015000 - 2030000           6      0.872%  << BAD
2030000 - 2045000           1      0.145%  << BAD
2045000 <                   0      0.000%

The worst boot was 2032.577 Mhz, over 1.5% off!

It appears that on rare occasions, mach_countup() is taking longer to
complete than necessary.

I suspect that this is caused by the CPU taking a periodic SMI
interrupt right at the end of the 30ms calibration loop.  This would
cause the loop to delay while the SMI BIOS hander runs. The resulting
TSC value is beyond what it actually should be resulting in a higher
tsc_khz.

The below patch makes native_calculate_cpu_khz() take the best
(shortest duration, lowest khz) run of it's 3 calibration loops.  If a
SMI goes off causing a bad result (long duration, higher khz) it will
be discarded.

With the patch applied, 300 boots of the same system produce good
results:

 range of tsc_khz  # of boots  % of boots
 ----------------  ----------  ----------
        < 1999750           0      0.000%
1999750 - 1999800          30     10.000%
1999800 - 1999850         166     55.333%
1999850 - 1999900          89     29.667%
1999900 - 1999950          15      5.000%
1999950 <                   0      0.000%

Problem was found and tested against 2.6.18.  Patch is against 2.6.22.

Signed-off-by: Dave Johnson <djohnson@sw.starentnetworks.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 arch/i386/kernel/tsc.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

--- a/arch/i386/kernel/tsc.c
+++ b/arch/i386/kernel/tsc.c
@@ -137,7 +137,7 @@ unsigned long native_calculate_cpu_khz(v
 {
 	unsigned long long start, end;
 	unsigned long count;
-	u64 delta64;
+	u64 delta64 = (u64)ULLONG_MAX;
 	int i;
 	unsigned long flags;
 
@@ -149,6 +149,7 @@ unsigned long native_calculate_cpu_khz(v
 		rdtscll(start);
 		mach_countup(&count);
 		rdtscll(end);
+		delta64 = min(delta64, (end - start));
 	}
 	/*
 	 * Error: ECTCNEVERSET
@@ -159,8 +160,6 @@ unsigned long native_calculate_cpu_khz(v
 	if (count <= 1)
 		goto err;
 
-	delta64 = end - start;
-
 	/* cpu freq too fast: */
 	if (delta64 > (1ULL<<32))
 		goto err;

-- 

  parent reply	other threads:[~2007-11-15  6:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20071115054813.977066477@mini.kroah.org>
2007-11-15  6:14 ` [patch 00/19] 2.6.23-stable review, arch specific stuff Greg KH
2007-11-15  6:14   ` [patch 01/19] Fix sparc64 niagara optimized RAID xor asm Greg KH
2007-11-15  6:14   ` [patch 02/19] Fix sparc64 MAP_FIXED handling of framebuffer mmaps Greg KH
2007-11-15  6:14   ` [patch 03/19] MIPS: MT: Fix bug in multithreaded kernels Greg KH
2007-11-15  6:14   ` [patch 04/19] MIPS: R1: Fix hazard barriers to make kernels work on R2 also Greg KH
2007-11-15  6:14   ` [patch 05/19] POWERPC: Fix handling of stfiwx math emulation Greg KH
2007-11-15  6:14   ` [patch 06/19] POWERPC: Make sure to of_node_get() the result of pci_device_to_OF_node() Greg KH
2007-11-15  6:14   ` [patch 07/19] UML - Stop using libc asm/page.h Greg KH
2007-11-15  6:14   ` [patch 08/19] UML - Fix kernel vs libc symbols clash Greg KH
2007-11-15  6:14   ` [patch 09/19] UML - stop using libc asm/user.h Greg KH
2007-11-15  6:14   ` [patch 10/19] UML - kill subprocesses on exit Greg KH
2007-11-15  6:14   ` [patch 11/19] xen: add batch completion callbacks Greg KH
2007-11-15  6:15   ` [patch 12/19] xen: deal with stale cr3 values when unpinning pagetables Greg KH
2007-11-15  6:15   ` [patch 13/19] xen: fix incorrect vcpu_register_vcpu_info hypercall argument Greg KH
2007-11-15  6:15   ` [patch 14/19] xfs: eagerly remove vmap mappings to avoid upsetting Xen Greg KH
2007-11-15  6:15   ` [patch 15/19] x86: fix global_flush_tlb() bug Greg KH
2007-11-15  6:15   ` [patch 16/19] x86 setup: handle boot loaders which set up the stack incorrectly Greg KH
2007-11-15  7:27     ` H. Peter Anvin
2007-11-15 16:42       ` Greg KH
2007-11-15  6:15   ` [patch 17/19] x86 setup: sizeof() is unsigned, unbreak comparisons Greg KH
2007-11-15  6:15   ` Greg KH [this message]
2007-11-15  6:15   ` [patch 19/19] revert "x86_64: allocate sparsemem memmap above 4G" Greg KH

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=20071115061525.GS7980@kroah.com \
    --to=gregkh@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=cavokz@gmail.com \
    --cc=cebbert@redhat.com \
    --cc=chuckw@quantumlinux.com \
    --cc=davej@redhat.com \
    --cc=djohnson@sw.starentnetworks.com \
    --cc=jmforbes@linuxtx.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=mkrufky@linuxtv.org \
    --cc=rdunlap@xenotime.net \
    --cc=reviews@ml.cw.f00f.org \
    --cc=stable@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    --cc=zwane@arm.linux.org.uk \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox