Skip to content

Commit a2f4ba5

Browse files
sandip4nsean-jc
authored andcommitted
KVM: x86/pmu: Always stuff GuestOnly=1,HostOnly=0 for mediated PMCs on AMD
On AMD platforms, there is no way to restore PerfCntrGlobalCtl at VM-Entry or clear it at VM-Exit. Since the register states will be restored before entering and saved after exiting guest context, the counters can keep ticking and even overflow leading to chaos while still in host context. To avoid this, intecept event selectors, which is already done by mediated PMU. In addition, always set the GuestOnly bit and clear the HostOnly bit for PMU selectors on AMD. Doing so allows the counters run only in guest context even if their enable bits are still set after VM exit and before host/guest PMU context switch. Signed-off-by: Sandipan Das <sandipan.das@amd.com> Signed-off-by: Mingwei Zhang <mizhang@google.com> [sean: massage shortlog] Tested-by: Xudong Hao <xudong.hao@intel.com> Tested-by: Manali Shukla <manali.shukla@amd.com> Link: https://patch.msgid.link/20251206001720.468579-27-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3db871f commit a2f4ba5

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

arch/x86/kvm/svm/pmu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ 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;
169+
pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
170+
AMD64_EVENTSEL_GUESTONLY;
170171
kvm_pmu_request_counter_reprogram(pmc);
171172
}
172173
return 0;

0 commit comments

Comments
 (0)