From patchwork Wed Mar 10 19:30:59 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chase Douglas X-Patchwork-Id: 47278 X-Patchwork-Delegate: apw@canonical.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from chlorine.canonical.com (chlorine.canonical.com [91.189.94.204]) by ozlabs.org (Postfix) with ESMTP id 086C9B7CF9 for ; Thu, 11 Mar 2010 06:31:26 +1100 (EST) Received: from localhost ([127.0.0.1] helo=chlorine.canonical.com) by chlorine.canonical.com with esmtp (Exim 4.69) (envelope-from ) id 1NpRcn-0003yj-PC; Wed, 10 Mar 2010 19:31:21 +0000 Received: from adelie.canonical.com ([91.189.90.139]) by chlorine.canonical.com with esmtp (Exim 4.69) (envelope-from ) id 1NpRcm-0003y6-6N for kernel-team@lists.ubuntu.com; Wed, 10 Mar 2010 19:31:20 +0000 Received: from hutte.canonical.com ([91.189.90.181]) by adelie.canonical.com with esmtp (Exim 4.69 #1 (Debian)) id 1NpRcl-0004Qz-M2 for ; Wed, 10 Mar 2010 19:31:19 +0000 Received: from cpe-98-31-46-159.woh.res.rr.com ([98.31.46.159] helo=canonical.com) by hutte.canonical.com with esmtpsa (TLS-1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.69) (envelope-from ) id 1NpRck-0004j9-VO for kernel-team@lists.ubuntu.com; Wed, 10 Mar 2010 19:31:19 +0000 From: Chase Douglas To: kernel-team@lists.ubuntu.com Subject: [PATCH 1/1] Switch to jiffies for native_sched_clock() when TSC warps Date: Wed, 10 Mar 2010 14:30:59 -0500 Message-Id: <1268249459-30306-2-git-send-email-chase.douglas@canonical.com> X-Mailer: git-send-email 1.6.3.3 In-Reply-To: <1268249459-30306-1-git-send-email-chase.douglas@canonical.com> References: <1268249459-30306-1-git-send-email-chase.douglas@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.9 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: kernel-team-bounces@lists.ubuntu.com Errors-To: kernel-team-bounces@lists.ubuntu.com Some newer x86 processors (seen on core 2 duo, arrandale) warp their TSC registers after a suspend. It is believed that a microcode fix may be a solution, but for now we should work around the issue. This change adds an upper bound on the difference between TSC readings. It should be very generous (multiple year difference) so as to only catch TSC warping which appears to generate timestamps many years in the future. When a warp is found, usage of the TSC for timing is disabled. The kernel falls back to using the jiffies counter, which is not as precise but should be accurate. Signed-off-by: Chase Douglas --- arch/x86/kernel/tsc.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 597683a..3e2921d 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -43,7 +43,9 @@ static int tsc_clocksource_reliable; u64 native_sched_clock(void) { u64 this_offset; + static u64 prev_offset; +jiffies: /* * Fall back to jiffies if there's no TSC available: * ( But note that we still use it if the TSC is marked @@ -60,6 +62,19 @@ u64 native_sched_clock(void) /* read the Time Stamp Counter: */ rdtscll(this_offset); + /* + * if new time stamp is many years later, assume warping and disable + * TSC usage: + */ + if (__cycles_2_ns(this_offset - prev_offset) > 0x100000000000000 + && prev_offset) { + printk(KERN_WARNING "TSC warped, using jiffies\n"); + tsc_disabled = 1; + goto jiffies; + } + + prev_offset = this_offset; + /* return the value in ns */ return __cycles_2_ns(this_offset); }