mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode
@ 2026-11-30 14:44 Fuad Tabba
  2026-11-30 14:44 ` [PATCH 1/3] KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run Fuad Tabba
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Fuad Tabba @ 2026-11-30 14:44 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
	Catalin Marinas, Will Deacon, Mark Rutland, Vincent Donnefort,
	Fuad Tabba, linux-arm-kernel, kvmarm, linux-kernel

Hi folks,

KVM marks a vCPU as uninitialized once its guest has entered a
disallowed AArch32 mode, and the VMM has to call KVM_ARM_VCPU_INIT
before it can run the vCPU again. This series fixes how
KVM_ARM_VCPU_INIT and KVM_RUN handle such a vCPU.

KVM_ARM_VCPU_INIT sets the vCPU up a second time, as if it never ran.
For a nested vCPU, this can free the VNCR page that the virtual timer
still reads, and for any vCPU it resets MPIDR_EL1 and CLIDR_EL1 a
second time. With the first patch, a vCPU that has run is only reset.

KVM_RUN checks that a vCPU is initialized only when the thread issuing
it changes, so the same thread can still run the vCPU. The second
patch makes KVM_RUN check every time. For a protected vCPU, EL2 marks
its own copy of the vCPU but never checks it. With the third patch,
EL2 doesn't run a vCPU that it has marked.

Tested on QEMU. The KASAN report in the first patch is from a host
booted with kvm-arm.mode=nested, with fault injection failing the
nested MMU allocation when the VMM calls KVM_ARM_VCPU_INIT again. With
the series, KVM_ARM_VCPU_INIT doesn't reach that allocation, MPIDR_EL1
keeps the value that the VMM wrote, and a protected guest that enters
AArch32 EL0 doesn't run again.

Based on Linux 7.4-rc3 (aebb727447640).

Cheers,
/fuad

Fuad Tabba (3):
  KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run
  KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized
  KVM: arm64: Don't run a protected vCPU that was caught in AArch32

 arch/arm64/kvm/arm.c               | 8 +++++++-
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++++
 arch/arm64/kvm/hyp/nvhe/switch.c   | 7 +++----
 3 files changed, 14 insertions(+), 5 deletions(-)


base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run
  2026-11-30 14:44 [PATCH 0/3] KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode Fuad Tabba
@ 2026-11-30 14:44 ` Fuad Tabba
  2026-11-30 14:44 ` [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized Fuad Tabba
  2026-11-30 14:44 ` [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32 Fuad Tabba
  2 siblings, 0 replies; 4+ messages in thread
From: Fuad Tabba @ 2026-11-30 14:44 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
	Catalin Marinas, Will Deacon, Mark Rutland, Vincent Donnefort,
	Fuad Tabba, linux-arm-kernel, kvmarm, linux-kernel, stable

KVM clears VCPU_INITIALIZED on a vCPU whose guest enters a disallowed
AArch32 mode, and the VMM can initialize it again. However, with the
flag clear, KVM_ARM_VCPU_INIT sets the vCPU up a second time with
kvm_setup_vcpu(), as if it never ran.

For a nested vCPU, if growing the nested MMU array fails in the second
setup, the error path frees the VNCR page. The virtual timer's offset,
CNTVOFF_EL2, is in that page, and the timer reads it from the freed
page the next time the vCPU is loaded:

  BUG: KASAN: use-after-free in timer_restore_state (arch/arm64/kvm/arch_timer.c:642)
  [...]
  Call trace:
   timer_restore_state (arch/arm64/kvm/arch_timer.c:642)
   kvm_timer_vcpu_load (arch/arm64/kvm/arch_timer.c:910)
   vhe_vcpu_load (arch/arm64/kvm/arm.c:764)
   kvm_arch_vcpu_load (arch/arm64/kvm/arm.c:814)
   vcpu_load (arch/arm64/kvm/../../../virt/kvm/kvm_main.c:179)
   kvm_arch_vcpu_ioctl_run (arch/arm64/kvm/arm.c:1426)
   kvm_vcpu_ioctl (arch/arm64/kvm/../../../virt/kvm/kvm_main.c:4575)

When the second setup succeeds, the vCPU is reset with the flag still
clear, and that reset overwrites what the VMM wrote to MPIDR_EL1 and
CLIDR_EL1, which KVM must reset only once.

Only set up an uninitialized vCPU that hasn't run. Reset one that has,
as for an initialized vCPU, and set VCPU_INITIALIZED before the reset
so that the two registers are kept.

Fixes: 469c4713d4802 ("KVM: arm64: nv: Allocate VNCR page when required")
Fixes: e016333745c70 ("KVM: arm64: Only reset vCPU-scoped feature ID regs once")
Cc: stable@vger.kernel.org
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/kvm/arm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 157bf4b812020..cfaaadd97233f 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1883,12 +1883,15 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 	if (ret)
 		return ret;
 
-	if (!kvm_vcpu_initialized(vcpu))
+	/* A vCPU that has run is only reset even if it is no longer initialized. */
+	if (!kvm_vcpu_initialized(vcpu) && !vcpu_has_run_once(vcpu))
 		return __kvm_vcpu_set_target(vcpu, init);
 
 	if (kvm_vcpu_init_changed(vcpu, init))
 		return -EINVAL;
 
+	/* The reset keeps the vCPU-scoped ID registers only with the flag set. */
+	vcpu_set_flag(vcpu, VCPU_INITIALIZED);
 	kvm_reset_vcpu(vcpu);
 	return 0;
 }
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized
  2026-11-30 14:44 [PATCH 0/3] KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode Fuad Tabba
  2026-11-30 14:44 ` [PATCH 1/3] KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run Fuad Tabba
@ 2026-11-30 14:44 ` Fuad Tabba
  2026-11-30 14:44 ` [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32 Fuad Tabba
  2 siblings, 0 replies; 4+ messages in thread
From: Fuad Tabba @ 2026-11-30 14:44 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
	Catalin Marinas, Will Deacon, Mark Rutland, Vincent Donnefort,
	Fuad Tabba, linux-arm-kernel, kvmarm, linux-kernel

KVM_RUN returns -ENOEXEC on a vCPU that isn't initialized. The check was
there in kvm_arch_vcpu_ioctl_run() until commit b5aa368abfbf4 ("KVM: arm64:
Merge kvm_arch_vcpu_run_pid_change() and kvm_vcpu_first_run_init()")
moved it to kvm_arch_vcpu_run_pid_change(), which is called only when
the thread issuing KVM_RUN changes. However, KVM clears VCPU_INITIALIZED
on a vCPU that enters a disallowed AArch32 mode, and still runs it on
the next KVM_RUN from the same thread.

Restore the check in kvm_arch_vcpu_ioctl_run(). Keep the one in
kvm_arch_vcpu_run_pid_change(), which also prepares a vCPU for its
first run.

Fixes: b5aa368abfbf4 ("KVM: arm64: Merge kvm_arch_vcpu_run_pid_change() and kvm_vcpu_first_run_init()")
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/kvm/arm.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index cfaaadd97233f..6c2d3811f2814 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1417,6 +1417,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 	struct kvm_run *run = vcpu->run;
 	int ret;
 
+	if (unlikely(!kvm_vcpu_initialized(vcpu)))
+		return -ENOEXEC;
+
 	if (run->exit_reason == KVM_EXIT_MMIO) {
 		ret = kvm_handle_mmio_return(vcpu);
 		if (ret <= 0)
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32
  2026-11-30 14:44 [PATCH 0/3] KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode Fuad Tabba
  2026-11-30 14:44 ` [PATCH 1/3] KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run Fuad Tabba
  2026-11-30 14:44 ` [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized Fuad Tabba
@ 2026-11-30 14:44 ` Fuad Tabba
  2 siblings, 0 replies; 4+ messages in thread
From: Fuad Tabba @ 2026-11-30 14:44 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
	Catalin Marinas, Will Deacon, Mark Rutland, Vincent Donnefort,
	Fuad Tabba, linux-arm-kernel, kvmarm, linux-kernel

When a protected vCPU exits to EL2 in AArch32, EL2 fails the run and
clears VCPU_INITIALIZED on its copy of the vCPU to make the vCPU
invalid, but EL2 never reads that flag. The host's copy of the flag
remains set, so the next KVM_RUN runs the vCPU again.

Don't run a vCPU whose flag EL2 has cleared. pkvm_filter_vcpu_ioctl()
rejects KVM_ARM_VCPU_INIT for a protected vCPU that has run, so reword
the comment in fixup_guest_exit().

Fixes: be66e67f17509 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++++
 arch/arm64/kvm/hyp/nvhe/switch.c   | 7 +++----
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 179d7c8277b88..19509d460e06e 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -924,6 +924,10 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
 		if (unlikely(system_supports_sme() && read_sysreg_s(SYS_SVCR)))
 			return ret;
 
+		/* Cleared by fixup_guest_exit() for a protected vCPU caught in AArch32. */
+		if (unlikely(!kvm_vcpu_initialized(&hyp_vcpu->vcpu)))
+			return ret;
+
 		/*
 		 * ON has a single writer, pkvm_reset_vcpu() on this CPU, so
 		 * READ_ONCE suffices. ON_PENDING takes the reset; -ECANCELED
diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
index 89fb9dd351ad5..b69295f8c122d 100644
--- a/arch/arm64/kvm/hyp/nvhe/switch.c
+++ b/arch/arm64/kvm/hyp/nvhe/switch.c
@@ -262,10 +262,9 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
 	if (unlikely(vcpu_is_protected(vcpu) && vcpu_mode_is_32bit(vcpu))) {
 		/*
 		 * As we have caught the guest red-handed, decide that it isn't
-		 * fit for purpose anymore by making the vcpu invalid. The VMM
-		 * can try and fix it by re-initializing the vcpu with
-		 * KVM_ARM_VCPU_INIT, however, this is likely not possible for
-		 * protected VMs.
+		 * fit for purpose anymore by making the vcpu invalid. EL2
+		 * can't run it again, and a protected vcpu that has run
+		 * can't be re-initialized with KVM_ARM_VCPU_INIT.
 		 */
 		vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
 		*exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT);
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 14:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 14:44 [PATCH 0/3] KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode Fuad Tabba
2026-11-30 14:44 ` [PATCH 1/3] KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run Fuad Tabba
2026-11-30 14:44 ` [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized Fuad Tabba
2026-11-30 14:44 ` [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32 Fuad Tabba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®