Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp191815imj; Thu, 14 Feb 2019 18:21:07 -0800 (PST) X-Google-Smtp-Source: AHgI3Ibx+AQ+jt4i9NXYLOC4u7mqyMv+V8RHX3bHABRwwl7YDrmyN6iPrv9Vnyd7uUytat0Z5nsj X-Received: by 2002:a63:c345:: with SMTP id e5mr3073494pgd.103.1550197267864; Thu, 14 Feb 2019 18:21:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550197267; cv=none; d=google.com; s=arc-20160816; b=hLELbu5l5n53kdSlJlRqeYOqtZbentxC/phdNK3iIYZ8OXsb2JyiHo7/J9ASIyaIyz Ssfb9xKGYUlJxbHHFYGAMeHZX4PUpwu+Cldoq5iiFnlc/qM4ELUKtCOF8TX+Y3ZNcEFV c7ksm7b3mWmkleeCPy/Gc8DM9vVFEgs7UEtg800MY4rcO11wBHze+Fp8EZtNU/vtpi1b nt/v3Jx6roSmTwrmR8JOs/I4+9eIKGBQ68yg6/SPtRYxXXL351TG6N5ODDabwTLq2PYb UMt3AFbH5nOdDvYJz7aY/qMO+BUqT2X3VIPby/dwUiM6GJ6UQZ9bWyTJvUoW/vHnKfqt GZHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=KQ6N/zdDy1Xxsel8p0jd5gYXlTgUKJI3ZKOkdmxwWgw=; b=gCPyzgCNO1wbQxCgUfTe0Jaru7wxfKjp9eoZqLQCGEevlTKJpkdeXi8SIzgJitHPhW 8wI2p+JXKG1eOWXXigX012bPNpgwGIG2aDMYn9iAe81v5IR5B6MLPsHNFtZouQj3V/qP ivX3Q1GlgfESd2nGrWb0TGT/QQ2LxbdZ13zgLYPeNAaeXcY8TMoS+tKqcF6UltD/WPOw Mt+kr/EGydgWVNyXz9X+IrKD5IVq61xqCxQQwtW8UmV9EuqN2W5SmMhRaOh/L2OmekXM rpTEs5uqr+YxR7S4V8vGNOI9ZwS/xQFT1owhDIurfyxa31Sevjg/7No8C7GabA02htTN Yz5w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 123si4105668pfx.109.2019.02.14.18.20.51; Thu, 14 Feb 2019 18:21:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2503395AbfBNWY6 (ORCPT + 99 others); Thu, 14 Feb 2019 17:24:58 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:51427 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406621AbfBNWY5 (ORCPT ); Thu, 14 Feb 2019 17:24:57 -0500 Received: from p5492e0d8.dip0.t-ipconnect.de ([84.146.224.216] helo=nanos) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1guPQu-0002wd-CC; Thu, 14 Feb 2019 23:24:40 +0100 Date: Thu, 14 Feb 2019 23:24:39 +0100 (CET) From: Thomas Gleixner To: =?ISO-8859-15?Q?Jan_H=2E_Sch=F6nherr?= cc: Borislav Petkov , Ingo Molnar , x86@kernel.org, Paul Menzel , Thomas Lendacky , "H. Peter Anvin" , LKML , Linus Torvalds Subject: Re: [PATCH v2] x86/tsc: Allow quick PIT calibration despite interruptions In-Reply-To: <20190214214608.8672-1-jan@schnhrr.de> Message-ID: References: <20190214214608.8672-1-jan@schnhrr.de> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323329-1302279153-1550183080=:1646" X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-1302279153-1550183080=:1646 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT On Thu, 14 Feb 2019, Jan H. Schönherr wrote: Cc+: Linus (he wrote the original implementation and might have opinions) > Some systems experience regular interruptions (60 Hz SMI?), that prevent > the quick PIT calibration from succeeding: individual interruptions can be > so long, that the PIT MSB is observed to decrement by 2 or 3 instead of 1. > The existing code cannot recover from this. > > The system in question is an AMD Ryzen Threadripper 2950X, microcode > 0x800820b, on an ASRock Fatal1ty X399 Professional Gaming, BIOS P3.30. > > Change the code to handle (almost) arbitrary interruptions, as long > as they happen only once in a while and they do not take too long. > Specifically, also cover an interruption during the very first reads. > > Signed-off-by: Jan H. Schönherr > --- > > v2: > - Dropped the other hacky patch for the time being. > - Fixed the early exit check. > - Hopefully fixed all inaccurate math in v1. > - Extended comments. That looks halfways sane, but I'm way too tired to wrap my head around it right now. Vs. comments: The big comment above pit_verify_msb() needs a big overhaul as well. Thanks, tglx Keeping patch for reference. > arch/x86/kernel/tsc.c | 91 +++++++++++++++++++++++++++---------------- > 1 file changed, 57 insertions(+), 34 deletions(-) > > diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c > index e9f777bfed40..aced427371f7 100644 > --- a/arch/x86/kernel/tsc.c > +++ b/arch/x86/kernel/tsc.c > @@ -485,7 +485,7 @@ static inline int pit_verify_msb(unsigned char val) > static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap) > { > int count; > - u64 tsc = 0, prev_tsc = 0; > + u64 tsc = get_cycles(), prev_tsc = 0; > > for (count = 0; count < 50000; count++) { > if (!pit_verify_msb(val)) > @@ -500,7 +500,7 @@ static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *de > * We require _some_ success, but the quality control > * will be based on the error terms on the TSC values. > */ > - return count > 5; > + return count > 0 && pit_verify_msb(val - 1); > } > > /* > @@ -515,7 +515,8 @@ static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *de > static unsigned long quick_pit_calibrate(void) > { > int i; > - u64 tsc, delta; > + u64 tsc = 0, delta; > + unsigned char start; > unsigned long d1, d2; > > if (!has_legacy_pic()) > @@ -547,43 +548,65 @@ static unsigned long quick_pit_calibrate(void) > */ > pit_verify_msb(0); > > - if (pit_expect_msb(0xff, &tsc, &d1)) { > - for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { > - if (!pit_expect_msb(0xff-i, &delta, &d2)) > - break; > - > - delta -= tsc; > - > - /* > - * Extrapolate the error and fail fast if the error will > - * never be below 500 ppm. > - */ > - if (i == 1 && > - d1 + d2 >= (delta * MAX_QUICK_PIT_ITERATIONS) >> 11) > - return 0; > - > - /* > - * Iterate until the error is less than 500 ppm > - */ > - if (d1+d2 >= delta >> 11) > - continue; > - > - /* > - * Check the PIT one more time to verify that > - * all TSC reads were stable wrt the PIT. > - * > - * This also guarantees serialization of the > - * last cycle read ('d2') in pit_expect_msb. > - */ > - if (!pit_verify_msb(0xfe - i)) > - break; > - goto success; > + /* > + * Reading the PIT may fail or experience unexpected delays (due to > + * SMIs, for example). Assuming, that these underlying interruptions > + * happen only once in a while, we wait for two successful reads. > + * Of these, we assume that the better one was not delayed and use > + * it as the base for later calculations. > + */ > + for (i = 0; i <= MAX_QUICK_PIT_ITERATIONS; i++) { > + if (!pit_expect_msb(0xff - i, &delta, &d2)) > + continue; > + > + if (!tsc) { > + /* first success */ > + start = i; > + tsc = delta; > + d1 = d2; > + continue; > } > + > + /* second success */ > + delta -= tsc; > + do_div(delta, i - start); > + if (d2 < d1) { > + start = i; > + tsc += delta; > + d1 = d2; > + } > + goto calibrate; > + } > + > + pr_info("Fast TSC calibration failed (couldn't even start)\n"); > + return 0; > + > +calibrate: > + /* > + * Extrapolate the error based on the better of the first two successes > + * and fail fast if the error will never be below 500 ppm. > + */ > + if (d1 + d1 >= (delta * MAX_QUICK_PIT_ITERATIONS) >> 11) { > + pr_info("Fast TSC calibration failed (wouldn't work)\n"); > + return 0; > } > + > + for (i++; i <= MAX_QUICK_PIT_ITERATIONS; i++) { > + if (!pit_expect_msb(0xff - i, &delta, &d2)) > + continue; > + > + delta -= tsc; > + > + /* Stop when the error is less than 500 ppm */ > + if (d1 + d2 < delta >> 11) > + goto success; > + } > + > pr_info("Fast TSC calibration failed\n"); > return 0; > > success: > + i -= start; > /* > * Ok, if we get here, then we've seen the > * MSB of the PIT decrement 'i' times, and the > -- > 2.19.2 > > --8323329-1302279153-1550183080=:1646--