Skip to content

Commit 6d88d0e

Browse files
committed
KVM: x86/pmu: Require nr fixed_pmc_events to match nr max fixed counters
Assert that the number of known fixed_pmc_events matches the max number of fixed counters supported by KVM, and clean up related code. Opportunistically extend setup_fixed_pmc_eventsel()'s use of array_index_nospec() to cover fixed_counters, as nr_arch_fixed_counters is set based on userspace input (but capped using KVM-controlled values). Link: https://lore.kernel.org/r/20230607010206.1425277-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent bc96589 commit 6d88d0e

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,17 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
468468

469469
static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu)
470470
{
471-
size_t size = ARRAY_SIZE(fixed_pmc_events);
472-
struct kvm_pmc *pmc;
473-
u32 event;
474471
int i;
475472

473+
BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_events) != KVM_PMC_MAX_FIXED);
474+
476475
for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
477-
pmc = &pmu->fixed_counters[i];
478-
event = fixed_pmc_events[array_index_nospec(i, size)];
476+
int index = array_index_nospec(i, KVM_PMC_MAX_FIXED);
477+
struct kvm_pmc *pmc = &pmu->fixed_counters[index];
478+
u32 event = fixed_pmc_events[index];
479+
479480
pmc->eventsel = (intel_arch_events[event].unit_mask << 8) |
480-
intel_arch_events[event].eventsel;
481+
intel_arch_events[event].eventsel;
481482
}
482483
}
483484

@@ -538,10 +539,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
538539
if (pmu->version == 1) {
539540
pmu->nr_arch_fixed_counters = 0;
540541
} else {
541-
pmu->nr_arch_fixed_counters =
542-
min3(ARRAY_SIZE(fixed_pmc_events),
543-
(size_t) edx.split.num_counters_fixed,
544-
(size_t)kvm_pmu_cap.num_counters_fixed);
542+
pmu->nr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,
543+
kvm_pmu_cap.num_counters_fixed);
545544
edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
546545
kvm_pmu_cap.bit_width_fixed);
547546
pmu->counter_bitmask[KVM_PMC_FIXED] =

0 commit comments

Comments
 (0)