Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp967077imd; Thu, 1 Nov 2018 08:16:41 -0700 (PDT) X-Google-Smtp-Source: AJdET5effaC9RJuk4aAjlmg7juD3rfVA12ORHqR9397YClSc6uK+txudSoYHK4WMd7ESUzraOiUR X-Received: by 2002:a63:396:: with SMTP id 144mr7644137pgd.68.1541085401449; Thu, 01 Nov 2018 08:16:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541085401; cv=none; d=google.com; s=arc-20160816; b=OWiNuZkq1KL8IIO97shB+0qrT8B5bpd3iaZIJf3WVDx6W8P4uACK+zqzBBRGs24DjX M+ZnB04hlOLIEGvBethao1Vu8z3MVLMs2BcMYXjzpNtpM5BpDWBAs9c3bgrj6DBd+zxA yFBLbrz6BND3zGf0lQiBLx5NFMIzeq2jncZfykcWlJKAiZQ8VG6fCddOrz4W5q3ulJfZ 39Luyz+w0+GCmZ9mozha+7YR4UM+E0KLIs486EquFKvUnu8iuJ1j6iuwWojkSQzhqP+8 2rK5kQ6TmMhCTVh0QGLrLvvBqq/E72UtFbPSZhVX3YZz8JtmpDzu/oSRgYy6tScpbe02 KniQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=Ri0Hghpbkp508ZVagZwZQbRsOULMwyAu7DQlAikPeg4=; b=apwo7ycOACRs0K0usyh0w6tLRIzpuy4JbdpAoTzeGPEann1vtmW/46Jay2ZjXFNzUQ Jci8FX0bp4/VNtE3U3rn27cefbVOeMSNg6lsfpkm+x+UvM9GNwX8atmbaCbl3AIhz3wh ePZta+eM3UI009z6FU2jkomrWSOjPWACKaan3PryVQk5EywrqGdY+r+S5DZa/O7Gayan /T8500wn43OVWZn+5VWAbauD6LXKZhFuBAItuldhVN01kwlWhj5AdqJRNsp8mlQwlzXX 5Gq65UBrZP9POHKZ+QTOhMSv8NGeHC6nYV4KuQvHDc0acCXeLjkWVxRmWd3tN62feMV1 K5pg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o22-v6si27348591pfi.279.2018.11.01.08.16.25; Thu, 01 Nov 2018 08:16:41 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728978AbeKBAQg (ORCPT + 99 others); Thu, 1 Nov 2018 20:16:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33170 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728372AbeKBAQf (ORCPT ); Thu, 1 Nov 2018 20:16:35 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A17C03086252; Thu, 1 Nov 2018 15:13:12 +0000 (UTC) Received: from slurm.brq.redhat.com (dhcp-27-164.brq.redhat.com [10.34.27.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29D4E101E597; Thu, 1 Nov 2018 15:13:04 +0000 (UTC) From: Daniel Vacek To: x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , linux-kernel@vger.kernel.org, Daniel Vacek Subject: [PATCH] tsc: make calibration refinement more robust Date: Thu, 1 Nov 2018 16:12:13 +0100 Message-Id: <1541085133-32534-1-git-send-email-neelx@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 01 Nov 2018 15:13:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The threshold in tsc_read_refs() is constant which may favor slower CPUs but may not be optimal for simple reading of reference on faster ones. Hence make it proportional to tsc_khz to compensate for this. The threshold guards against any disturbance like IRQs, NMIs, SMIs or CPU stealing by host on guest systems so rename it accordingly and fix comments as well. Also on some systems there is noticeable DMI bus contention at some point during boot keeping the readout failing (observed with about one in ~300 boots when testing). In that case retry also the second readout instead of simply bailing out unrefined. Usually the next second the readout returns fast just fine without any issues. Signed-off-by: Daniel Vacek --- arch/x86/kernel/tsc.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 6d5dc5dabfd7..7812cd7bd45b 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -297,11 +297,11 @@ static int __init tsc_setup(char *str) __setup("tsc=", tsc_setup); -#define MAX_RETRIES 5 -#define SMI_TRESHOLD 50000 +#define MAX_RETRIES 5 +#define TSC_THRESHOLD (tsc_khz >> 5) /* - * Read TSC and the reference counters. Take care of SMI disturbance + * Read TSC and the reference counters. Take care of any disturbances */ static u64 tsc_read_refs(u64 *p, int hpet) { @@ -315,7 +315,7 @@ static u64 tsc_read_refs(u64 *p, int hpet) else *p = acpi_pm_read_early(); t2 = get_cycles(); - if ((t2 - t1) < SMI_TRESHOLD) + if ((t2 - t1) < TSC_THRESHOLD) return t2; } return ULLONG_MAX; @@ -1268,7 +1268,7 @@ struct system_counterval_t convert_art_ns_to_tsc(u64 art_ns) */ static void tsc_refine_calibration_work(struct work_struct *work) { - static u64 tsc_start = -1, ref_start; + static u64 tsc_start = ULLONG_MAX, ref_start; static int hpet; u64 tsc_stop, ref_stop, delta; unsigned long freq; @@ -1283,14 +1283,15 @@ static void tsc_refine_calibration_work(struct work_struct *work) * delayed the first time we expire. So set the workqueue * again once we know timers are working. */ - if (tsc_start == -1) { + if (tsc_start == ULLONG_MAX) { +restart: /* * Only set hpet once, to avoid mixing hardware * if the hpet becomes enabled later. */ hpet = is_hpet_enabled(); - schedule_delayed_work(&tsc_irqwork, HZ); tsc_start = tsc_read_refs(&ref_start, hpet); + schedule_delayed_work(&tsc_irqwork, HZ); return; } @@ -1300,9 +1301,9 @@ static void tsc_refine_calibration_work(struct work_struct *work) if (ref_start == ref_stop) goto out; - /* Check, whether the sampling was disturbed by an SMI */ - if (tsc_start == ULLONG_MAX || tsc_stop == ULLONG_MAX) - goto out; + /* Check, whether the sampling was disturbed */ + if (tsc_stop == ULLONG_MAX) + goto restart; delta = tsc_stop - tsc_start; delta *= 1000000LL; -- 2.19.1