]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - pending-4.19/kvm-x86-pmu-mask-the-result-of-rdpmc-according-to-th.patch
move existing queues out of the way for the moment...
[thirdparty/kernel/stable-queue.git] / pending-4.19 / kvm-x86-pmu-mask-the-result-of-rdpmc-according-to-th.patch
1 From be536c2e23dc305f7ac01a9474b170bafa6ed761 Mon Sep 17 00:00:00 2001
2 From: Paolo Bonzini <pbonzini@redhat.com>
3 Date: Mon, 20 May 2019 17:20:40 +0200
4 Subject: KVM: x86/pmu: mask the result of rdpmc according to the width of the
5 counters
6
7 [ Upstream commit 0e6f467ee28ec97f68c7b74e35ec1601bb1368a7 ]
8
9 This patch will simplify the changes in the next, by enforcing the
10 masking of the counters to RDPMC and RDMSR.
11
12 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
13 Signed-off-by: Sasha Levin <sashal@kernel.org>
14 ---
15 arch/x86/kvm/pmu.c | 10 +++-------
16 arch/x86/kvm/pmu.h | 3 ++-
17 arch/x86/kvm/pmu_amd.c | 2 +-
18 arch/x86/kvm/pmu_intel.c | 13 +++++++++----
19 4 files changed, 15 insertions(+), 13 deletions(-)
20
21 diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
22 index 58ead7db71a3..952aebd0a8a3 100644
23 --- a/arch/x86/kvm/pmu.c
24 +++ b/arch/x86/kvm/pmu.c
25 @@ -282,20 +282,16 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
26 {
27 bool fast_mode = idx & (1u << 31);
28 struct kvm_pmc *pmc;
29 - u64 ctr_val;
30 + u64 mask = fast_mode ? ~0u : ~0ull;
31
32 if (is_vmware_backdoor_pmc(idx))
33 return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
34
35 - pmc = kvm_x86_ops->pmu_ops->msr_idx_to_pmc(vcpu, idx);
36 + pmc = kvm_x86_ops->pmu_ops->msr_idx_to_pmc(vcpu, idx, &mask);
37 if (!pmc)
38 return 1;
39
40 - ctr_val = pmc_read_counter(pmc);
41 - if (fast_mode)
42 - ctr_val = (u32)ctr_val;
43 -
44 - *data = ctr_val;
45 + *data = pmc_read_counter(pmc) & mask;
46 return 0;
47 }
48
49 diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
50 index ba8898e1a854..22dff661145a 100644
51 --- a/arch/x86/kvm/pmu.h
52 +++ b/arch/x86/kvm/pmu.h
53 @@ -25,7 +25,8 @@ struct kvm_pmu_ops {
54 unsigned (*find_fixed_event)(int idx);
55 bool (*pmc_is_enabled)(struct kvm_pmc *pmc);
56 struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx);
57 - struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, unsigned idx);
58 + struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, unsigned idx,
59 + u64 *mask);
60 int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu, unsigned idx);
61 bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
62 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
63 diff --git a/arch/x86/kvm/pmu_amd.c b/arch/x86/kvm/pmu_amd.c
64 index 1495a735b38e..41dff881e0f0 100644
65 --- a/arch/x86/kvm/pmu_amd.c
66 +++ b/arch/x86/kvm/pmu_amd.c
67 @@ -186,7 +186,7 @@ static int amd_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
68 }
69
70 /* idx is the ECX register of RDPMC instruction */
71 -static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsigned idx)
72 +static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *mask)
73 {
74 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
75 struct kvm_pmc *counters;
76 diff --git a/arch/x86/kvm/pmu_intel.c b/arch/x86/kvm/pmu_intel.c
77 index 5ab4a364348e..ad7ea81fbfbf 100644
78 --- a/arch/x86/kvm/pmu_intel.c
79 +++ b/arch/x86/kvm/pmu_intel.c
80 @@ -126,7 +126,7 @@ static int intel_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
81 }
82
83 static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
84 - unsigned idx)
85 + unsigned idx, u64 *mask)
86 {
87 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
88 bool fixed = idx & (1u << 30);
89 @@ -138,6 +138,7 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
90 if (fixed && idx >= pmu->nr_arch_fixed_counters)
91 return NULL;
92 counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
93 + *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
94
95 return &counters[idx];
96 }
97 @@ -183,9 +184,13 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
98 *data = pmu->global_ovf_ctrl;
99 return 0;
100 default:
101 - if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
102 - (pmc = get_fixed_pmc(pmu, msr))) {
103 - *data = pmc_read_counter(pmc);
104 + if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0))) {
105 + u64 val = pmc_read_counter(pmc);
106 + *data = val & pmu->counter_bitmask[KVM_PMC_GP];
107 + return 0;
108 + } else if ((pmc = get_fixed_pmc(pmu, msr))) {
109 + u64 val = pmc_read_counter(pmc);
110 + *data = val & pmu->counter_bitmask[KVM_PMC_FIXED];
111 return 0;
112 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
113 *data = pmc->eventsel;
114 --
115 2.20.1
116