mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH v5 6/8] KVM: SVM: Handle HLT intercept when Restricted Injection is enabled.
       [not found] <cover.1794190621.git.huibo.wang@amd.com>
@ 2026-11-09  3:18 ` Melody Wang
  0 siblings, 0 replies; only message in thread
From: Melody Wang @ 2026-11-09  3:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: LKML, kvm, Joerg Roedel, x86, Melody Wang

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-09  3:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cover.1794190621.git.huibo.wang@amd.com>
2026-11-09  3:18 ` [RFC PATCH v5 6/8] KVM: SVM: Handle HLT intercept when Restricted Injection is enabled Melody Wang

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®