]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: x86/pmu: Limit the maximum number of supported AMD GP counters
authorLike Xu <likexu@tencent.com>
Mon, 19 Sep 2022 09:10:08 +0000 (17:10 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 9 Nov 2022 17:26:54 +0000 (12:26 -0500)
The AMD PerfMonV2 specification allows for a maximum of 16 GP counters,
but currently only 6 pairs of MSRs are accepted by KVM.

While AMD64_NUM_COUNTERS_CORE is already equal to 6, increasing without
adjusting msrs_to_save_all[] could result in out-of-bounds accesses.
Therefore introduce a macro (named KVM_AMD_PMC_MAX_GENERIC) to
refer to the number of counters supported by KVM.

Signed-off-by: Like Xu <likexu@tencent.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Message-Id: <20220919091008.60695-3-likexu@tencent.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/svm/pmu.c
arch/x86/kvm/x86.c

index 286f6130dcb475233f48359b478f46ccffceb3a1..f05ebaa26f0ff52a43294ea0c9e60175037c123e 100644 (file)
@@ -506,6 +506,7 @@ struct kvm_pmc {
 #define MSR_ARCH_PERFMON_PERFCTR_MAX   (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
 #define MSR_ARCH_PERFMON_EVENTSEL_MAX  (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
 #define KVM_PMC_MAX_FIXED      3
+#define KVM_AMD_PMC_MAX_GENERIC        6
 struct kvm_pmu {
        unsigned nr_arch_gp_counters;
        unsigned nr_arch_fixed_counters;
index b68956299fa8ec1240a280a7e3718a41a154efcd..9d65cd095691be1aad51b745eb4c7d5dba992dfe 100644 (file)
@@ -192,9 +192,10 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
        int i;
 
-       BUILD_BUG_ON(AMD64_NUM_COUNTERS_CORE > INTEL_PMC_MAX_GENERIC);
+       BUILD_BUG_ON(KVM_AMD_PMC_MAX_GENERIC > AMD64_NUM_COUNTERS_CORE);
+       BUILD_BUG_ON(KVM_AMD_PMC_MAX_GENERIC > INTEL_PMC_MAX_GENERIC);
 
-       for (i = 0; i < AMD64_NUM_COUNTERS_CORE ; i++) {
+       for (i = 0; i < KVM_AMD_PMC_MAX_GENERIC ; i++) {
                pmu->gp_counters[i].type = KVM_PMC_GP;
                pmu->gp_counters[i].vcpu = vcpu;
                pmu->gp_counters[i].idx = i;
@@ -207,7 +208,7 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
        int i;
 
-       for (i = 0; i < AMD64_NUM_COUNTERS_CORE; i++) {
+       for (i = 0; i < KVM_AMD_PMC_MAX_GENERIC; i++) {
                struct kvm_pmc *pmc = &pmu->gp_counters[i];
 
                pmc_stop_counter(pmc);
index 1e25f410d1fea3dfa8dec0652891302c2a9f35ab..ecea83f0da498fbba6e4f1887012cbd59fa95390 100644 (file)
@@ -1452,10 +1452,13 @@ static const u32 msrs_to_save_all[] = {
 
        MSR_K7_EVNTSEL0, MSR_K7_EVNTSEL1, MSR_K7_EVNTSEL2, MSR_K7_EVNTSEL3,
        MSR_K7_PERFCTR0, MSR_K7_PERFCTR1, MSR_K7_PERFCTR2, MSR_K7_PERFCTR3,
+
+       /* This part of MSRs should match KVM_AMD_PMC_MAX_GENERIC. */
        MSR_F15H_PERF_CTL0, MSR_F15H_PERF_CTL1, MSR_F15H_PERF_CTL2,
        MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5,
        MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2,
        MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5,
+
        MSR_IA32_XFD, MSR_IA32_XFD_ERR,
 };