Skip to content

Commit a634c76

Browse files
committed
KVM: x86/pmu: Explicitly check for RDPMC of unsupported Intel PMC types
Explicitly check for attempts to read unsupported PMC types instead of letting the bounds check fail. Functionally, letting the check fail is ok, but it's unnecessarily subtle and does a poor job of documenting the architectural behavior that KVM is emulating. Reviewed-by: Dapeng Mi  <dapeng1.mi@linux.intel.com> Tested-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20240109230250.424295-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7a0fc73 commit a634c76

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* further confuse things, non-architectural PMUs use bit 31 as a flag for
2727
* "fast" reads, whereas the "type" is an explicit value.
2828
*/
29+
#define INTEL_RDPMC_GP 0
2930
#define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE
3031

3132
#define INTEL_RDPMC_TYPE_MASK GENMASK(31, 16)
@@ -89,21 +90,29 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
8990
return NULL;
9091

9192
/*
92-
* Fixed PMCs are supported on all architectural PMUs. Note, KVM only
93-
* emulates fixed PMCs for PMU v2+, but the flag itself is still valid,
94-
* i.e. let RDPMC fail due to accessing a non-existent counter.
93+
* General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
94+
* are supported on all architectural PMUs, i.e. on all virtual PMUs
95+
* supported by KVM. Note, KVM only emulates fixed PMCs for PMU v2+,
96+
* but the type itself is still valid, i.e. let RDPMC fail due to
97+
* accessing a non-existent counter. Reject attempts to read all other
98+
* types, which are unknown/unsupported.
9599
*/
96-
idx &= ~INTEL_RDPMC_FIXED;
97-
if (type == INTEL_RDPMC_FIXED) {
100+
switch (type) {
101+
case INTEL_RDPMC_FIXED:
98102
counters = pmu->fixed_counters;
99103
num_counters = pmu->nr_arch_fixed_counters;
100104
bitmask = pmu->counter_bitmask[KVM_PMC_FIXED];
101-
} else {
105+
break;
106+
case INTEL_RDPMC_GP:
102107
counters = pmu->gp_counters;
103108
num_counters = pmu->nr_arch_gp_counters;
104109
bitmask = pmu->counter_bitmask[KVM_PMC_GP];
110+
break;
111+
default:
112+
return NULL;
105113
}
106114

115+
idx &= INTEL_RDPMC_INDEX_MASK;
107116
if (idx >= num_counters)
108117
return NULL;
109118

0 commit comments

Comments
 (0)