Re: [PATCH v3 13/18] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap

From: Chen, Zide

Date: Wed Nov 11 2026 - 13:11:09 EST




On 11/11/2026 9:37 AM, Sean Christopherson wrote:
> On Tue, Nov 10, 2026, Zide Chen wrote:
>>>>> KVM's principle of "not policing guest CPUID" means KVM allows userspace
>>>>> to configure an arbitrary guest CPUID without rejecting the ioctl or
>>>>> mutating the vCPUID entries.
>>>>>
>>>>> However, KVM's internal vCPU state (pmu->version) tracks the "effective
>>>>> PMU version" that KVM can actually virtualize.
>>>>>
>>>>> This distinction is documented in commit 4f1bd6b16074 ("KVM: selftests:
>>>>> Test Intel PMU architectural events on gp counters"):
>>>>>
>>>>> "KVM only officially supports up to version 2, but the behavior of the
>>>>> counters is backwards compatible, i.e. KVM shouldn't do something
>>>>> completely different for a higher, architecturally-defined vPMU version.
>>>>> Verify KVM behavior against the effective vPMU version, e.g. dvertising
>>>>> vPMU 5 when KVM only supports vPMU 2 shouldn't magically unlock vPMU 5
>>>>> features."
>>>>>
>>>>> pmu_counters_test itself explicitly mentions this rule:
>>>>>
>>>>> /*
>>>>> * Return the effective PMU version, i.e. the minimum between what KVM
>>>>> * supports and what is enumerated to the guest. The host deliberately
>>>>> * advertises a PMU version to the guest beyond what is actually
>>>>> * supported by KVM to verify KVM doesn't freak out and do something
>>>>> * bizarre with an architecturally valid, but unsupported, version.
>>>>> */
>
> ...
>
>>>>> Without a PMU version cap, guest resource validation could become
>>>>> increasingly complicated as more advanced PMU features are supported, as
>>>>> KVM would need to allow the guest to access PMU resources only when it
>>>>> satisfies both the vPMU version and the KVM capability.
>>>> The above sounds reasonable at first glance, but in practice it is
>>>> problematic because it arbitrarily masks out features that can be 100%
>>>> emulated in software even when there is zero host hardware dependency:
>>>>
>>>> - Features that can be emulated entirely in software (e.g. there is no
>>>> problem emulating the PerfMon v5 AnyThread deprecation, or emulating
>>>> MSRs like GLOBAL_STATUS_SET or GLOBAL_INUSE on a v2/v3 host). Clamping
>>>> the version unnecessarily prevents the guest from using features that
>>>> correspond to its configured PMU version.
>
> Just because we *can* do something in KVM doesn't mean we *should* do that thing
> in KVM.

Got it.

>>>> - Features that actually need host hardware support need to be checked
>>>> against both guest and host PMU capabilities.
>>>>
>>>> I'd propose something like this instead, leaving pmu->version unclamped:
>
> Why? What real world use case is there for KVM actively supporting a PMU version
> "greater" than the underlying hardware? E.g. emulating AnyThread deprecation is
> (presumably) easy, emulating *all* of v5 on a v3 host, not so much...

Got it. I can think of a few discrepancies:

For example, the guest is supposed to handle Freeze_LBRs_On_PMI (legacy
vs. streamlined) based on the guest PMU version. If KVM emulates the
legacy behavior, but a v4+ guest expects the streamlined one, it may
run into issues.

Similarly, for v4+ MSRs like 0x391 and 0x392, if KVM emulates them
based on the effective (clamped) PMU version, the guest will receive
unexpected #GPs for accesses that should architecturally succeed under
its configured version.

By clamping the PMU version, we either have to accept these
discrepancies, or it may be troublesome to decouple the emulation of
these cases from pmu->version.

If we don't clamp the PMU version, we can gate the emulation of a higher
PMU version feature with kvm_pmu_has_version(), which seemed easier.

Please let me know your thoughts on these trade-offs, and I will align
the patches accordingly.