Skip to content

Commit f287bef

Browse files
Dapeng Misean-jc
authored andcommitted
KVM: x86/pmu: Introduce distinct macros for GP/fixed counter max number
Refine the macros which define maximum General Purpose (GP) and fixed counter numbers. Currently the macro KVM_INTEL_PMC_MAX_GENERIC is used to represent the maximum supported General Purpose (GP) counter number ambiguously across Intel and AMD platforms. This would cause issues if AMD begins to support more GP counters than Intel. Thus a bunch of new macros including vendor specific and vendor independent are introduced to replace the old macros. The vendor independent macros are used in x86 common code to hide vendor difference and eliminate the ambiguity. No logic changes are introduced in this patch. Signed-off-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]>
1 parent 3b65a69 commit f287bef

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,16 @@ struct kvm_pmc {
533533
};
534534

535535
/* More counters may conflict with other existing Architectural MSRs */
536-
#define KVM_INTEL_PMC_MAX_GENERIC 8
537-
#define MSR_ARCH_PERFMON_PERFCTR_MAX (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
538-
#define MSR_ARCH_PERFMON_EVENTSEL_MAX (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
539-
#define KVM_PMC_MAX_FIXED 3
540-
#define MSR_ARCH_PERFMON_FIXED_CTR_MAX (MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_PMC_MAX_FIXED - 1)
541-
#define KVM_AMD_PMC_MAX_GENERIC 6
536+
#define KVM_MAX(a, b) ((a) >= (b) ? (a) : (b))
537+
#define KVM_MAX_NR_INTEL_GP_COUNTERS 8
538+
#define KVM_MAX_NR_AMD_GP_COUNTERS 6
539+
#define KVM_MAX_NR_GP_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
540+
KVM_MAX_NR_AMD_GP_COUNTERS)
541+
542+
#define KVM_MAX_NR_INTEL_FIXED_COUTNERS 3
543+
#define KVM_MAX_NR_AMD_FIXED_COUTNERS 0
544+
#define KVM_MAX_NR_FIXED_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUTNERS, \
545+
KVM_MAX_NR_AMD_FIXED_COUTNERS)
542546

543547
struct kvm_pmu {
544548
u8 version;
@@ -554,8 +558,8 @@ struct kvm_pmu {
554558
u64 global_status_rsvd;
555559
u64 reserved_bits;
556560
u64 raw_event_mask;
557-
struct kvm_pmc gp_counters[KVM_INTEL_PMC_MAX_GENERIC];
558-
struct kvm_pmc fixed_counters[KVM_PMC_MAX_FIXED];
561+
struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
562+
struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
559563

560564
/*
561565
* Overlay the bitmap with a 64-bit atomic so that all bits can be

arch/x86/kvm/pmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {
6969
* code. Each pmc, stored in kvm_pmc.idx field, is unique across
7070
* all perf counters (both gp and fixed). The mapping relationship
7171
* between pmc and perf counters is as the following:
72-
* * Intel: [0 .. KVM_INTEL_PMC_MAX_GENERIC-1] <=> gp counters
72+
* * Intel: [0 .. KVM_MAX_NR_INTEL_GP_COUNTERS-1] <=> gp counters
7373
* [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + 2] <=> fixed
7474
* * AMD: [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H
7575
* and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] <=> gp counters

arch/x86/kvm/pmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
219219
kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
220220
pmu_ops->MAX_NR_GP_COUNTERS);
221221
kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
222-
KVM_PMC_MAX_FIXED);
222+
KVM_MAX_NR_FIXED_COUNTERS);
223223

224224
kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
225225
perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);

arch/x86/kvm/svm/pmu.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)
217217
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
218218
int i;
219219

220-
BUILD_BUG_ON(KVM_AMD_PMC_MAX_GENERIC > AMD64_NUM_COUNTERS_CORE);
221-
BUILD_BUG_ON(KVM_AMD_PMC_MAX_GENERIC > INTEL_PMC_MAX_GENERIC);
220+
BUILD_BUG_ON(KVM_MAX_NR_AMD_GP_COUNTERS > AMD64_NUM_COUNTERS_CORE);
222221

223-
for (i = 0; i < KVM_AMD_PMC_MAX_GENERIC ; i++) {
222+
for (i = 0; i < KVM_MAX_NR_AMD_GP_COUNTERS; i++) {
224223
pmu->gp_counters[i].type = KVM_PMC_GP;
225224
pmu->gp_counters[i].vcpu = vcpu;
226225
pmu->gp_counters[i].idx = i;
@@ -238,6 +237,6 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
238237
.refresh = amd_pmu_refresh,
239238
.init = amd_pmu_init,
240239
.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,
241-
.MAX_NR_GP_COUNTERS = KVM_AMD_PMC_MAX_GENERIC,
240+
.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,
242241
.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,
243242
};

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
436436
};
437437
u64 eventsel;
438438

439-
BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_perf_ids) != KVM_PMC_MAX_FIXED);
440-
BUILD_BUG_ON(index >= KVM_PMC_MAX_FIXED);
439+
BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_perf_ids) != KVM_MAX_NR_INTEL_FIXED_COUTNERS);
440+
BUILD_BUG_ON(index >= KVM_MAX_NR_INTEL_FIXED_COUTNERS);
441441

442442
/*
443443
* Yell if perf reports support for a fixed counter but perf doesn't
@@ -570,14 +570,14 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
570570
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
571571
struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
572572

573-
for (i = 0; i < KVM_INTEL_PMC_MAX_GENERIC; i++) {
573+
for (i = 0; i < KVM_MAX_NR_INTEL_GP_COUNTERS; i++) {
574574
pmu->gp_counters[i].type = KVM_PMC_GP;
575575
pmu->gp_counters[i].vcpu = vcpu;
576576
pmu->gp_counters[i].idx = i;
577577
pmu->gp_counters[i].current_config = 0;
578578
}
579579

580-
for (i = 0; i < KVM_PMC_MAX_FIXED; i++) {
580+
for (i = 0; i < KVM_MAX_NR_INTEL_FIXED_COUTNERS; i++) {
581581
pmu->fixed_counters[i].type = KVM_PMC_FIXED;
582582
pmu->fixed_counters[i].vcpu = vcpu;
583583
pmu->fixed_counters[i].idx = i + KVM_FIXED_PMC_BASE_IDX;
@@ -737,6 +737,6 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {
737737
.deliver_pmi = intel_pmu_deliver_pmi,
738738
.cleanup = intel_pmu_cleanup,
739739
.EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT,
740-
.MAX_NR_GP_COUNTERS = KVM_INTEL_PMC_MAX_GENERIC,
740+
.MAX_NR_GP_COUNTERS = KVM_MAX_NR_INTEL_GP_COUNTERS,
741741
.MIN_NR_GP_COUNTERS = 1,
742742
};

arch/x86/kvm/x86.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ static const u32 msrs_to_save_pmu[] = {
14731473
MSR_CORE_PERF_GLOBAL_CTRL,
14741474
MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
14751475

1476-
/* This part of MSRs should match KVM_INTEL_PMC_MAX_GENERIC. */
1476+
/* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */
14771477
MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
14781478
MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
14791479
MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
@@ -1486,7 +1486,7 @@ static const u32 msrs_to_save_pmu[] = {
14861486
MSR_K7_EVNTSEL0, MSR_K7_EVNTSEL1, MSR_K7_EVNTSEL2, MSR_K7_EVNTSEL3,
14871487
MSR_K7_PERFCTR0, MSR_K7_PERFCTR1, MSR_K7_PERFCTR2, MSR_K7_PERFCTR3,
14881488

1489-
/* This part of MSRs should match KVM_AMD_PMC_MAX_GENERIC. */
1489+
/* This part of MSRs should match KVM_MAX_NR_AMD_GP_COUNTERS. */
14901490
MSR_F15H_PERF_CTL0, MSR_F15H_PERF_CTL1, MSR_F15H_PERF_CTL2,
14911491
MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5,
14921492
MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2,
@@ -7420,17 +7420,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)
74207420
intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
74217421
return;
74227422
break;
7423-
case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX:
7423+
case MSR_ARCH_PERFMON_PERFCTR0 ...
7424+
MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
74247425
if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
74257426
kvm_pmu_cap.num_counters_gp)
74267427
return;
74277428
break;
7428-
case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX:
7429+
case MSR_ARCH_PERFMON_EVENTSEL0 ...
7430+
MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:
74297431
if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >=
74307432
kvm_pmu_cap.num_counters_gp)
74317433
return;
74327434
break;
7433-
case MSR_ARCH_PERFMON_FIXED_CTR0 ... MSR_ARCH_PERFMON_FIXED_CTR_MAX:
7435+
case MSR_ARCH_PERFMON_FIXED_CTR0 ...
7436+
MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:
74347437
if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >=
74357438
kvm_pmu_cap.num_counters_fixed)
74367439
return;
@@ -7461,7 +7464,7 @@ static void kvm_init_msr_lists(void)
74617464
{
74627465
unsigned i;
74637466

7464-
BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 3,
7467+
BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
74657468
"Please update the fixed PMCs in msrs_to_save_pmu[]");
74667469

74677470
num_msrs_to_save = 0;

0 commit comments

Comments
 (0)