Skip to contentSkip to conversation

KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

PATCH Applied Reiji Watanabe 2 participants 2 messages Mar 13, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

Presently, when a guest writes 1 to PMCR_EL0.{C,P}, which is WO/RAZ, KVM saves the register value, including these bits.
When userspace reads the register using KVM_GET_ONE_REG, KVM returns the saved register value as it is (the saved value might have these bits set). This could result in userspace setting these bits on the destination during migration. Consequently, KVM may end up resetting the vPMU counter registers (PMCCNTR_EL0 and/or PMEVCNTR<n>_EL0) to zero on the first KVM_RUN after migration.

Fix this by not saving those bits when a guest writes 1 to those bits.

Fixes ab9468340d2b ("arm64: KVM: Add access handler for PMCR register")
---
 arch/arm64/kvm/pmu-emul.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +2 -1
index 24908400e190..c243b10f3e15 100644
@@ -538,7 +538,8 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 	if (!kvm_pmu_is_3p5(vcpu))
 		val &= ~ARMV8_PMU_PMCR_LP;
 
-	__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
+	/* The reset bits don't indicate any state, and shouldn't be saved. */
+	__vcpu_sys_reg(vcpu, PMCR_EL0) = val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);
 
 	if (val & ARMV8_PMU_PMCR_E) {
 		kvm_pmu_enable_counter_mask(vcpu,
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
Applied
#1 lore raw
Quoted 10 lines · Reiji Watanabe

Presently, when a guest writes 1 to PMCR_EL0.{C,P}, which is WO/RAZ, KVM saves the register value, including these bits.
When userspace reads the register using KVM_GET_ONE_REG, KVM returns the saved register value as it is (the saved value might have these bits set). This could result in userspace setting these bits on the destination during migration. Consequently, KVM may end up resetting the vPMU counter registers (PMCCNTR_EL0 and/or PMEVCNTR<n>_EL0) to zero on the first KVM_RUN after migration.

[...]

Applied to kvmarm/fixes, thanks!

[2/2] KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

      https://git.kernel.org/kvmarm/kvmarm/c/f6da81f650fa

--
Best,
Oliver