Skip to contentSkip to conversation

KVM: arm64: Handle hidden FEAT_PMUv3 in ID_AA64DFR0_EL1

PATCH Applied Colton Lewis 2 participants 4 messages Sep 30 – Oct 3

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

v2 cover KVM: arm64: Handle hidden FEAT_PMUv3 in ID_AA64DFR0_EL1

When userspace initializes a vCPU with KVM_ARM_VCPU_PMU_V3 and hides FEAT_PMUv3 via ID_AA64DFR0_EL1.PMUVer, KVM hits a WARN_ONCE("Unknown PMU version 0") during reset_pmevtyper() and continues to expose PMU system registers instead of emulating guest accesses as UNDEF.

Patch 1 handles ID_AA64DFR0_EL1_PMUVer_NI in __kvm_pmu_event_mask() and removes the redundant kvm_vcpu_has_pmu() check in reset_pmevtyper(). Patch 2 updates pmu_visibility() to check ID_AA64DFR0_EL1.PMUVer via kvm_has_feat() so PMU registers are marked REG_HIDDEN when FEAT_PMUv3 is not advertised.

Changes in v2:
- Handle ID_AA64DFR0_EL1_PMUVer_NI in __kvm_pmu_event_mask() and drop
the kvm_vcpu_has_pmu() check in reset_pmevtyper() (Oliver)
- Drop Cc: [email protected] (Oliver)
- Add patch 2 to check ID_AA64DFR0_EL1.PMUVer in pmu_visibility() (Oliver)
- Link to v1: https://lore.kernel.org/r/[email protected]

Colton Lewis (2):
KVM: arm64: Handle ID_AA64DFR0_EL1.PMUVer == NI in

    __kvm_pmu_event_mask()

KVM: arm64: Check ID_AA64DFR0_EL1.PMUVer in pmu_visibility()

 arch/arm64/kvm/pmu-emul.c | 5 ++++-
 arch/arm64/kvm/sys_regs.c | 6 +-----
 2 files changed, 5 insertions(+), 6 deletions(-)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#1 lore raw

PATCH v2 1/2 KVM: arm64: Handle ID_AA64DFR0_EL1.PMUVer == NI in __kvm_pmu_event_mask()

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() and calls kvm_pmu_evtyper_mask(). Because __kvm_pmu_event_mask() does not handle ID_AA64DFR0_EL1_PMUVer_NI, it triggers a WARN_ONCE("Unknown PMU version 0").

Explicitly handle ID_AA64DFR0_EL1_PMUVer_NI in __kvm_pmu_event_mask() by returning an event mask of 0, and drop the redundant kvm_vcpu_has_pmu() check in reset_pmevtyper().

Fixes bc512d6a9b92 ("KVM: arm64: Make PMEVTYPER<n>_EL0.NSH RES0 if EL2 isn't advertised")
Assisted-by LLM
---
 arch/arm64/kvm/pmu-emul.c | 5 ++++-
 arch/arm64/kvm/sys_regs.c | 4 ----
 2 files changed, 4 insertions(+), 5 deletions(-)
arch/arm64/kvm/pmu-emul.c +4 -1
index 5b1af7e2176fa..e2b28dbc0451b 100644
@@ -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 +0 -4
index a2f4e769a4282..e4ad9c171c7ca 100644
@@ -1059,10 +1059,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));
 
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#2 lore raw

PATCH v2 2/2 KVM: arm64: Check ID_AA64DFR0_EL1.PMUVer in pmu_visibility()

In reply to Colton Lewis

pmu_visibility() currently checks kvm_vcpu_has_pmu(), which only tests whether the vCPU was initialized with KVM_ARM_VCPU_PMU_V3. If userspace initializes a vCPU with KVM_ARM_VCPU_PMU_V3 and subsequently hides FEAT_PMUv3 via ID_AA64DFR0_EL1.PMUVer, KVM continues to expose the PMU system registers instead of emulating guest accesses as UNDEF.

Check ID_AA64DFR0_EL1.PMUVer directly via kvm_has_feat() in pmu_visibility() so that PMU system registers are marked REG_HIDDEN whenever FEAT_PMUv3 is not advertised in the ID registers.

---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
index e4ad9c171c7ca..46dd6e16de28e 100644
@@ -1029,7 +1029,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;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
maintainer Applied
#3 lore raw
In reply to Colton Lewis
Quoted 5 lines · Colton Lewis

When userspace initializes a vCPU with KVM_ARM_VCPU_PMU_V3 and hides FEAT_PMUv3 via ID_AA64DFR0_EL1.PMUVer, KVM hits a WARN_ONCE("Unknown PMU version 0") during reset_pmevtyper() and continues to expose PMU system registers instead of emulating guest accesses as UNDEF.

Quoted 7 lines

Patch 1 handles ID_AA64DFR0_EL1_PMUVer_NI in __kvm_pmu_event_mask() and removes the redundant kvm_vcpu_has_pmu() check in reset_pmevtyper(). Patch 2 updates pmu_visibility() to check ID_AA64DFR0_EL1.PMUVer via kvm_has_feat() so PMU registers are marked REG_HIDDEN when FEAT_PMUv3 is not advertised.

[...]

Applied to next, thanks!

[1/2] KVM: arm64: Handle ID_AA64DFR0_EL1.PMUVer == NI in __kvm_pmu_event_mask()

      commit: aa41a7c3beb59f9dac4e60c9cf1d146f6c9025c4
[2/2] KVM: arm64: Check ID_AA64DFR0_EL1.PMUVer in pmu_visibility()
      commit: 517cdec3d4b28ccf5dae2dd03dee2a0a1eef1981

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.