Re: [PATCH v3 1/3] KVM: nVMX: Fill VM-Exit instruction length before saving pending event

From: Jim Mattson

Date: Mon Nov 16 2026 - 19:49:06 EST


On Mon, Nov 16, 2026 at 2:30 PM Mahmoud Abumandour
<ma.mandourr@xxxxxxxxx> wrote:
>
> Fill vmcs12's VM-Exit instruction length before calling
> vmcs12_save_pending_event(), so that the length it saves for a software
> exception isn't overwritten. Commit c3634d25fbee ("KVM: nVMX: Leave most
> VM-Exit info fields unmodified on failed VM-Entry") moved the assignment
> after the call.
>
> L1 sees no difference since both lengths are read from vmcs02's VM-Exit
> instruction length.
>
> Link: https://lore.kernel.org/all/sashiko-outbox-179791@xxxxxxxxxx

Sashiko is wrong. Neither of the scenarios it cited in v1 holds:

1. After vmx_cancel_injection(): if nested_run_pending is set,
vmx_check_nested_events() blocks all nested VM-Exits (and
__nested_vmx_vmexit() warns if called with nested_run_pending set).
Once vmcs02 actually runs and exits, hardware copies
VM_ENTRY_INSTRUCTION_LEN into VM_EXIT_INSTRUCTION_LEN if a VM-Exit
occurs during delivery of an injected soft event (SDM vol. 3,
section 30.2.5). If nested_run_pending is clear,
VM_ENTRY_INSTRUCTION_LEN came from event_exit_inst_len, which
already came from vmcs02's VM_EXIT_INSTRUCTION_LEN.

2. After KVM_SET_NESTED_STATE + KVM_SET_VCPU_EVENTS:
kvm_vcpu_ioctl_x86_set_vcpu_events() never updates
vcpu->arch.event_exit_inst_len (struct kvm_vcpu_events has no
instruction length field), so on a reused vCPU event_exit_inst_len
is just stale junk.

Moreover, L1 *can* see a difference, and this patch introduces a subtle
regression for SGX enclave-mode #BP. Per the SDM, volume 3, sections
30.2.1, 30.2.4, and 30.2.5:

- A #BP that occurs in enclave mode is reported as a hardware
exception (INTR_TYPE_HARD_EXCEPTION).
- A VM-Exit due to an exception encountered during delivery of an
enclave-mode exception still sets bit 27 (enclave mode) of the exit
reason.
- "If the VM exit occurred in enclave mode, this field [VM-exit
instruction length] is cleared (none of the previous items apply)."

In __vmx_complete_interrupts(), INTR_TYPE_HARD_EXCEPTION does not update
vcpu->arch.event_exit_inst_len, so it retains whatever stale length was
saved on an earlier soft event. Because vmcs12_save_pending_event()
checks kvm_exception_is_soft(nr) unconditionally, it misclassifies an
enclave-mode #BP as a software exception (a pre-existing bug, outside
the scope of this series). Before this patch, prepare_vmcs12() at least
overwrote vmcs12->vm_exit_instruction_len with exit_insn_len (0). With
this patch, vmcs12_save_pending_event() clobbers 0 with stale
vcpu->arch.event_exit_inst_len.

AFAICT, there is no valid path to prepare_vmcs12() where
vcpu->arch.event_exit_inst_len is better than exit_insn_len. Can't we
just drop this patch and delete both vcpu->arch.event_exit_inst_len
assignments in vmcs12_save_pending_event() in patch 2, letting
prepare_vmcs12() be the sole writer of vmcs12->vm_exit_instruction_len?