From: Melody Wang <huibo.wang@amd.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <bonzini@redhat.com>
Cc: LKML <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
Joerg Roedel <joro@8bytes.org>, <x86@kernel.org>,
Melody Wang <huibo.wang@amd.com>
Subject: [RFC PATCH v5 6/8] KVM: SVM: Handle HLT intercept when Restricted Injection is enabled.
Date: Mon, 9 Nov 2026 03:18:38 +0000 [thread overview]
Message-ID: <d9490eaad48062f0e8cd1a0e4f6312d04d012095.1794190621.git.huibo.wang@amd.com> (raw)
In-Reply-To: <cover.1794190621.git.huibo.wang@amd.com>
With SEV-SNP Restricted Injection enabled, #HV events are delivered
without regard to interrupt shadows, causing guests to lose control over
the interaction between HLT and interrupts. Under this convention,
a guest entering a halt state executes HLT with interrupts disabled
rather than checking pending interrupt state or enabling interrupts
first. If the host intercepts HLT while the guest has any interrupts or
non-maskable events pending, KVM must immediately re-enter the guest.
Intercept HLT when Restricted Injection is enabled, immediately re-enter
the guest if any pending events including the pending events in the HV
doorbell page exist.
Signed-off-by: Melody Wang <huibo.wang@amd.com>
---
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 1 +
arch/x86/kvm/svm/sev.c | 19 +++++++++++++
arch/x86/kvm/svm/svm.c | 44 ++++++++++++++++++++++++++++++
arch/x86/kvm/svm/svm.h | 2 ++
arch/x86/kvm/x86.c | 3 ++
6 files changed, 70 insertions(+)
diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index e3da9fb0b2b6..9396e2c661ab 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -98,6 +98,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr)
KVM_X86_OP_OPTIONAL_RET0(get_mt_mask)
KVM_X86_OP_OPTIONAL_RET0(tdp_has_smep)
KVM_X86_OP_OPTIONAL_RET0(is_noack_required)
+KVM_X86_OP_OPTIONAL_RET0(hv_doorbell_has_events)
KVM_X86_OP(load_mmu_pgd)
KVM_X86_OP_OPTIONAL_RET0(set_external_spte)
KVM_X86_OP_OPTIONAL(free_external_spt)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index fa83477ebcab..f3690a5dc953 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1620,6 +1620,7 @@ struct kvm_x86_ops {
void (*enable_irq_window)(struct kvm_vcpu *vcpu);
void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
bool (*is_noack_required)(struct kvm_vcpu *vcpu);
+ bool (*hv_doorbell_has_events)(struct kvm_vcpu *vcpu);
const bool x2apic_icr_is_split;
const unsigned long required_apicv_inhibits;
bool allow_apicv_in_x2apic_without_x2apic_virtualization;
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 6a1262c274ff..9aafa6167ae6 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -5748,3 +5748,22 @@ void sev_snp_update_ri_status(struct kvm_vcpu *vcpu)
unmap_hvdb(vcpu, &hvdb_map);
}
+
+bool sev_snp_hvdb_has_events(struct kvm_vcpu *vcpu)
+{
+ struct kvm_host_map hvdb_map;
+ struct hvdb *hvdb;
+ bool has_events;
+
+ hvdb = map_hvdb(vcpu, &hvdb_map);
+ if (!hvdb) {
+ WARN_ONCE(1, "Restricted Injection enabled, hvdb page mapping failed\n");
+ return false;
+ }
+
+ has_events = hvdb->events.vector || hvdb->events.nmi || hvdb->events.mce;
+
+ unmap_hvdb(vcpu, &hvdb_map);
+
+ return has_events;
+}
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index ad99b2b1b07b..ff22a79e0ddd 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4130,6 +4130,49 @@ static int svm_mce_allowed(struct kvm_vcpu *vcpu)
return 1;
}
+static bool svm_hv_doorbell_has_events(struct kvm_vcpu *vcpu)
+{
+ unsigned int state;
+ int irq;
+ unsigned int num_pending = 0;
+
+ if (!sev_snp_is_rinj_active(vcpu))
+ return false;
+
+ /*
+ * Evaluate vCPU events before the #HV doorbell page, as
+ * kvm_vcpu_has_events() bypasses them under Restricted Injection.
+ */
+ irq = kvm_cpu_get_interrupt_noack(vcpu, &num_pending);
+ if ((vcpu->arch.exception.pending && vcpu->arch.exception.vector == MC_VECTOR) ||
+ vcpu->arch.nmi_pending || irq >= 0)
+ return true;
+
+ /*
+ * Mapping the #HV doorbell page via kvm_vcpu_map() can sleep (e.g.,
+ * on page faults). In the HLT fast-path, local IRQs are disabled, so
+ * invoking kvm_vcpu_map() there can trigger a panic. To avoid sleeping
+ * in atomic context, assume an event is pending during the fast-path
+ * and defer the actual check until re-entering outside the fast-path.
+ *
+ * A related issue occurs during normal vCPU block polling:
+ * kvm_vcpu_block() sets the task state to TASK_INTERRUPTIBLE before
+ * calling kvm_vcpu_check_block(). If kvm_vcpu_map() sleeps during that
+ * check, waking up resets the task state to TASK_RUNNING, causing the
+ * subsequent schedule() to return immediately instead of blocking.
+ *
+ * Therefore, avoid calling kvm_vcpu_map() from
+ * svm_hv_doorbell_has_events() unless the task state is TASK_RUNNING,
+ * preventing both sleeping with IRQs disabled and livelocks during
+ * vCPU blocking.
+ */
+ state = get_current_state();
+ if (irqs_disabled() || state != TASK_RUNNING)
+ return true;
+
+ return sev_snp_hvdb_has_events(vcpu);
+}
+
static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
{
struct vcpu_svm *svm = to_svm(vcpu);
@@ -5452,6 +5495,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
.enable_irq_window = svm_enable_irq_window,
.update_cr8_intercept = svm_update_cr8_intercept,
.is_noack_required = svm_is_noack_required,
+ .hv_doorbell_has_events = svm_hv_doorbell_has_events,
.x2apic_icr_is_split = true,
.set_virtual_apic_mode = avic_refresh_virtual_apic_mode,
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index e19b398a3df5..2d4996445b78 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -1035,6 +1035,7 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu);
bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu);
void sev_snp_update_ri_status(struct kvm_vcpu *vcpu);
void sev_snp_handle_ri_ack(struct kvm_vcpu *vcpu, struct hvdb *hvdb, int num_pending);
+bool sev_snp_hvdb_has_events(struct kvm_vcpu *vcpu);
static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu)
{
struct kvm_sev_info *sev = to_kvm_sev_info(vcpu->kvm);
@@ -1076,6 +1077,7 @@ static inline void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) {}
static inline bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) { return false; }
static inline void sev_snp_update_ri_status(struct kvm_vcpu *vcpu)i {};
static inline void sev_snp_handle_ri_ack(struct kvm_vcpu *vcpu, struct hvdb *hvdb, int num_pending) {};
+static inline bool sev_snp_hvdb_has_events(struct kvm_vcpu *vcpu) { return false; }
static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) { return false; }
#endif
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 1492a06a6935..f8295c5ffb50 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8515,6 +8515,9 @@ static bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
{
+ if (kvm_x86_call(hv_doorbell_has_events)(vcpu))
+ return true;
+
if (!list_empty_careful(&vcpu->async_pf.done))
return true;
--
2.43.0
parent reply other threads:[~2026-11-09 3:20 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <cover.1794190621.git.huibo.wang@amd.com>]
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=d9490eaad48062f0e8cd1a0e4f6312d04d012095.1794190621.git.huibo.wang@amd.com \
--to=huibo.wang@amd.com \
--cc=bonzini@redhat.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=seanjc@google.com \
--cc=x86@kernel.org \
/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
all inboxes | Powered by JetHome®