Skip to contentSkip to conversation

KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode

PATCH In review Fuad Tabba 2 participants 6 messages Nov 30

Subsystems (3)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: Fixes for a vCPU caught in a disallowed AArch32 mode

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

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/3 KVM: arm64: Don't call kvm_setup_vcpu() again once a vCPU has run

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")
---
 arch/arm64/kvm/arm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/kvm/arm.c +4 -1
index 157bf4b812020..cfaaadd97233f 100644
@@ -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;
 }
Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH 2/3 KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized

In reply to Fuad Tabba

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()")
---
 arch/arm64/kvm/arm.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index cfaaadd97233f..6c2d3811f2814 100644
@@ -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)
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] KVM: arm64: stolen time underflow in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] KVM: arm64: stolen time underflow in kvm_update_stolen_time()
[1]
--

commit bfa878fdb69c60cb82e53267508c4265191ade79
Author: Fuad Tabba <[email protected]>

KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized

This commit restores the kvm_vcpu_initialized() check in kvm_arch_vcpu_ioctl_run() so that KVM_RUN returns -ENOEXEC if a vCPU has been marked uninitialized after its first run.

Quoted 5 lines
arch/arm64/kvm/arm.c +0 -0
index cfaaadd97233f..6c2d3811f2814 100644

arch/arm64/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 1425

Quoted 9 lines
arch/arm64/kvm/arm.c +3 -0
@@ -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)

[Severity: High]
This problem wasn't introduced by this patch, but can vcpu->arch.steal.last_steal underflow in kvm_update_stolen_time() [1] when a vCPU migrates to a new host thread?

When a new thread runs a vCPU that has already run once, kvm_arch_vcpu_run_pid_change() returns early without resetting vcpu->arch.steal.last_steal to the new task's run_delay:

arch/arm64/kvm/arm.c:kvm_arch_vcpu_run_pid_change() {
	...
	if (likely(vcpu_has_run_once(vcpu)))
		return 0;
	...
}

On the next guest entry, kvm_update_stolen_time() subtracts the previous thread's cached run_delay from the new thread's current->sched_info.run_delay:

arch/arm64/kvm/pvtime.c:kvm_update_stolen_time() {
	...
	if (!kvm_get_guest(kvm, base + offset, steal)) {
		steal = le64_to_cpu(steal);
		vcpu->arch.steal.last_steal = READ_ONCE(current->sched_info.run_delay);
		steal += vcpu->arch.steal.last_steal - last_steal;
		kvm_put_guest(kvm, base + offset, cpu_to_le64(steal));
	}
	...
}

If the new thread has accumulated less runqueue delay than the old thread, the unsigned subtraction wraps around and writes a bogus stolen_time value to guest memory, corrupting guest CPU time accounting.

[1] https://sashiko.dev/#/bug/linux-9029bf19-3622-4a26-a158-ad236ad8cc5f

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#4 lore raw

Re: [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized

Quoted 6 lines · On Mon, 30 Nov 2026 at 14:55, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] KVM: arm64: stolen time underflow in kvm_update_stolen_time()
[1]

Known issue, Dongli Zhang's series fixes it [1].

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/

author reviewer
#5 lore raw

PATCH 3/3 KVM: arm64: Don't run a protected vCPU that was caught in AArch32

In reply to Fuad Tabba

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()")
---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++++
 arch/arm64/kvm/hyp/nvhe/switch.c   | 7 +++----
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4 -0
index 179d7c8277b88..19509d460e06e 100644
@@ -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
arch/arm64/kvm/hyp/nvhe/switch.c +3 -4
index 89fb9dd351ad5..b69295f8c122d 100644
@@ -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);
Signature · 1 line
2.47.3