Linux Perf Users
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Sean Christopherson <seanjc@google.com>,
	 Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	 Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, linux-perf-users@vger.kernel.org,
	 linux-kernel@vger.kernel.org, Like Xu <likexu@tencent.com>,
	Jim Mattson <jmattson@google.com>,
	 Mingwei Zhang <mizhang@google.com>
Subject: Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events
Date: Wed, 25 Nov 2026 23:23:43 +0000	[thread overview]
Message-ID: <awdnFkLVRkAglrXv@google.com> (raw)
In-Reply-To: <20231129112019.GG30650@noisy.programming.kicks-ass.net>

On Wed, Nov 29, 2023 at 12:20:19PM +0100, Peter Zijlstra wrote:
> On Tue, Nov 28, 2023 at 05:33:16PM -0800, Sean Christopherson wrote:
> 
> > If programming a "period" of 1 puts the host at risk in some way, then I agree
> > that this is unsafe and we need a different solution. 
> 
> IIRC if you put in -1 on a Nehalem, you end up with an NMI-storm which
> wasn't trivial to recover from if at all (it's too long ago and I don't
> have ancient hardware like that anymore :/)
> 
> > But if the worst case
> > scenario is non-determinstic or odd behavior from the guest's perspective, then
> > that's the guest's problem (with the caveat that the guest might not have accurate
> > Family/Model/Stepping data to make informed decisions).
> 
> Things like bdm_limit_period() will cause odd behaviour IIRC, it does
> daft things like generate extra PEBS records on overflow and gives
> otherwise daft results for PDIR.
> 
> glc_limit_period() lacks a useful comment :/

Sorry for thread necromancy, but it seems like the discussion stopped
here and I'd like to get this fixed as I am observing the same failures
that Sean did.

Basically, if KVM needs to program period=1 (guest counter is -1), perf
overwrites it to 2. After 1 event, the guest counter should overflow,
but the hardware counter doesn't, so there is no PMI. On the other hand,
KVM relies on the hardware PMIs to signal overflows while perf events
are counting.

The overflow is then lost: when KVM pauses the event (e.g. to account
for an emulated instruction), it folds the perf count into its software
counter and restarts the event with a new period, so the late hardware
overflow never comes, and KVM only checks for wraps on its own emulated
counts.

Sean's fix makes the problem go away on all CPU platforms, and Peter's
makes it go away on Skylake+ on Intel (with updated microcode) and all
AMD platforms. So I am mostly happy with both. It's worth noting that
the blast radius is smaller with Sean's fix as it only affects guest
events.

A middle ground would be skipping the left < 2 clamp on exclude_host
events, but not the x86_pmu_limit_period() callback, which makes the
problem go away on slightly more CPUs than Peter's patch (I think
Sandy/Ivy Bridge, and Skylake on older microcode versions). This keeps
the *_limit_period() subtleties in place.

All three versions seem to make the problem go away on AMD Turin.

WDYT?

An alternative to all of this is a "fix" on the KVM side where KVM is
always responsible for signaling all PMC overflows, instead of signaling
some of them from the PMI code path. KVM would keep track of the guest
counter value as of the last pause or write, and on PMI it would just
pause the event, manually check for the counter wrapping, and deliver
the PMI to the guest.

This would obviously be a more complex change.

      reply	other threads:[~2026-11-25 23:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07 18:36 [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events Sean Christopherson
2023-11-07 19:38 ` Mingwei Zhang
2023-11-07 23:02   ` Sean Christopherson
2023-11-07 23:47     ` Mingwei Zhang
2023-11-17 10:32 ` Peter Zijlstra
2023-11-29  1:33   ` Sean Christopherson
2023-11-29 11:20     ` Peter Zijlstra
2026-11-25 23:23       ` Yosry Ahmed [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=awdnFkLVRkAglrXv@google.com \
    --to=yosry@kernel.org \
    --cc=acme@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=jmattson@google.com \
    --cc=likexu@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=mizhang@google.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --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