* [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; 8+ 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 related [flat|nested] 8+ 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:55 ` sashiko-bot
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, 1 reply; 8+ 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 related [flat|nested] 8+ messages in thread* Re: [PATCH 2/3] KVM: arm64: Reject KVM_RUN on a vCPU that's no longer initialized
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:55 ` sashiko-bot
2026-11-30 15:11 ` Fuad Tabba
0 siblings, 1 reply; 8+ messages in thread
From: sashiko-bot @ 2026-11-30 14:55 UTC (permalink / raw)
To: Fuad Tabba; +Cc: kvmarm, Oliver Upton, Marc Zyngier
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 <fuad.tabba@linux.dev>
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.
> 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)
[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
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130144438.3176276-1-fuad.tabba@linux.dev?part=2
^ permalink raw reply [flat|nested] 8+ 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
2026-12-02 14:02 ` Marc Zyngier
2 siblings, 1 reply; 8+ 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 related [flat|nested] 8+ messages in thread* Re: [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32
2026-11-30 14:44 ` [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32 Fuad Tabba
@ 2026-12-02 14:02 ` Marc Zyngier
2026-12-02 14:56 ` Fuad Tabba
0 siblings, 1 reply; 8+ messages in thread
From: Marc Zyngier @ 2026-12-02 14:02 UTC (permalink / raw)
To: Fuad Tabba
Cc: Oliver Upton, 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
On Mon, 30 Nov 2026 14:44:38 +0000,
Fuad Tabba <fuad.tabba@linux.dev> wrote:
>
> 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);
This is yet another weird difference in behaviour that is visible from
userspace. Also, once a vcpu is "unrunnable", the whole guest is
pretty much dead (I can't see how it is going to recover from a vcpu
being gone).
I wonder if we should promote the "red-handed" behaviour to being a
case of kvm_vm_dead(). This would solve all our problems once and for
all.
The other solution is of course to wipe AArch32 support altogether.
M. (tempted...)
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 3/3] KVM: arm64: Don't run a protected vCPU that was caught in AArch32
2026-12-02 14:02 ` Marc Zyngier
@ 2026-12-02 14:56 ` Fuad Tabba
0 siblings, 0 replies; 8+ messages in thread
From: Fuad Tabba @ 2026-12-02 14:56 UTC (permalink / raw)
To: Marc Zyngier
Cc: Oliver Upton, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Vincent Donnefort, linux-arm-kernel, kvmarm, linux-kernel
Hi Marc,
On Wed, 02 Dec 2026 14:02:02 +0000, Marc Zyngier <maz@kernel.org> wrote:
[...]
> This is yet another weird difference in behaviour that is visible from
> userspace. Also, once a vcpu is "unrunnable", the whole guest is
> pretty much dead (I can't see how it is going to recover from a vcpu
> being gone).
>
> I wonder if we should promote the "red-handed" behaviour to being a
> case of kvm_vm_dead(). This would solve all our problems once and for
> all.
I'll do that in v2, for both protected and non-protected vCPUs.
I'll also drop the first two patches, since KVM would no longer clear
VCPU_INITIALIZED on a vCPU that has run, unless you'd like to keep the
first one for stable.
> The other solution is of course to wipe AArch32 support altogether.
>
> M. (tempted...)
First nVHE, now AArch32. You're on a roll... off with their heads!
Cheers,
/fuad
^ permalink raw reply [flat|nested] 8+ messages in thread