Skip to content

Commit 0c2f9ac

Browse files
reijiw-kvmMarc Zyngier
authored andcommitted
KVM: arm64: PMU: Don't overwrite PMUSERENR with vcpu loaded
Currently, with VHE, KVM sets ER, CR, SW and EN bits of PMUSERENR_EL0 to 1 on vcpu_load(), and saves and restores the register value for the host on vcpu_load() and vcpu_put(). If the value of those bits are cleared on a pCPU with a vCPU loaded (armv8pmu_start() would do that when PMU counters are programmed for the guest), PMU access from the guest EL0 might be trapped to the guest EL1 directly regardless of the current PMUSERENR_EL0 value of the vCPU. Fix this by not letting armv8pmu_start() overwrite PMUSERENR_EL0 on the pCPU where PMUSERENR_EL0 for the guest is loaded, and instead updating the saved shadow register value for the host so that the value can be restored on vcpu_put() later. While vcpu_{put,load}() are manipulating PMUSERENR_EL0, disable IRQs to prevent a race condition between these processes and IPIs that attempt to update PMUSERENR_EL0 for the host EL0. Suggested-by: Mark Rutland <[email protected]> Suggested-by: Marc Zyngier <[email protected]> Fixes: 83a7a4d ("arm64: perf: Enable PMU counter userspace access for perf event") Signed-off-by: Reiji Watanabe <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 8681f71 commit 0c2f9ac

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

arch/arm/include/asm/arm_pmuv3.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
222222
return false;
223223
}
224224

225+
static inline bool kvm_set_pmuserenr(u64 val)
226+
{
227+
return false;
228+
}
229+
225230
/* PMU Version in DFR Register */
226231
#define ARMV8_PMU_DFR_VER_NI 0
227232
#define ARMV8_PMU_DFR_VER_V3P4 0x5

arch/arm64/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ struct kvm_vcpu_arch {
699699
#define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(4))
700700
/* Software step state is Active-pending */
701701
#define DBG_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(5))
702+
/* PMUSERENR for the guest EL0 is on physical CPU */
703+
#define PMUSERENR_ON_CPU __vcpu_single_flag(sflags, BIT(6))
702704

703705

704706
/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */
@@ -1065,9 +1067,14 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
10651067
#ifdef CONFIG_KVM
10661068
void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
10671069
void kvm_clr_pmu_events(u32 clr);
1070+
bool kvm_set_pmuserenr(u64 val);
10681071
#else
10691072
static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
10701073
static inline void kvm_clr_pmu_events(u32 clr) {}
1074+
static inline bool kvm_set_pmuserenr(u64 val)
1075+
{
1076+
return false;
1077+
}
10711078
#endif
10721079

10731080
void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
8989
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
9090
ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
9191
write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
92+
vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
9293
}
9394

9495
vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
@@ -116,6 +117,7 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
116117

117118
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
118119
write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
120+
vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
119121
}
120122

121123
if (cpus_have_final_cap(ARM64_SME)) {

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,28 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
9292
}
9393
NOKPROBE_SYMBOL(__deactivate_traps);
9494

95+
/*
96+
* Disable IRQs in {activate,deactivate}_traps_vhe_{load,put}() to
97+
* prevent a race condition between context switching of PMUSERENR_EL0
98+
* in __{activate,deactivate}_traps_common() and IPIs that attempts to
99+
* update PMUSERENR_EL0. See also kvm_set_pmuserenr().
100+
*/
95101
void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
96102
{
103+
unsigned long flags;
104+
105+
local_irq_save(flags);
97106
__activate_traps_common(vcpu);
107+
local_irq_restore(flags);
98108
}
99109

100110
void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu)
101111
{
112+
unsigned long flags;
113+
114+
local_irq_save(flags);
102115
__deactivate_traps_common(vcpu);
116+
local_irq_restore(flags);
103117
}
104118

105119
static const exit_handler_fn hyp_exit_handlers[] = {

arch/arm64/kvm/pmu.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,30 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)
209209
kvm_vcpu_pmu_enable_el0(events_host);
210210
kvm_vcpu_pmu_disable_el0(events_guest);
211211
}
212+
213+
/*
214+
* With VHE, keep track of the PMUSERENR_EL0 value for the host EL0 on the pCPU
215+
* where PMUSERENR_EL0 for the guest is loaded, since PMUSERENR_EL0 is switched
216+
* to the value for the guest on vcpu_load(). The value for the host EL0
217+
* will be restored on vcpu_put(), before returning to userspace.
218+
* This isn't necessary for nVHE, as the register is context switched for
219+
* every guest enter/exit.
220+
*
221+
* Return true if KVM takes care of the register. Otherwise return false.
222+
*/
223+
bool kvm_set_pmuserenr(u64 val)
224+
{
225+
struct kvm_cpu_context *hctxt;
226+
struct kvm_vcpu *vcpu;
227+
228+
if (!kvm_arm_support_pmu_v3() || !has_vhe())
229+
return false;
230+
231+
vcpu = kvm_get_running_vcpu();
232+
if (!vcpu || !vcpu_get_flag(vcpu, PMUSERENR_ON_CPU))
233+
return false;
234+
235+
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
236+
ctxt_sys_reg(hctxt, PMUSERENR_EL0) = val;
237+
return true;
238+
}

drivers/perf/arm_pmuv3.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,25 @@ static inline u32 armv8pmu_getreset_flags(void)
677677
return value;
678678
}
679679

680+
static void update_pmuserenr(u64 val)
681+
{
682+
lockdep_assert_irqs_disabled();
683+
684+
/*
685+
* The current PMUSERENR_EL0 value might be the value for the guest.
686+
* If that's the case, have KVM keep tracking of the register value
687+
* for the host EL0 so that KVM can restore it before returning to
688+
* the host EL0. Otherwise, update the register now.
689+
*/
690+
if (kvm_set_pmuserenr(val))
691+
return;
692+
693+
write_pmuserenr(val);
694+
}
695+
680696
static void armv8pmu_disable_user_access(void)
681697
{
682-
write_pmuserenr(0);
698+
update_pmuserenr(0);
683699
}
684700

685701
static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
@@ -695,8 +711,7 @@ static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
695711
armv8pmu_write_evcntr(i, 0);
696712
}
697713

698-
write_pmuserenr(0);
699-
write_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR);
714+
update_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR);
700715
}
701716

702717
static void armv8pmu_enable_event(struct perf_event *event)

0 commit comments

Comments
 (0)