Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC

From: Yosry Ahmed

Date: Wed Dec 02 2026 - 16:04:17 EST


> >> IMO, making active_events really track active events could be the simplest
> >> and straightforward way to fix this issue.
> > Taking a closer look, I don't think this is viable:
> > - active_events is global, not per-CPU.
>
> Yes, but we can extend the active_events to per-cpu, PMI is per-cpu, right?

Maybe? I didn't take a closer look to check how straightforward it might be.

> > - If active_events tracks currently scheduled events (which is what we
> > need here AFAICT), bailing in the NMI handler can miss a PMI if a PMI
> > was in-flight or delayed when the event is scheduled out.
>
> Generally the GLOBAL_CTRL is cleared in the entire schedule_in or
> schedule_out process until the scheduling finishes, this would prevent the
> PMI is triggered again in the event scheduling in theory.

Hmm yeah if we always disable the PMU first (and account for NMI delays).

> > - active_events is not necessarily 0 while the guest mediated PMU
> > context is loaded. Other PMUs like Intel PT/BTS could have active
> > events AFAICT.
>
> Yes, Intel PT is an issue. The simplest way to fix the issue is to disallow
> PT and mediated vPMU to work in coexistence.

That was also a sticking point in the dynamic PMU context switching
RFC, Intel PT and BTS use the same PMI vector but are not mediated (so
are not scheduled out).

I think changing the semantics of active_events or adding mutual
exclusion between Intel PT/BTS and the mediated vPMU is a larger lift
that can be explored separately.

> An alternative way is to move perf_load_guest_lvtpc() into
> perf_load_guest_context(), call perf_load_guest_context() between the
> disabling perf contexts and enabling perf contexts. Actually it goes back
> our previous implementation.

I think this works as an alternative to this patch, although it still
doesn't guarantee PERF_GLOBAL_CTRL being 0 when LVTPC is switched
AFAICT. I think perf_ctx_disable() may not clear PERF_GLOBAL_CTRL if
there are no core PMU events being scheduled out. Also,
perf_ctx_enable() will re-enable PERF_GLOBAL_CTRL before returning
anyway, so KVM still needs to clear it afterwards. So it doesn't buy
us much compared to this patch, does it?

Additionally, we'd need to restore the arch hook which I think Peter
didn't like in previous implementations:
https://lore.kernel.org/all/20250815113951.GC4067720@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

> I'm fine with either way.

I think out of all the options I prefer this patch because it's a much
smaller change and is more stable-friendly. I am also fine with moving
perf_load_guest_lvtpc() into perf_load_guest_context() if others are
okay with it.