Skip to content

Commit 8911ce6

Browse files
committed
KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max
Limit kvm_pmu_cap.num_counters_gp during kvm_init_pmu_capability() based on the vendor PMU capabilities so that consuming num_counters_gp naturally does the right thing. This fixes a mostly theoretical bug where KVM could over-report its PMU support in KVM_GET_SUPPORTED_CPUID for leaf 0xA, e.g. if the number of counters reported by perf is greater than KVM's hardcoded internal limit. Incorporating input from the AMD PMU also avoids over-reporting MSRs to save when running on AMD. Link: https://lore.kernel.org/r/20230124234905.3774678-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 2a3003e commit 8911ce6

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

arch/x86/kvm/pmu.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct kvm_pmu_ops {
3636
void (*cleanup)(struct kvm_vcpu *vcpu);
3737

3838
const u64 EVENTSEL_EVENT;
39+
const int MAX_NR_GP_COUNTERS;
3940
};
4041

4142
void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
@@ -157,7 +158,7 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
157158

158159
extern struct x86_pmu_capability kvm_pmu_cap;
159160

160-
static inline void kvm_init_pmu_capability(void)
161+
static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
161162
{
162163
bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
163164

@@ -176,6 +177,8 @@ static inline void kvm_init_pmu_capability(void)
176177
}
177178

178179
kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
180+
kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
181+
pmu_ops->MAX_NR_GP_COUNTERS);
179182
kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
180183
KVM_PMC_MAX_FIXED);
181184
}

arch/x86/kvm/svm/pmu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,5 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
232232
.init = amd_pmu_init,
233233
.reset = amd_pmu_reset,
234234
.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,
235+
.MAX_NR_GP_COUNTERS = KVM_AMD_PMC_MAX_GENERIC,
235236
};

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,4 +815,5 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {
815815
.deliver_pmi = intel_pmu_deliver_pmi,
816816
.cleanup = intel_pmu_cleanup,
817817
.EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT,
818+
.MAX_NR_GP_COUNTERS = KVM_INTEL_PMC_MAX_GENERIC,
818819
};

arch/x86/kvm/x86.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7061,12 +7061,12 @@ static void kvm_init_msr_list(void)
70617061
break;
70627062
case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX:
70637063
if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
7064-
min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp))
7064+
kvm_pmu_cap.num_counters_gp)
70657065
continue;
70667066
break;
70677067
case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX:
70687068
if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
7069-
min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp))
7069+
kvm_pmu_cap.num_counters_gp)
70707070
continue;
70717071
break;
70727072
case MSR_IA32_XFD:
@@ -9386,7 +9386,7 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
93869386
if (boot_cpu_has(X86_FEATURE_XSAVES))
93879387
rdmsrl(MSR_IA32_XSS, host_xss);
93889388

9389-
kvm_init_pmu_capability();
9389+
kvm_init_pmu_capability(ops->pmu_ops);
93909390

93919391
r = ops->hardware_setup();
93929392
if (r != 0)

0 commit comments

Comments
 (0)