Re: [PATCH linux-next 2/2] x86/xen/time: cleanup xen_tsc_safe_clocksource

From: Marcelo Tosatti
Date: Thu Feb 23 2023 - 11:01:20 EST


On Mon, Feb 20, 2023 at 08:14:40PM -0800, Krister Johansen wrote:
> On Mon, Feb 20, 2023 at 11:01:18PM +0100, Thomas Gleixner wrote:
> > On Mon, Feb 20 2023 at 09:17, Krister Johansen wrote:
> > > @@ -495,8 +496,7 @@ static int __init xen_tsc_safe_clocksource(void)
> > > /* Leaf 4, sub-leaf 0 (0x40000x03) */
> > > cpuid_count(xen_cpuid_base() + 3, 0, &eax, &ebx, &ecx, &edx);
> > >
> > > - /* tsc_mode = no_emulate (2) */
> > > - if (ebx != 2)
> > > + if (ebx != XEN_CPUID_TSC_MODE_NEVER_EMULATE)
> > > return 0;
> > >
> > > return 1;
> >
> > What about removing more stupidity from that function?
> >
> > static bool __init xen_tsc_safe_clocksource(void)
> > {
> > u32 eax, ebx. ecx, edx;
> >
> > /* Leaf 4, sub-leaf 0 (0x40000x03) */
> > cpuid_count(xen_cpuid_base() + 3, 0, &eax, &ebx, &ecx, &edx);
> >
> > return ebx == XEN_CPUID_TSC_MODE_NEVER_EMULATE;
> > }
>
> I'm all for simplifying. I'm happy to clean up that return to be more
> idiomatic. I was under the impression, perhaps mistaken, though, that
> the X86_FEATURE_CONSTANT_TSC, X86_FEATURE_NONSTOP_TSC, and
> check_tsc_unstable() checks were actually serving a purpose: to ensure
> that we don't rely on the tsc in environments where it's being emulated
> and the OS would be better served by using a PV clock. Specifically,
> kvmclock_init() makes a very similar set of checks that I also thought
> were load-bearing.

kvmclock_init will lower the rating of kvmclock so that TSC clocksource
can be used instead:

/*
* X86_FEATURE_NONSTOP_TSC is TSC runs at constant rate
* with P/T states and does not stop in deep C-states.
*
* Invariant TSC exposed by host means kvmclock is not necessary:
* can use TSC as clocksource.
*
*/
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
!check_tsc_unstable())
kvm_clock.rating = 299;