Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Manuel Andreas" <manuel.andreas@tum.de>, kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
Date: Tue, 24 Nov 2026 13:56:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-231274@kernel.org> (raw)
In-Reply-To: <20261124133704.5563-1-manuel.andreas@tum.de>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: nVMX: premature GUEST_INTR_STATE_NMI clear in
  prepare_vmcs02_early()
--

commit 0a8a662b2bf25c4df8703f3943666ec724f2d386
Author: Manuel Andreas <manuel.andreas@tum.de>

KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs

This commit clears GUEST_INTR_STATE_NMI in vmcs02 when L1 injects an NMI
while NMI-blocked without virtual NMIs enabled in vmcs12, avoiding a
hardware VM-Entry failure when KVM enables virtual NMIs in vmcs02.

> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index b252168627408..0adb671ae8c2f 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -2586,14 +2586,30 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
>  	 * Interrupt/Exception Fields
>  	 */
>  	if (vmx->vcpu.arch.nested_run_pending) {
> -		vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
> -			     vmcs12->vm_entry_intr_info_field);
> +		u32 intr_info = vmcs12->vm_entry_intr_info_field;
> +		u32 interruptibility = vmcs12->guest_interruptibility_info;
> +
> +		/*
> +		 * vmcs02 uses virtual NMIs if KVM does (i.e. enable_vnmi=true),
> +		 * regardless of vmcs12, in which case the CPU rejects VM-Enter
> +		 * if an NMI is being injected while blocking by NMI is set.
> +		 * Architecturally, there is no such restriction for L1 if
> +		 * vmcs12 doesn't use virtual NMIs, so clear blocking by NMI in
> +		 * vmcs02 if L1 is injecting an NMI.  Delivering the NMI blocks
> +		 * NMIs, i.e. L2 still ends up with blocking by NMI set, the same
> +		 * as it would on bare metal.  Note, nmi_known_unmasked is derived
> +		 * from vmcs12's original value and so stays false, as required.
> +		 */
> +		if ((pin_controls_get(vmx) & PIN_BASED_VIRTUAL_NMIS) &&
> +		    !nested_cpu_has_virtual_nmis(vmcs12) && is_nmi(intr_info))
> +			interruptibility &= ~GUEST_INTR_STATE_NMI;
> +
> +		vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
>  		vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
>  			     vmcs12->vm_entry_exception_error_code);
>  		vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
>  			     vmcs12->vm_entry_instruction_len);
> -		vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
> -			     vmcs12->guest_interruptibility_info);
> +		vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
>  		vmx->loaded_vmcs->nmi_known_unmasked =
>  			!(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
>  	} else {

[Severity: Medium]
Can clearing GUEST_INTR_STATE_NMI in vmcs02 during prepare_vmcs02_early()
corrupt vmx->loaded_vmcs->nmi_known_unmasked and
vmcs12->guest_interruptibility_info before hardware VM-Entry delivers the
NMI?

In vmx_get_nmi_mask(), nmi_known_unmasked == false is treated as a cache
miss that reads GUEST_INTERRUPTIBILITY_INFO from the active VMCS and
latches nmi_known_unmasked:

arch/x86/kvm/vmx/vmx.c:vmx_get_nmi_mask() {
	if (vmx->loaded_vmcs->nmi_known_unmasked)
		return false;
	masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
	vmx->loaded_vmcs->nmi_known_unmasked = !masked;
	return masked;
}

If vmx_get_nmi_mask() runs between prepare_vmcs02_early() and
vmx_vcpu_run() (for example via process_nmi() when servicing KVM_REQ_NMI
in vcpu_enter_guest(), or via KVM_GET_VCPU_EVENTS), it reads the cleared
GUEST_INTR_STATE_NMI bit from vmcs02, returns false, and flips
nmi_known_unmasked to true.  After hardware VM-Entry delivers the NMI and
re-sets GUEST_INTR_STATE_NMI, nmi_known_unmasked stays true, causing
subsequent vmx_get_nmi_mask() and vmx_recover_nmi_blocking() calls on L2
VM-Exits to treat NMIs as unmasked.

In addition, if sync_vmcs02_to_vmcs12() runs before the NMI is delivered
(such as on a hardware guest-state VM-Entry failure in
__nested_vmx_vmexit() or during vmx_get_nested_state()), it overwrites
vmcs12->guest_interruptibility_info with the cleared bit from vmcs02, and
if a VM-Exit occurs while vectoring the injected NMI,
__vmx_complete_interrupts() clears GUEST_INTR_STATE_NMI via
vmx_set_nmi_mask(vcpu, false), losing L1's pre-existing NMI blocking.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124133704.5563-1-manuel.andreas@tum.de?part=1

      reply	other threads:[~2026-11-24 13:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:37 [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs Manuel Andreas
2026-11-24 13:56 ` sashiko-bot [this message]

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=sashiko-outbox-231274@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=manuel.andreas@tum.de \
    --cc=sashiko-reviews@lists.linux.dev \
    /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