[PATCH v3 6/9] KVM: arm64: nv: Replace a pending SError when injecting a new one

From: Dan Carpenter

Date: Mon Nov 30 2026 - 03:48:50 EST


The kvm_inject_serror_esr() function injects SErrors as the name
implies. The bug is that it doesn't handle already pending errors
correctly in nested virtualization.

In vanilla KVM the kvm_inject_serror_esr() function always sets
the SError to pending. However with nested KVM, it might set it
to pending or it might deliver the SError directly. We can only
have one pending error at a time. Imagine we enter the function
with an SError pending already. In the current code if we decide
to pend the new error that's fine, everything works. However if
we decide to deliver it directly then the pend flags should be
cleared but they are not. So we deliver the SError directly and
then deliver the SError that was pending on the next exit or entry.

This bug can be triggered in the current code, however in a later patch
in this series "KVM: arm64: nv: Re-inject a stored SError after the exit
is handled" the bug will become much easier to trigger.

In the current code this can only happen when userspace changes where
the SError goes between two KVM_RUN calls. For example, userspace
injects an SError with the KVM_SET_VCPU_EVENTS ioctl while the vcpu is
at vEL2 with SErrors masked, so KVM sets HCR_EL2.VSE. Then userspace
uses KVM_SET_ONE_REG to move the vcpu to L2 and injects a second SError.
L1 takes SErrors from L2, so KVM delivers the second SError to L1
straight away, but HCR_EL2.VSE is still set. When L1 unmasks SErrors,
it takes the first SError as well.

After the "KVM: arm64: nv: Re-inject a stored SError after the exit
is handled" commit then kvm_nested_sync_hwstate() only stores a pending
SError when the guest exits. Then kvm_nested_flush_hwstate() re-injects
the SError before the next entry. In between, KVM handles the exit and
may return to userspace. If userspace injects an SError during that
time, for example on an MMIO exit, the stored SError is still pending,
without any change to the routing or masking. So this patch has to come
first before the re-inject path.

Clear the pend flags unconditionally at the start of the function.

Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/all/sashiko-outbox-229065@xxxxxxxxxx/
Fixes: ce66109cec86 ("KVM: arm64: nv: Take "masked" aborts to EL2 when HCRX_EL2.TMEA is set")
Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Dan Carpenter <dan@xxxxxxxx>
---
v3: Add how the bug is triggered to the commit message
v2: New patch
---
arch/arm64/kvm/inject_fault.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/arch/arm64/kvm/inject_fault.c b/arch/arm64/kvm/inject_fault.c
index aa2011cc0d5a..0f6a89b65074 100644
--- a/arch/arm64/kvm/inject_fault.c
+++ b/arch/arm64/kvm/inject_fault.c
@@ -361,6 +361,10 @@ int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr)
{
lockdep_assert_held(&vcpu->mutex);

+ /* A new SError replaces any that is already pending. */
+ vcpu_clear_flag(vcpu, NESTED_SERROR_PENDING);
+ *vcpu_hcr(vcpu) &= ~HCR_EL2_VSE;
+
if (is_nested_ctxt(vcpu) && kvm_serror_target_is_el2(vcpu))
return kvm_inject_nested_serror(vcpu, esr);


--
2.53.0