Skip to content

Commit d652981

Browse files
committed
KVM: x86/pmu: Apply "fast" RDPMC only to Intel PMUs
Move the handling of "fast" RDPMC instructions, which drop bits 63:32 of the count, to Intel. The "fast" flag, and all modifiers for that matter, are Intel-only and aren't supported by AMD. Opportunistically replace open coded bit crud with proper #defines, and add comments to try and disentangle the flags vs. values mess for non-architectural vs. architectural PMUs. Fixes: ca72430 ("KVM: x86/vPMU: Implement AMD vPMU code for KVM") Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Tested-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20240109230250.424295-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7bb7fce commit d652981

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

arch/x86/kvm/pmu.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,9 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
576576

577577
int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
578578
{
579-
bool fast_mode = idx & (1u << 31);
580579
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
581580
struct kvm_pmc *pmc;
582-
u64 mask = fast_mode ? ~0u : ~0ull;
581+
u64 mask = ~0ull;
583582

584583
if (!pmu->version)
585584
return 1;

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
#include "nested.h"
2121
#include "pmu.h"
2222

23+
/*
24+
* Perf's "BASE" is wildly misleading, architectural PMUs use bits 31:16 of ECX
25+
* to encode the "type" of counter to read, i.e. this is not a "base". And to
26+
* further confuse things, non-architectural PMUs use bit 31 as a flag for
27+
* "fast" reads, whereas the "type" is an explicit value.
28+
*/
29+
#define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE
30+
#define INTEL_RDPMC_FAST BIT(31)
31+
2332
#define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0)
2433

2534
static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
@@ -59,11 +68,14 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
5968
unsigned int idx, u64 *mask)
6069
{
6170
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
62-
bool fixed = idx & (1u << 30);
71+
bool fixed = idx & INTEL_RDPMC_FIXED;
6372
struct kvm_pmc *counters;
6473
unsigned int num_counters;
6574

66-
idx &= ~(3u << 30);
75+
if (idx & INTEL_RDPMC_FAST)
76+
*mask &= GENMASK_ULL(31, 0);
77+
78+
idx &= ~(INTEL_RDPMC_FIXED | INTEL_RDPMC_FAST);
6779
if (fixed) {
6880
counters = pmu->fixed_counters;
6981
num_counters = pmu->nr_arch_fixed_counters;

0 commit comments

Comments
 (0)