* [PATCH v2 1/8] KVM: arm64: Drop an incomplete MMIO access when resetting a vcpu
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 2/8] KVM: arm64: Reset SP_EL1, ELR_EL1 and SPSR_EL1 " Dan Carpenter
` (5 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest, stable
When a vcpu exits to userspace for an MMIO access, the next KVM_RUN
completes it. So for a read, the next run increments the PC counter
and copies data from userspace to the destination vcpu register.
A reset should cancel the MMIO read/write access. Otherwise the next
run will copy memory from userspace to the vcpu and increment the PC
which skips the first instruction.
Clear vcpu->mmio_needed in kvm_reset_vcpu().
Fixes: 45e96ea6b369 ("KVM: ARM: Handle I/O aborts")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: new patch
---
arch/arm64/kvm/reset.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 10eb7249aa9e..26081d273408 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -199,6 +199,7 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
/* Reset core registers */
kvm_reset_vcpu_core(vcpu);
+ vcpu->mmio_needed = false;
/* Reset system registers */
kvm_reset_sys_regs(vcpu);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 2/8] KVM: arm64: Reset SP_EL1, ELR_EL1 and SPSR_EL1 when resetting a vcpu
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 1/8] KVM: arm64: Drop an incomplete MMIO access when resetting a vcpu Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 3/8] KVM: arm64: Discard a pending SError " Dan Carpenter
` (4 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest, stable
Resetting a vcpu keeps the old values of SP_EL1, ELR_EL1 and SPSR_EL1.
Commit e47c2055c68e ("KVM: arm64: Make struct kvm_regs userspace-only")
changed the reset to only clear the general purpose registers, SP, PC
and PSTATE, and later commits moved these three registers to the system
register array without a reset value. Commit 85d703746154 ("KVM: arm64:
Fully zero the vcpu state on reset") fixed the FP registers and the
32-bit SPSRs the same commit missed, but not these three.
The impact is that the guest that boots next can read the previous
kernel's stack pointer and exception return address. KVM_GET_ONE_REG
also returns the old values to userspace, and a rebooted VM doesn't
start in the same state as a new one.
Zero them in kvm_reset_vcpu().
Fixes: e47c2055c68e ("KVM: arm64: Make struct kvm_regs userspace-only")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
arch/arm64/kvm/reset.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 26081d273408..9c1b42890c96 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -199,6 +199,9 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
/* Reset core registers */
kvm_reset_vcpu_core(vcpu);
+ __vcpu_assign_sys_reg(vcpu, SP_EL1, 0);
+ __vcpu_assign_sys_reg(vcpu, ELR_EL1, 0);
+ __vcpu_assign_sys_reg(vcpu, SPSR_EL1, 0);
vcpu->mmio_needed = false;
/* Reset system registers */
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 3/8] KVM: arm64: Discard a pending SError when resetting a vcpu
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 1/8] KVM: arm64: Drop an incomplete MMIO access when resetting a vcpu Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 2/8] KVM: arm64: Reset SP_EL1, ELR_EL1 and SPSR_EL1 " Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 4/8] KVM: arm64: nv: Discard nested SError in kvm_reset_vcpu() Dan Carpenter
` (3 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest, stable
Resetting a vcpu with the KVM_ARM_VCPU_INIT ioctl keeps a pending
SError. We used to rely on vcpu_reset_hcr() to clear the SError but
commit f1ff3fc5209a ("KVM: arm64: unify code to prepare traps") changed
it to only reset HCR_EL2 before the vcpu's very first KVM_RUN.
If we reboot a guest while there is a pending SError then KVM delivers
it to the freshly booted guest.
Clear HCR_EL2.VSE in kvm_reset_vcpu().
Fixes: f1ff3fc5209a ("KVM: arm64: unify code to prepare traps")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: new patch
---
arch/arm64/kvm/reset.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 9c1b42890c96..a5d298d5ce61 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -207,6 +207,9 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
/* Reset system registers */
kvm_reset_sys_regs(vcpu);
+ /* Discard a pending SError */
+ vcpu->arch.hcr_el2 &= ~HCR_EL2_VSE;
+
/*
* Additional reset state handling that PSCI may have imposed on us.
* Must be done after all the sys_reg reset.
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 4/8] KVM: arm64: nv: Discard nested SError in kvm_reset_vcpu()
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
` (2 preceding siblings ...)
2026-11-25 6:56 ` [PATCH v2 3/8] KVM: arm64: Discard a pending SError " Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one Dan Carpenter
` (2 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest, stable
Vanilla KVM stores a pending SError in ->hcr_el2 but nested KVM stores
it in the NESTED_SERROR_PENDING flag. Clear the flag for nested
SErrors when we reset the vcpu.
Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: New patch. I split it up like this to make backporting easier.
---
arch/arm64/kvm/reset.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index a5d298d5ce61..d80b38b2ce6c 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -209,6 +209,7 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
/* Discard a pending SError */
vcpu->arch.hcr_el2 &= ~HCR_EL2_VSE;
+ vcpu_clear_flag(vcpu, NESTED_SERROR_PENDING);
/*
* Additional reset state handling that PSCI may have imposed on us.
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
` (3 preceding siblings ...)
2026-11-25 6:56 ` [PATCH v2 4/8] KVM: arm64: nv: Discard nested SError in kvm_reset_vcpu() Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 8:00 ` Oliver Upton
2026-11-25 6:56 ` [PATCH v2 6/8] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 7/8] KVM: arm64: nv: Re-inject a stored SError after the exit is handled Dan Carpenter
6 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest,
Sashiko, stable
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 the SError that was pending on the next exit or entry.
Clear the pend flags unconditionally at the start of the function.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-229065@kernel.org/
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@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one
2026-11-25 6:56 ` [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one Dan Carpenter
@ 2026-11-25 8:00 ` Oliver Upton
0 siblings, 0 replies; 9+ messages in thread
From: Oliver Upton @ 2026-11-25 8:00 UTC (permalink / raw)
To: Dan Carpenter
Cc: Marc Zyngier, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan, Karthik Bhargavan,
Clément Blaudeau, linux-arm-kernel, kvmarm, linux-kernel,
kvm, linux-kselftest, Sashiko, stable
Hi Dan,
On Wed, Nov 25, 2026 at 09:56:20AM +0300, Dan Carpenter wrote:
> 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 the SError that was pending on the next exit or entry.
I could use a bit more color on how we got here. From what I can tell
this state is only possible if userspace changed the routing / masking
of the SError outside the context of a KVM_RUN. Otherwise, if the SError
is deliverable on guest exit we perform the injection prior to exiting
to userspace.
Thanks,
Oliver
> Clear the pend flags unconditionally at the start of the function.
>
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://lore.kernel.org/all/sashiko-outbox-229065@kernel.org/
> 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@vger.kernel.org
> Signed-off-by: Dan Carpenter <dan@pereq.eu>
> ---
> 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
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 6/8] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
` (4 preceding siblings ...)
2026-11-25 6:56 ` [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 7/8] KVM: arm64: nv: Re-inject a stored SError after the exit is handled Dan Carpenter
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest,
Sashiko, stable
With nested virtualization, when we inject an SError that the guest
has masked, we set the VSE bit in ->hcr_el2 and the CPU delivers it once
the guest unmasks SErrors. The problem is that the bit stays set when
we switch between L1 and L2, so an SError meant for L2 can be delivered
to L1, and the other way around.
When we switch, clear the VSE bit and set NESTED_SERROR_PENDING instead.
This matches what kvm_nested_sync_hwstate() does after every guest exit.
Then kvm_nested_flush_hwstate() injects the SError again and decides
where it should go in the new context.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-229065@kernel.org/
Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: new patch
---
arch/arm64/kvm/emulate-nested.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index 39136a89d74b..cccaf02610e8 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -2792,6 +2792,18 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr)
return spsr;
}
+/*
+ * HCR_EL2.VSE was set for the context we are leaving. Store the SError
+ * instead, so that kvm_nested_flush_hwstate() routes it in the new context.
+ */
+static void kvm_nested_stash_serror(struct kvm_vcpu *vcpu)
+{
+ if (*vcpu_hcr(vcpu) & HCR_EL2_VSE) {
+ *vcpu_hcr(vcpu) &= ~HCR_EL2_VSE;
+ vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
+ }
+}
+
void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu)
{
u64 spsr, elr, esr;
@@ -2823,6 +2835,7 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu)
preempt_disable();
vcpu_set_flag(vcpu, IN_NESTED_ERET);
kvm_arch_vcpu_put(vcpu);
+ kvm_nested_stash_serror(vcpu);
if (!esr_iss_is_eretax(esr))
elr = __vcpu_sys_reg(vcpu, ELR_EL2);
@@ -2909,6 +2922,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
__kvm_adjust_pc(vcpu);
kvm_arch_vcpu_put(vcpu);
+ kvm_nested_stash_serror(vcpu);
kvm_inject_el2_exception(vcpu, esr_el2, type);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 7/8] KVM: arm64: nv: Re-inject a stored SError after the exit is handled
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
` (5 preceding siblings ...)
2026-11-25 6:56 ` [PATCH v2 6/8] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest Dan Carpenter
@ 2026-11-25 6:56 ` Dan Carpenter
6 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2026-11-25 6:56 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Christoffer Dall, Rusty Russell, Marcelo Tosatti,
Sebastian Ott, Eric Auger, James Morse, Paolo Bonzini,
Sean Christopherson, Shuah Khan
Cc: Karthik Bhargavan, Clément Blaudeau, linux-arm-kernel,
kvmarm, Dan Carpenter, linux-kernel, kvm, linux-kselftest, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: No change
---
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(-)
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index 2d7c82cfb8a4..ab2ec9078b76 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -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);
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index a148e80d0aef..2a2add60b72d 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -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)));
diff --git a/arch/arm64/kvm/inject_fault.c b/arch/arm64/kvm/inject_fault.c
index 0f6a89b65074..1d5dfd4b9e00 100644
--- a/arch/arm64/kvm/inject_fault.c
+++ b/arch/arm64/kvm/inject_fault.c
@@ -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);
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 78d5b18dbd2d..f063c01c565a 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -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));
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread