mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Maksim Davydov <davydov-max@yandex-team.ru>
Cc: linux-kernel@vger.kernel.org, tglx@kernel.org, mingo@redhat.com,
	 peterz@infradead.org, bp@alien8.de, dave.hansen@linux.intel.com,
	 x86@kernel.org, hpa@zytor.com
Subject: Re: [PATCH] clocksource: stop monitoring TSC in VMs
Date: Tue, 21 Jul 2026 12:35:15 -0700	[thread overview]
Message-ID: <al_J8wAVEpZlns2X@google.com> (raw)
In-Reply-To: <20260715125905.104313-1-davydov-max@yandex-team.ru>

On Wed, Jul 15, 2026, Maksim Davydov wrote:
> It seems that in a virtual machine hardware clocks shouldn't be monitored
> by software (paravirtualized or emulated) clocks because they have
> different behaviour. For example, a VM can be live-migrated to another
> host or it can be paused. Both of them can cause false positive marking of
> TSC as unstable:
> 
> w/ kvm-clock:
>   clocksource: Marking clocksource tsc unstable due to frequency skew
>   clocksource: Watchdog               kvm-clock interval:        301338133ns
>   clocksource: Clocksource                  tsc interval:        620070548ns
>   tsc: Marking TSC unstable due to clocksource watchdog
>   clocksource: Switched to clocksource kvm-clock
> 
> w/o kvm-clock:
>   clocksource: Marking clocksource tsc unstable due to frequency skew
>   clocksource: Watchdog                    hpet interval:        248411930ns
>   clocksource: Clocksource                  tsc interval:        480045119ns
>   tsc: Marking TSC unstable due to clocksource watchdog
>   TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
>   sched_clock: Marking unstable (302400491443, 145482655)<-(302687230264, -141256934)
>   clocksource: Switched to clocksource hpet
> 
> Both examples were created in the VM with invariant TSC, but without
> TSC_ADJUST MSR in order to fail check in check_system_tsc_reliable(). So,
> it is stable enough to be the best clocksource but not reliable enough
> to be without a watchdog.
> 
> The reason why TSC can be marked as unstable is the different way of
> saving and restoring state of clocks. In virtualized environment TSC is a
> "hardware" clock that QEMU doesn't stop during VM pause. On the other hand,
> QEMU stops hpet and kvm-clock. So live-migration to paused state (e.g. in
> order to hot-plug some devices) or short stop+cont can cause divergence
> of TSC and any other "software" clocks. (The point to have the same
> behaviour for all clocks will be discussed later in qemu-devel.)
> 
> Nevertheless, it is important to point out, kvm_clock_read() used to touch
> watchdog and that prevents false positive marking TSC as unstable,
> because a hypervisor usually notifies a VM about clocks interference and
> a guest OS usually checks the appropriate flags. This behaviour was changed
> in 8739c6811572. But still, this issue has also existed (and now exists)
> with other clocks.
> 
> Thus, it seems that the clocksource watchdog has to be disabled for TSC
> in VMs unless the opposite is explicitly requested by `tsc=watchdog`.
> "Hardware" stable TSC is more realible than "software" clocks that
> usually use host's TSC inside.
> 
> Fixes: 8739c6811572 ("sched/clock/x86: Mark sched_clock() noinstr")
> Signed-off-by: Maksim Davydov <davydov-max@yandex-team.ru>
> ---
>  arch/x86/kernel/tsc.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
> index ce10ae4b298b..11b24f0c19bd 100644
> --- a/arch/x86/kernel/tsc.c
> +++ b/arch/x86/kernel/tsc.c
> @@ -1553,7 +1553,8 @@ void __init tsc_init(void)
>  		return;
>  	}
>  
> -	if (tsc_clocksource_reliable || tsc_watchdog == TSC_WATCHDOG_OFF)
> +	if (tsc_clocksource_reliable || tsc_watchdog == TSC_WATCHDOG_OFF ||
> +	    boot_cpu_has(X86_FEATURE_HYPERVISOR))
>  		tsc_disable_clocksource_watchdog();

I've been working on fixing this issue, along with a whole pile of other TSC-related
virtualization issues, for ~1.5 years (yikes!).  I'm hoping to land the series
"soon', ideally in 7.3.  Any testing/review you can provide that series would be
much appreciated!

https://lore.kernel.org/all/20260701193212.749551-23-seanjc@google.com

>  
>  	clocksource_register_khz(&clocksource_tsc_early, tsc_khz);
> -- 
> 2.34.1
> 

      reply	other threads:[~2026-07-21 19:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-15 12:59 Maksim Davydov
2026-07-21 19:35 ` Sean Christopherson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=al_J8wAVEpZlns2X@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=davydov-max@yandex-team.ru \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®