public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: "Michal Koutný" <mkoutny@suse.com>
Cc: Yafang Shao <laoar.shao@gmail.com>,
	Dan Carpenter <dan.carpenter@linaro.org>,
	kvm@vger.kernel.org,  Peter Zijlstra <peterz@infradead.org>,
	Vincent Guittot <vincent.guittot@linaro.org>
Subject: Re: [bug report] KVM: x86: Unify TSC logic (sleeping in atomic?)
Date: Fri, 24 Jan 2025 07:36:45 -0800	[thread overview]
Message-ID: <Z5OzjYqOVz7nrnJ1@google.com> (raw)
In-Reply-To: <pfx63yk5euw6zsjmmpuetfuhhk7jcann3trlirp6y5u26lljn7@mtbwoswzoae3>

On Fri, Jan 24, 2025, Michal Koutný wrote:
> On Fri, Jan 17, 2025 at 09:51:41AM -0800, Sean Christopherson <seanjc@google.com> wrote:
> > That's not the problematic commit.  This popped because commit 8722903cbb8f
> > ("sched: Define sched_clock_irqtime as static key") in the tip tree turned
> > sched_clock_irqtime into a static key (it was a simple "int").
> > 
> > https://lore.kernel.org/all/20250103022409.2544-2-laoar.shao@gmail.com
> 
> Thanks for the analysis, it's spot on. What a bad luck.
> 
> Is there a precedent for static key switching in non-preemptible
> contexts?

Abuse static_key_deferred to push the patching to a workqueue?

> More generally, why does KVM do this tsc check in vcpu_load? 

The logic triggers when a vCPU migrates to a different pCPU.  The code detects
that the case where TSC is inconsistent between pCPUs, and would cause time to go
backwards from the guest's perspective.  E.g. TSC = X on CPU0, migrate to CPU1
where TSC = X - Y.

> Shouldn't possible unstability for that cpu be already checked and decided at
> boot (regardless of KVM)? (Unless unstability itself is not stable property.
> Which means any previously measured IRQ times are skewed.)

This isn't a problem that's unique to KVM.  The clocksource watchdog also marks
TSC unstable from non-preemptible context (presumably from IRQ context?)

  clocksource_watchdog()
  |
  -> spin_lock(&watchdog_lock);
     |
     -> __clocksource_unstable()
        |
        -> clocksource.mark_unstable() == tsc_cs_mark_unstable()
           |
           -> disable_sched_clock_irqtime()

Uh, and sched_clock_register() toggles the static key on with IRQs disabled...

	/* Cannot register a sched_clock with interrupts on */
	local_irq_save(flags);

	...

	/* Enable IRQ time accounting if we have a fast enough sched_clock() */
	if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
		enable_sched_clock_irqtime();

	local_irq_restore(flags);

> (Or a third option to revert the static-keyness if Yafang doesn't have

Given there are issues all over the place, either a revert or a generic fix.

  reply	other threads:[~2025-01-24 15:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-17  6:08 [bug report] KVM: x86: Unify TSC logic (sleeping in atomic?) Dan Carpenter
2025-01-17 17:51 ` Sean Christopherson
2025-01-24 10:34   ` Michal Koutný
2025-01-24 15:36     ` Sean Christopherson [this message]
2025-01-26  2:16       ` Yafang Shao

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=Z5OzjYqOVz7nrnJ1@google.com \
    --to=seanjc@google.com \
    --cc=dan.carpenter@linaro.org \
    --cc=kvm@vger.kernel.org \
    --cc=laoar.shao@gmail.com \
    --cc=mkoutny@suse.com \
    --cc=peterz@infradead.org \
    --cc=vincent.guittot@linaro.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