Skip to content

Commit 02918f0

Browse files
mzhang3579sean-jc
authored andcommitted
KVM: x86/pmu: Introduce eventsel_hw to prepare for pmu event filtering
Introduce eventsel_hw and fixed_ctr_ctrl_hw to store the actual HW value in PMU event selector MSRs. In mediated PMU checks events before allowing the event values written to the PMU MSRs. However, to match the HW behavior, when PMU event checks fails, KVM should allow guest to read the value back. This essentially requires an extra variable to separate the guest requested value from actual PMU MSR value. Note this only applies to event selectors. Signed-off-by: Mingwei Zhang <mizhang@google.com> Co-developed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Tested-by: Xudong Hao <xudong.hao@intel.com> Tested-by: Manali Shukla <manali.shukla@amd.com> Link: https://patch.msgid.link/20251206001720.468579-25-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 0ea0d63 commit 02918f0

4 files changed

Lines changed: 10 additions & 2 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ struct kvm_pmc {
529529
*/
530530
u64 emulated_counter;
531531
u64 eventsel;
532+
u64 eventsel_hw;
532533
struct perf_event *perf_event;
533534
struct kvm_vcpu *vcpu;
534535
/*
@@ -557,6 +558,7 @@ struct kvm_pmu {
557558
unsigned nr_arch_fixed_counters;
558559
unsigned available_event_types;
559560
u64 fixed_ctr_ctrl;
561+
u64 fixed_ctr_ctrl_hw;
560562
u64 fixed_ctr_ctrl_rsvd;
561563
u64 global_ctrl;
562564
u64 global_status;

arch/x86/kvm/pmu.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,14 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
900900
pmc->counter = 0;
901901
pmc->emulated_counter = 0;
902902

903-
if (pmc_is_gp(pmc))
903+
if (pmc_is_gp(pmc)) {
904904
pmc->eventsel = 0;
905+
pmc->eventsel_hw = 0;
906+
}
905907
}
906908

907-
pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = 0;
909+
pmu->fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw = 0;
910+
pmu->global_ctrl = pmu->global_status = 0;
908911

909912
kvm_pmu_call(reset)(vcpu);
910913
}

arch/x86/kvm/svm/pmu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
166166
data &= ~pmu->reserved_bits;
167167
if (data != pmc->eventsel) {
168168
pmc->eventsel = data;
169+
pmc->eventsel_hw = data;
169170
kvm_pmu_request_counter_reprogram(pmc);
170171
}
171172
return 0;

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
6161
int i;
6262

6363
pmu->fixed_ctr_ctrl = data;
64+
pmu->fixed_ctr_ctrl_hw = data;
6465
for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
6566
u8 new_ctrl = fixed_ctrl_field(data, i);
6667
u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
@@ -430,6 +431,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
430431

431432
if (data != pmc->eventsel) {
432433
pmc->eventsel = data;
434+
pmc->eventsel_hw = data;
433435
kvm_pmu_request_counter_reprogram(pmc);
434436
}
435437
break;

0 commit comments

Comments
 (0)