Skip to contentSkip to conversation

KVM: arm64: nv: Re-inject a stored SError after the exit is handled

PATCH In review Dan Carpenter 2 participants 3 messages Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: nv: Re-inject a stored SError after the exit is handled

KVM only has room for one pending exception or PC increment per vcpu. If something is already pending and we try to add another through kvm_pend_exception() or kvm_incr_pc(), it triggers a WARN(). If we add an exception on top of an exception, the first one is lost. If we add a PC increment on top of an exception, the PC increment is lost and the trapped instruction runs again.

Normally an exit handler records an exception or a PC increment, and __kvm_adjust_pc() commits it before the next guest entry. However, with nested virtualization kvm_nested_sync_hwstate() can add an exception before the exit handler runs. It re-injects a stored SError after the guest exits but before handle_exit(), which can pend the SError as an exception. The exit handler then records its own on top, for example an HVC forwarded to the guest hypervisor, the FPAC for a failed ERETAA, or the PC increment for a trapped instruction it has emulated. This triggers the WARN().

In this situation where we have two exceptions, the correct order is to handle the exit first and then the SError. Change the kvm_nested_sync_hwstate() function to only store the SError. Then kvm_nested_flush_hwstate() uses __kvm_adjust_pc() to commit the exit handler's exception or PC increment, re-injects the SError, and the guest is entered. The early re-injection was there so that a pending SError wakes the vcpu from WFI, so kvm_arch_vcpu_runnable() now treats a deliverable stored SError as a wakeup.

Fixes 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")
Fixes ce66109cec86 ("KVM: arm64: nv: Take "masked" aborts to EL2 when HCRX_EL2.TMEA is set")
---
 arch/arm64/include/asm/kvm_emulate.h |  1 +
 arch/arm64/kvm/arm.c                 |  1 +
 arch/arm64/kvm/inject_fault.c        |  6 ++++++
 arch/arm64/kvm/nested.c              | 16 ++++++----------
 4 files changed, 14 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +1 -0
index 2d7c82cfb8a4..ab2ec9078b76 100644
@@ -48,6 +48,7 @@ void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
+bool kvm_nested_serror_deliverable(struct kvm_vcpu *vcpu);
 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
index a148e80d0aef..2a2add60b72d 100644
@@ -966,6 +966,7 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 {
 	bool irq_lines = *vcpu_hcr(v) & (HCR_EL2_VI | HCR_EL2_VF | HCR_EL2_VSE);
 
+	irq_lines |= kvm_nested_serror_deliverable(v);
 	irq_lines |= (!irqchip_in_kernel(v->kvm) &&
 		      (kvm_timer_should_notify_user(v) ||
 		       kvm_pmu_should_notify_user(v)));
arch/arm64/kvm/inject_fault.c +6 -0
index aa2011cc0d5a..05c98acb014c 100644
@@ -357,6 +357,12 @@ static bool kvm_serror_undeliverable_at_el2(struct kvm_vcpu *vcpu)
 	return !(vcpu_el2_tge_is_set(vcpu) || vcpu_el2_amo_is_set(vcpu));
 }
 
+bool kvm_nested_serror_deliverable(struct kvm_vcpu *vcpu)
+{
+	return vcpu_get_flag(vcpu, NESTED_SERROR_PENDING) &&
+	       !(vcpu_is_el2(vcpu) && kvm_serror_undeliverable_at_el2(vcpu));
+}
+
 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr)
 {
 	lockdep_assert_held(&vcpu->mutex);
arch/arm64/kvm/nested.c +6 -10
index 78d5b18dbd2d..f063c01c565a 100644
@@ -2213,8 +2213,11 @@ void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu)
 	if (!vcpu_has_nv(vcpu))
 		return;
 
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
+	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING))) {
+		/* Commit the exit handler's exception first */
+		__kvm_adjust_pc(vcpu);
 		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
+	}
 
 	vgic_v3_nested_update_mi(vcpu);
 
@@ -2231,18 +2234,11 @@ void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu)
 
 	/*
 	 * We previously decided that an SError was deliverable to the guest.
-	 * Reap the pending state from HCR_EL2 and...
+	 * Reap the pending state from HCR_EL2, and re-inject it from
+	 * kvm_nested_flush_hwstate() once the exit has been handled.
 	 */
 	if (unlikely(__test_and_clear_bit(__ffs(HCR_EL2_VSE), hcr)))
 		vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
-
-	/*
-	 * Re-attempt SError injection in case the deliverability has changed,
-	 * which is necessary to faithfully emulate WFI the case of a pending
-	 * SError being a wakeup condition.
-	 */
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
-		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
 }
 
 /*

---
base-commit: adcad7f4e6332cff3d6d7165f41584899f598534
change-id: 20261120-double_exception-a34dcc521118

Best regards,

Signature · 1 line
Dan Carpenter <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: arm64: nv: duplicate SError injection when NESTED_SERROR_PENDING is set - [Medium] KVM…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: nv: duplicate SError injection when NESTED_SERROR_PENDING is set
- [Medium] KVM: arm64: nv: premature SError re-injection in kvm_nested_flush_hwstate()
--

commit a6e9a58a0688b0de9bbbb40738acfc3f5921f92e
Author: Dan Carpenter <[email protected]>

KVM: arm64: nv: Re-inject a stored SError after the exit is handled

This commit defers SError re-injection for nested guests from kvm_nested_sync_hwstate() to kvm_nested_flush_hwstate() so that exit handlers can record their exception or PC increment before the SError is re-injected, and updates kvm_arch_vcpu_runnable() to treat a deliverable stored SError as a wakeup condition.

Quoted 7 lines
arch/arm64/kvm/nested.c +0 -0
index 78d5b18dbd2d6..f063c01c565a0 100644
@@ -2213,8 +2213,11 @@ void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu)
 	if (!vcpu_has_nv(vcpu))
 		return;

arch/arm64/kvm/nested.c void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu) · line 2223

Quoted 10 lines
arch/arm64/kvm/nested.c +4 -1
 
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
+	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING))) {
+		/* Commit the exit handler's exception first */
+		__kvm_adjust_pc(vcpu);
 		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
+	}
 
 	vgic_v3_nested_update_mi(vcpu);
 

[Severity: Medium]
Can re-injecting NESTED_SERROR_PENDING here run prematurely when guest entry is subsequently aborted?

In kvm_arch_vcpu_ioctl_run(), kvm_nested_flush_hwstate() runs before checking ret <= 0 or kvm_vcpu_exit_request(), and inside kvm_nested_flush_hwstate() the SError is re-injected before vgic_v3_nested_update_mi() and the KVM_REQ_GUEST_HYP_IRQ_PENDING check:

arch/arm64/kvm/nested.c:kvm_nested_flush_hwstate() {
	...
	vgic_v3_nested_update_mi(vcpu);

	if (vgic_state_is_nested(vcpu) && kvm_vgic_vcpu_pending_irq(vcpu))
		kvm_make_request(KVM_REQ_GUEST_HYP_IRQ_PENDING, vcpu);
}

If L2 has PSTATE.A == 1 (with HCR_EL2.AMO == 0 and HCRX_EL2.TMEA == 0) and NESTED_SERROR_PENDING set while an L1 IRQ or maintenance interrupt is pending, kvm_nested_flush_hwstate() clears NESTED_SERROR_PENDING, sets HCR_EL2_VSE in *vcpu_hcr(vcpu), and posts KVM_REQ_GUEST_HYP_IRQ_PENDING. kvm_vcpu_exit_request() then aborts guest entry without calling kvm_nested_sync_hwstate(), and on the next loop iteration check_nested_vcpu_requests() -> kvm_inject_nested_irq() switches the vCPU to L1 vEL2 with HCR_EL2_VSE still set in vcpu->arch.hcr_el2 even when TGE and AMO are clear. Similarly, if PSTATE.A == 0 in L2 or if ret <= 0 exits to userspace, pend_serror_exception() pends the SError into L2 state that gets committed by __kvm_adjust_pc() even though L2 was never entered.

Quoted 9 lines
arch/arm64/kvm/nested.c +2 -1
@@ -2231,18 +2234,11 @@ void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu)
 
 	/*
 	 * We previously decided that an SError was deliverable to the guest.
-	 * Reap the pending state from HCR_EL2 and...
+	 * Reap the pending state from HCR_EL2, and re-inject it from
+	 * kvm_nested_flush_hwstate() once the exit has been handled.
 	 */
 	if (unlikely(__test_and_clear_bit(__ffs(HCR_EL2_VSE), hcr)))

arch/arm64/kvm/nested.c void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu) · line 2242

Quoted 10 lines
arch/arm64/kvm/nested.c +0 -8
 		vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
-
-	/*
-	 * Re-attempt SError injection in case the deliverability has changed,
-	 * which is necessary to faithfully emulate WFI the case of a pending
-	 * SError being a wakeup condition.
-	 */
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
-		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
 }

[Severity: Medium]
Does leaving NESTED_SERROR_PENDING set across handle_exit() and exits to userspace cause a duplicate SError injection when kvm_inject_serror_esr() is called before the next kvm_nested_flush_hwstate()?

Because kvm_inject_serror_esr() does not clear NESTED_SERROR_PENDING when it delivers an SError via kvm_inject_nested_serror(), pend_serror_exception(), or HCR_EL2_VSE, a KVM_GET_VCPU_EVENTS followed by KVM_SET_VCPU_EVENTS in __kvm_arm_vcpu_set_events() injects and immediately commits the SError via commit_pending_events() while NESTED_SERROR_PENDING remains set:

arch/arm64/kvm/guest.c:__kvm_arm_vcpu_set_events() {
	...
	if (has_esr)
		ret = kvm_inject_serror_esr(vcpu, esr);
	else
		ret = kvm_inject_serror(vcpu);

	commit_pending_events(vcpu);
}

On the subsequent KVM_RUN, kvm_nested_flush_hwstate() sees NESTED_SERROR_PENDING still set and injects a second SError into the guest. The same double injection happens in-kernel if handle_exit() calls kvm_handle_guest_sea() -> kvm_inject_serror() while NESTED_SERROR_PENDING is set.

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

Yep. These are sort of existing bugs which my patch makes worse...

Claude had previously told me it would review patches to see if they expose any new bugs, but now it's saying that promise only extented to "crash fixes" and not to "warnings like this". :P

Anyway, I will send these fixes.

regards,
dan carpenter

Quoted 116 lines · On Mon, Nov 23, 2026 at 10:42:10AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: nv: duplicate SError injection when NESTED_SERROR_PENDING is set
- [Medium] KVM: arm64: nv: premature SError re-injection in kvm_nested_flush_hwstate()
--

commit a6e9a58a0688b0de9bbbb40738acfc3f5921f92e
Author: Dan Carpenter <[email protected]>

KVM: arm64: nv: Re-inject a stored SError after the exit is handled

This commit defers SError re-injection for nested guests from kvm_nested_sync_hwstate() to kvm_nested_flush_hwstate() so that exit handlers can record their exception or PC increment before the SError is re-injected, and updates kvm_arch_vcpu_runnable() to treat a deliverable stored SError as a wakeup condition.

Quoted 7 lines
arch/arm64/kvm/nested.c +0 -0
index 78d5b18dbd2d6..f063c01c565a0 100644
@@ -2213,8 +2213,11 @@ void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu)
 	if (!vcpu_has_nv(vcpu))
 		return;

arch/arm64/kvm/nested.c void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu) · line 2223

Quoted 10 lines
arch/arm64/kvm/nested.c +4 -1
 
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
+	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING))) {
+		/* Commit the exit handler's exception first */
+		__kvm_adjust_pc(vcpu);
 		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
+	}
 
 	vgic_v3_nested_update_mi(vcpu);
 

[Severity: Medium]
Can re-injecting NESTED_SERROR_PENDING here run prematurely when guest entry is subsequently aborted?

In kvm_arch_vcpu_ioctl_run(), kvm_nested_flush_hwstate() runs before checking ret <= 0 or kvm_vcpu_exit_request(), and inside kvm_nested_flush_hwstate() the SError is re-injected before vgic_v3_nested_update_mi() and the KVM_REQ_GUEST_HYP_IRQ_PENDING check:

arch/arm64/kvm/nested.c:kvm_nested_flush_hwstate() {
	...
	vgic_v3_nested_update_mi(vcpu);

	if (vgic_state_is_nested(vcpu) && kvm_vgic_vcpu_pending_irq(vcpu))
		kvm_make_request(KVM_REQ_GUEST_HYP_IRQ_PENDING, vcpu);
}

If L2 has PSTATE.A == 1 (with HCR_EL2.AMO == 0 and HCRX_EL2.TMEA == 0) and NESTED_SERROR_PENDING set while an L1 IRQ or maintenance interrupt is pending, kvm_nested_flush_hwstate() clears NESTED_SERROR_PENDING, sets HCR_EL2_VSE in *vcpu_hcr(vcpu), and posts KVM_REQ_GUEST_HYP_IRQ_PENDING. kvm_vcpu_exit_request() then aborts guest entry without calling kvm_nested_sync_hwstate(), and on the next loop iteration check_nested_vcpu_requests() -> kvm_inject_nested_irq() switches the vCPU to L1 vEL2 with HCR_EL2_VSE still set in vcpu->arch.hcr_el2 even when TGE and AMO are clear. Similarly, if PSTATE.A == 0 in L2 or if ret <= 0 exits to userspace, pend_serror_exception() pends the SError into L2 state that gets committed by __kvm_adjust_pc() even though L2 was never entered.

Quoted 9 lines
arch/arm64/kvm/nested.c +2 -1
@@ -2231,18 +2234,11 @@ void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu)
 
 	/*
 	 * We previously decided that an SError was deliverable to the guest.
-	 * Reap the pending state from HCR_EL2 and...
+	 * Reap the pending state from HCR_EL2, and re-inject it from
+	 * kvm_nested_flush_hwstate() once the exit has been handled.
 	 */
 	if (unlikely(__test_and_clear_bit(__ffs(HCR_EL2_VSE), hcr)))

arch/arm64/kvm/nested.c void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu) · line 2242

Quoted 10 lines
arch/arm64/kvm/nested.c +0 -8
 		vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
-
-	/*
-	 * Re-attempt SError injection in case the deliverability has changed,
-	 * which is necessary to faithfully emulate WFI the case of a pending
-	 * SError being a wakeup condition.
-	 */
-	if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
-		kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
 }

[Severity: Medium]
Does leaving NESTED_SERROR_PENDING set across handle_exit() and exits to userspace cause a duplicate SError injection when kvm_inject_serror_esr() is called before the next kvm_nested_flush_hwstate()?

Because kvm_inject_serror_esr() does not clear NESTED_SERROR_PENDING when it delivers an SError via kvm_inject_nested_serror(), pend_serror_exception(), or HCR_EL2_VSE, a KVM_GET_VCPU_EVENTS followed by KVM_SET_VCPU_EVENTS in __kvm_arm_vcpu_set_events() injects and immediately commits the SError via commit_pending_events() while NESTED_SERROR_PENDING remains set:

arch/arm64/kvm/guest.c:__kvm_arm_vcpu_set_events() {
	...
	if (has_esr)
		ret = kvm_inject_serror_esr(vcpu, esr);
	else
		ret = kvm_inject_serror(vcpu);

	commit_pending_events(vcpu);
}

On the subsequent KVM_RUN, kvm_nested_flush_hwstate() sees NESTED_SERROR_PENDING still set and injects a second SError into the guest. The same double injection happens in-kernel if handle_exit() calls kvm_handle_guest_sea() -> kvm_inject_serror() while NESTED_SERROR_PENDING is set.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1