KVM: arm64: Check ID_AA64DFR0_EL1.PMUVer in reset_pmevtyper()
Subsystems (3)
PATCH 1/1 KVM: arm64: Check ID_AA64DFR0_EL1.PMUVer in reset_pmevtyper()
When userspace initializes a vCPU with KVM_ARM_VCPU_PMU_V3 but sets ID_AA64DFR0_EL1.PMUVer to NI (0) or IMP_DEF (0xf, which is sanitized to NI), resetting the vCPU invokes reset_pmevtyper(). Because reset_pmevtyper() only checks kvm_vcpu_has_pmu(), it proceeds to call kvm_pmu_evtyper_mask(), which reads PMUVer == 0 from ID_AA64DFR0_EL1 and triggers a WARN_ONCE("Unknown PMU version 0") in __kvm_pmu_event_mask().
Check ID_AA64DFR0_EL1.PMUVer directly in reset_pmevtyper() via kvm_has_feat() instead of kvm_vcpu_has_pmu() so that PMEVTYPER<n>_EL0 resets to 0 without querying the event mask when PMUv3 is not exposed to the guest.
--- arch/arm64/kvm/sys_regs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
@@ -1060,7 +1060,7 @@ static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)static u64 reset_pmevtyper (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) {/* This thing will UNDEF, who cares about the reset value? */ -if (!kvm_vcpu_has_pmu (vcpu)) +if (!kvm_has_feat (vcpu->kvm,ID_AA64DFR0_EL1 , PMUVer,IMP ))return 0 ;reset_unknown (vcpu, r);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Oliver Upton
maintainerHi Colton,
Quoted 11 lines · Colton Lewis
When userspace initializes a vCPU with
KVM_ARM_VCPU_PMU_V3but sets ID_AA64DFR0_EL1.PMUVer to NI (0) orIMP_DEF(0xf, which is sanitized to NI), resetting the vCPU invokesreset_pmevtyper(). Becausereset_pmevtyper()only checkskvm_vcpu_has_pmu(), it proceeds to callkvm_pmu_evtyper_mask(), which reads PMUVer == 0 fromID_AA64DFR0_EL1and triggers aWARN_ONCE("Unknown PMU version 0")in__kvm_pmu_event_mask().Check ID_AA64DFR0_EL1.PMUVer directly in
reset_pmevtyper()viakvm_has_feat()instead ofkvm_vcpu_has_pmu()so that PMEVTYPER<n>_EL0 resets to 0 without querying the event mask when PMUv3 is not exposed to the guest.
So this goes a bit further than just this WARN_ONCE()... Ultimately if FEAT_PMUv3 is hidden via the ID registers then we should expect KVM to emulate these sysregs as UNDEF.
Quoted 3 lines
Fixes bc512d6a9b92 ("KVM: arm64: Make PMEVTYPER<n>_EL0.NSH RES0 if EL2 isn't advertised")Suggested-by Oliver Upton <[email protected]>
Avoiding a WARN_ONCE() is not stable material.
Quoted 11 lines
Assisted-by LLMSigned-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/sys_regs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm64/kvm/sys_regs.c +0
-0
arch/arm64/kvm/sys_regs.c static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) · line 1064
Quoted 6 lines
arch/arm64/kvm/sys_regs.c +1
-1@@ -1060,7 +1060,7 @@ static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)static u64 reset_pmevtyper (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) {/* This thing will UNDEF, who cares about the reset value? */ -if (!kvm_vcpu_has_pmu (vcpu)) +if (!kvm_has_feat (vcpu->kvm,ID_AA64DFR0_EL1 , PMUVer,IMP ))return 0 ;
I have this diff locally, separating the visibility change into a separate patch. Do you want to give it a spin?
Thanks,
Oliver
arch/arm64/kvm/pmu-emul.c +4 -1
@@ -50,8 +50,11 @@ static u32 __kvm_pmu_event_mask(unsigned int pmuver)case ID_AA64DFR0_EL1_PMUVer_V3P5:case ID_AA64DFR0_EL1_PMUVer_V3P7:return GENMASK (15 ,0 ); -default :/* Shouldn't be here, just for sanity */ +default : +/* Shouldn't be here, just for sanity */ WARN_ONCE (1 ,"Unknown PMU version %d\n" , pmuver); + fallthrough; +case ID_AA64DFR0_EL1_PMUVer_NI:return 0 ; } }
arch/arm64/kvm/sys_regs.c +1 -5
@@ -1028,7 +1028,7 @@ static unsigned int hidden_visibility(const struct kvm_vcpu *vcpu,static unsigned int pmu_visibility (const struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) { -if (kvm_vcpu_has_pmu (vcpu)) +if (kvm_has_feat (vcpu->kvm,ID_AA64DFR0_EL1 , PMUVer,IMP ))return 0 ;return REG_HIDDEN ;
@@ -1058,10 +1058,6 @@ static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)static u64 reset_pmevtyper (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) { -/* This thing will UNDEF, who cares about the reset value? */ -if (!kvm_vcpu_has_pmu (vcpu)) -return 0 ; -reset_unknown (vcpu, r); __vcpu_rmw_sys_reg(vcpu, r->reg, &=, kvm_pmu_evtyper_mask(vcpu->kvm));