Message ID | 20231120105528.760306-6-vschneid@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp2115350vqn; Mon, 20 Nov 2023 02:59:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwzgTxmGD0RDjAR3OugDzQ8AUvAMe9RGdtmdfUzSFs7XlT5moc3fBfbbN005qujl8HZRC0 X-Received: by 2002:a05:6870:44d0:b0:1e9:cdad:4903 with SMTP id t16-20020a05687044d000b001e9cdad4903mr9186909oai.50.1700477994193; Mon, 20 Nov 2023 02:59:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700477994; cv=none; d=google.com; s=arc-20160816; b=uFsr893HC11sY02QAY/WNxKS0LzKdWYDS72q2T+8fCXBG/yXsf5B4p7Hp+S6vyASPj /V4WKUY1pece/U91q/IcrcngfWLw3mJptJk+EbSpmv2j9zzmtgAjPhdm+c8IEza8/qc3 bE8TIY4i3NX8h/0TTq6WgjfwPr327nPiSXyqYDlzHpYcwiDuvJreLSZwfe2SbBmNboTt DYJmfu9YRAdVSM2Lx5XWCGw1yp0KCgBjXmRsDOGzzND/ooyh+Tm7epgkkzqAJkJE9oMR Y/dGuW/9iw5ND4yO2JIss70KBoGGwSS0gG1nhsDLUdOptmI8dh1ZERDpww7GPB/urAnU G2nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Fh5nWCS7m0i97fxRSND8fdK+nCY+MD6Me1hw+2UF/3c=; fh=bEaXtmS8lzRItYrE/RfaVyKNdc9i8y6GHBEJVBz/DUE=; b=GiARDmR3BkaHHnvTWVL2zOnOBwmo/WNKw2gEO27G0YKKgbSlsOG0LV/jC0OYbl+0wJ 8+pwHaTBx2CoX5Kf53aluOl77Mhil95wDtVdx/rvsGHSEcwHeSnCTKloGO1ooiNbWPPR QoqauaRRU6Gv417njmVULq45SOOhIKZuLJTrgNY0qVA4AB5P7bFInaZcucFmJ3rKY2l/ hELCh9lcucy10FBalKqh8J+K93nVmyg8V/d7utEq+PaZvpZp9Z8an4T69isNXqd2UmPF 964OHudIm2zuNncjFtwqqvF5N6zZ3RiablsMH1eBEpYF9QqWcJiwmQaBKX2bTz8rEye9 KwIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aL2R4fer; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id r18-20020aa79892000000b006cb901a9883si2197262pfl.326.2023.11.20.02.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 02:59:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aL2R4fer; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 39F0B806CC36; Mon, 20 Nov 2023 02:56:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233365AbjKTK4q (ORCPT <rfc822;heyuhang3455@gmail.com> + 27 others); Mon, 20 Nov 2023 05:56:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233115AbjKTK43 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 20 Nov 2023 05:56:29 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3680BD4C for <linux-kernel@vger.kernel.org>; Mon, 20 Nov 2023 02:56:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700477776; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fh5nWCS7m0i97fxRSND8fdK+nCY+MD6Me1hw+2UF/3c=; b=aL2R4ferCNTSW0/DM4wCLENT5BUyJ7WVaXa3M6x0ATKoxDUAXKkstGMplnZxLz1uyYE/FR xCCUnWiZdKVQqHQ8YL+9E/4f/M0K8jLsrYPQNiOrcOUKRJkFJvb99eKNc6xdMnhWmhePS1 58GVkehSPcacpMI+EvsbA2Qj0ZvHtfw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-88-Q63KOD7EPAGRzHG2NpasBw-1; Mon, 20 Nov 2023 05:56:12 -0500 X-MC-Unique: Q63KOD7EPAGRzHG2NpasBw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7DB26811003; Mon, 20 Nov 2023 10:56:11 +0000 (UTC) Received: from vschneid-thinkpadt14sgen2i.remote.csb (unknown [10.39.195.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BAC3C2026D4C; Mon, 20 Nov 2023 10:56:06 +0000 (UTC) From: Valentin Schneider <vschneid@redhat.com> To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-arch@vger.kernel.org, x86@kernel.org Cc: Thomas Gleixner <tglx@linutronix.de>, Borislav Petkov <bp@alien8.de>, Peter Zijlstra <peterz@infradead.org>, Josh Poimboeuf <jpoimboe@kernel.org>, Pawan Gupta <pawan.kumar.gupta@linux.intel.com>, Ingo Molnar <mingo@redhat.com>, Dave Hansen <dave.hansen@linux.intel.com>, "H. Peter Anvin" <hpa@zytor.com>, Paolo Bonzini <pbonzini@redhat.com>, Wanpeng Li <wanpengli@tencent.com>, Vitaly Kuznetsov <vkuznets@redhat.com>, Arnd Bergmann <arnd@arndb.de>, Jason Baron <jbaron@akamai.com>, Steven Rostedt <rostedt@goodmis.org>, Ard Biesheuvel <ardb@kernel.org>, Frederic Weisbecker <frederic@kernel.org>, "Paul E. McKenney" <paulmck@kernel.org>, Feng Tang <feng.tang@intel.com>, Andrew Morton <akpm@linux-foundation.org>, "Mike Rapoport (IBM)" <rppt@kernel.org>, Vlastimil Babka <vbabka@suse.cz>, David Hildenbrand <david@redhat.com>, "ndesaulniers@google.com" <ndesaulniers@google.com>, Michael Kelley <mikelley@microsoft.com>, "Masami Hiramatsu (Google)" <mhiramat@kernel.org> Subject: [PATCH 5/5] x86/tsc: Make __use_tsc __ro_after_init Date: Mon, 20 Nov 2023 11:55:28 +0100 Message-ID: <20231120105528.760306-6-vschneid@redhat.com> In-Reply-To: <20231120105528.760306-1-vschneid@redhat.com> References: <20231120105528.760306-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 20 Nov 2023 02:56:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783080413135331773 X-GMAIL-MSGID: 1783080413135331773 |
Series |
jump_label: Fix __ro_after_init keys for modules & annotate some keys
|
|
Commit Message
Valentin Schneider
Nov. 20, 2023, 10:55 a.m. UTC
__use_tsc is only ever enabled in __init tsc_enable_sched_clock(), so mark
it as __ro_after_init.
Signed-off-by: Valentin Schneider <vschneid@redhat.com>
---
arch/x86/kernel/tsc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Mon, Nov 20, 2023 at 11:55:28AM +0100, Valentin Schneider wrote: > __use_tsc is only ever enabled in __init tsc_enable_sched_clock(), so mark > it as __ro_after_init. > > Signed-off-by: Valentin Schneider <vschneid@redhat.com> > --- > arch/x86/kernel/tsc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c > index 15f97c0abc9d0..f19b42ea40573 100644 > --- a/arch/x86/kernel/tsc.c > +++ b/arch/x86/kernel/tsc.c > @@ -44,7 +44,7 @@ EXPORT_SYMBOL(tsc_khz); > static int __read_mostly tsc_unstable; > static unsigned int __initdata tsc_early_khz; > > -static DEFINE_STATIC_KEY_FALSE(__use_tsc); > +static DEFINE_STATIC_KEY_FALSE_RO(__use_tsc); So sure, we can absolutely do that, but do we want to take this one further perhaps? "notsc" on x86_64 makes no sense what so ever. Lets drag things into this millennium. --- diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 15f97c0abc9d..4cfcf5946162 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -44,7 +44,9 @@ EXPORT_SYMBOL(tsc_khz); static int __read_mostly tsc_unstable; static unsigned int __initdata tsc_early_khz; +#ifndef CONFIG_X86_64 static DEFINE_STATIC_KEY_FALSE(__use_tsc); +#endif int tsc_clocksource_reliable; @@ -230,24 +232,26 @@ static void __init cyc2ns_init_secondary_cpus(void) */ noinstr u64 native_sched_clock(void) { - if (static_branch_likely(&__use_tsc)) { - u64 tsc_now = rdtsc(); +#ifndef CONFIG_X86_64 + if (!static_branch_unlikely(&__use_tsc)) { + /* + * Fall back to jiffies if there's no TSC available: + * ( But note that we still use it if the TSC is marked + * unstable. We do this because unlike Time Of Day, + * the scheduler clock tolerates small errors and it's + * very important for it to be as fast as the platform + * can achieve it. ) + */ - /* return the value in ns */ - return __cycles_2_ns(tsc_now); + /* No locking but a rare wrong value is not a big deal: */ + return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ); } +#endif - /* - * Fall back to jiffies if there's no TSC available: - * ( But note that we still use it if the TSC is marked - * unstable. We do this because unlike Time Of Day, - * the scheduler clock tolerates small errors and it's - * very important for it to be as fast as the platform - * can achieve it. ) - */ + u64 tsc_now = rdtsc(); - /* No locking but a rare wrong value is not a big deal: */ - return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ); + /* return the value in ns */ + return __cycles_2_ns(tsc_now); } /* @@ -291,7 +295,8 @@ int check_tsc_unstable(void) } EXPORT_SYMBOL_GPL(check_tsc_unstable); -#ifdef CONFIG_X86_TSC +#ifndef CONFIG_X86_64 +#if defined(CONFIG_X86_TSC int __init notsc_setup(char *str) { mark_tsc_unstable("boot parameter notsc"); @@ -310,6 +315,7 @@ int __init notsc_setup(char *str) #endif __setup("notsc", notsc_setup); +#endif static int no_sched_irq_time; static int no_tsc_watchdog; @@ -1556,7 +1562,9 @@ static void __init tsc_enable_sched_clock(void) /* Sanitize TSC ADJUST before cyc2ns gets initialized */ tsc_store_and_check_tsc_adjust(true); cyc2ns_init_boot_cpu(); +#ifndef CONFIG_X86_64 static_branch_enable(&__use_tsc); +#endif } void __init tsc_early_init(void)
On 20/11/23 13:05, Peter Zijlstra wrote: > On Mon, Nov 20, 2023 at 11:55:28AM +0100, Valentin Schneider wrote: >> __use_tsc is only ever enabled in __init tsc_enable_sched_clock(), so mark >> it as __ro_after_init. >> >> Signed-off-by: Valentin Schneider <vschneid@redhat.com> >> --- >> arch/x86/kernel/tsc.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c >> index 15f97c0abc9d0..f19b42ea40573 100644 >> --- a/arch/x86/kernel/tsc.c >> +++ b/arch/x86/kernel/tsc.c >> @@ -44,7 +44,7 @@ EXPORT_SYMBOL(tsc_khz); >> static int __read_mostly tsc_unstable; >> static unsigned int __initdata tsc_early_khz; >> >> -static DEFINE_STATIC_KEY_FALSE(__use_tsc); >> +static DEFINE_STATIC_KEY_FALSE_RO(__use_tsc); > > So sure, we can absolutely do that, but do we want to take this one > further perhaps? "notsc" on x86_64 makes no sense what so ever. Lets > drag things into this millennium. > Just to make sure I follow: currently, for the static key to be enabled, we (mostly) need: o X86_FEATURE_TSC is in CPUID o determine_cpu_tsc_frequencies()->pit_hpet_ptimer_calibrate_cpu() passes IIUC all X86_64 systems have a TSC, so the CPUID feature should be a given. AFAICT pit_hpt_ptimer_calibrate_cpu() relies on having either HPET or the ACPI PM timer, the latter should be widely available, though X86_PM_TIMER can be disabled via EXPERT - is that a fringe case we don't care about, or did I miss something? I don't really know this stuff, and I'm trying to write a changelog...
On Mon, Dec 04, 2023 at 05:51:49PM +0100, Valentin Schneider wrote: > On 20/11/23 13:05, Peter Zijlstra wrote: > > On Mon, Nov 20, 2023 at 11:55:28AM +0100, Valentin Schneider wrote: > >> __use_tsc is only ever enabled in __init tsc_enable_sched_clock(), so mark > >> it as __ro_after_init. > >> > >> Signed-off-by: Valentin Schneider <vschneid@redhat.com> > >> --- > >> arch/x86/kernel/tsc.c | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c > >> index 15f97c0abc9d0..f19b42ea40573 100644 > >> --- a/arch/x86/kernel/tsc.c > >> +++ b/arch/x86/kernel/tsc.c > >> @@ -44,7 +44,7 @@ EXPORT_SYMBOL(tsc_khz); > >> static int __read_mostly tsc_unstable; > >> static unsigned int __initdata tsc_early_khz; > >> > >> -static DEFINE_STATIC_KEY_FALSE(__use_tsc); > >> +static DEFINE_STATIC_KEY_FALSE_RO(__use_tsc); > > > > So sure, we can absolutely do that, but do we want to take this one > > further perhaps? "notsc" on x86_64 makes no sense what so ever. Lets > > drag things into this millennium. > > > > Just to make sure I follow: currently, for the static key to be enabled, we > (mostly) need: > o X86_FEATURE_TSC is in CPUID > o determine_cpu_tsc_frequencies()->pit_hpet_ptimer_calibrate_cpu() passes > > IIUC all X86_64 systems have a TSC, so the CPUID feature should be a given. > > AFAICT pit_hpt_ptimer_calibrate_cpu() relies on having either HPET or the > ACPI PM timer, the latter should be widely available, though X86_PM_TIMER > can be disabled via EXPERT - is that a fringe case we don't care about, or > did I miss something? I don't really know this stuff, and I'm trying to > write a changelog... Ah, I was mostly just going by the fact that all of x86_64 have TSC and disabling it makes no sense. TSC calibration is always 'fun', but I don't know of a system where its failure causes us to not use TSC, Thomas?
On 04/12/23 19:20, Peter Zijlstra wrote: > On Mon, Dec 04, 2023 at 05:51:49PM +0100, Valentin Schneider wrote: >> On 20/11/23 13:05, Peter Zijlstra wrote: >> > On Mon, Nov 20, 2023 at 11:55:28AM +0100, Valentin Schneider wrote: >> >> __use_tsc is only ever enabled in __init tsc_enable_sched_clock(), so mark >> >> it as __ro_after_init. >> >> >> >> Signed-off-by: Valentin Schneider <vschneid@redhat.com> >> >> --- >> >> arch/x86/kernel/tsc.c | 2 +- >> >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> >> >> diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c >> >> index 15f97c0abc9d0..f19b42ea40573 100644 >> >> --- a/arch/x86/kernel/tsc.c >> >> +++ b/arch/x86/kernel/tsc.c >> >> @@ -44,7 +44,7 @@ EXPORT_SYMBOL(tsc_khz); >> >> static int __read_mostly tsc_unstable; >> >> static unsigned int __initdata tsc_early_khz; >> >> >> >> -static DEFINE_STATIC_KEY_FALSE(__use_tsc); >> >> +static DEFINE_STATIC_KEY_FALSE_RO(__use_tsc); >> > >> > So sure, we can absolutely do that, but do we want to take this one >> > further perhaps? "notsc" on x86_64 makes no sense what so ever. Lets >> > drag things into this millennium. >> > >> >> Just to make sure I follow: currently, for the static key to be enabled, we >> (mostly) need: >> o X86_FEATURE_TSC is in CPUID >> o determine_cpu_tsc_frequencies()->pit_hpet_ptimer_calibrate_cpu() passes >> >> IIUC all X86_64 systems have a TSC, so the CPUID feature should be a given. >> >> AFAICT pit_hpt_ptimer_calibrate_cpu() relies on having either HPET or the >> ACPI PM timer, the latter should be widely available, though X86_PM_TIMER >> can be disabled via EXPERT - is that a fringe case we don't care about, or >> did I miss something? I don't really know this stuff, and I'm trying to >> write a changelog... > > Ah, I was mostly just going by the fact that all of x86_64 have TSC and > disabling it makes no sense. > > TSC calibration is always 'fun', but I don't know of a system where its > failure causes us to not use TSC, Thomas? Having another look at this, it looks like the actual requirements for the TSC being used are either of: o CPUID accepting 0x16 as eax input (cf. cpu_khz_from_cpuid()) o MSR_FSB_FREQ being available (cf. cpu_khz_from_msr()) o pit_hpet_ptimer_calibrate_cpu() doesn't mess up I couldn't find any guarantees for x86_64 on having the processor frequency information CPUID leaf, nor for the FSB_FREQ MSR (both tsc_msr_cpu_ids and the SDM seem to point at only a handful of models). Also for x86_64 there is this "apicpmtimer" cmdline arg which currently disables the TSC. The commit that introduced it [1] suggests there are x86_64 systems out there with calibration issues, so now I'm not sure whether we can kill the static key for x86_64 :( [1]: 0c3749c41f5e ("[PATCH] x86_64: Calibrate APIC timer using PM timer") followed by: 7fd67843b96f ("[PATCH] x86_64: Disable tsc when apicpmtimer is active")
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 15f97c0abc9d0..f19b42ea40573 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -44,7 +44,7 @@ EXPORT_SYMBOL(tsc_khz); static int __read_mostly tsc_unstable; static unsigned int __initdata tsc_early_khz; -static DEFINE_STATIC_KEY_FALSE(__use_tsc); +static DEFINE_STATIC_KEY_FALSE_RO(__use_tsc); int tsc_clocksource_reliable;