Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Tom Lendacky <thomas.lendacky@amd.com>
To: "Pratik R. Sampat" <prsampat@amd.com>,
	kvm@vger.kernel.org, x86@kernel.org,
	linux-kernel@vger.kernel.org
Cc: tglx@kernel.org, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com, seanjc@google.com,
	pbonzini@redhat.com, kim.phillips@amd.com, nikunj@amd.com,
	michael.roth@amd.com, ashish.kalra@amd.com
Subject: Re: [Patch v2 1/4] KVM: SVM: Re-queue events that were never injected
Date: Fri, 9 Oct 2026 14:21:44 -0500	[thread overview]
Message-ID: <0b99f2f4-14ef-462f-9cac-2a091ed355d0@amd.com> (raw)
In-Reply-To: <06cc309d12d59aed6d4b36688d8ef37fe8b089ea.1791302373.git.prsampat@amd.com>

On 10/6/26 11:54, Pratik R. Sampat wrote:
> When injecting an event into the guest via the event_inj field, a
> non-zero event_inj value on #VMEXIT means that the hardware was not able
> to inject the event into the guest.  This used to only occur for the
> VMEXIT_INVALID intercept code, which was a fatal error and resulted in
> the guest being torn down.
> 
> Enhanced SMT Protection (ESMTP) invalidates that assumption.  When ESMTP
> is enabled, VMRUN doesn't enter guest mode immediately; it stalls at a
> synchronization point until every sibling thread is either idle or has
> executed VMRUN for a legal sibling vCPU.  If an ESMTP timeout / illegal
> sibling exit / interrupt arrives while VMRUN is stalled, VMRUN can now
> terminate with the corresponding #VMEXIT intercept code, without
> entering guest mode, and thus without injecting the event.
> 
> For example, on a 2-way SMT core running vCPU0 on thread 0 and vCPU1 on
> thread 1:
>   Thread 0 (vCPU0)                         Thread 1 (vCPU1)
>   ----------------                         ----------------
>   Interrupt A injected to the guest             in host
>   VMRUN                                           |
>     |                                             |
>     v                                             |
>   Stall waiting for sibling                       |
>     |                                             |
>     |  host INTR arrives on thread 0              |
>     |  while it waits                             |
>     |                                             |
>     v                                             |
>   #VMEXIT                                         v
>   Interrupt B injected to the guest              idle
>   VMRUN
> 
> In this case injecting interrupt B clobbers the last event. Interrupt A
> is never delivered and lost forever.
> 
> Therefore, in preparation for ESMTP support, accommodate for potentially
> lost interrupts by detecting an undelivered injected event and
> re-queuing it so as not to lose the event.
> 
> svm_cancel_injection() already recovers a staged event this way and
> becomes redundant, so fold it in.
> 
> Signed-off-by: Pratik R. Sampat <prsampat@amd.com>

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>

> ---
> v1..v2: No changes
> ---
>  arch/x86/kvm/svm/svm.c | 33 +++++++++++++++++++++++++++++----
>  1 file changed, 29 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 7d59d301e1e5..5d15c706e43b 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -4340,6 +4340,7 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
>  	struct vcpu_svm *svm = to_svm(vcpu);
>  	u8 vector;
>  	int type;
> +	struct vmcb_control_area *control = &svm->vmcb->control;
>  	u32 exitintinfo = svm->vmcb->control.exit_int_info;
>  	bool nmi_l1_to_l2 = svm->nmi_l1_to_l2;
>  	bool soft_int_injected = svm->soft_int_injected;
> @@ -4347,6 +4348,27 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
>  	svm->nmi_l1_to_l2 = false;
>  	svm->soft_int_injected = false;
>  
> +	/*
> +	 * Hardware clears EVENTINJ field when it injects an event.
> +	 * A non-empty EVENTINJ on #VMEXIT means the vCPU never entered guest
> +	 * mode, and thus that the event was never delivered.  Migrate the event
> +	 * to EXITINTINFO so that it's requeued instead of being dropped.
> +	 *
> +	 * An undelivered event doesn't imply a fatal VMEXIT_INVALID. With
> +	 * Enhanced SMT Protection, VMRUN may exit with an ordinary #VMEXIT
> +	 * without having injected that event into the guest.
> +	 *
> +	 * Clobbering EXITINTINFO is safe precisely because the vCPU never
> +	 * entered guest mode.
> +	 */
> +	if (control->event_inj) {
> +		control->exit_int_info = control->event_inj;
> +		control->exit_int_info_err = control->event_inj_err;
> +		control->event_inj = 0;
> +
> +		exitintinfo = control->exit_int_info;
> +	}
> +
>  	/*
>  	 * If we've made progress since setting awaiting_iret_completion, we've
>  	 * executed an IRET and can allow NMI injection.
> @@ -4410,11 +4432,14 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
>  static void svm_cancel_injection(struct kvm_vcpu *vcpu)
>  {
>  	struct vcpu_svm *svm = to_svm(vcpu);
> -	struct vmcb_control_area *control = &svm->vmcb->control;
>  
> -	control->exit_int_info = control->event_inj;
> -	control->exit_int_info_err = control->event_inj_err;
> -	control->event_inj = 0;
> +	/*
> +	 * EXITINTINFO is stale as it holds the event from the previous #VMEXIT
> +	 * (or from the last time the current VMCB was run).  Invalidate it so
> +	 * that svm_complete_interrupts() requeues if and only if KVM staged an
> +	 * event in EVENTINJ.
> +	 */
> +	svm->vmcb->control.exit_int_info = 0;
>  	svm_complete_interrupts(vcpu);
>  }
>  


  parent reply	other threads:[~2026-10-09 19:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-06 16:54 [Patch v2 0/4] Introduce Enhanced SMT Protection for SEV-SNP Pratik R. Sampat
2026-10-06 16:54 ` [Patch v2 1/4] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat
2026-10-06 17:12   ` sashiko-bot
2026-10-09 19:21   ` Tom Lendacky [this message]
2026-10-06 16:54 ` [Patch v2 2/4] KVM: SVM: Add host support for Enhanced SMT Protection Pratik R. Sampat
2026-10-06 17:08   ` sashiko-bot
2026-10-09 19:05   ` Tom Lendacky
2026-10-06 16:55 ` [Patch v2 3/4] x86/sev: Add guest " Pratik R. Sampat
2026-10-06 17:03   ` sashiko-bot
2026-10-09 19:18   ` Tom Lendacky
2026-10-06 16:55 ` [Patch v2 4/4] x86/hyperv: " Pratik R. Sampat

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=0b99f2f4-14ef-462f-9cac-2a091ed355d0@amd.com \
    --to=thomas.lendacky@amd.com \
    --cc=ashish.kalra@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=kim.phillips@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=mingo@redhat.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=prsampat@amd.com \
    --cc=seanjc@google.com \
    --cc=tglx@kernel.org \
    --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