Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events
From: Yosry Ahmed
Date: Tue Dec 01 2026 - 23:16:39 EST
On Tue, Dec 1, 2026 at 5:34 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
>
> On Wed, Nov 25, 2026, Yosry Ahmed wrote:
> > 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?
>
> If Peter's fix works, go with that. I'm largely at the "I don't care" stage when
> it comes to the emulated vPMU. The only reason I sent this patch is because of
> KVM's testcase for this exact edgecase. In practice, the emulated vPMU can't get
> anywhere near perfect, we can only make it suck less.
It works for most modern CPUs so I am happy with it. Peter, any preferences?