[PATCH v4 1/2] KVM: arm64: Clear the VM's feature bitmap when kvm_setup_vcpu() fails

Fuad Tabba fuad.tabba at linux.dev
Sun Nov 1 23:58:42 PST 2026


KVM_ARM_VCPU_INIT records the requested features in the VM-wide bitmap
before kvm_setup_vcpu() runs, and leaves them there when setup fails
(e.g., HAS_EL2_E2H0 on a host without FEAT_NV1). The PMU attributes
read that bitmap before the vCPU is initialized, so after such a
failure with PMU_V3 requested, KVM_SET_DEVICE_ATTR on the PMU group no
longer returns -ENODEV.

Until an INIT has succeeded, every INIT that gets past the feature
checks rewrites the bitmap. After that, kvm_vcpu_init_changed()
requires the requested features to match it, so the copy is a no-op.
Clear the bitmap on failure while KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED
is clear.

Fixes: 427733579744e ("KVM: arm64: Select default PMU in KVM_ARM_VCPU_INIT handler")
Suggested-by: Oliver Upton <oupton at kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs at kernel.org>
Signed-off-by: Fuad Tabba <fuad.tabba at linux.dev>
---
 arch/arm64/kvm/arm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 157bf4b812020..3e91e01b032c4 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1856,8 +1856,13 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 	bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
 
 	ret = kvm_setup_vcpu(vcpu);
-	if (ret)
+	if (ret) {
+		/* Clear the bitmap if setup fails on the first vCPU to be initialized. */
+		if (!test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags))
+			bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
+
 		goto out_unlock;
+	}
 
 	/* Now we know what it is, we can reset it. */
 	kvm_reset_vcpu(vcpu);
-- 
2.47.3




More information about the linux-arm-kernel mailing list