From: Fuad Tabba <fuad.tabba@linux.dev>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>
Cc: Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>, Will Deacon <will@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
Jack Thomson <jackabt@amazon.com>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Fuad Tabba <tabba@google.com>
Subject: [PATCH v2] KVM: arm64: Restore the VM's feature bitmap when kvm_setup_vcpu() fails
Date: Mon, 21 Sep 2026 07:37:18 +0100 [thread overview]
Message-ID: <20260921063718.1604533-1-fuad.tabba@linux.dev> (raw)
__kvm_vcpu_set_target() copies the requested features into the VM-wide
bitmap before kvm_setup_vcpu() runs and doesn't undo it when setup
fails, so a rejected KVM_ARM_VCPU_INIT leaves the VM recording features
that were never set up. With HAS_EL2 | HAS_EL2_E2H0 on a host without
FEAT_NV1, kvm_vcpu_init_nested() returns -EINVAL before it allocates any
nested stage-2 MMU, and vcpu_has_nv() is then true with
nested_mmus_size == 0; its -ENOMEM paths do the same on a VM's first
INIT.
Nothing in the tree loads a vCPU whose init failed, so this is latent.
The upcoming series that enables KVM_PRE_FAULT_MEMORY for arm64 exposes
it: the generic kvm_vcpu_pre_fault_memory() calls vcpu_load() whether
or not the vCPU has been initialised. After the rejected INIT, the first
call's vcpu_load() finds hw_mmu still set to the canonical MMU and
leaves it alone, but its vcpu_put() takes the vcpu_has_nv() branch into
kvm_vcpu_put_hw_mmu(), which clears hw_mmu. The second call's
vcpu_load() finds hw_mmu NULL and takes the nested branch into
get_s2_mmu_nested(), whose search over nested_mmus_size == 0 leaves
s2_mmu NULL for the BUG_ON(atomic_read(&s2_mmu->refcnt)), under
mmu_lock. On kvmarm/next with the series applied:
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000074
Call trace:
kvm_vcpu_load_hw_mmu (arch/arm64/kvm/nested.c:891) (P)
kvm_arch_vcpu_load (arch/arm64/kvm/arm.c:662)
kvm_vcpu_pre_fault_memory (virt/kvm/kvm_main.c:170 virt/kvm/kvm_main.c:4349)
kvm_vcpu_ioctl (virt/kvm/kvm_main.c:4639)
Setup reads the VM-wide bitmap, so the copy can't be deferred; restore
the previous value instead when kvm_setup_vcpu() fails.
Fixes: 427733579744e ("KVM: arm64: Select default PMU in KVM_ARM_VCPU_INIT handler")
Link: https://lore.kernel.org/r/20260825-kvm-arm-prefault-v1-0-befe8947702e@kernel.org/
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
v2:
- Commit message: say what the prefault series enables, bring the
two-ioctl walk and the trace up from below the fold, the trace
decoded, and drop the Fixes: on 1de10b7d13a97, which had nothing
fallible after the copy (Lorenzo).
- Fold Lorenzo's Reviewed-by.
Reproduced on kvmarm/next plus the series under QEMU (-cpu max with an
Apple M2 MIDR, which has_nv1() denies; kvm-arm.mode=nested):
KVM_ARM_VCPU_INIT with HAS_EL2 | HAS_EL2_E2H0 returns -EINVAL, then
KVM_PRE_FAULT_MEMORY twice. With the fix both calls return -ENOENT and
the host is unaffected. Applies unchanged to v7.3-rc3.
v1: https://lore.kernel.org/r/20260918120553.163139-1-fuad.tabba@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 eaf583b771931..b25725f91c925 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1685,6 +1685,7 @@ static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
const struct kvm_vcpu_init *init)
{
+ DECLARE_BITMAP(old_features, KVM_VCPU_MAX_FEATURES);
unsigned long features = init->features[0];
struct kvm *kvm = vcpu->kvm;
int ret = -EINVAL;
@@ -1695,11 +1696,15 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
kvm_vcpu_init_changed(vcpu, init))
goto out_unlock;
+ /* Setup reads the VM-wide bitmap, so undo the copy if setup fails. */
+ bitmap_copy(old_features, kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
ret = kvm_setup_vcpu(vcpu);
- if (ret)
+ if (ret) {
+ bitmap_copy(kvm->arch.vcpu_features, old_features, KVM_VCPU_MAX_FEATURES);
goto out_unlock;
+ }
/* Now we know what it is, we can reset it. */
kvm_reset_vcpu(vcpu);
base-commit: 089e4f3c4862ba3f29dff2361caa8084879194fd
--
2.39.5
next reply other threads:[~2026-09-21 6:37 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 6:37 Fuad Tabba [this message]
2026-09-21 7:09 ` [PATCH v2] KVM: arm64: Restore the VM's feature bitmap when kvm_setup_vcpu() fails Oliver Upton
2026-09-21 7:24 ` Fuad Tabba
2026-09-21 11:42 ` Lorenzo Stoakes (ARM)
2026-09-21 13:34 ` Lorenzo Stoakes (ARM)
2026-09-21 18:39 ` Oliver Upton
2026-09-22 7:31 ` Lorenzo Stoakes (ARM)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260921063718.1604533-1-fuad.tabba@linux.dev \
--to=fuad.tabba@linux.dev \
--cc=jackabt@amazon.com \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.