Re: [PATCH 1/4] perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has isolation

Sean Christopherson
2026-04-17T00:23:34Z

On Thu, Apr 16, 2026, Namhyung Kim wrote:
> On Thu, Apr 16, 2026 at 12:38:49PM -0700, Sean Christopherson wrote:
> > On Thu, Apr 16, 2026, Namhyung Kim wrote:
> > > > + /*
> > > > + * Disable counters where the guest PMC is different than the host PMC
> > > > + * being used on behalf of the guest, as the PEBS record includes
> > > > + * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the
> > > > + * wrong counter(s). Similarly, disallow PEBS in the guest if the host
> > > > + * is using PEBS, to avoid bleeding host state into PEBS records.
> > > > + */
> > > > + guest_pebs_mask &= kvm_pmu->pebs_enable & ~kvm_pmu->host_cross_mapped_mask;
> > > > + if (pebs_mask & ~cpuc->intel_ctrl_guest_mask)
> > > > + guest_pebs_mask = 0;
> > > >
> > > > + /*
> > > > + * Do NOT mess with PEBS_ENABLED. As above, disabling counters via
> > > > + * PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED,
> > > > + * e.g. on VM-Exit, can put the system in a bad state. Simply enable
> > > > + * counters in PERF_GLOBAL_CTRL, as perf load PEBS_ENABLED with the
> > > > + * full value, i.e. perf *also* relies on PERF_GLOBAL_CTRL.
> > > > + */
> > > > + arr[global_ctrl].guest |= guest_pebs_mask;
> > >
> > > I was confused by the earlier comment in the funcion that says it is not
> > > enough to disable counters but I've realized it's only for the case PEBS
> > > isolation is not supported by CPU/ucode.
> >
> > Yeah, me too, more than once. :-/
> >
> > > I think it's ok for disabling guest PEBS, but I'm curious if there's a
> > > case to enable PEBS only in guest and it'd be handled correctly.
> >
> > Yep, if PEBS is being virtualized for the guest, unless the host is also profiling,
> > then PEBS will be active for the guest but not the host. KVM tests for PEBS pass,
> > and while they aren't exactly comprehensive, they should detect outright breakage.
>
> In that case, wouldn't it need to update PEBS_ENABLED here?

No, because KVM only supports guest usage of PEBS when KVM is proxying the guest
PMU via perf. I.e. when perf fully controls PEBS_ENABLED. On perf's side, events
created by KVM show up as "guest only", but are otherwise programmed normally,
including getting shoved into PEBS_ENABLED as needed. So to activate PEBS PMCs
for the guest, KVM just needs to load PERF_GLOBAL_CTRL with the counters that
should be enabled for the guest (taking care to not load "guest only" PEBS events
that were (stupidly) created by host userspace, as those can crash the guest.