* [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
@ 2026-11-24 13:37 Manuel Andreas
2026-11-24 13:56 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Manuel Andreas @ 2026-11-24 13:37 UTC (permalink / raw)
To: kvm; +Cc: seanjc, pbonzini, Manuel Andreas
The SDM requires blocking by NMI to be clear when VM-Entry injects an NMI
only if the "virtual NMIs" VM-execution control is 1. If virtual NMIs are
disabled, injecting an NMI while blocking by NMI is set is legal.
KVM enables virtual NMIs in vmcs02 regardless of vmcs12 (and vmcs01 always
uses virtual NMIs if enable_vnmi=true), but copies vmcs12's guest
interruptibility state and VM-Entry interruption information verbatim.
If L1 doesn't use virtual NMIs and injects an NMI while blocking by NMI
is set, the CPU thus rejects vmcs02 and KVM reflects a bogus
"VM-entry failure due to invalid guest state" to L1.
Clear blocking by NMI in vmcs02 in that case. Delivering the injected NMI
blocks NMIs, i.e. L2 still runs with blocking by NMI set, which is the
state L2 would be in on bare metal. Leave nmi_known_unmasked alone,
it's derived from vmcs12's original value and so remains false.
Signed-off-by: Manuel Andreas <manuel.andreas@tum.de>
---
arch/x86/kvm/vmx/nested.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 1a98f28651f7..7229f4b98eee 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2563,14 +2563,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 {
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24 13:56 UTC (permalink / raw)
To: Manuel Andreas, kvm
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 13:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox