Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maxim Levitsky" <mlevitsk@redhat.com>, kvm@vger.kernel.org
Subject: Re: [PATCH v4 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts
Date: Wed, 25 Nov 2026 05:52:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-232632@kernel.org> (raw)
In-Reply-To: <20261125052139.362046-6-mlevitsk@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] KVM: nVMX: delayed KVM_NESTED_PI_ACTIVE misdelivers PI to
  halted/restored L2
- [High] KVM: nVMX: gating KVM_NESTED_PI_ACTIVE on pi_desc breaks
  deferred error exit
- [Medium] KVM: nVMX: __nested_vmx_vmexit() processes nested PI on
  failed VM-Entry
--

commit aef7be5a3f3ba4f7dee7a4841c7b946e8a11d349
Author: Maxim Levitsky <mlevitsk@redhat.com>

KVM: x86: nVMX: fix races in delivery of nested posted interrupts

This commit serializes nested posted interrupt delivery against nested VM
exit using atomic operations on vcpu->arch.nested_guest_state and delays
enabling nested posted interrupts until vmcs12 pages are mapped.

> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index eb33528619c2f..a5ef9a15e7e9c 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -3440,7 +3440,18 @@ static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
>  }
>  #endif
>  
> -static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
> +static void nested_vmx_enable_posted_interrupts(struct kvm_vcpu *vcpu)
> +{
> +	struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
> +
> +	WARN_ON_ONCE(vmcs12->posted_intr_nv & ~KVM_NESTED_PI_VECTOR_MASK);
> +
> +	atomic_or(KVM_NESTED_PI_ACTIVE |
> +		 (vmcs12->posted_intr_nv & KVM_NESTED_PI_VECTOR_MASK),
> +		  &vcpu->arch.nested_guest_state);
> +}
> +
> +static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, bool from_vmentry)
>  {
>  	struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
>  	struct vcpu_vmx *vmx = to_vmx(vcpu);
> @@ -3505,6 +3516,9 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
>  				offset_in_page(vmcs12->posted_intr_desc_addr));
>  			vmcs_write64(POSTED_INTR_DESC_ADDR,
>  				     pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
> +
> +			if (!from_vmentry)
> +				nested_vmx_enable_posted_interrupts(vcpu);
>  		} else {
>  			/*
>  			 * Defer the KVM_INTERNAL_EXIT until KVM tries to
> @@ -3540,7 +3554,7 @@ static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
>  	}
>  #endif
>  
> -	if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
> +	if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu, false))
>  		return false;
>  
>  	return true;
> @@ -3671,7 +3685,7 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
>  	prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12);
>  
>  	if (from_vmentry) {
> -		if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
> +		if (unlikely(!nested_get_vmcs12_pages(vcpu, true))) {
>  			vmx_switch_vmcs(vcpu, &vmx->vmcs01);
>  			return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
>  		}
> @@ -3701,6 +3715,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
>  			vmcs12->exit_qualification = failed_index;
>  			goto vmentry_fail_vmexit_guest_mode;
>  		}
> +
> +		if (nested_cpu_has_posted_intr(vmcs12) && vmx->nested.pi_desc)
> +			nested_vmx_enable_posted_interrupts(vcpu);

[Severity: High]
Does gating nested_vmx_enable_posted_interrupts() on mapping
vmx->nested.pi_desc break the deferred KVM_EXIT_INTERNAL_ERROR when
posted_intr_desc_addr cannot be mapped?

When kvm_vcpu_map() fails in nested_get_vmcs12_pages(), KVM sets
vmx->nested.pi_desc = NULL to defer the internal error exit until
__vmx_complete_nested_posted_interrupt() tries to access the descriptor.
Because KVM_NESTED_PI_ACTIVE is now left unset when pi_desc is NULL,
neither vmx_deliver_nested_posted_interrupt() nor
vmx_check_nested_events() recognizes vmcs12->posted_intr_nv as L2's
posted interrupt notification vector:

arch/x86/kvm/vmx/nested.c:vmx_check_nested_events() {
    ...
		if ((nested_state & KVM_NESTED_PI_ACTIVE) &&
			((nested_state & KVM_NESTED_PI_VECTOR_MASK) == irq)) {
			vmx_complete_nested_posted_interrupt(vcpu);
			goto no_vmexit;
		}
		nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT,
				  INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq, 0);
    ...
}

Instead of setting KVM_NESTED_PI_PENDING and exiting to userspace with
KVM_EXIT_INTERNAL_ERROR via __vmx_complete_nested_posted_interrupt(), KVM
injects a bogus EXIT_REASON_EXTERNAL_INTERRUPT VM-Exit into L1, and the
!vmx->nested.pi_desc check in __vmx_complete_nested_posted_interrupt()
becomes unreachable.

>  	} else {
>  		/*
>  		 * The MMU is not initialized to point at the right entities yet and
> @@ -3712,9 +3729,6 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
>  		kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
>  	}
>  
> -	if (nested_cpu_has_posted_intr(vmcs12))
> -		atomic_or(KVM_NESTED_PI_ACTIVE | vmcs12->posted_intr_nv,
> -			  &vcpu->arch.nested_guest_state);

[Severity: High]
Does deferring nested_vmx_enable_posted_interrupts() until
KVM_REQ_GET_NESTED_STATE_PAGES misdeliver nested posted interrupts when L2
is restored in KVM_MP_STATE_HALTED?

After nested_vmx_enter_non_root_mode(vcpu, false) during
KVM_SET_NESTED_STATE (or vmx_leave_smm()), a halted L2 vCPU enters
vcpu_block() rather than vcpu_enter_guest(), so
KVM_REQ_GET_NESTED_STATE_PAGES is not serviced while blocked.  When
another vCPU sends vmcs12->posted_intr_nv to wake L2,
vmx_deliver_nested_posted_interrupt() sees KVM_NESTED_PI_ACTIVE == 0 and
queues the vector into L1's LAPIC IRR.  vcpu_block() then runs
kvm_check_nested_events() -> vmx_check_nested_events() while
KVM_NESTED_PI_ACTIVE is still unset, failing to morph the notification
vector and instead reflecting a spurious EXIT_REASON_EXTERNAL_INTERRUPT
VM-Exit to L1.

> @@ -4025,22 +4039,15 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
>  	}
>  }
>  
> -static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
> +static int __vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
>  {
>  	struct vcpu_vmx *vmx = to_vmx(vcpu);
>  	int max_irr;
>  	void *vapic_page;
>  	u16 status;
>  
> -	int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
> -
> -	if (!(nested_state & KVM_NESTED_PI_PENDING))
> -		return 0;
> -
>  	if (!vmx->nested.pi_desc)
> -		goto mmio_needed;
> -
> -	atomic_andnot(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
> +		return -ENXIO;
>  
>  	/*
>  	 * Don't test the value of PID.ON.
> @@ -4057,7 +4064,7 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
>  	if (max_irr > 0) {
>  		vapic_page = vmx->nested.virtual_apic_map.hva;
>  		if (!vapic_page)
> -			goto mmio_needed;
> +			return -ENXIO;
>  
>  		__kvm_apic_update_irr(vmx->nested.pi_desc->pir,
>  			vapic_page, &max_irr);

[ ... ]

> @@ -5121,7 +5141,10 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
>  	if (enable_ept && is_pae_paging(vcpu))
>  		vmx_ept_load_pdptrs(vcpu);
>  
> -	leave_guest_mode(vcpu);
> +	old_nested_state = leave_guest_mode(vcpu);
> +
> +	if (old_nested_state & KVM_NESTED_PI_PENDING)
> +		__vmx_complete_nested_posted_interrupt(vcpu);

[Severity: Medium]
Can calling __vmx_complete_nested_posted_interrupt() unconditionally in
__nested_vmx_vmexit() lose posted interrupts when vmx->fail is set or
virtual_apic_map is unmapped?

When a hardware VM-Entry of vmcs02 fails (vmx->fail == 1),
__nested_vmx_vmexit() skips sync_vmcs02_to_vmcs12().  Running
__vmx_complete_nested_posted_interrupt() before checking !vmx->fail
clears PID.ON and harvests PID.PIR into the virtual APIC page, but writes
the updated GUEST_INTR_STATUS.RVI to vmcs02 where it is discarded without
being synced to vmcs12.

Also, if virtual_apic_page_addr could not be mapped (which forces a
vmcs02 VM-Entry failure while leaving pi_desc mapped),
__vmx_complete_nested_posted_interrupt() clears PID.ON in guest memory
before checking !vapic_page and returns -ENXIO without harvesting PIR or
marking pi_desc_map dirty, while __nested_vmx_vmexit() ignores the return
value.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125052139.362046-1-mlevitsk@redhat.com?part=5

      reply	other threads:[~2026-11-25  5:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  5:21 [PATCH v4 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
2026-11-25  5:21 ` [PATCH v4 1/5] KVM: nVMX: don't check PID.ON when processing nested posted interrupts Maxim Levitsky
2026-11-25  5:21 ` [PATCH v4 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_state and smm_mode Maxim Levitsky
2026-11-25  5:21 ` [PATCH v4 3/5] KVM: x86: convert nested_guest_state into an atomic variable Maxim Levitsky
2026-11-25  5:21 ` [PATCH v4 4/5] KVM: x86: nVMX: move nested APICv state to nested_guest_state Maxim Levitsky
2026-11-25  5:21 ` [PATCH v4 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky
2026-11-25  5:52   ` 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-232632@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=mlevitsk@redhat.com \
    --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