[PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition()

Dan Carpenter dan at pereq.eu
Wed Nov 18 05:05:54 PST 2026


With the KVM_ARM_VCPU_PMU_V3_STRICT feature, kvm->arch.arm_pmu stays
NULL until userspace selects a PMU with the KVM_ARM_VCPU_PMU_V3_SET_PMU
attribute.  The KVM_SET_VCPU_EVENTS ioctl can inject an SError or
external abort before that which leads to a NULL pointer dereference.

Unable to handle kernel NULL pointer dereference at virtual address 00000000000001e8
  kvm_pmu_create_perf_event (arch/arm64/kvm/pmu-emul.c:710) (P)
  kvm_pmu_nested_transition (arch/arm64/kvm/pmu-emul.c:1357)
  kvm_inject_nested (arch/arm64/kvm/emulate-nested.c:2927)
  kvm_inject_nested_serror (arch/arm64/kvm/emulate-nested.c:2979)
  kvm_inject_serror_esr (arch/arm64/kvm/inject_fault.c:365)
  __kvm_arm_vcpu_set_events (arch/arm64/kvm/guest.c:827)
  kvm_arch_vcpu_ioctl (arch/arm64/kvm/arm.c:2161)
  kvm_vcpu_ioctl (arch/arm64/kvm/../../../virt/kvm/kvm_main.c:4772)

Return early if the PMU hasn't been initialized to fix this.  After all,
we can't transition a PMU that doesn't exist.

Reported-by: Sashiko <sashiko-bot at kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-217134@kernel.org/
Fixes: ad220e275c39 ("KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature")
Cc: stable at vger.kernel.org
Signed-off-by: Dan Carpenter <dan at pereq.eu>
---
v2: This is a new patch.

 arch/arm64/kvm/pmu-emul.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index e2b28dbc0451..388a6244e7b2 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -1342,6 +1342,9 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
 	unsigned long mask;
 	int i;
 
+	if (!vcpu->arch.pmu.created)
+		return;
+
 	mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
 	for_each_set_bit(i, &mask, 32) {
 		struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
-- 
2.53.0




More information about the linux-arm-kernel mailing list