On 1/2/2025 3:02 PM, Borislav Petkov wrote: > On Thu, Jan 02, 2025 at 10:50:53AM +0530, Nikunj A. Dadhania wrote: >> This is what was suggested by tglx: >> >> "So if you know you want TSC to be selected, then upgrade the rating of >> both the early and the regular TSC clocksource and be done with it." > > I highly doubt that he saw what you have now: > > Your commit message is talking about virtualized environments but your diff is > doing a global, unconditional change which affects *everything*. Right, let me limit this only to virtualized environments as part of CONFIG_PARAVIRT. Subject: [PATCH] x86/tsc: Upgrade TSC clocksource rating for guests Hypervisor platform setup (x86_hyper_init::init_platform) routines register their own PV clock sources (KVM, HyperV, and Xen) at different clock ratings resulting in selection of PV clock source even though a stable TSC clock source is available. Upgrade the clock rating of the TSC early and regular clock source to prefer TSC over PV clock sources when TSC is invariant, non-stop and stable Cc: Alexey Makhalov <alexey.makhalov@xxxxxxxxxxxx> Cc: Juergen Gross <jgross@xxxxxxxx> Cc: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx> Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Signed-off-by: Nikunj A Dadhania <nikunj@xxxxxxx> --- arch/x86/kernel/tsc.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 34dec0b72ea8..5c6831a42889 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -274,10 +274,31 @@ bool using_native_sched_clock(void) { return static_call_query(pv_sched_clock) == native_sched_clock; } + +/* + * Upgrade the clock rating for TSC early and regular clocksource when the + * underlying platform provides non-stop, invariant, and stable TSC. TSC + * early/regular clocksource will be preferred over other para-virtualized clock + * sources. + */ +static void __init upgrade_clock_rating(struct clocksource *tsc_early, + struct clocksource *tsc) +{ + if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR) && + cpu_feature_enabled(X86_FEATURE_CONSTANT_TSC) && + cpu_feature_enabled(X86_FEATURE_NONSTOP_TSC) && + !tsc_unstable) { + tsc_early->rating = 449; + tsc->rating = 450; + } +} #else u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock"))); bool using_native_sched_clock(void) { return true; } + +static void __init upgrade_clock_rating(struct clocksource *tsc_early, + struct clocksource *tsc) { } #endif notrace u64 sched_clock(void) @@ -1564,6 +1585,8 @@ void __init tsc_init(void) if (tsc_clocksource_reliable || no_tsc_watchdog) tsc_disable_clocksource_watchdog(); + upgrade_clock_rating(&clocksource_tsc_early, &clocksource_tsc); + clocksource_register_khz(&clocksource_tsc_early, tsc_khz); detect_art(); } -- 2.34.1