Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events

From: Yosry Ahmed

Date: Wed Nov 25 2026 - 18:23:48 EST


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.