From: Dan Carpenter <dan@pereq.eu>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>
Cc: "Karthik Bhargavan" <karthik@pereq.eu>,
"Clément Blaudeau" <clement@cryspen.com>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
stable@vger.kernel.org, "Dan Carpenter" <dan@pereq.eu>
Subject: [PATCH] KVM: arm64: nv: Re-inject a stored SError after the exit is handled
Date: Mon, 23 Nov 2026 13:05:17 +0300 [thread overview]
Message-ID: <20261123-double_exception-v1-1-cd82b59a232d@pereq.eu> (raw)
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>
---
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 aa2011cc0d5a..05c98acb014c 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));
}
/*
---
base-commit: adcad7f4e6332cff3d6d7165f41584899f598534
change-id: 20261120-double_exception-a34dcc521118
Best regards,
--
Dan Carpenter <dan@pereq.eu>
reply other threads:[~2026-11-23 10:05 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123-double_exception-v1-1-cd82b59a232d@pereq.eu \
--to=dan@pereq.eu \
--cc=catalin.marinas@arm.com \
--cc=clement@cryspen.com \
--cc=fuad.tabba@linux.dev \
--cc=joey.gouly@arm.com \
--cc=karthik@pereq.eu \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=stable@vger.kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox